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;
46 using Mono.CompilerServices.SymbolWriter;
48 namespace Mono.CSharp {
58 /// This is the base class for structs and classes.
60 public abstract class TypeContainer : DeclSpace, IMemberContainer {
62 public class MemberCoreArrayList: ArrayList
65 /// Defines the MemberCore objects that are in this array
67 public virtual void DefineContainerMembers ()
69 foreach (MemberCore mc in this) {
74 public virtual void Emit ()
76 foreach (MemberCore mc in this)
81 public class MethodArrayList : MemberCoreArrayList
89 CachedMethods cached_method;
90 TypeContainer container;
92 public MethodArrayList (TypeContainer container)
94 this.container = container;
98 /// Method container contains Equals method
100 public bool HasEquals {
102 cached_method |= CachedMethods.Equals;
106 return (cached_method & CachedMethods.Equals) != 0;
111 /// Method container contains GetHashCode method
113 public bool HasGetHashCode {
115 cached_method |= CachedMethods.GetHashCode;
119 return (cached_method & CachedMethods.GetHashCode) != 0;
123 public override void DefineContainerMembers ()
125 base.DefineContainerMembers ();
127 if (HasEquals && !HasGetHashCode) {
128 Report.Warning (659, 3, container.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
134 public sealed class IndexerArrayList : MemberCoreArrayList
137 /// The indexer name for this container
139 public string IndexerName = DefaultIndexerName;
141 bool seen_normal_indexers = false;
143 TypeContainer container;
145 public IndexerArrayList (TypeContainer container)
147 this.container = container;
151 /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
152 /// class is consistent. Either it is `Item' or it is the name defined by all the
153 /// indexers with the `IndexerName' attribute.
155 /// Turns out that the IndexerNameAttribute is applied to each indexer,
156 /// but it is never emitted, instead a DefaultMember attribute is attached
159 public override void DefineContainerMembers()
161 base.DefineContainerMembers ();
163 string class_indexer_name = null;
166 // If there's both an explicit and an implicit interface implementation, the
167 // explicit one actually implements the interface while the other one is just
168 // a normal indexer. See bug #37714.
171 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
172 foreach (Indexer i in this) {
173 if (i.InterfaceType != null) {
174 if (seen_normal_indexers)
175 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
179 seen_normal_indexers = true;
181 if (class_indexer_name == null) {
182 class_indexer_name = i.ShortName;
186 if (i.ShortName != class_indexer_name)
187 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");
190 if (class_indexer_name != null)
191 IndexerName = class_indexer_name;
194 public override void Emit ()
198 if (!seen_normal_indexers)
201 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
202 container.TypeBuilder.SetCustomAttribute (cb);
206 public class OperatorArrayList: MemberCoreArrayList
208 TypeContainer container;
210 public OperatorArrayList (TypeContainer container)
212 this.container = container;
216 // Operator pair checking
221 public Type ret_type;
222 public Type type1, type2;
224 public Operator.OpType ot;
226 public OperatorEntry (int f, Operator o)
230 ret_type = o.OperatorMethod.ReturnType;
231 Type [] pt = o.OperatorMethod.ParameterTypes;
238 public override int GetHashCode ()
240 return ret_type.GetHashCode ();
243 public override bool Equals (object o)
245 OperatorEntry other = (OperatorEntry) o;
247 if (other.ret_type != ret_type)
249 if (other.type1 != type1)
251 if (other.type2 != type2)
258 // Checks that some operators come in pairs:
264 // They are matched based on the return type and the argument types
266 void CheckPairedOperators ()
268 IDictionary pairs = new HybridDictionary ();
269 Operator true_op = null;
270 Operator false_op = null;
271 bool has_equality_or_inequality = false;
273 // Register all the operators we care about.
274 foreach (Operator op in this){
277 // Skip erroneous code.
278 if (op.OperatorMethod == null)
281 switch (op.OperatorType){
282 case Operator.OpType.Equality:
284 has_equality_or_inequality = true;
286 case Operator.OpType.Inequality:
288 has_equality_or_inequality = true;
291 case Operator.OpType.True:
294 case Operator.OpType.False:
298 case Operator.OpType.GreaterThan:
300 case Operator.OpType.LessThan:
303 case Operator.OpType.GreaterThanOrEqual:
305 case Operator.OpType.LessThanOrEqual:
311 OperatorEntry oe = new OperatorEntry (reg, op);
313 object o = pairs [oe];
317 oe = (OperatorEntry) o;
322 if (true_op != null){
323 if (false_op == null)
324 Report.Error (216, true_op.Location, "The operator `{0}' requires a matching operator `false' to also be defined",
325 true_op.GetSignatureForError ());
326 } else if (false_op != null)
327 Report.Error (216, false_op.Location, "The operator `{0}' requires a matching operator `true' to also be defined",
328 false_op.GetSignatureForError ());
331 // Look for the mistakes.
333 foreach (DictionaryEntry de in pairs){
334 OperatorEntry oe = (OperatorEntry) de.Key;
341 case Operator.OpType.Equality:
344 case Operator.OpType.Inequality:
347 case Operator.OpType.GreaterThan:
350 case Operator.OpType.LessThan:
353 case Operator.OpType.GreaterThanOrEqual:
356 case Operator.OpType.LessThanOrEqual:
360 Report.Error (216, oe.op.Location,
361 "The operator `{0}' requires a matching operator `{1}' to also be defined",
362 oe.op.GetSignatureForError (), s);
365 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
366 if (container.Methods == null || !container.Methods.HasEquals)
367 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
369 if (container.Methods == null || !container.Methods.HasGetHashCode)
370 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
374 public override void DefineContainerMembers ()
376 base.DefineContainerMembers ();
377 CheckPairedOperators ();
382 // Whether this is a struct, class or interface
383 public readonly Kind Kind;
385 // Holds a list of classes and structures
386 protected ArrayList types;
388 // Holds the list of properties
389 MemberCoreArrayList properties;
391 // Holds the list of enumerations
392 MemberCoreArrayList enums;
394 // Holds the list of delegates
395 MemberCoreArrayList delegates;
397 // Holds the list of constructors
398 protected MemberCoreArrayList instance_constructors;
400 // Holds the list of fields
401 MemberCoreArrayList fields;
403 // Holds a list of fields that have initializers
404 protected ArrayList initialized_fields;
406 // Holds a list of static fields that have initializers
407 protected ArrayList initialized_static_fields;
409 // Holds the list of constants
410 MemberCoreArrayList constants;
413 MemberCoreArrayList interfaces;
415 // Holds the methods.
416 MethodArrayList methods;
419 protected MemberCoreArrayList events;
421 // Holds the indexers
422 IndexerArrayList indexers;
424 // Holds the operators
425 MemberCoreArrayList operators;
427 // Holds the iterators
431 // Pointers to the default constructor and the default static constructor
433 Constructor default_constructor;
434 Constructor default_static_constructor;
437 // Points to the first non-static field added to the container.
439 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
440 // and the first one's as good as any.
442 FieldBase first_nonstatic_field = null;
445 // This one is computed after we can distinguish interfaces
446 // from classes from the arraylist `type_bases'
449 TypeExpr[] iface_exprs;
451 ArrayList type_bases;
453 bool members_defined;
454 bool members_defined_ok;
456 // The interfaces we implement.
457 protected Type[] ifaces;
459 // The base member cache and our member cache
460 MemberCache base_cache;
461 MemberCache member_cache;
463 public const string DefaultIndexerName = "Item";
466 GenericTypeParameterBuilder[] gen_params;
468 public TypeContainer PartialContainer;
469 ArrayList partial_parts;
472 /// The pending methods that need to be implemented
473 // (interfaces or abstract methods)
475 PendingImplementation pending;
477 public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
478 Attributes attrs, Kind kind)
479 : base (ns, parent, name, attrs)
481 if (parent != null && parent != RootContext.Tree.Types && parent.NamespaceEntry != ns)
482 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
487 public bool AddToMemberContainer (MemberCore symbol)
489 return AddToContainer (symbol, symbol.MemberName.MethodName);
492 protected virtual bool AddToTypeContainer (DeclSpace ds)
494 return AddToContainer (ds, ds.Basename);
497 public void AddConstant (Const constant)
499 if (!AddToMemberContainer (constant))
502 if (constants == null)
503 constants = new MemberCoreArrayList ();
505 constants.Add (constant);
508 public void AddEnum (Mono.CSharp.Enum e)
510 if (!AddToTypeContainer (e))
514 enums = new MemberCoreArrayList ();
519 public bool AddClassOrStruct (TypeContainer c)
521 if (!AddToTypeContainer (c))
525 types = new ArrayList (2);
527 RootContext.Tree.RecordDecl (c.NamespaceEntry.NS, c.MemberName, c);
532 public virtual TypeContainer AddPartial (TypeContainer nextPart)
534 return AddPartial (nextPart, nextPart.Basename);
537 protected TypeContainer AddPartial (TypeContainer nextPart, string name)
539 nextPart.ModFlags |= Modifiers.PARTIAL;
540 TypeContainer tc = defined_names [name] as TypeContainer;
543 if (nextPart is Interface)
544 AddInterface (nextPart);
546 AddClassOrStruct (nextPart);
550 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
551 Report.SymbolRelatedToPreviousError (tc);
552 Error_MissingPartialModifier (nextPart);
556 if (tc.Kind != nextPart.Kind) {
557 Report.SymbolRelatedToPreviousError (tc);
558 Report.Error (261, nextPart.Location,
559 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
560 nextPart.GetSignatureForError ());
564 if ((tc.ModFlags & Modifiers.Accessibility) != (nextPart.ModFlags & Modifiers.Accessibility)) {
565 Report.SymbolRelatedToPreviousError (tc);
566 Report.Error (262, nextPart.Location,
567 "Partial declarations of `{0}' have conflicting accessibility modifiers",
568 nextPart.GetSignatureForError ());
572 if (tc.partial_parts == null)
573 tc.partial_parts = new ArrayList (1);
575 tc.ModFlags |= nextPart.ModFlags;
576 if (nextPart.attributes != null) {
577 if (tc.attributes == null)
578 tc.attributes = nextPart.attributes;
580 tc.attributes.AddAttributes (nextPart.attributes.Attrs);
583 nextPart.PartialContainer = tc;
584 tc.partial_parts.Add (nextPart);
588 public void AddDelegate (Delegate d)
590 if (!AddToTypeContainer (d))
593 if (delegates == null)
594 delegates = new MemberCoreArrayList ();
599 public void AddMethod (Method method)
601 if (!AddToMemberContainer (method))
605 methods = new MethodArrayList (this);
607 if (method.MemberName.Left != null)
608 methods.Insert (0, method);
610 methods.Add (method);
614 // Do not use this method: use AddMethod.
616 // This is only used by iterators.
618 public void AppendMethod (Method method)
620 if (!AddToMemberContainer (method))
624 methods = new MethodArrayList (this);
626 methods.Add (method);
629 public void AddConstructor (Constructor c)
631 if (c.Name != MemberName.Name) {
632 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
635 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
638 if (default_static_constructor != null) {
639 Report.SymbolRelatedToPreviousError (default_static_constructor);
640 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
644 default_static_constructor = c;
647 if (default_constructor != null) {
648 Report.SymbolRelatedToPreviousError (default_constructor);
649 Report.Error (111, c.Location, Error111, c.GetSignatureForError ());
652 default_constructor = c;
655 if (instance_constructors == null)
656 instance_constructors = new MemberCoreArrayList ();
658 instance_constructors.Add (c);
662 internal static string Error111 {
664 return "`{0}' is already defined. Rename this member or use different parameter types";
668 public bool AddInterface (TypeContainer iface)
670 if (!AddToTypeContainer (iface))
673 if (interfaces == null) {
674 interfaces = new MemberCoreArrayList ();
677 RootContext.Tree.RecordDecl (iface.NamespaceEntry.NS, iface.MemberName, iface);
678 interfaces.Add (iface);
682 public void AddField (FieldMember field)
684 if (!AddToMemberContainer (field))
688 fields = new MemberCoreArrayList ();
692 if ((field.ModFlags & Modifiers.STATIC) != 0)
695 if (first_nonstatic_field == null) {
696 first_nonstatic_field = field;
700 if (Kind == Kind.Struct &&
701 first_nonstatic_field.Parent != field.Parent &&
702 RootContext.WarningLevel >= 3) {
703 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
704 Report.Warning (282, 3, field.Location,
705 "struct instance field `{0}' found in different declaration from instance field `{1}'",
706 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
710 public void AddProperty (Property prop)
712 if (!AddToMemberContainer (prop) ||
713 !AddToMemberContainer (prop.Get) || !AddToMemberContainer (prop.Set))
716 if (properties == null)
717 properties = new MemberCoreArrayList ();
719 if (prop.MemberName.Left != null)
720 properties.Insert (0, prop);
722 properties.Add (prop);
725 public void AddEvent (Event e)
727 if (!AddToMemberContainer (e))
730 if (e is EventProperty) {
731 if (!AddToMemberContainer (e.Add))
734 if (!AddToMemberContainer (e.Remove))
739 events = new MemberCoreArrayList ();
745 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
747 public void AddIndexer (Indexer i)
749 if (indexers == null)
750 indexers = new IndexerArrayList (this);
752 if (i.IsExplicitImpl)
753 indexers.Insert (0, i);
758 public void AddOperator (Operator op)
760 if (!AddToMemberContainer (op))
763 if (operators == null)
764 operators = new OperatorArrayList (this);
769 public void AddIterator (Iterator i)
771 if (iterators == null)
772 iterators = new ArrayList ();
777 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
779 if (a.Type == TypeManager.default_member_type) {
780 if (Indexers != null) {
781 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
786 base.ApplyAttributeBuilder (a, cb);
789 public override AttributeTargets AttributeTargets {
791 throw new NotSupportedException ();
795 public ArrayList Types {
801 public MethodArrayList Methods {
807 public ArrayList Constants {
813 public ArrayList Interfaces {
819 public ArrayList Iterators {
825 protected Type BaseType {
827 return TypeBuilder.BaseType;
831 public ArrayList Bases {
841 public ArrayList Fields {
847 public ArrayList InstanceConstructors {
849 return instance_constructors;
853 public ArrayList Properties {
859 public ArrayList Events {
865 public ArrayList Enums {
871 public ArrayList Indexers {
877 public ArrayList Operators {
883 public ArrayList Delegates {
889 protected override TypeAttributes TypeAttr {
891 return Modifiers.TypeAttr (ModFlags, this) | base.TypeAttr;
895 public string IndexerName {
897 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
901 public bool IsComImport {
903 if (OptAttributes == null)
906 return OptAttributes.Contains (TypeManager.comimport_attr_type);
910 public virtual void RegisterFieldForInitialization (FieldBase field)
912 if ((field.ModFlags & Modifiers.STATIC) != 0){
913 if (initialized_static_fields == null)
914 initialized_static_fields = new ArrayList (4);
916 initialized_static_fields.Add (field);
918 if (initialized_fields == null)
919 initialized_fields = new ArrayList (4);
921 initialized_fields.Add (field);
926 // Emits the instance field initializers
928 public bool EmitFieldInitializers (EmitContext ec)
933 fields = initialized_static_fields;
935 fields = initialized_fields;
941 foreach (FieldBase f in fields) {
942 f.EmitInitializer (ec);
948 // Defines the default constructors
950 protected void DefineDefaultConstructor (bool is_static)
952 // The default instance constructor is public
953 // If the class is abstract, the default constructor is protected
954 // The default static constructor is private
958 if (initialized_static_fields == null || default_static_constructor != null)
960 mods = Modifiers.STATIC | Modifiers.PRIVATE;
962 if (instance_constructors != null)
965 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
968 Constructor c = new Constructor (this, MemberName.Name, mods,
969 Parameters.EmptyReadOnlyParameters,
970 new GeneratedBaseInitializer (Location),
974 c.Block = new ToplevelBlock (null, Location);
977 public override string DocComment {
989 public PendingImplementation PendingImplementations
996 public override bool GetClsCompliantAttributeValue ()
998 if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
999 return (caching_flags & Flags.ClsCompliantAttributeTrue) != 0;
1002 return PartialContainer.GetClsCompliantAttributeValue ();
1004 return base.GetClsCompliantAttributeValue ();
1007 TypeExpr[] GetNormalBases (out TypeExpr base_class)
1013 int count = Bases.Count;
1014 int start = 0, i, j;
1016 if (Kind == Kind.Class){
1017 TypeExpr name = ((Expression) Bases [0]).ResolveAsBaseTerminal (this, false);
1023 if (!name.IsInterface) {
1024 // base_class could be a class, struct, enum, delegate.
1025 // This is validated in GetClassBases.
1031 TypeExpr [] ifaces = new TypeExpr [count-start];
1033 for (i = start, j = 0; i < count; i++, j++){
1034 TypeExpr resolved = ((Expression) Bases [i]).ResolveAsTypeTerminal (this, false);
1035 if (resolved == null) {
1039 ifaces [j] = resolved;
1042 return ifaces.Length == 0 ? null : ifaces;
1046 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
1048 ArrayList ifaces = new ArrayList (0);
1049 if (iface_exprs != null)
1050 ifaces.AddRange (iface_exprs);
1052 foreach (TypeContainer part in partial_parts) {
1053 TypeExpr new_base_class;
1054 TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
1055 if (new_base_class != TypeManager.system_object_expr) {
1056 if (base_class == TypeManager.system_object_expr)
1057 base_class = new_base_class;
1059 if (new_base_class != null && !new_base_class.Equals (base_class)) {
1060 Report.SymbolRelatedToPreviousError (base_class.Location, "");
1061 Report.Error (263, part.Location,
1062 "Partial declarations of `{0}' must not specify different base classes",
1063 part.GetSignatureForError ());
1070 if (new_ifaces == null)
1073 foreach (TypeExpr iface in new_ifaces) {
1074 if (ifaces.Contains (iface))
1081 if (ifaces.Count == 0)
1084 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
1088 /// This function computes the Base class and also the
1089 /// list of interfaces that the class or struct @c implements.
1091 /// The return value is an array (might be null) of
1092 /// interfaces implemented (as Types).
1094 /// The @base_class argument is set to the base object or null
1095 /// if this is `System.Object'.
1097 public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
1099 TypeExpr[] ifaces = GetNormalBases (out base_class);
1104 int count = ifaces.Length;
1106 for (int i = 0; i < count; i++) {
1107 TypeExpr iface = (TypeExpr) ifaces [i];
1109 if (!iface.IsInterface) {
1110 if (Kind != Kind.Class) {
1111 // TODO: location of symbol related ....
1112 Error_TypeInListIsNotInterface (Location, iface.FullName);
1114 else if (base_class != null)
1115 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
1116 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
1118 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
1119 GetSignatureForError (), iface.GetSignatureForError ());
1124 for (int x = 0; x < i; x++) {
1125 if (iface.Equals (ifaces [x])) {
1126 Report.Error (528, Location,
1127 "`{0}' is already listed in " +
1128 "interface list", iface.GetSignatureForError ());
1133 if ((Kind == Kind.Interface) &&
1134 !iface.AsAccessible (Parent, ModFlags)) {
1135 Report.Error (61, Location,
1136 "Inconsistent accessibility: base " +
1137 "interface `{0}' is less accessible " +
1138 "than interface `{1}'", iface.Name,
1146 bool CheckGenericInterfaces (Type[] ifaces)
1148 ArrayList already_checked = new ArrayList ();
1150 for (int i = 0; i < ifaces.Length; i++) {
1151 Type iface = ifaces [i];
1152 foreach (Type t in already_checked) {
1156 Type[] infered = new Type [CountTypeParameters];
1157 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, infered, null))
1160 Report.Error (695, Location,
1161 "`{0}' cannot implement both `{1}' and `{2}' " +
1162 "because they may unify for some type parameter substitutions",
1163 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
1164 TypeManager.CSharpName (t));
1168 already_checked.Add (iface);
1176 protected void Error_TypeInListIsNotInterface (Location loc, string type)
1178 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
1181 bool DefineTypeBuilder ()
1185 if (TypeManager.NamespaceClash (Name, Location)) {
1189 ModuleBuilder builder = CodeGen.Module.Builder;
1190 Type default_parent = null;
1191 if (Kind == Kind.Struct)
1192 default_parent = TypeManager.value_type;
1193 TypeBuilder = builder.DefineType (
1194 Name, TypeAttr, default_parent, null);
1196 TypeBuilder builder = Parent.TypeBuilder;
1198 TypeBuilder = builder.DefineNestedType (
1199 Basename, TypeAttr, null, null);
1201 } catch (ArgumentException) {
1202 Report.RuntimeMissingSupport (Location, "static classes");
1206 TypeManager.AddUserType (this);
1209 string[] param_names = new string [TypeParameters.Length];
1210 for (int i = 0; i < TypeParameters.Length; i++)
1211 param_names [i] = TypeParameters [i].Name;
1213 gen_params = TypeBuilder.DefineGenericParameters (param_names);
1215 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1216 for (int i = offset; i < gen_params.Length; i++)
1217 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1220 iface_exprs = GetClassBases (out base_type);
1221 if (partial_parts != null) {
1222 iface_exprs = GetNormalPartialBases (ref base_type);
1226 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1227 // which in turn should have called DefineType()s on base types if necessary.
1229 // None of the code below should trigger DefineType()s on classes that we depend on.
1230 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1232 // Let's do it as soon as possible, since code below can call DefineType() on classes
1233 // that depend on us to be populated before they are.
1235 if (!(this is Iterator))
1236 RootContext.RegisterOrder (this);
1238 if (base_type != null) {
1239 if (IsGeneric && TypeManager.IsAttributeType (base_type.Type)) {
1240 Report.Error (698, base_type.Location,
1241 "A generic type cannot derive from `{0}' " +
1242 "because it is an attribute class",
1247 TypeBuilder.SetParent (base_type.Type);
1249 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1250 if (obsolete_attr != null && !IsInObsoleteScope) {
1251 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1255 if (!CheckRecursiveDefinition (this)) {
1259 // add interfaces that were not added at type creation
1260 if (iface_exprs != null) {
1261 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1262 if (ifaces == null) {
1266 foreach (Type itype in ifaces)
1267 TypeBuilder.AddInterfaceImplementation (itype);
1269 if (!CheckGenericInterfaces (ifaces)) {
1273 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1276 if (this is Iterator && !ResolveType ()) {
1284 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1286 public override TypeBuilder DefineType ()
1288 if (TypeBuilder != null)
1294 if (!DefineTypeBuilder ()) {
1299 if (partial_parts != null) {
1300 foreach (TypeContainer part in partial_parts)
1301 part.TypeBuilder = TypeBuilder;
1304 DefineNestedTypes ();
1309 Constraints [] constraints;
1310 public override void SetParameterInfo (ArrayList constraints_list)
1313 base.SetParameterInfo (constraints_list);
1317 if (constraints_list == null)
1320 constraints = new Constraints [PartialContainer.CountCurrentTypeParameters];
1322 TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
1323 for (int i = 0; i < constraints.Length; i++) {
1324 foreach (Constraints constraint in constraints_list) {
1325 if (constraint.TypeParameter == current_params [i].Name) {
1326 constraints [i] = constraint;
1333 bool UpdateTypeParameterConstraints ()
1336 TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
1338 if (constraints == null)
1341 for (int i = 0; i < current_params.Length; i++) {
1342 if (!current_params [i].UpdateConstraints (this, constraints [i])) {
1343 Report.Error (265, Location, "Partial declarations of `{0}' have " +
1344 "inconsistent constraints for type parameter `{1}'.",
1345 MemberName.GetTypeName (), current_params [i].Name);
1353 public bool ResolveType ()
1355 if ((base_type != null) &&
1356 (base_type.ResolveAsTypeTerminal (this, false) == null)) {
1365 throw new InternalErrorException ();
1367 TypeExpr current_type = null;
1369 foreach (TypeParameter type_param in CurrentTypeParameters) {
1370 if (!type_param.Resolve (this)) {
1376 if (partial_parts != null) {
1377 foreach (TypeContainer part in partial_parts) {
1378 if (!part.UpdateTypeParameterConstraints ()) {
1385 foreach (TypeParameter type_param in TypeParameters) {
1386 if (!type_param.DefineType (this)) {
1392 current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
1394 foreach (TypeParameter type_param in TypeParameters)
1395 if (!type_param.CheckDependencies ()) {
1400 if (current_type != null) {
1401 current_type = current_type.ResolveAsTypeTerminal (this, false);
1402 if (current_type == null) {
1407 CurrentType = current_type.Type;
1413 protected virtual bool DefineNestedTypes ()
1415 if (Interfaces != null) {
1416 foreach (TypeContainer iface in Interfaces)
1417 if (iface.DefineType () == null)
1421 if (Types != null) {
1422 foreach (TypeContainer tc in Types)
1423 if (tc.DefineType () == null)
1427 if (Delegates != null) {
1428 foreach (Delegate d in Delegates)
1429 if (d.DefineType () == null)
1433 if (Enums != null) {
1434 foreach (Enum en in Enums)
1435 if (en.DefineType () == null)
1442 TypeContainer InTransit;
1444 protected bool CheckRecursiveDefinition (TypeContainer tc)
1446 if (InTransit != null) {
1447 Report.SymbolRelatedToPreviousError (this);
1448 if (this is Interface)
1450 529, tc.Location, "Inherited interface `{0}' causes a " +
1451 "cycle in the interface hierarchy of `{1}'",
1452 GetSignatureForError (), tc.GetSignatureForError ());
1455 146, tc.Location, "Circular base class dependency " +
1456 "involving `{0}' and `{1}'",
1457 tc.GetSignatureForError (), GetSignatureForError ());
1463 if (BaseType != null) {
1464 Type t = TypeManager.DropGenericTypeArguments (BaseType);
1465 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1466 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1470 if (iface_exprs != null) {
1471 foreach (TypeExpr iface in iface_exprs) {
1472 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1473 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1474 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1483 public static void Error_KeywordNotAllowed (Location loc)
1485 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1489 /// Populates our TypeBuilder with fields and methods
1491 public override bool DefineMembers ()
1493 if (members_defined)
1494 return members_defined_ok;
1496 if (!base.DefineMembers ())
1499 members_defined_ok = DoDefineMembers ();
1500 members_defined = true;
1502 return members_defined_ok;
1505 public override bool IsPartial {
1507 return PartialContainer != null;
1511 public virtual void DefineDefaultConstructor ()
1515 protected virtual bool DoDefineMembers ()
1517 if (iface_exprs != null) {
1518 foreach (TypeExpr iface in iface_exprs) {
1519 ConstructedType ct = iface as ConstructedType;
1520 if ((ct != null) && !ct.CheckConstraints (this))
1525 if (base_type != null) {
1526 ConstructedType ct = base_type as ConstructedType;
1527 if ((ct != null) && !ct.CheckConstraints (this))
1532 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1533 if (conflict_symbol == null) {
1534 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1535 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1537 if ((ModFlags & Modifiers.NEW) == 0) {
1538 Report.SymbolRelatedToPreviousError (conflict_symbol);
1539 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1540 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1545 DefineContainerMembers (constants);
1546 DefineContainerMembers (fields);
1548 DefineDefaultConstructor ();
1550 if (Kind == Kind.Struct || Kind == Kind.Class) {
1551 pending = PendingImplementation.GetPendingImplementations (this);
1555 // Constructors are not in the defined_names array
1557 DefineContainerMembers (instance_constructors);
1559 if (default_static_constructor != null)
1560 default_static_constructor.Define ();
1562 DefineContainerMembers (properties);
1563 DefineContainerMembers (events);
1564 DefineContainerMembers (indexers);
1565 DefineContainerMembers (methods);
1566 DefineContainerMembers (operators);
1567 DefineContainerMembers (enums);
1568 DefineContainerMembers (delegates);
1570 if (CurrentType != null) {
1571 GenericType = CurrentType;
1576 member_cache = new MemberCache (this);
1577 if (partial_parts != null) {
1578 foreach (TypeContainer part in partial_parts)
1579 part.member_cache = member_cache;
1582 if (iterators != null) {
1583 foreach (Iterator iterator in iterators) {
1584 if (iterator.DefineType () == null)
1588 foreach (Iterator iterator in iterators) {
1589 if (!iterator.DefineMembers ())
1597 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1600 mcal.DefineContainerMembers ();
1603 public override bool Define ()
1605 if (iterators != null) {
1606 foreach (Iterator iterator in iterators) {
1607 if (!iterator.Define ())
1615 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1617 return BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1621 /// This function is based by a delegate to the FindMembers routine
1623 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1629 /// This filter is used by FindMembers, and we just keep
1630 /// a global for the filter to `AlwaysAccept'
1632 static MemberFilter accepting_filter;
1635 static TypeContainer ()
1637 accepting_filter = new MemberFilter (AlwaysAccept);
1640 public MethodInfo[] GetMethods ()
1642 ArrayList members = new ArrayList ();
1646 if (methods != null) {
1647 int len = methods.Count;
1648 for (int i = 0; i < len; i++) {
1649 Method m = (Method) methods [i];
1651 members.Add (m.MethodBuilder);
1655 if (operators != null) {
1656 int len = operators.Count;
1657 for (int i = 0; i < len; i++) {
1658 Operator o = (Operator) operators [i];
1660 members.Add (o.OperatorMethodBuilder);
1664 if (properties != null) {
1665 int len = properties.Count;
1666 for (int i = 0; i < len; i++) {
1667 Property p = (Property) properties [i];
1669 if (p.GetBuilder != null)
1670 members.Add (p.GetBuilder);
1671 if (p.SetBuilder != null)
1672 members.Add (p.SetBuilder);
1676 if (indexers != null) {
1677 int len = indexers.Count;
1678 for (int i = 0; i < len; i++) {
1679 Indexer ix = (Indexer) indexers [i];
1681 if (ix.GetBuilder != null)
1682 members.Add (ix.GetBuilder);
1683 if (ix.SetBuilder != null)
1684 members.Add (ix.SetBuilder);
1688 if (events != null) {
1689 int len = events.Count;
1690 for (int i = 0; i < len; i++) {
1691 Event e = (Event) events [i];
1693 if (e.AddBuilder != null)
1694 members.Add (e.AddBuilder);
1695 if (e.RemoveBuilder != null)
1696 members.Add (e.RemoveBuilder);
1700 MethodInfo[] retMethods = new MethodInfo [members.Count];
1701 members.CopyTo (retMethods, 0);
1705 // Indicated whether container has StructLayout attribute set Explicit
1706 public bool HasExplicitLayout {
1708 return (caching_flags & Flags.HasExplicitLayout) != 0;
1711 caching_flags |= Flags.HasExplicitLayout;
1715 public override Type FindNestedType (string name)
1718 return PartialContainer.FindNestedType (name);
1720 ArrayList [] lists = { types, enums, delegates, interfaces };
1722 for (int j = 0; j < lists.Length; ++j) {
1723 ArrayList list = lists [j];
1727 int len = list.Count;
1728 for (int i = 0; i < len; ++i) {
1729 DeclSpace ds = (DeclSpace) list [i];
1730 if (ds.Basename == name) {
1731 return ds.DefineType ();
1739 private void FindMembers_NestedTypes (int modflags,
1740 BindingFlags bf, MemberFilter filter, object criteria,
1741 ref ArrayList members)
1743 ArrayList [] lists = { types, enums, delegates, interfaces };
1745 for (int j = 0; j < lists.Length; ++j) {
1746 ArrayList list = lists [j];
1750 int len = list.Count;
1751 for (int i = 0; i < len; i++) {
1752 DeclSpace ds = (DeclSpace) list [i];
1754 if ((ds.ModFlags & modflags) == 0)
1757 TypeBuilder tb = ds.TypeBuilder;
1759 if (!(criteria is string) || ds.Basename.Equals (criteria))
1760 tb = ds.DefineType ();
1763 if (tb != null && (filter (tb, criteria) == true)) {
1764 if (members == null)
1765 members = new ArrayList ();
1774 /// This method returns the members of this type just like Type.FindMembers would
1775 /// Only, we need to use this for types which are _being_ defined because MS'
1776 /// implementation can't take care of that.
1779 // FIXME: return an empty static array instead of null, that cleans up
1780 // some code and is consistent with some coding conventions I just found
1784 // Notice that in various cases we check if our field is non-null,
1785 // something that would normally mean that there was a bug elsewhere.
1787 // The problem happens while we are defining p-invoke methods, as those
1788 // will trigger a FindMembers, but this happens before things are defined
1790 // Since the whole process is a no-op, it is fine to check for null here.
1792 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1793 MemberFilter filter, object criteria)
1795 ArrayList members = null;
1798 if ((bf & BindingFlags.Public) != 0)
1799 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1801 if ((bf & BindingFlags.NonPublic) != 0)
1802 modflags |= Modifiers.PRIVATE;
1804 int static_mask = 0, static_flags = 0;
1805 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1806 case BindingFlags.Static:
1807 static_mask = static_flags = Modifiers.STATIC;
1810 case BindingFlags.Instance:
1811 static_mask = Modifiers.STATIC;
1816 static_mask = static_flags = 0;
1820 Timer.StartTimer (TimerType.TcFindMembers);
1823 filter = accepting_filter;
1825 if ((mt & MemberTypes.Field) != 0) {
1826 if (fields != null) {
1827 int len = fields.Count;
1828 for (int i = 0; i < len; i++) {
1829 FieldMember f = (FieldMember) fields [i];
1831 if ((f.ModFlags & modflags) == 0)
1833 if ((f.ModFlags & static_mask) != static_flags)
1836 FieldBuilder fb = f.FieldBuilder;
1837 if (fb != null && filter (fb, criteria) == true) {
1838 if (members == null)
1839 members = new ArrayList ();
1846 if (constants != null) {
1847 int len = constants.Count;
1848 for (int i = 0; i < len; i++) {
1849 Const con = (Const) constants [i];
1851 if ((con.ModFlags & modflags) == 0)
1853 if ((con.ModFlags & static_mask) != static_flags)
1856 FieldBuilder fb = con.FieldBuilder;
1859 fb = con.FieldBuilder;
1861 if (fb != null && filter (fb, criteria) == true) {
1862 if (members == null)
1863 members = new ArrayList ();
1871 if ((mt & MemberTypes.Method) != 0) {
1872 if (methods != null) {
1873 int len = methods.Count;
1874 for (int i = 0; i < len; i++) {
1875 Method m = (Method) methods [i];
1877 if ((m.ModFlags & modflags) == 0)
1879 if ((m.ModFlags & static_mask) != static_flags)
1882 MethodBuilder mb = m.MethodBuilder;
1884 if (mb != null && filter (mb, criteria) == true) {
1885 if (members == null)
1886 members = new ArrayList ();
1893 if (operators != null) {
1894 int len = operators.Count;
1895 for (int i = 0; i < len; i++) {
1896 Operator o = (Operator) operators [i];
1898 if ((o.ModFlags & modflags) == 0)
1900 if ((o.ModFlags & static_mask) != static_flags)
1903 MethodBuilder ob = o.OperatorMethodBuilder;
1904 if (ob != null && filter (ob, criteria) == true) {
1905 if (members == null)
1906 members = new ArrayList ();
1913 if (properties != null) {
1914 int len = properties.Count;
1915 for (int i = 0; i < len; i++) {
1916 Property p = (Property) properties [i];
1918 if ((p.ModFlags & modflags) == 0)
1920 if ((p.ModFlags & static_mask) != static_flags)
1926 if (b != null && filter (b, criteria) == true) {
1927 if (members == null)
1928 members = new ArrayList ();
1934 if (b != null && filter (b, criteria) == true) {
1935 if (members == null)
1936 members = new ArrayList ();
1943 if (indexers != null) {
1944 int len = indexers.Count;
1945 for (int i = 0; i < len; i++) {
1946 Indexer ix = (Indexer) indexers [i];
1948 if ((ix.ModFlags & modflags) == 0)
1950 if ((ix.ModFlags & static_mask) != static_flags)
1956 if (b != null && filter (b, criteria) == true) {
1957 if (members == null)
1958 members = new ArrayList ();
1964 if (b != null && filter (b, criteria) == true) {
1965 if (members == null)
1966 members = new ArrayList ();
1974 if ((mt & MemberTypes.Event) != 0) {
1975 if (events != null) {
1976 int len = events.Count;
1977 for (int i = 0; i < len; i++) {
1978 Event e = (Event) events [i];
1980 if ((e.ModFlags & modflags) == 0)
1982 if ((e.ModFlags & static_mask) != static_flags)
1985 MemberInfo eb = e.EventBuilder;
1986 if (eb != null && filter (eb, criteria) == true) {
1987 if (members == null)
1988 members = new ArrayList ();
1990 members.Add (e.EventBuilder);
1996 if ((mt & MemberTypes.Property) != 0){
1997 if (properties != null) {
1998 int len = properties.Count;
1999 for (int i = 0; i < len; i++) {
2000 Property p = (Property) properties [i];
2002 if ((p.ModFlags & modflags) == 0)
2004 if ((p.ModFlags & static_mask) != static_flags)
2007 MemberInfo pb = p.PropertyBuilder;
2008 if (pb != null && filter (pb, criteria) == true) {
2009 if (members == null)
2010 members = new ArrayList ();
2012 members.Add (p.PropertyBuilder);
2017 if (indexers != null) {
2018 int len = indexers.Count;
2019 for (int i = 0; i < len; i++) {
2020 Indexer ix = (Indexer) indexers [i];
2022 if ((ix.ModFlags & modflags) == 0)
2024 if ((ix.ModFlags & static_mask) != static_flags)
2027 MemberInfo ib = ix.PropertyBuilder;
2028 if (ib != null && filter (ib, criteria) == true) {
2029 if (members == null)
2030 members = new ArrayList ();
2032 members.Add (ix.PropertyBuilder);
2038 if ((mt & MemberTypes.NestedType) != 0)
2039 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
2041 if ((mt & MemberTypes.Constructor) != 0){
2042 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
2043 int len = instance_constructors.Count;
2044 for (int i = 0; i < len; i++) {
2045 Constructor c = (Constructor) instance_constructors [i];
2047 ConstructorBuilder cb = c.ConstructorBuilder;
2048 if (cb != null && filter (cb, criteria) == true) {
2049 if (members == null)
2050 members = new ArrayList ();
2057 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2058 ConstructorBuilder cb =
2059 default_static_constructor.ConstructorBuilder;
2061 if (cb != null && filter (cb, criteria) == true) {
2062 if (members == null)
2063 members = new ArrayList ();
2071 // Lookup members in base if requested.
2073 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2074 if (TypeBuilder.BaseType != null) {
2075 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2076 if (list.Count > 0) {
2077 if (members == null)
2078 members = new ArrayList ();
2080 members.AddRange (list);
2086 Timer.StopTimer (TimerType.TcFindMembers);
2088 if (members == null)
2089 return MemberList.Empty;
2091 return new MemberList (members);
2094 public override MemberCache MemberCache {
2096 return member_cache;
2100 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2101 MemberFilter filter, object criteria)
2103 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2106 return ds.FindMembers (mt, bf, filter, criteria);
2108 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2112 // FindMethods will look for methods not only in the type `t', but in
2113 // any interfaces implemented by the type.
2115 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
2116 MemberFilter filter, object criteria)
2122 /// Emits the values for the constants
2124 public void EmitConstants ()
2126 if (constants != null)
2127 foreach (Const con in constants)
2132 void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2137 foreach (MemberCore mc in al) {
2138 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2142 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2147 public virtual void VerifyMembers ()
2150 // Check for internal or private fields that were never assigned
2152 if (RootContext.WarningLevel >= 3) {
2153 CheckMemberUsage (properties, "property");
2154 CheckMemberUsage (methods, "method");
2155 CheckMemberUsage (constants, "constant");
2157 if (fields != null){
2158 foreach (FieldMember f in fields) {
2159 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2163 if ((f.caching_flags & Flags.IsAssigned) == 0)
2164 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2167 const int error_code = 414;
2169 const int error_code = 169;
2171 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2172 f.GetSignatureForError ());
2178 // Only report 649 on level 4
2180 if (RootContext.WarningLevel < 4)
2183 if ((f.caching_flags & Flags.IsAssigned) != 0)
2186 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2187 f.GetSignatureForError (), f.Type.Type.IsValueType ? Activator.CreateInstance (f.Type.Type).ToString() : "null");
2193 // TODO: move to ClassOrStruct
2194 void EmitConstructors ()
2196 if (instance_constructors == null)
2199 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2200 bool has_compliant_args = false;
2202 foreach (Constructor c in instance_constructors) {
2205 if (has_compliant_args)
2208 has_compliant_args = c.HasCompliantArgs;
2210 if (!has_compliant_args)
2211 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2213 foreach (Constructor c in instance_constructors)
2219 /// Emits the code, this step is performed after all
2220 /// the types, enumerations, constructors
2222 public virtual void EmitType ()
2224 if (OptAttributes != null)
2225 OptAttributes.Emit ();
2228 int offset = CountTypeParameters - CurrentTypeParameters.Length;
2229 for (int i = offset; i < gen_params.Length; i++)
2230 CurrentTypeParameters [i - offset].EmitAttributes ();
2234 // Structs with no fields need to have at least one byte.
2235 // The right thing would be to set the PackingSize in a DefineType
2236 // but there are no functions that allow interfaces *and* the size to
2240 if (Kind == Kind.Struct && first_nonstatic_field == null){
2241 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2242 FieldAttributes.Private);
2244 if (HasExplicitLayout){
2245 object [] ctor_args = new object [1];
2248 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2249 TypeManager.field_offset_attribute_ctor, ctor_args);
2250 fb.SetCustomAttribute (cba);
2256 EmitConstructors ();
2258 // Can not continue if constants are broken
2260 if (Report.Errors > 0)
2263 if (default_static_constructor != null)
2264 default_static_constructor.Emit ();
2266 if (methods != null){
2267 foreach (Method m in methods)
2271 if (operators != null)
2272 foreach (Operator o in operators)
2275 if (properties != null)
2276 foreach (Property p in properties)
2279 if (indexers != null){
2284 foreach (FieldMember f in fields)
2287 if (events != null){
2288 foreach (Event e in Events)
2292 if (delegates != null) {
2293 foreach (Delegate d in Delegates) {
2298 if (enums != null) {
2299 foreach (Enum e in enums) {
2304 if (pending != null)
2305 if (pending.VerifyPendingMethods ())
2308 if (iterators != null)
2309 foreach (Iterator iterator in iterators)
2310 iterator.EmitType ();
2313 public override void CloseType ()
2315 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2319 caching_flags |= Flags.CloseTypeCreated;
2320 TypeBuilder.CreateType ();
2321 } catch (TypeLoadException){
2323 // This is fine, the code still created the type
2325 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2326 // Console.WriteLine (e.Message);
2328 Console.WriteLine ("In type: " + Name);
2333 foreach (Enum en in Enums)
2337 foreach (TypeContainer tc in Types)
2338 if (tc.Kind == Kind.Struct)
2341 foreach (TypeContainer tc in Types)
2342 if (tc.Kind != Kind.Struct)
2346 if (Delegates != null)
2347 foreach (Delegate d in Delegates)
2350 if (Iterators != null)
2351 foreach (Iterator i in Iterators)
2359 initialized_fields = null;
2360 initialized_static_fields = null;
2368 default_constructor = null;
2369 default_static_constructor = null;
2371 OptAttributes = null;
2374 member_cache = null;
2378 // Performs the validation on a Method's modifiers (properties have
2379 // the same properties).
2381 public bool MethodModifiersValid (MemberCore mc)
2383 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2384 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2385 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2387 int flags = mc.ModFlags;
2390 // At most one of static, virtual or override
2392 if ((flags & Modifiers.STATIC) != 0){
2393 if ((flags & vao) != 0){
2394 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2395 mc.GetSignatureForError ());
2400 if (Kind == Kind.Struct){
2401 if ((flags & va) != 0){
2402 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2407 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2408 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2409 mc.GetSignatureForError ());
2414 // If the declaration includes the abstract modifier, then the
2415 // declaration does not include static, virtual or extern
2417 if ((flags & Modifiers.ABSTRACT) != 0){
2418 if ((flags & Modifiers.EXTERN) != 0){
2420 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2424 if ((flags & Modifiers.SEALED) != 0) {
2425 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2429 if ((flags & Modifiers.VIRTUAL) != 0){
2430 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2434 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2435 Report.Error (513, mc.Location, "`{0}' is abstract but it is contained in nonabstract class", mc.GetSignatureForError ());
2440 if ((flags & Modifiers.PRIVATE) != 0){
2441 if ((flags & vao) != 0){
2442 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2447 if ((flags & Modifiers.SEALED) != 0){
2448 if ((flags & Modifiers.OVERRIDE) == 0){
2449 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2457 public bool UserDefinedStaticConstructor {
2459 return default_static_constructor != null;
2463 public Constructor DefaultStaticConstructor {
2464 get { return default_static_constructor; }
2467 protected override bool VerifyClsCompliance ()
2469 if (!base.VerifyClsCompliance ())
2474 Type base_type = TypeBuilder.BaseType;
2475 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2476 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2479 if (!Parent.IsClsComplianceRequired ()) {
2480 Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
2481 GetSignatureForError (), Parent.GetSignatureForError ());
2488 /// Checks whether container name is CLS Compliant
2490 void VerifyClsName ()
2492 Hashtable base_members = base_cache == null ?
2494 base_cache.GetPublicMembers ();
2495 Hashtable this_members = new Hashtable ();
2497 foreach (DictionaryEntry entry in defined_names) {
2498 MemberCore mc = (MemberCore)entry.Value;
2499 if (!mc.IsClsComplianceRequired ())
2502 string name = (string) entry.Key;
2503 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2505 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2506 object found = base_members [lcase];
2507 if (found == null) {
2508 found = this_members [lcase];
2509 if (found == null) {
2510 this_members.Add (lcase, mc);
2515 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2518 if (found is MemberInfo) {
2519 if (basename == ((MemberInfo) found).Name)
2521 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2523 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2525 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2531 /// Performs checks for an explicit interface implementation. First it
2532 /// checks whether the `interface_type' is a base inteface implementation.
2533 /// Then it checks whether `name' exists in the interface type.
2535 public virtual bool VerifyImplements (MemberBase mb)
2537 if (ifaces != null) {
2538 foreach (Type t in ifaces){
2539 if (t == mb.InterfaceType)
2544 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2545 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2546 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2550 public void Mark_HasEquals ()
2552 Methods.HasEquals = true;
2555 public void Mark_HasGetHashCode ()
2557 Methods.HasGetHashCode = true;
2564 string IMemberContainer.Name {
2570 Type IMemberContainer.Type {
2576 MemberCache IMemberContainer.MemberCache {
2578 return member_cache;
2582 bool IMemberContainer.IsInterface {
2584 return Kind == Kind.Interface;
2588 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2590 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2592 if (GenericType != null)
2593 return TypeManager.FindMembers (GenericType, mt, new_bf,
2596 return FindMembers (mt, new_bf, null, null);
2600 // Generates xml doc comments (if any), and if required,
2601 // handle warning report.
2603 internal override void GenerateDocComment (DeclSpace ds)
2605 DocUtil.GenerateTypeDocComment (this, ds);
2608 public override string DocCommentHeader {
2609 get { return "T:"; }
2612 public virtual MemberCache BaseCache {
2614 if (base_cache != null)
2616 if (TypeBuilder.BaseType != null)
2617 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2618 if (TypeBuilder.IsInterface)
2619 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2626 public abstract class ClassOrStruct : TypeContainer {
2627 ListDictionary declarative_security;
2629 public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
2630 MemberName name, Attributes attrs, Kind kind)
2631 : base (ns, parent, name, attrs, kind)
2635 protected override bool AddToContainer (MemberCore symbol, string name)
2637 if (name == MemberName.Name) {
2638 if (symbol is TypeParameter) {
2639 Report.Error (694, symbol.Location,
2640 "Type parameter `{0}' has same name as " +
2641 "containing type, or method", name);
2645 Report.SymbolRelatedToPreviousError (this);
2646 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2647 symbol.GetSignatureForError ());
2651 return base.AddToContainer (symbol, name);
2654 public override void DefineDefaultConstructor ()
2656 DefineDefaultConstructor (true);
2659 public override void VerifyMembers ()
2661 base.VerifyMembers ();
2663 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2664 foreach (Event e in events){
2665 if ((e.caching_flags & Flags.IsAssigned) == 0)
2666 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2671 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2673 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2674 if (declarative_security == null)
2675 declarative_security = new ListDictionary ();
2677 a.ExtractSecurityPermissionSet (declarative_security);
2681 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2682 HasExplicitLayout = true;
2685 base.ApplyAttributeBuilder (a, cb);
2688 public override void Emit()
2692 if (declarative_security != null) {
2693 foreach (DictionaryEntry de in declarative_security) {
2694 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2701 // TODO: should be sealed
2702 public class Class : ClassOrStruct {
2703 const int AllowedModifiers =
2706 Modifiers.PROTECTED |
2707 Modifiers.INTERNAL |
2709 Modifiers.ABSTRACT |
2714 public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2716 : base (ns, parent, name, attrs, Kind.Class)
2718 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2719 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2721 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2722 Report.FeatureIsNotStandardized (Location, "static classes");
2726 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2728 if (a.Type == TypeManager.attribute_usage_type) {
2729 if (BaseType != TypeManager.attribute_type && !BaseType.IsSubclassOf (TypeManager.attribute_type) &&
2730 TypeBuilder.FullName != "System.Attribute") {
2731 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2735 if (a.Type == TypeManager.conditional_attribute_type &&
2736 !(BaseType == TypeManager.attribute_type || BaseType.IsSubclassOf (TypeManager.attribute_type))) {
2737 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2741 if (AttributeTester.IsAttributeExcluded (a.Type))
2744 base.ApplyAttributeBuilder (a, cb);
2747 public override AttributeTargets AttributeTargets {
2749 return AttributeTargets.Class;
2753 protected override void DefineContainerMembers (MemberCoreArrayList list)
2759 base.DefineContainerMembers (list);
2763 foreach (MemberCore m in list) {
2764 if (m is Operator) {
2765 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2769 if (m is Destructor) {
2770 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2774 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2775 Report.Error (1057, m.Location, "`{0}': Static classes cannot contain protected members", m.GetSignatureForError ());
2780 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2784 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2787 if (m is Constructor) {
2788 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2792 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2795 base.DefineContainerMembers (list);
2798 public override void DefineDefaultConstructor ()
2801 DefineDefaultConstructor (false);
2803 base.DefineDefaultConstructor ();
2806 public override TypeBuilder DefineType ()
2808 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2809 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2813 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2814 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2818 return base.DefineType ();
2821 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2823 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2825 if (base_class == null) {
2826 if (RootContext.StdLib)
2827 base_class = TypeManager.system_object_expr;
2828 else if (Name != "System.Object")
2829 base_class = TypeManager.system_object_expr;
2831 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2833 689, base_class.Location,
2834 "Cannot derive from `{0}' because it is a type parameter",
2835 base_class.GetSignatureForError ());
2839 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
2840 Report.Error (1521, base_class.Location, "Invalid base type");
2844 if (base_class.IsSealed){
2845 Report.SymbolRelatedToPreviousError (base_class.Type);
2846 if (base_class.Type.IsAbstract) {
2847 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2848 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2850 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2851 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2856 if (!base_class.CanInheritFrom ()){
2857 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2858 GetSignatureForError (), base_class.GetSignatureForError ());
2862 if (!base_class.AsAccessible (this, ModFlags)) {
2863 Report.SymbolRelatedToPreviousError (base_class.Type);
2864 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2865 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2870 if (base_class != TypeManager.system_object_expr) {
2871 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2872 GetSignatureForError (), base_class.GetSignatureForError ());
2876 if (ifaces != null) {
2877 foreach (TypeExpr t in ifaces)
2878 Report.SymbolRelatedToPreviousError (t.Type);
2879 Report.Error (714, Location, "`{0}': static classes cannot implement interfaces", GetSignatureForError ());
2886 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2887 /// Valid only for attribute classes.
2888 public bool IsExcluded ()
2890 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2891 return (caching_flags & Flags.Excluded) != 0;
2893 caching_flags &= ~Flags.Excluded_Undetected;
2895 if (OptAttributes == null)
2898 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
2903 foreach (Attribute a in attrs) {
2904 string condition = a.GetConditionalAttributeValue ();
2905 if (RootContext.AllDefines.Contains (condition))
2909 caching_flags |= Flags.Excluded;
2915 return (ModFlags & Modifiers.STATIC) != 0;
2920 // FIXME: How do we deal with the user specifying a different
2923 protected override TypeAttributes TypeAttr {
2925 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2927 ta |= TypeAttributes.Abstract | TypeAttributes.Sealed;
2933 public sealed class Struct : ClassOrStruct {
2935 // Modifiers allowed in a struct declaration
2937 const int AllowedModifiers =
2940 Modifiers.PROTECTED |
2941 Modifiers.INTERNAL |
2945 public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
2946 int mod, Attributes attrs)
2947 : base (ns, parent, name, attrs, Kind.Struct)
2951 if (parent.Parent == null)
2952 accmods = Modifiers.INTERNAL;
2954 accmods = Modifiers.PRIVATE;
2956 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2958 this.ModFlags |= Modifiers.SEALED;
2961 public override AttributeTargets AttributeTargets {
2963 return AttributeTargets.Struct;
2967 const TypeAttributes DefaultTypeAttributes =
2968 TypeAttributes.SequentialLayout |
2969 TypeAttributes.Sealed |
2970 TypeAttributes.BeforeFieldInit;
2973 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2975 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2977 // If we are compiling our runtime,
2978 // and we are defining ValueType, then our
2979 // base is `System.Object'.
2981 if (base_class == null) {
2982 if (!RootContext.StdLib && Name == "System.ValueType")
2983 base_class = TypeManager.system_object_expr;
2985 base_class = TypeManager.system_valuetype_expr;
2992 // FIXME: Allow the user to specify a different set of attributes
2993 // in some cases (Sealed for example is mandatory for a class,
2994 // but what SequentialLayout can be changed
2996 protected override TypeAttributes TypeAttr {
2998 return base.TypeAttr | DefaultTypeAttributes;
3002 public override void RegisterFieldForInitialization (FieldBase field)
3004 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3005 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3006 field.GetSignatureForError ());
3009 base.RegisterFieldForInitialization (field);
3017 public sealed class Interface : TypeContainer, IMemberContainer {
3020 /// Modifiers allowed in a class declaration
3022 public const int AllowedModifiers =
3025 Modifiers.PROTECTED |
3026 Modifiers.INTERNAL |
3030 public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
3032 : base (ns, parent, name, attrs, Kind.Interface)
3036 if (parent.Parent == null)
3037 accmods = Modifiers.INTERNAL;
3039 accmods = Modifiers.PRIVATE;
3041 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3044 public override AttributeTargets AttributeTargets {
3046 return AttributeTargets.Interface;
3050 const TypeAttributes DefaultTypeAttributes =
3051 TypeAttributes.AutoLayout |
3052 TypeAttributes.Abstract |
3053 TypeAttributes.Interface;
3055 protected override TypeAttributes TypeAttr {
3057 return base.TypeAttr | DefaultTypeAttributes;
3061 protected override bool VerifyClsCompliance ()
3063 if (!base.VerifyClsCompliance ())
3066 if (ifaces != null) {
3067 foreach (Type t in ifaces) {
3068 if (AttributeTester.IsClsCompliant (t))
3071 Report.SymbolRelatedToPreviousError (t);
3072 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3073 GetSignatureForError (), TypeManager.CSharpName (t));
3081 public abstract class MethodCore : MemberBase {
3082 public readonly Parameters Parameters;
3083 protected ToplevelBlock block;
3085 // Whether this is an operator method.
3086 public Operator IsOperator;
3089 // The method we're overriding if this is an override method.
3091 protected MethodInfo base_method = null;
3093 static string[] attribute_targets = new string [] { "method", "return" };
3095 public MethodCore (TypeContainer parent, GenericMethod generic,
3096 Expression type, int mod, int allowed_mod, bool is_iface,
3097 MemberName name, Attributes attrs, Parameters parameters)
3098 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3101 Parameters = parameters;
3102 IsInterface = is_iface;
3106 // Returns the System.Type array for the parameters of this method
3108 public Type [] ParameterTypes {
3110 return Parameters.Types;
3114 public Parameters ParameterInfo
3121 public ToplevelBlock Block {
3131 public void SetYields ()
3133 ModFlags |= Modifiers.METHOD_YIELDS;
3136 protected override bool CheckBase ()
3138 if (!base.CheckBase ())
3141 // Check whether arguments were correct.
3142 if (!DoDefineParameters ())
3145 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3151 // Is null for System.Object while compiling corlib and base interfaces
3152 if (Parent.BaseCache == null) {
3153 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3154 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3159 Type base_ret_type = null;
3160 base_method = FindOutBaseMethod (ref base_ret_type);
3162 // method is override
3163 if (base_method != null) {
3165 if (!CheckMethodAgainstBase ())
3168 if ((ModFlags & Modifiers.NEW) == 0) {
3169 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_ret_type))) {
3170 Report.SymbolRelatedToPreviousError (base_method);
3171 if (this is PropertyBase) {
3172 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3173 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3176 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3177 GetSignatureForError (), TypeManager.CSharpName (base_ret_type), TypeManager.CSharpSignature (base_method));
3182 if (base_method.IsAbstract && !IsInterface) {
3183 Report.SymbolRelatedToPreviousError (base_method);
3184 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3185 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3190 if (base_method.IsSpecialName && !(this is PropertyBase)) {
3191 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3195 if (Name == "Equals" && Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type)
3196 Parent.Mark_HasEquals ();
3197 else if (Name == "GetHashCode" && Parameters.Empty)
3198 Parent.Mark_HasGetHashCode ();
3200 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3201 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3203 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3204 Report.SymbolRelatedToPreviousError (base_method);
3205 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3206 GetSignatureForError (), TypeManager.CSharpSignature (base_method) );
3213 MemberInfo conflict_symbol = Parent.FindBaseMemberWithSameName (Name, !(this is Property));
3214 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3215 if (conflict_symbol != null) {
3216 Report.SymbolRelatedToPreviousError (conflict_symbol);
3217 if (this is PropertyBase)
3218 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3220 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3222 Report.Error (115, Location, "`{0}': no suitable method found to override", GetSignatureForError ());
3226 if (conflict_symbol == null) {
3227 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3228 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3233 if ((ModFlags & Modifiers.NEW) == 0) {
3234 if (this is Method && conflict_symbol is MethodBase)
3237 Report.SymbolRelatedToPreviousError (conflict_symbol);
3238 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3239 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3246 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3247 // that have been defined.
3249 // `name' is the user visible name for reporting errors (this is used to
3250 // provide the right name regarding method names and properties)
3252 bool CheckMethodAgainstBase ()
3256 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3257 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3258 Report.Error (506, Location,
3259 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3260 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3264 // Now we check that the overriden method is not final
3266 if (base_method.IsFinal) {
3267 Report.SymbolRelatedToPreviousError (base_method);
3268 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3269 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3273 // Check that the permissions are not being changed
3275 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3276 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3278 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3279 Error_CannotChangeAccessModifiers (base_method, base_classp, null);
3284 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3285 ModFlags |= Modifiers.NEW;
3286 Report.SymbolRelatedToPreviousError (base_method);
3287 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3288 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",
3289 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3291 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3292 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3299 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3301 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3303 // when overriding protected internal, the method can be declared
3304 // protected internal only within the same assembly
3307 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3308 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3310 // assemblies differ - report an error
3314 } else if (thisp != base_classp) {
3316 // same assembly, but other attributes differ - report an error
3321 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3323 // if it's not "protected internal", it must be "protected"
3327 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3329 // protected within the same assembly - an error
3332 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3333 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3335 // protected ok, but other attributes differ - report an error
3341 return (thisp == base_classp);
3345 public bool CheckAbstractAndExtern (bool has_block)
3347 if (Parent.Kind == Kind.Interface)
3351 if ((ModFlags & Modifiers.EXTERN) != 0) {
3352 Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
3353 GetSignatureForError ());
3357 if ((ModFlags & Modifiers.ABSTRACT) != 0) {
3358 Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
3359 GetSignatureForError ());
3363 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
3364 Report.Error (501, Location, "`{0}' must declare a body because it is not marked abstract or extern",
3365 GetSignatureForError ());
3373 protected void Error_CannotChangeAccessModifiers (MemberInfo base_method, MethodAttributes ma, string suffix)
3375 Report.SymbolRelatedToPreviousError (base_method);
3376 string base_name = TypeManager.GetFullNameSignature (base_method);
3377 string this_name = GetSignatureForError ();
3378 if (suffix != null) {
3379 base_name += suffix;
3380 this_name += suffix;
3383 Report.Error (507, Location, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3384 this_name, Modifiers.GetDescription (ma), base_name);
3387 protected static string Error722 {
3389 return "`{0}': static types cannot be used as return types";
3394 /// For custom member duplication search in a container
3396 protected abstract bool CheckForDuplications ();
3399 /// Gets base method and its return type
3401 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3403 protected bool DoDefineParameters ()
3405 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3407 // Check if arguments were correct
3408 if (!Parameters.Resolve (rc))
3411 return CheckParameters (ParameterTypes);
3414 bool CheckParameters (Type [] parameters)
3418 foreach (Type partype in parameters){
3419 if (partype == TypeManager.void_type) {
3421 1547, Location, "Keyword 'void' cannot " +
3422 "be used in this context");
3426 if (partype.IsPointer){
3427 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
3431 if (ds.AsAccessible (partype, ModFlags))
3434 if (this is Indexer)
3435 Report.Error (55, Location,
3436 "Inconsistent accessibility: parameter type `" +
3437 TypeManager.CSharpName (partype) + "' is less " +
3438 "accessible than indexer `" + GetSignatureForError () + "'");
3439 else if ((this is Method) && ((Method) this).IsOperator != null)
3440 Report.Error (57, Location,
3441 "Inconsistent accessibility: parameter type `" +
3442 TypeManager.CSharpName (partype) + "' is less " +
3443 "accessible than operator `" + GetSignatureForError () + "'");
3445 Report.Error (51, Location,
3446 "Inconsistent accessibility: parameter type `" +
3447 TypeManager.CSharpName (partype) + "' is less " +
3448 "accessible than method `" + GetSignatureForError () + "'");
3455 public override string[] ValidAttributeTargets {
3457 return attribute_targets;
3461 protected override bool VerifyClsCompliance ()
3463 if (!base.VerifyClsCompliance ()) {
3464 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3465 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3470 if (Parameters.HasArglist) {
3471 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3474 if (!AttributeTester.IsClsCompliant (MemberType)) {
3475 if (this is PropertyBase)
3476 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3477 GetSignatureForError ());
3479 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3480 GetSignatureForError ());
3483 Parameters.VerifyClsCompliance ();
3488 protected bool IsDuplicateImplementation (MethodCore method)
3490 if (method == this || !(method.MemberName.Equals (MemberName)))
3493 Type[] param_types = method.ParameterTypes;
3494 // This never happen. Rewrite this as Equal
3495 if (param_types == null && ParameterTypes == null)
3497 if (param_types == null || ParameterTypes == null)
3500 if (param_types.Length != ParameterTypes.Length)
3503 if (method.Parameters.HasArglist != Parameters.HasArglist)
3508 for (int i = 0; i < param_types.Length; i++) {
3509 if (param_types [i] != ParameterTypes [i])
3513 if (IsExplicitImpl && (method.InterfaceType != InterfaceType))
3516 // TODO: make operator compatible with MethodCore to avoid this
3517 if (this is Operator && method is Operator) {
3518 if (MemberType != method.MemberType)
3524 // Try to report 663: method only differs on out/ref
3526 Parameters info = ParameterInfo;
3527 Parameters other_info = method.ParameterInfo;
3528 for (int i = 0; i < info.Count; i++){
3530 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3531 Report.SymbolRelatedToPreviousError (method);
3532 Report.Error (663, Location, "`{0}': Methods cannot differ only on their use of ref and out on a parameters",
3533 GetSignatureForError ());
3536 Console.WriteLine ("Method is: {0} {1}", method.Location, method);
3537 Console.WriteLine ("this is: {0} {1}", Location, this);
3541 Report.SymbolRelatedToPreviousError (method);
3542 if (this is Operator && method is Operator)
3543 Report.Error (557, Location, "Duplicate user-defined conversion in type `{0}'", Parent.Name);
3545 Report.Error (111, Location, TypeContainer.Error111, GetSignatureForError ());
3553 public override bool IsUsed {
3554 get { return IsExplicitImpl || base.IsUsed; }
3558 // Returns a string that represents the signature for this
3559 // member which should be used in XML documentation.
3561 public override string GetDocCommentName (DeclSpace ds)
3563 return DocUtil.GetMethodDocCommentName (this, ds);
3567 // Raised (and passed an XmlElement that contains the comment)
3568 // when GenerateDocComment is writing documentation expectedly.
3570 // FIXME: with a few effort, it could be done with XmlReader,
3571 // that means removal of DOM use.
3573 internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
3575 DocUtil.OnMethodGenerateDocComment (this, ds, el);
3579 // Represents header string for documentation comment.
3581 public override string DocCommentHeader {
3582 get { return "M:"; }
3587 public class SourceMethod : ISourceMethod
3589 TypeContainer container;
3592 protected SourceMethod (TypeContainer container, MethodBase builder,
3593 ISourceFile file, Location start, Location end)
3595 this.container = container;
3596 this.builder = builder;
3598 CodeGen.SymbolWriter.OpenMethod (
3599 file, this, start.Row, start.Column, end.Row, start.Column);
3602 public string Name {
3603 get { return builder.Name; }
3606 public int NamespaceID {
3607 get { return container.NamespaceEntry.SymbolFileID; }
3612 if (builder is MethodBuilder)
3613 return ((MethodBuilder) builder).GetToken ().Token;
3614 else if (builder is ConstructorBuilder)
3615 return ((ConstructorBuilder) builder).GetToken ().Token;
3617 throw new NotSupportedException ();
3621 public void CloseMethod ()
3623 if (CodeGen.SymbolWriter != null)
3624 CodeGen.SymbolWriter.CloseMethod ();
3627 public static SourceMethod Create (TypeContainer parent,
3628 MethodBase builder, Block block)
3630 if (CodeGen.SymbolWriter == null)
3635 Location start_loc = block.StartLocation;
3636 if (start_loc.IsNull)
3639 Location end_loc = block.EndLocation;
3643 ISourceFile file = start_loc.SourceFile;
3647 return new SourceMethod (
3648 parent, builder, file, start_loc, end_loc);
3652 public class Method : MethodCore, IIteratorContainer, IMethodData {
3653 public MethodBuilder MethodBuilder;
3654 public MethodData MethodData;
3655 ReturnParameter return_attributes;
3656 ListDictionary declarative_security;
3659 /// Modifiers allowed in a class declaration
3661 const int AllowedModifiers =
3664 Modifiers.PROTECTED |
3665 Modifiers.INTERNAL |
3670 Modifiers.OVERRIDE |
3671 Modifiers.ABSTRACT |
3673 Modifiers.METHOD_YIELDS |
3676 const int AllowedInterfaceModifiers =
3677 Modifiers.NEW | Modifiers.UNSAFE;
3680 // return_type can be "null" for VOID values.
3682 public Method (TypeContainer parent, GenericMethod generic,
3683 Expression return_type, int mod, bool is_iface,
3684 MemberName name, Parameters parameters, Attributes attrs)
3685 : base (parent, generic, return_type, mod,
3686 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3687 is_iface, name, attrs, parameters)
3691 public override AttributeTargets AttributeTargets {
3693 return AttributeTargets.Method;
3697 public override string GetSignatureForError()
3699 if (IsOperator != null)
3700 return IsOperator.GetSignatureForError ();
3702 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
3705 void DuplicateEntryPoint (MethodInfo b, Location location)
3709 "Program `" + CodeGen.FileName +
3710 "' has more than one entry point defined: `" +
3711 TypeManager.CSharpSignature(b) + "'");
3714 bool IsEntryPoint (MethodBuilder b, Parameters pinfo)
3716 if (b.ReturnType != TypeManager.void_type &&
3717 b.ReturnType != TypeManager.int32_type)
3720 if (pinfo.Count == 0)
3723 if (pinfo.Count > 1)
3726 Type t = pinfo.ParameterType(0);
3728 (t.GetArrayRank() == 1) &&
3729 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3730 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3736 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3738 if (a.Target == AttributeTargets.ReturnValue) {
3739 if (return_attributes == null)
3740 return_attributes = new ReturnParameter (MethodBuilder, Location);
3742 return_attributes.ApplyAttributeBuilder (a, cb);
3746 if (a.Type == TypeManager.methodimpl_attr_type &&
3747 (a.GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0) {
3748 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3751 if (a.Type == TypeManager.dllimport_type) {
3752 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3753 if ((ModFlags & extern_static) != extern_static) {
3754 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3760 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3761 if (declarative_security == null)
3762 declarative_security = new ListDictionary ();
3763 a.ExtractSecurityPermissionSet (declarative_security);
3767 if (a.Type == TypeManager.conditional_attribute_type) {
3768 if (IsOperator != null || IsExplicitImpl) {
3769 Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3770 GetSignatureForError ());
3774 if (ReturnType != TypeManager.void_type) {
3775 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
3779 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3780 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
3785 Report.Error (582, Location, "Conditional not valid on interface members");
3789 if (MethodData.implementing != null) {
3790 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
3791 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
3795 for (int i = 0; i < ParameterInfo.Count; ++i) {
3796 if ((ParameterInfo.ParameterModifier (i) & Parameter.Modifier.OUTMASK) != 0) {
3797 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
3803 MethodBuilder.SetCustomAttribute (cb);
3806 protected override bool CheckForDuplications ()
3808 ArrayList ar = Parent.Methods;
3810 int arLen = ar.Count;
3812 for (int i = 0; i < arLen; i++) {
3813 Method m = (Method) ar [i];
3814 if (IsDuplicateImplementation (m))
3819 ar = Parent.Properties;
3821 for (int i = 0; i < ar.Count; ++i) {
3822 PropertyBase pb = (PropertyBase) ar [i];
3823 if (pb.AreAccessorsDuplicateImplementation (this))
3828 ar = Parent.Indexers;
3830 for (int i = 0; i < ar.Count; ++i) {
3831 PropertyBase pb = (PropertyBase) ar [i];
3832 if (pb.AreAccessorsDuplicateImplementation (this))
3839 for (int i = 0; i < ar.Count; ++i) {
3840 Event ev = (Event) ar [i];
3841 if (ev.AreAccessorsDuplicateImplementation (this))
3852 public override bool Define ()
3854 if (!DoDefineBase ())
3857 MethodBuilder mb = null;
3858 if (GenericMethod != null) {
3859 string method_name = MemberName.Name;
3861 if (IsExplicitImpl) {
3862 method_name = TypeManager.CSharpName (InterfaceType) +
3866 mb = Parent.TypeBuilder.DefineMethod (method_name, flags);
3868 if (!GenericMethod.Define (mb))
3875 if (!CheckAbstractAndExtern (block != null))
3878 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
3879 Error1599 (Location, ReturnType);
3886 if (IsOperator != null)
3887 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
3889 MethodData = new MethodData (this, ModFlags, flags, this, mb, GenericMethod, base_method);
3891 if (!MethodData.Define (ParentContainer))
3894 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
3895 Name == "Finalize" && !(this is Destructor)) {
3896 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
3900 // Setup iterator if we are one
3902 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3903 Iterator iterator = new Iterator (
3904 this, Parent, GenericMethod, ModFlags);
3906 if (!iterator.DefineIterator ())
3910 MethodBuilder = MethodData.MethodBuilder;
3913 // This is used to track the Entry Point,
3915 if (Name == "Main" &&
3916 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3917 (RootContext.MainClass == null ||
3918 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3919 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3920 IMethodData md = TypeManager.GetMethod (MethodBuilder);
3921 md.SetMemberIsUsed ();
3923 if (RootContext.EntryPoint == null) {
3924 if (Parent.IsGeneric){
3925 Report.Error (-201, Location,
3926 "Entry point can not be defined in a generic class");
3929 RootContext.EntryPoint = MethodBuilder;
3930 RootContext.EntryPointLocation = Location;
3932 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3933 DuplicateEntryPoint (MethodBuilder, Location);
3936 if (RootContext.WarningLevel >= 4)
3937 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder));
3941 if (MemberType.IsAbstract && MemberType.IsSealed) {
3942 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3952 public override void Emit ()
3954 MethodData.Emit (Parent);
3957 if (declarative_security != null) {
3958 foreach (DictionaryEntry de in declarative_security) {
3959 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3967 public static void Error1599 (Location loc, Type t)
3969 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
3972 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
3974 MethodInfo mi = (MethodInfo) Parent.BaseCache.FindMemberToOverride (
3975 Parent.TypeBuilder, Name, ParameterTypes, GenericMethod, false);
3980 base_ret_type = mi.ReturnType;
3984 public override bool MarkForDuplicationCheck ()
3986 caching_flags |= Flags.TestMethodDuplication;
3990 protected override bool VerifyClsCompliance ()
3992 if (!base.VerifyClsCompliance ())
3995 if (ParameterInfo.Count > 0) {
3996 ArrayList al = (ArrayList)ParentContainer.MemberCache.Members [Name];
3998 ParentContainer.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4004 #region IMethodData Members
4006 public CallingConventions CallingConventions {
4008 CallingConventions cc = Parameters.CallingConvention;
4009 if (Parameters.HasArglist)
4010 block.HasVarargs = true;
4013 if ((ModFlags & Modifiers.STATIC) == 0)
4014 cc |= CallingConventions.HasThis;
4016 // FIXME: How is `ExplicitThis' used in C#?
4022 public Type ReturnType {
4028 public MemberName MethodName {
4034 public new Location Location {
4036 return base.Location;
4040 protected override bool CheckBase() {
4041 if (!base.CheckBase ())
4044 // TODO: Destructor should derive from MethodCore
4045 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
4046 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
4047 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
4054 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4056 EmitContext ec = new EmitContext (this,
4057 tc, ds, Location, ig, ReturnType, ModFlags, false);
4059 Iterator iterator = tc as Iterator;
4060 if (iterator != null)
4061 ec.CurrentAnonymousMethod = iterator.Host;
4067 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
4069 public bool IsExcluded ()
4071 if ((caching_flags & Flags.Excluded_Undetected) == 0)
4072 return (caching_flags & Flags.Excluded) != 0;
4074 caching_flags &= ~Flags.Excluded_Undetected;
4076 if (base_method == null) {
4077 if (OptAttributes == null)
4080 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
4085 foreach (Attribute a in attrs) {
4086 string condition = a.GetConditionalAttributeValue ();
4087 if (RootContext.AllDefines.Contains (condition))
4091 caching_flags |= Flags.Excluded;
4095 IMethodData md = TypeManager.GetMethod (base_method);
4097 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
4098 caching_flags |= Flags.Excluded;
4104 if (md.IsExcluded ()) {
4105 caching_flags |= Flags.Excluded;
4111 GenericMethod IMethodData.GenericMethod {
4113 return GenericMethod;
4120 public abstract class ConstructorInitializer {
4121 ArrayList argument_list;
4122 protected ConstructorInfo base_constructor;
4125 public ConstructorInitializer (ArrayList argument_list, Location loc)
4127 this.argument_list = argument_list;
4131 public ArrayList Arguments {
4133 return argument_list;
4137 public bool Resolve (ConstructorBuilder caller_builder, Block block, EmitContext ec)
4139 Expression base_constructor_group;
4143 ec.CurrentBlock = block;
4145 if (argument_list != null){
4146 foreach (Argument a in argument_list){
4147 if (!a.Resolve (ec, loc))
4151 ec.CurrentBlock = null;
4153 if (this is ConstructorBaseInitializer) {
4154 if (ec.ContainerType.BaseType == null)
4157 t = ec.ContainerType.BaseType;
4158 if (ec.ContainerType.IsValueType) {
4159 Report.Error (522, loc,
4160 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4164 t = ec.ContainerType;
4166 base_constructor_group = Expression.MemberLookup (
4167 ec.ContainerType, t, ".ctor", MemberTypes.Constructor,
4168 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4171 if (base_constructor_group == null){
4173 base_constructor_group = Expression.MemberLookup (
4174 t, null, t, ".ctor", MemberTypes.Constructor,
4175 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4179 int errors = Report.Errors;
4180 if (base_constructor_group != null)
4181 base_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4182 ec, (MethodGroupExpr) base_constructor_group, argument_list,
4185 if (base_constructor == null) {
4186 if (errors == Report.Errors)
4187 Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpSignature (caller_builder),
4188 argument_list == null ? 0 : argument_list.Count);
4193 Expression.ErrorIsInaccesible (loc, TypeManager.CSharpSignature (base_constructor));
4194 base_constructor = null;
4198 if (base_constructor == caller_builder){
4199 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4206 public virtual void Emit (EmitContext ec)
4208 if (base_constructor != null){
4209 ec.Mark (loc, false);
4211 Invocation.EmitCall (ec, true, true, null, base_constructor, argument_list, loc);
4213 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), base_constructor, argument_list, loc);
4218 public class ConstructorBaseInitializer : ConstructorInitializer {
4219 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4220 base (argument_list, l)
4225 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4226 public GeneratedBaseInitializer (Location loc):
4231 public override void Emit(EmitContext ec)
4233 bool old = ec.TestObsoleteMethodUsage;
4234 ec.TestObsoleteMethodUsage = false;
4236 ec.TestObsoleteMethodUsage = old;
4240 public class ConstructorThisInitializer : ConstructorInitializer {
4241 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4242 base (argument_list, l)
4247 public class Constructor : MethodCore, IMethodData {
4248 public ConstructorBuilder ConstructorBuilder;
4249 public ConstructorInitializer Initializer;
4250 ListDictionary declarative_security;
4253 // Modifiers allowed for a constructor.
4255 public const int AllowedModifiers =
4257 Modifiers.PROTECTED |
4258 Modifiers.INTERNAL |
4264 bool has_compliant_args = false;
4266 // The spec claims that static is not permitted, but
4267 // my very own code has static constructors.
4269 public Constructor (TypeContainer ds, string name, int mod, Parameters args,
4270 ConstructorInitializer init, Location loc)
4271 : base (ds, null, null, mod, AllowedModifiers, false,
4272 new MemberName (name, loc), null, args)
4277 public bool HasCompliantArgs {
4279 return has_compliant_args;
4283 public override AttributeTargets AttributeTargets {
4285 return AttributeTargets.Constructor;
4291 // Returns true if this is a default constructor
4293 public bool IsDefault ()
4295 if ((ModFlags & Modifiers.STATIC) != 0)
4296 return Parameters.Empty;
4298 return Parameters.Empty &&
4299 (Initializer is ConstructorBaseInitializer) &&
4300 (Initializer.Arguments == null);
4303 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4305 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4306 if (declarative_security == null) {
4307 declarative_security = new ListDictionary ();
4309 a.ExtractSecurityPermissionSet (declarative_security);
4313 ConstructorBuilder.SetCustomAttribute (cb);
4316 protected override bool CheckForDuplications ()
4318 ArrayList ar = Parent.InstanceConstructors;
4320 int arLen = ar.Count;
4322 for (int i = 0; i < arLen; i++) {
4323 Constructor m = (Constructor) ar [i];
4324 if (IsDuplicateImplementation (m))
4331 protected override bool CheckBase ()
4333 // Check whether arguments were correct.
4334 if (!DoDefineParameters ())
4337 // TODO: skip the rest for generated ctor
4338 if ((ModFlags & Modifiers.STATIC) != 0)
4341 if (!CheckForDuplications ())
4344 if (Parent.Kind == Kind.Struct) {
4345 if (ParameterTypes.Length == 0) {
4346 Report.Error (568, Location,
4347 "Structs cannot contain explicit parameterless constructors");
4351 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4352 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4357 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4358 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4365 // Creates the ConstructorBuilder
4367 public override bool Define ()
4369 if (ConstructorBuilder != null)
4372 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4373 MethodAttributes.SpecialName);
4375 if ((ModFlags & Modifiers.STATIC) != 0){
4376 ca |= MethodAttributes.Static | MethodAttributes.Private;
4378 ca |= MethodAttributes.HideBySig;
4380 if ((ModFlags & Modifiers.PUBLIC) != 0)
4381 ca |= MethodAttributes.Public;
4382 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4383 if ((ModFlags & Modifiers.INTERNAL) != 0)
4384 ca |= MethodAttributes.FamORAssem;
4386 ca |= MethodAttributes.Family;
4387 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4388 ca |= MethodAttributes.Assembly;
4389 else if (IsDefault ())
4390 ca |= MethodAttributes.Public;
4392 ca |= MethodAttributes.Private;
4395 if (!CheckAbstractAndExtern (block != null))
4398 // Check if arguments were correct.
4402 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4403 ca, CallingConventions,
4406 if ((ModFlags & Modifiers.UNSAFE) != 0)
4407 ConstructorBuilder.InitLocals = false;
4409 if (Parent.IsComImport) {
4410 if (!IsDefault ()) {
4411 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4412 Parent.GetSignatureForError ());
4415 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4418 TypeManager.AddMethod (ConstructorBuilder, this);
4426 public override void Emit ()
4428 EmitContext ec = CreateEmitContext (null, null);
4429 if (GetObsoleteAttribute () != null || Parent.GetObsoleteAttribute () != null)
4430 ec.TestObsoleteMethodUsage = false;
4432 // If this is a non-static `struct' constructor and doesn't have any
4433 // initializer, it must initialize all of the struct's fields.
4434 if ((Parent.Kind == Kind.Struct) &&
4435 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4436 Block.AddThisVariable (Parent, Location);
4438 if (block != null) {
4439 if (!block.ResolveMeta (ec, ParameterInfo))
4443 if ((ModFlags & Modifiers.STATIC) == 0){
4444 if (Parent.Kind == Kind.Class && Initializer == null)
4445 Initializer = new GeneratedBaseInitializer (Location);
4449 // Spec mandates that Initializers will not have
4453 if ((Initializer != null) &&
4454 !Initializer.Resolve (ConstructorBuilder, block, ec))
4456 ec.IsStatic = false;
4459 Parameters.ApplyAttributes (ConstructorBuilder);
4461 SourceMethod source = SourceMethod.Create (
4462 Parent, ConstructorBuilder, block);
4465 // Classes can have base initializers and instance field initializers.
4467 if (Parent.Kind == Kind.Class){
4468 if ((ModFlags & Modifiers.STATIC) == 0){
4471 // If we use a "this (...)" constructor initializer, then
4472 // do not emit field initializers, they are initialized in the other constructor
4474 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4475 ParentContainer.EmitFieldInitializers (ec);
4478 if (Initializer != null) {
4479 Initializer.Emit (ec);
4482 if ((ModFlags & Modifiers.STATIC) != 0)
4483 ParentContainer.EmitFieldInitializers (ec);
4485 if (OptAttributes != null)
4486 OptAttributes.Emit ();
4488 ec.EmitTopBlock (this, block);
4491 source.CloseMethod ();
4495 if (declarative_security != null) {
4496 foreach (DictionaryEntry de in declarative_security) {
4497 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4504 // Is never override
4505 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4510 public override string GetSignatureForError()
4512 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4515 protected override bool VerifyClsCompliance ()
4517 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4521 if (ParameterInfo.Count > 0) {
4522 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4524 Parent.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4526 if (Parent.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4527 foreach (Type param in ParameterTypes) {
4528 if (param.IsArray) {
4534 has_compliant_args = true;
4538 #region IMethodData Members
4540 public System.Reflection.CallingConventions CallingConventions {
4542 CallingConventions cc = Parameters.CallingConvention;
4544 if (Parent.Kind == Kind.Class)
4545 if ((ModFlags & Modifiers.STATIC) == 0)
4546 cc |= CallingConventions.HasThis;
4548 // FIXME: How is `ExplicitThis' used in C#?
4554 public new Location Location {
4556 return base.Location;
4560 public MemberName MethodName {
4566 public Type ReturnType {
4572 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4574 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4575 return new EmitContext (this, Parent, Location, ig_, null, ModFlags, true);
4578 public bool IsExcluded()
4583 GenericMethod IMethodData.GenericMethod {
4593 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4595 public interface IMethodData
4597 CallingConventions CallingConventions { get; }
4598 Location Location { get; }
4599 MemberName MethodName { get; }
4600 Type ReturnType { get; }
4601 GenericMethod GenericMethod { get; }
4602 Parameters ParameterInfo { get; }
4604 Attributes OptAttributes { get; }
4605 ToplevelBlock Block { get; set; }
4607 EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
4608 ObsoleteAttribute GetObsoleteAttribute ();
4609 string GetSignatureForError ();
4611 bool IsClsComplianceRequired ();
4612 void SetMemberIsUsed ();
4616 // Encapsulates most of the Method's state
4618 public class MethodData {
4620 readonly IMethodData method;
4622 public readonly GenericMethod GenericMethod;
4625 // Are we implementing an interface ?
4627 public MethodInfo implementing;
4632 protected MemberBase member;
4633 protected int modifiers;
4634 protected MethodAttributes flags;
4635 protected Type declaring_type;
4636 protected MethodInfo parent_method;
4638 MethodBuilder builder = null;
4639 public MethodBuilder MethodBuilder {
4645 public Type DeclaringType {
4647 return declaring_type;
4651 public MethodData (MemberBase member,
4652 int modifiers, MethodAttributes flags, IMethodData method)
4654 this.member = member;
4655 this.modifiers = modifiers;
4658 this.method = method;
4661 public MethodData (MemberBase member,
4662 int modifiers, MethodAttributes flags,
4663 IMethodData method, MethodBuilder builder,
4664 GenericMethod generic, MethodInfo parent_method)
4665 : this (member, modifiers, flags, method)
4667 this.builder = builder;
4668 this.GenericMethod = generic;
4669 this.parent_method = parent_method;
4672 public bool Define (TypeContainer container)
4674 string name = method.MethodName.Basename;
4675 string method_name = method.MethodName.FullName;
4677 PendingImplementation pending = container.PendingImplementations;
4678 if (pending != null){
4679 if (member is Indexer) // TODO: test it, but it should work without this IF
4680 implementing = pending.IsInterfaceIndexer (
4681 member.InterfaceType, method.ReturnType, method.ParameterInfo);
4683 implementing = pending.IsInterfaceMethod (
4684 member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
4686 if (member.InterfaceType != null){
4687 if (implementing == null){
4688 if (member is PropertyBase) {
4689 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4690 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4691 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4694 Report.Error (539, method.Location,
4695 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4696 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4700 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4701 Report.SymbolRelatedToPreviousError (implementing);
4702 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4703 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4707 method_name = TypeManager.GetFullName (member.InterfaceType) +
4710 if (implementing != null) {
4711 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4712 if (prop_method != null) {
4713 if (!implementing.IsSpecialName) {
4714 Report.SymbolRelatedToPreviousError (implementing);
4715 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4716 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4719 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4720 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4721 Report.SymbolRelatedToPreviousError (implementing);
4722 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4723 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4732 // For implicit implementations, make sure we are public, for
4733 // explicit implementations, make sure we are private.
4735 if (implementing != null){
4737 // Setting null inside this block will trigger a more
4738 // verbose error reporting for missing interface implementations
4740 // The "candidate" function has been flagged already
4741 // but it wont get cleared
4743 if (member.IsExplicitImpl){
4744 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4745 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4746 implementing = null;
4748 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4749 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4751 // If this is an interface method implementation,
4752 // check for public accessibility
4754 implementing = null;
4755 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4756 // We may never be private.
4757 implementing = null;
4758 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4760 // We may be protected if we're overriding something.
4762 implementing = null;
4767 // Static is not allowed
4769 if ((modifiers & Modifiers.STATIC) != 0){
4770 implementing = null;
4775 // If implementing is still valid, set flags
4777 if (implementing != null){
4779 // When implementing interface methods, set NewSlot
4780 // unless, we are overwriting a method.
4782 if (implementing.DeclaringType.IsInterface){
4783 if ((modifiers & Modifiers.OVERRIDE) == 0)
4784 flags |= MethodAttributes.NewSlot;
4787 MethodAttributes.Virtual |
4788 MethodAttributes.HideBySig;
4790 // Set Final unless we're virtual, abstract or already overriding a method.
4791 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4792 flags |= MethodAttributes.Final;
4795 EmitContext ec = method.CreateEmitContext (container, null);
4797 DefineMethodBuilder (container, method_name, method.ParameterInfo.Types);
4799 if (builder == null)
4802 if (container.CurrentType != null)
4803 declaring_type = container.CurrentType;
4805 declaring_type = container.TypeBuilder;
4807 if ((modifiers & Modifiers.UNSAFE) != 0)
4808 builder.InitLocals = false;
4810 if (implementing != null){
4812 // clear the pending implemntation flag
4814 if (member is Indexer) {
4815 pending.ImplementIndexer (
4816 member.InterfaceType, builder, method.ReturnType,
4817 method.ParameterInfo, member.IsExplicitImpl);
4819 pending.ImplementMethod (
4820 member.InterfaceType, name, method.ReturnType,
4821 method.ParameterInfo, member.IsExplicitImpl);
4823 if (member.IsExplicitImpl)
4824 container.TypeBuilder.DefineMethodOverride (
4825 builder, implementing);
4829 TypeManager.AddMethod (builder, method);
4831 if (GenericMethod != null) {
4832 bool is_override = member.IsExplicitImpl |
4833 ((modifiers & Modifiers.OVERRIDE) != 0);
4835 if (implementing != null)
4836 parent_method = implementing;
4838 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
4847 /// Create the MethodBuilder for the method
4849 void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
4851 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4853 if ((modifiers & extern_static) == extern_static) {
4855 if (method.OptAttributes != null) {
4856 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type);
4857 if (dllimport_attribute != null) {
4858 flags |= MethodAttributes.PinvokeImpl;
4859 builder = dllimport_attribute.DefinePInvokeMethod (
4860 container.TypeBuilder, method_name, flags,
4861 method.ReturnType, ParameterTypes);
4867 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4868 // We are more strict than Microsoft and report CS0626 like error
4869 if (method.OptAttributes == null ||
4870 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type)) {
4871 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",
4872 method.GetSignatureForError ());
4877 if (builder == null) {
4878 builder = container.TypeBuilder.DefineMethod (
4879 method_name, flags, method.CallingConventions,
4880 method.ReturnType, ParameterTypes);
4884 builder.SetGenericMethodSignature (
4885 flags, method.CallingConventions,
4886 method.ReturnType, ParameterTypes);
4893 public void Emit (TypeContainer container)
4896 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4897 ec = method.CreateEmitContext (container, builder.GetILGenerator ());
4899 ec = method.CreateEmitContext (container, null);
4901 if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute () != null)
4902 ec.TestObsoleteMethodUsage = false;
4904 method.ParameterInfo.ApplyAttributes (MethodBuilder);
4906 Attributes OptAttributes = method.OptAttributes;
4908 if (OptAttributes != null)
4909 OptAttributes.Emit ();
4911 if (GenericMethod != null)
4912 GenericMethod.EmitAttributes ();
4914 ToplevelBlock block = method.Block;
4916 SourceMethod source = SourceMethod.Create (
4917 container, MethodBuilder, method.Block);
4920 // Handle destructors specially
4922 // FIXME: This code generates buggy code
4924 if (member is Destructor)
4925 EmitDestructor (ec, block);
4927 ec.EmitTopBlock (method, block);
4930 source.CloseMethod ();
4933 void EmitDestructor (EmitContext ec, ToplevelBlock block)
4935 ILGenerator ig = ec.ig;
4937 Label finish = ig.DefineLabel ();
4939 block.SetDestructor ();
4941 ig.BeginExceptionBlock ();
4942 ec.ReturnLabel = finish;
4943 ec.HasReturnLabel = true;
4944 ec.EmitTopBlock (method, block);
4946 // ig.MarkLabel (finish);
4947 ig.BeginFinallyBlock ();
4949 if (ec.ContainerType.BaseType != null) {
4950 Expression member_lookup = Expression.MemberLookup (
4951 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
4952 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
4954 if (member_lookup != null){
4955 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
4957 ig.Emit (OpCodes.Ldarg_0);
4958 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
4962 ig.EndExceptionBlock ();
4963 //ig.MarkLabel (ec.ReturnLabel);
4964 ig.Emit (OpCodes.Ret);
4968 // TODO: Should derive from MethodCore
4969 public class Destructor : Method {
4971 public Destructor (TypeContainer ds, Expression return_type, int mod,
4972 string name, Parameters parameters, Attributes attrs,
4974 : base (ds, null, return_type, mod, false, new MemberName (name, l),
4978 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
4980 if (a.Type == TypeManager.conditional_attribute_type) {
4981 Report.Error (577, Location, "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
4982 GetSignatureForError ());
4986 base.ApplyAttributeBuilder (a, cb);
4989 public override string GetSignatureForError ()
4991 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
4996 abstract public class MemberBase : MemberCore {
4997 public Expression Type;
4999 public MethodAttributes flags;
5000 public readonly DeclSpace ds;
5001 public readonly GenericMethod GenericMethod;
5003 protected readonly int explicit_mod_flags;
5006 // The "short" name of this property / indexer / event. This is the
5007 // name without the explicit interface.
5009 public string ShortName {
5010 get { return MemberName.Name; }
5011 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
5014 public new TypeContainer Parent {
5015 get { return (TypeContainer) base.Parent; }
5019 /// Use this for access to parent container from partial classes (not master partial).
5020 /// This should be used rarely as all members are hosted in master partial class.
5021 /// Also with some effort this should be eliminated (not easy now).
5023 public TypeContainer ParentContainer {
5025 return base.Parent.IsPartial ?
5026 Parent.PartialContainer : Parent;
5031 // The type of this property / indexer / event
5033 protected Type member_type;
5034 public Type MemberType {
5036 if (member_type == null && Type != null) {
5037 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
5038 Type = Type.ResolveAsTypeTerminal (rc, false);
5040 member_type = Type.Type;
5048 // Whether this is an interface member.
5050 public bool IsInterface;
5053 // If true, this is an explicit interface implementation
5055 public bool IsExplicitImpl;
5058 // The interface type we are explicitly implementing
5060 public Type InterfaceType = null;
5063 // The constructor is only exposed to our children
5065 protected MemberBase (TypeContainer parent, GenericMethod generic,
5066 Expression type, int mod, int allowed_mod, int def_mod,
5067 MemberName name, Attributes attrs)
5068 : base (parent, name, attrs)
5070 this.ds = generic != null ? generic : (DeclSpace) parent;
5071 explicit_mod_flags = mod;
5073 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5074 IsExplicitImpl = (MemberName.Left != null);
5075 GenericMethod = generic;
5076 if (GenericMethod != null)
5077 GenericMethod.ModFlags = ModFlags;
5080 protected virtual bool CheckBase ()
5082 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
5083 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
5087 if ((RootContext.WarningLevel >= 4) &&
5088 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
5089 ((ModFlags & Modifiers.PROTECTED) != 0) &&
5090 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
5091 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
5096 protected virtual bool DoDefineBase ()
5099 throw new InternalErrorException ();
5102 ModFlags = Modifiers.PUBLIC |
5103 Modifiers.ABSTRACT |
5104 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
5106 flags = MethodAttributes.Public |
5107 MethodAttributes.Abstract |
5108 MethodAttributes.HideBySig |
5109 MethodAttributes.NewSlot |
5110 MethodAttributes.Virtual;
5112 if (!Parent.MethodModifiersValid (this))
5115 flags = Modifiers.MethodAttr (ModFlags);
5118 if (IsExplicitImpl) {
5119 Expression expr = MemberName.Left.GetTypeExpression ();
5120 TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (this, false);
5121 if (iface_texpr == null)
5124 InterfaceType = iface_texpr.Type;
5126 if (!InterfaceType.IsInterface) {
5127 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
5131 if (!ParentContainer.VerifyImplements (this))
5134 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5140 protected virtual bool DoDefine ()
5142 if (MemberType == null)
5145 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5146 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5147 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5148 GetSignatureForError (), Parent.GetSignatureForError ());
5152 // verify accessibility
5153 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5154 Report.SymbolRelatedToPreviousError (MemberType);
5155 if (this is Property)
5156 Report.Error (53, Location,
5157 "Inconsistent accessibility: property type `" +
5158 TypeManager.CSharpName (MemberType) + "' is less " +
5159 "accessible than property `" + GetSignatureForError () + "'");
5160 else if (this is Indexer)
5161 Report.Error (54, Location,
5162 "Inconsistent accessibility: indexer return type `" +
5163 TypeManager.CSharpName (MemberType) + "' is less " +
5164 "accessible than indexer `" + GetSignatureForError () + "'");
5165 else if (this is MethodCore) {
5166 if (this is Operator)
5167 Report.Error (56, Location,
5168 "Inconsistent accessibility: return type `" +
5169 TypeManager.CSharpName (MemberType) + "' is less " +
5170 "accessible than operator `" + GetSignatureForError () + "'");
5172 Report.Error (50, Location,
5173 "Inconsistent accessibility: return type `" +
5174 TypeManager.CSharpName (MemberType) + "' is less " +
5175 "accessible than method `" + GetSignatureForError () + "'");
5177 Report.Error (52, Location,
5178 "Inconsistent accessibility: field type `" +
5179 TypeManager.CSharpName (MemberType) + "' is less " +
5180 "accessible than field `" + GetSignatureForError () + "'");
5185 if (IsExplicitImpl) {
5186 Expression expr = MemberName.Left.GetTypeExpression ();
5187 TypeExpr texpr = expr.ResolveAsTypeTerminal (this, false);
5191 InterfaceType = texpr.Type;
5193 if (!InterfaceType.IsInterface) {
5194 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
5198 if (!ParentContainer.VerifyImplements (this))
5201 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5207 protected bool IsTypePermitted ()
5209 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5210 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5216 protected override bool VerifyClsCompliance()
5218 if (base.VerifyClsCompliance ()) {
5222 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
5223 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
5231 // Fields and Events both generate FieldBuilders, we use this to share
5232 // their common bits. This is also used to flag usage of the field
5234 abstract public class FieldBase : MemberBase {
5235 public FieldBuilder FieldBuilder;
5236 public Status status;
5237 protected Expression initializer;
5240 public enum Status : byte {
5241 HAS_OFFSET = 4 // Used by FieldMember.
5244 static string[] attribute_targets = new string [] { "field" };
5247 /// Symbol with same name in base class/struct
5249 public MemberInfo conflict_symbol;
5251 protected FieldBase (TypeContainer parent, Expression type, int mod,
5252 int allowed_mod, MemberName name, Attributes attrs)
5253 : base (parent, null, type, mod, allowed_mod, Modifiers.PRIVATE,
5258 public override AttributeTargets AttributeTargets {
5260 return AttributeTargets.Field;
5264 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5266 if (a.Type == TypeManager.marshal_as_attr_type) {
5267 UnmanagedMarshal marshal = a.GetMarshal (this);
5268 if (marshal != null) {
5269 FieldBuilder.SetMarshal (marshal);
5274 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5275 a.Error_InvalidSecurityParent ();
5279 FieldBuilder.SetCustomAttribute (cb);
5282 public void EmitInitializer (EmitContext ec)
5284 // Replace DeclSpace because of partial classes
5285 ec.DeclContainer = DeclContainer;
5287 ec.IsFieldInitializer = true;
5288 initializer = initializer.Resolve (ec);
5289 ec.IsFieldInitializer = false;
5290 if (initializer == null)
5292 FieldExpr fe = new FieldExpr (FieldBuilder, Location, true);
5293 if ((ModFlags & Modifiers.STATIC) == 0)
5294 fe.InstanceExpression = new This (Location).Resolve (ec);
5296 ExpressionStatement a = new Assign (fe, initializer, Location);
5298 a = a.ResolveStatement (ec);
5302 Constant c = initializer as Constant;
5303 if (c != null && CanElideInitializer (c))
5306 a.EmitStatement (ec);
5309 bool CanElideInitializer (Constant c)
5311 if (MemberType == c.Type)
5312 return c.IsDefaultValue;
5314 if (c.Type == TypeManager.null_type)
5320 protected override bool CheckBase ()
5322 if (!base.CheckBase ())
5329 conflict_symbol = Parent.FindBaseMemberWithSameName (Name, false);
5330 if (conflict_symbol == null) {
5331 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5332 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5337 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5338 Report.SymbolRelatedToPreviousError (conflict_symbol);
5339 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5340 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5346 public Expression Initializer {
5348 if (value != null) {
5349 this.initializer = value;
5350 ParentContainer.RegisterFieldForInitialization (this);
5355 protected virtual bool IsFieldClsCompliant {
5357 if (FieldBuilder == null)
5360 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5364 public override string[] ValidAttributeTargets {
5366 return attribute_targets;
5370 protected override bool VerifyClsCompliance ()
5372 if (!base.VerifyClsCompliance ())
5375 if (!IsFieldClsCompliant) {
5376 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5382 public void SetAssigned ()
5384 caching_flags |= Flags.IsAssigned;
5388 public abstract class FieldMember : FieldBase
5390 protected FieldMember (TypeContainer parent, Expression type, int mod,
5391 int allowed_mod, MemberName name, Attributes attrs)
5392 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, name, attrs)
5394 if ((mod & Modifiers.ABSTRACT) != 0)
5395 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5398 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5400 if (a.Type == TypeManager.field_offset_attribute_type)
5402 status |= Status.HAS_OFFSET;
5404 if (!Parent.HasExplicitLayout) {
5405 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5409 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5410 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5415 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5416 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5420 base.ApplyAttributeBuilder (a, cb);
5423 public override bool Define()
5425 if (MemberType == null || Type == null)
5428 if (MemberType == TypeManager.void_type) {
5429 Report.Error (1547, Location, "Keyword 'void' cannot be used in this context");
5436 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5437 Report.Error (52, Location,
5438 "Inconsistent accessibility: field type `" +
5439 TypeManager.CSharpName (MemberType) + "' is less " +
5440 "accessible than field `" + GetSignatureForError () + "'");
5444 if (!IsTypePermitted ())
5450 public override void Emit ()
5452 if (OptAttributes != null) {
5453 OptAttributes.Emit ();
5456 if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
5457 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5464 // Represents header string for documentation comment.
5466 public override string DocCommentHeader {
5467 get { return "F:"; }
5471 interface IFixedBuffer
5473 FieldInfo Element { get; }
5474 Type ElementType { get; }
5477 public class FixedFieldExternal: IFixedBuffer
5479 FieldInfo element_field;
5481 public FixedFieldExternal (FieldInfo fi)
5483 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5486 #region IFixedField Members
5488 public FieldInfo Element {
5490 return element_field;
5494 public Type ElementType {
5496 return element_field.FieldType;
5504 /// Fixed buffer implementation
5506 public class FixedField : FieldMember, IFixedBuffer
5508 public const string FixedElementName = "FixedElementField";
5509 static int GlobalCounter = 0;
5510 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5511 static FieldInfo[] fi;
5513 TypeBuilder fixed_buffer_type;
5514 FieldBuilder element;
5515 Expression size_expr;
5518 const int AllowedModifiers =
5521 Modifiers.PROTECTED |
5522 Modifiers.INTERNAL |
5525 public FixedField (TypeContainer parent, Expression type, int mod, string name,
5526 Expression size_expr, Attributes attrs, Location loc):
5527 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5529 if (RootContext.Version == LanguageVersion.ISO_1)
5530 Report.FeatureIsNotStandardized (loc, "fixed size buffers");
5532 this.size_expr = size_expr;
5535 public override bool Define()
5538 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5539 Report.Warning (-23, 1, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5542 if (Parent.Kind != Kind.Struct) {
5543 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5544 GetSignatureForError ());
5548 if (!base.Define ())
5551 if (!TypeManager.IsPrimitiveType (MemberType)) {
5552 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",
5553 GetSignatureForError ());
5557 EmitContext ec = new EmitContext (this, Parent, Location, null, null, ModFlags);
5558 Constant c = size_expr.ResolveAsConstant (ec, this);
5562 IntConstant buffer_size_const = c.ToInt (Location);
5563 if (buffer_size_const == null)
5566 buffer_size = buffer_size_const.Value;
5568 if (buffer_size <= 0) {
5569 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5573 int type_size = Expression.GetTypeSize (MemberType);
5575 if (buffer_size > int.MaxValue / type_size) {
5576 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5577 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5581 buffer_size *= type_size;
5584 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5586 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5587 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5588 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5589 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5591 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5592 TypeManager.RegisterFieldBase (FieldBuilder, this);
5597 public override void Emit()
5600 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5602 object[] fi_val = new object[1];
5603 fi_val [0] = buffer_size;
5605 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5606 ctor_args, fi, fi_val);
5607 fixed_buffer_type.SetCustomAttribute (cab);
5609 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5610 FieldBuilder.SetCustomAttribute (cab);
5614 protected override bool IsFieldClsCompliant {
5620 #region IFixedField Members
5622 public FieldInfo Element {
5628 public Type ElementType {
5638 // The Field class is used to represents class/struct fields during parsing.
5640 public class Field : FieldMember {
5642 // Modifiers allowed in a class declaration
5644 const int AllowedModifiers =
5647 Modifiers.PROTECTED |
5648 Modifiers.INTERNAL |
5651 Modifiers.VOLATILE |
5655 public Field (TypeContainer parent, Expression type, int mod, string name,
5656 Attributes attrs, Location loc)
5657 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5662 public override bool Define ()
5664 if (!base.Define ())
5667 if (RootContext.WarningLevel > 1){
5668 Type ptype = Parent.TypeBuilder.BaseType;
5670 // ptype is only null for System.Object while compiling corlib.
5672 TypeContainer.FindMembers (
5673 ptype, MemberTypes.Method,
5674 BindingFlags.Public |
5675 BindingFlags.Static | BindingFlags.Instance,
5676 System.Type.FilterName, Name);
5680 if ((ModFlags & Modifiers.VOLATILE) != 0){
5681 if (!MemberType.IsClass){
5682 Type vt = MemberType;
5684 if (TypeManager.IsEnumType (vt))
5685 vt = TypeManager.EnumToUnderlying (MemberType);
5687 if (!((vt == TypeManager.bool_type) ||
5688 (vt == TypeManager.sbyte_type) ||
5689 (vt == TypeManager.byte_type) ||
5690 (vt == TypeManager.short_type) ||
5691 (vt == TypeManager.ushort_type) ||
5692 (vt == TypeManager.int32_type) ||
5693 (vt == TypeManager.uint32_type) ||
5694 (vt == TypeManager.char_type) ||
5695 (vt == TypeManager.float_type) ||
5696 (!vt.IsValueType))){
5697 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5698 GetSignatureForError (), TypeManager.CSharpName (vt));
5703 if ((ModFlags & Modifiers.READONLY) != 0){
5704 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5705 GetSignatureForError ());
5710 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5712 if (Parent.Kind == Kind.Struct &&
5713 ((fa & FieldAttributes.Static) == 0) &&
5714 MemberType == Parent.TypeBuilder &&
5715 !TypeManager.IsBuiltinType (MemberType)){
5716 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5717 "' causes a cycle in the structure layout");
5722 FieldBuilder = Parent.TypeBuilder.DefineField (
5723 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5725 TypeManager.RegisterFieldBase (FieldBuilder, this);
5727 catch (ArgumentException) {
5728 Report.Warning (-24, 1, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5735 protected override bool VerifyClsCompliance ()
5737 if (!base.VerifyClsCompliance ())
5740 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5741 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5749 // `set' and `get' accessors are represented with an Accessor.
5751 public class Accessor : IIteratorContainer {
5753 // Null if the accessor is empty, or a Block if not
5755 public const int AllowedModifiers =
5757 Modifiers.PROTECTED |
5758 Modifiers.INTERNAL |
5761 public ToplevelBlock Block;
5762 public Attributes Attributes;
5763 public Location Location;
5764 public int ModFlags;
5767 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5772 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5775 public void SetYields ()
5781 // Ooouh Martin, templates are missing here.
5782 // When it will be possible move here a lot of child code and template method type.
5783 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5784 protected MethodData method_data;
5785 protected ToplevelBlock block;
5786 protected ListDictionary declarative_security;
5788 // The accessor are created event if they are not wanted.
5789 // But we need them because their names are reserved.
5790 // Field says whether accessor will be emited or not
5791 public readonly bool IsDummy;
5793 protected readonly string prefix;
5795 ReturnParameter return_attributes;
5797 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5798 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5800 this.prefix = prefix;
5804 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5806 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5807 accessor.Attributes)
5809 this.prefix = prefix;
5810 this.block = accessor.Block;
5813 static MemberName SetupName (string prefix, MemberBase member, Location loc)
5815 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5818 public void UpdateName (MemberBase member)
5820 SetMemberName (SetupName (prefix, member, Location));
5823 #region IMethodData Members
5825 public ToplevelBlock Block {
5835 public CallingConventions CallingConventions {
5837 return CallingConventions.Standard;
5841 public bool IsExcluded ()
5846 GenericMethod IMethodData.GenericMethod {
5852 public MemberName MethodName {
5858 public Type[] ParameterTypes {
5860 return ParameterInfo.Types;
5864 public abstract Parameters ParameterInfo { get ; }
5865 public abstract Type ReturnType { get; }
5866 public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
5870 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5872 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5873 a.Type == TypeManager.conditional_attribute_type) {
5874 Report.Error (1667, a.Location,
5875 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
5876 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5880 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5881 if (declarative_security == null)
5882 declarative_security = new ListDictionary ();
5883 a.ExtractSecurityPermissionSet (declarative_security);
5887 if (a.Target == AttributeTargets.Method) {
5888 method_data.MethodBuilder.SetCustomAttribute (cb);
5892 if (a.Target == AttributeTargets.ReturnValue) {
5893 if (return_attributes == null)
5894 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5896 return_attributes.ApplyAttributeBuilder (a, cb);
5900 ApplyToExtraTarget (a, cb);
5903 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5905 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5908 public override bool Define()
5910 throw new NotSupportedException ();
5913 public virtual void Emit (TypeContainer container)
5915 EmitMethod (container);
5917 if (declarative_security != null) {
5918 foreach (DictionaryEntry de in declarative_security) {
5919 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5926 protected virtual void EmitMethod (TypeContainer container)
5928 method_data.Emit (container);
5931 public override bool IsClsComplianceRequired()
5936 public bool IsDuplicateImplementation (MethodCore method)
5938 if (!MemberName.Equals (method.MemberName))
5941 Type[] param_types = method.ParameterTypes;
5943 if (param_types.Length != ParameterTypes.Length)
5946 for (int i = 0; i < param_types.Length; i++)
5947 if (param_types [i] != ParameterTypes [i])
5950 Report.SymbolRelatedToPreviousError (method);
5951 Report.Error (111, Location, TypeContainer.Error111, method.GetSignatureForError ());
5955 public override bool IsUsed
5965 public new Location Location {
5967 return base.Location;
5972 // Represents header string for documentation comment.
5974 public override string DocCommentHeader {
5975 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5981 // Properties and Indexers both generate PropertyBuilders, we use this to share
5982 // their common bits.
5984 abstract public class PropertyBase : MethodCore {
5986 public class GetMethod : PropertyMethod
5988 static string[] attribute_targets = new string [] { "method", "return" };
5990 public GetMethod (MethodCore method):
5991 base (method, "get_")
5995 public GetMethod (MethodCore method, Accessor accessor):
5996 base (method, accessor, "get_")
6000 public override MethodBuilder Define(TypeContainer container)
6002 base.Define (container);
6004 method_data = new MethodData (method, ModFlags, flags, this);
6006 if (!method_data.Define (container))
6009 return method_data.MethodBuilder;
6012 public override Type ReturnType {
6014 return method.MemberType;
6018 public override Parameters ParameterInfo {
6020 return Parameters.EmptyReadOnlyParameters;
6024 public override string[] ValidAttributeTargets {
6026 return attribute_targets;
6031 public class SetMethod : PropertyMethod {
6033 static string[] attribute_targets = new string [] { "method", "param", "return" };
6034 ImplicitParameter param_attr;
6035 protected Parameters parameters;
6037 public SetMethod (MethodCore method):
6038 base (method, "set_")
6042 public SetMethod (MethodCore method, Accessor accessor):
6043 base (method, accessor, "set_")
6047 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6049 if (a.Target == AttributeTargets.Parameter) {
6050 if (param_attr == null)
6051 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6053 param_attr.ApplyAttributeBuilder (a, cb);
6057 base.ApplyAttributeBuilder (a, cb);
6060 public override Parameters ParameterInfo {
6066 protected virtual void DefineParameters ()
6068 parameters = new Parameters (
6069 new Parameter[] { new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location) },
6070 new Type[] { method.MemberType });
6073 public override MethodBuilder Define (TypeContainer container)
6075 DefineParameters ();
6079 base.Define (container);
6081 method_data = new MethodData (method, ModFlags, flags, this);
6083 if (!method_data.Define (container))
6086 return method_data.MethodBuilder;
6089 public override Type ReturnType {
6091 return TypeManager.void_type;
6095 public override string[] ValidAttributeTargets {
6097 return attribute_targets;
6102 static string[] attribute_targets = new string [] { "property" };
6104 public abstract class PropertyMethod : AbstractPropertyEventMethod
6106 protected readonly MethodCore method;
6107 protected MethodAttributes flags;
6110 public PropertyMethod (MethodCore method, string prefix)
6111 : base (method, prefix)
6113 this.method = method;
6116 public PropertyMethod (MethodCore method, Accessor accessor,
6118 : base (method, accessor, prefix)
6120 this.method = method;
6121 this.ModFlags = accessor.ModFlags;
6122 yields = accessor.Yields;
6124 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6125 Report.FeatureIsNotStandardized (Location, "access modifiers on properties");
6129 public override AttributeTargets AttributeTargets {
6131 return AttributeTargets.Method;
6135 public override bool IsClsComplianceRequired ()
6137 return method.IsClsComplianceRequired ();
6140 public virtual MethodBuilder Define (TypeContainer container)
6142 if (!method.CheckAbstractAndExtern (block != null))
6146 // Check for custom access modifier
6148 if (ModFlags == 0) {
6149 ModFlags = method.ModFlags;
6150 flags = method.flags;
6152 if (container.Kind == Kind.Interface)
6153 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6154 GetSignatureForError ());
6156 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6157 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6160 CheckModifiers (container, ModFlags);
6161 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6162 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6163 flags = Modifiers.MethodAttr (ModFlags);
6164 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6168 // Setup iterator if we are one
6171 Iterator iterator = new Iterator (this, Parent as TypeContainer, null, ModFlags);
6173 if (!iterator.DefineIterator ())
6180 public bool HasCustomAccessModifier
6183 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6187 public override EmitContext CreateEmitContext (TypeContainer tc,
6190 return new EmitContext (method,
6191 tc, method.ds, method.Location, ig, ReturnType,
6192 method.ModFlags, false);
6195 public override ObsoleteAttribute GetObsoleteAttribute ()
6197 return method.GetObsoleteAttribute ();
6200 public override string GetSignatureForError()
6202 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6205 void CheckModifiers (TypeContainer container, int modflags)
6208 int mflags = method.ModFlags & Modifiers.Accessibility;
6210 if ((mflags & Modifiers.PUBLIC) != 0) {
6211 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6213 else if ((mflags & Modifiers.PROTECTED) != 0) {
6214 if ((mflags & Modifiers.INTERNAL) != 0)
6215 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6217 flags |= Modifiers.PRIVATE;
6219 else if ((mflags & Modifiers.INTERNAL) != 0)
6220 flags |= Modifiers.PRIVATE;
6222 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6223 Report.Error (273, Location,
6224 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6225 GetSignatureForError (), method.GetSignatureForError ());
6229 public override bool MarkForDuplicationCheck ()
6231 caching_flags |= Flags.TestMethodDuplication;
6236 public PropertyMethod Get, Set;
6237 public PropertyBuilder PropertyBuilder;
6238 public MethodBuilder GetBuilder, SetBuilder;
6240 protected EmitContext ec;
6242 public PropertyBase (TypeContainer parent, Expression type, int mod_flags,
6243 int allowed_mod, bool is_iface, MemberName name,
6244 Parameters parameters, Attributes attrs)
6245 : base (parent, null, type, mod_flags, allowed_mod, is_iface, name,
6250 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6252 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6253 a.Error_InvalidSecurityParent ();
6257 PropertyBuilder.SetCustomAttribute (cb);
6260 public override AttributeTargets AttributeTargets {
6262 return AttributeTargets.Property;
6266 public override bool Define ()
6271 if (!IsTypePermitted ())
6277 protected override bool DoDefine ()
6279 if (!base.DoDefine ())
6283 // Accessors modifiers check
6285 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6286 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6287 GetSignatureForError ());
6291 if ((Get.IsDummy || Set.IsDummy)
6292 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6293 Report.Error (276, Location,
6294 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6295 GetSignatureForError ());
6299 if (MemberType.IsAbstract && MemberType.IsSealed) {
6300 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6304 ec = new EmitContext (this, Parent, Location, null, MemberType, ModFlags);
6308 protected override bool CheckForDuplications ()
6310 ArrayList ar = Parent.Indexers;
6312 int arLen = ar.Count;
6314 for (int i = 0; i < arLen; i++) {
6315 Indexer m = (Indexer) ar [i];
6316 if (IsDuplicateImplementation (m))
6321 ar = Parent.Properties;
6323 int arLen = ar.Count;
6325 for (int i = 0; i < arLen; i++) {
6326 Property m = (Property) ar [i];
6327 if (IsDuplicateImplementation (m))
6335 // TODO: rename to Resolve......
6336 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6338 PropertyInfo base_property = Parent.BaseCache.FindMemberToOverride (
6339 Parent.TypeBuilder, Name, ParameterTypes, null, true) as PropertyInfo;
6341 if (base_property == null)
6344 base_ret_type = base_property.PropertyType;
6345 MethodInfo get_accessor = base_property.GetGetMethod (true);
6346 MethodInfo set_accessor = base_property.GetSetMethod (true);
6347 MethodAttributes get_accessor_access, set_accessor_access;
6349 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6350 if (Get != null && !Get.IsDummy && get_accessor == null) {
6351 Report.SymbolRelatedToPreviousError (base_property);
6352 Report.Error (545, Location, "`{0}.get': cannot override because `{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6355 if (Set != null && !Set.IsDummy && set_accessor == null) {
6356 Report.SymbolRelatedToPreviousError (base_property);
6357 Report.Error (546, Location, "`{0}.set': cannot override because `{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6362 // Check base class accessors access
6365 // TODO: rewrite to reuse Get|Set.CheckAccessModifiers and share code there
6366 get_accessor_access = set_accessor_access = 0;
6367 if ((ModFlags & Modifiers.NEW) == 0) {
6368 if (get_accessor != null) {
6369 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6370 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6372 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6373 Error_CannotChangeAccessModifiers (get_accessor, get_accessor_access, ".get");
6376 if (set_accessor != null) {
6377 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6378 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6380 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6381 Error_CannotChangeAccessModifiers (set_accessor, set_accessor_access, ".set");
6386 // Get the less restrictive access
6388 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6391 public override void Emit ()
6394 // The PropertyBuilder can be null for explicit implementations, in that
6395 // case, we do not actually emit the ".property", so there is nowhere to
6396 // put the attribute
6398 if (PropertyBuilder != null && OptAttributes != null)
6399 OptAttributes.Emit ();
6411 /// Tests whether accessors are not in collision with some method (CS0111)
6413 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6415 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6418 public override bool IsUsed
6424 return Get.IsUsed | Set.IsUsed;
6428 protected override void SetMemberName (MemberName new_name)
6430 base.SetMemberName (new_name);
6432 Get.UpdateName (this);
6433 Set.UpdateName (this);
6436 public override string[] ValidAttributeTargets {
6438 return attribute_targets;
6443 // Represents header string for documentation comment.
6445 public override string DocCommentHeader {
6446 get { return "P:"; }
6450 public class Property : PropertyBase {
6451 const int AllowedModifiers =
6454 Modifiers.PROTECTED |
6455 Modifiers.INTERNAL |
6459 Modifiers.OVERRIDE |
6460 Modifiers.ABSTRACT |
6463 Modifiers.METHOD_YIELDS |
6466 const int AllowedInterfaceModifiers =
6469 public Property (TypeContainer ds, Expression type, int mod, bool is_iface,
6470 MemberName name, Attributes attrs, Accessor get_block,
6472 : base (ds, type, mod,
6473 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6474 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs)
6476 if (get_block == null)
6477 Get = new GetMethod (this);
6479 Get = new GetMethod (this, get_block);
6481 if (set_block == null)
6482 Set = new SetMethod (this);
6484 Set = new SetMethod (this, set_block);
6487 public override bool Define ()
6489 if (!DoDefineBase ())
6492 if (!base.Define ())
6498 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6501 GetBuilder = Get.Define (Parent);
6502 if (GetBuilder == null)
6506 SetBuilder = Set.Define (Parent);
6508 if (SetBuilder == null)
6512 // FIXME - PropertyAttributes.HasDefault ?
6514 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6515 MemberName.ToString (), PropertyAttributes.None, MemberType, null);
6518 PropertyBuilder.SetGetMethod (GetBuilder);
6521 PropertyBuilder.SetSetMethod (SetBuilder);
6523 TypeManager.RegisterProperty (PropertyBuilder, this);
6529 /// Gigantic workaround for lameness in SRE follows :
6530 /// This class derives from EventInfo and attempts to basically
6531 /// wrap around the EventBuilder so that FindMembers can quickly
6532 /// return this in it search for members
6534 public class MyEventBuilder : EventInfo {
6537 // We use this to "point" to our Builder which is
6538 // not really a MemberInfo
6540 EventBuilder MyBuilder;
6543 // We "catch" and wrap these methods
6545 MethodInfo raise, remove, add;
6547 EventAttributes attributes;
6548 Type declaring_type, reflected_type, event_type;
6553 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6555 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6557 // And now store the values in our own fields.
6559 declaring_type = type_builder;
6561 reflected_type = type_builder;
6563 attributes = event_attr;
6566 this.event_type = event_type;
6570 // Methods that you have to override. Note that you only need
6571 // to "implement" the variants that take the argument (those are
6572 // the "abstract" methods, the others (GetAddMethod()) are
6575 public override MethodInfo GetAddMethod (bool nonPublic)
6580 public override MethodInfo GetRemoveMethod (bool nonPublic)
6585 public override MethodInfo GetRaiseMethod (bool nonPublic)
6591 // These methods make "MyEventInfo" look like a Builder
6593 public void SetRaiseMethod (MethodBuilder raiseMethod)
6595 raise = raiseMethod;
6596 MyBuilder.SetRaiseMethod (raiseMethod);
6599 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6601 remove = removeMethod;
6602 MyBuilder.SetRemoveOnMethod (removeMethod);
6605 public void SetAddOnMethod (MethodBuilder addMethod)
6608 MyBuilder.SetAddOnMethod (addMethod);
6611 public void SetCustomAttribute (CustomAttributeBuilder cb)
6613 MyBuilder.SetCustomAttribute (cb);
6616 public override object [] GetCustomAttributes (bool inherit)
6618 // FIXME : There's nothing which can be seemingly done here because
6619 // we have no way of getting at the custom attribute objects of the
6624 public override object [] GetCustomAttributes (Type t, bool inherit)
6626 // FIXME : Same here !
6630 public override bool IsDefined (Type t, bool b)
6635 public override EventAttributes Attributes {
6641 public override string Name {
6647 public override Type DeclaringType {
6649 return declaring_type;
6653 public override Type ReflectedType {
6655 return reflected_type;
6659 public Type EventType {
6665 public void SetUsed ()
6667 if (my_event != null) {
6668 my_event.SetAssigned ();
6669 my_event.SetMemberIsUsed ();
6675 /// For case when event is declared like property (with add and remove accessors).
6677 public class EventProperty: Event {
6679 static string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6681 public EventProperty (TypeContainer parent, Expression type, int mod_flags,
6682 bool is_iface, MemberName name,
6683 Attributes attrs, Accessor add, Accessor remove)
6684 : base (parent, type, mod_flags, is_iface, name, attrs)
6686 Add = new AddDelegateMethod (this, add);
6687 Remove = new RemoveDelegateMethod (this, remove);
6689 // For this event syntax we don't report error CS0067
6690 // because it is hard to do it.
6694 public override string[] ValidAttributeTargets {
6696 return attribute_targets;
6702 /// Event is declared like field.
6704 public class EventField : Event {
6706 static string[] attribute_targets = new string [] { "event", "field", "method" };
6707 static string[] attribute_targets_interface = new string[] { "event", "method" };
6709 public EventField (TypeContainer parent, Expression type, int mod_flags,
6710 bool is_iface, MemberName name,
6712 : base (parent, type, mod_flags, is_iface, name, attrs)
6714 Add = new AddDelegateMethod (this);
6715 Remove = new RemoveDelegateMethod (this);
6718 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6720 if (a.Target == AttributeTargets.Field) {
6721 FieldBuilder.SetCustomAttribute (cb);
6725 if (a.Target == AttributeTargets.Method) {
6726 Add.ApplyAttributeBuilder (a, cb);
6727 Remove.ApplyAttributeBuilder (a, cb);
6731 base.ApplyAttributeBuilder (a, cb);
6734 public override bool Define()
6736 if (!base.Define ())
6739 if (initializer != null) {
6740 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
6741 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
6742 GetSignatureForError ());
6750 public override string[] ValidAttributeTargets {
6752 return IsInterface ? attribute_targets_interface : attribute_targets;
6757 public abstract class Event : FieldBase {
6759 protected sealed class AddDelegateMethod: DelegateMethod
6762 public AddDelegateMethod (Event method):
6763 base (method, "add_")
6767 public AddDelegateMethod (Event method, Accessor accessor):
6768 base (method, accessor, "add_")
6772 protected override MethodInfo DelegateMethodInfo {
6774 return TypeManager.delegate_combine_delegate_delegate;
6780 protected sealed class RemoveDelegateMethod: DelegateMethod
6782 public RemoveDelegateMethod (Event method):
6783 base (method, "remove_")
6787 public RemoveDelegateMethod (Event method, Accessor accessor):
6788 base (method, accessor, "remove_")
6792 protected override MethodInfo DelegateMethodInfo {
6794 return TypeManager.delegate_remove_delegate_delegate;
6800 public abstract class DelegateMethod: AbstractPropertyEventMethod
6802 protected readonly Event method;
6803 ImplicitParameter param_attr;
6805 static string[] attribute_targets = new string [] { "method", "param", "return" };
6807 public DelegateMethod (Event method, string prefix)
6808 : base (method, prefix)
6810 this.method = method;
6813 public DelegateMethod (Event method, Accessor accessor, string prefix)
6814 : base (method, accessor, prefix)
6816 this.method = method;
6819 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6821 if (a.Target == AttributeTargets.Parameter) {
6822 if (param_attr == null)
6823 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6825 param_attr.ApplyAttributeBuilder (a, cb);
6829 base.ApplyAttributeBuilder (a, cb);
6832 public override AttributeTargets AttributeTargets {
6834 return AttributeTargets.Method;
6838 public override bool IsClsComplianceRequired ()
6840 return method.IsClsComplianceRequired ();
6843 public MethodBuilder Define (TypeContainer container)
6845 method_data = new MethodData (method, method.ModFlags,
6846 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6848 if (!method_data.Define (container))
6851 MethodBuilder mb = method_data.MethodBuilder;
6852 ParameterInfo.ApplyAttributes (mb);
6857 protected override void EmitMethod (TypeContainer tc)
6859 if (block != null) {
6860 base.EmitMethod (tc);
6864 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
6867 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6868 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6870 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6871 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6872 ig.Emit (OpCodes.Ldsfld, field_info);
6873 ig.Emit (OpCodes.Ldarg_0);
6874 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6875 ig.Emit (OpCodes.Castclass, method.MemberType);
6876 ig.Emit (OpCodes.Stsfld, field_info);
6878 ig.Emit (OpCodes.Ldarg_0);
6879 ig.Emit (OpCodes.Ldarg_0);
6880 ig.Emit (OpCodes.Ldfld, field_info);
6881 ig.Emit (OpCodes.Ldarg_1);
6882 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6883 ig.Emit (OpCodes.Castclass, method.MemberType);
6884 ig.Emit (OpCodes.Stfld, field_info);
6886 ig.Emit (OpCodes.Ret);
6889 protected abstract MethodInfo DelegateMethodInfo { get; }
6891 public override Type ReturnType {
6893 return TypeManager.void_type;
6897 public override EmitContext CreateEmitContext (TypeContainer tc,
6900 return new EmitContext (
6901 tc, method.Parent, Location, ig, ReturnType,
6902 method.ModFlags, false);
6905 public override ObsoleteAttribute GetObsoleteAttribute ()
6907 return method.GetObsoleteAttribute ();
6910 public override string[] ValidAttributeTargets {
6912 return attribute_targets;
6916 public override Parameters ParameterInfo {
6918 return method.parameters;
6925 const int AllowedModifiers =
6928 Modifiers.PROTECTED |
6929 Modifiers.INTERNAL |
6934 Modifiers.OVERRIDE |
6938 const int AllowedInterfaceModifiers =
6941 public DelegateMethod Add, Remove;
6942 public MyEventBuilder EventBuilder;
6943 public MethodBuilder AddBuilder, RemoveBuilder;
6944 Parameters parameters;
6946 protected Event (TypeContainer parent, Expression type, int mod_flags,
6947 bool is_iface, MemberName name, Attributes attrs)
6948 : base (parent, type, mod_flags,
6949 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6952 IsInterface = is_iface;
6955 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6957 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6958 a.Error_InvalidSecurityParent ();
6962 EventBuilder.SetCustomAttribute (cb);
6965 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6967 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6970 public override AttributeTargets AttributeTargets {
6972 return AttributeTargets.Event;
6976 public override bool Define ()
6978 EventAttributes e_attr;
6979 e_attr = EventAttributes.None;
6981 if (!DoDefineBase ())
6987 if (!TypeManager.IsDelegateType (MemberType)) {
6988 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
6992 parameters = new Parameters (
6993 new Parameter[] { new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location) },
6994 new Type[] { MemberType } );
7000 // Now define the accessors
7003 AddBuilder = Add.Define (Parent);
7004 if (AddBuilder == null)
7007 RemoveBuilder = Remove.Define (Parent);
7008 if (RemoveBuilder == null)
7011 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
7013 if (Add.Block == null && Remove.Block == null && !IsInterface) {
7014 FieldBuilder = Parent.TypeBuilder.DefineField (
7016 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7017 TypeManager.RegisterPrivateFieldOfEvent (
7018 (EventInfo) EventBuilder, FieldBuilder);
7019 TypeManager.RegisterFieldBase (FieldBuilder, this);
7022 EventBuilder.SetAddOnMethod (AddBuilder);
7023 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7025 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
7029 protected override bool CheckBase ()
7031 if (!base.CheckBase ())
7034 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
7035 if (!(conflict_symbol is EventInfo)) {
7036 Report.SymbolRelatedToPreviousError (conflict_symbol);
7037 Report.Error (72, Location, "Event `{0}' can override only event", GetSignatureForError ());
7045 public override void Emit ()
7047 if (OptAttributes != null) {
7048 OptAttributes.Emit ();
7052 Remove.Emit (Parent);
7057 public override string GetSignatureForError ()
7059 return base.GetSignatureForError ();
7063 // Represents header string for documentation comment.
7065 public override string DocCommentHeader {
7066 get { return "E:"; }
7071 public class Indexer : PropertyBase, IIteratorContainer {
7073 class GetIndexerMethod : GetMethod
7075 public GetIndexerMethod (MethodCore method):
7080 public GetIndexerMethod (MethodCore method, Accessor accessor):
7081 base (method, accessor)
7085 public override Parameters ParameterInfo {
7087 return method.ParameterInfo;
7092 class SetIndexerMethod: SetMethod
7094 public SetIndexerMethod (MethodCore method):
7099 public SetIndexerMethod (MethodCore method, Accessor accessor):
7100 base (method, accessor)
7104 protected override void DefineParameters ()
7106 parameters = Parameters.MergeGenerated (method.Parameters,
7107 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
7111 const int AllowedModifiers =
7114 Modifiers.PROTECTED |
7115 Modifiers.INTERNAL |
7119 Modifiers.OVERRIDE |
7124 const int AllowedInterfaceModifiers =
7128 // Are we implementing an interface ?
7130 public Indexer (TypeContainer parent, Expression type, MemberName name, int mod,
7131 bool is_iface, Parameters parameters, Attributes attrs,
7132 Accessor get_block, Accessor set_block)
7133 : base (parent, type, mod,
7134 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7135 is_iface, name, parameters, attrs)
7137 if (get_block == null)
7138 Get = new GetIndexerMethod (this);
7140 Get = new GetIndexerMethod (this, get_block);
7142 if (set_block == null)
7143 Set = new SetIndexerMethod (this);
7145 Set = new SetIndexerMethod (this, set_block);
7148 public override bool Define ()
7150 if (!DoDefineBase ())
7153 if (!base.Define ())
7156 if (MemberType == TypeManager.void_type) {
7157 Report.Error (620, Location, "Indexers cannot have void type");
7161 if (OptAttributes != null) {
7162 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
7163 if (indexer_attr != null) {
7164 // Remove the attribute from the list because it is not emitted
7165 OptAttributes.Attrs.Remove (indexer_attr);
7167 ShortName = indexer_attr.GetIndexerAttributeValue ();
7169 if (IsExplicitImpl) {
7170 Report.Error (415, indexer_attr.Location,
7171 "The `IndexerName' attribute is valid only on an " +
7172 "indexer that is not an explicit interface member declaration");
7176 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7177 Report.Error (609, indexer_attr.Location,
7178 "Cannot set the `IndexerName' attribute on an indexer marked override");
7182 if (!Tokenizer.IsValidIdentifier (ShortName)) {
7183 Report.Error (633, indexer_attr.Location,
7184 "The argument to the `IndexerName' attribute must be a valid identifier");
7190 if (InterfaceType != null) {
7191 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7192 if (base_IndexerName != Name)
7193 ShortName = base_IndexerName;
7196 if (!Parent.AddToMemberContainer (this) ||
7197 !Parent.AddToMemberContainer (Get) || !Parent.AddToMemberContainer (Set))
7203 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7205 GetBuilder = Get.Define (Parent);
7206 if (GetBuilder == null)
7210 // Setup iterator if we are one
7212 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
7213 Iterator iterator = new Iterator (
7214 Get, Parent, null, ModFlags);
7216 if (!iterator.DefineIterator ())
7221 SetBuilder = Set.Define (Parent);
7223 if (SetBuilder == null)
7228 // Now name the parameters
7230 Parameter [] p = Parameters.FixedParameters;
7232 // TODO: should be done in parser and it needs to do cycle
7233 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
7234 Report.Error (631, Location, "ref and out are not valid in this context");
7239 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7240 Name, PropertyAttributes.None, MemberType, ParameterTypes);
7243 PropertyBuilder.SetGetMethod (GetBuilder);
7246 PropertyBuilder.SetSetMethod (SetBuilder);
7248 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
7253 public override string GetSignatureForError ()
7255 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7256 if (MemberName.Left != null) {
7258 sb.Append (MemberName.Left);
7261 sb.Append (".this");
7262 sb.Append (Parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7263 return sb.ToString ();
7266 public override bool MarkForDuplicationCheck ()
7268 caching_flags |= Flags.TestMethodDuplication;
7273 public class Operator : MethodCore, IIteratorContainer {
7275 const int AllowedModifiers =
7281 public enum OpType : byte {
7291 // Unary and Binary operators
7314 // Implicit and Explicit
7318 // Just because of enum
7322 public readonly OpType OperatorType;
7323 public MethodBuilder OperatorMethodBuilder;
7325 public Method OperatorMethod;
7327 static string[] attribute_targets = new string [] { "method", "return" };
7329 public Operator (TypeContainer parent, OpType type, Expression ret_type,
7330 int mod_flags, Parameters parameters,
7331 ToplevelBlock block, Attributes attrs, Location loc)
7332 : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
7333 new MemberName ("op_" + type, loc), attrs, parameters)
7335 OperatorType = type;
7339 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7341 OperatorMethod.ApplyAttributeBuilder (a, cb);
7344 public override AttributeTargets AttributeTargets {
7346 return AttributeTargets.Method;
7350 protected override bool CheckForDuplications()
7352 ArrayList ar = Parent.Operators;
7354 int arLen = ar.Count;
7356 for (int i = 0; i < arLen; i++) {
7357 Operator o = (Operator) ar [i];
7358 if (IsDuplicateImplementation (o))
7363 ar = Parent.Methods;
7365 int arLen = ar.Count;
7367 for (int i = 0; i < arLen; i++) {
7368 Method m = (Method) ar [i];
7369 if (IsDuplicateImplementation (m))
7377 public override bool Define ()
7379 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7380 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7381 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7388 if (MemberType == TypeManager.void_type) {
7389 Report.Error (590, Location, "User-defined operators cannot return void");
7393 OperatorMethod = new Method (
7394 Parent, null, Type, ModFlags, false, MemberName,
7395 Parameters, OptAttributes);
7397 OperatorMethod.Block = Block;
7398 OperatorMethod.IsOperator = this;
7399 OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7400 OperatorMethod.Define ();
7402 if (OperatorMethod.MethodBuilder == null)
7405 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
7407 Type[] parameter_types = OperatorMethod.ParameterTypes;
7408 Type declaring_type = OperatorMethod.MethodData.DeclaringType;
7409 Type return_type = OperatorMethod.ReturnType;
7410 Type first_arg_type = parameter_types [0];
7415 // Rules for conversion operators
7417 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7418 if (first_arg_type == return_type && first_arg_type == declaring_type){
7419 Report.Error (555, Location,
7420 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7424 if (first_arg_type != declaring_type && return_type != declaring_type){
7427 "User-defined conversion must convert to or from the " +
7432 if (first_arg_type == TypeManager.object_type ||
7433 return_type == TypeManager.object_type){
7436 "User-defined conversion cannot convert to or from " +
7441 if (first_arg_type.IsInterface || return_type.IsInterface){
7442 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7443 GetSignatureForError ());
7447 if (first_arg_type.IsSubclassOf (return_type)
7448 || return_type.IsSubclassOf (first_arg_type)){
7449 if (declaring_type.IsSubclassOf (return_type)) {
7450 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from base class",
7451 GetSignatureForError ());
7454 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from derived class",
7455 GetSignatureForError ());
7458 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7459 if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
7460 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");
7463 } else if (Parameters.Count == 1) {
7464 // Checks for Unary operators
7466 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7467 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7468 Report.Error (448, Location,
7469 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7472 if (first_arg_type != declaring_type) {
7474 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7479 if (first_arg_type != declaring_type){
7482 "The parameter of a unary operator must be the " +
7487 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7488 if (return_type != TypeManager.bool_type){
7491 "The return type of operator True or False " +
7498 // Checks for Binary operators
7500 if (first_arg_type != declaring_type &&
7501 parameter_types [1] != declaring_type){
7504 "One of the parameters of a binary operator must " +
7505 "be the containing type");
7513 public override void Emit ()
7516 // abstract or extern methods have no bodies
7518 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7521 OperatorMethod.Emit ();
7525 // Operator cannot be override
7526 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7531 public static string GetName (OpType ot)
7534 case OpType.LogicalNot:
7536 case OpType.OnesComplement:
7538 case OpType.Increment:
7540 case OpType.Decrement:
7546 case OpType.Addition:
7548 case OpType.Subtraction:
7550 case OpType.UnaryPlus:
7552 case OpType.UnaryNegation:
7554 case OpType.Multiply:
7556 case OpType.Division:
7558 case OpType.Modulus:
7560 case OpType.BitwiseAnd:
7562 case OpType.BitwiseOr:
7564 case OpType.ExclusiveOr:
7566 case OpType.LeftShift:
7568 case OpType.RightShift:
7570 case OpType.Equality:
7572 case OpType.Inequality:
7574 case OpType.GreaterThan:
7576 case OpType.LessThan:
7578 case OpType.GreaterThanOrEqual:
7580 case OpType.LessThanOrEqual:
7582 case OpType.Implicit:
7584 case OpType.Explicit:
7590 public static OpType GetOperatorType (string name)
7592 if (name.StartsWith ("op_")){
7593 for (int i = 0; i < Unary.oper_names.Length; ++i) {
7594 if (Unary.oper_names [i] == name)
7598 for (int i = 0; i < Binary.oper_names.Length; ++i) {
7599 if (Binary.oper_names [i] == name)
7606 public override string GetSignatureForError ()
7608 StringBuilder sb = new StringBuilder ();
7609 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7610 sb.AppendFormat ("{0}.{1} operator {2}", Parent.GetSignatureForError (), GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type));
7613 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7616 sb.Append (Parameters.GetSignatureForError ());
7617 return sb.ToString ();
7620 public override bool MarkForDuplicationCheck ()
7622 caching_flags |= Flags.TestMethodDuplication;
7626 public override string[] ValidAttributeTargets {
7628 return attribute_targets;
7634 // This is used to compare method signatures
7636 struct MethodSignature {
7638 public Type RetType;
7639 public Type [] Parameters;
7642 /// This delegate is used to extract methods which have the
7643 /// same signature as the argument
7645 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7647 public MethodSignature (string name, Type ret_type, Type [] parameters)
7652 if (parameters == null)
7653 Parameters = Type.EmptyTypes;
7655 Parameters = parameters;
7658 public override string ToString ()
7661 if (Parameters.Length != 0){
7662 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7663 for (int i = 0; i < Parameters.Length; i++){
7664 sb.Append (Parameters [i]);
7665 if (i+1 < Parameters.Length)
7668 pars = sb.ToString ();
7671 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7674 public override int GetHashCode ()
7676 return Name.GetHashCode ();
7679 public override bool Equals (Object o)
7681 MethodSignature other = (MethodSignature) o;
7683 if (other.Name != Name)
7686 if (other.RetType != RetType)
7689 if (Parameters == null){
7690 if (other.Parameters == null)
7695 if (other.Parameters == null)
7698 int c = Parameters.Length;
7699 if (other.Parameters.Length != c)
7702 for (int i = 0; i < c; i++)
7703 if (other.Parameters [i] != Parameters [i])
7709 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7711 MethodSignature sig = (MethodSignature) filter_criteria;
7713 if (m.Name != sig.Name)
7717 MethodInfo mi = m as MethodInfo;
7718 PropertyInfo pi = m as PropertyInfo;
7721 ReturnType = mi.ReturnType;
7722 else if (pi != null)
7723 ReturnType = pi.PropertyType;
7728 // we use sig.RetType == null to mean `do not check the
7729 // method return value.
7731 if (sig.RetType != null)
7732 if (ReturnType != sig.RetType)
7737 args = TypeManager.GetParameterData (mi).Types;
7739 args = TypeManager.GetArgumentTypes (pi);
7740 Type [] sigp = sig.Parameters;
7742 if (args.Length != sigp.Length)
7745 for (int i = args.Length; i > 0; ){
7747 if (args [i] != sigp [i])