2 // class.cs: Class and Struct handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@ximian.com)
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)
11 // (C) 2004 Novell, Inc
14 // 2002-10-11 Miguel de Icaza <miguel@ximian.com>
16 // * class.cs: Following the comment from 2002-09-26 to AddMethod, I
17 // have fixed a remaining problem: not every AddXXXX was adding a
18 // fully qualified name.
20 // Now everyone registers a fully qualified name in the DeclSpace as
21 // being defined instead of the partial name.
23 // Downsides: we are slower than we need to be due to the excess
24 // copies and the names being registered this way.
26 // The reason for this is that we currently depend (on the corlib
27 // bootstrap for instance) that types are fully qualified, because
28 // we dump all the types in the namespace, and we should really have
29 // types inserted into the proper namespace, so we can only store the
30 // basenames in the defined_names array.
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41 using System.Security;
42 using System.Security.Permissions;
45 #if BOOTSTRAP_WITH_OLDLIB
46 using XmlElement = System.Object;
51 using Mono.CompilerServices.SymbolWriter;
53 namespace Mono.CSharp {
63 /// This is the base class for structs and classes.
65 public abstract class TypeContainer : DeclSpace, IMemberContainer {
67 public class MemberCoreArrayList: ArrayList
70 /// Defines the MemberCore objects that are in this array
72 public virtual void DefineContainerMembers ()
74 foreach (MemberCore mc in this) {
79 public virtual void Emit ()
81 foreach (MemberCore mc in this)
86 public class MethodArrayList: MemberCoreArrayList
94 CachedMethods cached_method;
95 TypeContainer container;
97 public MethodArrayList (TypeContainer container)
99 this.container = container;
103 /// Method container contains Equals method
105 public bool HasEquals {
107 cached_method |= CachedMethods.Equals;
111 return (cached_method & CachedMethods.Equals) != 0;
116 /// Method container contains GetHashCode method
118 public bool HasGetHashCode {
120 cached_method |= CachedMethods.GetHashCode;
124 return (cached_method & CachedMethods.GetHashCode) != 0;
128 public override void DefineContainerMembers ()
130 base.DefineContainerMembers ();
132 if ((RootContext.WarningLevel >= 3) && HasEquals && !HasGetHashCode) {
133 Report.Warning (659, container.Location, "'{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
139 public sealed class IndexerArrayList: MemberCoreArrayList
142 /// The indexer name for this container
144 public string IndexerName = DefaultIndexerName;
146 bool seen_normal_indexers = false;
148 TypeContainer container;
150 public IndexerArrayList (TypeContainer container)
152 this.container = container;
156 /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
157 /// class is consistent. Either it is `Item' or it is the name defined by all the
158 /// indexers with the `IndexerName' attribute.
160 /// Turns out that the IndexerNameAttribute is applied to each indexer,
161 /// but it is never emitted, instead a DefaultMember attribute is attached
164 public override void DefineContainerMembers()
166 base.DefineContainerMembers ();
168 string class_indexer_name = null;
171 // If there's both an explicit and an implicit interface implementation, the
172 // explicit one actually implements the interface while the other one is just
173 // a normal indexer. See bug #37714.
176 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
177 foreach (Indexer i in this) {
178 if (i.InterfaceType != null) {
179 if (seen_normal_indexers)
180 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
184 seen_normal_indexers = true;
186 if (class_indexer_name == null) {
187 class_indexer_name = i.ShortName;
191 if (i.ShortName != class_indexer_name)
192 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");
195 if (class_indexer_name != null)
196 IndexerName = class_indexer_name;
199 public override void Emit ()
203 if (!seen_normal_indexers)
206 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
207 container.TypeBuilder.SetCustomAttribute (cb);
211 public class OperatorArrayList: MemberCoreArrayList
213 TypeContainer container;
215 public OperatorArrayList (TypeContainer container)
217 this.container = container;
221 // Operator pair checking
226 public Type ret_type;
227 public Type type1, type2;
229 public Operator.OpType ot;
231 public OperatorEntry (int f, Operator o)
235 ret_type = o.OperatorMethod.ReturnType;
236 Type [] pt = o.OperatorMethod.ParameterTypes;
243 public override int GetHashCode ()
245 return ret_type.GetHashCode ();
248 public override bool Equals (object o)
250 OperatorEntry other = (OperatorEntry) o;
252 if (other.ret_type != ret_type)
254 if (other.type1 != type1)
256 if (other.type2 != type2)
263 // Checks that some operators come in pairs:
269 // They are matched based on the return type and the argument types
271 void CheckPairedOperators ()
273 Hashtable pairs = new Hashtable (null, null);
274 Operator true_op = null;
275 Operator false_op = null;
276 bool has_equality_or_inequality = false;
278 // Register all the operators we care about.
279 foreach (Operator op in this){
282 switch (op.OperatorType){
283 case Operator.OpType.Equality:
285 has_equality_or_inequality = true;
287 case Operator.OpType.Inequality:
289 has_equality_or_inequality = true;
292 case Operator.OpType.True:
295 case Operator.OpType.False:
299 case Operator.OpType.GreaterThan:
301 case Operator.OpType.LessThan:
304 case Operator.OpType.GreaterThanOrEqual:
306 case Operator.OpType.LessThanOrEqual:
312 OperatorEntry oe = new OperatorEntry (reg, op);
314 object o = pairs [oe];
318 oe = (OperatorEntry) o;
323 if (true_op != null){
324 if (false_op == null)
325 Report.Error (216, true_op.Location, "operator true requires a matching operator false");
326 } else if (false_op != null)
327 Report.Error (216, false_op.Location, "operator false requires a matching operator true");
330 // Look for the mistakes.
332 foreach (DictionaryEntry de in pairs){
333 OperatorEntry oe = (OperatorEntry) de.Key;
340 case Operator.OpType.Equality:
343 case Operator.OpType.Inequality:
346 case Operator.OpType.GreaterThan:
349 case Operator.OpType.LessThan:
352 case Operator.OpType.GreaterThanOrEqual:
355 case Operator.OpType.LessThanOrEqual:
359 Report.Error (216, oe.op.Location,
360 "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
363 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
364 if (container.Methods == null || !container.Methods.HasEquals)
365 Report.Warning (660, container.Location, "'{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
367 if (container.Methods == null || !container.Methods.HasGetHashCode)
368 Report.Warning (661, container.Location, "'{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
372 public override void DefineContainerMembers ()
374 base.DefineContainerMembers ();
375 CheckPairedOperators ();
380 // Whether this is a struct, class or interface
381 public readonly Kind Kind;
383 // Holds a list of classes and structures
386 // Holds the list of properties
387 MemberCoreArrayList properties;
389 // Holds the list of enumerations
390 MemberCoreArrayList enums;
392 // Holds the list of delegates
393 MemberCoreArrayList delegates;
395 // Holds the list of constructors
396 protected MemberCoreArrayList instance_constructors;
398 // Holds the list of fields
399 MemberCoreArrayList fields;
401 // Holds a list of fields that have initializers
402 protected ArrayList initialized_fields;
404 // Holds a list of static fields that have initializers
405 protected ArrayList initialized_static_fields;
407 // Holds the list of constants
408 MemberCoreArrayList constants;
411 MemberCoreArrayList interfaces;
413 // Holds the methods.
414 MethodArrayList methods;
417 protected MemberCoreArrayList events;
419 // Holds the indexers
420 IndexerArrayList indexers;
422 // Holds the operators
423 MemberCoreArrayList operators;
425 // Holds the iterators
428 // Holds the parts of a partial class;
432 // Pointers to the default constructor and the default static constructor
434 protected Constructor default_constructor;
435 protected Constructor default_static_constructor;
438 // Whether we have at least one non-static field
440 bool have_nonstatic_fields = false;
443 // This one is computed after we can distinguish interfaces
444 // from classes from the arraylist `type_bases'
446 string base_class_name;
449 ArrayList type_bases;
451 bool members_defined;
452 bool members_defined_ok;
454 // The interfaces we implement.
455 protected Type [] ifaces;
456 protected Type ptype;
458 // The base member cache and our member cache
459 MemberCache base_cache;
460 MemberCache member_cache;
462 public const string DefaultIndexerName = "Item";
464 public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
465 Attributes attrs, Kind kind, Location l)
466 : base (ns, parent, name, attrs, l)
468 if (parent != null && parent != RootContext.Tree.Types && parent.NamespaceEntry != ns)
469 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
473 types = new ArrayList ();
475 base_class_name = null;
478 public bool AddToMemberContainer (MemberCore symbol)
480 return AddToContainer (symbol, symbol.Name);
483 bool AddToTypeContainer (DeclSpace ds)
485 // Parent == null ==> this == RootContext.Tree.Types
486 return AddToContainer (ds, (Parent == null) ? ds.Name : ds.Basename);
489 public void AddConstant (Const constant)
491 if (!AddToMemberContainer (constant))
494 if (constants == null)
495 constants = new MemberCoreArrayList ();
497 constants.Add (constant);
500 public void AddEnum (Mono.CSharp.Enum e)
502 if (!AddToTypeContainer (e))
506 enums = new MemberCoreArrayList ();
511 public void AddClassOrStruct (TypeContainer c)
513 if (!AddToTypeContainer (c))
519 public void AddDelegate (Delegate d)
521 if (!AddToTypeContainer (d))
524 if (delegates == null)
525 delegates = new MemberCoreArrayList ();
530 public void AddMethod (Method method)
532 if (!AddToMemberContainer (method))
536 methods = new MethodArrayList (this);
538 if (method.MemberName.Left != null)
539 methods.Insert (0, method);
541 methods.Add (method);
544 public void AddConstructor (Constructor c)
546 if (c.Name != Basename) {
547 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
550 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
553 if (default_static_constructor != null) {
554 Report.SymbolRelatedToPreviousError (default_static_constructor);
555 Report.Error (111, c.Location, Error111, Name, c.Name);
559 default_static_constructor = c;
562 if (default_constructor != null) {
563 Report.SymbolRelatedToPreviousError (default_constructor);
564 Report.Error (111, c.Location, Error111, c.Location, Name, c.Name);
567 default_constructor = c;
570 if (instance_constructors == null)
571 instance_constructors = new MemberCoreArrayList ();
573 instance_constructors.Add (c);
577 internal static string Error111 {
579 return "Type '{0}' already defines a member called '{1}' with the same parameter types";
583 public void AddInterface (TypeContainer iface)
585 if (!AddToTypeContainer (iface))
588 if (interfaces == null) {
589 interfaces = new MemberCoreArrayList ();
592 interfaces.Add (iface);
595 public void AddField (FieldMember field)
597 if (!AddToMemberContainer (field))
601 fields = new MemberCoreArrayList ();
605 if (field.HasInitializer){
606 if ((field.ModFlags & Modifiers.STATIC) != 0){
607 if (initialized_static_fields == null)
608 initialized_static_fields = new ArrayList ();
610 initialized_static_fields.Add (field);
612 if (initialized_fields == null)
613 initialized_fields = new ArrayList ();
615 initialized_fields.Add (field);
619 if ((field.ModFlags & Modifiers.STATIC) == 0)
620 have_nonstatic_fields = true;
623 public void AddProperty (Property prop)
625 if (!AddToMemberContainer (prop) ||
626 !AddToMemberContainer (prop.Get) || !AddToMemberContainer (prop.Set))
629 if (properties == null)
630 properties = new MemberCoreArrayList ();
632 if (prop.MemberName.Left != null)
633 properties.Insert (0, prop);
635 properties.Add (prop);
638 public void AddEvent (Event e)
640 if (!AddToMemberContainer (e))
643 if (e is EventProperty) {
644 if (!AddToMemberContainer (e.Add))
647 if (!AddToMemberContainer (e.Remove))
652 events = new MemberCoreArrayList ();
659 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
661 public void AddIndexer (Indexer i)
663 if (indexers == null)
664 indexers = new IndexerArrayList (this);
666 if (i.IsExplicitImpl)
667 indexers.Insert (0, i);
672 public void AddOperator (Operator op)
674 if (!AddToMemberContainer (op))
677 if (operators == null)
678 operators = new OperatorArrayList (this);
683 public void AddIterator (Iterator i)
685 if (iterators == null)
686 iterators = new ArrayList ();
691 public void AddType (TypeContainer tc)
696 public void AddPart (ClassPart part)
699 parts = new ArrayList ();
704 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
706 if (a.Type == TypeManager.default_member_type) {
707 if (Indexers != null) {
708 Report.Error (646, a.Location,
709 "Cannot specify the DefaultMember attribute on" +
710 " a type containing an indexer");
715 base.ApplyAttributeBuilder (a, cb);
718 public override AttributeTargets AttributeTargets {
722 return AttributeTargets.Class;
724 return AttributeTargets.Struct;
726 return AttributeTargets.Interface;
728 throw new NotSupportedException ();
733 public ArrayList Types {
739 public MethodArrayList Methods {
745 public ArrayList Constants {
751 public ArrayList Interfaces {
757 public ArrayList Iterators {
765 return base_class_name;
769 public ArrayList Bases {
779 public ArrayList Fields {
785 public ArrayList InstanceConstructors {
787 return instance_constructors;
791 public ArrayList Properties {
797 public ArrayList Events {
803 public ArrayList Enums {
809 public ArrayList Indexers {
815 public ArrayList Operators {
821 public ArrayList Delegates {
827 public ArrayList Parts {
833 public virtual TypeAttributes TypeAttr {
835 return Modifiers.TypeAttr (ModFlags, this);
839 public string IndexerName {
841 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
846 // Emits the instance field initializers
848 public virtual bool EmitFieldInitializers (EmitContext ec)
851 Expression instance_expr;
854 fields = initialized_static_fields;
855 instance_expr = null;
857 fields = initialized_fields;
858 instance_expr = new This (Location.Null).Resolve (ec);
864 foreach (Field f in fields){
865 Expression e = f.GetInitializerExpression (ec);
869 Location l = f.Location;
870 FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
871 fe.InstanceExpression = instance_expr;
872 ExpressionStatement a = new Assign (fe, e, l);
874 a = a.ResolveStatement (ec);
878 if (RootContext.Optimize) {
879 Constant c = e as Constant;
881 if (c.IsDefaultValue)
886 a.EmitStatement (ec);
893 // Defines the default constructors
895 protected void DefineDefaultConstructor (bool is_static)
899 // The default constructor is public
900 // If the class is abstract, the default constructor is protected
901 // The default static constructor is private
903 int mods = Modifiers.PUBLIC;
905 mods = Modifiers.STATIC | Modifiers.PRIVATE;
906 else if ((ModFlags & Modifiers.ABSTRACT) != 0)
907 mods = Modifiers.PROTECTED;
909 TypeContainer constructor_parent = this;
911 constructor_parent = (TypeContainer) Parts [0];
913 c = new Constructor (constructor_parent, Basename, mods,
914 Parameters.EmptyReadOnlyParameters,
915 new ConstructorBaseInitializer (
916 null, Parameters.EmptyReadOnlyParameters,
922 c.Block = new ToplevelBlock (null, Location);
927 /// The pending methods that need to be implemented
928 // (interfaces or abstract methods)
930 public PendingImplementation Pending;
932 public abstract PendingImplementation GetPendingImplementations ();
934 TypeExpr[] GetPartialBases (out TypeExpr base_class, out bool error)
936 ArrayList ifaces = new ArrayList ();
939 Location base_loc = Location.Null;
941 foreach (ClassPart part in parts) {
942 TypeExpr new_base_class;
943 TypeExpr[] new_ifaces;
945 new_ifaces = part.GetClassBases (out new_base_class, out error);
949 if ((base_class != null) && (new_base_class != null) &&
950 !base_class.Equals (new_base_class)) {
951 Report.Error (263, part.Location,
952 "Partial declarations of `{0}' must " +
953 "not specify different base classes",
956 if (!Location.IsNull (base_loc))
957 Report.LocationOfPreviousError (base_loc);
963 if ((base_class == null) && (new_base_class != null)) {
964 base_class = new_base_class;
965 base_loc = part.Location;
968 if (new_ifaces == null)
971 foreach (TypeExpr iface in new_ifaces) {
973 foreach (TypeExpr old_iface in ifaces) {
974 if (old_iface.Equals (iface)) {
987 TypeExpr[] retval = new TypeExpr [ifaces.Count];
988 ifaces.CopyTo (retval, 0);
992 TypeExpr[] GetNormalBases (out TypeExpr base_class, out bool error)
996 int count = Bases.Count;
999 if (Kind == Kind.Class){
1000 TypeExpr name = ResolveBaseTypeExpr (
1001 (Expression) Bases [0], false, Location);
1008 if (!name.IsInterface) {
1009 // base_class could be a class, struct, enum, delegate.
1010 // This is validated in GetClassBases.
1016 TypeExpr [] ifaces = new TypeExpr [count-start];
1018 for (i = start, j = 0; i < count; i++, j++){
1019 TypeExpr resolved = ResolveBaseTypeExpr ((Expression) Bases [i], false, Location);
1020 if (resolved == null) {
1025 ifaces [j] = resolved;
1033 /// This function computes the Base class and also the
1034 /// list of interfaces that the class or struct @c implements.
1036 /// The return value is an array (might be null) of
1037 /// interfaces implemented (as Types).
1039 /// The @base_class argument is set to the base object or null
1040 /// if this is `System.Object'.
1042 TypeExpr [] GetClassBases (out TypeExpr base_class, out bool error)
1051 ifaces = GetPartialBases (out base_class, out error);
1052 else if (Bases == null){
1056 ifaces = GetNormalBases (out base_class, out error);
1061 if ((base_class != null) && (Kind == Kind.Class)){
1063 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
1064 Report.Error (1521, base_class.Location, "Invalid base type");
1069 if (base_class.IsSealed){
1071 Report.SymbolRelatedToPreviousError (base_class.Type);
1072 if (base_class.Type.IsAbstract) {
1073 Report.Error (709, Location, "'{0}': Cannot derive from static class", GetSignatureForError ());
1075 Report.Error (509, Location, "'{0}': Cannot derive from sealed class", GetSignatureForError ());
1080 if (!base_class.CanInheritFrom ()){
1081 Report.Error (644, Location,
1082 "`{0}' cannot inherit from special class `{1}'",
1083 Name, base_class.Name);
1088 if (!base_class.AsAccessible (this, ModFlags)) {
1089 Report.SymbolRelatedToPreviousError (base_class.Type);
1090 Report.Error (60, Location, "Inconsistent accessibility: base class '{0}' is less accessible than class '{1}'",
1091 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
1095 if (base_class != null)
1096 base_class_name = base_class.Name;
1101 int count = ifaces != null ? ifaces.Length : 0;
1103 for (i = 0; i < count; i++) {
1104 TypeExpr iface = (TypeExpr) ifaces [i];
1106 if (!iface.IsInterface) {
1108 if (Kind != Kind.Class) {
1109 string what = Kind == Kind.Struct ? "Struct" : "Interface";
1111 Report.Error (527, Location,
1112 "In {0} `{1}', type `{2}' is not "+
1113 "an interface", what, Name, iface.Name);
1115 else if (base_class != null)
1116 Report.Error (1721, Location,
1117 "In Class `{0}', `{1}' is not an interface, and a base class has already been defined",
1120 Report.Error (1722, Location,
1121 "In Class `{0}', `{1}' is not " +
1122 "an interface, a base class must be listed first", Name, iface.Name);
1127 for (int x = 0; x < i; x++) {
1128 if (iface.Equals (ifaces [x])) {
1129 Report.Error (528, Location,
1130 "`{0}' is already listed in " +
1131 "interface list", iface.Name);
1136 if ((Kind == Kind.Interface) &&
1137 !iface.AsAccessible (Parent, ModFlags)) {
1138 Report.Error (61, Location,
1139 "Inconsistent accessibility: base " +
1140 "interface `{0}' is less accessible " +
1141 "than interface `{1}'", iface.Name,
1156 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1158 public override TypeBuilder DefineType ()
1160 if (TypeBuilder != null)
1167 Report.Error (146, Location, "Class definition is circular: `{0}'", Name);
1174 TypeExpr[] iface_exprs = GetClassBases (out base_type, out error);
1178 if (base_type == null) {
1179 if (Kind == Kind.Class){
1180 if (RootContext.StdLib)
1181 base_type = TypeManager.system_object_expr;
1182 else if (Name != "System.Object")
1183 base_type = TypeManager.system_object_expr;
1184 } else if (Kind == Kind.Struct) {
1186 // If we are compiling our runtime,
1187 // and we are defining ValueType, then our
1188 // base is `System.Object'.
1190 if (!RootContext.StdLib && Name == "System.ValueType")
1191 base_type = TypeManager.system_object_expr;
1193 base_type = TypeManager.system_valuetype_expr;
1197 if ((Kind == Kind.Struct) && TypeManager.value_type == null)
1198 throw new Exception ();
1200 TypeAttributes type_attributes = TypeAttr;
1202 if (base_type != null) {
1203 // FIXME: I think this should be ...ResolveType (Parent.EmitContext).
1204 // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
1205 ptype = base_type.ResolveType (TypeResolveEmitContext);
1206 if (ptype == null) {
1214 if (TypeManager.NamespaceClash (Name, Location)) {
1219 ModuleBuilder builder = CodeGen.Module.Builder;
1220 TypeBuilder = builder.DefineType (
1221 Name, type_attributes, ptype, null);
1224 TypeBuilder builder = Parent.TypeBuilder;
1225 if (builder == null)
1228 TypeBuilder = builder.DefineNestedType (
1229 Basename, type_attributes, ptype, null);
1232 catch (ArgumentException) {
1233 Report.RuntimeMissingSupport (Location, "static classes");
1237 if (Parts != null) {
1239 foreach (ClassPart part in Parts) {
1240 part.TypeBuilder = TypeBuilder;
1242 part.ec = new EmitContext (part, Mono.CSharp.Location.Null, null, null, ModFlags);
1243 part.ec.ContainerType = TypeBuilder;
1247 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
1248 ec.ContainerType = TypeBuilder;
1252 // Structs with no fields need to have at least one byte.
1253 // The right thing would be to set the PackingSize in a DefineType
1254 // but there are no functions that allow interfaces *and* the size to
1258 if ((Kind == Kind.Struct) && !have_nonstatic_fields){
1259 TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
1260 FieldAttributes.Private);
1263 // add interfaces that were not added at type creation
1264 if (iface_exprs != null) {
1265 // FIXME: I think this should be ...ExpandInterfaces (Parent.EmitContext, ...).
1266 // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
1267 TypeResolveEmitContext.ContainerType = TypeBuilder;
1268 ifaces = TypeManager.ExpandInterfaces (TypeResolveEmitContext, iface_exprs);
1269 if (ifaces == null) {
1274 foreach (Type itype in ifaces)
1275 TypeBuilder.AddInterfaceImplementation (itype);
1277 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1280 TypeManager.AddUserType (Name, TypeBuilder, this);
1282 if (!(this is Iterator))
1283 RootContext.RegisterOrder (this);
1285 if (!DefineNestedTypes ()) {
1294 protected virtual bool DefineNestedTypes ()
1296 if (Interfaces != null) {
1297 foreach (TypeContainer iface in Interfaces)
1298 if (iface.DefineType () == null)
1302 if (Types != null) {
1303 foreach (TypeContainer tc in Types)
1304 if (tc.DefineType () == null)
1308 if (Delegates != null) {
1309 foreach (Delegate d in Delegates)
1310 if (d.DefineType () == null)
1314 if (Enums != null) {
1315 foreach (Enum en in Enums)
1316 if (en.DefineType () == null)
1323 static void Error_KeywordNotAllowed (Location loc)
1325 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
1329 /// Populates our TypeBuilder with fields and methods
1331 public override bool DefineMembers (TypeContainer container)
1333 if (members_defined)
1334 return members_defined_ok;
1336 members_defined_ok = DoDefineMembers ();
1337 members_defined = true;
1339 return members_defined_ok;
1342 bool DoDefineMembers ()
1345 if ((ModFlags & Modifiers.NEW) != 0)
1346 Error_KeywordNotAllowed (Location);
1348 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1349 if (conflict_symbol == null) {
1350 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1351 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1353 if ((ModFlags & Modifiers.NEW) == 0) {
1354 Report.SymbolRelatedToPreviousError (conflict_symbol);
1355 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError ());
1360 DefineContainerMembers (constants);
1361 DefineContainerMembers (fields);
1363 if ((Kind == Kind.Class) && !(this is ClassPart)){
1364 if ((instance_constructors == null) &&
1365 !(this is StaticClass)) {
1366 if (default_constructor == null)
1367 DefineDefaultConstructor (false);
1370 if (initialized_static_fields != null &&
1371 default_static_constructor == null)
1372 DefineDefaultConstructor (true);
1375 if (Kind == Kind.Struct){
1377 // Structs can not have initialized instance
1380 if (initialized_static_fields != null &&
1381 default_static_constructor == null)
1382 DefineDefaultConstructor (true);
1384 if (initialized_fields != null)
1385 ReportStructInitializedInstanceError ();
1388 Pending = GetPendingImplementations ();
1390 if (parts != null) {
1391 foreach (ClassPart part in parts) {
1392 if (!part.DefineMembers (this))
1398 // Constructors are not in the defined_names array
1400 DefineContainerMembers (instance_constructors);
1402 if (default_static_constructor != null)
1403 default_static_constructor.Define ();
1405 DefineContainerMembers (properties);
1406 DefineContainerMembers (events);
1407 DefineContainerMembers (indexers);
1408 DefineContainerMembers (methods);
1409 DefineContainerMembers (operators);
1410 DefineContainerMembers (enums);
1411 DefineContainerMembers (delegates);
1414 if (!(this is ClassPart))
1415 member_cache = new MemberCache (this);
1418 if (parts != null) {
1419 foreach (ClassPart part in parts)
1420 part.member_cache = member_cache;
1423 if (iterators != null) {
1424 foreach (Iterator iterator in iterators) {
1425 if (iterator.DefineType () == null)
1429 foreach (Iterator iterator in iterators) {
1430 if (!iterator.DefineMembers (this))
1438 void ReportStructInitializedInstanceError ()
1440 string n = TypeBuilder.FullName;
1442 foreach (Field f in initialized_fields){
1445 "`" + n + "." + f.Name + "': can not have " +
1446 "instance field initializers in structs");
1450 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1453 mcal.DefineContainerMembers ();
1456 public override bool Define ()
1458 if (parts != null) {
1459 foreach (ClassPart part in parts) {
1460 if (!part.Define ())
1468 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1470 return BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1474 /// This function is based by a delegate to the FindMembers routine
1476 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1482 /// This filter is used by FindMembers, and we just keep
1483 /// a global for the filter to `AlwaysAccept'
1485 static MemberFilter accepting_filter;
1488 static TypeContainer ()
1490 accepting_filter = new MemberFilter (AlwaysAccept);
1493 public MethodInfo[] GetMethods ()
1495 ArrayList members = new ArrayList ();
1497 DefineMembers (null);
1499 if (methods != null) {
1500 int len = methods.Count;
1501 for (int i = 0; i < len; i++) {
1502 Method m = (Method) methods [i];
1504 members.Add (m.MethodBuilder);
1508 if (operators != null) {
1509 int len = operators.Count;
1510 for (int i = 0; i < len; i++) {
1511 Operator o = (Operator) operators [i];
1513 members.Add (o.OperatorMethodBuilder);
1517 if (properties != null) {
1518 int len = properties.Count;
1519 for (int i = 0; i < len; i++) {
1520 Property p = (Property) properties [i];
1522 if (p.GetBuilder != null)
1523 members.Add (p.GetBuilder);
1524 if (p.SetBuilder != null)
1525 members.Add (p.SetBuilder);
1529 if (indexers != null) {
1530 int len = indexers.Count;
1531 for (int i = 0; i < len; i++) {
1532 Indexer ix = (Indexer) indexers [i];
1534 if (ix.GetBuilder != null)
1535 members.Add (ix.GetBuilder);
1536 if (ix.SetBuilder != null)
1537 members.Add (ix.SetBuilder);
1541 if (events != null) {
1542 int len = events.Count;
1543 for (int i = 0; i < len; i++) {
1544 Event e = (Event) events [i];
1546 if (e.AddBuilder != null)
1547 members.Add (e.AddBuilder);
1548 if (e.RemoveBuilder != null)
1549 members.Add (e.RemoveBuilder);
1553 MethodInfo[] retMethods = new MethodInfo [members.Count];
1554 members.CopyTo (retMethods, 0);
1558 // Indicated whether container has StructLayout attribute set Explicit
1559 public virtual bool HasExplicitLayout {
1565 public override Type FindNestedType (string name)
1567 ArrayList [] lists = { types, enums, delegates, interfaces };
1569 for (int j = 0; j < lists.Length; ++j) {
1570 ArrayList list = lists [j];
1574 int len = list.Count;
1575 for (int i = 0; i < len; ++i) {
1576 DeclSpace ds = (DeclSpace) list [i];
1577 if (ds.Basename == name) {
1579 return ds.TypeBuilder;
1587 private void FindMembers_NestedTypes (int modflags,
1588 BindingFlags bf, MemberFilter filter, object criteria,
1589 ref ArrayList members)
1591 ArrayList [] lists = { types, enums, delegates, interfaces };
1593 for (int j = 0; j < lists.Length; ++j) {
1594 ArrayList list = lists [j];
1598 int len = list.Count;
1599 for (int i = 0; i < len; i++) {
1600 DeclSpace ds = (DeclSpace) list [i];
1602 if ((ds.ModFlags & modflags) == 0)
1605 TypeBuilder tb = ds.TypeBuilder;
1607 if (!(criteria is string) || ds.Basename.Equals (criteria))
1608 tb = ds.DefineType ();
1611 if (tb != null && (filter (tb, criteria) == true)) {
1612 if (members == null)
1613 members = new ArrayList ();
1622 /// This method returns the members of this type just like Type.FindMembers would
1623 /// Only, we need to use this for types which are _being_ defined because MS'
1624 /// implementation can't take care of that.
1627 // FIXME: return an empty static array instead of null, that cleans up
1628 // some code and is consistent with some coding conventions I just found
1632 // Notice that in various cases we check if our field is non-null,
1633 // something that would normally mean that there was a bug elsewhere.
1635 // The problem happens while we are defining p-invoke methods, as those
1636 // will trigger a FindMembers, but this happens before things are defined
1638 // Since the whole process is a no-op, it is fine to check for null here.
1640 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1641 MemberFilter filter, object criteria)
1643 ArrayList members = null;
1646 if ((bf & BindingFlags.Public) != 0)
1647 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1649 if ((bf & BindingFlags.NonPublic) != 0)
1650 modflags |= Modifiers.PRIVATE;
1652 int static_mask = 0, static_flags = 0;
1653 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1654 case BindingFlags.Static:
1655 static_mask = static_flags = Modifiers.STATIC;
1658 case BindingFlags.Instance:
1659 static_mask = Modifiers.STATIC;
1664 static_mask = static_flags = 0;
1668 Timer.StartTimer (TimerType.TcFindMembers);
1671 filter = accepting_filter;
1673 if ((mt & MemberTypes.Field) != 0) {
1674 if (fields != null) {
1675 int len = fields.Count;
1676 for (int i = 0; i < len; i++) {
1677 FieldMember f = (FieldMember) fields [i];
1679 if ((f.ModFlags & modflags) == 0)
1681 if ((f.ModFlags & static_mask) != static_flags)
1684 FieldBuilder fb = f.FieldBuilder;
1685 if (fb != null && filter (fb, criteria) == true) {
1686 if (members == null)
1687 members = new ArrayList ();
1694 if (constants != null) {
1695 int len = constants.Count;
1696 for (int i = 0; i < len; i++) {
1697 Const con = (Const) constants [i];
1699 if ((con.ModFlags & modflags) == 0)
1701 if ((con.ModFlags & static_mask) != static_flags)
1704 FieldBuilder fb = con.FieldBuilder;
1707 fb = con.FieldBuilder;
1709 if (fb != null && filter (fb, criteria) == true) {
1710 if (members == null)
1711 members = new ArrayList ();
1719 if ((mt & MemberTypes.Method) != 0) {
1720 if (methods != null) {
1721 int len = methods.Count;
1722 for (int i = 0; i < len; i++) {
1723 Method m = (Method) methods [i];
1725 if ((m.ModFlags & modflags) == 0)
1727 if ((m.ModFlags & static_mask) != static_flags)
1730 MethodBuilder mb = m.MethodBuilder;
1732 if (mb != null && filter (mb, criteria) == true) {
1733 if (members == null)
1734 members = new ArrayList ();
1741 if (operators != null) {
1742 int len = operators.Count;
1743 for (int i = 0; i < len; i++) {
1744 Operator o = (Operator) operators [i];
1746 if ((o.ModFlags & modflags) == 0)
1748 if ((o.ModFlags & static_mask) != static_flags)
1751 MethodBuilder ob = o.OperatorMethodBuilder;
1752 if (ob != null && filter (ob, criteria) == true) {
1753 if (members == null)
1754 members = new ArrayList ();
1761 if (properties != null) {
1762 int len = properties.Count;
1763 for (int i = 0; i < len; i++) {
1764 Property p = (Property) properties [i];
1766 if ((p.ModFlags & modflags) == 0)
1768 if ((p.ModFlags & static_mask) != static_flags)
1774 if (b != null && filter (b, criteria) == true) {
1775 if (members == null)
1776 members = new ArrayList ();
1782 if (b != null && filter (b, criteria) == true) {
1783 if (members == null)
1784 members = new ArrayList ();
1791 if (indexers != null) {
1792 int len = indexers.Count;
1793 for (int i = 0; i < len; i++) {
1794 Indexer ix = (Indexer) indexers [i];
1796 if ((ix.ModFlags & modflags) == 0)
1798 if ((ix.ModFlags & static_mask) != static_flags)
1804 if (b != null && filter (b, criteria) == true) {
1805 if (members == null)
1806 members = new ArrayList ();
1812 if (b != null && filter (b, criteria) == true) {
1813 if (members == null)
1814 members = new ArrayList ();
1822 if ((mt & MemberTypes.Event) != 0) {
1823 if (events != null) {
1824 int len = events.Count;
1825 for (int i = 0; i < len; i++) {
1826 Event e = (Event) events [i];
1828 if ((e.ModFlags & modflags) == 0)
1830 if ((e.ModFlags & static_mask) != static_flags)
1833 MemberInfo eb = e.EventBuilder;
1834 if (eb != null && filter (eb, criteria) == true) {
1835 if (members == null)
1836 members = new ArrayList ();
1838 members.Add (e.EventBuilder);
1844 if ((mt & MemberTypes.Property) != 0){
1845 if (properties != null) {
1846 int len = properties.Count;
1847 for (int i = 0; i < len; i++) {
1848 Property p = (Property) properties [i];
1850 if ((p.ModFlags & modflags) == 0)
1852 if ((p.ModFlags & static_mask) != static_flags)
1855 MemberInfo pb = p.PropertyBuilder;
1856 if (pb != null && filter (pb, criteria) == true) {
1857 if (members == null)
1858 members = new ArrayList ();
1860 members.Add (p.PropertyBuilder);
1865 if (indexers != null) {
1866 int len = indexers.Count;
1867 for (int i = 0; i < len; i++) {
1868 Indexer ix = (Indexer) indexers [i];
1870 if ((ix.ModFlags & modflags) == 0)
1872 if ((ix.ModFlags & static_mask) != static_flags)
1875 MemberInfo ib = ix.PropertyBuilder;
1876 if (ib != null && filter (ib, criteria) == true) {
1877 if (members == null)
1878 members = new ArrayList ();
1880 members.Add (ix.PropertyBuilder);
1886 if ((mt & MemberTypes.NestedType) != 0)
1887 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1889 if ((mt & MemberTypes.Constructor) != 0){
1890 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1891 int len = instance_constructors.Count;
1892 for (int i = 0; i < len; i++) {
1893 Constructor c = (Constructor) instance_constructors [i];
1895 ConstructorBuilder cb = c.ConstructorBuilder;
1896 if (cb != null && filter (cb, criteria) == true) {
1897 if (members == null)
1898 members = new ArrayList ();
1905 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1906 ConstructorBuilder cb =
1907 default_static_constructor.ConstructorBuilder;
1909 if (cb != null && filter (cb, criteria) == true) {
1910 if (members == null)
1911 members = new ArrayList ();
1919 // Lookup members in base if requested.
1921 if ((bf & BindingFlags.DeclaredOnly) == 0) {
1922 if (TypeBuilder.BaseType != null) {
1923 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1924 if (list.Count > 0) {
1925 if (members == null)
1926 members = new ArrayList ();
1928 members.AddRange (list);
1931 if (ifaces != null) {
1932 foreach (Type base_type in ifaces) {
1933 MemberList list = TypeContainer.FindMembers (base_type, mt, bf, filter, criteria);
1935 if (list.Count > 0) {
1936 if (members == null)
1937 members = new ArrayList ();
1938 members.AddRange (list);
1944 Timer.StopTimer (TimerType.TcFindMembers);
1946 if (members == null)
1947 return MemberList.Empty;
1949 return new MemberList (members);
1952 public override MemberCache MemberCache {
1954 return member_cache;
1958 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1959 MemberFilter filter, object criteria)
1961 DeclSpace ds = TypeManager.LookupDeclSpace (t);
1964 return ds.FindMembers (mt, bf, filter, criteria);
1966 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1971 // FindMethods will look for methods not only in the type `t', but in
1972 // any interfaces implemented by the type.
1974 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
1975 MemberFilter filter, object criteria)
1981 /// Emits the values for the constants
1983 public void EmitConstants ()
1985 if (constants != null)
1986 foreach (Const con in constants)
1991 void CheckMemberUsage (MemberCoreArrayList al, string member_type)
1996 foreach (MemberCore mc in al) {
1997 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2001 Report.Warning (169, mc.Location, "The private {0} '{1}' is never used", member_type, mc.GetSignatureForError ());
2006 public virtual void VerifyMembers ()
2009 // Check for internal or private fields that were never assigned
2011 if (RootContext.WarningLevel >= 3) {
2012 CheckMemberUsage (properties, "property");
2013 CheckMemberUsage (methods, "method");
2014 CheckMemberUsage (constants, "constant");
2016 if (fields != null){
2017 foreach (FieldMember f in fields) {
2018 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2022 Report.Warning (169, f.Location, "The private field '{0}' is never used", f.GetSignatureForError ());
2027 // Only report 649 on level 4
2029 if (RootContext.WarningLevel < 4)
2032 if ((f.status & Field.Status.ASSIGNED) != 0)
2035 Report.Warning (649, f.Location, "Field '{0}' is never assigned to, and will always have its default value '{1}'", f.GetSignatureForError (), "");
2042 /// Emits the code, this step is performed after all
2043 /// the types, enumerations, constructors
2045 public void EmitType ()
2047 if (OptAttributes != null)
2048 OptAttributes.Emit (ec, this);
2052 if (instance_constructors != null) {
2053 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) {
2054 bool has_compliant_args = false;
2056 foreach (Constructor c in instance_constructors) {
2059 if (has_compliant_args)
2062 has_compliant_args = c.HasCompliantArgs;
2064 if (!has_compliant_args)
2065 Report.Error (3015, Location, "'{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2067 foreach (Constructor c in instance_constructors)
2074 if (default_static_constructor != null)
2075 default_static_constructor.Emit ();
2077 if (methods != null)
2078 foreach (Method m in methods)
2081 if (operators != null)
2082 foreach (Operator o in operators)
2085 if (properties != null)
2086 foreach (Property p in properties)
2089 if (indexers != null){
2094 foreach (FieldMember f in fields)
2097 if (events != null){
2098 foreach (Event e in Events)
2102 if (delegates != null) {
2103 foreach (Delegate d in Delegates) {
2108 if (enums != null) {
2109 foreach (Enum e in enums) {
2114 if (parts != null) {
2115 foreach (ClassPart part in parts)
2119 if ((Pending != null) && !(this is ClassPart))
2120 if (Pending.VerifyPendingMethods ())
2123 if (iterators != null)
2124 foreach (Iterator iterator in iterators)
2125 iterator.EmitType ();
2127 // if (types != null)
2128 // foreach (TypeContainer tc in types)
2132 public override void CloseType ()
2134 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2138 caching_flags |= Flags.CloseTypeCreated;
2139 TypeBuilder.CreateType ();
2140 } catch (TypeLoadException){
2142 // This is fine, the code still created the type
2144 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2145 // Console.WriteLine (e.Message);
2147 Console.WriteLine ("In type: " + Name);
2152 foreach (Enum en in Enums)
2156 foreach (TypeContainer tc in Types)
2157 if (tc.Kind == Kind.Struct)
2160 foreach (TypeContainer tc in Types)
2161 if (tc.Kind != Kind.Struct)
2165 if (Delegates != null)
2166 foreach (Delegate d in Delegates)
2169 if (Iterators != null)
2170 foreach (Iterator i in Iterators)
2178 initialized_fields = null;
2179 initialized_static_fields = null;
2188 default_constructor = null;
2189 default_static_constructor = null;
2191 OptAttributes = null;
2194 member_cache = null;
2197 // TODO: make it obsolete and use GetSignatureForError
2198 public string MakeName (string n)
2200 return "`" + Name + "." + n + "'";
2204 // Performs the validation on a Method's modifiers (properties have
2205 // the same properties).
2207 public bool MethodModifiersValid (MemberCore mc)
2209 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2210 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2211 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2213 int flags = mc.ModFlags;
2216 // At most one of static, virtual or override
2218 if ((flags & Modifiers.STATIC) != 0){
2219 if ((flags & vao) != 0){
2220 Report.Error (112, mc.Location, "static method '{0}' can not be marked as virtual, abstract or override",
2221 GetSignatureForError ());
2226 if (Kind == Kind.Struct){
2227 if ((flags & va) != 0){
2228 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2233 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2234 Report.Error (113, mc.Location, "'{0}' marked as override cannot be marked as new or virtual", mc.GetSignatureForError ());
2239 // If the declaration includes the abstract modifier, then the
2240 // declaration does not include static, virtual or extern
2242 if ((flags & Modifiers.ABSTRACT) != 0){
2243 if ((flags & Modifiers.EXTERN) != 0){
2245 180, mc.Location, "'{0}' can not be both abstract and extern", mc.GetSignatureForError ());
2249 if ((flags & Modifiers.SEALED) != 0) {
2250 Report.Error (502, mc.Location, "'{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2254 if ((flags & Modifiers.VIRTUAL) != 0){
2255 Report.Error (503, mc.Location, "'{0}' can not be both abstract and virtual", mc.GetSignatureForError ());
2259 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2260 Report.Error (513, mc.Location, "'{0}' is abstract but its container class is not", mc.GetSignatureForError ());
2265 if ((flags & Modifiers.PRIVATE) != 0){
2266 if ((flags & vao) != 0){
2267 Report.Error (621, mc.Location, "'{0}' virtual or abstract members can not be private", mc.GetSignatureForError ());
2272 if ((flags & Modifiers.SEALED) != 0){
2273 if ((flags & Modifiers.OVERRIDE) == 0){
2274 Report.Error (238, mc.Location, "'{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2282 public bool UserDefinedStaticConstructor {
2284 return default_static_constructor != null;
2288 public Constructor DefaultStaticConstructor {
2289 get { return default_static_constructor; }
2292 protected override bool VerifyClsCompliance (DeclSpace ds)
2294 if (!base.VerifyClsCompliance (ds))
2299 Type base_type = TypeBuilder.BaseType;
2300 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2301 Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2304 if (!Parent.IsClsCompliaceRequired (ds)) {
2305 Report.Error (3018, Location, "'{0}' cannot be marked as CLS-Compliant because it is a member of non CLS-Compliant type '{1}'",
2306 GetSignatureForError (), Parent.GetSignatureForError ());
2313 /// Checks whether container name is CLS Compliant
2315 void VerifyClsName ()
2317 Hashtable base_members = base_cache == null ?
2319 base_cache.GetPublicMembers ();
2320 Hashtable this_members = new Hashtable ();
2322 foreach (DictionaryEntry entry in defined_names) {
2323 MemberCore mc = (MemberCore)entry.Value;
2324 if (!mc.IsClsCompliaceRequired (this))
2327 string name = (string)entry.Key;
2328 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2330 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2331 object found = base_members [lcase];
2332 if (found == null) {
2333 found = this_members [lcase];
2334 if (found == null) {
2335 this_members.Add (lcase, mc);
2340 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2343 if (found is MemberInfo) {
2344 if (basename == ((MemberInfo)found).Name)
2346 Report.SymbolRelatedToPreviousError ((MemberInfo)found);
2348 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2350 Report.Error (3005, mc.Location, "Identifier '{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2356 /// Performs checks for an explicit interface implementation. First it
2357 /// checks whether the `interface_type' is a base inteface implementation.
2358 /// Then it checks whether `name' exists in the interface type.
2360 public virtual bool VerifyImplements (Type interface_type, string full,
2361 string name, Location loc)
2365 if (ifaces != null){
2366 foreach (Type t in ifaces){
2367 if (t == interface_type){
2375 Report.Error (540, loc, "`{0}': containing class does not implement interface `{1}'",
2376 full, interface_type.FullName);
2383 protected override void VerifyObsoleteAttribute()
2385 CheckUsageOfObsoleteAttribute (ptype);
2390 foreach (Type iface in ifaces) {
2391 CheckUsageOfObsoleteAttribute (iface);
2400 string IMemberContainer.Name {
2406 Type IMemberContainer.Type {
2412 MemberCache IMemberContainer.MemberCache {
2414 return member_cache;
2418 bool IMemberContainer.IsInterface {
2420 return Kind == Kind.Interface;
2424 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2426 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
2430 // Generates xml doc comments (if any), and if required,
2431 // handle warning report.
2433 internal override void GenerateDocComment (DeclSpace ds)
2435 DocUtil.GenerateTypeDocComment (this, ds);
2438 public override string DocCommentHeader {
2439 get { return "T:"; }
2442 public virtual MemberCache BaseCache {
2444 if (base_cache != null)
2446 if (TypeBuilder.BaseType != null)
2447 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2448 if (TypeBuilder.IsInterface)
2449 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2455 public class PartialContainer : TypeContainer {
2457 public readonly Namespace Namespace;
2458 public readonly int OriginalModFlags;
2459 public readonly int AllowedModifiers;
2460 public readonly TypeAttributes DefaultTypeAttributes;
2462 static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
2463 MemberName member_name, int mod_flags, Kind kind,
2466 PartialContainer pc;
2467 DeclSpace ds = RootContext.Tree.GetDecl (member_name);
2469 pc = ds as PartialContainer;
2473 260, ds.Location, "Missing partial modifier " +
2474 "on declaration of type `{0}'; another " +
2475 "partial implementation of this type exists",
2476 member_name.GetPartialName());
2478 Report.LocationOfPreviousError (loc);
2482 if (pc.Kind != kind) {
2484 261, loc, "Partial declarations of `{0}' " +
2485 "must be all classes, all structs or " +
2486 "all interfaces", member_name.GetPartialName ());
2490 if (pc.OriginalModFlags != mod_flags) {
2492 262, loc, "Partial declarations of `{0}' " +
2493 "have conflicting accessibility modifiers",
2494 member_name.GetPartialName ());
2501 if (parent is ClassPart)
2502 parent = ((ClassPart) parent).PartialContainer;
2504 pc = new PartialContainer (ns.NS, parent, member_name, mod_flags, kind, loc);
2505 RootContext.Tree.RecordDecl (member_name, pc);
2507 if (kind == Kind.Interface)
2508 parent.AddInterface (pc);
2509 else if (kind == Kind.Class || kind == Kind.Struct)
2510 parent.AddClassOrStruct (pc);
2512 throw new InvalidOperationException ();
2517 public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
2518 MemberName name, int mod, Attributes attrs,
2519 Kind kind, Location loc)
2521 PartialContainer pc = Create (ns, parent, name, mod, kind, loc);
2523 // An error occured; create a dummy container, but don't
2525 pc = new PartialContainer (ns.NS, parent, name, mod, kind, loc);
2528 ClassPart part = new ClassPart (ns, pc, parent, mod, attrs, kind, loc);
2533 protected PartialContainer (Namespace ns, TypeContainer parent,
2534 MemberName name, int mod, Kind kind, Location l)
2535 : base (null, parent, name, null, kind, l)
2537 this.Namespace = ns;
2541 AllowedModifiers = Class.AllowedModifiers;
2542 DefaultTypeAttributes = Class.DefaultTypeAttributes;
2546 AllowedModifiers = Struct.AllowedModifiers;
2547 DefaultTypeAttributes = Struct.DefaultTypeAttributes;
2550 case Kind.Interface:
2551 AllowedModifiers = Interface.AllowedModifiers;
2552 DefaultTypeAttributes = Interface.DefaultTypeAttributes;
2556 throw new InvalidOperationException ();
2560 if (parent.Parent == null)
2561 accmods = Modifiers.INTERNAL;
2563 accmods = Modifiers.PRIVATE;
2565 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2566 this.OriginalModFlags = mod;
2569 public override PendingImplementation GetPendingImplementations ()
2571 return PendingImplementation.GetPendingImplementations (this);
2574 public override TypeAttributes TypeAttr {
2576 return base.TypeAttr | DefaultTypeAttributes;
2581 public class ClassPart : TypeContainer, IMemberContainer {
2582 public readonly PartialContainer PartialContainer;
2583 public readonly bool IsPartial;
2585 public ClassPart (NamespaceEntry ns, PartialContainer pc, TypeContainer parent,
2586 int mod, Attributes attrs, Kind kind, Location l)
2587 : base (ns, parent, pc.MemberName, attrs, kind, l)
2589 this.PartialContainer = pc;
2590 this.IsPartial = true;
2593 if (parent == null || parent == RootContext.Tree.Types)
2594 accmods = Modifiers.INTERNAL;
2596 accmods = Modifiers.PRIVATE;
2598 this.ModFlags = Modifiers.Check (pc.AllowedModifiers, mod, accmods, l);
2601 public override PendingImplementation GetPendingImplementations ()
2603 return PartialContainer.Pending;
2606 public override bool VerifyImplements (Type interface_type, string full,
2607 string name, Location loc)
2609 return PartialContainer.VerifyImplements (
2610 interface_type, full, name, loc);
2613 public override bool EmitFieldInitializers (EmitContext ec)
2615 return PartialContainer.EmitFieldInitializers (ec);
2618 public override Type FindNestedType (string name)
2620 return PartialContainer.FindNestedType (name);
2623 public override MemberCache BaseCache {
2625 return PartialContainer.BaseCache;
2630 public abstract class ClassOrStruct : TypeContainer {
2631 bool hasExplicitLayout = false;
2632 ListDictionary declarative_security;
2634 public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
2635 MemberName name, Attributes attrs, Kind kind,
2637 : base (ns, parent, name, attrs, kind, l)
2641 public override PendingImplementation GetPendingImplementations ()
2643 return PendingImplementation.GetPendingImplementations (this);
2646 public override bool HasExplicitLayout {
2648 return hasExplicitLayout;
2652 public override void VerifyMembers ()
2654 base.VerifyMembers ();
2656 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2657 foreach (Event e in events){
2659 Report.Warning (67, e.Location, "The event '{0}' is never used", e.GetSignatureForError ());
2664 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2666 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2667 if (declarative_security == null)
2668 declarative_security = new ListDictionary ();
2670 a.ExtractSecurityPermissionSet (declarative_security);
2674 if (a.Type == TypeManager.struct_layout_attribute_type &&
2675 a.GetLayoutKindValue () == LayoutKind.Explicit)
2676 hasExplicitLayout = true;
2678 base.ApplyAttributeBuilder (a, cb);
2681 public override void Emit()
2685 if (declarative_security != null) {
2686 foreach (DictionaryEntry de in declarative_security) {
2687 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2694 /// Class handles static classes declaration
2696 public sealed class StaticClass: Class {
2697 public StaticClass (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2698 Attributes attrs, Location l)
2699 : base (ns, parent, name, mod, attrs, l)
2701 if (RootContext.Version == LanguageVersion.ISO_1) {
2702 Report.FeatureIsNotStandardized (l, "static classes");
2706 protected override int AllowedModifiersProp {
2708 return Modifiers.NEW | Modifiers.PUBLIC | Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE |
2709 Modifiers.STATIC | Modifiers.UNSAFE;
2713 protected override void DefineContainerMembers (MemberCoreArrayList list)
2718 foreach (MemberCore m in list) {
2719 if (m is Operator) {
2720 Report.Error (715, m.Location, "'{0}': static classes cannot contain user-defined operators", m.GetSignatureForError (this));
2724 if ((m.ModFlags & Modifiers.PROTECTED) != 0)
2725 Report.Warning (628, 4, m.Location, "'{0}': new protected member declared in static class", m.GetSignatureForError (this));
2728 Report.Error (720, m.Location, "'{0}': cannot declare indexers in a static class", m.GetSignatureForError (this));
2732 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2735 if (m is Constructor) {
2736 Report.Error (710, m.Location, "'{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2740 if (m is Destructor) {
2741 Report.Error (711, m.Location, "'{0}': Static class cannot contain destructor", GetSignatureForError ());
2745 Report.Error (708, m.Location, "'{0}': cannot declare instance members in a static class", m.GetSignatureForError (this));
2748 base.DefineContainerMembers (list);
2751 public override TypeBuilder DefineType()
2753 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2754 Report.Error (441, Location, "'{0}': a class cannot be both static and sealed", GetSignatureForError ());
2758 TypeBuilder tb = base.DefineType ();
2762 if (ptype != TypeManager.object_type) {
2763 Report.Error (713, Location, "Static class '{0}' cannot derive from type '{1}'. Static classes must derive from object", GetSignatureForError (), TypeManager.CSharpName (ptype));
2767 if (ifaces != null) {
2768 foreach (Type t in ifaces)
2769 Report.SymbolRelatedToPreviousError (t);
2770 Report.Error (714, Location, "'{0}': static classes cannot implement interfaces", GetSignatureForError ());
2775 public override TypeAttributes TypeAttr {
2777 return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
2782 public class Class : ClassOrStruct {
2783 // TODO: remove this and use only AllowedModifiersProp to fix partial classes bugs
2784 public const int AllowedModifiers =
2787 Modifiers.PROTECTED |
2788 Modifiers.INTERNAL |
2790 Modifiers.ABSTRACT |
2794 public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2795 Attributes attrs, Location l)
2796 : base (ns, parent, name, attrs, Kind.Class, l)
2798 this.ModFlags = mod;
2801 virtual protected int AllowedModifiersProp {
2803 return AllowedModifiers;
2807 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2809 if (a.Type == TypeManager.attribute_usage_type) {
2810 if (ptype != TypeManager.attribute_type && !ptype.IsSubclassOf (TypeManager.attribute_type) &&
2811 TypeBuilder.FullName != "System.Attribute") {
2812 Report.Error (641, a.Location, "Attribute '{0}' is only valid on classes derived from System.Attribute", a.Name);
2816 if (a.Type == TypeManager.conditional_attribute_type &&
2817 !(ptype == TypeManager.attribute_type || ptype.IsSubclassOf (TypeManager.attribute_type))) {
2818 Report.Error (1689, a.Location, "Attribute 'System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2822 if (AttributeTester.IsAttributeExcluded (a.Type))
2825 base.ApplyAttributeBuilder (a, cb);
2828 public const TypeAttributes DefaultTypeAttributes =
2829 TypeAttributes.AutoLayout | TypeAttributes.Class;
2831 public override TypeBuilder DefineType()
2833 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2834 Report.Error (418, Location, "'{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2838 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2839 ModFlags = Modifiers.Check (AllowedModifiersProp, ModFlags, accmods, Location);
2841 return base.DefineType ();
2844 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2845 /// Valid only for attribute classes.
2846 public bool IsExcluded ()
2848 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2849 return (caching_flags & Flags.Excluded) != 0;
2851 caching_flags &= ~Flags.Excluded_Undetected;
2853 if (OptAttributes == null)
2856 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
2861 foreach (Attribute a in attrs) {
2862 string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
2863 if (RootContext.AllDefines.Contains (condition))
2867 caching_flags |= Flags.Excluded;
2872 // FIXME: How do we deal with the user specifying a different
2875 public override TypeAttributes TypeAttr {
2877 return base.TypeAttr | DefaultTypeAttributes;
2882 public class Struct : ClassOrStruct {
2884 // Modifiers allowed in a struct declaration
2886 public const int AllowedModifiers =
2889 Modifiers.PROTECTED |
2890 Modifiers.INTERNAL |
2894 public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
2895 int mod, Attributes attrs, Location l)
2896 : base (ns, parent, name, attrs, Kind.Struct, l)
2900 if (parent.Parent == null)
2901 accmods = Modifiers.INTERNAL;
2903 accmods = Modifiers.PRIVATE;
2905 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2907 this.ModFlags |= Modifiers.SEALED;
2910 public const TypeAttributes DefaultTypeAttributes =
2911 TypeAttributes.SequentialLayout |
2912 TypeAttributes.Sealed |
2913 TypeAttributes.BeforeFieldInit;
2916 // FIXME: Allow the user to specify a different set of attributes
2917 // in some cases (Sealed for example is mandatory for a class,
2918 // but what SequentialLayout can be changed
2920 public override TypeAttributes TypeAttr {
2922 return base.TypeAttr | DefaultTypeAttributes;
2930 public class Interface : TypeContainer, IMemberContainer {
2932 /// Modifiers allowed in a class declaration
2934 public const int AllowedModifiers =
2937 Modifiers.PROTECTED |
2938 Modifiers.INTERNAL |
2942 public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2943 Attributes attrs, Location l)
2944 : base (ns, parent, name, attrs, Kind.Interface, l)
2948 if (parent.Parent == null)
2949 accmods = Modifiers.INTERNAL;
2951 accmods = Modifiers.PRIVATE;
2953 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2956 public override PendingImplementation GetPendingImplementations ()
2961 public const TypeAttributes DefaultTypeAttributes =
2962 TypeAttributes.AutoLayout |
2963 TypeAttributes.Abstract |
2964 TypeAttributes.Interface;
2966 public override TypeAttributes TypeAttr {
2968 return base.TypeAttr | DefaultTypeAttributes;
2973 public abstract class MethodCore : MemberBase {
2974 public readonly Parameters Parameters;
2975 protected ToplevelBlock block;
2978 // Parameters, cached for semantic analysis.
2980 protected InternalParameters parameter_info;
2981 protected Type [] parameter_types;
2983 // Whether this is an operator method.
2984 public bool IsOperator;
2987 // The method we're overriding if this is an override method.
2989 protected MethodInfo base_method = null;
2991 static string[] attribute_targets = new string [] { "method", "return" };
2993 public MethodCore (TypeContainer parent, Expression type, int mod,
2994 int allowed_mod, bool is_interface, MemberName name,
2995 Attributes attrs, Parameters parameters, Location loc)
2996 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
2999 Parameters = parameters;
3000 IsInterface = is_interface;
3004 // Returns the System.Type array for the parameters of this method
3006 public Type [] ParameterTypes {
3008 return parameter_types;
3012 public InternalParameters ParameterInfo
3015 return parameter_info;
3019 public ToplevelBlock Block {
3029 protected override bool CheckBase ()
3031 if (!base.CheckBase ())
3034 // Check whether arguments were correct.
3035 if (!DoDefineParameters ())
3038 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3044 // Is null for System.Object while compiling corlib and base interfaces
3045 if (Parent.BaseCache == null) {
3046 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3047 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3052 Type base_ret_type = null;
3053 base_method = FindOutBaseMethod (Parent, ref base_ret_type);
3055 // method is override
3056 if (base_method != null) {
3058 if (!CheckMethodAgainstBase ())
3061 if ((ModFlags & Modifiers.NEW) == 0) {
3062 if (MemberType != TypeManager.TypeToCoreType (base_ret_type)) {
3063 Report.SymbolRelatedToPreviousError (base_method);
3064 if (this is PropertyBase) {
3065 Report.Error (1715, Location, "'{0}': type must be '{1}' to match overridden member '{2}'",
3066 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3069 Report.Error (508, Location, GetSignatureForError (Parent) + ": cannot " +
3070 "change return type when overriding inherited member");
3075 if (base_method.IsAbstract && !IsInterface) {
3076 Report.SymbolRelatedToPreviousError (base_method);
3077 Report.Error (533, Location, "'{0}' hides inherited abstract member", GetSignatureForError (Parent));
3082 if (base_method.IsSpecialName && !(this is PropertyBase)) {
3083 Report.Error (561, Location, "'{0}': cannot override '{1}' because it is a special compiler-generated method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (base_method));
3087 if (RootContext.WarningLevel > 2) {
3088 if (Name == "Equals" && parameter_types.Length == 1 && parameter_types [0] == TypeManager.object_type)
3089 Parent.Methods.HasEquals = true;
3090 else if (Name == "GetHashCode" && parameter_types.Length == 0)
3091 Parent.Methods.HasGetHashCode = true;
3094 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3095 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3097 EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
3098 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
3099 Report.SymbolRelatedToPreviousError (base_method);
3100 Report.Warning (672, 1, Location, "Member '{0}' overrides obsolete member. Add the Obsolete attribute to '{0}'", GetSignatureForError (Parent));
3107 MemberInfo conflict_symbol = Parent.FindBaseMemberWithSameName (Name, !(this is Property));
3108 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3109 if (conflict_symbol != null) {
3110 Report.SymbolRelatedToPreviousError (conflict_symbol);
3111 if (this is PropertyBase)
3112 Report.Error (544, Location, "'{0}': cannot override because '{1}' is not a property", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
3114 Report.Error (505, Location, "'{0}': cannot override because '{1}' is not a method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
3116 Report.Error (115, Location, "'{0}': no suitable methods found to override", GetSignatureForError (Parent));
3120 if (conflict_symbol == null) {
3121 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3122 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3127 if ((ModFlags & Modifiers.NEW) == 0) {
3128 if (this is Method && conflict_symbol is MethodBase)
3131 Report.SymbolRelatedToPreviousError (conflict_symbol);
3132 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3140 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3141 // that have been defined.
3143 // `name' is the user visible name for reporting errors (this is used to
3144 // provide the right name regarding method names and properties)
3146 bool CheckMethodAgainstBase ()
3150 // TODO: replace with GetSignatureForError
3151 string name = base_method.DeclaringType.Name + "." + base_method.Name;
3153 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3154 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3156 506, Location, Parent.MakeName (Name) +
3157 ": cannot override inherited member `" +
3158 name + "' because it is not " +
3159 "virtual, abstract or override");
3163 // Now we check that the overriden method is not final
3165 if (base_method.IsFinal) {
3166 Report.SymbolRelatedToPreviousError (base_method);
3167 Report.Error (239, Location, "'{0}': cannot override inherited member '{1}' because it is sealed",
3168 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3172 // Check that the permissions are not being changed
3174 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3175 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3177 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3178 Error_CannotChangeAccessModifiers (Parent, base_method, name);
3183 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3184 ModFlags |= Modifiers.NEW;
3185 Report.SymbolRelatedToPreviousError (base_method);
3186 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3187 if (RootContext.WarningLevel >= 2)
3188 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), TypeManager.CSharpSignature (base_method));
3190 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3196 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3198 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3200 // when overriding protected internal, the method can be declared
3201 // protected internal only within the same assembly
3204 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3205 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3207 // assemblies differ - report an error
3211 } else if (thisp != base_classp) {
3213 // same assembly, but other attributes differ - report an error
3218 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3220 // if it's not "protected internal", it must be "protected"
3224 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3226 // protected within the same assembly - an error
3229 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3230 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3232 // protected ok, but other attributes differ - report an error
3238 return (thisp == base_classp);
3242 void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo base_method, string name)
3245 // FIXME: report the old/new permissions?
3248 507, Location, parent.MakeName (Name) +
3249 ": can't change the access modifiers when overriding inherited " +
3250 "member `" + name + "'");
3253 protected static string Error722 {
3255 return "'{0}': static types cannot be used as return types";
3260 /// For custom member duplication search in a container
3262 protected abstract bool CheckForDuplications ();
3265 /// Gets base method and its return type
3267 protected abstract MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type);
3269 protected virtual bool DoDefineParameters ()
3271 EmitContext ec = Parent.EmitContext;
3273 throw new InternalErrorException ("DoDefineParameters invoked too early");
3275 bool old_unsafe = ec.InUnsafe;
3276 ec.InUnsafe = InUnsafe;
3277 // Check if arguments were correct
3278 parameter_types = Parameters.GetParameterInfo (ec);
3279 ec.InUnsafe = old_unsafe;
3281 if ((parameter_types == null) ||
3282 !CheckParameters (Parent, parameter_types))
3285 parameter_info = new InternalParameters (parameter_types, Parameters);
3287 Parameter array_param = Parameters.ArrayParameter;
3288 if ((array_param != null) &&
3289 (!array_param.ParameterType.IsArray ||
3290 (array_param.ParameterType.GetArrayRank () != 1))) {
3291 Report.Error (225, Location, "params parameter has to be a single dimensional array");
3298 public override string[] ValidAttributeTargets {
3300 return attribute_targets;
3304 protected override bool VerifyClsCompliance (DeclSpace ds)
3306 if (!base.VerifyClsCompliance (ds)) {
3307 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
3308 Report.Error (3011, Location, "'{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3313 if (Parameters.HasArglist) {
3314 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3317 if (!AttributeTester.IsClsCompliant (MemberType)) {
3318 if (this is PropertyBase)
3319 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3320 GetSignatureForError ());
3322 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant",
3323 GetSignatureForError ());
3326 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
3331 protected bool IsDuplicateImplementation (MethodCore method)
3333 if ((method == this) || (method.Name != Name))
3336 Type[] param_types = method.ParameterTypes;
3337 if (param_types == null)
3340 if (param_types.Length != ParameterTypes.Length)
3343 for (int i = 0; i < param_types.Length; i++)
3344 if (param_types [i] != ParameterTypes [i])
3347 // TODO: make operator compatible with MethodCore to avoid this
3348 if (this is Operator && method is Operator) {
3349 if (MemberType != method.MemberType)
3354 // Try to report 663: method only differs on out/ref
3356 ParameterData info = ParameterInfo;
3357 ParameterData other_info = method.ParameterInfo;
3358 for (int i = 0; i < info.Count; i++){
3359 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3360 Report.Error (663, Location, "Overload method only differs in parameter modifier");
3365 Report.SymbolRelatedToPreviousError (method);
3366 if (this is Operator && method is Operator)
3367 Report.Error (557, Location, "Duplicate user-defined conversion in type '{0}'", Parent.Name);
3369 Report.Error (111, Location, TypeContainer.Error111, Parent.Name, Name);
3373 public override bool IsUsed
3384 // Returns a string that represents the signature for this
3385 // member which should be used in XML documentation.
3387 public override string GetDocCommentName (DeclSpace ds)
3389 return DocUtil.GetMethodDocCommentName (this, ds);
3393 // Raised (and passed an XmlElement that contains the comment)
3394 // when GenerateDocComment is writing documentation expectedly.
3396 // FIXME: with a few effort, it could be done with XmlReader,
3397 // that means removal of DOM use.
3399 internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
3401 DocUtil.OnMethodGenerateDocComment (this, ds, el);
3405 // Represents header string for documentation comment.
3407 public override string DocCommentHeader {
3408 get { return "M:"; }
3411 protected override void VerifyObsoleteAttribute()
3413 base.VerifyObsoleteAttribute ();
3415 if (parameter_types == null)
3418 foreach (Type type in parameter_types) {
3419 CheckUsageOfObsoleteAttribute (type);
3424 public class SourceMethod : ISourceMethod
3426 TypeContainer container;
3429 protected SourceMethod (TypeContainer container, MethodBase builder,
3430 ISourceFile file, Location start, Location end)
3432 this.container = container;
3433 this.builder = builder;
3435 CodeGen.SymbolWriter.OpenMethod (
3436 file, this, start.Row, 0, end.Row, 0);
3439 public string Name {
3440 get { return builder.Name; }
3443 public int NamespaceID {
3444 get { return container.NamespaceEntry.SymbolFileID; }
3449 if (builder is MethodBuilder)
3450 return ((MethodBuilder) builder).GetToken ().Token;
3451 else if (builder is ConstructorBuilder)
3452 return ((ConstructorBuilder) builder).GetToken ().Token;
3454 throw new NotSupportedException ();
3458 public void CloseMethod ()
3460 if (CodeGen.SymbolWriter != null)
3461 CodeGen.SymbolWriter.CloseMethod ();
3464 public static SourceMethod Create (TypeContainer parent,
3465 MethodBase builder, Block block)
3467 if (CodeGen.SymbolWriter == null)
3472 Location start_loc = block.StartLocation;
3473 if (Location.IsNull (start_loc))
3476 Location end_loc = block.EndLocation;
3477 if (Location.IsNull (end_loc))
3480 ISourceFile file = start_loc.SourceFile;
3484 return new SourceMethod (
3485 parent, builder, file, start_loc, end_loc);
3489 public class Method : MethodCore, IIteratorContainer, IMethodData {
3490 public MethodBuilder MethodBuilder;
3491 public MethodData MethodData;
3492 ReturnParameter return_attributes;
3493 ListDictionary declarative_security;
3496 /// Modifiers allowed in a class declaration
3498 const int AllowedModifiers =
3501 Modifiers.PROTECTED |
3502 Modifiers.INTERNAL |
3507 Modifiers.OVERRIDE |
3508 Modifiers.ABSTRACT |
3510 Modifiers.METHOD_YIELDS |
3513 const int AllowedInterfaceModifiers =
3514 Modifiers.NEW | Modifiers.UNSAFE;
3517 // return_type can be "null" for VOID values.
3519 public Method (TypeContainer ds, Expression return_type, int mod, bool is_iface,
3520 MemberName name, Parameters parameters, Attributes attrs,
3522 : base (ds, return_type, mod,
3523 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3524 is_iface, name, attrs, parameters, l)
3528 public override AttributeTargets AttributeTargets {
3530 return AttributeTargets.Method;
3534 public override string GetSignatureForError()
3536 if (MethodBuilder == null) {
3537 return GetSignatureForError (Parent);
3539 return TypeManager.CSharpSignature (MethodBuilder);
3543 /// Use this method when MethodBuilder is null
3545 public override string GetSignatureForError (TypeContainer tc)
3547 // TODO: get params from somewhere
3548 if (parameter_info == null)
3549 return base.GetSignatureForError (tc);
3551 // TODO: move to parameters
3552 System.Text.StringBuilder args = new System.Text.StringBuilder ();
3553 if (parameter_info.Parameters.FixedParameters != null) {
3554 for (int i = 0; i < parameter_info.Parameters.FixedParameters.Length; ++i) {
3555 Parameter p = parameter_info.Parameters.FixedParameters [i];
3556 args.Append (p.GetSignatureForError ());
3558 if (i < parameter_info.Parameters.FixedParameters.Length - 1)
3563 return String.Concat (base.GetSignatureForError (tc), "(", args.ToString (), ")");
3566 void DuplicateEntryPoint (MethodInfo b, Location location)
3570 "Program `" + CodeGen.FileName +
3571 "' has more than one entry point defined: `" +
3572 TypeManager.CSharpSignature(b) + "'");
3575 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
3577 if (b.ReturnType != TypeManager.void_type &&
3578 b.ReturnType != TypeManager.int32_type)
3581 if (pinfo.Count == 0)
3584 if (pinfo.Count > 1)
3587 Type t = pinfo.ParameterType(0);
3589 (t.GetArrayRank() == 1) &&
3590 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3591 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3597 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3599 if (a.Target == AttributeTargets.ReturnValue) {
3600 if (return_attributes == null)
3601 return_attributes = new ReturnParameter (MethodBuilder, Location);
3603 return_attributes.ApplyAttributeBuilder (a, cb);
3607 if (a.Type == TypeManager.methodimpl_attr_type &&
3608 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
3609 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3612 if (a.Type == TypeManager.dllimport_type) {
3613 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3614 if ((ModFlags & extern_static) != extern_static) {
3615 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3621 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3622 if (declarative_security == null)
3623 declarative_security = new ListDictionary ();
3624 a.ExtractSecurityPermissionSet (declarative_security);
3628 if (a.Type == TypeManager.conditional_attribute_type) {
3629 if (IsOperator || IsExplicitImpl) {
3630 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
3634 if (ReturnType != TypeManager.void_type) {
3635 Report.Error (578, Location, "Conditional not valid on '{0}' because its return type is not void", GetSignatureForError ());
3639 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3640 Report.Error (243, Location, "Conditional not valid on '{0}' because it is an override method", GetSignatureForError ());
3645 Report.Error (582, Location, "Conditional not valid on interface members");
3649 if (MethodData.IsImplementing) {
3650 Report.Error (629, Location, "Conditional member '{0}' cannot implement interface member", GetSignatureForError ());
3654 for (int i = 0; i < parameter_info.Count; ++i) {
3655 if ((parameter_info.ParameterModifier (i) & Parameter.Modifier.OUT) != 0) {
3656 Report.Error (685, Location, "Conditional method '{0}' cannot have an out parameter", GetSignatureForError ());
3662 MethodBuilder.SetCustomAttribute (cb);
3665 protected override bool CheckForDuplications ()
3667 ArrayList ar = Parent.Methods;
3669 int arLen = ar.Count;
3671 for (int i = 0; i < arLen; i++) {
3672 Method m = (Method) ar [i];
3673 if (IsDuplicateImplementation (m))
3678 ar = Parent.Properties;
3680 for (int i = 0; i < ar.Count; ++i) {
3681 PropertyBase pb = (PropertyBase) ar [i];
3682 if (pb.AreAccessorsDuplicateImplementation (this))
3687 ar = Parent.Indexers;
3689 for (int i = 0; i < ar.Count; ++i) {
3690 PropertyBase pb = (PropertyBase) ar [i];
3691 if (pb.AreAccessorsDuplicateImplementation (this))
3698 for (int i = 0; i < ar.Count; ++i) {
3699 Event ev = (Event) ar [i];
3700 if (ev.AreAccessorsDuplicateImplementation (this))
3711 public override bool Define ()
3716 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
3717 Error1599 (Location, ReturnType);
3725 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
3727 MethodData = new MethodData (this, ParameterInfo, ModFlags, flags, this);
3729 if (!MethodData.Define (Parent))
3733 // Setup iterator if we are one
3735 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3736 Iterator iterator = new Iterator (
3737 Parent, Name, MemberType,
3738 ParameterInfo, ModFlags, block, Location);
3740 if (!iterator.DefineIterator ())
3743 block = iterator.Block;
3746 MethodBuilder = MethodData.MethodBuilder;
3749 // This is used to track the Entry Point,
3751 if (Name == "Main" &&
3752 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3753 (RootContext.MainClass == null ||
3754 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3755 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3756 IMethodData md = TypeManager.GetMethod (MethodBuilder);
3757 md.SetMemberIsUsed ();
3759 if (RootContext.EntryPoint == null) {
3760 RootContext.EntryPoint = MethodBuilder;
3761 RootContext.EntryPointLocation = Location;
3763 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3764 DuplicateEntryPoint (MethodBuilder, Location);
3767 if (RootContext.WarningLevel >= 4)
3768 Report.Warning (28, Location, "'{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder));
3772 if (MemberType.IsAbstract && MemberType.IsSealed) {
3773 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3783 public override void Emit ()
3785 MethodData.Emit (Parent, this);
3788 if (declarative_security != null) {
3789 foreach (DictionaryEntry de in declarative_security) {
3790 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3798 public static void Error1599 (Location loc, Type t)
3800 Report.Error (1599, loc, "Method or delegate cannot return type '{0}'", TypeManager.CSharpName (t));
3803 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
3805 MethodInfo mi = (MethodInfo) container.BaseCache.FindMemberToOverride (
3806 container.TypeBuilder, Name, ParameterTypes, false);
3811 base_ret_type = mi.ReturnType;
3815 public override bool MarkForDuplicationCheck ()
3817 caching_flags |= Flags.TestMethodDuplication;
3821 protected override bool VerifyClsCompliance(DeclSpace ds)
3823 if (!base.VerifyClsCompliance (ds))
3826 if (parameter_types.Length > 0) {
3827 ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
3829 ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
3836 void IIteratorContainer.SetYields ()
3838 ModFlags |= Modifiers.METHOD_YIELDS;
3841 #region IMethodData Members
3843 public CallingConventions CallingConventions {
3845 CallingConventions cc = Parameters.GetCallingConvention ();
3846 if (Parameters.HasArglist)
3847 block.HasVarargs = true;
3850 if ((ModFlags & Modifiers.STATIC) == 0)
3851 cc |= CallingConventions.HasThis;
3853 // FIXME: How is `ExplicitThis' used in C#?
3859 public Type ReturnType {
3865 public MemberName MethodName {
3871 public new Location Location {
3873 return base.Location;
3877 protected override bool CheckBase() {
3878 if (!base.CheckBase ())
3881 // TODO: Destructor should derive from MethodCore
3882 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3883 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3884 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3891 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
3893 return new EmitContext (
3894 tc, Parent, Location, ig, ReturnType, ModFlags, false);
3897 public ObsoleteAttribute GetObsoleteAttribute ()
3899 return GetObsoleteAttribute (Parent);
3903 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3905 public bool IsExcluded (EmitContext ec)
3907 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3908 return (caching_flags & Flags.Excluded) != 0;
3910 caching_flags &= ~Flags.Excluded_Undetected;
3912 if (base_method == null) {
3913 if (OptAttributes == null)
3916 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
3921 foreach (Attribute a in attrs) {
3922 string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
3923 if (RootContext.AllDefines.Contains (condition))
3927 caching_flags |= Flags.Excluded;
3931 IMethodData md = TypeManager.GetMethod (base_method);
3933 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
3934 caching_flags |= Flags.Excluded;
3940 if (md.IsExcluded (ec)) {
3941 caching_flags |= Flags.Excluded;
3950 public abstract class ConstructorInitializer {
3951 ArrayList argument_list;
3952 protected ConstructorInfo base_constructor;
3953 Parameters parameters;
3956 public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
3959 this.argument_list = argument_list;
3960 this.parameters = parameters;
3964 public ArrayList Arguments {
3966 return argument_list;
3970 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
3972 Expression base_constructor_group;
3976 ec.CurrentBlock = new ToplevelBlock (Block.Flags.Implicit, parameters, loc);
3978 if (argument_list != null){
3979 foreach (Argument a in argument_list){
3980 if (!a.Resolve (ec, loc))
3984 ec.CurrentBlock = null;
3986 if (this is ConstructorBaseInitializer) {
3987 if (ec.ContainerType.BaseType == null)
3990 t = ec.ContainerType.BaseType;
3991 if (ec.ContainerType.IsValueType) {
3992 Report.Error (522, loc,
3993 "structs cannot call base class constructors");
3997 t = ec.ContainerType;
3999 base_constructor_group = Expression.MemberLookup (
4000 ec, t, ".ctor", MemberTypes.Constructor,
4001 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4004 if (base_constructor_group == null){
4006 base_constructor_group = Expression.MemberLookup (
4007 ec, t, null, t, ".ctor", MemberTypes.Constructor,
4008 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4012 int errors = Report.Errors;
4013 if (base_constructor_group != null)
4014 base_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4015 ec, (MethodGroupExpr) base_constructor_group, argument_list,
4018 if (base_constructor == null) {
4019 if (errors == Report.Errors)
4020 Report.Error (1501, loc, "Can not find a constructor for this argument list");
4025 Report.Error (122, loc, "`{0}' is inaccessible due to its protection level",
4026 TypeManager.CSharpSignature (base_constructor));
4027 base_constructor = null;
4031 if (base_constructor == caller_builder){
4032 Report.Error (516, loc, "Constructor `{0}' can not call itself", TypeManager.CSharpSignature (caller_builder));
4039 public void Emit (EmitContext ec)
4041 if (base_constructor != null){
4042 ec.Mark (loc, false);
4044 Invocation.EmitCall (ec, true, true, null, base_constructor, argument_list, loc);
4046 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), base_constructor, argument_list, loc);
4051 /// Method search for base ctor. (We do not cache it).
4053 Constructor GetOverloadedConstructor (TypeContainer tc)
4055 if (tc.InstanceConstructors == null)
4058 foreach (Constructor c in tc.InstanceConstructors) {
4059 if (Arguments == null) {
4060 if (c.ParameterTypes.Length == 0)
4068 int count = c.ParameterInfo.Count;
4070 c.ParameterInfo.ParameterModifier (count - 1) == Parameter.Modifier.PARAMS) {
4071 for (int i = 0; i < count-1; i++)
4072 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4077 if (c.ParameterTypes.Length != Arguments.Count)
4080 for (int i = 0; i < Arguments.Count; ++i)
4081 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4096 //TODO: implement caching when it will be necessary
4097 public virtual void CheckObsoleteAttribute (TypeContainer tc, Location loc)
4099 Constructor ctor = GetOverloadedConstructor (tc);
4103 ObsoleteAttribute oa = ctor.GetObsoleteAttribute (tc);
4107 AttributeTester.Report_ObsoleteMessage (oa, ctor.GetSignatureForError (), loc);
4111 public class ConstructorBaseInitializer : ConstructorInitializer {
4112 public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
4113 base (argument_list, pars, l)
4117 public override void CheckObsoleteAttribute(TypeContainer tc, Location loc) {
4118 if (base_constructor == null)
4121 TypeContainer type_ds = TypeManager.LookupTypeContainer (tc.TypeBuilder.BaseType);
4122 if (type_ds == null) {
4123 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (base_constructor);
4126 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (base_constructor), loc);
4131 base.CheckObsoleteAttribute (type_ds, loc);
4136 public class ConstructorThisInitializer : ConstructorInitializer {
4137 public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
4138 base (argument_list, pars, l)
4143 public class Constructor : MethodCore, IMethodData {
4144 public ConstructorBuilder ConstructorBuilder;
4145 public ConstructorInitializer Initializer;
4146 ListDictionary declarative_security;
4149 // Modifiers allowed for a constructor.
4151 public const int AllowedModifiers =
4153 Modifiers.PROTECTED |
4154 Modifiers.INTERNAL |
4160 bool has_compliant_args = false;
4162 // The spec claims that static is not permitted, but
4163 // my very own code has static constructors.
4165 public Constructor (TypeContainer ds, string name, int mod, Parameters args,
4166 ConstructorInitializer init, Location l)
4167 : base (ds, null, mod, AllowedModifiers, false, new MemberName (name),
4173 public override string GetSignatureForError()
4175 if (ConstructorBuilder == null)
4176 return GetSignatureForError (Parent);
4178 return TypeManager.CSharpSignature (ConstructorBuilder);
4181 public bool HasCompliantArgs {
4183 return has_compliant_args;
4187 public override AttributeTargets AttributeTargets {
4189 return AttributeTargets.Constructor;
4195 // Returns true if this is a default constructor
4197 public bool IsDefault ()
4199 if ((ModFlags & Modifiers.STATIC) != 0)
4200 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4201 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
4204 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4205 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
4206 (Initializer is ConstructorBaseInitializer) &&
4207 (Initializer.Arguments == null);
4210 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4212 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4213 if (declarative_security == null) {
4214 declarative_security = new ListDictionary ();
4216 a.ExtractSecurityPermissionSet (declarative_security);
4220 ConstructorBuilder.SetCustomAttribute (cb);
4223 protected override bool CheckForDuplications ()
4225 ArrayList ar = Parent.InstanceConstructors;
4227 int arLen = ar.Count;
4229 for (int i = 0; i < arLen; i++) {
4230 Constructor m = (Constructor) ar [i];
4231 if (IsDuplicateImplementation (m))
4238 protected override bool CheckBase ()
4240 // Check whether arguments were correct.
4241 if (!DoDefineParameters ())
4244 // TODO: skip the rest for generated ctor
4245 if ((ModFlags & Modifiers.STATIC) != 0)
4248 if (!CheckForDuplications ())
4251 if (Parent.Kind == Kind.Struct) {
4252 if (ParameterTypes.Length == 0) {
4253 Report.Error (568, Location,
4254 "Structs can not contain explicit parameterless " +
4259 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4260 Report.Error (666, Location, "Protected member in struct declaration");
4265 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4266 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
4273 // Creates the ConstructorBuilder
4275 public override bool Define ()
4277 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4278 MethodAttributes.SpecialName);
4280 if ((ModFlags & Modifiers.STATIC) != 0) {
4281 ca |= MethodAttributes.Static | MethodAttributes.Private;
4283 ca |= MethodAttributes.HideBySig;
4285 if ((ModFlags & Modifiers.PUBLIC) != 0)
4286 ca |= MethodAttributes.Public;
4287 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4288 if ((ModFlags & Modifiers.INTERNAL) != 0)
4289 ca |= MethodAttributes.FamORAssem;
4291 ca |= MethodAttributes.Family;
4292 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4293 ca |= MethodAttributes.Assembly;
4294 else if (IsDefault ())
4295 ca |= MethodAttributes.Public;
4297 ca |= MethodAttributes.Private;
4300 // Check if arguments were correct.
4304 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4305 ca, CallingConventions,
4308 if ((ModFlags & Modifiers.UNSAFE) != 0)
4309 ConstructorBuilder.InitLocals = false;
4311 TypeManager.AddMethod (ConstructorBuilder, this);
4314 // HACK because System.Reflection.Emit is lame
4316 TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes);
4324 public override void Emit ()
4326 EmitContext ec = CreateEmitContext (null, null);
4329 // extern methods have no bodies
4331 if ((ModFlags & Modifiers.EXTERN) != 0) {
4332 if ((block != null) && ((ModFlags & Modifiers.EXTERN) != 0)) {
4334 179, Location, "External constructor `" +
4335 TypeManager.CSharpSignature (ConstructorBuilder) +
4336 "' can not have a body");
4339 } else if (block == null) {
4341 501, Location, "Constructor `" +
4342 TypeManager.CSharpSignature (ConstructorBuilder) +
4343 "' must declare a body since it is not marked extern");
4347 if ((ModFlags & Modifiers.STATIC) == 0){
4348 if (Parent.Kind == Kind.Class && Initializer == null)
4349 Initializer = new ConstructorBaseInitializer (
4350 null, Parameters.EmptyReadOnlyParameters, Location);
4354 // Spec mandates that Initializers will not have
4358 if (Initializer != null && !Initializer.Resolve (ConstructorBuilder, ec))
4360 ec.IsStatic = false;
4363 Parameters.LabelParameters (ec, ConstructorBuilder, Location);
4365 SourceMethod source = SourceMethod.Create (
4366 Parent, ConstructorBuilder, block);
4369 // Classes can have base initializers and instance field initializers.
4371 if (Parent.Kind == Kind.Class){
4372 if ((ModFlags & Modifiers.STATIC) == 0){
4375 // If we use a "this (...)" constructor initializer, then
4376 // do not emit field initializers, they are initialized in the other constructor
4378 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4379 Parent.EmitFieldInitializers (ec);
4382 if (Initializer != null) {
4383 if (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent) == null)
4384 Initializer.CheckObsoleteAttribute (Parent, Location);
4386 ec.TestObsoleteMethodUsage = false;
4387 Initializer.Emit (ec);
4390 if ((ModFlags & Modifiers.STATIC) != 0)
4391 Parent.EmitFieldInitializers (ec);
4393 if (OptAttributes != null)
4394 OptAttributes.Emit (ec, this);
4396 // If this is a non-static `struct' constructor and doesn't have any
4397 // initializer, it must initialize all of the struct's fields.
4398 if ((Parent.Kind == Kind.Struct) &&
4399 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4400 Block.AddThisVariable (Parent, Location);
4402 ec.EmitTopBlock (block, ParameterInfo, Location);
4405 source.CloseMethod ();
4409 if (declarative_security != null) {
4410 foreach (DictionaryEntry de in declarative_security) {
4411 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4418 // Is never override
4419 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
4424 protected override bool VerifyClsCompliance (DeclSpace ds)
4426 if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
4430 if (parameter_types.Length > 0) {
4431 ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
4433 ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4435 if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4436 foreach (Type param in parameter_types) {
4437 if (param.IsArray) {
4443 has_compliant_args = true;
4447 #region IMethodData Members
4449 public System.Reflection.CallingConventions CallingConventions {
4451 CallingConventions cc = Parameters.GetCallingConvention ();
4453 if (Parent.Kind == Kind.Class)
4454 if ((ModFlags & Modifiers.STATIC) == 0)
4455 cc |= CallingConventions.HasThis;
4457 // FIXME: How is `ExplicitThis' used in C#?
4463 public new Location Location {
4465 return base.Location;
4469 public MemberName MethodName {
4475 public Type ReturnType {
4481 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4483 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4484 return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
4487 public ObsoleteAttribute GetObsoleteAttribute ()
4492 public bool IsExcluded(EmitContext ec)
4501 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4503 public interface IMethodData
4505 CallingConventions CallingConventions { get; }
4506 Location Location { get; }
4507 MemberName MethodName { get; }
4508 Type[] ParameterTypes { get; }
4509 Type ReturnType { get; }
4511 Attributes OptAttributes { get; }
4512 ToplevelBlock Block { get; }
4514 EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
4515 ObsoleteAttribute GetObsoleteAttribute ();
4516 string GetSignatureForError (TypeContainer tc);
4517 bool IsExcluded (EmitContext ec);
4518 bool IsClsCompliaceRequired (DeclSpace ds);
4519 void SetMemberIsUsed ();
4523 // Encapsulates most of the Method's state
4525 public class MethodData {
4527 readonly IMethodData method;
4530 // The return type of this method
4532 public readonly InternalParameters ParameterInfo;
4535 // Are we implementing an interface ?
4537 public bool IsImplementing = false;
4542 protected MemberBase member;
4543 protected int modifiers;
4544 protected MethodAttributes flags;
4546 MethodBuilder builder = null;
4547 public MethodBuilder MethodBuilder {
4553 public MethodData (MemberBase member, InternalParameters parameters,
4554 int modifiers, MethodAttributes flags, IMethodData method)
4556 this.member = member;
4557 this.ParameterInfo = parameters;
4558 this.modifiers = modifiers;
4561 this.method = method;
4564 public bool Define (TypeContainer container)
4566 MethodInfo implementing = null;
4568 string name = method.MethodName.Name;
4569 string method_name = name;
4571 Type[] ParameterTypes = method.ParameterTypes;
4573 if (container.Pending != null){
4574 if (member is Indexer) // TODO: test it, but it should work without this IF
4575 implementing = container.Pending.IsInterfaceIndexer (
4576 member.InterfaceType, method.ReturnType, ParameterInfo);
4578 implementing = container.Pending.IsInterfaceMethod (
4579 member.InterfaceType, name, method.ReturnType, ParameterInfo);
4581 if (member.InterfaceType != null){
4582 if (implementing == null){
4583 if (member is PropertyBase) {
4584 Report.Error (550, method.Location, "'{0}' is an accessor not found in interface member '{1}'",
4585 method.GetSignatureForError (container), member.Name);
4588 Report.Error (539, method.Location,
4589 "'{0}' in explicit interface declaration is not a member of interface", member.GetSignatureForError () );
4593 if (implementing.IsSpecialName && !((member is PropertyBase || member is EventProperty))) {
4594 Report.SymbolRelatedToPreviousError (implementing);
4595 Report.Error (683, method.Location, "'{0}' explicit method implementation cannot implement '{1}' because it is an accessor",
4596 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4599 method_name = member.InterfaceType.FullName + "." + name;
4601 if (implementing != null && method is AbstractPropertyEventMethod && !implementing.IsSpecialName) {
4602 Report.SymbolRelatedToPreviousError (implementing);
4603 Report.Error (686, method.Location, "Accessor '{0}' cannot implement interface member '{1}' for type '{2}'. Use an explicit interface implementation",
4604 method.GetSignatureForError (container), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4611 // For implicit implementations, make sure we are public, for
4612 // explicit implementations, make sure we are private.
4614 if (implementing != null){
4616 // Setting null inside this block will trigger a more
4617 // verbose error reporting for missing interface implementations
4619 // The "candidate" function has been flagged already
4620 // but it wont get cleared
4622 if (member.IsExplicitImpl){
4623 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4624 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4625 implementing = null;
4627 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4628 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4630 // If this is an interface method implementation,
4631 // check for public accessibility
4633 implementing = null;
4634 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4635 // We may never be private.
4636 implementing = null;
4637 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4639 // We may be protected if we're overriding something.
4641 implementing = null;
4646 // Static is not allowed
4648 if ((modifiers & Modifiers.STATIC) != 0){
4649 implementing = null;
4650 Modifiers.Error_InvalidModifier (method.Location, "static");
4655 // If implementing is still valid, set flags
4657 if (implementing != null){
4659 // When implementing interface methods, set NewSlot
4660 // unless, we are overwriting a method.
4662 if (implementing.DeclaringType.IsInterface){
4663 if ((modifiers & Modifiers.OVERRIDE) == 0)
4664 flags |= MethodAttributes.NewSlot;
4667 MethodAttributes.Virtual |
4668 MethodAttributes.HideBySig;
4670 // Set Final unless we're virtual, abstract or already overriding a method.
4671 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4672 flags |= MethodAttributes.Final;
4674 IsImplementing = true;
4677 EmitContext ec = method.CreateEmitContext (container, null);
4679 DefineMethodBuilder (ec, container, method_name, ParameterTypes);
4681 if (builder == null)
4684 if ((modifiers & Modifiers.UNSAFE) != 0)
4685 builder.InitLocals = false;
4687 if (IsImplementing){
4689 // clear the pending implemntation flag
4691 if (member is Indexer) {
4692 container.Pending.ImplementIndexer (
4693 member.InterfaceType, builder, method.ReturnType,
4694 ParameterInfo, member.IsExplicitImpl);
4696 container.Pending.ImplementMethod (
4697 member.InterfaceType, name, method.ReturnType,
4698 ParameterInfo, member.IsExplicitImpl);
4700 if (member.IsExplicitImpl)
4701 container.TypeBuilder.DefineMethodOverride (
4702 builder, implementing);
4706 TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes);
4707 TypeManager.AddMethod (builder, method);
4714 /// Create the MethodBuilder for the method
4716 void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
4718 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4720 if ((modifiers & extern_static) == extern_static) {
4722 if (method.OptAttributes != null) {
4723 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
4724 if (dllimport_attribute != null) {
4725 flags |= MethodAttributes.PinvokeImpl;
4726 builder = dllimport_attribute.DefinePInvokeMethod (
4727 ec, container.TypeBuilder, method_name, flags,
4728 method.ReturnType, ParameterTypes);
4734 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4735 // We are more strict than Microsoft and report CS0626 like error
4736 if (method.OptAttributes == null ||
4737 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
4738 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));
4743 builder = container.TypeBuilder.DefineMethod (
4744 method_name, flags, method.CallingConventions,
4745 method.ReturnType, ParameterTypes);
4751 public void Emit (TypeContainer container, Attributable kind)
4754 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4755 ec = method.CreateEmitContext (container, builder.GetILGenerator ());
4757 ec = method.CreateEmitContext (container, null);
4759 if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute (container) != null)
4760 ec.TestObsoleteMethodUsage = false;
4762 Location loc = method.Location;
4763 Attributes OptAttributes = method.OptAttributes;
4765 if (OptAttributes != null)
4766 OptAttributes.Emit (ec, kind);
4768 if (member is MethodCore)
4769 ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
4771 ToplevelBlock block = method.Block;
4774 // abstract or extern methods have no bodies
4776 if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
4781 // abstract or extern methods have no bodies.
4783 if ((modifiers & Modifiers.ABSTRACT) != 0)
4785 500, method.Location, "Abstract method `" +
4786 TypeManager.CSharpSignature (builder) +
4787 "' can not have a body");
4789 if ((modifiers & Modifiers.EXTERN) != 0)
4791 179, method.Location, "External method `" +
4792 TypeManager.CSharpSignature (builder) +
4793 "' can not have a body");
4799 // Methods must have a body unless they're extern or abstract
4801 if (block == null) {
4803 501, method.Location, "Method `" +
4804 TypeManager.CSharpSignature (builder) +
4805 "' must declare a body since it is not marked " +
4806 "abstract or extern");
4810 SourceMethod source = SourceMethod.Create (
4811 container, MethodBuilder, method.Block);
4814 // Handle destructors specially
4816 // FIXME: This code generates buggy code
4818 if (member is Destructor)
4819 EmitDestructor (ec, block);
4821 ec.EmitTopBlock (block, ParameterInfo, loc);
4824 source.CloseMethod ();
4827 void EmitDestructor (EmitContext ec, ToplevelBlock block)
4829 ILGenerator ig = ec.ig;
4831 Label finish = ig.DefineLabel ();
4833 block.SetDestructor ();
4835 ig.BeginExceptionBlock ();
4836 ec.ReturnLabel = finish;
4837 ec.HasReturnLabel = true;
4838 ec.EmitTopBlock (block, null, method.Location);
4840 // ig.MarkLabel (finish);
4841 ig.BeginFinallyBlock ();
4843 if (ec.ContainerType.BaseType != null) {
4844 Expression member_lookup = Expression.MemberLookup (
4845 ec, ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4846 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4848 if (member_lookup != null){
4849 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
4851 ig.Emit (OpCodes.Ldarg_0);
4852 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
4856 ig.EndExceptionBlock ();
4857 //ig.MarkLabel (ec.ReturnLabel);
4858 ig.Emit (OpCodes.Ret);
4862 // Should derive from MethodCore
4863 public class Destructor : Method {
4865 public Destructor (TypeContainer ds, Expression return_type, int mod,
4866 string name, Parameters parameters, Attributes attrs,
4868 : base (ds, return_type, mod, false, new MemberName (name),
4869 parameters, attrs, l)
4872 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4874 if (a.Type == TypeManager.conditional_attribute_type) {
4875 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
4879 base.ApplyAttributeBuilder (a, cb);
4883 abstract public class MemberBase : MemberCore {
4884 public Expression Type;
4886 public MethodAttributes flags;
4888 protected readonly int explicit_mod_flags;
4891 // The "short" name of this property / indexer / event. This is the
4892 // name without the explicit interface.
4894 public string ShortName {
4895 get { return MemberName.Name; }
4897 SetMemberName (new MemberName (MemberName.Left, value));
4902 // The type of this property / indexer / event
4905 public Type MemberType {
4907 if (member_type == null && Type != null) {
4908 EmitContext ec = Parent.EmitContext;
4909 bool old_unsafe = ec.InUnsafe;
4910 ec.InUnsafe = InUnsafe;
4911 Type = Type.ResolveAsTypeTerminal (ec, false);
4912 ec.InUnsafe = old_unsafe;
4914 member_type = Type == null ? null : Type.Type;
4921 // Whether this is an interface member.
4923 public bool IsInterface;
4926 // If true, this is an explicit interface implementation
4928 public bool IsExplicitImpl;
4931 // The interface type we are explicitly implementing
4933 public Type InterfaceType = null;
4936 // The constructor is only exposed to our children
4938 protected MemberBase (TypeContainer parent, Expression type, int mod,
4939 int allowed_mod, int def_mod, MemberName name,
4940 Attributes attrs, Location loc)
4941 : base (parent, name, attrs, loc)
4943 explicit_mod_flags = mod;
4945 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
4946 IsExplicitImpl = (MemberName.Left != null);
4949 protected virtual bool CheckBase ()
4951 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
4952 Report.Error (666, Location, "Protected member in struct declaration");
4956 if ((RootContext.WarningLevel >= 4) &&
4957 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
4958 ((ModFlags & Modifiers.PROTECTED) != 0) &&
4959 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
4960 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
4965 protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
4969 foreach (Type partype in parameters){
4970 if (partype == TypeManager.void_type) {
4972 1547, Location, "Keyword 'void' cannot " +
4973 "be used in this context");
4977 if (partype.IsPointer){
4980 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
4984 if (ds.AsAccessible (partype, ModFlags))
4987 if (this is Indexer)
4988 Report.Error (55, Location,
4989 "Inconsistent accessibility: parameter type `" +
4990 TypeManager.CSharpName (partype) + "' is less " +
4991 "accessible than indexer `" + Name + "'");
4992 else if ((this is Method) && ((Method) this).IsOperator)
4993 Report.Error (57, Location,
4994 "Inconsistent accessibility: parameter type `" +
4995 TypeManager.CSharpName (partype) + "' is less " +
4996 "accessible than operator `" + Name + "'");
4998 Report.Error (51, Location,
4999 "Inconsistent accessibility: parameter type `" +
5000 TypeManager.CSharpName (partype) + "' is less " +
5001 "accessible than method `" + Name + "'");
5008 protected virtual bool DoDefine ()
5010 EmitContext ec = Parent.EmitContext;
5012 throw new InternalErrorException ("MemberBase.DoDefine called too early");
5015 throw new InternalErrorException ();
5018 ModFlags = Modifiers.PUBLIC |
5019 Modifiers.ABSTRACT |
5020 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
5022 flags = MethodAttributes.Public |
5023 MethodAttributes.Abstract |
5024 MethodAttributes.HideBySig |
5025 MethodAttributes.NewSlot |
5026 MethodAttributes.Virtual;
5028 if (!Parent.MethodModifiersValid (this))
5031 flags = Modifiers.MethodAttr (ModFlags);
5034 if (MemberType == null)
5037 if ((Parent.ModFlags & Modifiers.SEALED) != 0){
5038 if ((ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0){
5039 Report.Error (549, Location, "Virtual method can not be contained in sealed class");
5044 // verify accessibility
5045 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5046 Report.SymbolRelatedToPreviousError (MemberType);
5047 if (this is Property)
5048 Report.Error (53, Location,
5049 "Inconsistent accessibility: property type `" +
5050 TypeManager.CSharpName (MemberType) + "' is less " +
5051 "accessible than property `" + GetSignatureForError () + "'");
5052 else if (this is Indexer)
5053 Report.Error (54, Location,
5054 "Inconsistent accessibility: indexer return type `" +
5055 TypeManager.CSharpName (MemberType) + "' is less " +
5056 "accessible than indexer `" + Name + "'");
5057 else if (this is MethodCore) {
5058 if (this is Operator)
5059 Report.Error (56, Location,
5060 "Inconsistent accessibility: return type `" +
5061 TypeManager.CSharpName (MemberType) + "' is less " +
5062 "accessible than operator `" + Name + "'");
5064 Report.Error (50, Location,
5065 "Inconsistent accessibility: return type `" +
5066 TypeManager.CSharpName (MemberType) + "' is less " +
5067 "accessible than method `" + Name + "'");
5069 Report.Error (52, Location,
5070 "Inconsistent accessibility: field type `" +
5071 TypeManager.CSharpName (MemberType) + "' is less " +
5072 "accessible than field `" + Name + "'");
5077 if (MemberType.IsPointer && !UnsafeOK (Parent))
5080 if (IsExplicitImpl) {
5081 Expression expr = MemberName.Left.GetTypeExpression (Location);
5082 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
5086 InterfaceType = texpr.ResolveType (ec);
5088 if (!InterfaceType.IsInterface) {
5089 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
5093 if (!Parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
5096 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5102 public override string GetSignatureForError (TypeContainer tc)
5104 return String.Concat (tc.Name, '.', base.GetSignatureForError (tc));
5107 protected bool IsTypePermitted ()
5109 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5110 Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
5116 protected override bool VerifyClsCompliance(DeclSpace ds)
5118 if (base.VerifyClsCompliance (ds)) {
5122 if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
5123 Report.Error (3010, Location, "'{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
5128 protected override void VerifyObsoleteAttribute()
5130 CheckUsageOfObsoleteAttribute (MemberType);
5135 // Fields and Events both generate FieldBuilders, we use this to share
5136 // their common bits. This is also used to flag usage of the field
5138 abstract public class FieldBase : MemberBase {
5139 public FieldBuilder FieldBuilder;
5140 public Status status;
5143 public enum Status : byte {
5145 HAS_OFFSET = 4 // Used by FieldMember.
5148 static string[] attribute_targets = new string [] { "field" };
5151 /// Symbol with same name in base class/struct
5153 public MemberInfo conflict_symbol;
5156 // The constructor is only exposed to our children
5158 protected FieldBase (TypeContainer parent, Expression type, int mod,
5159 int allowed_mod, MemberName name, object init,
5160 Attributes attrs, Location loc)
5161 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
5167 public override AttributeTargets AttributeTargets {
5169 return AttributeTargets.Field;
5173 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5175 if (a.Type == TypeManager.marshal_as_attr_type) {
5176 UnmanagedMarshal marshal = a.GetMarshal (this);
5177 if (marshal != null) {
5178 FieldBuilder.SetMarshal (marshal);
5183 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5184 a.Error_InvalidSecurityParent ();
5188 FieldBuilder.SetCustomAttribute (cb);
5192 // Whether this field has an initializer.
5194 public bool HasInitializer {
5196 return init != null;
5200 protected readonly Object init;
5203 Expression init_expr;
5204 bool init_expr_initialized = false;
5207 // Resolves and returns the field initializer.
5209 public Expression GetInitializerExpression (EmitContext ec)
5211 if (init_expr_initialized)
5215 if (init is Expression)
5216 e = (Expression) init;
5218 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
5220 EmitContext parent_ec = Parent.EmitContext;
5222 bool old_is_static = parent_ec.IsStatic;
5223 parent_ec.IsStatic = ec.IsStatic;
5224 parent_ec.IsFieldInitializer = true;
5225 e = e.DoResolve (parent_ec);
5226 parent_ec.IsFieldInitializer = false;
5227 parent_ec.IsStatic = old_is_static;
5230 init_expr_initialized = true;
5235 protected override bool CheckBase ()
5237 if (!base.CheckBase ())
5244 conflict_symbol = Parent.FindBaseMemberWithSameName (Name, false);
5245 if (conflict_symbol == null) {
5246 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5247 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
5252 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5253 Report.SymbolRelatedToPreviousError (conflict_symbol);
5254 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
5260 public override string GetSignatureForError ()
5262 if (FieldBuilder == null) {
5263 return base.GetSignatureForError (Parent);
5265 return TypeManager.GetFullNameSignature (FieldBuilder);
5268 protected virtual bool IsFieldClsCompliant {
5270 if (FieldBuilder == null)
5273 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5277 public override string[] ValidAttributeTargets {
5279 return attribute_targets;
5283 protected override bool VerifyClsCompliance (DeclSpace ds)
5285 if (!base.VerifyClsCompliance (ds))
5288 if (!IsFieldClsCompliant) {
5289 Report.Error (3003, Location, "Type of '{0}' is not CLS-compliant", GetSignatureForError ());
5295 public void SetAssigned ()
5297 status |= Status.ASSIGNED;
5301 public abstract class FieldMember: FieldBase
5303 protected FieldMember (TypeContainer parent, Expression type, int mod,
5304 int allowed_mod, MemberName name, object init, Attributes attrs, Location loc)
5305 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, init, attrs, loc)
5307 if ((mod & Modifiers.ABSTRACT) != 0)
5308 Report.Error (681, loc, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5311 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5313 if (a.Type == TypeManager.field_offset_attribute_type)
5315 status |= Status.HAS_OFFSET;
5317 if (!Parent.HasExplicitLayout) {
5318 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5322 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5323 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5329 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5330 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5335 base.ApplyAttributeBuilder (a, cb);
5339 public override bool Define()
5341 EmitContext ec = Parent.EmitContext;
5343 throw new InternalErrorException ("FieldMember.Define called too early");
5345 if (MemberType == null)
5348 if (MemberType == TypeManager.void_type) {
5349 Report.Error (1547, Location, "Keyword 'void' cannot be used in this context");
5356 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5357 Report.Error (52, Location,
5358 "Inconsistent accessibility: field type `" +
5359 TypeManager.CSharpName (MemberType) + "' is less " +
5360 "accessible than field `" + Name + "'");
5364 if (!IsTypePermitted ())
5367 if (MemberType.IsPointer && !UnsafeOK (Parent))
5373 public override void Emit ()
5375 if (OptAttributes != null) {
5376 EmitContext ec = new EmitContext (Parent, Location, null, FieldBuilder.FieldType, ModFlags);
5377 OptAttributes.Emit (ec, this);
5380 if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
5381 Report.Error (625, Location, "'{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5388 // Represents header string for documentation comment.
5390 public override string DocCommentHeader {
5391 get { return "F:"; }
5395 interface IFixedBuffer
5397 FieldInfo Element { get; }
5398 Type ElementType { get; }
5401 public class FixedFieldExternal: IFixedBuffer
5403 FieldInfo element_field;
5405 public FixedFieldExternal (FieldInfo fi)
5407 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5410 #region IFixedField Members
5412 public FieldInfo Element {
5414 return element_field;
5418 public Type ElementType {
5420 return element_field.FieldType;
5428 /// Fixed buffer implementation
5430 public class FixedField: FieldMember, IFixedBuffer
5432 public const string FixedElementName = "FixedElementField";
5433 static int GlobalCounter = 0;
5434 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5435 static FieldInfo[] fi;
5437 TypeBuilder fixed_buffer_type;
5438 FieldBuilder element;
5439 Expression size_expr;
5442 const int AllowedModifiers =
5445 Modifiers.PROTECTED |
5446 Modifiers.INTERNAL |
5449 public FixedField (TypeContainer parent, Expression type, int mod, string name,
5450 Expression size_expr, Attributes attrs, Location loc):
5451 base (parent, type, mod, AllowedModifiers, new MemberName (name), null, attrs, loc)
5453 if (RootContext.Version == LanguageVersion.ISO_1)
5454 Report.FeatureIsNotStandardized (loc, "fixed sized buffers");
5456 this.size_expr = size_expr;
5459 public override bool Define()
5462 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5463 Report.Warning (-23, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5466 if (Parent.Kind != Kind.Struct) {
5467 Report.Error (1642, Location, "Fixed buffer fields may only be members of structs");
5471 if (!base.Define ())
5474 if (!TypeManager.IsPrimitiveType (MemberType)) {
5475 Report.Error (1663, Location, "Fixed sized buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double");
5479 Expression e = size_expr.Resolve (Parent.EmitContext);
5483 Constant c = e as Constant;
5485 Report.Error (133, Location, "The expression being assigned to '{0}' must be constant", GetSignatureForError ());
5489 IntConstant buffer_size_const = c.ToInt (Location);
5490 if (buffer_size_const == null)
5493 buffer_size = buffer_size_const.Value;
5495 if (buffer_size <= 0) {
5496 Report.Error (1665, Location, "Fixed sized buffer '{0}' must have a length greater than zero", GetSignatureForError ());
5500 int type_size = Expression.GetTypeSize (MemberType);
5502 if (buffer_size > int.MaxValue / type_size) {
5503 Report.Error (1664, Location, "Fixed sized buffer of length '{0}' and type '{1}' exceeded 2^31 limit",
5504 buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5508 buffer_size *= type_size;
5511 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5513 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5514 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5515 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5516 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5518 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5519 TypeManager.RegisterFieldBase (FieldBuilder, this);
5524 public override void Emit()
5527 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5529 object[] fi_val = new object[1];
5530 fi_val [0] = buffer_size;
5532 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5533 ctor_args, fi, fi_val);
5534 fixed_buffer_type.SetCustomAttribute (cab);
5537 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5538 FieldBuilder.SetCustomAttribute (cab);
5543 protected override bool IsFieldClsCompliant {
5549 #region IFixedField Members
5551 public FieldInfo Element {
5557 public Type ElementType {
5567 // The Field class is used to represents class/struct fields during parsing.
5569 public class Field : FieldMember {
5571 // Modifiers allowed in a class declaration
5573 const int AllowedModifiers =
5576 Modifiers.PROTECTED |
5577 Modifiers.INTERNAL |
5580 Modifiers.VOLATILE |
5584 public Field (TypeContainer parent, Expression type, int mod, string name,
5585 Object expr_or_array_init, Attributes attrs, Location loc)
5586 : base (parent, type, mod, AllowedModifiers, new MemberName (name),
5587 expr_or_array_init, attrs, loc)
5591 public override bool Define ()
5593 if (!base.Define ())
5596 if (RootContext.WarningLevel > 1){
5597 Type ptype = Parent.TypeBuilder.BaseType;
5599 // ptype is only null for System.Object while compiling corlib.
5601 TypeContainer.FindMembers (
5602 ptype, MemberTypes.Method,
5603 BindingFlags.Public |
5604 BindingFlags.Static | BindingFlags.Instance,
5605 System.Type.FilterName, Name);
5609 if ((ModFlags & Modifiers.VOLATILE) != 0){
5610 if (!MemberType.IsClass){
5611 Type vt = MemberType;
5613 if (TypeManager.IsEnumType (vt))
5614 vt = TypeManager.EnumToUnderlying (MemberType);
5616 if (!((vt == TypeManager.bool_type) ||
5617 (vt == TypeManager.sbyte_type) ||
5618 (vt == TypeManager.byte_type) ||
5619 (vt == TypeManager.short_type) ||
5620 (vt == TypeManager.ushort_type) ||
5621 (vt == TypeManager.int32_type) ||
5622 (vt == TypeManager.uint32_type) ||
5623 (vt == TypeManager.char_type) ||
5624 (vt == TypeManager.float_type) ||
5625 (!vt.IsValueType))){
5627 677, Location, Parent.MakeName (Name) +
5628 " A volatile field can not be of type `" +
5629 TypeManager.CSharpName (vt) + "'");
5634 if ((ModFlags & Modifiers.READONLY) != 0){
5637 "A field can not be both volatile and readonly");
5642 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5644 if (Parent.Kind == Kind.Struct &&
5645 ((fa & FieldAttributes.Static) == 0) &&
5646 MemberType == Parent.TypeBuilder &&
5647 !TypeManager.IsBuiltinType (MemberType)){
5648 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5649 "' causes a cycle in the structure layout");
5654 FieldBuilder = Parent.TypeBuilder.DefineField (
5655 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5657 TypeManager.RegisterFieldBase (FieldBuilder, this);
5659 catch (ArgumentException) {
5660 Report.Warning (-24, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5669 // `set' and `get' accessors are represented with an Accessor.
5671 public class Accessor {
5673 // Null if the accessor is empty, or a Block if not
5675 public const int AllowedModifiers =
5677 Modifiers.PROTECTED |
5678 Modifiers.INTERNAL |
5681 public ToplevelBlock Block;
5682 public Attributes Attributes;
5683 public Location Location;
5684 public int ModFlags;
5686 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5691 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5696 // Ooouh Martin, templates are missing here.
5697 // When it will be possible move here a lot of child code and template method type.
5698 public abstract class AbstractPropertyEventMethod: MemberCore, IMethodData {
5699 protected MethodData method_data;
5700 protected ToplevelBlock block;
5701 protected ListDictionary declarative_security;
5703 // The accessor are created event if they are not wanted.
5704 // But we need them because their names are reserved.
5705 // Field says whether accessor will be emited or not
5706 public readonly bool IsDummy;
5708 protected readonly string prefix;
5710 ReturnParameter return_attributes;
5712 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5713 : base (null, SetupName (prefix, member), null, member.Location)
5715 this.prefix = prefix;
5719 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5721 : base (null, SetupName (prefix, member),
5722 accessor.Attributes, accessor.Location)
5724 this.prefix = prefix;
5725 this.block = accessor.Block;
5728 static MemberName SetupName (string prefix, MemberBase member)
5730 return new MemberName (member.MemberName.Left, prefix + member.ShortName);
5733 public void UpdateName (MemberBase member)
5735 SetMemberName (SetupName (prefix, member));
5738 #region IMethodData Members
5740 public ToplevelBlock Block {
5750 public CallingConventions CallingConventions {
5752 return CallingConventions.Standard;
5756 public bool IsExcluded (EmitContext ec)
5761 public MemberName MethodName {
5767 public abstract ObsoleteAttribute GetObsoleteAttribute ();
5768 public abstract Type[] ParameterTypes { get; }
5769 public abstract Type ReturnType { get; }
5770 public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
5774 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5776 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5777 a.Type == TypeManager.conditional_attribute_type) {
5778 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 ());
5782 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5783 if (declarative_security == null)
5784 declarative_security = new ListDictionary ();
5785 a.ExtractSecurityPermissionSet (declarative_security);
5789 if (a.Target == AttributeTargets.Method) {
5790 method_data.MethodBuilder.SetCustomAttribute (cb);
5794 if (a.Target == AttributeTargets.ReturnValue) {
5795 if (return_attributes == null)
5796 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5798 return_attributes.ApplyAttributeBuilder (a, cb);
5802 ApplyToExtraTarget (a, cb);
5805 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5807 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5810 public override bool Define()
5812 throw new NotSupportedException ();
5815 public virtual void Emit (TypeContainer container)
5817 EmitMethod (container);
5819 if (declarative_security != null) {
5820 foreach (DictionaryEntry de in declarative_security) {
5821 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5828 protected virtual void EmitMethod (TypeContainer container)
5830 method_data.Emit (container, this);
5833 public override bool IsClsCompliaceRequired(DeclSpace ds)
5838 public bool IsDuplicateImplementation (MethodCore method)
5840 if (Name != method.Name)
5843 Type[] param_types = method.ParameterTypes;
5845 if (param_types.Length != ParameterTypes.Length)
5848 for (int i = 0; i < param_types.Length; i++)
5849 if (param_types [i] != ParameterTypes [i])
5852 Report.SymbolRelatedToPreviousError (method);
5853 Report.Error (111, Location, TypeContainer.Error111, method.Parent.GetSignatureForError (), Name);
5857 public override bool IsUsed
5867 public new Location Location {
5869 return base.Location;
5874 // Represents header string for documentation comment.
5876 public override string DocCommentHeader {
5877 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5880 protected override void VerifyObsoleteAttribute()
5887 // Properties and Indexers both generate PropertyBuilders, we use this to share
5888 // their common bits.
5890 abstract public class PropertyBase : MethodCore {
5892 public class GetMethod: PropertyMethod
5894 static string[] attribute_targets = new string [] { "method", "return" };
5896 public GetMethod (MethodCore method):
5897 base (method, "get_")
5901 public GetMethod (MethodCore method, Accessor accessor):
5902 base (method, accessor, "get_")
5906 public override MethodBuilder Define(TypeContainer container)
5908 base.Define (container);
5910 method_data = new MethodData (method, method.ParameterInfo, ModFlags, flags, this);
5912 if (!method_data.Define (container))
5915 return method_data.MethodBuilder;
5918 public override string GetSignatureForError (TypeContainer tc)
5920 return String.Concat (base.GetSignatureForError (tc), ".get");
5923 public override Type ReturnType {
5925 return method.MemberType;
5929 public override string[] ValidAttributeTargets {
5931 return attribute_targets;
5936 public class SetMethod: PropertyMethod {
5938 static string[] attribute_targets = new string [] { "method", "param", "return" };
5939 ImplicitParameter param_attr;
5941 public SetMethod (MethodCore method):
5942 base (method, "set_")
5946 public SetMethod (MethodCore method, Accessor accessor):
5947 base (method, accessor, "set_")
5951 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
5953 if (a.Target == AttributeTargets.Parameter) {
5954 if (param_attr == null)
5955 param_attr = new ImplicitParameter (method_data.MethodBuilder);
5957 param_attr.ApplyAttributeBuilder (a, cb);
5961 base.ApplyAttributeBuilder (a, cb);
5964 protected virtual InternalParameters GetParameterInfo (EmitContext ec)
5966 Parameter [] parms = new Parameter [1];
5967 parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null);
5968 Parameters parameters = new Parameters (parms, null, method.Location);
5970 bool old_unsafe = ec.InUnsafe;
5971 ec.InUnsafe = InUnsafe;
5972 Type [] types = parameters.GetParameterInfo (ec);
5973 ec.InUnsafe = old_unsafe;
5975 return new InternalParameters (types, parameters);
5978 public override MethodBuilder Define (TypeContainer container)
5980 if (container.EmitContext == null)
5981 throw new InternalErrorException ("SetMethod.Define called too early");
5983 base.Define (container);
5985 method_data = new MethodData (method, GetParameterInfo (container.EmitContext), ModFlags, flags, this);
5987 if (!method_data.Define (container))
5990 return method_data.MethodBuilder;
5993 public override string GetSignatureForError (TypeContainer tc)
5995 return String.Concat (base.GetSignatureForError (tc), ".set");
5998 public override Type[] ParameterTypes {
6000 return new Type[] { method.MemberType };
6004 public override Type ReturnType {
6006 return TypeManager.void_type;
6010 public override string[] ValidAttributeTargets {
6012 return attribute_targets;
6017 static string[] attribute_targets = new string [] { "property" };
6019 public abstract class PropertyMethod: AbstractPropertyEventMethod
6021 protected readonly MethodCore method;
6022 protected MethodAttributes flags;
6024 public PropertyMethod (MethodCore method, string prefix)
6025 : base (method, prefix)
6027 this.method = method;
6028 Parent = method.Parent;
6031 public PropertyMethod (MethodCore method, Accessor accessor,
6033 : base (method, accessor, prefix)
6035 this.method = method;
6036 Parent = method.Parent;
6037 this.ModFlags = accessor.ModFlags;
6039 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6040 Report.FeatureIsNotStandardized (Location, "accessor modifiers");
6044 public override AttributeTargets AttributeTargets {
6046 return AttributeTargets.Method;
6050 public override bool IsClsCompliaceRequired(DeclSpace ds)
6052 return method.IsClsCompliaceRequired (ds);
6055 public InternalParameters ParameterInfo
6058 return method_data.ParameterInfo;
6062 public virtual MethodBuilder Define (TypeContainer container)
6065 // Check for custom access modifier
6067 if (ModFlags == 0) {
6068 ModFlags = method.ModFlags;
6069 flags = method.flags;
6071 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6072 Report.Error (442, Location, "{0}': abstract properties cannot have private accessors", GetSignatureForError (container));
6075 CheckModifiers (container, ModFlags);
6076 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6077 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6078 flags = Modifiers.MethodAttr (ModFlags);
6079 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6086 public bool HasCustomAccessModifier
6089 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6093 public override Type[] ParameterTypes {
6095 return TypeManager.NoTypes;
6099 public override EmitContext CreateEmitContext (TypeContainer tc,
6102 return new EmitContext (
6103 tc, method.Parent, method.Location, ig, ReturnType,
6104 method.ModFlags, false);
6107 public override ObsoleteAttribute GetObsoleteAttribute ()
6109 return method.GetObsoleteAttribute (method.Parent);
6112 public override string GetSignatureForError (TypeContainer tc)
6114 return String.Concat (tc.Name, '.', method.Name);
6117 void CheckModifiers (TypeContainer container, int modflags)
6120 int mflags = method.ModFlags & Modifiers.Accessibility;
6122 if ((mflags & Modifiers.PUBLIC) != 0) {
6123 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6125 else if ((mflags & Modifiers.PROTECTED) != 0) {
6126 if ((mflags & Modifiers.INTERNAL) != 0)
6127 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6129 flags |= Modifiers.PRIVATE;
6131 else if ((mflags & Modifiers.INTERNAL) != 0)
6132 flags |= Modifiers.PRIVATE;
6134 if ((mflags == modflags) || (modflags & (~flags)) != 0)
6135 Report.Error (273, Location, "{0}: accessibility modifier must be more restrictive than the property or indexer",
6136 GetSignatureForError (container));
6139 public override bool MarkForDuplicationCheck ()
6141 caching_flags |= Flags.TestMethodDuplication;
6147 public PropertyMethod Get, Set;
6148 public PropertyBuilder PropertyBuilder;
6149 public MethodBuilder GetBuilder, SetBuilder;
6151 protected EmitContext ec;
6153 public PropertyBase (TypeContainer ds, Expression type, int mod_flags,
6154 int allowed_mod, bool is_iface, MemberName name,
6155 Parameters parameters, Attributes attrs,
6157 : base (ds, type, mod_flags, allowed_mod, is_iface, name,
6158 attrs, parameters, loc)
6162 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6164 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6165 a.Error_InvalidSecurityParent ();
6169 PropertyBuilder.SetCustomAttribute (cb);
6172 public override AttributeTargets AttributeTargets {
6174 return AttributeTargets.Property;
6178 public override bool Define ()
6183 if (!IsTypePermitted ())
6189 protected override bool DoDefine ()
6191 if (!base.DoDefine ())
6195 // Accessors modifiers check
6197 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6198 Report.Error (274, Location, "'{0}': cannot specify accessibility modifiers for both accessors of the property or indexer.",
6199 GetSignatureForError ());
6203 if ((Get.IsDummy || Set.IsDummy)
6204 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6205 Report.Error (276, Location,
6206 "'{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor.",
6207 GetSignatureForError ());
6211 if (MemberType.IsAbstract && MemberType.IsSealed) {
6212 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6216 ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
6220 public override string GetSignatureForError()
6222 if (PropertyBuilder == null)
6223 return GetSignatureForError (Parent);
6225 return TypeManager.CSharpSignature (PropertyBuilder, false);
6228 protected override bool CheckForDuplications ()
6230 ArrayList ar = Parent.Indexers;
6232 int arLen = ar.Count;
6234 for (int i = 0; i < arLen; i++) {
6235 Indexer m = (Indexer) ar [i];
6236 if (IsDuplicateImplementation (m))
6241 ar = Parent.Properties;
6243 int arLen = ar.Count;
6245 for (int i = 0; i < arLen; i++) {
6246 Property m = (Property) ar [i];
6247 if (IsDuplicateImplementation (m))
6255 // TODO: rename to Resolve......
6256 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
6258 PropertyInfo base_property = container.BaseCache.FindMemberToOverride (
6259 container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
6261 if (base_property == null)
6264 base_ret_type = base_property.PropertyType;
6265 MethodInfo get_accessor = base_property.GetGetMethod (true);
6266 MethodInfo set_accessor = base_property.GetSetMethod (true);
6267 MethodAttributes get_accessor_access, set_accessor_access;
6269 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6270 if (Get != null && !Get.IsDummy && get_accessor == null) {
6271 Report.SymbolRelatedToPreviousError (base_property);
6272 Report.Error (545, Location, "'{0}': cannot override because '{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6275 if (Set != null && !Set.IsDummy && set_accessor == null) {
6276 Report.SymbolRelatedToPreviousError (base_property);
6277 Report.Error (546, Location, "'{0}': cannot override because '{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6282 // Check base class accessors access
6284 get_accessor_access = set_accessor_access = 0;
6285 if ((ModFlags & Modifiers.NEW) == 0) {
6286 if (get_accessor != null) {
6287 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6288 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6290 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6291 Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
6292 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6295 if (set_accessor != null) {
6296 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6297 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6299 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6300 Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
6301 GetSignatureForError (container), TypeManager.GetFullNameSignature (base_property));
6306 // Get the less restrictive access
6308 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6311 public override void Emit ()
6314 // The PropertyBuilder can be null for explicit implementations, in that
6315 // case, we do not actually emit the ".property", so there is nowhere to
6316 // put the attribute
6318 if (PropertyBuilder != null && OptAttributes != null)
6319 OptAttributes.Emit (ec, this);
6331 /// Tests whether accessors are not in collision with some method (CS0111)
6333 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6335 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6338 public override bool IsUsed
6344 return Get.IsUsed | Set.IsUsed;
6348 protected override void SetMemberName (MemberName new_name)
6350 base.SetMemberName (new_name);
6352 Get.UpdateName (this);
6353 Set.UpdateName (this);
6356 protected override bool VerifyClsCompliance (DeclSpace ds)
6358 if (!base.VerifyClsCompliance (ds))
6361 if ((Get.ModFlags != ModFlags && !Get.IsDummy) || (Set.ModFlags != ModFlags && !Set.IsDummy)) {
6362 Report.Error (3025, Get.ModFlags != ModFlags ? Get.Location : Set.Location,
6363 "CLS-compliant accessors must have the same accessibility as their property");
6368 public override string[] ValidAttributeTargets {
6370 return attribute_targets;
6375 // Represents header string for documentation comment.
6377 public override string DocCommentHeader {
6378 get { return "P:"; }
6382 public class Property : PropertyBase, IIteratorContainer {
6383 const int AllowedModifiers =
6386 Modifiers.PROTECTED |
6387 Modifiers.INTERNAL |
6391 Modifiers.OVERRIDE |
6392 Modifiers.ABSTRACT |
6395 Modifiers.METHOD_YIELDS |
6398 const int AllowedInterfaceModifiers =
6401 public Property (TypeContainer ds, Expression type, int mod, bool is_iface,
6402 MemberName name, Attributes attrs, Accessor get_block,
6403 Accessor set_block, Location loc)
6404 : base (ds, type, mod,
6405 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6406 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs,
6409 if (get_block == null)
6410 Get = new GetMethod (this);
6412 Get = new GetMethod (this, get_block);
6414 if (set_block == null)
6415 Set = new SetMethod (this);
6417 Set = new SetMethod (this, set_block);
6420 public override bool Define ()
6422 if (!base.Define ())
6428 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6432 GetBuilder = Get.Define (Parent);
6433 if (GetBuilder == null)
6437 // Setup iterator if we are one
6439 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
6440 Iterator iterator = new Iterator (
6441 Parent, "get", MemberType,
6443 ModFlags, Get.Block, Location);
6445 if (!iterator.DefineIterator ())
6447 Get.Block = iterator.Block;
6452 SetBuilder = Set.Define (Parent);
6453 if (SetBuilder == null)
6456 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
6459 // FIXME - PropertyAttributes.HasDefault ?
6461 PropertyAttributes prop_attr = PropertyAttributes.None;
6463 prop_attr |= PropertyAttributes.RTSpecialName |
6464 PropertyAttributes.SpecialName;
6466 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6467 Name, prop_attr, MemberType, null);
6470 PropertyBuilder.SetGetMethod (GetBuilder);
6473 PropertyBuilder.SetSetMethod (SetBuilder);
6475 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
6479 public void SetYields ()
6481 ModFlags |= Modifiers.METHOD_YIELDS;
6486 /// Gigantic workaround for lameness in SRE follows :
6487 /// This class derives from EventInfo and attempts to basically
6488 /// wrap around the EventBuilder so that FindMembers can quickly
6489 /// return this in it search for members
6491 public class MyEventBuilder : EventInfo {
6494 // We use this to "point" to our Builder which is
6495 // not really a MemberInfo
6497 EventBuilder MyBuilder;
6500 // We "catch" and wrap these methods
6502 MethodInfo raise, remove, add;
6504 EventAttributes attributes;
6505 Type declaring_type, reflected_type, event_type;
6510 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6512 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6514 // And now store the values in our own fields.
6516 declaring_type = type_builder;
6518 reflected_type = type_builder;
6520 attributes = event_attr;
6523 this.event_type = event_type;
6527 // Methods that you have to override. Note that you only need
6528 // to "implement" the variants that take the argument (those are
6529 // the "abstract" methods, the others (GetAddMethod()) are
6532 public override MethodInfo GetAddMethod (bool nonPublic)
6537 public override MethodInfo GetRemoveMethod (bool nonPublic)
6542 public override MethodInfo GetRaiseMethod (bool nonPublic)
6548 // These methods make "MyEventInfo" look like a Builder
6550 public void SetRaiseMethod (MethodBuilder raiseMethod)
6552 raise = raiseMethod;
6553 MyBuilder.SetRaiseMethod (raiseMethod);
6556 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6558 remove = removeMethod;
6559 MyBuilder.SetRemoveOnMethod (removeMethod);
6562 public void SetAddOnMethod (MethodBuilder addMethod)
6565 MyBuilder.SetAddOnMethod (addMethod);
6568 public void SetCustomAttribute (CustomAttributeBuilder cb)
6570 MyBuilder.SetCustomAttribute (cb);
6573 public override object [] GetCustomAttributes (bool inherit)
6575 // FIXME : There's nothing which can be seemingly done here because
6576 // we have no way of getting at the custom attribute objects of the
6581 public override object [] GetCustomAttributes (Type t, bool inherit)
6583 // FIXME : Same here !
6587 public override bool IsDefined (Type t, bool b)
6592 public override EventAttributes Attributes {
6598 public override string Name {
6604 public override Type DeclaringType {
6606 return declaring_type;
6610 public override Type ReflectedType {
6612 return reflected_type;
6616 public Type EventType {
6622 public void SetUsed ()
6624 if (my_event != null) {
6625 my_event.status = FieldBase.Status.ASSIGNED;
6626 my_event.SetMemberIsUsed ();
6632 /// For case when event is declared like property (with add and remove accessors).
6634 public class EventProperty: Event {
6636 static string[] attribute_targets = new string [] { "event", "property" };
6638 public EventProperty (TypeContainer parent, Expression type, int mod_flags,
6639 bool is_iface, MemberName name, Object init,
6640 Attributes attrs, Accessor add, Accessor remove,
6642 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6644 Add = new AddDelegateMethod (this, add);
6645 Remove = new RemoveDelegateMethod (this, remove);
6647 // For this event syntax we don't report error CS0067
6648 // because it is hard to do it.
6652 public override string[] ValidAttributeTargets {
6654 return attribute_targets;
6660 /// Event is declared like field.
6662 public class EventField: Event {
6664 static string[] attribute_targets = new string [] { "event", "field", "method" };
6665 static string[] attribute_targets_interface = new string[] { "event", "method" };
6667 public EventField (TypeContainer parent, Expression type, int mod_flags,
6668 bool is_iface, MemberName name, Object init,
6669 Attributes attrs, Location loc)
6670 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6672 Add = new AddDelegateMethod (this);
6673 Remove = new RemoveDelegateMethod (this);
6676 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6678 if (a.Target == AttributeTargets.Field) {
6679 FieldBuilder.SetCustomAttribute (cb);
6683 if (a.Target == AttributeTargets.Method) {
6684 Add.ApplyAttributeBuilder (a, cb);
6685 Remove.ApplyAttributeBuilder (a, cb);
6689 base.ApplyAttributeBuilder (a, cb);
6692 public override string[] ValidAttributeTargets {
6694 return IsInterface ? attribute_targets_interface : attribute_targets;
6699 public abstract class Event : FieldBase {
6701 protected sealed class AddDelegateMethod: DelegateMethod
6704 public AddDelegateMethod (Event method):
6705 base (method, "add_")
6709 public AddDelegateMethod (Event method, Accessor accessor):
6710 base (method, accessor, "add_")
6714 protected override MethodInfo DelegateMethodInfo {
6716 return TypeManager.delegate_combine_delegate_delegate;
6722 protected sealed class RemoveDelegateMethod: DelegateMethod
6724 public RemoveDelegateMethod (Event method):
6725 base (method, "remove_")
6729 public RemoveDelegateMethod (Event method, Accessor accessor):
6730 base (method, accessor, "remove_")
6734 protected override MethodInfo DelegateMethodInfo {
6736 return TypeManager.delegate_remove_delegate_delegate;
6742 public abstract class DelegateMethod: AbstractPropertyEventMethod
6744 protected readonly Event method;
6745 ImplicitParameter param_attr;
6747 static string[] attribute_targets = new string [] { "method", "param", "return" };
6749 public DelegateMethod (Event method, string prefix)
6750 : base (method, prefix)
6752 this.method = method;
6755 public DelegateMethod (Event method, Accessor accessor, string prefix)
6756 : base (method, accessor, prefix)
6758 this.method = method;
6761 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6763 if (a.Target == AttributeTargets.Parameter) {
6764 if (param_attr == null)
6765 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6767 param_attr.ApplyAttributeBuilder (a, cb);
6771 base.ApplyAttributeBuilder (a, cb);
6774 public override AttributeTargets AttributeTargets {
6776 return AttributeTargets.Method;
6780 public override bool IsClsCompliaceRequired(DeclSpace ds)
6782 return method.IsClsCompliaceRequired (ds);
6785 public MethodBuilder Define (TypeContainer container, InternalParameters ip)
6787 method_data = new MethodData (method, ip, method.ModFlags,
6788 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6790 if (!method_data.Define (container))
6793 MethodBuilder mb = method_data.MethodBuilder;
6794 mb.DefineParameter (1, ParameterAttributes.None, "value");
6799 protected override void EmitMethod (TypeContainer tc)
6801 if (block != null) {
6802 base.EmitMethod (tc);
6806 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
6809 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6810 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6812 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6813 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6814 ig.Emit (OpCodes.Ldsfld, field_info);
6815 ig.Emit (OpCodes.Ldarg_0);
6816 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6817 ig.Emit (OpCodes.Castclass, method.MemberType);
6818 ig.Emit (OpCodes.Stsfld, field_info);
6820 ig.Emit (OpCodes.Ldarg_0);
6821 ig.Emit (OpCodes.Ldarg_0);
6822 ig.Emit (OpCodes.Ldfld, field_info);
6823 ig.Emit (OpCodes.Ldarg_1);
6824 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6825 ig.Emit (OpCodes.Castclass, method.MemberType);
6826 ig.Emit (OpCodes.Stfld, field_info);
6828 ig.Emit (OpCodes.Ret);
6831 protected abstract MethodInfo DelegateMethodInfo { get; }
6833 public override Type[] ParameterTypes {
6835 return new Type[] { method.MemberType };
6839 public override Type ReturnType {
6841 return TypeManager.void_type;
6845 public override EmitContext CreateEmitContext (TypeContainer tc,
6848 return new EmitContext (
6849 tc, method.Parent, Location, ig, ReturnType,
6850 method.ModFlags, false);
6853 public override string GetSignatureForError (TypeContainer tc)
6855 return String.Concat (tc.Name, '.', method.Name);
6858 public override ObsoleteAttribute GetObsoleteAttribute ()
6860 return method.GetObsoleteAttribute (method.Parent);
6863 public override string[] ValidAttributeTargets {
6865 return attribute_targets;
6871 const int AllowedModifiers =
6874 Modifiers.PROTECTED |
6875 Modifiers.INTERNAL |
6880 Modifiers.OVERRIDE |
6884 const int AllowedInterfaceModifiers =
6887 public DelegateMethod Add, Remove;
6888 public MyEventBuilder EventBuilder;
6889 public MethodBuilder AddBuilder, RemoveBuilder;
6891 public Event (TypeContainer parent, Expression type, int mod_flags,
6892 bool is_iface, MemberName name, Object init, Attributes attrs,
6894 : base (parent, type, mod_flags,
6895 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6896 name, init, attrs, loc)
6898 IsInterface = is_iface;
6901 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6903 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6904 a.Error_InvalidSecurityParent ();
6908 EventBuilder.SetCustomAttribute (cb);
6911 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6913 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6916 public override AttributeTargets AttributeTargets {
6918 return AttributeTargets.Event;
6922 public override bool Define ()
6924 EventAttributes e_attr;
6925 e_attr = EventAttributes.None;
6930 if (init != null && ((ModFlags & Modifiers.ABSTRACT) != 0)){
6931 Report.Error (74, Location, "'" + Parent.Name + "." + Name +
6932 "': abstract event can not have an initializer");
6936 if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
6937 Report.Error (66, Location, "'" + Parent.Name + "." + Name +
6938 "' : event must be of a delegate type");
6942 EmitContext ec = Parent.EmitContext;
6944 throw new InternalErrorException ("Event.Define called too early?");
6945 bool old_unsafe = ec.InUnsafe;
6946 ec.InUnsafe = InUnsafe;
6948 Parameter [] parms = new Parameter [1];
6949 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
6950 Parameters parameters = new Parameters (parms, null, Location);
6951 Type [] types = parameters.GetParameterInfo (ec);
6952 InternalParameters ip = new InternalParameters (types, parameters);
6954 ec.InUnsafe = old_unsafe;
6960 // Now define the accessors
6963 AddBuilder = Add.Define (Parent, ip);
6964 if (AddBuilder == null)
6967 RemoveBuilder = Remove.Define (Parent, ip);
6968 if (RemoveBuilder == null)
6971 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
6973 if (Add.Block == null && Remove.Block == null && !IsInterface) {
6974 FieldBuilder = Parent.TypeBuilder.DefineField (
6976 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
6977 TypeManager.RegisterPrivateFieldOfEvent (
6978 (EventInfo) EventBuilder, FieldBuilder);
6979 TypeManager.RegisterFieldBase (FieldBuilder, this);
6982 EventBuilder.SetAddOnMethod (AddBuilder);
6983 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
6985 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
6989 protected override bool CheckBase ()
6991 if (!base.CheckBase ())
6994 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
6995 if (!(conflict_symbol is EventInfo)) {
6996 Report.SymbolRelatedToPreviousError (conflict_symbol);
6997 Report.Error (72, Location, "Event '{0}' can override only event", GetSignatureForError (Parent));
7005 public override void Emit ()
7007 if (OptAttributes != null) {
7008 EmitContext ec = new EmitContext (
7009 Parent, Location, null, MemberType, ModFlags);
7010 OptAttributes.Emit (ec, this);
7014 Remove.Emit (Parent);
7019 public override string GetSignatureForError ()
7021 if (EventBuilder == null)
7022 return base.GetSignatureForError (Parent);
7024 return TypeManager.GetFullNameSignature (EventBuilder);
7028 // Represents header string for documentation comment.
7030 public override string DocCommentHeader {
7031 get { return "E:"; }
7036 public class Indexer : PropertyBase {
7038 class GetIndexerMethod: GetMethod
7040 public GetIndexerMethod (MethodCore method):
7045 public GetIndexerMethod (MethodCore method, Accessor accessor):
7046 base (method, accessor)
7050 // TODO: one GetSignatureForError is enough (reuse Parent member)
7051 public override string GetSignatureForError (TypeContainer tc)
7053 string core = base.GetSignatureForError (tc);
7054 return core.Replace (TypeContainer.DefaultIndexerName,
7055 String.Format ("this[{0}]", TypeManager.CSharpName (ParameterTypes)));
7058 public override Type[] ParameterTypes {
7060 return method.ParameterTypes;
7065 class SetIndexerMethod: SetMethod
7067 readonly Parameters parameters;
7069 public SetIndexerMethod (MethodCore method):
7074 public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
7075 base (method, accessor)
7077 this.parameters = parameters;
7080 public override Type[] ParameterTypes {
7082 int top = method.ParameterTypes.Length;
7083 Type [] set_pars = new Type [top + 1];
7084 method.ParameterTypes.CopyTo (set_pars, 0);
7085 set_pars [top] = method.MemberType;
7090 protected override InternalParameters GetParameterInfo (EmitContext ec)
7092 Parameter [] fixed_parms = parameters.FixedParameters;
7094 if (fixed_parms == null){
7095 throw new Exception ("We currently do not support only array arguments in an indexer at: " + method.Location);
7096 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7097 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7099 // Here is the problem: the `value' parameter has
7100 // to come *after* the array parameter in the declaration
7102 // X (object [] x, Type value)
7105 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7106 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7110 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
7112 fixed_parms.CopyTo (tmp, 0);
7113 tmp [fixed_parms.Length] = new Parameter (
7114 method.Type, "value", Parameter.Modifier.NONE, null);
7116 Parameters set_formal_params = new Parameters (tmp, null, method.Location);
7117 Type [] types = set_formal_params.GetParameterInfo (ec);
7119 return new InternalParameters (types, set_formal_params);
7124 const int AllowedModifiers =
7127 Modifiers.PROTECTED |
7128 Modifiers.INTERNAL |
7132 Modifiers.OVERRIDE |
7137 const int AllowedInterfaceModifiers =
7141 public Indexer (TypeContainer ds, Expression type, MemberName name, int mod,
7142 bool is_iface, Parameters parameters, Attributes attrs,
7143 Accessor get_block, Accessor set_block, Location loc)
7144 : base (ds, type, mod,
7145 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7146 is_iface, name, parameters, attrs, loc)
7148 if (get_block == null)
7149 Get = new GetIndexerMethod (this);
7151 Get = new GetIndexerMethod (this, get_block);
7153 if (set_block == null)
7154 Set = new SetIndexerMethod (this);
7156 Set = new SetIndexerMethod (this, parameters, set_block);
7159 public override bool Define ()
7161 PropertyAttributes prop_attr =
7162 PropertyAttributes.RTSpecialName |
7163 PropertyAttributes.SpecialName;
7165 if (!base.Define ())
7168 if (MemberType == TypeManager.void_type) {
7169 Report.Error (620, Location, "Indexers cannot have void type");
7173 if (OptAttributes != null) {
7174 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type, ec);
7175 if (indexer_attr != null) {
7176 // Remove the attribute from the list because it is not emitted
7177 OptAttributes.Attrs.Remove (indexer_attr);
7179 ShortName = indexer_attr.GetIndexerAttributeValue (ec);
7181 if (IsExplicitImpl) {
7182 Report.Error (415, indexer_attr.Location,
7183 "The 'IndexerName' attribute is valid only on an " +
7184 "indexer that is not an explicit interface member declaration");
7188 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7189 Report.Error (609, indexer_attr.Location,
7190 "Cannot set the 'IndexerName' attribute on an indexer marked override");
7194 if (!Tokenizer.IsValidIdentifier (ShortName)) {
7195 Report.Error (633, indexer_attr.Location,
7196 "The argument to the 'IndexerName' attribute must be a valid identifier");
7202 if (InterfaceType != null) {
7203 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7204 if (base_IndexerName != Name)
7205 ShortName = base_IndexerName;
7208 if (!Parent.AddToMemberContainer (this) ||
7209 !Parent.AddToMemberContainer (Get) || !Parent.AddToMemberContainer (Set))
7215 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7217 GetBuilder = Get.Define (Parent);
7218 if (GetBuilder == null)
7223 SetBuilder = Set.Define (Parent);
7224 if (SetBuilder == null)
7229 // Now name the parameters
7231 Parameter [] p = Parameters.FixedParameters;
7233 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
7234 Report.Error (631, Location, "ref and out are not valid in this context");
7240 for (i = 0; i < p.Length; ++i) {
7242 GetBuilder.DefineParameter (
7243 i + 1, p [i].Attributes, p [i].Name);
7246 SetBuilder.DefineParameter (
7247 i + 1, p [i].Attributes, p [i].Name);
7251 SetBuilder.DefineParameter (
7252 i + 1, ParameterAttributes.None, "value");
7254 if (i != ParameterTypes.Length) {
7255 Parameter array_param = Parameters.ArrayParameter;
7257 SetBuilder.DefineParameter (
7258 i + 1, array_param.Attributes, array_param.Name);
7262 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7263 Name, prop_attr, MemberType, ParameterTypes);
7266 PropertyBuilder.SetGetMethod (GetBuilder);
7269 PropertyBuilder.SetSetMethod (SetBuilder);
7271 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
7276 public override string GetSignatureForError ()
7278 if (PropertyBuilder == null)
7279 return GetSignatureForError (Parent);
7281 return TypeManager.CSharpSignature (PropertyBuilder, true);
7284 public override string GetSignatureForError(TypeContainer tc)
7286 return String.Concat (tc.Name, ".this[", Parameters.FixedParameters [0].TypeName.ToString (), ']');
7289 public override bool MarkForDuplicationCheck ()
7291 caching_flags |= Flags.TestMethodDuplication;
7297 public class Operator : MethodCore, IIteratorContainer {
7299 const int AllowedModifiers =
7305 public enum OpType : byte {
7315 // Unary and Binary operators
7338 // Implicit and Explicit
7343 public readonly OpType OperatorType;
7344 public MethodBuilder OperatorMethodBuilder;
7346 public Method OperatorMethod;
7348 static string[] attribute_targets = new string [] { "method", "return" };
7350 public Operator (TypeContainer parent, OpType type, Expression ret_type,
7351 int mod_flags, Parameters parameters,
7352 ToplevelBlock block, Attributes attrs, Location loc)
7353 : base (parent, ret_type, mod_flags, AllowedModifiers, false,
7354 new MemberName ("op_" + type), attrs, parameters, loc)
7356 OperatorType = type;
7360 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7362 OperatorMethod.ApplyAttributeBuilder (a, cb);
7365 public override AttributeTargets AttributeTargets {
7367 return AttributeTargets.Method;
7371 protected override bool CheckForDuplications()
7373 ArrayList ar = Parent.Operators;
7375 int arLen = ar.Count;
7377 for (int i = 0; i < arLen; i++) {
7378 Operator o = (Operator) ar [i];
7379 if (IsDuplicateImplementation (o))
7384 ar = Parent.Methods;
7386 int arLen = ar.Count;
7388 for (int i = 0; i < arLen; i++) {
7389 Method m = (Method) ar [i];
7390 if (IsDuplicateImplementation (m))
7398 public override bool Define ()
7401 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7402 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7403 Report.Error (558, Location, "User defined operators '{0}' must be declared static and public", GetSignatureForError (Parent));
7410 if (MemberType == TypeManager.void_type) {
7411 Report.Error (590, Location, "User-defined operators cannot return void");
7415 OperatorMethod = new Method (
7416 Parent, Type, ModFlags, false, MemberName,
7417 Parameters, OptAttributes, Location);
7419 OperatorMethod.Block = Block;
7420 OperatorMethod.IsOperator = true;
7421 OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7422 OperatorMethod.Define ();
7424 if (OperatorMethod.MethodBuilder == null)
7427 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
7429 parameter_types = OperatorMethod.ParameterTypes;
7430 Type declaring_type = OperatorMethodBuilder.DeclaringType;
7431 Type return_type = OperatorMethod.ReturnType;
7432 Type first_arg_type = parameter_types [0];
7437 // Rules for conversion operators
7439 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7440 if (first_arg_type == return_type && first_arg_type == declaring_type){
7443 "User-defined conversion cannot take an object of the " +
7444 "enclosing type and convert to an object of the enclosing" +
7449 if (first_arg_type != declaring_type && return_type != declaring_type){
7452 "User-defined conversion must convert to or from the " +
7457 if (first_arg_type == TypeManager.object_type ||
7458 return_type == TypeManager.object_type){
7461 "User-defined conversion cannot convert to or from " +
7466 if (first_arg_type.IsInterface || return_type.IsInterface){
7469 "User-defined conversion cannot convert to or from an " +
7474 if (first_arg_type.IsSubclassOf (return_type)
7475 || return_type.IsSubclassOf (first_arg_type)){
7476 if (declaring_type.IsSubclassOf (return_type)) {
7477 Report.Error (553, Location, "'{0}' : user defined conversion to/from base class", GetSignatureForError ());
7480 Report.Error (554, Location, "'{0}' : user defined conversion to/from derived class", GetSignatureForError ());
7483 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7484 if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
7485 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");
7488 } else if (Parameters.FixedParameters.Length == 1) {
7489 // Checks for Unary operators
7491 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7492 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7493 Report.Error (448, Location,
7494 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7497 if (first_arg_type != declaring_type) {
7499 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7504 if (first_arg_type != declaring_type){
7507 "The parameter of a unary operator must be the " +
7512 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7513 if (return_type != TypeManager.bool_type){
7516 "The return type of operator True or False " +
7523 // Checks for Binary operators
7525 if (first_arg_type != declaring_type &&
7526 parameter_types [1] != declaring_type){
7529 "One of the parameters of a binary operator must " +
7530 "be the containing type");
7538 public override void Emit ()
7541 // abstract or extern methods have no bodies
7543 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7546 OperatorMethod.Emit ();
7550 // Operator cannot be override
7551 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
7556 public static string GetName (OpType ot)
7559 case OpType.LogicalNot:
7561 case OpType.OnesComplement:
7563 case OpType.Increment:
7565 case OpType.Decrement:
7571 case OpType.Addition:
7573 case OpType.Subtraction:
7575 case OpType.UnaryPlus:
7577 case OpType.UnaryNegation:
7579 case OpType.Multiply:
7581 case OpType.Division:
7583 case OpType.Modulus:
7585 case OpType.BitwiseAnd:
7587 case OpType.BitwiseOr:
7589 case OpType.ExclusiveOr:
7591 case OpType.LeftShift:
7593 case OpType.RightShift:
7595 case OpType.Equality:
7597 case OpType.Inequality:
7599 case OpType.GreaterThan:
7601 case OpType.LessThan:
7603 case OpType.GreaterThanOrEqual:
7605 case OpType.LessThanOrEqual:
7607 case OpType.Implicit:
7609 case OpType.Explicit:
7615 public override string GetSignatureForError (TypeContainer tc)
7617 StringBuilder sb = new StringBuilder ();
7618 sb.AppendFormat ("{0}.operator {1} {2}({3}", tc.Name, GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type),
7619 Parameters.FixedParameters [0].GetSignatureForError ());
7621 if (Parameters.FixedParameters.Length > 1) {
7623 sb.Append (Parameters.FixedParameters [1].GetSignatureForError ());
7626 return sb.ToString ();
7629 public override string GetSignatureForError ()
7634 public override bool MarkForDuplicationCheck ()
7636 caching_flags |= Flags.TestMethodDuplication;
7640 public override string ToString ()
7642 if (OperatorMethod == null)
7645 Type return_type = OperatorMethod.ReturnType;
7646 Type [] param_types = OperatorMethod.ParameterTypes;
7648 if (Parameters.FixedParameters.Length == 1)
7649 return String.Format (
7650 "{0} operator {1}({2})",
7651 TypeManager.CSharpName (return_type),
7652 GetName (OperatorType),
7655 return String.Format (
7656 "{0} operator {1}({2}, {3})",
7657 TypeManager.CSharpName (return_type),
7658 GetName (OperatorType),
7659 param_types [0], param_types [1]);
7662 public override string[] ValidAttributeTargets {
7664 return attribute_targets;
7668 public void SetYields ()
7670 ModFlags |= Modifiers.METHOD_YIELDS;
7675 // This is used to compare method signatures
7677 struct MethodSignature {
7679 public Type RetType;
7680 public Type [] Parameters;
7683 /// This delegate is used to extract methods which have the
7684 /// same signature as the argument
7686 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7688 public MethodSignature (string name, Type ret_type, Type [] parameters)
7693 if (parameters == null)
7694 Parameters = TypeManager.NoTypes;
7696 Parameters = parameters;
7699 public override string ToString ()
7702 if (Parameters.Length != 0){
7703 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7704 for (int i = 0; i < Parameters.Length; i++){
7705 sb.Append (Parameters [i]);
7706 if (i+1 < Parameters.Length)
7709 pars = sb.ToString ();
7712 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7715 public override int GetHashCode ()
7717 return Name.GetHashCode ();
7720 public override bool Equals (Object o)
7722 MethodSignature other = (MethodSignature) o;
7724 if (other.Name != Name)
7727 if (other.RetType != RetType)
7730 if (Parameters == null){
7731 if (other.Parameters == null)
7736 if (other.Parameters == null)
7739 int c = Parameters.Length;
7740 if (other.Parameters.Length != c)
7743 for (int i = 0; i < c; i++)
7744 if (other.Parameters [i] != Parameters [i])
7750 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7752 MethodSignature sig = (MethodSignature) filter_criteria;
7754 if (m.Name != sig.Name)
7758 MethodInfo mi = m as MethodInfo;
7759 PropertyInfo pi = m as PropertyInfo;
7762 ReturnType = mi.ReturnType;
7763 else if (pi != null)
7764 ReturnType = pi.PropertyType;
7769 // we use sig.RetType == null to mean `do not check the
7770 // method return value.
7772 if (sig.RetType != null)
7773 if (ReturnType != sig.RetType)
7778 args = TypeManager.GetArgumentTypes (mi);
7780 args = TypeManager.GetArgumentTypes (pi);
7781 Type [] sigp = sig.Parameters;
7783 if (args.Length != sigp.Length)
7786 for (int i = args.Length; i > 0; ){
7788 if (args [i] != sigp [i])