2 // class.cs: Class and Struct handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@gnome.org)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
13 // 2002-10-11 Miguel de Icaza <miguel@ximian.com>
15 // * class.cs: Following the comment from 2002-09-26 to AddMethod, I
16 // have fixed a remaining problem: not every AddXXXX was adding a
17 // fully qualified name.
19 // Now everyone registers a fully qualified name in the DeclSpace as
20 // being defined instead of the partial name.
22 // Downsides: we are slower than we need to be due to the excess
23 // copies and the names being registered this way.
25 // The reason for this is that we currently depend (on the corlib
26 // bootstrap for instance) that types are fully qualified, because
27 // we dump all the types in the namespace, and we should really have
28 // types inserted into the proper namespace, so we can only store the
29 // 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.Runtime.InteropServices;
41 using Mono.CompilerServices.SymbolWriter;
43 namespace Mono.CSharp {
53 /// This is the base class for structs and classes.
55 public abstract class TypeContainer : DeclSpace, IMemberContainer {
57 public class MemberCoreArrayList: ArrayList
60 /// Defines the MemberCore objects that are in this array
62 public virtual void DefineContainerMembers ()
64 foreach (MemberCore mc in this) {
69 public virtual void Emit ()
71 foreach (MemberCore mc in this)
76 public class MethodArrayList: MemberCoreArrayList
84 CachedMethods cached_method;
85 TypeContainer container;
87 public MethodArrayList (TypeContainer container)
89 this.container = container;
93 /// Method container contains Equals method
95 public bool HasEquals {
97 cached_method |= CachedMethods.Equals;
101 return (cached_method & CachedMethods.Equals) != 0;
106 /// Method container contains GetHashCode method
108 public bool HasGetHashCode {
110 cached_method |= CachedMethods.GetHashCode;
114 return (cached_method & CachedMethods.GetHashCode) != 0;
118 public override void DefineContainerMembers ()
120 base.DefineContainerMembers ();
122 if ((RootContext.WarningLevel >= 3) && HasEquals && !HasGetHashCode) {
123 Report.Warning (659, container.Location, "'{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
129 public sealed class IndexerArrayList: MemberCoreArrayList
132 /// The indexer name for this container
134 public string IndexerName = DefaultIndexerName;
136 bool seen_normal_indexers = false;
138 TypeContainer container;
140 public IndexerArrayList (TypeContainer container)
142 this.container = container;
146 /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
147 /// class is consistent. Either it is `Item' or it is the name defined by all the
148 /// indexers with the `IndexerName' attribute.
150 /// Turns out that the IndexerNameAttribute is applied to each indexer,
151 /// but it is never emitted, instead a DefaultMember attribute is attached
154 public override void DefineContainerMembers()
156 base.DefineContainerMembers ();
158 string class_indexer_name = null;
161 // If there's both an explicit and an implicit interface implementation, the
162 // explicit one actually implements the interface while the other one is just
163 // a normal indexer. See bug #37714.
166 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
167 foreach (Indexer i in this) {
168 if (i.InterfaceType != null) {
169 if (seen_normal_indexers)
170 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
174 seen_normal_indexers = true;
176 if (class_indexer_name == null) {
177 class_indexer_name = i.ShortName;
181 if (i.ShortName != class_indexer_name)
182 Report.Error (668, i.Location, "Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type");
185 if (class_indexer_name != null)
186 IndexerName = class_indexer_name;
189 public override void Emit ()
193 if (!seen_normal_indexers)
196 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
197 container.TypeBuilder.SetCustomAttribute (cb);
201 public class OperatorArrayList: MemberCoreArrayList
203 TypeContainer container;
205 public OperatorArrayList (TypeContainer container)
207 this.container = container;
211 // Operator pair checking
216 public Type ret_type;
217 public Type type1, type2;
219 public Operator.OpType ot;
221 public OperatorEntry (int f, Operator o)
225 ret_type = o.OperatorMethod.ReturnType;
226 Type [] pt = o.OperatorMethod.ParameterTypes;
233 public override int GetHashCode ()
235 return ret_type.GetHashCode ();
238 public override bool Equals (object o)
240 OperatorEntry other = (OperatorEntry) o;
242 if (other.ret_type != ret_type)
244 if (other.type1 != type1)
246 if (other.type2 != type2)
253 // Checks that some operators come in pairs:
259 // They are matched based on the return type and the argument types
261 void CheckPairedOperators ()
263 Hashtable pairs = new Hashtable (null, null);
264 Operator true_op = null;
265 Operator false_op = null;
266 bool has_equality_or_inequality = false;
268 // Register all the operators we care about.
269 foreach (Operator op in this){
272 switch (op.OperatorType){
273 case Operator.OpType.Equality:
275 has_equality_or_inequality = true;
277 case Operator.OpType.Inequality:
279 has_equality_or_inequality = true;
282 case Operator.OpType.True:
285 case Operator.OpType.False:
289 case Operator.OpType.GreaterThan:
291 case Operator.OpType.LessThan:
294 case Operator.OpType.GreaterThanOrEqual:
296 case Operator.OpType.LessThanOrEqual:
302 OperatorEntry oe = new OperatorEntry (reg, op);
304 object o = pairs [oe];
308 oe = (OperatorEntry) o;
313 if (true_op != null){
314 if (false_op == null)
315 Report.Error (216, true_op.Location, "operator true requires a matching operator false");
316 } else if (false_op != null)
317 Report.Error (216, false_op.Location, "operator false requires a matching operator true");
320 // Look for the mistakes.
322 foreach (DictionaryEntry de in pairs){
323 OperatorEntry oe = (OperatorEntry) de.Key;
330 case Operator.OpType.Equality:
333 case Operator.OpType.Inequality:
336 case Operator.OpType.GreaterThan:
339 case Operator.OpType.LessThan:
342 case Operator.OpType.GreaterThanOrEqual:
345 case Operator.OpType.LessThanOrEqual:
349 Report.Error (216, oe.op.Location,
350 "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
353 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
354 if (container.Methods == null || !container.Methods.HasEquals)
355 Report.Warning (660, container.Location, "'{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
357 if (container.Methods == null || !container.Methods.HasGetHashCode)
358 Report.Warning (661, container.Location, "'{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
362 public override void DefineContainerMembers ()
364 base.DefineContainerMembers ();
365 CheckPairedOperators ();
370 // Whether this is a struct, class or interface
371 public readonly Kind Kind;
373 // Holds a list of classes and structures
376 // Holds the list of properties
377 MemberCoreArrayList properties;
379 // Holds the list of enumerations
380 MemberCoreArrayList enums;
382 // Holds the list of delegates
383 MemberCoreArrayList delegates;
385 // Holds the list of constructors
386 protected MemberCoreArrayList instance_constructors;
388 // Holds the list of fields
389 MemberCoreArrayList fields;
391 // Holds a list of fields that have initializers
392 protected ArrayList initialized_fields;
394 // Holds a list of static fields that have initializers
395 protected ArrayList initialized_static_fields;
397 // Holds the list of constants
398 MemberCoreArrayList constants;
401 MemberCoreArrayList interfaces;
403 // Holds the methods.
404 MethodArrayList methods;
407 MemberCoreArrayList events;
409 // Holds the indexers
410 IndexerArrayList indexers;
412 // Holds the operators
413 MemberCoreArrayList operators;
415 // Holds the iterators
418 // Holds the parts of a partial class;
421 // The emit context for toplevel objects.
425 // Pointers to the default constructor and the default static constructor
427 protected Constructor default_constructor;
428 protected Constructor default_static_constructor;
431 // Whether we have at least one non-static field
433 bool have_nonstatic_fields = false;
436 // This one is computed after we can distinguish interfaces
437 // from classes from the arraylist `type_bases'
439 string base_class_name;
440 TypeExpr parent_type;
442 ArrayList type_bases;
444 bool members_defined;
445 bool members_defined_ok;
447 // The interfaces we implement.
448 protected Type[] ifaces;
449 protected Type ptype;
451 // The parent member container and our member cache
452 IMemberContainer parent_container;
453 MemberCache member_cache;
455 public const string DefaultIndexerName = "Item";
459 public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
460 Attributes attrs, Kind kind, Location l)
461 : base (ns, parent, name, attrs, l)
465 types = new ArrayList ();
467 base_class_name = null;
470 public bool AddToMemberContainer (MemberCore symbol, bool is_method)
472 return AddToContainer (symbol, is_method, String.Concat (Name, '.', symbol.Name), symbol.Name);
475 bool AddToTypeContainer (DeclSpace ds)
477 return AddToContainer (ds, false, ds.Name, ds.Basename);
480 public void AddConstant (Const constant)
482 if (!AddToMemberContainer (constant, false))
485 if (constants == null)
486 constants = new MemberCoreArrayList ();
488 constants.Add (constant);
491 public void AddEnum (Mono.CSharp.Enum e)
493 if (!AddToTypeContainer (e))
497 enums = new MemberCoreArrayList ();
502 public void AddClassOrStruct (TypeContainer c)
504 if (!AddToTypeContainer (c))
510 public void AddDelegate (Delegate d)
512 if (!AddToTypeContainer (d))
515 if (delegates == null)
516 delegates = new MemberCoreArrayList ();
521 public void AddMethod (Method method)
523 if (!AddToMemberContainer (method, true))
527 methods = new MethodArrayList (this);
529 if (method.Name.IndexOf ('.') != -1)
530 methods.Insert (0, method);
532 methods.Add (method);
535 public void AddConstructor (Constructor c)
537 if (c.Name != Basename) {
538 Report.Error (1520, Location, "Class, struct, or interface method must have a return type");
541 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
544 if (default_static_constructor != null) {
545 Report.SymbolRelatedToPreviousError (default_static_constructor);
546 Report.Error (111, c.Location, "Type '{0}' already defines a member " +
547 "called '{1}' with the same parameter types", Name, c.Name);
551 default_static_constructor = c;
554 if (default_constructor != null) {
555 Report.SymbolRelatedToPreviousError (default_constructor);
556 Report.Error (111, c.Location, "Type '{0}' already defines a member " +
557 "called '{1}' with the same parameter types", Name, c.Name);
560 default_constructor = c;
563 if (instance_constructors == null)
564 instance_constructors = new MemberCoreArrayList ();
566 instance_constructors.Add (c);
570 public void AddInterface (TypeContainer iface)
572 if (!AddToTypeContainer (iface))
575 if (interfaces == null) {
576 interfaces = new MemberCoreArrayList ();
579 interfaces.Add (iface);
582 public void AddField (Field field)
584 if (!AddToMemberContainer (field, false))
588 fields = new MemberCoreArrayList ();
592 if (field.HasInitializer){
593 if ((field.ModFlags & Modifiers.STATIC) != 0){
594 if (initialized_static_fields == null)
595 initialized_static_fields = new ArrayList ();
597 initialized_static_fields.Add (field);
600 if (initialized_fields == null)
601 initialized_fields = new ArrayList ();
603 initialized_fields.Add (field);
607 if ((field.ModFlags & Modifiers.STATIC) == 0)
608 have_nonstatic_fields = true;
611 public void AddProperty (Property prop)
613 if (!AddToMemberContainer (prop, false) ||
614 !AddToMemberContainer (prop.Get, true) || !AddToMemberContainer (prop.Set, true))
617 if (properties == null)
618 properties = new MemberCoreArrayList ();
620 if (prop.Name.IndexOf ('.') != -1)
621 properties.Insert (0, prop);
623 properties.Add (prop);
626 public void AddEvent (Event e)
628 if (!AddToMemberContainer (e, false))
631 if (e is EventProperty) {
632 if (!AddToMemberContainer (e.Add, true))
635 if (!AddToMemberContainer (e.Remove, true))
640 events = new MemberCoreArrayList ();
646 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
648 public void AddIndexer (Indexer i)
650 if (indexers == null)
651 indexers = new IndexerArrayList (this);
653 if (i.IsExplicitImpl)
654 indexers.Insert (0, i);
659 public void AddOperator (Operator op)
661 if (!AddToMemberContainer (op, true))
664 if (operators == null)
665 operators = new OperatorArrayList (this);
670 public void AddIterator (Iterator i)
672 if (iterators == null)
673 iterators = new ArrayList ();
678 public void AddType (TypeContainer tc)
683 public void AddPart (ClassPart part)
686 parts = new ArrayList ();
691 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
693 if (a.Type == TypeManager.default_member_type) {
694 if (Indexers != null) {
695 Report.Error (646, a.Location,
696 "Cannot specify the DefaultMember attribute on" +
697 " a type containing an indexer");
702 base.ApplyAttributeBuilder (a, cb);
705 public override AttributeTargets AttributeTargets {
707 throw new NotSupportedException ();
711 public ArrayList Types {
717 public MethodArrayList Methods {
723 public ArrayList Constants {
729 public ArrayList Interfaces {
735 public ArrayList Iterators {
743 return base_class_name;
747 public ArrayList Bases {
757 public ArrayList Fields {
763 public ArrayList InstanceConstructors {
765 return instance_constructors;
769 public ArrayList Properties {
775 public ArrayList Events {
781 public ArrayList Enums {
787 public ArrayList Indexers {
793 public ArrayList Operators {
799 public ArrayList Delegates {
805 public ArrayList Parts {
811 public virtual TypeAttributes TypeAttr {
813 return Modifiers.TypeAttr (ModFlags, this);
817 public string IndexerName {
819 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
824 // Emits the instance field initializers
826 public bool EmitFieldInitializers (EmitContext ec)
829 Expression instance_expr;
832 fields = initialized_static_fields;
833 instance_expr = null;
835 fields = initialized_fields;
836 instance_expr = new This (Location.Null).Resolve (ec);
842 foreach (Field f in fields){
843 Expression e = f.GetInitializerExpression (ec);
847 Location l = f.Location;
848 FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
849 fe.InstanceExpression = instance_expr;
850 fe.IsFieldInitializer = true;
852 ExpressionStatement a = new Assign (fe, e, l);
854 a = a.ResolveStatement (ec);
858 a.EmitStatement (ec);
865 // Defines the default constructors
867 protected void DefineDefaultConstructor (bool is_static)
871 // The default constructor is public
872 // If the class is abstract, the default constructor is protected
873 // The default static constructor is private
875 int mods = Modifiers.PUBLIC;
877 mods = Modifiers.STATIC | Modifiers.PRIVATE;
878 else if ((ModFlags & Modifiers.ABSTRACT) != 0)
879 mods = Modifiers.PROTECTED;
881 c = new Constructor (this, Basename, mods, Parameters.EmptyReadOnlyParameters,
882 new ConstructorBaseInitializer (
883 null, Parameters.EmptyReadOnlyParameters,
889 c.Block = new ToplevelBlock (null, Location);
894 /// The pending methods that need to be implemented
895 // (interfaces or abstract methods)
897 public PendingImplementation Pending;
899 public abstract void Register ();
901 public abstract PendingImplementation GetPendingImplementations ();
903 TypeExpr[] GetPartialBases (out TypeExpr parent, out bool error)
905 ArrayList ifaces = new ArrayList ();
908 Location parent_loc = Location.Null;
910 foreach (ClassPart part in parts) {
912 TypeExpr[] new_ifaces;
914 new_ifaces = part.GetClassBases (out new_parent, out error);
918 if ((parent != null) && (new_parent != null) &&
919 !parent.Equals (new_parent)) {
920 Report.Error (263, part.Location,
921 "Partial declarations of `{0}' must " +
922 "not specify different base classes",
925 if (!Location.IsNull (parent_loc))
926 Report.LocationOfPreviousError (parent_loc);
932 if ((parent == null) && (new_parent != null)) {
934 parent_loc = part.Location;
937 if (new_ifaces == null)
940 foreach (TypeExpr iface in new_ifaces) {
942 foreach (TypeExpr old_iface in ifaces) {
943 if (old_iface.Equals (iface)) {
956 TypeExpr[] retval = new TypeExpr [ifaces.Count];
957 ifaces.CopyTo (retval, 0);
961 TypeExpr[] GetNormalBases (out TypeExpr parent, out bool error)
965 int count = Bases.Count;
968 if (Kind == Kind.Class){
969 TypeExpr name = ResolveTypeExpr (
970 (Expression) Bases [0], false, Location);
987 TypeExpr [] ifaces = new TypeExpr [count-start];
989 for (i = start, j = 0; i < count; i++, j++){
990 Expression name = (Expression) Bases [i];
991 TypeExpr resolved = ResolveTypeExpr (name, false, Location);
992 if (resolved == null) {
997 ifaces [j] = resolved;
1005 /// This function computes the Base class and also the
1006 /// list of interfaces that the class or struct @c implements.
1008 /// The return value is an array (might be null) of
1009 /// interfaces implemented (as Types).
1011 /// The @parent argument is set to the parent object or null
1012 /// if this is `System.Object'.
1014 TypeExpr [] GetClassBases (out TypeExpr parent, out bool error)
1016 ArrayList bases = Bases;
1024 ifaces = GetPartialBases (out parent, out error);
1025 else if (Bases == null){
1029 ifaces = GetNormalBases (out parent, out error);
1034 if ((parent != null) && (Kind == Kind.Class)){
1035 if (parent is TypeParameterExpr){
1037 689, parent.Location,
1038 "Type parameter `{0}' can not be used as a " +
1039 "base class or interface", parent.Name);
1044 if (IsGeneric && parent.IsAttribute){
1046 698, parent.Location,
1047 "A generic type cannot derive from `{0}' " +
1048 "because it is an attribute class",
1054 if (parent.IsSealed){
1056 Report.SymbolRelatedToPreviousError (parent.Type);
1057 if (parent.Type.IsAbstract) {
1058 Report.Error (709, Location, "'{0}': Cannot derive from static class", GetSignatureForError ());
1060 Report.Error (509, Location, "'{0}': Cannot derive from sealed class", GetSignatureForError ());
1065 if (!parent.CanInheritFrom ()){
1066 Report.Error (644, Location,
1067 "`{0}' cannot inherit from special class `{1}'",
1068 Name, parent_type.Name);
1073 if (!parent.AsAccessible (this, ModFlags))
1074 Report.Error (60, Location,
1075 "Inconsistent accessibility: base class `" +
1076 parent.Name + "' is less accessible than class `" +
1081 base_class_name = parent.Name;
1086 int count = ifaces != null ? ifaces.Length : 0;
1088 for (i = 0; i < count; i++) {
1089 TypeExpr iface = (TypeExpr) ifaces [i];
1091 if ((Kind != Kind.Class) && !iface.IsInterface){
1092 string what = Kind == Kind.Struct ?
1093 "Struct" : "Interface";
1095 Report.Error (527, Location,
1096 "In {0} `{1}', type `{2}' is not "+
1097 "an interface", what, Name, iface.Name);
1102 if (iface.IsClass) {
1103 if (parent != null){
1104 Report.Error (527, Location,
1105 "In Class `{0}', `{1}' is not " +
1106 "an interface", Name, iface.Name);
1112 for (int x = 0; x < i; x++) {
1113 if (iface.Equals (ifaces [x])) {
1114 Report.Error (528, Location,
1115 "`{0}' is already listed in " +
1116 "interface list", iface.Name);
1122 if ((Kind == Kind.Interface) &&
1123 !iface.AsAccessible (Parent, ModFlags))
1124 Report.Error (61, Location,
1125 "Inconsistent accessibility: base " +
1126 "interface `{0}' is less accessible " +
1127 "than interface `{1}'", iface.Name,
1134 bool CheckGenericInterfaces (Type[] ifaces)
1136 ArrayList already_checked = new ArrayList ();
1138 for (int i = 0; i < ifaces.Length; i++) {
1139 Type iface = ifaces [i];
1140 foreach (Type t in already_checked) {
1144 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t))
1149 "`{0}' cannot implement both `{1}' and `{2}' " +
1150 "because they may unify for some type " +
1151 "parameter substitutions",
1152 TypeManager.GetFullName (TypeBuilder),
1157 already_checked.Add (iface);
1166 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1168 public override TypeBuilder DefineType ()
1175 if (TypeBuilder != null)
1178 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
1180 TypeAttributes type_attributes = TypeAttr;
1184 if (TypeManager.NamespaceClash (Name, Location)) {
1189 ModuleBuilder builder = CodeGen.Module.Builder;
1190 TypeBuilder = builder.DefineType (
1191 Name, type_attributes, null, null);
1193 TypeBuilder builder;
1194 if (Parent.TypeBuilder != null)
1195 builder = Parent.TypeBuilder;
1197 builder = Parent.DefineType ();
1199 if (builder == null) {
1204 TypeBuilder = builder.DefineNestedType (
1205 MemberName.Basename, type_attributes,
1209 catch (ArgumentException) {
1210 Report.RuntimeMissingSupport ("static classes");
1214 TypeManager.AddUserType (Name, TypeBuilder, this);
1217 foreach (TypeParameter type_param in TypeParameters) {
1218 if (!type_param.Resolve (this)) {
1224 CurrentType = new ConstructedType (
1225 Name, TypeParameters, Location);
1227 string[] param_names = new string [TypeParameters.Length];
1228 for (int i = 0; i < TypeParameters.Length; i++)
1229 param_names [i] = TypeParameters [i].Name;
1231 GenericTypeParameterBuilder[] gen_params;
1233 gen_params = TypeBuilder.DefineGenericParameters (param_names);
1235 for (int i = 0; i < gen_params.Length; i++)
1236 TypeParameters [i].Define (gen_params [i]);
1240 foreach (TypeParameter type_param in TypeParameters)
1241 if (!type_param.DefineType (ec)) {
1247 if ((Kind == Kind.Struct) && TypeManager.value_type == null)
1248 throw new Exception ();
1250 TypeExpr[] iface_exprs = GetClassBases (out parent_type, out error);
1254 if (parent_type == null) {
1255 if (Kind == Kind.Class){
1256 if (RootContext.StdLib)
1257 parent_type = TypeManager.system_object_expr;
1258 else if (Name != "System.Object")
1259 parent_type = TypeManager.system_object_expr;
1260 } else if (Kind == Kind.Struct){
1262 // If we are compiling our runtime,
1263 // and we are defining ValueType, then our
1264 // parent is `System.Object'.
1266 if (!RootContext.StdLib && Name == "System.ValueType")
1267 parent_type = TypeManager.system_object_expr;
1268 else if (Kind == Kind.Struct)
1269 parent_type = TypeManager.system_valuetype_expr;
1273 ConstructedType constructed = parent_type as ConstructedType;
1274 if ((constructed == null) && (parent_type != null))
1275 ptype = parent_type.ResolveType (ec);
1279 if (constructed != null) {
1280 ptype = constructed.ResolveType (ec);
1281 if (ptype == null) {
1287 if (!CheckRecursiveDefinition ()) {
1293 TypeBuilder.SetParent (ptype);
1296 // Structs with no fields need to have at least one byte.
1297 // The right thing would be to set the PackingSize in a DefineType
1298 // but there are no functions that allow interfaces *and* the size to
1302 if ((Kind == Kind.Struct) && !have_nonstatic_fields){
1303 TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
1304 FieldAttributes.Private);
1307 // add interfaces that were not added at type creation
1308 if (iface_exprs != null) {
1309 ifaces = TypeManager.ExpandInterfaces (ec, iface_exprs);
1310 if (ifaces == null) {
1315 foreach (Type itype in ifaces)
1316 TypeBuilder.AddInterfaceImplementation (itype);
1318 if (!CheckGenericInterfaces (ifaces)) {
1323 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1327 foreach (TypeParameter type_param in TypeParameters)
1328 if (!type_param.CheckDependencies (ec)) {
1335 // Finish the setup for the EmitContext
1337 ec.ContainerType = TypeBuilder;
1339 if ((parent_type != null) && parent_type.IsAttribute) {
1340 RootContext.RegisterAttribute (this);
1341 } else if (!(this is Iterator))
1342 RootContext.RegisterOrder (this);
1344 if (!DefineNestedTypes ()) {
1352 protected virtual bool DefineNestedTypes ()
1354 if (Interfaces != null) {
1355 foreach (TypeContainer iface in Interfaces)
1356 if (iface.DefineType () == null)
1360 if (Types != null) {
1361 foreach (TypeContainer tc in Types)
1362 if (tc.DefineType () == null)
1366 if (Delegates != null) {
1367 foreach (Delegate d in Delegates)
1368 if (d.DefineType () == null)
1372 if (Enums != null) {
1373 foreach (Enum en in Enums)
1374 if (en.DefineType () == null)
1378 if (Parts != null) {
1379 foreach (ClassPart part in Parts) {
1380 part.TypeBuilder = TypeBuilder;
1381 part.parent_type = parent_type;
1388 protected bool CheckRecursiveDefinition ()
1391 Report.Error (146, Location,
1392 "Class definition is circular: `{0}'",
1393 GetSignatureForError ());
1400 Type parent = ptype;
1401 if (parent != null) {
1402 if (parent.IsGenericInstance)
1403 parent = parent.GetGenericTypeDefinition ();
1405 TypeContainer ptc = TypeManager.LookupTypeContainer (parent);
1406 if ((ptc != null) && !ptc.CheckRecursiveDefinition ())
1414 static void Error_KeywordNotAllowed (Location loc)
1416 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
1420 /// Populates our TypeBuilder with fields and methods
1422 public override bool DefineMembers (TypeContainer container)
1424 if (members_defined)
1425 return members_defined_ok;
1427 members_defined_ok = DoDefineMembers ();
1428 members_defined = true;
1430 return members_defined_ok;
1433 bool DoDefineMembers ()
1436 // We need to be able to use the member cache while we are checking/defining
1438 if (TypeBuilder.BaseType != null)
1439 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
1442 //if (TypeBuilder.IsInterface) {
1443 // parent_container = TypeManager.LookupInterfaceContainer (base_inteface_types);
1447 if ((ModFlags & Modifiers.NEW) != 0)
1448 Error_KeywordNotAllowed (Location);
1450 // HACK: missing implemenation
1451 // This is not fully functional. Better way how to handle this is to have recursive definition of containers
1452 // instead of flat as we have now.
1453 // Now we are not able to check inner attribute class because its parent had not been defined.
1455 // TODO: remove this if
1456 if (Parent.MemberCache != null) {
1457 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1458 if (conflict_symbol == null) {
1459 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1460 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1462 if ((ModFlags & Modifiers.NEW) == 0) {
1463 Report.SymbolRelatedToPreviousError (conflict_symbol);
1464 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError ());
1470 DefineContainerMembers (constants);
1471 DefineContainerMembers (fields);
1473 if ((Kind == Kind.Class) && !(this is ClassPart) && !(this is StaticClass)){
1474 if ((instance_constructors == null) &&
1475 !(this is StaticClass)) {
1476 if (default_constructor == null)
1477 DefineDefaultConstructor (false);
1480 if (initialized_static_fields != null &&
1481 default_static_constructor == null)
1482 DefineDefaultConstructor (true);
1485 if (Kind == Kind.Struct){
1487 // Structs can not have initialized instance
1490 if (initialized_static_fields != null &&
1491 default_static_constructor == null)
1492 DefineDefaultConstructor (true);
1494 if (initialized_fields != null)
1495 ReportStructInitializedInstanceError ();
1498 Pending = GetPendingImplementations ();
1500 if (parts != null) {
1501 foreach (ClassPart part in parts) {
1502 if (!part.DefineMembers (this))
1508 // Constructors are not in the defined_names array
1510 DefineContainerMembers (instance_constructors);
1512 if (default_static_constructor != null)
1513 default_static_constructor.Define ();
1515 DefineContainerMembers (properties);
1516 DefineContainerMembers (events);
1517 DefineContainerMembers (indexers);
1518 DefineContainerMembers (methods);
1519 DefineContainerMembers (operators);
1520 DefineContainerMembers (enums);
1521 DefineContainerMembers (delegates);
1523 if (CurrentType != null) {
1524 GenericType = CurrentType.ResolveType (ec);
1526 ec.ContainerType = GenericType;
1531 if (!(this is ClassPart))
1532 member_cache = new MemberCache (this);
1535 if (parts != null) {
1536 foreach (ClassPart part in parts)
1537 part.member_cache = member_cache;
1540 if (iterators != null) {
1541 foreach (Iterator iterator in iterators) {
1542 if (iterator.DefineType () == null)
1546 foreach (Iterator iterator in iterators) {
1547 if (!iterator.DefineMembers (this))
1555 void ReportStructInitializedInstanceError ()
1557 string n = TypeBuilder.FullName;
1559 foreach (Field f in initialized_fields){
1562 "`" + n + "." + f.Name + "': can not have " +
1563 "instance field initializers in structs");
1567 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1570 mcal.DefineContainerMembers ();
1573 public override bool Define ()
1575 if (parts != null) {
1576 foreach (ClassPart part in parts) {
1577 if (!part.Define ())
1585 public MemberInfo FindMemberWithSameName (string name, bool ignore_methods)
1587 return ParentContainer.MemberCache.FindMemberWithSameName (name, ignore_methods, null);
1591 /// This function is based by a delegate to the FindMembers routine
1593 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1599 /// This filter is used by FindMembers, and we just keep
1600 /// a global for the filter to `AlwaysAccept'
1602 static MemberFilter accepting_filter;
1605 static TypeContainer ()
1607 accepting_filter = new MemberFilter (AlwaysAccept);
1610 public MethodInfo[] GetMethods ()
1612 ArrayList members = new ArrayList ();
1614 DefineMembers (null);
1616 if (methods != null) {
1617 int len = methods.Count;
1618 for (int i = 0; i < len; i++) {
1619 Method m = (Method) methods [i];
1621 members.Add (m.MethodBuilder);
1625 if (operators != null) {
1626 int len = operators.Count;
1627 for (int i = 0; i < len; i++) {
1628 Operator o = (Operator) operators [i];
1630 members.Add (o.OperatorMethodBuilder);
1634 if (properties != null) {
1635 int len = properties.Count;
1636 for (int i = 0; i < len; i++) {
1637 Property p = (Property) properties [i];
1639 if (p.GetBuilder != null)
1640 members.Add (p.GetBuilder);
1641 if (p.SetBuilder != null)
1642 members.Add (p.SetBuilder);
1646 if (indexers != null) {
1647 int len = indexers.Count;
1648 for (int i = 0; i < len; i++) {
1649 Indexer ix = (Indexer) indexers [i];
1651 if (ix.GetBuilder != null)
1652 members.Add (ix.GetBuilder);
1653 if (ix.SetBuilder != null)
1654 members.Add (ix.SetBuilder);
1658 if (events != null) {
1659 int len = events.Count;
1660 for (int i = 0; i < len; i++) {
1661 Event e = (Event) events [i];
1663 if (e.AddBuilder != null)
1664 members.Add (e.AddBuilder);
1665 if (e.RemoveBuilder != null)
1666 members.Add (e.RemoveBuilder);
1670 MethodInfo[] retMethods = new MethodInfo [members.Count];
1671 members.CopyTo (retMethods, 0);
1676 /// This method returns the members of this type just like Type.FindMembers would
1677 /// Only, we need to use this for types which are _being_ defined because MS'
1678 /// implementation can't take care of that.
1681 // FIXME: return an empty static array instead of null, that cleans up
1682 // some code and is consistent with some coding conventions I just found
1686 // Notice that in various cases we check if our field is non-null,
1687 // something that would normally mean that there was a bug elsewhere.
1689 // The problem happens while we are defining p-invoke methods, as those
1690 // will trigger a FindMembers, but this happens before things are defined
1692 // Since the whole process is a no-op, it is fine to check for null here.
1694 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1695 MemberFilter filter, object criteria)
1697 ArrayList members = null;
1700 if ((bf & BindingFlags.Public) != 0)
1701 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1703 if ((bf & BindingFlags.NonPublic) != 0)
1704 modflags |= Modifiers.PRIVATE;
1706 int static_mask = 0, static_flags = 0;
1707 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1708 case BindingFlags.Static:
1709 static_mask = static_flags = Modifiers.STATIC;
1712 case BindingFlags.Instance:
1713 static_mask = Modifiers.STATIC;
1718 static_mask = static_flags = 0;
1722 Timer.StartTimer (TimerType.TcFindMembers);
1725 filter = accepting_filter;
1727 if ((mt & MemberTypes.Field) != 0) {
1728 if (fields != null) {
1729 int len = fields.Count;
1730 for (int i = 0; i < len; i++) {
1731 Field f = (Field) fields [i];
1733 if ((f.ModFlags & modflags) == 0)
1735 if ((f.ModFlags & static_mask) != static_flags)
1738 FieldBuilder fb = f.FieldBuilder;
1739 if (fb != null && filter (fb, criteria) == true) {
1740 if (members == null)
1741 members = new ArrayList ();
1748 if (constants != null) {
1749 int len = constants.Count;
1750 for (int i = 0; i < len; i++) {
1751 Const con = (Const) constants [i];
1753 if ((con.ModFlags & modflags) == 0)
1755 if ((con.ModFlags & static_mask) != static_flags)
1758 FieldBuilder fb = con.FieldBuilder;
1759 if (fb != null && filter (fb, criteria) == true) {
1760 if (members == null)
1761 members = new ArrayList ();
1769 if ((mt & MemberTypes.Method) != 0) {
1770 if (methods != null) {
1771 int len = methods.Count;
1772 for (int i = 0; i < len; i++) {
1773 Method m = (Method) methods [i];
1775 if ((m.ModFlags & modflags) == 0)
1777 if ((m.ModFlags & static_mask) != static_flags)
1780 MethodBuilder mb = m.MethodBuilder;
1782 if (mb != null && filter (mb, criteria) == true) {
1783 if (members == null)
1784 members = new ArrayList ();
1791 if (operators != null) {
1792 int len = operators.Count;
1793 for (int i = 0; i < len; i++) {
1794 Operator o = (Operator) operators [i];
1796 if ((o.ModFlags & modflags) == 0)
1798 if ((o.ModFlags & static_mask) != static_flags)
1801 MethodBuilder ob = o.OperatorMethodBuilder;
1802 if (ob != null && filter (ob, criteria) == true) {
1803 if (members == null)
1804 members = new ArrayList ();
1811 if (properties != null) {
1812 int len = properties.Count;
1813 for (int i = 0; i < len; i++) {
1814 Property p = (Property) properties [i];
1816 if ((p.ModFlags & modflags) == 0)
1818 if ((p.ModFlags & static_mask) != static_flags)
1824 if (b != null && filter (b, criteria) == true) {
1825 if (members == null)
1826 members = new ArrayList ();
1832 if (b != null && filter (b, criteria) == true) {
1833 if (members == null)
1834 members = new ArrayList ();
1841 if (indexers != null) {
1842 int len = indexers.Count;
1843 for (int i = 0; i < len; i++) {
1844 Indexer ix = (Indexer) indexers [i];
1846 if ((ix.ModFlags & modflags) == 0)
1848 if ((ix.ModFlags & static_mask) != static_flags)
1854 if (b != null && filter (b, criteria) == true) {
1855 if (members == null)
1856 members = new ArrayList ();
1862 if (b != null && filter (b, criteria) == true) {
1863 if (members == null)
1864 members = new ArrayList ();
1872 if ((mt & MemberTypes.Event) != 0) {
1873 if (events != null) {
1874 int len = events.Count;
1875 for (int i = 0; i < len; i++) {
1876 Event e = (Event) events [i];
1878 if ((e.ModFlags & modflags) == 0)
1880 if ((e.ModFlags & static_mask) != static_flags)
1883 MemberInfo eb = e.EventBuilder;
1884 if (eb != null && filter (eb, criteria) == true) {
1885 if (members == null)
1886 members = new ArrayList ();
1888 members.Add (e.EventBuilder);
1894 if ((mt & MemberTypes.Property) != 0){
1895 if (properties != null) {
1896 int len = properties.Count;
1897 for (int i = 0; i < len; i++) {
1898 Property p = (Property) properties [i];
1900 if ((p.ModFlags & modflags) == 0)
1902 if ((p.ModFlags & static_mask) != static_flags)
1905 MemberInfo pb = p.PropertyBuilder;
1906 if (pb != null && filter (pb, criteria) == true) {
1907 if (members == null)
1908 members = new ArrayList ();
1910 members.Add (p.PropertyBuilder);
1915 if (indexers != null) {
1916 int len = indexers.Count;
1917 for (int i = 0; i < len; i++) {
1918 Indexer ix = (Indexer) indexers [i];
1920 if ((ix.ModFlags & modflags) == 0)
1922 if ((ix.ModFlags & static_mask) != static_flags)
1925 MemberInfo ib = ix.PropertyBuilder;
1926 if (ib != null && filter (ib, criteria) == true) {
1927 if (members == null)
1928 members = new ArrayList ();
1930 members.Add (ix.PropertyBuilder);
1936 if ((mt & MemberTypes.NestedType) != 0) {
1937 if (types != null) {
1938 int len = types.Count;
1939 for (int i = 0; i < len; i++) {
1940 TypeContainer t = (TypeContainer) types [i];
1942 if ((t.ModFlags & modflags) == 0)
1945 TypeBuilder tb = t.TypeBuilder;
1946 if (tb != null && (filter (tb, criteria) == true)) {
1947 if (members == null)
1948 members = new ArrayList ();
1955 if (enums != null) {
1956 int len = enums.Count;
1957 for (int i = 0; i < len; i++) {
1958 Enum en = (Enum) enums [i];
1960 if ((en.ModFlags & modflags) == 0)
1963 TypeBuilder tb = en.TypeBuilder;
1964 if (tb != null && (filter (tb, criteria) == true)) {
1965 if (members == null)
1966 members = new ArrayList ();
1973 if (delegates != null) {
1974 int len = delegates.Count;
1975 for (int i = 0; i < len; i++) {
1976 Delegate d = (Delegate) delegates [i];
1978 if ((d.ModFlags & modflags) == 0)
1981 TypeBuilder tb = d.TypeBuilder;
1982 if (tb != null && (filter (tb, criteria) == true)) {
1983 if (members == null)
1984 members = new ArrayList ();
1991 if (interfaces != null) {
1992 int len = interfaces.Count;
1993 for (int i = 0; i < len; i++) {
1994 TypeContainer iface = (TypeContainer) interfaces [i];
1996 if ((iface.ModFlags & modflags) == 0)
1999 TypeBuilder tb = iface.TypeBuilder;
2000 if (tb != null && (filter (tb, criteria) == true)) {
2001 if (members == null)
2002 members = new ArrayList ();
2010 if ((mt & MemberTypes.Constructor) != 0){
2011 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
2012 int len = instance_constructors.Count;
2013 for (int i = 0; i < len; i++) {
2014 Constructor c = (Constructor) instance_constructors [i];
2016 ConstructorBuilder cb = c.ConstructorBuilder;
2017 if (cb != null && filter (cb, criteria) == true) {
2018 if (members == null)
2019 members = new ArrayList ();
2026 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2027 ConstructorBuilder cb =
2028 default_static_constructor.ConstructorBuilder;
2030 if (cb != null && filter (cb, criteria) == true) {
2031 if (members == null)
2032 members = new ArrayList ();
2040 // Lookup members in parent if requested.
2042 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2043 if (TypeBuilder.BaseType != null) {
2044 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2045 if (list.Count > 0) {
2046 if (members == null)
2047 members = new ArrayList ();
2049 members.AddRange (list);
2055 Timer.StopTimer (TimerType.TcFindMembers);
2057 if (members == null)
2058 return MemberList.Empty;
2060 return new MemberList (members);
2063 public override MemberCache MemberCache {
2065 return member_cache;
2069 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2070 MemberFilter filter, object criteria)
2072 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2075 return ds.FindMembers (mt, bf, filter, criteria);
2077 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2081 // FindMethods will look for methods not only in the type `t', but in
2082 // any interfaces implemented by the type.
2084 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
2085 MemberFilter filter, object criteria)
2091 /// Emits the values for the constants
2093 public void EmitConstants ()
2095 if (constants != null)
2096 foreach (Const con in constants)
2101 protected virtual void VerifyMembers (EmitContext ec)
2104 // Check for internal or private fields that were never assigned
2106 if (RootContext.WarningLevel >= 3) {
2107 if (fields != null){
2108 foreach (Field f in fields) {
2109 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2112 if ((f.status & Field.Status.USED) == 0){
2113 Report.Warning (169, f.Location, "The private field '{0}' is never used", f.GetSignatureForError ());
2118 // Only report 649 on level 4
2120 if (RootContext.WarningLevel < 4)
2123 if ((f.status & Field.Status.ASSIGNED) != 0)
2126 Report.Warning (649, f.Location, "Field '{0}' is never assigned to, and will always have its default value '{1}'", f.GetSignatureForError (), "");
2130 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2131 foreach (Event e in events){
2133 Report.Warning (67, e.Location, "The event '{0}' is never used", e.GetSignatureForError ());
2140 /// Emits the code, this step is performed after all
2141 /// the types, enumerations, constructors
2143 public void EmitType ()
2145 if (OptAttributes != null)
2146 OptAttributes.Emit (ec, this);
2150 if (instance_constructors != null) {
2151 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) {
2152 bool has_compliant_args = false;
2154 foreach (Constructor c in instance_constructors) {
2157 if (has_compliant_args)
2160 has_compliant_args = c.HasCompliantArgs;
2162 if (!has_compliant_args)
2163 Report.Error (3015, Location, "'{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2165 foreach (Constructor c in instance_constructors)
2170 if (default_static_constructor != null)
2171 default_static_constructor.Emit ();
2173 if (methods != null)
2174 foreach (Method m in methods)
2177 if (operators != null)
2178 foreach (Operator o in operators)
2181 if (properties != null)
2182 foreach (Property p in properties)
2185 if (indexers != null){
2190 foreach (Field f in fields)
2193 if (events != null){
2194 foreach (Event e in Events)
2198 if (delegates != null) {
2199 foreach (Delegate d in Delegates) {
2204 if (enums != null) {
2205 foreach (Enum e in enums) {
2210 if (parts != null) {
2211 foreach (ClassPart part in parts)
2215 if ((Pending != null) && !(this is ClassPart))
2216 if (Pending.VerifyPendingMethods ())
2221 if (iterators != null)
2222 foreach (Iterator iterator in iterators)
2223 iterator.EmitType ();
2225 // if (types != null)
2226 // foreach (TypeContainer tc in types)
2230 public override void CloseType ()
2232 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2236 caching_flags |= Flags.CloseTypeCreated;
2237 TypeBuilder.CreateType ();
2238 } catch (TypeLoadException){
2240 // This is fine, the code still created the type
2242 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2243 // Console.WriteLine (e.Message);
2245 Console.WriteLine ("In type: " + Name);
2250 foreach (Enum en in Enums)
2254 foreach (TypeContainer tc in Types)
2255 if (tc.Kind == Kind.Struct)
2258 foreach (TypeContainer tc in Types)
2259 if (tc.Kind != Kind.Struct)
2263 if (Delegates != null)
2264 foreach (Delegate d in Delegates)
2267 if (Iterators != null)
2268 foreach (Iterator i in Iterators)
2276 initialized_fields = null;
2277 initialized_static_fields = null;
2286 default_constructor = null;
2287 default_static_constructor = null;
2289 OptAttributes = null;
2291 parent_container = null;
2292 member_cache = null;
2295 // TODO: make it obsolete and use GetSignatureForError
2296 public string MakeName (string n)
2298 return "`" + Name + "." + n + "'";
2302 // Performs the validation on a Method's modifiers (properties have
2303 // the same properties).
2305 public bool MethodModifiersValid (int flags, string n, Location loc)
2307 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2308 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2309 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2313 // At most one of static, virtual or override
2315 if ((flags & Modifiers.STATIC) != 0){
2316 if ((flags & vao) != 0){
2318 112, loc, "static method " + MakeName (n) + "can not be marked " +
2319 "as virtual, abstract or override");
2324 if (Kind == Kind.Struct){
2325 if ((flags & va) != 0){
2326 Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
2331 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2333 113, loc, MakeName (n) +
2334 " marked as override cannot be marked as new or virtual");
2339 // If the declaration includes the abstract modifier, then the
2340 // declaration does not include static, virtual or extern
2342 if ((flags & Modifiers.ABSTRACT) != 0){
2343 if ((flags & Modifiers.EXTERN) != 0){
2345 180, loc, MakeName (n) + " can not be both abstract and extern");
2349 if ((flags & Modifiers.VIRTUAL) != 0){
2351 503, loc, MakeName (n) + " can not be both abstract and virtual");
2355 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2357 513, loc, MakeName (n) +
2358 " is abstract but its container class is not");
2364 if ((flags & Modifiers.PRIVATE) != 0){
2365 if ((flags & vao) != 0){
2367 621, loc, MakeName (n) +
2368 " virtual or abstract members can not be private");
2373 if ((flags & Modifiers.SEALED) != 0){
2374 if ((flags & Modifiers.OVERRIDE) == 0){
2376 238, loc, MakeName (n) +
2377 " cannot be sealed because it is not an override");
2385 public bool UserDefinedStaticConstructor {
2387 return default_static_constructor != null;
2391 protected override bool VerifyClsCompliance (DeclSpace ds)
2393 if (!base.VerifyClsCompliance (ds))
2398 // parent_container is null for System.Object
2399 if (parent_container != null && !AttributeTester.IsClsCompliant (parent_container.Type)) {
2400 Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (parent_container.Type));
2407 /// Checks whether container name is CLS Compliant
2409 void VerifyClsName ()
2411 Hashtable parent_members = parent_container == null ?
2413 parent_container.MemberCache.GetPublicMembers ();
2414 Hashtable this_members = new Hashtable ();
2416 foreach (DictionaryEntry entry in defined_names) {
2417 MemberCore mc = (MemberCore)entry.Value;
2418 if (!mc.IsClsCompliaceRequired (this))
2421 string name = (string)entry.Key;
2422 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2424 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2425 object found = parent_members [lcase];
2426 if (found == null) {
2427 found = this_members [lcase];
2428 if (found == null) {
2429 this_members.Add (lcase, mc);
2434 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2437 if (found is MemberInfo) {
2438 if (basename == ((MemberInfo)found).Name)
2440 Report.SymbolRelatedToPreviousError ((MemberInfo)found);
2442 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2444 Report.Error (3005, mc.Location, "Identifier '{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2450 /// Performs checks for an explicit interface implementation. First it
2451 /// checks whether the `interface_type' is a base inteface implementation.
2452 /// Then it checks whether `name' exists in the interface type.
2454 public virtual bool VerifyImplements (Type interface_type, string full,
2455 string name, Location loc)
2459 if (ifaces != null){
2460 foreach (Type t in ifaces){
2461 if (t == interface_type){
2469 Report.Error (540, "`" + full + "': containing class does not implement interface `" + interface_type.FullName + "'");
2476 protected override void VerifyObsoleteAttribute()
2478 CheckUsageOfObsoleteAttribute (TypeBuilder.BaseType);
2483 foreach (Type iface in ifaces) {
2484 CheckUsageOfObsoleteAttribute (iface);
2492 string IMemberContainer.Name {
2498 Type IMemberContainer.Type {
2504 MemberCache IMemberContainer.MemberCache {
2506 return member_cache;
2510 bool IMemberContainer.IsInterface {
2512 return Kind == Kind.Interface;
2516 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2518 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2520 if (GenericType != null)
2521 return TypeManager.FindMembers (GenericType, mt, new_bf,
2524 return FindMembers (mt, new_bf, null, null);
2527 public virtual IMemberContainer ParentContainer {
2529 return parent_container;
2535 public class PartialContainer : TypeContainer {
2537 public readonly Namespace Namespace;
2538 public readonly int OriginalModFlags;
2539 public readonly int AllowedModifiers;
2540 public readonly TypeAttributes DefaultTypeAttributes;
2542 static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
2543 MemberName name, int mod_flags, Kind kind,
2546 PartialContainer pc;
2547 string full_name = name.GetName (true);
2548 DeclSpace ds = (DeclSpace) RootContext.Tree.Decls [full_name];
2550 pc = ds as PartialContainer;
2554 260, ds.Location, "Missing partial modifier " +
2555 "on declaration of type `{0}'; another " +
2556 "partial implementation of this type exists",
2559 Report.LocationOfPreviousError (loc);
2563 if (pc.Kind != kind) {
2565 261, loc, "Partial declarations of `{0}' " +
2566 "must be all classes, all structs or " +
2567 "all interfaces", name);
2571 if (pc.OriginalModFlags != mod_flags) {
2573 262, loc, "Partial declarations of `{0}' " +
2574 "have conflicting accessibility modifiers",
2582 pc = new PartialContainer (ns, parent, name, mod_flags, kind, loc);
2583 RootContext.Tree.RecordDecl (full_name, pc);
2584 parent.AddType (pc);
2589 public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
2590 MemberName name, int mod, Attributes attrs,
2591 Kind kind, Location loc)
2593 PartialContainer pc = Create (ns, parent, name, mod, kind, loc);
2595 // An error occured; create a dummy container, but don't
2597 pc = new PartialContainer (ns, parent, name, mod, kind, loc);
2600 ClassPart part = new ClassPart (ns, pc, mod, attrs, kind, loc);
2605 protected PartialContainer (NamespaceEntry ns, TypeContainer parent,
2606 MemberName name, int mod, Kind kind, Location l)
2607 : base (ns, parent, name, null, kind, l)
2609 this.Namespace = ns.NS;
2613 AllowedModifiers = Class.AllowedModifiers;
2614 DefaultTypeAttributes = Class.DefaultTypeAttributes;
2618 AllowedModifiers = Struct.AllowedModifiers;
2619 DefaultTypeAttributes = Struct.DefaultTypeAttributes;
2622 case Kind.Interface:
2623 AllowedModifiers = Interface.AllowedModifiers;
2624 DefaultTypeAttributes = Interface.DefaultTypeAttributes;
2628 throw new InvalidOperationException ();
2632 if (parent.Parent == null)
2633 accmods = Modifiers.INTERNAL;
2635 accmods = Modifiers.PRIVATE;
2637 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2638 this.OriginalModFlags = mod;
2641 public override void Register ()
2643 if (Kind == Kind.Interface)
2644 Parent.AddInterface (this);
2645 else if (Kind == Kind.Class || Kind == Kind.Struct)
2646 Parent.AddClassOrStruct (this);
2648 throw new InvalidOperationException ();
2651 public override PendingImplementation GetPendingImplementations ()
2653 return PendingImplementation.GetPendingImplementations (this);
2656 public ClassPart AddPart (NamespaceEntry ns, int mod, Attributes attrs,
2659 ClassPart part = new ClassPart (ns, this, mod, attrs, Kind, l);
2664 public override TypeAttributes TypeAttr {
2666 return base.TypeAttr | DefaultTypeAttributes;
2671 public class ClassPart : TypeContainer, IMemberContainer {
2672 public readonly PartialContainer PartialContainer;
2673 public readonly bool IsPartial;
2675 public ClassPart (NamespaceEntry ns, PartialContainer parent,
2676 int mod, Attributes attrs, Kind kind, Location l)
2677 : base (ns, parent.Parent, parent.MemberName, attrs, kind, l)
2679 this.PartialContainer = parent;
2680 this.IsPartial = true;
2683 if (parent.Parent == null)
2684 accmods = Modifiers.INTERNAL;
2686 accmods = Modifiers.PRIVATE;
2688 this.ModFlags = Modifiers.Check (
2689 parent.AllowedModifiers, mod, accmods, l);
2692 public override void Register ()
2696 public override PendingImplementation GetPendingImplementations ()
2698 return PartialContainer.Pending;
2701 public override bool VerifyImplements (Type interface_type, string full,
2702 string name, Location loc)
2704 return PartialContainer.VerifyImplements (
2705 interface_type, full, name, loc);
2708 public override IMemberContainer ParentContainer {
2710 return PartialContainer.ParentContainer;
2715 public abstract class ClassOrStruct : TypeContainer {
2716 bool hasExplicitLayout = false;
2718 public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
2719 MemberName name, Attributes attrs, Kind kind,
2721 : base (ns, parent, name, attrs, kind, l)
2725 public override PendingImplementation GetPendingImplementations ()
2727 return PendingImplementation.GetPendingImplementations (this);
2730 protected override void VerifyMembers (EmitContext ec)
2732 if (Fields != null) {
2733 foreach (Field f in Fields) {
2734 if ((f.ModFlags & Modifiers.STATIC) != 0)
2736 if (hasExplicitLayout) {
2737 if (f.OptAttributes == null
2738 || !f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
2739 Report.Error (625, f.Location,
2740 "Instance field of type marked with"
2741 + " StructLayout(LayoutKind.Explicit) must have a"
2742 + " FieldOffset attribute.");
2746 if (f.OptAttributes != null
2747 && f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
2748 Report.Error (636, f.Location,
2749 "The FieldOffset attribute can only be placed on members of "
2750 + "types marked with the StructLayout(LayoutKind.Explicit)");
2755 base.VerifyMembers (ec);
2758 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2760 if (a.Type == TypeManager.struct_layout_attribute_type
2761 && (LayoutKind) a.GetPositionalValue (0) == LayoutKind.Explicit)
2762 hasExplicitLayout = true;
2764 base.ApplyAttributeBuilder (a, cb);
2767 public override void Register ()
2769 Parent.AddClassOrStruct (this);
2774 /// Class handles static classes declaration
2776 public sealed class StaticClass: Class {
2777 public StaticClass (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2778 Attributes attrs, Location l)
2779 : base (ns, parent, name, mod & ~Modifiers.STATIC, attrs, l)
2781 if (RootContext.Version == LanguageVersion.ISO_1) {
2782 Report.FeatureIsNotStandardized (l, "static classes");
2783 Environment.Exit (1);
2787 protected override void DefineContainerMembers (MemberCoreArrayList list)
2792 foreach (MemberCore m in list) {
2793 if (m is Operator) {
2794 Report.Error (715, m.Location, "'{0}': static classes cannot contain user-defined operators", m.GetSignatureForError (this));
2798 if ((m.ModFlags & Modifiers.STATIC) != 0)
2801 if (m is Constructor) {
2802 Report.Error (710, m.Location, "'{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2806 if (m is Destructor) {
2807 Report.Error (711, m.Location, "'{0}': Static class cannot contain destructor", GetSignatureForError ());
2810 Report.Error (708, m.Location, "'{0}': cannot declare instance members in a static class", m.GetSignatureForError (this));
2813 base.DefineContainerMembers (list);
2816 public override TypeBuilder DefineType()
2818 TypeBuilder tb = base.DefineType ();
2822 if ((ptype != null) && (ptype != TypeManager.object_type)) {
2825 "Static class '{0}' cannot derive from type '{1}'. " +
2826 "Static classes must derive from object",
2827 GetSignatureForError (), ptype);
2831 if (ifaces != null) {
2832 foreach (Type t in ifaces)
2833 Report.SymbolRelatedToPreviousError (t);
2836 "'{0}': static classes cannot implement interfaces",
2837 GetSignatureForError ());
2842 public override TypeAttributes TypeAttr {
2844 return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
2849 public class Class : ClassOrStruct {
2851 // Modifiers allowed in a class declaration
2853 public const int AllowedModifiers =
2856 Modifiers.PROTECTED |
2857 Modifiers.INTERNAL |
2859 Modifiers.ABSTRACT |
2863 // Information in the case we are an attribute type
2864 AttributeUsageAttribute attribute_usage;
2866 public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2867 Attributes attrs, Location l)
2868 : base (ns, parent, name, attrs, Kind.Class, l)
2872 if (parent.Parent == null)
2873 accmods = Modifiers.INTERNAL;
2875 accmods = Modifiers.PRIVATE;
2877 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2878 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.SEALED)) == (Modifiers.ABSTRACT | Modifiers.SEALED)) {
2879 Report.Error (502, Location, "'{0}' cannot be both abstract and sealed", GetSignatureForError ());
2882 attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
2885 public override AttributeTargets AttributeTargets {
2887 return AttributeTargets.Class;
2891 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2893 if (a.UsageAttribute != null) {
2894 if (ptype != TypeManager.attribute_type &&
2895 !ptype.IsSubclassOf (TypeManager.attribute_type) &&
2896 TypeBuilder.FullName != "System.Attribute") {
2897 Report.Error (641, a.Location, "Attribute '{0}' is only valid on classes derived from System.Attribute", a.Name);
2899 attribute_usage = a.UsageAttribute;
2902 base.ApplyAttributeBuilder (a, cb);
2905 public AttributeUsageAttribute AttributeUsage {
2907 return attribute_usage;
2911 public const TypeAttributes DefaultTypeAttributes =
2912 TypeAttributes.AutoLayout | TypeAttributes.Class;
2915 // FIXME: How do we deal with the user specifying a different
2918 public override TypeAttributes TypeAttr {
2920 return base.TypeAttr | DefaultTypeAttributes;
2925 public class Struct : ClassOrStruct {
2927 // Modifiers allowed in a struct declaration
2929 public const int AllowedModifiers =
2932 Modifiers.PROTECTED |
2933 Modifiers.INTERNAL |
2937 public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
2938 int mod, Attributes attrs, Location l)
2939 : base (ns, parent, name, attrs, Kind.Struct, l)
2943 if (parent.Parent == null)
2944 accmods = Modifiers.INTERNAL;
2946 accmods = Modifiers.PRIVATE;
2948 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2950 this.ModFlags |= Modifiers.SEALED;
2953 public override AttributeTargets AttributeTargets {
2955 return AttributeTargets.Struct;
2959 public const TypeAttributes DefaultTypeAttributes =
2960 TypeAttributes.SequentialLayout |
2961 TypeAttributes.Sealed |
2962 TypeAttributes.BeforeFieldInit;
2965 // FIXME: Allow the user to specify a different set of attributes
2966 // in some cases (Sealed for example is mandatory for a class,
2967 // but what SequentialLayout can be changed
2969 public override TypeAttributes TypeAttr {
2971 return base.TypeAttr | DefaultTypeAttributes;
2979 public class Interface : TypeContainer, IMemberContainer {
2981 /// Modifiers allowed in a class declaration
2983 public const int AllowedModifiers =
2986 Modifiers.PROTECTED |
2987 Modifiers.INTERNAL |
2991 public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name,
2992 int mod, Attributes attrs, Location l)
2993 : base (ns, parent, name, attrs, Kind.Interface, l)
2997 if (parent.Parent == null)
2998 accmods = Modifiers.INTERNAL;
3000 accmods = Modifiers.PRIVATE;
3002 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
3005 public override void Register ()
3007 Parent.AddInterface (this);
3010 public override PendingImplementation GetPendingImplementations ()
3015 public override AttributeTargets AttributeTargets {
3017 return AttributeTargets.Interface;
3021 public const TypeAttributes DefaultTypeAttributes =
3022 TypeAttributes.AutoLayout |
3023 TypeAttributes.Abstract |
3024 TypeAttributes.Interface;
3026 public override TypeAttributes TypeAttr {
3028 return base.TypeAttr | DefaultTypeAttributes;
3033 public abstract class MethodCore : MemberBase {
3034 public readonly Parameters Parameters;
3035 public readonly GenericMethod GenericMethod;
3036 public readonly DeclSpace ds;
3037 protected Block block;
3040 // Parameters, cached for semantic analysis.
3042 protected InternalParameters parameter_info;
3043 protected Type [] parameter_types;
3045 // Whether this is an operator method.
3046 public bool IsOperator;
3049 // The method we're overriding if this is an override method.
3051 protected MethodInfo parent_method = null;
3053 static string[] attribute_targets = new string [] { "method", "return" };
3055 public MethodCore (TypeContainer parent, GenericMethod generic,
3056 Expression type, int mod, int allowed_mod, bool is_iface,
3057 MemberName name, Attributes attrs, Parameters parameters,
3059 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
3062 Parameters = parameters;
3063 IsInterface = is_iface;
3064 this.GenericMethod = generic;
3066 if (generic != null)
3073 // Returns the System.Type array for the parameters of this method
3075 public Type [] ParameterTypes {
3077 return parameter_types;
3081 public InternalParameters ParameterInfo
3084 return parameter_info;
3088 public Block Block {
3098 protected override bool CheckBase ()
3100 if (!base.CheckBase ())
3103 // Check whether arguments were correct.
3104 if (!DoDefineParameters ())
3107 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3113 // Is null for System.Object while compiling corlib and base interfaces
3114 if (Parent.ParentContainer == null) {
3115 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3116 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3121 Type parent_ret_type = null;
3122 parent_method = FindOutParentMethod (Parent, ref parent_ret_type);
3124 // method is override
3125 if (parent_method != null) {
3127 if (!CheckMethodAgainstBase ())
3130 if ((ModFlags & Modifiers.NEW) == 0) {
3131 if (!MemberType.Equals (TypeManager.TypeToCoreType (parent_ret_type))) {
3132 Report.SymbolRelatedToPreviousError (parent_method);
3133 Report.Error (508, Location, GetSignatureForError (Parent) + ": cannot " +
3134 "change return type when overriding inherited member");
3139 if (RootContext.WarningLevel > 2) {
3140 if (Name == "Equals" && parameter_types.Length == 1 && parameter_types [0] == TypeManager.object_type)
3141 Parent.Methods.HasEquals = true;
3142 else if (Name == "GetHashCode" && parameter_types.Length == 0)
3143 Parent.Methods.HasGetHashCode = true;
3146 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (parent_method);
3148 EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
3149 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
3150 Report.SymbolRelatedToPreviousError (parent_method);
3151 Report.Warning (672, 1, Location, "Member '{0}' overrides obsolete member. Add the Obsolete attribute to '{0}'", GetSignatureForError (Parent));
3157 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3158 Report.Error (115, Location, "'{0}': no suitable methods found to override", GetSignatureForError (Parent));
3162 MemberInfo conflict_symbol = Parent.FindMemberWithSameName (Name, !(this is Property));
3163 if (conflict_symbol == null) {
3164 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3165 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3170 if ((ModFlags & Modifiers.NEW) == 0) {
3171 if (this is Method && conflict_symbol is MethodBase)
3174 Report.SymbolRelatedToPreviousError (conflict_symbol);
3175 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3183 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3184 // that have been defined.
3186 // `name' is the user visible name for reporting errors (this is used to
3187 // provide the right name regarding method names and properties)
3189 bool CheckMethodAgainstBase ()
3193 // TODO: replace with GetSignatureForError
3194 string name = parent_method.DeclaringType.Name + "." + parent_method.Name;
3196 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3197 if (!(parent_method.IsAbstract || parent_method.IsVirtual)){
3199 506, Location, Parent.MakeName (Name) +
3200 ": cannot override inherited member `" +
3201 name + "' because it is not " +
3202 "virtual, abstract or override");
3206 // Now we check that the overriden method is not final
3208 if (parent_method.IsFinal) {
3209 // This happens when implementing interface methods.
3210 if (parent_method.IsHideBySig && parent_method.IsVirtual) {
3212 506, Location, Parent.MakeName (Name) +
3213 ": cannot override inherited member `" +
3214 name + "' because it is not " +
3215 "virtual, abstract or override");
3217 Report.Error (239, Location, Parent.MakeName (Name) + " : cannot " +
3218 "override inherited member `" + name +
3219 "' because it is sealed.");
3223 // Check that the permissions are not being changed
3225 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3226 MethodAttributes parentp = parent_method.Attributes & MethodAttributes.MemberAccessMask;
3229 // special case for "protected internal"
3232 if ((parentp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3234 // when overriding protected internal, the method can be declared
3235 // protected internal only within the same assembly
3238 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3239 if (Parent.TypeBuilder.Assembly != parent_method.DeclaringType.Assembly){
3241 // assemblies differ - report an error
3244 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3246 } else if (thisp != parentp) {
3248 // same assembly, but other attributes differ - report an error
3251 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3254 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3256 // if it's not "protected internal", it must be "protected"
3259 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3261 } else if (Parent.TypeBuilder.Assembly == parent_method.DeclaringType.Assembly) {
3263 // protected within the same assembly - an error
3265 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3267 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3268 (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3270 // protected ok, but other attributes differ - report an error
3272 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3276 if (thisp != parentp){
3277 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3283 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3284 ModFlags |= Modifiers.NEW;
3285 Report.SymbolRelatedToPreviousError (parent_method);
3286 if (!IsInterface && (parent_method.IsVirtual || parent_method.IsAbstract)) {
3287 if (RootContext.WarningLevel >= 2)
3288 Report.Warning (114, Location, "'{0}' hides inherited member '{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword", GetSignatureForError (Parent), parent_method);
3290 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3296 void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method, string name)
3299 // FIXME: report the old/new permissions?
3302 507, Location, parent.MakeName (Name) +
3303 ": can't change the access modifiers when overriding inherited " +
3304 "member `" + name + "'");
3307 protected static string Error722 {
3309 return "'{0}': static types cannot be used as return types";
3314 /// For custom member duplication search in a container
3316 protected abstract bool CheckForDuplications ();
3319 /// Gets parent method and its return type
3321 protected abstract MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type);
3323 protected virtual bool DoDefineParameters ()
3325 // Check if arguments were correct
3326 parameter_types = Parameters.GetParameterInfo (ds);
3327 if ((parameter_types == null) ||
3328 !CheckParameters (ds, parameter_types))
3331 TypeParameter[] tparam = ds.IsGeneric ? ds.TypeParameters : null;
3332 parameter_info = new InternalParameters (ds, Parameters, tparam);
3334 Parameter array_param = Parameters.ArrayParameter;
3335 if ((array_param != null) &&
3336 (!array_param.ParameterType.IsArray ||
3337 (array_param.ParameterType.GetArrayRank () != 1))) {
3338 Report.Error (225, Location, "params parameter has to be a single dimensional array");
3345 void error_425 (Type old, Type t, string name)
3347 Report.Error (425, Location,
3348 "The constraints of type parameter `{0}' " +
3349 "of method `{1}' must match the constraints for " +
3350 "type parameter `{2}' of method `{3}'",
3351 TypeManager.CSharpName (old), Name,
3352 TypeManager.CSharpName (t), name);
3355 protected override bool CheckGenericOverride (MethodInfo method, string name)
3357 ParameterData pd = Invocation.GetParameterData (method);
3359 for (int i = 0; i < ParameterTypes.Length; i++) {
3360 GenericConstraints ogc = pd.GenericConstraints (i);
3361 GenericConstraints gc = ParameterInfo.GenericConstraints (i);
3363 if ((gc == null) && (ogc == null))
3366 Type ot = pd.ParameterType (i);
3367 Type t = ParameterTypes [i];
3369 if (!((gc != null) && (ogc != null))) {
3370 error_425 (ot, t, name);
3374 if (gc.HasConstructor != ogc.HasConstructor) {
3375 error_425 (ot, t, name);
3379 if (ogc.HasClassConstraint != gc.HasClassConstraint) {
3380 error_425 (ot, t, name);
3384 if (ogc.HasClassConstraint &&
3385 !ogc.ClassConstraint.Equals (gc.ClassConstraint)) {
3386 error_425 (ot, t, name);
3390 Type[] oct = ogc.InterfaceConstraints;
3391 Type[] ct = gc.InterfaceConstraints;
3393 if (oct.Length != ct.Length) {
3394 error_425 (ot, t, name);
3398 for (int j = 0; j < oct.Length; j++)
3399 if (!oct [j].Equals (ct [j])) {
3400 error_425 (ot, t, name);
3408 public override string[] ValidAttributeTargets {
3410 return attribute_targets;
3414 protected override bool VerifyClsCompliance (DeclSpace ds)
3416 if (!base.VerifyClsCompliance (ds)) {
3417 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
3418 Report.Error (3011, Location, "'{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3423 if (Parameters.HasArglist) {
3424 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3427 if (!AttributeTester.IsClsCompliant (MemberType)) {
3428 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
3431 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
3436 protected bool IsDuplicateImplementation (MethodCore method)
3438 if ((method == this) ||
3439 (method.MemberName.GetTypeName () != MemberName.GetTypeName ()))
3442 Type[] param_types = method.ParameterTypes;
3443 if (param_types == null)
3446 if (param_types.Length != ParameterTypes.Length)
3449 int type_params = 0;
3450 if (GenericMethod != null)
3451 type_params = GenericMethod.CountTypeParameters;
3453 int m_type_params = 0;
3454 if (method.GenericMethod != null)
3455 m_type_params = method.GenericMethod.CountTypeParameters;
3457 if (type_params != m_type_params)
3463 Type[] infered_types;
3464 if (type_params > 0)
3465 infered_types = new Type [type_params];
3467 infered_types = null;
3469 may_unify = Invocation.InferTypeArguments (
3470 param_types, ParameterTypes, ref infered_types);
3473 if (type_params > 0)
3474 infered_types = new Type [type_params];
3476 infered_types = null;
3478 may_unify = Invocation.InferTypeArguments (
3479 ParameterTypes, param_types, ref infered_types);
3482 for (int i = 0; i < param_types.Length; i++) {
3483 if (param_types [i] != ParameterTypes [i])
3487 // TODO: make operator compatible with MethodCore to avoid this
3488 if (this is Operator && method is Operator) {
3489 if (MemberType != method.MemberType)
3495 // Try to report 663: method only differs on out/ref
3497 ParameterData info = ParameterInfo;
3498 ParameterData other_info = method.ParameterInfo;
3499 for (int i = 0; i < info.Count; i++){
3500 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3501 Report.Error (663, Location,
3502 "Overload method only differs " +
3503 "in parameter modifier");
3508 Report.SymbolRelatedToPreviousError (method);
3509 Report.Error (111, Location, "Type '{0}' already defines a member called '{1}' with the same parameter types", Parent.Name, Name);
3511 } else if (may_unify) {
3512 Report.Error (408, Location,
3513 "`{0}' cannot define overload members that " +
3514 "may unify for some type parameter substitutions",
3522 protected override void VerifyObsoleteAttribute()
3524 base.VerifyObsoleteAttribute ();
3526 if (parameter_types == null)
3529 foreach (Type type in parameter_types) {
3530 CheckUsageOfObsoleteAttribute (type);
3535 public class SourceMethod : ISourceMethod
3537 TypeContainer container;
3540 protected SourceMethod (TypeContainer container, MethodBase builder,
3541 ISourceFile file, Location start, Location end)
3543 this.container = container;
3544 this.builder = builder;
3546 CodeGen.SymbolWriter.OpenMethod (
3547 file, this, start.Row, 0, end.Row, 0);
3550 public string Name {
3551 get { return builder.Name; }
3554 public int NamespaceID {
3555 get { return container.NamespaceEntry.SymbolFileID; }
3560 if (builder is MethodBuilder)
3561 return ((MethodBuilder) builder).GetToken ().Token;
3562 else if (builder is ConstructorBuilder)
3563 return ((ConstructorBuilder) builder).GetToken ().Token;
3565 throw new NotSupportedException ();
3569 public void CloseMethod ()
3571 if (CodeGen.SymbolWriter != null)
3572 CodeGen.SymbolWriter.CloseMethod ();
3575 public static SourceMethod Create (TypeContainer parent,
3576 MethodBase builder, Block block)
3578 if (CodeGen.SymbolWriter == null)
3583 Location start_loc = block.StartLocation;
3584 if (Location.IsNull (start_loc))
3587 Location end_loc = block.EndLocation;
3588 if (Location.IsNull (end_loc))
3591 ISourceFile file = start_loc.SourceFile;
3595 return new SourceMethod (
3596 parent, builder, file, start_loc, end_loc);
3600 public class Method : MethodCore, IIteratorContainer, IMethodData {
3601 public MethodBuilder MethodBuilder;
3602 public MethodData MethodData;
3603 ReturnParameter return_attributes;
3606 /// Modifiers allowed in a class declaration
3608 const int AllowedModifiers =
3611 Modifiers.PROTECTED |
3612 Modifiers.INTERNAL |
3617 Modifiers.OVERRIDE |
3618 Modifiers.ABSTRACT |
3620 Modifiers.METHOD_YIELDS |
3623 const int AllowedInterfaceModifiers =
3624 Modifiers.NEW | Modifiers.UNSAFE;
3627 // return_type can be "null" for VOID values.
3629 public Method (TypeContainer parent, GenericMethod generic,
3630 Expression return_type, int mod, bool is_iface,
3631 MemberName name, Parameters parameters, Attributes attrs,
3633 : base (parent, generic, return_type, mod,
3634 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3635 is_iface, name, attrs, parameters, l)
3639 public override AttributeTargets AttributeTargets {
3641 return AttributeTargets.Method;
3645 public override string GetSignatureForError()
3647 if (MethodBuilder == null) {
3648 return GetSignatureForError (Parent);
3650 return TypeManager.CSharpSignature (MethodBuilder);
3654 /// Use this method when MethodBuilder is null
3656 public override string GetSignatureForError (TypeContainer tc)
3658 // TODO: get params from somewhere
3659 if (parameter_info == null)
3660 return base.GetSignatureForError (tc);
3662 // TODO: move to parameters
3663 System.Text.StringBuilder args = new System.Text.StringBuilder ();
3664 if (parameter_info.Parameters.FixedParameters != null) {
3665 for (int i = 0; i < parameter_info.Parameters.FixedParameters.Length; ++i) {
3666 Parameter p = parameter_info.Parameters.FixedParameters [i];
3667 args.Append (p.GetSignatureForError ());
3669 if (i < parameter_info.Parameters.FixedParameters.Length - 1)
3674 return String.Concat (base.GetSignatureForError (tc), "(", args.ToString (), ")");
3677 void DuplicateEntryPoint (MethodInfo b, Location location)
3681 "Program `" + CodeGen.FileName +
3682 "' has more than one entry point defined: `" +
3683 TypeManager.CSharpSignature(b) + "'");
3686 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
3688 if (b.ReturnType != TypeManager.void_type &&
3689 b.ReturnType != TypeManager.int32_type)
3692 if (pinfo.Count == 0)
3695 if (pinfo.Count > 1)
3698 Type t = pinfo.ParameterType(0);
3700 (t.GetArrayRank() == 1) &&
3701 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3702 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3708 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3710 if (a.Target == AttributeTargets.ReturnValue) {
3711 if (return_attributes == null)
3712 return_attributes = new ReturnParameter (MethodBuilder, Location);
3714 return_attributes.ApplyAttributeBuilder (a, cb);
3718 if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall) {
3719 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3722 if (a.Type == TypeManager.dllimport_type) {
3723 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3724 if ((ModFlags & extern_static) != extern_static) {
3725 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3731 if (a.Type == TypeManager.conditional_attribute_type) {
3732 if (IsOperator || IsExplicitImpl) {
3733 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
3737 if (ReturnType != TypeManager.void_type) {
3738 Report.Error (578, Location, "Conditional not valid on '{0}' because its return new ErrorData ( type is not void", GetSignatureForError ());
3742 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3743 Report.Error (243, Location, "Conditional not valid on '{0}' because it is an override method", GetSignatureForError ());
3748 Report.Error (582, Location, "Conditional not valid on interface members");
3752 if (MethodData.IsImplementing) {
3753 Report.Error (629, Location, "Conditional member '{0}' cannot implement interface member", GetSignatureForError ());
3757 for (int i = 0; i < parameter_info.Count; ++i) {
3758 if ((parameter_info.ParameterModifier (i) & Parameter.Modifier.OUT) != 0) {
3759 Report.Error (685, Location, "Conditional method '{0}' cannot have an out parameter", GetSignatureForError ());
3765 MethodBuilder.SetCustomAttribute (cb);
3768 protected override bool CheckForDuplications ()
3770 ArrayList ar = Parent.Methods;
3772 int arLen = ar.Count;
3774 for (int i = 0; i < arLen; i++) {
3775 Method m = (Method) ar [i];
3776 if (IsDuplicateImplementation (m))
3781 ar = Parent.Properties;
3783 for (int i = 0; i < ar.Count; ++i) {
3784 PropertyBase pb = (PropertyBase) ar [i];
3785 if (pb.AreAccessorsDuplicateImplementation (this))
3790 ar = Parent.Indexers;
3792 for (int i = 0; i < ar.Count; ++i) {
3793 PropertyBase pb = (PropertyBase) ar [i];
3794 if (pb.AreAccessorsDuplicateImplementation (this))
3801 for (int i = 0; i < ar.Count; ++i) {
3802 Event ev = (Event) ar [i];
3803 if (ev.AreAccessorsDuplicateImplementation (this))
3814 public override bool Define ()
3816 if (!DoDefineBase ())
3819 MethodBuilder mb = null;
3820 if (GenericMethod != null) {
3821 string mname = MemberName.GetMethodName ();
3822 mb = Parent.TypeBuilder.DefineGenericMethod (mname, flags);
3823 if (!GenericMethod.Define (mb))
3834 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
3836 MethodData = new MethodData (this, ParameterInfo, ModFlags, flags,
3837 this, mb, GenericMethod);
3839 if (!MethodData.Define (Parent))
3843 // Setup iterator if we are one
3845 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3846 Iterator iterator = new Iterator (
3847 Parent, Name, MemberType, ParameterTypes,
3848 ParameterInfo, ModFlags, block, Location);
3850 if (!iterator.DefineIterator ())
3853 block = iterator.Block;
3856 MethodBuilder = MethodData.MethodBuilder;
3859 // This is used to track the Entry Point,
3861 if (Name == "Main" &&
3862 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3863 (RootContext.MainClass == null ||
3864 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3865 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3866 if (RootContext.EntryPoint == null) {
3867 if (Parent.IsGeneric){
3868 Report.Error (-201, Location,
3869 "Entry point can not be defined in a generic class");
3872 RootContext.EntryPoint = MethodBuilder;
3873 RootContext.EntryPointLocation = Location;
3875 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3876 DuplicateEntryPoint (MethodBuilder, Location);
3879 if (RootContext.WarningLevel >= 4)
3880 Report.Warning (28, Location, "'{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder) );
3884 if (MemberType.IsAbstract && MemberType.IsSealed) {
3885 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3895 public override void Emit ()
3897 MethodData.Emit (Parent, this);
3903 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
3905 MethodInfo mi = (MethodInfo) container.ParentContainer.MemberCache.FindMemberToOverride (
3906 container.TypeBuilder, Name, ParameterTypes, false);
3911 parent_ret_type = mi.ReturnType;
3915 protected override bool VerifyClsCompliance(DeclSpace ds)
3917 if (!base.VerifyClsCompliance (ds))
3920 if (parameter_types.Length > 0) {
3921 ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
3923 ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
3930 void IIteratorContainer.SetYields ()
3932 ModFlags |= Modifiers.METHOD_YIELDS;
3935 #region IMethodData Members
3937 public CallingConventions CallingConventions {
3939 CallingConventions cc = Parameters.GetCallingConvention ();
3940 if (Parameters.HasArglist)
3941 block.HasVarargs = true;
3944 if ((ModFlags & Modifiers.STATIC) == 0)
3945 cc |= CallingConventions.HasThis;
3947 // FIXME: How is `ExplicitThis' used in C#?
3953 public Type ReturnType {
3959 public MemberName MethodName {
3965 public new Location Location {
3967 return base.Location;
3971 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
3973 return new EmitContext (
3974 tc, ds, Location, ig, ReturnType, ModFlags, false);
3977 public ObsoleteAttribute GetObsoleteAttribute ()
3979 return GetObsoleteAttribute (ds);
3983 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3985 public bool IsExcluded (EmitContext ec)
3987 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3988 return (caching_flags & Flags.Excluded) != 0;
3990 caching_flags &= ~Flags.Excluded_Undetected;
3992 if (parent_method == null) {
3993 if (OptAttributes == null)
3996 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
4001 foreach (Attribute a in attrs) {
4002 string condition = a.GetConditionalAttributeValue (ds);
4003 if (RootContext.AllDefines.Contains (condition))
4007 caching_flags |= Flags.Excluded;
4011 IMethodData md = TypeManager.GetMethod (parent_method);
4013 if (AttributeTester.IsConditionalMethodExcluded (parent_method)) {
4014 caching_flags |= Flags.Excluded;
4020 if (md.IsExcluded (ec)) {
4021 caching_flags |= Flags.Excluded;
4027 GenericMethod IMethodData.GenericMethod {
4029 return GenericMethod;
4036 public abstract class ConstructorInitializer {
4037 ArrayList argument_list;
4038 protected ConstructorInfo parent_constructor;
4039 Parameters parameters;
4042 public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
4045 this.argument_list = argument_list;
4046 this.parameters = parameters;
4050 public ArrayList Arguments {
4052 return argument_list;
4056 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4058 Expression parent_constructor_group;
4061 ec.CurrentBlock = new ToplevelBlock (Block.Flags.Implicit, parameters, loc);
4063 if (argument_list != null){
4064 foreach (Argument a in argument_list){
4065 if (!a.Resolve (ec, loc))
4069 ec.CurrentBlock = null;
4071 if (this is ConstructorBaseInitializer) {
4072 if (ec.ContainerType.BaseType == null)
4075 t = ec.ContainerType.BaseType;
4076 if (ec.ContainerType.IsValueType) {
4077 Report.Error (522, loc,
4078 "structs cannot call base class constructors");
4082 t = ec.ContainerType;
4084 parent_constructor_group = Expression.MemberLookup (
4085 ec, t, ".ctor", MemberTypes.Constructor,
4086 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4089 if (parent_constructor_group == null){
4090 parent_constructor_group = Expression.MemberLookup (
4091 ec, t, ".ctor", MemberTypes.Constructor,
4092 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4095 if (parent_constructor_group != null)
4097 112, loc, "`{0}.{1}' is inaccessible due to " +
4098 "its protection level", t.FullName, t.Name);
4101 1501, loc, "Can not find a constructor for " +
4102 "this argument list");
4106 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4107 ec, (MethodGroupExpr) parent_constructor_group, argument_list,
4110 if (parent_constructor == null){
4111 Report.Error (1501, loc,
4112 "Can not find a constructor for this argument list");
4116 if (parent_constructor == caller_builder){
4117 Report.Error (516, String.Format ("Constructor `{0}' can not call itself", TypeManager.CSharpSignature (caller_builder)));
4124 public void Emit (EmitContext ec)
4126 if (parent_constructor != null){
4127 ec.Mark (loc, false);
4129 Invocation.EmitCall (ec, true, true, null, parent_constructor, argument_list, loc);
4131 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), parent_constructor, argument_list, loc);
4136 /// Method search for base ctor. (We do not cache it).
4138 Constructor GetOverloadedConstructor (TypeContainer tc)
4140 if (tc.InstanceConstructors == null)
4143 foreach (Constructor c in tc.InstanceConstructors) {
4144 if (Arguments == null) {
4145 if (c.ParameterTypes.Length == 0)
4153 int count = c.ParameterInfo.Count;
4155 c.ParameterInfo.ParameterModifier (count - 1) == Parameter.Modifier.PARAMS) {
4156 for (int i = 0; i < count-1; i++)
4157 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4162 if (c.ParameterTypes.Length != Arguments.Count)
4165 for (int i = 0; i < Arguments.Count; ++i)
4166 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4181 //TODO: implement caching when it will be necessary
4182 public virtual void CheckObsoleteAttribute (TypeContainer tc, Location loc)
4184 Constructor ctor = GetOverloadedConstructor (tc);
4188 ObsoleteAttribute oa = ctor.GetObsoleteAttribute (tc);
4192 AttributeTester.Report_ObsoleteMessage (oa, ctor.GetSignatureForError (), loc);
4196 public class ConstructorBaseInitializer : ConstructorInitializer {
4197 public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
4198 base (argument_list, pars, l)
4202 public override void CheckObsoleteAttribute(TypeContainer tc, Location loc) {
4203 if (parent_constructor == null)
4206 TypeContainer type_ds = TypeManager.LookupTypeContainer (tc.TypeBuilder.BaseType);
4207 if (type_ds == null) {
4208 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (parent_constructor);
4211 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (parent_constructor), loc);
4216 base.CheckObsoleteAttribute (type_ds, loc);
4221 public class ConstructorThisInitializer : ConstructorInitializer {
4222 public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
4223 base (argument_list, pars, l)
4228 public class Constructor : MethodCore, IMethodData {
4229 public ConstructorBuilder ConstructorBuilder;
4230 public ConstructorInitializer Initializer;
4233 // Modifiers allowed for a constructor.
4235 public const int AllowedModifiers =
4237 Modifiers.PROTECTED |
4238 Modifiers.INTERNAL |
4244 bool has_compliant_args = false;
4246 // The spec claims that static is not permitted, but
4247 // my very own code has static constructors.
4249 public Constructor (TypeContainer ds, string name, int mod, Parameters args,
4250 ConstructorInitializer init, Location l)
4251 : base (ds, null, null, mod, AllowedModifiers, false,
4252 new MemberName (name), null, args, l)
4257 public override string GetSignatureForError()
4259 if (ConstructorBuilder == null)
4260 return GetSignatureForError (Parent);
4262 return TypeManager.CSharpSignature (ConstructorBuilder);
4265 public bool HasCompliantArgs {
4267 return has_compliant_args;
4271 public override AttributeTargets AttributeTargets {
4273 return AttributeTargets.Constructor;
4279 // Returns true if this is a default constructor
4281 public bool IsDefault ()
4283 if ((ModFlags & Modifiers.STATIC) != 0)
4284 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4285 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
4288 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4289 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
4290 (Initializer is ConstructorBaseInitializer) &&
4291 (Initializer.Arguments == null);
4294 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4296 ConstructorBuilder.SetCustomAttribute (cb);
4299 protected override bool CheckForDuplications ()
4301 ArrayList ar = Parent.InstanceConstructors;
4303 int arLen = ar.Count;
4305 for (int i = 0; i < arLen; i++) {
4306 Constructor m = (Constructor) ar [i];
4307 if (IsDuplicateImplementation (m))
4314 protected override bool CheckBase ()
4316 // Check whether arguments were correct.
4317 if (!DoDefineParameters ())
4320 // TODO: skip the rest for generated ctor
4321 if ((ModFlags & Modifiers.STATIC) != 0)
4324 if (!CheckForDuplications ())
4327 if (Parent.Kind == Kind.Struct) {
4328 if (ParameterTypes.Length == 0) {
4329 Report.Error (568, Location,
4330 "Structs can not contain explicit parameterless " +
4335 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4336 Report.Error (666, Location, "Protected member in struct declaration");
4341 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4342 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
4349 // Creates the ConstructorBuilder
4351 public override bool Define ()
4353 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4354 MethodAttributes.SpecialName);
4356 if ((ModFlags & Modifiers.STATIC) != 0){
4357 ca |= MethodAttributes.Static | MethodAttributes.Private;
4359 ca |= MethodAttributes.HideBySig;
4361 if ((ModFlags & Modifiers.PUBLIC) != 0)
4362 ca |= MethodAttributes.Public;
4363 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4364 if ((ModFlags & Modifiers.INTERNAL) != 0)
4365 ca |= MethodAttributes.FamORAssem;
4367 ca |= MethodAttributes.Family;
4368 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4369 ca |= MethodAttributes.Assembly;
4370 else if (IsDefault ())
4371 ca |= MethodAttributes.Public;
4373 ca |= MethodAttributes.Private;
4376 // Check if arguments were correct.
4380 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4381 ca, CallingConventions,
4384 if ((ModFlags & Modifiers.UNSAFE) != 0)
4385 ConstructorBuilder.InitLocals = false;
4387 TypeManager.AddMethod (ConstructorBuilder, this);
4390 // HACK because System.Reflection.Emit is lame
4392 TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes);
4400 public override void Emit ()
4402 EmitContext ec = CreateEmitContext (null, null);
4405 // extern methods have no bodies
4407 if ((ModFlags & Modifiers.EXTERN) != 0) {
4408 if ((block != null) && ((ModFlags & Modifiers.EXTERN) != 0)) {
4410 179, Location, "External constructor `" +
4411 TypeManager.CSharpSignature (ConstructorBuilder) +
4412 "' can not have a body");
4415 } else if (block == null) {
4417 501, Location, "Constructor `" +
4418 TypeManager.CSharpSignature (ConstructorBuilder) +
4419 "' must declare a body since it is not marked extern");
4423 if ((ModFlags & Modifiers.STATIC) == 0){
4424 if (Parent.Kind == Kind.Class && Initializer == null)
4425 Initializer = new ConstructorBaseInitializer (
4426 null, Parameters.EmptyReadOnlyParameters, Location);
4430 // Spec mandates that Initializers will not have
4434 if (Initializer != null && !Initializer.Resolve (ConstructorBuilder, ec))
4436 ec.IsStatic = false;
4439 Parameters.LabelParameters (ec, ConstructorBuilder, Location);
4441 SourceMethod source = SourceMethod.Create (
4442 Parent, ConstructorBuilder, block);
4445 // Classes can have base initializers and instance field initializers.
4447 if (Parent.Kind == Kind.Class){
4448 if ((ModFlags & Modifiers.STATIC) == 0){
4451 // If we use a "this (...)" constructor initializer, then
4452 // do not emit field initializers, they are initialized in the other constructor
4454 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4455 Parent.EmitFieldInitializers (ec);
4458 if (Initializer != null) {
4459 Initializer.CheckObsoleteAttribute (Parent, Location);
4460 Initializer.Emit (ec);
4463 if ((ModFlags & Modifiers.STATIC) != 0)
4464 Parent.EmitFieldInitializers (ec);
4466 if (OptAttributes != null)
4467 OptAttributes.Emit (ec, this);
4469 // If this is a non-static `struct' constructor and doesn't have any
4470 // initializer, it must initialize all of the struct's fields.
4471 if ((Parent.Kind == Kind.Struct) &&
4472 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4473 Block.AddThisVariable (Parent, Location);
4475 ec.EmitTopBlock (block, ParameterInfo, Location);
4478 source.CloseMethod ();
4485 // Is never override
4486 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
4491 protected override bool VerifyClsCompliance (DeclSpace ds)
4493 if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
4497 if (parameter_types.Length > 0) {
4498 ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
4500 ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4502 if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4503 foreach (Type param in parameter_types) {
4504 if (param.IsArray) {
4510 has_compliant_args = true;
4514 #region IMethodData Members
4516 public System.Reflection.CallingConventions CallingConventions {
4518 CallingConventions cc = Parameters.GetCallingConvention ();
4520 if (Parent.Kind == Kind.Class)
4521 if ((ModFlags & Modifiers.STATIC) == 0)
4522 cc |= CallingConventions.HasThis;
4524 // FIXME: How is `ExplicitThis' used in C#?
4530 public new Location Location {
4532 return base.Location;
4536 public MemberName MethodName {
4542 public Type ReturnType {
4548 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4550 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4551 return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
4554 public ObsoleteAttribute GetObsoleteAttribute ()
4559 public bool IsExcluded(EmitContext ec)
4564 GenericMethod IMethodData.GenericMethod {
4574 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4576 public interface IMethodData
4578 CallingConventions CallingConventions { get; }
4579 Location Location { get; }
4580 MemberName MethodName { get; }
4581 Type[] ParameterTypes { get; }
4582 Type ReturnType { get; }
4583 GenericMethod GenericMethod { get; }
4585 Attributes OptAttributes { get; }
4586 Block Block { get; }
4588 EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
4589 ObsoleteAttribute GetObsoleteAttribute ();
4590 string GetSignatureForError (TypeContainer tc);
4591 bool IsExcluded (EmitContext ec);
4592 bool IsClsCompliaceRequired (DeclSpace ds);
4596 // Encapsulates most of the Method's state
4598 public class MethodData {
4600 readonly IMethodData method;
4603 // The return type of this method
4605 public readonly GenericMethod GenericMethod;
4606 public readonly InternalParameters ParameterInfo;
4609 // Are we implementing an interface ?
4611 public bool IsImplementing = false;
4616 protected MemberBase member;
4617 protected int modifiers;
4618 protected MethodAttributes flags;
4619 protected Type declaring_type;
4623 MethodBuilder builder = null;
4624 public MethodBuilder MethodBuilder {
4630 public Type DeclaringType {
4632 return declaring_type;
4636 public MethodData (MemberBase member, InternalParameters parameters,
4637 int modifiers, MethodAttributes flags, IMethodData method)
4639 this.member = member;
4640 this.ParameterInfo = parameters;
4641 this.modifiers = modifiers;
4644 this.method = method;
4647 public MethodData (MemberBase member, InternalParameters parameters,
4648 int modifiers, MethodAttributes flags,
4649 IMethodData method, MethodBuilder builder,
4650 GenericMethod generic)
4651 : this (member, parameters, modifiers, flags, method)
4653 this.builder = builder;
4654 this.GenericMethod = generic;
4657 static string RemoveArity (string name)
4660 StringBuilder sb = new StringBuilder ();
4661 while (start < name.Length) {
4662 int pos = name.IndexOf ('`', start);
4664 sb.Append (name.Substring (start));
4668 sb.Append (name.Substring (start, pos-start));
4671 while ((pos < name.Length) && Char.IsNumber (name [pos]))
4677 return sb.ToString ();
4680 public bool Define (TypeContainer container)
4682 MethodInfo implementing = null;
4685 if (member.IsExplicitImpl)
4686 prefix = RemoveArity (member.InterfaceType.FullName) + ".";
4690 string name = method.MethodName.Name;
4691 string method_name = prefix + name;
4693 Type[] ParameterTypes = method.ParameterTypes;
4695 if (container.Pending != null){
4696 if (member is Indexer)
4697 implementing = container.Pending.IsInterfaceIndexer (
4698 member.InterfaceType, method.ReturnType, ParameterTypes);
4700 implementing = container.Pending.IsInterfaceMethod (
4701 member.InterfaceType, name, method.ReturnType, ParameterTypes);
4703 if (member.InterfaceType != null && implementing == null){
4704 Report.Error (539, method.Location, "'{0}' in explicit interface declaration is not an interface", method_name);
4710 // For implicit implementations, make sure we are public, for
4711 // explicit implementations, make sure we are private.
4713 if (implementing != null){
4715 // Setting null inside this block will trigger a more
4716 // verbose error reporting for missing interface implementations
4718 // The "candidate" function has been flagged already
4719 // but it wont get cleared
4721 if (member.IsExplicitImpl){
4722 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4723 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4724 implementing = null;
4726 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4727 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4729 // If this is an interface method implementation,
4730 // check for public accessibility
4732 implementing = null;
4733 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4734 // We may never be private.
4735 implementing = null;
4736 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4738 // We may be protected if we're overriding something.
4740 implementing = null;
4745 // Static is not allowed
4747 if ((modifiers & Modifiers.STATIC) != 0){
4748 implementing = null;
4749 Modifiers.Error_InvalidModifier (method.Location, "static");
4754 // If implementing is still valid, set flags
4756 if (implementing != null){
4758 // When implementing interface methods, set NewSlot
4759 // unless, we are overwriting a method.
4761 if (implementing.DeclaringType.IsInterface){
4762 if ((modifiers & Modifiers.OVERRIDE) == 0)
4763 flags |= MethodAttributes.NewSlot;
4766 MethodAttributes.Virtual |
4767 MethodAttributes.HideBySig;
4769 // Set Final unless we're virtual, abstract or already overriding a method.
4770 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4771 flags |= MethodAttributes.Final;
4773 IsImplementing = true;
4776 EmitContext ec = method.CreateEmitContext (container, null);
4778 DefineMethodBuilder (ec, container, method_name, ParameterTypes);
4780 if (builder == null)
4783 if (container.CurrentType != null)
4784 declaring_type = container.CurrentType.ResolveType (ec);
4786 declaring_type = container.TypeBuilder;
4788 if ((modifiers & Modifiers.UNSAFE) != 0)
4789 builder.InitLocals = false;
4791 if (IsImplementing){
4793 // clear the pending implemntation flag
4795 if (member is Indexer) {
4796 container.Pending.ImplementIndexer (
4797 member.InterfaceType, builder, method.ReturnType,
4798 ParameterTypes, member.IsExplicitImpl);
4800 container.Pending.ImplementMethod (
4801 member.InterfaceType, name, method.ReturnType,
4802 ParameterTypes, member.IsExplicitImpl);
4804 if (member.IsExplicitImpl)
4805 container.TypeBuilder.DefineMethodOverride (
4806 builder, implementing);
4810 TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes);
4811 TypeManager.AddMethod (builder, method);
4813 if (GenericMethod != null) {
4814 bool is_override = member.IsExplicitImpl |
4815 ((modifiers & Modifiers.OVERRIDE) != 0);
4817 is_override &= IsImplementing;
4819 if (!GenericMethod.DefineType (
4820 ec, builder, implementing, is_override))
4828 /// Create the MethodBuilder for the method
4830 void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
4832 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4834 if ((modifiers & extern_static) == extern_static) {
4836 if (method.OptAttributes != null) {
4837 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
4838 if (dllimport_attribute != null) {
4839 flags |= MethodAttributes.PinvokeImpl;
4840 builder = dllimport_attribute.DefinePInvokeMethod (
4841 ec, container.TypeBuilder, method_name, flags,
4842 method.ReturnType, ParameterTypes);
4848 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4849 // We are more strict than Microsoft and report CS0626 like error
4850 if (method.OptAttributes == null ||
4851 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
4852 Report.Error (626, method.Location, "Method, operator, or accessor '{0}' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation", method.GetSignatureForError (container));
4857 if (builder == null)
4858 builder = container.TypeBuilder.DefineMethod (
4859 method_name, flags, method.CallingConventions,
4860 method.ReturnType, ParameterTypes);
4862 builder.SetGenericMethodSignature (
4863 flags, method.CallingConventions,
4864 method.ReturnType, ParameterTypes);
4870 public void Emit (TypeContainer container, Attributable kind)
4873 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4874 ec = method.CreateEmitContext (container, builder.GetILGenerator ());
4876 ec = method.CreateEmitContext (container, null);
4878 Location loc = method.Location;
4879 Attributes OptAttributes = method.OptAttributes;
4881 if (OptAttributes != null)
4882 OptAttributes.Emit (ec, kind);
4884 if (member is MethodCore)
4885 ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
4887 Block block = method.Block;
4890 // abstract or extern methods have no bodies
4892 if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
4897 // abstract or extern methods have no bodies.
4899 if ((modifiers & Modifiers.ABSTRACT) != 0)
4901 500, method.Location, "Abstract method `" +
4902 TypeManager.CSharpSignature (builder) +
4903 "' can not have a body");
4905 if ((modifiers & Modifiers.EXTERN) != 0)
4907 179, method.Location, "External method `" +
4908 TypeManager.CSharpSignature (builder) +
4909 "' can not have a body");
4915 // Methods must have a body unless they're extern or abstract
4917 if (block == null) {
4919 501, method.Location, "Method `" +
4920 TypeManager.CSharpSignature (builder) +
4921 "' must declare a body since it is not marked " +
4922 "abstract or extern");
4926 SourceMethod source = SourceMethod.Create (
4927 container, MethodBuilder, method.Block);
4930 // Handle destructors specially
4932 // FIXME: This code generates buggy code
4934 if (member is Destructor)
4935 EmitDestructor (ec, block);
4937 ec.EmitTopBlock (block, ParameterInfo, loc);
4940 source.CloseMethod ();
4943 void EmitDestructor (EmitContext ec, Block block)
4945 ILGenerator ig = ec.ig;
4947 Label finish = ig.DefineLabel ();
4949 block.SetDestructor ();
4951 ig.BeginExceptionBlock ();
4952 ec.ReturnLabel = finish;
4953 ec.HasReturnLabel = true;
4954 ec.EmitTopBlock (block, null, method.Location);
4956 // ig.MarkLabel (finish);
4957 ig.BeginFinallyBlock ();
4959 if (ec.ContainerType.BaseType != null) {
4960 Expression member_lookup = Expression.MemberLookup (
4961 ec, ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4962 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4964 if (member_lookup != null){
4965 MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
4967 ig.Emit (OpCodes.Ldarg_0);
4968 ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
4972 ig.EndExceptionBlock ();
4973 //ig.MarkLabel (ec.ReturnLabel);
4974 ig.Emit (OpCodes.Ret);
4978 public class Destructor : Method {
4980 public Destructor (TypeContainer ds, Expression return_type, int mod, string name,
4981 Parameters parameters, Attributes attrs, Location l)
4982 : base (ds, null, return_type, mod, false, new MemberName (name),
4983 parameters, attrs, l)
4986 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4988 if (a.Type == TypeManager.conditional_attribute_type) {
4989 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
4993 base.ApplyAttributeBuilder (a, cb);
4997 abstract public class MemberBase : MemberCore {
4998 public Expression Type;
5000 public MethodAttributes flags;
5002 protected readonly int explicit_mod_flags;
5005 // The "short" name of this property / indexer / event. This is the
5006 // name without the explicit interface.
5008 public string ShortName;
5011 // The type of this property / indexer / event
5013 public Type MemberType;
5016 // If true, this is an explicit interface implementation
5018 public bool IsExplicitImpl = false;
5021 // The name of the interface we are explicitly implementing
5023 public MemberName ExplicitInterfaceName = null;
5026 // Whether this is an interface member.
5028 public bool IsInterface;
5031 // If true, the interface type we are explicitly implementing
5033 public Type InterfaceType = null;
5036 // The constructor is only exposed to our children
5038 protected MemberBase (TypeContainer parent, Expression type, int mod,
5039 int allowed_mod, int def_mod, MemberName name,
5040 Attributes attrs, Location loc)
5041 : base (parent, name, attrs, loc)
5043 explicit_mod_flags = mod;
5045 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
5047 // Check for explicit interface implementation
5048 if (MemberName.Left != null) {
5049 ExplicitInterfaceName = MemberName.Left;
5050 ShortName = MemberName.Name;
5051 IsExplicitImpl = true;
5056 protected virtual bool CheckBase ()
5058 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
5059 Report.Error (666, Location, "Protected member in struct declaration");
5063 if ((RootContext.WarningLevel >= 4) &&
5064 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
5065 ((ModFlags & Modifiers.PROTECTED) != 0) &&
5066 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
5067 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
5073 protected abstract bool CheckGenericOverride (MethodInfo method, string name);
5075 protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
5079 foreach (Type partype in parameters){
5080 if (partype == TypeManager.void_type) {
5082 1547, Location, "Keyword 'void' cannot " +
5083 "be used in this context");
5087 if (partype.IsPointer){
5090 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
5094 if (ds.AsAccessible (partype, ModFlags))
5097 if (this is Indexer)
5098 Report.Error (55, Location,
5099 "Inconsistent accessibility: parameter type `" +
5100 TypeManager.CSharpName (partype) + "' is less " +
5101 "accessible than indexer `" + Name + "'");
5102 else if ((this is Method) && ((Method) this).IsOperator)
5103 Report.Error (57, Location,
5104 "Inconsistent accessibility: parameter type `" +
5105 TypeManager.CSharpName (partype) + "' is less " +
5106 "accessible than operator `" + Name + "'");
5108 Report.Error (51, Location,
5109 "Inconsistent accessibility: parameter type `" +
5110 TypeManager.CSharpName (partype) + "' is less " +
5111 "accessible than method `" + Name + "'");
5118 protected virtual bool DoDefineBase ()
5121 throw new InternalErrorException ();
5124 ModFlags = Modifiers.PUBLIC |
5125 Modifiers.ABSTRACT |
5126 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
5128 flags = MethodAttributes.Public |
5129 MethodAttributes.Abstract |
5130 MethodAttributes.HideBySig |
5131 MethodAttributes.NewSlot |
5132 MethodAttributes.Virtual;
5134 if (!Parent.MethodModifiersValid (ModFlags, Name, Location))
5137 flags = Modifiers.MethodAttr (ModFlags);
5143 protected virtual bool DoDefine (DeclSpace decl)
5145 // Lookup Type, verify validity
5146 MemberType = decl.ResolveType (Type, false, Location);
5147 if (MemberType == null)
5150 if ((Parent.ModFlags & Modifiers.SEALED) != 0){
5151 if ((ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0){
5152 Report.Error (549, Location, "Virtual method can not be contained in sealed class");
5157 // verify accessibility
5158 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5159 if (this is Property)
5160 Report.Error (53, Location,
5161 "Inconsistent accessibility: property type `" +
5162 TypeManager.CSharpName (MemberType) + "' is less " +
5163 "accessible than property `" + Name + "'");
5164 else if (this is Indexer)
5165 Report.Error (54, Location,
5166 "Inconsistent accessibility: indexer return type `" +
5167 TypeManager.CSharpName (MemberType) + "' is less " +
5168 "accessible than indexer `" + Name + "'");
5169 else if (this is Method) {
5170 if (((Method) this).IsOperator)
5171 Report.Error (56, Location,
5172 "Inconsistent accessibility: return type `" +
5173 TypeManager.CSharpName (MemberType) + "' is less " +
5174 "accessible than operator `" + Name + "'");
5176 Report.Error (50, Location,
5177 "Inconsistent accessibility: return type `" +
5178 TypeManager.CSharpName (MemberType) + "' is less " +
5179 "accessible than method `" + Name + "'");
5181 Report.Error (52, Location,
5182 "Inconsistent accessibility: field type `" +
5183 TypeManager.CSharpName (MemberType) + "' is less " +
5184 "accessible than field `" + Name + "'");
5188 if (MemberType.IsPointer && !UnsafeOK (Parent))
5191 if (IsExplicitImpl) {
5192 InterfaceType = Parent.ResolveType (
5193 ExplicitInterfaceName.GetTypeExpression (Location), false, Location);
5194 if (InterfaceType == null)
5197 if (InterfaceType.IsClass) {
5198 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", ExplicitInterfaceName);
5202 if (!Parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
5205 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5212 /// The name of the member can be changed during definition (see IndexerName attribute)
5214 protected virtual void UpdateMemberName ()
5216 MemberName.Name = ShortName;
5219 public override string GetSignatureForError (TypeContainer tc)
5221 return String.Concat (tc.Name, '.', base.GetSignatureForError (tc));
5224 protected override bool VerifyClsCompliance(DeclSpace ds)
5226 if (base.VerifyClsCompliance (ds)) {
5230 if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
5231 Report.Error (3010, Location, "'{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
5236 protected override void VerifyObsoleteAttribute()
5238 CheckUsageOfObsoleteAttribute (MemberType);
5243 // Fields and Events both generate FieldBuilders, we use this to share
5244 // their common bits. This is also used to flag usage of the field
5246 abstract public class FieldBase : MemberBase {
5247 public FieldBuilder FieldBuilder;
5248 public Status status;
5251 public enum Status : byte { ASSIGNED = 1, USED = 2 }
5253 static string[] attribute_targets = new string [] { "field" };
5256 /// Symbol with same name in parent class/struct
5258 public MemberInfo conflict_symbol;
5261 // The constructor is only exposed to our children
5263 protected FieldBase (TypeContainer parent, Expression type, int mod,
5264 int allowed_mod, MemberName name, object init,
5265 Attributes attrs, Location loc)
5266 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
5272 public override AttributeTargets AttributeTargets {
5274 return AttributeTargets.Field;
5278 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5280 if (a.Type == TypeManager.marshal_as_attr_type) {
5281 UnmanagedMarshal marshal = a.GetMarshal ();
5282 if (marshal != null) {
5283 FieldBuilder.SetMarshal (marshal);
5286 Report.Warning (-24, a.Location, "The Microsoft Runtime cannot set this marshal info. Please use the Mono runtime instead.");
5291 FieldBuilder.SetCustomAttribute (cb);
5295 // Whether this field has an initializer.
5297 public bool HasInitializer {
5299 return init != null;
5303 protected readonly Object init;
5305 Expression init_expr;
5306 bool init_expr_initialized = false;
5308 protected override bool CheckGenericOverride (MethodInfo method, string name)
5314 // Resolves and returns the field initializer.
5316 public Expression GetInitializerExpression (EmitContext ec)
5318 if (init_expr_initialized)
5322 if (init is Expression)
5323 e = (Expression) init;
5325 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
5327 ec.IsFieldInitializer = true;
5328 e = e.DoResolve (ec);
5329 ec.IsFieldInitializer = false;
5332 init_expr_initialized = true;
5337 protected override bool CheckBase ()
5339 if (!base.CheckBase ())
5346 conflict_symbol = Parent.FindMemberWithSameName (Name, false);
5347 if (conflict_symbol == null) {
5348 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5349 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
5354 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5355 Report.SymbolRelatedToPreviousError (conflict_symbol);
5356 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
5362 protected override bool DoDefine (DeclSpace ds)
5364 if (!base.DoDefine (ds))
5367 if (MemberType == TypeManager.void_type) {
5368 Report.Error (1547, Location,
5369 "Keyword 'void' cannot be used in this context");
5373 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5374 Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
5381 public override string GetSignatureForError ()
5383 if (FieldBuilder == null) {
5384 return base.GetSignatureForError (Parent);
5386 return TypeManager.GetFullNameSignature (FieldBuilder);
5389 public override string[] ValidAttributeTargets {
5391 return attribute_targets;
5395 protected override bool VerifyClsCompliance (DeclSpace ds)
5397 if (!base.VerifyClsCompliance (ds))
5400 if (FieldBuilder == null) {
5404 if (!AttributeTester.IsClsCompliant (FieldBuilder.FieldType)) {
5405 Report.Error (3003, Location, "Type of '{0}' is not CLS-compliant", GetSignatureForError ());
5411 public void SetAssigned ()
5413 status |= Status.ASSIGNED;
5418 // The Field class is used to represents class/struct fields during parsing.
5420 public class Field : FieldBase {
5422 // Modifiers allowed in a class declaration
5424 const int AllowedModifiers =
5427 Modifiers.PROTECTED |
5428 Modifiers.INTERNAL |
5431 Modifiers.VOLATILE |
5435 public Field (TypeContainer parent, Expression type, int mod, string name,
5436 Object expr_or_array_init, Attributes attrs, Location loc)
5437 : base (parent, type, mod, AllowedModifiers, new MemberName (name),
5438 expr_or_array_init, attrs, loc)
5442 public override bool Define ()
5444 MemberType = Parent.ResolveType (Type, false, Location);
5446 if (MemberType == null)
5451 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5452 Report.Error (52, Location,
5453 "Inconsistent accessibility: field type `" +
5454 TypeManager.CSharpName (MemberType) + "' is less " +
5455 "accessible than field `" + Name + "'");
5459 if (MemberType.IsPointer && !UnsafeOK (Parent))
5462 if (RootContext.WarningLevel > 1){
5463 Type ptype = Parent.TypeBuilder.BaseType;
5465 // ptype is only null for System.Object while compiling corlib.
5467 TypeContainer.FindMembers (
5468 ptype, MemberTypes.Method,
5469 BindingFlags.Public |
5470 BindingFlags.Static | BindingFlags.Instance,
5471 System.Type.FilterName, Name);
5475 if ((ModFlags & Modifiers.VOLATILE) != 0){
5476 if (!MemberType.IsClass){
5477 Type vt = MemberType;
5479 if (TypeManager.IsEnumType (vt))
5480 vt = TypeManager.EnumToUnderlying (MemberType);
5482 if (!((vt == TypeManager.bool_type) ||
5483 (vt == TypeManager.sbyte_type) ||
5484 (vt == TypeManager.byte_type) ||
5485 (vt == TypeManager.short_type) ||
5486 (vt == TypeManager.ushort_type) ||
5487 (vt == TypeManager.int32_type) ||
5488 (vt == TypeManager.uint32_type) ||
5489 (vt == TypeManager.char_type) ||
5490 (vt == TypeManager.float_type) ||
5491 (!vt.IsValueType))){
5493 677, Location, Parent.MakeName (Name) +
5494 " A volatile field can not be of type `" +
5495 TypeManager.CSharpName (vt) + "'");
5500 if ((ModFlags & Modifiers.READONLY) != 0){
5503 "A field can not be both volatile and readonly");
5508 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5510 if (Parent.Kind == Kind.Struct &&
5511 ((fa & FieldAttributes.Static) == 0) &&
5512 MemberType == Parent.TypeBuilder &&
5513 !TypeManager.IsBuiltinType (MemberType)){
5514 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5515 "' causes a cycle in the structure layout");
5520 FieldBuilder = Parent.TypeBuilder.DefineField (
5521 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5523 TypeManager.RegisterFieldBase (FieldBuilder, this);
5525 catch (ArgumentException) {
5526 Report.Warning (-24, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5533 public override void Emit ()
5535 if (OptAttributes != null) {
5536 EmitContext ec = new EmitContext (
5537 Parent, Location, null, FieldBuilder.FieldType,
5539 OptAttributes.Emit (ec, this);
5547 // `set' and `get' accessors are represented with an Accessor.
5549 public class Accessor {
5551 // Null if the accessor is empty, or a Block if not
5554 public Attributes Attributes;
5555 public Location Location;
5557 public Accessor (Block b, Attributes attrs, Location loc)
5566 // Ooouh Martin, templates are missing here.
5567 // When it will be possible move here a lot of child code and template method type.
5568 public abstract class AbstractPropertyEventMethod: MemberCore, IMethodData {
5569 protected MethodData method_data;
5570 protected Block block;
5572 // The accessor are created event if they are not wanted.
5573 // But we need them because their names are reserved.
5574 // Field says whether accessor will be emited or not
5575 public readonly bool IsDummy;
5577 protected readonly string prefix;
5579 ReturnParameter return_attributes;
5581 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5582 : base (null, SetupName (prefix, member), null, member.Location)
5584 this.prefix = prefix;
5588 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5590 : base (null, SetupName (prefix, member),
5591 accessor.Attributes, accessor.Location)
5593 this.prefix = prefix;
5594 this.block = accessor.Block;
5597 static MemberName SetupName (string prefix, MemberBase member)
5599 MemberName name = member.MemberName.Clone ();
5600 name.Name = prefix + member.ShortName;
5604 public void UpdateName (MemberBase member)
5606 MemberName.Name = prefix + member.ShortName;
5609 #region IMethodData Members
5611 public Block Block {
5621 public CallingConventions CallingConventions {
5623 return CallingConventions.Standard;
5627 public bool IsExcluded (EmitContext ec)
5632 GenericMethod IMethodData.GenericMethod {
5638 public MemberName MethodName {
5644 public abstract ObsoleteAttribute GetObsoleteAttribute ();
5645 public abstract Type[] ParameterTypes { get; }
5646 public abstract Type ReturnType { get; }
5647 public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
5651 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5653 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5654 a.Type == TypeManager.conditional_attribute_type) {
5655 Report.Error (1667, a.Location, "'{0}' is not valid on property or event accessors. It is valid on '{1}' declarations only", TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5659 if (a.Target == AttributeTargets.Method) {
5660 method_data.MethodBuilder.SetCustomAttribute (cb);
5664 if (a.Target == AttributeTargets.ReturnValue) {
5665 if (return_attributes == null)
5666 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5668 return_attributes.ApplyAttributeBuilder (a, cb);
5672 ApplyToExtraTarget (a, cb);
5675 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5677 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5680 public override bool Define()
5685 public virtual void Emit (TypeContainer container)
5687 method_data.Emit (container, this);
5691 public override bool IsClsCompliaceRequired(DeclSpace ds)
5696 public bool IsDuplicateImplementation (MethodCore method)
5698 if (Name != method.Name)
5701 Type[] param_types = method.ParameterTypes;
5703 if (param_types.Length != ParameterTypes.Length)
5706 for (int i = 0; i < param_types.Length; i++)
5707 if (param_types [i] != ParameterTypes [i])
5710 Report.SymbolRelatedToPreviousError (method);
5711 Report.Error (111, Location, "Type '{0}' already defines a member called '{1}' with " +
5712 "the same parameter types", Parent.Name, Name);
5716 public new Location Location {
5718 return base.Location;
5722 protected override void VerifyObsoleteAttribute()
5729 // Properties and Indexers both generate PropertyBuilders, we use this to share
5730 // their common bits.
5732 abstract public class PropertyBase : MethodCore {
5734 public class GetMethod: PropertyMethod
5736 static string[] attribute_targets = new string [] { "method", "return" };
5738 public GetMethod (MethodCore method):
5739 base (method, "get_")
5743 public GetMethod (MethodCore method, Accessor accessor):
5744 base (method, accessor, "get_")
5748 public override MethodBuilder Define(TypeContainer container)
5750 method_data = new MethodData (method, method.ParameterInfo, method.ModFlags, method.flags, this);
5752 if (!method_data.Define (container))
5755 return method_data.MethodBuilder;
5758 public override string GetSignatureForError (TypeContainer tc)
5760 return String.Concat (base.GetSignatureForError (tc), ".get");
5763 public override Type ReturnType {
5765 return method.MemberType;
5769 public override string[] ValidAttributeTargets {
5771 return attribute_targets;
5776 public class SetMethod: PropertyMethod {
5778 static string[] attribute_targets = new string [] { "method", "param", "return" };
5779 ImplicitParameter param_attr;
5781 public SetMethod (MethodCore method):
5782 base (method, "set_")
5786 public SetMethod (MethodCore method, Accessor accessor):
5787 base (method, accessor, "set_")
5791 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
5793 if (a.Target == AttributeTargets.Parameter) {
5794 if (param_attr == null)
5795 param_attr = new ImplicitParameter (method_data.MethodBuilder);
5797 param_attr.ApplyAttributeBuilder (a, cb);
5801 base.ApplyAttributeBuilder (a, cb);
5804 protected virtual InternalParameters GetParameterInfo (TypeContainer container)
5806 Parameter [] parms = new Parameter [1];
5807 parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null);
5808 return new InternalParameters (
5809 container, new Parameters (parms, null, method.Location));
5812 public override MethodBuilder Define(TypeContainer container)
5814 method_data = new MethodData (method, GetParameterInfo (container), method.ModFlags, method.flags, this);
5816 if (!method_data.Define (container))
5819 return method_data.MethodBuilder;
5822 public override string GetSignatureForError (TypeContainer tc)
5824 return String.Concat (base.GetSignatureForError (tc), ".set");
5827 public override Type[] ParameterTypes {
5829 return new Type[] { method.MemberType };
5833 public override Type ReturnType {
5835 return TypeManager.void_type;
5839 public override string[] ValidAttributeTargets {
5841 return attribute_targets;
5846 static string[] attribute_targets = new string [] { "property" };
5848 public abstract class PropertyMethod: AbstractPropertyEventMethod {
5849 protected readonly MethodCore method;
5851 public PropertyMethod (MethodCore method, string prefix)
5852 : base (method, prefix)
5854 this.method = method;
5857 public PropertyMethod (MethodCore method, Accessor accessor, string prefix)
5858 : base (method, accessor, prefix)
5860 this.method = method;
5863 public override AttributeTargets AttributeTargets {
5865 return AttributeTargets.Method;
5869 public override bool IsClsCompliaceRequired(DeclSpace ds)
5871 return method.IsClsCompliaceRequired (ds);
5874 public InternalParameters ParameterInfo
5877 return method_data.ParameterInfo;
5881 public abstract MethodBuilder Define (TypeContainer container);
5883 public override Type[] ParameterTypes {
5885 return TypeManager.NoTypes;
5889 public override EmitContext CreateEmitContext (TypeContainer tc,
5892 return new EmitContext (
5893 tc, method.ds, method.Location, ig, ReturnType,
5894 method.ModFlags, false);
5897 public override ObsoleteAttribute GetObsoleteAttribute ()
5899 return method.GetObsoleteAttribute (method.ds);
5902 public override string GetSignatureForError (TypeContainer tc)
5904 return String.Concat (tc.Name, '.', method.Name);
5908 public PropertyMethod Get, Set;
5909 public PropertyBuilder PropertyBuilder;
5910 public MethodBuilder GetBuilder, SetBuilder;
5912 protected EmitContext ec;
5914 public PropertyBase (TypeContainer parent, Expression type, int mod_flags,
5915 int allowed_mod, bool is_iface, MemberName name,
5916 Parameters parameters, Attributes attrs,
5918 : base (parent, null, type, mod_flags, allowed_mod, is_iface, name,
5919 attrs, parameters, loc)
5923 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5925 PropertyBuilder.SetCustomAttribute (cb);
5928 public override AttributeTargets AttributeTargets {
5930 return AttributeTargets.Property;
5934 protected override bool DoDefine (DeclSpace decl)
5936 if (!base.DoDefine (decl))
5939 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5940 Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
5944 if (MemberType.IsAbstract && MemberType.IsSealed) {
5945 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
5949 ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
5953 public override string GetSignatureForError()
5955 if (PropertyBuilder == null)
5956 return GetSignatureForError (Parent);
5958 return TypeManager.CSharpSignature (PropertyBuilder, false);
5962 protected override bool CheckForDuplications ()
5964 ArrayList ar = Parent.Indexers;
5966 int arLen = ar.Count;
5968 for (int i = 0; i < arLen; i++) {
5969 Indexer m = (Indexer) ar [i];
5970 if (IsDuplicateImplementation (m))
5975 ar = Parent.Properties;
5977 int arLen = ar.Count;
5979 for (int i = 0; i < arLen; i++) {
5980 Property m = (Property) ar [i];
5981 if (IsDuplicateImplementation (m))
5989 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
5991 PropertyInfo parent_property = container.ParentContainer.MemberCache.FindMemberToOverride (
5992 container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
5994 if (parent_property == null)
5997 parent_ret_type = parent_property.PropertyType;
6000 temp_m = parent_property.GetGetMethod (true);
6004 System.Diagnostics.Debug.Assert (parent_property.GetSetMethod (true) != null, "Internal error property without get/set");
6005 return parent_property.GetSetMethod (true);
6008 public override void Emit ()
6011 // The PropertyBuilder can be null for explicit implementations, in that
6012 // case, we do not actually emit the ".property", so there is nowhere to
6013 // put the attribute
6015 if (PropertyBuilder != null && OptAttributes != null)
6016 OptAttributes.Emit (ec, this);
6028 /// Tests whether accessors are not in collision with some method (CS0111)
6030 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6032 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6035 protected override void UpdateMemberName ()
6037 base.UpdateMemberName ();
6039 Get.UpdateName (this);
6040 Set.UpdateName (this);
6044 public override string[] ValidAttributeTargets {
6046 return attribute_targets;
6051 public class Property : PropertyBase, IIteratorContainer {
6052 const int AllowedModifiers =
6055 Modifiers.PROTECTED |
6056 Modifiers.INTERNAL |
6060 Modifiers.OVERRIDE |
6061 Modifiers.ABSTRACT |
6064 Modifiers.METHOD_YIELDS |
6067 const int AllowedInterfaceModifiers =
6070 public Property (TypeContainer parent, Expression type, int mod_flags,
6071 bool is_iface, MemberName name, Attributes attrs,
6072 Accessor get_block, Accessor set_block, Location loc)
6073 : base (parent, type, mod_flags,
6074 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6075 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs,
6078 if (get_block == null)
6079 Get = new GetMethod (this);
6081 Get = new GetMethod (this, get_block);
6083 if (set_block == null)
6084 Set = new SetMethod (this);
6086 Set = new SetMethod (this, set_block);
6089 public override bool Define ()
6091 if (!DoDefineBase ())
6094 if (!DoDefine (Parent))
6100 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6104 GetBuilder = Get.Define (Parent);
6105 if (GetBuilder == null)
6109 // Setup iterator if we are one
6111 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
6112 Iterator iterator = new Iterator (
6113 Parent, "get", MemberType,
6114 TypeManager.NoTypes, Get.ParameterInfo,
6115 ModFlags, Get.Block, Location);
6117 if (!iterator.DefineIterator ())
6119 Get.Block = iterator.Block;
6124 SetBuilder = Set.Define (Parent);
6125 if (SetBuilder == null)
6128 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
6131 // FIXME - PropertyAttributes.HasDefault ?
6133 PropertyAttributes prop_attr = PropertyAttributes.None;
6135 prop_attr |= PropertyAttributes.RTSpecialName |
6136 PropertyAttributes.SpecialName;
6138 if (!IsExplicitImpl){
6139 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6140 Name, prop_attr, MemberType, null);
6143 PropertyBuilder.SetGetMethod (GetBuilder);
6146 PropertyBuilder.SetSetMethod (SetBuilder);
6148 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
6153 public void SetYields ()
6155 ModFlags |= Modifiers.METHOD_YIELDS;
6160 /// Gigantic workaround for lameness in SRE follows :
6161 /// This class derives from EventInfo and attempts to basically
6162 /// wrap around the EventBuilder so that FindMembers can quickly
6163 /// return this in it search for members
6165 public class MyEventBuilder : EventInfo {
6168 // We use this to "point" to our Builder which is
6169 // not really a MemberInfo
6171 EventBuilder MyBuilder;
6174 // We "catch" and wrap these methods
6176 MethodInfo raise, remove, add;
6178 EventAttributes attributes;
6179 Type declaring_type, reflected_type, event_type;
6184 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6186 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6188 // And now store the values in our own fields.
6190 declaring_type = type_builder;
6192 reflected_type = type_builder;
6194 attributes = event_attr;
6197 this.event_type = event_type;
6201 // Methods that you have to override. Note that you only need
6202 // to "implement" the variants that take the argument (those are
6203 // the "abstract" methods, the others (GetAddMethod()) are
6206 public override MethodInfo GetAddMethod (bool nonPublic)
6211 public override MethodInfo GetRemoveMethod (bool nonPublic)
6216 public override MethodInfo GetRaiseMethod (bool nonPublic)
6222 // These methods make "MyEventInfo" look like a Builder
6224 public void SetRaiseMethod (MethodBuilder raiseMethod)
6226 raise = raiseMethod;
6227 MyBuilder.SetRaiseMethod (raiseMethod);
6230 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6232 remove = removeMethod;
6233 MyBuilder.SetRemoveOnMethod (removeMethod);
6236 public void SetAddOnMethod (MethodBuilder addMethod)
6239 MyBuilder.SetAddOnMethod (addMethod);
6242 public void SetCustomAttribute (CustomAttributeBuilder cb)
6244 MyBuilder.SetCustomAttribute (cb);
6247 public override object [] GetCustomAttributes (bool inherit)
6249 // FIXME : There's nothing which can be seemingly done here because
6250 // we have no way of getting at the custom attribute objects of the
6255 public override object [] GetCustomAttributes (Type t, bool inherit)
6257 // FIXME : Same here !
6261 public override bool IsDefined (Type t, bool b)
6266 public override EventAttributes Attributes {
6272 public override string Name {
6278 public override Type DeclaringType {
6280 return declaring_type;
6284 public override Type ReflectedType {
6286 return reflected_type;
6290 public Type EventType {
6296 public void SetUsed ()
6298 if (my_event != null)
6299 my_event.status = (FieldBase.Status.ASSIGNED | FieldBase.Status.USED);
6304 /// For case when event is declared like property (with add and remove accessors).
6306 public class EventProperty: Event {
6308 static string[] attribute_targets = new string [] { "event", "property" };
6310 public EventProperty (TypeContainer parent, Expression type, int mod_flags,
6311 bool is_iface, MemberName name, Object init,
6312 Attributes attrs, Accessor add, Accessor remove,
6314 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6316 Add = new AddDelegateMethod (this, add);
6317 Remove = new RemoveDelegateMethod (this, remove);
6320 public override string[] ValidAttributeTargets {
6322 return attribute_targets;
6328 /// Event is declared like field.
6330 public class EventField: Event {
6332 static string[] attribute_targets = new string [] { "event", "field", "method" };
6334 public EventField (TypeContainer parent, Expression type, int mod_flags,
6335 bool is_iface, MemberName name, Object init,
6336 Attributes attrs, Location loc)
6337 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6339 Add = new AddDelegateMethod (this);
6340 Remove = new RemoveDelegateMethod (this);
6343 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6345 if (a.Target == AttributeTargets.Field) {
6346 FieldBuilder.SetCustomAttribute (cb);
6350 if (a.Target == AttributeTargets.Method) {
6351 AddBuilder.SetCustomAttribute (cb);
6352 RemoveBuilder.SetCustomAttribute (cb);
6356 base.ApplyAttributeBuilder (a, cb);
6359 public override string[] ValidAttributeTargets {
6361 return attribute_targets;
6366 public abstract class Event : FieldBase {
6368 protected sealed class AddDelegateMethod: DelegateMethod
6371 public AddDelegateMethod (Event method):
6372 base (method, "add_")
6376 public AddDelegateMethod (Event method, Accessor accessor):
6377 base (method, accessor, "add_")
6381 protected override MethodInfo DelegateMethodInfo {
6383 return TypeManager.delegate_combine_delegate_delegate;
6389 protected sealed class RemoveDelegateMethod: DelegateMethod
6391 public RemoveDelegateMethod (Event method):
6392 base (method, "remove_")
6396 public RemoveDelegateMethod (Event method, Accessor accessor):
6397 base (method, accessor, "remove_")
6401 protected override MethodInfo DelegateMethodInfo {
6403 return TypeManager.delegate_remove_delegate_delegate;
6409 public abstract class DelegateMethod: AbstractPropertyEventMethod
6411 protected readonly Event method;
6412 ImplicitParameter param_attr;
6414 static string[] attribute_targets = new string [] { "method", "param", "return" };
6416 public DelegateMethod (Event method, string prefix)
6417 : base (method, prefix)
6419 this.method = method;
6422 public DelegateMethod (Event method, Accessor accessor, string prefix)
6423 : base (method, accessor, prefix)
6425 this.method = method;
6428 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6430 if (a.Target == AttributeTargets.Parameter) {
6431 if (param_attr == null)
6432 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6434 param_attr.ApplyAttributeBuilder (a, cb);
6438 base.ApplyAttributeBuilder (a, cb);
6441 public override AttributeTargets AttributeTargets {
6443 return AttributeTargets.Method;
6447 public override bool IsClsCompliaceRequired(DeclSpace ds)
6449 return method.IsClsCompliaceRequired (ds);
6452 public MethodBuilder Define (TypeContainer container, InternalParameters ip)
6454 method_data = new MethodData (method, ip, method.ModFlags,
6455 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6457 if (!method_data.Define (container))
6460 MethodBuilder mb = method_data.MethodBuilder;
6461 mb.DefineParameter (1, ParameterAttributes.None, "value");
6466 public override void Emit (TypeContainer tc)
6468 if (block != null) {
6473 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6474 EmitContext ec = CreateEmitContext (tc, ig);
6475 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6477 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6478 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6479 ig.Emit (OpCodes.Ldsfld, field_info);
6480 ig.Emit (OpCodes.Ldarg_0);
6481 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6482 ig.Emit (OpCodes.Castclass, method.MemberType);
6483 ig.Emit (OpCodes.Stsfld, field_info);
6485 ig.Emit (OpCodes.Ldarg_0);
6486 ig.Emit (OpCodes.Ldarg_0);
6487 ig.Emit (OpCodes.Ldfld, field_info);
6488 ig.Emit (OpCodes.Ldarg_1);
6489 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6490 ig.Emit (OpCodes.Castclass, method.MemberType);
6491 ig.Emit (OpCodes.Stfld, field_info);
6493 ig.Emit (OpCodes.Ret);
6496 protected abstract MethodInfo DelegateMethodInfo { get; }
6498 public override Type[] ParameterTypes {
6500 return new Type[] { method.MemberType };
6504 public override Type ReturnType {
6506 return TypeManager.void_type;
6510 public override EmitContext CreateEmitContext (TypeContainer tc,
6513 return new EmitContext (
6514 tc, method.Parent, Location, ig, ReturnType,
6515 method.ModFlags, false);
6518 public override string GetSignatureForError (TypeContainer tc)
6520 return String.Concat (tc.Name, '.', method.Name);
6523 public override ObsoleteAttribute GetObsoleteAttribute ()
6525 return method.GetObsoleteAttribute (method.Parent);
6528 public override string[] ValidAttributeTargets {
6530 return attribute_targets;
6536 const int AllowedModifiers =
6539 Modifiers.PROTECTED |
6540 Modifiers.INTERNAL |
6545 Modifiers.OVERRIDE |
6549 const int AllowedInterfaceModifiers =
6552 public DelegateMethod Add, Remove;
6553 public MyEventBuilder EventBuilder;
6554 public MethodBuilder AddBuilder, RemoveBuilder;
6556 MethodData AddData, RemoveData;
6558 public Event (TypeContainer parent, Expression type, int mod_flags,
6559 bool is_iface, MemberName name, Object init, Attributes attrs,
6561 : base (parent, type, mod_flags,
6562 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6563 name, init, attrs, loc)
6565 IsInterface = is_iface;
6568 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6570 EventBuilder.SetCustomAttribute (cb);
6573 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6575 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6578 public override AttributeTargets AttributeTargets {
6580 return AttributeTargets.Event;
6584 public override bool Define ()
6586 EventAttributes e_attr;
6587 e_attr = EventAttributes.None;
6589 if (!DoDefineBase ())
6592 if (!DoDefine (Parent))
6595 if (init != null && ((ModFlags & Modifiers.ABSTRACT) != 0)){
6596 Report.Error (74, Location, "'" + Parent.Name + "." + Name +
6597 "': abstract event can not have an initializer");
6601 if (!TypeManager.IsDelegateType (MemberType)) {
6602 Report.Error (66, Location, "'" + Parent.Name + "." + Name +
6603 "' : event must be of a delegate type");
6607 Parameter [] parms = new Parameter [1];
6608 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
6609 InternalParameters ip = new InternalParameters (
6610 Parent, new Parameters (parms, null, Location));
6616 // Now define the accessors
6619 AddBuilder = Add.Define (Parent, ip);
6620 if (AddBuilder == null)
6623 RemoveBuilder = Remove.Define (Parent, ip);
6624 if (RemoveBuilder == null)
6627 if (!IsExplicitImpl){
6628 EventBuilder = new MyEventBuilder (this,
6629 Parent.TypeBuilder, Name, e_attr, MemberType);
6631 if (Add.Block == null && Remove.Block == null &&
6633 FieldBuilder = Parent.TypeBuilder.DefineField (
6635 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
6636 TypeManager.RegisterPrivateFieldOfEvent (
6637 (EventInfo) EventBuilder, FieldBuilder);
6638 TypeManager.RegisterFieldBase (FieldBuilder, this);
6641 EventBuilder.SetAddOnMethod (AddBuilder);
6642 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
6644 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
6650 protected override bool CheckBase ()
6652 if (!base.CheckBase ())
6655 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
6656 if (!(conflict_symbol is EventInfo)) {
6657 Report.SymbolRelatedToPreviousError (conflict_symbol);
6658 Report.Error (72, Location, "Event '{0}' can override only event", GetSignatureForError (Parent));
6666 public override void Emit ()
6668 if (OptAttributes != null) {
6669 EmitContext ec = new EmitContext (
6670 Parent, Location, null, MemberType, ModFlags);
6671 OptAttributes.Emit (ec, this);
6676 Remove.Emit (Parent);
6682 public override string GetSignatureForError ()
6684 if (EventBuilder == null)
6685 return base.GetSignatureForError (Parent);
6687 return TypeManager.GetFullNameSignature (EventBuilder);
6692 public class Indexer : PropertyBase {
6694 class GetIndexerMethod: GetMethod
6696 public GetIndexerMethod (MethodCore method):
6701 public GetIndexerMethod (MethodCore method, Accessor accessor):
6702 base (method, accessor)
6706 public override Type[] ParameterTypes {
6708 return method.ParameterTypes;
6713 class SetIndexerMethod: SetMethod
6715 readonly Parameters parameters;
6717 public SetIndexerMethod (MethodCore method):
6722 public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
6723 base (method, accessor)
6725 this.parameters = parameters;
6728 public override Type[] ParameterTypes {
6730 int top = method.ParameterTypes.Length;
6731 Type [] set_pars = new Type [top + 1];
6732 method.ParameterTypes.CopyTo (set_pars, 0);
6733 set_pars [top] = method.MemberType;
6738 protected override InternalParameters GetParameterInfo (TypeContainer container)
6740 Parameter [] fixed_parms = parameters.FixedParameters;
6742 if (fixed_parms == null){
6743 throw new Exception ("We currently do not support only array arguments in an indexer at: " + method.Location);
6744 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
6745 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
6747 // Here is the problem: the `value' parameter has
6748 // to come *after* the array parameter in the declaration
6750 // X (object [] x, Type value)
6753 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
6754 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
6758 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
6760 fixed_parms.CopyTo (tmp, 0);
6761 tmp [fixed_parms.Length] = new Parameter (
6762 method.Type, "value", Parameter.Modifier.NONE, null);
6764 Parameters set_formal_params = new Parameters (tmp, null, method.Location);
6766 return new InternalParameters (container, set_formal_params);
6772 const int AllowedModifiers =
6775 Modifiers.PROTECTED |
6776 Modifiers.INTERNAL |
6780 Modifiers.OVERRIDE |
6785 const int AllowedInterfaceModifiers =
6789 // Are we implementing an interface ?
6791 public Indexer (TypeContainer parent, Expression type, MemberName name, int mod,
6792 bool is_iface, Parameters parameters, Attributes attrs,
6793 Accessor get_block, Accessor set_block, Location loc)
6794 : base (parent, type, mod,
6795 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6796 is_iface, name, parameters, attrs, loc)
6798 if (get_block == null)
6799 Get = new GetIndexerMethod (this);
6801 Get = new GetIndexerMethod (this, get_block);
6803 if (set_block == null)
6804 Set = new SetIndexerMethod (this);
6806 Set = new SetIndexerMethod (this, parameters, set_block);
6809 public override bool Define ()
6811 PropertyAttributes prop_attr =
6812 PropertyAttributes.RTSpecialName |
6813 PropertyAttributes.SpecialName;
6815 if (!DoDefineBase ())
6818 if (!DoDefine (Parent))
6821 if (OptAttributes != null) {
6822 Attribute indexer_attr = OptAttributes.GetIndexerNameAttribute (ec);
6823 if (indexer_attr != null) {
6824 ShortName = indexer_attr.GetIndexerAttributeValue (ec);
6826 if (IsExplicitImpl) {
6827 Report.Error (415, indexer_attr.Location, "The 'IndexerName' attribute is valid only on an indexer that is not an explicit interface member declaration");
6831 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6832 Report.Error (609, indexer_attr.Location, "Cannot set the 'IndexerName' attribute on an indexer marked override");
6836 if (!Tokenizer.IsValidIdentifier (ShortName)) {
6837 Report.Error (633, indexer_attr.Location, "The argument to the 'IndexerName' attribute must be a valid identifier");
6841 UpdateMemberName ();
6845 if (InterfaceType != null) {
6846 string parent_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
6847 if (parent_IndexerName != Name)
6848 ShortName = parent_IndexerName;
6849 UpdateMemberName ();
6852 if (!Parent.AddToMemberContainer (this, true) ||
6853 !Parent.AddToMemberContainer (Get, true) || !Parent.AddToMemberContainer (Set, true))
6859 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6861 GetBuilder = Get.Define (Parent);
6862 if (GetBuilder == null)
6867 SetBuilder = Set.Define (Parent);
6868 if (SetBuilder == null)
6873 // Now name the parameters
6875 Parameter [] p = Parameters.FixedParameters;
6877 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
6878 Report.Error (631, Location, "ref and out are not valid in this context");
6884 for (i = 0; i < p.Length; ++i) {
6886 GetBuilder.DefineParameter (
6887 i + 1, p [i].Attributes, p [i].Name);
6890 SetBuilder.DefineParameter (
6891 i + 1, p [i].Attributes, p [i].Name);
6895 SetBuilder.DefineParameter (
6896 i + 1, ParameterAttributes.None, "value");
6898 if (i != ParameterTypes.Length) {
6899 Parameter array_param = Parameters.ArrayParameter;
6901 SetBuilder.DefineParameter (
6902 i + 1, array_param.Attributes, array_param.Name);
6907 // Define the PropertyBuilder if one of the following conditions are met:
6908 // a) we're not implementing an interface indexer.
6909 // b) the indexer has a different IndexerName and this is no
6910 // explicit interface implementation.
6912 if (!IsExplicitImpl) {
6913 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6914 ShortName, prop_attr, MemberType, ParameterTypes);
6917 PropertyBuilder.SetGetMethod (GetBuilder);
6920 PropertyBuilder.SetSetMethod (SetBuilder);
6922 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
6929 public override string GetSignatureForError ()
6931 if (PropertyBuilder == null)
6932 return GetSignatureForError (Parent);
6934 return TypeManager.CSharpSignature (PropertyBuilder, true);
6937 public override string GetSignatureForError(TypeContainer tc)
6939 return String.Concat (tc.Name, ".this[", Parameters.FixedParameters [0].TypeName.ToString (), ']');
6943 public class Operator : MethodCore, IIteratorContainer {
6945 const int AllowedModifiers =
6951 const int RequiredModifiers =
6955 public enum OpType : byte {
6965 // Unary and Binary operators
6988 // Implicit and Explicit
6993 public readonly OpType OperatorType;
6994 public MethodBuilder OperatorMethodBuilder;
6996 public Method OperatorMethod;
6998 static string[] attribute_targets = new string [] { "method", "return" };
7000 public Operator (TypeContainer parent, OpType type, Expression ret_type,
7001 int mod_flags, Parameters parameters,
7002 Block block, Attributes attrs, Location loc)
7003 : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
7004 new MemberName ("op_" + type), attrs, parameters, loc)
7006 OperatorType = type;
7010 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7012 OperatorMethod.ApplyAttributeBuilder (a, cb);
7015 public override AttributeTargets AttributeTargets {
7017 return AttributeTargets.Method;
7021 protected override bool CheckGenericOverride (MethodInfo method, string name)
7026 protected override bool CheckForDuplications()
7028 ArrayList ar = Parent.Operators;
7030 int arLen = ar.Count;
7032 for (int i = 0; i < arLen; i++) {
7033 Operator o = (Operator) ar [i];
7034 if (IsDuplicateImplementation (o))
7039 ar = Parent.Methods;
7041 int arLen = ar.Count;
7043 for (int i = 0; i < arLen; i++) {
7044 Method m = (Method) ar [i];
7045 if (IsDuplicateImplementation (m))
7053 public override bool Define ()
7055 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7058 "User defined operators `" +
7059 GetSignatureForError (Parent) +
7060 "' must be declared static and public");
7067 OperatorMethod = new Method (
7068 Parent, null, Type, ModFlags, false, MemberName,
7069 Parameters, OptAttributes, Location);
7071 OperatorMethod.Block = Block;
7072 OperatorMethod.IsOperator = true;
7073 OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7074 OperatorMethod.Define ();
7076 if (OperatorMethod.MethodBuilder == null)
7079 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
7081 parameter_types = OperatorMethod.ParameterTypes;
7082 Type declaring_type = OperatorMethod.MethodData.DeclaringType;
7083 Type return_type = OperatorMethod.ReturnType;
7084 Type first_arg_type = parameter_types [0];
7089 // Rules for conversion operators
7091 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7092 if (first_arg_type == return_type && first_arg_type == declaring_type){
7095 "User-defined conversion cannot take an object of the " +
7096 "enclosing type and convert to an object of the enclosing" +
7101 if (first_arg_type != declaring_type && return_type != declaring_type){
7104 "User-defined conversion must convert to or from the " +
7109 if (first_arg_type == TypeManager.object_type ||
7110 return_type == TypeManager.object_type){
7113 "User-defined conversion cannot convert to or from " +
7118 if (first_arg_type.IsInterface || return_type.IsInterface){
7121 "User-defined conversion cannot convert to or from an " +
7126 if (first_arg_type.IsSubclassOf (return_type)
7127 || return_type.IsSubclassOf (first_arg_type)){
7128 if (declaring_type.IsSubclassOf (return_type)) {
7129 Report.Error (553, Location, "'{0}' : user defined conversion to/from base class", GetSignatureForError ());
7132 Report.Error (554, Location, "'{0}' : user defined conversion to/from derived class", GetSignatureForError ());
7135 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7136 if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
7137 Report.Error (564, Location, "Overloaded shift operator must have the type of the first operand be the containing type, and the type of the second operand must be int");
7140 } else if (Parameters.FixedParameters.Length == 1) {
7141 // Checks for Unary operators
7143 if (first_arg_type != declaring_type){
7146 "The parameter of a unary operator must be the " +
7151 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7152 if (return_type != declaring_type){
7155 "The parameter and return type for ++ and -- " +
7156 "must be the containing type");
7162 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7163 if (return_type != TypeManager.bool_type){
7166 "The return type of operator True or False " +
7173 // Checks for Binary operators
7175 if (first_arg_type != declaring_type &&
7176 parameter_types [1] != declaring_type){
7179 "One of the parameters of a binary operator must " +
7180 "be the containing type");
7188 public override void Emit ()
7191 // abstract or extern methods have no bodies
7193 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7196 OperatorMethod.Emit ();
7200 // Operator cannot be override
7201 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
7206 public static string GetName (OpType ot)
7209 case OpType.LogicalNot:
7211 case OpType.OnesComplement:
7213 case OpType.Increment:
7215 case OpType.Decrement:
7221 case OpType.Addition:
7223 case OpType.Subtraction:
7225 case OpType.UnaryPlus:
7227 case OpType.UnaryNegation:
7229 case OpType.Multiply:
7231 case OpType.Division:
7233 case OpType.Modulus:
7235 case OpType.BitwiseAnd:
7237 case OpType.BitwiseOr:
7239 case OpType.ExclusiveOr:
7241 case OpType.LeftShift:
7243 case OpType.RightShift:
7245 case OpType.Equality:
7247 case OpType.Inequality:
7249 case OpType.GreaterThan:
7251 case OpType.LessThan:
7253 case OpType.GreaterThanOrEqual:
7255 case OpType.LessThanOrEqual:
7257 case OpType.Implicit:
7259 case OpType.Explicit:
7265 public override string GetSignatureForError (TypeContainer tc)
7267 StringBuilder sb = new StringBuilder ();
7268 sb.AppendFormat ("{0}.operator {1} {2}({3}", tc.Name, GetName (OperatorType), Type.ToString (), Parameters.FixedParameters [0].GetSignatureForError ());
7270 if (Parameters.FixedParameters.Length > 1) {
7272 sb.Append (Parameters.FixedParameters [1].GetSignatureForError ());
7275 return sb.ToString ();
7278 public override string GetSignatureForError ()
7283 public override string ToString ()
7285 if (OperatorMethod == null)
7288 Type return_type = OperatorMethod.ReturnType;
7289 Type [] param_types = OperatorMethod.ParameterTypes;
7291 if (Parameters.FixedParameters.Length == 1)
7292 return String.Format (
7293 "{0} operator {1}({2})",
7294 TypeManager.CSharpName (return_type),
7295 GetName (OperatorType),
7298 return String.Format (
7299 "{0} operator {1}({2}, {3})",
7300 TypeManager.CSharpName (return_type),
7301 GetName (OperatorType),
7302 param_types [0], param_types [1]);
7305 public override string[] ValidAttributeTargets {
7307 return attribute_targets;
7311 public void SetYields ()
7313 ModFlags |= Modifiers.METHOD_YIELDS;
7318 // This is used to compare method signatures
7320 struct MethodSignature {
7322 public Type RetType;
7323 public Type [] Parameters;
7326 /// This delegate is used to extract methods which have the
7327 /// same signature as the argument
7329 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7331 public MethodSignature (string name, Type ret_type, Type [] parameters)
7336 if (parameters == null)
7337 Parameters = TypeManager.NoTypes;
7339 Parameters = parameters;
7342 public override string ToString ()
7345 if (Parameters.Length != 0){
7346 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7347 for (int i = 0; i < Parameters.Length; i++){
7348 sb.Append (Parameters [i]);
7349 if (i+1 < Parameters.Length)
7352 pars = sb.ToString ();
7355 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7358 public override int GetHashCode ()
7360 return Name.GetHashCode ();
7363 public override bool Equals (Object o)
7365 MethodSignature other = (MethodSignature) o;
7367 if (other.Name != Name)
7370 if (other.RetType != RetType)
7373 if (Parameters == null){
7374 if (other.Parameters == null)
7379 if (other.Parameters == null)
7382 int c = Parameters.Length;
7383 if (other.Parameters.Length != c)
7386 for (int i = 0; i < c; i++)
7387 if (other.Parameters [i] != Parameters [i])
7393 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7395 MethodSignature sig = (MethodSignature) filter_criteria;
7397 if (m.Name != sig.Name)
7401 MethodInfo mi = m as MethodInfo;
7402 PropertyInfo pi = m as PropertyInfo;
7405 ReturnType = mi.ReturnType;
7406 else if (pi != null)
7407 ReturnType = pi.PropertyType;
7412 // we use sig.RetType == null to mean `do not check the
7413 // method return value.
7415 if (sig.RetType != null)
7416 if (ReturnType != sig.RetType)
7421 args = TypeManager.GetArgumentTypes (mi);
7423 args = TypeManager.GetArgumentTypes (pi);
7424 Type [] sigp = sig.Parameters;
7426 if (args.Length != sigp.Length)
7429 for (int i = args.Length; i > 0; ){
7431 if (args [i] != sigp [i])