3 // class.cs: Class and Struct handlers
5 // Authors: Miguel de Icaza (miguel@gnu.org)
6 // Martin Baulig (martin@gnome.org)
7 // Anirban Bhattacharjee (banirban@novell.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
14 // 2002-10-11 Miguel de Icaza <miguel@ximian.com>
16 // * class.cs: Following the comment from 2002-09-26 to AddMethod, I
17 // have fixed a remaining problem: not every AddXXXX was adding a
18 // fully qualified name.
20 // Now everyone registers a fully qualified name in the DeclSpace as
21 // being defined instead of the partial name.
23 // Downsides: we are slower than we need to be due to the excess
24 // copies and the names being registered this way.
26 // The reason for this is that we currently depend (on the corlib
27 // bootstrap for instance) that types are fully qualified, because
28 // we dump all the types in the namespace, and we should really have
29 // types inserted into the proper namespace, so we can only store the
30 // basenames in the defined_names array.
35 using System.Collections;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Runtime.CompilerServices;
39 using System.Diagnostics.SymbolStore;
41 namespace Mono.MonoBASIC {
44 /// This is the base class for structs and classes.
46 public class TypeContainer : DeclSpace, IMemberContainer {
47 // Holds a list of classes and structures
50 // Holds the list of properties
53 // Holds the list of enumerations
56 // Holds the list of delegates
59 // Holds the list of constructors
60 ArrayList instance_constructors;
62 // Holds the list of fields
65 // Holds a list of fields that have initializers
66 ArrayList initialized_fields;
68 // Holds a list of static fields that have initializers
69 ArrayList initialized_static_fields;
71 // Holds the list of constants
77 // Holds order in which interfaces must be closed
78 ArrayList interface_order;
89 // Holds the operators
92 // Holds AddHandlers stements for events
95 // The emit context for toplevel objects.
99 // Pointers to the default constructor and the default static constructor
101 Constructor default_constructor;
102 Constructor default_static_constructor;
105 // Whether we have seen a static constructor for this class or not
107 bool have_static_constructor = false;
110 // Whether we have at least one non-static field
112 bool have_nonstatic_fields = false;
115 // This one is computed after we can distinguish interfaces
116 // from classes from the arraylist `type_bases'
118 string base_class_name;
120 ArrayList type_bases;
122 // Attributes for this type
123 protected Attributes attributes;
125 // Information in the case we are an attribute type
127 public AttributeTargets Targets = AttributeTargets.All;
128 public bool AllowMultiple = false;
129 public bool Inherited;
131 // The interfaces we implement.
134 // The parent member container and our member cache
135 IMemberContainer parent_container;
136 MemberCache member_cache;
139 // The indexer name for this class
141 public string IndexerName;
143 public TypeContainer (TypeContainer parent, string name, Location l)
144 : base (parent, name, l)
147 types = new ArrayList ();
154 base_class_name = null;
156 //Console.WriteLine ("New class " + name + " inside " + n);
159 public AdditionResult AddConstant (Const constant)
162 string basename = constant.Name;
164 if ((res = IsValid (basename)) != AdditionResult.Success)
167 if (constants == null)
168 constants = new ArrayList ();
170 constants.Add (constant);
171 DefineName (Name + "." + basename, constant);
173 return AdditionResult.Success;
176 public AdditionResult AddEnum (Mono.MonoBASIC.Enum e)
180 if ((res = IsValid (e.Basename)) != AdditionResult.Success)
184 enums = new ArrayList ();
187 DefineName (e.Name, e);
189 return AdditionResult.Success;
192 public AdditionResult AddClass (Class c)
196 if ((res = IsValid (c.Basename)) != AdditionResult.Success)
201 DefineName (c.Name, c);
204 // FIXME: Do we really need to explicitly add an empty default static constructor?
205 // Apparently we don't
206 /* if (c.default_static_constructor == null)
208 bool isModule = c is Mono.MonoBASIC.Module;
209 Constructor dc = new Constructor ("New", Parameters.EmptyReadOnlyParameters, null, c.Location);
210 dc.ModFlags = isModule ? Modifiers.PUBLIC | Modifiers.STATIC : Modifiers.PUBLIC;
211 c.AddConstructor (dc);
214 //--------------------------------------------------------------
216 return AdditionResult.Success;
219 public AdditionResult AddStruct (Struct s)
223 if ((res = IsValid (s.Basename)) != AdditionResult.Success)
226 DefineName (s.Name, s);
229 return AdditionResult.Success;
232 public AdditionResult AddDelegate (Delegate d)
236 if ((res = IsValid (d.Basename)) != AdditionResult.Success)
239 if (delegates == null)
240 delegates = new ArrayList ();
242 DefineName (d.Name, d);
245 return AdditionResult.Success;
248 public AdditionResult AddMethod (Method method)
250 string basename = method.Name;
251 string fullname = Name + "." + basename;
253 Object value = defined_names [fullname];
255 if (value != null && (!(value is Method)))
256 return AdditionResult.NameExists;
258 if (basename == Basename)
259 return AdditionResult.EnclosingClash;
262 methods = new ArrayList ();
264 if (method.Name.IndexOf (".") != -1)
265 methods.Insert (0, method);
267 methods.Add (method);
270 DefineName (fullname, method);
272 return AdditionResult.Success;
275 public AdditionResult AddConstructor (Constructor c)
278 return AdditionResult.NotAConstructor;
280 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
283 have_static_constructor = true;
284 if (default_static_constructor != null){
285 Console.WriteLine ("I have a static constructor already");
286 Console.WriteLine (" " + default_static_constructor);
287 return AdditionResult.MethodExists;
290 default_static_constructor = c;
293 /*if (default_constructor != null)
294 return AdditionResult.MethodExists;*/
295 default_constructor = c;
298 if (instance_constructors == null)
299 instance_constructors = new ArrayList ();
301 instance_constructors.Add (c);
304 return AdditionResult.Success;
307 public AdditionResult AddInterface (Interface iface)
311 if ((res = IsValid (iface.Basename)) != AdditionResult.Success)
314 if (interfaces == null)
315 interfaces = new ArrayList ();
316 interfaces.Add (iface);
317 DefineName (iface.Name, iface);
319 return AdditionResult.Success;
322 public AdditionResult AddField (Field field)
325 string basename = field.Name;
327 if ((res = IsValid (basename)) != AdditionResult.Success)
331 fields = new ArrayList ();
335 if (field.HasInitializer){
336 if ((field.ModFlags & Modifiers.STATIC) != 0) {
337 if (initialized_static_fields == null)
338 initialized_static_fields = new ArrayList ();
340 initialized_static_fields.Add (field);
343 // We have not seen a static constructor,
344 // but we will provide static initialization of fields
346 have_static_constructor = true;
348 if (initialized_fields == null)
349 initialized_fields = new ArrayList ();
351 initialized_fields.Add (field);
355 if ((field.ModFlags & Modifiers.STATIC) == 0)
356 have_nonstatic_fields = true;
358 DefineName (Name + "." + basename, field);
359 return AdditionResult.Success;
362 public AdditionResult AddProperty (Property prop)
365 string basename = prop.Name;
367 if ((res = IsValid (basename)) != AdditionResult.Success)
370 if (properties == null)
371 properties = new ArrayList ();
373 if (prop.Name.IndexOf (".") != -1)
374 properties.Insert (0, prop);
376 properties.Add (prop);
377 DefineName (Name + "." + basename, prop);
379 return AdditionResult.Success;
382 public AdditionResult AddEvent (Event e)
385 string basename = e.Name;
387 if ((res = IsValid (basename)) != AdditionResult.Success)
391 events = new ArrayList ();
394 DefineName (Name + "." + basename, e);
396 return AdditionResult.Success;
399 public AdditionResult AddIndexer (Indexer i)
401 if (indexers == null)
402 indexers = new ArrayList ();
404 if (i.InterfaceType != null)
405 indexers.Insert (0, i);
409 return AdditionResult.Success;
412 public AdditionResult AddOperator (Operator op)
414 if (operators == null)
415 operators = new ArrayList ();
419 return AdditionResult.Success;
422 public AdditionResult AddEventHandler (Statement stmt)
424 if (handlers == null)
425 handlers = new ArrayList ();
428 return AdditionResult.Success;
431 public void RegisterOrder (Interface iface)
433 if (interface_order == null)
434 interface_order = new ArrayList ();
436 interface_order.Add (iface);
439 public ArrayList Types {
445 public ArrayList Methods {
451 public ArrayList Constants {
457 public ArrayList Interfaces {
465 return base_class_name;
469 public ArrayList Bases {
479 public ArrayList Fields {
489 public ArrayList InstanceConstructors {
491 return instance_constructors;
495 public ArrayList Properties {
501 public ArrayList Events {
507 public ArrayList Enums {
513 public ArrayList Indexers {
519 public ArrayList Operators {
525 public ArrayList Delegates {
531 public Attributes OptAttributes {
537 public bool HaveStaticConstructor {
539 return have_static_constructor;
543 public virtual TypeAttributes TypeAttr {
545 return Modifiers.TypeAttr (ModFlags, this);
549 public ArrayList EventHandlers {
556 // Emits the instance field initializers
558 public bool EmitFieldInitializers (EmitContext ec)
561 ILGenerator ig = ec.ig;
562 Expression instance_expr;
565 fields = initialized_static_fields;
566 instance_expr = null;
568 fields = initialized_fields;
569 instance_expr = new This (Location.Null).Resolve (ec);
575 foreach (Field f in fields){
576 Expression e = f.GetInitializerExpression (ec);
580 Location l = f.Location;
581 FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
582 fe.InstanceExpression = instance_expr;
583 Expression a = new Assign (fe, e, l);
589 if (a is ExpressionStatement)
590 ((ExpressionStatement) a).EmitStatement (ec);
592 throw new Exception ("Assign.Resolve returned a non ExpressionStatement");
600 // Defines the default constructors
602 void DefineDefaultConstructor (bool is_static)
607 c = new Constructor ("New", Parameters.EmptyReadOnlyParameters,
612 mods = Modifiers.STATIC;
616 c.Initializer = new ConstructorBaseInitializer (
617 null, Parameters.EmptyReadOnlyParameters,
622 c.Block = new Block (null);
626 public void ReportStructInitializedInstanceError ()
628 string n = TypeBuilder.FullName;
630 foreach (Field f in initialized_fields){
633 "`" + n + "." + f.Name + "': can not have " +
634 "instance field initializers in structs");
639 /// The pending methods that need to be implemented (interfaces or abstract methods)
641 public PendingImplementation Pending;
644 /// This function computes the Base class and also the
645 /// list of interfaces that the class or struct @c implements.
647 /// The return value is an array (might be null) of
648 /// interfaces implemented (as Types).
650 /// The @parent argument is set to the parent object or null
651 /// if this is `System.Object'.
653 Type [] GetClassBases (bool is_class, out Type parent, out bool error)
655 ArrayList bases = Bases;
664 parent = TypeManager.value_type;
668 if (RootContext.StdLib)
669 parent = TypeManager.object_type;
670 else if (Name != "System.Object")
671 parent = TypeManager.object_type;
674 // If we are compiling our runtime,
675 // and we are defining ValueType, then our
676 // parent is `System.Object'.
678 if (!RootContext.StdLib && Name == "System.ValueType")
679 parent = TypeManager.object_type;
686 // Bases should be null if there are no bases at all
691 Expression name = (Expression) bases [0];
692 name = ResolveTypeExpr (name, false, Location);
699 Type first = name.Type;
705 parent = TypeManager.object_type;
709 if (parent.IsSealed )
710 Report.Error (30299, Location,
711 "Class " + Name + " cannot inherit " +
712 "'NotInheritable' class " + TypeManager.MonoBASIC_Name (parent));
714 if (!AsAccessible (parent, ModFlags))
715 Report.Error (30389, Location,
716 "Inconsistent accessibility: base class `" +
717 TypeManager.MonoBASIC_Name (parent) + "' is less " +
718 "accessible than class `" +
725 Type [] ifaces = new Type [count-start];
727 for (i = start, j = 0; i < count; i++, j++){
728 Expression name = (Expression) bases [i];
729 Expression resolved = ResolveTypeExpr (name, false, Location);
730 bases [i] = resolved;
731 Type t = resolved.Type;
738 if (is_class == false && !t.IsInterface){
739 Report.Error (527, "In Struct `" + Name + "', type `"+
740 name +"' is not an interface");
747 Report.Error (30258, "class `"+ Name +
748 "': a class can not inherit from a struct/enum");
750 /*Report.Error (509, "class `"+ Name +
751 "': Cannot inherit from sealed class `"+
759 Report.Error (30121, Name + ": A class cannot inherit " +
760 "more than one class");
766 for (int x = 0; x < j; x++) {
767 if (t == ifaces [x]) {
768 Report.Error (528, "`" + name + "' is already listed in interface list");
777 return TypeManager.ExpandInterfaces (ifaces);
781 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
783 public override TypeBuilder DefineType ()
789 if (TypeBuilder != null)
802 ec = new EmitContext (this, Mono.MonoBASIC.Location.Null, null, null, ModFlags);
804 if (((ModFlags & Modifiers.ABSTRACT ) != 0) &&
805 ((ModFlags & Modifiers.SEALED) != 0)){
806 Report.Error (31408, Location,
807 "Class declared as 'MustInherit' cannot be declared as 'NotInheritable'");
810 ifaces = GetClassBases (is_class, out parent, out error);
815 if (this is Interface)
818 if (is_class && parent != null){
819 if (parent == TypeManager.enum_type ||
820 (parent == TypeManager.value_type && RootContext.StdLib) ||
821 parent == TypeManager.delegate_type ||
822 parent == TypeManager.array_type){
824 644, Location, "`" + Name + "' cannot inherit from " +
825 "special class `" + TypeManager.MonoBASIC_Name (parent) + "'");
830 if (!is_class && TypeManager.value_type == null)
831 throw new Exception ();
833 if (is_class && Parent.Parent == null && (!(this is Interface)))
\r
835 if ((ModFlags & Modifiers.PRIVATE) != 0)
836 Report.Error (31089, Location,
837 "Only internal classes can be declared as 'Private'");
839 if ((ModFlags & Modifiers.PROTECTED) != 0)
840 Report.Error (31047, Location,
841 "Only internal classes can be declared as 'Protected'");
844 if ((Parent is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
845 Report.Error (30735, Location,
846 "'Type' inside a 'Module' can not be " +
847 "declared as 'Protected'");
849 if ((Parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
850 Report.Error (30435, Location,
851 "'Type' inside a 'Structure' can not be " +
852 "declared as 'Protected'");
854 TypeAttributes type_attributes = TypeAttr;
856 // if (parent_builder is ModuleBuilder) {
858 ModuleBuilder builder = CodeGen.ModuleBuilder;
859 TypeBuilder = builder.DefineType (
860 Name, type_attributes, parent, ifaces);
863 TypeBuilder builder = Parent.TypeBuilder;
864 TypeBuilder = builder.DefineNestedType (
865 Basename, type_attributes, parent, ifaces);
870 // structure must contain atleast one member variable
871 if(!have_nonstatic_fields){
873 30281, Location, "Structure `" + Name + "' do not " +
874 "contain any member Variable");
876 /*TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
877 FieldAttributes.Private);*/
880 // add interfaces that were not added at type creation (weird API issue)
881 if (!have_nonstatic_fields && (ifaces != null)) {
882 foreach (Type i in ifaces)
883 TypeBuilder.AddInterfaceImplementation (i);
889 // Finish the setup for the EmitContext
891 ec.ContainerType = TypeBuilder;
893 TypeManager.AddUserType (Name, TypeBuilder, this, ifaces);
895 if ((parent != null) &&
896 (parent == TypeManager.attribute_type ||
897 parent.IsSubclassOf (TypeManager.attribute_type))) {
898 RootContext.RegisterAttribute (this);
899 TypeManager.RegisterAttrType (TypeBuilder, this);
901 RootContext.RegisterOrder (this);
903 if (Interfaces != null) {
904 foreach (Interface iface in Interfaces)
909 foreach (TypeContainer tc in Types)
913 if (Delegates != null) {
914 foreach (Delegate d in Delegates)
919 foreach (Enum en in Enums)
929 /// Defines the MemberCore objects that are in the `list' Arraylist
931 /// The `defined_names' array contains a list of members defined in
934 static ArrayList remove_list = new ArrayList ();
935 void DefineMembers (ArrayList list, MemberInfo [] defined_names)
939 // if one of the overloaded method is having
940 // Shadows or Overloads modifier all other should
941 // have the same modifier
942 Hashtable members = new Hashtable();
944 foreach (MemberCore mc in list)
\r
947 if(members[mc.Name] == null)
949 foreach (MemberCore m in list)
951 if(m.Name == mc.Name)
953 if ((m.ModFlags & Modifiers.SHADOWS) != 0)
955 modval = Modifiers.SHADOWS;
958 else if((m.ModFlags & Modifiers.NEW) != 0)
960 modval = Modifiers.NEW;
964 members.Add(mc.Name, modval);
967 modval = (int)members[mc.Name];
970 if(((modval & Modifiers.SHADOWS) != 0) && ((mc.ModFlags & Modifiers.SHADOWS) == 0))
973 "Function '" + mc.Name + "': must be declared 'Shadows' " +
974 "because another '" + mc.Name + "' declared 'Shadows'");
975 else if(((modval & Modifiers.NEW) != 0) && ((mc.ModFlags & Modifiers.NEW) == 0))
978 "Function '" + mc.Name + "': must be declared 'Overloads' " +
979 "because another '" + mc.Name + "' declared 'Overloads'");
983 remove_list.Clear ();
985 foreach (MemberCore mc in list){
986 if (!mc.Define (this)){
987 remove_list.Add (mc);
991 if (defined_names == null)
994 idx = Array.BinarySearch (defined_names, mc.Name, mif_compare);
996 if (RootContext.WarningLevel >= 4){
997 if ((mc.ModFlags & Modifiers.NEW) != 0)
998 Warning_KewywordNewNotRequired (mc.Location, mc);
999 if ((mc.ModFlags & Modifiers.SHADOWS) != 0)
1000 Warning_KewywordShadowsNotRequired (mc.Location, mc);
1005 MemberInfo match = defined_names [idx];
1007 if (match is PropertyInfo && ((mc.ModFlags & Modifiers.OVERRIDE) != 0))
1011 // If we are both methods, let the method resolution emit warnings
1013 if (match is MethodBase && mc is MethodCore)
1016 if (((mc.ModFlags & Modifiers.SHADOWS) == 0) && idx > 0)
1017 Warning_KeywordShadowsRequired (mc.Location, defined_names [idx]);
1021 foreach (object o in remove_list)
1024 remove_list.Clear ();
1028 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
1029 // class is consisten. Either it is `Item' or it is the name defined by all the
1030 // indexers with the `IndexerName' attribute.
1032 // Turns out that the IndexerNameAttribute is applied to each indexer,
1033 // but it is never emitted, instead a DefaultName attribute is attached
1036 void DefineIndexers ()
1038 string class_indexer_name = null;
1040 foreach (Indexer i in Indexers){
1045 name = i.IndexerName;
1047 if (i.InterfaceType != null)
1050 if (class_indexer_name == null){
1051 class_indexer_name = name;
1055 if (name == class_indexer_name)
1059 668, "Two indexers have different names, " +
1060 " you should use the same name for all your indexers");
1062 if (class_indexer_name == null)
1063 class_indexer_name = "Item";
1064 IndexerName = class_indexer_name;
1067 static void Error_KeywordNotAllowed (Location loc)
1069 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
1073 /// Populates our TypeBuilder with fields and methods
1075 public override bool DefineMembers (TypeContainer parent)
1077 MemberInfo [] defined_names = null;
1079 if (interface_order != null){
1080 foreach (Interface iface in interface_order)
1081 if ((iface.ModFlags & Modifiers.NEW) == 0)
1082 iface.DefineMembers (this);
1084 Error_KeywordNotAllowed (iface.Location);
1087 if (RootContext.WarningLevel > 1){
1091 // This code throws an exception in the comparer
1092 // I guess the string is not an object?
1094 ptype = TypeBuilder.BaseType;
1096 defined_names = (MemberInfo []) FindMembers (
1097 ptype, MemberTypes.All & ~MemberTypes.Constructor,
1098 BindingFlags.Public | BindingFlags.Instance |
1099 BindingFlags.Static, null, null);
1101 Array.Sort (defined_names, mif_compare);
1105 if (constants != null)
1106 DefineMembers (constants, defined_names);
1109 DefineMembers (fields, defined_names);
1111 if (this is Class && (!(this is Interface))){
1112 if (instance_constructors == null){
1113 if (default_constructor == null)
\r
1114 DefineDefaultConstructor (false);
1117 if (initialized_static_fields != null &&
1118 default_static_constructor == null)
1119 DefineDefaultConstructor (true);
1122 if (this is Struct){
1124 // Structs can not have initialized instance
1127 if (initialized_static_fields != null &&
1128 default_static_constructor == null)
1129 DefineDefaultConstructor (true);
1131 if (initialized_fields != null)
1132 ReportStructInitializedInstanceError ();
1135 Pending = PendingImplementation.GetPendingImplementations (this);
1138 // Constructors are not in the defined_names array
1140 if (instance_constructors != null)
\r
1141 DefineMembers (instance_constructors, null);
1143 if (default_static_constructor != null)
1144 default_static_constructor.Define (this);
1146 if (methods != null)
1147 DefineMembers (methods, defined_names);
1149 if (properties != null)
1150 DefineMembers (properties, defined_names);
1153 DefineMembers (events, defined_names);
1155 if (indexers != null) {
1158 IndexerName = "Item";
1160 if (operators != null){
1161 DefineMembers (operators, null);
1163 CheckPairedOperators ();
1167 DefineMembers (enums, defined_names);
1169 if (delegates != null)
1170 DefineMembers (delegates, defined_names);
1173 if (TypeBuilder.BaseType != null)
1174 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
1176 member_cache = new MemberCache (this);
1182 public override bool Define (TypeContainer parent)
1184 if (interface_order != null){
1185 foreach (Interface iface in interface_order)
1186 if ((iface.ModFlags & Modifiers.NEW) == 0)
1187 iface.Define (this);
1194 /// This function is based by a delegate to the FindMembers routine
1196 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1202 /// This filter is used by FindMembers, and we just keep
1203 /// a global for the filter to `AlwaysAccept'
1205 static MemberFilter accepting_filter;
1209 /// A member comparission method based on name only
1211 static IComparer mif_compare;
1213 static TypeContainer ()
1215 accepting_filter = new MemberFilter (AlwaysAccept);
1216 mif_compare = new MemberInfoCompare ();
1220 /// This method returns the members of this type just like Type.FindMembers would
1221 /// Only, we need to use this for types which are _being_ defined because MS'
1222 /// implementation can't take care of that.
1225 // FIXME: return an empty static array instead of null, that cleans up
1226 // some code and is consistent with some coding conventions I just found
1230 // Notice that in various cases we check if our field is non-null,
1231 // something that would normally mean that there was a bug elsewhere.
1233 // The problem happens while we are defining p-invoke methods, as those
1234 // will trigger a FindMembers, but this happens before things are defined
1236 // Since the whole process is a no-op, it is fine to check for null here.
1238 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1239 MemberFilter filter, object criteria)
1241 ArrayList members = new ArrayList ();
1244 if ((bf & BindingFlags.Public) != 0)
1245 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1247 if ((bf & BindingFlags.NonPublic) != 0)
1248 modflags |= Modifiers.PRIVATE;
1250 int static_mask = 0, static_flags = 0;
1251 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1252 case BindingFlags.Static:
1253 static_mask = static_flags = Modifiers.STATIC;
1256 case BindingFlags.Instance:
1257 static_mask = Modifiers.STATIC;
1262 static_mask = static_flags = 0;
1266 Timer.StartTimer (TimerType.TcFindMembers);
1269 filter = accepting_filter;
1271 if ((mt & MemberTypes.Field) != 0) {
1272 if (fields != null) {
1273 foreach (Field f in fields) {
1274 if ((f.ModFlags & modflags) == 0)
1276 if ((f.ModFlags & static_mask) != static_flags)
1279 FieldBuilder fb = f.FieldBuilder;
1280 if (fb != null && filter (fb, criteria) == true)
1285 if (constants != null) {
1286 foreach (Const con in constants) {
1287 if ((con.ModFlags & modflags) == 0)
1289 if ((con.ModFlags & static_mask) != static_flags)
1292 FieldBuilder fb = con.FieldBuilder;
1293 if (fb != null && filter (fb, criteria) == true)
1299 if ((mt & MemberTypes.Method) != 0) {
1300 if (methods != null) {
1301 foreach (Method m in methods) {
1302 if ((m.ModFlags & modflags) == 0)
1304 if ((m.ModFlags & static_mask) != static_flags)
1307 MethodBuilder mb = m.MethodBuilder;
1309 if (mb != null && filter (mb, criteria) == true)
1314 if (operators != null){
1315 foreach (Operator o in operators) {
1316 if ((o.ModFlags & modflags) == 0)
1318 if ((o.ModFlags & static_mask) != static_flags)
1321 MethodBuilder ob = o.OperatorMethodBuilder;
1322 if (ob != null && filter (ob, criteria) == true)
1327 if (properties != null){
1328 foreach (Property p in properties){
1329 if ((p.ModFlags & modflags) == 0)
1331 if ((p.ModFlags & static_mask) != static_flags)
1337 if (b != null && filter (b, criteria) == true)
1341 if (b != null && filter (b, criteria) == true)
1346 if (indexers != null){
1347 foreach (Indexer ix in indexers){
1348 if ((ix.ModFlags & modflags) == 0)
1350 if ((ix.ModFlags & static_mask) != static_flags)
1356 if (b != null && filter (b, criteria) == true)
1360 if (b != null && filter (b, criteria) == true)
1366 if ((mt & MemberTypes.Event) != 0) {
1368 foreach (Event e in events) {
1369 if ((e.ModFlags & modflags) == 0)
1371 if ((e.ModFlags & static_mask) != static_flags)
1374 MemberInfo eb = e.EventBuilder;
1375 if (eb != null && filter (eb, criteria) == true)
1376 members.Add (e.EventBuilder);
1380 if ((mt & MemberTypes.Property) != 0){
1381 if (properties != null)
1382 foreach (Property p in properties) {
1383 if ((p.ModFlags & modflags) == 0)
1385 if ((p.ModFlags & static_mask) != static_flags)
1388 MemberInfo pb = p.PropertyBuilder;
1390 if (pb != null && filter (pb, criteria) == true) {
1391 members.Add (p.PropertyBuilder);
1395 if (indexers != null)
1396 foreach (Indexer ix in indexers) {
1397 if ((ix.ModFlags & modflags) == 0)
1399 if ((ix.ModFlags & static_mask) != static_flags)
1402 MemberInfo ib = ix.PropertyBuilder;
1403 if (ib != null && filter (ib, criteria) == true) {
1404 members.Add (ix.PropertyBuilder);
1409 if ((mt & MemberTypes.NestedType) != 0) {
1411 foreach (TypeContainer t in types) {
1412 if ((t.ModFlags & modflags) == 0)
1415 TypeBuilder tb = t.TypeBuilder;
1416 if (tb != null && (filter (tb, criteria) == true))
1422 foreach (Enum en in enums){
1423 if ((en.ModFlags & modflags) == 0)
1426 TypeBuilder tb = en.TypeBuilder;
1427 if (tb != null && (filter (tb, criteria) == true))
1432 if (delegates != null){
1433 foreach (Delegate d in delegates){
1434 if ((d.ModFlags & modflags) == 0)
1437 TypeBuilder tb = d.TypeBuilder;
1438 if (tb != null && (filter (tb, criteria) == true))
1443 if (interfaces != null){
1444 foreach (Interface iface in interfaces){
1445 if ((iface.ModFlags & modflags) == 0)
1448 TypeBuilder tb = iface.TypeBuilder;
1449 if (tb != null && (filter (tb, criteria) == true))
1455 if ((mt & MemberTypes.Constructor) != 0){
1456 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1457 foreach (Constructor c in instance_constructors){
1458 ConstructorBuilder cb = c.ConstructorBuilder;
1460 if (filter (cb, criteria) == true)
1465 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1466 ConstructorBuilder cb =
1467 default_static_constructor.ConstructorBuilder;
1470 if (filter (cb, criteria) == true)
1476 // Lookup members in parent if requested.
1478 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
1479 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1480 members.AddRange (list);
1483 Timer.StopTimer (TimerType.TcFindMembers);
1485 return new MemberList (members);
1488 public override MemberCache MemberCache {
1490 return member_cache;
1494 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1495 MemberFilter filter, object criteria)
1497 TypeContainer tc = TypeManager.LookupTypeContainer (t);
1500 return tc.FindMembers (mt, bf, filter, criteria);
1502 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1506 // FindMethods will look for methods not only in the type `t', but in
1507 // any interfaces implemented by the type.
1509 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
1510 MemberFilter filter, object criteria)
1516 /// Emits the values for the constants
1518 public void EmitConstants ()
1520 if (constants != null)
1521 foreach (Const con in constants)
1522 con.EmitConstant (this);
1527 /// Emits the code, this step is performed after all
1528 /// the types, enumerations, constructors
1532 if (instance_constructors != null)
1533 foreach (Constructor c in instance_constructors)
1536 if (default_static_constructor != null)
1537 default_static_constructor.Emit (this);
1539 if (methods != null)
1540 foreach (Method m in methods)
1543 if (operators != null)
1544 foreach (Operator o in operators)
1547 if (properties != null)
1548 foreach (Property p in properties)
1551 if (indexers != null){
1552 foreach (Indexer ix in indexers)
1555 /*CustomAttributeBuilder cb = Interface.EmitDefaultMemberAttr (
1556 this, IndexerName, ModFlags, Location);
1557 TypeBuilder.SetCustomAttribute (cb);*/
1561 foreach (Field f in fields)
1564 if (events != null){
1565 foreach (Event e in Events)
1569 if (Pending != null)
1570 if (Pending.VerifyPendingMethods ())
1573 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
1576 // Check for internal or private fields that were never assigned
1578 if (fields != null && RootContext.WarningLevel >= 3) {
1579 foreach (Field f in fields) {
1580 if ((f.ModFlags & Modifiers.PUBLIC) != 0)
1585 169, f.Location, "Private field " +
1586 MakeName (f.Name) + " is never used");
1591 // Only report 649 on level 4
1593 if (RootContext.WarningLevel < 4)
1596 if ((f.status & Field.Status.ASSIGNED) != 0)
1601 "Field " + MakeName (f.Name) + " is never assigned " +
1602 " to and will always have its default value");
1606 // if (types != null)
1607 // foreach (TypeContainer tc in types)
1611 public override void CloseType ()
1616 TypeBuilder.CreateType ();
1618 } catch (TypeLoadException){
1620 // This is fine, the code still created the type
1622 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
1623 // Console.WriteLine (e.Message);
1625 Console.WriteLine ("In type: " + Name);
1630 foreach (Enum en in Enums)
1633 if (interface_order != null){
1634 foreach (Interface iface in interface_order)
1639 foreach (TypeContainer tc in Types)
1643 foreach (TypeContainer tc in Types)
1644 if (!(tc is Struct))
1648 if (Delegates != null)
1649 foreach (Delegate d in Delegates)
1653 public string MakeName (string n)
1655 return "`" + Name + "." + n + "'";
1658 public void Warning_KeywordShadowsRequired (Location l, MemberInfo mi)
1661 108, l, "The keyword 'Shadows' is required on " +
1662 MakeName (mi.Name) + " because it shadows `" +
1663 mi.ReflectedType.Name + "." + mi.Name + "'");
1666 public void Warning_KewywordShadowsNotRequired (Location l, MemberCore mc)
1669 109, l, "The member " + MakeName (mc.Name) + " does not hide an " +
1670 "inherited member, the keyword shadows is not required");
1673 public void Warning_KewywordNewNotRequired (Location l, MemberCore mc)
1676 109, l, "The member " + MakeName (mc.Name) + " does not hide an " +
1677 "inherited member, the keyword new is not required");
1680 public static int CheckMember (string name, MemberInfo mi, int ModFlags)
1686 // Performs the validation on a Method's modifiers (properties have
1687 // the same properties).
1689 public bool MethodModifiersValid (int flags, string n, Location loc)
1691 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
1692 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
1693 const int nv = (Modifiers.SHADOWS | Modifiers.VIRTUAL);
1695 string name = MakeName (n);
1698 // At most one of static, virtual or override
1700 if ((flags & Modifiers.STATIC) != 0){
1701 if ((flags & vao) != 0){
1703 30501, loc, "Shared method " + name + " can not be " +
1704 "declared as Overridable");
1709 if (this is Struct){
1710 if ((flags & va) != 0){
1711 Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
1716 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & Modifiers.VIRTUAL) != 0)
\r
1720 ": Methods marked as Overrides cannot be made Overridable");
1724 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & Modifiers.SHADOWS) != 0){
1727 ": Methods marked as Overrides cannot be marked as Shadows");
1732 // If the declaration includes the abstract modifier, then the
1733 // declaration does not include static, virtual or extern
1735 if ((flags & Modifiers.ABSTRACT) != 0){
1736 if ((flags & Modifiers.EXTERN) != 0){
1738 180, loc, name + " can not be both abstract and extern");
1742 if ((flags & Modifiers.VIRTUAL) != 0){
1744 503, loc, name + " can not be both abstract and virtual");
1748 if((ModFlags & Modifiers.SEALED) != 0){
1751 "Class declared as 'NotInheritable' " +
1752 "cannot have a 'MustOverride' member");
1755 else if ((ModFlags & Modifiers.ABSTRACT) == 0){
1758 " is declared as 'MustOverride', hence its container " +
1759 "class should be declared as 'MustInherit'");
1765 if ((flags & Modifiers.PRIVATE) != 0){
1766 if ((flags & vao) != 0){
1769 ": Members marked as Overridable or Overrides can not be Private");
1774 if ((flags & Modifiers.SEALED) != 0){
1775 if ((flags & Modifiers.OVERRIDE) == 0){
1778 ": cannot be sealed because it is not an override");
1782 if ((flags & Modifiers.NEW) != 0){
1783 if ((flags & Modifiers.SHADOWS) != 0){
1786 " 'Overloads' and 'Shadows' cannot be combined ");
1794 // Access level of a type.
1797 ProtectedInternal = 1,
1803 // Check whether `flags' denotes a more restricted access than `level'
1804 // and return the new level.
1805 static AccessLevel CheckAccessLevel (AccessLevel level, int flags)
1807 AccessLevel old_level = level;
1809 if ((flags & Modifiers.INTERNAL) != 0) {
1810 if ((flags & Modifiers.PROTECTED) != 0) {
1811 if ((int) level < (int) AccessLevel.ProtectedInternal)
1812 level = AccessLevel.ProtectedInternal;
1814 if ((int) level < (int) AccessLevel.Internal)
1815 level = AccessLevel.Internal;
1817 } else if ((flags & Modifiers.PROTECTED) != 0) {
1818 if ((int) level < (int) AccessLevel.Protected)
1819 level = AccessLevel.Protected;
1820 } else if ((flags & Modifiers.PRIVATE) != 0)
1821 level = AccessLevel.Private;
1826 // Return the access level for a new member which is defined in the current
1827 // TypeContainer with access modifiers `flags'.
1828 AccessLevel GetAccessLevel (int flags)
1830 if ((flags & Modifiers.PRIVATE) != 0)
1831 return AccessLevel.Private;
1834 if (!IsTopLevel && (Parent != null))
1835 level = Parent.GetAccessLevel (flags);
1837 level = AccessLevel.Public;
1839 return CheckAccessLevel (CheckAccessLevel (level, flags), ModFlags);
1842 // Return the access level for type `t', but don't give more access than `flags'.
1843 static AccessLevel GetAccessLevel (Type t, int flags)
1845 if (((flags & Modifiers.PRIVATE) != 0) || t.IsNestedPrivate)
1846 return AccessLevel.Private;
1849 if (TypeManager.IsBuiltinType (t))
1850 return AccessLevel.Public;
1851 else if ((t.DeclaringType != null) && (t != t.DeclaringType))
1852 level = GetAccessLevel (t.DeclaringType, flags);
1854 level = CheckAccessLevel (AccessLevel.Public, flags);
1857 if (t.IsNestedPublic)
1860 if (t.IsNestedAssembly || t.IsNotPublic) {
1861 if ((int) level < (int) AccessLevel.Internal)
1862 level = AccessLevel.Internal;
1865 if (t.IsNestedFamily) {
1866 if ((int) level < (int) AccessLevel.Protected)
1867 level = AccessLevel.Protected;
1870 if (t.IsNestedFamORAssem) {
1871 if ((int) level < (int) AccessLevel.ProtectedInternal)
1872 level = AccessLevel.ProtectedInternal;
1879 // Returns true if `parent' is as accessible as the flags `flags'
1880 // given for this member.
1882 public bool AsAccessible (Type parent, int flags)
1884 while (parent.IsArray || parent.IsPointer || parent.IsByRef)
1885 parent = parent.GetElementType ();
1887 AccessLevel level = GetAccessLevel (flags);
1888 AccessLevel level2 = GetAccessLevel (parent, flags);
1890 return (int) level >= (int) level2;
1893 Hashtable builder_and_args;
1895 public bool RegisterMethod (MethodBuilder mb, InternalParameters ip, Type [] args)
1897 if (builder_and_args == null)
1898 builder_and_args = new Hashtable ();
1903 /// Performs checks for an explicit interface implementation. First it
1904 /// checks whether the `interface_type' is a base inteface implementation.
1905 /// Then it checks whether `name' exists in the interface type.
1907 public bool VerifyImplements (Type interface_type, string full, string name, Location loc)
1911 if (ifaces != null){
1912 foreach (Type t in ifaces){
1913 if (t == interface_type){
1921 Report.Error (540, "`" + full + "': containing class does not implement interface `" + interface_type.FullName + "'");
1928 public static void Error_NotInterfaceMember (Location loc, string member_name, string iface_name)
1930 Report.Error (30401, loc, "'" + member_name + "' is not a member of the interface '" + iface_name + "'");
1937 string IMemberContainer.Name {
1943 Type IMemberContainer.Type {
1949 IMemberContainer IMemberContainer.Parent {
1951 return parent_container;
1955 MemberCache IMemberContainer.MemberCache {
1957 return member_cache;
1961 bool IMemberContainer.IsInterface {
1963 return this is Interface;
1967 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
1969 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
1973 // Operator pair checking
1976 class OperatorEntry {
1978 public Type ret_type;
1979 public Type type1, type2;
1981 public Operator.OpType ot;
1983 public OperatorEntry (int f, Operator o)
1987 ret_type = o.OperatorMethod.GetReturnType ();
1988 Type [] pt = o.OperatorMethod.ParameterTypes;
1992 ot = o.OperatorType;
1995 public override int GetHashCode ()
1997 return ret_type.GetHashCode ();
2000 public override bool Equals (object o)
2002 OperatorEntry other = (OperatorEntry) o;
2004 if (other.ret_type != ret_type)
2006 if (other.type1 != type1)
2008 if (other.type2 != type2)
2015 // Checks that some operators come in pairs:
2020 // They are matched based on the return type and the argument types
2022 void CheckPairedOperators ()
2024 Hashtable pairs = new Hashtable (null, null);
2026 // Register all the operators we care about.
2027 foreach (Operator op in operators){
2030 switch (op.OperatorType){
2031 case Operator.OpType.Equality:
2033 case Operator.OpType.Inequality:
2036 case Operator.OpType.GreaterThan:
2038 case Operator.OpType.LessThan:
2041 case Operator.OpType.GreaterThanOrEqual:
2043 case Operator.OpType.LessThanOrEqual:
2049 OperatorEntry oe = new OperatorEntry (reg, op);
2051 object o = pairs [oe];
2055 oe = (OperatorEntry) o;
2061 // Look for the mistakes.
2063 foreach (DictionaryEntry de in pairs){
2064 OperatorEntry oe = (OperatorEntry) de.Key;
2071 case Operator.OpType.Equality:
2074 case Operator.OpType.Inequality:
2077 case Operator.OpType.GreaterThan:
2080 case Operator.OpType.LessThan:
2083 case Operator.OpType.GreaterThanOrEqual:
2086 case Operator.OpType.LessThanOrEqual:
2090 Report.Error (216, oe.op.Location,
2091 "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
2098 public class Class : TypeContainer {
2100 // Modifiers allowed in a class declaration
2102 public const int AllowedModifiers =
2105 Modifiers.PROTECTED |
2106 Modifiers.INTERNAL |
2108 Modifiers.ABSTRACT |
2111 public Class (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
2112 : base (parent, name, l)
2116 if (parent.Parent == null)
2117 accmods = Modifiers.INTERNAL;
2119 accmods = Modifiers.PUBLIC;
2121 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2122 this.attributes = attrs;
2126 // FIXME: How do we deal with the user specifying a different
2129 public override TypeAttributes TypeAttr {
2131 return base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2136 public class Struct : TypeContainer {
2138 // Modifiers allowed in a struct declaration
2140 public const int AllowedModifiers =
2143 Modifiers.PROTECTED |
2144 Modifiers.INTERNAL |
2148 public Struct (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
2149 : base (parent, name, l)
2153 if (parent.Parent == null)
2154 accmods = Modifiers.INTERNAL;
2156 accmods = Modifiers.PUBLIC;
2158 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2160 this.ModFlags |= Modifiers.SEALED;
2161 this.attributes = attrs;
2166 // FIXME: Allow the user to specify a different set of attributes
2167 // in some cases (Sealed for example is mandatory for a class,
2168 // but what SequentialLayout can be changed
2170 public override TypeAttributes TypeAttr {
2172 return base.TypeAttr |
2173 TypeAttributes.SequentialLayout |
2174 TypeAttributes.Sealed |
2175 TypeAttributes.BeforeFieldInit;
2180 public abstract class MethodCore : MemberBase {
2181 public /* readonly */ Parameters Parameters;
2185 // Parameters, cached for semantic analysis.
2187 protected InternalParameters parameter_info;
2188 protected Type [] parameter_types;
2190 // Whether this is an operator
2191 public bool IsOperator;
2193 public MethodCore (Expression type, int mod, int allowed_mod, string name,
2194 Attributes attrs, Parameters parameters, Location loc)
2195 : base (type, mod, allowed_mod, name, attrs, loc)
2197 Parameters = parameters;
2201 // Returns the System.Type array for the parameters of this method
2203 public Type [] ParameterTypes {
2205 return parameter_types;
2209 public InternalParameters ParameterInfo
2212 return parameter_info;
2216 public Block Block {
2226 protected virtual bool DoDefineParameters (TypeContainer parent)
2228 // Check if arguments were correct
2229 parameter_types = Parameters.GetParameterInfo (parent);
2230 if ((parameter_types == null) || !CheckParameters (parent, parameter_types))
2233 parameter_info = new InternalParameters (parent, Parameters);
2238 public CallingConventions GetCallingConvention (bool is_class)
2240 CallingConventions cc = 0;
2242 cc = Parameters.GetCallingConvention ();
2245 if ((ModFlags & Modifiers.STATIC) == 0)
2246 cc |= CallingConventions.HasThis;
2248 // FIXME: How is `ExplicitThis' used in C#?
2253 public void LabelParameters (EmitContext ec, Type [] parameters, MethodBase builder)
2255 LabelParameters (ec, parameters, builder, null);
2258 public void LabelParameters (EmitContext ec, Type [] parameters, MethodBase builder, Parameters p_params)
2261 // Define each type attribute (in/out/ref) and
2262 // the argument names.
2264 Parameter [] p = p_params == null ? Parameters.FixedParameters : p_params.FixedParameters;
2267 MethodBuilder mb = null;
2268 ConstructorBuilder cb = null;
2270 if (builder is MethodBuilder)
2271 mb = (MethodBuilder) builder;
2273 cb = (ConstructorBuilder) builder;
2276 for (i = 0; i < p.Length; i++) {
2277 ParameterBuilder pb;
2280 pb = cb.DefineParameter (
2281 i + 1, p [i].Attributes, p [i].Name);
2283 pb = mb.DefineParameter (
2284 i + 1, p [i].Attributes, p [i].Name);
2286 Attributes attr = p [i].OptAttributes;
2288 Attribute.ApplyAttributes (ec, pb, pb, attr, Location);
2292 if (Parameters.ArrayParameter != null){
2293 ParameterBuilder pb;
2294 Parameter array_param = Parameters.ArrayParameter;
2297 pb = cb.DefineParameter (
2298 i + 1, array_param.Attributes,
2301 pb = mb.DefineParameter (
2302 i + 1, array_param.Attributes,
2305 CustomAttributeBuilder a = new CustomAttributeBuilder (
2306 TypeManager.cons_param_array_attribute, new object [0]);
2308 pb.SetCustomAttribute (a);
2313 public class Method : MethodCore {
2314 public MethodBuilder MethodBuilder;
2315 public MethodData MethodData;
2318 /// Modifiers allowed in a class declaration
2320 const int AllowedModifiers =
2323 Modifiers.PROTECTED |
2324 Modifiers.INTERNAL |
2328 Modifiers.NONVIRTUAL |
2329 Modifiers.OVERRIDE |
2330 Modifiers.ABSTRACT |
2336 // return_type can be "null" for VOID values.
2338 public Method (Expression return_type, int mod, string name, Parameters parameters,
2339 Attributes attrs, Location l)
2340 : base (return_type, mod, AllowedModifiers, name, attrs, parameters, l)
2345 public Method (Expression return_type, int mod, string name, Parameters parameters,
2346 Attributes attrs, ArrayList impl_what, Location l)
2347 : base (return_type, mod, AllowedModifiers, name, attrs, parameters, l)
2349 Implements = impl_what;
2353 // Returns the `System.Type' for the ReturnType of this
2354 // function. Provides a nice cache. (used between semantic analysis
2355 // and actual code generation
2357 public Type GetReturnType ()
2362 void DuplicateEntryPoint (MethodInfo b, Location location)
2366 "Program `" + CodeGen.FileName +
2367 "' has more than one entry point defined: `" +
2368 TypeManager.MonoBASIC_Signature(b) + "'");
2371 void Report28 (MethodInfo b)
2373 if (RootContext.WarningLevel < 4)
2378 "`" + TypeManager.MonoBASIC_Signature(b) +
2379 "' has the wrong signature to be an entry point");
2382 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
2384 if (b.ReturnType != TypeManager.void_type &&
2385 b.ReturnType != TypeManager.int32_type)
2388 if (pinfo.Count == 0)
2391 if (pinfo.Count > 1)
2394 Type t = pinfo.ParameterType(0);
2396 (t.GetArrayRank() == 1) &&
2397 (t.GetElementType() == TypeManager.string_type) &&
2398 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
2405 // Checks our base implementation if any
2407 protected override bool CheckBase (TypeContainer parent)
2409 // Check whether arguments were correct.
2410 if (!DoDefineParameters (parent))
2413 MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
2417 mi_this = TypeContainer.FindMembers (
2418 parent.TypeBuilder, MemberTypes.Method,
2419 BindingFlags.NonPublic | BindingFlags.Public |
2420 BindingFlags.Static | BindingFlags.Instance |
2421 BindingFlags.DeclaredOnly,
2422 MethodSignature.method_signature_filter, ms);
2424 if (mi_this.Count > 0) {
2425 Report.Error (111, Location, "Class `" + parent.Name + "' " +
2426 "already defines a member called `" + Name + "' " +
2427 "with the same parameter types");
2433 // Verify if the parent has a type with the same name, and then
2434 // check whether we have to create a new slot for it or not.
2436 Type ptype = parent.TypeBuilder.BaseType;
2438 // ptype is only null for System.Object while compiling corlib.
2440 MemberList mi, mi_static, mi_instance;
2442 mi_static = TypeContainer.FindMembers (
2443 ptype, MemberTypes.Method,
2444 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
2445 MethodSignature.inheritable_method_signature_filter, ms);
2447 mi_instance = TypeContainer.FindMembers (
2448 ptype, MemberTypes.Method,
2449 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
2450 MethodSignature.inheritable_method_signature_filter,
2453 if (mi_instance.Count > 0){
2455 } else if (mi_static.Count > 0)
2460 if (mi != null && mi.Count > 0){
2461 parent_method = (MethodInfo) mi [0];
2462 string name = parent_method.DeclaringType.Name + "." +
2465 if (!CheckMethodAgainstBase (parent, flags, parent_method, name))
2468 if ((ModFlags & Modifiers.NEW) == 0) {
2469 Type parent_ret = TypeManager.TypeToCoreType (
2470 parent_method.ReturnType);
2472 if (parent_ret != MemberType) {
2474 508, parent.MakeName (Name) + ": cannot " +
2475 "change return type when overriding " +
2476 "inherited member " + name);
2481 /*if ((ModFlags & Modifiers.NEW) != 0)
2482 WarningNotHiding (parent);*/
2484 if ((ModFlags & Modifiers.OVERRIDE) != 0){
2485 Report.Error (30284, Location,
2486 parent.MakeName (Name) +
2487 " : No suitable methods found to override");
2489 if ((ModFlags & ( Modifiers.NEW | Modifiers.SHADOWS | Modifiers.OVERRIDE )) == 0)
\r
2491 if ((ModFlags & Modifiers.NONVIRTUAL) != 0)
\r
2493 Report.Error (31088, Location,
2494 parent.MakeName (Name) + " : Cannot " +
2495 "be declared NotOverridable since this method is " +
2496 "not maked as Overrides");
2499 // if a member of module is not inherited from Object class
2500 // can not be declared protected
2501 if ((parent is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
2502 Report.Error (31066, Location,
2503 "'Sub' or 'Function' inside a 'Module' can not be declared as " +
2504 "'Protected' or 'Protected Friend'");
2507 /* else if ((ModFlags & Modifiers.NEW) != 0)
2508 WarningNotHiding (parent);
2517 public override bool Define (TypeContainer parent)
2519 if (!DoDefine (parent))
2522 if (!CheckBase (parent))
2525 if ((parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
2526 Report.Error (31067, Location,
2527 "'Sub' or 'Function' inside a 'Structure' can not be declared as " +
2528 "'Protected' or 'Protected Friend'");
2530 CallingConventions cc = GetCallingConvention (parent is Class);
2532 MethodData = new MethodData (this, null, MemberType, ParameterTypes,
2533 ParameterInfo, cc, OptAttributes,
2534 ModFlags, flags, true);
2536 if (!MethodData.Define (parent))
2539 MethodBuilder = MethodData.MethodBuilder;
2542 // This is used to track the Entry Point,
2544 if (Name.ToUpper() == "MAIN" &&
2545 ((ModFlags & Modifiers.STATIC) != 0) &&
2546 (RootContext.MainClass == null ||
2547 RootContext.MainClass == parent.TypeBuilder.FullName ||
2548 (RootContext.RootNamespace != null &&
2549 RootContext.RootNamespace.Length > 0 &&
2550 (RootContext.RootNamespace + "." + RootContext.MainClass) == parent.TypeBuilder.FullName))) {
2551 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
2552 if (RootContext.EntryPoint == null) {
2553 RootContext.EntryPoint = MethodBuilder;
2554 RootContext.EntryPointLocation = Location;
2556 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
2557 DuplicateEntryPoint (MethodBuilder, Location);
2560 Report28(MethodBuilder);
2569 public void Emit (TypeContainer parent)
2571 MethodData.Emit (parent, Block, this);
2575 public abstract class ConstructorInitializer {
2576 ArrayList argument_list;
2577 ConstructorInfo parent_constructor;
2578 Parameters parameters;
2580 public bool implicit_initialization;
2582 public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
2585 this.argument_list = argument_list;
2586 this.parameters = parameters;
2588 this.implicit_initialization = false;
2591 public ArrayList Arguments {
2593 return argument_list;
2597 public ConstructorInfo ParentConstructor
2601 return parent_constructor;
2605 public bool Resolve (EmitContext ec)
2607 Expression parent_constructor_group;
2610 ec.CurrentBlock = new Block (null, true, parameters);
2612 if (argument_list != null){
2613 foreach (Argument a in argument_list){
2614 if (!a.Resolve (ec, loc))
2619 ec.CurrentBlock = null;
2621 if (this is ConstructorBaseInitializer) {
2622 if (ec.ContainerType.BaseType == null)
2625 t = ec.ContainerType.BaseType;
2626 if (ec.ContainerType.IsValueType){
2627 Report.Error (522, loc,
2628 "structs cannot call base class constructors");
2633 t = ec.ContainerType;
2635 parent_constructor_group = Expression.MemberLookup (
2637 MemberTypes.Constructor,
2638 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
2641 if (parent_constructor_group == null){
2642 Report.Error (30455, loc, "Class '" + t + "' can not find a constructor for this argument list" );
2646 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (ec,
2647 (MethodGroupExpr) parent_constructor_group, argument_list, loc);
2649 if (parent_constructor == null) {
2650 if (this.implicit_initialization)
2651 Report.Error (30148, loc, "Must declare 'MyBase.New' in the constructor " +
\r
2652 "of the class '" + ec.TypeContainer.Name + "' with appropriate arguments, since the base class '" +
\r
2653 t.FullName + "' does not contain a definition of 'New' without any parameter");
2655 Report.Error (30455, loc, "Class '" + t + "' can not find a constructor for this argument list" );
2663 public void Emit (EmitContext ec)
2665 if (parent_constructor != null){
2667 Invocation.EmitCall (ec, true, true, null, parent_constructor, argument_list, loc);
2669 Invocation.EmitCall (ec, true, false, ec.This, parent_constructor, argument_list, loc);
2676 public class ConstructorBaseInitializer : ConstructorInitializer {
2677 public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
2678 base (argument_list, pars, l)
2683 public class ConstructorThisInitializer : ConstructorInitializer {
2684 public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
2685 base (argument_list, pars, l)
2690 public class Constructor : MethodCore {
2691 public ConstructorBuilder ConstructorBuilder;
2692 public ConstructorInitializer Initializer;
2693 new public Attributes OptAttributes;
2696 // Modifiers allowed for a constructor.
2698 public const int AllowedModifiers =
2700 Modifiers.PROTECTED |
2701 Modifiers.INTERNAL |
2708 // The spec claims that static is not permitted, but
2709 // my very own code has static constructors.
2711 public Constructor (string name, Parameters args, ConstructorInitializer init, Location l)
2712 : base (null, 0, AllowedModifiers, name, null, args, l)
2717 public Constructor (string name, int mod, Parameters args, ConstructorInitializer init, Location l)
2718 : base (null, mod, AllowedModifiers, name, null, args, l)
2724 // Returns true if this is a default constructor
2726 public bool IsDefault ()
2728 if ((ModFlags & Modifiers.STATIC) != 0)
2729 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
2730 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
2733 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
2734 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
2735 (Initializer is ConstructorBaseInitializer) &&
2736 (Initializer.Arguments == null);
2740 // Creates the ConstructorBuilder
2742 public override bool Define (TypeContainer parent)
2744 MethodAttributes ca = (MethodAttributes.RTSpecialName |
2745 MethodAttributes.SpecialName);
2747 if (parent.EventHandlers != null) {
2748 ArrayList hdlrs = parent.EventHandlers;
2749 foreach(Statement stmt in hdlrs)
2750 this.Block.AddStatement (stmt);
2754 // Check if arguments were correct.
2755 if (!DoDefineParameters (parent))
2758 if ((ModFlags & Modifiers.STATIC) != 0) {
2759 ca |= MethodAttributes.Static;
2761 if (this.Parameters != Parameters.EmptyReadOnlyParameters)
2764 "Shared constructor can not have parameters");
2766 if ((ModFlags & Modifiers.Accessibility) != 0)
2769 "Shared constructor can not be declared " +
2770 "explicitly as public, private, friend or protected");
2772 if (this.Initializer != null)
2775 "Keywords like MyBase, MyClass, Me are not " +
2776 "valid inside a Shared Constructor");
2779 if (parent is Struct && ParameterTypes.Length == 0) {
2782 "Structs can not contain explicit parameterless " +
2786 ca |= MethodAttributes.HideBySig;
2788 if ((ModFlags & Modifiers.PUBLIC) != 0)
2789 ca |= MethodAttributes.Public;
2790 else if ((ModFlags & Modifiers.PROTECTED) != 0) {
2791 if ((ModFlags & Modifiers.INTERNAL) != 0)
2792 ca |= MethodAttributes.FamORAssem;
2794 ca |= MethodAttributes.Family;
2796 else if ((ModFlags & Modifiers.INTERNAL) != 0)
2797 ca |= MethodAttributes.Assembly;
2798 else if (IsDefault ())
2799 ca |= MethodAttributes.Public;
2801 ca |= MethodAttributes.Private;
2804 ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
2805 ca, GetCallingConvention (parent is Class), ParameterTypes);
2808 // HACK because System.Reflection.Emit is lame
2810 if (!TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes)) {
2813 "Class `" +parent.Name+ "' already contains a definition with the " +
2814 "same return value and parameter types for constructor `" + Name
2825 public void Emit (TypeContainer parent)
2827 ILGenerator ig = ConstructorBuilder.GetILGenerator ();
2828 EmitContext ec = new EmitContext (parent, Location, ig, null, ModFlags, true);
2830 if ((ModFlags & Modifiers.STATIC) == 0){
2831 if (parent is Class && Initializer == null) {
2832 Initializer = new ConstructorBaseInitializer (
2833 null, Parameters.EmptyReadOnlyParameters, parent.Location);
2834 Initializer.implicit_initialization = true;
2838 // Spec mandates that Initializers will not have
2842 if (Initializer != null && !Initializer.Resolve (ec))
2844 ec.IsStatic = false;
2847 LabelParameters (ec, ParameterTypes, ConstructorBuilder);
2850 // Classes can have base initializers and instance field initializers.
2852 if (parent is Class){
2853 if ((ModFlags & Modifiers.STATIC) == 0)
2854 parent.EmitFieldInitializers (ec);
2857 if (Initializer != null) {
2858 if (this.ConstructorBuilder.Equals (Initializer.ParentConstructor))
2861 "A constructor can not call itself" );
2863 Initializer.Emit (ec);
2866 if ((ModFlags & Modifiers.STATIC) != 0)
2867 parent.EmitFieldInitializers (ec);
2869 Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes, Location);
2871 // If this is a non-static `struct' constructor and doesn't have any
2872 // initializer, it must initialize all of the struct's fields.
2873 if ((parent is Struct) && ((ModFlags & Modifiers.STATIC) == 0) &&
2874 (Initializer == null))
2875 Block.AddThisVariable (parent, Location);
2877 ec.EmitTopBlock (Block, ParameterInfo, Location);
2881 public class MethodData {
2883 // The return type of this method
2885 public readonly Type ReturnType;
2886 public readonly Type[] ParameterTypes;
2887 public readonly InternalParameters ParameterInfo;
2888 public readonly CallingConventions CallingConventions;
2889 public readonly Attributes OptAttributes;
2890 public readonly Location Location;
2893 // Are we implementing an interface ?
2895 public bool IsImplementing = false;
2900 protected MemberBase member;
2901 protected int modifiers;
2902 protected MethodAttributes flags;
2903 protected bool is_method;
2904 protected string accessor_name;
2905 ArrayList conditionals;
2907 MethodBuilder builder = null;
2908 public MethodBuilder MethodBuilder {
2914 public MethodData (MemberBase member, string name, Type return_type,
2915 Type [] parameter_types, InternalParameters parameters,
2916 CallingConventions cc, Attributes opt_attrs,
2917 int modifiers, MethodAttributes flags, bool is_method)
2919 this.member = member;
2920 this.accessor_name = name;
2921 this.ReturnType = return_type;
2922 this.ParameterTypes = parameter_types;
2923 this.ParameterInfo = parameters;
2924 this.CallingConventions = cc;
2925 this.OptAttributes = opt_attrs;
2926 this.modifiers = modifiers;
2928 this.is_method = is_method;
2929 this.Location = member.Location;
2930 this.conditionals = new ArrayList ();
2936 Attribute dllimport_attribute = null;
2937 string obsolete = null;
2938 bool obsolete_error = false;
2940 public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method)
2942 if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
2945 foreach (AttributeSection asec in opt_attrs.AttributeSections) {
2946 if (asec.Attributes == null)
2949 foreach (Attribute a in asec.Attributes) {
2950 if (a.Name == "Conditional") {
2951 if (!ApplyConditionalAttribute (a))
2953 } else if (a.Name == "Obsolete") {
2954 if (!ApplyObsoleteAttribute (a))
2956 } else if (a.Name.IndexOf ("DllImport") != -1) {
2958 a.Type = TypeManager.dllimport_type;
2959 Attribute.Error_AttributeNotValidForElement (a, Location);
2962 if (!ApplyDllImportAttribute (a))
2972 // Applies the `DllImport' attribute to the method.
2974 protected virtual bool ApplyDllImportAttribute (Attribute a)
2976 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
2977 if ((modifiers & extern_static) != extern_static) {
2978 Report.Error (601, Location,
2979 "The DllImport attribute must be specified on a method " +
2980 "marked `static' and `extern'.");
2984 flags |= MethodAttributes.PinvokeImpl;
2985 dllimport_attribute = a;
2990 // Applies the `Obsolete' attribute to the method.
2992 protected virtual bool ApplyObsoleteAttribute (Attribute a)
2994 if (obsolete != null) {
2995 Report.Error (579, Location, "Duplicate `Obsolete' attribute");
2999 obsolete = a.Obsolete_GetObsoleteMessage (out obsolete_error);
3000 return obsolete != null;
3004 // Applies the `Conditional' attribute to the method.
3006 protected virtual bool ApplyConditionalAttribute (Attribute a)
3008 // The Conditional attribute is only valid on methods.
3010 Attribute.Error_AttributeNotValidForElement (a, Location);
3014 string condition = a.Conditional_GetConditionName ();
3016 if (condition == null)
3019 if (ReturnType != TypeManager.void_type) {
3020 Report.Error (578, Location,
3021 "Conditional not valid on `" + member.Name + "' " +
3022 "because its return type is not void");
3026 if ((modifiers & Modifiers.OVERRIDE) != 0) {
3027 Report.Error (243, Location,
3028 "Conditional not valid on `" + member.Name + "' " +
3029 "because it is an override method");
3033 if (member.IsExplicitImpl) {
3034 Report.Error (577, Location,
3035 "Conditional not valid on `" + member.Name + "' " +
3036 "because it is an explicit interface implementation");
3040 if (IsImplementing) {
3041 Report.Error (623, Location,
3042 "Conditional not valid on `" + member.Name + "' " +
3043 "because it is an interface method");
3047 conditionals.Add (condition);
3053 // Checks whether this method should be ignored due to its Conditional attributes.
3055 bool ShouldIgnore (Location loc)
3057 // When we're overriding a virtual method, we implicitly inherit the
3058 // Conditional attributes from our parent.
3059 if (member.ParentMethod != null) {
3060 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (
3061 member.ParentMethod, loc);
3063 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
3067 foreach (string condition in conditionals)
3068 if (RootContext.AllDefines [condition] == null)
3075 // Returns the TypeManager.MethodFlags for this method.
3076 // This emits an error 619 / warning 618 if the method is obsolete.
3077 // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
3079 public virtual TypeManager.MethodFlags GetMethodFlags (Location loc)
3081 TypeManager.MethodFlags flags = 0;
3083 if (obsolete != null) {
3084 if (obsolete_error) {
3085 Report.Error (619, loc, "Method `" + member.Name +
3086 "' is obsolete: `" + obsolete + "'");
3087 return TypeManager.MethodFlags.IsObsoleteError;
3089 Report.Warning (618, loc, "Method `" + member.Name +
3090 "' is obsolete: `" + obsolete + "'");
3092 flags |= TypeManager.MethodFlags.IsObsolete;
3095 if (ShouldIgnore (loc))
3096 flags |= TypeManager.MethodFlags.ShouldIgnore;
3101 public virtual bool Define (TypeContainer parent)
3103 MethodInfo implementing = null;
3104 ArrayList implementing_list = null;
3105 string method_name, name, prefix, impl_method_name;
3108 if (OptAttributes != null)
3109 if (!ApplyAttributes (OptAttributes, is_method))
3112 if (accessor_name != null)
3113 name = accessor_name + "_" + member.ShortName;
3115 name = member.ShortName;
3118 impl_method_name = name;
3120 if (member.Implements != null) {
3121 implementing_list = new ArrayList();
3123 foreach (Expression Impl in member.Implements) {
3124 name = Impl.ToString();
3125 prefix = name.Substring(0, name.LastIndexOf("."));
3126 name = name.Substring(name.LastIndexOf(".") + 1);
3128 if (accessor_name != null)
3129 impl_method_name = accessor_name + "_" + name;
3131 impl_method_name = name;
3133 if (member is Indexer)
3134 implementing = parent.Pending.IsInterfaceIndexer (
3135 (Type) member.InterfaceTypes[++pos] , ReturnType, ParameterTypes);
3137 implementing = parent.Pending.IsInterfaceMethod (
3138 (Type) member.InterfaceTypes[++pos], impl_method_name, ReturnType, ParameterTypes);
3140 if (implementing == null) {
3141 TypeContainer.Error_NotInterfaceMember (
3142 Location, name, prefix);
3146 implementing_list.Add (implementing);
3147 IsImplementing = true;
3152 // For implicit implementations, make sure we are public, for
3153 // explicit implementations, make sure we are private.
3155 if (IsImplementing){
3157 // Setting null inside this block will trigger a more
3158 // verbose error reporting for missing interface implementations
3160 // The "candidate" function has been flagged already
3161 // but it wont get cleared
3163 /* if (!member.IsExplicitImpl){
3165 // We already catch different accessibility settings
3166 // so we just need to check that we are not private
3168 if ((modifiers & Modifiers.PRIVATE) != 0)
3169 implementing = null;
3172 // Static is not allowed
3174 if ((modifiers & Modifiers.STATIC) != 0)
3175 implementing = null;
3177 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
3178 Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
3179 implementing = null;
3185 // If implementing is still valid, set flags
3187 if (IsImplementing){
3189 // When implementing interface methods, set NewSlot.
3191 if (implementing.DeclaringType.IsInterface)
3192 flags |= MethodAttributes.NewSlot;
3195 MethodAttributes.Virtual |
3196 MethodAttributes.HideBySig;
3200 // Create the MethodBuilder for the method
3202 if ((flags & MethodAttributes.PinvokeImpl) != 0) {
3203 if ((modifiers & Modifiers.STATIC) == 0) {
3204 Report.Error (601, Location,
3205 "The DllImport attribute must be specified on " +
3206 "a method marked 'static' and 'extern'.");
3210 EmitContext ec = new EmitContext (
3211 parent, Location, null, ReturnType, modifiers);
3213 builder = dllimport_attribute.DefinePInvokeMethod (
3214 ec, parent.TypeBuilder, method_name, flags,
3215 ReturnType, ParameterTypes);
3217 builder = parent.TypeBuilder.DefineMethod (
3218 method_name, flags, CallingConventions,
3219 ReturnType, ParameterTypes);
3221 if (builder == null)
3224 if (IsImplementing) {
3226 // clear the pending implemntation flag
3229 foreach (MethodInfo Impl in implementing_list) {
3230 if (member is Indexer) {
3231 parent.Pending.ImplementIndexer (
3232 (Type) member.InterfaceTypes[pos++],
3233 builder, ReturnType,
3234 ParameterTypes, true);
3236 parent.Pending.ImplementMethod (
3237 (Type) member.InterfaceTypes[pos++],
3238 Impl.Name, ReturnType,
3239 ParameterTypes, member.IsExplicitImpl);
3241 parent.TypeBuilder.DefineMethodOverride (
3246 if (!TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes)) {
3247 Report.Error (111, Location,
3248 "Class `" + parent.Name +
3249 "' already contains a definition with the " +
3250 "same return value and parameter types as the " +
3251 "'get' method of property `" + member.Name + "'");
3255 TypeManager.AddMethod (builder, this);
3263 public virtual void Emit (TypeContainer parent, Block block, object kind)
3268 if ((flags & MethodAttributes.PinvokeImpl) == 0)
3269 ig = builder.GetILGenerator ();
3273 ec = new EmitContext (parent, Location, ig, ReturnType, modifiers);
3275 if (OptAttributes != null)
3276 Attribute.ApplyAttributes (ec, builder, kind, OptAttributes, Location);
3278 if (member is MethodCore)
3279 ((MethodCore) member).LabelParameters (ec, ParameterTypes, MethodBuilder);
3282 // abstract or extern methods have no bodies
3284 if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
3289 // abstract or extern methods have no bodies.
3291 if ((modifiers & Modifiers.ABSTRACT) != 0)
3293 500, Location, "Abstract method `" +
3294 TypeManager.MonoBASIC_Signature (builder) +
3295 "' can not have a body");
3297 if ((modifiers & Modifiers.EXTERN) != 0)
3299 179, Location, "External method `" +
3300 TypeManager.MonoBASIC_Signature (builder) +
3301 "' can not have a body");
3307 // Methods must have a body unless they're extern or abstract
3309 if (block == null) {
3311 501, Location, "Method `" +
3312 TypeManager.MonoBASIC_Signature (builder) +
3313 "' must declare a body since it is not marked " +
3314 "abstract or extern");
3319 // Handle destructors specially
3321 // FIXME: This code generates buggy code
3323 if (member.Name == "Finalize" && ReturnType == TypeManager.void_type)
3324 EmitDestructor (ec, block);
3326 ISymbolWriter sw = CodeGen.SymbolWriter;
3328 if ((sw != null) && !Location.IsNull (Location) &&
3329 !Location.IsNull (block.EndLocation)) {
3330 Location end = block.EndLocation;
3331 MethodToken token = MethodBuilder.GetToken ();
3332 sw.OpenMethod (new SymbolToken (token.Token));
3333 // Avoid error if we don't support debugging for the platform
3335 sw.SetMethodSourceRange (Location.SymbolDocument,
3339 } catch (Exception) {
3342 ec.EmitTopBlock (block, member.Name, ParameterInfo, Location);
3346 ec.EmitTopBlock (block, member.Name, ParameterInfo, Location);
3350 void EmitDestructor (EmitContext ec, Block block)
3352 ILGenerator ig = ec.ig;
3354 Label finish = ig.DefineLabel ();
3355 bool old_in_try = ec.InTry;
3357 ig.BeginExceptionBlock ();
3359 ec.ReturnLabel = finish;
3360 ec.HasReturnLabel = true;
3361 ec.EmitTopBlock (block, null, Location);
3362 ec.InTry = old_in_try;
3364 // ig.MarkLabel (finish);
3365 bool old_in_finally = ec.InFinally;
3366 ec.InFinally = true;
3367 ig.BeginFinallyBlock ();
3369 if (ec.ContainerType.BaseType != null) {
3370 Expression member_lookup = Expression.MemberLookup (
3371 ec, ec.ContainerType.BaseType, ec.ContainerType.BaseType, "Finalize",
3372 MemberTypes.Method, Expression.AllBindingFlags, Location);
3374 if (member_lookup != null){
3375 MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
3377 ig.Emit (OpCodes.Ldarg_0);
3378 ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
3381 ec.InFinally = old_in_finally;
3383 ig.EndExceptionBlock ();
3384 //ig.MarkLabel (ec.ReturnLabel);
3385 ig.Emit (OpCodes.Ret);
3389 abstract public class MemberBase : MemberCore {
3390 public Expression Type;
3391 public readonly Attributes OptAttributes;
3392 public ArrayList Implements;
3394 protected MethodAttributes flags;
3397 // The "short" name of this property / indexer / event. This is the
3398 // name without the explicit interface.
3400 public string ShortName;
3403 // The type of this property / indexer / event
3405 public Type MemberType;
3408 // If true, this is an explicit interface implementation
3410 public bool IsExplicitImpl = false;
3413 // The name of the interface we are explicitly implementing
3415 public string ExplicitInterfaceName = null;
3418 // If true, the interface type we are explicitly implementing
3420 public Type InterfaceType = null;
3421 public ArrayList InterfaceTypes = null;
3424 // The method we're overriding if this is an override method.
3426 protected MethodInfo parent_method = null;
3427 public MethodInfo ParentMethod {
3429 return parent_method;
3434 // The constructor is only exposed to our children
3436 protected MemberBase (Expression type, int mod, int allowed_mod, string name,
3437 Attributes attrs, Location loc)
3441 ModFlags = Modifiers.Check (allowed_mod, mod, Modifiers.PUBLIC, loc);
3442 OptAttributes = attrs;
3445 protected virtual bool CheckBase (TypeContainer parent)
3450 protected virtual bool CheckParameters (TypeContainer parent, Type [] parameters)
3454 foreach (Type partype in parameters){
3455 if (partype.IsPointer && !UnsafeOK (parent))
3458 if (parent.AsAccessible (partype, ModFlags))
3461 if (this is Indexer)
3462 Report.Error (55, Location,
3463 "Inconsistent accessibility: parameter type `" +
3464 TypeManager.MonoBASIC_Name (partype) + "' is less " +
3465 "accessible than indexer `" + Name + "'");
3467 Report.Error (51, Location,
3468 "Inconsistent accessibility: parameter type `" +
3469 TypeManager.MonoBASIC_Name (partype) + "' is less " +
3470 "accessible than method `" + Name + "'");
3477 protected virtual bool DoDefine (TypeContainer parent)
3482 if (!parent.MethodModifiersValid (ModFlags, Name, Location))
3485 flags = Modifiers.MethodAttr (ModFlags);
3487 // Lookup Type, verify validity
3488 MemberType = parent.ResolveType (Type, false, Location);
3489 if (MemberType == null)
3492 // check for whether the Interface is implemented by the class
3493 if (Implements != null) {
3494 InterfaceTypes = new ArrayList ();
3495 foreach (Expression Impls in Implements) {
3496 string iname = Impls.ToString();
3497 iname = iname.Substring(0, iname.LastIndexOf("."));
3498 bool iface_found = false;
3500 InterfaceType = RootContext.LookupType (
3501 parent, iname, false, Location);
3502 if (InterfaceType == null)
3505 InterfaceTypes.Add (InterfaceType);
3507 ArrayList bases = parent.Bases;
3508 if (bases != null) {
3509 foreach (Expression tbase in bases) {
3510 string bname = tbase.ToString();
3511 if (bname.LastIndexOf(".") != -1)
3512 bname = bname.Substring(bname.LastIndexOf("."));
3514 if (bname == iname) {
3522 Report.Error (31035, Location,
3523 "Class '" + parent.Name + "' doesn't implement interface '" + iname + "'");
3529 // verify accessibility
3530 if (!parent.AsAccessible (MemberType, ModFlags)) {
3531 if (this is Property)
3532 Report.Error (53, Location,
3533 "Inconsistent accessibility: property type `" +
3534 TypeManager.MonoBASIC_Name (MemberType) + "' is less " +
3535 "accessible than property `" + Name + "'");
3536 else if (this is Indexer)
3537 Report.Error (54, Location,
3538 "Inconsistent accessibility: indexer return type `" +
3539 TypeManager.MonoBASIC_Name (MemberType) + "' is less " +
3540 "accessible than indexer `" + Name + "'");
3541 else if (this is Method)
3542 Report.Error (50, Location,
3543 "Inconsistent accessibility: return type `" +
3544 TypeManager.MonoBASIC_Name (MemberType) + "' is less " +
3545 "accessible than method `" + Name + "'");
3547 Report.Error (52, Location,
3548 "Inconsistent accessibility: field type `" +
3549 TypeManager.MonoBASIC_Name (MemberType) + "' is less " +
3550 "accessible than field `" + Name + "'");
3554 if (MemberType.IsPointer && !UnsafeOK (parent))
3558 // Check for explicit interface implementation
3560 if ((ExplicitInterfaceName == null) && (Name.IndexOf (".") != -1)) {
3561 int pos = Name.LastIndexOf (".");
3563 ExplicitInterfaceName = Name.Substring (0, pos);
3564 ShortName = Name.Substring (pos + 1);
3573 // Fields and Events both generate FieldBuilders, we use this to share
3574 // their common bits. This is also used to flag usage of the field
3576 abstract public class FieldBase : MemberBase {
3577 public FieldBuilder FieldBuilder;
3578 public Status status;
3581 public enum Status : byte { ASSIGNED = 1, USED = 2 }
3584 // The constructor is only exposed to our children
3586 protected FieldBase (Expression type, int mod, int allowed_mod, string name,
3587 object init, Attributes attrs, Location loc)
3588 : base (type, mod, allowed_mod, name, attrs, loc)
3594 // Whether this field has an initializer.
3596 public bool HasInitializer {
3598 return init != null;
3603 readonly Object init;
3604 Expression init_expr;
3605 bool init_expr_initialized = false;
3608 // Resolves and returns the field initializer.
3610 public Expression GetInitializerExpression (EmitContext ec)
3612 if (init_expr_initialized)
3616 if (init is Expression)
3617 e = (Expression) init;
3619 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
3621 ec.IsFieldInitializer = true;
3622 e = e.DoResolve (ec);
3623 ec.IsFieldInitializer = false;
3626 init_expr_initialized = true;
3634 // The Field class is used to represents class/struct fields during parsing.
3636 public class Field : FieldBase {
3638 // Modifiers allowed in a class declaration
3640 const int AllowedModifiers =
3643 Modifiers.PROTECTED |
3644 Modifiers.INTERNAL |
3647 // Modifiers.VOLATILE |
3648 // Modifiers.UNSAFE |
3651 public Field (Expression type, int mod, string name, Object expr_or_array_init,
3652 Attributes attrs, Location loc)
3653 : base (type, mod, AllowedModifiers, name, expr_or_array_init, attrs, loc)
3657 public override bool Define (TypeContainer parent)
3659 Type t = parent.ResolveType (Type, false, Location);
3664 if (!parent.AsAccessible (t, ModFlags)) {
3665 Report.Error (52, Location,
3666 "Inconsistent accessibility: field type `" +
3667 TypeManager.MonoBASIC_Name (t) + "' is less " +
3668 "accessible than field `" + Name + "'");
3672 if (t.IsPointer && !UnsafeOK (parent))
3675 Type ptype = parent.TypeBuilder.BaseType;
3677 // ptype is only null for System.Object while compiling corlib.
3679 MemberList list = TypeContainer.FindMembers (
3680 ptype, MemberTypes.Field,
3681 BindingFlags.Public |
3682 BindingFlags.Static | BindingFlags.Instance,
3683 System.Type.FilterName, Name);
3685 if (RootContext.WarningLevel > 1){
3686 if ((list.Count > 0) && ((ModFlags & Modifiers.SHADOWS) == 0))
\r
3690 "Variable '" + Name + "' should be declared " +
3691 "Shadows since the base type '" + ptype.Name +
3692 "' has a variable with same name");
3694 ModFlags |= Modifiers.SHADOWS;
3697 if (list.Count == 0)
3698 // if a member of module is not inherited from Object class
3699 // can not be declared protected
3700 if ((parent is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
3701 Report.Error (30593, Location,
3702 "'Variable' inside a 'Module' can not be " +
3703 "declared as 'Protected'");
3706 if ((parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
3707 Report.Error (30435, Location,
3708 "'Variable' inside a 'Structure' can not be " +
3709 "declared as 'Protected'");
3711 if ((ModFlags & Modifiers.VOLATILE) != 0){
3713 if (TypeManager.IsEnumType (t))
3714 t = TypeManager.EnumToUnderlying (t);
3716 if (!((t == TypeManager.bool_type) ||
3717 (t == TypeManager.sbyte_type) ||
3718 (t == TypeManager.byte_type) ||
3719 (t == TypeManager.short_type) ||
3720 (t == TypeManager.ushort_type) ||
3721 (t == TypeManager.int32_type) ||
3722 (t == TypeManager.uint32_type) ||
3723 (t == TypeManager.char_type) ||
3724 (t == TypeManager.float_type))){
3726 677, Location, parent.MakeName (Name) +
3727 " A volatile field can not be of type `" +
3728 TypeManager.MonoBASIC_Name (t) + "'");
3734 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
3736 if (parent is Struct &&
3737 ((fa & FieldAttributes.Static) == 0) &&
3738 t == parent.TypeBuilder &&
3739 !TypeManager.IsBuiltinType (t)){
3740 Report.Error (523, Location, "Struct member `" + parent.Name + "." + Name +
3741 "' causes a cycle in the structure layout");
3744 FieldBuilder = parent.TypeBuilder.DefineField (
3745 Name, t, Modifiers.FieldAttr (ModFlags));
3747 TypeManager.RegisterFieldBase (FieldBuilder, this);
3751 public void Emit (TypeContainer tc)
3753 EmitContext ec = new EmitContext (tc, Location, null,
3754 FieldBuilder.FieldType, ModFlags);
3756 Attribute.ApplyAttributes (ec, FieldBuilder, this, OptAttributes, Location);
3761 // `set' and `get' accessors are represented with an Accessor.
3763 public class Accessor {
3765 // Null if the accessor is empty, or a Block if not
3768 public Attributes OptAttributes;
3770 public Accessor (Block b, Attributes attrs)
3773 OptAttributes = attrs;
3778 // Properties and Indexers both generate PropertyBuilders, we use this to share
3779 // their common bits.
3781 abstract public class PropertyBase : MethodCore {
3782 public Accessor Get, Set;
3783 public PropertyBuilder PropertyBuilder;
3784 public MethodBuilder GetBuilder, SetBuilder;
3785 public MethodData GetData, SetData;
3787 protected EmitContext ec;
3789 public PropertyBase (Expression type, string name, int mod_flags, int allowed_mod,
3790 Parameters parameters, Accessor get_block, Accessor set_block,
3791 Attributes attrs, Location loc)
3792 : base (type, mod_flags, allowed_mod, name, attrs, parameters, loc)
3798 protected override bool DoDefine (TypeContainer parent)
3800 if (!base.DoDefine (parent))
3803 ec = new EmitContext (parent, Location, null, MemberType, ModFlags);
3809 // Checks our base implementation if any
3811 protected override bool CheckBase (TypeContainer container)
3813 base.CheckBase (container);
3815 // Check whether arguments were correct.
3816 if (!DoDefineParameters (container))
3822 MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
3827 mi_this = TypeContainer.FindMembers (
3828 container.TypeBuilder, MemberTypes.Property,
3829 BindingFlags.NonPublic | BindingFlags.Public |
3830 BindingFlags.Static | BindingFlags.Instance |
3831 BindingFlags.DeclaredOnly,
3832 MethodSignature.method_signature_filter, ms);
3834 if (mi_this.Count > 0) {
3835 Report.Error (111, Location, "Class `" + container.Name + "' " +
3836 "already defines a member called `" + Name + "' " +
3837 "with the same parameter types");
3842 if (container is Interface)
3846 MethodSignature base_ms;
3847 if (this is Indexer) {
3848 string name, base_name;
3850 report_name = "this";
3851 name = TypeManager.IndexerPropertyName (container.TypeBuilder);
3852 ms = new MethodSignature (name, null, ParameterTypes);
3853 base_name = TypeManager.IndexerPropertyName (container.TypeBuilder.BaseType);
3854 base_ms = new MethodSignature (base_name, null, ParameterTypes);
3857 ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
3861 // Verify if the parent has a type with the same name, and then
3862 // check whether we have to create a new slot for it or not.
3864 Type ptype = container.TypeBuilder.BaseType;
3866 MemberInfo parent_member = null;
3867 MemberList mi, mi_static, mi_instance;
3869 mi_static = TypeContainer.FindMembers (
3870 ptype, MemberTypes.Property,
3871 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
3872 MethodSignature.inheritable_method_signature_filter, ms);
3874 mi_instance = TypeContainer.FindMembers (
3875 ptype, MemberTypes.Property,
3876 BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
3877 MethodSignature.inheritable_method_signature_filter,
3880 if (mi_instance.Count > 0) {
3882 } else if (mi_static.Count > 0)
3887 if (mi != null && mi.Count > 0)
\r
3888 parent_member = (MethodInfo) mi [0];
3890 if (parent_member is PropertyInfo) {
3891 PropertyInfo parent_property = (PropertyInfo)parent_member;
3893 string name = parent_property.DeclaringType.Name + "." +
3894 parent_property.Name;
3896 MethodInfo get, set, parent_method;
3897 get = parent_property.GetGetMethod (true);
3898 set = parent_property.GetSetMethod (true);
3901 parent_method = get;
3902 else if (set != null)
3903 parent_method = set;
3905 throw new Exception ("Internal error!");
3907 if (!CheckMethodAgainstBase (container, flags, parent_method, name))
3910 if ((ModFlags & Modifiers.NEW) == 0) {
3911 Type parent_type = TypeManager.TypeToCoreType (
3912 parent_property.PropertyType);
3914 if (parent_type != MemberType) {
3916 508, Location, container.MakeName (Name) + ": cannot " +
3917 "change return type when overriding " +
3918 "inherited member " + name);
3922 } else if (parent_member == null) {
3923 /*if ((ModFlags & Modifiers.NEW) != 0)
3924 WarningNotHiding (container);
3926 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3927 if (this is Indexer)
3928 Report.Error (115, Location,
3929 container.MakeName (Name) +
3930 " no suitable indexers found to override");
3932 Report.Error (115, Location,
3933 container.MakeName (Name) +
3934 " no suitable properties found to override");
3938 if ((ModFlags & ( Modifiers.NEW | Modifiers.SHADOWS | Modifiers.OVERRIDE )) == 0) {
3939 if ((ModFlags & Modifiers.NONVIRTUAL) != 0) {
3940 Report.Error (31088, Location,
3941 container.MakeName (Name) + " : Cannot " +
3942 "be declared NotOverridable since this method is " +
3943 "not maked as Overrides");
3946 // if a member of module is not inherited from Object class
3947 // can not be declared protected
3948 if ((container is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
3949 Report.Error (31066, Location,
3950 "'Property' inside a 'Module' can not be declared as " +
3951 "'Protected' or 'Protected Friend'");
3956 public virtual void Emit (TypeContainer tc)
3959 // The PropertyBuilder can be null for explicit implementations, in that
3960 // case, we do not actually emit the ".property", so there is nowhere to
3961 // put the attribute
3963 if (PropertyBuilder != null)
3964 Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes, Location);
3966 if (GetData != null)
3967 GetData.Emit (tc, Get.Block, Get);
3969 if (SetData != null)
3970 SetData.Emit (tc, Set.Block, Set);
3975 public class Property : PropertyBase {
3976 const int AllowedModifiers =
3979 Modifiers.PROTECTED |
3980 Modifiers.INTERNAL |
3984 Modifiers.OVERRIDE |
3985 Modifiers.ABSTRACT |
3989 Modifiers.NONVIRTUAL |
3991 Modifiers.READONLY |
3992 Modifiers.WRITEONLY |
3995 string set_parameter_name;
3996 Parameters get_params;
3997 Parameters set_params;
3999 public Property (Expression type, string name, int mod_flags,
4000 Accessor get_block, Accessor set_block,
4001 Attributes attrs, Location loc, string set_name,
4002 Parameters p_get, Parameters p_set, ArrayList impl_what)
4003 : base (type, name, mod_flags, AllowedModifiers,
4005 get_block, set_block, attrs, loc)
4007 set_parameter_name = set_name;
4010 Implements = impl_what;
4013 public Property (Expression type, string name, int mod_flags,
4014 Accessor get_block, Accessor set_block,
4015 Attributes attrs, Location loc)
4016 : this (type, name, mod_flags, get_block, set_block, attrs, loc,
4017 "Value", Parameters.EmptyReadOnlyParameters, Parameters.EmptyReadOnlyParameters, null)
4021 public override bool Define (TypeContainer parent)
4023 Type [] g_parameters=null, s_parameters=null;
4024 Parameter [] g_parms, s_parms;
4025 InternalParameters g_ip=null, s_ip=null;
4027 if ((parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
4028 Report.Error (30435, Location,
4029 "'Property' inside a 'Structure' can not be declared as " +
4030 "'Protected' or 'Protected Friend'");
4032 if (!DoDefine (parent))
4035 if (!CheckBase (parent))
4038 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
4041 if ((ModFlags & Modifiers.WRITEONLY) == 0)
4044 "Property without 'Get' accessor must have a 'WriteOnly' modifier");
4047 if (get_params == Parameters.EmptyReadOnlyParameters) {
4048 g_parameters = TypeManager.NoTypes;
4049 g_ip = new InternalParameters (
4050 parent, Parameters.EmptyReadOnlyParameters);
4052 g_parameters = new Type [get_params.FixedParameters.Length];
4053 for (int i = 0; i < get_params.FixedParameters.Length; i ++) {
4054 g_parameters[i] = get_params.FixedParameters[i].ParameterType;
4056 g_parms = new Parameter [get_params.FixedParameters.Length];
4057 for (int i = 0; i < get_params.FixedParameters.Length; i ++) {
4058 Parameter tp = get_params.FixedParameters[i];
4059 g_parms[i] = new Parameter (tp.TypeName, tp.Name,
4060 Parameter.Modifier.NONE, null);
4062 g_ip = new InternalParameters (
4063 parent, new Parameters (g_parms, null, Location));
4066 GetData = new MethodData (this, "get", MemberType,
4067 g_parameters, g_ip, CallingConventions.Standard,
4068 Get.OptAttributes, ModFlags, flags, false);
4070 if (!GetData.Define (parent))
4073 GetBuilder = GetData.MethodBuilder;
4077 if ((ModFlags & Modifiers.READONLY) == 0)
4080 "Property without 'Set' accessor must have a 'ReadOnly' modifier");
4085 if (set_params == Parameters.EmptyReadOnlyParameters)
4087 s_parameters = new Type [1];
4088 s_parameters [0] = MemberType;
4090 s_parms = new Parameter [1];
4091 s_parms [0] = new Parameter (Type, set_parameter_name,
4092 Parameter.Modifier.NONE, null);
4094 s_parameters = new Type [set_params.FixedParameters.Length];
4095 for (int i = 0; i < set_params.FixedParameters.Length; i ++) {
4096 s_parameters[i] = set_params.FixedParameters[i].ParameterType;
4099 s_parms = new Parameter [set_params.FixedParameters.Length];
4100 for (int i = 0; i < set_params.FixedParameters.Length; i ++) {
4101 Parameter tp = set_params.FixedParameters[i];
4102 s_parms[i] = new Parameter (tp.TypeName, tp.Name,
4103 Parameter.Modifier.NONE, null);
4107 s_ip = new InternalParameters (
4108 parent, new Parameters (s_parms, null, Location));
4110 SetData = new MethodData (this, "set", TypeManager.void_type,
4111 s_parameters, s_ip, CallingConventions.Standard,
4112 Set.OptAttributes, ModFlags, flags, false);
4114 if (!SetData.Define (parent))
4117 SetBuilder = SetData.MethodBuilder;
4118 SetBuilder.DefineParameter (1, ParameterAttributes.None,
4119 set_parameter_name);
4122 // FIXME - PropertyAttributes.HasDefault ?
4124 PropertyAttributes prop_attr =
4125 PropertyAttributes.RTSpecialName |
4126 PropertyAttributes.SpecialName;
4128 if (!IsExplicitImpl){
4129 PropertyBuilder = parent.TypeBuilder.DefineProperty (
4130 Name, prop_attr, MemberType, null);
4132 PropertyBuilder.SetGetMethod (GetBuilder);
4133 PropertyBuilder.SetSetMethod (SetBuilder);
4136 // HACK for the reasons exposed above
4138 if (!TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder)) {
4141 "Class `" + parent.Name +
4142 "' already contains a definition for the property `" +
4150 public override void Emit (TypeContainer tc)
4154 if (GetData != null)
4156 Parameters = get_params;
4157 GetData.Emit (tc, Get.Block, Get);
4160 if (SetData != null)
4162 Parameters = set_params;
4163 SetData.Emit (tc, Set.Block, Set);
4170 /// Gigantic workaround for lameness in SRE follows :
4171 /// This class derives from EventInfo and attempts to basically
4172 /// wrap around the EventBuilder so that FindMembers can quickly
4173 /// return this in it search for members
4175 public class MyEventBuilder : EventInfo {
4178 // We use this to "point" to our Builder which is
4179 // not really a MemberInfo
4181 EventBuilder MyBuilder;
4184 // We "catch" and wrap these methods
4186 MethodInfo raise, remove, add;
4188 EventAttributes attributes;
4189 Type declaring_type, reflected_type, event_type;
4192 public MyEventBuilder (TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
4194 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
4196 // And now store the values in our own fields.
4198 declaring_type = type_builder;
4200 reflected_type = type_builder;
4202 attributes = event_attr;
4204 this.event_type = event_type;
4208 // Methods that you have to override. Note that you only need
4209 // to "implement" the variants that take the argument (those are
4210 // the "abstract" methods, the others (GetAddMethod()) are
4213 public override MethodInfo GetAddMethod (bool nonPublic)
4218 public override MethodInfo GetRemoveMethod (bool nonPublic)
4223 public override MethodInfo GetRaiseMethod (bool nonPublic)
4229 // These methods make "MyEventInfo" look like a Builder
4231 public void SetRaiseMethod (MethodBuilder raiseMethod)
4233 raise = raiseMethod;
4234 MyBuilder.SetRaiseMethod (raiseMethod);
4237 public void SetRemoveOnMethod (MethodBuilder removeMethod)
4239 remove = removeMethod;
4240 MyBuilder.SetRemoveOnMethod (removeMethod);
4243 public void SetAddOnMethod (MethodBuilder addMethod)
4246 MyBuilder.SetAddOnMethod (addMethod);
4249 public void SetCustomAttribute (CustomAttributeBuilder cb)
4251 MyBuilder.SetCustomAttribute (cb);
4254 public override object [] GetCustomAttributes (bool inherit)
4256 // FIXME : There's nothing which can be seemingly done here because
4257 // we have no way of getting at the custom attribute objects of the
4262 public override object [] GetCustomAttributes (Type t, bool inherit)
4264 // FIXME : Same here !
4268 public override bool IsDefined (Type t, bool b)
4273 public override EventAttributes Attributes {
4279 public override string Name {
4285 public override Type DeclaringType {
4287 return declaring_type;
4291 public override Type ReflectedType {
4293 return reflected_type;
4297 public Type EventType {
4304 public class Event : FieldBase {
4305 const int AllowedModifiers =
4308 Modifiers.PROTECTED |
4309 Modifiers.INTERNAL |
4314 Modifiers.OVERRIDE |
4318 public readonly Accessor Add;
4319 public readonly Accessor Remove;
4320 public MyEventBuilder EventBuilder;
4322 MethodBuilder AddBuilder, RemoveBuilder;
4323 MethodData AddData, RemoveData;
4325 public Event (Expression type, string name, Object init, int mod, Accessor add,
4326 Accessor remove, Attributes attrs, Location loc)
4327 : base (type, mod, AllowedModifiers, name, init, attrs, loc)
4334 public Event (Expression type, string name, Object init, int mod, Accessor add,
4335 Accessor remove, Attributes attrs, ArrayList impl_what, Location loc)
4336 : base (type, mod, AllowedModifiers, name, init, attrs, loc)
4340 Implements = impl_what;
4344 public override bool Define (TypeContainer parent)
4346 EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
4348 if (!DoDefine (parent))
4351 if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
4352 Report.Error (31044, Location, "'" + parent.Name + "." + Name +
4353 "' : event must be of a delegate type");
4357 Type [] parameter_types = new Type [1];
4358 parameter_types [0] = MemberType;
4360 Parameter [] parms = new Parameter [1];
4361 parms [0] = new Parameter (Type, /* was "value" */ this.Name, Parameter.Modifier.NONE, null);
4362 InternalParameters ip = new InternalParameters (
4363 parent, new Parameters (parms, null, Location));
4365 if (!CheckBase (parent))
4369 // Now define the accessors
4371 AddData = new MethodData (this, "add", TypeManager.void_type,
4372 parameter_types, ip, CallingConventions.Standard,
4373 (Add != null) ? Add.OptAttributes : null,
4374 ModFlags, flags, false);
4376 if (!AddData.Define (parent))
4379 AddBuilder = AddData.MethodBuilder;
4380 AddBuilder.DefineParameter (1, ParameterAttributes.None, /* was "value" */ this.Name);
4382 RemoveData = new MethodData (this, "remove", TypeManager.void_type,
4383 parameter_types, ip, CallingConventions.Standard,
4384 (Remove != null) ? Remove.OptAttributes : null,
4385 ModFlags, flags, false);
4387 if (!RemoveData.Define (parent))
4390 RemoveBuilder = RemoveData.MethodBuilder;
4391 RemoveBuilder.DefineParameter (1, ParameterAttributes.None, /* was "value" */ this.Name);
4393 if (!IsExplicitImpl){
4394 EventBuilder = new MyEventBuilder (
4395 parent.TypeBuilder, Name, e_attr, MemberType);
4397 if (Add == null && Remove == null) {
4398 FieldBuilder = parent.TypeBuilder.DefineField (
4400 FieldAttributes.FamANDAssem | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
4401 TypeManager.RegisterPrivateFieldOfEvent (
4402 (EventInfo) EventBuilder, FieldBuilder);
4403 TypeManager.RegisterFieldBase (FieldBuilder, this);
4406 EventBuilder.SetAddOnMethod (AddBuilder);
4407 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
4409 if (!TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder)) {
4410 Report.Error (111, Location,
4411 "Class `" + parent.Name +
4412 "' already contains a definition for the event `" +
4421 void EmitDefaultMethod (EmitContext ec, bool is_add)
4423 ILGenerator ig = ec.ig;
4424 MethodInfo method = null;
4427 method = TypeManager.delegate_combine_delegate_delegate;
4429 method = TypeManager.delegate_remove_delegate_delegate;
4431 if ((ModFlags & Modifiers.STATIC) != 0) {
4432 ig.Emit (OpCodes.Ldsfld, (FieldInfo) FieldBuilder);
4433 ig.Emit (OpCodes.Ldarg_0);
4434 ig.Emit (OpCodes.Call, method);
4435 ig.Emit (OpCodes.Castclass, MemberType);
4436 ig.Emit (OpCodes.Stsfld, (FieldInfo) FieldBuilder);
4438 ig.Emit (OpCodes.Ldarg_0);
4439 ig.Emit (OpCodes.Ldarg_0);
4440 ig.Emit (OpCodes.Ldfld, (FieldInfo) FieldBuilder);
4441 ig.Emit (OpCodes.Ldarg_1);
4442 ig.Emit (OpCodes.Call, method);
4443 ig.Emit (OpCodes.Castclass, MemberType);
4444 ig.Emit (OpCodes.Stfld, (FieldInfo) FieldBuilder);
4446 ig.Emit (OpCodes.Ret);
4449 public void Emit (TypeContainer tc)
4453 ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
4454 Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes, Location);
4457 AddData.Emit (tc, Add.Block, Add);
4459 ILGenerator ig = AddData.MethodBuilder.GetILGenerator ();
4460 ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
4461 EmitDefaultMethod (ec, true);
4465 RemoveData.Emit (tc, Remove.Block, Remove);
4467 ILGenerator ig = RemoveData.MethodBuilder.GetILGenerator ();
4468 ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
4469 EmitDefaultMethod (ec, false);
4476 // FIXME: This does not handle:
4478 // int INTERFACENAME [ args ]
4483 // int this [ args ]
4485 public class Indexer : PropertyBase {
4487 const int AllowedModifiers =
4490 Modifiers.PROTECTED |
4491 Modifiers.INTERNAL |
4495 Modifiers.OVERRIDE |
4500 public string IndexerName;
4501 public string InterfaceIndexerName;
4504 // Are we implementing an interface ?
4506 bool IsImplementing = false;
4508 public Indexer (Expression type, string int_type, int flags, Parameters parameters,
4509 Accessor get_block, Accessor set_block, Attributes attrs, Location loc)
4510 : base (type, "", flags, AllowedModifiers, parameters, get_block, set_block,
4513 ExplicitInterfaceName = int_type;
4516 public override bool Define (TypeContainer parent)
4518 PropertyAttributes prop_attr =
4519 PropertyAttributes.RTSpecialName |
4520 PropertyAttributes.SpecialName;
4522 if (!DoDefine (parent))
4525 IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
4526 if (IndexerName == null)
4527 IndexerName = "Item";
4528 else if (IsExplicitImpl)
4529 Report.Error (592, Location,
4530 "Attribute 'IndexerName' is not valid on this declaration " +
4531 "type. It is valid on `property' declarations only.");
4533 ShortName = IndexerName;
4534 if (IsExplicitImpl) {
4535 InterfaceIndexerName = TypeManager.IndexerPropertyName (InterfaceType);
4536 Name = InterfaceType.FullName + "." + IndexerName;
4538 InterfaceIndexerName = IndexerName;
4542 if (!CheckBase (parent))
4546 InternalParameters ip = new InternalParameters (parent, Parameters);
4548 GetData = new MethodData (this, "get", MemberType,
4549 ParameterTypes, ip, CallingConventions.Standard,
4550 Get.OptAttributes, ModFlags, flags, false);
4552 if (!GetData.Define (parent))
4555 GetBuilder = GetData.MethodBuilder;
4559 int top = ParameterTypes.Length;
4560 Type [] set_pars = new Type [top + 1];
4561 ParameterTypes.CopyTo (set_pars, 0);
4562 set_pars [top] = MemberType;
4564 Parameter [] fixed_parms = Parameters.FixedParameters;
4566 if (fixed_parms == null){
4567 throw new Exception ("We currently do not support only array arguments in an indexer");
4568 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4569 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4571 // Here is the problem: the `value' parameter has
4572 // to come *after* the array parameter in the declaration
4574 // X (object [] x, Type value)
4577 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4578 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
4582 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
4585 fixed_parms.CopyTo (tmp, 0);
4586 tmp [fixed_parms.Length] = new Parameter (
4587 Type, /* was "value" */ this.Name, Parameter.Modifier.NONE, null);
4589 Parameters set_formal_params = new Parameters (tmp, null, Location);
4591 InternalParameters ip = new InternalParameters (parent, set_formal_params);
4593 SetData = new MethodData (this, "set", TypeManager.void_type,
4594 set_pars, ip, CallingConventions.Standard,
4595 Set.OptAttributes, ModFlags, flags, false);
4597 if (!SetData.Define (parent))
4600 SetBuilder = SetData.MethodBuilder;
4604 // Now name the parameters
4606 Parameter [] p = Parameters.FixedParameters;
4610 for (i = 0; i < p.Length; ++i) {
4612 GetBuilder.DefineParameter (
4613 i + 1, p [i].Attributes, p [i].Name);
4616 SetBuilder.DefineParameter (
4617 i + 1, p [i].Attributes, p [i].Name);
4622 SetBuilder.DefineParameter (
4623 i + 1, ParameterAttributes.None, /* was "value" */ this.Name);
4625 if (i != ParameterTypes.Length) {
4626 Parameter array_param = Parameters.ArrayParameter;
4627 SetBuilder.DefineParameter (
4628 i + 1, array_param.Attributes, array_param.Name);
4632 if (GetData != null)
4633 IsImplementing = GetData.IsImplementing;
4634 else if (SetData != null)
4635 IsImplementing = SetData.IsImplementing;
4638 // Define the PropertyBuilder if one of the following conditions are met:
4639 // a) we're not implementing an interface indexer.
4640 // b) the indexer has a different IndexerName and this is no
4641 // explicit interface implementation.
4643 if (!IsExplicitImpl) {
4644 PropertyBuilder = parent.TypeBuilder.DefineProperty (
4645 IndexerName, prop_attr, MemberType, ParameterTypes);
4647 if (GetData != null)
4648 PropertyBuilder.SetGetMethod (GetBuilder);
4650 if (SetData != null)
4651 PropertyBuilder.SetSetMethod (SetBuilder);
4653 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
4661 public class Operator : MemberCore {
4663 const int AllowedModifiers =
4669 const int RequiredModifiers =
4673 public enum OpType : byte {
4683 // Unary and Binary operators
4706 // Implicit and Explicit
4711 public readonly OpType OperatorType;
4712 public readonly Expression ReturnType;
4713 public readonly Expression FirstArgType, SecondArgType;
4714 public readonly string FirstArgName, SecondArgName;
4715 public readonly Block Block;
4716 public Attributes OptAttributes;
4717 public MethodBuilder OperatorMethodBuilder;
4719 public string MethodName;
4720 public Method OperatorMethod;
4722 public Operator (OpType type, Expression ret_type, int flags,
4723 Expression arg1type, string arg1name,
4724 Expression arg2type, string arg2name,
4725 Block block, Attributes attrs, Location loc)
4728 OperatorType = type;
4729 ReturnType = ret_type;
4730 ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PUBLIC, loc);
4731 FirstArgType = arg1type;
4732 FirstArgName = arg1name;
4733 SecondArgType = arg2type;
4734 SecondArgName = arg2name;
4736 OptAttributes = attrs;
4739 string Prototype (TypeContainer parent)
4741 return parent.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
4742 SecondArgType + ")";
4745 public override bool Define (TypeContainer parent)
4748 MethodName = "op_" + OperatorType;
4750 if (SecondArgType != null)
4753 Parameter [] param_list = new Parameter [length];
4755 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
4758 "User defined operators `" +
4759 Prototype (parent) +
4760 "' must be declared static and public");
4764 param_list[0] = new Parameter (FirstArgType, FirstArgName,
4765 Parameter.Modifier.NONE, null);
4766 if (SecondArgType != null)
4767 param_list[1] = new Parameter (SecondArgType, SecondArgName,
4768 Parameter.Modifier.NONE, null);
4770 OperatorMethod = new Method (ReturnType, ModFlags, MethodName,
4771 new Parameters (param_list, null, Location),
4772 OptAttributes, Mono.MonoBASIC.Location.Null);
4774 OperatorMethod.IsOperator = true;
4775 OperatorMethod.Define (parent);
4777 if (OperatorMethod.MethodBuilder == null)
4780 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
4782 Type [] param_types = OperatorMethod.ParameterTypes;
4783 Type declaring_type = OperatorMethodBuilder.DeclaringType;
4784 Type return_type = OperatorMethod.GetReturnType ();
4785 Type first_arg_type = param_types [0];
4787 // Rules for conversion operators
4789 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
4790 if (first_arg_type == return_type && first_arg_type == declaring_type){
4793 "User-defined conversion cannot take an object of the " +
4794 "enclosing type and convert to an object of the enclosing" +
4799 if (first_arg_type != declaring_type && return_type != declaring_type){
4802 "User-defined conversion must convert to or from the " +
4807 if (first_arg_type == TypeManager.object_type ||
4808 return_type == TypeManager.object_type){
4811 "User-defined conversion cannot convert to or from " +
4816 if (first_arg_type.IsInterface || return_type.IsInterface){
4819 "User-defined conversion cannot convert to or from an " +
4824 if (first_arg_type.IsSubclassOf (return_type) ||
4825 return_type.IsSubclassOf (first_arg_type)){
4828 "User-defined conversion cannot convert between types " +
4829 "that derive from each other");
4832 } else if (SecondArgType == null) {
4833 // Checks for Unary operators
4835 if (first_arg_type != declaring_type){
4838 "The parameter of a unary operator must be the " +
4843 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
4844 if (return_type != declaring_type){
4847 "The parameter and return type for ++ and -- " +
4848 "must be the containing type");
4854 if (OperatorType == OpType.True || OperatorType == OpType.False) {
4855 if (return_type != TypeManager.bool_type){
4858 "The return type of operator True or False " +
4865 // Checks for Binary operators
4867 if (first_arg_type != declaring_type &&
4868 param_types [1] != declaring_type){
4871 "One of the parameters of a binary operator must " +
4872 "be the containing type");
4880 public void Emit (TypeContainer parent)
4882 EmitContext ec = new EmitContext (parent, Location, null, null, ModFlags);
4883 Attribute.ApplyAttributes (ec, OperatorMethodBuilder, this, OptAttributes, Location);
4886 // abstract or extern methods have no bodies
4888 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
4891 OperatorMethod.Block = Block;
4892 OperatorMethod.Emit (parent);
4895 public static string GetName (OpType ot)
4898 case OpType.LogicalNot:
4900 case OpType.OnesComplement:
4902 case OpType.Increment:
4904 case OpType.Decrement:
4910 case OpType.Addition:
4912 case OpType.Subtraction:
4914 case OpType.UnaryPlus:
4916 case OpType.UnaryNegation:
4918 case OpType.Multiply:
4920 case OpType.Division:
4922 case OpType.Modulus:
4924 case OpType.BitwiseAnd:
4926 case OpType.BitwiseOr:
4928 case OpType.ExclusiveOr:
4930 case OpType.LeftShift:
4932 case OpType.RightShift:
4934 case OpType.Equality:
4936 case OpType.Inequality:
4938 case OpType.GreaterThan:
4940 case OpType.LessThan:
4942 case OpType.GreaterThanOrEqual:
4944 case OpType.LessThanOrEqual:
4946 case OpType.Implicit:
4948 case OpType.Explicit:
4954 public override string ToString ()
4956 Type return_type = OperatorMethod.GetReturnType();
4957 Type [] param_types = OperatorMethod.ParameterTypes;
4959 if (SecondArgType == null)
4960 return String.Format (
4961 "{0} operator {1}({2})",
4962 TypeManager.MonoBASIC_Name (return_type),
4963 GetName (OperatorType),
4966 return String.Format (
4967 "{0} operator {1}({2}, {3})",
4968 TypeManager.MonoBASIC_Name (return_type),
4969 GetName (OperatorType),
4970 param_types [0], param_types [1]);
4975 // This is used to compare method signatures
4977 struct MethodSignature {
4979 public Type RetType;
4980 public Type [] Parameters;
4983 /// This delegate is used to extract methods which have the
4984 /// same signature as the argument
4986 public static MemberFilter method_signature_filter;
4989 /// This delegate is used to extract methods which have the
4990 /// same signature as the argument except for the name
4992 public static MemberFilter method_signature_noname_filter;
4995 /// This delegate is used to extract inheritable methods which
4996 /// have the same signature as the argument. By inheritable,
4997 /// this means that we have permissions to override the method
4998 /// from the current assembly and class
5000 public static MemberFilter inheritable_method_signature_filter;
5003 /// This delegate is used to extract inheritable methods which
5004 /// have the same signature as the argument. By inheritable,
5005 /// this means that we have permissions to override the method
5006 /// from the current assembly and class
5008 public static MemberFilter inheritable_property_signature_filter;
5010 static MethodSignature ()
5012 method_signature_filter = new MemberFilter (MemberSignatureCompare);
5013 method_signature_noname_filter = new MemberFilter (MemberSignatureCompareNoName);
5014 inheritable_method_signature_filter = new MemberFilter (
5015 InheritableMemberSignatureCompare);
5016 inheritable_property_signature_filter = new MemberFilter (
5017 InheritablePropertySignatureCompare);
5020 public MethodSignature (string name, Type ret_type, Type [] parameters)
5025 if (parameters == null)
5026 Parameters = TypeManager.NoTypes;
5028 Parameters = parameters;
5031 public override int GetHashCode ()
5033 return Name.GetHashCode ();
5036 public override bool Equals (Object o)
5038 MethodSignature other = (MethodSignature) o;
5040 if (other.Name != Name)
5043 if (other.RetType != RetType)
5046 if (Parameters == null){
5047 if (other.Parameters == null)
5052 if (other.Parameters == null)
5055 int c = Parameters.Length;
5056 if (other.Parameters.Length != c)
5059 for (int i = 0; i < c; i++)
5060 if (other.Parameters [i] != Parameters [i])
5066 static bool MemberSignatureCompareNoName (MemberInfo m, object filter_criteria)
5068 return MemberSignatureCompare (m, filter_criteria, false);
5071 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
5073 return MemberSignatureCompare (m, filter_criteria, true);
5076 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria, bool use_name)
5078 MethodSignature sig = (MethodSignature) filter_criteria;
5080 if (use_name && (m.Name != sig.Name))
5084 MethodInfo mi = m as MethodInfo;
5085 PropertyInfo pi = m as PropertyInfo;
5088 ReturnType = mi.ReturnType;
5089 else if (pi != null)
5090 ReturnType = pi.PropertyType;
5095 // we use sig.RetType == null to mean `do not check the
5096 // method return value.
5098 if (sig.RetType != null)
5099 if (ReturnType != sig.RetType)
5104 args = TypeManager.GetArgumentTypes (mi);
5106 args = TypeManager.GetArgumentTypes (pi);
5107 Type [] sigp = sig.Parameters;
5109 if (args.Length != sigp.Length)
5112 for (int i = args.Length; i > 0; ){
5114 if (args [i] != sigp [i])
5121 // This filter should be used when we are requesting methods that
5122 // we want to override.
5124 // This makes a number of assumptions, for example
5125 // that the methods being extracted are of a parent
5126 // class (this means we know implicitly that we are
5127 // being called to find out about members by a derived
5130 static bool InheritableMemberSignatureCompare (MemberInfo m, object filter_criteria)
5132 if (MemberSignatureCompare (m, filter_criteria)){
5133 MethodInfo mi = (MethodInfo) m;
5134 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
5136 // If only accessible to the current class.
5137 if (prot == MethodAttributes.Private)
5140 // If only accessible to the defining assembly or
5141 if (prot == MethodAttributes.FamANDAssem ||
5142 prot == MethodAttributes.Assembly){
5143 if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
5149 // Anything else (FamOrAssembly and Public) is fine
5156 // This filter should be used when we are requesting properties that
5157 // we want to override.
5159 // This makes a number of assumptions, for example
5160 // that the methods being extracted are of a parent
5161 // class (this means we know implicitly that we are
5162 // being called to find out about members by a derived
5165 static bool InheritablePropertySignatureCompare (MemberInfo m, object filter_criteria)
5167 if (MemberSignatureCompare (m, filter_criteria)){
5168 PropertyInfo pi = (PropertyInfo) m;
5170 MethodInfo inherited_get = TypeManager.GetPropertyGetter (pi);
5171 MethodInfo inherited_set = TypeManager.GetPropertySetter (pi);
5173 MethodInfo mi = inherited_get == null ? inherited_set : inherited_get;
5175 MethodAttributes prot = mi.Attributes & MethodAttributes.MemberAccessMask;
5177 // If only accessible to the current class.
5178 if (prot == MethodAttributes.Private)
5181 // If only accessible to the defining assembly or
5182 if (prot == MethodAttributes.FamANDAssem ||
5183 prot == MethodAttributes.Assembly){
5184 if (m.DeclaringType.Assembly == CodeGen.AssemblyBuilder)
5190 // Anything else (FamOrAssembly and Public) is fine