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 throw new InternalErrorException (mc.Location, mc.GetSignatureForError (), e);
84 public virtual void Emit ()
86 foreach (MemberCore mc in this)
91 public class MethodArrayList : MemberCoreArrayList
99 CachedMethods cached_method;
100 TypeContainer container;
102 public MethodArrayList (TypeContainer container)
104 this.container = container;
108 /// Method container contains Equals method
110 public bool HasEquals {
112 cached_method |= CachedMethods.Equals;
116 return (cached_method & CachedMethods.Equals) != 0;
121 /// Method container contains GetHashCode method
123 public bool HasGetHashCode {
125 cached_method |= CachedMethods.GetHashCode;
129 return (cached_method & CachedMethods.GetHashCode) != 0;
133 public override void DefineContainerMembers ()
135 base.DefineContainerMembers ();
137 if (HasEquals && !HasGetHashCode) {
138 Report.Warning (659, 3, container.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
144 public sealed class IndexerArrayList : MemberCoreArrayList
147 /// The indexer name for this container
149 public string IndexerName = DefaultIndexerName;
151 bool seen_normal_indexers = false;
153 TypeContainer container;
155 public IndexerArrayList (TypeContainer container)
157 this.container = container;
161 /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
162 /// class is consistent. Either it is `Item' or it is the name defined by all the
163 /// indexers with the `IndexerName' attribute.
165 /// Turns out that the IndexerNameAttribute is applied to each indexer,
166 /// but it is never emitted, instead a DefaultMember attribute is attached
169 public override void DefineContainerMembers()
171 base.DefineContainerMembers ();
173 string class_indexer_name = null;
176 // If there's both an explicit and an implicit interface implementation, the
177 // explicit one actually implements the interface while the other one is just
178 // a normal indexer. See bug #37714.
181 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
182 foreach (Indexer i in this) {
183 if (i.InterfaceType != null) {
184 if (seen_normal_indexers)
185 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
189 seen_normal_indexers = true;
191 if (class_indexer_name == null) {
192 class_indexer_name = i.ShortName;
196 if (i.ShortName != class_indexer_name)
197 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");
200 if (class_indexer_name != null)
201 IndexerName = class_indexer_name;
204 public override void Emit ()
208 if (!seen_normal_indexers)
211 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
212 container.TypeBuilder.SetCustomAttribute (cb);
216 public class OperatorArrayList: MemberCoreArrayList
218 TypeContainer container;
220 public OperatorArrayList (TypeContainer container)
222 this.container = container;
226 // Operator pair checking
231 public Type ret_type;
232 public Type type1, type2;
234 public Operator.OpType ot;
236 public OperatorEntry (int f, Operator o)
240 ret_type = o.MemberType;
241 Type [] pt = o.ParameterTypes;
248 public override int GetHashCode ()
250 return ret_type.GetHashCode ();
253 public override bool Equals (object o)
255 OperatorEntry other = (OperatorEntry) o;
257 if (other.ret_type != ret_type)
259 if (other.type1 != type1)
261 if (other.type2 != type2)
268 // Checks that some operators come in pairs:
274 // They are matched based on the return type and the argument types
276 void CheckPairedOperators ()
278 IDictionary pairs = new HybridDictionary ();
279 Operator true_op = null;
280 Operator false_op = null;
281 bool has_equality_or_inequality = false;
283 // Register all the operators we care about.
284 foreach (Operator op in this){
287 // Skip erroneous code.
288 if (op.MethodBuilder == null)
291 switch (op.OperatorType){
292 case Operator.OpType.Equality:
294 has_equality_or_inequality = true;
296 case Operator.OpType.Inequality:
298 has_equality_or_inequality = true;
301 case Operator.OpType.True:
304 case Operator.OpType.False:
308 case Operator.OpType.GreaterThan:
310 case Operator.OpType.LessThan:
313 case Operator.OpType.GreaterThanOrEqual:
315 case Operator.OpType.LessThanOrEqual:
321 OperatorEntry oe = new OperatorEntry (reg, op);
323 object o = pairs [oe];
327 oe = (OperatorEntry) o;
332 if (true_op != null){
333 if (false_op == null)
334 Report.Error (216, true_op.Location, "The operator `{0}' requires a matching operator `false' to also be defined",
335 true_op.GetSignatureForError ());
336 } else if (false_op != null)
337 Report.Error (216, false_op.Location, "The operator `{0}' requires a matching operator `true' to also be defined",
338 false_op.GetSignatureForError ());
341 // Look for the mistakes.
343 foreach (DictionaryEntry de in pairs){
344 OperatorEntry oe = (OperatorEntry) de.Key;
351 case Operator.OpType.Equality:
354 case Operator.OpType.Inequality:
357 case Operator.OpType.GreaterThan:
360 case Operator.OpType.LessThan:
363 case Operator.OpType.GreaterThanOrEqual:
366 case Operator.OpType.LessThanOrEqual:
370 Report.Error (216, oe.op.Location,
371 "The operator `{0}' requires a matching operator `{1}' to also be defined",
372 oe.op.GetSignatureForError (), s);
375 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
376 if (container.Methods == null || !container.Methods.HasEquals)
377 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
379 if (container.Methods == null || !container.Methods.HasGetHashCode)
380 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
384 public override void DefineContainerMembers ()
386 base.DefineContainerMembers ();
387 CheckPairedOperators ();
392 // Whether this is a struct, class or interface
393 public readonly Kind Kind;
395 // Holds a list of classes and structures
396 protected ArrayList types;
398 // Holds the list of properties
399 MemberCoreArrayList properties;
401 // Holds the list of enumerations
402 MemberCoreArrayList enums;
404 // Holds the list of delegates
405 MemberCoreArrayList delegates;
407 // Holds the list of constructors
408 protected MemberCoreArrayList instance_constructors;
410 // Holds the list of fields
411 MemberCoreArrayList fields;
413 // Holds a list of fields that have initializers
414 protected ArrayList initialized_fields;
416 // Holds a list of static fields that have initializers
417 protected ArrayList initialized_static_fields;
419 // Holds the list of constants
420 MemberCoreArrayList constants;
423 MemberCoreArrayList interfaces;
425 // Holds the methods.
426 MethodArrayList methods;
429 protected MemberCoreArrayList events;
431 // Holds the indexers
432 IndexerArrayList indexers;
434 // Holds the operators
435 MemberCoreArrayList operators;
437 // Holds the iterators
441 // Pointers to the default constructor and the default static constructor
443 protected Constructor default_constructor;
444 protected Constructor default_static_constructor;
447 // Points to the first non-static field added to the container.
449 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
450 // and the first one's as good as any.
452 FieldBase first_nonstatic_field = null;
455 // This one is computed after we can distinguish interfaces
456 // from classes from the arraylist `type_bases'
459 TypeExpr[] iface_exprs;
461 ArrayList type_bases;
463 bool members_defined;
464 bool members_defined_ok;
466 // The interfaces we implement.
467 protected Type[] ifaces;
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 PartialContainer;
476 ArrayList partial_parts;
479 /// The pending methods that need to be implemented
480 // (interfaces or abstract methods)
482 PendingImplementation pending;
484 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
485 Attributes attrs, Kind kind)
486 : base (ns, parent, name, attrs)
488 if (parent != null && parent != RootContext.Tree.Types && parent.NamespaceEntry != ns)
489 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
492 this.PartialContainer = this;
495 public bool AddToMemberContainer (MemberCore symbol)
497 return AddToContainer (symbol, symbol.Name);
500 protected virtual bool AddToTypeContainer (DeclSpace ds)
502 return AddToContainer (ds, ds.Basename);
505 public void AddConstant (Const constant)
507 if (!AddToMemberContainer (constant))
510 if (constants == null)
511 constants = new MemberCoreArrayList ();
513 constants.Add (constant);
516 public void AddEnum (Mono.CSharp.Enum e)
518 if (!AddToTypeContainer (e))
522 enums = new MemberCoreArrayList ();
527 public bool AddClassOrStruct (TypeContainer c)
529 if (!AddToTypeContainer (c))
533 types = new ArrayList (2);
535 RootContext.Tree.RecordDecl (c.NamespaceEntry.NS, c.MemberName, c);
540 public virtual TypeContainer AddPartial (TypeContainer nextPart)
542 return AddPartial (nextPart, nextPart.Basename);
545 protected TypeContainer AddPartial (TypeContainer nextPart, string name)
547 nextPart.ModFlags |= Modifiers.PARTIAL;
548 TypeContainer tc = defined_names [name] as TypeContainer;
551 if (nextPart is Interface)
552 AddInterface (nextPart);
554 AddClassOrStruct (nextPart);
558 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
559 Report.SymbolRelatedToPreviousError (tc);
560 Error_MissingPartialModifier (nextPart);
564 if (tc.Kind != nextPart.Kind) {
565 Report.SymbolRelatedToPreviousError (tc);
566 Report.Error (261, nextPart.Location,
567 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
568 nextPart.GetSignatureForError ());
572 if ((tc.ModFlags & Modifiers.Accessibility) != (nextPart.ModFlags & Modifiers.Accessibility) &&
573 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
574 (nextPart.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
575 Report.SymbolRelatedToPreviousError (tc);
576 Report.Error (262, nextPart.Location,
577 "Partial declarations of `{0}' have conflicting accessibility modifiers",
578 nextPart.GetSignatureForError ());
582 if (tc.partial_parts == null)
583 tc.partial_parts = new ArrayList (1);
585 tc.ModFlags |= nextPart.ModFlags;
586 if (nextPart.attributes != null) {
587 if (tc.attributes == null)
588 tc.attributes = nextPart.attributes;
590 tc.attributes.AddAttributes (nextPart.attributes.Attrs);
593 nextPart.PartialContainer = tc;
594 tc.partial_parts.Add (nextPart);
598 public void AddDelegate (Delegate d)
600 if (!AddToTypeContainer (d))
603 if (delegates == null)
604 delegates = new MemberCoreArrayList ();
609 public void AddMethod (Method method)
611 if (!AddToMemberContainer (method))
615 methods = new MethodArrayList (this);
617 if (method.MemberName.Left != null)
618 methods.Insert (0, method);
620 methods.Add (method);
624 // Do not use this method: use AddMethod.
626 // This is only used by iterators.
628 public void AppendMethod (Method method)
630 if (!AddToMemberContainer (method))
634 methods = new MethodArrayList (this);
636 methods.Add (method);
639 public void AddConstructor (Constructor c)
641 if (c.Name != Basename) {
642 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
645 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
648 if (default_static_constructor != null) {
649 Report.SymbolRelatedToPreviousError (default_static_constructor);
650 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
654 default_static_constructor = c;
656 if (c.Parameters.Empty){
657 if (default_constructor != null) {
658 Report.SymbolRelatedToPreviousError (default_constructor);
659 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
662 default_constructor = c;
665 if (instance_constructors == null)
666 instance_constructors = new MemberCoreArrayList ();
668 instance_constructors.Add (c);
672 internal static string Error111 {
674 return "`{0}' is already defined. Rename this member or use different parameter types";
678 public bool AddInterface (TypeContainer iface)
680 if (!AddToTypeContainer (iface))
683 if (interfaces == null) {
684 interfaces = new MemberCoreArrayList ();
687 RootContext.Tree.RecordDecl (iface.NamespaceEntry.NS, iface.MemberName, iface);
688 interfaces.Add (iface);
692 public void AddField (FieldMember field)
694 if (!AddToMemberContainer (field))
698 fields = new MemberCoreArrayList ();
702 if ((field.ModFlags & Modifiers.STATIC) != 0)
705 if (first_nonstatic_field == null) {
706 first_nonstatic_field = field;
710 if (Kind == Kind.Struct &&
711 first_nonstatic_field.Parent != field.Parent &&
712 RootContext.WarningLevel >= 3) {
713 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
714 Report.Warning (282, 3, field.Location,
715 "struct instance field `{0}' found in different declaration from instance field `{1}'",
716 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
720 public void AddProperty (Property prop)
722 if (!AddToMemberContainer (prop) ||
723 !AddToMemberContainer (prop.Get) || !AddToMemberContainer (prop.Set))
726 if (properties == null)
727 properties = new MemberCoreArrayList ();
729 if (prop.MemberName.Left != null)
730 properties.Insert (0, prop);
732 properties.Add (prop);
735 public void AddEvent (Event e)
737 if (!AddToMemberContainer (e))
740 if (e is EventProperty) {
741 if (!AddToMemberContainer (e.Add))
744 if (!AddToMemberContainer (e.Remove))
749 events = new MemberCoreArrayList ();
755 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
757 public void AddIndexer (Indexer i)
759 if (indexers == null)
760 indexers = new IndexerArrayList (this);
762 if (i.IsExplicitImpl)
763 indexers.Insert (0, i);
768 public void AddOperator (Operator op)
770 if (!AddToMemberContainer (op))
773 if (operators == null)
774 operators = new OperatorArrayList (this);
779 public void AddIterator (Iterator i)
781 if (iterators == null)
782 iterators = new ArrayList ();
787 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
789 if (a.Type == TypeManager.default_member_type) {
790 if (Indexers != null) {
791 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
796 base.ApplyAttributeBuilder (a, cb);
799 public override AttributeTargets AttributeTargets {
801 throw new NotSupportedException ();
805 public ArrayList Types {
811 public MethodArrayList Methods {
817 public ArrayList Constants {
823 public ArrayList Interfaces {
829 public ArrayList Iterators {
835 protected Type BaseType {
837 return TypeBuilder.BaseType;
841 public ArrayList Bases {
851 public ArrayList Fields {
857 public ArrayList InstanceConstructors {
859 return instance_constructors;
863 public ArrayList Properties {
869 public ArrayList Events {
875 public ArrayList Enums {
881 public ArrayList Indexers {
887 public ArrayList Operators {
893 public ArrayList Delegates {
899 protected override TypeAttributes TypeAttr {
901 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
905 public string IndexerName {
907 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
911 public bool IsComImport {
913 if (OptAttributes == null)
916 return OptAttributes.Contains (TypeManager.comimport_attr_type);
920 public virtual void RegisterFieldForInitialization (FieldBase field)
922 if ((field.ModFlags & Modifiers.STATIC) != 0){
923 if (initialized_static_fields == null)
924 initialized_static_fields = new ArrayList (4);
926 initialized_static_fields.Add (field);
928 if (initialized_fields == null)
929 initialized_fields = new ArrayList (4);
931 initialized_fields.Add (field);
936 // Emits the instance field initializers
938 public bool EmitFieldInitializers (EmitContext ec)
943 fields = initialized_static_fields;
945 fields = initialized_fields;
951 foreach (FieldBase f in fields) {
952 f.EmitInitializer (ec);
957 public override string DocComment {
969 public PendingImplementation PendingImplementations {
970 get { return pending; }
973 public override bool GetClsCompliantAttributeValue ()
975 if (PartialContainer != this)
976 return PartialContainer.GetClsCompliantAttributeValue ();
978 return base.GetClsCompliantAttributeValue ();
981 TypeExpr[] GetNormalBases (out TypeExpr base_class)
987 int count = Bases.Count;
990 if (Kind == Kind.Class){
991 TypeExpr name = ((Expression) Bases [0]).ResolveAsBaseTerminal (this, false);
997 if (!name.IsInterface) {
998 // base_class could be a class, struct, enum, delegate.
999 // This is validated in GetClassBases.
1005 TypeExpr [] ifaces = new TypeExpr [count-start];
1007 for (i = start, j = 0; i < count; i++, j++){
1008 TypeExpr resolved = ((Expression) Bases [i]).ResolveAsTypeTerminal (this, false);
1009 if (resolved == null) {
1013 ifaces [j] = resolved;
1016 return ifaces.Length == 0 ? null : ifaces;
1020 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
1022 ArrayList ifaces = new ArrayList (0);
1023 if (iface_exprs != null)
1024 ifaces.AddRange (iface_exprs);
1026 foreach (TypeContainer part in partial_parts) {
1027 TypeExpr new_base_class;
1028 TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
1029 if (new_base_class != TypeManager.system_object_expr) {
1030 if (base_class == TypeManager.system_object_expr)
1031 base_class = new_base_class;
1033 if (new_base_class != null && !new_base_class.Equals (base_class)) {
1034 Report.SymbolRelatedToPreviousError (base_class.Location, "");
1035 Report.Error (263, part.Location,
1036 "Partial declarations of `{0}' must not specify different base classes",
1037 part.GetSignatureForError ());
1044 if (new_ifaces == null)
1047 foreach (TypeExpr iface in new_ifaces) {
1048 if (ifaces.Contains (iface))
1055 if (ifaces.Count == 0)
1058 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
1062 /// This function computes the Base class and also the
1063 /// list of interfaces that the class or struct @c implements.
1065 /// The return value is an array (might be null) of
1066 /// interfaces implemented (as Types).
1068 /// The @base_class argument is set to the base object or null
1069 /// if this is `System.Object'.
1071 public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
1073 TypeExpr[] ifaces = GetNormalBases (out base_class);
1078 int count = ifaces.Length;
1080 for (int i = 0; i < count; i++) {
1081 TypeExpr iface = (TypeExpr) ifaces [i];
1083 if (!iface.IsInterface) {
1084 if (Kind != Kind.Class) {
1085 // TODO: location of symbol related ....
1086 Error_TypeInListIsNotInterface (Location, iface.FullName);
1088 else if (base_class != null)
1089 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
1090 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
1092 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
1093 GetSignatureForError (), iface.GetSignatureForError ());
1098 for (int x = 0; x < i; x++) {
1099 if (iface.Equals (ifaces [x])) {
1100 Report.Error (528, Location,
1101 "`{0}' is already listed in " +
1102 "interface list", iface.GetSignatureForError ());
1107 if ((Kind == Kind.Interface) &&
1108 !iface.AsAccessible (Parent, ModFlags)) {
1109 Report.Error (61, Location,
1110 "Inconsistent accessibility: base " +
1111 "interface `{0}' is less accessible " +
1112 "than interface `{1}'", iface.Name,
1122 protected void Error_TypeInListIsNotInterface (Location loc, string type)
1124 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
1127 bool DefineTypeBuilder ()
1131 if (TypeManager.NamespaceClash (Name, Location)) {
1135 ModuleBuilder builder = CodeGen.Module.Builder;
1136 TypeBuilder = builder.DefineType (
1137 Name, TypeAttr, null, null);
1139 TypeBuilder builder = Parent.TypeBuilder;
1141 TypeBuilder = builder.DefineNestedType (
1142 Basename, TypeAttr, null, null);
1144 } catch (ArgumentException) {
1145 Report.RuntimeMissingSupport (Location, "static classes");
1149 TypeManager.AddUserType (this);
1151 iface_exprs = GetClassBases (out base_type);
1152 if (partial_parts != null) {
1153 iface_exprs = GetNormalPartialBases (ref base_type);
1157 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1158 // which in turn should have called DefineType()s on base types if necessary.
1160 // None of the code below should trigger DefineType()s on classes that we depend on.
1161 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1163 // Let's do it as soon as possible, since code below can call DefineType() on classes
1164 // that depend on us to be populated before they are.
1166 if (!(this is Iterator))
1167 RootContext.RegisterOrder (this);
1170 if (base_type != null) {
1171 TypeBuilder.SetParent (base_type.Type);
1173 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1174 if (obsolete_attr != null && !IsInObsoleteScope) {
1175 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1179 if (!CheckRecursiveDefinition (this)) {
1183 // add interfaces that were not added at type creation
1184 if (iface_exprs != null) {
1185 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1186 if (ifaces == null) {
1190 foreach (Type itype in ifaces)
1191 TypeBuilder.AddInterfaceImplementation (itype);
1193 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1200 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1202 public override TypeBuilder DefineType ()
1204 if (TypeBuilder != null)
1210 if (!DefineTypeBuilder ()) {
1215 if (partial_parts != null) {
1216 foreach (TypeContainer part in partial_parts)
1217 part.TypeBuilder = TypeBuilder;
1220 DefineNestedTypes ();
1225 protected virtual bool DefineNestedTypes ()
1227 if (Interfaces != null) {
1228 foreach (TypeContainer iface in Interfaces)
1229 if (iface.DefineType () == null)
1233 if (Types != null) {
1234 foreach (TypeContainer tc in Types)
1235 if (tc.DefineType () == null)
1239 if (Delegates != null) {
1240 foreach (Delegate d in Delegates)
1241 if (d.DefineType () == null)
1245 if (Enums != null) {
1246 foreach (Enum en in Enums)
1247 if (en.DefineType () == null)
1254 TypeContainer InTransit;
1256 protected bool CheckRecursiveDefinition (TypeContainer tc)
1258 if (InTransit != null) {
1259 Report.SymbolRelatedToPreviousError (this);
1260 if (this is Interface)
1262 529, tc.Location, "Inherited interface `{0}' causes a " +
1263 "cycle in the interface hierarchy of `{1}'",
1264 GetSignatureForError (), tc.GetSignatureForError ());
1267 146, tc.Location, "Circular base class dependency " +
1268 "involving `{0}' and `{1}'",
1269 tc.GetSignatureForError (), GetSignatureForError ());
1275 if (BaseType != null) {
1276 TypeContainer ptc = TypeManager.LookupTypeContainer (BaseType);
1277 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1281 if (iface_exprs != null) {
1282 foreach (TypeExpr iface in iface_exprs) {
1283 Type itype = iface.Type;
1285 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1286 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1295 public static void Error_KeywordNotAllowed (Location loc)
1297 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1301 /// Populates our TypeBuilder with fields and methods
1303 public override bool DefineMembers ()
1305 if (members_defined)
1306 return members_defined_ok;
1308 if (!base.DefineMembers ())
1311 members_defined_ok = DoDefineMembers ();
1312 members_defined = true;
1314 return members_defined_ok;
1317 protected virtual bool DoDefineMembers ()
1320 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1321 if (conflict_symbol == null) {
1322 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1323 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1325 if ((ModFlags & Modifiers.NEW) == 0) {
1326 Report.SymbolRelatedToPreviousError (conflict_symbol);
1327 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1328 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1333 DefineContainerMembers (constants);
1334 DefineContainerMembers (fields);
1336 if (Kind == Kind.Struct || Kind == Kind.Class) {
1337 pending = PendingImplementation.GetPendingImplementations (this);
1341 // Constructors are not in the defined_names array
1343 DefineContainerMembers (instance_constructors);
1345 DefineContainerMembers (properties);
1346 DefineContainerMembers (events);
1347 DefineContainerMembers (indexers);
1348 DefineContainerMembers (methods);
1349 DefineContainerMembers (operators);
1350 DefineContainerMembers (enums);
1351 DefineContainerMembers (delegates);
1354 member_cache = new MemberCache (this);
1355 if (partial_parts != null) {
1356 foreach (TypeContainer part in partial_parts)
1357 part.member_cache = member_cache;
1360 if (iterators != null) {
1361 foreach (Iterator iterator in iterators) {
1362 if (iterator.DefineType () == null)
1366 foreach (Iterator iterator in iterators) {
1367 if (!iterator.DefineMembers ())
1375 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1378 mcal.DefineContainerMembers ();
1381 public override bool Define ()
1383 if (iterators != null) {
1384 foreach (Iterator iterator in iterators) {
1385 if (!iterator.Define ())
1393 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1395 return BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1399 /// This function is based by a delegate to the FindMembers routine
1401 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1407 /// This filter is used by FindMembers, and we just keep
1408 /// a global for the filter to `AlwaysAccept'
1410 static MemberFilter accepting_filter;
1413 static TypeContainer ()
1415 accepting_filter = new MemberFilter (AlwaysAccept);
1418 public MethodInfo[] GetMethods ()
1420 ArrayList members = new ArrayList ();
1424 if (methods != null) {
1425 int len = methods.Count;
1426 for (int i = 0; i < len; i++) {
1427 Method m = (Method) methods [i];
1429 members.Add (m.MethodBuilder);
1433 if (operators != null) {
1434 int len = operators.Count;
1435 for (int i = 0; i < len; i++) {
1436 Operator o = (Operator) operators [i];
1438 members.Add (o.MethodBuilder);
1442 if (properties != null) {
1443 int len = properties.Count;
1444 for (int i = 0; i < len; i++) {
1445 Property p = (Property) properties [i];
1447 if (p.GetBuilder != null)
1448 members.Add (p.GetBuilder);
1449 if (p.SetBuilder != null)
1450 members.Add (p.SetBuilder);
1454 if (indexers != null) {
1455 int len = indexers.Count;
1456 for (int i = 0; i < len; i++) {
1457 Indexer ix = (Indexer) indexers [i];
1459 if (ix.GetBuilder != null)
1460 members.Add (ix.GetBuilder);
1461 if (ix.SetBuilder != null)
1462 members.Add (ix.SetBuilder);
1466 if (events != null) {
1467 int len = events.Count;
1468 for (int i = 0; i < len; i++) {
1469 Event e = (Event) events [i];
1471 if (e.AddBuilder != null)
1472 members.Add (e.AddBuilder);
1473 if (e.RemoveBuilder != null)
1474 members.Add (e.RemoveBuilder);
1478 MethodInfo[] retMethods = new MethodInfo [members.Count];
1479 members.CopyTo (retMethods, 0);
1483 // Indicated whether container has StructLayout attribute set Explicit
1484 public bool HasExplicitLayout {
1486 return (caching_flags & Flags.HasExplicitLayout) != 0;
1489 caching_flags |= Flags.HasExplicitLayout;
1493 public override Type FindNestedType (string name)
1495 if (PartialContainer != this)
1496 return PartialContainer.FindNestedType (name);
1498 ArrayList [] lists = { types, enums, delegates, interfaces };
1500 for (int j = 0; j < lists.Length; ++j) {
1501 ArrayList list = lists [j];
1505 int len = list.Count;
1506 for (int i = 0; i < len; ++i) {
1507 DeclSpace ds = (DeclSpace) list [i];
1508 if (ds.Basename == name) {
1509 return ds.DefineType ();
1517 private void FindMembers_NestedTypes (int modflags,
1518 BindingFlags bf, MemberFilter filter, object criteria,
1519 ref ArrayList members)
1521 ArrayList [] lists = { types, enums, delegates, interfaces };
1523 for (int j = 0; j < lists.Length; ++j) {
1524 ArrayList list = lists [j];
1528 int len = list.Count;
1529 for (int i = 0; i < len; i++) {
1530 DeclSpace ds = (DeclSpace) list [i];
1532 if ((ds.ModFlags & modflags) == 0)
1535 TypeBuilder tb = ds.TypeBuilder;
1537 if (!(criteria is string) || ds.Basename.Equals (criteria))
1538 tb = ds.DefineType ();
1541 if (tb != null && (filter (tb, criteria) == true)) {
1542 if (members == null)
1543 members = new ArrayList ();
1552 /// This method returns the members of this type just like Type.FindMembers would
1553 /// Only, we need to use this for types which are _being_ defined because MS'
1554 /// implementation can't take care of that.
1557 // FIXME: return an empty static array instead of null, that cleans up
1558 // some code and is consistent with some coding conventions I just found
1562 // Notice that in various cases we check if our field is non-null,
1563 // something that would normally mean that there was a bug elsewhere.
1565 // The problem happens while we are defining p-invoke methods, as those
1566 // will trigger a FindMembers, but this happens before things are defined
1568 // Since the whole process is a no-op, it is fine to check for null here.
1570 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1571 MemberFilter filter, object criteria)
1573 ArrayList members = null;
1576 if ((bf & BindingFlags.Public) != 0)
1577 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1579 if ((bf & BindingFlags.NonPublic) != 0)
1580 modflags |= Modifiers.PRIVATE;
1582 int static_mask = 0, static_flags = 0;
1583 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1584 case BindingFlags.Static:
1585 static_mask = static_flags = Modifiers.STATIC;
1588 case BindingFlags.Instance:
1589 static_mask = Modifiers.STATIC;
1594 static_mask = static_flags = 0;
1598 Timer.StartTimer (TimerType.TcFindMembers);
1601 filter = accepting_filter;
1603 if ((mt & MemberTypes.Field) != 0) {
1604 if (fields != null) {
1605 int len = fields.Count;
1606 for (int i = 0; i < len; i++) {
1607 FieldMember f = (FieldMember) fields [i];
1609 if ((f.ModFlags & modflags) == 0)
1611 if ((f.ModFlags & static_mask) != static_flags)
1614 FieldBuilder fb = f.FieldBuilder;
1615 if (fb != null && filter (fb, criteria) == true) {
1616 if (members == null)
1617 members = new ArrayList ();
1624 if (constants != null) {
1625 int len = constants.Count;
1626 for (int i = 0; i < len; i++) {
1627 Const con = (Const) constants [i];
1629 if ((con.ModFlags & modflags) == 0)
1631 if ((con.ModFlags & static_mask) != static_flags)
1634 FieldBuilder fb = con.FieldBuilder;
1637 fb = con.FieldBuilder;
1639 if (fb != null && filter (fb, criteria) == true) {
1640 if (members == null)
1641 members = new ArrayList ();
1649 if ((mt & MemberTypes.Method) != 0) {
1650 if (methods != null) {
1651 int len = methods.Count;
1652 for (int i = 0; i < len; i++) {
1653 Method m = (Method) methods [i];
1655 if ((m.ModFlags & modflags) == 0)
1657 if ((m.ModFlags & static_mask) != static_flags)
1660 MethodBuilder mb = m.MethodBuilder;
1662 if (mb != null && filter (mb, criteria) == true) {
1663 if (members == null)
1664 members = new ArrayList ();
1671 if (operators != null) {
1672 int len = operators.Count;
1673 for (int i = 0; i < len; i++) {
1674 Operator o = (Operator) operators [i];
1676 if ((o.ModFlags & modflags) == 0)
1678 if ((o.ModFlags & static_mask) != static_flags)
1681 MethodBuilder ob = o.MethodBuilder;
1682 if (ob != null && filter (ob, criteria) == true) {
1683 if (members == null)
1684 members = new ArrayList ();
1691 if (properties != null) {
1692 int len = properties.Count;
1693 for (int i = 0; i < len; i++) {
1694 Property p = (Property) properties [i];
1696 if ((p.ModFlags & modflags) == 0)
1698 if ((p.ModFlags & static_mask) != static_flags)
1704 if (b != null && filter (b, criteria) == true) {
1705 if (members == null)
1706 members = new ArrayList ();
1712 if (b != null && filter (b, criteria) == true) {
1713 if (members == null)
1714 members = new ArrayList ();
1721 if (indexers != null) {
1722 int len = indexers.Count;
1723 for (int i = 0; i < len; i++) {
1724 Indexer ix = (Indexer) indexers [i];
1726 if ((ix.ModFlags & modflags) == 0)
1728 if ((ix.ModFlags & static_mask) != static_flags)
1734 if (b != null && filter (b, criteria) == true) {
1735 if (members == null)
1736 members = new ArrayList ();
1742 if (b != null && filter (b, criteria) == true) {
1743 if (members == null)
1744 members = new ArrayList ();
1752 if ((mt & MemberTypes.Event) != 0) {
1753 if (events != null) {
1754 int len = events.Count;
1755 for (int i = 0; i < len; i++) {
1756 Event e = (Event) events [i];
1758 if ((e.ModFlags & modflags) == 0)
1760 if ((e.ModFlags & static_mask) != static_flags)
1763 MemberInfo eb = e.EventBuilder;
1764 if (eb != null && filter (eb, criteria) == true) {
1765 if (members == null)
1766 members = new ArrayList ();
1768 members.Add (e.EventBuilder);
1774 if ((mt & MemberTypes.Property) != 0){
1775 if (properties != null) {
1776 int len = properties.Count;
1777 for (int i = 0; i < len; i++) {
1778 Property p = (Property) properties [i];
1780 if ((p.ModFlags & modflags) == 0)
1782 if ((p.ModFlags & static_mask) != static_flags)
1785 MemberInfo pb = p.PropertyBuilder;
1786 if (pb != null && filter (pb, criteria) == true) {
1787 if (members == null)
1788 members = new ArrayList ();
1790 members.Add (p.PropertyBuilder);
1795 if (indexers != null) {
1796 int len = indexers.Count;
1797 for (int i = 0; i < len; i++) {
1798 Indexer ix = (Indexer) indexers [i];
1800 if ((ix.ModFlags & modflags) == 0)
1802 if ((ix.ModFlags & static_mask) != static_flags)
1805 MemberInfo ib = ix.PropertyBuilder;
1806 if (ib != null && filter (ib, criteria) == true) {
1807 if (members == null)
1808 members = new ArrayList ();
1810 members.Add (ix.PropertyBuilder);
1816 if ((mt & MemberTypes.NestedType) != 0)
1817 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1819 if ((mt & MemberTypes.Constructor) != 0){
1820 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1821 int len = instance_constructors.Count;
1822 for (int i = 0; i < len; i++) {
1823 Constructor c = (Constructor) instance_constructors [i];
1825 ConstructorBuilder cb = c.ConstructorBuilder;
1826 if (cb != null && filter (cb, criteria) == true) {
1827 if (members == null)
1828 members = new ArrayList ();
1835 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1836 ConstructorBuilder cb =
1837 default_static_constructor.ConstructorBuilder;
1839 if (cb != null && filter (cb, criteria) == true) {
1840 if (members == null)
1841 members = new ArrayList ();
1849 // Lookup members in base if requested.
1851 if ((bf & BindingFlags.DeclaredOnly) == 0) {
1852 if (TypeBuilder.BaseType != null) {
1853 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1854 if (list.Count > 0) {
1855 if (members == null)
1856 members = new ArrayList ();
1858 members.AddRange (list);
1861 if (ifaces != null) {
1862 foreach (Type base_type in ifaces) {
1863 MemberList list = TypeContainer.FindMembers (base_type, mt, bf, filter, criteria);
1865 if (list.Count > 0) {
1866 if (members == null)
1867 members = new ArrayList ();
1868 members.AddRange (list);
1874 Timer.StopTimer (TimerType.TcFindMembers);
1876 if (members == null)
1877 return MemberList.Empty;
1879 return new MemberList (members);
1882 public override MemberCache MemberCache {
1884 return member_cache;
1888 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1889 MemberFilter filter, object criteria)
1891 DeclSpace ds = TypeManager.LookupDeclSpace (t);
1894 return ds.FindMembers (mt, bf, filter, criteria);
1896 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1900 /// Emits the values for the constants
1902 public void EmitConstants ()
1904 if (constants != null)
1905 foreach (Const con in constants)
1910 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
1915 foreach (MemberCore mc in al) {
1916 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
1920 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
1925 public virtual void VerifyMembers ()
1928 // Check for internal or private fields that were never assigned
1930 if (RootContext.WarningLevel >= 3) {
1931 CheckMemberUsage (properties, "property");
1932 CheckMemberUsage (methods, "method");
1933 CheckMemberUsage (constants, "constant");
1935 if (fields != null){
1936 foreach (FieldMember f in fields) {
1937 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
1941 if ((f.caching_flags & Flags.IsAssigned) == 0)
1942 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
1945 const int error_code = 414;
1947 const int error_code = 169;
1949 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
1950 f.GetSignatureForError ());
1956 // Only report 649 on level 4
1958 if (RootContext.WarningLevel < 4)
1961 if ((f.caching_flags & Flags.IsAssigned) != 0)
1964 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
1965 f.GetSignatureForError (), f.Type.Type.IsValueType ? Activator.CreateInstance (f.Type.Type).ToString() : "null");
1971 // TODO: move to ClassOrStruct
1972 void EmitConstructors ()
1974 if (instance_constructors == null)
1977 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
1978 bool has_compliant_args = false;
1980 foreach (Constructor c in instance_constructors) {
1983 if (has_compliant_args)
1986 has_compliant_args = c.HasCompliantArgs;
1988 if (!has_compliant_args)
1989 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
1991 foreach (Constructor c in instance_constructors)
1997 /// Emits the code, this step is performed after all
1998 /// the types, enumerations, constructors
2000 public virtual void EmitType ()
2002 if (OptAttributes != null)
2003 OptAttributes.Emit ();
2006 // Structs with no fields need to have at least one byte.
2007 // The right thing would be to set the PackingSize in a DefineType
2008 // but there are no functions that allow interfaces *and* the size to
2012 if (Kind == Kind.Struct && first_nonstatic_field == null){
2013 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2014 FieldAttributes.Private);
2016 if (HasExplicitLayout){
2017 object [] ctor_args = new object [1];
2020 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2021 TypeManager.field_offset_attribute_ctor, ctor_args);
2022 fb.SetCustomAttribute (cba);
2028 EmitConstructors ();
2030 // Can not continue if constants are broken
2032 if (Report.Errors > 0)
2035 if (default_static_constructor != null)
2036 default_static_constructor.Emit ();
2038 if (methods != null){
2039 foreach (Method m in methods)
2043 if (operators != null)
2044 foreach (Operator o in operators)
2047 if (properties != null)
2048 foreach (Property p in properties)
2051 if (indexers != null){
2056 foreach (FieldMember f in fields)
2059 if (events != null){
2060 foreach (Event e in Events)
2064 if (delegates != null) {
2065 foreach (Delegate d in Delegates) {
2070 if (enums != null) {
2071 foreach (Enum e in enums) {
2076 if (pending != null)
2077 if (pending.VerifyPendingMethods ())
2080 if (iterators != null)
2081 foreach (Iterator iterator in iterators)
2082 iterator.EmitType ();
2085 public override void CloseType ()
2087 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2091 caching_flags |= Flags.CloseTypeCreated;
2092 TypeBuilder.CreateType ();
2093 } catch (TypeLoadException){
2095 // This is fine, the code still created the type
2097 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2098 // Console.WriteLine (e.Message);
2100 Console.WriteLine ("In type: " + Name);
2105 foreach (Enum en in Enums)
2109 foreach (TypeContainer tc in Types)
2110 if (tc.Kind == Kind.Struct)
2113 foreach (TypeContainer tc in Types)
2114 if (tc.Kind != Kind.Struct)
2118 if (Delegates != null)
2119 foreach (Delegate d in Delegates)
2122 if (Iterators != null)
2123 foreach (Iterator i in Iterators)
2131 initialized_fields = null;
2132 initialized_static_fields = null;
2140 default_constructor = null;
2141 default_static_constructor = null;
2143 OptAttributes = null;
2146 member_cache = null;
2150 // Performs the validation on a Method's modifiers (properties have
2151 // the same properties).
2153 public bool MethodModifiersValid (MemberCore mc)
2155 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2156 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2157 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2159 int flags = mc.ModFlags;
2162 // At most one of static, virtual or override
2164 if ((flags & Modifiers.STATIC) != 0){
2165 if ((flags & vao) != 0){
2166 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2167 mc.GetSignatureForError ());
2172 if (Kind == Kind.Struct){
2173 if ((flags & va) != 0){
2174 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2179 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2180 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2181 mc.GetSignatureForError ());
2186 // If the declaration includes the abstract modifier, then the
2187 // declaration does not include static, virtual or extern
2189 if ((flags & Modifiers.ABSTRACT) != 0){
2190 if ((flags & Modifiers.EXTERN) != 0){
2192 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2196 if ((flags & Modifiers.SEALED) != 0) {
2197 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2201 if ((flags & Modifiers.VIRTUAL) != 0){
2202 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2206 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2207 Report.Error (513, mc.Location, "`{0}' is abstract but it is contained in nonabstract class", mc.GetSignatureForError ());
2212 if ((flags & Modifiers.PRIVATE) != 0){
2213 if ((flags & vao) != 0){
2214 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2219 if ((flags & Modifiers.SEALED) != 0){
2220 if ((flags & Modifiers.OVERRIDE) == 0){
2221 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2229 public Constructor DefaultStaticConstructor {
2230 get { return default_static_constructor; }
2233 protected override bool VerifyClsCompliance ()
2235 if (!base.VerifyClsCompliance ())
2240 Type base_type = TypeBuilder.BaseType;
2241 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2242 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2245 if (!Parent.IsClsComplianceRequired ()) {
2246 Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
2247 GetSignatureForError (), Parent.GetSignatureForError ());
2254 /// Checks whether container name is CLS Compliant
2256 void VerifyClsName ()
2258 Hashtable base_members = base_cache == null ?
2260 base_cache.GetPublicMembers ();
2261 Hashtable this_members = new Hashtable ();
2263 foreach (DictionaryEntry entry in defined_names) {
2264 MemberCore mc = (MemberCore)entry.Value;
2265 if (!mc.IsClsComplianceRequired ())
2268 string name = (string) entry.Key;
2269 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2271 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2272 object found = base_members [lcase];
2273 if (found == null) {
2274 found = this_members [lcase];
2275 if (found == null) {
2276 this_members.Add (lcase, mc);
2281 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2284 if (found is MemberInfo) {
2285 if (basename == ((MemberInfo) found).Name)
2287 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2289 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2291 Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2297 /// Performs checks for an explicit interface implementation. First it
2298 /// checks whether the `interface_type' is a base inteface implementation.
2299 /// Then it checks whether `name' exists in the interface type.
2301 public virtual bool VerifyImplements (MemberBase mb)
2303 if (ifaces != null) {
2304 foreach (Type t in ifaces){
2305 if (t == mb.InterfaceType)
2310 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2311 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2312 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2316 public void Mark_HasEquals ()
2318 Methods.HasEquals = true;
2321 public void Mark_HasGetHashCode ()
2323 Methods.HasGetHashCode = true;
2330 string IMemberContainer.Name {
2336 Type IMemberContainer.Type {
2342 MemberCache IMemberContainer.MemberCache {
2344 return member_cache;
2348 bool IMemberContainer.IsInterface {
2350 return Kind == Kind.Interface;
2354 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2356 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
2360 // Generates xml doc comments (if any), and if required,
2361 // handle warning report.
2363 internal override void GenerateDocComment (DeclSpace ds)
2365 DocUtil.GenerateTypeDocComment (this, ds);
2368 public override string DocCommentHeader {
2369 get { return "T:"; }
2372 public virtual MemberCache BaseCache {
2374 if (base_cache != null)
2376 if (TypeBuilder.BaseType != null)
2377 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2378 if (TypeBuilder.IsInterface)
2379 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2385 public abstract class ClassOrStruct : TypeContainer {
2386 ListDictionary declarative_security;
2388 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2389 MemberName name, Attributes attrs, Kind kind)
2390 : base (ns, parent, name, attrs, kind)
2394 protected override bool AddToContainer (MemberCore symbol, string name)
2396 if (name == Basename) {
2397 Report.SymbolRelatedToPreviousError (this);
2398 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2399 symbol.GetSignatureForError ());
2403 return base.AddToContainer (symbol, name);
2406 public override void VerifyMembers ()
2408 base.VerifyMembers ();
2410 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2411 foreach (Event e in events){
2412 if ((e.caching_flags & Flags.IsAssigned) == 0)
2413 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2418 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2420 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2421 if (declarative_security == null)
2422 declarative_security = new ListDictionary ();
2424 a.ExtractSecurityPermissionSet (declarative_security);
2428 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2429 HasExplicitLayout = true;
2432 base.ApplyAttributeBuilder (a, cb);
2436 /// Defines the default constructors
2438 protected void DefineDefaultConstructor (bool is_static)
2440 // The default instance constructor is public
2441 // If the class is abstract, the default constructor is protected
2442 // The default static constructor is private
2446 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2448 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2451 Constructor c = new Constructor (this, MemberName.Name, mods,
2452 Parameters.EmptyReadOnlyParameters,
2453 new GeneratedBaseInitializer (Location),
2457 c.Block = new ToplevelBlock (null, Location);
2460 void DefineFieldInitializers ()
2462 if (initialized_fields != null) {
2463 for (int i = 0; i < initialized_fields.Count; ++i) {
2464 FieldBase fb = (FieldBase)initialized_fields[i];
2465 fb.ResolveInitializer ();
2466 if (fb.HasDefaultInitializer && RootContext.Optimize) {
2467 // Field is re-initialized to its default value => removed
2468 initialized_fields.RemoveAt (i);
2474 if (initialized_static_fields != null) {
2475 bool has_complex_initializer = false;
2477 foreach (FieldBase fb in initialized_static_fields) {
2478 if (fb.ResolveInitializer () is Constant)
2481 has_complex_initializer = true;
2484 // Need special check to not optimize code like this
2485 // static int a = b = 5;
2486 // static int b = 0;
2487 if (!has_complex_initializer && RootContext.Optimize) {
2488 for (int i = 0; i < initialized_static_fields.Count; ++i) {
2489 FieldBase fb = (FieldBase)initialized_static_fields[i];
2490 if (fb.HasDefaultInitializer) {
2491 initialized_static_fields.RemoveAt (i);
2497 if (default_static_constructor == null && initialized_static_fields.Count > 0) {
2498 DefineDefaultConstructor (true);
2504 public override bool Define ()
2506 DefineFieldInitializers ();
2508 if (default_static_constructor != null)
2509 default_static_constructor.Define ();
2511 return base.Define ();
2514 public override void Emit ()
2518 if (declarative_security != null) {
2519 foreach (DictionaryEntry de in declarative_security) {
2520 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2525 protected override TypeAttributes TypeAttr {
2527 if (default_static_constructor == null)
2528 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2530 return base.TypeAttr;
2536 // TODO: should be sealed
2537 public class Class : ClassOrStruct {
2538 const int AllowedModifiers =
2541 Modifiers.PROTECTED |
2542 Modifiers.INTERNAL |
2544 Modifiers.ABSTRACT |
2549 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2551 : base (ns, parent, name, attrs, Kind.Class)
2553 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2554 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2556 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2557 Report.FeatureIsNotStandardized (Location, "static classes");
2561 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2563 if (a.Type == TypeManager.attribute_usage_type) {
2564 if (BaseType != TypeManager.attribute_type && !BaseType.IsSubclassOf (TypeManager.attribute_type) &&
2565 TypeBuilder.FullName != "System.Attribute") {
2566 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2570 if (a.Type == TypeManager.conditional_attribute_type &&
2571 !(BaseType == TypeManager.attribute_type || BaseType.IsSubclassOf (TypeManager.attribute_type))) {
2572 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2576 if (a.Type == TypeManager.comimport_attr_type &&
2577 !attributes.Contains (TypeManager.guid_attr_type)) {
2578 a.Error_MissingGuidAttribute ();
2582 if (AttributeTester.IsAttributeExcluded (a.Type))
2585 base.ApplyAttributeBuilder (a, cb);
2588 public override AttributeTargets AttributeTargets {
2590 return AttributeTargets.Class;
2594 protected override void DefineContainerMembers (MemberCoreArrayList list)
2600 base.DefineContainerMembers (list);
2604 foreach (MemberCore m in list) {
2605 if (m is Operator) {
2606 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2610 if (m is Destructor) {
2611 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2615 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2616 Report.Error (1057, m.Location, "`{0}': Static classes cannot contain protected members", m.GetSignatureForError ());
2621 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2625 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2628 if (m is Constructor) {
2629 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2633 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2636 base.DefineContainerMembers (list);
2639 public override TypeBuilder DefineType ()
2641 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2642 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2646 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2647 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2651 return base.DefineType ();
2654 protected override bool DoDefineMembers ()
2656 if (InstanceConstructors == null && !IsStatic)
2657 DefineDefaultConstructor (false);
2659 return base.DoDefineMembers ();
2662 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2664 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2666 if (base_class == null) {
2667 if (RootContext.StdLib)
2668 base_class = TypeManager.system_object_expr;
2669 else if (Name != "System.Object")
2670 base_class = TypeManager.system_object_expr;
2672 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
2673 Report.Error (1521, base_class.Location, "Invalid base type");
2677 if (base_class.IsSealed){
2678 Report.SymbolRelatedToPreviousError (base_class.Type);
2679 if (base_class.Type.IsAbstract) {
2680 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2681 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2683 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2684 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2689 if (!base_class.CanInheritFrom ()){
2690 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2691 GetSignatureForError (), base_class.GetSignatureForError ());
2695 if (!base_class.AsAccessible (this, ModFlags)) {
2696 Report.SymbolRelatedToPreviousError (base_class.Type);
2697 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2698 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2703 if (base_class != TypeManager.system_object_expr) {
2704 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2705 GetSignatureForError (), base_class.GetSignatureForError ());
2709 if (ifaces != null) {
2710 foreach (TypeExpr t in ifaces)
2711 Report.SymbolRelatedToPreviousError (t.Type);
2712 Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
2719 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2720 /// Valid only for attribute classes.
2721 public bool IsExcluded ()
2723 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2724 return (caching_flags & Flags.Excluded) != 0;
2726 caching_flags &= ~Flags.Excluded_Undetected;
2728 if (OptAttributes == null)
2731 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
2736 foreach (Attribute a in attrs) {
2737 string condition = a.GetConditionalAttributeValue ();
2738 if (RootContext.AllDefines.Contains (condition))
2742 caching_flags |= Flags.Excluded;
2748 return (ModFlags & Modifiers.STATIC) != 0;
2753 // FIXME: How do we deal with the user specifying a different
2756 protected override TypeAttributes TypeAttr {
2758 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2760 ta |= TypeAttributes.Abstract | TypeAttributes.Sealed;
2766 public sealed class Struct : ClassOrStruct {
2768 // Modifiers allowed in a struct declaration
2770 const int AllowedModifiers =
2773 Modifiers.PROTECTED |
2774 Modifiers.INTERNAL |
2778 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2779 int mod, Attributes attrs)
2780 : base (ns, parent, name, attrs, Kind.Struct)
2784 if (parent.Parent == null)
2785 accmods = Modifiers.INTERNAL;
2787 accmods = Modifiers.PRIVATE;
2789 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2791 this.ModFlags |= Modifiers.SEALED;
2794 public override AttributeTargets AttributeTargets {
2796 return AttributeTargets.Struct;
2800 const TypeAttributes DefaultTypeAttributes =
2801 TypeAttributes.SequentialLayout |
2802 TypeAttributes.Sealed |
2803 TypeAttributes.BeforeFieldInit;
2806 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2808 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2810 // If we are compiling our runtime,
2811 // and we are defining ValueType, then our
2812 // base is `System.Object'.
2814 if (base_class == null) {
2815 if (!RootContext.StdLib && Name == "System.ValueType")
2816 base_class = TypeManager.system_object_expr;
2818 base_class = TypeManager.system_valuetype_expr;
2825 // FIXME: Allow the user to specify a different set of attributes
2826 // in some cases (Sealed for example is mandatory for a class,
2827 // but what SequentialLayout can be changed
2829 protected override TypeAttributes TypeAttr {
2831 return base.TypeAttr | DefaultTypeAttributes;
2835 public override void RegisterFieldForInitialization (FieldBase field)
2837 if ((field.ModFlags & Modifiers.STATIC) == 0) {
2838 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
2839 field.GetSignatureForError ());
2842 base.RegisterFieldForInitialization (field);
2850 public sealed class Interface : TypeContainer, IMemberContainer {
2853 /// Modifiers allowed in a class declaration
2855 public const int AllowedModifiers =
2858 Modifiers.PROTECTED |
2859 Modifiers.INTERNAL |
2863 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2865 : base (ns, parent, name, attrs, Kind.Interface)
2869 if (parent.Parent == null)
2870 accmods = Modifiers.INTERNAL;
2872 accmods = Modifiers.PRIVATE;
2874 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
2877 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2879 if (a.Type == TypeManager.comimport_attr_type &&
2880 !attributes.Contains (TypeManager.guid_attr_type)) {
2881 a.Error_MissingGuidAttribute ();
2884 base.ApplyAttributeBuilder (a, cb);
2888 public override AttributeTargets AttributeTargets {
2890 return AttributeTargets.Interface;
2894 const TypeAttributes DefaultTypeAttributes =
2895 TypeAttributes.AutoLayout |
2896 TypeAttributes.Abstract |
2897 TypeAttributes.Interface;
2899 protected override TypeAttributes TypeAttr {
2901 return base.TypeAttr | DefaultTypeAttributes;
2905 protected override bool VerifyClsCompliance ()
2907 if (!base.VerifyClsCompliance ())
2910 if (ifaces != null) {
2911 foreach (Type t in ifaces) {
2912 if (AttributeTester.IsClsCompliant (t))
2915 Report.SymbolRelatedToPreviousError (t);
2916 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
2917 GetSignatureForError (), TypeManager.CSharpName (t));
2925 public abstract class MethodCore : MemberBase {
2926 public readonly Parameters Parameters;
2927 protected ToplevelBlock block;
2930 // The method we're overriding if this is an override method.
2932 protected MethodInfo base_method = null;
2934 public MethodCore (DeclSpace parent, Expression type, int mod,
2935 int allowed_mod, bool is_interface, MemberName name,
2936 Attributes attrs, Parameters parameters)
2937 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
2940 Parameters = parameters;
2941 IsInterface = is_interface;
2945 // Returns the System.Type array for the parameters of this method
2947 public Type [] ParameterTypes {
2949 return Parameters.Types;
2953 public Parameters ParameterInfo
2960 public ToplevelBlock Block {
2970 public void SetYields ()
2972 ModFlags |= Modifiers.METHOD_YIELDS;
2975 protected override bool CheckBase ()
2977 if (!base.CheckBase ())
2980 // Check whether arguments were correct.
2981 if (!DoDefineParameters ())
2984 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
2990 // Is null for System.Object while compiling corlib and base interfaces
2991 if (ParentContainer.BaseCache == null) {
2992 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
2993 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
2998 Type base_ret_type = null;
2999 base_method = FindOutBaseMethod (ref base_ret_type);
3001 // method is override
3002 if (base_method != null) {
3004 if (!CheckMethodAgainstBase ())
3007 if ((ModFlags & Modifiers.NEW) == 0) {
3008 if (MemberType != TypeManager.TypeToCoreType (base_ret_type)) {
3009 Report.SymbolRelatedToPreviousError (base_method);
3010 if (this is PropertyBase) {
3011 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3012 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3015 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3016 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3021 if (base_method.IsAbstract && !IsInterface) {
3022 Report.SymbolRelatedToPreviousError (base_method);
3023 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3024 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3029 if (base_method.IsSpecialName && !(this is PropertyBase)) {
3030 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3034 if (Name == "Equals" && Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type)
3035 ParentContainer.Mark_HasEquals ();
3036 else if (Name == "GetHashCode" && Parameters.Empty)
3037 ParentContainer.Mark_HasGetHashCode ();
3039 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3040 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3042 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3043 Report.SymbolRelatedToPreviousError (base_method);
3044 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3045 GetSignatureForError (), TypeManager.CSharpSignature (base_method) );
3052 MemberInfo conflict_symbol = ParentContainer.FindBaseMemberWithSameName (Name, !(this is Property));
3053 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3054 if (conflict_symbol != null) {
3055 Report.SymbolRelatedToPreviousError (conflict_symbol);
3056 if (this is PropertyBase)
3057 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3059 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3061 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3065 if (conflict_symbol == null) {
3066 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3067 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3072 if ((ModFlags & Modifiers.NEW) == 0) {
3073 if (this is Method && conflict_symbol is MethodBase)
3076 Report.SymbolRelatedToPreviousError (conflict_symbol);
3077 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3078 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3085 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3086 // that have been defined.
3088 // `name' is the user visible name for reporting errors (this is used to
3089 // provide the right name regarding method names and properties)
3091 bool CheckMethodAgainstBase ()
3095 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3096 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3097 Report.Error (506, Location,
3098 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3099 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3103 // Now we check that the overriden method is not final
3105 if (base_method.IsFinal) {
3106 Report.SymbolRelatedToPreviousError (base_method);
3107 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3108 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3112 // Check that the permissions are not being changed
3114 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3115 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3117 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3118 Error_CannotChangeAccessModifiers (base_method, base_classp, null);
3123 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3124 ModFlags |= Modifiers.NEW;
3125 Report.SymbolRelatedToPreviousError (base_method);
3126 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3127 Report.Warning (114, 2, Location, "`{0}' hides inherited member `{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword",
3128 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3130 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3131 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3138 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3140 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3142 // when overriding protected internal, the method can be declared
3143 // protected internal only within the same assembly
3146 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3147 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3149 // assemblies differ - report an error
3153 } else if (thisp != base_classp) {
3155 // same assembly, but other attributes differ - report an error
3160 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3162 // if it's not "protected internal", it must be "protected"
3166 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3168 // protected within the same assembly - an error
3171 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3172 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3174 // protected ok, but other attributes differ - report an error
3180 return (thisp == base_classp);
3184 public bool CheckAbstractAndExtern (bool has_block)
3186 if (ParentContainer.Kind == Kind.Interface)
3190 if ((ModFlags & Modifiers.EXTERN) != 0) {
3191 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
3192 GetSignatureForError ());
3196 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
3197 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
3198 GetSignatureForError ());
3202 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
3203 Report.Error (501, Location, "`{0}' must declare a body because it is not marked abstract or extern",
3204 GetSignatureForError ());
3212 protected void Error_CannotChangeAccessModifiers (MemberInfo base_method, MethodAttributes ma, string suffix)
3214 Report.SymbolRelatedToPreviousError (base_method);
3215 string base_name = TypeManager.GetFullNameSignature (base_method);
3216 string this_name = GetSignatureForError ();
3217 if (suffix != null) {
3218 base_name += suffix;
3219 this_name += suffix;
3222 Report.Error (507, Location, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3223 this_name, Modifiers.GetDescription (ma), base_name);
3226 protected static string Error722 {
3228 return "`{0}': static types cannot be used as return types";
3233 /// For custom member duplication search in a container
3235 protected abstract bool CheckForDuplications ();
3238 /// Gets base method and its return type
3240 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3242 protected bool DoDefineParameters ()
3244 // Check if arguments were correct
3245 if (!Parameters.Resolve (this))
3248 return CheckParameters (ParameterTypes);
3251 bool CheckParameters (Type [] parameters)
3254 DeclSpace ds = Parent;
3256 foreach (Type partype in parameters){
3257 if (partype == TypeManager.void_type) {
3259 1547, Location, "Keyword 'void' cannot " +
3260 "be used in this context");
3264 if (partype.IsPointer){
3265 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
3269 if (ds.AsAccessible (partype, ModFlags))
3272 Report.SymbolRelatedToPreviousError (partype);
3273 if (this is Indexer)
3274 Report.Error (55, Location,
3275 "Inconsistent accessibility: parameter type `" +
3276 TypeManager.CSharpName (partype) + "' is less " +
3277 "accessible than indexer `" + GetSignatureForError () + "'");
3278 else if (this is Operator)
3279 Report.Error (57, Location,
3280 "Inconsistent accessibility: parameter type `" +
3281 TypeManager.CSharpName (partype) + "' is less " +
3282 "accessible than operator `" + GetSignatureForError () + "'");
3284 Report.Error (51, Location,
3285 "Inconsistent accessibility: parameter type `" +
3286 TypeManager.CSharpName (partype) + "' is less " +
3287 "accessible than method `" + GetSignatureForError () + "'");
3294 protected override bool VerifyClsCompliance ()
3296 if (!base.VerifyClsCompliance ()) {
3297 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3298 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3303 if (Parameters.HasArglist) {
3304 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3307 if (!AttributeTester.IsClsCompliant (MemberType)) {
3308 if (this is PropertyBase)
3309 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3310 GetSignatureForError ());
3312 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3313 GetSignatureForError ());
3316 Parameters.VerifyClsCompliance ();
3321 protected bool IsDuplicateImplementation (MethodCore method)
3323 if (method == this || !(method.MemberName.Equals (MemberName)))
3326 Type[] param_types = method.ParameterTypes;
3327 // This never happen. Rewrite this as Equal
3328 if (param_types == null && ParameterTypes == null)
3330 if (param_types == null || ParameterTypes == null)
3333 if (param_types.Length != ParameterTypes.Length)
3336 if (method.Parameters.HasArglist != Parameters.HasArglist)
3339 for (int i = 0; i < param_types.Length; i++)
3340 if (param_types [i] != ParameterTypes [i])
3343 // TODO: make operator compatible with MethodCore to avoid this
3344 if (this is Operator && method is Operator) {
3345 if (MemberType != method.MemberType)
3350 // Try to report 663: method only differs on out/ref
3352 Parameters info = ParameterInfo;
3353 Parameters other_info = method.ParameterInfo;
3354 for (int i = 0; i < info.Count; i++){
3355 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3356 Report.SymbolRelatedToPreviousError (method);
3357 Report.Error (663, Location, "`{0}': Methods cannot differ only on their use of ref and out on a parameters",
3358 GetSignatureForError ());
3363 Report.SymbolRelatedToPreviousError (method);
3364 if (this is Operator && method is Operator)
3365 Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
3367 Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
3371 public override bool IsUsed {
3372 get { return IsExplicitImpl || base.IsUsed; }
3376 // Returns a string that represents the signature for this
3377 // member which should be used in XML documentation.
3379 public override string GetDocCommentName (DeclSpace ds)
3381 return DocUtil.GetMethodDocCommentName (this, ds);
3385 // Raised (and passed an XmlElement that contains the comment)
3386 // when GenerateDocComment is writing documentation expectedly.
3388 // FIXME: with a few effort, it could be done with XmlReader,
3389 // that means removal of DOM use.
3391 internal override void OnGenerateDocComment (XmlElement el)
3393 DocUtil.OnMethodGenerateDocComment (this, el);
3397 // Represents header string for documentation comment.
3399 public override string DocCommentHeader {
3400 get { return "M:"; }
3405 public abstract class MethodOrOperator : MethodCore, IMethodData
3407 public MethodBuilder MethodBuilder;
3408 ReturnParameter return_attributes;
3409 ListDictionary declarative_security;
3410 protected MethodData MethodData;
3412 static string[] attribute_targets = new string [] { "method", "return" };
3414 protected MethodOrOperator (DeclSpace parent, Expression type, int mod,
3415 int allowed_mod, bool is_interface, MemberName name,
3416 Attributes attrs, Parameters parameters)
3417 : base (parent, type, mod, allowed_mod, is_interface, name,
3422 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3424 if (a.Target == AttributeTargets.ReturnValue) {
3425 if (return_attributes == null)
3426 return_attributes = new ReturnParameter (MethodBuilder, Location);
3428 return_attributes.ApplyAttributeBuilder (a, cb);
3432 if (a.Type == TypeManager.methodimpl_attr_type &&
3433 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
3434 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3437 if (a.Type == TypeManager.dllimport_type) {
3438 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3439 if ((ModFlags & extern_static) != extern_static) {
3440 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3446 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3447 if (declarative_security == null)
3448 declarative_security = new ListDictionary ();
3449 a.ExtractSecurityPermissionSet (declarative_security);
3453 MethodBuilder.SetCustomAttribute (cb);
3456 public override AttributeTargets AttributeTargets {
3458 return AttributeTargets.Method;
3462 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
3464 EmitContext ec = new EmitContext (this, ds, Parent, Location, ig, MemberType, ModFlags, false);
3466 ec.CurrentIterator = ds as Iterator;
3467 if (ec.CurrentIterator != null)
3468 ec.CurrentAnonymousMethod = ec.CurrentIterator.Host;
3473 public override bool Define ()
3478 if (!CheckAbstractAndExtern (block != null))
3484 MethodData = new MethodData (this, ModFlags, flags, this);
3486 if (!MethodData.Define (ParentContainer))
3489 MethodBuilder = MethodData.MethodBuilder;
3491 if (MemberType.IsAbstract && MemberType.IsSealed) {
3492 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3499 public override void Emit ()
3501 if (OptAttributes != null)
3502 OptAttributes.Emit ();
3504 if (declarative_security != null) {
3505 foreach (DictionaryEntry de in declarative_security) {
3506 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3513 protected void Error_ConditionalAttributeIsNotValid ()
3515 Report.Error (577, Location,
3516 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3517 GetSignatureForError ());
3520 public override bool MarkForDuplicationCheck ()
3522 caching_flags |= Flags.TestMethodDuplication;
3526 public override string[] ValidAttributeTargets {
3528 return attribute_targets;
3532 #region IMethodData Members
3534 public CallingConventions CallingConventions {
3536 CallingConventions cc = Parameters.CallingConvention;
3537 if (Parameters.HasArglist)
3538 block.HasVarargs = true;
3541 if ((ModFlags & Modifiers.STATIC) == 0)
3542 cc |= CallingConventions.HasThis;
3544 // FIXME: How is `ExplicitThis' used in C#?
3550 public Type ReturnType {
3556 public MemberName MethodName {
3562 public new Location Location {
3564 return base.Location;
3568 protected override bool CheckBase() {
3569 if (!base.CheckBase ())
3572 // TODO: Destructor should derive from MethodCore
3573 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3574 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3575 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3583 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3585 public bool IsExcluded () {
3586 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3587 return (caching_flags & Flags.Excluded) != 0;
3589 caching_flags &= ~Flags.Excluded_Undetected;
3591 if (base_method == null) {
3592 if (OptAttributes == null)
3595 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3600 foreach (Attribute a in attrs) {
3601 string condition = a.GetConditionalAttributeValue ();
3602 if (condition == null)
3605 if (RootContext.AllDefines.Contains (condition))
3609 caching_flags |= Flags.Excluded;
3613 IMethodData md = TypeManager.GetMethod (base_method);
3615 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
3616 caching_flags |= Flags.Excluded;
3622 if (md.IsExcluded ()) {
3623 caching_flags |= Flags.Excluded;
3633 public class SourceMethod : ISourceMethod
3638 protected SourceMethod (DeclSpace parent, MethodBase builder,
3639 ISourceFile file, Location start, Location end)
3641 this.parent = parent;
3642 this.builder = builder;
3644 CodeGen.SymbolWriter.OpenMethod (file, this, start.Row, start.Column, end.Row, start.Column);
3647 public string Name {
3648 get { return builder.Name; }
3651 public int NamespaceID {
3652 get { return parent.NamespaceEntry.SymbolFileID; }
3657 if (builder is MethodBuilder)
3658 return ((MethodBuilder) builder).GetToken ().Token;
3659 else if (builder is ConstructorBuilder)
3660 return ((ConstructorBuilder) builder).GetToken ().Token;
3662 throw new NotSupportedException ();
3666 public void CloseMethod ()
3668 if (CodeGen.SymbolWriter != null)
3669 CodeGen.SymbolWriter.CloseMethod ();
3672 public static SourceMethod Create (DeclSpace parent, MethodBase builder, Block block)
3674 if (CodeGen.SymbolWriter == null)
3679 Location start_loc = block.StartLocation;
3680 if (start_loc.IsNull)
3683 Location end_loc = block.EndLocation;
3687 ISourceFile file = start_loc.SourceFile;
3691 return new SourceMethod (
3692 parent, builder, file, start_loc, end_loc);
3696 public class Method : MethodOrOperator, IIteratorContainer {
3699 /// Modifiers allowed in a class declaration
3701 const int AllowedModifiers =
3704 Modifiers.PROTECTED |
3705 Modifiers.INTERNAL |
3710 Modifiers.OVERRIDE |
3711 Modifiers.ABSTRACT |
3713 Modifiers.METHOD_YIELDS |
3716 const int AllowedInterfaceModifiers =
3717 Modifiers.NEW | Modifiers.UNSAFE;
3720 // return_type can be "null" for VOID values.
3722 public Method (DeclSpace parent, Expression return_type, int mod, bool is_iface,
3723 MemberName name, Parameters parameters, Attributes attrs)
3724 : base (parent, return_type, mod,
3725 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3726 is_iface, name, attrs, parameters)
3730 public override string GetSignatureForError()
3732 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
3735 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
3737 Report.Error (17, location,
3738 "Program `{0}' has more than one entry point defined: `{1}'",
3739 CodeGen.FileName, TypeManager.CSharpSignature(b));
3742 bool IsEntryPoint (MethodBuilder b, Parameters pinfo)
3744 if (b.ReturnType != TypeManager.void_type &&
3745 b.ReturnType != TypeManager.int32_type)
3748 if (pinfo.Count == 0)
3751 if (pinfo.Count > 1)
3754 Type t = pinfo.ParameterType(0);
3756 (t.GetArrayRank() == 1) &&
3757 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3758 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3764 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3766 if (a.Type == TypeManager.conditional_attribute_type) {
3767 if (IsExplicitImpl) {
3768 Error_ConditionalAttributeIsNotValid ();
3772 if (ReturnType != TypeManager.void_type) {
3773 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
3777 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3778 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
3783 Report.Error (582, Location, "Conditional not valid on interface members");
3787 if (MethodData.implementing != null) {
3788 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
3789 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
3793 for (int i = 0; i < ParameterInfo.Count; ++i) {
3794 if ((ParameterInfo.ParameterModifier (i) & Parameter.Modifier.OUTMASK) != 0) {
3795 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
3801 base.ApplyAttributeBuilder (a, cb);
3804 protected override bool CheckForDuplications ()
3806 ArrayList ar = ParentContainer.Methods;
3808 int arLen = ar.Count;
3810 for (int i = 0; i < arLen; i++) {
3811 Method m = (Method) ar [i];
3812 if (IsDuplicateImplementation (m))
3817 ar = ParentContainer.Properties;
3819 for (int i = 0; i < ar.Count; ++i) {
3820 PropertyBase pb = (PropertyBase) ar [i];
3821 if (pb.AreAccessorsDuplicateImplementation (this))
3826 ar = ParentContainer.Indexers;
3828 for (int i = 0; i < ar.Count; ++i) {
3829 PropertyBase pb = (PropertyBase) ar [i];
3830 if (pb.AreAccessorsDuplicateImplementation (this))
3835 ar = ParentContainer.Events;
3837 for (int i = 0; i < ar.Count; ++i) {
3838 Event ev = (Event) ar [i];
3839 if (ev.AreAccessorsDuplicateImplementation (this))
3850 public override bool Define ()
3852 if (!base.Define ())
3855 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
3856 Error1599 (Location, ReturnType);
3860 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
3861 Name == "Finalize" && !(this is Destructor)) {
3862 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
3866 // Setup iterator if we are one
3868 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3869 Iterator iterator = new Iterator (this,
3873 if (!iterator.DefineIterator ())
3878 // This is used to track the Entry Point,
3880 if (Name == "Main" &&
3881 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3882 (RootContext.MainClass == null ||
3883 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3884 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3885 IMethodData md = TypeManager.GetMethod (MethodBuilder);
3886 md.SetMemberIsUsed ();
3888 if (RootContext.EntryPoint == null) {
3889 if (Parent.IsGeneric){
3890 Report.Error (-201, Location,
3891 "Entry point can not be defined in a generic class");
3894 RootContext.EntryPoint = MethodBuilder;
3895 RootContext.EntryPointLocation = Location;
3897 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3898 Error_DuplicateEntryPoint (MethodBuilder, Location);
3901 if (RootContext.WarningLevel >= 4)
3902 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder));
3912 public override void Emit ()
3914 MethodData.Emit (Parent);
3921 public static void Error1599 (Location loc, Type t)
3923 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
3926 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
3928 MethodInfo mi = (MethodInfo) ParentContainer.BaseCache.FindMemberToOverride (
3929 Parent.TypeBuilder, Name, ParameterTypes, false);
3934 base_ret_type = mi.ReturnType;
3938 protected override bool VerifyClsCompliance ()
3940 if (!base.VerifyClsCompliance ())
3943 if (ParameterInfo.Count > 0) {
3944 ArrayList al = (ArrayList)ParentContainer.MemberCache.Members [Name];
3946 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
3953 public abstract class ConstructorInitializer {
3954 ArrayList argument_list;
3955 protected ConstructorInfo base_constructor;
3958 public ConstructorInitializer (ArrayList argument_list, Location loc)
3960 this.argument_list = argument_list;
3964 public ArrayList Arguments {
3966 return argument_list;
3970 public bool Resolve (ConstructorBuilder caller_builder, Block block, EmitContext ec)
3972 Expression base_constructor_group;
3976 ec.CurrentBlock = block;
3978 if (argument_list != null){
3979 foreach (Argument a in argument_list){
3980 if (!a.Resolve (ec, loc))
3984 ec.CurrentBlock = null;
3986 if (this is ConstructorBaseInitializer) {
3987 if (ec.ContainerType.BaseType == null)
3990 t = ec.ContainerType.BaseType;
3991 if (ec.ContainerType.IsValueType) {
3992 Report.Error (522, loc,
3993 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
3997 t = ec.ContainerType;
3999 base_constructor_group = Expression.MemberLookup (
4000 ec.ContainerType, t, ".ctor", MemberTypes.Constructor,
4001 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4004 if (base_constructor_group == null){
4006 base_constructor_group = Expression.MemberLookup (
4007 t, null, t, ".ctor", MemberTypes.Constructor,
4008 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4012 int errors = Report.Errors;
4013 if (base_constructor_group != null)
4014 base_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4015 ec, (MethodGroupExpr) base_constructor_group, argument_list,
4018 if (base_constructor == null) {
4019 if (errors == Report.Errors)
4020 Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpSignature (caller_builder),
4021 argument_list == null ? 0 : argument_list.Count);
4026 Expression.ErrorIsInaccesible (loc, TypeManager.CSharpSignature (base_constructor));
4027 base_constructor = null;
4031 if (base_constructor == caller_builder){
4032 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4039 public virtual void Emit (EmitContext ec)
4041 if (base_constructor != null){
4042 ec.Mark (loc, false);
4044 Invocation.EmitCall (ec, true, true, null, base_constructor, argument_list, loc);
4046 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), base_constructor, argument_list, loc);
4051 public class ConstructorBaseInitializer : ConstructorInitializer {
4052 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4053 base (argument_list, l)
4058 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4059 public GeneratedBaseInitializer (Location loc):
4065 public class ConstructorThisInitializer : ConstructorInitializer {
4066 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4067 base (argument_list, l)
4072 public class Constructor : MethodCore, IMethodData {
4073 public ConstructorBuilder ConstructorBuilder;
4074 public ConstructorInitializer Initializer;
4075 ListDictionary declarative_security;
4078 // Modifiers allowed for a constructor.
4080 public const int AllowedModifiers =
4082 Modifiers.PROTECTED |
4083 Modifiers.INTERNAL |
4089 static string[] attribute_targets = new string [] { "method" };
4091 bool has_compliant_args = false;
4093 // The spec claims that static is not permitted, but
4094 // my very own code has static constructors.
4096 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4097 ConstructorInitializer init, Location loc)
4098 : base (parent, null, mod, AllowedModifiers, false, new MemberName (name, loc),
4104 public bool HasCompliantArgs {
4105 get { return has_compliant_args; }
4108 public override AttributeTargets AttributeTargets {
4109 get { return AttributeTargets.Constructor; }
4114 // Returns true if this is a default constructor
4116 public bool IsDefault ()
4118 if ((ModFlags & Modifiers.STATIC) != 0)
4119 return Parameters.Empty;
4121 return Parameters.Empty &&
4122 (Initializer is ConstructorBaseInitializer) &&
4123 (Initializer.Arguments == null);
4126 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4128 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4129 if (declarative_security == null) {
4130 declarative_security = new ListDictionary ();
4132 a.ExtractSecurityPermissionSet (declarative_security);
4136 if (a.Type == TypeManager.methodimpl_attr_type &&
4137 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
4138 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
4141 ConstructorBuilder.SetCustomAttribute (cb);
4144 protected override bool CheckForDuplications ()
4146 ArrayList ar = ParentContainer.InstanceConstructors;
4148 int arLen = ar.Count;
4150 for (int i = 0; i < arLen; i++) {
4151 Constructor m = (Constructor) ar [i];
4152 if (IsDuplicateImplementation (m))
4159 protected override bool CheckBase ()
4161 if ((ModFlags & Modifiers.STATIC) != 0) {
4162 if (!Parameters.Empty) {
4163 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4164 GetSignatureForError ());
4168 // the rest can be ignored
4172 // Check whether arguments were correct.
4173 if (!DoDefineParameters ())
4176 if (!CheckForDuplications ())
4179 if (ParentContainer.Kind == Kind.Struct) {
4180 if (ParameterTypes.Length == 0) {
4181 Report.Error (568, Location,
4182 "Structs cannot contain explicit parameterless constructors");
4186 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4187 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4192 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4193 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4200 // Creates the ConstructorBuilder
4202 public override bool Define ()
4204 if (ConstructorBuilder != null)
4207 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4208 MethodAttributes.SpecialName);
4210 if ((ModFlags & Modifiers.STATIC) != 0) {
4211 ca |= MethodAttributes.Static | MethodAttributes.Private;
4213 ca |= MethodAttributes.HideBySig;
4215 if ((ModFlags & Modifiers.PUBLIC) != 0)
4216 ca |= MethodAttributes.Public;
4217 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4218 if ((ModFlags & Modifiers.INTERNAL) != 0)
4219 ca |= MethodAttributes.FamORAssem;
4221 ca |= MethodAttributes.Family;
4222 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4223 ca |= MethodAttributes.Assembly;
4225 ca |= MethodAttributes.Private;
4228 if (!CheckAbstractAndExtern (block != null))
4231 // Check if arguments were correct.
4235 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4236 ca, CallingConventions,
4239 if ((ModFlags & Modifiers.UNSAFE) != 0)
4240 ConstructorBuilder.InitLocals = false;
4242 if (ParentContainer.IsComImport) {
4243 if (!IsDefault ()) {
4244 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4245 Parent.GetSignatureForError ());
4248 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4251 TypeManager.AddMethod (ConstructorBuilder, this);
4259 public override void Emit ()
4261 if (OptAttributes != null)
4262 OptAttributes.Emit ();
4264 EmitContext ec = CreateEmitContext (null, null);
4266 if (block != null) {
4267 // If this is a non-static `struct' constructor and doesn't have any
4268 // initializer, it must initialize all of the struct's fields.
4269 if ((ParentContainer.Kind == Kind.Struct) &&
4270 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4271 block.AddThisVariable (Parent, Location);
4273 if (!block.ResolveMeta (ec, ParameterInfo))
4277 if ((ModFlags & Modifiers.STATIC) == 0){
4278 if (ParentContainer.Kind == Kind.Class && Initializer == null)
4279 Initializer = new GeneratedBaseInitializer (Location);
4283 // Spec mandates that Initializers will not have
4287 if ((Initializer != null) &&
4288 !Initializer.Resolve (ConstructorBuilder, block, ec))
4290 ec.IsStatic = false;
4293 Parameters.ApplyAttributes (ConstructorBuilder);
4295 SourceMethod source = SourceMethod.Create (
4296 Parent, ConstructorBuilder, block);
4299 // Classes can have base initializers and instance field initializers.
4301 if (ParentContainer.Kind == Kind.Class){
4302 if ((ModFlags & Modifiers.STATIC) == 0){
4305 // If we use a "this (...)" constructor initializer, then
4306 // do not emit field initializers, they are initialized in the other constructor
4308 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4309 ParentContainer.EmitFieldInitializers (ec);
4312 if (Initializer != null) {
4313 Initializer.Emit (ec);
4316 if ((ModFlags & Modifiers.STATIC) != 0)
4317 ParentContainer.EmitFieldInitializers (ec);
4319 ec.EmitTopBlock (this, block);
4322 source.CloseMethod ();
4326 if (declarative_security != null) {
4327 foreach (DictionaryEntry de in declarative_security) {
4328 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4335 // Is never override
4336 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4341 public override string GetSignatureForError()
4343 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4346 public override string[] ValidAttributeTargets {
4348 return attribute_targets;
4352 protected override bool VerifyClsCompliance ()
4354 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4358 if (ParameterInfo.Count > 0) {
4359 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4361 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4363 if (Parent.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4364 foreach (Type param in ParameterTypes) {
4365 if (param.IsArray) {
4371 has_compliant_args = true;
4375 #region IMethodData Members
4377 public System.Reflection.CallingConventions CallingConventions {
4379 CallingConventions cc = Parameters.CallingConvention;
4381 if (ParentContainer.Kind == Kind.Class)
4382 if ((ModFlags & Modifiers.STATIC) == 0)
4383 cc |= CallingConventions.HasThis;
4385 // FIXME: How is `ExplicitThis' used in C#?
4391 public new Location Location {
4393 return base.Location;
4397 public MemberName MethodName {
4403 public Type ReturnType {
4409 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4411 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4412 return new EmitContext (this, Parent, Location, ig_, null, ModFlags, true);
4415 public bool IsExcluded()
4424 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4426 public interface IMethodData
4428 CallingConventions CallingConventions { get; }
4429 Location Location { get; }
4430 MemberName MethodName { get; }
4431 Type ReturnType { get; }
4432 Parameters ParameterInfo { get; }
4434 Attributes OptAttributes { get; }
4435 ToplevelBlock Block { get; set; }
4437 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4438 ObsoleteAttribute GetObsoleteAttribute ();
4439 string GetSignatureForError ();
4441 bool IsClsComplianceRequired ();
4442 void SetMemberIsUsed ();
4446 // Encapsulates most of the Method's state
4448 public class MethodData {
4450 readonly IMethodData method;
4453 // Are we implementing an interface ?
4455 public MethodInfo implementing;
4460 protected MemberBase member;
4461 protected int modifiers;
4462 protected MethodAttributes flags;
4464 MethodBuilder builder = null;
4465 public MethodBuilder MethodBuilder {
4471 public MethodData (MemberBase member,
4472 int modifiers, MethodAttributes flags, IMethodData method)
4474 this.member = member;
4475 this.modifiers = modifiers;
4478 this.method = method;
4481 public bool Define (DeclSpace parent)
4483 string name = method.MethodName.Name;
4484 string method_name = name;
4486 TypeContainer container = ((TypeContainer) parent).PartialContainer;
4488 PendingImplementation pending = container.PendingImplementations;
4489 if (pending != null){
4490 if (member is Indexer) // TODO: test it, but it should work without this IF
4491 implementing = pending.IsInterfaceIndexer (
4492 member.InterfaceType, method.ReturnType, method.ParameterInfo);
4494 implementing = pending.IsInterfaceMethod (
4495 member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
4497 if (member.InterfaceType != null){
4498 if (implementing == null){
4499 if (member is PropertyBase) {
4500 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4501 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4502 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4505 Report.Error (539, method.Location,
4506 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4507 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4511 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4512 Report.SymbolRelatedToPreviousError (implementing);
4513 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4514 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4517 method_name = member.InterfaceType.FullName + "." + name;
4519 if (implementing != null) {
4520 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4521 if (prop_method != null) {
4522 if (!implementing.IsSpecialName) {
4523 Report.SymbolRelatedToPreviousError (implementing);
4524 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4525 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4528 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4529 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4530 Report.SymbolRelatedToPreviousError (implementing);
4531 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4532 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4541 // For implicit implementations, make sure we are public, for
4542 // explicit implementations, make sure we are private.
4544 if (implementing != null){
4546 // Setting null inside this block will trigger a more
4547 // verbose error reporting for missing interface implementations
4549 // The "candidate" function has been flagged already
4550 // but it wont get cleared
4552 if (member.IsExplicitImpl){
4553 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4554 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4555 implementing = null;
4557 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4558 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4560 // If this is an interface method implementation,
4561 // check for public accessibility
4563 implementing = null;
4564 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4565 // We may never be private.
4566 implementing = null;
4567 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4569 // We may be protected if we're overriding something.
4571 implementing = null;
4576 // Static is not allowed
4578 if ((modifiers & Modifiers.STATIC) != 0){
4579 implementing = null;
4584 // If implementing is still valid, set flags
4586 if (implementing != null){
4588 // When implementing interface methods, set NewSlot
4589 // unless, we are overwriting a method.
4591 if (implementing.DeclaringType.IsInterface){
4592 if ((modifiers & Modifiers.OVERRIDE) == 0)
4593 flags |= MethodAttributes.NewSlot;
4596 MethodAttributes.Virtual |
4597 MethodAttributes.HideBySig;
4599 // Set Final unless we're virtual, abstract or already overriding a method.
4600 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4601 flags |= MethodAttributes.Final;
4604 DefineMethodBuilder (container, method_name, method.ParameterInfo.Types);
4606 if (builder == null)
4609 if ((modifiers & Modifiers.UNSAFE) != 0)
4610 builder.InitLocals = false;
4612 if (implementing != null){
4614 // clear the pending implemntation flag
4616 if (member is Indexer) {
4617 pending.ImplementIndexer (
4618 member.InterfaceType, builder, method.ReturnType,
4619 method.ParameterInfo, member.IsExplicitImpl);
4621 pending.ImplementMethod (
4622 member.InterfaceType, name, method.ReturnType,
4623 method.ParameterInfo, member.IsExplicitImpl);
4625 if (member.IsExplicitImpl)
4626 container.TypeBuilder.DefineMethodOverride (
4627 builder, implementing);
4631 TypeManager.AddMethod (builder, method);
4638 /// Create the MethodBuilder for the method
4640 void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
4642 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4644 if ((modifiers & extern_static) == extern_static) {
4646 if (method.OptAttributes != null) {
4647 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type);
4648 if (dllimport_attribute != null) {
4649 flags |= MethodAttributes.PinvokeImpl;
4650 builder = dllimport_attribute.DefinePInvokeMethod (
4651 container.TypeBuilder, method_name, flags,
4652 method.ReturnType, ParameterTypes);
4658 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4659 // We are more strict than Microsoft and report CS0626 like error
4660 if (method.OptAttributes == null ||
4661 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type)) {
4662 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",
4663 method.GetSignatureForError ());
4668 builder = container.TypeBuilder.DefineMethod (
4669 method_name, flags, method.CallingConventions,
4670 method.ReturnType, ParameterTypes);
4676 public void Emit (DeclSpace parent)
4679 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4680 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
4682 ec = method.CreateEmitContext (parent, null);
4684 method.ParameterInfo.ApplyAttributes (MethodBuilder);
4686 ToplevelBlock block = method.Block;
4688 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
4691 // Handle destructors specially
4693 // FIXME: This code generates buggy code
4695 if (member is Destructor)
4696 EmitDestructor (ec, block);
4698 ec.EmitTopBlock (method, block);
4701 source.CloseMethod ();
4704 void EmitDestructor (EmitContext ec, ToplevelBlock block)
4706 ILGenerator ig = ec.ig;
4708 Label finish = ig.DefineLabel ();
4710 block.SetDestructor ();
4712 ig.BeginExceptionBlock ();
4713 ec.ReturnLabel = finish;
4714 ec.HasReturnLabel = true;
4715 ec.EmitTopBlock (method, block);
4717 // ig.MarkLabel (finish);
4718 ig.BeginFinallyBlock ();
4720 if (ec.ContainerType.BaseType != null) {
4721 Expression member_lookup = Expression.MemberLookup (
4722 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4723 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4725 if (member_lookup != null){
4726 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
4728 ig.Emit (OpCodes.Ldarg_0);
4729 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
4733 ig.EndExceptionBlock ();
4734 //ig.MarkLabel (ec.ReturnLabel);
4735 ig.Emit (OpCodes.Ret);
4739 // TODO: Should derive from MethodCore
4740 public class Destructor : Method {
4742 static string[] attribute_targets = new string [] { "method" };
4744 public Destructor (DeclSpace parent, Expression return_type, int mod,
4745 string name, Parameters parameters, Attributes attrs,
4747 : base (parent, return_type, mod, false, new MemberName (name, l),
4751 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4753 if (a.Type == TypeManager.conditional_attribute_type) {
4754 Error_ConditionalAttributeIsNotValid ();
4758 base.ApplyAttributeBuilder (a, cb);
4761 public override string GetSignatureForError ()
4763 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
4766 public override string[] ValidAttributeTargets {
4768 return attribute_targets;
4773 abstract public class MemberBase : MemberCore {
4774 public Expression Type;
4776 public MethodAttributes flags;
4778 protected readonly int explicit_mod_flags;
4781 // The "short" name of this property / indexer / event. This is the
4782 // name without the explicit interface.
4784 public string ShortName {
4785 get { return MemberName.Name; }
4786 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
4789 public TypeContainer ParentContainer {
4790 get { return ((TypeContainer) Parent).PartialContainer; }
4794 // The type of this property / indexer / event
4797 public Type MemberType {
4799 if (member_type == null && Type != null) {
4800 Type = Type.ResolveAsTypeTerminal (ResolveContext, false);
4802 member_type = Type.Type;
4810 // Whether this is an interface member.
4812 public bool IsInterface;
4815 // If true, this is an explicit interface implementation
4817 public bool IsExplicitImpl;
4820 // The interface type we are explicitly implementing
4822 public Type InterfaceType = null;
4825 // The constructor is only exposed to our children
4827 protected MemberBase (DeclSpace parent, Expression type, int mod,
4828 int allowed_mod, int def_mod, MemberName name,
4830 : base (parent, name, attrs)
4832 explicit_mod_flags = mod;
4834 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
4835 IsExplicitImpl = (MemberName.Left != null);
4838 protected virtual bool CheckBase ()
4840 if ((ModFlags & Modifiers.PROTECTED) != 0 && ParentContainer.Kind == Kind.Struct) {
4841 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4845 if ((RootContext.WarningLevel >= 4) &&
4846 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
4847 ((ModFlags & Modifiers.PROTECTED) != 0) &&
4848 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
4849 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4854 protected virtual bool DoDefine ()
4857 throw new InternalErrorException ();
4860 ModFlags = Modifiers.PUBLIC |
4861 Modifiers.ABSTRACT |
4862 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
4864 flags = MethodAttributes.Public |
4865 MethodAttributes.Abstract |
4866 MethodAttributes.HideBySig |
4867 MethodAttributes.NewSlot |
4868 MethodAttributes.Virtual;
4870 if (!ParentContainer.MethodModifiersValid (this))
4873 flags = Modifiers.MethodAttr (ModFlags);
4876 if (MemberType == null)
4879 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
4880 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
4881 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
4882 GetSignatureForError (), Parent.GetSignatureForError ());
4886 // verify accessibility
4887 if (!Parent.AsAccessible (MemberType, ModFlags)) {
4888 Report.SymbolRelatedToPreviousError (MemberType);
4889 if (this is Property)
4890 Report.Error (53, Location,
4891 "Inconsistent accessibility: property type `" +
4892 TypeManager.CSharpName (MemberType) + "' is less " +
4893 "accessible than property `" + GetSignatureForError () + "'");
4894 else if (this is Indexer)
4895 Report.Error (54, Location,
4896 "Inconsistent accessibility: indexer return type `" +
4897 TypeManager.CSharpName (MemberType) + "' is less " +
4898 "accessible than indexer `" + GetSignatureForError () + "'");
4899 else if (this is MethodCore) {
4900 if (this is Operator)
4901 Report.Error (56, Location,
4902 "Inconsistent accessibility: return type `" +
4903 TypeManager.CSharpName (MemberType) + "' is less " +
4904 "accessible than operator `" + GetSignatureForError () + "'");
4906 Report.Error (50, Location,
4907 "Inconsistent accessibility: return type `" +
4908 TypeManager.CSharpName (MemberType) + "' is less " +
4909 "accessible than method `" + GetSignatureForError () + "'");
4911 Report.Error (52, Location,
4912 "Inconsistent accessibility: field type `" +
4913 TypeManager.CSharpName (MemberType) + "' is less " +
4914 "accessible than field `" + GetSignatureForError () + "'");
4919 if (IsExplicitImpl) {
4920 Expression expr = MemberName.Left.GetTypeExpression ();
4921 TypeExpr texpr = expr.ResolveAsTypeTerminal (ResolveContext, false);
4925 InterfaceType = texpr.Type;
4927 if (!InterfaceType.IsInterface) {
4928 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
4932 if (!ParentContainer.VerifyImplements (this))
4935 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
4941 protected bool IsTypePermitted ()
4943 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
4944 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
4950 protected override bool VerifyClsCompliance()
4952 if (base.VerifyClsCompliance ()) {
4956 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
4957 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
4965 // Fields and Events both generate FieldBuilders, we use this to share
4966 // their common bits. This is also used to flag usage of the field
4968 abstract public class FieldBase : MemberBase {
4969 public FieldBuilder FieldBuilder;
4970 public Status status;
4971 protected Expression initializer;
4972 ExpressionStatement initializerStatement;
4975 public enum Status : byte {
4976 HAS_OFFSET = 4 // Used by FieldMember.
4979 static string[] attribute_targets = new string [] { "field" };
4982 /// Symbol with same name in base class/struct
4984 public MemberInfo conflict_symbol;
4986 protected FieldBase (DeclSpace parent, Expression type, int mod,
4987 int allowed_mod, MemberName name, Attributes attrs)
4988 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
4993 public override AttributeTargets AttributeTargets {
4995 return AttributeTargets.Field;
4999 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5001 if (a.Type == TypeManager.marshal_as_attr_type) {
5002 UnmanagedMarshal marshal = a.GetMarshal (this);
5003 if (marshal != null) {
5004 FieldBuilder.SetMarshal (marshal);
5009 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5010 a.Error_InvalidSecurityParent ();
5014 FieldBuilder.SetCustomAttribute (cb);
5017 public void EmitInitializer (EmitContext ec)
5019 initializerStatement.EmitStatement (ec);
5022 protected override bool CheckBase ()
5024 if (!base.CheckBase ())
5031 conflict_symbol = ParentContainer.FindBaseMemberWithSameName (Name, false);
5032 if (conflict_symbol == null) {
5033 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5034 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5039 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5040 Report.SymbolRelatedToPreviousError (conflict_symbol);
5041 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5042 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5048 public Expression Initializer {
5050 if (value != null) {
5051 this.initializer = value;
5052 ParentContainer.RegisterFieldForInitialization (this);
5057 protected virtual bool IsFieldClsCompliant {
5059 if (FieldBuilder == null)
5062 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5066 public Expression ResolveInitializer ()
5068 // TODO: again it's too heavy-weight
5069 EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
5070 ec.IsFieldInitializer = true;
5071 initializer = initializer.Resolve (ec);
5072 if (initializer == null)
5075 FieldExpr fe = new FieldExpr (FieldBuilder, Location, true);
5076 if ((ModFlags & Modifiers.STATIC) == 0)
5078 fe.InstanceExpression = CompilerGeneratedThis.Instance;
5081 initializerStatement = new Assign (fe, initializer, Location).ResolveStatement (ec);
5085 public bool HasDefaultInitializer
5089 Constant c = initializer as Constant;
5093 return c.IsDefaultInitializer (MemberType);
5097 public override string[] ValidAttributeTargets
5100 return attribute_targets;
5104 protected override bool VerifyClsCompliance ()
5106 if (!base.VerifyClsCompliance ())
5109 if (!IsFieldClsCompliant) {
5110 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5116 public void SetAssigned ()
5118 caching_flags |= Flags.IsAssigned;
5122 public abstract class FieldMember : FieldBase
5124 protected FieldMember (DeclSpace parent, Expression type, int mod,
5125 int allowed_mod, MemberName name, Attributes attrs)
5126 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, attrs)
5128 if ((mod & Modifiers.ABSTRACT) != 0)
5129 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5132 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5134 if (a.Type == TypeManager.field_offset_attribute_type)
5136 status |= Status.HAS_OFFSET;
5138 if (!ParentContainer.HasExplicitLayout) {
5139 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5143 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5144 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5150 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5151 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5156 base.ApplyAttributeBuilder (a, cb);
5159 public override bool Define()
5161 if (MemberType == null || Type == null)
5164 if (MemberType == TypeManager.void_type) {
5165 Report.Error (1547, Location, "Keyword 'void' cannot be used in this context");
5172 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5173 Report.Error (52, Location,
5174 "Inconsistent accessibility: field type `" +
5175 TypeManager.CSharpName (MemberType) + "' is less " +
5176 "accessible than field `" + GetSignatureForError () + "'");
5180 if (!IsTypePermitted ())
5186 public override void Emit ()
5188 if (OptAttributes != null) {
5189 OptAttributes.Emit ();
5192 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0 && ParentContainer.HasExplicitLayout) {
5193 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5200 // Represents header string for documentation comment.
5202 public override string DocCommentHeader {
5203 get { return "F:"; }
5207 interface IFixedBuffer
5209 FieldInfo Element { get; }
5210 Type ElementType { get; }
5213 public class FixedFieldExternal: IFixedBuffer
5215 FieldInfo element_field;
5217 public FixedFieldExternal (FieldInfo fi)
5219 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5222 #region IFixedField Members
5224 public FieldInfo Element {
5226 return element_field;
5230 public Type ElementType {
5232 return element_field.FieldType;
5240 /// Fixed buffer implementation
5242 public class FixedField : FieldMember, IFixedBuffer
5244 public const string FixedElementName = "FixedElementField";
5245 static int GlobalCounter = 0;
5246 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5247 static FieldInfo[] fi;
5249 TypeBuilder fixed_buffer_type;
5250 FieldBuilder element;
5251 Expression size_expr;
5254 const int AllowedModifiers =
5257 Modifiers.PROTECTED |
5258 Modifiers.INTERNAL |
5261 public FixedField (DeclSpace parent, Expression type, int mod, string name,
5262 Expression size_expr, Attributes attrs, Location loc):
5263 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5265 if (RootContext.Version == LanguageVersion.ISO_1)
5266 Report.FeatureIsNotStandardized (loc, "fixed size buffers");
5268 this.size_expr = size_expr;
5271 public override bool Define()
5274 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5275 Report.Warning (-23, 1, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5278 if (ParentContainer.Kind != Kind.Struct) {
5279 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5280 GetSignatureForError ());
5284 if (!base.Define ())
5287 if (!TypeManager.IsPrimitiveType (MemberType)) {
5288 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",
5289 GetSignatureForError ());
5293 EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
5294 Constant c = size_expr.ResolveAsConstant (ec, this);
5298 IntConstant buffer_size_const = c.ToInt (Location);
5299 if (buffer_size_const == null)
5302 buffer_size = buffer_size_const.Value;
5304 if (buffer_size <= 0) {
5305 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5309 int type_size = Expression.GetTypeSize (MemberType);
5311 if (buffer_size > int.MaxValue / type_size) {
5312 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5313 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5317 buffer_size *= type_size;
5320 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5322 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5323 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5324 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5325 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5327 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5328 TypeManager.RegisterFieldBase (FieldBuilder, this);
5333 public override void Emit()
5336 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5338 object[] fi_val = new object[1];
5339 fi_val [0] = buffer_size;
5341 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5342 ctor_args, fi, fi_val);
5343 fixed_buffer_type.SetCustomAttribute (cab);
5346 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5347 FieldBuilder.SetCustomAttribute (cab);
5352 protected override bool IsFieldClsCompliant {
5358 #region IFixedField Members
5360 public FieldInfo Element {
5366 public Type ElementType {
5376 // The Field class is used to represents class/struct fields during parsing.
5378 public class Field : FieldMember {
5380 // Modifiers allowed in a class declaration
5382 const int AllowedModifiers =
5385 Modifiers.PROTECTED |
5386 Modifiers.INTERNAL |
5389 Modifiers.VOLATILE |
5393 public Field (DeclSpace parent, Expression type, int mod, string name,
5394 Attributes attrs, Location loc)
5395 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5400 public override bool Define ()
5402 if (!base.Define ())
5405 if (RootContext.WarningLevel > 1){
5406 Type ptype = Parent.TypeBuilder.BaseType;
5408 // ptype is only null for System.Object while compiling corlib.
5410 TypeContainer.FindMembers (
5411 ptype, MemberTypes.Method,
5412 BindingFlags.Public |
5413 BindingFlags.Static | BindingFlags.Instance,
5414 System.Type.FilterName, Name);
5418 if ((ModFlags & Modifiers.VOLATILE) != 0){
5419 if (!MemberType.IsClass){
5420 Type vt = MemberType;
5422 if (TypeManager.IsEnumType (vt))
5423 vt = TypeManager.EnumToUnderlying (MemberType);
5425 if (!((vt == TypeManager.bool_type) ||
5426 (vt == TypeManager.sbyte_type) ||
5427 (vt == TypeManager.byte_type) ||
5428 (vt == TypeManager.short_type) ||
5429 (vt == TypeManager.ushort_type) ||
5430 (vt == TypeManager.int32_type) ||
5431 (vt == TypeManager.uint32_type) ||
5432 (vt == TypeManager.char_type) ||
5433 (vt == TypeManager.float_type) ||
5434 (!vt.IsValueType))){
5435 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5436 GetSignatureForError (), TypeManager.CSharpName (vt));
5441 if ((ModFlags & Modifiers.READONLY) != 0){
5442 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5443 GetSignatureForError ());
5448 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5450 if (ParentContainer.Kind == Kind.Struct &&
5451 ((fa & FieldAttributes.Static) == 0) &&
5452 MemberType == Parent.TypeBuilder &&
5453 !TypeManager.IsBuiltinType (MemberType)){
5454 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5455 "' causes a cycle in the structure layout");
5460 FieldBuilder = Parent.TypeBuilder.DefineField (
5461 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5463 TypeManager.RegisterFieldBase (FieldBuilder, this);
5465 catch (ArgumentException) {
5466 Report.Warning (-24, 1, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5473 protected override bool VerifyClsCompliance ()
5475 if (!base.VerifyClsCompliance ())
5478 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5479 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5487 // `set' and `get' accessors are represented with an Accessor.
5489 public class Accessor : IIteratorContainer {
5491 // Null if the accessor is empty, or a Block if not
5493 public const int AllowedModifiers =
5495 Modifiers.PROTECTED |
5496 Modifiers.INTERNAL |
5499 public ToplevelBlock Block;
5500 public Attributes Attributes;
5501 public Location Location;
5502 public int ModFlags;
5505 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5510 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5513 public void SetYields ()
5519 // Ooouh Martin, templates are missing here.
5520 // When it will be possible move here a lot of child code and template method type.
5521 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5522 protected MethodData method_data;
5523 protected ToplevelBlock block;
5524 protected ListDictionary declarative_security;
5526 // The accessor are created event if they are not wanted.
5527 // But we need them because their names are reserved.
5528 // Field says whether accessor will be emited or not
5529 public readonly bool IsDummy;
5531 protected readonly string prefix;
5533 ReturnParameter return_attributes;
5535 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5536 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5538 this.prefix = prefix;
5542 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5544 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5545 accessor.Attributes)
5547 this.prefix = prefix;
5548 this.block = accessor.Block;
5551 static MemberName SetupName (string prefix, MemberBase member, Location loc)
5553 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5556 public void UpdateName (MemberBase member)
5558 SetMemberName (SetupName (prefix, member, Location));
5561 #region IMethodData Members
5563 public ToplevelBlock Block {
5573 public CallingConventions CallingConventions {
5575 return CallingConventions.Standard;
5579 public bool IsExcluded ()
5584 public MemberName MethodName {
5590 public Type[] ParameterTypes {
5592 return ParameterInfo.Types;
5596 public abstract Parameters ParameterInfo { get ; }
5597 public abstract Type ReturnType { get; }
5598 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
5602 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5604 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5605 a.Type == TypeManager.conditional_attribute_type) {
5606 Report.Error (1667, a.Location,
5607 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
5608 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5612 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5613 if (declarative_security == null)
5614 declarative_security = new ListDictionary ();
5615 a.ExtractSecurityPermissionSet (declarative_security);
5619 if (a.Target == AttributeTargets.Method) {
5620 method_data.MethodBuilder.SetCustomAttribute (cb);
5624 if (a.Target == AttributeTargets.ReturnValue) {
5625 if (return_attributes == null)
5626 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5628 return_attributes.ApplyAttributeBuilder (a, cb);
5632 ApplyToExtraTarget (a, cb);
5635 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5637 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5640 public override bool Define()
5642 throw new NotSupportedException ();
5645 public void Emit (DeclSpace parent)
5647 EmitMethod (parent);
5649 if (OptAttributes != null)
5650 OptAttributes.Emit ();
5652 if (declarative_security != null) {
5653 foreach (DictionaryEntry de in declarative_security) {
5654 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5661 protected virtual void EmitMethod (DeclSpace parent)
5663 method_data.Emit (parent);
5666 public override bool IsClsComplianceRequired()
5671 public bool IsDuplicateImplementation (MethodCore method)
5673 if (!MemberName.Equals (method.MemberName))
5676 Type[] param_types = method.ParameterTypes;
5678 if (param_types.Length != ParameterTypes.Length)
5681 for (int i = 0; i < param_types.Length; i++)
5682 if (param_types [i] != ParameterTypes [i])
5685 Report.SymbolRelatedToPreviousError (method);
5686 Report.Error (111, Location, TypeContainer.Error111, method.GetSignatureForError ());
5690 public override bool IsUsed
5700 public new Location Location {
5702 return base.Location;
5707 // Represents header string for documentation comment.
5709 public override string DocCommentHeader {
5710 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5716 // Properties and Indexers both generate PropertyBuilders, we use this to share
5717 // their common bits.
5719 abstract public class PropertyBase : MethodCore {
5721 public class GetMethod : PropertyMethod
5723 static string[] attribute_targets = new string [] { "method", "return" };
5725 public GetMethod (MethodCore method):
5726 base (method, "get_")
5730 public GetMethod (MethodCore method, Accessor accessor):
5731 base (method, accessor, "get_")
5735 public override MethodBuilder Define (DeclSpace parent)
5737 base.Define (parent);
5739 method_data = new MethodData (method, ModFlags, flags, this);
5741 if (!method_data.Define (parent))
5744 return method_data.MethodBuilder;
5747 public override Type ReturnType {
5749 return method.MemberType;
5753 public override Parameters ParameterInfo {
5755 return Parameters.EmptyReadOnlyParameters;
5759 public override string[] ValidAttributeTargets {
5761 return attribute_targets;
5766 public class SetMethod : PropertyMethod {
5768 static string[] attribute_targets = new string [] { "method", "param", "return" };
5769 ImplicitParameter param_attr;
5770 protected Parameters parameters;
5772 public SetMethod (MethodCore method):
5773 base (method, "set_")
5777 public SetMethod (MethodCore method, Accessor accessor):
5778 base (method, accessor, "set_")
5782 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
5784 if (a.Target == AttributeTargets.Parameter) {
5785 if (param_attr == null)
5786 param_attr = new ImplicitParameter (method_data.MethodBuilder);
5788 param_attr.ApplyAttributeBuilder (a, cb);
5792 base.ApplyAttributeBuilder (a, cb);
5795 public override Parameters ParameterInfo {
5801 protected virtual void DefineParameters ()
5803 parameters = new Parameters (
5804 new Parameter[] { new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location) },
5805 new Type[] { method.MemberType });
5808 public override MethodBuilder Define (DeclSpace parent)
5810 DefineParameters ();
5814 base.Define (parent);
5816 method_data = new MethodData (method, ModFlags, flags, this);
5818 if (!method_data.Define (parent))
5821 return method_data.MethodBuilder;
5824 public override Type ReturnType {
5826 return TypeManager.void_type;
5830 public override string[] ValidAttributeTargets {
5832 return attribute_targets;
5837 static string[] attribute_targets = new string [] { "property" };
5839 public abstract class PropertyMethod : AbstractPropertyEventMethod
5841 protected readonly MethodCore method;
5842 protected MethodAttributes flags;
5845 public PropertyMethod (MethodCore method, string prefix)
5846 : base (method, prefix)
5848 this.method = method;
5851 public PropertyMethod (MethodCore method, Accessor accessor,
5853 : base (method, accessor, prefix)
5855 this.method = method;
5856 this.ModFlags = accessor.ModFlags;
5857 yields = accessor.Yields;
5859 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
5860 Report.FeatureIsNotStandardized (Location, "access modifiers on properties");
5864 public override AttributeTargets AttributeTargets {
5866 return AttributeTargets.Method;
5870 public override bool IsClsComplianceRequired()
5872 return method.IsClsComplianceRequired ();
5875 public virtual MethodBuilder Define (DeclSpace parent)
5877 if (!method.CheckAbstractAndExtern (block != null))
5880 TypeContainer container = ((TypeContainer) parent).PartialContainer;
5883 // Check for custom access modifier
5885 if (ModFlags == 0) {
5886 ModFlags = method.ModFlags;
5887 flags = method.flags;
5889 if (container.Kind == Kind.Interface)
5890 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
5891 GetSignatureForError ());
5893 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
5894 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
5897 CheckModifiers (ModFlags);
5898 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
5899 ModFlags |= Modifiers.PROPERTY_CUSTOM;
5900 flags = Modifiers.MethodAttr (ModFlags);
5901 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
5905 // Setup iterator if we are one
5908 Iterator iterator = new Iterator (this, Parent as TypeContainer, ModFlags);
5910 if (!iterator.DefineIterator ())
5917 public bool HasCustomAccessModifier
5920 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
5924 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
5926 return new EmitContext (method,
5927 ds, method.Parent, method.Location, ig, ReturnType,
5928 method.ModFlags, false);
5931 public override ObsoleteAttribute GetObsoleteAttribute ()
5933 return method.GetObsoleteAttribute ();
5936 public override string GetSignatureForError()
5938 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
5941 void CheckModifiers (int modflags)
5944 int mflags = method.ModFlags & Modifiers.Accessibility;
5946 if ((mflags & Modifiers.PUBLIC) != 0) {
5947 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
5949 else if ((mflags & Modifiers.PROTECTED) != 0) {
5950 if ((mflags & Modifiers.INTERNAL) != 0)
5951 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
5953 flags |= Modifiers.PRIVATE;
5955 else if ((mflags & Modifiers.INTERNAL) != 0)
5956 flags |= Modifiers.PRIVATE;
5958 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
5959 Report.Error (273, Location,
5960 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
5961 GetSignatureForError (), method.GetSignatureForError ());
5965 public override bool MarkForDuplicationCheck ()
5967 caching_flags |= Flags.TestMethodDuplication;
5972 public PropertyMethod Get, Set;
5973 public PropertyBuilder PropertyBuilder;
5974 public MethodBuilder GetBuilder, SetBuilder;
5976 protected EmitContext ec;
5978 public PropertyBase (DeclSpace parent, Expression type, int mod_flags,
5979 int allowed_mod, bool is_iface, MemberName name,
5980 Parameters parameters, Attributes attrs)
5981 : base (parent, type, mod_flags, allowed_mod, is_iface, name,
5986 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5988 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5989 a.Error_InvalidSecurityParent ();
5993 PropertyBuilder.SetCustomAttribute (cb);
5996 public override AttributeTargets AttributeTargets {
5998 return AttributeTargets.Property;
6002 public override bool Define ()
6007 if (!IsTypePermitted ())
6013 protected override bool DoDefine ()
6015 if (!base.DoDefine ())
6019 // Accessors modifiers check
6021 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6022 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6023 GetSignatureForError ());
6027 if ((Get.IsDummy || Set.IsDummy)
6028 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6029 Report.Error (276, Location,
6030 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6031 GetSignatureForError ());
6035 if (MemberType.IsAbstract && MemberType.IsSealed) {
6036 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6043 protected override bool CheckForDuplications ()
6045 ArrayList ar = ParentContainer.Indexers;
6047 int arLen = ar.Count;
6049 for (int i = 0; i < arLen; i++) {
6050 Indexer m = (Indexer) ar [i];
6051 if (IsDuplicateImplementation (m))
6056 ar = ParentContainer.Properties;
6058 int arLen = ar.Count;
6060 for (int i = 0; i < arLen; i++) {
6061 Property m = (Property) ar [i];
6062 if (IsDuplicateImplementation (m))
6070 // TODO: rename to Resolve......
6071 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6073 PropertyInfo base_property = ParentContainer.BaseCache.FindMemberToOverride (
6074 Parent.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
6076 if (base_property == null)
6079 base_ret_type = base_property.PropertyType;
6080 MethodInfo get_accessor = base_property.GetGetMethod (true);
6081 MethodInfo set_accessor = base_property.GetSetMethod (true);
6082 MethodAttributes get_accessor_access, set_accessor_access;
6084 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6085 if (Get != null && !Get.IsDummy && get_accessor == null) {
6086 Report.SymbolRelatedToPreviousError (base_property);
6087 Report.Error (545, Location, "`{0}.get': cannot override because `{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6090 if (Set != null && !Set.IsDummy && set_accessor == null) {
6091 Report.SymbolRelatedToPreviousError (base_property);
6092 Report.Error (546, Location, "`{0}.set': cannot override because `{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6097 // Check base class accessors access
6100 // TODO: rewrite to reuse Get|Set.CheckAccessModifiers and share code there
6101 get_accessor_access = set_accessor_access = 0;
6102 if ((ModFlags & Modifiers.NEW) == 0) {
6103 if (get_accessor != null) {
6104 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6105 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6107 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6108 Error_CannotChangeAccessModifiers (get_accessor, get_accessor_access, ".get");
6111 if (set_accessor != null) {
6112 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6113 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6115 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6116 Error_CannotChangeAccessModifiers (set_accessor, set_accessor_access, ".set");
6121 // Get the less restrictive access
6123 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6126 public override void Emit ()
6129 // The PropertyBuilder can be null for explicit implementations, in that
6130 // case, we do not actually emit the ".property", so there is nowhere to
6131 // put the attribute
6133 if (PropertyBuilder != null && OptAttributes != null)
6134 OptAttributes.Emit ();
6146 /// Tests whether accessors are not in collision with some method (CS0111)
6148 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6150 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6153 public override bool IsUsed
6159 return Get.IsUsed | Set.IsUsed;
6163 protected override void SetMemberName (MemberName new_name)
6165 base.SetMemberName (new_name);
6167 Get.UpdateName (this);
6168 Set.UpdateName (this);
6171 public override string[] ValidAttributeTargets {
6173 return attribute_targets;
6178 // Represents header string for documentation comment.
6180 public override string DocCommentHeader {
6181 get { return "P:"; }
6185 public class Property : PropertyBase {
6186 const int AllowedModifiers =
6189 Modifiers.PROTECTED |
6190 Modifiers.INTERNAL |
6194 Modifiers.OVERRIDE |
6195 Modifiers.ABSTRACT |
6198 Modifiers.METHOD_YIELDS |
6201 const int AllowedInterfaceModifiers =
6204 public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
6205 MemberName name, Attributes attrs, Accessor get_block,
6207 : base (parent, type, mod,
6208 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6209 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs)
6211 if (get_block == null)
6212 Get = new GetMethod (this);
6214 Get = new GetMethod (this, get_block);
6216 if (set_block == null)
6217 Set = new SetMethod (this);
6219 Set = new SetMethod (this, set_block);
6222 public override bool Define ()
6224 if (!base.Define ())
6230 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6233 GetBuilder = Get.Define (Parent);
6234 if (GetBuilder == null)
6238 SetBuilder = Set.Define (Parent);
6240 if (SetBuilder == null)
6244 // FIXME - PropertyAttributes.HasDefault ?
6246 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6247 Name, PropertyAttributes.None, MemberType, null);
6250 PropertyBuilder.SetGetMethod (GetBuilder);
6253 PropertyBuilder.SetSetMethod (SetBuilder);
6255 TypeManager.RegisterProperty (PropertyBuilder, this);
6261 /// Gigantic workaround for lameness in SRE follows :
6262 /// This class derives from EventInfo and attempts to basically
6263 /// wrap around the EventBuilder so that FindMembers can quickly
6264 /// return this in it search for members
6266 public class MyEventBuilder : EventInfo {
6269 // We use this to "point" to our Builder which is
6270 // not really a MemberInfo
6272 EventBuilder MyBuilder;
6275 // We "catch" and wrap these methods
6277 MethodInfo raise, remove, add;
6279 EventAttributes attributes;
6280 Type declaring_type, reflected_type, event_type;
6285 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6287 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6289 // And now store the values in our own fields.
6291 declaring_type = type_builder;
6293 reflected_type = type_builder;
6295 attributes = event_attr;
6298 this.event_type = event_type;
6302 // Methods that you have to override. Note that you only need
6303 // to "implement" the variants that take the argument (those are
6304 // the "abstract" methods, the others (GetAddMethod()) are
6307 public override MethodInfo GetAddMethod (bool nonPublic)
6312 public override MethodInfo GetRemoveMethod (bool nonPublic)
6317 public override MethodInfo GetRaiseMethod (bool nonPublic)
6323 // These methods make "MyEventInfo" look like a Builder
6325 public void SetRaiseMethod (MethodBuilder raiseMethod)
6327 raise = raiseMethod;
6328 MyBuilder.SetRaiseMethod (raiseMethod);
6331 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6333 remove = removeMethod;
6334 MyBuilder.SetRemoveOnMethod (removeMethod);
6337 public void SetAddOnMethod (MethodBuilder addMethod)
6340 MyBuilder.SetAddOnMethod (addMethod);
6343 public void SetCustomAttribute (CustomAttributeBuilder cb)
6345 MyBuilder.SetCustomAttribute (cb);
6348 public override object [] GetCustomAttributes (bool inherit)
6350 // FIXME : There's nothing which can be seemingly done here because
6351 // we have no way of getting at the custom attribute objects of the
6356 public override object [] GetCustomAttributes (Type t, bool inherit)
6358 // FIXME : Same here !
6362 public override bool IsDefined (Type t, bool b)
6367 public override EventAttributes Attributes {
6373 public override string Name {
6379 public override Type DeclaringType {
6381 return declaring_type;
6385 public override Type ReflectedType {
6387 return reflected_type;
6391 public Type EventType {
6397 public void SetUsed ()
6399 if (my_event != null) {
6400 my_event.SetAssigned ();
6401 my_event.SetMemberIsUsed ();
6407 /// For case when event is declared like property (with add and remove accessors).
6409 public class EventProperty: Event {
6411 static string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6413 public EventProperty (DeclSpace parent, Expression type, int mod_flags,
6414 bool is_iface, MemberName name,
6415 Attributes attrs, Accessor add, Accessor remove)
6416 : base (parent, type, mod_flags, is_iface, name, attrs)
6418 Add = new AddDelegateMethod (this, add);
6419 Remove = new RemoveDelegateMethod (this, remove);
6421 // For this event syntax we don't report error CS0067
6422 // because it is hard to do it.
6426 public override string[] ValidAttributeTargets {
6428 return attribute_targets;
6434 /// Event is declared like field.
6436 public class EventField : Event {
6438 static string[] attribute_targets = new string [] { "event", "field", "method" };
6439 static string[] attribute_targets_interface = new string[] { "event", "method" };
6441 public EventField (DeclSpace parent, Expression type, int mod_flags,
6442 bool is_iface, MemberName name,
6444 : base (parent, type, mod_flags, is_iface, name, attrs)
6446 Add = new AddDelegateMethod (this);
6447 Remove = new RemoveDelegateMethod (this);
6450 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6452 if (a.Target == AttributeTargets.Field) {
6453 FieldBuilder.SetCustomAttribute (cb);
6457 if (a.Target == AttributeTargets.Method) {
6458 Add.ApplyAttributeBuilder (a, cb);
6459 Remove.ApplyAttributeBuilder (a, cb);
6463 base.ApplyAttributeBuilder (a, cb);
6466 public override bool Define()
6468 if (!base.Define ())
6471 if (initializer != null) {
6472 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
6473 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
6474 GetSignatureForError ());
6482 public override string[] ValidAttributeTargets {
6484 return IsInterface ? attribute_targets_interface : attribute_targets;
6489 public abstract class Event : FieldBase {
6491 protected sealed class AddDelegateMethod: DelegateMethod
6494 public AddDelegateMethod (Event method):
6495 base (method, "add_")
6499 public AddDelegateMethod (Event method, Accessor accessor):
6500 base (method, accessor, "add_")
6504 protected override MethodInfo DelegateMethodInfo {
6506 return TypeManager.delegate_combine_delegate_delegate;
6512 protected sealed class RemoveDelegateMethod: DelegateMethod
6514 public RemoveDelegateMethod (Event method):
6515 base (method, "remove_")
6519 public RemoveDelegateMethod (Event method, Accessor accessor):
6520 base (method, accessor, "remove_")
6524 protected override MethodInfo DelegateMethodInfo {
6526 return TypeManager.delegate_remove_delegate_delegate;
6532 public abstract class DelegateMethod : AbstractPropertyEventMethod
6534 protected readonly Event method;
6535 ImplicitParameter param_attr;
6537 static string[] attribute_targets = new string [] { "method", "param", "return" };
6539 public DelegateMethod (Event method, string prefix)
6540 : base (method, prefix)
6542 this.method = method;
6545 public DelegateMethod (Event method, Accessor accessor, string prefix)
6546 : base (method, accessor, prefix)
6548 this.method = method;
6551 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6553 if (a.Target == AttributeTargets.Parameter) {
6554 if (param_attr == null)
6555 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6557 param_attr.ApplyAttributeBuilder (a, cb);
6561 base.ApplyAttributeBuilder (a, cb);
6564 public override AttributeTargets AttributeTargets {
6566 return AttributeTargets.Method;
6570 public override bool IsClsComplianceRequired ()
6572 return method.IsClsComplianceRequired ();
6575 public MethodBuilder Define (DeclSpace parent)
6577 method_data = new MethodData (method, method.ModFlags,
6578 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6580 if (!method_data.Define (parent))
6583 MethodBuilder mb = method_data.MethodBuilder;
6584 ParameterInfo.ApplyAttributes (mb);
6589 protected override void EmitMethod (DeclSpace parent)
6591 if (block != null) {
6592 base.EmitMethod (parent);
6596 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
6599 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6600 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6602 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6603 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6604 ig.Emit (OpCodes.Ldsfld, field_info);
6605 ig.Emit (OpCodes.Ldarg_0);
6606 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6607 ig.Emit (OpCodes.Castclass, method.MemberType);
6608 ig.Emit (OpCodes.Stsfld, field_info);
6610 ig.Emit (OpCodes.Ldarg_0);
6611 ig.Emit (OpCodes.Ldarg_0);
6612 ig.Emit (OpCodes.Ldfld, field_info);
6613 ig.Emit (OpCodes.Ldarg_1);
6614 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6615 ig.Emit (OpCodes.Castclass, method.MemberType);
6616 ig.Emit (OpCodes.Stfld, field_info);
6618 ig.Emit (OpCodes.Ret);
6621 protected abstract MethodInfo DelegateMethodInfo { get; }
6623 public override Type ReturnType {
6625 return TypeManager.void_type;
6629 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6631 return new EmitContext (method,
6632 ds, method.Parent, Location, ig, ReturnType,
6633 method.ModFlags, false);
6636 public override ObsoleteAttribute GetObsoleteAttribute ()
6638 return method.GetObsoleteAttribute ();
6641 public override string[] ValidAttributeTargets {
6643 return attribute_targets;
6647 public override Parameters ParameterInfo {
6649 return method.parameters;
6656 const int AllowedModifiers =
6659 Modifiers.PROTECTED |
6660 Modifiers.INTERNAL |
6665 Modifiers.OVERRIDE |
6669 const int AllowedInterfaceModifiers =
6672 public DelegateMethod Add, Remove;
6673 public MyEventBuilder EventBuilder;
6674 public MethodBuilder AddBuilder, RemoveBuilder;
6675 Parameters parameters;
6677 protected Event (DeclSpace parent, Expression type, int mod_flags,
6678 bool is_iface, MemberName name, Attributes attrs)
6679 : base (parent, type, mod_flags,
6680 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6683 IsInterface = is_iface;
6686 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6688 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6689 a.Error_InvalidSecurityParent ();
6693 EventBuilder.SetCustomAttribute (cb);
6696 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6698 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6701 public override AttributeTargets AttributeTargets {
6703 return AttributeTargets.Event;
6707 public override bool Define ()
6709 EventAttributes e_attr;
6710 e_attr = EventAttributes.None;
6715 if (!TypeManager.IsDelegateType (MemberType)) {
6716 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
6720 parameters = new Parameters (
6721 new Parameter[] { new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location) },
6722 new Type[] { MemberType } );
6728 // Now define the accessors
6731 AddBuilder = Add.Define (Parent);
6732 if (AddBuilder == null)
6735 RemoveBuilder = Remove.Define (Parent);
6736 if (RemoveBuilder == null)
6739 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
6741 if (Add.Block == null && Remove.Block == null && !IsInterface) {
6742 FieldBuilder = Parent.TypeBuilder.DefineField (
6744 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
6745 TypeManager.RegisterPrivateFieldOfEvent (
6746 (EventInfo) EventBuilder, FieldBuilder);
6747 TypeManager.RegisterFieldBase (FieldBuilder, this);
6750 EventBuilder.SetAddOnMethod (AddBuilder);
6751 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
6753 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
6757 protected override bool CheckBase ()
6759 if (!base.CheckBase ())
6762 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
6763 if (!(conflict_symbol is EventInfo)) {
6764 Report.SymbolRelatedToPreviousError (conflict_symbol);
6765 Report.Error (72, Location, "Event `{0}' can override only event", GetSignatureForError ());
6773 public override void Emit ()
6775 if (OptAttributes != null) {
6776 OptAttributes.Emit ();
6780 Remove.Emit (Parent);
6785 public override string GetSignatureForError ()
6787 return base.GetSignatureForError ();
6791 // Represents header string for documentation comment.
6793 public override string DocCommentHeader {
6794 get { return "E:"; }
6799 public class Indexer : PropertyBase {
6801 class GetIndexerMethod : GetMethod
6803 public GetIndexerMethod (MethodCore method):
6808 public GetIndexerMethod (MethodCore method, Accessor accessor):
6809 base (method, accessor)
6813 public override Parameters ParameterInfo {
6815 return method.ParameterInfo;
6820 class SetIndexerMethod: SetMethod
6822 public SetIndexerMethod (MethodCore method):
6827 public SetIndexerMethod (MethodCore method, Accessor accessor):
6828 base (method, accessor)
6832 protected override void DefineParameters ()
6834 parameters = Parameters.MergeGenerated (method.Parameters,
6835 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
6839 const int AllowedModifiers =
6842 Modifiers.PROTECTED |
6843 Modifiers.INTERNAL |
6847 Modifiers.OVERRIDE |
6852 const int AllowedInterfaceModifiers =
6856 public Indexer (DeclSpace parent, Expression type, MemberName name, int mod,
6857 bool is_iface, Parameters parameters, Attributes attrs,
6858 Accessor get_block, Accessor set_block)
6859 : base (parent, type, mod,
6860 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6861 is_iface, name, parameters, attrs)
6863 if (get_block == null)
6864 Get = new GetIndexerMethod (this);
6866 Get = new GetIndexerMethod (this, get_block);
6868 if (set_block == null)
6869 Set = new SetIndexerMethod (this);
6871 Set = new SetIndexerMethod (this, set_block);
6874 public override bool Define ()
6876 if (!base.Define ())
6879 if (MemberType == TypeManager.void_type) {
6880 Report.Error (620, Location, "Indexers cannot have void type");
6884 if (OptAttributes != null) {
6885 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
6886 if (indexer_attr != null) {
6887 // Remove the attribute from the list because it is not emitted
6888 OptAttributes.Attrs.Remove (indexer_attr);
6890 string name = indexer_attr.GetIndexerAttributeValue ();
6896 if (IsExplicitImpl) {
6897 Report.Error (415, indexer_attr.Location,
6898 "The `IndexerName' attribute is valid only on an " +
6899 "indexer that is not an explicit interface member declaration");
6903 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6904 Report.Error (609, indexer_attr.Location,
6905 "Cannot set the `IndexerName' attribute on an indexer marked override");
6911 if (InterfaceType != null) {
6912 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
6913 if (base_IndexerName != Name)
6914 ShortName = base_IndexerName;
6917 if (!ParentContainer.AddToMemberContainer (this) ||
6918 !ParentContainer.AddToMemberContainer (Get) || !ParentContainer.AddToMemberContainer (Set))
6924 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6926 GetBuilder = Get.Define (Parent);
6927 if (GetBuilder == null)
6931 SetBuilder = Set.Define (Parent);
6933 if (SetBuilder == null)
6938 // Now name the parameters
6940 Parameter [] p = Parameters.FixedParameters;
6942 // TODO: should be done in parser and it needs to do cycle
6943 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
6944 Report.Error (631, Location, "ref and out are not valid in this context");
6949 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6950 Name, PropertyAttributes.None, MemberType, ParameterTypes);
6953 PropertyBuilder.SetGetMethod (GetBuilder);
6956 PropertyBuilder.SetSetMethod (SetBuilder);
6958 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
6963 public override string GetSignatureForError ()
6965 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
6966 if (MemberName.Left != null) {
6968 sb.Append (MemberName.Left);
6971 sb.Append (".this");
6972 sb.Append (Parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
6973 return sb.ToString ();
6976 public override bool MarkForDuplicationCheck ()
6978 caching_flags |= Flags.TestMethodDuplication;
6983 public class Operator : MethodOrOperator, IIteratorContainer {
6985 const int AllowedModifiers =
6991 public enum OpType : byte {
7001 // Unary and Binary operators
7024 // Implicit and Explicit
7028 // Just because of enum
7032 public readonly OpType OperatorType;
7034 public Operator (DeclSpace parent, OpType type, Expression ret_type,
7035 int mod_flags, Parameters parameters,
7036 ToplevelBlock block, Attributes attrs, Location loc)
7037 : base (parent, ret_type, mod_flags, AllowedModifiers, false,
7038 new MemberName ("op_" + type, loc), attrs, parameters)
7040 OperatorType = type;
7044 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7046 if (a.Type == TypeManager.conditional_attribute_type) {
7047 Error_ConditionalAttributeIsNotValid ();
7051 base.ApplyAttributeBuilder (a, cb);
7054 protected override bool CheckForDuplications ()
7056 ArrayList ar = ParentContainer.Operators;
7058 int arLen = ar.Count;
7060 for (int i = 0; i < arLen; i++) {
7061 Operator o = (Operator) ar [i];
7062 if (IsDuplicateImplementation (o))
7067 ar = ParentContainer.Methods;
7069 int arLen = ar.Count;
7071 for (int i = 0; i < arLen; i++) {
7072 Method m = (Method) ar [i];
7073 if (IsDuplicateImplementation (m))
7081 public override bool Define ()
7083 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7084 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7085 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7089 if (!base.Define ())
7092 if (MemberType == TypeManager.void_type) {
7093 Report.Error (590, Location, "User-defined operators cannot return void");
7097 Type declaring_type = MethodBuilder.DeclaringType;
7098 Type return_type = MemberType;
7099 Type first_arg_type = ParameterTypes [0];
7101 // Rules for conversion operators
7103 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7104 if (first_arg_type == return_type && first_arg_type == declaring_type){
7105 Report.Error (555, Location,
7106 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7110 if (first_arg_type != declaring_type && return_type != declaring_type){
7113 "User-defined conversion must convert to or from the " +
7118 if (first_arg_type.IsInterface || return_type.IsInterface){
7119 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7120 GetSignatureForError ());
7124 if (first_arg_type.IsSubclassOf (return_type) || return_type.IsSubclassOf (first_arg_type)) {
7125 if (declaring_type.IsSubclassOf (return_type) || declaring_type.IsSubclassOf (first_arg_type)) {
7126 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from base class",
7127 GetSignatureForError ());
7130 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from derived class",
7131 GetSignatureForError ());
7134 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7135 if (first_arg_type != declaring_type || ParameterTypes [1] != TypeManager.int32_type) {
7136 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");
7139 } else if (Parameters.Count == 1) {
7140 // Checks for Unary operators
7142 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7143 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7144 Report.Error (448, Location,
7145 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7148 if (first_arg_type != declaring_type) {
7150 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7155 if (first_arg_type != declaring_type){
7158 "The parameter of a unary operator must be the " +
7163 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7164 if (return_type != TypeManager.bool_type){
7167 "The return type of operator True or False " +
7174 // Checks for Binary operators
7176 if (first_arg_type != declaring_type &&
7177 ParameterTypes [1] != declaring_type){
7180 "One of the parameters of a binary operator must " +
7181 "be the containing type");
7189 protected override bool DoDefine ()
7191 if (!base.DoDefine ())
7194 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7198 public override void Emit ()
7202 Parameters.ApplyAttributes (MethodBuilder);
7205 // abstract or extern methods have no bodies
7207 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7211 if ((flags & MethodAttributes.PinvokeImpl) == 0)
7212 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
7214 ec = CreateEmitContext (Parent, null);
7216 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
7217 ec.EmitTopBlock (this, Block);
7220 source.CloseMethod ();
7225 // Operator cannot be override
7226 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7231 public static string GetName (OpType ot)
7234 case OpType.LogicalNot:
7236 case OpType.OnesComplement:
7238 case OpType.Increment:
7240 case OpType.Decrement:
7246 case OpType.Addition:
7248 case OpType.Subtraction:
7250 case OpType.UnaryPlus:
7252 case OpType.UnaryNegation:
7254 case OpType.Multiply:
7256 case OpType.Division:
7258 case OpType.Modulus:
7260 case OpType.BitwiseAnd:
7262 case OpType.BitwiseOr:
7264 case OpType.ExclusiveOr:
7266 case OpType.LeftShift:
7268 case OpType.RightShift:
7270 case OpType.Equality:
7272 case OpType.Inequality:
7274 case OpType.GreaterThan:
7276 case OpType.LessThan:
7278 case OpType.GreaterThanOrEqual:
7280 case OpType.LessThanOrEqual:
7282 case OpType.Implicit:
7284 case OpType.Explicit:
7290 public static OpType GetOperatorType (string name)
7292 if (name.StartsWith ("op_")){
7293 for (int i = 0; i < Unary.oper_names.Length; ++i) {
7294 if (Unary.oper_names [i] == name)
7298 for (int i = 0; i < Binary.oper_names.Length; ++i) {
7299 if (Binary.oper_names [i] == name)
7306 public override string GetSignatureForError ()
7308 StringBuilder sb = new StringBuilder ();
7309 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7310 sb.AppendFormat ("{0}.{1} operator {2}", Parent.GetSignatureForError (), GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type));
7313 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7316 sb.Append (Parameters.GetSignatureForError ());
7317 return sb.ToString ();
7322 // This is used to compare method signatures
7324 struct MethodSignature {
7326 public Type RetType;
7327 public Type [] Parameters;
7330 /// This delegate is used to extract methods which have the
7331 /// same signature as the argument
7333 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7335 public MethodSignature (string name, Type ret_type, Type [] parameters)
7340 if (parameters == null)
7341 Parameters = Type.EmptyTypes;
7343 Parameters = parameters;
7346 public override string ToString ()
7349 if (Parameters.Length != 0){
7350 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7351 for (int i = 0; i < Parameters.Length; i++){
7352 sb.Append (Parameters [i]);
7353 if (i+1 < Parameters.Length)
7356 pars = sb.ToString ();
7359 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7362 public override int GetHashCode ()
7364 return Name.GetHashCode ();
7367 public override bool Equals (Object o)
7369 MethodSignature other = (MethodSignature) o;
7371 if (other.Name != Name)
7374 if (other.RetType != RetType)
7377 if (Parameters == null){
7378 if (other.Parameters == null)
7383 if (other.Parameters == null)
7386 int c = Parameters.Length;
7387 if (other.Parameters.Length != c)
7390 for (int i = 0; i < c; i++)
7391 if (other.Parameters [i] != Parameters [i])
7397 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7399 MethodSignature sig = (MethodSignature) filter_criteria;
7401 if (m.Name != sig.Name)
7405 MethodInfo mi = m as MethodInfo;
7406 PropertyInfo pi = m as PropertyInfo;
7409 ReturnType = mi.ReturnType;
7410 else if (pi != null)
7411 ReturnType = pi.PropertyType;
7416 // we use sig.RetType == null to mean `do not check the
7417 // method return value.
7419 if (sig.RetType != null)
7420 if (ReturnType != sig.RetType)
7425 args = TypeManager.GetParameterData (mi).Types;
7427 args = TypeManager.GetArgumentTypes (pi);
7428 Type [] sigp = sig.Parameters;
7430 if (args.Length != sigp.Length)
7433 for (int i = args.Length; i > 0; ){
7435 if (args [i] != sigp [i])