2 // class.cs: Class and Struct handlers
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
19 public class TypeContainer : DeclSpace {
20 protected int mod_flags;
22 // Holds a list of classes and structures
25 // Holds the list of properties
28 // Holds the list of enumerations
31 // Holds the list of delegates
34 // Holds the list of constructors
35 ArrayList constructors;
37 // Holds the list of fields
40 // Holds a list of fields that have initializers
41 ArrayList initialized_fields;
43 // Holds a list of static fields that have initializers
44 ArrayList initialized_static_fields;
46 // Holds the list of constants
61 // Holds the operators
64 // Maps MethodBuilders to Methods
65 static Hashtable method_builders_to_parameters;
68 // Pointers to the default constructor and the default static constructor
70 Constructor default_constructor;
71 Constructor default_static_constructor;
74 // Whether we have seen a static constructor for this class or not
76 bool have_static_constructor = false;
79 // This is the namespace in which this typecontainer
80 // was declared. We use this to resolve names.
82 Namespace my_namespace;
85 // This one is computed after we can distinguish interfaces
86 // from classes from the arraylist `type_bases'
88 string base_class_name;
94 // This behaves like a property ;-)
96 public readonly RootContext RootContext;
98 // Attributes for this type
99 protected Attributes attributes;
101 public TypeContainer (RootContext rc, TypeContainer parent, string name, Location l)
105 types = new ArrayList ();
106 this.parent = parent;
114 base_class_name = null;
116 //Console.WriteLine ("New class " + name + " inside " + n);
119 public AdditionResult AddConstant (Constant constant)
122 string name = constant.Name;
124 if ((res = IsValid (name)) != AdditionResult.Success)
127 if (constants == null)
128 constants = new ArrayList ();
130 constants.Add (constant);
131 DefineName (name, constant);
133 return AdditionResult.Success;
136 public AdditionResult AddEnum (CIR.Enum e)
139 string name = e.Name;
141 if ((res = IsValid (name)) != AdditionResult.Success)
145 enums = new ArrayList ();
148 DefineName (name, e);
150 return AdditionResult.Success;
153 public AdditionResult AddClass (Class c)
156 string name = c.Name;
159 if ((res = IsValid (name)) != AdditionResult.Success)
162 DefineName (name, c);
165 return AdditionResult.Success;
168 public AdditionResult AddStruct (Struct s)
171 string name = s.Name;
173 if ((res = IsValid (name)) != AdditionResult.Success)
176 DefineName (name, s);
179 return AdditionResult.Success;
182 public AdditionResult AddDelegate (Delegate d)
185 string name = d.Name;
187 if ((res = IsValid (name)) != AdditionResult.Success)
190 if (delegates == null)
191 delegates = new ArrayList ();
193 DefineName (name, d);
196 return AdditionResult.Success;
199 public AdditionResult AddMethod (Method method)
201 string name = method.Name;
202 Object value = defined_names [name];
204 if (value != null && (!(value is Method)))
205 return AdditionResult.NameExists;
208 methods = new ArrayList ();
210 methods.Add (method);
212 DefineName (name, method);
214 return AdditionResult.Success;
217 public AdditionResult AddConstructor (Constructor c)
219 if (c.Name != Basename)
220 return AdditionResult.NotAConstructor;
222 if (constructors == null)
223 constructors = new ArrayList ();
225 constructors.Add (c);
227 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
230 have_static_constructor = true;
232 if (c.IsDefault ()) {
234 default_static_constructor = c;
236 default_constructor = c;
239 return AdditionResult.Success;
242 public AdditionResult AddInterface (Interface iface)
245 string name = iface.Name;
247 if ((res = IsValid (name)) != AdditionResult.Success)
250 if (interfaces == null)
251 interfaces = new ArrayList ();
252 interfaces.Add (iface);
253 DefineName (name, iface);
255 return AdditionResult.Success;
258 public AdditionResult AddField (Field field)
261 string name = field.Name;
263 if ((res = IsValid (name)) != AdditionResult.Success)
267 fields = new ArrayList ();
270 if (field.Initializer != null){
271 if ((field.ModFlags & Modifiers.STATIC) != 0){
272 if (initialized_static_fields == null)
273 initialized_static_fields = new ArrayList ();
275 initialized_static_fields.Add (field);
278 // We have not seen a static constructor,
279 // but we will provide static initialization of fields
281 have_static_constructor = true;
283 if (initialized_fields == null)
284 initialized_fields = new ArrayList ();
286 initialized_fields.Add (field);
290 DefineName (name, field);
291 return AdditionResult.Success;
294 public AdditionResult AddProperty (Property prop)
297 string name = prop.Name;
299 if ((res = IsValid (name)) != AdditionResult.Success)
302 if (properties == null)
303 properties = new ArrayList ();
305 properties.Add (prop);
306 DefineName (name, prop);
308 return AdditionResult.Success;
311 public AdditionResult AddEvent (Event e)
314 string name = e.Name;
316 if ((res = IsValid (name)) != AdditionResult.Success)
320 events = new ArrayList ();
323 DefineName (name, e);
325 return AdditionResult.Success;
328 public AdditionResult AddIndexer (Indexer i)
330 if (indexers == null)
331 indexers = new ArrayList ();
335 return AdditionResult.Success;
338 public AdditionResult AddOperator (Operator op)
340 if (operators == null)
341 operators = new ArrayList ();
345 return AdditionResult.Success;
348 public TypeContainer Parent {
354 public ArrayList Types {
360 public ArrayList Methods {
366 public ArrayList Constants {
372 public ArrayList Interfaces {
378 public int ModFlags {
386 return base_class_name;
390 public ArrayList Bases {
400 public ArrayList Fields {
406 public ArrayList Constructors {
412 public ArrayList Properties {
418 public ArrayList Events {
424 public ArrayList Enums {
430 public ArrayList Indexers {
436 public ArrayList Operators {
442 public ArrayList Delegates {
448 public Attributes OptAttributes {
454 public Namespace Namespace {
460 my_namespace = value;
465 // root_types contains all the types. All TopLevel types
466 // hence have a parent that points to `root_types', that is
467 // why there is a non-obvious test down here.
469 public bool IsTopLevel {
472 if (parent.Parent == null)
479 public bool HaveStaticConstructor {
481 return have_static_constructor;
485 public virtual TypeAttributes TypeAttr {
487 return Modifiers.TypeAttr (mod_flags, this);
492 // Emits the instance field initializers
494 public bool EmitFieldInitializers (EmitContext ec, bool is_static)
497 ILGenerator ig = ec.ig;
500 fields = initialized_static_fields;
502 fields = initialized_fields;
507 foreach (Field f in fields){
508 Object init = f.Initializer;
510 if (init is Expression){
511 Expression e = (Expression) init;
518 ig.Emit (OpCodes.Ldarg_0);
523 ig.Emit (OpCodes.Stsfld, f.FieldBuilder);
525 ig.Emit (OpCodes.Stfld, f.FieldBuilder);
533 // Defines the default constructors
535 void DefineDefaultConstructor (bool is_static)
540 c = new Constructor (Basename, new Parameters (null, null),
541 new ConstructorBaseInitializer (null, new Location (-1)),
546 c.Block = new Block (null);
549 mods = Modifiers.STATIC;
555 public void ReportStructInitializedInstanceError ()
557 string n = TypeBuilder.FullName;
559 foreach (Field f in initialized_fields){
562 "`" + n + "." + f.Name + "': can not have " +
563 "instance field initializers in structs");
567 public void RegisterRequiredImplementations ()
569 Type [] ifaces = TypeBuilder.GetInterfaces ();
570 Type b = TypeBuilder.BaseType;
573 SetRequiredInterfaces (ifaces);
576 MemberInfo [] abstract_methods;
578 abstract_methods = FindMembers (
579 TypeBuilder.BaseType,
580 MemberTypes.Method, BindingFlags.Public,
581 abstract_method_filter, null);
583 if (abstract_methods != null){
584 MethodInfo [] mi = new MethodInfo [abstract_methods.Length];
586 abstract_methods.CopyTo (mi, 0);
587 RequireMethods (mi, b);
593 static object MakeKey (MethodBase mb)
595 if (mb is MethodBuilder || mb is ConstructorBuilder)
596 return mb.ReflectedType.FullName + ":" + mb;
598 return mb.MethodHandle.ToString ();
602 public static string MakeFQN (string nsn, string name)
604 string prefix = (nsn == "" ? "" : nsn + ".");
606 return prefix + name;
609 Type LookupInterfaceOrClass (object builder, string ns, string name, bool is_class, out bool error)
611 TypeContainer parent;
615 name = MakeFQN (ns, name);
617 t = RootContext.TypeManager.LookupType (name);
622 parent = (Class) RootContext.Tree.Classes [name];
624 parent = (Struct) RootContext.Tree.Structs [name];
628 t = parent.DefineType (builder);
630 Report.Error (146, "Class definition is circular: `"+name+"'");
642 // returns the type for an interface or a class, this will recursively
643 // try to define the types that it depends on.
645 Type GetInterfaceOrClass (object builder, string name, bool is_class)
651 // Attempt to lookup the class on our namespace
653 t = LookupInterfaceOrClass (builder, Namespace.Name, name, is_class, out error);
661 // Attempt to lookup the class on any of the `using'
665 for (Namespace ns = Namespace; ns != null; ns = ns.Parent){
666 ArrayList using_list = ns.UsingTable;
668 if (using_list == null)
671 foreach (string n in using_list){
672 t = LookupInterfaceOrClass (builder, n, name, is_class, out error);
681 Report.Error (246, "Can not find type `"+name+"'");
686 // This function computes the Base class and also the
687 // list of interfaces that the class or struct @c implements.
689 // The return value is an array (might be null) of
690 // interfaces implemented (as Types).
692 // The @parent argument is set to the parent object or null
693 // if this is `System.Object'.
695 Type [] GetClassBases (object builder, bool is_class, out Type parent, out bool error)
697 ArrayList bases = Bases;
706 parent = TypeManager.value_type;
710 if (RootContext.StdLib)
711 parent = TypeManager.object_type;
712 else if (Name != "System.Object")
713 parent = TypeManager.object_type;
716 // If we are compiling our runtime,
717 // and we are defining ValueType, then our
718 // parent is `System.Object'.
720 if (!RootContext.StdLib && Name == "System.ValueType")
721 parent = TypeManager.object_type;
728 // Bases should be null if there are no bases at all
733 string name = (string) bases [0];
734 Type first = GetInterfaceOrClass (builder, name, is_class);
745 parent = TypeManager.object_type;
752 Type [] ifaces = new Type [count-start];
754 for (i = start, j = 0; i < count; i++, j++){
755 string name = (string) bases [i];
756 Type t = GetInterfaceOrClass (builder, name, is_class);
763 if (is_class == false && !t.IsInterface){
764 Report.Error (527, "In Struct `" + Name + "', type `"+
765 name +"' is not an interface");
774 detail = " (a class can not inherit from a struct)";
776 Report.Error (509, "class `"+ Name +
777 "': Cannot inherit from sealed class `"+
778 bases [i]+"'"+detail);
785 Report.Error (527, "In Class `" + Name + "', type `"+
786 name+"' is not an interface");
799 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
801 public TypeBuilder DefineType (object parent_builder)
818 ifaces = GetClassBases (parent_builder, is_class, out parent, out error);
823 if (parent_builder is ModuleBuilder) {
824 ModuleBuilder builder = (ModuleBuilder) parent_builder;
827 // Structs with no fields need to have a ".size 1"
830 if (!is_class && Fields == null)
831 TypeBuilder = builder.DefineType (Name,
834 PackingSize.Unspecified, 1);
837 // classes or structs with fields
839 TypeBuilder = builder.DefineType (Name,
845 TypeBuilder builder = (TypeBuilder) parent_builder;
848 // Structs with no fields need to have a ".size 1"
851 if (!is_class && Fields == null)
852 TypeBuilder = builder.DefineNestedType (Name,
855 PackingSize.Unspecified);
858 // classes or structs with fields
860 TypeBuilder = builder.DefineNestedType (Name,
866 RootContext.TypeManager.AddUserType (Name, TypeBuilder, this);
869 foreach (TypeContainer tc in Types)
870 tc.DefineType (TypeBuilder);
873 if (Delegates != null) {
874 foreach (Delegate d in Delegates)
875 d.DefineDelegate (TypeBuilder);
883 // Populates our TypeBuilder with fields and methods
885 public void Populate ()
887 if (Constants != null){
888 foreach (Constant c in Constants)
889 c.EmitConstant (RootContext, this);
893 foreach (Field f in Fields)
897 if (this is Class && constructors == null){
898 if (default_constructor == null)
899 DefineDefaultConstructor (false);
901 if (initialized_static_fields != null &&
902 default_static_constructor == null)
903 DefineDefaultConstructor (true);
908 // Structs can not have initialized instance
911 if (initialized_static_fields != null &&
912 default_static_constructor == null)
913 DefineDefaultConstructor (true);
915 if (initialized_fields != null)
916 ReportStructInitializedInstanceError ();
919 RegisterRequiredImplementations ();
921 ArrayList remove_list = new ArrayList ();
923 if (constructors != null || methods != null ||
924 properties != null || operators != null){
925 if (method_builders_to_parameters == null)
926 method_builders_to_parameters = new Hashtable ();
929 if (constructors != null){
930 foreach (Constructor c in constructors){
931 MethodBase builder = c.Define (this);
936 InternalParameters ip = c.ParameterInfo;
938 method_builders_to_parameters.Add (MakeKey (builder), ip);
942 foreach (object o in remove_list)
943 constructors.Remove (o);
945 remove_list.Clear ();
948 if (Methods != null){
949 foreach (Method m in methods){
950 MethodBase key = m.Define (this);
954 // The following key is not enoug
955 // class x { public void X () {} }
956 // class y : x { public void X () {}}
962 InternalParameters ip = m.ParameterInfo;
963 method_builders_to_parameters.Add (MakeKey (key), ip);
966 foreach (object o in remove_list)
969 remove_list.Clear ();
972 if (Properties != null) {
973 foreach (Property p in Properties)
978 foreach (Enum e in Enums)
982 if (Events != null) {
983 foreach (Event e in Events)
987 if (Indexers != null) {
988 foreach (Indexer i in Indexers)
992 if (Operators != null) {
993 foreach (Operator o in Operators) {
996 InternalParameters ip = o.OperatorMethod.ParameterInfo;
998 method_builders_to_parameters.Add (
999 MakeKey (o.OperatorMethodBuilder), ip);
1003 if (Delegates != null) {
1004 foreach (Delegate d in Delegates)
1008 if (Types != null) {
1009 foreach (TypeContainer tc in Types)
1016 // Since System.Reflection.Emit can not retrieve parameter information
1017 // from methods that are dynamically defined, we have to look those
1018 // up ourselves using this
1020 static public ParameterData LookupParametersByBuilder (MethodBase mb)
1022 return (ParameterData) method_builders_to_parameters [MakeKey (mb)];
1026 // Indexers and properties can register more than one method at once,
1027 // so we need to provide a mechanism for those to register their
1028 // various methods to parameter info mappers.
1030 static public void RegisterParameterForBuilder (MethodBase mb, InternalParameters pi)
1032 method_builders_to_parameters.Add (MakeKey (mb), pi);
1035 public Type LookupType (string name, bool silent)
1037 return RootContext.LookupType (this, name, silent);
1040 public string LookupAlias (string Name)
1043 // Read the comments on `mcs/mcs/TODO' for details
1049 // This function is based by a delegate to the FindMembers routine
1051 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1057 static bool IsAbstractMethod (MemberInfo m, object filter_criteria)
1059 MethodInfo mi = (MethodInfo) m;
1061 return mi.IsAbstract;
1064 // This filter is used by FindMembers, and we just keep
1065 // a global for the filter to `AlwaysAccept'
1067 static MemberFilter accepting_filter;
1070 // This delegate is a MemberFilter used to extract the
1071 // abstact methods from a type.
1073 static MemberFilter abstract_method_filter;
1075 static TypeContainer ()
1077 abstract_method_filter = new MemberFilter (IsAbstractMethod);
1078 accepting_filter = new MemberFilter (AlwaysAccept);
1082 // This method returns the members of this type just like Type.FindMembers would
1083 // Only, we need to use this for types which are _being_ defined because MS'
1084 // implementation can't take care of that.
1086 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf,
1087 MemberFilter filter, object criteria)
1089 ArrayList members = new ArrayList ();
1092 filter = accepting_filter;
1094 if ((mt & MemberTypes.Field) != 0 && Fields != null) {
1095 foreach (Field f in Fields) {
1096 if (filter (f.FieldBuilder, criteria) == true)
1097 members.Add (f.FieldBuilder);
1101 if ((mt & MemberTypes.Method) != 0) {
1102 if (Methods != null){
1103 foreach (Method m in Methods) {
1104 MethodBuilder mb = m.MethodBuilder;
1106 if (filter (mb, criteria) == true)
1111 if (Operators != null){
1112 foreach (Operator o in Operators) {
1113 MethodBuilder ob = o.OperatorMethodBuilder;
1115 if (filter (ob, criteria) == true)
1121 // FIXME : This ain't right because EventBuilder is not a
1122 // MemberInfo. What do we do ?
1124 if ((mt & MemberTypes.Event) != 0 && Events != null) {
1125 //foreach (Event e in Events) {
1126 // if (filter (e.EventBuilder, criteria) == true)
1127 // mi [i++] = e.EventBuilder;
1131 if ((mt & MemberTypes.Property) != 0){
1132 if (Properties != null)
1133 foreach (Property p in Properties) {
1134 if (filter (p.PropertyBuilder, criteria) == true)
1135 members.Add (p.PropertyBuilder);
1138 if (Indexers != null)
1139 foreach (Indexer ix in Indexers){
1140 if (filter (ix.PropertyBuilder, criteria) == true)
1141 members.Add (ix.PropertyBuilder);
1145 if ((mt & MemberTypes.NestedType) != 0 && Types != null) {
1146 foreach (TypeContainer t in Types) {
1147 if (filter (t.TypeBuilder, criteria) == true)
1148 members.Add (t.TypeBuilder);
1152 if ((mt & MemberTypes.Constructor) != 0){
1153 if (Constructors != null){
1154 foreach (Constructor c in Constructors){
1155 ConstructorBuilder cb = c.ConstructorBuilder;
1157 if (filter (cb, criteria) == true)
1164 // Lookup members in parent if requested.
1166 if ((bf & BindingFlags.DeclaredOnly) == 0){
1169 mi = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1171 members.AddRange (mi);
1174 int count = members.Count;
1176 MemberInfo [] mi = new MemberInfo [count];
1177 members.CopyTo (mi);
1184 public static MemberInfo [] FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1185 MemberFilter filter, object criteria)
1187 TypeContainer tc = TypeManager.LookupTypeContainer (t);
1190 return tc.FindMembers (mt, bf, filter, criteria);
1192 return t.FindMembers (mt, bf, filter, criteria);
1196 Hashtable pending_implementations;
1199 // Requires that the methods in `mi' be implemented for this
1202 public void RequireMethods (MethodInfo [] mi, object data)
1204 if (pending_implementations == null)
1205 pending_implementations = new Hashtable ();
1207 foreach (MethodInfo m in mi){
1208 Type [] types = TypeManager.GetArgumentTypes (m);
1210 pending_implementations.Add (
1212 (m.Name, m.ReturnType, types), data);
1217 // Used to set the list of interfaces that this typecontainer
1222 // For each element exposed by the type, we create a MethodSignature
1223 // struct that we will label as `implemented' as we define the various
1226 public void SetRequiredInterfaces (Type [] ifaces)
1228 foreach (Type t in ifaces){
1231 if (t is TypeBuilder){
1232 Interface iface = RootContext.TypeManager.LookupInterface (t);
1234 mi = iface.GetMethods ();
1236 mi = t.GetMethods ();
1238 RequireMethods (mi, t);
1243 // If a method with name `Name', return type `ret_type' and
1244 // arguments `args' implements an interface, this method will
1247 // This will remove the method from the list of "pending" methods
1248 // that are required to be implemented for this class as a side effect.
1251 public bool IsInterfaceMethod (string Name, Type ret_type, Type [] args)
1253 MethodSignature query;
1255 if (pending_implementations == null)
1258 query = new MethodSignature (Name, ret_type, args);
1260 if (pending_implementations.Contains (query)){
1261 pending_implementations.Remove (query);
1269 // Verifies that any pending abstract methods or interface methods
1270 // were implemented.
1272 void VerifyPendingMethods ()
1276 foreach (object m in pending_implementations){
1277 DictionaryEntry de = (DictionaryEntry) m;
1278 Type t = (Type) de.Value;
1281 MethodSignature method = (MethodSignature) de.Key;
1286 "`" + Name + "' does not implement interface member `" +
1287 t.FullName + "." + method.Name + "'");
1291 "`" + Name + "' does not implement inherited abstract " +
1292 "member `" + t.FullName + "." + method.Name + "'");
1297 // Emits the code, this step is performed after all
1298 // the types, enumerations, constructors
1302 if (Constructors != null)
1303 foreach (Constructor c in Constructors)
1306 if (methods != null)
1307 foreach (Method m in methods)
1310 if (operators != null)
1311 foreach (Operator o in operators)
1314 if (properties != null)
1315 foreach (Property p in properties)
1318 if (indexers != null)
1319 foreach (Indexer ix in indexers)
1322 if (pending_implementations != null)
1323 VerifyPendingMethods ();
1326 foreach (TypeContainer tc in types)
1331 public void CloseType ()
1334 TypeBuilder.CreateType ();
1335 } catch (InvalidOperationException e){
1336 Console.WriteLine ("Exception while creating class: " + TypeBuilder.Name);
1337 Console.WriteLine ("Message:" + e.Message);
1340 foreach (TypeContainer tc in Types)
1343 if (Delegates != null)
1344 foreach (Delegate d in Delegates)
1348 string MakeName (string n)
1350 return "`" + Name + "." + n + "'";
1353 public static int CheckMember (string name, MemberInfo mi, int ModFlags)
1359 public class Class : TypeContainer {
1361 // Modifiers allowed in a class declaration
1363 public const int AllowedModifiers =
1366 Modifiers.PROTECTED |
1367 Modifiers.INTERNAL |
1369 Modifiers.ABSTRACT |
1372 public Class (RootContext rc, TypeContainer parent, string name, int mod,
1373 Attributes attrs, Location l)
1374 : base (rc, parent, name, l)
1378 if (parent.Parent == null)
1379 accmods = Modifiers.INTERNAL;
1381 accmods = Modifiers.PRIVATE;
1383 this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
1384 this.attributes = attrs;
1388 // FIXME: How do we deal with the user specifying a different
1391 public override TypeAttributes TypeAttr {
1393 return base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
1398 public class Struct : TypeContainer {
1400 // Modifiers allowed in a struct declaration
1402 public const int AllowedModifiers =
1405 Modifiers.PROTECTED |
1406 Modifiers.INTERNAL |
1409 public Struct (RootContext rc, TypeContainer parent, string name, int mod,
1410 Attributes attrs, Location l)
1411 : base (rc, parent, name, l)
1415 if (parent.Parent == null)
1416 accmods = Modifiers.INTERNAL;
1418 accmods = Modifiers.PRIVATE;
1420 this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
1422 this.mod_flags |= Modifiers.SEALED;
1423 this.attributes = attrs;
1428 // FIXME: Allow the user to specify a different set of attributes
1429 // in some cases (Sealed for example is mandatory for a class,
1430 // but what SequentialLayout can be changed
1432 public override TypeAttributes TypeAttr {
1434 return base.TypeAttr |
1435 TypeAttributes.SequentialLayout |
1436 TypeAttributes.Sealed |
1437 TypeAttributes.BeforeFieldInit;
1442 public class MethodCore {
1443 public readonly Parameters Parameters;
1444 public readonly string Name;
1445 public int ModFlags;
1447 public readonly Location Location;
1450 // Parameters, cached for semantic analysis.
1452 InternalParameters parameter_info;
1454 public MethodCore (string name, Parameters parameters, Location l)
1457 Parameters = parameters;
1462 // Returns the System.Type array for the parameters of this method
1464 Type [] parameter_types;
1465 public Type [] ParameterTypes (TypeContainer parent)
1467 if (Parameters == null)
1470 if (parameter_types == null)
1471 parameter_types = Parameters.GetParameterInfo (parent);
1473 return parameter_types;
1476 public InternalParameters ParameterInfo
1479 return parameter_info;
1483 parameter_info = value;
1487 public Block Block {
1497 public CallingConventions GetCallingConvention (bool is_class)
1499 CallingConventions cc = 0;
1501 cc = Parameters.GetCallingConvention ();
1504 if ((ModFlags & Modifiers.STATIC) == 0)
1505 cc |= CallingConventions.HasThis;
1507 // FIXME: How is `ExplicitThis' used in C#?
1513 public class Method : MethodCore {
1514 public readonly string ReturnType;
1515 public MethodBuilder MethodBuilder;
1516 public readonly Attributes OptAttributes;
1519 // Modifiers allowed in a class declaration
1521 const int AllowedModifiers =
1524 Modifiers.PROTECTED |
1525 Modifiers.INTERNAL |
1530 Modifiers.OVERRIDE |
1531 Modifiers.ABSTRACT |
1534 // return_type can be "null" for VOID values.
1535 public Method (string return_type, int mod, string name, Parameters parameters,
1536 Attributes attrs, Location l)
1537 : base (name, parameters, l)
1539 ReturnType = return_type;
1540 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
1541 OptAttributes = attrs;
1544 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
1548 if (! (m is MethodInfo))
1551 MethodSignature sig = (MethodSignature) filter_criteria;
1553 if (m.Name != sig.Name)
1556 mi = (MethodInfo) m;
1558 if (mi.ReturnType != sig.RetType)
1561 Type [] args = TypeManager.GetArgumentTypes (mi);
1562 Type [] sigp = sig.Parameters;
1565 // FIXME: remove this assumption if we manage to
1566 // not enter a null as a the Parameters in TypeManager's
1567 // MethodBase to Type argument mapper.
1574 } else if (sigp == null)
1577 if (args.Length != sigp.Length)
1580 for (int i = args.Length; i > 0; ){
1582 if (args [i] != sigp [i])
1589 // This delegate is used to extract methods which have the
1590 // same signature as the argument
1592 static MemberFilter method_signature_filter;
1596 method_signature_filter = new MemberFilter (MemberSignatureCompare);
1600 // Returns the `System.Type' for the ReturnType of this
1601 // function. Provides a nice cache. (used between semantic analysis
1602 // and actual code generation
1604 Type type_return_type;
1605 public Type GetReturnType (TypeContainer parent)
1607 if (type_return_type == null)
1608 type_return_type = parent.LookupType (ReturnType, false);
1610 return type_return_type;
1613 void WarningNotHiding (TypeContainer parent)
1617 "The member `" + parent.Name + "." + Name + "' does not hide an " +
1618 "inherited member. The keyword new is not required");
1622 string MakeName (TypeContainer parent)
1624 return "`" + parent.Name + "." + Name + "'";
1627 string MethodBaseName (MethodBase mb)
1629 return "`" + mb.ReflectedType.Name + "." + mb.Name + "'";
1632 bool CheckMethod (TypeContainer parent, MemberInfo [] mi)
1634 MethodInfo mb = (MethodInfo) mi [0];
1636 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
1638 108, Location, "The keyword new is required on " +
1639 MakeName (parent) + " because it hides `" +
1640 mb.ReflectedType.Name + "." +
1644 if (mb.IsVirtual || mb.IsAbstract){
1645 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
1647 114, Location, MakeName (parent) +
1648 "hides inherited member " + MethodBaseName (mb) +
1649 ". To make the current member override that " +
1650 "implementation, add the override keyword, " +
1651 "otherwise use the new keyword");
1662 public MethodBuilder Define (TypeContainer parent)
1664 Type ret_type = GetReturnType (parent);
1665 Type [] parameters = ParameterTypes (parent);
1666 MethodAttributes flags;
1670 // Verify if the parent has a type with the same name, and then
1671 // check whether we have to create a new slot for it or not.
1673 Type ptype = parent.TypeBuilder.BaseType;
1675 // ptype is only null for System.Object while compiling corlib.
1677 MethodSignature ms = new MethodSignature (Name, ret_type, parameters);
1680 mi = TypeContainer.FindMembers (
1681 ptype, MemberTypes.Method,
1682 BindingFlags.Public, method_signature_filter,
1685 if (mi != null && mi.Length > 0){
1686 CheckMethod (parent, mi);
1688 if ((ModFlags & Modifiers.NEW) != 0)
1689 WarningNotHiding (parent);
1691 if ((ModFlags & Modifiers.OVERRIDE) != 0)
1692 Report.Error (115, Location,
1694 " no suitable methods found to override");
1696 } else if ((ModFlags & Modifiers.NEW) != 0)
1697 WarningNotHiding (parent);
1700 // If we implement an interface, then set the proper flags.
1702 flags = Modifiers.MethodAttr (ModFlags);
1704 if (parent.IsInterfaceMethod (Name, ret_type, parameters))
1705 flags |= MethodAttributes.Virtual | MethodAttributes.Final |
1706 MethodAttributes.NewSlot | MethodAttributes.HideBySig;
1709 // Catch invalid uses of virtual and abtract modifiers
1711 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
1712 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
1713 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
1715 if ((ModFlags & va) == va){
1716 if ((ModFlags & va) == va){
1718 503, Location, "The abstract method " +
1719 MakeName (parent) + "can not be marked virtual");
1724 if ((ModFlags & Modifiers.ABSTRACT) != 0){
1725 if ((parent.ModFlags & Modifiers.ABSTRACT) == 0){
1727 513, Location, MakeName (parent) +
1728 " is abstract but its container class is not");
1733 if ((ModFlags & va) != 0 && ((ModFlags & Modifiers.PRIVATE) != 0)){
1735 621, Location, MakeName (parent) +
1736 " virtual or abstract members can not be private");
1740 if ((ModFlags & Modifiers.STATIC) != 0){
1741 if ((ModFlags & vao) != 0){
1743 112, Location, "static method " + MakeName (parent) +
1744 " can not be marked as virtual, abstract or override");
1750 if ((ModFlags & Modifiers.OVERRIDE) != 0 && ((ModFlags & nv) != 0)){
1752 113, Location, MakeName (parent) +
1753 "marked as override cannot be marked as new or virtual");
1761 // Finally, define the method
1764 MethodBuilder = parent.TypeBuilder.DefineMethod (
1766 GetCallingConvention (parent is Class),
1767 ret_type, parameters);
1770 // HACK because System.Reflection.Emit is lame
1772 TypeManager.RegisterMethod (MethodBuilder, parameters);
1774 ParameterInfo = new InternalParameters (parent, Parameters);
1777 // This is used to track the Entry Point,
1779 // FIXME: Allow pluggable entry point, check arguments, etc.
1781 if (Name == "Main"){
1782 if ((ModFlags & Modifiers.STATIC) != 0){
1783 parent.RootContext.EntryPoint = MethodBuilder;
1788 // Define each type attribute (in/out/ref) and
1789 // the argument names.
1791 Parameter [] p = Parameters.FixedParameters;
1795 for (i = 0; i < p.Length; i++)
1796 MethodBuilder.DefineParameter (
1797 i + 1, p [i].Attributes, p [i].Name);
1799 if (i != parameters.Length)
1800 Console.WriteLine ("Implement the type definition for params");
1803 return MethodBuilder;
1809 public void Emit (TypeContainer parent)
1811 ILGenerator ig = MethodBuilder.GetILGenerator ();
1812 EmitContext ec = new EmitContext (parent, ig, GetReturnType (parent), ModFlags);
1814 ec.EmitTopBlock (Block);
1818 public abstract class ConstructorInitializer {
1819 ArrayList argument_list;
1820 ConstructorInfo parent_constructor;
1823 public ConstructorInitializer (ArrayList argument_list, Location location)
1825 this.argument_list = argument_list;
1826 this.location = location;
1829 public ArrayList Arguments {
1831 return argument_list;
1835 public bool Resolve (EmitContext ec)
1837 Expression parent_constructor_group;
1839 if (argument_list != null){
1840 for (int i = argument_list.Count; i > 0; ){
1843 Argument a = (Argument) argument_list [i];
1844 if (!a.Resolve (ec))
1849 parent_constructor_group = Expression.MemberLookup (
1851 ec.TypeContainer.TypeBuilder.BaseType, ".ctor", false,
1852 MemberTypes.Constructor,
1853 BindingFlags.Public | BindingFlags.Instance, location);
1855 //Console.WriteLine ("Base type : " + ec.TypeContainer.TypeBuilder.BaseType);
1856 if (parent_constructor_group == null){
1857 Console.WriteLine ("Could not find a constructor in our parent");
1861 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec,
1862 (MethodGroupExpr) parent_constructor_group, argument_list, location);
1864 if (parent_constructor == null)
1870 public void Emit (EmitContext ec)
1872 ec.ig.Emit (OpCodes.Ldarg_0);
1873 if (argument_list != null)
1874 Invocation.EmitArguments (ec, argument_list);
1875 ec.ig.Emit (OpCodes.Call, parent_constructor);
1879 public class ConstructorBaseInitializer : ConstructorInitializer {
1880 public ConstructorBaseInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
1885 public class ConstructorThisInitializer : ConstructorInitializer {
1886 public ConstructorThisInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
1891 public class Constructor : MethodCore {
1892 public ConstructorBuilder ConstructorBuilder;
1893 public ConstructorInitializer Initializer;
1896 // Modifiers allowed for a constructor.
1898 const int AllowedModifiers =
1900 Modifiers.PROTECTED |
1901 Modifiers.INTERNAL |
1906 // The spec claims that static is not permitted, but
1907 // my very own code has static constructors.
1909 public Constructor (string name, Parameters args, ConstructorInitializer init, Location l)
1910 : base (name, args, l)
1916 // Returns true if this is a default constructor
1918 public bool IsDefault ()
1920 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
1921 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
1922 (Initializer is ConstructorBaseInitializer) &&
1923 (Initializer.Arguments == null);
1927 // Creates the ConstructorBuilder
1929 public ConstructorBuilder Define (TypeContainer parent)
1931 MethodAttributes ca = (MethodAttributes.RTSpecialName |
1932 MethodAttributes.SpecialName);
1934 Type [] parameters = ParameterTypes (parent);
1936 if ((ModFlags & Modifiers.STATIC) != 0)
1937 ca |= MethodAttributes.Static;
1939 if (parent is Struct && parameters == null){
1942 "Structs can not contain explicit parameterless " +
1948 ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
1949 ca, GetCallingConvention (parent is Class),
1952 // HACK because System.Reflection.Emit is lame
1954 TypeManager.RegisterMethod (ConstructorBuilder, parameters);
1956 ParameterInfo = new InternalParameters (parent, Parameters);
1958 return ConstructorBuilder;
1964 public void Emit (TypeContainer parent)
1966 ILGenerator ig = ConstructorBuilder.GetILGenerator ();
1967 EmitContext ec = new EmitContext (parent, ig, null, ModFlags, true);
1969 if (parent is Class){
1970 if (Initializer == null)
1971 Initializer = new ConstructorBaseInitializer (null, parent.Location);
1973 if (!Initializer.Resolve (ec))
1978 // Classes can have base initializers and instance field initializers.
1980 if (parent is Class){
1981 if ((ModFlags & Modifiers.STATIC) == 0)
1982 Initializer.Emit (ec);
1983 parent.EmitFieldInitializers (ec, false);
1986 if ((ModFlags & Modifiers.STATIC) != 0)
1987 parent.EmitFieldInitializers (ec, true);
1989 ec.EmitTopBlock (Block);
1993 public class Field {
1994 public readonly string Type;
1995 public readonly Object Initializer;
1996 public readonly string Name;
1997 public readonly int ModFlags;
1998 public readonly Attributes OptAttributes;
1999 public FieldBuilder FieldBuilder;
2003 // Modifiers allowed in a class declaration
2005 const int AllowedModifiers =
2008 Modifiers.PROTECTED |
2009 Modifiers.INTERNAL |
2014 public Field (string type, int mod, string name, Object expr_or_array_init, Attributes attrs)
2017 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
2019 Initializer = expr_or_array_init;
2020 OptAttributes = attrs;
2023 public void Define (TypeContainer parent)
2025 Type t = parent.LookupType (Type, false);
2030 FieldBuilder = parent.TypeBuilder.DefineField (
2031 Name, t, Modifiers.FieldAttr (ModFlags));
2035 public class Property {
2037 public readonly string Type;
2038 public readonly string Name;
2039 public readonly int ModFlags;
2040 public Block Get, Set;
2041 public PropertyBuilder PropertyBuilder;
2042 public Attributes OptAttributes;
2043 MethodBuilder GetBuilder, SetBuilder;
2046 // The type, once we compute it.
2050 const int AllowedModifiers =
2053 Modifiers.PROTECTED |
2054 Modifiers.INTERNAL |
2058 Modifiers.OVERRIDE |
2059 Modifiers.ABSTRACT |
2062 public Property (string type, string name, int mod_flags, Block get_block, Block set_block, Attributes attrs)
2066 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
2069 OptAttributes = attrs;
2072 public void Define (TypeContainer parent)
2075 MethodAttributes method_attr = Modifiers.MethodAttr(ModFlags);
2077 // FIXME - PropertyAttributes.HasDefault ?
2079 PropertyAttributes prop_attr = PropertyAttributes.RTSpecialName |
2080 PropertyAttributes.SpecialName;
2083 PropertyType = parent.LookupType (Type, false);
2084 Type [] parameters = new Type [1];
2085 parameters [0] = PropertyType;
2087 PropertyBuilder = parent.TypeBuilder.DefineProperty (
2088 Name, prop_attr, PropertyType, null);
2092 GetBuilder = parent.TypeBuilder.DefineMethod (
2093 "get_" + Name, method_attr, PropertyType, null);
2094 PropertyBuilder.SetGetMethod (GetBuilder);
2096 // HACK because System.Reflection.Emit is lame
2098 TypeManager.RegisterMethod (GetBuilder, null);
2103 SetBuilder = parent.TypeBuilder.DefineMethod (
2104 "set_" + Name, method_attr, null, parameters);
2105 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
2106 PropertyBuilder.SetSetMethod (SetBuilder);
2108 // HACK because System.Reflection.Emit is lame
2110 TypeManager.RegisterMethod (SetBuilder, parameters);
2114 // HACK for the reasons exposed above
2116 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
2119 public void Emit (TypeContainer tc)
2125 ig = GetBuilder.GetILGenerator ();
2126 ec = new EmitContext (tc, ig, PropertyType, ModFlags);
2128 ec.EmitTopBlock (Get);
2132 ig = SetBuilder.GetILGenerator ();
2133 ec = new EmitContext (tc, ig, null, ModFlags);
2135 ec.EmitTopBlock (Set);
2140 public class Event {
2142 const int AllowedModifiers =
2145 Modifiers.PROTECTED |
2146 Modifiers.INTERNAL |
2151 Modifiers.OVERRIDE |
2154 public readonly string Type;
2155 public readonly string Name;
2156 public readonly Object Initializer;
2157 public readonly int ModFlags;
2158 public readonly Block Add;
2159 public readonly Block Remove;
2160 public EventBuilder EventBuilder;
2161 public Attributes OptAttributes;
2163 public Event (string type, string name, Object init, int flags, Block add_block, Block rem_block,
2169 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE);
2172 OptAttributes = attrs;
2175 public void Define (TypeContainer parent)
2177 MethodAttributes m_attr = Modifiers.MethodAttr (ModFlags);
2179 EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
2183 Type t = parent.LookupType (Type, false);
2184 Type [] parameters = new Type [1];
2187 EventBuilder = parent.TypeBuilder.DefineEvent (Name, e_attr, t);
2190 mb = parent.TypeBuilder.DefineMethod ("add_" + Name, m_attr, null,
2192 mb.DefineParameter (1, ParameterAttributes.None, "value");
2193 EventBuilder.SetAddOnMethod (mb);
2195 // HACK because System.Reflection.Emit is lame
2197 TypeManager.RegisterMethod (mb, parameters);
2200 if (Remove != null) {
2201 mb = parent.TypeBuilder.DefineMethod ("remove_" + Name, m_attr, null,
2203 mb.DefineParameter (1, ParameterAttributes.None, "value");
2204 EventBuilder.SetRemoveOnMethod (mb);
2207 // HACK because System.Reflection.Emit is lame
2209 TypeManager.RegisterMethod (mb, parameters);
2216 // FIXME: This does not handle:
2218 // int INTERFACENAME [ args ]
2222 // int this [ args ]
2224 public class Indexer {
2226 const int AllowedModifiers =
2229 Modifiers.PROTECTED |
2230 Modifiers.INTERNAL |
2234 Modifiers.OVERRIDE |
2237 public readonly string Type;
2238 public readonly string InterfaceType;
2239 public readonly Parameters FormalParameters;
2240 public readonly int ModFlags;
2241 public readonly Block Get;
2242 public readonly Block Set;
2243 public Attributes OptAttributes;
2244 public MethodBuilder GetBuilder;
2245 public MethodBuilder SetBuilder;
2246 public PropertyBuilder PropertyBuilder;
2247 public Type IndexerType;
2249 public Indexer (string type, string int_type, int flags, Parameters parms,
2250 Block get_block, Block set_block, Attributes attrs)
2254 InterfaceType = int_type;
2255 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE);
2256 FormalParameters = parms;
2259 OptAttributes = attrs;
2262 public void Define (TypeContainer parent)
2264 MethodAttributes attr = Modifiers.MethodAttr (ModFlags);
2265 PropertyAttributes prop_attr =
2266 PropertyAttributes.RTSpecialName |
2267 PropertyAttributes.SpecialName;
2269 IndexerType = parent.LookupType (Type, false);
2270 Type [] parameters = FormalParameters.GetParameterInfo (parent);
2272 PropertyBuilder = parent.TypeBuilder.DefineProperty (
2273 TypeManager.IndexerPropertyName (parent.TypeBuilder),
2274 prop_attr, IndexerType, parameters);
2277 GetBuilder = parent.TypeBuilder.DefineMethod (
2278 "get_Item", attr, IndexerType, parameters);
2279 TypeManager.RegisterMethod (GetBuilder, parameters);
2280 TypeContainer.RegisterParameterForBuilder (
2281 GetBuilder, new InternalParameters (parent, FormalParameters));
2285 int top = parameters.Length;
2286 Type [] set_pars = new Type [top + 1];
2287 parameters.CopyTo (set_pars, 0);
2288 set_pars [top] = IndexerType;
2290 Parameter [] fixed_parms = FormalParameters.FixedParameters;
2292 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
2294 fixed_parms.CopyTo (tmp, 0);
2295 tmp [fixed_parms.Length] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
2297 Parameters set_formal_params = new Parameters (tmp, null);
2299 SetBuilder = parent.TypeBuilder.DefineMethod (
2300 "set_Item", attr, null, set_pars);
2301 TypeManager.RegisterMethod (SetBuilder, set_pars);
2302 TypeContainer.RegisterParameterForBuilder (
2303 SetBuilder, new InternalParameters (parent, set_formal_params));
2306 PropertyBuilder.SetGetMethod (GetBuilder);
2307 PropertyBuilder.SetSetMethod (SetBuilder);
2309 Parameter [] p = FormalParameters.FixedParameters;
2314 for (i = 0; i < p.Length; ++i) {
2316 GetBuilder.DefineParameter (
2317 i + 1, p [i].Attributes, p [i].Name);
2320 SetBuilder.DefineParameter (
2321 i + 1, p [i].Attributes, p [i].Name);
2325 SetBuilder.DefineParameter (
2326 i + 1, ParameterAttributes.None, "value");
2328 if (i != parameters.Length)
2329 Console.WriteLine ("Implement type definition for params");
2332 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
2335 public void Emit (TypeContainer tc)
2341 ig = GetBuilder.GetILGenerator ();
2342 ec = new EmitContext (tc, ig, IndexerType, ModFlags);
2344 ec.EmitTopBlock (Get);
2348 ig = SetBuilder.GetILGenerator ();
2349 ec = new EmitContext (tc, ig, null, ModFlags);
2351 ec.EmitTopBlock (Set);
2356 public class Operator {
2358 const int AllowedModifiers =
2362 const int RequiredModifiers =
2366 public enum OpType {
2376 // Unary and Binary operators
2399 // Implicit and Explicit
2404 public readonly OpType OperatorType;
2405 public readonly string ReturnType;
2406 public readonly string FirstArgType;
2407 public readonly string FirstArgName;
2408 public readonly string SecondArgType;
2409 public readonly string SecondArgName;
2410 public readonly int ModFlags;
2411 public readonly Block Block;
2412 public Attributes OptAttributes;
2413 public MethodBuilder OperatorMethodBuilder;
2414 public Location Location;
2416 public string MethodName;
2417 public Method OperatorMethod;
2419 public Operator (OpType type, string ret_type, int flags, string arg1type, string arg1name,
2420 string arg2type, string arg2name, Block block, Attributes attrs, Location loc)
2422 OperatorType = type;
2423 ReturnType = ret_type;
2424 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PUBLIC);
2425 FirstArgType = arg1type;
2426 FirstArgName = arg1name;
2427 SecondArgType = arg2type;
2428 SecondArgName = arg2name;
2430 OptAttributes = attrs;
2434 string Prototype (TypeContainer parent)
2436 return parent.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
2437 SecondArgType + ")";
2440 public void Define (TypeContainer parent)
2443 MethodName = "op_" + OperatorType;
2445 if (SecondArgType != null)
2448 Parameter [] param_list = new Parameter [length];
2450 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2453 "User defined operators `" +
2454 Prototype (parent) +
2455 "' must be declared static and public");
2458 param_list[0] = new Parameter (FirstArgType, FirstArgName,
2459 Parameter.Modifier.NONE, null);
2460 if (SecondArgType != null)
2461 param_list[1] = new Parameter (SecondArgType, SecondArgName,
2462 Parameter.Modifier.NONE, null);
2464 OperatorMethod = new Method (ReturnType, ModFlags, MethodName,
2465 new Parameters (param_list, null),
2466 OptAttributes, Location.Null);
2468 OperatorMethod.Define (parent);
2469 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
2471 Type [] param_types = OperatorMethod.ParameterTypes (parent);
2472 Type declaring_type = OperatorMethodBuilder.DeclaringType;
2473 Type return_type = OperatorMethod.GetReturnType (parent);
2474 Type first_arg_type = param_types [0];
2476 // Rules for conversion operators
2478 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2480 if (first_arg_type == return_type && first_arg_type == declaring_type)
2481 Report.Error (555, Location,
2482 "User-defined conversion cannot take an object of the enclosing type " +
2483 "and convert to an object of the enclosing type");
2485 if (first_arg_type != declaring_type && return_type != declaring_type)
2486 Report.Error (556, Location,
2487 "User-defined conversion must convert to or from the enclosing type");
2489 if (first_arg_type == TypeManager.object_type || return_type == TypeManager.object_type)
2490 Report.Error (-8, Location,
2491 "User-defined conversion cannot convert to or from object type");
2493 if (first_arg_type.IsInterface || return_type.IsInterface)
2494 Report.Error (-9, Location,
2495 "User-defined conversion cannot convert to or from an interface type");
2497 if (first_arg_type.IsSubclassOf (return_type) || return_type.IsSubclassOf (first_arg_type))
2498 Report.Error (-10, Location,
2499 "User-defined conversion cannot convert between types that " +
2500 "derive from each other");
2502 } else if (SecondArgType == null) {
2503 // Checks for Unary operators
2505 if (first_arg_type != declaring_type)
2506 Report.Error (562, Location,
2507 "The parameter of a unary operator must be the containing type");
2510 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2511 if (return_type != declaring_type)
2512 Report.Error (559, Location,
2513 "The parameter and return type for ++ and -- " +
2514 "must be the containing type");
2518 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2519 if (return_type != TypeManager.bool_type)
2520 Report.Error (215, Location,
2521 "The return type of operator True or False " +
2526 // Checks for Binary operators
2528 if (first_arg_type != declaring_type &&
2529 param_types [1] != declaring_type)
2530 Report.Error (563, Location,
2531 "One of the parameters of a binary operator must be the containing type");
2538 public void Emit (TypeContainer parent)
2540 OperatorMethod.Block = Block;
2541 OperatorMethod.Emit (parent);
2548 // This is used to compare method signatures
2550 struct MethodSignature {
2552 public Type RetType;
2553 public Type [] Parameters;
2555 public MethodSignature (string name, Type ret_type, Type [] parameters)
2559 Parameters = parameters;
2561 if (parameters != null){
2562 if (parameters.Length == 0)
2567 public override int GetHashCode ()
2569 return Name.GetHashCode ();
2572 public override bool Equals (Object o)
2574 MethodSignature other = (MethodSignature) o;
2576 if (other.Name != Name)
2579 if (other.RetType != RetType)
2582 if (Parameters == null){
2583 if (other.Parameters == null)
2588 if (other.Parameters == null)
2591 int c = Parameters.Length;
2592 if (other.Parameters.Length != c)
2595 for (int i = 0; i < c; i++)
2596 if (other.Parameters [i] != Parameters [i])