3 // class.cs: Class and Struct handlers
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Martin Baulig (martin@gnome.org)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Runtime.CompilerServices;
19 using System.Diagnostics.SymbolStore;
21 namespace Mono.CSharp {
24 /// This is the base class for structs and classes.
26 public class TypeContainer : DeclSpace, IMemberContainer {
27 // Holds a list of classes and structures
30 // Holds the list of properties
33 // Holds the list of enumerations
36 // Holds the list of delegates
39 // Holds the list of constructors
40 ArrayList instance_constructors;
42 // Holds the list of fields
45 // Holds a list of fields that have initializers
46 ArrayList initialized_fields;
48 // Holds a list of static fields that have initializers
49 ArrayList initialized_static_fields;
51 // Holds the list of constants
57 // Holds order in which interfaces must be closed
58 ArrayList interface_order;
69 // Holds the operators
72 // The emit context for toplevel objects.
76 // Pointers to the default constructor and the default static constructor
78 Constructor default_constructor;
79 Constructor default_static_constructor;
82 // Whether we have seen a static constructor for this class or not
84 bool have_static_constructor = false;
87 // Whether we have at least one non-static field
89 bool have_nonstatic_fields = false;
92 // This one is computed after we can distinguish interfaces
93 // from classes from the arraylist `type_bases'
95 string base_class_name;
99 // Attributes for this type
100 protected Attributes attributes;
102 // Information in the case we are an attribute type
104 public AttributeTargets Targets = AttributeTargets.All;
105 public bool AllowMultiple = false;
106 public bool Inherited;
108 // The interfaces we implement.
111 // The parent member container and our member cache
112 IMemberContainer parent_container;
113 MemberCache member_cache;
116 // The indexer name for this class
118 public string IndexerName;
120 public TypeContainer (TypeContainer parent, string name, Location l)
121 : base (parent, name, l)
124 types = new ArrayList ();
131 base_class_name = null;
133 //Console.WriteLine ("New class " + name + " inside " + n);
136 public AdditionResult AddConstant (Const constant)
139 string name = constant.Name;
141 if ((res = IsValid (name)) != AdditionResult.Success)
144 if (constants == null)
145 constants = new ArrayList ();
147 constants.Add (constant);
148 DefineName (name, constant);
150 return AdditionResult.Success;
153 public AdditionResult AddEnum (Mono.CSharp.Enum e)
156 string name = e.Name;
158 if ((res = IsValid (name)) != AdditionResult.Success)
162 enums = new ArrayList ();
165 DefineName (name, e);
167 return AdditionResult.Success;
170 public AdditionResult AddClass (Class c)
173 string name = c.Name;
176 if ((res = IsValid (name)) != AdditionResult.Success)
179 DefineName (name, c);
182 return AdditionResult.Success;
185 public AdditionResult AddStruct (Struct s)
188 string name = s.Name;
190 if ((res = IsValid (name)) != AdditionResult.Success)
193 DefineName (name, s);
196 return AdditionResult.Success;
199 public AdditionResult AddDelegate (Delegate d)
202 string name = d.Name;
204 if ((res = IsValid (name)) != AdditionResult.Success)
207 if (delegates == null)
208 delegates = new ArrayList ();
210 DefineName (name, d);
213 return AdditionResult.Success;
216 public AdditionResult AddMethod (Method method)
218 string name = method.Name;
219 Object value = defined_names [name];
221 if (value != null && (!(value is Method)))
222 return AdditionResult.NameExists;
224 if (name == Basename)
225 return AdditionResult.EnclosingClash;
228 methods = new ArrayList ();
230 if (method.Name.IndexOf (".") != -1)
231 methods.Insert (0, method);
233 methods.Add (method);
236 DefineName (name, method);
238 return AdditionResult.Success;
241 public AdditionResult AddConstructor (Constructor c)
243 if (c.Name != Basename)
244 return AdditionResult.NotAConstructor;
246 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
249 have_static_constructor = true;
250 if (default_static_constructor != null){
251 Console.WriteLine ("I have a static constructor already");
252 Console.WriteLine (" " + default_static_constructor);
253 return AdditionResult.MethodExists;
256 default_static_constructor = c;
259 if (default_constructor != null)
260 return AdditionResult.MethodExists;
261 default_constructor = c;
264 if (instance_constructors == null)
265 instance_constructors = new ArrayList ();
267 instance_constructors.Add (c);
270 return AdditionResult.Success;
273 public AdditionResult AddInterface (Interface iface)
276 string name = iface.Name;
278 if ((res = IsValid (name)) != AdditionResult.Success)
281 if (interfaces == null)
282 interfaces = new ArrayList ();
283 interfaces.Add (iface);
284 DefineName (name, iface);
286 return AdditionResult.Success;
289 public AdditionResult AddField (Field field)
292 string name = field.Name;
294 if ((res = IsValid (name)) != AdditionResult.Success)
298 fields = new ArrayList ();
302 if (field.HasInitializer){
303 if ((field.ModFlags & Modifiers.STATIC) != 0){
304 if (initialized_static_fields == null)
305 initialized_static_fields = new ArrayList ();
307 initialized_static_fields.Add (field);
310 // We have not seen a static constructor,
311 // but we will provide static initialization of fields
313 have_static_constructor = true;
315 if (initialized_fields == null)
316 initialized_fields = new ArrayList ();
318 initialized_fields.Add (field);
322 if ((field.ModFlags & Modifiers.STATIC) == 0)
323 have_nonstatic_fields = true;
325 DefineName (name, field);
326 return AdditionResult.Success;
329 public AdditionResult AddProperty (Property prop)
332 string name = prop.Name;
334 if ((res = IsValid (name)) != AdditionResult.Success)
337 if (properties == null)
338 properties = new ArrayList ();
340 if (prop.Name.IndexOf (".") != -1)
341 properties.Insert (0, prop);
343 properties.Add (prop);
344 DefineName (name, prop);
346 return AdditionResult.Success;
349 public AdditionResult AddEvent (Event e)
352 string name = e.Name;
354 if ((res = IsValid (name)) != AdditionResult.Success)
358 events = new ArrayList ();
361 DefineName (name, e);
363 return AdditionResult.Success;
366 public AdditionResult AddIndexer (Indexer i)
368 if (indexers == null)
369 indexers = new ArrayList ();
371 if (i.InterfaceType != null)
372 indexers.Insert (0, i);
376 return AdditionResult.Success;
379 public AdditionResult AddOperator (Operator op)
381 if (operators == null)
382 operators = new ArrayList ();
386 return AdditionResult.Success;
389 public void RegisterOrder (Interface iface)
391 if (interface_order == null)
392 interface_order = new ArrayList ();
394 interface_order.Add (iface);
397 public ArrayList Types {
403 public ArrayList Methods {
409 public ArrayList Constants {
415 public ArrayList Interfaces {
423 return base_class_name;
427 public ArrayList Bases {
437 public ArrayList Fields {
447 public ArrayList InstanceConstructors {
449 return instance_constructors;
453 public ArrayList Properties {
459 public ArrayList Events {
465 public ArrayList Enums {
471 public ArrayList Indexers {
477 public ArrayList Operators {
483 public ArrayList Delegates {
489 public Attributes OptAttributes {
495 public bool HaveStaticConstructor {
497 return have_static_constructor;
501 public virtual TypeAttributes TypeAttr {
503 return Modifiers.TypeAttr (ModFlags, this);
508 // Emits the instance field initializers
510 public bool EmitFieldInitializers (EmitContext ec)
513 ILGenerator ig = ec.ig;
514 Expression instance_expr;
517 fields = initialized_static_fields;
518 instance_expr = null;
520 fields = initialized_fields;
521 instance_expr = new This (Location.Null).Resolve (ec);
527 foreach (Field f in fields){
528 Expression e = f.GetInitializerExpression (ec);
532 Location l = f.Location;
533 FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
534 fe.InstanceExpression = instance_expr;
535 Expression a = new Assign (fe, e, l);
541 if (a is ExpressionStatement)
542 ((ExpressionStatement) a).EmitStatement (ec);
544 throw new Exception ("Assign.Resolve returned a non ExpressionStatement");
552 // Defines the default constructors
554 void DefineDefaultConstructor (bool is_static)
559 c = new Constructor (Basename, Parameters.EmptyReadOnlyParameters,
560 new ConstructorBaseInitializer (
561 null, Parameters.EmptyReadOnlyParameters,
566 mods = Modifiers.STATIC;
572 c.Block = new Block (null);
576 public void ReportStructInitializedInstanceError ()
578 string n = TypeBuilder.FullName;
580 foreach (Field f in initialized_fields){
583 "`" + n + "." + f.Name + "': can not have " +
584 "instance field initializers in structs");
589 /// The pending methods that need to be implemented (interfaces or abstract methods)
591 public PendingImplementation Pending;
594 /// This function computes the Base class and also the
595 /// list of interfaces that the class or struct @c implements.
597 /// The return value is an array (might be null) of
598 /// interfaces implemented (as Types).
600 /// The @parent argument is set to the parent object or null
601 /// if this is `System.Object'.
603 Type [] GetClassBases (bool is_class, out Type parent, out bool error)
605 ArrayList bases = Bases;
614 parent = TypeManager.value_type;
618 if (RootContext.StdLib)
619 parent = TypeManager.object_type;
620 else if (Name != "System.Object")
621 parent = TypeManager.object_type;
624 // If we are compiling our runtime,
625 // and we are defining ValueType, then our
626 // parent is `System.Object'.
628 if (!RootContext.StdLib && Name == "System.ValueType")
629 parent = TypeManager.object_type;
636 // Bases should be null if there are no bases at all
641 Expression name = (Expression) bases [0];
642 name = ResolveTypeExpr (name, false, Location);
649 Type first = name.Type;
655 parent = TypeManager.object_type;
659 if (!AsAccessible (parent, ModFlags))
660 Report.Error (60, Location,
661 "Inconsistent accessibility: base class `" +
662 TypeManager.CSharpName (parent) + "' is less " +
663 "accessible than class `" +
670 Type [] ifaces = new Type [count-start];
672 for (i = start, j = 0; i < count; i++, j++){
673 Expression name = (Expression) bases [i];
674 Expression resolved = ResolveTypeExpr (name, false, Location);
675 bases [i] = resolved;
676 Type t = resolved.Type;
683 if (is_class == false && !t.IsInterface){
684 Report.Error (527, "In Struct `" + Name + "', type `"+
685 name +"' is not an interface");
694 detail = " (a class can not inherit from a struct/enum)";
696 Report.Error (509, "class `"+ Name +
697 "': Cannot inherit from sealed class `"+
698 bases [i]+"'"+detail);
705 Report.Error (527, "In Class `" + Name + "', type `"+
706 name+"' is not an interface");
712 for (int x = 0; x < j; x++) {
713 if (t == ifaces [x]) {
714 Report.Error (528, "`" + name + "' is already listed in interface list");
723 return TypeManager.ExpandInterfaces (ifaces);
727 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
729 public override TypeBuilder DefineType ()
735 if (TypeBuilder != null)
748 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
750 ifaces = GetClassBases (is_class, out parent, out error);
755 if (is_class && parent != null){
756 if (parent == TypeManager.enum_type ||
757 (parent == TypeManager.value_type && RootContext.StdLib) ||
758 parent == TypeManager.delegate_type ||
759 parent == TypeManager.array_type){
761 644, Location, "`" + Name + "' cannot inherit from " +
762 "special class `" + TypeManager.CSharpName (parent) + "'");
767 if (!is_class && TypeManager.value_type == null)
768 throw new Exception ();
770 TypeAttributes type_attributes = TypeAttr;
772 // if (parent_builder is ModuleBuilder) {
774 ModuleBuilder builder = CodeGen.ModuleBuilder;
775 TypeBuilder = builder.DefineType (
776 Name, type_attributes, parent, ifaces);
779 TypeBuilder builder = Parent.TypeBuilder;
780 TypeBuilder = builder.DefineNestedType (
781 Basename, type_attributes, parent, ifaces);
785 // Structs with no fields need to have at least one byte.
786 // The right thing would be to set the PackingSize in a DefineType
787 // but there are no functions that allow interfaces *and* the size to
791 if (!is_class && !have_nonstatic_fields){
792 TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
793 FieldAttributes.Private);
796 // add interfaces that were not added at type creation (weird API issue)
797 if (!is_class && !have_nonstatic_fields && (ifaces != null)) {
798 foreach (Type i in ifaces)
799 TypeBuilder.AddInterfaceImplementation (i);
803 // Finish the setup for the EmitContext
805 ec.ContainerType = TypeBuilder;
807 TypeManager.AddUserType (Name, TypeBuilder, this, ifaces);
809 if ((parent != null) &&
810 (parent == TypeManager.attribute_type ||
811 parent.IsSubclassOf (TypeManager.attribute_type))) {
812 RootContext.RegisterAttribute (this);
813 TypeManager.RegisterAttrType (TypeBuilder, this);
815 RootContext.RegisterOrder (this);
817 if (Interfaces != null) {
818 foreach (Interface iface in Interfaces)
823 foreach (TypeContainer tc in Types)
827 if (Delegates != null) {
828 foreach (Delegate d in Delegates)
833 foreach (Enum en in Enums)
843 /// Defines the MemberCore objects that are in the `list' Arraylist
845 /// The `defined_names' array contains a list of members defined in
848 static ArrayList remove_list = new ArrayList ();
849 void DefineMembers (ArrayList list, MemberInfo [] defined_names)
853 remove_list.Clear ();
855 foreach (MemberCore mc in list){
856 if (!mc.Define (this)){
857 remove_list.Add (mc);
861 if (defined_names == null)
864 idx = Array.BinarySearch (defined_names, mc.Name, mif_compare);
866 if (RootContext.WarningLevel >= 4){
867 if ((mc.ModFlags & Modifiers.NEW) != 0)
868 Warning_KewywordNewNotRequired (mc.Location, mc);
873 MemberInfo match = defined_names [idx];
875 if (match is PropertyInfo && ((mc.ModFlags & Modifiers.OVERRIDE) != 0))
879 // If we are both methods, let the method resolution emit warnings
881 if (match is MethodBase && mc is MethodCore)
884 if ((mc.ModFlags & Modifiers.NEW) == 0)
885 Warning_KeywordNewRequired (mc.Location, defined_names [idx]);
888 foreach (object o in remove_list)
891 remove_list.Clear ();
895 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
896 // class is consisten. Either it is `Item' or it is the name defined by all the
897 // indexers with the `IndexerName' attribute.
899 // Turns out that the IndexerNameAttribute is applied to each indexer,
900 // but it is never emitted, instead a DefaultName attribute is attached
903 void DefineIndexers ()
905 string class_indexer_name = null;
907 foreach (Indexer i in Indexers){
912 name = i.IndexerName;
914 if (i.InterfaceType != null)
917 if (class_indexer_name == null){
918 class_indexer_name = name;
922 if (name == class_indexer_name)
926 668, "Two indexers have different names, " +
927 " you should use the same name for all your indexers");
929 if (class_indexer_name == null)
930 class_indexer_name = "Item";
931 IndexerName = class_indexer_name;
934 static void Error_KeywordNotAllowed (Location loc)
936 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
940 /// Populates our TypeBuilder with fields and methods
942 public override bool DefineMembers (TypeContainer parent)
944 MemberInfo [] defined_names = null;
946 if (interface_order != null){
947 foreach (Interface iface in interface_order)
948 if ((iface.ModFlags & Modifiers.NEW) == 0)
949 iface.DefineMembers (this);
951 Error_KeywordNotAllowed (iface.Location);
954 if (RootContext.WarningLevel > 1){
958 // This code throws an exception in the comparer
959 // I guess the string is not an object?
961 ptype = TypeBuilder.BaseType;
963 defined_names = (MemberInfo []) FindMembers (
964 ptype, MemberTypes.All & ~MemberTypes.Constructor,
965 BindingFlags.Public | BindingFlags.Instance |
966 BindingFlags.Static, null, null);
968 Array.Sort (defined_names, mif_compare);
972 if (constants != null)
973 DefineMembers (constants, defined_names);
976 DefineMembers (fields, defined_names);
979 if (instance_constructors == null){
980 if (default_constructor == null)
981 DefineDefaultConstructor (false);
984 if (initialized_static_fields != null &&
985 default_static_constructor == null)
986 DefineDefaultConstructor (true);
991 // Structs can not have initialized instance
994 if (initialized_static_fields != null &&
995 default_static_constructor == null)
996 DefineDefaultConstructor (true);
998 if (initialized_fields != null)
999 ReportStructInitializedInstanceError ();
1002 Pending = PendingImplementation.GetPendingImplementations (this);
1005 // Constructors are not in the defined_names array
1007 if (instance_constructors != null)
1008 DefineMembers (instance_constructors, null);
1010 if (default_static_constructor != null)
1011 default_static_constructor.Define (this);
1013 if (methods != null)
1014 DefineMembers (methods, defined_names);
1016 if (properties != null)
1017 DefineMembers (properties, defined_names);
1020 DefineMembers (events, defined_names);
1022 if (indexers != null) {
1025 IndexerName = "Item";
1027 if (operators != null){
1028 DefineMembers (operators, null);
1030 CheckPairedOperators ();
1034 DefineMembers (enums, defined_names);
1036 if (delegates != null)
1037 DefineMembers (delegates, defined_names);
1040 if (TypeBuilder.BaseType != null)
1041 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
1043 member_cache = new MemberCache (this);
1049 public override bool Define (TypeContainer parent)
1051 if (interface_order != null){
1052 foreach (Interface iface in interface_order)
1053 if ((iface.ModFlags & Modifiers.NEW) == 0)
1054 iface.Define (this);
1061 /// This function is based by a delegate to the FindMembers routine
1063 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1069 /// This filter is used by FindMembers, and we just keep
1070 /// a global for the filter to `AlwaysAccept'
1072 static MemberFilter accepting_filter;
1076 /// A member comparission method based on name only
1078 static IComparer mif_compare;
1080 static TypeContainer ()
1082 accepting_filter = new MemberFilter (AlwaysAccept);
1083 mif_compare = new MemberInfoCompare ();
1087 /// This method returns the members of this type just like Type.FindMembers would
1088 /// Only, we need to use this for types which are _being_ defined because MS'
1089 /// implementation can't take care of that.
1092 // FIXME: return an empty static array instead of null, that cleans up
1093 // some code and is consistent with some coding conventions I just found
1097 // Notice that in various cases we check if our field is non-null,
1098 // something that would normally mean that there was a bug elsewhere.
1100 // The problem happens while we are defining p-invoke methods, as those
1101 // will trigger a FindMembers, but this happens before things are defined
1103 // Since the whole process is a no-op, it is fine to check for null here.
1105 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1106 MemberFilter filter, object criteria)
1108 ArrayList members = new ArrayList ();
1111 if ((bf & BindingFlags.Public) != 0)
1112 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1114 if ((bf & BindingFlags.NonPublic) != 0)
1115 modflags |= Modifiers.PRIVATE;
1117 int static_mask = 0, static_flags = 0;
1118 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1119 case BindingFlags.Static:
1120 static_mask = static_flags = Modifiers.STATIC;
1123 case BindingFlags.Instance:
1124 static_mask = Modifiers.STATIC;
1129 static_mask = static_flags = 0;
1133 Timer.StartTimer (TimerType.TcFindMembers);
1136 filter = accepting_filter;
1138 if ((mt & MemberTypes.Field) != 0) {
1139 if (fields != null) {
1140 foreach (Field f in fields) {
1141 if ((f.ModFlags & modflags) == 0)
1143 if ((f.ModFlags & static_mask) != static_flags)
1146 FieldBuilder fb = f.FieldBuilder;
1147 if (fb != null && filter (fb, criteria) == true)
1152 if (constants != null) {
1153 foreach (Const con in constants) {
1154 if ((con.ModFlags & modflags) == 0)
1156 if ((con.ModFlags & static_mask) != static_flags)
1159 FieldBuilder fb = con.FieldBuilder;
1160 if (fb != null && filter (fb, criteria) == true)
1166 if ((mt & MemberTypes.Method) != 0) {
1167 if (methods != null) {
1168 foreach (Method m in methods) {
1169 if ((m.ModFlags & modflags) == 0)
1171 if ((m.ModFlags & static_mask) != static_flags)
1174 MethodBuilder mb = m.MethodBuilder;
1176 if (mb != null && filter (mb, criteria) == true)
1181 if (operators != null){
1182 foreach (Operator o in operators) {
1183 if ((o.ModFlags & modflags) == 0)
1185 if ((o.ModFlags & static_mask) != static_flags)
1188 MethodBuilder ob = o.OperatorMethodBuilder;
1189 if (ob != null && filter (ob, criteria) == true)
1194 if (properties != null){
1195 foreach (Property p in properties){
1196 if ((p.ModFlags & modflags) == 0)
1198 if ((p.ModFlags & static_mask) != static_flags)
1204 if (b != null && filter (b, criteria) == true)
1208 if (b != null && filter (b, criteria) == true)
1213 if (indexers != null){
1214 foreach (Indexer ix in indexers){
1215 if ((ix.ModFlags & modflags) == 0)
1217 if ((ix.ModFlags & static_mask) != static_flags)
1223 if (b != null && filter (b, criteria) == true)
1227 if (b != null && filter (b, criteria) == true)
1233 if ((mt & MemberTypes.Event) != 0) {
1235 foreach (Event e in events) {
1236 if ((e.ModFlags & modflags) == 0)
1238 if ((e.ModFlags & static_mask) != static_flags)
1241 MemberInfo eb = e.EventBuilder;
1242 if (eb != null && filter (eb, criteria) == true)
1243 members.Add (e.EventBuilder);
1247 if ((mt & MemberTypes.Property) != 0){
1248 if (properties != null)
1249 foreach (Property p in properties) {
1250 if ((p.ModFlags & modflags) == 0)
1252 if ((p.ModFlags & static_mask) != static_flags)
1255 MemberInfo pb = p.PropertyBuilder;
1256 if (pb != null && filter (pb, criteria) == true) {
1257 members.Add (p.PropertyBuilder);
1261 if (indexers != null)
1262 foreach (Indexer ix in indexers) {
1263 if ((ix.ModFlags & modflags) == 0)
1265 if ((ix.ModFlags & static_mask) != static_flags)
1268 MemberInfo ib = ix.PropertyBuilder;
1269 if (ib != null && filter (ib, criteria) == true) {
1270 members.Add (ix.PropertyBuilder);
1275 if ((mt & MemberTypes.NestedType) != 0) {
1277 foreach (TypeContainer t in types) {
1278 if ((t.ModFlags & modflags) == 0)
1281 TypeBuilder tb = t.TypeBuilder;
1282 if (tb != null && (filter (tb, criteria) == true))
1288 foreach (Enum en in enums){
1289 if ((en.ModFlags & modflags) == 0)
1292 TypeBuilder tb = en.TypeBuilder;
1293 if (tb != null && (filter (tb, criteria) == true))
1298 if (delegates != null){
1299 foreach (Delegate d in delegates){
1300 if ((d.ModFlags & modflags) == 0)
1303 TypeBuilder tb = d.TypeBuilder;
1304 if (tb != null && (filter (tb, criteria) == true))
1309 if (interfaces != null){
1310 foreach (Interface iface in interfaces){
1311 if ((iface.ModFlags & modflags) == 0)
1314 TypeBuilder tb = iface.TypeBuilder;
1315 if (tb != null && (filter (tb, criteria) == true))
1321 if ((mt & MemberTypes.Constructor) != 0){
1322 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1323 foreach (Constructor c in instance_constructors){
1324 ConstructorBuilder cb = c.ConstructorBuilder;
1326 if (filter (cb, criteria) == true)
1331 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1332 ConstructorBuilder cb =
1333 default_static_constructor.ConstructorBuilder;
1336 if (filter (cb, criteria) == true)
1342 // Lookup members in parent if requested.
1344 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
1345 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1346 members.AddRange (list);
1349 Timer.StopTimer (TimerType.TcFindMembers);
1351 return new MemberList (members);
1354 public override MemberCache MemberCache {
1356 return member_cache;
1360 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1361 MemberFilter filter, object criteria)
1363 TypeContainer tc = TypeManager.LookupTypeContainer (t);
1366 return tc.FindMembers (mt, bf, filter, criteria);
1368 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1372 // FindMethods will look for methods not only in the type `t', but in
1373 // any interfaces implemented by the type.
1375 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
1376 MemberFilter filter, object criteria)
1382 /// Emits the values for the constants
1384 public void EmitConstants ()
1386 if (constants != null)
1387 foreach (Const con in constants)
1388 con.EmitConstant (this);
1393 /// Emits the code, this step is performed after all
1394 /// the types, enumerations, constructors
1398 if (instance_constructors != null)
1399 foreach (Constructor c in instance_constructors)
1402 if (default_static_constructor != null)
1403 default_static_constructor.Emit (this);
1405 if (methods != null)
1406 foreach (Method m in methods)
1409 if (operators != null)
1410 foreach (Operator o in operators)
1413 if (properties != null)
1414 foreach (Property p in properties)
1417 if (indexers != null){
1418 foreach (Indexer ix in indexers)
1421 CustomAttributeBuilder cb = Interface.EmitDefaultMemberAttr (
1422 this, IndexerName, ModFlags, Location);
1423 TypeBuilder.SetCustomAttribute (cb);
1427 foreach (Field f in fields)
1430 if (events != null){
1431 foreach (Event e in Events)
1435 if (Pending != null)
1436 if (Pending.VerifyPendingMethods ())
1439 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
1442 // Check for internal or private fields that were never assigned
1444 if (fields != null && RootContext.WarningLevel >= 3) {
1445 foreach (Field f in fields) {
1446 if ((f.ModFlags & Modifiers.PUBLIC) != 0)
1451 169, f.Location, "Private field " +
1452 MakeName (f.Name) + " is never used");
1457 // Only report 649 on level 4
1459 if (RootContext.WarningLevel < 4)
1462 if ((f.status & Field.Status.ASSIGNED) != 0)
1467 "Field " + MakeName (f.Name) + " is never assigned " +
1468 " to and will always have its default value");
1472 // if (types != null)
1473 // foreach (TypeContainer tc in types)
1477 public override void CloseType ()
1482 TypeBuilder.CreateType ();
1484 } catch (TypeLoadException){
1486 // This is fine, the code still created the type
1488 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
1489 // Console.WriteLine (e.Message);
1491 Console.WriteLine ("In type: " + Name);
1496 foreach (Enum en in Enums)
1499 if (interface_order != null){
1500 foreach (Interface iface in interface_order)
1505 foreach (TypeContainer tc in Types)
1509 foreach (TypeContainer tc in Types)
1510 if (!(tc is Struct))
1514 if (Delegates != null)
1515 foreach (Delegate d in Delegates)
1519 public string MakeName (string n)
1521 return "`" + Name + "." + n + "'";
1524 public void Warning_KeywordNewRequired (Location l, MemberInfo mi)
1527 108, l, "The keyword new is required on " +
1528 MakeName (mi.Name) + " because it hides `" +
1529 mi.ReflectedType.Name + "." + mi.Name + "'");
1532 public void Warning_KewywordNewNotRequired (Location l, MemberCore mc)
1535 109, l, "The member " + MakeName (mc.Name) + " does not hide an " +
1536 "inherited member, the keyword new is not required");
1539 public static int CheckMember (string name, MemberInfo mi, int ModFlags)
1545 // Performs the validation on a Method's modifiers (properties have
1546 // the same properties).
1548 public bool MethodModifiersValid (int flags, string n, Location loc)
1550 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
1551 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
1552 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
1554 string name = MakeName (n);
1557 // At most one of static, virtual or override
1559 if ((flags & Modifiers.STATIC) != 0){
1560 if ((flags & vao) != 0){
1562 112, loc, "static method " + name + "can not be marked " +
1563 "as virtual, abstract or override");
1568 if (this is Struct){
1569 if ((flags & va) != 0){
1570 Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
1575 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
1578 " marked as override cannot be marked as new or virtual");
1583 // If the declaration includes the abstract modifier, then the
1584 // declaration does not include static, virtual or extern
1586 if ((flags & Modifiers.ABSTRACT) != 0){
1587 if ((flags & Modifiers.EXTERN) != 0){
1589 180, loc, name + " can not be both abstract and extern");
1593 if ((flags & Modifiers.VIRTUAL) != 0){
1595 503, loc, name + " can not be both abstract and virtual");
1599 if ((ModFlags & Modifiers.ABSTRACT) == 0){
1602 " is abstract but its container class is not");
1608 if ((flags & Modifiers.PRIVATE) != 0){
1609 if ((flags & vao) != 0){
1612 " virtual or abstract members can not be private");
1617 if ((flags & Modifiers.SEALED) != 0){
1618 if ((flags & Modifiers.OVERRIDE) == 0){
1621 " cannot be sealed because it is not an override");
1629 // Access level of a type.
1632 ProtectedInternal = 1,
1638 // Check whether `flags' denotes a more restricted access than `level'
1639 // and return the new level.
1640 static AccessLevel CheckAccessLevel (AccessLevel level, int flags)
1642 AccessLevel old_level = level;
1644 if ((flags & Modifiers.INTERNAL) != 0) {
1645 if ((flags & Modifiers.PROTECTED) != 0) {
1646 if ((int) level < (int) AccessLevel.ProtectedInternal)
1647 level = AccessLevel.ProtectedInternal;
1649 if ((int) level < (int) AccessLevel.Internal)
1650 level = AccessLevel.Internal;
1652 } else if ((flags & Modifiers.PROTECTED) != 0) {
1653 if ((int) level < (int) AccessLevel.Protected)
1654 level = AccessLevel.Protected;
1655 } else if ((flags & Modifiers.PRIVATE) != 0)
1656 level = AccessLevel.Private;
1661 // Return the access level for a new member which is defined in the current
1662 // TypeContainer with access modifiers `flags'.
1663 AccessLevel GetAccessLevel (int flags)
1665 if ((flags & Modifiers.PRIVATE) != 0)
1666 return AccessLevel.Private;
1669 if (!IsTopLevel && (Parent != null))
1670 level = Parent.GetAccessLevel (flags);
1672 level = AccessLevel.Public;
1674 return CheckAccessLevel (CheckAccessLevel (level, flags), ModFlags);
1677 // Return the access level for type `t', but don't give more access than `flags'.
1678 static AccessLevel GetAccessLevel (Type t, int flags)
1680 if (((flags & Modifiers.PRIVATE) != 0) || t.IsNestedPrivate)
1681 return AccessLevel.Private;
1684 if (TypeManager.IsBuiltinType (t))
1685 return AccessLevel.Public;
1686 else if ((t.DeclaringType != null) && (t != t.DeclaringType))
1687 level = GetAccessLevel (t.DeclaringType, flags);
1689 level = CheckAccessLevel (AccessLevel.Public, flags);
1692 if (t.IsNestedPublic)
1695 if (t.IsNestedAssembly || t.IsNotPublic) {
1696 if ((int) level < (int) AccessLevel.Internal)
1697 level = AccessLevel.Internal;
1700 if (t.IsNestedFamily) {
1701 if ((int) level < (int) AccessLevel.Protected)
1702 level = AccessLevel.Protected;
1705 if (t.IsNestedFamORAssem) {
1706 if ((int) level < (int) AccessLevel.ProtectedInternal)
1707 level = AccessLevel.ProtectedInternal;
1714 // Returns true if `parent' is as accessible as the flags `flags'
1715 // given for this member.
1717 public bool AsAccessible (Type parent, int flags)
1719 while (parent.IsArray || parent.IsPointer || parent.IsByRef)
1720 parent = parent.GetElementType ();
1722 AccessLevel level = GetAccessLevel (flags);
1723 AccessLevel level2 = GetAccessLevel (parent, flags);
1725 return (int) level >= (int) level2;
1728 Hashtable builder_and_args;
1730 public bool RegisterMethod (MethodBuilder mb, InternalParameters ip, Type [] args)
1732 if (builder_and_args == null)
1733 builder_and_args = new Hashtable ();
1738 /// Performs checks for an explicit interface implementation. First it
1739 /// checks whether the `interface_type' is a base inteface implementation.
1740 /// Then it checks whether `name' exists in the interface type.
1742 public bool VerifyImplements (Type interface_type, string full, string name, Location loc)
1746 if (ifaces != null){
1747 foreach (Type t in ifaces){
1748 if (t == interface_type){
1756 Report.Error (540, "`" + full + "': containing class does not implement interface `" + interface_type.FullName + "'");
1763 public static void Error_ExplicitInterfaceNotMemberInterface (Location loc, string name)
1765 Report.Error (539, loc, "Explicit implementation: `" + name + "' is not a member of the interface");
1772 string IMemberContainer.Name {
1778 Type IMemberContainer.Type {
1784 IMemberContainer IMemberContainer.Parent {
1786 return parent_container;
1790 MemberCache IMemberContainer.MemberCache {
1792 return member_cache;
1796 bool IMemberContainer.IsInterface {
1802 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
1804 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
1808 // Operator pair checking
1811 class OperatorEntry {
1813 public Type ret_type;
1814 public Type type1, type2;
1816 public Operator.OpType ot;
1818 public OperatorEntry (int f, Operator o)
1822 ret_type = o.OperatorMethod.GetReturnType ();
1823 Type [] pt = o.OperatorMethod.ParameterTypes;
1827 ot = o.OperatorType;
1830 public override int GetHashCode ()
1832 return ret_type.GetHashCode ();
1835 public override bool Equals (object o)
1837 OperatorEntry other = (OperatorEntry) o;
1839 if (other.ret_type != ret_type)
1841 if (other.type1 != type1)
1843 if (other.type2 != type2)
1850 // Checks that some operators come in pairs:
1855 // They are matched based on the return type and the argument types
1857 void CheckPairedOperators ()
1859 Hashtable pairs = new Hashtable (null, null);
1861 // Register all the operators we care about.
1862 foreach (Operator op in operators){
1865 switch (op.OperatorType){
1866 case Operator.OpType.Equality:
1868 case Operator.OpType.Inequality:
1871 case Operator.OpType.GreaterThan:
1873 case Operator.OpType.LessThan:
1876 case Operator.OpType.GreaterThanOrEqual:
1878 case Operator.OpType.LessThanOrEqual:
1884 OperatorEntry oe = new OperatorEntry (reg, op);
1886 object o = pairs [oe];
1890 oe = (OperatorEntry) o;
1896 // Look for the mistakes.
1898 foreach (DictionaryEntry de in pairs){
1899 OperatorEntry oe = (OperatorEntry) de.Key;
1906 case Operator.OpType.Equality:
1909 case Operator.OpType.Inequality:
1912 case Operator.OpType.GreaterThan:
1915 case Operator.OpType.LessThan:
1918 case Operator.OpType.GreaterThanOrEqual:
1921 case Operator.OpType.LessThanOrEqual:
1925 Report.Error (216, oe.op.Location,
1926 "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
1933 public class Class : TypeContainer {
1935 // Modifiers allowed in a class declaration
1937 public const int AllowedModifiers =
1940 Modifiers.PROTECTED |
1941 Modifiers.INTERNAL |
1943 Modifiers.ABSTRACT |
1947 public Class (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
1948 : base (parent, name, l)
1952 if (parent.Parent == null)
1953 accmods = Modifiers.INTERNAL;
1955 accmods = Modifiers.PRIVATE;
1957 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
1958 this.attributes = attrs;
1962 // FIXME: How do we deal with the user specifying a different
1965 public override TypeAttributes TypeAttr {
1967 return base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
1972 public class Struct : TypeContainer {
1974 // Modifiers allowed in a struct declaration
1976 public const int AllowedModifiers =
1979 Modifiers.PROTECTED |
1980 Modifiers.INTERNAL |
1984 public Struct (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
1985 : base (parent, name, l)
1989 if (parent.Parent == null)
1990 accmods = Modifiers.INTERNAL;
1992 accmods = Modifiers.PRIVATE;
1994 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
1996 this.ModFlags |= Modifiers.SEALED;
1997 this.attributes = attrs;
2002 // FIXME: Allow the user to specify a different set of attributes
2003 // in some cases (Sealed for example is mandatory for a class,
2004 // but what SequentialLayout can be changed
2006 public override TypeAttributes TypeAttr {
2008 return base.TypeAttr |
2009 TypeAttributes.SequentialLayout |
2010 TypeAttributes.Sealed |
2011 TypeAttributes.BeforeFieldInit;
2016 public abstract class MethodCore : MemberBase {
2017 public readonly Parameters Parameters;
2021 // Parameters, cached for semantic analysis.
2023 protected InternalParameters parameter_info;
2024 protected Type [] parameter_types;
2026 public MethodCore (Expression type, int mod, int allowed_mod, string name,
2027 Attributes attrs, Parameters parameters, Location loc)
2028 : base (type, mod, allowed_mod, name, attrs, loc)
2030 Parameters = parameters;
2034 // Returns the System.Type array for the parameters of this method
2036 public Type [] ParameterTypes {
2038 return parameter_types;
2042 public InternalParameters ParameterInfo
2045 return parameter_info;
2049 public Block Block {
2059 protected virtual bool DoDefineParameters (TypeContainer parent)
2061 // Check if arguments were correct
2062 parameter_types = Parameters.GetParameterInfo (parent);
2063 if ((parameter_types == null) || !CheckParameters (parent, parameter_types))
2066 parameter_info = new InternalParameters (parent, Parameters);
2071 public CallingConventions GetCallingConvention (bool is_class)
2073 CallingConventions cc = 0;
2075 cc = Parameters.GetCallingConvention ();
2078 if ((ModFlags & Modifiers.STATIC) == 0)
2079 cc |= CallingConventions.HasThis;
2081 // FIXME: How is `ExplicitThis' used in C#?
2086 public void LabelParameters (EmitContext ec, Type [] parameters, MethodBase builder)
2089 // Define each type attribute (in/out/ref) and
2090 // the argument names.
2092 Parameter [] p = Parameters.FixedParameters;
2095 MethodBuilder mb = null;
2096 ConstructorBuilder cb = null;
2098 if (builder is MethodBuilder)
2099 mb = (MethodBuilder) builder;
2101 cb = (ConstructorBuilder) builder;
2104 for (i = 0; i < p.Length; i++) {
2105 ParameterBuilder pb;
2108 pb = cb.DefineParameter (
2109 i + 1, p [i].Attributes, p [i].Name);
2111 pb = mb.DefineParameter (
2112 i + 1, p [i].Attributes, p [i].Name);
2114 Attributes attr = p [i].OptAttributes;
2116 Attribute.ApplyAttributes (ec, pb, pb, attr, Location);
2120 if (Parameters.ArrayParameter != null){
2121 ParameterBuilder pb;
2122 Parameter array_param = Parameters.ArrayParameter;
2125 pb = cb.DefineParameter (
2126 i + 1, array_param.Attributes,
2129 pb = mb.DefineParameter (
2130 i + 1, array_param.Attributes,
2133 CustomAttributeBuilder a = new CustomAttributeBuilder (
2134 TypeManager.cons_param_array_attribute, new object [0]);
2136 pb.SetCustomAttribute (a);
2141 public class Method : MethodCore {
2142 public MethodBuilder MethodBuilder;
2143 public MethodData MethodData;
2146 /// Modifiers allowed in a class declaration
2148 const int AllowedModifiers =
2151 Modifiers.PROTECTED |
2152 Modifiers.INTERNAL |
2157 Modifiers.OVERRIDE |
2158 Modifiers.ABSTRACT |
2163 // return_type can be "null" for VOID values.
2165 public Method (Expression return_type, int mod, string name, Parameters parameters,
2166 Attributes attrs, Location l)
2167 : base (return_type, mod, AllowedModifiers, name, attrs, parameters, l)
2171 // Returns the `System.Type' for the ReturnType of this
2172 // function. Provides a nice cache. (used between semantic analysis
2173 // and actual code generation
2175 public Type GetReturnType ()
2180 // Whether this is an operator method.
2181 public bool IsOperator;
2183 void DuplicateEntryPoint (MethodInfo b, Location location)
2187 "Program `" + CodeGen.FileName +
2188 "' has more than one entry point defined: `" +
2189 TypeManager.CSharpSignature(b) + "'");
2192 void Report28 (MethodInfo b)
2194 if (RootContext.WarningLevel < 4)
2199 "`" + TypeManager.CSharpSignature(b) +
2200 "' has the wrong signature to be an entry point");
2203 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
2205 if (b.ReturnType != TypeManager.void_type &&
2206 b.ReturnType != TypeManager.int32_type)
2209 if (pinfo.Count == 0)
2212 if (pinfo.Count > 1)
2215 Type t = pinfo.ParameterType(0);
2217 (t.GetArrayRank() == 1) &&
2218 (t.GetElementType() == TypeManager.string_type) &&
2219 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
2226 // Checks our base implementation if any
2228 protected override bool CheckBase (TypeContainer parent)
2230 // Check whether arguments were correct.
2231 if (!DoDefineParameters (parent))
2234 MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
2238 mi_this = TypeContainer.FindMembers (
2239 parent.TypeBuilder, MemberTypes.Method,
2240 BindingFlags.NonPublic | BindingFlags.Public |
2241 BindingFlags.Static | BindingFlags.Instance |
2242 BindingFlags.DeclaredOnly,
2243 MethodSignature.method_signature_filter, ms);
2245 if (mi_this.Count > 0) {
2246 Report.Error (111, Location, "Class `" + parent.Name + "' " +
2247 "already defines a member called `" + Name + "' " +
2248 "with the same parameter types");
2254 // Verify if the parent has a type with the same name, and then
2255 // check whether we have to create a new slot for it or not.
2257 Type ptype = parent.TypeBuilder.BaseType;
2259 // ptype is only null for System.Object while compiling corlib.
2261 MemberList mi, mi_static, mi_instance;
2263 mi_static = TypeContainer.FindMembers (
2264 ptype, MemberTypes.Method,
2265 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
2266 MethodSignature.inheritable_method_signature_filter, ms);
2268 mi_instance = TypeContainer.FindMembers (
2269 ptype, MemberTypes.Method,
2270 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
2271 MethodSignature.inheritable_method_signature_filter,
2274 if (mi_instance.Count > 0){
2276 } else if (mi_static.Count > 0)
2281 if (mi != null && mi.Count > 0){
2282 parent_method = (MethodInfo) mi [0];
2283 string name = parent_method.DeclaringType.Name + "." +
2286 if (!CheckMethodAgainstBase (parent, flags, parent_method, name))
2289 if ((ModFlags & Modifiers.NEW) == 0) {
2290 Type parent_ret = TypeManager.TypeToCoreType (
2291 parent_method.ReturnType);
2293 if (parent_ret != MemberType) {
2295 508, parent.MakeName (Name) + ": cannot " +
2296 "change return type when overriding " +
2297 "inherited member " + name);
2302 if ((ModFlags & Modifiers.NEW) != 0)
2303 WarningNotHiding (parent);
2305 if ((ModFlags & Modifiers.OVERRIDE) != 0){
2306 Report.Error (115, Location,
2307 parent.MakeName (Name) +
2308 " no suitable methods found to override");
2311 } else if ((ModFlags & Modifiers.NEW) != 0)
2312 WarningNotHiding (parent);
2320 public override bool Define (TypeContainer parent)
2322 if (!DoDefine (parent))
2325 if (!CheckBase (parent))
2328 CallingConventions cc = GetCallingConvention (parent is Class);
2330 MethodData = new MethodData (this, null, MemberType, ParameterTypes,
2331 ParameterInfo, cc, OptAttributes,
2332 ModFlags, flags, true);
2334 if (!MethodData.Define (parent))
2337 MethodBuilder = MethodData.MethodBuilder;
2340 // This is used to track the Entry Point,
2342 if (Name == "Main" &&
2343 ((ModFlags & Modifiers.STATIC) != 0) &&
2344 (RootContext.MainClass == null ||
2345 RootContext.MainClass == parent.TypeBuilder.FullName)){
2346 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
2347 if (RootContext.EntryPoint == null) {
2348 RootContext.EntryPoint = MethodBuilder;
2349 RootContext.EntryPointLocation = Location;
2351 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
2352 DuplicateEntryPoint (MethodBuilder, Location);
2355 Report28(MethodBuilder);
2364 public void Emit (TypeContainer parent)
2366 MethodData.Emit (parent, Block, this);
2370 public abstract class ConstructorInitializer {
2371 ArrayList argument_list;
2372 ConstructorInfo parent_constructor;
2373 Parameters parameters;
2376 public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
2379 this.argument_list = argument_list;
2380 this.parameters = parameters;
2384 public ArrayList Arguments {
2386 return argument_list;
2390 public bool Resolve (EmitContext ec)
2392 Expression parent_constructor_group;
2395 ec.CurrentBlock = new Block (null, true, parameters);
2397 if (argument_list != null){
2398 foreach (Argument a in argument_list){
2399 if (!a.Resolve (ec, loc))
2404 ec.CurrentBlock = null;
2406 if (this is ConstructorBaseInitializer) {
2407 if (ec.ContainerType.BaseType == null)
2410 t = ec.ContainerType.BaseType;
2411 if (ec.ContainerType.IsValueType) {
2412 Report.Error (522, loc,
2413 "structs cannot call base class constructors");
2417 t = ec.ContainerType;
2419 parent_constructor_group = Expression.MemberLookup (
2421 MemberTypes.Constructor,
2422 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
2425 if (parent_constructor_group == null){
2426 Report.Error (1501, loc,
2427 "Can not find a constructor for this argument list");
2431 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec,
2432 (MethodGroupExpr) parent_constructor_group, argument_list, loc);
2434 if (parent_constructor == null){
2435 Report.Error (1501, loc,
2436 "Can not find a constructor for this argument list");
2443 public void Emit (EmitContext ec)
2445 if (parent_constructor != null)
2446 ec.ig.Emit (OpCodes.Ldarg_0);
2447 if (argument_list != null)
2448 Invocation.EmitArguments (ec, null, argument_list);
2449 if (parent_constructor != null)
2450 ec.ig.Emit (OpCodes.Call, parent_constructor);
2454 public class ConstructorBaseInitializer : ConstructorInitializer {
2455 public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
2456 base (argument_list, pars, l)
2461 public class ConstructorThisInitializer : ConstructorInitializer {
2462 public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
2463 base (argument_list, pars, l)
2468 public class Constructor : MethodCore {
2469 public ConstructorBuilder ConstructorBuilder;
2470 public ConstructorInitializer Initializer;
2471 new public Attributes OptAttributes;
2474 // Modifiers allowed for a constructor.
2476 const int AllowedModifiers =
2478 Modifiers.PROTECTED |
2479 Modifiers.INTERNAL |
2485 // The spec claims that static is not permitted, but
2486 // my very own code has static constructors.
2488 public Constructor (string name, Parameters args, ConstructorInitializer init, Location l)
2489 : base (null, 0, AllowedModifiers, name, null, args, l)
2495 // Returns true if this is a default constructor
2497 public bool IsDefault ()
2499 if ((ModFlags & Modifiers.STATIC) != 0)
2500 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
2501 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
2504 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
2505 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
2506 (Initializer is ConstructorBaseInitializer) &&
2507 (Initializer.Arguments == null);
2511 // Creates the ConstructorBuilder
2513 public override bool Define (TypeContainer parent)
2515 MethodAttributes ca = (MethodAttributes.RTSpecialName |
2516 MethodAttributes.SpecialName);
2518 // Check if arguments were correct.
2519 if (!DoDefineParameters (parent))
2522 if ((ModFlags & Modifiers.STATIC) != 0)
2523 ca |= MethodAttributes.Static;
2525 if (parent is Struct && ParameterTypes.Length == 0){
2528 "Structs can not contain explicit parameterless " +
2532 ca |= MethodAttributes.HideBySig;
2534 if ((ModFlags & Modifiers.PRIVATE) != 0)
2535 ca |= MethodAttributes.Private;
2536 else if ((ModFlags & Modifiers.PROTECTED) != 0){
2537 if ((ModFlags & Modifiers.INTERNAL) != 0)
2538 ca |= MethodAttributes.FamORAssem;
2540 ca |= MethodAttributes.Family;
2541 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
2542 ca |= MethodAttributes.Assembly;
2544 ca |= MethodAttributes.Public;
2547 ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
2548 ca, GetCallingConvention (parent is Class), ParameterTypes);
2551 // HACK because System.Reflection.Emit is lame
2553 if (!TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes)) {
2556 "Class `" +parent.Name+ "' already contains a definition with the " +
2557 "same return value and parameter types for constructor `" + Name
2568 public void Emit (TypeContainer parent)
2570 ILGenerator ig = ConstructorBuilder.GetILGenerator ();
2571 EmitContext ec = new EmitContext (parent, Location, ig, null, ModFlags, true);
2573 if ((ModFlags & Modifiers.STATIC) == 0){
2574 if (parent is Class && Initializer == null)
2575 Initializer = new ConstructorBaseInitializer (
2576 null, Parameters.EmptyReadOnlyParameters, parent.Location);
2580 // Spec mandates that Initializers will not have
2584 if (Initializer != null && !Initializer.Resolve (ec))
2586 ec.IsStatic = false;
2589 LabelParameters (ec, ParameterTypes, ConstructorBuilder);
2592 // Classes can have base initializers and instance field initializers.
2594 if (parent is Class){
2595 if ((ModFlags & Modifiers.STATIC) == 0)
2596 parent.EmitFieldInitializers (ec);
2598 if (Initializer != null)
2599 Initializer.Emit (ec);
2601 if ((ModFlags & Modifiers.STATIC) != 0)
2602 parent.EmitFieldInitializers (ec);
2604 Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes, Location);
2606 // If this is a non-static `struct' constructor and doesn't have any
2607 // initializer, it must initialize all of the struct's fields.
2608 if ((parent is Struct) && ((ModFlags & Modifiers.STATIC) == 0) &&
2609 (Initializer == null))
2610 Block.AddThisVariable (parent, Location);
2612 ec.EmitTopBlock (Block, ParameterInfo, Location);
2616 public class MethodData {
2618 // The return type of this method
2620 public readonly Type ReturnType;
2621 public readonly Type[] ParameterTypes;
2622 public readonly InternalParameters ParameterInfo;
2623 public readonly CallingConventions CallingConventions;
2624 public readonly Attributes OptAttributes;
2625 public readonly Location Location;
2628 // Are we implementing an interface ?
2630 public bool IsImplementing = false;
2635 protected MemberBase member;
2636 protected int modifiers;
2637 protected MethodAttributes flags;
2638 protected bool is_method;
2639 protected string accessor_name;
2640 ArrayList conditionals;
2642 MethodBuilder builder = null;
2643 public MethodBuilder MethodBuilder {
2649 public MethodData (MemberBase member, string name, Type return_type,
2650 Type [] parameter_types, InternalParameters parameters,
2651 CallingConventions cc, Attributes opt_attrs,
2652 int modifiers, MethodAttributes flags, bool is_method)
2654 this.member = member;
2655 this.accessor_name = name;
2656 this.ReturnType = return_type;
2657 this.ParameterTypes = parameter_types;
2658 this.ParameterInfo = parameters;
2659 this.CallingConventions = cc;
2660 this.OptAttributes = opt_attrs;
2661 this.modifiers = modifiers;
2663 this.is_method = is_method;
2664 this.Location = member.Location;
2665 this.conditionals = new ArrayList ();
2671 Attribute dllimport_attribute = null;
2672 string obsolete = null;
2673 bool obsolete_error = false;
2675 public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method)
2677 if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
2680 foreach (AttributeSection asec in opt_attrs.AttributeSections) {
2681 if (asec.Attributes == null)
2684 foreach (Attribute a in asec.Attributes) {
2685 if (a.Name == "Conditional") {
2686 if (!ApplyConditionalAttribute (a))
2688 } else if (a.Name == "Obsolete") {
2689 if (!ApplyObsoleteAttribute (a))
2691 } else if (a.Name.IndexOf ("DllImport") != -1) {
2693 a.Type = TypeManager.dllimport_type;
2694 Attribute.Error_AttributeNotValidForElement (a, Location);
2697 if (!ApplyDllImportAttribute (a))
2707 // Applies the `DllImport' attribute to the method.
2709 protected virtual bool ApplyDllImportAttribute (Attribute a)
2711 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
2712 if ((modifiers & extern_static) != extern_static) {
2713 Report.Error (601, Location,
2714 "The DllImport attribute must be specified on a method " +
2715 "marked `static' and `extern'.");
2719 flags |= MethodAttributes.PinvokeImpl;
2720 dllimport_attribute = a;
2725 // Applies the `Obsolete' attribute to the method.
2727 protected virtual bool ApplyObsoleteAttribute (Attribute a)
2729 if (obsolete != null) {
2730 Report.Error (579, Location, "Duplicate `Obsolete' attribute");
2734 obsolete = a.Obsolete_GetObsoleteMessage (out obsolete_error);
2735 return obsolete != null;
2739 // Applies the `Conditional' attribute to the method.
2741 protected virtual bool ApplyConditionalAttribute (Attribute a)
2743 // The Conditional attribute is only valid on methods.
2745 Attribute.Error_AttributeNotValidForElement (a, Location);
2749 string condition = a.Conditional_GetConditionName ();
2751 if (condition == null)
2754 if (ReturnType != TypeManager.void_type) {
2755 Report.Error (578, Location,
2756 "Conditional not valid on `" + member.Name + "' " +
2757 "because its return type is not void");
2761 if ((modifiers & Modifiers.OVERRIDE) != 0) {
2762 Report.Error (243, Location,
2763 "Conditional not valid on `" + member.Name + "' " +
2764 "because it is an override method");
2768 if (member.IsExplicitImpl) {
2769 Report.Error (577, Location,
2770 "Conditional not valid on `" + member.Name + "' " +
2771 "because it is an explicit interface implementation");
2775 if (IsImplementing) {
2776 Report.Error (623, Location,
2777 "Conditional not valid on `" + member.Name + "' " +
2778 "because it is an interface method");
2782 conditionals.Add (condition);
2788 // Checks whether this method should be ignored due to its Conditional attributes.
2790 bool ShouldIgnore (Location loc)
2792 // When we're overriding a virtual method, we implicitly inherit the
2793 // Conditional attributes from our parent.
2794 if (member.ParentMethod != null) {
2795 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (
2796 member.ParentMethod, loc);
2798 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
2802 foreach (string condition in conditionals)
2803 if (RootContext.AllDefines [condition] == null)
2810 // Returns the TypeManager.MethodFlags for this method.
2811 // This emits an error 619 / warning 618 if the method is obsolete.
2812 // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
2814 public virtual TypeManager.MethodFlags GetMethodFlags (Location loc)
2816 TypeManager.MethodFlags flags = 0;
2818 if (obsolete != null) {
2819 if (obsolete_error) {
2820 Report.Error (619, loc, "Method `" + member.Name +
2821 "' is obsolete: `" + obsolete + "'");
2822 return TypeManager.MethodFlags.IsObsoleteError;
2824 Report.Warning (618, loc, "Method `" + member.Name +
2825 "' is obsolete: `" + obsolete + "'");
2827 flags |= TypeManager.MethodFlags.IsObsolete;
2830 if (ShouldIgnore (loc))
2831 flags |= TypeManager.MethodFlags.ShouldIgnore;
2836 public virtual bool Define (TypeContainer parent)
2838 MethodInfo implementing = null;
2839 string method_name, name, prefix;
2841 if (OptAttributes != null)
2842 if (!ApplyAttributes (OptAttributes, is_method))
2845 if (member.IsExplicitImpl)
2846 prefix = member.InterfaceType.FullName + ".";
2850 if (accessor_name != null)
2851 name = accessor_name + "_" + member.ShortName;
2853 name = member.ShortName;
2854 method_name = prefix + name;
2856 if (parent.Pending != null){
2857 if (member is Indexer)
2858 implementing = parent.Pending.IsInterfaceIndexer (
2859 member.InterfaceType, ReturnType, ParameterTypes);
2861 implementing = parent.Pending.IsInterfaceMethod (
2862 member.InterfaceType, name, ReturnType, ParameterTypes);
2864 if (member.InterfaceType != null && implementing == null){
2865 TypeContainer.Error_ExplicitInterfaceNotMemberInterface (
2872 // For implicit implementations, make sure we are public, for
2873 // explicit implementations, make sure we are private.
2875 if (implementing != null){
2877 // Setting null inside this block will trigger a more
2878 // verbose error reporting for missing interface implementations
2880 // The "candidate" function has been flagged already
2881 // but it wont get cleared
2883 if (!member.IsExplicitImpl){
2885 // We already catch different accessibility settings
2886 // so we just need to check that we are not private
2888 if ((modifiers & Modifiers.PRIVATE) != 0)
2889 implementing = null;
2892 // Static is not allowed
2894 if ((modifiers & Modifiers.STATIC) != 0)
2895 implementing = null;
2897 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
2898 Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
2899 implementing = null;
2905 // If implementing is still valid, set flags
2907 if (implementing != null){
2909 // When implementing interface methods, set NewSlot.
2911 if (implementing.DeclaringType.IsInterface)
2912 flags |= MethodAttributes.NewSlot;
2915 MethodAttributes.Virtual |
2916 MethodAttributes.HideBySig;
2918 // Get the method name from the explicit interface.
2919 if (member.InterfaceType != null) {
2920 name = implementing.Name;
2921 method_name = prefix + name;
2924 IsImplementing = true;
2928 // Create the MethodBuilder for the method
2930 if ((flags & MethodAttributes.PinvokeImpl) != 0) {
2931 if ((modifiers & Modifiers.STATIC) == 0) {
2932 Report.Error (601, Location,
2933 "The DllImport attribute must be specified on " +
2934 "a method marked 'static' and 'extern'.");
2938 EmitContext ec = new EmitContext (
2939 parent, Location, null, ReturnType, modifiers);
2941 builder = dllimport_attribute.DefinePInvokeMethod (
2942 ec, parent.TypeBuilder, method_name, flags,
2943 ReturnType, ParameterTypes);
2945 builder = parent.TypeBuilder.DefineMethod (
2946 method_name, flags, CallingConventions,
2947 ReturnType, ParameterTypes);
2949 if (builder == null)
2952 if (IsImplementing) {
2954 // clear the pending implemntation flag
2956 if (member is Indexer) {
2957 parent.Pending.ImplementIndexer (
2958 member.InterfaceType, builder, ReturnType,
2959 ParameterTypes, true);
2961 parent.Pending.ImplementMethod (
2962 member.InterfaceType, name, ReturnType,
2963 ParameterTypes, member.IsExplicitImpl);
2965 if (member.IsExplicitImpl)
2966 parent.TypeBuilder.DefineMethodOverride (
2967 builder, implementing);
2970 if (!TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes)) {
2971 Report.Error (111, Location,
2972 "Class `" + parent.Name +
2973 "' already contains a definition with the " +
2974 "same return value and parameter types as the " +
2975 "'get' method of property `" + member.Name + "'");
2979 TypeManager.AddMethod (builder, this);
2987 public virtual void Emit (TypeContainer parent, Block block, object kind)
2992 if ((flags & MethodAttributes.PinvokeImpl) == 0)
2993 ig = builder.GetILGenerator ();
2997 ec = new EmitContext (parent, Location, ig, ReturnType, modifiers);
2999 if (OptAttributes != null)
3000 Attribute.ApplyAttributes (ec, builder, kind, OptAttributes, Location);
3002 if (member is MethodCore)
3003 ((MethodCore) member).LabelParameters (ec, ParameterTypes, MethodBuilder);
3006 // abstract or extern methods have no bodies
3008 if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
3013 // abstract or extern methods have no bodies.
3015 if ((modifiers & Modifiers.ABSTRACT) != 0)
3017 500, Location, "Abstract method `" +
3018 TypeManager.CSharpSignature (builder) +
3019 "' can not have a body");
3021 if ((modifiers & Modifiers.EXTERN) != 0)
3023 179, Location, "External method `" +
3024 TypeManager.CSharpSignature (builder) +
3025 "' can not have a body");
3031 // Methods must have a body unless they're extern or abstract
3033 if (block == null) {
3035 501, Location, "Method `" +
3036 TypeManager.CSharpSignature (builder) +
3037 "' must declare a body since it is not marked " +
3038 "abstract or extern");
3043 // Handle destructors specially
3045 // FIXME: This code generates buggy code
3047 if (member.Name == "Finalize" && ReturnType == TypeManager.void_type)
3048 EmitDestructor (ec, block);
3050 ISymbolWriter sw = CodeGen.SymbolWriter;
3052 if ((sw != null) && !Location.IsNull (Location) &&
3053 !Location.IsNull (block.EndLocation)) {
3054 Location end = block.EndLocation;
3055 MethodToken token = MethodBuilder.GetToken ();
3056 sw.OpenMethod (new SymbolToken (token.Token));
3057 sw.SetMethodSourceRange (Location.SymbolDocument,
3062 ec.EmitTopBlock (block, ParameterInfo, Location);
3066 ec.EmitTopBlock (block, ParameterInfo, Location);
3070 void EmitDestructor (EmitContext ec, Block block)
3072 ILGenerator ig = ec.ig;
3074 Label finish = ig.DefineLabel ();
3075 bool old_in_try = ec.InTry;
3077 ig.BeginExceptionBlock ();
3079 ec.ReturnLabel = finish;
3080 ec.HasReturnLabel = true;
3081 ec.EmitTopBlock (block, null, Location);
3082 ec.InTry = old_in_try;
3084 // ig.MarkLabel (finish);
3085 bool old_in_finally = ec.InFinally;
3086 ec.InFinally = true;
3087 ig.BeginFinallyBlock ();
3089 if (ec.ContainerType.BaseType != null) {
3090 Expression member_lookup = Expression.MemberLookup (
3091 ec, ec.ContainerType.BaseType, ec.ContainerType.BaseType, "Finalize",
3092 MemberTypes.Method, Expression.AllBindingFlags, Location);
3094 if (member_lookup != null){
3095 MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
3097 ig.Emit (OpCodes.Ldarg_0);
3098 ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
3101 ec.InFinally = old_in_finally;
3103 ig.EndExceptionBlock ();
3104 //ig.MarkLabel (ec.ReturnLabel);
3105 ig.Emit (OpCodes.Ret);
3109 abstract public class MemberBase : MemberCore {
3110 public Expression Type;
3111 public readonly Attributes OptAttributes;
3113 protected MethodAttributes flags;
3116 // The "short" name of this property / indexer / event. This is the
3117 // name without the explicit interface.
3119 public string ShortName;
3122 // The type of this property / indexer / event
3124 public Type MemberType;
3127 // If true, this is an explicit interface implementation
3129 public bool IsExplicitImpl = false;
3132 // The name of the interface we are explicitly implementing
3134 public string ExplicitInterfaceName = null;
3137 // If true, the interface type we are explicitly implementing
3139 public Type InterfaceType = null;
3142 // The method we're overriding if this is an override method.
3144 protected MethodInfo parent_method = null;
3145 public MethodInfo ParentMethod {
3147 return parent_method;
3152 // The constructor is only exposed to our children
3154 protected MemberBase (Expression type, int mod, int allowed_mod, string name,
3155 Attributes attrs, Location loc)
3159 ModFlags = Modifiers.Check (allowed_mod, mod, Modifiers.PRIVATE, loc);
3160 OptAttributes = attrs;
3163 protected virtual bool CheckBase (TypeContainer parent)
3168 protected virtual bool CheckParameters (TypeContainer parent, Type [] parameters)
3172 foreach (Type partype in parameters){
3173 if (partype.IsPointer && !UnsafeOK (parent))
3176 if (parent.AsAccessible (partype, ModFlags))
3179 if (this is Indexer)
3180 Report.Error (55, Location,
3181 "Inconsistent accessibility: parameter type `" +
3182 TypeManager.CSharpName (partype) + "' is less " +
3183 "accessible than indexer `" + Name + "'");
3185 Report.Error (51, Location,
3186 "Inconsistent accessibility: parameter type `" +
3187 TypeManager.CSharpName (partype) + "' is less " +
3188 "accessible than method `" + Name + "'");
3195 protected virtual bool DoDefine (TypeContainer parent)
3200 if (!parent.MethodModifiersValid (ModFlags, Name, Location))
3203 flags = Modifiers.MethodAttr (ModFlags);
3205 // Lookup Type, verify validity
3206 MemberType = parent.ResolveType (Type, false, Location);
3207 if (MemberType == null)
3210 // verify accessibility
3211 if (!parent.AsAccessible (MemberType, ModFlags)) {
3212 if (this is Property)
3213 Report.Error (53, Location,
3214 "Inconsistent accessibility: property type `" +
3215 TypeManager.CSharpName (MemberType) + "' is less " +
3216 "accessible than property `" + Name + "'");
3217 else if (this is Indexer)
3218 Report.Error (54, Location,
3219 "Inconsistent accessibility: indexer return type `" +
3220 TypeManager.CSharpName (MemberType) + "' is less " +
3221 "accessible than indexer `" + Name + "'");
3222 else if (this is Method)
3223 Report.Error (50, Location,
3224 "Inconsistent accessibility: return type `" +
3225 TypeManager.CSharpName (MemberType) + "' is less " +
3226 "accessible than method `" + Name + "'");
3228 Report.Error (52, Location,
3229 "Inconsistent accessibility: field type `" +
3230 TypeManager.CSharpName (MemberType) + "' is less " +
3231 "accessible than field `" + Name + "'");
3235 if (MemberType.IsPointer && !UnsafeOK (parent))
3239 // Check for explicit interface implementation
3241 if ((ExplicitInterfaceName == null) && (Name.IndexOf (".") != -1)){
3242 int pos = Name.LastIndexOf (".");
3244 ExplicitInterfaceName = Name.Substring (0, pos);
3245 ShortName = Name.Substring (pos + 1);
3249 if (ExplicitInterfaceName != null) {
3250 InterfaceType = RootContext.LookupType (
3251 parent, ExplicitInterfaceName, false, Location);
3252 if (InterfaceType == null)
3255 // Compute the full name that we need to export.
3256 Name = InterfaceType.FullName + "." + ShortName;
3258 if (!parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
3261 IsExplicitImpl = true;
3263 IsExplicitImpl = false;
3270 // Fields and Events both generate FieldBuilders, we use this to share
3271 // their common bits. This is also used to flag usage of the field
3273 abstract public class FieldBase : MemberBase {
3274 public FieldBuilder FieldBuilder;
3275 public Status status;
3278 public enum Status : byte { ASSIGNED = 1, USED = 2 }
3281 // The constructor is only exposed to our children
3283 protected FieldBase (Expression type, int mod, int allowed_mod, string name,
3284 object init, Attributes attrs, Location loc)
3285 : base (type, mod, allowed_mod, name, attrs, loc)
3291 // Whether this field has an initializer.
3293 public bool HasInitializer {
3295 return init != null;
3300 readonly Object init;
3301 Expression init_expr;
3302 bool init_expr_initialized = false;
3305 // Resolves and returns the field initializer.
3307 public Expression GetInitializerExpression (EmitContext ec)
3309 if (init_expr_initialized)
3313 if (init is Expression)
3314 e = (Expression) init;
3316 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
3318 ec.IsFieldInitializer = true;
3319 e = e.DoResolve (ec);
3320 ec.IsFieldInitializer = false;
3323 init_expr_initialized = true;
3330 // The Field class is used to represents class/struct fields during parsing.
3332 public class Field : FieldBase {
3334 // Modifiers allowed in a class declaration
3336 const int AllowedModifiers =
3339 Modifiers.PROTECTED |
3340 Modifiers.INTERNAL |
3343 Modifiers.VOLATILE |
3347 public Field (Expression type, int mod, string name, Object expr_or_array_init,
3348 Attributes attrs, Location loc)
3349 : base (type, mod, AllowedModifiers, name, expr_or_array_init, attrs, loc)
3353 public override bool Define (TypeContainer parent)
3355 Type t = parent.ResolveType (Type, false, Location);
3360 if (!parent.AsAccessible (t, ModFlags)) {
3361 Report.Error (52, Location,
3362 "Inconsistent accessibility: field type `" +
3363 TypeManager.CSharpName (t) + "' is less " +
3364 "accessible than field `" + Name + "'");
3368 if (t.IsPointer && !UnsafeOK (parent))
3371 if (RootContext.WarningLevel > 1){
3372 Type ptype = parent.TypeBuilder.BaseType;
3374 // ptype is only null for System.Object while compiling corlib.
3376 TypeContainer.FindMembers (
3377 ptype, MemberTypes.Method,
3378 BindingFlags.Public |
3379 BindingFlags.Static | BindingFlags.Instance,
3380 System.Type.FilterName, Name);
3384 if ((ModFlags & Modifiers.VOLATILE) != 0){
3386 if (TypeManager.IsEnumType (t))
3387 t = TypeManager.EnumToUnderlying (t);
3389 if (!((t == TypeManager.bool_type) ||
3390 (t == TypeManager.sbyte_type) ||
3391 (t == TypeManager.byte_type) ||
3392 (t == TypeManager.short_type) ||
3393 (t == TypeManager.ushort_type) ||
3394 (t == TypeManager.int32_type) ||
3395 (t == TypeManager.uint32_type) ||
3396 (t == TypeManager.char_type) ||
3397 (t == TypeManager.float_type))){
3399 677, Location, parent.MakeName (Name) +
3400 " A volatile field can not be of type `" +
3401 TypeManager.CSharpName (t) + "'");
3407 FieldBuilder = parent.TypeBuilder.DefineField (
3408 Name, t, Modifiers.FieldAttr (ModFlags));
3410 TypeManager.RegisterFieldBase (FieldBuilder, this);
3414 public void Emit (TypeContainer tc)
3416 EmitContext ec = new EmitContext (tc, Location, null,
3417 FieldBuilder.FieldType, ModFlags);
3419 Attribute.ApplyAttributes (ec, FieldBuilder, this, OptAttributes, Location);
3424 // `set' and `get' accessors are represented with an Accessor.
3426 public class Accessor {
3428 // Null if the accessor is empty, or a Block if not
3431 public Attributes OptAttributes;
3433 public Accessor (Block b, Attributes attrs)
3436 OptAttributes = attrs;
3441 // Properties and Indexers both generate PropertyBuilders, we use this to share
3442 // their common bits.
3444 abstract public class PropertyBase : MethodCore {
3445 public Accessor Get, Set;
3446 public PropertyBuilder PropertyBuilder;
3447 public MethodBuilder GetBuilder, SetBuilder;
3448 public MethodData GetData, SetData;
3450 protected EmitContext ec;
3452 public PropertyBase (Expression type, string name, int mod_flags, int allowed_mod,
3453 Parameters parameters, Accessor get_block, Accessor set_block,
3454 Attributes attrs, Location loc)
3455 : base (type, mod_flags, allowed_mod, name, attrs, parameters, loc)
3461 protected override bool DoDefine (TypeContainer parent)
3463 if (!base.DoDefine (parent))
3466 ec = new EmitContext (parent, Location, null, MemberType, ModFlags);
3472 // Checks our base implementation if any
3474 protected override bool CheckBase (TypeContainer parent)
3476 // Check whether arguments were correct.
3477 if (!DoDefineParameters (parent))
3484 MethodSignature ms, base_ms;
3485 if (this is Indexer) {
3486 string name, base_name;
3488 report_name = "this";
3489 name = TypeManager.IndexerPropertyName (parent.TypeBuilder);
3490 ms = new MethodSignature (name, null, ParameterTypes);
3491 base_name = TypeManager.IndexerPropertyName (parent.TypeBuilder.BaseType);
3492 base_ms = new MethodSignature (base_name, null, ParameterTypes);
3495 ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
3498 MemberList props_this;
3500 props_this = TypeContainer.FindMembers (
3501 parent.TypeBuilder, MemberTypes.Property,
3502 BindingFlags.NonPublic | BindingFlags.Public |
3503 BindingFlags.Static | BindingFlags.Instance |
3504 BindingFlags.DeclaredOnly,
3505 MethodSignature.method_signature_filter, ms);
3507 if (props_this.Count > 0) {
3508 Report.Error (111, Location, "Class `" + parent.Name + "' " +
3509 "already defines a member called `" + report_name + "' " +
3510 "with the same parameter types");
3515 // Find properties with the same name on the base class
3518 MemberList props_static = TypeContainer.FindMembers (
3519 parent.TypeBuilder.BaseType, MemberTypes.Property,
3520 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
3521 MethodSignature.inheritable_property_signature_filter, base_ms);
3523 MemberList props_instance = TypeContainer.FindMembers (
3524 parent.TypeBuilder.BaseType, MemberTypes.Property,
3525 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
3526 MethodSignature.inheritable_property_signature_filter,
3530 // Find if we have anything
3532 if (props_static.Count > 0)
3533 props = props_static;
3534 else if (props_instance.Count > 0)
3535 props = props_instance;
3540 // If we have something on the base.
3541 if (props != null && props.Count > 0){
3542 PropertyInfo pi = (PropertyInfo) props [0];
3544 MethodInfo inherited_get = TypeManager.GetPropertyGetter (pi);
3545 MethodInfo inherited_set = TypeManager.GetPropertySetter (pi);
3547 MethodInfo reference = inherited_get == null ?
3548 inherited_set : inherited_get;
3550 if (reference != null) {
3551 string name = reference.DeclaringType.Name + "." + report_name;
3553 if (!CheckMethodAgainstBase (parent, flags, reference, name))
3557 if (((ModFlags & Modifiers.NEW) == 0) && (pi.PropertyType != MemberType)) {
3558 Report.Error (508, parent.MakeName (Name) + ": cannot " +
3559 "change return type when overriding inherited " +
3560 "member `" + pi.DeclaringType + "." + pi.Name + "'");
3564 if ((ModFlags & Modifiers.NEW) != 0)
3565 WarningNotHiding (parent);
3567 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3568 if (this is Indexer)
3569 Report.Error (115, Location,
3570 parent.MakeName (Name) +
3571 " no suitable indexers found to override");
3573 Report.Error (115, Location,
3574 parent.MakeName (Name) +
3575 " no suitable properties found to override");
3582 public void Emit (TypeContainer tc)
3585 // The PropertyBuilder can be null for explicit implementations, in that
3586 // case, we do not actually emit the ".property", so there is nowhere to
3587 // put the attribute
3589 if (PropertyBuilder != null)
3590 Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
3592 if (GetData != null)
3593 GetData.Emit (tc, Get.Block, Get);
3595 if (SetData != null)
3596 SetData.Emit (tc, Set.Block, Set);
3600 public class Property : PropertyBase {
3601 const int AllowedModifiers =
3604 Modifiers.PROTECTED |
3605 Modifiers.INTERNAL |
3609 Modifiers.OVERRIDE |
3610 Modifiers.ABSTRACT |
3615 public Property (Expression type, string name, int mod_flags,
3616 Accessor get_block, Accessor set_block,
3617 Attributes attrs, Location loc)
3618 : base (type, name, mod_flags, AllowedModifiers,
3619 Parameters.EmptyReadOnlyParameters,
3620 get_block, set_block, attrs, loc)
3624 public override bool Define (TypeContainer parent)
3626 if (!DoDefine (parent))
3629 if (!CheckBase (parent))
3632 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
3635 Type [] parameters = TypeManager.NoTypes;
3637 InternalParameters ip = new InternalParameters (
3638 parent, Parameters.EmptyReadOnlyParameters);
3640 GetData = new MethodData (this, "get", MemberType,
3641 parameters, ip, CallingConventions.Standard,
3642 Get.OptAttributes, ModFlags, flags, false);
3644 if (!GetData.Define (parent))
3647 GetBuilder = GetData.MethodBuilder;
3651 Type [] parameters = new Type [1];
3652 parameters [0] = MemberType;
3654 Parameter [] parms = new Parameter [1];
3655 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
3656 InternalParameters ip = new InternalParameters (
3657 parent, new Parameters (parms, null, Location));
3659 SetData = new MethodData (this, "set", TypeManager.void_type,
3660 parameters, ip, CallingConventions.Standard,
3661 Set.OptAttributes, ModFlags, flags, false);
3663 if (!SetData.Define (parent))
3666 SetBuilder = SetData.MethodBuilder;
3667 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
3670 // FIXME - PropertyAttributes.HasDefault ?
3672 PropertyAttributes prop_attr =
3673 PropertyAttributes.RTSpecialName |
3674 PropertyAttributes.SpecialName;
3676 if (!IsExplicitImpl){
3677 PropertyBuilder = parent.TypeBuilder.DefineProperty (
3678 Name, prop_attr, MemberType, null);
3681 PropertyBuilder.SetGetMethod (GetBuilder);
3684 PropertyBuilder.SetSetMethod (SetBuilder);
3687 // HACK for the reasons exposed above
3689 if (!TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder)) {
3692 "Class `" + parent.Name +
3693 "' already contains a definition for the property `" +
3703 /// Gigantic workaround for lameness in SRE follows :
3704 /// This class derives from EventInfo and attempts to basically
3705 /// wrap around the EventBuilder so that FindMembers can quickly
3706 /// return this in it search for members
3708 public class MyEventBuilder : EventInfo {
3711 // We use this to "point" to our Builder which is
3712 // not really a MemberInfo
3714 EventBuilder MyBuilder;
3717 // We "catch" and wrap these methods
3719 MethodInfo raise, remove, add;
3721 EventAttributes attributes;
3722 Type declaring_type, reflected_type, event_type;
3725 public MyEventBuilder (TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
3727 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
3729 // And now store the values in our own fields.
3731 declaring_type = type_builder;
3733 reflected_type = type_builder;
3735 attributes = event_attr;
3737 this.event_type = event_type;
3741 // Methods that you have to override. Note that you only need
3742 // to "implement" the variants that take the argument (those are
3743 // the "abstract" methods, the others (GetAddMethod()) are
3746 public override MethodInfo GetAddMethod (bool nonPublic)
3751 public override MethodInfo GetRemoveMethod (bool nonPublic)
3756 public override MethodInfo GetRaiseMethod (bool nonPublic)
3762 // These methods make "MyEventInfo" look like a Builder
3764 public void SetRaiseMethod (MethodBuilder raiseMethod)
3766 raise = raiseMethod;
3767 MyBuilder.SetRaiseMethod (raiseMethod);
3770 public void SetRemoveOnMethod (MethodBuilder removeMethod)
3772 remove = removeMethod;
3773 MyBuilder.SetRemoveOnMethod (removeMethod);
3776 public void SetAddOnMethod (MethodBuilder addMethod)
3779 MyBuilder.SetAddOnMethod (addMethod);
3782 public void SetCustomAttribute (CustomAttributeBuilder cb)
3784 MyBuilder.SetCustomAttribute (cb);
3787 public override object [] GetCustomAttributes (bool inherit)
3789 // FIXME : There's nothing which can be seemingly done here because
3790 // we have no way of getting at the custom attribute objects of the
3795 public override object [] GetCustomAttributes (Type t, bool inherit)
3797 // FIXME : Same here !
3801 public override bool IsDefined (Type t, bool b)
3806 public override EventAttributes Attributes {
3812 public override string Name {
3818 public override Type DeclaringType {
3820 return declaring_type;
3824 public override Type ReflectedType {
3826 return reflected_type;
3830 public Type EventType {
3837 public class Event : FieldBase {
3838 const int AllowedModifiers =
3841 Modifiers.PROTECTED |
3842 Modifiers.INTERNAL |
3847 Modifiers.OVERRIDE |
3851 public readonly Accessor Add;
3852 public readonly Accessor Remove;
3853 public MyEventBuilder EventBuilder;
3855 MethodBuilder AddBuilder, RemoveBuilder;
3856 MethodData AddData, RemoveData;
3858 public Event (Expression type, string name, Object init, int mod, Accessor add,
3859 Accessor remove, Attributes attrs, Location loc)
3860 : base (type, mod, AllowedModifiers, name, init, attrs, loc)
3866 public override bool Define (TypeContainer parent)
3868 EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
3870 if (!DoDefine (parent))
3873 if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
3874 Report.Error (66, Location, "'" + parent.Name + "." + Name +
3875 "' : event must be of a delegate type");
3879 Type [] parameter_types = new Type [1];
3880 parameter_types [0] = MemberType;
3882 Parameter [] parms = new Parameter [1];
3883 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
3884 InternalParameters ip = new InternalParameters (
3885 parent, new Parameters (parms, null, Location));
3887 if (!CheckBase (parent))
3891 // Now define the accessors
3893 AddData = new MethodData (this, "add", TypeManager.void_type,
3894 parameter_types, ip, CallingConventions.Standard,
3895 (Add != null) ? Add.OptAttributes : null,
3896 ModFlags, flags, false);
3898 if (!AddData.Define (parent))
3901 AddBuilder = AddData.MethodBuilder;
3902 AddBuilder.DefineParameter (1, ParameterAttributes.None, "value");
3904 RemoveData = new MethodData (this, "remove", TypeManager.void_type,
3905 parameter_types, ip, CallingConventions.Standard,
3906 (Remove != null) ? Remove.OptAttributes : null,
3907 ModFlags, flags, false);
3909 if (!RemoveData.Define (parent))
3912 RemoveBuilder = RemoveData.MethodBuilder;
3913 RemoveBuilder.DefineParameter (1, ParameterAttributes.None, "value");
3915 if (!IsExplicitImpl){
3916 EventBuilder = new MyEventBuilder (
3917 parent.TypeBuilder, Name, e_attr, MemberType);
3919 if (Add == null && Remove == null) {
3920 FieldBuilder = parent.TypeBuilder.DefineField (
3921 Name, MemberType, FieldAttributes.FamANDAssem);
3922 TypeManager.RegisterPrivateFieldOfEvent (
3923 (EventInfo) EventBuilder, FieldBuilder);
3924 TypeManager.RegisterFieldBase (FieldBuilder, this);
3927 EventBuilder.SetAddOnMethod (AddBuilder);
3928 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
3930 if (!TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder)) {
3931 Report.Error (111, Location,
3932 "Class `" + parent.Name +
3933 "' already contains a definition for the event `" +
3942 void EmitDefaultMethod (EmitContext ec, bool is_add)
3944 ILGenerator ig = ec.ig;
3945 MethodInfo method = null;
3948 method = TypeManager.delegate_combine_delegate_delegate;
3950 method = TypeManager.delegate_remove_delegate_delegate;
3952 if ((ModFlags & Modifiers.STATIC) != 0) {
3953 ig.Emit (OpCodes.Ldsfld, (FieldInfo) FieldBuilder);
3954 ig.Emit (OpCodes.Ldarg_0);
3955 ig.Emit (OpCodes.Call, method);
3956 ig.Emit (OpCodes.Castclass, MemberType);
3957 ig.Emit (OpCodes.Stsfld, (FieldInfo) FieldBuilder);
3959 ig.Emit (OpCodes.Ldarg_0);
3960 ig.Emit (OpCodes.Ldarg_0);
3961 ig.Emit (OpCodes.Ldfld, (FieldInfo) FieldBuilder);
3962 ig.Emit (OpCodes.Ldarg_1);
3963 ig.Emit (OpCodes.Call, method);
3964 ig.Emit (OpCodes.Castclass, MemberType);
3965 ig.Emit (OpCodes.Stfld, (FieldInfo) FieldBuilder);
3967 ig.Emit (OpCodes.Ret);
3970 public void Emit (TypeContainer tc)
3974 ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
3975 Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes, Location);
3978 AddData.Emit (tc, Add.Block, Add);
3980 ILGenerator ig = AddData.MethodBuilder.GetILGenerator ();
3981 ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
3982 EmitDefaultMethod (ec, true);
3986 RemoveData.Emit (tc, Remove.Block, Remove);
3988 ILGenerator ig = RemoveData.MethodBuilder.GetILGenerator ();
3989 ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
3990 EmitDefaultMethod (ec, false);
3997 // FIXME: This does not handle:
3999 // int INTERFACENAME [ args ]
4004 // int this [ args ]
4006 public class Indexer : PropertyBase {
4008 const int AllowedModifiers =
4011 Modifiers.PROTECTED |
4012 Modifiers.INTERNAL |
4016 Modifiers.OVERRIDE |
4021 public string IndexerName;
4022 public string InterfaceIndexerName;
4025 // Are we implementing an interface ?
4027 bool IsImplementing = false;
4029 public Indexer (Expression type, string int_type, int flags, Parameters parameters,
4030 Accessor get_block, Accessor set_block, Attributes attrs, Location loc)
4031 : base (type, "", flags, AllowedModifiers, parameters, get_block, set_block,
4034 ExplicitInterfaceName = int_type;
4037 public override bool Define (TypeContainer parent)
4039 PropertyAttributes prop_attr =
4040 PropertyAttributes.RTSpecialName |
4041 PropertyAttributes.SpecialName;
4043 if (!DoDefine (parent))
4046 IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
4047 if (IndexerName == null)
4048 IndexerName = "Item";
4049 else if (IsExplicitImpl)
4050 Report.Error (592, Location,
4051 "Attribute 'IndexerName' is not valid on this declaration " +
4052 "type. It is valid on `property' declarations only.");
4054 ShortName = IndexerName;
4055 if (IsExplicitImpl) {
4056 InterfaceIndexerName = TypeManager.IndexerPropertyName (InterfaceType);
4057 Name = InterfaceType.FullName + "." + IndexerName;
4059 InterfaceIndexerName = IndexerName;
4063 if (!CheckBase (parent))
4067 InternalParameters ip = new InternalParameters (parent, Parameters);
4069 GetData = new MethodData (this, "get", MemberType,
4070 ParameterTypes, ip, CallingConventions.Standard,
4071 Get.OptAttributes, ModFlags, flags, false);
4073 if (!GetData.Define (parent))
4076 GetBuilder = GetData.MethodBuilder;
4080 int top = ParameterTypes.Length;
4081 Type [] set_pars = new Type [top + 1];
4082 ParameterTypes.CopyTo (set_pars, 0);
4083 set_pars [top] = MemberType;
4085 Parameter [] fixed_parms = Parameters.FixedParameters;
4087 if (fixed_parms == null){
4088 throw new Exception ("We currently do not support only array arguments in an indexer");
4089 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4090 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4092 // Here is the problem: the `value' parameter has
4093 // to come *after* the array parameter in the declaration
4095 // X (object [] x, Type value)
4098 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4099 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4103 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
4106 fixed_parms.CopyTo (tmp, 0);
4107 tmp [fixed_parms.Length] = new Parameter (
4108 Type, "value", Parameter.Modifier.NONE, null);
4110 Parameters set_formal_params = new Parameters (tmp, null, Location);
4112 InternalParameters ip = new InternalParameters (parent, set_formal_params);
4114 SetData = new MethodData (this, "set", TypeManager.void_type,
4115 set_pars, ip, CallingConventions.Standard,
4116 Set.OptAttributes, ModFlags, flags, false);
4118 if (!SetData.Define (parent))
4121 SetBuilder = SetData.MethodBuilder;
4125 // Now name the parameters
4127 Parameter [] p = Parameters.FixedParameters;
4131 for (i = 0; i < p.Length; ++i) {
4133 GetBuilder.DefineParameter (
4134 i + 1, p [i].Attributes, p [i].Name);
4137 SetBuilder.DefineParameter (
4138 i + 1, p [i].Attributes, p [i].Name);
4142 SetBuilder.DefineParameter (
4143 i + 1, ParameterAttributes.None, "value");
4145 if (i != ParameterTypes.Length) {
4146 Parameter array_param = Parameters.ArrayParameter;
4147 SetBuilder.DefineParameter (
4148 i + 1, array_param.Attributes, array_param.Name);
4152 if (GetData != null)
4153 IsImplementing = GetData.IsImplementing;
4154 else if (SetData != null)
4155 IsImplementing = SetData.IsImplementing;
4158 // Define the PropertyBuilder if one of the following conditions are met:
4159 // a) we're not implementing an interface indexer.
4160 // b) the indexer has a different IndexerName and this is no
4161 // explicit interface implementation.
4163 if (!IsExplicitImpl) {
4164 PropertyBuilder = parent.TypeBuilder.DefineProperty (
4165 IndexerName, prop_attr, MemberType, ParameterTypes);
4167 if (GetData != null)
4168 PropertyBuilder.SetGetMethod (GetBuilder);
4170 if (SetData != null)
4171 PropertyBuilder.SetSetMethod (SetBuilder);
4173 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
4181 public class Operator : MemberCore {
4183 const int AllowedModifiers =
4189 const int RequiredModifiers =
4193 public enum OpType : byte {
4203 // Unary and Binary operators
4226 // Implicit and Explicit
4231 public readonly OpType OperatorType;
4232 public readonly Expression ReturnType;
4233 public readonly Expression FirstArgType, SecondArgType;
4234 public readonly string FirstArgName, SecondArgName;
4235 public readonly Block Block;
4236 public Attributes OptAttributes;
4237 public MethodBuilder OperatorMethodBuilder;
4239 public string MethodName;
4240 public Method OperatorMethod;
4242 public Operator (OpType type, Expression ret_type, int flags,
4243 Expression arg1type, string arg1name,
4244 Expression arg2type, string arg2name,
4245 Block block, Attributes attrs, Location loc)
4248 OperatorType = type;
4249 ReturnType = ret_type;
4250 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PUBLIC, loc);
4251 FirstArgType = arg1type;
4252 FirstArgName = arg1name;
4253 SecondArgType = arg2type;
4254 SecondArgName = arg2name;
4256 OptAttributes = attrs;
4259 string Prototype (TypeContainer parent)
4261 return parent.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
4262 SecondArgType + ")";
4265 public override bool Define (TypeContainer parent)
4268 MethodName = "op_" + OperatorType;
4270 if (SecondArgType != null)
4273 Parameter [] param_list = new Parameter [length];
4275 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
4278 "User defined operators `" +
4279 Prototype (parent) +
4280 "' must be declared static and public");
4284 param_list[0] = new Parameter (FirstArgType, FirstArgName,
4285 Parameter.Modifier.NONE, null);
4286 if (SecondArgType != null)
4287 param_list[1] = new Parameter (SecondArgType, SecondArgName,
4288 Parameter.Modifier.NONE, null);
4290 OperatorMethod = new Method (ReturnType, ModFlags, MethodName,
4291 new Parameters (param_list, null, Location),
4292 OptAttributes, Mono.CSharp.Location.Null);
4294 OperatorMethod.IsOperator = true;
4295 OperatorMethod.Define (parent);
4297 if (OperatorMethod.MethodBuilder == null)
4300 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
4302 Type [] param_types = OperatorMethod.ParameterTypes;
4303 Type declaring_type = OperatorMethodBuilder.DeclaringType;
4304 Type return_type = OperatorMethod.GetReturnType ();
4305 Type first_arg_type = param_types [0];
4307 // Rules for conversion operators
4309 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
4310 if (first_arg_type == return_type && first_arg_type == declaring_type){
4313 "User-defined conversion cannot take an object of the " +
4314 "enclosing type and convert to an object of the enclosing" +
4319 if (first_arg_type != declaring_type && return_type != declaring_type){
4322 "User-defined conversion must convert to or from the " +
4327 if (first_arg_type == TypeManager.object_type ||
4328 return_type == TypeManager.object_type){
4331 "User-defined conversion cannot convert to or from " +
4336 if (first_arg_type.IsInterface || return_type.IsInterface){
4339 "User-defined conversion cannot convert to or from an " +
4344 if (first_arg_type.IsSubclassOf (return_type) ||
4345 return_type.IsSubclassOf (first_arg_type)){
4348 "User-defined conversion cannot convert between types " +
4349 "that derive from each other");
4352 } else if (SecondArgType == null) {
4353 // Checks for Unary operators
4355 if (first_arg_type != declaring_type){
4358 "The parameter of a unary operator must be the " +
4363 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
4364 if (return_type != declaring_type){
4367 "The parameter and return type for ++ and -- " +
4368 "must be the containing type");
4374 if (OperatorType == OpType.True || OperatorType == OpType.False) {
4375 if (return_type != TypeManager.bool_type){
4378 "The return type of operator True or False " +
4385 // Checks for Binary operators
4387 if (first_arg_type != declaring_type &&
4388 param_types [1] != declaring_type){
4391 "One of the parameters of a binary operator must " +
4392 "be the containing type");
4400 public void Emit (TypeContainer parent)
4402 EmitContext ec = new EmitContext (parent, Location, null, null, ModFlags);
4403 Attribute.ApplyAttributes (ec, OperatorMethodBuilder, this, OptAttributes, Location);
4406 // abstract or extern methods have no bodies
4408 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
4411 OperatorMethod.Block = Block;
4412 OperatorMethod.Emit (parent);
4415 public static string GetName (OpType ot)
4418 case OpType.LogicalNot:
4420 case OpType.OnesComplement:
4422 case OpType.Increment:
4424 case OpType.Decrement:
4430 case OpType.Addition:
4432 case OpType.Subtraction:
4434 case OpType.UnaryPlus:
4436 case OpType.UnaryNegation:
4438 case OpType.Multiply:
4440 case OpType.Division:
4442 case OpType.Modulus:
4444 case OpType.BitwiseAnd:
4446 case OpType.BitwiseOr:
4448 case OpType.ExclusiveOr:
4450 case OpType.LeftShift:
4452 case OpType.RightShift:
4454 case OpType.Equality:
4456 case OpType.Inequality:
4458 case OpType.GreaterThan:
4460 case OpType.LessThan:
4462 case OpType.GreaterThanOrEqual:
4464 case OpType.LessThanOrEqual:
4466 case OpType.Implicit:
4468 case OpType.Explicit:
4474 public override string ToString ()
4476 Type return_type = OperatorMethod.GetReturnType();
4477 Type [] param_types = OperatorMethod.ParameterTypes;
4479 if (SecondArgType == null)
4480 return String.Format (
4481 "{0} operator {1}({2})",
4482 TypeManager.CSharpName (return_type),
4483 GetName (OperatorType),
4486 return String.Format (
4487 "{0} operator {1}({2}, {3})",
4488 TypeManager.CSharpName (return_type),
4489 GetName (OperatorType),
4490 param_types [0], param_types [1]);
4495 // This is used to compare method signatures
4497 struct MethodSignature {
4499 public Type RetType;
4500 public Type [] Parameters;
4503 /// This delegate is used to extract methods which have the
4504 /// same signature as the argument
4506 public static MemberFilter method_signature_filter;
4509 /// This delegate is used to extract inheritable methods which
4510 /// have the same signature as the argument. By inheritable,
4511 /// this means that we have permissions to override the method
4512 /// from the current assembly and class
4514 public static MemberFilter inheritable_method_signature_filter;
4517 /// This delegate is used to extract inheritable methods which
4518 /// have the same signature as the argument. By inheritable,
4519 /// this means that we have permissions to override the method
4520 /// from the current assembly and class
4522 public static MemberFilter inheritable_property_signature_filter;
4524 static MethodSignature ()
4526 method_signature_filter = new MemberFilter (MemberSignatureCompare);
4527 inheritable_method_signature_filter = new MemberFilter (
4528 InheritableMemberSignatureCompare);
4529 inheritable_property_signature_filter = new MemberFilter (
4530 InheritablePropertySignatureCompare);
4533 public MethodSignature (string name, Type ret_type, Type [] parameters)
4538 if (parameters == null)
4539 Parameters = TypeManager.NoTypes;
4541 Parameters = parameters;
4544 public override int GetHashCode ()
4546 return Name.GetHashCode ();
4549 public override bool Equals (Object o)
4551 MethodSignature other = (MethodSignature) o;
4553 if (other.Name != Name)
4556 if (other.RetType != RetType)
4559 if (Parameters == null){
4560 if (other.Parameters == null)
4565 if (other.Parameters == null)
4568 int c = Parameters.Length;
4569 if (other.Parameters.Length != c)
4572 for (int i = 0; i < c; i++)
4573 if (other.Parameters [i] != Parameters [i])
4579 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
4581 MethodSignature sig = (MethodSignature) filter_criteria;
4583 if (m.Name != sig.Name)
4587 MethodInfo mi = m as MethodInfo;
4588 PropertyInfo pi = m as PropertyInfo;
4591 ReturnType = mi.ReturnType;
4592 else if (pi != null)
4593 ReturnType = pi.PropertyType;
4598 // we use sig.RetType == null to mean `do not check the
4599 // method return value.
4601 if (sig.RetType != null)
4602 if (ReturnType != sig.RetType)
4607 args = TypeManager.GetArgumentTypes (mi);
4609 args = TypeManager.GetArgumentTypes (pi);
4610 Type [] sigp = sig.Parameters;
4612 if (args.Length != sigp.Length)
4615 for (int i = args.Length; i > 0; ){
4617 if (args [i] != sigp [i])
4624 // This filter should be used when we are requesting methods that
4625 // we want to override.
4627 // This makes a number of assumptions, for example
4628 // that the methods being extracted are of a parent
4629 // class (this means we know implicitly that we are
4630 // being called to find out about members by a derived
4633 static bool InheritableMemberSignatureCompare (MemberInfo m, object filter_criteria)
4635 if (MemberSignatureCompare (m, filter_criteria)){
4636 MethodInfo mi = (MethodInfo) m;
4637 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
4639 // If only accessible to the current class.
4640 if (prot == MethodAttributes.Private)
4643 // If only accessible to the defining assembly or
4644 if (prot == MethodAttributes.FamANDAssem ||
4645 prot == MethodAttributes.Assembly){
4646 if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
4652 // Anything else (FamOrAssembly and Public) is fine
4659 // This filter should be used when we are requesting properties that
4660 // we want to override.
4662 // This makes a number of assumptions, for example
4663 // that the methods being extracted are of a parent
4664 // class (this means we know implicitly that we are
4665 // being called to find out about members by a derived
4668 static bool InheritablePropertySignatureCompare (MemberInfo m, object filter_criteria)
4670 if (MemberSignatureCompare (m, filter_criteria)){
4671 PropertyInfo pi = (PropertyInfo) m;
4673 MethodInfo inherited_get = TypeManager.GetPropertyGetter (pi);
4674 MethodInfo inherited_set = TypeManager.GetPropertySetter (pi);
4676 MethodInfo mi = inherited_get == null ? inherited_set : inherited_get;
4678 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
4680 // If only accessible to the current class.
4681 if (prot == MethodAttributes.Private)
4684 // If only accessible to the defining assembly or
4685 if (prot == MethodAttributes.FamANDAssem ||
4686 prot == MethodAttributes.Assembly){
4687 if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
4693 // Anything else (FamOrAssembly and Public) is fine