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 ArrayList partial_parts;
478 /// The pending methods that need to be implemented
479 // (interfaces or abstract methods)
481 PendingImplementation pending;
483 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
484 Attributes attrs, Kind kind)
485 : base (ns, parent, name, attrs)
487 if (parent != null && parent.NamespaceEntry != ns)
488 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
491 this.PartialContainer = this;
494 public bool AddMember (MemberCore symbol)
496 return AddToContainer (symbol, symbol.Name);
499 protected virtual bool AddMemberType (DeclSpace ds)
501 return AddToContainer (ds, ds.Basename);
504 public void AddConstant (Const constant)
506 if (!AddMember (constant))
509 if (constants == null)
510 constants = new MemberCoreArrayList ();
512 constants.Add (constant);
515 public void AddEnum (Mono.CSharp.Enum e)
517 if (!AddMemberType (e))
521 enums = new MemberCoreArrayList ();
526 public TypeContainer AddTypeContainer (TypeContainer tc, bool is_interface)
528 if (!AddMemberType (tc))
532 if (interfaces == null)
533 interfaces = new MemberCoreArrayList ();
537 types = new ArrayList (2);
543 public virtual TypeContainer AddPartial (TypeContainer nextPart, bool is_interface)
545 return AddPartial (nextPart, nextPart.Basename, is_interface);
548 protected TypeContainer AddPartial (TypeContainer nextPart, string name, bool is_interface)
550 nextPart.ModFlags |= Modifiers.PARTIAL;
551 TypeContainer tc = defined_names [name] as TypeContainer;
554 return AddTypeContainer (nextPart, is_interface);
556 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
557 Report.SymbolRelatedToPreviousError (tc);
558 Error_MissingPartialModifier (nextPart);
562 if (tc.Kind != nextPart.Kind) {
563 Report.SymbolRelatedToPreviousError (tc);
564 Report.Error (261, nextPart.Location,
565 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
566 nextPart.GetSignatureForError ());
570 if ((tc.ModFlags & Modifiers.Accessibility) != (nextPart.ModFlags & Modifiers.Accessibility) &&
571 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
572 (nextPart.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
573 Report.SymbolRelatedToPreviousError (tc);
574 Report.Error (262, nextPart.Location,
575 "Partial declarations of `{0}' have conflicting accessibility modifiers",
576 nextPart.GetSignatureForError ());
580 if (tc.partial_parts == null)
581 tc.partial_parts = new ArrayList (1);
583 tc.ModFlags |= nextPart.ModFlags;
584 if (nextPart.attributes != null) {
585 if (tc.attributes == null)
586 tc.attributes = nextPart.attributes;
588 tc.attributes.AddAttributes (nextPart.attributes.Attrs);
591 nextPart.PartialContainer = tc;
592 tc.partial_parts.Add (nextPart);
596 public void AddDelegate (Delegate d)
598 if (!AddMemberType (d))
601 if (delegates == null)
602 delegates = new MemberCoreArrayList ();
607 public void AddMethod (Method method)
609 if (!AddMember (method))
613 methods = new MethodArrayList (this);
615 if (method.MemberName.Left != null)
616 methods.Insert (0, method);
618 methods.Add (method);
622 // Do not use this method: use AddMethod.
624 // This is only used by iterators.
626 public void AppendMethod (Method method)
628 if (!AddMember (method))
632 methods = new MethodArrayList (this);
634 methods.Add (method);
637 public void AddConstructor (Constructor c)
639 if (c.Name != Basename) {
640 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
643 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
646 if (default_static_constructor != null) {
647 Report.SymbolRelatedToPreviousError (default_static_constructor);
648 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
652 default_static_constructor = c;
654 if (c.Parameters.Empty){
655 if (default_constructor != null) {
656 Report.SymbolRelatedToPreviousError (default_constructor);
657 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
660 default_constructor = c;
663 if (instance_constructors == null)
664 instance_constructors = new MemberCoreArrayList ();
666 instance_constructors.Add (c);
670 internal static string Error111 {
672 return "`{0}' is already defined. Rename this member or use different parameter types";
676 public void AddField (FieldMember field)
678 if (!AddMember (field))
682 fields = new MemberCoreArrayList ();
686 if ((field.ModFlags & Modifiers.STATIC) != 0)
689 if (first_nonstatic_field == null) {
690 first_nonstatic_field = field;
694 if (Kind == Kind.Struct &&
695 first_nonstatic_field.Parent != field.Parent &&
696 RootContext.WarningLevel >= 3) {
697 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
698 Report.Warning (282, 3, field.Location,
699 "struct instance field `{0}' found in different declaration from instance field `{1}'",
700 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
704 public void AddProperty (Property prop)
706 if (!AddMember (prop) ||
707 !AddMember (prop.Get) || !AddMember (prop.Set))
710 if (properties == null)
711 properties = new MemberCoreArrayList ();
713 if (prop.MemberName.Left != null)
714 properties.Insert (0, prop);
716 properties.Add (prop);
719 public void AddEvent (Event e)
724 if (e is EventProperty) {
725 if (!AddMember (e.Add))
728 if (!AddMember (e.Remove))
733 events = new MemberCoreArrayList ();
739 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
741 public void AddIndexer (Indexer i)
743 if (indexers == null)
744 indexers = new IndexerArrayList (this);
746 if (i.IsExplicitImpl)
747 indexers.Insert (0, i);
752 public void AddOperator (Operator op)
757 if (operators == null)
758 operators = new OperatorArrayList (this);
763 public void AddIterator (Iterator i)
765 if (iterators == null)
766 iterators = new ArrayList ();
771 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
773 if (a.Type == TypeManager.default_member_type) {
774 if (Indexers != null) {
775 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
780 base.ApplyAttributeBuilder (a, cb);
783 public override AttributeTargets AttributeTargets {
785 throw new NotSupportedException ();
789 public ArrayList Types {
795 public MethodArrayList Methods {
801 public ArrayList Constants {
807 public ArrayList Interfaces {
813 public ArrayList Iterators {
819 protected Type BaseType {
821 return TypeBuilder.BaseType;
825 public ArrayList Bases {
835 public ArrayList Fields {
841 public ArrayList InstanceConstructors {
843 return instance_constructors;
847 public ArrayList Properties {
853 public ArrayList Events {
859 public ArrayList Enums {
865 public ArrayList Indexers {
871 public ArrayList Operators {
877 public ArrayList Delegates {
883 protected override TypeAttributes TypeAttr {
885 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
889 public string IndexerName {
891 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
895 public bool IsComImport {
897 if (OptAttributes == null)
900 return OptAttributes.Contains (TypeManager.comimport_attr_type);
904 public virtual void RegisterFieldForInitialization (FieldBase field)
906 if ((field.ModFlags & Modifiers.STATIC) != 0){
907 if (initialized_static_fields == null)
908 initialized_static_fields = new ArrayList (4);
910 initialized_static_fields.Add (field);
912 if (initialized_fields == null)
913 initialized_fields = new ArrayList (4);
915 initialized_fields.Add (field);
920 // Emits the instance field initializers
922 public bool EmitFieldInitializers (EmitContext ec)
927 fields = initialized_static_fields;
929 fields = initialized_fields;
935 foreach (FieldBase f in fields) {
936 f.EmitInitializer (ec);
941 public override string DocComment {
953 public PendingImplementation PendingImplementations {
954 get { return pending; }
957 public override bool GetClsCompliantAttributeValue ()
959 if (PartialContainer != this)
960 return PartialContainer.GetClsCompliantAttributeValue ();
962 return base.GetClsCompliantAttributeValue ();
965 public void AddBasesForPart (DeclSpace part, ArrayList bases)
967 // FIXME: get rid of partial_parts and store lists of bases of each part here
968 // assumed, not verified: 'part' is in 'partial_parts'
969 ((TypeContainer) part).Bases = bases;
972 TypeExpr[] GetNormalBases (out TypeExpr base_class)
978 int count = Bases.Count;
981 if (Kind == Kind.Class){
982 TypeExpr name = ((Expression) Bases [0]).ResolveAsBaseTerminal (this, false);
988 if (!name.IsInterface) {
989 // base_class could be a class, struct, enum, delegate.
990 // This is validated in GetClassBases.
996 TypeExpr [] ifaces = new TypeExpr [count-start];
998 for (i = start, j = 0; i < count; i++, j++){
999 TypeExpr resolved = ((Expression) Bases [i]).ResolveAsTypeTerminal (this, false);
1000 if (resolved == null) {
1004 ifaces [j] = resolved;
1007 return ifaces.Length == 0 ? null : ifaces;
1011 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
1013 ArrayList ifaces = new ArrayList (0);
1014 if (iface_exprs != null)
1015 ifaces.AddRange (iface_exprs);
1017 foreach (TypeContainer part in partial_parts) {
1018 TypeExpr new_base_class;
1019 TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
1020 if (new_base_class != TypeManager.system_object_expr) {
1021 if (base_class == TypeManager.system_object_expr)
1022 base_class = new_base_class;
1024 if (new_base_class != null && !new_base_class.Equals (base_class)) {
1025 Report.SymbolRelatedToPreviousError (base_class.Location, "");
1026 Report.Error (263, part.Location,
1027 "Partial declarations of `{0}' must not specify different base classes",
1028 part.GetSignatureForError ());
1035 if (new_ifaces == null)
1038 foreach (TypeExpr iface in new_ifaces) {
1039 if (ifaces.Contains (iface))
1046 if (ifaces.Count == 0)
1049 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
1053 /// This function computes the Base class and also the
1054 /// list of interfaces that the class or struct @c implements.
1056 /// The return value is an array (might be null) of
1057 /// interfaces implemented (as Types).
1059 /// The @base_class argument is set to the base object or null
1060 /// if this is `System.Object'.
1062 public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
1064 TypeExpr[] ifaces = GetNormalBases (out base_class);
1069 int count = ifaces.Length;
1071 for (int i = 0; i < count; i++) {
1072 TypeExpr iface = (TypeExpr) ifaces [i];
1074 if (!iface.IsInterface) {
1075 if (Kind != Kind.Class) {
1076 // TODO: location of symbol related ....
1077 Error_TypeInListIsNotInterface (Location, iface.FullName);
1079 else if (base_class != null)
1080 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
1081 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
1083 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
1084 GetSignatureForError (), iface.GetSignatureForError ());
1089 for (int x = 0; x < i; x++) {
1090 if (iface.Equals (ifaces [x])) {
1091 Report.Error (528, Location,
1092 "`{0}' is already listed in " +
1093 "interface list", iface.GetSignatureForError ());
1098 if ((Kind == Kind.Interface) &&
1099 !iface.AsAccessible (Parent, ModFlags)) {
1100 Report.Error (61, Location,
1101 "Inconsistent accessibility: base " +
1102 "interface `{0}' is less accessible " +
1103 "than interface `{1}'", iface.Name,
1113 protected void Error_TypeInListIsNotInterface (Location loc, string type)
1115 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
1118 bool DefineTypeBuilder ()
1122 if (TypeManager.NamespaceClash (Name, Location)) {
1126 ModuleBuilder builder = CodeGen.Module.Builder;
1127 TypeBuilder = builder.DefineType (
1128 Name, TypeAttr, null, null);
1130 TypeBuilder builder = Parent.TypeBuilder;
1132 TypeBuilder = builder.DefineNestedType (
1133 Basename, TypeAttr, null, null);
1135 } catch (ArgumentException) {
1136 Report.RuntimeMissingSupport (Location, "static classes");
1140 TypeManager.AddUserType (this);
1142 iface_exprs = GetClassBases (out base_type);
1143 if (partial_parts != null) {
1144 iface_exprs = GetNormalPartialBases (ref base_type);
1148 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1149 // which in turn should have called DefineType()s on base types if necessary.
1151 // None of the code below should trigger DefineType()s on classes that we depend on.
1152 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1154 // Let's do it as soon as possible, since code below can call DefineType() on classes
1155 // that depend on us to be populated before they are.
1157 if (!(this is Iterator))
1158 RootContext.RegisterOrder (this);
1161 if (base_type != null) {
1162 TypeBuilder.SetParent (base_type.Type);
1164 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1165 if (obsolete_attr != null && !IsInObsoleteScope) {
1166 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1170 if (!CheckRecursiveDefinition (this)) {
1174 // add interfaces that were not added at type creation
1175 if (iface_exprs != null) {
1176 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1177 if (ifaces == null) {
1181 foreach (Type itype in ifaces)
1182 TypeBuilder.AddInterfaceImplementation (itype);
1184 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1191 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1193 public override TypeBuilder DefineType ()
1195 if (TypeBuilder != null)
1201 if (!DefineTypeBuilder ()) {
1206 if (partial_parts != null) {
1207 foreach (TypeContainer part in partial_parts)
1208 part.TypeBuilder = TypeBuilder;
1211 DefineNestedTypes ();
1216 protected virtual bool DefineNestedTypes ()
1218 if (Interfaces != null) {
1219 foreach (TypeContainer iface in Interfaces)
1220 if (iface.DefineType () == null)
1224 if (Types != null) {
1225 foreach (TypeContainer tc in Types)
1226 if (tc.DefineType () == null)
1230 if (Delegates != null) {
1231 foreach (Delegate d in Delegates)
1232 if (d.DefineType () == null)
1236 if (Enums != null) {
1237 foreach (Enum en in Enums)
1238 if (en.DefineType () == null)
1245 TypeContainer InTransit;
1247 protected bool CheckRecursiveDefinition (TypeContainer tc)
1249 if (InTransit != null) {
1250 Report.SymbolRelatedToPreviousError (this);
1251 if (this is Interface)
1253 529, tc.Location, "Inherited interface `{0}' causes a " +
1254 "cycle in the interface hierarchy of `{1}'",
1255 GetSignatureForError (), tc.GetSignatureForError ());
1258 146, tc.Location, "Circular base class dependency " +
1259 "involving `{0}' and `{1}'",
1260 tc.GetSignatureForError (), GetSignatureForError ());
1266 if (BaseType != null) {
1267 TypeContainer ptc = TypeManager.LookupTypeContainer (BaseType);
1268 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1272 if (iface_exprs != null) {
1273 foreach (TypeExpr iface in iface_exprs) {
1274 Type itype = iface.Type;
1276 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1277 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1282 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1289 public static void Error_KeywordNotAllowed (Location loc)
1291 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1295 /// Populates our TypeBuilder with fields and methods
1297 public override bool DefineMembers ()
1299 if (members_defined)
1300 return members_defined_ok;
1302 if (!base.DefineMembers ())
1305 members_defined_ok = DoDefineMembers ();
1306 members_defined = true;
1308 return members_defined_ok;
1311 protected virtual bool DoDefineMembers ()
1314 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1315 if (conflict_symbol == null) {
1316 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1317 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1319 if ((ModFlags & Modifiers.NEW) == 0) {
1320 Report.SymbolRelatedToPreviousError (conflict_symbol);
1321 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1322 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1327 DefineContainerMembers (constants);
1328 DefineContainerMembers (fields);
1330 if (Kind == Kind.Struct || Kind == Kind.Class) {
1331 pending = PendingImplementation.GetPendingImplementations (this);
1335 // Constructors are not in the defined_names array
1337 DefineContainerMembers (instance_constructors);
1339 DefineContainerMembers (properties);
1340 DefineContainerMembers (events);
1341 DefineContainerMembers (indexers);
1342 DefineContainerMembers (methods);
1343 DefineContainerMembers (operators);
1344 DefineContainerMembers (enums);
1345 DefineContainerMembers (delegates);
1348 member_cache = new MemberCache (this);
1349 if (partial_parts != null) {
1350 foreach (TypeContainer part in partial_parts)
1351 part.member_cache = member_cache;
1354 if (iterators != null) {
1355 foreach (Iterator iterator in iterators) {
1356 if (iterator.DefineType () == null)
1360 foreach (Iterator iterator in iterators) {
1361 if (!iterator.DefineMembers ())
1369 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1372 mcal.DefineContainerMembers ();
1375 public override bool Define ()
1377 if (iterators != null) {
1378 foreach (Iterator iterator in iterators) {
1379 if (!iterator.Define ())
1387 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1389 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1393 /// This function is based by a delegate to the FindMembers routine
1395 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1401 /// This filter is used by FindMembers, and we just keep
1402 /// a global for the filter to `AlwaysAccept'
1404 static MemberFilter accepting_filter;
1407 static TypeContainer ()
1409 accepting_filter = new MemberFilter (AlwaysAccept);
1412 public MethodInfo[] GetMethods ()
1414 ArrayList members = new ArrayList ();
1418 if (methods != null) {
1419 int len = methods.Count;
1420 for (int i = 0; i < len; i++) {
1421 Method m = (Method) methods [i];
1423 members.Add (m.MethodBuilder);
1427 if (operators != null) {
1428 int len = operators.Count;
1429 for (int i = 0; i < len; i++) {
1430 Operator o = (Operator) operators [i];
1432 members.Add (o.MethodBuilder);
1436 if (properties != null) {
1437 int len = properties.Count;
1438 for (int i = 0; i < len; i++) {
1439 Property p = (Property) properties [i];
1441 if (p.GetBuilder != null)
1442 members.Add (p.GetBuilder);
1443 if (p.SetBuilder != null)
1444 members.Add (p.SetBuilder);
1448 if (indexers != null) {
1449 int len = indexers.Count;
1450 for (int i = 0; i < len; i++) {
1451 Indexer ix = (Indexer) indexers [i];
1453 if (ix.GetBuilder != null)
1454 members.Add (ix.GetBuilder);
1455 if (ix.SetBuilder != null)
1456 members.Add (ix.SetBuilder);
1460 if (events != null) {
1461 int len = events.Count;
1462 for (int i = 0; i < len; i++) {
1463 Event e = (Event) events [i];
1465 if (e.AddBuilder != null)
1466 members.Add (e.AddBuilder);
1467 if (e.RemoveBuilder != null)
1468 members.Add (e.RemoveBuilder);
1472 MethodInfo[] retMethods = new MethodInfo [members.Count];
1473 members.CopyTo (retMethods, 0);
1477 // Indicated whether container has StructLayout attribute set Explicit
1478 public bool HasExplicitLayout {
1479 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1480 set { caching_flags |= Flags.HasExplicitLayout; }
1484 // Return the nested type with name @name. Ensures that the nested type
1485 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1487 public Type FindNestedType (string name)
1489 if (PartialContainer != this)
1490 throw new InternalErrorException ("should not happen");
1492 ArrayList [] lists = { types, enums, delegates, interfaces };
1494 for (int j = 0; j < lists.Length; ++j) {
1495 ArrayList list = lists [j];
1499 int len = list.Count;
1500 for (int i = 0; i < len; ++i) {
1501 DeclSpace ds = (DeclSpace) list [i];
1502 if (ds.Basename == name) {
1503 return ds.DefineType ();
1511 private void FindMembers_NestedTypes (int modflags,
1512 BindingFlags bf, MemberFilter filter, object criteria,
1513 ref ArrayList members)
1515 ArrayList [] lists = { types, enums, delegates, interfaces };
1517 for (int j = 0; j < lists.Length; ++j) {
1518 ArrayList list = lists [j];
1522 int len = list.Count;
1523 for (int i = 0; i < len; i++) {
1524 DeclSpace ds = (DeclSpace) list [i];
1526 if ((ds.ModFlags & modflags) == 0)
1529 TypeBuilder tb = ds.TypeBuilder;
1531 if (!(criteria is string) || ds.Basename.Equals (criteria))
1532 tb = ds.DefineType ();
1535 if (tb != null && (filter (tb, criteria) == true)) {
1536 if (members == null)
1537 members = new ArrayList ();
1546 /// This method returns the members of this type just like Type.FindMembers would
1547 /// Only, we need to use this for types which are _being_ defined because MS'
1548 /// implementation can't take care of that.
1551 // FIXME: return an empty static array instead of null, that cleans up
1552 // some code and is consistent with some coding conventions I just found
1556 // Notice that in various cases we check if our field is non-null,
1557 // something that would normally mean that there was a bug elsewhere.
1559 // The problem happens while we are defining p-invoke methods, as those
1560 // will trigger a FindMembers, but this happens before things are defined
1562 // Since the whole process is a no-op, it is fine to check for null here.
1564 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1565 MemberFilter filter, object criteria)
1567 ArrayList members = null;
1570 if ((bf & BindingFlags.Public) != 0)
1571 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1573 if ((bf & BindingFlags.NonPublic) != 0)
1574 modflags |= Modifiers.PRIVATE;
1576 int static_mask = 0, static_flags = 0;
1577 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1578 case BindingFlags.Static:
1579 static_mask = static_flags = Modifiers.STATIC;
1582 case BindingFlags.Instance:
1583 static_mask = Modifiers.STATIC;
1588 static_mask = static_flags = 0;
1592 Timer.StartTimer (TimerType.TcFindMembers);
1595 filter = accepting_filter;
1597 if ((mt & MemberTypes.Field) != 0) {
1598 if (fields != null) {
1599 int len = fields.Count;
1600 for (int i = 0; i < len; i++) {
1601 FieldMember f = (FieldMember) fields [i];
1603 if ((f.ModFlags & modflags) == 0)
1605 if ((f.ModFlags & static_mask) != static_flags)
1608 FieldBuilder fb = f.FieldBuilder;
1609 if (fb != null && filter (fb, criteria) == true) {
1610 if (members == null)
1611 members = new ArrayList ();
1618 if (constants != null) {
1619 int len = constants.Count;
1620 for (int i = 0; i < len; i++) {
1621 Const con = (Const) constants [i];
1623 if ((con.ModFlags & modflags) == 0)
1625 if ((con.ModFlags & static_mask) != static_flags)
1628 FieldBuilder fb = con.FieldBuilder;
1631 fb = con.FieldBuilder;
1633 if (fb != null && filter (fb, criteria) == true) {
1634 if (members == null)
1635 members = new ArrayList ();
1643 if ((mt & MemberTypes.Method) != 0) {
1644 if (methods != null) {
1645 int len = methods.Count;
1646 for (int i = 0; i < len; i++) {
1647 Method m = (Method) methods [i];
1649 if ((m.ModFlags & modflags) == 0)
1651 if ((m.ModFlags & static_mask) != static_flags)
1654 MethodBuilder mb = m.MethodBuilder;
1656 if (mb != null && filter (mb, criteria) == true) {
1657 if (members == null)
1658 members = new ArrayList ();
1665 if (operators != null) {
1666 int len = operators.Count;
1667 for (int i = 0; i < len; i++) {
1668 Operator o = (Operator) operators [i];
1670 if ((o.ModFlags & modflags) == 0)
1672 if ((o.ModFlags & static_mask) != static_flags)
1675 MethodBuilder ob = o.MethodBuilder;
1676 if (ob != null && filter (ob, criteria) == true) {
1677 if (members == null)
1678 members = new ArrayList ();
1685 if (events != null) {
1686 foreach (Event e in events) {
1687 if ((e.ModFlags & modflags) == 0)
1689 if ((e.ModFlags & static_mask) != static_flags)
1692 MethodBuilder b = e.AddBuilder;
1693 if (b != null && filter (b, criteria)) {
1694 if (members == null)
1695 members = new ArrayList (4);
1700 b = e.RemoveBuilder;
1701 if (b != null && filter (b, criteria)) {
1702 if (members == null)
1703 members = new ArrayList (4);
1710 if (properties != null) {
1711 int len = properties.Count;
1712 for (int i = 0; i < len; i++) {
1713 Property p = (Property) properties [i];
1715 if ((p.ModFlags & modflags) == 0)
1717 if ((p.ModFlags & static_mask) != static_flags)
1723 if (b != null && filter (b, criteria) == true) {
1724 if (members == null)
1725 members = new ArrayList ();
1731 if (b != null && filter (b, criteria) == true) {
1732 if (members == null)
1733 members = new ArrayList ();
1740 if (indexers != null) {
1741 int len = indexers.Count;
1742 for (int i = 0; i < len; i++) {
1743 Indexer ix = (Indexer) indexers [i];
1745 if ((ix.ModFlags & modflags) == 0)
1747 if ((ix.ModFlags & static_mask) != static_flags)
1753 if (b != null && filter (b, criteria) == true) {
1754 if (members == null)
1755 members = new ArrayList ();
1761 if (b != null && filter (b, criteria) == true) {
1762 if (members == null)
1763 members = new ArrayList ();
1771 if ((mt & MemberTypes.Event) != 0) {
1772 if (events != null) {
1773 int len = events.Count;
1774 for (int i = 0; i < len; i++) {
1775 Event e = (Event) events [i];
1777 if ((e.ModFlags & modflags) == 0)
1779 if ((e.ModFlags & static_mask) != static_flags)
1782 MemberInfo eb = e.EventBuilder;
1783 if (eb != null && filter (eb, criteria) == true) {
1784 if (members == null)
1785 members = new ArrayList ();
1787 members.Add (e.EventBuilder);
1793 if ((mt & MemberTypes.Property) != 0){
1794 if (properties != null) {
1795 int len = properties.Count;
1796 for (int i = 0; i < len; i++) {
1797 Property p = (Property) properties [i];
1799 if ((p.ModFlags & modflags) == 0)
1801 if ((p.ModFlags & static_mask) != static_flags)
1804 MemberInfo pb = p.PropertyBuilder;
1805 if (pb != null && filter (pb, criteria) == true) {
1806 if (members == null)
1807 members = new ArrayList ();
1809 members.Add (p.PropertyBuilder);
1814 if (indexers != null) {
1815 int len = indexers.Count;
1816 for (int i = 0; i < len; i++) {
1817 Indexer ix = (Indexer) indexers [i];
1819 if ((ix.ModFlags & modflags) == 0)
1821 if ((ix.ModFlags & static_mask) != static_flags)
1824 MemberInfo ib = ix.PropertyBuilder;
1825 if (ib != null && filter (ib, criteria) == true) {
1826 if (members == null)
1827 members = new ArrayList ();
1829 members.Add (ix.PropertyBuilder);
1835 if ((mt & MemberTypes.NestedType) != 0)
1836 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1838 if ((mt & MemberTypes.Constructor) != 0){
1839 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1840 int len = instance_constructors.Count;
1841 for (int i = 0; i < len; i++) {
1842 Constructor c = (Constructor) instance_constructors [i];
1844 ConstructorBuilder cb = c.ConstructorBuilder;
1845 if (cb != null && filter (cb, criteria) == true) {
1846 if (members == null)
1847 members = new ArrayList ();
1854 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1855 ConstructorBuilder cb =
1856 default_static_constructor.ConstructorBuilder;
1858 if (cb != null && filter (cb, criteria) == true) {
1859 if (members == null)
1860 members = new ArrayList ();
1868 // Lookup members in base if requested.
1870 if ((bf & BindingFlags.DeclaredOnly) == 0) {
1871 if (TypeBuilder.BaseType != null) {
1872 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1873 if (list.Count > 0) {
1874 if (members == null)
1875 members = new ArrayList ();
1877 members.AddRange (list);
1880 if (ifaces != null) {
1881 foreach (Type base_type in ifaces) {
1882 MemberList list = TypeContainer.FindMembers (base_type, mt, bf, filter, criteria);
1884 if (list.Count > 0) {
1885 if (members == null)
1886 members = new ArrayList ();
1887 members.AddRange (list);
1893 Timer.StopTimer (TimerType.TcFindMembers);
1895 if (members == null)
1896 return MemberList.Empty;
1898 return new MemberList (members);
1901 public override MemberCache MemberCache {
1903 return member_cache;
1907 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1908 MemberFilter filter, object criteria)
1910 DeclSpace ds = TypeManager.LookupDeclSpace (t);
1913 return ds.FindMembers (mt, bf, filter, criteria);
1915 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1919 /// Emits the values for the constants
1921 public void EmitConstants ()
1923 if (constants != null)
1924 foreach (Const con in constants)
1929 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
1934 foreach (MemberCore mc in al) {
1935 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
1939 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
1944 public virtual void VerifyMembers ()
1947 // Check for internal or private fields that were never assigned
1949 if (RootContext.WarningLevel >= 3) {
1950 CheckMemberUsage (properties, "property");
1951 CheckMemberUsage (methods, "method");
1952 CheckMemberUsage (constants, "constant");
1954 if (fields != null){
1955 foreach (FieldMember f in fields) {
1956 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
1960 if ((f.caching_flags & Flags.IsAssigned) == 0)
1961 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
1964 const int error_code = 414;
1966 const int error_code = 169;
1968 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
1969 f.GetSignatureForError ());
1975 // Only report 649 on level 4
1977 if (RootContext.WarningLevel < 4)
1980 if ((f.caching_flags & Flags.IsAssigned) != 0)
1983 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
1984 f.GetSignatureForError (), f.Type.Type.IsValueType ? Activator.CreateInstance (f.Type.Type).ToString() : "null");
1990 // TODO: move to ClassOrStruct
1991 void EmitConstructors ()
1993 if (instance_constructors == null)
1996 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
1997 bool has_compliant_args = false;
1999 foreach (Constructor c in instance_constructors) {
2002 if (has_compliant_args)
2005 has_compliant_args = c.HasCompliantArgs;
2007 if (!has_compliant_args)
2008 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2010 foreach (Constructor c in instance_constructors)
2016 /// Emits the code, this step is performed after all
2017 /// the types, enumerations, constructors
2019 public virtual void EmitType ()
2021 if (OptAttributes != null)
2022 OptAttributes.Emit ();
2025 // Structs with no fields need to have at least one byte.
2026 // The right thing would be to set the PackingSize in a DefineType
2027 // but there are no functions that allow interfaces *and* the size to
2031 if (Kind == Kind.Struct && first_nonstatic_field == null){
2032 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2033 FieldAttributes.Private);
2035 if (HasExplicitLayout){
2036 object [] ctor_args = new object [1];
2039 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2040 TypeManager.field_offset_attribute_ctor, ctor_args);
2041 fb.SetCustomAttribute (cba);
2047 EmitConstructors ();
2049 // Can not continue if constants are broken
2051 if (Report.Errors > 0)
2054 if (default_static_constructor != null)
2055 default_static_constructor.Emit ();
2057 if (methods != null){
2058 foreach (Method m in methods)
2062 if (operators != null)
2063 foreach (Operator o in operators)
2066 if (properties != null)
2067 foreach (Property p in properties)
2070 if (indexers != null){
2075 foreach (FieldMember f in fields)
2078 if (events != null){
2079 foreach (Event e in Events)
2083 if (delegates != null) {
2084 foreach (Delegate d in Delegates) {
2089 if (enums != null) {
2090 foreach (Enum e in enums) {
2095 if (pending != null)
2096 if (pending.VerifyPendingMethods ())
2099 if (iterators != null)
2100 foreach (Iterator iterator in iterators)
2101 iterator.EmitType ();
2104 public override void CloseType ()
2106 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2110 caching_flags |= Flags.CloseTypeCreated;
2111 TypeBuilder.CreateType ();
2112 } catch (TypeLoadException){
2114 // This is fine, the code still created the type
2116 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2117 // Console.WriteLine (e.Message);
2119 Console.WriteLine ("In type: " + Name);
2124 foreach (Enum en in Enums)
2128 foreach (TypeContainer tc in Types)
2129 if (tc.Kind == Kind.Struct)
2132 foreach (TypeContainer tc in Types)
2133 if (tc.Kind != Kind.Struct)
2137 if (Delegates != null)
2138 foreach (Delegate d in Delegates)
2141 if (Iterators != null)
2142 foreach (Iterator i in Iterators)
2150 initialized_fields = null;
2151 initialized_static_fields = null;
2159 default_constructor = null;
2160 default_static_constructor = null;
2162 OptAttributes = null;
2165 member_cache = null;
2169 // Performs the validation on a Method's modifiers (properties have
2170 // the same properties).
2172 public bool MethodModifiersValid (MemberCore mc)
2174 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2175 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2176 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2178 int flags = mc.ModFlags;
2181 // At most one of static, virtual or override
2183 if ((flags & Modifiers.STATIC) != 0){
2184 if ((flags & vao) != 0){
2185 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2186 mc.GetSignatureForError ());
2191 if (Kind == Kind.Struct){
2192 if ((flags & va) != 0){
2193 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2198 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2199 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2200 mc.GetSignatureForError ());
2205 // If the declaration includes the abstract modifier, then the
2206 // declaration does not include static, virtual or extern
2208 if ((flags & Modifiers.ABSTRACT) != 0){
2209 if ((flags & Modifiers.EXTERN) != 0){
2211 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2215 if ((flags & Modifiers.SEALED) != 0) {
2216 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2220 if ((flags & Modifiers.VIRTUAL) != 0){
2221 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2225 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2226 Report.Error (513, mc.Location, "`{0}' is abstract but it is contained in nonabstract class", mc.GetSignatureForError ());
2231 if ((flags & Modifiers.PRIVATE) != 0){
2232 if ((flags & vao) != 0){
2233 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2238 if ((flags & Modifiers.SEALED) != 0){
2239 if ((flags & Modifiers.OVERRIDE) == 0){
2240 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2248 public Constructor DefaultStaticConstructor {
2249 get { return default_static_constructor; }
2252 protected override bool VerifyClsCompliance ()
2254 if (!base.VerifyClsCompliance ())
2259 Type base_type = TypeBuilder.BaseType;
2260 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2261 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2264 if (!Parent.IsClsComplianceRequired ()) {
2265 Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
2266 GetSignatureForError (), Parent.GetSignatureForError ());
2273 /// Checks whether container name is CLS Compliant
2275 void VerifyClsName ()
2277 Hashtable base_members = base_cache == null ?
2279 base_cache.GetPublicMembers ();
2280 Hashtable this_members = new Hashtable ();
2282 foreach (DictionaryEntry entry in defined_names) {
2283 MemberCore mc = (MemberCore)entry.Value;
2284 if (!mc.IsClsComplianceRequired ())
2287 string name = (string) entry.Key;
2288 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2290 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2291 object found = base_members [lcase];
2292 if (found == null) {
2293 found = this_members [lcase];
2294 if (found == null) {
2295 this_members.Add (lcase, mc);
2300 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2303 if (found is MemberInfo) {
2304 if (basename == ((MemberInfo) found).Name)
2306 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2308 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2310 Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2316 /// Performs checks for an explicit interface implementation. First it
2317 /// checks whether the `interface_type' is a base inteface implementation.
2318 /// Then it checks whether `name' exists in the interface type.
2320 public virtual bool VerifyImplements (MemberBase mb)
2322 if (ifaces != null) {
2323 foreach (Type t in ifaces){
2324 if (t == mb.InterfaceType)
2329 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2330 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2331 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2335 public void Mark_HasEquals ()
2337 Methods.HasEquals = true;
2340 public void Mark_HasGetHashCode ()
2342 Methods.HasGetHashCode = true;
2349 string IMemberContainer.Name {
2355 Type IMemberContainer.Type {
2361 MemberCache IMemberContainer.MemberCache {
2363 return member_cache;
2367 bool IMemberContainer.IsInterface {
2369 return Kind == Kind.Interface;
2373 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2375 return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
2379 // Generates xml doc comments (if any), and if required,
2380 // handle warning report.
2382 internal override void GenerateDocComment (DeclSpace ds)
2384 DocUtil.GenerateTypeDocComment (this, ds);
2387 public override string DocCommentHeader {
2388 get { return "T:"; }
2391 public virtual MemberCache BaseCache {
2393 if (base_cache != null)
2395 if (TypeBuilder.BaseType != null)
2396 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2397 if (TypeBuilder.IsInterface)
2398 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2404 public abstract class ClassOrStruct : TypeContainer {
2405 ListDictionary declarative_security;
2407 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2408 MemberName name, Attributes attrs, Kind kind)
2409 : base (ns, parent, name, attrs, kind)
2413 protected override bool AddToContainer (MemberCore symbol, string name)
2415 if (name == Basename) {
2416 Report.SymbolRelatedToPreviousError (this);
2417 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2418 symbol.GetSignatureForError ());
2422 return base.AddToContainer (symbol, name);
2425 public override void VerifyMembers ()
2427 base.VerifyMembers ();
2429 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2430 foreach (Event e in events){
2431 if ((e.caching_flags & Flags.IsAssigned) == 0)
2432 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2437 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2439 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2440 if (declarative_security == null)
2441 declarative_security = new ListDictionary ();
2443 a.ExtractSecurityPermissionSet (declarative_security);
2447 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2448 HasExplicitLayout = true;
2451 base.ApplyAttributeBuilder (a, cb);
2455 /// Defines the default constructors
2457 protected void DefineDefaultConstructor (bool is_static)
2459 // The default instance constructor is public
2460 // If the class is abstract, the default constructor is protected
2461 // The default static constructor is private
2465 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2467 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2470 Constructor c = new Constructor (this, MemberName.Name, mods,
2471 Parameters.EmptyReadOnlyParameters,
2472 new GeneratedBaseInitializer (Location),
2476 c.Block = new ToplevelBlock (null, Location);
2479 void DefineFieldInitializers ()
2481 if (initialized_fields != null) {
2482 for (int i = 0; i < initialized_fields.Count; ++i) {
2483 FieldBase fb = (FieldBase)initialized_fields[i];
2484 fb.ResolveInitializer ();
2485 if (fb.HasDefaultInitializer && RootContext.Optimize) {
2486 // Field is re-initialized to its default value => removed
2487 initialized_fields.RemoveAt (i);
2493 if (initialized_static_fields != null) {
2494 bool has_complex_initializer = false;
2496 foreach (FieldBase fb in initialized_static_fields) {
2497 if (fb.ResolveInitializer () is Constant)
2500 has_complex_initializer = true;
2503 // Need special check to not optimize code like this
2504 // static int a = b = 5;
2505 // static int b = 0;
2506 if (!has_complex_initializer && RootContext.Optimize) {
2507 for (int i = 0; i < initialized_static_fields.Count; ++i) {
2508 FieldBase fb = (FieldBase)initialized_static_fields[i];
2509 if (fb.HasDefaultInitializer) {
2510 initialized_static_fields.RemoveAt (i);
2516 if (default_static_constructor == null && initialized_static_fields.Count > 0) {
2517 DefineDefaultConstructor (true);
2523 public override bool Define ()
2525 DefineFieldInitializers ();
2527 if (default_static_constructor != null)
2528 default_static_constructor.Define ();
2530 return base.Define ();
2533 public override void Emit ()
2537 if (declarative_security != null) {
2538 foreach (DictionaryEntry de in declarative_security) {
2539 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2544 protected override TypeAttributes TypeAttr {
2546 if (default_static_constructor == null)
2547 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2549 return base.TypeAttr;
2555 // TODO: should be sealed
2556 public class Class : ClassOrStruct {
2557 const int AllowedModifiers =
2560 Modifiers.PROTECTED |
2561 Modifiers.INTERNAL |
2563 Modifiers.ABSTRACT |
2568 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2570 : base (ns, parent, name, attrs, Kind.Class)
2572 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2573 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2575 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2576 Report.FeatureIsNotStandardized (Location, "static classes");
2580 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2582 if (a.Type == TypeManager.attribute_usage_type) {
2583 if (BaseType != TypeManager.attribute_type && !BaseType.IsSubclassOf (TypeManager.attribute_type) &&
2584 TypeBuilder.FullName != "System.Attribute") {
2585 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2589 if (a.Type == TypeManager.conditional_attribute_type &&
2590 !(BaseType == TypeManager.attribute_type || BaseType.IsSubclassOf (TypeManager.attribute_type))) {
2591 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2595 if (a.Type == TypeManager.comimport_attr_type &&
2596 !attributes.Contains (TypeManager.guid_attr_type)) {
2597 a.Error_MissingGuidAttribute ();
2601 if (AttributeTester.IsAttributeExcluded (a.Type))
2604 base.ApplyAttributeBuilder (a, cb);
2607 public override AttributeTargets AttributeTargets {
2609 return AttributeTargets.Class;
2613 protected override void DefineContainerMembers (MemberCoreArrayList list)
2619 base.DefineContainerMembers (list);
2623 foreach (MemberCore m in list) {
2624 if (m is Operator) {
2625 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2629 if (m is Destructor) {
2630 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2634 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2635 Report.Error (1057, m.Location, "`{0}': Static classes cannot contain protected members", m.GetSignatureForError ());
2640 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2644 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2647 if (m is Constructor) {
2648 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2652 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2655 base.DefineContainerMembers (list);
2658 public override TypeBuilder DefineType ()
2660 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2661 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2665 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2666 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2670 return base.DefineType ();
2673 protected override bool DoDefineMembers ()
2675 if (InstanceConstructors == null && !IsStatic)
2676 DefineDefaultConstructor (false);
2678 return base.DoDefineMembers ();
2681 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2683 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2685 if (base_class == null) {
2686 if (RootContext.StdLib)
2687 base_class = TypeManager.system_object_expr;
2688 else if (Name != "System.Object")
2689 base_class = TypeManager.system_object_expr;
2691 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
2692 Report.Error (1521, base_class.Location, "Invalid base type");
2696 if (base_class.IsSealed){
2697 Report.SymbolRelatedToPreviousError (base_class.Type);
2698 if (base_class.Type.IsAbstract) {
2699 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2700 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2702 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2703 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2708 if (!base_class.CanInheritFrom ()){
2709 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2710 GetSignatureForError (), base_class.GetSignatureForError ());
2714 if (!base_class.AsAccessible (this, ModFlags)) {
2715 Report.SymbolRelatedToPreviousError (base_class.Type);
2716 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2717 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2722 if (base_class != TypeManager.system_object_expr) {
2723 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2724 GetSignatureForError (), base_class.GetSignatureForError ());
2728 if (ifaces != null) {
2729 foreach (TypeExpr t in ifaces)
2730 Report.SymbolRelatedToPreviousError (t.Type);
2731 Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
2738 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2739 /// Valid only for attribute classes.
2740 public bool IsExcluded ()
2742 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2743 return (caching_flags & Flags.Excluded) != 0;
2745 caching_flags &= ~Flags.Excluded_Undetected;
2747 if (OptAttributes == null)
2750 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
2755 foreach (Attribute a in attrs) {
2756 string condition = a.GetConditionalAttributeValue ();
2757 if (RootContext.AllDefines.Contains (condition))
2761 caching_flags |= Flags.Excluded;
2767 return (ModFlags & Modifiers.STATIC) != 0;
2772 // FIXME: How do we deal with the user specifying a different
2775 protected override TypeAttributes TypeAttr {
2777 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2779 ta |= TypeAttributes.Abstract | TypeAttributes.Sealed;
2785 public sealed class Struct : ClassOrStruct {
2787 // Modifiers allowed in a struct declaration
2789 const int AllowedModifiers =
2792 Modifiers.PROTECTED |
2793 Modifiers.INTERNAL |
2797 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2798 int mod, Attributes attrs)
2799 : base (ns, parent, name, attrs, Kind.Struct)
2803 if (parent.Parent == null)
2804 accmods = Modifiers.INTERNAL;
2806 accmods = Modifiers.PRIVATE;
2808 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2810 this.ModFlags |= Modifiers.SEALED;
2813 public override AttributeTargets AttributeTargets {
2815 return AttributeTargets.Struct;
2819 const TypeAttributes DefaultTypeAttributes =
2820 TypeAttributes.SequentialLayout |
2821 TypeAttributes.Sealed |
2822 TypeAttributes.BeforeFieldInit;
2825 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2827 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2829 // If we are compiling our runtime,
2830 // and we are defining ValueType, then our
2831 // base is `System.Object'.
2833 if (base_class == null) {
2834 if (!RootContext.StdLib && Name == "System.ValueType")
2835 base_class = TypeManager.system_object_expr;
2837 base_class = TypeManager.system_valuetype_expr;
2844 // FIXME: Allow the user to specify a different set of attributes
2845 // in some cases (Sealed for example is mandatory for a class,
2846 // but what SequentialLayout can be changed
2848 protected override TypeAttributes TypeAttr {
2850 return base.TypeAttr | DefaultTypeAttributes;
2854 public override void RegisterFieldForInitialization (FieldBase field)
2856 if ((field.ModFlags & Modifiers.STATIC) == 0) {
2857 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
2858 field.GetSignatureForError ());
2861 base.RegisterFieldForInitialization (field);
2869 public sealed class Interface : TypeContainer, IMemberContainer {
2872 /// Modifiers allowed in a class declaration
2874 public const int AllowedModifiers =
2877 Modifiers.PROTECTED |
2878 Modifiers.INTERNAL |
2882 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2884 : base (ns, parent, name, attrs, Kind.Interface)
2888 if (parent.Parent == null)
2889 accmods = Modifiers.INTERNAL;
2891 accmods = Modifiers.PRIVATE;
2893 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
2896 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2898 if (a.Type == TypeManager.comimport_attr_type &&
2899 !attributes.Contains (TypeManager.guid_attr_type)) {
2900 a.Error_MissingGuidAttribute ();
2903 base.ApplyAttributeBuilder (a, cb);
2907 public override AttributeTargets AttributeTargets {
2909 return AttributeTargets.Interface;
2913 const TypeAttributes DefaultTypeAttributes =
2914 TypeAttributes.AutoLayout |
2915 TypeAttributes.Abstract |
2916 TypeAttributes.Interface;
2918 protected override TypeAttributes TypeAttr {
2920 return base.TypeAttr | DefaultTypeAttributes;
2924 protected override bool VerifyClsCompliance ()
2926 if (!base.VerifyClsCompliance ())
2929 if (ifaces != null) {
2930 foreach (Type t in ifaces) {
2931 if (AttributeTester.IsClsCompliant (t))
2934 Report.SymbolRelatedToPreviousError (t);
2935 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
2936 GetSignatureForError (), TypeManager.CSharpName (t));
2944 public abstract class MethodCore : MemberBase {
2945 public readonly Parameters Parameters;
2946 protected ToplevelBlock block;
2949 // The method we're overriding if this is an override method.
2951 protected MethodInfo base_method = null;
2953 public MethodCore (DeclSpace parent, Expression type, int mod,
2954 int allowed_mod, bool is_interface, MemberName name,
2955 Attributes attrs, Parameters parameters)
2956 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
2959 Parameters = parameters;
2960 IsInterface = is_interface;
2964 // Returns the System.Type array for the parameters of this method
2966 public Type [] ParameterTypes {
2968 return Parameters.Types;
2972 public Parameters ParameterInfo
2979 public ToplevelBlock Block {
2989 public void SetYields ()
2991 ModFlags |= Modifiers.METHOD_YIELDS;
2994 protected override bool CheckBase ()
2996 if (!base.CheckBase ())
2999 // Check whether arguments were correct.
3000 if (!DoDefineParameters ())
3003 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3009 // Is null for System.Object while compiling corlib and base interfaces
3010 if (Parent.PartialContainer.BaseCache == null) {
3011 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3012 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3017 Type base_ret_type = null;
3018 base_method = FindOutBaseMethod (ref base_ret_type);
3020 // method is override
3021 if (base_method != null) {
3023 if (!CheckMethodAgainstBase ())
3026 if ((ModFlags & Modifiers.NEW) == 0) {
3027 if (MemberType != TypeManager.TypeToCoreType (base_ret_type)) {
3028 Report.SymbolRelatedToPreviousError (base_method);
3029 if (this is PropertyBase) {
3030 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3031 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3034 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3035 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3040 if (base_method.IsAbstract && !IsInterface) {
3041 Report.SymbolRelatedToPreviousError (base_method);
3042 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3043 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3048 if (base_method.IsSpecialName && !(this is PropertyBase)) {
3049 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3053 if (Name == "Equals" && Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type)
3054 Parent.PartialContainer.Mark_HasEquals ();
3055 else if (Name == "GetHashCode" && Parameters.Empty)
3056 Parent.PartialContainer.Mark_HasGetHashCode ();
3058 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3059 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3061 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3062 Report.SymbolRelatedToPreviousError (base_method);
3063 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3064 GetSignatureForError (), TypeManager.CSharpSignature (base_method) );
3071 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !(this is Property));
3072 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3073 if (conflict_symbol != null) {
3074 Report.SymbolRelatedToPreviousError (conflict_symbol);
3075 if (this is PropertyBase)
3076 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3078 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3080 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3084 if (conflict_symbol == null) {
3085 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3086 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3091 if ((ModFlags & Modifiers.NEW) == 0) {
3092 if (this is Method && conflict_symbol is MethodBase)
3095 Report.SymbolRelatedToPreviousError (conflict_symbol);
3096 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3097 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3104 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3105 // that have been defined.
3107 // `name' is the user visible name for reporting errors (this is used to
3108 // provide the right name regarding method names and properties)
3110 bool CheckMethodAgainstBase ()
3114 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3115 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3116 Report.Error (506, Location,
3117 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3118 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3122 // Now we check that the overriden method is not final
3124 if (base_method.IsFinal) {
3125 Report.SymbolRelatedToPreviousError (base_method);
3126 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3127 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3131 // Check that the permissions are not being changed
3133 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3134 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3136 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3137 Error_CannotChangeAccessModifiers (base_method, base_classp, null);
3142 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3143 ModFlags |= Modifiers.NEW;
3144 Report.SymbolRelatedToPreviousError (base_method);
3145 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3146 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",
3147 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3149 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3150 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3157 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3159 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3161 // when overriding protected internal, the method can be declared
3162 // protected internal only within the same assembly
3165 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3166 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3168 // assemblies differ - report an error
3172 } else if (thisp != base_classp) {
3174 // same assembly, but other attributes differ - report an error
3179 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3181 // if it's not "protected internal", it must be "protected"
3185 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3187 // protected within the same assembly - an error
3190 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3191 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3193 // protected ok, but other attributes differ - report an error
3199 return (thisp == base_classp);
3203 public bool CheckAbstractAndExtern (bool has_block)
3205 if (Parent.PartialContainer.Kind == Kind.Interface)
3209 if ((ModFlags & Modifiers.EXTERN) != 0) {
3210 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
3211 GetSignatureForError ());
3215 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
3216 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
3217 GetSignatureForError ());
3221 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
3222 Report.Error (501, Location, "`{0}' must declare a body because it is not marked abstract or extern",
3223 GetSignatureForError ());
3231 protected void Error_CannotChangeAccessModifiers (MemberInfo base_method, MethodAttributes ma, string suffix)
3233 Report.SymbolRelatedToPreviousError (base_method);
3234 string base_name = TypeManager.GetFullNameSignature (base_method);
3235 string this_name = GetSignatureForError ();
3236 if (suffix != null) {
3237 base_name += suffix;
3238 this_name += suffix;
3241 Report.Error (507, Location, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3242 this_name, Modifiers.GetDescription (ma), base_name);
3245 protected static string Error722 {
3247 return "`{0}': static types cannot be used as return types";
3252 /// For custom member duplication search in a container
3254 protected abstract bool CheckForDuplications ();
3257 /// Gets base method and its return type
3259 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3261 protected bool DoDefineParameters ()
3263 // Check if arguments were correct
3264 if (!Parameters.Resolve (this))
3267 return CheckParameters (ParameterTypes);
3270 bool CheckParameters (Type [] parameters)
3273 DeclSpace ds = Parent;
3275 foreach (Type partype in parameters){
3276 if (partype == TypeManager.void_type) {
3277 // TODO: location is wrong
3278 Expression.Error_VoidInvalidInTheContext (Location);
3282 if (partype.IsPointer){
3283 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
3287 if (ds.AsAccessible (partype, ModFlags))
3290 Report.SymbolRelatedToPreviousError (partype);
3291 if (this is Indexer)
3292 Report.Error (55, Location,
3293 "Inconsistent accessibility: parameter type `" +
3294 TypeManager.CSharpName (partype) + "' is less " +
3295 "accessible than indexer `" + GetSignatureForError () + "'");
3296 else if (this is Operator)
3297 Report.Error (57, Location,
3298 "Inconsistent accessibility: parameter type `" +
3299 TypeManager.CSharpName (partype) + "' is less " +
3300 "accessible than operator `" + GetSignatureForError () + "'");
3302 Report.Error (51, Location,
3303 "Inconsistent accessibility: parameter type `" +
3304 TypeManager.CSharpName (partype) + "' is less " +
3305 "accessible than method `" + GetSignatureForError () + "'");
3312 protected override bool VerifyClsCompliance ()
3314 if (!base.VerifyClsCompliance ()) {
3315 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3316 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3321 if (Parameters.HasArglist) {
3322 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3325 if (!AttributeTester.IsClsCompliant (MemberType)) {
3326 if (this is PropertyBase)
3327 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3328 GetSignatureForError ());
3330 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3331 GetSignatureForError ());
3334 Parameters.VerifyClsCompliance ();
3339 protected bool IsDuplicateImplementation (MethodCore method)
3341 if (method == this || !(method.MemberName.Equals (MemberName)))
3344 Type[] param_types = method.ParameterTypes;
3345 // This never happen. Rewrite this as Equal
3346 if (param_types == null && ParameterTypes == null)
3348 if (param_types == null || ParameterTypes == null)
3351 if (param_types.Length != ParameterTypes.Length)
3354 if (method.Parameters.HasArglist != Parameters.HasArglist)
3357 for (int i = 0; i < param_types.Length; i++)
3358 if (param_types [i] != ParameterTypes [i])
3361 // TODO: make operator compatible with MethodCore to avoid this
3362 if (this is Operator && method is Operator) {
3363 if (MemberType != method.MemberType)
3368 // Try to report 663: method only differs on out/ref
3370 Parameters info = ParameterInfo;
3371 Parameters other_info = method.ParameterInfo;
3372 for (int i = 0; i < info.Count; i++){
3373 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3374 Report.SymbolRelatedToPreviousError (method);
3375 Report.Error (663, Location, "`{0}': Methods cannot differ only on their use of ref and out on a parameters",
3376 GetSignatureForError ());
3381 Report.SymbolRelatedToPreviousError (method);
3382 if (this is Operator && method is Operator)
3383 Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
3385 Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
3389 public override bool IsUsed {
3390 get { return IsExplicitImpl || base.IsUsed; }
3394 // Returns a string that represents the signature for this
3395 // member which should be used in XML documentation.
3397 public override string GetDocCommentName (DeclSpace ds)
3399 return DocUtil.GetMethodDocCommentName (this, ds);
3403 // Raised (and passed an XmlElement that contains the comment)
3404 // when GenerateDocComment is writing documentation expectedly.
3406 // FIXME: with a few effort, it could be done with XmlReader,
3407 // that means removal of DOM use.
3409 internal override void OnGenerateDocComment (XmlElement el)
3411 DocUtil.OnMethodGenerateDocComment (this, el);
3415 // Represents header string for documentation comment.
3417 public override string DocCommentHeader {
3418 get { return "M:"; }
3423 public abstract class MethodOrOperator : MethodCore, IMethodData
3425 public MethodBuilder MethodBuilder;
3426 ReturnParameter return_attributes;
3427 ListDictionary declarative_security;
3428 protected MethodData MethodData;
3430 static string[] attribute_targets = new string [] { "method", "return" };
3432 protected MethodOrOperator (DeclSpace parent, Expression type, int mod,
3433 int allowed_mod, bool is_interface, MemberName name,
3434 Attributes attrs, Parameters parameters)
3435 : base (parent, type, mod, allowed_mod, is_interface, name,
3440 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3442 if (a.Target == AttributeTargets.ReturnValue) {
3443 if (return_attributes == null)
3444 return_attributes = new ReturnParameter (MethodBuilder, Location);
3446 return_attributes.ApplyAttributeBuilder (a, cb);
3450 if (a.Type == TypeManager.methodimpl_attr_type &&
3451 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
3452 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3455 if (a.Type == TypeManager.dllimport_type) {
3456 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3457 if ((ModFlags & extern_static) != extern_static) {
3458 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3464 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3465 if (declarative_security == null)
3466 declarative_security = new ListDictionary ();
3467 a.ExtractSecurityPermissionSet (declarative_security);
3471 MethodBuilder.SetCustomAttribute (cb);
3474 public override AttributeTargets AttributeTargets {
3476 return AttributeTargets.Method;
3480 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
3482 EmitContext ec = new EmitContext (this, ds, Parent, Location, ig, MemberType, ModFlags, false);
3484 ec.CurrentIterator = ds as Iterator;
3485 if (ec.CurrentIterator != null)
3486 ec.CurrentAnonymousMethod = ec.CurrentIterator.Host;
3491 public override bool Define ()
3496 if (!CheckAbstractAndExtern (block != null))
3502 MethodData = new MethodData (this, ModFlags, flags, this);
3504 if (!MethodData.Define (Parent.PartialContainer))
3507 MethodBuilder = MethodData.MethodBuilder;
3509 if (MemberType.IsAbstract && MemberType.IsSealed) {
3510 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3517 public override void Emit ()
3519 if (OptAttributes != null)
3520 OptAttributes.Emit ();
3522 if (declarative_security != null) {
3523 foreach (DictionaryEntry de in declarative_security) {
3524 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3531 protected void Error_ConditionalAttributeIsNotValid ()
3533 Report.Error (577, Location,
3534 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3535 GetSignatureForError ());
3538 public override bool MarkForDuplicationCheck ()
3540 caching_flags |= Flags.TestMethodDuplication;
3544 public override string[] ValidAttributeTargets {
3546 return attribute_targets;
3550 #region IMethodData Members
3552 public CallingConventions CallingConventions {
3554 CallingConventions cc = Parameters.CallingConvention;
3555 if (Parameters.HasArglist)
3556 block.HasVarargs = true;
3559 if ((ModFlags & Modifiers.STATIC) == 0)
3560 cc |= CallingConventions.HasThis;
3562 // FIXME: How is `ExplicitThis' used in C#?
3568 public Type ReturnType {
3574 public MemberName MethodName {
3580 public new Location Location {
3582 return base.Location;
3586 protected override bool CheckBase() {
3587 if (!base.CheckBase ())
3590 // TODO: Destructor should derive from MethodCore
3591 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3592 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3593 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3601 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3603 public bool IsExcluded () {
3604 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3605 return (caching_flags & Flags.Excluded) != 0;
3607 caching_flags &= ~Flags.Excluded_Undetected;
3609 if (base_method == null) {
3610 if (OptAttributes == null)
3613 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3618 foreach (Attribute a in attrs) {
3619 string condition = a.GetConditionalAttributeValue ();
3620 if (condition == null)
3623 if (RootContext.AllDefines.Contains (condition))
3627 caching_flags |= Flags.Excluded;
3631 IMethodData md = TypeManager.GetMethod (base_method);
3633 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
3634 caching_flags |= Flags.Excluded;
3640 if (md.IsExcluded ()) {
3641 caching_flags |= Flags.Excluded;
3651 public class SourceMethod : ISourceMethod
3656 protected SourceMethod (DeclSpace parent, MethodBase builder,
3657 ISourceFile file, Location start, Location end)
3659 this.parent = parent;
3660 this.builder = builder;
3662 CodeGen.SymbolWriter.OpenMethod (file, this, start.Row, start.Column, end.Row, start.Column);
3665 public string Name {
3666 get { return builder.Name; }
3669 public int NamespaceID {
3670 get { return parent.NamespaceEntry.SymbolFileID; }
3675 if (builder is MethodBuilder)
3676 return ((MethodBuilder) builder).GetToken ().Token;
3677 else if (builder is ConstructorBuilder)
3678 return ((ConstructorBuilder) builder).GetToken ().Token;
3680 throw new NotSupportedException ();
3684 public void CloseMethod ()
3686 if (CodeGen.SymbolWriter != null)
3687 CodeGen.SymbolWriter.CloseMethod ();
3690 public static SourceMethod Create (DeclSpace parent, MethodBase builder, Block block)
3692 if (CodeGen.SymbolWriter == null)
3697 Location start_loc = block.StartLocation;
3698 if (start_loc.IsNull)
3701 Location end_loc = block.EndLocation;
3705 ISourceFile file = start_loc.SourceFile;
3709 return new SourceMethod (
3710 parent, builder, file, start_loc, end_loc);
3714 public class Method : MethodOrOperator, IIteratorContainer {
3717 /// Modifiers allowed in a class declaration
3719 const int AllowedModifiers =
3722 Modifiers.PROTECTED |
3723 Modifiers.INTERNAL |
3728 Modifiers.OVERRIDE |
3729 Modifiers.ABSTRACT |
3731 Modifiers.METHOD_YIELDS |
3734 const int AllowedInterfaceModifiers =
3735 Modifiers.NEW | Modifiers.UNSAFE;
3738 // return_type can be "null" for VOID values.
3740 public Method (DeclSpace parent, Expression return_type, int mod, bool is_iface,
3741 MemberName name, Parameters parameters, Attributes attrs)
3742 : base (parent, return_type, mod,
3743 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3744 is_iface, name, attrs, parameters)
3748 public override string GetSignatureForError()
3750 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
3753 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
3755 Report.Error (17, location,
3756 "Program `{0}' has more than one entry point defined: `{1}'",
3757 CodeGen.FileName, TypeManager.CSharpSignature(b));
3760 bool IsEntryPoint (Parameters pinfo)
3762 if (ReturnType != TypeManager.void_type &&
3763 ReturnType != TypeManager.int32_type)
3766 if (pinfo.Count == 0)
3769 if (pinfo.Count > 1)
3772 Type t = pinfo.ParameterType (0);
3774 (t.GetArrayRank () == 1) &&
3775 (TypeManager.GetElementType (t) == TypeManager.string_type) &&
3776 (pinfo.ParameterModifier (0) == Parameter.Modifier.NONE))
3782 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3784 if (a.Type == TypeManager.conditional_attribute_type) {
3785 if (IsExplicitImpl) {
3786 Error_ConditionalAttributeIsNotValid ();
3790 if (ReturnType != TypeManager.void_type) {
3791 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
3795 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3796 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
3801 Report.Error (582, Location, "Conditional not valid on interface members");
3805 if (MethodData.implementing != null) {
3806 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
3807 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
3811 for (int i = 0; i < ParameterInfo.Count; ++i) {
3812 if ((ParameterInfo.ParameterModifier (i) & Parameter.Modifier.OUTMASK) != 0) {
3813 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
3819 base.ApplyAttributeBuilder (a, cb);
3822 protected override bool CheckForDuplications ()
3824 ArrayList ar = Parent.PartialContainer.Methods;
3826 int arLen = ar.Count;
3828 for (int i = 0; i < arLen; i++) {
3829 Method m = (Method) ar [i];
3830 if (IsDuplicateImplementation (m))
3835 ar = Parent.PartialContainer.Properties;
3837 for (int i = 0; i < ar.Count; ++i) {
3838 PropertyBase pb = (PropertyBase) ar [i];
3839 if (pb.AreAccessorsDuplicateImplementation (this))
3844 ar = Parent.PartialContainer.Indexers;
3846 for (int i = 0; i < ar.Count; ++i) {
3847 PropertyBase pb = (PropertyBase) ar [i];
3848 if (pb.AreAccessorsDuplicateImplementation (this))
3853 ar = Parent.PartialContainer.Events;
3855 for (int i = 0; i < ar.Count; ++i) {
3856 Event ev = (Event) ar [i];
3857 if (ev.AreAccessorsDuplicateImplementation (this))
3868 public override bool Define ()
3870 if (!base.Define ())
3873 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
3874 Error1599 (Location, ReturnType);
3878 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
3879 Name == "Finalize" && !(this is Destructor)) {
3880 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
3884 // Setup iterator if we are one
3886 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3887 Iterator iterator = new Iterator (this,
3891 if (!iterator.DefineIterator ())
3896 // This is used to track the Entry Point,
3898 if (RootContext.NeedsEntryPoint && ((ModFlags & Modifiers.STATIC) != 0) &&
3900 (RootContext.MainClass == null ||
3901 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3902 if (IsEntryPoint (ParameterInfo)) {
3904 if (RootContext.EntryPoint == null) {
3905 if (Parent.IsGeneric || MemberName.IsGeneric) {
3906 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
3907 GetSignatureForError ());
3909 IMethodData md = TypeManager.GetMethod (MethodBuilder);
3910 md.SetMemberIsUsed ();
3912 RootContext.EntryPoint = MethodBuilder;
3913 RootContext.EntryPointLocation = Location;
3916 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3917 Error_DuplicateEntryPoint (MethodBuilder, Location);
3920 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
3921 GetSignatureForError ());
3931 public override void Emit ()
3933 MethodData.Emit (Parent);
3940 public static void Error1599 (Location loc, Type t)
3942 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
3945 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
3947 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
3948 Parent.TypeBuilder, Name, ParameterTypes, false);
3953 if (mi.IsSpecialName)
3956 base_ret_type = mi.ReturnType;
3960 protected override bool VerifyClsCompliance ()
3962 if (!base.VerifyClsCompliance ())
3965 if (ParameterInfo.Count > 0) {
3966 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
3968 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
3975 public abstract class ConstructorInitializer {
3976 ArrayList argument_list;
3977 protected ConstructorInfo base_constructor;
3980 public ConstructorInitializer (ArrayList argument_list, Location loc)
3982 this.argument_list = argument_list;
3986 public ArrayList Arguments {
3988 return argument_list;
3992 public bool Resolve (ConstructorBuilder caller_builder, Block block, EmitContext ec)
3994 Expression base_constructor_group;
3998 ec.CurrentBlock = block;
4000 if (argument_list != null){
4001 foreach (Argument a in argument_list){
4002 if (!a.Resolve (ec, loc))
4006 ec.CurrentBlock = null;
4008 if (this is ConstructorBaseInitializer) {
4009 if (ec.ContainerType.BaseType == null)
4012 t = ec.ContainerType.BaseType;
4013 if (ec.ContainerType.IsValueType) {
4014 Report.Error (522, loc,
4015 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4019 t = ec.ContainerType;
4021 base_constructor_group = Expression.MemberLookup (
4022 ec.ContainerType, t, ".ctor", MemberTypes.Constructor,
4023 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4026 if (base_constructor_group == null){
4028 base_constructor_group = Expression.MemberLookup (
4029 t, null, t, ".ctor", MemberTypes.Constructor,
4030 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4034 int errors = Report.Errors;
4035 if (base_constructor_group != null)
4036 base_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4037 ec, (MethodGroupExpr) base_constructor_group, argument_list,
4040 if (base_constructor == null) {
4041 if (errors == Report.Errors)
4042 Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpSignature (caller_builder),
4043 argument_list == null ? 0 : argument_list.Count);
4048 Report.SymbolRelatedToPreviousError (base_constructor);
4049 Expression.ErrorIsInaccesible (loc, TypeManager.CSharpSignature (base_constructor));
4050 base_constructor = null;
4054 if (base_constructor == caller_builder){
4055 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4062 public virtual void Emit (EmitContext ec)
4064 if (base_constructor != null){
4065 ec.Mark (loc, false);
4067 Invocation.EmitCall (ec, true, true, null, base_constructor, argument_list, loc);
4069 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), base_constructor, argument_list, loc);
4074 public class ConstructorBaseInitializer : ConstructorInitializer {
4075 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4076 base (argument_list, l)
4081 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4082 public GeneratedBaseInitializer (Location loc):
4088 public class ConstructorThisInitializer : ConstructorInitializer {
4089 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4090 base (argument_list, l)
4095 public class Constructor : MethodCore, IMethodData {
4096 public ConstructorBuilder ConstructorBuilder;
4097 public ConstructorInitializer Initializer;
4098 ListDictionary declarative_security;
4101 // Modifiers allowed for a constructor.
4103 public const int AllowedModifiers =
4105 Modifiers.PROTECTED |
4106 Modifiers.INTERNAL |
4112 static string[] attribute_targets = new string [] { "method" };
4114 bool has_compliant_args = false;
4116 // The spec claims that static is not permitted, but
4117 // my very own code has static constructors.
4119 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4120 ConstructorInitializer init, Location loc)
4121 : base (parent, null, mod, AllowedModifiers, false, new MemberName (name, loc),
4127 public bool HasCompliantArgs {
4128 get { return has_compliant_args; }
4131 public override AttributeTargets AttributeTargets {
4132 get { return AttributeTargets.Constructor; }
4137 // Returns true if this is a default constructor
4139 public bool IsDefault ()
4141 if ((ModFlags & Modifiers.STATIC) != 0)
4142 return Parameters.Empty;
4144 return Parameters.Empty &&
4145 (Initializer is ConstructorBaseInitializer) &&
4146 (Initializer.Arguments == null);
4149 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4151 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4152 if (declarative_security == null) {
4153 declarative_security = new ListDictionary ();
4155 a.ExtractSecurityPermissionSet (declarative_security);
4159 if (a.Type == TypeManager.methodimpl_attr_type &&
4160 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
4161 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
4164 ConstructorBuilder.SetCustomAttribute (cb);
4167 protected override bool CheckForDuplications ()
4169 ArrayList ar = Parent.PartialContainer.InstanceConstructors;
4171 int arLen = ar.Count;
4173 for (int i = 0; i < arLen; i++) {
4174 Constructor m = (Constructor) ar [i];
4175 if (IsDuplicateImplementation (m))
4182 protected override bool CheckBase ()
4184 if ((ModFlags & Modifiers.STATIC) != 0) {
4185 if (!Parameters.Empty) {
4186 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4187 GetSignatureForError ());
4191 // the rest can be ignored
4195 // Check whether arguments were correct.
4196 if (!DoDefineParameters ())
4199 if (!CheckForDuplications ())
4202 if (Parent.PartialContainer.Kind == Kind.Struct) {
4203 if (ParameterTypes.Length == 0) {
4204 Report.Error (568, Location,
4205 "Structs cannot contain explicit parameterless constructors");
4209 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4210 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4215 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4216 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4223 // Creates the ConstructorBuilder
4225 public override bool Define ()
4227 if (ConstructorBuilder != null)
4230 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4231 MethodAttributes.SpecialName);
4233 if ((ModFlags & Modifiers.STATIC) != 0) {
4234 ca |= MethodAttributes.Static | MethodAttributes.Private;
4236 ca |= MethodAttributes.HideBySig;
4238 if ((ModFlags & Modifiers.PUBLIC) != 0)
4239 ca |= MethodAttributes.Public;
4240 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4241 if ((ModFlags & Modifiers.INTERNAL) != 0)
4242 ca |= MethodAttributes.FamORAssem;
4244 ca |= MethodAttributes.Family;
4245 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4246 ca |= MethodAttributes.Assembly;
4248 ca |= MethodAttributes.Private;
4251 if (!CheckAbstractAndExtern (block != null))
4254 // Check if arguments were correct.
4258 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4259 ca, CallingConventions,
4262 if ((ModFlags & Modifiers.UNSAFE) != 0)
4263 ConstructorBuilder.InitLocals = false;
4265 if (Parent.PartialContainer.IsComImport) {
4266 if (!IsDefault ()) {
4267 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4268 Parent.GetSignatureForError ());
4271 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4274 TypeManager.AddMethod (ConstructorBuilder, this);
4282 public override void Emit ()
4284 if (OptAttributes != null)
4285 OptAttributes.Emit ();
4287 EmitContext ec = CreateEmitContext (null, null);
4289 if (block != null) {
4290 // If this is a non-static `struct' constructor and doesn't have any
4291 // initializer, it must initialize all of the struct's fields.
4292 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4293 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4294 block.AddThisVariable (Parent, Location);
4296 if (!block.ResolveMeta (ec, ParameterInfo))
4300 if ((ModFlags & Modifiers.STATIC) == 0){
4301 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4302 Initializer = new GeneratedBaseInitializer (Location);
4306 // Spec mandates that Initializers will not have
4310 if ((Initializer != null) &&
4311 !Initializer.Resolve (ConstructorBuilder, block, ec))
4313 ec.IsStatic = false;
4316 Parameters.ApplyAttributes (ConstructorBuilder);
4318 SourceMethod source = SourceMethod.Create (
4319 Parent, ConstructorBuilder, block);
4322 // Classes can have base initializers and instance field initializers.
4324 if (Parent.PartialContainer.Kind == Kind.Class){
4325 if ((ModFlags & Modifiers.STATIC) == 0){
4328 // If we use a "this (...)" constructor initializer, then
4329 // do not emit field initializers, they are initialized in the other constructor
4331 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4332 Parent.PartialContainer.EmitFieldInitializers (ec);
4335 if (Initializer != null) {
4336 Initializer.Emit (ec);
4339 if ((ModFlags & Modifiers.STATIC) != 0)
4340 Parent.PartialContainer.EmitFieldInitializers (ec);
4342 ec.EmitTopBlock (this, block);
4345 source.CloseMethod ();
4349 if (declarative_security != null) {
4350 foreach (DictionaryEntry de in declarative_security) {
4351 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4358 // Is never override
4359 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4364 public override string GetSignatureForError()
4366 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4369 public override string[] ValidAttributeTargets {
4371 return attribute_targets;
4375 protected override bool VerifyClsCompliance ()
4377 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4381 if (ParameterInfo.Count > 0) {
4382 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4384 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4386 if (Parent.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4387 foreach (Type param in ParameterTypes) {
4388 if (param.IsArray) {
4394 has_compliant_args = true;
4398 #region IMethodData Members
4400 public System.Reflection.CallingConventions CallingConventions {
4402 CallingConventions cc = Parameters.CallingConvention;
4404 if (Parent.PartialContainer.Kind == Kind.Class)
4405 if ((ModFlags & Modifiers.STATIC) == 0)
4406 cc |= CallingConventions.HasThis;
4408 // FIXME: How is `ExplicitThis' used in C#?
4414 public new Location Location {
4416 return base.Location;
4420 public MemberName MethodName {
4426 public Type ReturnType {
4432 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4434 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4435 return new EmitContext (this, Parent, Location, ig_, null, ModFlags, true);
4438 public bool IsExcluded()
4447 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4449 public interface IMethodData
4451 CallingConventions CallingConventions { get; }
4452 Location Location { get; }
4453 MemberName MethodName { get; }
4454 Type ReturnType { get; }
4455 Parameters ParameterInfo { get; }
4457 Attributes OptAttributes { get; }
4458 ToplevelBlock Block { get; set; }
4460 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4461 ObsoleteAttribute GetObsoleteAttribute ();
4462 string GetSignatureForError ();
4464 bool IsClsComplianceRequired ();
4465 void SetMemberIsUsed ();
4469 // Encapsulates most of the Method's state
4471 public class MethodData {
4473 readonly IMethodData method;
4476 // Are we implementing an interface ?
4478 public MethodInfo implementing;
4483 protected MemberBase member;
4484 protected int modifiers;
4485 protected MethodAttributes flags;
4487 MethodBuilder builder = null;
4488 public MethodBuilder MethodBuilder {
4494 public MethodData (MemberBase member,
4495 int modifiers, MethodAttributes flags, IMethodData method)
4497 this.member = member;
4498 this.modifiers = modifiers;
4501 this.method = method;
4504 public bool Define (DeclSpace parent)
4506 string name = method.MethodName.Name;
4507 string method_name = name;
4509 TypeContainer container = parent.PartialContainer;
4511 PendingImplementation pending = container.PendingImplementations;
4512 if (pending != null){
4513 if (member is Indexer) // TODO: test it, but it should work without this IF
4514 implementing = pending.IsInterfaceIndexer (
4515 member.InterfaceType, method.ReturnType, method.ParameterInfo);
4517 implementing = pending.IsInterfaceMethod (
4518 member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
4520 if (member.InterfaceType != null){
4521 if (implementing == null){
4522 if (member is PropertyBase) {
4523 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4524 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4525 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4528 Report.Error (539, method.Location,
4529 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4530 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4534 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4535 Report.SymbolRelatedToPreviousError (implementing);
4536 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4537 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4540 method_name = member.InterfaceType.FullName + "." + name;
4542 if (implementing != null) {
4543 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4544 if (prop_method == null) {
4545 if (implementing.IsSpecialName) {
4546 Report.SymbolRelatedToPreviousError (implementing);
4547 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
4548 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
4549 implementing.Name.StartsWith ("get_") ? "get" : "set");
4552 } else if (implementing.DeclaringType.IsInterface) {
4553 if (!implementing.IsSpecialName) {
4554 Report.SymbolRelatedToPreviousError (implementing);
4555 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4556 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4559 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4560 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4561 Report.SymbolRelatedToPreviousError (implementing);
4562 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4563 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4572 // For implicit implementations, make sure we are public, for
4573 // explicit implementations, make sure we are private.
4575 if (implementing != null){
4577 // Setting null inside this block will trigger a more
4578 // verbose error reporting for missing interface implementations
4580 // The "candidate" function has been flagged already
4581 // but it wont get cleared
4583 if (member.IsExplicitImpl){
4584 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4585 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4586 implementing = null;
4588 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
4589 Report.SymbolRelatedToPreviousError (implementing);
4590 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
4591 method.GetSignatureForError ());
4594 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4595 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4597 // If this is an interface method implementation,
4598 // check for public accessibility
4600 implementing = null;
4601 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4602 // We may never be private.
4603 implementing = null;
4604 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4606 // We may be protected if we're overriding something.
4608 implementing = null;
4613 // Static is not allowed
4615 if ((modifiers & Modifiers.STATIC) != 0){
4616 implementing = null;
4621 // If implementing is still valid, set flags
4623 if (implementing != null){
4625 // When implementing interface methods, set NewSlot
4626 // unless, we are overwriting a method.
4628 if (implementing.DeclaringType.IsInterface){
4629 if ((modifiers & Modifiers.OVERRIDE) == 0)
4630 flags |= MethodAttributes.NewSlot;
4633 MethodAttributes.Virtual |
4634 MethodAttributes.HideBySig;
4636 // Set Final unless we're virtual, abstract or already overriding a method.
4637 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4638 flags |= MethodAttributes.Final;
4641 DefineMethodBuilder (container, method_name, method.ParameterInfo.Types);
4643 if (builder == null)
4646 if ((modifiers & Modifiers.UNSAFE) != 0)
4647 builder.InitLocals = false;
4649 if (implementing != null){
4651 // clear the pending implemntation flag
4653 if (member is Indexer) {
4654 pending.ImplementIndexer (
4655 member.InterfaceType, builder, method.ReturnType,
4656 method.ParameterInfo, member.IsExplicitImpl);
4658 pending.ImplementMethod (
4659 member.InterfaceType, name, method.ReturnType,
4660 method.ParameterInfo, member.IsExplicitImpl);
4662 if (member.IsExplicitImpl)
4663 container.TypeBuilder.DefineMethodOverride (
4664 builder, implementing);
4668 TypeManager.AddMethod (builder, method);
4675 /// Create the MethodBuilder for the method
4677 void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
4679 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4681 if ((modifiers & extern_static) == extern_static) {
4683 if (method.OptAttributes != null) {
4684 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type);
4685 if (dllimport_attribute != null) {
4686 flags |= MethodAttributes.PinvokeImpl;
4687 builder = dllimport_attribute.DefinePInvokeMethod (
4688 container.TypeBuilder, method_name, flags,
4689 method.ReturnType, ParameterTypes);
4695 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4696 // We are more strict than Microsoft and report CS0626 like error
4697 if (method.OptAttributes == null ||
4698 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type)) {
4699 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",
4700 method.GetSignatureForError ());
4705 builder = container.TypeBuilder.DefineMethod (
4706 method_name, flags, method.CallingConventions,
4707 method.ReturnType, ParameterTypes);
4713 public void Emit (DeclSpace parent)
4716 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4717 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
4719 ec = method.CreateEmitContext (parent, null);
4721 method.ParameterInfo.ApplyAttributes (MethodBuilder);
4723 ToplevelBlock block = method.Block;
4725 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
4728 // Handle destructors specially
4730 // FIXME: This code generates buggy code
4732 if (member is Destructor)
4733 EmitDestructor (ec, block);
4735 ec.EmitTopBlock (method, block);
4738 source.CloseMethod ();
4741 void EmitDestructor (EmitContext ec, ToplevelBlock block)
4743 ILGenerator ig = ec.ig;
4745 Label finish = ig.DefineLabel ();
4747 block.SetDestructor ();
4749 ig.BeginExceptionBlock ();
4750 ec.ReturnLabel = finish;
4751 ec.HasReturnLabel = true;
4752 ec.EmitTopBlock (method, block);
4754 // ig.MarkLabel (finish);
4755 ig.BeginFinallyBlock ();
4757 if (ec.ContainerType.BaseType != null) {
4758 Expression member_lookup = Expression.MemberLookup (
4759 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4760 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4762 if (member_lookup != null){
4763 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
4765 ig.Emit (OpCodes.Ldarg_0);
4766 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
4770 ig.EndExceptionBlock ();
4771 //ig.MarkLabel (ec.ReturnLabel);
4772 ig.Emit (OpCodes.Ret);
4776 // TODO: Should derive from MethodCore
4777 public class Destructor : Method {
4779 static string[] attribute_targets = new string [] { "method" };
4781 public Destructor (DeclSpace parent, Expression return_type, int mod,
4782 string name, Parameters parameters, Attributes attrs,
4784 : base (parent, return_type, mod, false, new MemberName (name, l),
4788 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4790 if (a.Type == TypeManager.conditional_attribute_type) {
4791 Error_ConditionalAttributeIsNotValid ();
4795 base.ApplyAttributeBuilder (a, cb);
4798 public override string GetSignatureForError ()
4800 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
4803 public override string[] ValidAttributeTargets {
4805 return attribute_targets;
4810 abstract public class MemberBase : MemberCore {
4811 public Expression Type;
4813 public MethodAttributes flags;
4815 protected readonly int explicit_mod_flags;
4818 // The "short" name of this property / indexer / event. This is the
4819 // name without the explicit interface.
4821 public string ShortName {
4822 get { return MemberName.Name; }
4823 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
4827 // The type of this property / indexer / event
4830 public Type MemberType {
4832 if (member_type == null && Type != null) {
4833 Type = Type.ResolveAsTypeTerminal (ResolveContext, false);
4835 member_type = Type.Type;
4843 // Whether this is an interface member.
4845 public bool IsInterface;
4848 // If true, this is an explicit interface implementation
4850 public bool IsExplicitImpl;
4853 // The interface type we are explicitly implementing
4855 public Type InterfaceType = null;
4858 // The constructor is only exposed to our children
4860 protected MemberBase (DeclSpace parent, Expression type, int mod,
4861 int allowed_mod, int def_mod, MemberName name,
4863 : base (parent, name, attrs)
4865 explicit_mod_flags = mod;
4867 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
4868 IsExplicitImpl = (MemberName.Left != null);
4871 protected virtual bool CheckBase ()
4873 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.PartialContainer.Kind == Kind.Struct) {
4874 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4878 if ((RootContext.WarningLevel >= 4) &&
4879 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
4880 ((ModFlags & Modifiers.PROTECTED) != 0) &&
4881 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
4882 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4887 protected virtual bool DoDefine ()
4890 throw new InternalErrorException ();
4893 ModFlags = Modifiers.PUBLIC |
4894 Modifiers.ABSTRACT |
4895 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
4897 flags = MethodAttributes.Public |
4898 MethodAttributes.Abstract |
4899 MethodAttributes.HideBySig |
4900 MethodAttributes.NewSlot |
4901 MethodAttributes.Virtual;
4903 if (!Parent.PartialContainer.MethodModifiersValid (this))
4906 flags = Modifiers.MethodAttr (ModFlags);
4909 if (MemberType == null)
4912 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
4913 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
4914 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
4915 GetSignatureForError (), Parent.GetSignatureForError ());
4919 // verify accessibility
4920 if (!Parent.AsAccessible (MemberType, ModFlags)) {
4921 Report.SymbolRelatedToPreviousError (MemberType);
4922 if (this is Property)
4923 Report.Error (53, Location,
4924 "Inconsistent accessibility: property type `" +
4925 TypeManager.CSharpName (MemberType) + "' is less " +
4926 "accessible than property `" + GetSignatureForError () + "'");
4927 else if (this is Indexer)
4928 Report.Error (54, Location,
4929 "Inconsistent accessibility: indexer return type `" +
4930 TypeManager.CSharpName (MemberType) + "' is less " +
4931 "accessible than indexer `" + GetSignatureForError () + "'");
4932 else if (this is MethodCore) {
4933 if (this is Operator)
4934 Report.Error (56, Location,
4935 "Inconsistent accessibility: return type `" +
4936 TypeManager.CSharpName (MemberType) + "' is less " +
4937 "accessible than operator `" + GetSignatureForError () + "'");
4939 Report.Error (50, Location,
4940 "Inconsistent accessibility: return type `" +
4941 TypeManager.CSharpName (MemberType) + "' is less " +
4942 "accessible than method `" + GetSignatureForError () + "'");
4944 Report.Error (52, Location,
4945 "Inconsistent accessibility: field type `" +
4946 TypeManager.CSharpName (MemberType) + "' is less " +
4947 "accessible than field `" + GetSignatureForError () + "'");
4952 if (IsExplicitImpl) {
4953 Expression expr = MemberName.Left.GetTypeExpression ();
4954 TypeExpr texpr = expr.ResolveAsTypeTerminal (ResolveContext, false);
4958 InterfaceType = texpr.Type;
4960 if (!InterfaceType.IsInterface) {
4961 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
4965 if (!Parent.PartialContainer.VerifyImplements (this))
4968 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
4974 protected bool IsTypePermitted ()
4976 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
4977 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
4983 protected override bool VerifyClsCompliance()
4985 if (base.VerifyClsCompliance ()) {
4989 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
4990 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
4998 // Fields and Events both generate FieldBuilders, we use this to share
4999 // their common bits. This is also used to flag usage of the field
5001 abstract public class FieldBase : MemberBase {
5002 public FieldBuilder FieldBuilder;
5003 public Status status;
5004 protected Expression initializer;
5005 ExpressionStatement initializerStatement;
5008 public enum Status : byte {
5009 HAS_OFFSET = 4 // Used by FieldMember.
5012 static string[] attribute_targets = new string [] { "field" };
5015 /// Symbol with same name in base class/struct
5017 public MemberInfo conflict_symbol;
5019 protected FieldBase (DeclSpace parent, Expression type, int mod,
5020 int allowed_mod, MemberName name, Attributes attrs)
5021 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
5026 public override AttributeTargets AttributeTargets {
5028 return AttributeTargets.Field;
5032 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5034 if (a.Type == TypeManager.marshal_as_attr_type) {
5035 UnmanagedMarshal marshal = a.GetMarshal (this);
5036 if (marshal != null) {
5037 FieldBuilder.SetMarshal (marshal);
5042 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5043 a.Error_InvalidSecurityParent ();
5047 FieldBuilder.SetCustomAttribute (cb);
5050 public void EmitInitializer (EmitContext ec)
5052 initializerStatement.EmitStatement (ec);
5055 protected override bool CheckBase ()
5057 if (!base.CheckBase ())
5064 conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5065 if (conflict_symbol == null) {
5066 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5067 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5072 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5073 Report.SymbolRelatedToPreviousError (conflict_symbol);
5074 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5075 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5081 public Expression Initializer {
5083 if (value != null) {
5084 this.initializer = value;
5085 Parent.PartialContainer.RegisterFieldForInitialization (this);
5090 protected virtual bool IsFieldClsCompliant {
5092 if (FieldBuilder == null)
5095 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5099 public Expression ResolveInitializer ()
5101 // TODO: again it's too heavy-weight
5102 EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
5103 ec.IsFieldInitializer = true;
5104 initializer = initializer.Resolve (ec);
5105 if (initializer == null)
5107 if (FieldBuilder == null)
5110 FieldExpr fe = new FieldExpr (FieldBuilder, Location, true);
5111 if ((ModFlags & Modifiers.STATIC) == 0)
5113 fe.InstanceExpression = CompilerGeneratedThis.Instance;
5116 initializerStatement = new Assign (fe, initializer, Location).ResolveStatement (ec);
5120 public bool HasDefaultInitializer
5124 Constant c = initializer as Constant;
5128 return c.IsDefaultInitializer (MemberType);
5132 public override string[] ValidAttributeTargets
5135 return attribute_targets;
5139 protected override bool VerifyClsCompliance ()
5141 if (!base.VerifyClsCompliance ())
5144 if (!IsFieldClsCompliant) {
5145 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5151 public void SetAssigned ()
5153 caching_flags |= Flags.IsAssigned;
5157 public abstract class FieldMember : FieldBase
5159 protected FieldMember (DeclSpace parent, Expression type, int mod,
5160 int allowed_mod, MemberName name, Attributes attrs)
5161 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, attrs)
5163 if ((mod & Modifiers.ABSTRACT) != 0)
5164 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5167 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5169 if (a.Type == TypeManager.field_offset_attribute_type)
5171 status |= Status.HAS_OFFSET;
5173 if (!Parent.PartialContainer.HasExplicitLayout) {
5174 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5178 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5179 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5185 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5186 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5191 base.ApplyAttributeBuilder (a, cb);
5194 public override bool Define()
5196 if (MemberType == null || Type == null)
5199 if (MemberType == TypeManager.void_type) {
5200 // TODO: wrong location
5201 Expression.Error_VoidInvalidInTheContext (Location);
5205 if (MemberType.IsSealed && MemberType.IsAbstract) {
5206 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5213 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5214 Report.Error (52, Location,
5215 "Inconsistent accessibility: field type `" +
5216 TypeManager.CSharpName (MemberType) + "' is less " +
5217 "accessible than field `" + GetSignatureForError () + "'");
5221 if (!IsTypePermitted ())
5227 public override void Emit ()
5229 if (OptAttributes != null) {
5230 OptAttributes.Emit ();
5233 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5234 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5240 public static void Error_VariableOfStaticClass (Location loc, string variableName, Type staticClass)
5242 Report.SymbolRelatedToPreviousError (staticClass);
5243 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5248 // Represents header string for documentation comment.
5250 public override string DocCommentHeader {
5251 get { return "F:"; }
5255 interface IFixedBuffer
5257 FieldInfo Element { get; }
5258 Type ElementType { get; }
5261 public class FixedFieldExternal: IFixedBuffer
5263 FieldInfo element_field;
5265 public FixedFieldExternal (FieldInfo fi)
5267 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5270 #region IFixedField Members
5272 public FieldInfo Element {
5274 return element_field;
5278 public Type ElementType {
5280 return element_field.FieldType;
5288 /// Fixed buffer implementation
5290 public class FixedField : FieldMember, IFixedBuffer
5292 public const string FixedElementName = "FixedElementField";
5293 static int GlobalCounter = 0;
5294 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5295 static FieldInfo[] fi;
5297 TypeBuilder fixed_buffer_type;
5298 FieldBuilder element;
5299 Expression size_expr;
5302 const int AllowedModifiers =
5305 Modifiers.PROTECTED |
5306 Modifiers.INTERNAL |
5309 public FixedField (DeclSpace parent, Expression type, int mod, string name,
5310 Expression size_expr, Attributes attrs, Location loc):
5311 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5313 if (RootContext.Version == LanguageVersion.ISO_1)
5314 Report.FeatureIsNotStandardized (loc, "fixed size buffers");
5316 this.size_expr = size_expr;
5319 public override bool Define()
5322 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5323 Report.Warning (-23, 1, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5326 if (Parent.PartialContainer.Kind != Kind.Struct) {
5327 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5328 GetSignatureForError ());
5332 if (!base.Define ())
5335 if (!TypeManager.IsPrimitiveType (MemberType)) {
5336 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",
5337 GetSignatureForError ());
5341 EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
5342 Constant c = size_expr.ResolveAsConstant (ec, this);
5346 IntConstant buffer_size_const = c.ToInt (Location);
5347 if (buffer_size_const == null)
5350 buffer_size = buffer_size_const.Value;
5352 if (buffer_size <= 0) {
5353 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5357 int type_size = Expression.GetTypeSize (MemberType);
5359 if (buffer_size > int.MaxValue / type_size) {
5360 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5361 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5365 buffer_size *= type_size;
5368 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5370 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5371 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5372 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5373 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5375 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5376 TypeManager.RegisterFieldBase (FieldBuilder, this);
5381 public override void Emit()
5384 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5386 object[] fi_val = new object[1];
5387 fi_val [0] = buffer_size;
5389 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5390 ctor_args, fi, fi_val);
5391 fixed_buffer_type.SetCustomAttribute (cab);
5394 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5395 FieldBuilder.SetCustomAttribute (cab);
5400 protected override bool IsFieldClsCompliant {
5406 #region IFixedField Members
5408 public FieldInfo Element {
5414 public Type ElementType {
5424 // The Field class is used to represents class/struct fields during parsing.
5426 public class Field : FieldMember {
5428 // Modifiers allowed in a class declaration
5430 const int AllowedModifiers =
5433 Modifiers.PROTECTED |
5434 Modifiers.INTERNAL |
5437 Modifiers.VOLATILE |
5441 public Field (DeclSpace parent, Expression type, int mod, string name,
5442 Attributes attrs, Location loc)
5443 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5448 public override bool Define ()
5450 if (!base.Define ())
5453 if (RootContext.WarningLevel > 1){
5454 Type ptype = Parent.TypeBuilder.BaseType;
5456 // ptype is only null for System.Object while compiling corlib.
5458 TypeContainer.FindMembers (
5459 ptype, MemberTypes.Method,
5460 BindingFlags.Public |
5461 BindingFlags.Static | BindingFlags.Instance,
5462 System.Type.FilterName, Name);
5466 if ((ModFlags & Modifiers.VOLATILE) != 0){
5467 if (!MemberType.IsClass){
5468 Type vt = MemberType;
5470 if (TypeManager.IsEnumType (vt))
5471 vt = TypeManager.EnumToUnderlying (MemberType);
5473 if (!((vt == TypeManager.bool_type) ||
5474 (vt == TypeManager.sbyte_type) ||
5475 (vt == TypeManager.byte_type) ||
5476 (vt == TypeManager.short_type) ||
5477 (vt == TypeManager.ushort_type) ||
5478 (vt == TypeManager.int32_type) ||
5479 (vt == TypeManager.uint32_type) ||
5480 (vt == TypeManager.char_type) ||
5481 (vt == TypeManager.float_type) ||
5482 (!vt.IsValueType))){
5483 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5484 GetSignatureForError (), TypeManager.CSharpName (vt));
5489 if ((ModFlags & Modifiers.READONLY) != 0){
5490 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5491 GetSignatureForError ());
5496 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5498 if (Parent.PartialContainer.Kind == Kind.Struct &&
5499 ((fa & FieldAttributes.Static) == 0) &&
5500 MemberType == Parent.TypeBuilder &&
5501 !TypeManager.IsBuiltinType (MemberType)){
5502 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5503 "' causes a cycle in the structure layout");
5508 FieldBuilder = Parent.TypeBuilder.DefineField (
5509 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5511 TypeManager.RegisterFieldBase (FieldBuilder, this);
5513 catch (ArgumentException) {
5514 Report.Warning (-24, 1, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5521 protected override bool VerifyClsCompliance ()
5523 if (!base.VerifyClsCompliance ())
5526 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5527 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5535 // `set' and `get' accessors are represented with an Accessor.
5537 public class Accessor : IIteratorContainer {
5539 // Null if the accessor is empty, or a Block if not
5541 public const int AllowedModifiers =
5543 Modifiers.PROTECTED |
5544 Modifiers.INTERNAL |
5547 public ToplevelBlock Block;
5548 public Attributes Attributes;
5549 public Location Location;
5550 public int ModFlags;
5553 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5558 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5561 public void SetYields ()
5567 // Ooouh Martin, templates are missing here.
5568 // When it will be possible move here a lot of child code and template method type.
5569 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5570 protected MethodData method_data;
5571 protected ToplevelBlock block;
5572 protected ListDictionary declarative_security;
5574 // The accessor are created event if they are not wanted.
5575 // But we need them because their names are reserved.
5576 // Field says whether accessor will be emited or not
5577 public readonly bool IsDummy;
5579 protected readonly string prefix;
5581 ReturnParameter return_attributes;
5583 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5584 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5586 this.prefix = prefix;
5590 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5592 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5593 accessor.Attributes)
5595 this.prefix = prefix;
5596 this.block = accessor.Block;
5599 static MemberName SetupName (string prefix, MemberBase member, Location loc)
5601 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5604 public void UpdateName (MemberBase member)
5606 SetMemberName (SetupName (prefix, member, Location));
5609 #region IMethodData Members
5611 public ToplevelBlock Block {
5621 public CallingConventions CallingConventions {
5623 return CallingConventions.Standard;
5627 public bool IsExcluded ()
5632 public MemberName MethodName {
5638 public Type[] ParameterTypes {
5640 return ParameterInfo.Types;
5644 public abstract Parameters ParameterInfo { get ; }
5645 public abstract Type ReturnType { get; }
5646 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
5650 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5652 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5653 a.Type == TypeManager.conditional_attribute_type) {
5654 Report.Error (1667, a.Location,
5655 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
5656 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5660 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5661 if (declarative_security == null)
5662 declarative_security = new ListDictionary ();
5663 a.ExtractSecurityPermissionSet (declarative_security);
5667 if (a.Target == AttributeTargets.Method) {
5668 method_data.MethodBuilder.SetCustomAttribute (cb);
5672 if (a.Target == AttributeTargets.ReturnValue) {
5673 if (return_attributes == null)
5674 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5676 return_attributes.ApplyAttributeBuilder (a, cb);
5680 ApplyToExtraTarget (a, cb);
5683 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5685 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5688 public override bool Define()
5690 throw new NotSupportedException ();
5693 public void Emit (DeclSpace parent)
5695 EmitMethod (parent);
5697 if (OptAttributes != null)
5698 OptAttributes.Emit ();
5700 if (declarative_security != null) {
5701 foreach (DictionaryEntry de in declarative_security) {
5702 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5709 protected virtual void EmitMethod (DeclSpace parent)
5711 method_data.Emit (parent);
5714 public override bool IsClsComplianceRequired()
5719 public bool IsDuplicateImplementation (MethodCore method)
5721 if (!MemberName.Equals (method.MemberName))
5724 Type[] param_types = method.ParameterTypes;
5726 if (param_types.Length != ParameterTypes.Length)
5729 for (int i = 0; i < param_types.Length; i++)
5730 if (param_types [i] != ParameterTypes [i])
5733 Report.SymbolRelatedToPreviousError (method);
5734 Report.Error (111, Location, TypeContainer.Error111, method.GetSignatureForError ());
5738 public override bool IsUsed
5748 public new Location Location {
5750 return base.Location;
5755 // Represents header string for documentation comment.
5757 public override string DocCommentHeader {
5758 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5764 // Properties and Indexers both generate PropertyBuilders, we use this to share
5765 // their common bits.
5767 abstract public class PropertyBase : MethodCore {
5769 public class GetMethod : PropertyMethod
5771 static string[] attribute_targets = new string [] { "method", "return" };
5773 public GetMethod (MethodCore method):
5774 base (method, "get_")
5778 public GetMethod (MethodCore method, Accessor accessor):
5779 base (method, accessor, "get_")
5783 public override MethodBuilder Define (DeclSpace parent)
5785 base.Define (parent);
5787 method_data = new MethodData (method, ModFlags, flags, this);
5789 if (!method_data.Define (parent))
5792 return method_data.MethodBuilder;
5795 public override Type ReturnType {
5797 return method.MemberType;
5801 public override Parameters ParameterInfo {
5803 return Parameters.EmptyReadOnlyParameters;
5807 public override string[] ValidAttributeTargets {
5809 return attribute_targets;
5814 public class SetMethod : PropertyMethod {
5816 static string[] attribute_targets = new string [] { "method", "param", "return" };
5817 ImplicitParameter param_attr;
5818 protected Parameters parameters;
5820 public SetMethod (MethodCore method):
5821 base (method, "set_")
5825 public SetMethod (MethodCore method, Accessor accessor):
5826 base (method, accessor, "set_")
5830 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
5832 if (a.Target == AttributeTargets.Parameter) {
5833 if (param_attr == null)
5834 param_attr = new ImplicitParameter (method_data.MethodBuilder);
5836 param_attr.ApplyAttributeBuilder (a, cb);
5840 base.ApplyAttributeBuilder (a, cb);
5843 public override Parameters ParameterInfo {
5849 protected virtual void DefineParameters ()
5851 parameters = new Parameters (
5852 new Parameter[] { new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location) },
5853 new Type[] { method.MemberType });
5856 public override MethodBuilder Define (DeclSpace parent)
5858 DefineParameters ();
5862 base.Define (parent);
5864 method_data = new MethodData (method, ModFlags, flags, this);
5866 if (!method_data.Define (parent))
5869 return method_data.MethodBuilder;
5872 public override Type ReturnType {
5874 return TypeManager.void_type;
5878 public override string[] ValidAttributeTargets {
5880 return attribute_targets;
5885 static string[] attribute_targets = new string [] { "property" };
5887 public abstract class PropertyMethod : AbstractPropertyEventMethod
5889 protected readonly MethodCore method;
5890 protected MethodAttributes flags;
5893 public PropertyMethod (MethodCore method, string prefix)
5894 : base (method, prefix)
5896 this.method = method;
5899 public PropertyMethod (MethodCore method, Accessor accessor,
5901 : base (method, accessor, prefix)
5903 this.method = method;
5904 this.ModFlags = accessor.ModFlags;
5905 yields = accessor.Yields;
5907 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
5908 Report.FeatureIsNotStandardized (Location, "access modifiers on properties");
5912 public override AttributeTargets AttributeTargets {
5914 return AttributeTargets.Method;
5918 public override bool IsClsComplianceRequired()
5920 return method.IsClsComplianceRequired ();
5923 public virtual MethodBuilder Define (DeclSpace parent)
5925 if (!method.CheckAbstractAndExtern (block != null))
5928 TypeContainer container = parent.PartialContainer;
5931 // Check for custom access modifier
5933 if (ModFlags == 0) {
5934 ModFlags = method.ModFlags;
5935 flags = method.flags;
5937 if (container.Kind == Kind.Interface)
5938 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
5939 GetSignatureForError ());
5941 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
5942 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
5945 CheckModifiers (ModFlags);
5946 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
5947 ModFlags |= Modifiers.PROPERTY_CUSTOM;
5948 flags = Modifiers.MethodAttr (ModFlags);
5949 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
5953 // Setup iterator if we are one
5956 Iterator iterator = new Iterator (this, Parent as TypeContainer, ModFlags);
5958 if (!iterator.DefineIterator ())
5965 public bool HasCustomAccessModifier
5968 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
5972 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
5974 return new EmitContext (method,
5975 ds, method.Parent, method.Location, ig, ReturnType,
5976 method.ModFlags, false);
5979 public override ObsoleteAttribute GetObsoleteAttribute ()
5981 return method.GetObsoleteAttribute ();
5984 public override string GetSignatureForError()
5986 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
5989 void CheckModifiers (int modflags)
5992 int mflags = method.ModFlags & Modifiers.Accessibility;
5994 if ((mflags & Modifiers.PUBLIC) != 0) {
5995 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
5997 else if ((mflags & Modifiers.PROTECTED) != 0) {
5998 if ((mflags & Modifiers.INTERNAL) != 0)
5999 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6001 flags |= Modifiers.PRIVATE;
6003 else if ((mflags & Modifiers.INTERNAL) != 0)
6004 flags |= Modifiers.PRIVATE;
6006 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6007 Report.Error (273, Location,
6008 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6009 GetSignatureForError (), method.GetSignatureForError ());
6013 public override bool MarkForDuplicationCheck ()
6015 caching_flags |= Flags.TestMethodDuplication;
6020 public PropertyMethod Get, Set;
6021 public PropertyBuilder PropertyBuilder;
6022 public MethodBuilder GetBuilder, SetBuilder;
6024 protected EmitContext ec;
6026 public PropertyBase (DeclSpace parent, Expression type, int mod_flags,
6027 int allowed_mod, bool is_iface, MemberName name,
6028 Parameters parameters, Attributes attrs)
6029 : base (parent, type, mod_flags, allowed_mod, is_iface, name,
6034 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6036 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6037 a.Error_InvalidSecurityParent ();
6041 PropertyBuilder.SetCustomAttribute (cb);
6044 public override AttributeTargets AttributeTargets {
6046 return AttributeTargets.Property;
6050 public override bool Define ()
6055 if (!IsTypePermitted ())
6061 protected override bool DoDefine ()
6063 if (!base.DoDefine ())
6067 // Accessors modifiers check
6069 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6070 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6071 GetSignatureForError ());
6075 if ((Get.IsDummy || Set.IsDummy)
6076 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6077 Report.Error (276, Location,
6078 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6079 GetSignatureForError ());
6083 if (MemberType.IsAbstract && MemberType.IsSealed) {
6084 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6091 protected override bool CheckForDuplications ()
6093 ArrayList ar = Parent.PartialContainer.Indexers;
6095 int arLen = ar.Count;
6097 for (int i = 0; i < arLen; i++) {
6098 Indexer m = (Indexer) ar [i];
6099 if (IsDuplicateImplementation (m))
6104 ar = Parent.PartialContainer.Properties;
6106 int arLen = ar.Count;
6108 for (int i = 0; i < arLen; i++) {
6109 Property m = (Property) ar [i];
6110 if (IsDuplicateImplementation (m))
6118 // TODO: rename to Resolve......
6119 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6121 PropertyInfo base_property = Parent.PartialContainer.BaseCache.FindMemberToOverride (
6122 Parent.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
6124 if (base_property == null)
6127 base_ret_type = base_property.PropertyType;
6128 MethodInfo get_accessor = base_property.GetGetMethod (true);
6129 MethodInfo set_accessor = base_property.GetSetMethod (true);
6130 MethodAttributes get_accessor_access, set_accessor_access;
6132 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6133 if (Get != null && !Get.IsDummy && get_accessor == null) {
6134 Report.SymbolRelatedToPreviousError (base_property);
6135 Report.Error (545, Location, "`{0}.get': cannot override because `{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6138 if (Set != null && !Set.IsDummy && set_accessor == null) {
6139 Report.SymbolRelatedToPreviousError (base_property);
6140 Report.Error (546, Location, "`{0}.set': cannot override because `{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6145 // Check base class accessors access
6148 // TODO: rewrite to reuse Get|Set.CheckAccessModifiers and share code there
6149 get_accessor_access = set_accessor_access = 0;
6150 if ((ModFlags & Modifiers.NEW) == 0) {
6151 if (get_accessor != null) {
6152 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6153 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6155 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6156 Error_CannotChangeAccessModifiers (get_accessor, get_accessor_access, ".get");
6159 if (set_accessor != null) {
6160 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6161 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6163 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6164 Error_CannotChangeAccessModifiers (set_accessor, set_accessor_access, ".set");
6169 // Get the less restrictive access
6171 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6174 public override void Emit ()
6177 // The PropertyBuilder can be null for explicit implementations, in that
6178 // case, we do not actually emit the ".property", so there is nowhere to
6179 // put the attribute
6181 if (PropertyBuilder != null && OptAttributes != null)
6182 OptAttributes.Emit ();
6194 /// Tests whether accessors are not in collision with some method (CS0111)
6196 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6198 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6201 public override bool IsUsed
6207 return Get.IsUsed | Set.IsUsed;
6211 protected override void SetMemberName (MemberName new_name)
6213 base.SetMemberName (new_name);
6215 Get.UpdateName (this);
6216 Set.UpdateName (this);
6219 public override string[] ValidAttributeTargets {
6221 return attribute_targets;
6226 // Represents header string for documentation comment.
6228 public override string DocCommentHeader {
6229 get { return "P:"; }
6233 public class Property : PropertyBase {
6234 const int AllowedModifiers =
6237 Modifiers.PROTECTED |
6238 Modifiers.INTERNAL |
6242 Modifiers.OVERRIDE |
6243 Modifiers.ABSTRACT |
6246 Modifiers.METHOD_YIELDS |
6249 const int AllowedInterfaceModifiers =
6252 public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
6253 MemberName name, Attributes attrs, Accessor get_block,
6255 : base (parent, type, mod,
6256 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6257 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs)
6259 if (get_block == null)
6260 Get = new GetMethod (this);
6262 Get = new GetMethod (this, get_block);
6264 if (set_block == null)
6265 Set = new SetMethod (this);
6267 Set = new SetMethod (this, set_block);
6270 public override bool Define ()
6272 if (!base.Define ())
6278 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6281 GetBuilder = Get.Define (Parent);
6282 if (GetBuilder == null)
6286 SetBuilder = Set.Define (Parent);
6288 if (SetBuilder == null)
6292 // FIXME - PropertyAttributes.HasDefault ?
6294 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6295 Name, PropertyAttributes.None, MemberType, null);
6298 PropertyBuilder.SetGetMethod (GetBuilder);
6301 PropertyBuilder.SetSetMethod (SetBuilder);
6303 TypeManager.RegisterProperty (PropertyBuilder, this);
6309 /// Gigantic workaround for lameness in SRE follows :
6310 /// This class derives from EventInfo and attempts to basically
6311 /// wrap around the EventBuilder so that FindMembers can quickly
6312 /// return this in it search for members
6314 public class MyEventBuilder : EventInfo {
6317 // We use this to "point" to our Builder which is
6318 // not really a MemberInfo
6320 EventBuilder MyBuilder;
6323 // We "catch" and wrap these methods
6325 MethodInfo raise, remove, add;
6327 EventAttributes attributes;
6328 Type declaring_type, reflected_type, event_type;
6333 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6335 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6337 // And now store the values in our own fields.
6339 declaring_type = type_builder;
6341 reflected_type = type_builder;
6343 attributes = event_attr;
6346 this.event_type = event_type;
6350 // Methods that you have to override. Note that you only need
6351 // to "implement" the variants that take the argument (those are
6352 // the "abstract" methods, the others (GetAddMethod()) are
6355 public override MethodInfo GetAddMethod (bool nonPublic)
6360 public override MethodInfo GetRemoveMethod (bool nonPublic)
6365 public override MethodInfo GetRaiseMethod (bool nonPublic)
6371 // These methods make "MyEventInfo" look like a Builder
6373 public void SetRaiseMethod (MethodBuilder raiseMethod)
6375 raise = raiseMethod;
6376 MyBuilder.SetRaiseMethod (raiseMethod);
6379 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6381 remove = removeMethod;
6382 MyBuilder.SetRemoveOnMethod (removeMethod);
6385 public void SetAddOnMethod (MethodBuilder addMethod)
6388 MyBuilder.SetAddOnMethod (addMethod);
6391 public void SetCustomAttribute (CustomAttributeBuilder cb)
6393 MyBuilder.SetCustomAttribute (cb);
6396 public override object [] GetCustomAttributes (bool inherit)
6398 // FIXME : There's nothing which can be seemingly done here because
6399 // we have no way of getting at the custom attribute objects of the
6404 public override object [] GetCustomAttributes (Type t, bool inherit)
6406 // FIXME : Same here !
6410 public override bool IsDefined (Type t, bool b)
6415 public override EventAttributes Attributes {
6421 public override string Name {
6427 public override Type DeclaringType {
6429 return declaring_type;
6433 public override Type ReflectedType {
6435 return reflected_type;
6439 public Type EventType {
6445 public void SetUsed ()
6447 if (my_event != null) {
6448 my_event.SetAssigned ();
6449 my_event.SetMemberIsUsed ();
6455 /// For case when event is declared like property (with add and remove accessors).
6457 public class EventProperty: Event {
6459 static string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6461 public EventProperty (DeclSpace parent, Expression type, int mod_flags,
6462 bool is_iface, MemberName name,
6463 Attributes attrs, Accessor add, Accessor remove)
6464 : base (parent, type, mod_flags, is_iface, name, attrs)
6466 Add = new AddDelegateMethod (this, add);
6467 Remove = new RemoveDelegateMethod (this, remove);
6469 // For this event syntax we don't report error CS0067
6470 // because it is hard to do it.
6474 public override string[] ValidAttributeTargets {
6476 return attribute_targets;
6482 /// Event is declared like field.
6484 public class EventField : Event {
6486 static string[] attribute_targets = new string [] { "event", "field", "method" };
6487 static string[] attribute_targets_interface = new string[] { "event", "method" };
6489 public EventField (DeclSpace parent, Expression type, int mod_flags,
6490 bool is_iface, MemberName name,
6492 : base (parent, type, mod_flags, is_iface, name, attrs)
6494 Add = new AddDelegateMethod (this);
6495 Remove = new RemoveDelegateMethod (this);
6498 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6500 if (a.Target == AttributeTargets.Field) {
6501 FieldBuilder.SetCustomAttribute (cb);
6505 if (a.Target == AttributeTargets.Method) {
6506 Add.ApplyAttributeBuilder (a, cb);
6507 Remove.ApplyAttributeBuilder (a, cb);
6511 base.ApplyAttributeBuilder (a, cb);
6514 public override bool Define()
6516 if (!base.Define ())
6519 if (initializer != null) {
6520 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
6521 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
6522 GetSignatureForError ());
6530 public override string[] ValidAttributeTargets {
6532 return IsInterface ? attribute_targets_interface : attribute_targets;
6537 public abstract class Event : FieldBase {
6539 protected sealed class AddDelegateMethod: DelegateMethod
6542 public AddDelegateMethod (Event method):
6543 base (method, "add_")
6547 public AddDelegateMethod (Event method, Accessor accessor):
6548 base (method, accessor, "add_")
6552 protected override MethodInfo DelegateMethodInfo {
6554 return TypeManager.delegate_combine_delegate_delegate;
6560 protected sealed class RemoveDelegateMethod: DelegateMethod
6562 public RemoveDelegateMethod (Event method):
6563 base (method, "remove_")
6567 public RemoveDelegateMethod (Event method, Accessor accessor):
6568 base (method, accessor, "remove_")
6572 protected override MethodInfo DelegateMethodInfo {
6574 return TypeManager.delegate_remove_delegate_delegate;
6580 public abstract class DelegateMethod : AbstractPropertyEventMethod
6582 protected readonly Event method;
6583 ImplicitParameter param_attr;
6585 static string[] attribute_targets = new string [] { "method", "param", "return" };
6587 public DelegateMethod (Event method, string prefix)
6588 : base (method, prefix)
6590 this.method = method;
6593 public DelegateMethod (Event method, Accessor accessor, string prefix)
6594 : base (method, accessor, prefix)
6596 this.method = method;
6599 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6601 if (a.Target == AttributeTargets.Parameter) {
6602 if (param_attr == null)
6603 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6605 param_attr.ApplyAttributeBuilder (a, cb);
6609 base.ApplyAttributeBuilder (a, cb);
6612 public override AttributeTargets AttributeTargets {
6614 return AttributeTargets.Method;
6618 public override bool IsClsComplianceRequired ()
6620 return method.IsClsComplianceRequired ();
6623 public MethodBuilder Define (DeclSpace parent)
6625 method_data = new MethodData (method, method.ModFlags,
6626 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6628 if (!method_data.Define (parent))
6631 MethodBuilder mb = method_data.MethodBuilder;
6632 ParameterInfo.ApplyAttributes (mb);
6637 protected override void EmitMethod (DeclSpace parent)
6639 if (block != null) {
6640 base.EmitMethod (parent);
6644 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
6647 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6648 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6650 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6651 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6652 ig.Emit (OpCodes.Ldsfld, field_info);
6653 ig.Emit (OpCodes.Ldarg_0);
6654 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6655 ig.Emit (OpCodes.Castclass, method.MemberType);
6656 ig.Emit (OpCodes.Stsfld, field_info);
6658 ig.Emit (OpCodes.Ldarg_0);
6659 ig.Emit (OpCodes.Ldarg_0);
6660 ig.Emit (OpCodes.Ldfld, field_info);
6661 ig.Emit (OpCodes.Ldarg_1);
6662 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6663 ig.Emit (OpCodes.Castclass, method.MemberType);
6664 ig.Emit (OpCodes.Stfld, field_info);
6666 ig.Emit (OpCodes.Ret);
6669 protected abstract MethodInfo DelegateMethodInfo { get; }
6671 public override Type ReturnType {
6673 return TypeManager.void_type;
6677 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6679 return new EmitContext (method,
6680 ds, method.Parent, Location, ig, ReturnType,
6681 method.ModFlags, false);
6684 public override ObsoleteAttribute GetObsoleteAttribute ()
6686 return method.GetObsoleteAttribute ();
6689 public override string[] ValidAttributeTargets {
6691 return attribute_targets;
6695 public override Parameters ParameterInfo {
6697 return method.parameters;
6704 const int AllowedModifiers =
6707 Modifiers.PROTECTED |
6708 Modifiers.INTERNAL |
6713 Modifiers.OVERRIDE |
6717 const int AllowedInterfaceModifiers =
6720 public DelegateMethod Add, Remove;
6721 public MyEventBuilder EventBuilder;
6722 public MethodBuilder AddBuilder, RemoveBuilder;
6723 Parameters parameters;
6725 protected Event (DeclSpace parent, Expression type, int mod_flags,
6726 bool is_iface, MemberName name, Attributes attrs)
6727 : base (parent, type, mod_flags,
6728 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6731 IsInterface = is_iface;
6734 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6736 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6737 a.Error_InvalidSecurityParent ();
6741 EventBuilder.SetCustomAttribute (cb);
6744 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6746 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6749 public override AttributeTargets AttributeTargets {
6751 return AttributeTargets.Event;
6755 public override bool Define ()
6757 EventAttributes e_attr;
6758 e_attr = EventAttributes.None;
6763 if (!TypeManager.IsDelegateType (MemberType)) {
6764 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
6768 parameters = new Parameters (
6769 new Parameter[] { new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location) },
6770 new Type[] { MemberType } );
6776 // Now define the accessors
6779 AddBuilder = Add.Define (Parent);
6780 if (AddBuilder == null)
6783 RemoveBuilder = Remove.Define (Parent);
6784 if (RemoveBuilder == null)
6787 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
6789 if (Add.Block == null && Remove.Block == null && !IsInterface) {
6790 FieldBuilder = Parent.TypeBuilder.DefineField (
6792 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
6793 TypeManager.RegisterPrivateFieldOfEvent (
6794 (EventInfo) EventBuilder, FieldBuilder);
6795 TypeManager.RegisterFieldBase (FieldBuilder, this);
6798 EventBuilder.SetAddOnMethod (AddBuilder);
6799 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
6801 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
6805 protected override bool CheckBase ()
6807 if (!base.CheckBase ())
6810 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
6811 if (!(conflict_symbol is EventInfo)) {
6812 Report.SymbolRelatedToPreviousError (conflict_symbol);
6813 Report.Error (72, Location, "Event `{0}' can override only event", GetSignatureForError ());
6821 public override void Emit ()
6823 if (OptAttributes != null) {
6824 OptAttributes.Emit ();
6828 Remove.Emit (Parent);
6833 public override string GetSignatureForError ()
6835 return base.GetSignatureForError ();
6839 // Represents header string for documentation comment.
6841 public override string DocCommentHeader {
6842 get { return "E:"; }
6847 public class Indexer : PropertyBase {
6849 class GetIndexerMethod : GetMethod
6851 public GetIndexerMethod (MethodCore method):
6856 public GetIndexerMethod (MethodCore method, Accessor accessor):
6857 base (method, accessor)
6861 public override Parameters ParameterInfo {
6863 return method.ParameterInfo;
6868 class SetIndexerMethod: SetMethod
6870 public SetIndexerMethod (MethodCore method):
6875 public SetIndexerMethod (MethodCore method, Accessor accessor):
6876 base (method, accessor)
6880 protected override void DefineParameters ()
6882 parameters = Parameters.MergeGenerated (method.Parameters,
6883 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
6887 const int AllowedModifiers =
6890 Modifiers.PROTECTED |
6891 Modifiers.INTERNAL |
6895 Modifiers.OVERRIDE |
6900 const int AllowedInterfaceModifiers =
6904 public Indexer (DeclSpace parent, Expression type, MemberName name, int mod,
6905 bool is_iface, Parameters parameters, Attributes attrs,
6906 Accessor get_block, Accessor set_block)
6907 : base (parent, type, mod,
6908 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6909 is_iface, name, parameters, attrs)
6911 if (get_block == null)
6912 Get = new GetIndexerMethod (this);
6914 Get = new GetIndexerMethod (this, get_block);
6916 if (set_block == null)
6917 Set = new SetIndexerMethod (this);
6919 Set = new SetIndexerMethod (this, set_block);
6922 public override bool Define ()
6924 if (!base.Define ())
6927 if (MemberType == TypeManager.void_type) {
6928 Report.Error (620, Location, "Indexers cannot have void type");
6932 if (OptAttributes != null) {
6933 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
6934 if (indexer_attr != null) {
6935 // Remove the attribute from the list because it is not emitted
6936 OptAttributes.Attrs.Remove (indexer_attr);
6938 string name = indexer_attr.GetIndexerAttributeValue ();
6944 if (IsExplicitImpl) {
6945 Report.Error (415, indexer_attr.Location,
6946 "The `IndexerName' attribute is valid only on an " +
6947 "indexer that is not an explicit interface member declaration");
6951 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6952 Report.Error (609, indexer_attr.Location,
6953 "Cannot set the `IndexerName' attribute on an indexer marked override");
6959 if (InterfaceType != null) {
6960 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
6961 if (base_IndexerName != Name)
6962 ShortName = base_IndexerName;
6965 if (!Parent.PartialContainer.AddMember (this) ||
6966 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
6972 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6974 GetBuilder = Get.Define (Parent);
6975 if (GetBuilder == null)
6979 SetBuilder = Set.Define (Parent);
6981 if (SetBuilder == null)
6986 // Now name the parameters
6988 Parameter [] p = Parameters.FixedParameters;
6990 // TODO: should be done in parser and it needs to do cycle
6991 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
6992 Report.Error (631, Location, "ref and out are not valid in this context");
6997 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6998 Name, PropertyAttributes.None, MemberType, ParameterTypes);
7001 PropertyBuilder.SetGetMethod (GetBuilder);
7004 PropertyBuilder.SetSetMethod (SetBuilder);
7006 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
7011 public override string GetSignatureForError ()
7013 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7014 if (MemberName.Left != null) {
7016 sb.Append (MemberName.Left);
7019 sb.Append (".this");
7020 sb.Append (Parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7021 return sb.ToString ();
7024 public override bool MarkForDuplicationCheck ()
7026 caching_flags |= Flags.TestMethodDuplication;
7031 public class Operator : MethodOrOperator, IIteratorContainer {
7033 const int AllowedModifiers =
7039 public enum OpType : byte {
7049 // Unary and Binary operators
7072 // Implicit and Explicit
7076 // Just because of enum
7080 public readonly OpType OperatorType;
7082 public Operator (DeclSpace parent, OpType type, Expression ret_type,
7083 int mod_flags, Parameters parameters,
7084 ToplevelBlock block, Attributes attrs, Location loc)
7085 : base (parent, ret_type, mod_flags, AllowedModifiers, false,
7086 new MemberName ("op_" + type, loc), attrs, parameters)
7088 OperatorType = type;
7092 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7094 if (a.Type == TypeManager.conditional_attribute_type) {
7095 Error_ConditionalAttributeIsNotValid ();
7099 base.ApplyAttributeBuilder (a, cb);
7102 protected override bool CheckForDuplications ()
7104 ArrayList ar = Parent.PartialContainer.Operators;
7106 int arLen = ar.Count;
7108 for (int i = 0; i < arLen; i++) {
7109 Operator o = (Operator) ar [i];
7110 if (IsDuplicateImplementation (o))
7115 ar = Parent.PartialContainer.Methods;
7117 int arLen = ar.Count;
7119 for (int i = 0; i < arLen; i++) {
7120 Method m = (Method) ar [i];
7121 if (IsDuplicateImplementation (m))
7129 public override bool Define ()
7131 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7132 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7133 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7137 if (!base.Define ())
7140 if (MemberType == TypeManager.void_type) {
7141 Report.Error (590, Location, "User-defined operators cannot return void");
7145 Type declaring_type = MethodBuilder.DeclaringType;
7146 Type return_type = MemberType;
7147 Type first_arg_type = ParameterTypes [0];
7149 // Rules for conversion operators
7151 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7152 if (first_arg_type == return_type && first_arg_type == declaring_type){
7153 Report.Error (555, Location,
7154 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7158 if (first_arg_type != declaring_type && return_type != declaring_type){
7161 "User-defined conversion must convert to or from the " +
7166 if (first_arg_type.IsInterface || return_type.IsInterface){
7167 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7168 GetSignatureForError ());
7172 if (first_arg_type.IsSubclassOf (return_type) || return_type.IsSubclassOf (first_arg_type)) {
7173 if (declaring_type.IsSubclassOf (return_type) || declaring_type.IsSubclassOf (first_arg_type)) {
7174 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from base class",
7175 GetSignatureForError ());
7178 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from derived class",
7179 GetSignatureForError ());
7182 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7183 if (first_arg_type != declaring_type || ParameterTypes [1] != TypeManager.int32_type) {
7184 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");
7187 } else if (Parameters.Count == 1) {
7188 // Checks for Unary operators
7190 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7191 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7192 Report.Error (448, Location,
7193 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7196 if (first_arg_type != declaring_type) {
7198 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7203 if (first_arg_type != declaring_type){
7206 "The parameter of a unary operator must be the " +
7211 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7212 if (return_type != TypeManager.bool_type){
7215 "The return type of operator True or False " +
7222 // Checks for Binary operators
7224 if (first_arg_type != declaring_type &&
7225 ParameterTypes [1] != declaring_type){
7228 "One of the parameters of a binary operator must " +
7229 "be the containing type");
7237 protected override bool DoDefine ()
7239 if (!base.DoDefine ())
7242 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7246 public override void Emit ()
7250 Parameters.ApplyAttributes (MethodBuilder);
7253 // abstract or extern methods have no bodies
7255 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7259 if ((flags & MethodAttributes.PinvokeImpl) == 0)
7260 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
7262 ec = CreateEmitContext (Parent, null);
7264 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
7265 ec.EmitTopBlock (this, Block);
7268 source.CloseMethod ();
7273 // Operator cannot be override
7274 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7279 public static string GetName (OpType ot)
7282 case OpType.LogicalNot:
7284 case OpType.OnesComplement:
7286 case OpType.Increment:
7288 case OpType.Decrement:
7294 case OpType.Addition:
7296 case OpType.Subtraction:
7298 case OpType.UnaryPlus:
7300 case OpType.UnaryNegation:
7302 case OpType.Multiply:
7304 case OpType.Division:
7306 case OpType.Modulus:
7308 case OpType.BitwiseAnd:
7310 case OpType.BitwiseOr:
7312 case OpType.ExclusiveOr:
7314 case OpType.LeftShift:
7316 case OpType.RightShift:
7318 case OpType.Equality:
7320 case OpType.Inequality:
7322 case OpType.GreaterThan:
7324 case OpType.LessThan:
7326 case OpType.GreaterThanOrEqual:
7328 case OpType.LessThanOrEqual:
7330 case OpType.Implicit:
7332 case OpType.Explicit:
7338 public static OpType GetOperatorType (string name)
7340 if (name.StartsWith ("op_")){
7341 for (int i = 0; i < Unary.oper_names.Length; ++i) {
7342 if (Unary.oper_names [i] == name)
7346 for (int i = 0; i < Binary.oper_names.Length; ++i) {
7347 if (Binary.oper_names [i] == name)
7354 public override string GetSignatureForError ()
7356 StringBuilder sb = new StringBuilder ();
7357 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7358 sb.AppendFormat ("{0}.{1} operator {2}", Parent.GetSignatureForError (), GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type));
7361 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7364 sb.Append (Parameters.GetSignatureForError ());
7365 return sb.ToString ();
7370 // This is used to compare method signatures
7372 struct MethodSignature {
7374 public Type RetType;
7375 public Type [] Parameters;
7378 /// This delegate is used to extract methods which have the
7379 /// same signature as the argument
7381 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7383 public MethodSignature (string name, Type ret_type, Type [] parameters)
7388 if (parameters == null)
7389 Parameters = Type.EmptyTypes;
7391 Parameters = parameters;
7394 public override string ToString ()
7397 if (Parameters.Length != 0){
7398 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7399 for (int i = 0; i < Parameters.Length; i++){
7400 sb.Append (Parameters [i]);
7401 if (i+1 < Parameters.Length)
7404 pars = sb.ToString ();
7407 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7410 public override int GetHashCode ()
7412 return Name.GetHashCode ();
7415 public override bool Equals (Object o)
7417 MethodSignature other = (MethodSignature) o;
7419 if (other.Name != Name)
7422 if (other.RetType != RetType)
7425 if (Parameters == null){
7426 if (other.Parameters == null)
7431 if (other.Parameters == null)
7434 int c = Parameters.Length;
7435 if (other.Parameters.Length != c)
7438 for (int i = 0; i < c; i++)
7439 if (other.Parameters [i] != Parameters [i])
7445 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7447 MethodSignature sig = (MethodSignature) filter_criteria;
7449 if (m.Name != sig.Name)
7453 MethodInfo mi = m as MethodInfo;
7454 PropertyInfo pi = m as PropertyInfo;
7457 ReturnType = mi.ReturnType;
7458 else if (pi != null)
7459 ReturnType = pi.PropertyType;
7464 // we use sig.RetType == null to mean `do not check the
7465 // method return value.
7467 if (sig.RetType != null)
7468 if (ReturnType != sig.RetType)
7473 args = TypeManager.GetParameterData (mi).Types;
7475 args = TypeManager.GetArgumentTypes (pi);
7476 Type [] sigp = sig.Parameters;
7478 if (args.Length != sigp.Length)
7481 for (int i = args.Length; i > 0; ){
7483 if (args [i] != sigp [i])