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 (pending_implementations != null)
1319 VerifyPendingMethods ();
1322 foreach (TypeContainer tc in types)
1327 public void CloseType ()
1329 TypeBuilder.CreateType ();
1331 foreach (TypeContainer tc in Types)
1334 if (Delegates != null)
1335 foreach (Delegate d in Delegates)
1339 string MakeName (string n)
1341 return "`" + Name + "." + n + "'";
1344 public static int CheckMember (string name, MemberInfo mi, int ModFlags)
1350 public class Class : TypeContainer {
1352 // Modifiers allowed in a class declaration
1354 public const int AllowedModifiers =
1357 Modifiers.PROTECTED |
1358 Modifiers.INTERNAL |
1360 Modifiers.ABSTRACT |
1363 public Class (RootContext rc, TypeContainer parent, string name, int mod,
1364 Attributes attrs, Location l)
1365 : base (rc, parent, name, l)
1369 if (parent.Parent == null)
1370 accmods = Modifiers.INTERNAL;
1372 accmods = Modifiers.PRIVATE;
1374 this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
1375 this.attributes = attrs;
1379 // FIXME: How do we deal with the user specifying a different
1382 public override TypeAttributes TypeAttr {
1384 return base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
1389 public class Struct : TypeContainer {
1391 // Modifiers allowed in a struct declaration
1393 public const int AllowedModifiers =
1396 Modifiers.PROTECTED |
1397 Modifiers.INTERNAL |
1400 public Struct (RootContext rc, TypeContainer parent, string name, int mod,
1401 Attributes attrs, Location l)
1402 : base (rc, parent, name, l)
1406 if (parent.Parent == null)
1407 accmods = Modifiers.INTERNAL;
1409 accmods = Modifiers.PRIVATE;
1411 this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
1413 this.mod_flags |= Modifiers.SEALED;
1414 this.attributes = attrs;
1419 // FIXME: Allow the user to specify a different set of attributes
1420 // in some cases (Sealed for example is mandatory for a class,
1421 // but what SequentialLayout can be changed
1423 public override TypeAttributes TypeAttr {
1425 return base.TypeAttr |
1426 TypeAttributes.SequentialLayout |
1427 TypeAttributes.Sealed |
1428 TypeAttributes.BeforeFieldInit;
1433 public class MethodCore {
1434 public readonly Parameters Parameters;
1435 public readonly string Name;
1436 public int ModFlags;
1438 public readonly Location Location;
1441 // Parameters, cached for semantic analysis.
1443 InternalParameters parameter_info;
1445 public MethodCore (string name, Parameters parameters, Location l)
1448 Parameters = parameters;
1453 // Returns the System.Type array for the parameters of this method
1455 Type [] parameter_types;
1456 public Type [] ParameterTypes (TypeContainer parent)
1458 if (Parameters == null)
1461 if (parameter_types == null)
1462 parameter_types = Parameters.GetParameterInfo (parent);
1464 return parameter_types;
1467 public InternalParameters ParameterInfo
1470 return parameter_info;
1474 parameter_info = value;
1478 public Block Block {
1488 public CallingConventions GetCallingConvention (bool is_class)
1490 CallingConventions cc = 0;
1492 cc = Parameters.GetCallingConvention ();
1495 if ((ModFlags & Modifiers.STATIC) == 0)
1496 cc |= CallingConventions.HasThis;
1498 // FIXME: How is `ExplicitThis' used in C#?
1504 public class Method : MethodCore {
1505 public readonly string ReturnType;
1506 public MethodBuilder MethodBuilder;
1507 public readonly Attributes OptAttributes;
1510 // Modifiers allowed in a class declaration
1512 const int AllowedModifiers =
1515 Modifiers.PROTECTED |
1516 Modifiers.INTERNAL |
1521 Modifiers.OVERRIDE |
1522 Modifiers.ABSTRACT |
1525 // return_type can be "null" for VOID values.
1526 public Method (string return_type, int mod, string name, Parameters parameters,
1527 Attributes attrs, Location l)
1528 : base (name, parameters, l)
1530 ReturnType = return_type;
1531 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
1532 OptAttributes = attrs;
1535 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
1539 if (! (m is MethodInfo))
1542 MethodSignature sig = (MethodSignature) filter_criteria;
1544 if (m.Name != sig.Name)
1547 mi = (MethodInfo) m;
1549 if (mi.ReturnType != sig.RetType)
1552 Type [] args = TypeManager.GetArgumentTypes (mi);
1553 Type [] sigp = sig.Parameters;
1556 // FIXME: remove this assumption if we manage to
1557 // not enter a null as a the Parameters in TypeManager's
1558 // MethodBase to Type argument mapper.
1565 } else if (sigp == null)
1568 if (args.Length != sigp.Length)
1571 for (int i = args.Length; i > 0; ){
1573 if (args [i] != sigp [i])
1580 // This delegate is used to extract methods which have the
1581 // same signature as the argument
1583 static MemberFilter method_signature_filter;
1587 method_signature_filter = new MemberFilter (MemberSignatureCompare);
1591 // Returns the `System.Type' for the ReturnType of this
1592 // function. Provides a nice cache. (used between semantic analysis
1593 // and actual code generation
1595 Type type_return_type;
1596 public Type GetReturnType (TypeContainer parent)
1598 if (type_return_type == null)
1599 type_return_type = parent.LookupType (ReturnType, false);
1601 return type_return_type;
1604 void WarningNotHiding (TypeContainer parent)
1608 "The member `" + parent.Name + "." + Name + "' does not hide an " +
1609 "inherited member. The keyword new is not required");
1613 string MakeName (TypeContainer parent)
1615 return "`" + parent.Name + "." + Name + "'";
1618 string MethodBaseName (MethodBase mb)
1620 return "`" + mb.ReflectedType.Name + "." + mb.Name + "'";
1623 bool CheckMethod (TypeContainer parent, MemberInfo [] mi)
1625 MethodInfo mb = (MethodInfo) mi [0];
1627 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
1629 108, Location, "The keyword new is required on " +
1630 MakeName (parent) + " because it hides `" +
1631 mb.ReflectedType.Name + "." +
1635 if (mb.IsVirtual || mb.IsAbstract){
1636 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
1638 114, Location, MakeName (parent) +
1639 "hides inherited member " + MethodBaseName (mb) +
1640 ". To make the current member override that " +
1641 "implementation, add the override keyword, " +
1642 "otherwise use the new keyword");
1653 public MethodBuilder Define (TypeContainer parent)
1655 Type ret_type = GetReturnType (parent);
1656 Type [] parameters = ParameterTypes (parent);
1657 MethodAttributes flags;
1661 // Verify if the parent has a type with the same name, and then
1662 // check whether we have to create a new slot for it or not.
1664 Type ptype = parent.TypeBuilder.BaseType;
1666 // ptype is only null for System.Object while compiling corlib.
1668 MethodSignature ms = new MethodSignature (Name, ret_type, parameters);
1671 mi = TypeContainer.FindMembers (
1672 ptype, MemberTypes.Method,
1673 BindingFlags.Public, method_signature_filter,
1676 if (mi != null && mi.Length > 0){
1677 CheckMethod (parent, mi);
1679 if ((ModFlags & Modifiers.NEW) != 0)
1680 WarningNotHiding (parent);
1682 if ((ModFlags & Modifiers.OVERRIDE) != 0)
1683 Report.Error (115, Location,
1685 " no suitable methods found to override");
1687 } else if ((ModFlags & Modifiers.NEW) != 0)
1688 WarningNotHiding (parent);
1691 // If we implement an interface, then set the proper flags.
1693 flags = Modifiers.MethodAttr (ModFlags);
1695 if (parent.IsInterfaceMethod (Name, ret_type, parameters))
1696 flags |= MethodAttributes.Virtual | MethodAttributes.Final |
1697 MethodAttributes.NewSlot | MethodAttributes.HideBySig;
1700 // Catch invalid uses of virtual and abtract modifiers
1702 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
1703 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
1704 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
1706 if ((ModFlags & va) == va){
1707 if ((ModFlags & va) == va){
1709 503, Location, "The abstract method " +
1710 MakeName (parent) + "can not be marked virtual");
1715 if ((ModFlags & Modifiers.ABSTRACT) != 0){
1716 if ((parent.ModFlags & Modifiers.ABSTRACT) == 0){
1718 513, Location, MakeName (parent) +
1719 " is abstract but its container class is not");
1724 if ((ModFlags & va) != 0 && ((ModFlags & Modifiers.PRIVATE) != 0)){
1726 621, Location, MakeName (parent) +
1727 " virtual or abstract members can not be private");
1731 if ((ModFlags & Modifiers.STATIC) != 0){
1732 if ((ModFlags & vao) != 0){
1734 112, Location, "static method " + MakeName (parent) +
1735 " can not be marked as virtual, abstract or override");
1741 if ((ModFlags & Modifiers.OVERRIDE) != 0 && ((ModFlags & nv) != 0)){
1743 113, Location, MakeName (parent) +
1744 "marked as override cannot be marked as new or virtual");
1752 // Finally, define the method
1755 MethodBuilder = parent.TypeBuilder.DefineMethod (
1757 GetCallingConvention (parent is Class),
1758 ret_type, parameters);
1761 // HACK because System.Reflection.Emit is lame
1763 TypeManager.RegisterMethod (MethodBuilder, parameters);
1765 ParameterInfo = new InternalParameters (parameters);
1768 // This is used to track the Entry Point,
1770 // FIXME: Allow pluggable entry point, check arguments, etc.
1772 if (Name == "Main"){
1773 if ((ModFlags & Modifiers.STATIC) != 0){
1774 parent.RootContext.EntryPoint = MethodBuilder;
1779 // Define each type attribute (in/out/ref) and
1780 // the argument names.
1782 Parameter [] p = Parameters.FixedParameters;
1786 for (i = 0; i < p.Length; i++)
1787 MethodBuilder.DefineParameter (
1788 i + 1, p [i].Attributes, p [i].Name);
1790 if (i != parameters.Length)
1791 Console.WriteLine ("Implement the type definition for params");
1794 return MethodBuilder;
1800 public void Emit (TypeContainer parent)
1802 ILGenerator ig = MethodBuilder.GetILGenerator ();
1803 EmitContext ec = new EmitContext (parent, ig, GetReturnType (parent), ModFlags);
1805 ec.EmitTopBlock (Block);
1809 public abstract class ConstructorInitializer {
1810 ArrayList argument_list;
1811 ConstructorInfo parent_constructor;
1814 public ConstructorInitializer (ArrayList argument_list, Location location)
1816 this.argument_list = argument_list;
1817 this.location = location;
1820 public ArrayList Arguments {
1822 return argument_list;
1826 public bool Resolve (EmitContext ec)
1828 Expression parent_constructor_group;
1830 if (argument_list != null){
1831 for (int i = argument_list.Count; i > 0; ){
1834 Argument a = (Argument) argument_list [i];
1835 if (!a.Resolve (ec))
1840 parent_constructor_group = Expression.MemberLookup (
1842 ec.TypeContainer.TypeBuilder.BaseType, ".ctor", false,
1843 MemberTypes.Constructor,
1844 BindingFlags.Public | BindingFlags.Instance, location);
1846 //Console.WriteLine ("Base type : " + ec.TypeContainer.TypeBuilder.BaseType);
1847 if (parent_constructor_group == null){
1848 Console.WriteLine ("Could not find a constructor in our parent");
1852 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec,
1853 (MethodGroupExpr) parent_constructor_group, argument_list, location);
1855 if (parent_constructor == null)
1861 public void Emit (EmitContext ec)
1863 ec.ig.Emit (OpCodes.Ldarg_0);
1864 if (argument_list != null)
1865 Invocation.EmitArguments (ec, parent_constructor, argument_list);
1866 ec.ig.Emit (OpCodes.Call, parent_constructor);
1870 public class ConstructorBaseInitializer : ConstructorInitializer {
1871 public ConstructorBaseInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
1876 public class ConstructorThisInitializer : ConstructorInitializer {
1877 public ConstructorThisInitializer (ArrayList argument_list, Location l) : base (argument_list, l)
1882 public class Constructor : MethodCore {
1883 public ConstructorBuilder ConstructorBuilder;
1884 public ConstructorInitializer Initializer;
1887 // Modifiers allowed for a constructor.
1889 const int AllowedModifiers =
1891 Modifiers.PROTECTED |
1892 Modifiers.INTERNAL |
1897 // The spec claims that static is not permitted, but
1898 // my very own code has static constructors.
1900 public Constructor (string name, Parameters args, ConstructorInitializer init, Location l)
1901 : base (name, args, l)
1907 // Returns true if this is a default constructor
1909 public bool IsDefault ()
1911 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
1912 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
1913 (Initializer is ConstructorBaseInitializer) &&
1914 (Initializer.Arguments == null);
1918 // Creates the ConstructorBuilder
1920 public ConstructorBuilder Define (TypeContainer parent)
1922 MethodAttributes ca = (MethodAttributes.RTSpecialName |
1923 MethodAttributes.SpecialName);
1925 Type [] parameters = ParameterTypes (parent);
1927 if ((ModFlags & Modifiers.STATIC) != 0)
1928 ca |= MethodAttributes.Static;
1930 if (parent is Struct && parameters == null){
1933 "Structs can not contain explicit parameterless " +
1939 ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
1940 ca, GetCallingConvention (parent is Class),
1943 // HACK because System.Reflection.Emit is lame
1945 TypeManager.RegisterMethod (ConstructorBuilder, parameters);
1947 ParameterInfo = new InternalParameters (parameters);
1949 return ConstructorBuilder;
1955 public void Emit (TypeContainer parent)
1957 ILGenerator ig = ConstructorBuilder.GetILGenerator ();
1958 EmitContext ec = new EmitContext (parent, ig, null, ModFlags, true);
1960 if (parent is Class){
1961 if (Initializer == null)
1962 Initializer = new ConstructorBaseInitializer (null, parent.Location);
1964 if (!Initializer.Resolve (ec))
1969 // Classes can have base initializers and instance field initializers.
1971 if (parent is Class){
1972 if ((ModFlags & Modifiers.STATIC) == 0)
1973 Initializer.Emit (ec);
1974 parent.EmitFieldInitializers (ec, false);
1977 if ((ModFlags & Modifiers.STATIC) != 0)
1978 parent.EmitFieldInitializers (ec, true);
1980 ec.EmitTopBlock (Block);
1984 public class Field {
1985 public readonly string Type;
1986 public readonly Object Initializer;
1987 public readonly string Name;
1988 public readonly int ModFlags;
1989 public readonly Attributes OptAttributes;
1990 public FieldBuilder FieldBuilder;
1994 // Modifiers allowed in a class declaration
1996 const int AllowedModifiers =
1999 Modifiers.PROTECTED |
2000 Modifiers.INTERNAL |
2005 public Field (string type, int mod, string name, Object expr_or_array_init, Attributes attrs)
2008 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
2010 Initializer = expr_or_array_init;
2011 OptAttributes = attrs;
2014 public void Define (TypeContainer parent)
2016 Type t = parent.LookupType (Type, false);
2021 FieldBuilder = parent.TypeBuilder.DefineField (
2022 Name, t, Modifiers.FieldAttr (ModFlags));
2026 public class Property {
2028 public readonly string Type;
2029 public readonly string Name;
2030 public readonly int ModFlags;
2031 public Block Get, Set;
2032 public PropertyBuilder PropertyBuilder;
2033 public Attributes OptAttributes;
2034 MethodBuilder GetBuilder, SetBuilder;
2037 // The type, once we compute it.
2041 const int AllowedModifiers =
2044 Modifiers.PROTECTED |
2045 Modifiers.INTERNAL |
2049 Modifiers.OVERRIDE |
2050 Modifiers.ABSTRACT |
2053 public Property (string type, string name, int mod_flags, Block get_block, Block set_block, Attributes attrs)
2057 ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
2060 OptAttributes = attrs;
2063 public void Define (TypeContainer parent)
2066 MethodAttributes method_attr = Modifiers.MethodAttr(ModFlags);
2068 // FIXME - PropertyAttributes.HasDefault ?
2070 PropertyAttributes prop_attr = PropertyAttributes.RTSpecialName |
2071 PropertyAttributes.SpecialName;
2074 PropertyType = parent.LookupType (Type, false);
2075 Type [] parameters = new Type [1];
2076 parameters [0] = PropertyType;
2078 PropertyBuilder = parent.TypeBuilder.DefineProperty (
2079 Name, prop_attr, PropertyType, null);
2083 GetBuilder = parent.TypeBuilder.DefineMethod (
2084 "get_" + Name, method_attr, PropertyType, null);
2085 PropertyBuilder.SetGetMethod (GetBuilder);
2087 // HACK because System.Reflection.Emit is lame
2089 TypeManager.RegisterMethod (GetBuilder, null);
2094 SetBuilder = parent.TypeBuilder.DefineMethod (
2095 "set_" + Name, method_attr, null, parameters);
2096 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
2097 PropertyBuilder.SetSetMethod (SetBuilder);
2099 // HACK because System.Reflection.Emit is lame
2101 TypeManager.RegisterMethod (SetBuilder, parameters);
2105 // HACK for the reasons exposed above
2107 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
2110 public void Emit (TypeContainer tc)
2116 ig = GetBuilder.GetILGenerator ();
2117 ec = new EmitContext (tc, ig, PropertyType, ModFlags);
2119 ec.EmitTopBlock (Get);
2123 ig = SetBuilder.GetILGenerator ();
2124 ec = new EmitContext (tc, ig, null, ModFlags);
2126 ec.EmitTopBlock (Set);
2131 public class Event {
2133 const int AllowedModifiers =
2136 Modifiers.PROTECTED |
2137 Modifiers.INTERNAL |
2142 Modifiers.OVERRIDE |
2145 public readonly string Type;
2146 public readonly string Name;
2147 public readonly Object Initializer;
2148 public readonly int ModFlags;
2149 public readonly Block Add;
2150 public readonly Block Remove;
2151 public EventBuilder EventBuilder;
2152 public Attributes OptAttributes;
2154 public Event (string type, string name, Object init, int flags, Block add_block, Block rem_block,
2160 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE);
2163 OptAttributes = attrs;
2166 public void Define (TypeContainer parent)
2168 MethodAttributes m_attr = Modifiers.MethodAttr (ModFlags);
2170 EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
2174 Type t = parent.LookupType (Type, false);
2175 Type [] parameters = new Type [1];
2178 EventBuilder = parent.TypeBuilder.DefineEvent (Name, e_attr, t);
2181 mb = parent.TypeBuilder.DefineMethod ("add_" + Name, m_attr, null,
2183 mb.DefineParameter (1, ParameterAttributes.None, "value");
2184 EventBuilder.SetAddOnMethod (mb);
2186 // HACK because System.Reflection.Emit is lame
2188 TypeManager.RegisterMethod (mb, parameters);
2191 if (Remove != null) {
2192 mb = parent.TypeBuilder.DefineMethod ("remove_" + Name, m_attr, null,
2194 mb.DefineParameter (1, ParameterAttributes.None, "value");
2195 EventBuilder.SetRemoveOnMethod (mb);
2198 // HACK because System.Reflection.Emit is lame
2200 TypeManager.RegisterMethod (mb, parameters);
2207 // FIXME: This does not handle:
2209 // int INTERFACENAME [ args ]
2213 // int this [ args ]
2215 public class Indexer {
2217 const int AllowedModifiers =
2220 Modifiers.PROTECTED |
2221 Modifiers.INTERNAL |
2225 Modifiers.OVERRIDE |
2228 public readonly string Type;
2229 public readonly string InterfaceType;
2230 public readonly Parameters FormalParameters;
2231 public readonly int ModFlags;
2232 public readonly Block Get;
2233 public readonly Block Set;
2234 public Attributes OptAttributes;
2235 public MethodBuilder GetBuilder;
2236 public MethodBuilder SetBuilder;
2237 public PropertyBuilder PropertyBuilder;
2238 public Type IndexerType;
2240 public Indexer (string type, string int_type, int flags, Parameters parms,
2241 Block get_block, Block set_block, Attributes attrs)
2245 InterfaceType = int_type;
2246 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE);
2247 FormalParameters = parms;
2250 OptAttributes = attrs;
2253 public void Define (TypeContainer parent)
2255 MethodAttributes attr = Modifiers.MethodAttr (ModFlags);
2256 PropertyAttributes prop_attr =
2257 PropertyAttributes.RTSpecialName |
2258 PropertyAttributes.SpecialName;
2260 Type IndexerType = parent.LookupType (Type, false);
2261 Type [] parameters = FormalParameters.GetParameterInfo (parent);
2263 PropertyBuilder = parent.TypeBuilder.DefineProperty (
2264 TypeManager.IndexerPropertyName (parent.TypeBuilder),
2265 prop_attr, IndexerType, parameters);
2268 GetBuilder = parent.TypeBuilder.DefineMethod (
2269 "get_Item", attr, IndexerType, parameters);
2270 TypeManager.RegisterMethod (GetBuilder, parameters);
2271 TypeContainer.RegisterParameterForBuilder (
2272 GetBuilder, new InternalParameters (parameters));
2276 int top = parameters.Length;
2277 Type [] set_pars = new Type [top + 1];
2278 parameters.CopyTo (set_pars, 0);
2279 set_pars [top] = IndexerType;
2281 SetBuilder = parent.TypeBuilder.DefineMethod (
2282 "set_Item", attr, null, set_pars);
2283 TypeManager.RegisterMethod (SetBuilder, set_pars);
2284 TypeContainer.RegisterParameterForBuilder (
2285 SetBuilder, new InternalParameters (set_pars));
2288 PropertyBuilder.SetGetMethod (GetBuilder);
2289 PropertyBuilder.SetSetMethod (SetBuilder);
2291 Parameter [] p = FormalParameters.FixedParameters;
2296 for (i = 0; i < p.Length; ++i) {
2298 GetBuilder.DefineParameter (
2299 i + 1, p [i].Attributes, p [i].Name);
2302 SetBuilder.DefineParameter (
2303 i + 1, p [i].Attributes, p [i].Name);
2307 SetBuilder.DefineParameter (
2308 i + 1, ParameterAttributes.None, "value");
2310 if (i != parameters.Length)
2311 Console.WriteLine ("Implement type definition for params");
2314 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
2317 public void Emit (TypeContainer tc)
2323 ig = GetBuilder.GetILGenerator ();
2324 ec = new EmitContext (tc, ig, IndexerType, ModFlags);
2326 ec.EmitTopBlock (Get);
2330 ig = SetBuilder.GetILGenerator ();
2331 ec = new EmitContext (tc, ig, null, ModFlags);
2333 ec.EmitTopBlock (Set);
2338 public class Operator {
2340 const int AllowedModifiers =
2344 const int RequiredModifiers =
2348 public enum OpType {
2358 // Unary and Binary operators
2381 // Implicit and Explicit
2386 public readonly OpType OperatorType;
2387 public readonly string ReturnType;
2388 public readonly string FirstArgType;
2389 public readonly string FirstArgName;
2390 public readonly string SecondArgType;
2391 public readonly string SecondArgName;
2392 public readonly int ModFlags;
2393 public readonly Block Block;
2394 public Attributes OptAttributes;
2395 public MethodBuilder OperatorMethodBuilder;
2396 public Location Location;
2398 public string MethodName;
2399 public Method OperatorMethod;
2401 public Operator (OpType type, string ret_type, int flags, string arg1type, string arg1name,
2402 string arg2type, string arg2name, Block block, Attributes attrs, Location loc)
2404 OperatorType = type;
2405 ReturnType = ret_type;
2406 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PUBLIC);
2407 FirstArgType = arg1type;
2408 FirstArgName = arg1name;
2409 SecondArgType = arg2type;
2410 SecondArgName = arg2name;
2412 OptAttributes = attrs;
2416 string Prototype (TypeContainer parent)
2418 return parent.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
2419 SecondArgType + ")";
2422 public void Define (TypeContainer parent)
2425 MethodName = "op_" + OperatorType;
2427 if (SecondArgType != null)
2430 Parameter [] param_list = new Parameter [length];
2432 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2435 "User defined operators `" +
2436 Prototype (parent) +
2437 "' must be declared static and public");
2440 param_list[0] = new Parameter (FirstArgType, FirstArgName,
2441 Parameter.Modifier.NONE, null);
2442 if (SecondArgType != null)
2443 param_list[1] = new Parameter (SecondArgType, SecondArgName,
2444 Parameter.Modifier.NONE, null);
2446 OperatorMethod = new Method (ReturnType, ModFlags, MethodName,
2447 new Parameters (param_list, null),
2448 OptAttributes, Location.Null);
2450 OperatorMethod.Define (parent);
2451 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
2453 Type [] param_types = OperatorMethod.ParameterTypes (parent);
2454 Type declaring_type = OperatorMethodBuilder.DeclaringType;
2455 Type return_type = OperatorMethod.GetReturnType (parent);
2456 Type first_arg_type = param_types [0];
2458 // Rules for conversion operators
2460 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2462 if (first_arg_type == return_type && first_arg_type == declaring_type)
2463 Report.Error (555, Location,
2464 "User-defined conversion cannot take an object of the enclosing type " +
2465 "and convert to an object of the enclosing type");
2467 if (first_arg_type != declaring_type && return_type != declaring_type)
2468 Report.Error (556, Location,
2469 "User-defined conversion must convert to or from the enclosing type");
2471 if (first_arg_type == TypeManager.object_type || return_type == TypeManager.object_type)
2472 Report.Error (-8, Location,
2473 "User-defined conversion cannot convert to or from object type");
2475 if (first_arg_type.IsInterface || return_type.IsInterface)
2476 Report.Error (-9, Location,
2477 "User-defined conversion cannot convert to or from an interface type");
2479 if (first_arg_type.IsSubclassOf (return_type) || return_type.IsSubclassOf (first_arg_type))
2480 Report.Error (-10, Location,
2481 "User-defined conversion cannot convert between types that " +
2482 "derive from each other");
2484 } else if (SecondArgType == null) {
2485 // Checks for Unary operators
2487 if (first_arg_type != declaring_type)
2488 Report.Error (562, Location,
2489 "The parameter of a unary operator must be the containing type");
2492 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2493 if (return_type != declaring_type)
2494 Report.Error (559, Location,
2495 "The parameter and return type for ++ and -- " +
2496 "must be the containing type");
2500 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2501 if (return_type != TypeManager.bool_type)
2502 Report.Error (215, Location,
2503 "The return type of operator True or False " +
2508 // Checks for Binary operators
2510 if (first_arg_type != declaring_type &&
2511 param_types [1] != declaring_type)
2512 Report.Error (563, Location,
2513 "One of the parameters of a binary operator must be the containing type");
2520 public void Emit (TypeContainer parent)
2522 OperatorMethod.Block = Block;
2523 OperatorMethod.Emit (parent);
2530 // This is used to compare method signatures
2532 struct MethodSignature {
2534 public Type RetType;
2535 public Type [] Parameters;
2537 public MethodSignature (string name, Type ret_type, Type [] parameters)
2541 Parameters = parameters;
2543 if (parameters != null){
2544 if (parameters.Length == 0)
2549 public override int GetHashCode ()
2551 return Name.GetHashCode ();
2554 public override bool Equals (Object o)
2556 MethodSignature other = (MethodSignature) o;
2558 if (other.Name != Name)
2561 if (other.RetType != RetType)
2564 if (Parameters == null){
2565 if (other.Parameters == null)
2570 if (other.Parameters == null)
2573 int c = Parameters.Length;
2574 if (other.Parameters.Length != c)
2577 for (int i = 0; i < c; i++)
2578 if (other.Parameters [i] != Parameters [i])