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 // Skip erroneous code.
283 if (op.OperatorMethod == null)
286 switch (op.OperatorType){
287 case Operator.OpType.Equality:
289 has_equality_or_inequality = true;
291 case Operator.OpType.Inequality:
293 has_equality_or_inequality = true;
296 case Operator.OpType.True:
299 case Operator.OpType.False:
303 case Operator.OpType.GreaterThan:
305 case Operator.OpType.LessThan:
308 case Operator.OpType.GreaterThanOrEqual:
310 case Operator.OpType.LessThanOrEqual:
316 OperatorEntry oe = new OperatorEntry (reg, op);
318 object o = pairs [oe];
322 oe = (OperatorEntry) o;
327 if (true_op != null){
328 if (false_op == null)
329 Report.Error (216, true_op.Location, "The operator `{0}' requires a matching operator `false' to also be defined",
330 true_op.GetSignatureForError ());
331 } else if (false_op != null)
332 Report.Error (216, false_op.Location, "The operator `{0}' requires a matching operator `true' to also be defined",
333 false_op.GetSignatureForError ());
336 // Look for the mistakes.
338 foreach (DictionaryEntry de in pairs){
339 OperatorEntry oe = (OperatorEntry) de.Key;
346 case Operator.OpType.Equality:
349 case Operator.OpType.Inequality:
352 case Operator.OpType.GreaterThan:
355 case Operator.OpType.LessThan:
358 case Operator.OpType.GreaterThanOrEqual:
361 case Operator.OpType.LessThanOrEqual:
365 Report.Error (216, oe.op.Location,
366 "The operator `{0}' requires a matching operator `{1}' to also be defined",
367 oe.op.GetSignatureForError (), s);
370 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
371 if (container.Methods == null || !container.Methods.HasEquals)
372 Report.Warning (660, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
374 if (container.Methods == null || !container.Methods.HasGetHashCode)
375 Report.Warning (661, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
379 public override void DefineContainerMembers ()
381 base.DefineContainerMembers ();
382 CheckPairedOperators ();
387 // Whether this is a struct, class or interface
388 public readonly Kind Kind;
390 // Holds a list of classes and structures
393 // Holds the list of properties
394 MemberCoreArrayList properties;
396 // Holds the list of enumerations
397 MemberCoreArrayList enums;
399 // Holds the list of delegates
400 MemberCoreArrayList delegates;
402 // Holds the list of constructors
403 protected MemberCoreArrayList instance_constructors;
405 // Holds the list of fields
406 MemberCoreArrayList fields;
408 // Holds a list of fields that have initializers
409 protected ArrayList initialized_fields;
411 // Holds a list of static fields that have initializers
412 protected ArrayList initialized_static_fields;
414 // Holds the list of constants
415 MemberCoreArrayList constants;
418 MemberCoreArrayList interfaces;
420 // Holds the methods.
421 MethodArrayList methods;
424 protected MemberCoreArrayList events;
426 // Holds the indexers
427 IndexerArrayList indexers;
429 // Holds the operators
430 MemberCoreArrayList operators;
432 // Holds the iterators
435 // Holds the parts of a partial class;
439 // Pointers to the default constructor and the default static constructor
441 protected Constructor default_constructor;
442 protected Constructor default_static_constructor;
445 // Points to the first non-static field added to the container.
447 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
448 // and the first one's as good as any.
450 FieldBase first_nonstatic_field = null;
453 // This one is computed after we can distinguish interfaces
454 // from classes from the arraylist `type_bases'
456 string base_class_name;
458 TypeExpr[] iface_exprs;
460 ArrayList type_bases;
462 bool members_defined;
463 bool members_defined_ok;
465 // The interfaces we implement.
466 protected Type[] ifaces;
467 protected Type ptype;
469 // The base member cache and our member cache
470 MemberCache base_cache;
471 MemberCache member_cache;
473 public const string DefaultIndexerName = "Item";
475 public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
476 Attributes attrs, Kind kind)
477 : base (ns, parent, name, attrs)
479 if (parent != null && parent != RootContext.Tree.Types && parent.NamespaceEntry != ns)
480 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
484 types = new ArrayList ();
486 base_class_name = null;
489 public bool AddToMemberContainer (MemberCore symbol)
491 return AddToContainer (symbol, symbol.Name);
494 protected virtual bool AddToTypeContainer (DeclSpace ds)
496 return AddToContainer (ds, ds.Basename);
499 public void AddConstant (Const constant)
501 if (!AddToMemberContainer (constant))
504 if (constants == null)
505 constants = new MemberCoreArrayList ();
507 constants.Add (constant);
510 public void AddEnum (Mono.CSharp.Enum e)
512 if (!AddToTypeContainer (e))
516 enums = new MemberCoreArrayList ();
521 public bool AddClassOrStruct (TypeContainer c)
523 if (!AddToTypeContainer (c))
530 public void AddDelegate (Delegate d)
532 if (!AddToTypeContainer (d))
535 if (delegates == null)
536 delegates = new MemberCoreArrayList ();
541 public void AddMethod (Method method)
543 if (!AddToMemberContainer (method))
547 methods = new MethodArrayList (this);
549 if (method.MemberName.Left != null)
550 methods.Insert (0, method);
552 methods.Add (method);
555 public void AddConstructor (Constructor c)
557 if (c.Name != Basename) {
558 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
561 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
564 if (default_static_constructor != null) {
565 Report.SymbolRelatedToPreviousError (default_static_constructor);
566 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
570 default_static_constructor = c;
573 if (default_constructor != null) {
574 Report.SymbolRelatedToPreviousError (default_constructor);
575 Report.Error (111, c.Location, Error111, c.Location, c.GetSignatureForError ());
578 default_constructor = c;
581 if (instance_constructors == null)
582 instance_constructors = new MemberCoreArrayList ();
584 instance_constructors.Add (c);
588 internal static string Error111 {
590 return "`{0}' is already defined. Rename this member or use different parameter types";
594 public bool AddInterface (TypeContainer iface)
596 if (!AddToTypeContainer (iface))
599 if (interfaces == null) {
600 interfaces = new MemberCoreArrayList ();
603 interfaces.Add (iface);
607 public void AddField (FieldMember field)
609 if (!AddToMemberContainer (field))
613 fields = new MemberCoreArrayList ();
617 if (field.HasInitializer)
618 RegisterFieldForInitialization (field);
620 if ((field.ModFlags & Modifiers.STATIC) != 0)
623 if (first_nonstatic_field == null) {
624 first_nonstatic_field = field;
628 if (Kind == Kind.Struct &&
629 first_nonstatic_field.Parent != field.Parent &&
630 RootContext.WarningLevel >= 3) {
631 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
632 Report.Warning (282, field.Location,
633 "struct instance field `{0}' found in different declaration from instance field `{1}'",
634 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
638 public void AddProperty (Property prop)
640 if (!AddToMemberContainer (prop) ||
641 !AddToMemberContainer (prop.Get) || !AddToMemberContainer (prop.Set))
644 if (properties == null)
645 properties = new MemberCoreArrayList ();
647 if (prop.MemberName.Left != null)
648 properties.Insert (0, prop);
650 properties.Add (prop);
653 public void AddEvent (Event e)
655 if (!AddToMemberContainer (e))
658 if (e is EventProperty) {
659 if (!AddToMemberContainer (e.Add))
662 if (!AddToMemberContainer (e.Remove))
667 events = new MemberCoreArrayList ();
674 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
676 public void AddIndexer (Indexer i)
678 if (indexers == null)
679 indexers = new IndexerArrayList (this);
681 if (i.IsExplicitImpl)
682 indexers.Insert (0, i);
687 public void AddOperator (Operator op)
689 if (!AddToMemberContainer (op))
692 if (operators == null)
693 operators = new OperatorArrayList (this);
698 public void AddIterator (Iterator i)
700 if (iterators == null)
701 iterators = new ArrayList ();
706 public void AddType (TypeContainer tc)
711 public void AddPart (ClassPart part)
714 parts = new ArrayList ();
719 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
721 if (a.Type == TypeManager.default_member_type) {
722 if (Indexers != null) {
723 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
728 base.ApplyAttributeBuilder (a, cb);
731 public override AttributeTargets AttributeTargets {
735 return AttributeTargets.Class;
737 return AttributeTargets.Struct;
739 return AttributeTargets.Interface;
741 throw new NotSupportedException ();
746 public ArrayList Types {
752 public MethodArrayList Methods {
758 public ArrayList Constants {
764 public ArrayList Interfaces {
770 public ArrayList Iterators {
778 return base_class_name;
782 public ArrayList Bases {
792 public ArrayList Fields {
798 public ArrayList InstanceConstructors {
800 return instance_constructors;
804 public ArrayList Properties {
810 public ArrayList Events {
816 public ArrayList Enums {
822 public ArrayList Indexers {
828 public ArrayList Operators {
834 public ArrayList Delegates {
840 public ArrayList Parts {
846 protected override TypeAttributes TypeAttr {
848 return Modifiers.TypeAttr (ModFlags, this) | base.TypeAttr;
852 public string IndexerName {
854 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
858 public virtual void RegisterFieldForInitialization (FieldMember field)
860 if ((field.ModFlags & Modifiers.STATIC) != 0){
861 if (initialized_static_fields == null)
862 initialized_static_fields = new ArrayList ();
864 initialized_static_fields.Add (field);
866 if (initialized_fields == null)
867 initialized_fields = new ArrayList ();
869 initialized_fields.Add (field);
873 bool CanElideInitializer (Type field_type, Constant c)
875 if (field_type == c.Type)
877 if (TypeManager.IsValueType (field_type) || TypeManager.HasElementType (field_type))
879 // Reference type with null initializer.
880 return c.Type == TypeManager.null_type;
884 // Emits the instance field initializers
886 public virtual bool EmitFieldInitializers (EmitContext ec)
889 Expression instance_expr;
892 fields = initialized_static_fields;
893 instance_expr = null;
895 fields = initialized_fields;
896 instance_expr = new This (Location.Null).Resolve (ec);
902 foreach (FieldMember f in fields){
903 Expression e = f.GetInitializerExpression (ec);
907 Location l = f.Location;
908 FieldExpr fe = new FieldExpr (f.FieldBuilder, l, true);
909 fe.InstanceExpression = instance_expr;
910 ExpressionStatement a = new Assign (fe, e, l);
912 a = a.ResolveStatement (ec);
916 Constant c = e as Constant;
917 if (c != null && c.IsDefaultValue && CanElideInitializer (f.MemberType, c))
920 a.EmitStatement (ec);
927 // Defines the default constructors
929 protected void DefineDefaultConstructor (bool is_static)
933 // The default constructor is public
934 // If the class is abstract, the default constructor is protected
935 // The default static constructor is private
937 int mods = Modifiers.PUBLIC;
939 mods = Modifiers.STATIC | Modifiers.PRIVATE;
940 else if ((ModFlags & Modifiers.ABSTRACT) != 0)
941 mods = Modifiers.PROTECTED;
943 TypeContainer constructor_parent = this;
945 constructor_parent = (TypeContainer) Parts [0];
947 c = new Constructor (constructor_parent, Basename, mods,
948 Parameters.EmptyReadOnlyParameters,
949 new GeneratedBaseInitializer (Location),
954 c.Block = new ToplevelBlock (null, Location);
959 /// The pending methods that need to be implemented
960 // (interfaces or abstract methods)
962 public PendingImplementation Pending;
964 public abstract PendingImplementation GetPendingImplementations ();
966 TypeExpr[] GetPartialBases (out TypeExpr base_class)
968 ArrayList ifaces = new ArrayList ();
971 Location base_loc = Location.Null;
973 foreach (ClassPart part in parts) {
974 TypeExpr new_base_class;
975 TypeExpr[] new_ifaces;
977 new_ifaces = part.GetClassBases (out new_base_class);
978 if (new_ifaces == null && new_base_class != null)
981 if ((base_class != null) && (new_base_class != null) &&
982 !base_class.Equals (new_base_class)) {
983 Report.Error (263, part.Location,
984 "Partial declarations of `{0}' must " +
985 "not specify different base classes",
988 if (!base_loc.IsNull)
989 Report.LocationOfPreviousError (base_loc);
994 if ((base_class == null) && (new_base_class != null)) {
995 base_class = new_base_class;
996 base_loc = part.Location;
999 if (new_ifaces == null)
1002 foreach (TypeExpr iface in new_ifaces) {
1004 foreach (TypeExpr old_iface in ifaces) {
1005 if (old_iface.Equals (iface)) {
1016 TypeExpr[] retval = new TypeExpr [ifaces.Count];
1017 ifaces.CopyTo (retval, 0);
1021 TypeExpr[] GetNormalBases (out TypeExpr base_class)
1025 int count = Bases.Count;
1026 int start = 0, i, j;
1028 if (Kind == Kind.Class){
1029 TypeExpr name = ResolveBaseTypeExpr (
1030 (Expression) Bases [0], false, Location);
1036 if (!name.IsInterface) {
1037 // base_class could be a class, struct, enum, delegate.
1038 // This is validated in GetClassBases.
1044 TypeExpr [] ifaces = new TypeExpr [count-start];
1046 for (i = start, j = 0; i < count; i++, j++){
1047 TypeExpr resolved = ResolveBaseTypeExpr ((Expression) Bases [i], false, Location);
1048 if (resolved == null) {
1052 ifaces [j] = resolved;
1059 /// This function computes the Base class and also the
1060 /// list of interfaces that the class or struct @c implements.
1062 /// The return value is an array (might be null) of
1063 /// interfaces implemented (as Types).
1065 /// The @base_class argument is set to the base object or null
1066 /// if this is `System.Object'.
1068 TypeExpr [] GetClassBases (out TypeExpr base_class)
1075 ifaces = GetPartialBases (out base_class);
1076 else if (Bases == null){
1080 ifaces = GetNormalBases (out base_class);
1085 if ((base_class != null) && (Kind == Kind.Class)){
1087 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
1088 Report.Error (1521, base_class.Location, "Invalid base type");
1092 if (base_class.IsSealed){
1093 Report.SymbolRelatedToPreviousError (base_class.Type);
1094 if (base_class.Type.IsAbstract) {
1095 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
1096 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
1098 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
1099 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
1104 if (!base_class.CanInheritFrom ()){
1105 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
1106 GetSignatureForError (), base_class.GetSignatureForError ());
1110 if (!base_class.AsAccessible (this, ModFlags)) {
1111 Report.SymbolRelatedToPreviousError (base_class.Type);
1112 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
1113 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
1117 if (base_class != null)
1118 base_class_name = base_class.Name;
1123 int count = ifaces != null ? ifaces.Length : 0;
1125 for (i = 0; i < count; i++) {
1126 TypeExpr iface = (TypeExpr) ifaces [i];
1128 if (!iface.IsInterface) {
1129 if (Kind != Kind.Class) {
1130 // TODO: location of symbol related ....
1131 Error_TypeInListIsNotInterface (Location, iface.FullName);
1133 else if (base_class != null)
1134 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
1135 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
1137 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
1138 GetSignatureForError (), iface.GetSignatureForError ());
1143 for (int x = 0; x < i; x++) {
1144 if (iface.Equals (ifaces [x])) {
1145 Report.Error (528, Location,
1146 "`{0}' is already listed in " +
1147 "interface list", iface.Name);
1152 if ((Kind == Kind.Interface) &&
1153 !iface.AsAccessible (Parent, ModFlags)) {
1154 Report.Error (61, Location,
1155 "Inconsistent accessibility: base " +
1156 "interface `{0}' is less accessible " +
1157 "than interface `{1}'", iface.Name,
1167 protected void Error_TypeInListIsNotInterface (Location loc, string type)
1169 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
1173 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1175 public override TypeBuilder DefineType ()
1180 if (TypeBuilder != null)
1183 TypeAttributes type_attributes = TypeAttr;
1187 if (TypeManager.NamespaceClash (Name, Location)) {
1192 ModuleBuilder builder = CodeGen.Module.Builder;
1193 TypeBuilder = builder.DefineType (
1194 Name, type_attributes, null, null);
1196 TypeBuilder builder = Parent.TypeBuilder;
1197 if (builder == null) {
1202 TypeBuilder = builder.DefineNestedType (
1203 Basename, type_attributes, ptype, null);
1205 } catch (ArgumentException) {
1206 Report.RuntimeMissingSupport (Location, "static classes");
1211 TypeManager.AddUserType (Name, this);
1213 if (Parts != null) {
1215 foreach (ClassPart part in Parts) {
1216 part.TypeBuilder = TypeBuilder;
1217 part.ec = new EmitContext (part, Mono.CSharp.Location.Null, null, null, ModFlags);
1218 part.ec.ContainerType = TypeBuilder;
1222 // Normally, we create the EmitContext here.
1223 // The only exception is if we're an Iterator - in this case,
1224 // we already have the `ec', so we don't want to create a new one.
1227 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
1228 ec.ContainerType = TypeBuilder;
1231 iface_exprs = GetClassBases (out base_type);
1232 if (iface_exprs == null && base_type != null) {
1238 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1239 // which in turn should have called DefineType()s on base types if necessary.
1241 // None of the code below should trigger DefineType()s on classes that we depend on.
1242 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1244 // Let's do it as soon as possible, since code below can call DefineType() on classes
1245 // that depend on us to be populated before they are.
1247 if (!(this is Iterator))
1248 RootContext.RegisterOrder (this);
1250 if (base_type == null) {
1251 if (Kind == Kind.Class){
1252 if (RootContext.StdLib)
1253 base_type = TypeManager.system_object_expr;
1254 else if (Name != "System.Object")
1255 base_type = TypeManager.system_object_expr;
1256 } else if (Kind == Kind.Struct) {
1258 // If we are compiling our runtime,
1259 // and we are defining ValueType, then our
1260 // base is `System.Object'.
1262 if (!RootContext.StdLib && Name == "System.ValueType")
1263 base_type = TypeManager.system_object_expr;
1265 base_type = TypeManager.system_valuetype_expr;
1269 if ((Kind == Kind.Struct) && TypeManager.value_type == null)
1270 throw new Exception ();
1272 // Avoid attributes check when parent is not set
1273 TypeResolveEmitContext.TestObsoleteMethodUsage = false;
1275 if (base_type != null) {
1276 // FIXME: I think this should be ...ResolveType (Parent.EmitContext).
1277 // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
1278 ptype = base_type.ResolveType (TypeResolveEmitContext);
1279 if (ptype == null) {
1285 if (!CheckRecursiveDefinition (this)) {
1290 if (ptype != null) {
1291 TypeBuilder.SetParent (ptype);
1294 // Attribute is undefined at the begining of corlib compilation
1295 if (TypeManager.obsolete_attribute_type != null) {
1296 TypeResolveEmitContext.TestObsoleteMethodUsage = GetObsoleteAttribute () == null;
1297 if (ptype != null && TypeResolveEmitContext.TestObsoleteMethodUsage) {
1298 CheckObsoleteType (base_type);
1302 // add interfaces that were not added at type creation
1303 if (iface_exprs != null) {
1304 // FIXME: I think this should be ...ExpandInterfaces (Parent.EmitContext, ...).
1305 // However, if Parent == RootContext.Tree.Types, its NamespaceEntry will be null.
1306 TypeResolveEmitContext.ContainerType = TypeBuilder;
1307 ifaces = TypeManager.ExpandInterfaces (TypeResolveEmitContext, iface_exprs);
1308 if (ifaces == null) {
1313 foreach (Type itype in ifaces)
1314 TypeBuilder.AddInterfaceImplementation (itype);
1316 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1319 if (!DefineNestedTypes ()) {
1327 protected virtual bool DefineNestedTypes ()
1329 if (Interfaces != null) {
1330 foreach (TypeContainer iface in Interfaces)
1331 if (iface.DefineType () == null)
1335 if (Types != null) {
1336 foreach (TypeContainer tc in Types)
1337 if (tc.DefineType () == null)
1341 if (Delegates != null) {
1342 foreach (Delegate d in Delegates)
1343 if (d.DefineType () == null)
1347 if (Enums != null) {
1348 foreach (Enum en in Enums)
1349 if (en.DefineType () == null)
1356 TypeContainer InTransit;
1358 protected bool CheckRecursiveDefinition (TypeContainer tc)
1360 if (InTransit != null) {
1361 Report.SymbolRelatedToPreviousError (this);
1362 if (this is Interface)
1364 529, tc.Location, "Inherited interface `{0}' causes a " +
1365 "cycle in the interface hierarchy of `{1}'",
1366 GetSignatureForError (), tc.GetSignatureForError ());
1369 146, tc.Location, "Circular base class dependency " +
1370 "involving `{0}' and `{1}'",
1371 tc.GetSignatureForError (), GetSignatureForError ());
1377 Type parent = ptype;
1378 if (parent != null) {
1379 TypeContainer ptc = TypeManager.LookupTypeContainer (parent);
1380 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1384 if (iface_exprs != null) {
1385 foreach (TypeExpr iface in iface_exprs) {
1386 Type itype = iface.Type;
1388 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1389 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1398 public static void Error_KeywordNotAllowed (Location loc)
1400 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1404 /// Populates our TypeBuilder with fields and methods
1406 public override bool DefineMembers (TypeContainer container)
1408 if (members_defined)
1409 return members_defined_ok;
1411 if (!base.DefineMembers (container))
1414 members_defined_ok = DoDefineMembers ();
1415 members_defined = true;
1417 return members_defined_ok;
1420 protected virtual bool DoDefineMembers ()
1423 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1424 if (conflict_symbol == null) {
1425 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1426 Report.Warning (109, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1428 if ((ModFlags & Modifiers.NEW) == 0) {
1429 Report.SymbolRelatedToPreviousError (conflict_symbol);
1430 Report.Warning (108, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1431 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1436 DefineContainerMembers (constants);
1437 DefineContainerMembers (fields);
1439 if ((Kind == Kind.Class) && !(this is ClassPart)){
1440 if ((instance_constructors == null) &&
1441 !(this is StaticClass)) {
1442 if (default_constructor == null)
1443 DefineDefaultConstructor (false);
1446 if (initialized_static_fields != null &&
1447 default_static_constructor == null)
1448 DefineDefaultConstructor (true);
1451 if (Kind == Kind.Struct){
1453 // Structs can not have initialized instance
1456 if (initialized_static_fields != null &&
1457 default_static_constructor == null)
1458 DefineDefaultConstructor (true);
1460 if (initialized_fields != null)
1461 ReportStructInitializedInstanceError ();
1464 Pending = GetPendingImplementations ();
1466 if (parts != null) {
1467 foreach (ClassPart part in parts) {
1468 if (!part.DefineMembers (this))
1474 // Constructors are not in the defined_names array
1476 DefineContainerMembers (instance_constructors);
1478 if (default_static_constructor != null)
1479 default_static_constructor.Define ();
1481 DefineContainerMembers (properties);
1482 DefineContainerMembers (events);
1483 DefineContainerMembers (indexers);
1484 DefineContainerMembers (methods);
1485 DefineContainerMembers (operators);
1486 DefineContainerMembers (enums);
1487 DefineContainerMembers (delegates);
1490 if (!(this is ClassPart))
1491 member_cache = new MemberCache (this);
1494 if (parts != null) {
1495 foreach (ClassPart part in parts)
1496 part.member_cache = member_cache;
1499 if (iterators != null) {
1500 foreach (Iterator iterator in iterators) {
1501 if (iterator.DefineType () == null)
1505 foreach (Iterator iterator in iterators) {
1506 if (!iterator.DefineMembers (this))
1514 void ReportStructInitializedInstanceError ()
1516 foreach (Field f in initialized_fields){
1517 Report.Error (573, Location,
1518 "`{0}': Structs cannot have instance field initializers",
1519 f.GetSignatureForError ());
1523 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1526 mcal.DefineContainerMembers ();
1529 public override bool Define ()
1531 if (parts != null) {
1532 foreach (ClassPart part in parts) {
1533 if (!part.Define ())
1538 if (iterators != null) {
1539 foreach (Iterator iterator in iterators) {
1540 if (!iterator.Define ())
1548 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1550 return BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1554 /// This function is based by a delegate to the FindMembers routine
1556 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1562 /// This filter is used by FindMembers, and we just keep
1563 /// a global for the filter to `AlwaysAccept'
1565 static MemberFilter accepting_filter;
1568 static TypeContainer ()
1570 accepting_filter = new MemberFilter (AlwaysAccept);
1573 public MethodInfo[] GetMethods ()
1575 ArrayList members = new ArrayList ();
1577 DefineMembers (null);
1579 if (methods != null) {
1580 int len = methods.Count;
1581 for (int i = 0; i < len; i++) {
1582 Method m = (Method) methods [i];
1584 members.Add (m.MethodBuilder);
1588 if (operators != null) {
1589 int len = operators.Count;
1590 for (int i = 0; i < len; i++) {
1591 Operator o = (Operator) operators [i];
1593 members.Add (o.OperatorMethodBuilder);
1597 if (properties != null) {
1598 int len = properties.Count;
1599 for (int i = 0; i < len; i++) {
1600 Property p = (Property) properties [i];
1602 if (p.GetBuilder != null)
1603 members.Add (p.GetBuilder);
1604 if (p.SetBuilder != null)
1605 members.Add (p.SetBuilder);
1609 if (indexers != null) {
1610 int len = indexers.Count;
1611 for (int i = 0; i < len; i++) {
1612 Indexer ix = (Indexer) indexers [i];
1614 if (ix.GetBuilder != null)
1615 members.Add (ix.GetBuilder);
1616 if (ix.SetBuilder != null)
1617 members.Add (ix.SetBuilder);
1621 if (events != null) {
1622 int len = events.Count;
1623 for (int i = 0; i < len; i++) {
1624 Event e = (Event) events [i];
1626 if (e.AddBuilder != null)
1627 members.Add (e.AddBuilder);
1628 if (e.RemoveBuilder != null)
1629 members.Add (e.RemoveBuilder);
1633 MethodInfo[] retMethods = new MethodInfo [members.Count];
1634 members.CopyTo (retMethods, 0);
1638 // Indicated whether container has StructLayout attribute set Explicit
1639 public virtual bool HasExplicitLayout {
1645 public override Type FindNestedType (string name)
1647 ArrayList [] lists = { types, enums, delegates, interfaces };
1649 for (int j = 0; j < lists.Length; ++j) {
1650 ArrayList list = lists [j];
1654 int len = list.Count;
1655 for (int i = 0; i < len; ++i) {
1656 DeclSpace ds = (DeclSpace) list [i];
1657 if (ds.Basename == name) {
1659 return ds.TypeBuilder;
1667 private void FindMembers_NestedTypes (int modflags,
1668 BindingFlags bf, MemberFilter filter, object criteria,
1669 ref ArrayList members)
1671 ArrayList [] lists = { types, enums, delegates, interfaces };
1673 for (int j = 0; j < lists.Length; ++j) {
1674 ArrayList list = lists [j];
1678 int len = list.Count;
1679 for (int i = 0; i < len; i++) {
1680 DeclSpace ds = (DeclSpace) list [i];
1682 if ((ds.ModFlags & modflags) == 0)
1685 TypeBuilder tb = ds.TypeBuilder;
1687 if (!(criteria is string) || ds.Basename.Equals (criteria))
1688 tb = ds.DefineType ();
1691 if (tb != null && (filter (tb, criteria) == true)) {
1692 if (members == null)
1693 members = new ArrayList ();
1702 /// This method returns the members of this type just like Type.FindMembers would
1703 /// Only, we need to use this for types which are _being_ defined because MS'
1704 /// implementation can't take care of that.
1707 // FIXME: return an empty static array instead of null, that cleans up
1708 // some code and is consistent with some coding conventions I just found
1712 // Notice that in various cases we check if our field is non-null,
1713 // something that would normally mean that there was a bug elsewhere.
1715 // The problem happens while we are defining p-invoke methods, as those
1716 // will trigger a FindMembers, but this happens before things are defined
1718 // Since the whole process is a no-op, it is fine to check for null here.
1720 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1721 MemberFilter filter, object criteria)
1723 ArrayList members = null;
1726 if ((bf & BindingFlags.Public) != 0)
1727 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1729 if ((bf & BindingFlags.NonPublic) != 0)
1730 modflags |= Modifiers.PRIVATE;
1732 int static_mask = 0, static_flags = 0;
1733 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1734 case BindingFlags.Static:
1735 static_mask = static_flags = Modifiers.STATIC;
1738 case BindingFlags.Instance:
1739 static_mask = Modifiers.STATIC;
1744 static_mask = static_flags = 0;
1748 Timer.StartTimer (TimerType.TcFindMembers);
1751 filter = accepting_filter;
1753 if ((mt & MemberTypes.Field) != 0) {
1754 if (fields != null) {
1755 int len = fields.Count;
1756 for (int i = 0; i < len; i++) {
1757 FieldMember f = (FieldMember) fields [i];
1759 if ((f.ModFlags & modflags) == 0)
1761 if ((f.ModFlags & static_mask) != static_flags)
1764 FieldBuilder fb = f.FieldBuilder;
1765 if (fb != null && filter (fb, criteria) == true) {
1766 if (members == null)
1767 members = new ArrayList ();
1774 if (constants != null) {
1775 int len = constants.Count;
1776 for (int i = 0; i < len; i++) {
1777 Const con = (Const) constants [i];
1779 if ((con.ModFlags & modflags) == 0)
1781 if ((con.ModFlags & static_mask) != static_flags)
1784 FieldBuilder fb = con.FieldBuilder;
1787 fb = con.FieldBuilder;
1789 if (fb != null && filter (fb, criteria) == true) {
1790 if (members == null)
1791 members = new ArrayList ();
1799 if ((mt & MemberTypes.Method) != 0) {
1800 if (methods != null) {
1801 int len = methods.Count;
1802 for (int i = 0; i < len; i++) {
1803 Method m = (Method) methods [i];
1805 if ((m.ModFlags & modflags) == 0)
1807 if ((m.ModFlags & static_mask) != static_flags)
1810 MethodBuilder mb = m.MethodBuilder;
1812 if (mb != null && filter (mb, criteria) == true) {
1813 if (members == null)
1814 members = new ArrayList ();
1821 if (operators != null) {
1822 int len = operators.Count;
1823 for (int i = 0; i < len; i++) {
1824 Operator o = (Operator) operators [i];
1826 if ((o.ModFlags & modflags) == 0)
1828 if ((o.ModFlags & static_mask) != static_flags)
1831 MethodBuilder ob = o.OperatorMethodBuilder;
1832 if (ob != null && filter (ob, criteria) == true) {
1833 if (members == null)
1834 members = new ArrayList ();
1841 if (properties != null) {
1842 int len = properties.Count;
1843 for (int i = 0; i < len; i++) {
1844 Property p = (Property) properties [i];
1846 if ((p.ModFlags & modflags) == 0)
1848 if ((p.ModFlags & static_mask) != static_flags)
1854 if (b != null && filter (b, criteria) == true) {
1855 if (members == null)
1856 members = new ArrayList ();
1862 if (b != null && filter (b, criteria) == true) {
1863 if (members == null)
1864 members = new ArrayList ();
1871 if (indexers != null) {
1872 int len = indexers.Count;
1873 for (int i = 0; i < len; i++) {
1874 Indexer ix = (Indexer) indexers [i];
1876 if ((ix.ModFlags & modflags) == 0)
1878 if ((ix.ModFlags & static_mask) != static_flags)
1884 if (b != null && filter (b, criteria) == true) {
1885 if (members == null)
1886 members = new ArrayList ();
1892 if (b != null && filter (b, criteria) == true) {
1893 if (members == null)
1894 members = new ArrayList ();
1902 if ((mt & MemberTypes.Event) != 0) {
1903 if (events != null) {
1904 int len = events.Count;
1905 for (int i = 0; i < len; i++) {
1906 Event e = (Event) events [i];
1908 if ((e.ModFlags & modflags) == 0)
1910 if ((e.ModFlags & static_mask) != static_flags)
1913 MemberInfo eb = e.EventBuilder;
1914 if (eb != null && filter (eb, criteria) == true) {
1915 if (members == null)
1916 members = new ArrayList ();
1918 members.Add (e.EventBuilder);
1924 if ((mt & MemberTypes.Property) != 0){
1925 if (properties != null) {
1926 int len = properties.Count;
1927 for (int i = 0; i < len; i++) {
1928 Property p = (Property) properties [i];
1930 if ((p.ModFlags & modflags) == 0)
1932 if ((p.ModFlags & static_mask) != static_flags)
1935 MemberInfo pb = p.PropertyBuilder;
1936 if (pb != null && filter (pb, criteria) == true) {
1937 if (members == null)
1938 members = new ArrayList ();
1940 members.Add (p.PropertyBuilder);
1945 if (indexers != null) {
1946 int len = indexers.Count;
1947 for (int i = 0; i < len; i++) {
1948 Indexer ix = (Indexer) indexers [i];
1950 if ((ix.ModFlags & modflags) == 0)
1952 if ((ix.ModFlags & static_mask) != static_flags)
1955 MemberInfo ib = ix.PropertyBuilder;
1956 if (ib != null && filter (ib, criteria) == true) {
1957 if (members == null)
1958 members = new ArrayList ();
1960 members.Add (ix.PropertyBuilder);
1966 if ((mt & MemberTypes.NestedType) != 0)
1967 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1969 if ((mt & MemberTypes.Constructor) != 0){
1970 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1971 int len = instance_constructors.Count;
1972 for (int i = 0; i < len; i++) {
1973 Constructor c = (Constructor) instance_constructors [i];
1975 ConstructorBuilder cb = c.ConstructorBuilder;
1976 if (cb != null && filter (cb, criteria) == true) {
1977 if (members == null)
1978 members = new ArrayList ();
1985 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1986 ConstructorBuilder cb =
1987 default_static_constructor.ConstructorBuilder;
1989 if (cb != null && filter (cb, criteria) == true) {
1990 if (members == null)
1991 members = new ArrayList ();
1999 // Lookup members in base if requested.
2001 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2002 if (TypeBuilder.BaseType != null) {
2003 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2004 if (list.Count > 0) {
2005 if (members == null)
2006 members = new ArrayList ();
2008 members.AddRange (list);
2011 if (ifaces != null) {
2012 foreach (Type base_type in ifaces) {
2013 MemberList list = TypeContainer.FindMembers (base_type, mt, bf, filter, criteria);
2015 if (list.Count > 0) {
2016 if (members == null)
2017 members = new ArrayList ();
2018 members.AddRange (list);
2024 Timer.StopTimer (TimerType.TcFindMembers);
2026 if (members == null)
2027 return MemberList.Empty;
2029 return new MemberList (members);
2032 public override MemberCache MemberCache {
2034 return member_cache;
2038 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2039 MemberFilter filter, object criteria)
2041 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2044 return ds.FindMembers (mt, bf, filter, criteria);
2046 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2051 // FindMethods will look for methods not only in the type `t', but in
2052 // any interfaces implemented by the type.
2054 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
2055 MemberFilter filter, object criteria)
2061 /// Emits the values for the constants
2063 public void EmitConstants ()
2065 if (constants != null)
2066 foreach (Const con in constants)
2071 void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2076 foreach (MemberCore mc in al) {
2077 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2081 Report.Warning (169, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2086 public virtual void VerifyMembers ()
2089 // Check for internal or private fields that were never assigned
2091 if (RootContext.WarningLevel >= 3) {
2092 CheckMemberUsage (properties, "property");
2093 CheckMemberUsage (methods, "method");
2094 CheckMemberUsage (constants, "constant");
2096 if (fields != null){
2097 foreach (FieldMember f in fields) {
2098 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2102 if ((f.caching_flags & Flags.IsAssigned) == 0)
2103 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2106 const int error_code = 414;
2108 const int error_code = 169;
2110 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2111 f.GetSignatureForError ());
2117 // Only report 649 on level 4
2119 if (RootContext.WarningLevel < 4)
2122 if ((f.caching_flags & Flags.IsAssigned) != 0)
2125 Report.Warning (649, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2126 f.GetSignatureForError (), f.Type.Type.IsValueType ? Activator.CreateInstance (f.Type.Type).ToString() : "null");
2133 /// Emits the code, this step is performed after all
2134 /// the types, enumerations, constructors
2136 public void EmitType ()
2138 if (OptAttributes != null)
2139 OptAttributes.Emit (ec, this);
2142 // Structs with no fields need to have at least one byte.
2143 // The right thing would be to set the PackingSize in a DefineType
2144 // but there are no functions that allow interfaces *and* the size to
2148 if (Kind == Kind.Struct && first_nonstatic_field == null){
2149 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2150 FieldAttributes.Private);
2152 if (HasExplicitLayout){
2153 object [] ctor_args = new object [1];
2156 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2157 TypeManager.field_offset_attribute_ctor, ctor_args);
2158 fb.SetCustomAttribute (cba);
2164 if (instance_constructors != null) {
2165 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) {
2166 bool has_compliant_args = false;
2168 foreach (Constructor c in instance_constructors) {
2171 if (has_compliant_args)
2174 has_compliant_args = c.HasCompliantArgs;
2176 if (!has_compliant_args)
2177 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2179 foreach (Constructor c in instance_constructors)
2184 // Can not continue if constants are broken
2186 if (Report.Errors > 0)
2189 if (default_static_constructor != null)
2190 default_static_constructor.Emit ();
2192 if (methods != null)
2193 foreach (Method m in methods)
2196 if (operators != null)
2197 foreach (Operator o in operators)
2200 if (properties != null)
2201 foreach (Property p in properties)
2204 if (indexers != null){
2209 foreach (FieldMember f in fields)
2212 if (events != null){
2213 foreach (Event e in Events)
2217 if (delegates != null) {
2218 foreach (Delegate d in Delegates) {
2223 if (enums != null) {
2224 foreach (Enum e in enums) {
2229 if (parts != null) {
2230 foreach (ClassPart part in parts)
2234 if ((Pending != null) && !(this is ClassPart))
2235 if (Pending.VerifyPendingMethods ())
2238 if (iterators != null)
2239 foreach (Iterator iterator in iterators)
2240 iterator.EmitType ();
2242 // if (types != null)
2243 // foreach (TypeContainer tc in types)
2247 public override void CloseType ()
2249 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2253 caching_flags |= Flags.CloseTypeCreated;
2254 TypeBuilder.CreateType ();
2255 } catch (TypeLoadException){
2257 // This is fine, the code still created the type
2259 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2260 // Console.WriteLine (e.Message);
2262 Console.WriteLine ("In type: " + Name);
2267 foreach (Enum en in Enums)
2271 foreach (TypeContainer tc in Types)
2272 if (tc.Kind == Kind.Struct)
2275 foreach (TypeContainer tc in Types)
2276 if (tc.Kind != Kind.Struct)
2280 if (Delegates != null)
2281 foreach (Delegate d in Delegates)
2284 if (Iterators != null)
2285 foreach (Iterator i in Iterators)
2293 initialized_fields = null;
2294 initialized_static_fields = null;
2303 default_constructor = null;
2304 default_static_constructor = null;
2306 OptAttributes = null;
2309 member_cache = null;
2313 // Performs the validation on a Method's modifiers (properties have
2314 // the same properties).
2316 public bool MethodModifiersValid (MemberCore mc)
2318 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2319 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2320 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2322 int flags = mc.ModFlags;
2325 // At most one of static, virtual or override
2327 if ((flags & Modifiers.STATIC) != 0){
2328 if ((flags & vao) != 0){
2329 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2330 mc.GetSignatureForError ());
2335 if (Kind == Kind.Struct){
2336 if ((flags & va) != 0){
2337 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2342 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2343 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2344 mc.GetSignatureForError ());
2349 // If the declaration includes the abstract modifier, then the
2350 // declaration does not include static, virtual or extern
2352 if ((flags & Modifiers.ABSTRACT) != 0){
2353 if ((flags & Modifiers.EXTERN) != 0){
2355 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2359 if ((flags & Modifiers.SEALED) != 0) {
2360 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2364 if ((flags & Modifiers.VIRTUAL) != 0){
2365 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2369 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2370 Report.Error (513, mc.Location, "`{0}' is abstract but it is contained in nonabstract class", mc.GetSignatureForError ());
2375 if ((flags & Modifiers.PRIVATE) != 0){
2376 if ((flags & vao) != 0){
2377 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2382 if ((flags & Modifiers.SEALED) != 0){
2383 if ((flags & Modifiers.OVERRIDE) == 0){
2384 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2392 public bool UserDefinedStaticConstructor {
2394 return default_static_constructor != null;
2398 public Constructor DefaultStaticConstructor {
2399 get { return default_static_constructor; }
2402 protected override bool VerifyClsCompliance (DeclSpace ds)
2404 if (!base.VerifyClsCompliance (ds))
2409 Type base_type = TypeBuilder.BaseType;
2410 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2411 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2414 if (!Parent.IsClsCompliaceRequired (ds)) {
2415 Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
2416 GetSignatureForError (), Parent.GetSignatureForError ());
2423 /// Checks whether container name is CLS Compliant
2425 void VerifyClsName ()
2427 Hashtable base_members = base_cache == null ?
2429 base_cache.GetPublicMembers ();
2430 Hashtable this_members = new Hashtable ();
2432 foreach (DictionaryEntry entry in defined_names) {
2433 MemberCore mc = (MemberCore)entry.Value;
2434 if (!mc.IsClsCompliaceRequired (mc.Parent))
2437 string name = (string) entry.Key;
2438 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2440 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2441 object found = base_members [lcase];
2442 if (found == null) {
2443 found = this_members [lcase];
2444 if (found == null) {
2445 this_members.Add (lcase, mc);
2450 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2453 if (found is MemberInfo) {
2454 if (basename == ((MemberInfo) found).Name)
2456 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2458 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2460 Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2466 /// Performs checks for an explicit interface implementation. First it
2467 /// checks whether the `interface_type' is a base inteface implementation.
2468 /// Then it checks whether `name' exists in the interface type.
2470 public virtual bool VerifyImplements (MemberBase mb)
2472 if (ifaces != null) {
2473 foreach (Type t in ifaces){
2474 if (t == mb.InterfaceType)
2479 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2480 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2489 string IMemberContainer.Name {
2495 Type IMemberContainer.Type {
2501 MemberCache IMemberContainer.MemberCache {
2503 return member_cache;
2507 bool IMemberContainer.IsInterface {
2509 return Kind == Kind.Interface;
2513 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2515 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
2519 // Generates xml doc comments (if any), and if required,
2520 // handle warning report.
2522 internal override void GenerateDocComment (DeclSpace ds)
2524 DocUtil.GenerateTypeDocComment (this, ds);
2527 public override string DocCommentHeader {
2528 get { return "T:"; }
2531 public virtual MemberCache BaseCache {
2533 if (base_cache != null)
2535 if (TypeBuilder.BaseType != null)
2536 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2537 if (TypeBuilder.IsInterface)
2538 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2544 public class PartialContainer : TypeContainer {
2546 public readonly Namespace Namespace;
2547 public readonly int OriginalModFlags;
2548 public readonly int AllowedModifiers;
2549 public readonly TypeAttributes DefaultTypeAttributes;
2551 static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
2552 MemberName member_name, int mod_flags, Kind kind)
2555 if (!CheckModFlags (0, mod_flags, member_name))
2558 PartialContainer pc = RootContext.Tree.GetDecl (member_name) as PartialContainer;
2560 if (pc.Kind != kind) {
2562 261, member_name.Location, "Partial declarations of `{0}' " +
2563 "must be all classes, all structs or " +
2564 "all interfaces", member_name.GetPartialName ());
2568 if (!CheckModFlags (pc.OriginalModFlags, mod_flags, member_name))
2570 pc.ModFlags |= (mod_flags & pc.AllowedModifiers);
2575 if (parent is ClassPart)
2576 parent = ((ClassPart) parent).PartialContainer;
2578 pc = new PartialContainer (ns.NS, parent, member_name, mod_flags, kind);
2580 if (kind == Kind.Interface) {
2581 if (!parent.AddInterface (pc))
2583 } else if (kind == Kind.Class || kind == Kind.Struct) {
2584 if (!parent.AddClassOrStruct (pc))
2587 throw new InvalidOperationException ();
2589 RootContext.Tree.RecordDecl (ns.NS, member_name, pc);
2593 static bool CheckModFlags (int flags_org, int flags, MemberName member_name)
2595 // Check (abstract|static|sealed) sanity.
2596 int tmp = (flags_org | flags) & (Modifiers.ABSTRACT | Modifiers.SEALED | Modifiers.STATIC);
2597 if ((tmp & Modifiers.ABSTRACT) != 0) {
2598 if ((tmp & (Modifiers.STATIC | Modifiers.SEALED)) != 0) {
2600 418, member_name.Location,
2601 "`{0}': an abstract class cannot be sealed or static", member_name.ToString ());
2604 } else if (tmp == (Modifiers.SEALED | Modifiers.STATIC)) {
2605 Report.Error (441, member_name.Location, "`{0}': a class cannot be both static and sealed", member_name.ToString ());
2613 if (0 != ((flags_org ^ flags) & (0xFFFFFFFF ^ (Modifiers.SEALED | Modifiers.ABSTRACT)))) {
2615 262, member_name.Location, "Partial declarations of `{0}' " +
2616 "have conflicting accessibility modifiers",
2617 member_name.GetPartialName ());
2623 public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
2624 MemberName name, int mod, Attributes attrs,
2625 Kind kind, Location loc)
2627 PartialContainer pc = Create (ns, parent, name, mod, kind);
2629 // An error occured; create a dummy container, but don't
2631 pc = new PartialContainer (ns.NS, parent, name, mod, kind);
2634 ClassPart part = new ClassPart (ns, pc, parent, mod, attrs, kind);
2639 protected PartialContainer (Namespace ns, TypeContainer parent,
2640 MemberName name, int mod, Kind kind)
2641 : base (null, parent, name, null, kind)
2643 this.Namespace = ns;
2647 AllowedModifiers = Class.AllowedModifiers;
2648 DefaultTypeAttributes = Class.DefaultTypeAttributes;
2652 AllowedModifiers = Struct.AllowedModifiers;
2653 DefaultTypeAttributes = Struct.DefaultTypeAttributes;
2656 case Kind.Interface:
2657 AllowedModifiers = Interface.AllowedModifiers;
2658 DefaultTypeAttributes = Interface.DefaultTypeAttributes;
2662 throw new InvalidOperationException ();
2666 if (parent.Parent == null)
2667 accmods = Modifiers.INTERNAL;
2669 accmods = Modifiers.PRIVATE;
2671 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2672 this.OriginalModFlags = mod;
2675 public override PendingImplementation GetPendingImplementations ()
2677 return PendingImplementation.GetPendingImplementations (this);
2680 public override bool MarkForDuplicationCheck ()
2685 protected override TypeAttributes TypeAttr {
2687 return base.TypeAttr | DefaultTypeAttributes;
2692 public class ClassPart : TypeContainer, IMemberContainer {
2693 public readonly PartialContainer PartialContainer;
2694 public readonly bool IsPartial;
2696 public ClassPart (NamespaceEntry ns, PartialContainer pc, TypeContainer parent,
2697 int mod, Attributes attrs, Kind kind)
2698 : base (ns, parent, pc.MemberName, attrs, kind)
2700 this.PartialContainer = pc;
2701 this.IsPartial = true;
2704 if (parent == null || parent == RootContext.Tree.Types)
2705 accmods = Modifiers.INTERNAL;
2707 accmods = Modifiers.PRIVATE;
2709 this.ModFlags = Modifiers.Check (pc.AllowedModifiers, mod, accmods, pc.MemberName.Location);
2712 public override PendingImplementation GetPendingImplementations ()
2714 return PartialContainer.Pending;
2717 public override bool VerifyImplements (MemberBase mb)
2719 return PartialContainer.VerifyImplements (mb);
2723 public override void RegisterFieldForInitialization (FieldMember field)
2725 PartialContainer.RegisterFieldForInitialization (field);
2728 public override bool EmitFieldInitializers (EmitContext ec)
2730 return PartialContainer.EmitFieldInitializers (ec);
2733 public override Type FindNestedType (string name)
2735 return PartialContainer.FindNestedType (name);
2738 public override MemberCache BaseCache {
2740 return PartialContainer.BaseCache;
2744 public override TypeBuilder DefineType ()
2746 throw new InternalErrorException ("Should not get here");
2751 public abstract class ClassOrStruct : TypeContainer {
2752 bool has_explicit_layout = false;
2753 ListDictionary declarative_security;
2755 public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
2756 MemberName name, Attributes attrs, Kind kind)
2757 : base (ns, parent, name, attrs, kind)
2761 public override PendingImplementation GetPendingImplementations ()
2763 return PendingImplementation.GetPendingImplementations (this);
2766 public override bool HasExplicitLayout {
2768 return has_explicit_layout;
2772 public override void VerifyMembers ()
2774 base.VerifyMembers ();
2776 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2777 foreach (Event e in events){
2778 if ((e.caching_flags & Flags.IsAssigned) == 0)
2779 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2784 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2786 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2787 if (declarative_security == null)
2788 declarative_security = new ListDictionary ();
2790 a.ExtractSecurityPermissionSet (declarative_security);
2794 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit)
2795 has_explicit_layout = true;
2797 base.ApplyAttributeBuilder (a, cb);
2800 public override void Emit()
2804 if (declarative_security != null) {
2805 foreach (DictionaryEntry de in declarative_security) {
2806 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2813 /// Class handles static classes declaration
2815 public sealed class StaticClass: Class {
2816 public StaticClass (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2818 : base (ns, parent, name, mod, attrs)
2820 if (RootContext.Version == LanguageVersion.ISO_1) {
2821 Report.FeatureIsNotStandardized (Location, "static classes");
2825 protected override int AllowedModifiersProp {
2827 return Modifiers.NEW | Modifiers.PUBLIC | Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE |
2828 Modifiers.STATIC | Modifiers.UNSAFE;
2832 protected override void DefineContainerMembers (MemberCoreArrayList list)
2837 foreach (MemberCore m in list) {
2838 if (m is Operator) {
2839 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2843 if (m is Destructor) {
2844 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2848 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2849 Report.Error (1057, m.Location, "`{0}': Static classes cannot contain protected members", m.GetSignatureForError ());
2854 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2858 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2861 if (m is Constructor) {
2862 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2866 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2869 base.DefineContainerMembers (list);
2872 public override TypeBuilder DefineType()
2874 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2875 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2879 TypeBuilder tb = base.DefineType ();
2883 if (ptype != TypeManager.object_type) {
2884 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object", GetSignatureForError (), TypeManager.CSharpName (ptype));
2888 if (ifaces != null) {
2889 foreach (Type t in ifaces)
2890 Report.SymbolRelatedToPreviousError (t);
2891 Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
2896 protected override TypeAttributes TypeAttr {
2898 return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
2903 public class Class : ClassOrStruct {
2904 // TODO: remove this and use only AllowedModifiersProp to fix partial classes bugs
2905 public const int AllowedModifiers =
2908 Modifiers.PROTECTED |
2909 Modifiers.INTERNAL |
2911 Modifiers.ABSTRACT |
2915 public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2917 : base (ns, parent, name, attrs, Kind.Class)
2919 this.ModFlags = mod;
2922 virtual protected int AllowedModifiersProp {
2924 return AllowedModifiers;
2928 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2930 if (a.Type == TypeManager.attribute_usage_type) {
2931 if (ptype != TypeManager.attribute_type && !ptype.IsSubclassOf (TypeManager.attribute_type) &&
2932 TypeBuilder.FullName != "System.Attribute") {
2933 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2937 if (a.Type == TypeManager.conditional_attribute_type &&
2938 !(ptype == TypeManager.attribute_type || ptype.IsSubclassOf (TypeManager.attribute_type))) {
2939 Report.Error (1689, a.Location, "Attribute 'System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2943 if (AttributeTester.IsAttributeExcluded (a.Type))
2946 base.ApplyAttributeBuilder (a, cb);
2949 public const TypeAttributes DefaultTypeAttributes =
2950 TypeAttributes.AutoLayout | TypeAttributes.Class;
2952 public override TypeBuilder DefineType()
2954 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2955 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2959 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2960 ModFlags = Modifiers.Check (AllowedModifiersProp, ModFlags, accmods, Location);
2962 return base.DefineType ();
2965 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2966 /// Valid only for attribute classes.
2967 public bool IsExcluded ()
2969 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2970 return (caching_flags & Flags.Excluded) != 0;
2972 caching_flags &= ~Flags.Excluded_Undetected;
2974 if (OptAttributes == null)
2977 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
2982 foreach (Attribute a in attrs) {
2983 string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
2984 if (RootContext.AllDefines.Contains (condition))
2988 caching_flags |= Flags.Excluded;
2993 // FIXME: How do we deal with the user specifying a different
2996 protected override TypeAttributes TypeAttr {
2998 return base.TypeAttr | DefaultTypeAttributes;
3003 public class Struct : ClassOrStruct {
3005 // Modifiers allowed in a struct declaration
3007 public const int AllowedModifiers =
3010 Modifiers.PROTECTED |
3011 Modifiers.INTERNAL |
3015 public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
3016 int mod, Attributes attrs)
3017 : base (ns, parent, name, attrs, Kind.Struct)
3021 if (parent.Parent == null)
3022 accmods = Modifiers.INTERNAL;
3024 accmods = Modifiers.PRIVATE;
3026 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
3028 this.ModFlags |= Modifiers.SEALED;
3031 public const TypeAttributes DefaultTypeAttributes =
3032 TypeAttributes.SequentialLayout |
3033 TypeAttributes.Sealed |
3034 TypeAttributes.BeforeFieldInit;
3037 // FIXME: Allow the user to specify a different set of attributes
3038 // in some cases (Sealed for example is mandatory for a class,
3039 // but what SequentialLayout can be changed
3041 protected override TypeAttributes TypeAttr {
3043 return base.TypeAttr | DefaultTypeAttributes;
3051 public class Interface : TypeContainer, IMemberContainer {
3054 /// Modifiers allowed in a class declaration
3056 public const int AllowedModifiers =
3059 Modifiers.PROTECTED |
3060 Modifiers.INTERNAL |
3064 public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
3066 : base (ns, parent, name, attrs, Kind.Interface)
3070 if (parent.Parent == null)
3071 accmods = Modifiers.INTERNAL;
3073 accmods = Modifiers.PRIVATE;
3075 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3078 public override PendingImplementation GetPendingImplementations ()
3083 public const TypeAttributes DefaultTypeAttributes =
3084 TypeAttributes.AutoLayout |
3085 TypeAttributes.Abstract |
3086 TypeAttributes.Interface;
3088 protected override TypeAttributes TypeAttr {
3090 return base.TypeAttr | DefaultTypeAttributes;
3094 protected override bool VerifyClsCompliance (DeclSpace ds)
3096 if (!base.VerifyClsCompliance (ds))
3099 if (ifaces != null) {
3100 foreach (Type t in ifaces) {
3101 if (AttributeTester.IsClsCompliant (t))
3104 Report.SymbolRelatedToPreviousError (t);
3105 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3106 GetSignatureForError (), TypeManager.CSharpName (t));
3114 public abstract class MethodCore : MemberBase {
3115 public readonly Parameters Parameters;
3116 protected ToplevelBlock block;
3119 // Parameters, cached for semantic analysis.
3121 protected InternalParameters parameter_info;
3122 protected Type [] parameter_types;
3124 // Whether this is an operator method.
3125 public Operator IsOperator;
3128 // The method we're overriding if this is an override method.
3130 protected MethodInfo base_method = null;
3132 static string[] attribute_targets = new string [] { "method", "return" };
3134 public MethodCore (TypeContainer parent, Expression type, int mod,
3135 int allowed_mod, bool is_interface, MemberName name,
3136 Attributes attrs, Parameters parameters)
3137 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
3140 Parameters = parameters;
3141 IsInterface = is_interface;
3145 // Returns the System.Type array for the parameters of this method
3147 public Type [] ParameterTypes {
3149 return parameter_types;
3153 public InternalParameters ParameterInfo
3156 return parameter_info;
3160 public ToplevelBlock Block {
3170 public void SetYields ()
3172 ModFlags |= Modifiers.METHOD_YIELDS;
3175 protected override bool CheckBase ()
3177 if (!base.CheckBase ())
3180 // Check whether arguments were correct.
3181 if (!DoDefineParameters ())
3184 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3190 // Is null for System.Object while compiling corlib and base interfaces
3191 if (Parent.BaseCache == null) {
3192 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3193 Report.Warning (109, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3198 Type base_ret_type = null;
3199 base_method = FindOutBaseMethod (Parent, ref base_ret_type);
3201 // method is override
3202 if (base_method != null) {
3204 if (!CheckMethodAgainstBase ())
3207 if ((ModFlags & Modifiers.NEW) == 0) {
3208 if (MemberType != TypeManager.TypeToCoreType (base_ret_type)) {
3209 Report.SymbolRelatedToPreviousError (base_method);
3210 if (this is PropertyBase) {
3211 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3212 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3215 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3216 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3221 if (base_method.IsAbstract && !IsInterface) {
3222 Report.SymbolRelatedToPreviousError (base_method);
3223 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3224 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3229 if (base_method.IsSpecialName && !(this is PropertyBase)) {
3230 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3234 if (RootContext.WarningLevel > 2) {
3235 if (Name == "Equals" && parameter_types.Length == 1 && parameter_types [0] == TypeManager.object_type)
3236 Parent.Methods.HasEquals = true;
3237 else if (Name == "GetHashCode" && parameter_types.Length == 0)
3238 Parent.Methods.HasGetHashCode = true;
3241 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3242 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3244 EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
3245 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
3246 Report.SymbolRelatedToPreviousError (base_method);
3247 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3248 GetSignatureForError (), TypeManager.CSharpSignature (base_method) );
3255 MemberInfo conflict_symbol = Parent.FindBaseMemberWithSameName (Name, !(this is Property));
3256 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3257 if (conflict_symbol != null) {
3258 Report.SymbolRelatedToPreviousError (conflict_symbol);
3259 if (this is PropertyBase)
3260 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3262 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3264 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3268 if (conflict_symbol == null) {
3269 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3270 Report.Warning (109, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3275 if ((ModFlags & Modifiers.NEW) == 0) {
3276 if (this is Method && conflict_symbol is MethodBase)
3279 Report.SymbolRelatedToPreviousError (conflict_symbol);
3280 Report.Warning (108, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3281 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3288 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3289 // that have been defined.
3291 // `name' is the user visible name for reporting errors (this is used to
3292 // provide the right name regarding method names and properties)
3294 bool CheckMethodAgainstBase ()
3298 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3299 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3300 Report.Error (506, Location,
3301 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3302 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3306 // Now we check that the overriden method is not final
3308 if (base_method.IsFinal) {
3309 Report.SymbolRelatedToPreviousError (base_method);
3310 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3311 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3315 // Check that the permissions are not being changed
3317 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3318 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3320 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3321 Error_CannotChangeAccessModifiers (base_method, base_classp, null);
3326 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3327 ModFlags |= Modifiers.NEW;
3328 Report.SymbolRelatedToPreviousError (base_method);
3329 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3330 if (RootContext.WarningLevel >= 2)
3331 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 (), TypeManager.CSharpSignature (base_method));
3333 Report.Warning (108, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3334 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3341 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3343 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3345 // when overriding protected internal, the method can be declared
3346 // protected internal only within the same assembly
3349 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3350 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3352 // assemblies differ - report an error
3356 } else if (thisp != base_classp) {
3358 // same assembly, but other attributes differ - report an error
3363 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3365 // if it's not "protected internal", it must be "protected"
3369 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3371 // protected within the same assembly - an error
3374 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3375 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3377 // protected ok, but other attributes differ - report an error
3383 return (thisp == base_classp);
3387 public bool CheckAbstractAndExtern (bool has_block)
3389 if (Parent.Kind == Kind.Interface)
3393 if ((ModFlags & Modifiers.EXTERN) != 0) {
3394 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
3395 GetSignatureForError ());
3399 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
3400 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
3401 GetSignatureForError ());
3405 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
3406 Report.Error (501, Location, "`{0}' must declare a body because it is not marked abstract or extern",
3407 GetSignatureForError ());
3415 protected void Error_CannotChangeAccessModifiers (MemberInfo base_method, MethodAttributes ma, string suffix)
3417 Report.SymbolRelatedToPreviousError (base_method);
3418 string base_name = TypeManager.GetFullNameSignature (base_method);
3419 string this_name = GetSignatureForError ();
3420 if (suffix != null) {
3421 base_name += suffix;
3422 this_name += suffix;
3425 Report.Error (507, Location, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3426 this_name, Modifiers.GetDescription (ma), base_name);
3429 protected static string Error722 {
3431 return "`{0}': static types cannot be used as return types";
3436 /// For custom member duplication search in a container
3438 protected abstract bool CheckForDuplications ();
3441 /// Gets base method and its return type
3443 protected abstract MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type);
3445 protected virtual bool DoDefineParameters ()
3447 EmitContext ec = Parent.EmitContext;
3449 throw new InternalErrorException ("DoDefineParameters invoked too early");
3451 bool old_unsafe = ec.InUnsafe;
3452 ec.InUnsafe = InUnsafe;
3453 // Check if arguments were correct
3454 parameter_types = Parameters.GetParameterInfo (ec);
3455 ec.InUnsafe = old_unsafe;
3457 if ((parameter_types == null) ||
3458 !CheckParameters (Parent, parameter_types))
3461 parameter_info = new InternalParameters (parameter_types, Parameters);
3463 Parameter array_param = Parameters.ArrayParameter;
3464 if ((array_param != null) &&
3465 (!array_param.ParameterType.IsArray ||
3466 (array_param.ParameterType.GetArrayRank () != 1))) {
3467 Report.Error (225, Location, "The params parameter must be a single dimensional array");
3474 public override string[] ValidAttributeTargets {
3476 return attribute_targets;
3480 protected override bool VerifyClsCompliance (DeclSpace ds)
3482 if (!base.VerifyClsCompliance (ds)) {
3483 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
3484 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3489 if (Parameters.HasArglist) {
3490 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3493 if (!AttributeTester.IsClsCompliant (MemberType)) {
3494 if (this is PropertyBase)
3495 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3496 GetSignatureForError ());
3498 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3499 GetSignatureForError ());
3502 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
3507 protected bool IsDuplicateImplementation (MethodCore method)
3509 if (method == this || !(method.MemberName.Equals (MemberName)))
3512 Type[] param_types = method.ParameterTypes;
3513 if (param_types == null && ParameterTypes == null)
3515 if (param_types == null || ParameterTypes == null)
3518 if (param_types.Length != ParameterTypes.Length)
3521 for (int i = 0; i < param_types.Length; i++)
3522 if (param_types [i] != ParameterTypes [i])
3525 // TODO: make operator compatible with MethodCore to avoid this
3526 if (this is Operator && method is Operator) {
3527 if (MemberType != method.MemberType)
3532 // Try to report 663: method only differs on out/ref
3534 ParameterData info = ParameterInfo;
3535 ParameterData other_info = method.ParameterInfo;
3536 for (int i = 0; i < info.Count; i++){
3537 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3538 Report.SymbolRelatedToPreviousError (method);
3539 Report.Error (663, Location, "`{0}': Methods cannot differ only on their use of ref and out on a parameters",
3540 GetSignatureForError ());
3545 Report.SymbolRelatedToPreviousError (method);
3546 if (this is Operator && method is Operator)
3547 Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
3549 Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
3553 public override bool IsUsed {
3554 get { return IsExplicitImpl || base.IsUsed; }
3558 // Returns a string that represents the signature for this
3559 // member which should be used in XML documentation.
3561 public override string GetDocCommentName (DeclSpace ds)
3563 return DocUtil.GetMethodDocCommentName (this, ds);
3567 // Raised (and passed an XmlElement that contains the comment)
3568 // when GenerateDocComment is writing documentation expectedly.
3570 // FIXME: with a few effort, it could be done with XmlReader,
3571 // that means removal of DOM use.
3573 internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
3575 DocUtil.OnMethodGenerateDocComment (this, ds, el);
3579 // Represents header string for documentation comment.
3581 public override string DocCommentHeader {
3582 get { return "M:"; }
3587 public class SourceMethod : ISourceMethod
3589 TypeContainer container;
3592 protected SourceMethod (TypeContainer container, MethodBase builder,
3593 ISourceFile file, Location start, Location end)
3595 this.container = container;
3596 this.builder = builder;
3598 CodeGen.SymbolWriter.OpenMethod (
3599 file, this, start.Row, start.Column, end.Row, start.Column);
3602 public string Name {
3603 get { return builder.Name; }
3606 public int NamespaceID {
3607 get { return container.NamespaceEntry.SymbolFileID; }
3612 if (builder is MethodBuilder)
3613 return ((MethodBuilder) builder).GetToken ().Token;
3614 else if (builder is ConstructorBuilder)
3615 return ((ConstructorBuilder) builder).GetToken ().Token;
3617 throw new NotSupportedException ();
3621 public void CloseMethod ()
3623 if (CodeGen.SymbolWriter != null)
3624 CodeGen.SymbolWriter.CloseMethod ();
3627 public static SourceMethod Create (TypeContainer parent,
3628 MethodBase builder, Block block)
3630 if (CodeGen.SymbolWriter == null)
3635 Location start_loc = block.StartLocation;
3636 if (start_loc.IsNull)
3639 Location end_loc = block.EndLocation;
3643 ISourceFile file = start_loc.SourceFile;
3647 return new SourceMethod (
3648 parent, builder, file, start_loc, end_loc);
3652 public class Method : MethodCore, IIteratorContainer, IMethodData {
3653 public MethodBuilder MethodBuilder;
3654 public MethodData MethodData;
3655 ReturnParameter return_attributes;
3656 ListDictionary declarative_security;
3659 /// Modifiers allowed in a class declaration
3661 const int AllowedModifiers =
3664 Modifiers.PROTECTED |
3665 Modifiers.INTERNAL |
3670 Modifiers.OVERRIDE |
3671 Modifiers.ABSTRACT |
3673 Modifiers.METHOD_YIELDS |
3676 const int AllowedInterfaceModifiers =
3677 Modifiers.NEW | Modifiers.UNSAFE;
3680 // return_type can be "null" for VOID values.
3682 public Method (TypeContainer ds, Expression return_type, int mod, bool is_iface,
3683 MemberName name, Parameters parameters, Attributes attrs)
3684 : base (ds, return_type, mod,
3685 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3686 is_iface, name, attrs, parameters)
3690 public override AttributeTargets AttributeTargets {
3692 return AttributeTargets.Method;
3696 public override string GetSignatureForError()
3698 if (IsOperator != null)
3699 return IsOperator.GetSignatureForError ();
3701 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
3704 void DuplicateEntryPoint (MethodInfo b, Location location)
3708 "Program `" + CodeGen.FileName +
3709 "' has more than one entry point defined: `" +
3710 TypeManager.CSharpSignature(b) + "'");
3713 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
3715 if (b.ReturnType != TypeManager.void_type &&
3716 b.ReturnType != TypeManager.int32_type)
3719 if (pinfo.Count == 0)
3722 if (pinfo.Count > 1)
3725 Type t = pinfo.ParameterType(0);
3727 (t.GetArrayRank() == 1) &&
3728 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3729 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3735 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3737 if (a.Target == AttributeTargets.ReturnValue) {
3738 if (return_attributes == null)
3739 return_attributes = new ReturnParameter (MethodBuilder, Location);
3741 return_attributes.ApplyAttributeBuilder (a, cb);
3745 if (a.Type == TypeManager.methodimpl_attr_type &&
3746 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
3747 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3750 if (a.Type == TypeManager.dllimport_type) {
3751 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3752 if ((ModFlags & extern_static) != extern_static) {
3753 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3759 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3760 if (declarative_security == null)
3761 declarative_security = new ListDictionary ();
3762 a.ExtractSecurityPermissionSet (declarative_security);
3766 if (a.Type == TypeManager.conditional_attribute_type) {
3767 if (IsOperator != null || IsExplicitImpl) {
3768 Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3769 GetSignatureForError ());
3773 if (ReturnType != TypeManager.void_type) {
3774 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
3778 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3779 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
3784 Report.Error (582, Location, "Conditional not valid on interface members");
3788 if (MethodData.implementing != null) {
3789 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
3790 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
3794 for (int i = 0; i < parameter_info.Count; ++i) {
3795 if ((parameter_info.ParameterModifier (i) & Parameter.Modifier.OUT) != 0) {
3796 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
3802 MethodBuilder.SetCustomAttribute (cb);
3805 protected override bool CheckForDuplications ()
3807 ArrayList ar = Parent.Methods;
3809 int arLen = ar.Count;
3811 for (int i = 0; i < arLen; i++) {
3812 Method m = (Method) ar [i];
3813 if (IsDuplicateImplementation (m))
3818 ar = Parent.Properties;
3820 for (int i = 0; i < ar.Count; ++i) {
3821 PropertyBase pb = (PropertyBase) ar [i];
3822 if (pb.AreAccessorsDuplicateImplementation (this))
3827 ar = Parent.Indexers;
3829 for (int i = 0; i < ar.Count; ++i) {
3830 PropertyBase pb = (PropertyBase) ar [i];
3831 if (pb.AreAccessorsDuplicateImplementation (this))
3838 for (int i = 0; i < ar.Count; ++i) {
3839 Event ev = (Event) ar [i];
3840 if (ev.AreAccessorsDuplicateImplementation (this))
3851 public override bool Define ()
3856 if (!CheckAbstractAndExtern (block != null))
3859 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
3860 Error1599 (Location, ReturnType);
3867 if (IsOperator != null)
3868 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
3870 MethodData = new MethodData (this, ParameterInfo, ModFlags, flags, this);
3872 if (!MethodData.Define (Parent))
3875 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
3876 Name == "Finalize" && !(this is Destructor)) {
3877 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
3881 // Setup iterator if we are one
3883 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3884 Iterator iterator = new Iterator (this,
3886 ParameterInfo, ModFlags);
3888 if (!iterator.DefineIterator ())
3892 MethodBuilder = MethodData.MethodBuilder;
3895 // This is used to track the Entry Point,
3897 if (Name == "Main" &&
3898 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3899 (RootContext.MainClass == null ||
3900 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3901 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3902 IMethodData md = TypeManager.GetMethod (MethodBuilder);
3903 md.SetMemberIsUsed ();
3905 if (RootContext.EntryPoint == null) {
3906 RootContext.EntryPoint = MethodBuilder;
3907 RootContext.EntryPointLocation = Location;
3909 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3910 DuplicateEntryPoint (MethodBuilder, Location);
3913 if (RootContext.WarningLevel >= 4)
3914 Report.Warning (28, Location, "`{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder));
3918 if (MemberType.IsAbstract && MemberType.IsSealed) {
3919 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3929 public override void Emit ()
3931 MethodData.Emit (Parent, this);
3934 if (declarative_security != null) {
3935 foreach (DictionaryEntry de in declarative_security) {
3936 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3944 public static void Error1599 (Location loc, Type t)
3946 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
3949 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
3951 MethodInfo mi = (MethodInfo) container.BaseCache.FindMemberToOverride (
3952 container.TypeBuilder, Name, ParameterTypes, false);
3957 base_ret_type = mi.ReturnType;
3961 public override bool MarkForDuplicationCheck ()
3963 caching_flags |= Flags.TestMethodDuplication;
3967 protected override bool VerifyClsCompliance(DeclSpace ds)
3969 if (!base.VerifyClsCompliance (ds))
3972 if (parameter_types.Length > 0) {
3973 ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
3975 ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
3981 #region IMethodData Members
3983 public CallingConventions CallingConventions {
3985 CallingConventions cc = Parameters.GetCallingConvention ();
3986 if (Parameters.HasArglist)
3987 block.HasVarargs = true;
3990 if ((ModFlags & Modifiers.STATIC) == 0)
3991 cc |= CallingConventions.HasThis;
3993 // FIXME: How is `ExplicitThis' used in C#?
3999 public Type ReturnType {
4005 public MemberName MethodName {
4011 public new Location Location {
4013 return base.Location;
4017 protected override bool CheckBase() {
4018 if (!base.CheckBase ())
4021 // TODO: Destructor should derive from MethodCore
4022 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
4023 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
4024 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
4031 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4033 EmitContext ec = new EmitContext (
4034 tc, Parent, Location, ig, ReturnType, ModFlags, false);
4036 ec.CurrentIterator = tc as Iterator;
4037 if (ec.CurrentIterator != null)
4038 ec.CurrentAnonymousMethod = ec.CurrentIterator.Host;
4044 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
4046 public bool IsExcluded (EmitContext ec)
4048 if ((caching_flags & Flags.Excluded_Undetected) == 0)
4049 return (caching_flags & Flags.Excluded) != 0;
4051 caching_flags &= ~Flags.Excluded_Undetected;
4053 if (base_method == null) {
4054 if (OptAttributes == null)
4057 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
4062 foreach (Attribute a in attrs) {
4063 string condition = a.GetConditionalAttributeValue (Parent.EmitContext);
4064 if (RootContext.AllDefines.Contains (condition))
4068 caching_flags |= Flags.Excluded;
4072 IMethodData md = TypeManager.GetMethod (base_method);
4074 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
4075 caching_flags |= Flags.Excluded;
4081 if (md.IsExcluded (ec)) {
4082 caching_flags |= Flags.Excluded;
4091 public abstract class ConstructorInitializer {
4092 ArrayList argument_list;
4093 protected ConstructorInfo base_constructor;
4096 public ConstructorInitializer (ArrayList argument_list, Location loc)
4098 this.argument_list = argument_list;
4102 public ArrayList Arguments {
4104 return argument_list;
4108 public bool Resolve (ConstructorBuilder caller_builder, Block block, EmitContext ec)
4110 Expression base_constructor_group;
4114 ec.CurrentBlock = block;
4116 if (argument_list != null){
4117 foreach (Argument a in argument_list){
4118 if (!a.Resolve (ec, loc))
4122 ec.CurrentBlock = null;
4124 if (this is ConstructorBaseInitializer) {
4125 if (ec.ContainerType.BaseType == null)
4128 t = ec.ContainerType.BaseType;
4129 if (ec.ContainerType.IsValueType) {
4130 Report.Error (522, loc,
4131 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4135 t = ec.ContainerType;
4137 base_constructor_group = Expression.MemberLookup (
4138 ec, t, ".ctor", MemberTypes.Constructor,
4139 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4142 if (base_constructor_group == null){
4144 base_constructor_group = Expression.MemberLookup (
4145 ec, t, null, t, ".ctor", MemberTypes.Constructor,
4146 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4150 int errors = Report.Errors;
4151 if (base_constructor_group != null)
4152 base_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4153 ec, (MethodGroupExpr) base_constructor_group, argument_list,
4156 if (base_constructor == null) {
4157 if (errors == Report.Errors)
4158 Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpSignature (caller_builder),
4159 argument_list.Count);
4164 Expression.ErrorIsInaccesible (loc, TypeManager.CSharpSignature (base_constructor));
4165 base_constructor = null;
4169 if (base_constructor == caller_builder){
4170 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4177 public virtual void Emit (EmitContext ec)
4179 if (base_constructor != null){
4180 ec.Mark (loc, false);
4182 Invocation.EmitCall (ec, true, true, null, base_constructor, argument_list, loc);
4184 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), base_constructor, argument_list, loc);
4189 public class ConstructorBaseInitializer : ConstructorInitializer {
4190 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4191 base (argument_list, l)
4196 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4197 public GeneratedBaseInitializer (Location loc):
4202 public override void Emit(EmitContext ec)
4204 bool old = ec.TestObsoleteMethodUsage;
4205 ec.TestObsoleteMethodUsage = false;
4207 ec.TestObsoleteMethodUsage = old;
4211 public class ConstructorThisInitializer : ConstructorInitializer {
4212 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4213 base (argument_list, l)
4218 public class Constructor : MethodCore, IMethodData {
4219 public ConstructorBuilder ConstructorBuilder;
4220 public ConstructorInitializer Initializer;
4221 ListDictionary declarative_security;
4224 // Modifiers allowed for a constructor.
4226 public const int AllowedModifiers =
4228 Modifiers.PROTECTED |
4229 Modifiers.INTERNAL |
4235 bool has_compliant_args = false;
4237 // The spec claims that static is not permitted, but
4238 // my very own code has static constructors.
4240 public Constructor (TypeContainer ds, string name, int mod, Parameters args,
4241 ConstructorInitializer init, Location loc)
4242 : base (ds, null, mod, AllowedModifiers, false, new MemberName (name, loc),
4248 public bool HasCompliantArgs {
4250 return has_compliant_args;
4254 public override AttributeTargets AttributeTargets {
4256 return AttributeTargets.Constructor;
4262 // Returns true if this is a default constructor
4264 public bool IsDefault ()
4266 if ((ModFlags & Modifiers.STATIC) != 0)
4267 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4268 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
4271 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4272 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
4273 (Initializer is ConstructorBaseInitializer) &&
4274 (Initializer.Arguments == null);
4277 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4279 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4280 if (declarative_security == null) {
4281 declarative_security = new ListDictionary ();
4283 a.ExtractSecurityPermissionSet (declarative_security);
4287 ConstructorBuilder.SetCustomAttribute (cb);
4290 protected override bool CheckForDuplications ()
4292 ArrayList ar = Parent.InstanceConstructors;
4294 int arLen = ar.Count;
4296 for (int i = 0; i < arLen; i++) {
4297 Constructor m = (Constructor) ar [i];
4298 if (IsDuplicateImplementation (m))
4305 protected override bool CheckBase ()
4307 // Check whether arguments were correct.
4308 if (!DoDefineParameters ())
4311 // TODO: skip the rest for generated ctor
4312 if ((ModFlags & Modifiers.STATIC) != 0)
4315 if (!CheckForDuplications ())
4318 if (Parent.Kind == Kind.Struct) {
4319 if (ParameterTypes.Length == 0) {
4320 Report.Error (568, Location,
4321 "Structs cannot contain explicit parameterless constructors");
4325 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4326 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4331 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4332 Report.Warning (628, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4339 // Creates the ConstructorBuilder
4341 public override bool Define ()
4343 if (ConstructorBuilder != null)
4346 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4347 MethodAttributes.SpecialName);
4349 if ((ModFlags & Modifiers.STATIC) != 0) {
4350 ca |= MethodAttributes.Static | MethodAttributes.Private;
4352 ca |= MethodAttributes.HideBySig;
4354 if ((ModFlags & Modifiers.PUBLIC) != 0)
4355 ca |= MethodAttributes.Public;
4356 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4357 if ((ModFlags & Modifiers.INTERNAL) != 0)
4358 ca |= MethodAttributes.FamORAssem;
4360 ca |= MethodAttributes.Family;
4361 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4362 ca |= MethodAttributes.Assembly;
4363 else if (IsDefault ())
4364 ca |= MethodAttributes.Public;
4366 ca |= MethodAttributes.Private;
4369 if (!CheckAbstractAndExtern (block != null))
4372 // Check if arguments were correct.
4376 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4377 ca, CallingConventions,
4380 if ((ModFlags & Modifiers.UNSAFE) != 0)
4381 ConstructorBuilder.InitLocals = false;
4383 TypeManager.AddMethod (ConstructorBuilder, this);
4386 // HACK because System.Reflection.Emit is lame
4388 TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes);
4396 public override void Emit ()
4398 EmitContext ec = CreateEmitContext (null, null);
4400 // If this is a non-static `struct' constructor and doesn't have any
4401 // initializer, it must initialize all of the struct's fields.
4402 if ((Parent.Kind == Kind.Struct) &&
4403 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4404 Block.AddThisVariable (Parent, Location);
4406 if (block != null) {
4407 if (!block.ResolveMeta (ec, ParameterInfo))
4411 if ((ModFlags & Modifiers.STATIC) == 0){
4412 if (Parent.Kind == Kind.Class && Initializer == null)
4413 Initializer = new GeneratedBaseInitializer (Location);
4417 // Spec mandates that Initializers will not have
4421 if ((Initializer != null) &&
4422 !Initializer.Resolve (ConstructorBuilder, block, ec))
4424 ec.IsStatic = false;
4427 Parameters.LabelParameters (ec, ConstructorBuilder);
4429 SourceMethod source = SourceMethod.Create (
4430 Parent, ConstructorBuilder, block);
4433 // Classes can have base initializers and instance field initializers.
4435 if (Parent.Kind == Kind.Class){
4436 if ((ModFlags & Modifiers.STATIC) == 0){
4439 // If we use a "this (...)" constructor initializer, then
4440 // do not emit field initializers, they are initialized in the other constructor
4442 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4443 Parent.EmitFieldInitializers (ec);
4446 if (Initializer != null) {
4447 if (GetObsoleteAttribute () != null || Parent.GetObsoleteAttribute () != null)
4448 ec.TestObsoleteMethodUsage = false;
4450 Initializer.Emit (ec);
4453 if ((ModFlags & Modifiers.STATIC) != 0)
4454 Parent.EmitFieldInitializers (ec);
4456 if (OptAttributes != null)
4457 OptAttributes.Emit (ec, this);
4459 ec.EmitTopBlock (this, block, ParameterInfo);
4462 source.CloseMethod ();
4466 if (declarative_security != null) {
4467 foreach (DictionaryEntry de in declarative_security) {
4468 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4475 // Is never override
4476 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
4481 public override string GetSignatureForError()
4483 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4486 protected override bool VerifyClsCompliance (DeclSpace ds)
4488 if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
4492 if (parameter_types.Length > 0) {
4493 ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
4495 ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4497 if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4498 foreach (Type param in parameter_types) {
4499 if (param.IsArray) {
4505 has_compliant_args = true;
4509 #region IMethodData Members
4511 public System.Reflection.CallingConventions CallingConventions {
4513 CallingConventions cc = Parameters.GetCallingConvention ();
4515 if (Parent.Kind == Kind.Class)
4516 if ((ModFlags & Modifiers.STATIC) == 0)
4517 cc |= CallingConventions.HasThis;
4519 // FIXME: How is `ExplicitThis' used in C#?
4525 public new Location Location {
4527 return base.Location;
4531 public MemberName MethodName {
4537 public Type ReturnType {
4543 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4545 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4546 return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
4549 public bool IsExcluded(EmitContext ec)
4558 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4560 public interface IMethodData
4562 CallingConventions CallingConventions { get; }
4563 Location Location { get; }
4564 MemberName MethodName { get; }
4565 Type[] ParameterTypes { get; }
4566 Type ReturnType { get; }
4568 Attributes OptAttributes { get; }
4569 ToplevelBlock Block { get; set; }
4571 EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
4572 ObsoleteAttribute GetObsoleteAttribute ();
4573 string GetSignatureForError ();
4574 bool IsExcluded (EmitContext ec);
4575 bool IsClsCompliaceRequired (DeclSpace ds);
4576 void SetMemberIsUsed ();
4580 // Encapsulates most of the Method's state
4582 public class MethodData {
4584 readonly IMethodData method;
4587 // The return type of this method
4589 public readonly InternalParameters ParameterInfo;
4592 // Are we implementing an interface ?
4594 public MethodInfo implementing;
4599 protected MemberBase member;
4600 protected int modifiers;
4601 protected MethodAttributes flags;
4603 MethodBuilder builder = null;
4604 public MethodBuilder MethodBuilder {
4610 public MethodData (MemberBase member, InternalParameters parameters,
4611 int modifiers, MethodAttributes flags, IMethodData method)
4613 this.member = member;
4614 this.ParameterInfo = parameters;
4615 this.modifiers = modifiers;
4618 this.method = method;
4621 public bool Define (TypeContainer container)
4623 string name = method.MethodName.Name;
4624 string method_name = name;
4626 Type[] ParameterTypes = method.ParameterTypes;
4628 if (container.Pending != null){
4629 if (member is Indexer) // TODO: test it, but it should work without this IF
4630 implementing = container.Pending.IsInterfaceIndexer (
4631 member.InterfaceType, method.ReturnType, ParameterInfo);
4633 implementing = container.Pending.IsInterfaceMethod (
4634 member.InterfaceType, name, method.ReturnType, ParameterInfo);
4636 if (member.InterfaceType != null){
4637 if (implementing == null){
4638 if (member is PropertyBase) {
4639 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4640 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4641 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4644 Report.Error (539, method.Location,
4645 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4646 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4650 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4651 Report.SymbolRelatedToPreviousError (implementing);
4652 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4653 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4656 method_name = member.InterfaceType.FullName + "." + name;
4658 if (implementing != null) {
4659 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4660 if (prop_method != null) {
4661 if (!implementing.IsSpecialName) {
4662 Report.SymbolRelatedToPreviousError (implementing);
4663 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4664 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4667 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4668 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4669 Report.SymbolRelatedToPreviousError (implementing);
4670 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4671 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4680 // For implicit implementations, make sure we are public, for
4681 // explicit implementations, make sure we are private.
4683 if (implementing != null){
4685 // Setting null inside this block will trigger a more
4686 // verbose error reporting for missing interface implementations
4688 // The "candidate" function has been flagged already
4689 // but it wont get cleared
4691 if (member.IsExplicitImpl){
4692 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4693 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4694 implementing = null;
4696 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4697 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4699 // If this is an interface method implementation,
4700 // check for public accessibility
4702 implementing = null;
4703 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4704 // We may never be private.
4705 implementing = null;
4706 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4708 // We may be protected if we're overriding something.
4710 implementing = null;
4715 // Static is not allowed
4717 if ((modifiers & Modifiers.STATIC) != 0){
4718 implementing = null;
4723 // If implementing is still valid, set flags
4725 if (implementing != null){
4727 // When implementing interface methods, set NewSlot
4728 // unless, we are overwriting a method.
4730 if (implementing.DeclaringType.IsInterface){
4731 if ((modifiers & Modifiers.OVERRIDE) == 0)
4732 flags |= MethodAttributes.NewSlot;
4735 MethodAttributes.Virtual |
4736 MethodAttributes.HideBySig;
4738 // Set Final unless we're virtual, abstract or already overriding a method.
4739 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4740 flags |= MethodAttributes.Final;
4743 EmitContext ec = method.CreateEmitContext (container, null);
4745 DefineMethodBuilder (ec, container, method_name, ParameterTypes);
4747 if (builder == null)
4750 if ((modifiers & Modifiers.UNSAFE) != 0)
4751 builder.InitLocals = false;
4753 if (implementing != null){
4755 // clear the pending implemntation flag
4757 if (member is Indexer) {
4758 container.Pending.ImplementIndexer (
4759 member.InterfaceType, builder, method.ReturnType,
4760 ParameterInfo, member.IsExplicitImpl);
4762 container.Pending.ImplementMethod (
4763 member.InterfaceType, name, method.ReturnType,
4764 ParameterInfo, member.IsExplicitImpl);
4766 if (member.IsExplicitImpl)
4767 container.TypeBuilder.DefineMethodOverride (
4768 builder, implementing);
4772 TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes);
4773 TypeManager.AddMethod (builder, method);
4780 /// Create the MethodBuilder for the method
4782 void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
4784 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4786 if ((modifiers & extern_static) == extern_static) {
4788 if (method.OptAttributes != null) {
4789 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
4790 if (dllimport_attribute != null) {
4791 flags |= MethodAttributes.PinvokeImpl;
4792 builder = dllimport_attribute.DefinePInvokeMethod (
4793 ec, container.TypeBuilder, method_name, flags,
4794 method.ReturnType, ParameterTypes);
4800 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4801 // We are more strict than Microsoft and report CS0626 like error
4802 if (method.OptAttributes == null ||
4803 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
4804 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",
4805 method.GetSignatureForError ());
4810 builder = container.TypeBuilder.DefineMethod (
4811 method_name, flags, method.CallingConventions,
4812 method.ReturnType, ParameterTypes);
4818 public void Emit (TypeContainer container, Attributable kind)
4821 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4822 ec = method.CreateEmitContext (container, builder.GetILGenerator ());
4824 ec = method.CreateEmitContext (container, null);
4826 if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute () != null)
4827 ec.TestObsoleteMethodUsage = false;
4829 Attributes OptAttributes = method.OptAttributes;
4831 if (OptAttributes != null)
4832 OptAttributes.Emit (ec, kind);
4834 if (member is MethodCore)
4835 ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder);
4837 ToplevelBlock block = method.Block;
4839 SourceMethod source = SourceMethod.Create (
4840 container, MethodBuilder, method.Block);
4843 // Handle destructors specially
4845 // FIXME: This code generates buggy code
4847 if (member is Destructor)
4848 EmitDestructor (ec, block);
4850 ec.EmitTopBlock (method, block, ParameterInfo);
4853 source.CloseMethod ();
4856 void EmitDestructor (EmitContext ec, ToplevelBlock block)
4858 ILGenerator ig = ec.ig;
4860 Label finish = ig.DefineLabel ();
4862 block.SetDestructor ();
4864 ig.BeginExceptionBlock ();
4865 ec.ReturnLabel = finish;
4866 ec.HasReturnLabel = true;
4867 ec.EmitTopBlock (method, block, null);
4869 // ig.MarkLabel (finish);
4870 ig.BeginFinallyBlock ();
4872 if (ec.ContainerType.BaseType != null) {
4873 Expression member_lookup = Expression.MemberLookup (
4874 ec, ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4875 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4877 if (member_lookup != null){
4878 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
4880 ig.Emit (OpCodes.Ldarg_0);
4881 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
4885 ig.EndExceptionBlock ();
4886 //ig.MarkLabel (ec.ReturnLabel);
4887 ig.Emit (OpCodes.Ret);
4891 // TODO: Should derive from MethodCore
4892 public class Destructor : Method {
4894 public Destructor (TypeContainer ds, Expression return_type, int mod,
4895 string name, Parameters parameters, Attributes attrs,
4897 : base (ds, return_type, mod, false, new MemberName (name, l),
4901 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4903 if (a.Type == TypeManager.conditional_attribute_type) {
4904 Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
4905 GetSignatureForError ());
4909 base.ApplyAttributeBuilder (a, cb);
4912 public override string GetSignatureForError ()
4914 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
4919 abstract public class MemberBase : MemberCore {
4920 public Expression Type;
4922 public MethodAttributes flags;
4924 protected readonly int explicit_mod_flags;
4927 // The "short" name of this property / indexer / event. This is the
4928 // name without the explicit interface.
4930 public string ShortName {
4931 get { return MemberName.Name; }
4933 SetMemberName (new MemberName (MemberName.Left, value, Location));
4938 // The type of this property / indexer / event
4941 public Type MemberType {
4943 if (member_type == null && Type != null) {
4944 EmitContext ec = Parent.EmitContext;
4945 bool old_unsafe = ec.InUnsafe;
4946 ec.InUnsafe = InUnsafe;
4947 Type = Type.ResolveAsTypeTerminal (ec, false);
4948 ec.InUnsafe = old_unsafe;
4950 member_type = Type.Type;
4958 // Whether this is an interface member.
4960 public bool IsInterface;
4963 // If true, this is an explicit interface implementation
4965 public bool IsExplicitImpl;
4968 // The interface type we are explicitly implementing
4970 public Type InterfaceType = null;
4973 // The constructor is only exposed to our children
4975 protected MemberBase (TypeContainer parent, Expression type, int mod,
4976 int allowed_mod, int def_mod, MemberName name,
4978 : base (parent, name, attrs)
4980 explicit_mod_flags = mod;
4982 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
4983 IsExplicitImpl = (MemberName.Left != null);
4986 protected virtual bool CheckBase ()
4988 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
4989 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4993 if ((RootContext.WarningLevel >= 4) &&
4994 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
4995 ((ModFlags & Modifiers.PROTECTED) != 0) &&
4996 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
4997 Report.Warning (628, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
5002 protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
5006 foreach (Type partype in parameters){
5007 if (partype == TypeManager.void_type) {
5009 1547, Location, "Keyword 'void' cannot " +
5010 "be used in this context");
5014 if (partype.IsPointer){
5017 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
5021 if (ds.AsAccessible (partype, ModFlags))
5024 if (this is Indexer)
5025 Report.Error (55, Location,
5026 "Inconsistent accessibility: parameter type `" +
5027 TypeManager.CSharpName (partype) + "' is less " +
5028 "accessible than indexer `" + GetSignatureForError () + "'");
5029 else if ((this is Method) && ((Method) this).IsOperator != null)
5030 Report.Error (57, Location,
5031 "Inconsistent accessibility: parameter type `" +
5032 TypeManager.CSharpName (partype) + "' is less " +
5033 "accessible than operator `" + GetSignatureForError () + "'");
5035 Report.Error (51, Location,
5036 "Inconsistent accessibility: parameter type `" +
5037 TypeManager.CSharpName (partype) + "' is less " +
5038 "accessible than method `" + GetSignatureForError () + "'");
5045 protected virtual bool DoDefine ()
5047 EmitContext ec = Parent.EmitContext;
5049 throw new InternalErrorException ("MemberBase.DoDefine called too early");
5052 throw new InternalErrorException ();
5055 ModFlags = Modifiers.PUBLIC |
5056 Modifiers.ABSTRACT |
5057 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
5059 flags = MethodAttributes.Public |
5060 MethodAttributes.Abstract |
5061 MethodAttributes.HideBySig |
5062 MethodAttributes.NewSlot |
5063 MethodAttributes.Virtual;
5065 if (!Parent.MethodModifiersValid (this))
5068 flags = Modifiers.MethodAttr (ModFlags);
5071 if (MemberType == null)
5074 CheckObsoleteType (Type);
5076 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5077 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5078 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5079 GetSignatureForError (), Parent.GetSignatureForError ());
5083 // verify accessibility
5084 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5085 Report.SymbolRelatedToPreviousError (MemberType);
5086 if (this is Property)
5087 Report.Error (53, Location,
5088 "Inconsistent accessibility: property type `" +
5089 TypeManager.CSharpName (MemberType) + "' is less " +
5090 "accessible than property `" + GetSignatureForError () + "'");
5091 else if (this is Indexer)
5092 Report.Error (54, Location,
5093 "Inconsistent accessibility: indexer return type `" +
5094 TypeManager.CSharpName (MemberType) + "' is less " +
5095 "accessible than indexer `" + GetSignatureForError () + "'");
5096 else if (this is MethodCore) {
5097 if (this is Operator)
5098 Report.Error (56, Location,
5099 "Inconsistent accessibility: return type `" +
5100 TypeManager.CSharpName (MemberType) + "' is less " +
5101 "accessible than operator `" + GetSignatureForError () + "'");
5103 Report.Error (50, Location,
5104 "Inconsistent accessibility: return type `" +
5105 TypeManager.CSharpName (MemberType) + "' is less " +
5106 "accessible than method `" + GetSignatureForError () + "'");
5108 Report.Error (52, Location,
5109 "Inconsistent accessibility: field type `" +
5110 TypeManager.CSharpName (MemberType) + "' is less " +
5111 "accessible than field `" + GetSignatureForError () + "'");
5116 if (MemberType.IsPointer && !UnsafeOK (Parent))
5119 if (IsExplicitImpl) {
5120 Expression expr = MemberName.Left.GetTypeExpression ();
5121 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
5125 InterfaceType = texpr.ResolveType (ec);
5127 if (!InterfaceType.IsInterface) {
5128 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
5132 if (!Parent.VerifyImplements (this))
5135 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5141 protected bool IsTypePermitted ()
5143 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5144 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5150 protected override bool VerifyClsCompliance(DeclSpace ds)
5152 if (base.VerifyClsCompliance (ds)) {
5156 if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
5157 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
5165 // Fields and Events both generate FieldBuilders, we use this to share
5166 // their common bits. This is also used to flag usage of the field
5168 abstract public class FieldBase : MemberBase {
5169 public FieldBuilder FieldBuilder;
5170 public Status status;
5173 public enum Status : byte {
5174 HAS_OFFSET = 4 // Used by FieldMember.
5177 static string[] attribute_targets = new string [] { "field" };
5180 /// Symbol with same name in base class/struct
5182 public MemberInfo conflict_symbol;
5185 // The constructor is only exposed to our children
5187 protected FieldBase (TypeContainer parent, Expression type, int mod,
5188 int allowed_mod, MemberName name, object init,
5190 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
5196 public override AttributeTargets AttributeTargets {
5198 return AttributeTargets.Field;
5202 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5204 if (a.Type == TypeManager.marshal_as_attr_type) {
5205 UnmanagedMarshal marshal = a.GetMarshal (this);
5206 if (marshal != null) {
5207 FieldBuilder.SetMarshal (marshal);
5212 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5213 a.Error_InvalidSecurityParent ();
5217 FieldBuilder.SetCustomAttribute (cb);
5221 // Whether this field has an initializer.
5223 public bool HasInitializer {
5225 return init != null;
5229 protected readonly Object init;
5232 Expression init_expr;
5233 bool init_expr_initialized = false;
5236 // Resolves and returns the field initializer.
5238 public Expression GetInitializerExpression (EmitContext ec)
5240 if (init_expr_initialized)
5244 if (init is Expression)
5245 e = (Expression) init;
5247 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
5249 // TODO: Any reason why we are using parent EC ?
5250 EmitContext parent_ec = Parent.EmitContext;
5252 bool old_is_static = parent_ec.IsStatic;
5253 bool old_is_ctor = parent_ec.IsConstructor;
5254 parent_ec.IsStatic = ec.IsStatic;
5255 parent_ec.IsConstructor = ec.IsConstructor;
5256 parent_ec.IsFieldInitializer = true;
5257 e = e.DoResolve (parent_ec);
5258 parent_ec.IsFieldInitializer = false;
5259 parent_ec.IsStatic = old_is_static;
5260 parent_ec.IsConstructor = old_is_ctor;
5263 init_expr_initialized = true;
5268 protected override bool CheckBase ()
5270 if (!base.CheckBase ())
5277 conflict_symbol = Parent.FindBaseMemberWithSameName (Name, false);
5278 if (conflict_symbol == null) {
5279 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5280 Report.Warning (109, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5285 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5286 Report.SymbolRelatedToPreviousError (conflict_symbol);
5287 Report.Warning (108, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5288 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5294 protected virtual bool IsFieldClsCompliant {
5296 if (FieldBuilder == null)
5299 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5303 public override string[] ValidAttributeTargets {
5305 return attribute_targets;
5309 protected override bool VerifyClsCompliance (DeclSpace ds)
5311 if (!base.VerifyClsCompliance (ds))
5314 if (!IsFieldClsCompliant) {
5315 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5321 public void SetAssigned ()
5323 caching_flags |= Flags.IsAssigned;
5327 public abstract class FieldMember: FieldBase
5329 protected FieldMember (TypeContainer parent, Expression type, int mod,
5330 int allowed_mod, MemberName name, object init, Attributes attrs)
5331 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, init, attrs)
5333 if ((mod & Modifiers.ABSTRACT) != 0)
5334 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5337 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5339 if (a.Type == TypeManager.field_offset_attribute_type)
5341 status |= Status.HAS_OFFSET;
5343 if (!Parent.HasExplicitLayout) {
5344 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5348 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5349 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5355 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5356 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5361 base.ApplyAttributeBuilder (a, cb);
5365 public override bool Define()
5367 EmitContext ec = Parent.EmitContext;
5369 throw new InternalErrorException ("FieldMember.Define called too early");
5371 if (MemberType == null || Type == null)
5374 CheckObsoleteType (Type);
5376 if (MemberType == TypeManager.void_type) {
5377 Report.Error (1547, Location, "Keyword 'void' cannot be used in this context");
5384 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5385 Report.Error (52, Location,
5386 "Inconsistent accessibility: field type `" +
5387 TypeManager.CSharpName (MemberType) + "' is less " +
5388 "accessible than field `" + GetSignatureForError () + "'");
5392 if (!IsTypePermitted ())
5395 if (MemberType.IsPointer && !UnsafeOK (Parent))
5401 public override void Emit ()
5403 if (OptAttributes != null) {
5404 EmitContext ec = new EmitContext (Parent, Location, null, FieldBuilder.FieldType, ModFlags);
5405 OptAttributes.Emit (ec, this);
5408 if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
5409 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5416 // Represents header string for documentation comment.
5418 public override string DocCommentHeader {
5419 get { return "F:"; }
5423 interface IFixedBuffer
5425 FieldInfo Element { get; }
5426 Type ElementType { get; }
5429 public class FixedFieldExternal: IFixedBuffer
5431 FieldInfo element_field;
5433 public FixedFieldExternal (FieldInfo fi)
5435 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5438 #region IFixedField Members
5440 public FieldInfo Element {
5442 return element_field;
5446 public Type ElementType {
5448 return element_field.FieldType;
5456 /// Fixed buffer implementation
5458 public class FixedField: FieldMember, IFixedBuffer
5460 public const string FixedElementName = "FixedElementField";
5461 static int GlobalCounter = 0;
5462 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5463 static FieldInfo[] fi;
5465 TypeBuilder fixed_buffer_type;
5466 FieldBuilder element;
5467 Expression size_expr;
5470 const int AllowedModifiers =
5473 Modifiers.PROTECTED |
5474 Modifiers.INTERNAL |
5477 public FixedField (TypeContainer parent, Expression type, int mod, string name,
5478 Expression size_expr, Attributes attrs, Location loc):
5479 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), null, attrs)
5481 if (RootContext.Version == LanguageVersion.ISO_1)
5482 Report.FeatureIsNotStandardized (loc, "fixed size buffers");
5484 this.size_expr = size_expr;
5487 public override bool Define()
5490 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5491 Report.Warning (-23, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5494 if (Parent.Kind != Kind.Struct) {
5495 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5496 GetSignatureForError ());
5500 if (!base.Define ())
5503 if (!TypeManager.IsPrimitiveType (MemberType)) {
5504 Report.Error (1663, Location, "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
5505 GetSignatureForError ());
5509 Constant c = size_expr.ResolveAsConstant (Parent.EmitContext, this);
5513 IntConstant buffer_size_const = c.ToInt (Location);
5514 if (buffer_size_const == null)
5517 buffer_size = buffer_size_const.Value;
5519 if (buffer_size <= 0) {
5520 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5524 int type_size = Expression.GetTypeSize (MemberType);
5526 if (buffer_size > int.MaxValue / type_size) {
5527 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5528 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5532 buffer_size *= type_size;
5535 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5537 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5538 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5539 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5540 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5542 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5543 TypeManager.RegisterFieldBase (FieldBuilder, this);
5548 public override void Emit()
5551 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5553 object[] fi_val = new object[1];
5554 fi_val [0] = buffer_size;
5556 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5557 ctor_args, fi, fi_val);
5558 fixed_buffer_type.SetCustomAttribute (cab);
5561 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5562 FieldBuilder.SetCustomAttribute (cab);
5567 protected override bool IsFieldClsCompliant {
5573 #region IFixedField Members
5575 public FieldInfo Element {
5581 public Type ElementType {
5591 // The Field class is used to represents class/struct fields during parsing.
5593 public class Field : FieldMember {
5595 // Modifiers allowed in a class declaration
5597 const int AllowedModifiers =
5600 Modifiers.PROTECTED |
5601 Modifiers.INTERNAL |
5604 Modifiers.VOLATILE |
5608 public Field (TypeContainer parent, Expression type, int mod, string name,
5609 Object expr_or_array_init, Attributes attrs, Location loc)
5610 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5611 expr_or_array_init, attrs)
5615 public override bool Define ()
5617 if (!base.Define ())
5620 if (RootContext.WarningLevel > 1){
5621 Type ptype = Parent.TypeBuilder.BaseType;
5623 // ptype is only null for System.Object while compiling corlib.
5625 TypeContainer.FindMembers (
5626 ptype, MemberTypes.Method,
5627 BindingFlags.Public |
5628 BindingFlags.Static | BindingFlags.Instance,
5629 System.Type.FilterName, Name);
5633 if ((ModFlags & Modifiers.VOLATILE) != 0){
5634 if (!MemberType.IsClass){
5635 Type vt = MemberType;
5637 if (TypeManager.IsEnumType (vt))
5638 vt = TypeManager.EnumToUnderlying (MemberType);
5640 if (!((vt == TypeManager.bool_type) ||
5641 (vt == TypeManager.sbyte_type) ||
5642 (vt == TypeManager.byte_type) ||
5643 (vt == TypeManager.short_type) ||
5644 (vt == TypeManager.ushort_type) ||
5645 (vt == TypeManager.int32_type) ||
5646 (vt == TypeManager.uint32_type) ||
5647 (vt == TypeManager.char_type) ||
5648 (vt == TypeManager.float_type) ||
5649 (!vt.IsValueType))){
5650 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5651 GetSignatureForError (), TypeManager.CSharpName (vt));
5656 if ((ModFlags & Modifiers.READONLY) != 0){
5657 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5658 GetSignatureForError ());
5663 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5665 if (Parent.Kind == Kind.Struct &&
5666 ((fa & FieldAttributes.Static) == 0) &&
5667 MemberType == Parent.TypeBuilder &&
5668 !TypeManager.IsBuiltinType (MemberType)){
5669 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5670 "' causes a cycle in the structure layout");
5675 FieldBuilder = Parent.TypeBuilder.DefineField (
5676 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5678 TypeManager.RegisterFieldBase (FieldBuilder, this);
5680 catch (ArgumentException) {
5681 Report.Warning (-24, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5688 protected override bool VerifyClsCompliance (DeclSpace ds)
5690 if (!base.VerifyClsCompliance (ds))
5693 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5694 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5702 // `set' and `get' accessors are represented with an Accessor.
5704 public class Accessor : IIteratorContainer {
5706 // Null if the accessor is empty, or a Block if not
5708 public const int AllowedModifiers =
5710 Modifiers.PROTECTED |
5711 Modifiers.INTERNAL |
5714 public ToplevelBlock Block;
5715 public Attributes Attributes;
5716 public Location Location;
5717 public int ModFlags;
5720 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5725 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5728 public void SetYields ()
5734 // Ooouh Martin, templates are missing here.
5735 // When it will be possible move here a lot of child code and template method type.
5736 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5737 protected MethodData method_data;
5738 protected ToplevelBlock block;
5739 protected ListDictionary declarative_security;
5741 // The accessor are created event if they are not wanted.
5742 // But we need them because their names are reserved.
5743 // Field says whether accessor will be emited or not
5744 public readonly bool IsDummy;
5746 protected readonly string prefix;
5748 ReturnParameter return_attributes;
5750 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5751 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5753 this.prefix = prefix;
5757 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5759 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5760 accessor.Attributes)
5762 this.prefix = prefix;
5763 this.block = accessor.Block;
5766 static MemberName SetupName (string prefix, MemberBase member, Location loc)
5768 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5771 public void UpdateName (MemberBase member)
5773 SetMemberName (SetupName (prefix, member, Location));
5776 #region IMethodData Members
5778 public ToplevelBlock Block {
5788 public CallingConventions CallingConventions {
5790 return CallingConventions.Standard;
5794 public bool IsExcluded (EmitContext ec)
5799 public MemberName MethodName {
5805 public abstract Type[] ParameterTypes { get; }
5806 public abstract Type ReturnType { get; }
5807 public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
5811 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5813 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5814 a.Type == TypeManager.conditional_attribute_type) {
5815 Report.Error (1667, a.Location,
5816 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
5817 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5821 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5822 if (declarative_security == null)
5823 declarative_security = new ListDictionary ();
5824 a.ExtractSecurityPermissionSet (declarative_security);
5828 if (a.Target == AttributeTargets.Method) {
5829 method_data.MethodBuilder.SetCustomAttribute (cb);
5833 if (a.Target == AttributeTargets.ReturnValue) {
5834 if (return_attributes == null)
5835 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5837 return_attributes.ApplyAttributeBuilder (a, cb);
5841 ApplyToExtraTarget (a, cb);
5844 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5846 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5849 public override bool Define()
5851 throw new NotSupportedException ();
5854 public virtual void Emit (TypeContainer container)
5856 EmitMethod (container);
5858 if (declarative_security != null) {
5859 foreach (DictionaryEntry de in declarative_security) {
5860 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5867 protected virtual void EmitMethod (TypeContainer container)
5869 method_data.Emit (container, this);
5872 public override bool IsClsCompliaceRequired(DeclSpace ds)
5877 public bool IsDuplicateImplementation (MethodCore method)
5879 if (!MemberName.Equals (method.MemberName))
5882 Type[] param_types = method.ParameterTypes;
5884 if (param_types.Length != ParameterTypes.Length)
5887 for (int i = 0; i < param_types.Length; i++)
5888 if (param_types [i] != ParameterTypes [i])
5891 Report.SymbolRelatedToPreviousError (method);
5892 Report.Error (111, Location, TypeContainer.Error111, method.GetSignatureForError ());
5896 public override bool IsUsed
5906 public new Location Location {
5908 return base.Location;
5913 // Represents header string for documentation comment.
5915 public override string DocCommentHeader {
5916 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5922 // Properties and Indexers both generate PropertyBuilders, we use this to share
5923 // their common bits.
5925 abstract public class PropertyBase : MethodCore {
5927 public class GetMethod: PropertyMethod
5929 static string[] attribute_targets = new string [] { "method", "return" };
5931 public GetMethod (MethodCore method):
5932 base (method, "get_")
5936 public GetMethod (MethodCore method, Accessor accessor):
5937 base (method, accessor, "get_")
5941 public override MethodBuilder Define(TypeContainer container)
5943 base.Define (container);
5945 method_data = new MethodData (method, method.ParameterInfo, ModFlags, flags, this);
5947 if (!method_data.Define (container))
5950 return method_data.MethodBuilder;
5953 public override Type ReturnType {
5955 return method.MemberType;
5959 public override string[] ValidAttributeTargets {
5961 return attribute_targets;
5966 public class SetMethod: PropertyMethod {
5968 static string[] attribute_targets = new string [] { "method", "param", "return" };
5969 ImplicitParameter param_attr;
5971 public SetMethod (MethodCore method):
5972 base (method, "set_")
5976 public SetMethod (MethodCore method, Accessor accessor):
5977 base (method, accessor, "set_")
5981 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
5983 if (a.Target == AttributeTargets.Parameter) {
5984 if (param_attr == null)
5985 param_attr = new ImplicitParameter (method_data.MethodBuilder, method.Location);
5987 param_attr.ApplyAttributeBuilder (a, cb);
5991 base.ApplyAttributeBuilder (a, cb);
5994 protected virtual InternalParameters GetParameterInfo (EmitContext ec)
5996 Parameter [] parms = new Parameter [1];
5997 parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null, method.Location);
5998 Parameters parameters = new Parameters (parms, null);
6000 bool old_unsafe = ec.InUnsafe;
6001 ec.InUnsafe = InUnsafe;
6002 Type [] types = parameters.GetParameterInfo (ec);
6003 ec.InUnsafe = old_unsafe;
6005 return new InternalParameters (types, parameters);
6008 public override MethodBuilder Define (TypeContainer container)
6010 if (container.EmitContext == null)
6011 throw new InternalErrorException ("SetMethod.Define called too early");
6013 base.Define (container);
6015 method_data = new MethodData (method, GetParameterInfo (container.EmitContext), ModFlags, flags, this);
6017 if (!method_data.Define (container))
6020 return method_data.MethodBuilder;
6023 public override Type[] ParameterTypes {
6025 return new Type[] { method.MemberType };
6029 public override Type ReturnType {
6031 return TypeManager.void_type;
6035 public override string[] ValidAttributeTargets {
6037 return attribute_targets;
6042 static string[] attribute_targets = new string [] { "property" };
6044 public abstract class PropertyMethod: AbstractPropertyEventMethod
6046 protected readonly MethodCore method;
6047 protected MethodAttributes flags;
6050 public PropertyMethod (MethodCore method, string prefix)
6051 : base (method, prefix)
6053 this.method = method;
6056 public PropertyMethod (MethodCore method, Accessor accessor,
6058 : base (method, accessor, prefix)
6060 this.method = method;
6061 this.ModFlags = accessor.ModFlags;
6062 yields = accessor.Yields;
6064 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6065 Report.FeatureIsNotStandardized (Location, "access modifiers on properties");
6069 public override AttributeTargets AttributeTargets {
6071 return AttributeTargets.Method;
6075 public override bool IsClsCompliaceRequired(DeclSpace ds)
6077 return method.IsClsCompliaceRequired (ds);
6080 public InternalParameters ParameterInfo
6083 return method_data.ParameterInfo;
6087 public virtual MethodBuilder Define (TypeContainer container)
6089 if (!method.CheckAbstractAndExtern (block != null))
6093 // Check for custom access modifier
6095 if (ModFlags == 0) {
6096 ModFlags = method.ModFlags;
6097 flags = method.flags;
6099 if (container.Kind == Kind.Interface)
6100 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6101 GetSignatureForError ());
6103 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6104 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6107 CheckModifiers (container, ModFlags);
6108 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6109 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6110 flags = Modifiers.MethodAttr (ModFlags);
6111 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6115 // Setup iterator if we are one
6118 Iterator iterator = new Iterator (this,
6119 Parent, method.ParameterInfo, ModFlags);
6121 if (!iterator.DefineIterator ())
6128 public bool HasCustomAccessModifier
6131 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6135 public override Type[] ParameterTypes {
6137 return TypeManager.NoTypes;
6141 public override EmitContext CreateEmitContext (TypeContainer tc,
6144 return new EmitContext (
6145 tc, method.Parent, method.Location, ig, ReturnType,
6146 method.ModFlags, false);
6149 public override ObsoleteAttribute GetObsoleteAttribute ()
6151 return method.GetObsoleteAttribute ();
6154 public override string GetSignatureForError()
6156 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6159 void CheckModifiers (TypeContainer container, int modflags)
6162 int mflags = method.ModFlags & Modifiers.Accessibility;
6164 if ((mflags & Modifiers.PUBLIC) != 0) {
6165 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6167 else if ((mflags & Modifiers.PROTECTED) != 0) {
6168 if ((mflags & Modifiers.INTERNAL) != 0)
6169 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6171 flags |= Modifiers.PRIVATE;
6173 else if ((mflags & Modifiers.INTERNAL) != 0)
6174 flags |= Modifiers.PRIVATE;
6176 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6177 Report.Error (273, Location,
6178 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6179 GetSignatureForError (), method.GetSignatureForError ());
6183 public override bool MarkForDuplicationCheck ()
6185 caching_flags |= Flags.TestMethodDuplication;
6191 public PropertyMethod Get, Set;
6192 public PropertyBuilder PropertyBuilder;
6193 public MethodBuilder GetBuilder, SetBuilder;
6195 protected EmitContext ec;
6197 public PropertyBase (TypeContainer ds, Expression type, int mod_flags,
6198 int allowed_mod, bool is_iface, MemberName name,
6199 Parameters parameters, Attributes attrs)
6200 : base (ds, type, mod_flags, allowed_mod, is_iface, name,
6205 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6207 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6208 a.Error_InvalidSecurityParent ();
6212 PropertyBuilder.SetCustomAttribute (cb);
6215 public override AttributeTargets AttributeTargets {
6217 return AttributeTargets.Property;
6221 public override bool Define ()
6226 if (!IsTypePermitted ())
6232 protected override bool DoDefine ()
6234 if (!base.DoDefine ())
6238 // Accessors modifiers check
6240 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6241 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6242 GetSignatureForError ());
6246 if ((Get.IsDummy || Set.IsDummy)
6247 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6248 Report.Error (276, Location,
6249 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6250 GetSignatureForError ());
6254 if (MemberType.IsAbstract && MemberType.IsSealed) {
6255 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6259 ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
6263 protected override bool CheckForDuplications ()
6265 ArrayList ar = Parent.Indexers;
6267 int arLen = ar.Count;
6269 for (int i = 0; i < arLen; i++) {
6270 Indexer m = (Indexer) ar [i];
6271 if (IsDuplicateImplementation (m))
6276 ar = Parent.Properties;
6278 int arLen = ar.Count;
6280 for (int i = 0; i < arLen; i++) {
6281 Property m = (Property) ar [i];
6282 if (IsDuplicateImplementation (m))
6290 // TODO: rename to Resolve......
6291 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
6293 PropertyInfo base_property = container.BaseCache.FindMemberToOverride (
6294 container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
6296 if (base_property == null)
6299 base_ret_type = base_property.PropertyType;
6300 MethodInfo get_accessor = base_property.GetGetMethod (true);
6301 MethodInfo set_accessor = base_property.GetSetMethod (true);
6302 MethodAttributes get_accessor_access, set_accessor_access;
6304 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6305 if (Get != null && !Get.IsDummy && get_accessor == null) {
6306 Report.SymbolRelatedToPreviousError (base_property);
6307 Report.Error (545, Location, "`{0}.get': cannot override because `{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6310 if (Set != null && !Set.IsDummy && set_accessor == null) {
6311 Report.SymbolRelatedToPreviousError (base_property);
6312 Report.Error (546, Location, "`{0}.set': cannot override because `{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6317 // Check base class accessors access
6320 // TODO: rewrite to reuse Get|Set.CheckAccessModifiers and share code there
6321 get_accessor_access = set_accessor_access = 0;
6322 if ((ModFlags & Modifiers.NEW) == 0) {
6323 if (get_accessor != null) {
6324 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6325 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6327 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6328 Error_CannotChangeAccessModifiers (get_accessor, get_accessor_access, ".get");
6331 if (set_accessor != null) {
6332 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6333 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6335 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6336 Error_CannotChangeAccessModifiers (set_accessor, set_accessor_access, ".set");
6341 // Get the less restrictive access
6343 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6346 public override void Emit ()
6349 // The PropertyBuilder can be null for explicit implementations, in that
6350 // case, we do not actually emit the ".property", so there is nowhere to
6351 // put the attribute
6353 if (PropertyBuilder != null && OptAttributes != null)
6354 OptAttributes.Emit (ec, this);
6366 /// Tests whether accessors are not in collision with some method (CS0111)
6368 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6370 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6373 public override bool IsUsed
6379 return Get.IsUsed | Set.IsUsed;
6383 protected override void SetMemberName (MemberName new_name)
6385 base.SetMemberName (new_name);
6387 Get.UpdateName (this);
6388 Set.UpdateName (this);
6391 public override string[] ValidAttributeTargets {
6393 return attribute_targets;
6398 // Represents header string for documentation comment.
6400 public override string DocCommentHeader {
6401 get { return "P:"; }
6405 public class Property : PropertyBase {
6406 const int AllowedModifiers =
6409 Modifiers.PROTECTED |
6410 Modifiers.INTERNAL |
6414 Modifiers.OVERRIDE |
6415 Modifiers.ABSTRACT |
6418 Modifiers.METHOD_YIELDS |
6421 const int AllowedInterfaceModifiers =
6424 public Property (TypeContainer ds, Expression type, int mod, bool is_iface,
6425 MemberName name, Attributes attrs, Accessor get_block,
6427 : base (ds, type, mod,
6428 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6429 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs)
6431 if (get_block == null)
6432 Get = new GetMethod (this);
6434 Get = new GetMethod (this, get_block);
6436 if (set_block == null)
6437 Set = new SetMethod (this);
6439 Set = new SetMethod (this, set_block);
6442 public override bool Define ()
6444 if (!base.Define ())
6450 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6453 GetBuilder = Get.Define (Parent);
6454 if (GetBuilder == null)
6459 SetBuilder = Set.Define (Parent);
6460 if (SetBuilder == null)
6463 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
6466 // FIXME - PropertyAttributes.HasDefault ?
6468 PropertyAttributes prop_attr = PropertyAttributes.None;
6470 prop_attr |= PropertyAttributes.RTSpecialName |
6471 PropertyAttributes.SpecialName;
6473 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6474 Name, prop_attr, MemberType, null);
6477 PropertyBuilder.SetGetMethod (GetBuilder);
6480 PropertyBuilder.SetSetMethod (SetBuilder);
6487 /// Gigantic workaround for lameness in SRE follows :
6488 /// This class derives from EventInfo and attempts to basically
6489 /// wrap around the EventBuilder so that FindMembers can quickly
6490 /// return this in it search for members
6492 public class MyEventBuilder : EventInfo {
6495 // We use this to "point" to our Builder which is
6496 // not really a MemberInfo
6498 EventBuilder MyBuilder;
6501 // We "catch" and wrap these methods
6503 MethodInfo raise, remove, add;
6505 EventAttributes attributes;
6506 Type declaring_type, reflected_type, event_type;
6511 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6513 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6515 // And now store the values in our own fields.
6517 declaring_type = type_builder;
6519 reflected_type = type_builder;
6521 attributes = event_attr;
6524 this.event_type = event_type;
6528 // Methods that you have to override. Note that you only need
6529 // to "implement" the variants that take the argument (those are
6530 // the "abstract" methods, the others (GetAddMethod()) are
6533 public override MethodInfo GetAddMethod (bool nonPublic)
6538 public override MethodInfo GetRemoveMethod (bool nonPublic)
6543 public override MethodInfo GetRaiseMethod (bool nonPublic)
6549 // These methods make "MyEventInfo" look like a Builder
6551 public void SetRaiseMethod (MethodBuilder raiseMethod)
6553 raise = raiseMethod;
6554 MyBuilder.SetRaiseMethod (raiseMethod);
6557 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6559 remove = removeMethod;
6560 MyBuilder.SetRemoveOnMethod (removeMethod);
6563 public void SetAddOnMethod (MethodBuilder addMethod)
6566 MyBuilder.SetAddOnMethod (addMethod);
6569 public void SetCustomAttribute (CustomAttributeBuilder cb)
6571 MyBuilder.SetCustomAttribute (cb);
6574 public override object [] GetCustomAttributes (bool inherit)
6576 // FIXME : There's nothing which can be seemingly done here because
6577 // we have no way of getting at the custom attribute objects of the
6582 public override object [] GetCustomAttributes (Type t, bool inherit)
6584 // FIXME : Same here !
6588 public override bool IsDefined (Type t, bool b)
6593 public override EventAttributes Attributes {
6599 public override string Name {
6605 public override Type DeclaringType {
6607 return declaring_type;
6611 public override Type ReflectedType {
6613 return reflected_type;
6617 public Type EventType {
6623 public void SetUsed ()
6625 if (my_event != null) {
6626 my_event.SetAssigned ();
6627 my_event.SetMemberIsUsed ();
6633 /// For case when event is declared like property (with add and remove accessors).
6635 public class EventProperty: Event {
6637 static string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6639 public EventProperty (TypeContainer parent, Expression type, int mod_flags,
6640 bool is_iface, MemberName name, Object init,
6641 Attributes attrs, Accessor add, Accessor remove)
6642 : base (parent, type, mod_flags, is_iface, name, init, attrs)
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,
6670 : base (parent, type, mod_flags, is_iface, name, init, attrs)
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, method.Location);
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 ObsoleteAttribute GetObsoleteAttribute ()
6855 return method.GetObsoleteAttribute ();
6858 public override string[] ValidAttributeTargets {
6860 return attribute_targets;
6866 const int AllowedModifiers =
6869 Modifiers.PROTECTED |
6870 Modifiers.INTERNAL |
6875 Modifiers.OVERRIDE |
6879 const int AllowedInterfaceModifiers =
6882 public DelegateMethod Add, Remove;
6883 public MyEventBuilder EventBuilder;
6884 public MethodBuilder AddBuilder, RemoveBuilder;
6886 public Event (TypeContainer parent, Expression type, int mod_flags,
6887 bool is_iface, MemberName name, Object init, Attributes attrs)
6888 : base (parent, type, mod_flags,
6889 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6892 IsInterface = is_iface;
6895 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6897 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6898 a.Error_InvalidSecurityParent ();
6902 EventBuilder.SetCustomAttribute (cb);
6905 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6907 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6910 public override AttributeTargets AttributeTargets {
6912 return AttributeTargets.Event;
6916 public override bool Define ()
6918 EventAttributes e_attr;
6919 e_attr = EventAttributes.None;
6924 if (init != null && ((ModFlags & Modifiers.ABSTRACT) != 0)){
6925 Report.Error (74, Location, "`" + GetSignatureForError () +
6926 "': abstract event cannot have an initializer");
6930 if (!MemberType.IsSubclassOf (TypeManager.delegate_type)) {
6931 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
6935 EmitContext ec = Parent.EmitContext;
6937 throw new InternalErrorException ("Event.Define called too early?");
6938 bool old_unsafe = ec.InUnsafe;
6939 ec.InUnsafe = InUnsafe;
6941 Parameter [] parms = new Parameter [1];
6942 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null, Location);
6943 Parameters parameters = new Parameters (parms, null);
6944 Type [] types = parameters.GetParameterInfo (ec);
6945 InternalParameters ip = new InternalParameters (types, parameters);
6947 ec.InUnsafe = old_unsafe;
6953 // Now define the accessors
6956 AddBuilder = Add.Define (Parent, ip);
6957 if (AddBuilder == null)
6960 RemoveBuilder = Remove.Define (Parent, ip);
6961 if (RemoveBuilder == null)
6964 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
6966 if (Add.Block == null && Remove.Block == null && !IsInterface) {
6967 FieldBuilder = Parent.TypeBuilder.DefineField (
6969 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
6970 TypeManager.RegisterPrivateFieldOfEvent (
6971 (EventInfo) EventBuilder, FieldBuilder);
6972 TypeManager.RegisterFieldBase (FieldBuilder, this);
6975 EventBuilder.SetAddOnMethod (AddBuilder);
6976 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
6978 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
6982 protected override bool CheckBase ()
6984 if (!base.CheckBase ())
6987 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
6988 if (!(conflict_symbol is EventInfo)) {
6989 Report.SymbolRelatedToPreviousError (conflict_symbol);
6990 Report.Error (72, Location, "Event `{0}' can override only event", GetSignatureForError ());
6998 public override void Emit ()
7000 if (OptAttributes != null) {
7001 EmitContext ec = new EmitContext (
7002 Parent, Location, null, MemberType, ModFlags);
7003 OptAttributes.Emit (ec, this);
7007 Remove.Emit (Parent);
7012 public override string GetSignatureForError ()
7014 return base.GetSignatureForError ();
7018 // Represents header string for documentation comment.
7020 public override string DocCommentHeader {
7021 get { return "E:"; }
7026 public class Indexer : PropertyBase {
7028 class GetIndexerMethod: GetMethod
7030 public GetIndexerMethod (MethodCore method):
7035 public GetIndexerMethod (MethodCore method, Accessor accessor):
7036 base (method, accessor)
7040 public override Type[] ParameterTypes {
7042 return method.ParameterTypes;
7047 class SetIndexerMethod: SetMethod
7049 readonly Parameters parameters;
7051 public SetIndexerMethod (MethodCore method):
7056 public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
7057 base (method, accessor)
7059 this.parameters = parameters;
7062 public override Type[] ParameterTypes {
7064 int top = method.ParameterTypes.Length;
7065 Type [] set_pars = new Type [top + 1];
7066 method.ParameterTypes.CopyTo (set_pars, 0);
7067 set_pars [top] = method.MemberType;
7072 protected override InternalParameters GetParameterInfo (EmitContext ec)
7074 Parameter [] fixed_parms = parameters.FixedParameters;
7076 if (fixed_parms == null){
7077 throw new Exception ("We currently do not support only array arguments in an indexer at: " + method.Location);
7078 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7079 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7081 // Here is the problem: the `value' parameter has
7082 // to come *after* the array parameter in the declaration
7084 // X (object [] x, Type value)
7087 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7088 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7092 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
7094 fixed_parms.CopyTo (tmp, 0);
7095 tmp [fixed_parms.Length] = new Parameter (
7096 method.Type, "value", Parameter.Modifier.NONE, null, method.Location);
7098 Parameters set_formal_params = new Parameters (tmp, null);
7099 Type [] types = set_formal_params.GetParameterInfo (ec);
7101 return new InternalParameters (types, set_formal_params);
7106 const int AllowedModifiers =
7109 Modifiers.PROTECTED |
7110 Modifiers.INTERNAL |
7114 Modifiers.OVERRIDE |
7119 const int AllowedInterfaceModifiers =
7123 public Indexer (TypeContainer ds, Expression type, MemberName name, int mod,
7124 bool is_iface, Parameters parameters, Attributes attrs,
7125 Accessor get_block, Accessor set_block)
7126 : base (ds, type, mod,
7127 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7128 is_iface, name, parameters, attrs)
7130 if (get_block == null)
7131 Get = new GetIndexerMethod (this);
7133 Get = new GetIndexerMethod (this, get_block);
7135 if (set_block == null)
7136 Set = new SetIndexerMethod (this);
7138 Set = new SetIndexerMethod (this, parameters, set_block);
7141 public override bool Define ()
7143 PropertyAttributes prop_attr =
7144 PropertyAttributes.RTSpecialName |
7145 PropertyAttributes.SpecialName;
7147 if (!base.Define ())
7150 if (MemberType == TypeManager.void_type) {
7151 Report.Error (620, Location, "Indexers cannot have void type");
7155 if (OptAttributes != null) {
7156 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type, ec);
7157 if (indexer_attr != null) {
7158 // Remove the attribute from the list because it is not emitted
7159 OptAttributes.Attrs.Remove (indexer_attr);
7161 ShortName = indexer_attr.GetIndexerAttributeValue (ec);
7163 if (IsExplicitImpl) {
7164 Report.Error (415, indexer_attr.Location,
7165 "The `IndexerName' attribute is valid only on an " +
7166 "indexer that is not an explicit interface member declaration");
7170 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7171 Report.Error (609, indexer_attr.Location,
7172 "Cannot set the `IndexerName' attribute on an indexer marked override");
7176 if (!Tokenizer.IsValidIdentifier (ShortName)) {
7177 Report.Error (633, indexer_attr.Location,
7178 "The argument to the `IndexerName' attribute must be a valid identifier");
7184 if (InterfaceType != null) {
7185 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7186 if (base_IndexerName != Name)
7187 ShortName = base_IndexerName;
7190 if (!Parent.AddToMemberContainer (this) ||
7191 !Parent.AddToMemberContainer (Get) || !Parent.AddToMemberContainer (Set))
7197 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7199 GetBuilder = Get.Define (Parent);
7200 if (GetBuilder == null)
7205 SetBuilder = Set.Define (Parent);
7206 if (SetBuilder == null)
7211 // Now name the parameters
7213 Parameter [] p = Parameters.FixedParameters;
7215 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
7216 Report.Error (631, Location, "ref and out are not valid in this context");
7222 for (i = 0; i < p.Length; ++i) {
7224 GetBuilder.DefineParameter (
7225 i + 1, p [i].Attributes, p [i].Name);
7228 SetBuilder.DefineParameter (
7229 i + 1, p [i].Attributes, p [i].Name);
7233 SetBuilder.DefineParameter (
7234 i + 1, ParameterAttributes.None, "value");
7236 if (i != ParameterTypes.Length) {
7237 Parameter array_param = Parameters.ArrayParameter;
7239 SetBuilder.DefineParameter (
7240 i + 1, array_param.Attributes, array_param.Name);
7244 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7245 Name, prop_attr, MemberType, ParameterTypes);
7248 PropertyBuilder.SetGetMethod (GetBuilder);
7251 PropertyBuilder.SetSetMethod (SetBuilder);
7253 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
7258 public override string GetSignatureForError ()
7260 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7261 if (MemberName.Left != null) {
7263 sb.Append (MemberName.Left);
7266 sb.Append (".this");
7267 sb.Append (Parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7268 return sb.ToString ();
7271 public override bool MarkForDuplicationCheck ()
7273 caching_flags |= Flags.TestMethodDuplication;
7279 public class Operator : MethodCore, IIteratorContainer {
7281 const int AllowedModifiers =
7287 public enum OpType : byte {
7297 // Unary and Binary operators
7320 // Implicit and Explicit
7324 // Just because of enum
7328 public readonly OpType OperatorType;
7329 public MethodBuilder OperatorMethodBuilder;
7331 public Method OperatorMethod;
7333 static string[] attribute_targets = new string [] { "method", "return" };
7335 public Operator (TypeContainer parent, OpType type, Expression ret_type,
7336 int mod_flags, Parameters parameters,
7337 ToplevelBlock block, Attributes attrs, Location loc)
7338 : base (parent, ret_type, mod_flags, AllowedModifiers, false,
7339 new MemberName ("op_" + type, loc), attrs, parameters)
7341 OperatorType = type;
7345 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7347 OperatorMethod.ApplyAttributeBuilder (a, cb);
7350 public override AttributeTargets AttributeTargets {
7352 return AttributeTargets.Method;
7356 protected override bool CheckForDuplications()
7358 ArrayList ar = Parent.Operators;
7360 int arLen = ar.Count;
7362 for (int i = 0; i < arLen; i++) {
7363 Operator o = (Operator) ar [i];
7364 if (IsDuplicateImplementation (o))
7369 ar = Parent.Methods;
7371 int arLen = ar.Count;
7373 for (int i = 0; i < arLen; i++) {
7374 Method m = (Method) ar [i];
7375 if (IsDuplicateImplementation (m))
7383 public override bool Define ()
7385 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7386 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7387 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7394 if (MemberType == TypeManager.void_type) {
7395 Report.Error (590, Location, "User-defined operators cannot return void");
7399 OperatorMethod = new Method (
7400 Parent, Type, ModFlags, false, MemberName,
7401 Parameters, OptAttributes);
7403 OperatorMethod.Block = Block;
7404 OperatorMethod.IsOperator = this;
7405 OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7406 OperatorMethod.Define ();
7408 if (OperatorMethod.MethodBuilder == null)
7411 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
7413 parameter_types = OperatorMethod.ParameterTypes;
7414 Type declaring_type = OperatorMethodBuilder.DeclaringType;
7415 Type return_type = OperatorMethod.ReturnType;
7416 Type first_arg_type = parameter_types [0];
7421 // Rules for conversion operators
7423 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7424 if (first_arg_type == return_type && first_arg_type == declaring_type){
7425 Report.Error (555, Location,
7426 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7430 if (first_arg_type != declaring_type && return_type != declaring_type){
7433 "User-defined conversion must convert to or from the " +
7438 if (first_arg_type == TypeManager.object_type ||
7439 return_type == TypeManager.object_type){
7442 "User-defined conversion cannot convert to or from " +
7447 if (first_arg_type.IsInterface || return_type.IsInterface){
7448 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7449 GetSignatureForError ());
7453 if (first_arg_type.IsSubclassOf (return_type)
7454 || return_type.IsSubclassOf (first_arg_type)){
7455 if (declaring_type.IsSubclassOf (return_type)) {
7456 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from base class",
7457 GetSignatureForError ());
7460 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from derived class",
7461 GetSignatureForError ());
7464 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7465 if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
7466 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");
7469 } else if (Parameters.FixedParameters.Length == 1) {
7470 // Checks for Unary operators
7472 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7473 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7474 Report.Error (448, Location,
7475 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7478 if (first_arg_type != declaring_type) {
7480 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7485 if (first_arg_type != declaring_type){
7488 "The parameter of a unary operator must be the " +
7493 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7494 if (return_type != TypeManager.bool_type){
7497 "The return type of operator True or False " +
7504 // Checks for Binary operators
7506 if (first_arg_type != declaring_type &&
7507 parameter_types [1] != declaring_type){
7510 "One of the parameters of a binary operator must " +
7511 "be the containing type");
7519 public override void Emit ()
7522 // abstract or extern methods have no bodies
7524 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7527 OperatorMethod.Emit ();
7531 // Operator cannot be override
7532 protected override MethodInfo FindOutBaseMethod (TypeContainer container, ref Type base_ret_type)
7537 public static string GetName (OpType ot)
7540 case OpType.LogicalNot:
7542 case OpType.OnesComplement:
7544 case OpType.Increment:
7546 case OpType.Decrement:
7552 case OpType.Addition:
7554 case OpType.Subtraction:
7556 case OpType.UnaryPlus:
7558 case OpType.UnaryNegation:
7560 case OpType.Multiply:
7562 case OpType.Division:
7564 case OpType.Modulus:
7566 case OpType.BitwiseAnd:
7568 case OpType.BitwiseOr:
7570 case OpType.ExclusiveOr:
7572 case OpType.LeftShift:
7574 case OpType.RightShift:
7576 case OpType.Equality:
7578 case OpType.Inequality:
7580 case OpType.GreaterThan:
7582 case OpType.LessThan:
7584 case OpType.GreaterThanOrEqual:
7586 case OpType.LessThanOrEqual:
7588 case OpType.Implicit:
7590 case OpType.Explicit:
7596 public static OpType GetOperatorType (string name)
7598 if (name.StartsWith ("op_")){
7599 for (int i = 0; i < Unary.oper_names.Length; ++i) {
7600 if (Unary.oper_names [i] == name)
7604 for (int i = 0; i < Binary.oper_names.Length; ++i) {
7605 if (Binary.oper_names [i] == name)
7612 public override string GetSignatureForError ()
7614 StringBuilder sb = new StringBuilder ();
7615 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7616 sb.AppendFormat ("{0}.{1} operator {2}", Parent.GetSignatureForError (), GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type));
7619 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7622 sb.Append (Parameters.GetSignatureForError ());
7623 return sb.ToString ();
7626 public override bool MarkForDuplicationCheck ()
7628 caching_flags |= Flags.TestMethodDuplication;
7632 public override string[] ValidAttributeTargets {
7634 return attribute_targets;
7640 // This is used to compare method signatures
7642 struct MethodSignature {
7644 public Type RetType;
7645 public Type [] Parameters;
7648 /// This delegate is used to extract methods which have the
7649 /// same signature as the argument
7651 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7653 public MethodSignature (string name, Type ret_type, Type [] parameters)
7658 if (parameters == null)
7659 Parameters = TypeManager.NoTypes;
7661 Parameters = parameters;
7664 public override string ToString ()
7667 if (Parameters.Length != 0){
7668 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7669 for (int i = 0; i < Parameters.Length; i++){
7670 sb.Append (Parameters [i]);
7671 if (i+1 < Parameters.Length)
7674 pars = sb.ToString ();
7677 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7680 public override int GetHashCode ()
7682 return Name.GetHashCode ();
7685 public override bool Equals (Object o)
7687 MethodSignature other = (MethodSignature) o;
7689 if (other.Name != Name)
7692 if (other.RetType != RetType)
7695 if (Parameters == null){
7696 if (other.Parameters == null)
7701 if (other.Parameters == null)
7704 int c = Parameters.Length;
7705 if (other.Parameters.Length != c)
7708 for (int i = 0; i < c; i++)
7709 if (other.Parameters [i] != Parameters [i])
7715 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7717 MethodSignature sig = (MethodSignature) filter_criteria;
7719 if (m.Name != sig.Name)
7723 MethodInfo mi = m as MethodInfo;
7724 PropertyInfo pi = m as PropertyInfo;
7727 ReturnType = mi.ReturnType;
7728 else if (pi != null)
7729 ReturnType = pi.PropertyType;
7734 // we use sig.RetType == null to mean `do not check the
7735 // method return value.
7737 if (sig.RetType != null)
7738 if (ReturnType != sig.RetType)
7743 args = TypeManager.GetArgumentTypes (mi);
7745 args = TypeManager.GetArgumentTypes (pi);
7746 Type [] sigp = sig.Parameters;
7748 if (args.Length != sigp.Length)
7751 for (int i = args.Length; i > 0; ){
7753 if (args [i] != sigp [i])