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.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Security;
41 using System.Security.Permissions;
44 #if BOOTSTRAP_WITH_OLDLIB || NET_2_1
45 using XmlElement = System.Object;
50 using Mono.CompilerServices.SymbolWriter;
52 namespace Mono.CSharp {
64 /// This is the base class for structs and classes.
66 public abstract class TypeContainer : DeclSpace, IMemberContainer {
68 public class MemberCoreArrayList: ArrayList
71 /// Defines the MemberCore objects that are in this array
73 public virtual void DefineContainerMembers ()
75 foreach (MemberCore mc in this) {
78 } catch (Exception e) {
79 throw new InternalErrorException (mc, e);
84 public virtual void Emit ()
86 foreach (MemberCore mc in this)
91 public class OperatorArrayList: MemberCoreArrayList
93 TypeContainer container;
95 public OperatorArrayList (TypeContainer container)
97 this.container = container;
101 // Checks that some operators come in pairs:
107 // They are matched based on the return type and the argument types
109 void CheckPairedOperators ()
111 bool has_equality_or_inequality = false;
112 Operator[] operators = (Operator[]) ToArray (typeof (Operator));
113 bool [] has_pair = new bool [operators.Length];
115 for (int i = 0; i < Count; ++i) {
116 if (operators [i] == null)
119 Operator o_a = operators [i];
120 Operator.OpType o_type = o_a.OperatorType;
121 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
122 has_equality_or_inequality = true;
124 Operator.OpType matching_type = o_a.GetMatchingOperator ();
125 if (matching_type == Operator.OpType.TOP) {
126 operators [i] = null;
130 for (int ii = 0; ii < Count; ++ii) {
131 Operator o_b = operators [ii];
132 if (o_b == null || o_b.OperatorType != matching_type)
135 if (!TypeManager.IsEqual (o_a.ReturnType, o_b.ReturnType))
138 if (!TypeManager.IsEqual (o_a.ParameterTypes, o_b.ParameterTypes))
141 operators [i] = null;
144 // Used to ignore duplicate user conversions
146 has_pair [ii] = true;
150 for (int i = 0; i < Count; ++i) {
151 if (operators [i] == null || has_pair [i])
154 Operator o = operators [i];
155 Report.Error (216, o.Location,
156 "The operator `{0}' requires a matching operator `{1}' to also be defined",
157 o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
160 if (has_equality_or_inequality && Report.WarningLevel > 2) {
161 if (container.Methods == null || !container.HasEquals)
162 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
164 if (container.Methods == null || !container.HasGetHashCode)
165 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
169 public override void DefineContainerMembers ()
171 base.DefineContainerMembers ();
172 CheckPairedOperators ();
180 GetHashCode = 1 << 1,
181 HasStaticFieldInitializer = 1 << 2
185 // Whether this is a struct, class or interface
186 public readonly Kind Kind;
188 // Holds a list of classes and structures
189 protected ArrayList types;
191 MemberCoreArrayList ordered_explicit_member_list;
192 MemberCoreArrayList ordered_member_list;
194 // Holds the list of properties
195 MemberCoreArrayList properties;
197 // Holds the list of delegates
198 MemberCoreArrayList delegates;
200 // Holds the list of constructors
201 protected MemberCoreArrayList instance_constructors;
203 // Holds the list of fields
204 MemberCoreArrayList fields;
206 // Holds a list of fields that have initializers
207 protected ArrayList initialized_fields;
209 // Holds a list of static fields that have initializers
210 protected ArrayList initialized_static_fields;
212 // Holds the list of constants
213 protected MemberCoreArrayList constants;
215 // Holds the methods.
216 MemberCoreArrayList methods;
219 protected MemberCoreArrayList events;
221 // Holds the indexers
224 // Holds the operators
225 MemberCoreArrayList operators;
227 // Holds the compiler generated classes
228 ArrayList compiler_generated;
231 // Pointers to the default constructor and the default static constructor
233 protected Constructor default_constructor;
234 protected Constructor default_static_constructor;
237 // Points to the first non-static field added to the container.
239 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
240 // and the first one's as good as any.
242 FieldBase first_nonstatic_field = null;
245 // This one is computed after we can distinguish interfaces
246 // from classes from the arraylist `type_bases'
249 TypeExpr[] iface_exprs;
252 ArrayList type_bases;
254 bool members_resolved;
255 bool members_resolved_ok;
256 bool members_defined;
257 bool members_defined_ok;
259 // The interfaces we implement.
260 protected Type[] ifaces;
262 // The base member cache and our member cache
263 MemberCache base_cache;
264 protected MemberCache member_cache;
266 public const string DefaultIndexerName = "Item";
268 private bool seen_normal_indexers = false;
269 private string indexer_name = DefaultIndexerName;
271 private CachedMethods cached_method;
274 GenericTypeParameterBuilder[] gen_params;
277 ArrayList partial_parts;
280 /// The pending methods that need to be implemented
281 // (interfaces or abstract methods)
283 PendingImplementation pending;
285 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
286 Attributes attrs, Kind kind)
287 : base (ns, parent, name, attrs)
289 if (parent != null && parent.NamespaceEntry != ns)
290 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
293 this.PartialContainer = this;
296 public bool AddMember (MemberCore symbol)
298 return AddToContainer (symbol, symbol.MemberName.MethodName);
301 protected virtual bool AddMemberType (DeclSpace ds)
303 return AddToContainer (ds, ds.Basename);
306 public void AddConstant (Const constant)
308 if (!AddMember (constant))
311 if (constants == null)
312 constants = new MemberCoreArrayList ();
314 constants.Add (constant);
317 public TypeContainer AddTypeContainer (TypeContainer tc)
319 if (!AddMemberType (tc))
323 types = new MemberCoreArrayList ();
329 public virtual TypeContainer AddPartial (TypeContainer next_part)
331 return AddPartial (next_part, next_part.Basename);
334 protected TypeContainer AddPartial (TypeContainer next_part, string name)
336 next_part.ModFlags |= Modifiers.PARTIAL;
337 TypeContainer tc = defined_names [name] as TypeContainer;
340 return AddTypeContainer (next_part);
342 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
343 Report.SymbolRelatedToPreviousError (tc);
344 Error_MissingPartialModifier (next_part);
347 if (tc.Kind != next_part.Kind) {
348 Report.SymbolRelatedToPreviousError (tc);
349 Report.Error (261, next_part.Location,
350 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
351 next_part.GetSignatureForError ());
354 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
355 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
356 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
357 Report.SymbolRelatedToPreviousError (tc);
358 Report.Error (262, next_part.Location,
359 "Partial declarations of `{0}' have conflicting accessibility modifiers",
360 next_part.GetSignatureForError ());
363 if (tc.MemberName.IsGeneric) {
364 TypeParameter[] tc_names = tc.TypeParameters;
365 TypeParameterName[] part_names = next_part.MemberName.TypeArguments.GetDeclarations ();
367 for (int i = 0; i < tc_names.Length; ++i) {
368 if (tc_names[i].Name == part_names[i].Name)
371 Report.SymbolRelatedToPreviousError (part_names[i].Location, "");
372 Report.Error (264, tc.Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
373 tc.GetSignatureForError ());
377 if (tc.partial_parts == null)
378 tc.partial_parts = new ArrayList (1);
380 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
381 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
382 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
383 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
384 tc.ModFlags |= next_part.ModFlags;
386 tc.ModFlags |= next_part.ModFlags;
389 if (next_part.attributes != null) {
390 if (tc.attributes == null)
391 tc.attributes = next_part.attributes;
393 tc.attributes.AddAttributes (next_part.attributes.Attrs);
396 next_part.PartialContainer = tc;
397 tc.partial_parts.Add (next_part);
401 public void AddDelegate (Delegate d)
403 if (!AddMemberType (d))
406 if (delegates == null)
407 delegates = new MemberCoreArrayList ();
412 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
414 if (ordered_explicit_member_list == null) {
415 ordered_explicit_member_list = new MemberCoreArrayList ();
416 ordered_member_list = new MemberCoreArrayList ();
420 ordered_explicit_member_list.Add (mc);
421 alist.Insert (0, mc);
424 ordered_member_list.Add (mc);
430 public void AddMethod (Method method)
432 MemberName mn = method.MemberName;
433 if (!AddToContainer (method, mn.IsGeneric ? mn.Basename : mn.MethodName))
437 methods = new MemberCoreArrayList ();
439 if (method.MemberName.Left != null)
440 AddMemberToList (method, methods, true);
442 AddMemberToList (method, methods, false);
446 // Do not use this method: use AddMethod.
448 // This is only used by iterators.
450 public void AppendMethod (Method method)
452 if (!AddMember (method))
456 methods = new MemberCoreArrayList ();
458 AddMemberToList (method, methods, false);
461 public void AddConstructor (Constructor c)
463 if (c.Name != MemberName.Name) {
464 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
467 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
468 if (!AddToContainer (c, is_static ?
469 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
472 if (is_static && c.Parameters.Empty){
473 if (default_static_constructor != null) {
474 Report.SymbolRelatedToPreviousError (default_static_constructor);
475 Report.Error (111, c.Location,
476 "A member `{0}' is already defined. Rename this member or use different parameter types",
477 c.GetSignatureForError ());
481 default_static_constructor = c;
483 if (c.Parameters.Empty)
484 default_constructor = c;
486 if (instance_constructors == null)
487 instance_constructors = new MemberCoreArrayList ();
489 instance_constructors.Add (c);
493 public bool AddField (FieldBase field)
495 if (!AddMember (field))
499 fields = new MemberCoreArrayList ();
503 if ((field.ModFlags & Modifiers.STATIC) != 0)
506 if (first_nonstatic_field == null) {
507 first_nonstatic_field = field;
511 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
512 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
513 Report.Warning (282, 3, field.Location,
514 "struct instance field `{0}' found in different declaration from instance field `{1}'",
515 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
520 public void AddProperty (Property prop)
522 if (!AddMember (prop) ||
523 !AddMember (prop.Get) || !AddMember (prop.Set))
526 if (properties == null)
527 properties = new MemberCoreArrayList ();
529 if (prop.MemberName.Left != null)
530 AddMemberToList (prop, properties, true);
532 AddMemberToList (prop, properties, false);
535 public void AddEvent (Event e)
540 if (e is EventProperty) {
541 if (!AddMember (e.Add))
544 if (!AddMember (e.Remove))
549 events = new MemberCoreArrayList ();
555 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
557 public void AddIndexer (Indexer i)
559 if (indexers == null)
560 indexers = new ArrayList ();
562 if (i.IsExplicitImpl)
563 AddMemberToList (i, indexers, true);
565 AddMemberToList (i, indexers, false);
568 public void AddOperator (Operator op)
573 if (operators == null)
574 operators = new OperatorArrayList (this);
579 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
581 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
583 if (compiler_generated == null)
584 compiler_generated = new ArrayList ();
586 compiler_generated.Add (c);
589 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
591 if (a.Type == TypeManager.default_member_type) {
592 if (Indexers != null) {
593 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
598 base.ApplyAttributeBuilder (a, cb);
601 public override AttributeTargets AttributeTargets {
603 throw new NotSupportedException ();
607 public ArrayList Types {
613 public MemberCoreArrayList Methods {
619 public ArrayList Constants {
625 public ArrayList CompilerGenerated {
627 return compiler_generated;
631 protected Type BaseType {
633 return TypeBuilder.BaseType;
637 public ArrayList Bases {
647 public ArrayList Fields {
653 public ArrayList InstanceConstructors {
655 return instance_constructors;
659 public ArrayList Properties {
665 public ArrayList Events {
671 public ArrayList Indexers {
677 public ArrayList Operators {
683 public ArrayList Delegates {
689 protected override TypeAttributes TypeAttr {
691 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
695 public string IndexerName {
697 return indexers == null ? DefaultIndexerName : indexer_name;
701 public bool IsComImport {
703 if (OptAttributes == null || TypeManager.comimport_attr_type == null)
706 return OptAttributes.Contains (TypeManager.comimport_attr_type);
710 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
712 if ((field.ModFlags & Modifiers.STATIC) != 0){
713 if (initialized_static_fields == null) {
714 PartialContainer.HasStaticFieldInitializer = true;
715 initialized_static_fields = new ArrayList (4);
718 initialized_static_fields.Add (expression);
720 if (initialized_fields == null)
721 initialized_fields = new ArrayList (4);
723 initialized_fields.Add (expression);
727 public void ResolveFieldInitializers (EmitContext ec)
729 // Field initializers are tricky for partial classes. They have to
730 // share same costructor (block) but they have they own resolve scope.
731 DeclSpace orig = ec.DeclContainer;
733 if (partial_parts != null) {
734 foreach (TypeContainer part in partial_parts) {
735 ec.DeclContainer = part;
736 part.DoResolveFieldInitializers (ec);
739 ec.DeclContainer = PartialContainer;
740 DoResolveFieldInitializers (ec);
741 ec.DeclContainer = orig;
744 void DoResolveFieldInitializers (EmitContext ec)
747 if (initialized_static_fields == null)
750 bool has_complex_initializer = false;
751 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
752 foreach (FieldInitializer fi in initialized_static_fields) {
753 fi.ResolveStatement (ec);
754 if (!fi.IsComplexInitializer)
757 has_complex_initializer = true;
760 // Need special check to not optimize code like this
761 // static int a = b = 5;
763 if (!has_complex_initializer && RootContext.Optimize) {
764 for (int i = 0; i < initialized_static_fields.Count; ++i) {
765 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
766 if (fi.IsDefaultInitializer) {
767 initialized_static_fields.RemoveAt (i);
777 if (initialized_fields == null)
780 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
781 for (int i = 0; i < initialized_fields.Count; ++i) {
782 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
783 fi.ResolveStatement (ec);
784 if (fi.IsDefaultInitializer && RootContext.Optimize) {
785 // Field is re-initialized to its default value => removed
786 initialized_fields.RemoveAt (i);
794 // Emits the instance field initializers
796 public bool EmitFieldInitializers (EmitContext ec)
798 if (partial_parts != null) {
799 foreach (TypeContainer part in partial_parts)
800 part.EmitFieldInitializers (ec);
806 fields = initialized_static_fields;
808 fields = initialized_fields;
814 foreach (FieldInitializer f in fields) {
815 f.EmitStatement (ec);
820 public override string DocComment {
832 public PendingImplementation PendingImplementations {
833 get { return pending; }
836 public override bool GetClsCompliantAttributeValue ()
838 if (PartialContainer != this)
839 return PartialContainer.GetClsCompliantAttributeValue ();
841 return base.GetClsCompliantAttributeValue ();
844 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
846 // FIXME: get rid of partial_parts and store lists of bases of each part here
847 // assumed, not verified: 'part' is in 'partial_parts'
848 ((TypeContainer) part).Bases = bases;
851 TypeExpr[] GetNormalBases (out TypeExpr base_class)
857 int count = Bases.Count;
860 if (Kind == Kind.Class){
861 TypeExpr name = ((Expression) Bases [0]).ResolveAsBaseTerminal (this, false);
867 if (!name.IsInterface) {
868 // base_class could be a class, struct, enum, delegate.
869 // This is validated in GetClassBases.
875 TypeExpr [] ifaces = new TypeExpr [count-start];
877 for (i = start, j = 0; i < count; i++, j++){
878 TypeExpr resolved = ((Expression) Bases [i]).ResolveAsBaseTerminal (this, false);
879 if (resolved == null) {
883 ifaces [j] = resolved;
886 return ifaces.Length == 0 ? null : ifaces;
890 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
892 ArrayList ifaces = new ArrayList (0);
893 if (iface_exprs != null)
894 ifaces.AddRange (iface_exprs);
896 foreach (TypeContainer part in partial_parts) {
897 TypeExpr new_base_class;
898 TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
899 if (new_base_class != TypeManager.system_object_expr) {
900 if (base_class == TypeManager.system_object_expr)
901 base_class = new_base_class;
903 if (new_base_class != null && !new_base_class.Equals (base_class)) {
904 Report.SymbolRelatedToPreviousError (base_class.Location, "");
905 Report.Error (263, part.Location,
906 "Partial declarations of `{0}' must not specify different base classes",
907 part.GetSignatureForError ());
914 if (new_ifaces == null)
917 foreach (TypeExpr iface in new_ifaces) {
918 if (ifaces.Contains (iface))
925 if (ifaces.Count == 0)
928 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
932 /// This function computes the Base class and also the
933 /// list of interfaces that the class or struct @c implements.
935 /// The return value is an array (might be null) of
936 /// interfaces implemented (as Types).
938 /// The @base_class argument is set to the base object or null
939 /// if this is `System.Object'.
941 public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
943 TypeExpr[] ifaces = GetNormalBases (out base_class);
948 int count = ifaces.Length;
950 for (int i = 0; i < count; i++) {
951 TypeExpr iface = (TypeExpr) ifaces [i];
953 if (!iface.IsInterface) {
954 if (Kind != Kind.Class) {
955 // TODO: location of symbol related ....
956 Error_TypeInListIsNotInterface (Location, iface.GetSignatureForError ());
958 else if (base_class != null)
959 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
960 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
962 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
963 GetSignatureForError (), iface.GetSignatureForError ());
968 for (int x = 0; x < i; x++) {
969 if (iface.Equals (ifaces [x])) {
970 Report.Error (528, Location,
971 "`{0}' is already listed in " +
972 "interface list", iface.GetSignatureForError ());
977 if ((Kind == Kind.Interface) &&
978 !iface.AsAccessible (this)) {
979 Report.Error (61, Location,
980 "Inconsistent accessibility: base " +
981 "interface `{0}' is less accessible " +
982 "than interface `{1}'", iface.GetSignatureForError (),
990 bool CheckGenericInterfaces (Type[] ifaces)
993 ArrayList already_checked = new ArrayList ();
995 for (int i = 0; i < ifaces.Length; i++) {
996 Type iface = ifaces [i];
997 foreach (Type t in already_checked) {
1001 Type[] inferred = new Type [CountTypeParameters];
1002 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
1005 Report.Error (695, Location,
1006 "`{0}' cannot implement both `{1}' and `{2}' " +
1007 "because they may unify for some type parameter substitutions",
1008 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
1009 TypeManager.CSharpName (t));
1013 already_checked.Add (iface);
1022 protected void Error_TypeInListIsNotInterface (Location loc, string type)
1024 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
1027 bool CreateTypeBuilder ()
1030 Type default_parent = null;
1031 if (Kind == Kind.Struct)
1032 default_parent = TypeManager.value_type;
1033 else if (Kind == Kind.Enum)
1034 default_parent = TypeManager.enum_type;
1037 if (TypeManager.NamespaceClash (Name, Location)) {
1041 ModuleBuilder builder = CodeGen.Module.Builder;
1042 TypeBuilder = builder.DefineType (
1043 Name, TypeAttr, default_parent, null);
1045 TypeBuilder builder = Parent.TypeBuilder;
1047 TypeBuilder = builder.DefineNestedType (
1048 Basename, TypeAttr, default_parent, null);
1050 } catch (ArgumentException) {
1051 Report.RuntimeMissingSupport (Location, "static classes");
1055 TypeManager.AddUserType (this);
1059 string[] param_names = new string [TypeParameters.Length];
1060 for (int i = 0; i < TypeParameters.Length; i++)
1061 param_names [i] = TypeParameters [i].Name;
1063 gen_params = TypeBuilder.DefineGenericParameters (param_names);
1065 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1066 for (int i = offset; i < gen_params.Length; i++)
1067 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1074 bool DefineBaseTypes ()
1076 iface_exprs = GetClassBases (out base_type);
1077 if (partial_parts != null) {
1078 iface_exprs = GetNormalPartialBases (ref base_type);
1082 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1083 // which in turn should have called DefineType()s on base types if necessary.
1085 // None of the code below should trigger DefineType()s on classes that we depend on.
1086 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1088 // Let's do it as soon as possible, since code below can call DefineType() on classes
1089 // that depend on us to be populated before they are.
1091 if (!(this is CompilerGeneratedClass))
1092 RootContext.RegisterOrder (this);
1094 if (IsGeneric && base_type != null && TypeManager.IsAttributeType (base_type.Type)) {
1095 Report.Error (698, base_type.Location,
1096 "A generic type cannot derive from `{0}' because it is an attribute class",
1101 if (!CheckRecursiveDefinition (this))
1104 if (base_type != null && base_type.Type != null) {
1105 TypeBuilder.SetParent (base_type.Type);
1107 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1108 if (obsolete_attr != null && !IsInObsoleteScope)
1109 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1112 // add interfaces that were not added at type creation
1113 if (iface_exprs != null) {
1114 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1118 foreach (Type itype in ifaces)
1119 TypeBuilder.AddInterfaceImplementation (itype);
1121 foreach (TypeExpr ie in iface_exprs) {
1122 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (ie.Type);
1123 if ((oa != null) && !IsInObsoleteScope)
1124 AttributeTester.Report_ObsoleteMessage (
1125 oa, ie.GetSignatureForError (), Location);
1128 if (!CheckGenericInterfaces (ifaces))
1131 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1138 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1140 public TypeBuilder CreateType ()
1142 if (TypeBuilder != null)
1148 if (!CreateTypeBuilder ()) {
1153 if (partial_parts != null) {
1154 foreach (TypeContainer part in partial_parts)
1155 part.TypeBuilder = TypeBuilder;
1158 if (Types != null) {
1159 foreach (TypeContainer tc in Types) {
1160 if (tc.CreateType () == null) {
1172 public override TypeBuilder DefineType ()
1179 type_defined = true;
1181 if (CreateType () == null) {
1186 if (!DefineBaseTypes ()) {
1191 if (!(this is CompilerGeneratedClass)) {
1192 if (!ResolveMembers ()) {
1198 if (!DefineNestedTypes ()) {
1206 public bool ResolveMembers ()
1208 if (members_resolved)
1209 return members_resolved_ok;
1211 members_resolved_ok = DoResolveMembers ();
1212 members_resolved = true;
1214 return members_resolved_ok;
1217 protected virtual bool DoResolveMembers ()
1219 if (methods != null) {
1220 foreach (Method method in methods) {
1221 if (!method.ResolveMembers ())
1226 if (instance_constructors != null) {
1227 foreach (Constructor c in instance_constructors) {
1228 if (!c.ResolveMembers ())
1233 if (default_static_constructor != null) {
1234 if (!default_static_constructor.ResolveMembers ())
1238 if (operators != null) {
1239 foreach (Operator o in operators) {
1240 if (!o.ResolveMembers ())
1245 if (properties != null) {
1246 foreach (PropertyBase p in properties) {
1247 if (!p.Get.IsDummy && !p.Get.ResolveMembers ())
1249 if (!p.Set.IsDummy && !p.Set.ResolveMembers ())
1254 if (indexers != null) {
1255 foreach (PropertyBase p in indexers) {
1256 if (!p.Get.IsDummy && !p.Get.ResolveMembers ())
1258 if (!p.Set.IsDummy && !p.Set.ResolveMembers ())
1263 if (events != null) {
1264 foreach (Event e in events) {
1265 if (!e.Add.ResolveMembers ())
1267 if (!e.Remove.ResolveMembers ())
1272 if (compiler_generated != null) {
1273 foreach (CompilerGeneratedClass c in compiler_generated) {
1274 if (c.DefineType () == null)
1282 Constraints [] constraints;
1283 public override void SetParameterInfo (ArrayList constraints_list)
1285 if (PartialContainer == this) {
1286 base.SetParameterInfo (constraints_list);
1290 if (constraints_list == null)
1293 constraints = new Constraints [PartialContainer.CountCurrentTypeParameters];
1295 TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
1296 for (int i = 0; i < constraints.Length; i++) {
1297 foreach (Constraints constraint in constraints_list) {
1298 if (constraint.TypeParameter == current_params [i].Name) {
1299 constraints [i] = constraint;
1306 bool UpdateTypeParameterConstraints ()
1308 if (constraints == null)
1311 TypeParameter[] current_params = PartialContainer.CurrentTypeParameters;
1312 for (int i = 0; i < current_params.Length; i++) {
1313 if (!current_params [i].UpdateConstraints (this, constraints [i])) {
1314 Report.SymbolRelatedToPreviousError (Location, "");
1315 Report.Error (265, PartialContainer.Location,
1316 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1317 PartialContainer.GetSignatureForError (), current_params [i].Name);
1325 public bool ResolveType ()
1327 if (!DoResolveType ())
1330 if (compiler_generated != null) {
1331 foreach (CompilerGeneratedClass c in compiler_generated)
1332 if (!c.ResolveType ())
1339 protected virtual bool DoResolveType ()
1341 if ((base_type != null) &&
1342 (base_type.ResolveAsTypeTerminal (this, false) == null)) {
1350 if (PartialContainer != this)
1351 throw new InternalErrorException ();
1353 TypeExpr current_type = null;
1355 foreach (TypeParameter type_param in CurrentTypeParameters) {
1356 if (!type_param.Resolve (this)) {
1362 if (partial_parts != null) {
1363 foreach (TypeContainer part in partial_parts) {
1364 if (!part.UpdateTypeParameterConstraints ()) {
1371 foreach (TypeParameter type_param in TypeParameters) {
1372 if (!type_param.DefineType (this)) {
1378 current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
1380 foreach (TypeParameter type_param in TypeParameters)
1381 if (!type_param.CheckDependencies ()) {
1386 if (current_type != null) {
1387 current_type = current_type.ResolveAsTypeTerminal (this, false);
1388 if (current_type == null) {
1393 CurrentType = current_type.Type;
1399 protected virtual bool DefineNestedTypes ()
1401 if (Types != null) {
1402 foreach (TypeContainer tc in Types)
1403 if (tc.DefineType () == null)
1407 if (Delegates != null) {
1408 foreach (Delegate d in Delegates)
1409 if (d.DefineType () == null)
1416 TypeContainer InTransit;
1418 protected bool CheckRecursiveDefinition (TypeContainer tc)
1420 if (InTransit != null) {
1421 Report.SymbolRelatedToPreviousError (this);
1422 if (this is Interface)
1424 529, tc.Location, "Inherited interface `{0}' causes a " +
1425 "cycle in the interface hierarchy of `{1}'",
1426 GetSignatureForError (), tc.GetSignatureForError ());
1429 146, tc.Location, "Circular base class dependency " +
1430 "involving `{0}' and `{1}'",
1431 tc.GetSignatureForError (), GetSignatureForError ());
1437 if (base_type != null && base_type.Type != null) {
1438 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1439 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1440 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1444 if (iface_exprs != null) {
1445 foreach (TypeExpr iface in iface_exprs) {
1446 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1447 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1448 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1453 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1460 public static void Error_KeywordNotAllowed (Location loc)
1462 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1466 /// Populates our TypeBuilder with fields and methods
1468 public override bool DefineMembers ()
1470 if (members_defined)
1471 return members_defined_ok;
1473 if (!base.DefineMembers ())
1476 members_defined_ok = DoDefineMembers ();
1477 members_defined = true;
1479 return members_defined_ok;
1482 protected virtual bool DoDefineMembers ()
1484 if (iface_exprs != null) {
1485 foreach (TypeExpr iface in iface_exprs) {
1486 ConstructedType ct = iface as ConstructedType;
1487 if ((ct != null) && !ct.CheckConstraints (this))
1492 if (base_type != null) {
1493 ConstructedType ct = base_type as ConstructedType;
1494 if ((ct != null) && !ct.CheckConstraints (this))
1497 member_cache = new MemberCache (base_type.Type, this);
1498 } else if (Kind == Kind.Interface) {
1499 member_cache = new MemberCache (null, this);
1500 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1501 for (int i = 0; i < ifaces.Length; ++i)
1502 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1504 member_cache = new MemberCache (null, this);
1508 foreach (TypeContainer tc in types)
1509 member_cache.AddNestedType (tc);
1511 if (delegates != null)
1512 foreach (Delegate d in delegates)
1513 member_cache.AddNestedType (d);
1515 if (partial_parts != null) {
1516 foreach (TypeContainer part in partial_parts)
1517 part.member_cache = member_cache;
1521 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1522 if (conflict_symbol == null) {
1523 if ((ModFlags & Modifiers.NEW) != 0)
1524 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1526 if ((ModFlags & Modifiers.NEW) == 0) {
1527 Report.SymbolRelatedToPreviousError (conflict_symbol);
1528 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1529 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1534 DefineContainerMembers (constants);
1535 DefineContainerMembers (fields);
1537 if (Kind == Kind.Struct || Kind == Kind.Class) {
1538 pending = PendingImplementation.GetPendingImplementations (this);
1542 // Constructors are not in the defined_names array
1544 DefineContainerMembers (instance_constructors);
1546 DefineContainerMembers (events);
1547 DefineContainerMembers (ordered_explicit_member_list);
1548 DefineContainerMembers (ordered_member_list);
1550 DefineContainerMembers (operators);
1551 DefineContainerMembers (delegates);
1553 ComputeIndexerName();
1554 CheckEqualsAndGetHashCode();
1556 if (CurrentType != null) {
1557 GenericType = CurrentType;
1562 // FIXME: This hack is needed because member cache does not work
1563 // with generic types, we rely on runtime to inflate dynamic types.
1564 // TODO: This hack requires member cache refactoring to be removed
1566 if (TypeBuilder.IsGenericType)
1567 member_cache = new MemberCache (this);
1573 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1576 mcal.DefineContainerMembers ();
1579 protected virtual void ComputeIndexerName ()
1581 if (indexers == null)
1584 string class_indexer_name = null;
1587 // If there's both an explicit and an implicit interface implementation, the
1588 // explicit one actually implements the interface while the other one is just
1589 // a normal indexer. See bug #37714.
1592 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1593 foreach (Indexer i in indexers) {
1594 if (i.InterfaceType != null) {
1595 if (seen_normal_indexers)
1596 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1600 seen_normal_indexers = true;
1602 if (class_indexer_name == null) {
1603 class_indexer_name = i.ShortName;
1607 if (i.ShortName != class_indexer_name)
1608 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");
1611 if (class_indexer_name != null)
1612 indexer_name = class_indexer_name;
1615 protected virtual void EmitIndexerName ()
1617 if (!seen_normal_indexers)
1620 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
1621 TypeBuilder.SetCustomAttribute (cb);
1624 protected virtual void CheckEqualsAndGetHashCode ()
1626 if (methods == null)
1629 if (HasEquals && !HasGetHashCode) {
1630 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1634 public override bool Define ()
1636 if (compiler_generated != null) {
1637 foreach (CompilerGeneratedClass c in compiler_generated) {
1646 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1648 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1652 /// This function is based by a delegate to the FindMembers routine
1654 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1660 /// This filter is used by FindMembers, and we just keep
1661 /// a global for the filter to `AlwaysAccept'
1663 static MemberFilter accepting_filter;
1666 static TypeContainer ()
1668 accepting_filter = new MemberFilter (AlwaysAccept);
1671 public MethodInfo[] GetMethods ()
1673 ArrayList members = new ArrayList ();
1677 if (methods != null) {
1678 int len = methods.Count;
1679 for (int i = 0; i < len; i++) {
1680 Method m = (Method) methods [i];
1682 members.Add (m.MethodBuilder);
1686 if (operators != null) {
1687 int len = operators.Count;
1688 for (int i = 0; i < len; i++) {
1689 Operator o = (Operator) operators [i];
1691 members.Add (o.MethodBuilder);
1695 if (properties != null) {
1696 int len = properties.Count;
1697 for (int i = 0; i < len; i++) {
1698 Property p = (Property) properties [i];
1700 if (p.GetBuilder != null)
1701 members.Add (p.GetBuilder);
1702 if (p.SetBuilder != null)
1703 members.Add (p.SetBuilder);
1707 if (indexers != null) {
1708 int len = indexers.Count;
1709 for (int i = 0; i < len; i++) {
1710 Indexer ix = (Indexer) indexers [i];
1712 if (ix.GetBuilder != null)
1713 members.Add (ix.GetBuilder);
1714 if (ix.SetBuilder != null)
1715 members.Add (ix.SetBuilder);
1719 if (events != null) {
1720 int len = events.Count;
1721 for (int i = 0; i < len; i++) {
1722 Event e = (Event) events [i];
1724 if (e.AddBuilder != null)
1725 members.Add (e.AddBuilder);
1726 if (e.RemoveBuilder != null)
1727 members.Add (e.RemoveBuilder);
1731 MethodInfo[] retMethods = new MethodInfo [members.Count];
1732 members.CopyTo (retMethods, 0);
1736 // Indicated whether container has StructLayout attribute set Explicit
1737 public bool HasExplicitLayout {
1738 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1739 set { caching_flags |= Flags.HasExplicitLayout; }
1743 // Return the nested type with name @name. Ensures that the nested type
1744 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1746 public Type FindNestedType (string name)
1748 if (PartialContainer != this)
1749 throw new InternalErrorException ("should not happen");
1751 ArrayList [] lists = { types, delegates };
1753 for (int j = 0; j < lists.Length; ++j) {
1754 ArrayList list = lists [j];
1758 int len = list.Count;
1759 for (int i = 0; i < len; ++i) {
1760 DeclSpace ds = (DeclSpace) list [i];
1761 if (ds.Basename == name) {
1762 return ds.DefineType ();
1770 private void FindMembers_NestedTypes (int modflags,
1771 BindingFlags bf, MemberFilter filter, object criteria,
1772 ref ArrayList members)
1774 ArrayList [] lists = { types, delegates };
1776 for (int j = 0; j < lists.Length; ++j) {
1777 ArrayList list = lists [j];
1781 int len = list.Count;
1782 for (int i = 0; i < len; i++) {
1783 DeclSpace ds = (DeclSpace) list [i];
1785 if ((ds.ModFlags & modflags) == 0)
1788 TypeBuilder tb = ds.TypeBuilder;
1790 if (!(criteria is string) || ds.Basename.Equals (criteria))
1791 tb = ds.DefineType ();
1794 if (tb != null && (filter (tb, criteria) == true)) {
1795 if (members == null)
1796 members = new ArrayList ();
1805 /// This method returns the members of this type just like Type.FindMembers would
1806 /// Only, we need to use this for types which are _being_ defined because MS'
1807 /// implementation can't take care of that.
1810 // FIXME: return an empty static array instead of null, that cleans up
1811 // some code and is consistent with some coding conventions I just found
1815 // Notice that in various cases we check if our field is non-null,
1816 // something that would normally mean that there was a bug elsewhere.
1818 // The problem happens while we are defining p-invoke methods, as those
1819 // will trigger a FindMembers, but this happens before things are defined
1821 // Since the whole process is a no-op, it is fine to check for null here.
1823 // TODO: This approach will be one day completely removed, it's already
1824 // used at few places only
1827 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1828 MemberFilter filter, object criteria)
1830 ArrayList members = null;
1833 if ((bf & BindingFlags.Public) != 0)
1834 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1836 if ((bf & BindingFlags.NonPublic) != 0)
1837 modflags |= Modifiers.PRIVATE;
1839 int static_mask = 0, static_flags = 0;
1840 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1841 case BindingFlags.Static:
1842 static_mask = static_flags = Modifiers.STATIC;
1845 case BindingFlags.Instance:
1846 static_mask = Modifiers.STATIC;
1851 static_mask = static_flags = 0;
1855 Timer.StartTimer (TimerType.TcFindMembers);
1858 filter = accepting_filter;
1860 if ((mt & MemberTypes.Field) != 0) {
1861 if (fields != null) {
1862 int len = fields.Count;
1863 for (int i = 0; i < len; i++) {
1864 FieldBase f = (FieldBase) fields [i];
1866 if ((f.ModFlags & modflags) == 0)
1868 if ((f.ModFlags & static_mask) != static_flags)
1871 FieldBuilder fb = f.FieldBuilder;
1872 if (fb != null && filter (fb, criteria) == true) {
1873 if (members == null)
1874 members = new ArrayList ();
1881 if (constants != null) {
1882 int len = constants.Count;
1883 for (int i = 0; i < len; i++) {
1884 Const con = (Const) constants [i];
1886 if ((con.ModFlags & modflags) == 0)
1888 if ((con.ModFlags & static_mask) != static_flags)
1891 FieldBuilder fb = con.FieldBuilder;
1894 fb = con.FieldBuilder;
1896 if (fb != null && filter (fb, criteria) == true) {
1897 if (members == null)
1898 members = new ArrayList ();
1906 if ((mt & MemberTypes.Method) != 0) {
1907 if (methods != null) {
1908 int len = methods.Count;
1909 for (int i = 0; i < len; i++) {
1910 Method m = (Method) methods [i];
1912 if ((m.ModFlags & modflags) == 0)
1914 if ((m.ModFlags & static_mask) != static_flags)
1917 MethodBuilder mb = m.MethodBuilder;
1919 if (mb != null && filter (mb, criteria) == true) {
1920 if (members == null)
1921 members = new ArrayList ();
1928 if (operators != null) {
1929 int len = operators.Count;
1930 for (int i = 0; i < len; i++) {
1931 Operator o = (Operator) operators [i];
1933 if ((o.ModFlags & modflags) == 0)
1935 if ((o.ModFlags & static_mask) != static_flags)
1938 MethodBuilder ob = o.MethodBuilder;
1939 if (ob != null && filter (ob, criteria) == true) {
1940 if (members == null)
1941 members = new ArrayList ();
1948 if (events != null) {
1949 foreach (Event e in events) {
1950 if ((e.ModFlags & modflags) == 0)
1952 if ((e.ModFlags & static_mask) != static_flags)
1955 MethodBuilder b = e.AddBuilder;
1956 if (b != null && filter (b, criteria)) {
1957 if (members == null)
1958 members = new ArrayList (4);
1963 b = e.RemoveBuilder;
1964 if (b != null && filter (b, criteria)) {
1965 if (members == null)
1966 members = new ArrayList (4);
1973 if (properties != null) {
1974 int len = properties.Count;
1975 for (int i = 0; i < len; i++) {
1976 Property p = (Property) properties [i];
1978 if ((p.ModFlags & modflags) == 0)
1980 if ((p.ModFlags & static_mask) != static_flags)
1986 if (b != null && filter (b, criteria) == true) {
1987 if (members == null)
1988 members = new ArrayList ();
1994 if (b != null && filter (b, criteria) == true) {
1995 if (members == null)
1996 members = new ArrayList ();
2003 if (indexers != null) {
2004 int len = indexers.Count;
2005 for (int i = 0; i < len; i++) {
2006 Indexer ix = (Indexer) indexers [i];
2008 if ((ix.ModFlags & modflags) == 0)
2010 if ((ix.ModFlags & static_mask) != static_flags)
2016 if (b != null && filter (b, criteria) == true) {
2017 if (members == null)
2018 members = new ArrayList ();
2024 if (b != null && filter (b, criteria) == true) {
2025 if (members == null)
2026 members = new ArrayList ();
2034 if ((mt & MemberTypes.Event) != 0) {
2035 if (events != null) {
2036 int len = events.Count;
2037 for (int i = 0; i < len; i++) {
2038 Event e = (Event) events [i];
2040 if ((e.ModFlags & modflags) == 0)
2042 if ((e.ModFlags & static_mask) != static_flags)
2045 MemberInfo eb = e.EventBuilder;
2046 if (eb != null && filter (eb, criteria) == true) {
2047 if (members == null)
2048 members = new ArrayList ();
2050 members.Add (e.EventBuilder);
2056 if ((mt & MemberTypes.Property) != 0){
2057 if (properties != null) {
2058 int len = properties.Count;
2059 for (int i = 0; i < len; i++) {
2060 Property p = (Property) properties [i];
2062 if ((p.ModFlags & modflags) == 0)
2064 if ((p.ModFlags & static_mask) != static_flags)
2067 MemberInfo pb = p.PropertyBuilder;
2068 if (pb != null && filter (pb, criteria) == true) {
2069 if (members == null)
2070 members = new ArrayList ();
2072 members.Add (p.PropertyBuilder);
2077 if (indexers != null) {
2078 int len = indexers.Count;
2079 for (int i = 0; i < len; i++) {
2080 Indexer ix = (Indexer) indexers [i];
2082 if ((ix.ModFlags & modflags) == 0)
2084 if ((ix.ModFlags & static_mask) != static_flags)
2087 MemberInfo ib = ix.PropertyBuilder;
2088 if (ib != null && filter (ib, criteria) == true) {
2089 if (members == null)
2090 members = new ArrayList ();
2092 members.Add (ix.PropertyBuilder);
2098 if ((mt & MemberTypes.NestedType) != 0)
2099 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
2101 if ((mt & MemberTypes.Constructor) != 0){
2102 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
2103 int len = instance_constructors.Count;
2104 for (int i = 0; i < len; i++) {
2105 Constructor c = (Constructor) instance_constructors [i];
2107 ConstructorBuilder cb = c.ConstructorBuilder;
2108 if (cb != null && filter (cb, criteria) == true) {
2109 if (members == null)
2110 members = new ArrayList ();
2117 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2118 ConstructorBuilder cb =
2119 default_static_constructor.ConstructorBuilder;
2121 if (cb != null && filter (cb, criteria) == true) {
2122 if (members == null)
2123 members = new ArrayList ();
2131 // Lookup members in base if requested.
2133 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2134 if (TypeBuilder.BaseType != null) {
2135 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2136 if (list.Count > 0) {
2137 if (members == null)
2138 members = new ArrayList ();
2140 members.AddRange (list);
2145 Timer.StopTimer (TimerType.TcFindMembers);
2147 if (members == null)
2148 return MemberList.Empty;
2150 return new MemberList (members);
2153 public override MemberCache MemberCache {
2155 return member_cache;
2159 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2160 MemberFilter filter, object criteria)
2162 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2165 return ds.FindMembers (mt, bf, filter, criteria);
2167 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2171 /// Emits the values for the constants
2173 public void EmitConstants ()
2175 if (constants != null)
2176 foreach (Const con in constants)
2181 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2186 foreach (MemberCore mc in al) {
2187 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2190 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2191 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2196 public virtual void VerifyMembers ()
2199 // Check for internal or private fields that were never assigned
2201 if (Report.WarningLevel >= 3) {
2202 CheckMemberUsage (properties, "property");
2203 CheckMemberUsage (methods, "method");
2204 CheckMemberUsage (constants, "constant");
2206 if (fields != null){
2207 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2208 foreach (FieldBase f in fields) {
2209 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2210 if (is_type_exposed)
2213 f.SetMemberIsUsed ();
2217 if ((f.caching_flags & Flags.IsAssigned) == 0)
2218 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2221 const int error_code = 414;
2223 const int error_code = 169;
2225 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2226 f.GetSignatureForError ());
2232 // Only report 649 on level 4
2234 if (Report.WarningLevel < 4)
2237 if ((f.caching_flags & Flags.IsAssigned) != 0)
2240 Constant c = New.Constantify (f.Type.Type);
2241 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2242 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2248 // TODO: move to ClassOrStruct
2249 void EmitConstructors ()
2251 if (instance_constructors == null)
2254 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2255 bool has_compliant_args = false;
2257 foreach (Constructor c in instance_constructors) {
2261 catch (Exception e) {
2262 throw new InternalErrorException (c, e);
2265 if (has_compliant_args)
2268 has_compliant_args = c.HasCompliantArgs;
2270 if (!has_compliant_args)
2271 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2273 foreach (Constructor c in instance_constructors) {
2277 catch (Exception e) {
2278 throw new InternalErrorException (c, e);
2285 /// Emits the code, this step is performed after all
2286 /// the types, enumerations, constructors
2288 public virtual void EmitType ()
2290 if (OptAttributes != null)
2291 OptAttributes.Emit ();
2295 int offset = CountTypeParameters - CurrentTypeParameters.Length;
2296 for (int i = offset; i < gen_params.Length; i++)
2297 CurrentTypeParameters [i - offset].Emit ();
2302 // Structs with no fields need to have at least one byte.
2303 // The right thing would be to set the PackingSize in a DefineType
2304 // but there are no functions that allow interfaces *and* the size to
2308 if (Kind == Kind.Struct && first_nonstatic_field == null){
2309 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2310 FieldAttributes.Private);
2312 if (HasExplicitLayout){
2313 object [] ctor_args = new object [1];
2316 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2317 TypeManager.field_offset_attribute_ctor, ctor_args);
2318 fb.SetCustomAttribute (cba);
2324 EmitConstructors ();
2326 // Can not continue if constants are broken
2328 if (Report.Errors > 0)
2331 if (default_static_constructor != null)
2332 default_static_constructor.Emit ();
2334 if (methods != null){
2335 for (int i = 0; i < methods.Count; ++i)
2336 ((Method)methods[i]).Emit ();
2339 if (operators != null)
2340 foreach (Operator o in operators)
2343 if (properties != null)
2344 foreach (Property p in properties)
2347 if (indexers != null) {
2348 foreach (Indexer indx in indexers)
2354 foreach (FieldBase f in fields)
2357 if (events != null){
2358 foreach (Event e in Events)
2362 if (delegates != null) {
2363 foreach (Delegate d in Delegates) {
2368 if (pending != null)
2369 if (pending.VerifyPendingMethods ())
2372 if (Report.Errors > 0)
2375 if (compiler_generated != null) {
2376 foreach (CompilerGeneratedClass c in compiler_generated) {
2377 if (!c.DefineMembers ())
2378 throw new InternalErrorException ();
2380 foreach (CompilerGeneratedClass c in compiler_generated)
2385 public override void CloseType ()
2387 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2391 caching_flags |= Flags.CloseTypeCreated;
2392 TypeBuilder.CreateType ();
2393 } catch (TypeLoadException){
2395 // This is fine, the code still created the type
2397 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2398 // Console.WriteLine (e.Message);
2399 } catch (Exception e) {
2400 throw new InternalErrorException (this, e);
2404 foreach (TypeContainer tc in Types)
2405 if (tc.Kind == Kind.Struct)
2408 foreach (TypeContainer tc in Types)
2409 if (tc.Kind != Kind.Struct)
2413 if (Delegates != null)
2414 foreach (Delegate d in Delegates)
2417 if (CompilerGenerated != null)
2418 foreach (CompilerGeneratedClass c in CompilerGenerated)
2425 initialized_fields = null;
2426 initialized_static_fields = null;
2428 ordered_explicit_member_list = null;
2429 ordered_member_list = null;
2434 compiler_generated = null;
2435 default_constructor = null;
2436 default_static_constructor = null;
2438 OptAttributes = null;
2441 member_cache = null;
2445 // Performs the validation on a Method's modifiers (properties have
2446 // the same properties).
2448 public bool MethodModifiersValid (MemberCore mc)
2450 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2451 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2452 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2454 int flags = mc.ModFlags;
2457 // At most one of static, virtual or override
2459 if ((flags & Modifiers.STATIC) != 0){
2460 if ((flags & vao) != 0){
2461 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2462 mc.GetSignatureForError ());
2467 if (Kind == Kind.Struct){
2468 if ((flags & va) != 0){
2469 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2474 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2475 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2476 mc.GetSignatureForError ());
2481 // If the declaration includes the abstract modifier, then the
2482 // declaration does not include static, virtual or extern
2484 if ((flags & Modifiers.ABSTRACT) != 0){
2485 if ((flags & Modifiers.EXTERN) != 0){
2487 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2491 if ((flags & Modifiers.SEALED) != 0) {
2492 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2496 if ((flags & Modifiers.VIRTUAL) != 0){
2497 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2501 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2502 Report.SymbolRelatedToPreviousError (this);
2503 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2504 mc.GetSignatureForError (), GetSignatureForError ());
2509 if ((flags & Modifiers.PRIVATE) != 0){
2510 if ((flags & vao) != 0){
2511 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2516 if ((flags & Modifiers.SEALED) != 0){
2517 if ((flags & Modifiers.OVERRIDE) == 0){
2518 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2526 public Constructor DefaultStaticConstructor {
2527 get { return default_static_constructor; }
2530 protected override bool VerifyClsCompliance ()
2532 if (!base.VerifyClsCompliance ())
2537 Type base_type = TypeBuilder.BaseType;
2538 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2539 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2546 /// Checks whether container name is CLS Compliant
2548 void VerifyClsName ()
2550 Hashtable base_members = base_cache == null ?
2552 base_cache.GetPublicMembers ();
2553 Hashtable this_members = new Hashtable ();
2555 foreach (DictionaryEntry entry in defined_names) {
2556 MemberCore mc = (MemberCore)entry.Value;
2557 if (!mc.IsClsComplianceRequired ())
2560 string name = (string) entry.Key;
2561 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2563 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2564 object found = base_members [lcase];
2565 if (found == null) {
2566 found = this_members [lcase];
2567 if (found == null) {
2568 this_members.Add (lcase, mc);
2573 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2576 if (found is MemberInfo) {
2577 if (basename == ((MemberInfo) found).Name)
2579 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2581 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2584 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2586 Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2593 /// Performs checks for an explicit interface implementation. First it
2594 /// checks whether the `interface_type' is a base inteface implementation.
2595 /// Then it checks whether `name' exists in the interface type.
2597 public virtual bool VerifyImplements (InterfaceMemberBase mb)
2599 if (ifaces != null) {
2600 foreach (Type t in ifaces){
2601 if (t == mb.InterfaceType)
2606 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2607 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2608 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2612 public override Type LookupAnyGeneric (string typeName)
2614 if (types != null) {
2615 foreach (TypeContainer tc in types) {
2619 int pos = tc.Basename.LastIndexOf ('`');
2620 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2621 return tc.TypeBuilder;
2625 return base.LookupAnyGeneric (typeName);
2628 public void Mark_HasEquals ()
2630 cached_method |= CachedMethods.Equals;
2633 public void Mark_HasGetHashCode ()
2635 cached_method |= CachedMethods.GetHashCode;
2639 /// Method container contains Equals method
2641 public bool HasEquals {
2643 return (cached_method & CachedMethods.Equals) != 0;
2648 /// Method container contains GetHashCode method
2650 public bool HasGetHashCode {
2652 return (cached_method & CachedMethods.GetHashCode) != 0;
2656 public bool HasStaticFieldInitializer {
2658 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2662 cached_method |= CachedMethods.HasStaticFieldInitializer;
2664 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2672 string IMemberContainer.Name {
2678 Type IMemberContainer.Type {
2684 bool IMemberContainer.IsInterface {
2686 return Kind == Kind.Interface;
2690 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2692 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2694 if (GenericType != null)
2695 return TypeManager.FindMembers (GenericType, mt, new_bf,
2698 return FindMembers (mt, new_bf, null, null);
2702 // Generates xml doc comments (if any), and if required,
2703 // handle warning report.
2705 internal override void GenerateDocComment (DeclSpace ds)
2707 DocUtil.GenerateTypeDocComment (this, ds);
2710 public override string DocCommentHeader {
2711 get { return "T:"; }
2714 public MemberCache BaseCache {
2716 if (base_cache != null)
2718 if (TypeBuilder.BaseType != null)
2719 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2720 if (TypeBuilder.IsInterface)
2721 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2727 public abstract class ClassOrStruct : TypeContainer {
2728 ListDictionary declarative_security;
2730 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2731 MemberName name, Attributes attrs, Kind kind)
2732 : base (ns, parent, name, attrs, kind)
2736 protected override bool AddToContainer (MemberCore symbol, string name)
2738 if (name == MemberName.Name) {
2739 if (symbol is TypeParameter) {
2740 Report.Error (694, symbol.Location,
2741 "Type parameter `{0}' has same name as " +
2742 "containing type, or method", name);
2746 Report.SymbolRelatedToPreviousError (this);
2747 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2748 symbol.GetSignatureForError ());
2752 return base.AddToContainer (symbol, name);
2755 public override void VerifyMembers ()
2757 base.VerifyMembers ();
2759 if ((events != null) && Report.WarningLevel >= 3) {
2760 foreach (Event e in events){
2761 // Note: The event can be assigned from same class only, so we can report
2762 // this warning for all accessibility modes
2763 if ((e.caching_flags & Flags.IsUsed) == 0)
2764 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2769 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2771 if (a.IsValidSecurityAttribute ()) {
2772 if (declarative_security == null)
2773 declarative_security = new ListDictionary ();
2775 a.ExtractSecurityPermissionSet (declarative_security);
2779 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2780 HasExplicitLayout = true;
2783 base.ApplyAttributeBuilder (a, cb);
2787 /// Defines the default constructors
2789 protected void DefineDefaultConstructor (bool is_static)
2791 // The default instance constructor is public
2792 // If the class is abstract, the default constructor is protected
2793 // The default static constructor is private
2797 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2799 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2802 Constructor c = new Constructor (this, MemberName.Name, mods,
2803 Parameters.EmptyReadOnlyParameters,
2804 new GeneratedBaseInitializer (Location),
2808 c.Block = new ToplevelBlock (null, Location);
2811 public override bool Define ()
2813 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer)
2814 DefineDefaultConstructor (true);
2816 if (default_static_constructor != null)
2817 default_static_constructor.Define ();
2819 return base.Define ();
2822 public override void Emit ()
2826 if (declarative_security != null) {
2827 foreach (DictionaryEntry de in declarative_security) {
2828 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2833 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name)
2835 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name);
2838 protected override TypeAttributes TypeAttr {
2840 if (default_static_constructor == null)
2841 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2843 return base.TypeAttr;
2849 // TODO: should be sealed
2850 public class Class : ClassOrStruct {
2851 const int AllowedModifiers =
2854 Modifiers.PROTECTED |
2855 Modifiers.INTERNAL |
2857 Modifiers.ABSTRACT |
2862 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2864 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2866 : base (ns, parent, name, attrs, Kind.Class)
2868 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2869 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2871 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2872 Report.FeatureIsNotAvailable (Location, "static classes");
2876 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2878 if (part.Name == "System.Object")
2879 Report.Error (537, part.Location,
2880 "The class System.Object cannot have a base class or implement an interface.");
2881 base.AddBasesForPart (part, bases);
2884 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2886 if (a.Type == TypeManager.attribute_usage_type) {
2887 if (!TypeManager.IsAttributeType (BaseType) &&
2888 TypeBuilder.FullName != "System.Attribute") {
2889 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2893 if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
2894 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2898 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
2899 !attributes.Contains (TypeManager.guid_attr_type)) {
2900 a.Error_MissingGuidAttribute ();
2904 if (a.Type == TypeManager.extension_attribute_type) {
2905 a.Error_MisusedExtensionAttribute ();
2909 if (AttributeTester.IsAttributeExcluded (a.Type))
2912 base.ApplyAttributeBuilder (a, cb);
2915 public override AttributeTargets AttributeTargets {
2917 return AttributeTargets.Class;
2921 protected override void DefineContainerMembers (MemberCoreArrayList list)
2927 base.DefineContainerMembers (list);
2931 foreach (MemberCore m in list) {
2932 if (m is Operator) {
2933 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2937 if (m is Destructor) {
2938 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2942 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2943 Report.Error (1057, m.Location, "`{0}': Static classes cannot contain protected members", m.GetSignatureForError ());
2948 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2952 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2955 if (m is Constructor) {
2956 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2960 Method method = m as Method;
2961 if (method != null && method.Parameters.HasExtensionMethodType) {
2962 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2966 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2969 base.DefineContainerMembers (list);
2972 public override TypeBuilder DefineType ()
2974 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2975 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2979 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2980 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2984 return base.DefineType ();
2987 protected override bool DoDefineMembers ()
2989 if (InstanceConstructors == null && !IsStatic)
2990 DefineDefaultConstructor (false);
2992 return base.DoDefineMembers ();
2995 public override void Emit ()
3000 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
3001 TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
3005 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
3007 TypeExpr[] ifaces = base.GetClassBases (out base_class);
3009 if (base_class == null) {
3010 if (RootContext.StdLib)
3011 base_class = TypeManager.system_object_expr;
3012 else if (Name != "System.Object")
3013 base_class = TypeManager.system_object_expr;
3015 if (Kind == Kind.Class && base_class is TypeParameterExpr){
3017 689, base_class.Location,
3018 "Cannot derive from `{0}' because it is a type parameter",
3019 base_class.GetSignatureForError ());
3023 if (base_class.Type.IsArray || base_class.Type.IsPointer) {
3024 Report.Error (1521, base_class.Location, "Invalid base type");
3028 if (base_class.IsSealed){
3029 Report.SymbolRelatedToPreviousError (base_class.Type);
3030 if (base_class.Type.IsAbstract) {
3031 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
3032 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
3034 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
3035 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
3040 if (!base_class.CanInheritFrom ()){
3041 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
3042 GetSignatureForError (), base_class.GetSignatureForError ());
3046 if (!base_class.AsAccessible (this)) {
3047 Report.SymbolRelatedToPreviousError (base_class.Type);
3048 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
3049 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
3053 if (PartialContainer.IsStaticClass) {
3054 if (base_class.Type != TypeManager.object_type) {
3055 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
3056 GetSignatureForError (), base_class.GetSignatureForError ());
3060 if (ifaces != null) {
3061 foreach (TypeExpr t in ifaces)
3062 Report.SymbolRelatedToPreviousError (t.Type);
3063 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
3070 /// Search for at least one defined condition in ConditionalAttribute of attribute class
3071 /// Valid only for attribute classes.
3072 public bool IsExcluded ()
3074 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3075 return (caching_flags & Flags.Excluded) != 0;
3077 caching_flags &= ~Flags.Excluded_Undetected;
3079 if (OptAttributes == null)
3082 if (TypeManager.conditional_attribute_type == null)
3085 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3090 foreach (Attribute a in attrs) {
3091 string condition = a.GetConditionalAttributeValue ();
3092 if (RootContext.AllDefines.Contains (condition))
3096 caching_flags |= Flags.Excluded;
3102 return (ModFlags & Modifiers.STATIC) != 0;
3107 // FIXME: How do we deal with the user specifying a different
3110 protected override TypeAttributes TypeAttr {
3112 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
3114 ta |= StaticClassAttribute;
3120 public sealed class Struct : ClassOrStruct {
3122 // Modifiers allowed in a struct declaration
3124 const int AllowedModifiers =
3127 Modifiers.PROTECTED |
3128 Modifiers.INTERNAL |
3132 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
3133 int mod, Attributes attrs)
3134 : base (ns, parent, name, attrs, Kind.Struct)
3138 if (parent.Parent == null)
3139 accmods = Modifiers.INTERNAL;
3141 accmods = Modifiers.PRIVATE;
3143 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
3145 this.ModFlags |= Modifiers.SEALED;
3148 public override AttributeTargets AttributeTargets {
3150 return AttributeTargets.Struct;
3154 const TypeAttributes DefaultTypeAttributes =
3155 TypeAttributes.SequentialLayout |
3156 TypeAttributes.Sealed |
3157 TypeAttributes.BeforeFieldInit;
3160 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
3162 TypeExpr[] ifaces = base.GetClassBases (out base_class);
3164 // If we are compiling our runtime,
3165 // and we are defining ValueType, then our
3166 // base is `System.Object'.
3168 if (base_class == null) {
3169 if (!RootContext.StdLib && Name == "System.ValueType")
3170 base_class = TypeManager.system_object_expr;
3172 base_class = TypeManager.system_valuetype_expr;
3179 // FIXME: Allow the user to specify a different set of attributes
3180 // in some cases (Sealed for example is mandatory for a class,
3181 // but what SequentialLayout can be changed
3183 protected override TypeAttributes TypeAttr {
3185 return base.TypeAttr | DefaultTypeAttributes;
3189 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3191 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3192 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3193 field.GetSignatureForError ());
3196 base.RegisterFieldForInitialization (field, expression);
3204 public sealed class Interface : TypeContainer, IMemberContainer {
3207 /// Modifiers allowed in a class declaration
3209 public const int AllowedModifiers =
3212 Modifiers.PROTECTED |
3213 Modifiers.INTERNAL |
3217 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3219 : base (ns, parent, name, attrs, Kind.Interface)
3223 if (parent.Parent == null)
3224 accmods = Modifiers.INTERNAL;
3226 accmods = Modifiers.PRIVATE;
3228 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3231 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3233 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
3234 !attributes.Contains (TypeManager.guid_attr_type)) {
3235 a.Error_MissingGuidAttribute ();
3238 base.ApplyAttributeBuilder (a, cb);
3242 public override AttributeTargets AttributeTargets {
3244 return AttributeTargets.Interface;
3248 const TypeAttributes DefaultTypeAttributes =
3249 TypeAttributes.AutoLayout |
3250 TypeAttributes.Abstract |
3251 TypeAttributes.Interface;
3253 protected override TypeAttributes TypeAttr {
3255 return base.TypeAttr | DefaultTypeAttributes;
3259 protected override bool VerifyClsCompliance ()
3261 if (!base.VerifyClsCompliance ())
3264 if (ifaces != null) {
3265 foreach (Type t in ifaces) {
3266 if (AttributeTester.IsClsCompliant (t))
3269 Report.SymbolRelatedToPreviousError (t);
3270 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3271 GetSignatureForError (), TypeManager.CSharpName (t));
3279 // It is used as a base class for all property based members
3280 // This includes properties, indexers, and events
3281 public abstract class PropertyBasedMember : InterfaceMemberBase
3283 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3284 Expression type, int mod, int allowed_mod, bool is_iface,
3285 MemberName name, Attributes attrs)
3286 : base (parent, generic, type, mod, allowed_mod, is_iface, name, attrs)
3290 protected override bool VerifyClsCompliance ()
3292 if (!base.VerifyClsCompliance ())
3295 if (!AttributeTester.IsClsCompliant (MemberType)) {
3296 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3297 GetSignatureForError ());
3305 public abstract class MethodCore : InterfaceMemberBase
3307 public readonly Parameters Parameters;
3308 protected ToplevelBlock block;
3310 public MethodCore (DeclSpace parent, GenericMethod generic,
3311 Expression type, int mod, int allowed_mod, bool is_iface,
3312 MemberName name, Attributes attrs, Parameters parameters)
3313 : base (parent, generic, type, mod, allowed_mod, is_iface, name, attrs)
3315 Parameters = parameters;
3319 // Returns the System.Type array for the parameters of this method
3321 public Type [] ParameterTypes {
3323 return Parameters.Types;
3327 public Parameters ParameterInfo
3334 public ToplevelBlock Block {
3344 protected override bool CheckBase ()
3346 // Check whether arguments were correct.
3347 if (!DefineParameters (Parameters))
3350 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
3351 if (!Parent.MemberCache.CheckExistingMembersOverloads (this,
3352 MemberName.IsGeneric ? MemberName.Basename : MemberName.MethodName, Parameters))
3355 // TODO: Find a better way how to check reserved accessors collision
3356 Method m = this as Method;
3358 if (!m.CheckForDuplications ())
3363 if (!base.CheckBase ())
3370 // Returns a string that represents the signature for this
3371 // member which should be used in XML documentation.
3373 public override string GetDocCommentName (DeclSpace ds)
3375 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3379 // Raised (and passed an XmlElement that contains the comment)
3380 // when GenerateDocComment is writing documentation expectedly.
3382 // FIXME: with a few effort, it could be done with XmlReader,
3383 // that means removal of DOM use.
3385 internal override void OnGenerateDocComment (XmlElement el)
3387 DocUtil.OnMethodGenerateDocComment (this, el);
3391 // Represents header string for documentation comment.
3393 public override string DocCommentHeader
3395 get { return "M:"; }
3398 public override bool EnableOverloadChecks (MemberCore overload)
3400 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3401 caching_flags |= Flags.MethodOverloadsExist;
3408 public virtual void SetYields ()
3410 ModFlags |= Modifiers.METHOD_YIELDS;
3413 protected override bool VerifyClsCompliance ()
3415 if (!base.VerifyClsCompliance ())
3418 if (Parameters.HasArglist) {
3419 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3422 if (!AttributeTester.IsClsCompliant (MemberType)) {
3423 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3424 GetSignatureForError ());
3427 Parameters.VerifyClsCompliance ();
3433 public abstract class InterfaceMemberBase : MemberBase {
3435 // Whether this is an interface member.
3437 public bool IsInterface;
3440 // If true, this is an explicit interface implementation
3442 public bool IsExplicitImpl;
3444 protected bool is_external_implementation;
3447 // The interface type we are explicitly implementing
3449 public Type InterfaceType;
3452 // The method we're overriding if this is an override method.
3454 protected MethodInfo base_method;
3456 readonly int explicit_mod_flags;
3457 public MethodAttributes flags;
3459 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3460 Expression type, int mod, int allowed_mod, bool is_iface,
3461 MemberName name, Attributes attrs)
3462 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3465 IsInterface = is_iface;
3466 IsExplicitImpl = (MemberName.Left != null);
3467 explicit_mod_flags = mod;
3470 protected override bool CheckBase ()
3472 if (!base.CheckBase ())
3478 // Is null for System.Object while compiling corlib and base interfaces
3479 if (Parent.PartialContainer.BaseCache == null) {
3480 if ((ModFlags & Modifiers.NEW) != 0) {
3481 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3486 Type base_ret_type = null;
3487 base_method = FindOutBaseMethod (ref base_ret_type);
3489 // method is override
3490 if (base_method != null) {
3491 if (!CheckMethodAgainstBase (base_ret_type))
3494 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3495 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3497 if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3498 Report.SymbolRelatedToPreviousError (base_method);
3499 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3500 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3503 if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3504 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3505 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3512 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3513 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3514 if (conflict_symbol != null) {
3515 Report.SymbolRelatedToPreviousError (conflict_symbol);
3517 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3518 else if (this is PropertyBase)
3519 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3521 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3523 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3524 GetSignatureForError (), SimpleName.GetMemberType (this));
3529 if (conflict_symbol == null) {
3530 if ((ModFlags & Modifiers.NEW) != 0) {
3531 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3536 if ((ModFlags & Modifiers.NEW) == 0) {
3537 if (this is Method && conflict_symbol is MethodBase)
3540 Report.SymbolRelatedToPreviousError (conflict_symbol);
3541 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3542 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3549 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3550 // that have been defined.
3552 // `name' is the user visible name for reporting errors (this is used to
3553 // provide the right name regarding method names and properties)
3555 bool CheckMethodAgainstBase (Type base_method_type)
3559 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3560 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3561 Report.Error (506, Location,
3562 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3563 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3567 // Now we check that the overriden method is not final
3569 if (base_method.IsFinal) {
3570 Report.SymbolRelatedToPreviousError (base_method);
3571 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3572 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3576 // Check that the permissions are not being changed
3578 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3579 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3581 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3582 Error_CannotChangeAccessModifiers (base_method, base_classp, null);
3586 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3587 Report.SymbolRelatedToPreviousError (base_method);
3588 if (this is PropertyBasedMember) {
3589 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3590 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3593 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3594 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3600 if ((ModFlags & Modifiers.NEW) == 0) {
3601 if ((ModFlags & Modifiers.OVERRIDE) == 0 && Name != "Finalize") {
3602 ModFlags |= Modifiers.NEW;
3603 Report.SymbolRelatedToPreviousError (base_method);
3604 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3605 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",
3606 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3608 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3609 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3613 if (base_method.IsAbstract && !IsInterface) {
3614 Report.SymbolRelatedToPreviousError (base_method);
3615 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3616 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3624 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3626 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3628 // when overriding protected internal, the method can be declared
3629 // protected internal only within the same assembly or assembly
3630 // which has InternalsVisibleTo
3632 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3633 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly)
3634 return TypeManager.IsFriendAssembly (base_method.DeclaringType.Assembly);
3636 if (thisp != base_classp) {
3638 // same assembly, but other attributes differ - report an error
3643 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3645 // if it's not "protected internal", it must be "protected"
3649 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3651 // protected within the same assembly - an error
3654 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3655 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3657 // protected ok, but other attributes differ - report an error
3663 return (thisp == base_classp);
3667 protected bool DefineParameters (Parameters parameters)
3669 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3671 if (!parameters.Resolve (rc))
3675 foreach (Parameter p in parameters.FixedParameters) {
3676 if (p.CheckAccessibility (this))
3679 Report.SymbolRelatedToPreviousError (p.ParameterType);
3680 if (this is Indexer)
3681 Report.Error (55, Location,
3682 "Inconsistent accessibility: parameter type `" +
3683 TypeManager.CSharpName (p.ParameterType) + "' is less " +
3684 "accessible than indexer `" + GetSignatureForError () + "'");
3685 else if (this is Operator)
3686 Report.Error (57, Location,
3687 "Inconsistent accessibility: parameter type `" +
3688 TypeManager.CSharpName (p.ParameterType) + "' is less " +
3689 "accessible than operator `" + GetSignatureForError () + "'");
3691 Report.Error (51, Location,
3692 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3693 TypeManager.CSharpName (p.ParameterType), GetSignatureForError ());
3699 protected override bool DoDefine()
3701 if (!base.DoDefine ())
3704 if (IsExplicitImpl) {
3705 Expression expr = MemberName.Left.GetTypeExpression ();
3706 TypeExpr texpr = expr.ResolveAsTypeTerminal (this, false);
3710 InterfaceType = texpr.Type;
3712 if (!InterfaceType.IsInterface) {
3713 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3717 if (!Parent.PartialContainer.VerifyImplements (this))
3724 protected virtual bool DoDefineBase ()
3727 throw new InternalErrorException ();
3730 ModFlags = Modifiers.PUBLIC |
3731 Modifiers.ABSTRACT |
3732 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
3734 flags = MethodAttributes.Public |
3735 MethodAttributes.Abstract |
3736 MethodAttributes.HideBySig |
3737 MethodAttributes.NewSlot |
3738 MethodAttributes.Virtual;
3740 if (!Parent.PartialContainer.MethodModifiersValid (this))
3743 flags = Modifiers.MethodAttr (ModFlags);
3746 if (IsExplicitImpl) {
3747 Expression expr = MemberName.Left.GetTypeExpression ();
3748 TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (this, false);
3749 if (iface_texpr == null)
3752 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3753 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3754 GetSignatureForError ());
3758 InterfaceType = iface_texpr.Type;
3760 if (!InterfaceType.IsInterface) {
3761 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3765 if (!Parent.PartialContainer.VerifyImplements (this))
3768 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3774 public override void Emit()
3776 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3777 // We are more strict than Microsoft and report CS0626 as error
3778 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3779 Report.Error (626, Location,
3780 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3781 GetSignatureForError ());
3787 protected void Error_CannotChangeAccessModifiers (MemberInfo base_method, MethodAttributes ma, string suffix)
3789 Report.SymbolRelatedToPreviousError (base_method);
3790 string base_name = TypeManager.GetFullNameSignature (base_method);
3791 string this_name = GetSignatureForError ();
3792 if (suffix != null) {
3793 base_name += suffix;
3794 this_name += suffix;
3797 Report.Error (507, Location, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3798 this_name, Modifiers.GetDescription (ma), base_name);
3801 protected static string Error722 {
3803 return "`{0}': static types cannot be used as return types";
3808 /// Gets base method and its return type
3810 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3813 // The "short" name of this property / indexer / event. This is the
3814 // name without the explicit interface.
3816 public string ShortName
3818 get { return MemberName.Name; }
3819 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3822 public string FullName {
3824 if (!IsExplicitImpl)
3827 return InterfaceType.FullName.Replace ('+', '.') + "." + ShortName;
3831 protected override bool VerifyClsCompliance ()
3833 if (!base.VerifyClsCompliance ()) {
3834 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3835 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3838 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3839 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3844 if (GenericMethod != null)
3845 GenericMethod.VerifyClsCompliance ();
3850 public override bool IsUsed
3852 get { return IsExplicitImpl || base.IsUsed; }
3857 public abstract class MethodOrOperator : MethodCore, IMethodData
3859 public MethodBuilder MethodBuilder;
3860 ReturnParameter return_attributes;
3861 ListDictionary declarative_security;
3862 protected MethodData MethodData;
3865 ArrayList anonymous_methods;
3867 static string[] attribute_targets = new string [] { "method", "return" };
3869 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, Expression type, int mod,
3870 int allowed_mod, bool is_interface, MemberName name,
3871 Attributes attrs, Parameters parameters)
3872 : base (parent, generic, type, mod, allowed_mod, is_interface, name,
3877 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3879 if (a.Target == AttributeTargets.ReturnValue) {
3880 if (return_attributes == null)
3881 return_attributes = new ReturnParameter (MethodBuilder, Location);
3883 return_attributes.ApplyAttributeBuilder (a, cb);
3887 if (a.IsInternalMethodImplAttribute) {
3888 is_external_implementation = true;
3891 if (a.Type == TypeManager.dllimport_type) {
3892 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3893 if ((ModFlags & extern_static) != extern_static) {
3894 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3896 is_external_implementation = true;
3899 if (a.IsValidSecurityAttribute ()) {
3900 if (declarative_security == null)
3901 declarative_security = new ListDictionary ();
3902 a.ExtractSecurityPermissionSet (declarative_security);
3906 if (MethodBuilder != null)
3907 MethodBuilder.SetCustomAttribute (cb);
3910 public override AttributeTargets AttributeTargets {
3912 return AttributeTargets.Method;
3916 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3918 return new EmitContext (
3919 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3922 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
3924 if (anonymous_methods == null)
3925 anonymous_methods = new ArrayList ();
3926 anonymous_methods.Add (anonymous);
3929 protected bool DefineGenericMethod ()
3931 if (!DoDefineBase ())
3935 if (GenericMethod != null) {
3938 MethodBuilder = Parent.TypeBuilder.DefineMethod (FullName, flags); //, ReturnType, null);
3940 MethodBuilder = Parent.TypeBuilder.DefineMethod (FullName, flags);
3943 if (!GenericMethod.Define (MethodBuilder, block))
3951 public bool ResolveMembers ()
3953 if (!DefineGenericMethod ())
3956 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0) {
3957 iterator = Iterator.CreateIterator (this, Parent, GenericMethod, ModFlags);
3958 if (iterator == null)
3962 if (anonymous_methods != null) {
3963 foreach (AnonymousMethodExpression ame in anonymous_methods) {
3964 if (!ame.CreateAnonymousHelpers ())
3972 public override bool Define ()
3977 if (!CheckAbstractAndExtern (block != null))
3980 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3981 for (int i = 0; i < Parameters.Count; ++i ) {
3982 if (Parameters.ParameterModifier (i) == Parameter.Modifier.OUT) {
3983 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3984 GetSignatureForError ());
3993 if (IsPartialDefinition) {
3994 caching_flags &= ~Flags.Excluded_Undetected;
3995 caching_flags |= Flags.Excluded;
3996 // Add to member cache only when a partial method implementation is not there
3997 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3998 MethodBase mb = new PartialMethodDefinitionInfo (this);
3999 Parent.MemberCache.AddMember (mb, this);
4000 TypeManager.AddMethod (mb, this);
4006 MethodData = new MethodData (
4007 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
4009 if (!MethodData.Define (Parent.PartialContainer))
4012 MethodBuilder = MethodData.MethodBuilder;
4015 if (MethodBuilder.IsGenericMethod)
4016 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
4019 Parent.MemberCache.AddMember (MethodBuilder, this);
4021 if (!TypeManager.IsGenericParameter (MemberType)) {
4022 if (MemberType.IsAbstract && MemberType.IsSealed) {
4023 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
4031 public override void Emit ()
4034 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0)
4035 MethodBuilder.SetCustomAttribute (TypeManager.compiler_generated_attr);
4037 if (OptAttributes != null)
4038 OptAttributes.Emit ();
4040 if (declarative_security != null) {
4041 foreach (DictionaryEntry de in declarative_security) {
4042 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4049 protected void Error_ConditionalAttributeIsNotValid ()
4051 Report.Error (577, Location,
4052 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
4053 GetSignatureForError ());
4056 public bool IsPartialDefinition {
4058 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
4062 public bool IsPartialImplementation {
4064 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
4068 public override string[] ValidAttributeTargets {
4070 return attribute_targets;
4074 #region IMethodData Members
4076 public CallingConventions CallingConventions {
4078 CallingConventions cc = Parameters.CallingConvention;
4079 if (Parameters.HasArglist && block != null)
4080 block.HasVarargs = true;
4083 if ((ModFlags & Modifiers.STATIC) == 0)
4084 cc |= CallingConventions.HasThis;
4086 // FIXME: How is `ExplicitThis' used in C#?
4092 public Type ReturnType {
4098 public MemberName MethodName {
4104 public Iterator Iterator {
4105 get { return iterator; }
4108 public new Location Location {
4110 return base.Location;
4114 protected override bool CheckBase ()
4116 if (!base.CheckBase ())
4119 // TODO: Destructor should derive from MethodCore
4120 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
4121 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
4122 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
4130 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
4132 public bool IsExcluded () {
4133 if ((caching_flags & Flags.Excluded_Undetected) == 0)
4134 return (caching_flags & Flags.Excluded) != 0;
4136 caching_flags &= ~Flags.Excluded_Undetected;
4138 if (base_method == null) {
4139 if (OptAttributes == null)
4142 if (TypeManager.conditional_attribute_type == null)
4145 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
4150 foreach (Attribute a in attrs) {
4151 string condition = a.GetConditionalAttributeValue ();
4152 if (condition == null)
4155 if (RootContext.AllDefines.Contains (condition))
4159 caching_flags |= Flags.Excluded;
4163 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
4165 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
4166 caching_flags |= Flags.Excluded;
4172 if (md.IsExcluded ()) {
4173 caching_flags |= Flags.Excluded;
4179 GenericMethod IMethodData.GenericMethod {
4181 return GenericMethod;
4189 public class SourceMethod : ISourceMethod
4194 protected SourceMethod (DeclSpace parent, MethodBase builder,
4195 ISourceFile file, Location start, Location end)
4197 this.parent = parent;
4198 this.builder = builder;
4200 CodeGen.SymbolWriter.OpenMethod (file, this, start.Row, start.Column, end.Row, start.Column);
4203 public string Name {
4204 get { return builder.Name; }
4207 public int NamespaceID {
4208 get { return parent.NamespaceEntry.SymbolFileID; }
4213 if (builder is MethodBuilder)
4214 return ((MethodBuilder) builder).GetToken ().Token;
4215 else if (builder is ConstructorBuilder)
4216 return ((ConstructorBuilder) builder).GetToken ().Token;
4218 throw new NotSupportedException ();
4222 public void CloseMethod ()
4224 if (CodeGen.SymbolWriter != null)
4225 CodeGen.SymbolWriter.CloseMethod ();
4228 public static SourceMethod Create (DeclSpace parent, MethodBase builder, Block block)
4230 if (CodeGen.SymbolWriter == null)
4235 Location start_loc = block.StartLocation;
4236 if (start_loc.IsNull)
4239 Location end_loc = block.EndLocation;
4243 ISourceFile file = start_loc.SourceFile;
4247 return new SourceMethod (
4248 parent, builder, file, start_loc, end_loc);
4252 public class Method : MethodOrOperator, IAnonymousHost {
4255 /// Modifiers allowed in a class declaration
4257 const int AllowedModifiers =
4260 Modifiers.PROTECTED |
4261 Modifiers.INTERNAL |
4266 Modifiers.OVERRIDE |
4267 Modifiers.ABSTRACT |
4269 Modifiers.METHOD_YIELDS |
4272 const int AllowedInterfaceModifiers =
4273 Modifiers.NEW | Modifiers.UNSAFE;
4276 // return_type can be "null" for VOID values.
4278 public Method (DeclSpace parent, GenericMethod generic,
4279 Expression return_type, int mod, bool is_iface,
4280 MemberName name, Parameters parameters, Attributes attrs)
4281 : base (parent, generic, return_type, mod,
4282 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
4283 is_iface, name, attrs, parameters)
4287 public override string GetSignatureForError()
4289 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4292 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
4294 Report.Error (17, location,
4295 "Program `{0}' has more than one entry point defined: `{1}'",
4296 CodeGen.FileName, TypeManager.CSharpSignature(b));
4299 bool IsEntryPoint ()
4301 if (ReturnType != TypeManager.void_type &&
4302 ReturnType != TypeManager.int32_type)
4305 if (Parameters.Count == 0)
4308 if (Parameters.Count > 1)
4311 Type t = Parameters.ParameterType (0);
4312 return t.IsArray && t.GetArrayRank () == 1 &&
4313 TypeManager.GetElementType (t) == TypeManager.string_type &&
4314 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4317 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4319 if (a.Type == TypeManager.conditional_attribute_type) {
4320 if (IsExplicitImpl) {
4321 Error_ConditionalAttributeIsNotValid ();
4325 if (ReturnType != TypeManager.void_type) {
4326 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4330 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4331 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4336 Report.Error (582, Location, "Conditional not valid on interface members");
4340 if (MethodData.implementing != null) {
4341 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4342 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4343 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4347 for (int i = 0; i < Parameters.Count; ++i) {
4348 if ((Parameters.ParameterModifier (i) & Parameter.Modifier.OUTMASK) != 0) {
4349 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4355 if (a.Type == TypeManager.extension_attribute_type) {
4356 a.Error_MisusedExtensionAttribute ();
4360 base.ApplyAttributeBuilder (a, cb);
4363 public bool CheckForDuplications ()
4365 ArrayList ar = Parent.PartialContainer.Properties;
4367 for (int i = 0; i < ar.Count; ++i) {
4368 PropertyBase pb = (PropertyBase) ar [i];
4369 if (pb.AreAccessorsDuplicateImplementation (this))
4374 ar = Parent.PartialContainer.Indexers;
4376 for (int i = 0; i < ar.Count; ++i) {
4377 PropertyBase pb = (PropertyBase) ar [i];
4378 if (pb.AreAccessorsDuplicateImplementation (this))
4389 public override bool Define ()
4391 if (!base.Define ())
4394 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
4395 Error1599 (Location, ReturnType);
4399 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
4400 Name == "Finalize" && !(this is Destructor)) {
4401 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4404 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4405 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4406 Parent.PartialContainer.Mark_HasEquals ();
4407 else if (Parameters.Empty && Name == "GetHashCode")
4408 Parent.PartialContainer.Mark_HasGetHashCode ();
4411 if ((ModFlags & Modifiers.STATIC) == 0)
4414 if (Parameters.HasExtensionMethodType) {
4415 if (Parent.IsStaticClass && !Parent.IsGeneric) {
4416 if (!Parent.IsTopLevel)
4417 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4418 GetSignatureForError ());
4420 if (TypeManager.extension_attribute_type == null)
4421 Report.Error (1110, Location,
4422 "`{0}': Extension methods cannot be declared without a reference to System.Core.dll assembly. Add the assembly reference or remove `this' modifer from the first parameter",
4423 GetSignatureForError ());
4425 ModFlags |= Modifiers.METHOD_EXTENSION;
4426 Parent.ModFlags |= Modifiers.METHOD_EXTENSION;
4427 CodeGen.Assembly.HasExtensionMethods = true;
4429 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4430 GetSignatureForError ());
4435 // This is used to track the Entry Point,
4437 if (RootContext.NeedsEntryPoint &&
4439 (RootContext.MainClass == null ||
4440 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4441 if (IsEntryPoint ()) {
4443 if (RootContext.EntryPoint == null) {
4444 if (Parent.IsGeneric || MemberName.IsGeneric) {
4445 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4446 GetSignatureForError ());
4448 IMethodData md = TypeManager.GetMethod (MethodBuilder);
4449 md.SetMemberIsUsed ();
4451 RootContext.EntryPoint = MethodBuilder;
4452 RootContext.EntryPointLocation = Location;
4455 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
4456 Error_DuplicateEntryPoint (MethodBuilder, Location);
4459 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4460 GetSignatureForError ());
4470 public override void Emit ()
4473 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4474 if (IsPartialDefinition) {
4476 // Do attribute checks only when partial implementation does not exist
4478 if (MethodBuilder == null)
4484 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
4485 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4486 GetSignatureForError ());
4488 MethodData.Emit (Parent);
4492 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4493 MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
4499 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4500 Location, MethodBuilder);
4505 public override bool EnableOverloadChecks (MemberCore overload)
4507 // TODO: It can be deleted when members will be defined in correct order
4508 if (overload is Operator)
4509 return overload.EnableOverloadChecks (this);
4511 if (overload is Indexer)
4514 return base.EnableOverloadChecks (overload);
4517 public static void Error1599 (Location loc, Type t)
4519 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4522 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4524 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4525 Parent.TypeBuilder, Name, ParameterTypes, GenericMethod, false);
4530 if (mi.IsSpecialName)
4533 base_ret_type = mi.ReturnType;
4537 public void SetPartialDefinition (Method methodDefinition)
4539 caching_flags |= Flags.PartialDefinitionExists;
4540 methodDefinition.MethodBuilder = MethodBuilder;
4541 if (methodDefinition.attributes == null)
4544 if (attributes == null) {
4545 attributes = methodDefinition.attributes;
4547 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4551 protected override bool VerifyClsCompliance ()
4553 if (!base.VerifyClsCompliance ())
4556 if (ParameterInfo.Count > 0) {
4557 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4559 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4566 public abstract class ConstructorInitializer : Expression
4568 ArrayList argument_list;
4569 MethodGroupExpr base_constructor_group;
4571 public ConstructorInitializer (ArrayList argument_list, Location loc)
4573 this.argument_list = argument_list;
4577 public ArrayList Arguments {
4579 return argument_list;
4583 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4585 if (argument_list != null){
4586 foreach (Argument a in argument_list){
4587 if (!a.Resolve (ec, loc))
4592 if (this is ConstructorBaseInitializer) {
4593 if (ec.ContainerType.BaseType == null)
4596 type = ec.ContainerType.BaseType;
4597 if (ec.ContainerType.IsValueType) {
4598 Report.Error (522, loc,
4599 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4604 // It is legal to have "this" initializers that take no arguments
4605 // in structs, they are just no-ops.
4607 // struct D { public D (int a) : this () {}
4609 if (ec.ContainerType.IsValueType && argument_list == null)
4612 type = ec.ContainerType;
4615 base_constructor_group = MemberLookupFinal (
4616 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4617 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4618 loc) as MethodGroupExpr;
4620 if (base_constructor_group == null)
4623 base_constructor_group = base_constructor_group.OverloadResolve (
4624 ec, ref argument_list, false, loc);
4626 if (base_constructor_group == null)
4629 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4631 if (base_ctor == caller_builder){
4632 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4638 public override Expression DoResolve (EmitContext ec)
4640 throw new NotSupportedException ();
4643 public override void Emit (EmitContext ec)
4645 // It can be null for static initializers
4646 if (base_constructor_group == null)
4649 ec.Mark (loc, false);
4651 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4653 base_constructor_group.EmitCall (ec, argument_list);
4657 public class ConstructorBaseInitializer : ConstructorInitializer {
4658 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4659 base (argument_list, l)
4664 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4665 public GeneratedBaseInitializer (Location loc):
4671 public class ConstructorThisInitializer : ConstructorInitializer {
4672 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4673 base (argument_list, l)
4678 public class Constructor : MethodCore, IMethodData, IAnonymousHost {
4679 public ConstructorBuilder ConstructorBuilder;
4680 public ConstructorInitializer Initializer;
4681 ListDictionary declarative_security;
4682 ArrayList anonymous_methods;
4683 bool has_compliant_args;
4686 // Modifiers allowed for a constructor.
4688 public const int AllowedModifiers =
4690 Modifiers.PROTECTED |
4691 Modifiers.INTERNAL |
4697 static readonly string[] attribute_targets = new string [] { "method" };
4700 // The spec claims that static is not permitted, but
4701 // my very own code has static constructors.
4703 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4704 ConstructorInitializer init, Location loc)
4705 : base (parent, null, null, mod, AllowedModifiers, false,
4706 new MemberName (name, loc), null, args)
4711 public bool HasCompliantArgs {
4712 get { return has_compliant_args; }
4715 public override AttributeTargets AttributeTargets {
4716 get { return AttributeTargets.Constructor; }
4719 public Iterator Iterator {
4720 get { return null; }
4724 // Returns true if this is a default constructor
4726 public bool IsDefault ()
4728 if ((ModFlags & Modifiers.STATIC) != 0)
4729 return Parameters.Empty;
4731 return Parameters.Empty &&
4732 (Initializer is ConstructorBaseInitializer) &&
4733 (Initializer.Arguments == null);
4736 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4738 if (a.IsValidSecurityAttribute ()) {
4739 if (declarative_security == null) {
4740 declarative_security = new ListDictionary ();
4742 a.ExtractSecurityPermissionSet (declarative_security);
4746 if (a.IsInternalMethodImplAttribute) {
4747 is_external_implementation = true;
4750 ConstructorBuilder.SetCustomAttribute (cb);
4753 public void AddAnonymousMethod (AnonymousMethodExpression anonymous)
4755 if (anonymous_methods == null)
4756 anonymous_methods = new ArrayList ();
4757 anonymous_methods.Add (anonymous);
4760 public bool ResolveMembers ()
4762 if (anonymous_methods != null) {
4763 foreach (AnonymousMethodExpression ame in anonymous_methods) {
4764 if (!ame.CreateAnonymousHelpers ())
4772 protected override bool CheckBase ()
4774 if ((ModFlags & Modifiers.STATIC) != 0) {
4775 if (!Parameters.Empty) {
4776 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4777 GetSignatureForError ());
4781 // the rest can be ignored
4785 // Check whether arguments were correct.
4786 if (!DefineParameters (Parameters))
4789 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4790 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorBuilder.ConstructorName,
4793 if (Parent.PartialContainer.Kind == Kind.Struct) {
4794 if (ParameterTypes.Length == 0) {
4795 Report.Error (568, Location,
4796 "Structs cannot contain explicit parameterless constructors");
4800 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4801 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
4806 if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0) {
4807 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
4814 // Creates the ConstructorBuilder
4816 public override bool Define ()
4818 if (ConstructorBuilder != null)
4821 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4822 MethodAttributes.SpecialName);
4824 if ((ModFlags & Modifiers.STATIC) != 0) {
4825 ca |= MethodAttributes.Static | MethodAttributes.Private;
4827 ca |= MethodAttributes.HideBySig;
4829 if ((ModFlags & Modifiers.PUBLIC) != 0)
4830 ca |= MethodAttributes.Public;
4831 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4832 if ((ModFlags & Modifiers.INTERNAL) != 0)
4833 ca |= MethodAttributes.FamORAssem;
4835 ca |= MethodAttributes.Family;
4836 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4837 ca |= MethodAttributes.Assembly;
4839 ca |= MethodAttributes.Private;
4842 if (!CheckAbstractAndExtern (block != null))
4845 // Check if arguments were correct.
4849 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4850 ca, CallingConventions,
4853 if ((ModFlags & Modifiers.UNSAFE) != 0)
4854 ConstructorBuilder.InitLocals = false;
4856 if (Parent.PartialContainer.IsComImport) {
4857 if (!IsDefault ()) {
4858 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4859 Parent.GetSignatureForError ());
4862 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4865 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4866 TypeManager.AddMethod (ConstructorBuilder, this);
4868 // It's here only to report an error
4869 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0) {
4870 member_type = TypeManager.void_type;
4871 Iterator.CreateIterator (this, Parent, null, ModFlags);
4880 public override void Emit ()
4882 if (OptAttributes != null)
4883 OptAttributes.Emit ();
4887 EmitContext ec = CreateEmitContext (null, null);
4890 // If we use a "this (...)" constructor initializer, then
4891 // do not emit field initializers, they are initialized in the other constructor
4893 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4894 !(Initializer is ConstructorThisInitializer);
4896 if (emit_field_initializers)
4897 Parent.PartialContainer.ResolveFieldInitializers (ec);
4899 if (block != null) {
4900 // If this is a non-static `struct' constructor and doesn't have any
4901 // initializer, it must initialize all of the struct's fields.
4902 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4903 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4904 block.AddThisVariable (Parent, Location);
4906 if (!block.ResolveMeta (ec, ParameterInfo))
4910 if ((ModFlags & Modifiers.STATIC) == 0){
4911 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4912 Initializer = new GeneratedBaseInitializer (Location);
4915 // Spec mandates that Initializers will not have
4919 if ((Initializer != null) &&
4920 !Initializer.Resolve (ConstructorBuilder, ec))
4922 ec.IsStatic = false;
4925 Parameters.ApplyAttributes (ConstructorBuilder);
4927 SourceMethod source = SourceMethod.Create (
4928 Parent, ConstructorBuilder, block);
4930 bool unreachable = false;
4931 if (block != null) {
4932 if (!ec.ResolveTopBlock (null, block, ParameterInfo, this, out unreachable))
4935 ec.EmitMeta (block);
4937 if (Report.Errors > 0)
4940 if (emit_field_initializers)
4941 Parent.PartialContainer.EmitFieldInitializers (ec);
4943 if (block.ScopeInfo != null) {
4944 ExpressionStatement init = block.ScopeInfo.GetScopeInitializer (ec);
4945 init.EmitStatement (ec);
4948 if (Initializer != null)
4949 Initializer.Emit (ec);
4951 ec.EmitResolvedTopBlock (block, unreachable);
4955 source.CloseMethod ();
4957 if (declarative_security != null) {
4958 foreach (DictionaryEntry de in declarative_security) {
4959 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4966 // Is never override
4967 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4972 public override string GetSignatureForError()
4974 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4977 public override string[] ValidAttributeTargets {
4979 return attribute_targets;
4983 protected override bool VerifyClsCompliance ()
4985 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4989 if (ParameterInfo.Count > 0) {
4990 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4992 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4994 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4995 foreach (Type param in ParameterTypes) {
4996 if (param.IsArray) {
5002 has_compliant_args = true;
5006 #region IMethodData Members
5008 public System.Reflection.CallingConventions CallingConventions {
5010 CallingConventions cc = Parameters.CallingConvention;
5012 if (Parent.PartialContainer.Kind == Kind.Class)
5013 if ((ModFlags & Modifiers.STATIC) == 0)
5014 cc |= CallingConventions.HasThis;
5016 // FIXME: How is `ExplicitThis' used in C#?
5022 public new Location Location {
5024 return base.Location;
5028 public MemberName MethodName {
5034 public Type ReturnType {
5040 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
5042 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
5043 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
5044 ec.CurrentBlock = block;
5048 public bool IsExcluded()
5053 GenericMethod IMethodData.GenericMethod {
5063 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
5065 public interface IMethodData
5067 CallingConventions CallingConventions { get; }
5068 Location Location { get; }
5069 MemberName MethodName { get; }
5070 Type ReturnType { get; }
5071 GenericMethod GenericMethod { get; }
5072 Parameters ParameterInfo { get; }
5074 Iterator Iterator { get; }
5076 Attributes OptAttributes { get; }
5077 ToplevelBlock Block { get; set; }
5079 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
5080 ObsoleteAttribute GetObsoleteAttribute ();
5081 string GetSignatureForError ();
5083 bool IsClsComplianceRequired ();
5084 void SetMemberIsUsed ();
5088 // Encapsulates most of the Method's state
5090 public class MethodData {
5092 readonly IMethodData method;
5094 public readonly GenericMethod GenericMethod;
5097 // Are we implementing an interface ?
5099 public MethodInfo implementing;
5104 protected InterfaceMemberBase member;
5105 protected int modifiers;
5106 protected MethodAttributes flags;
5107 protected Type declaring_type;
5108 protected MethodInfo parent_method;
5110 MethodBuilder builder = null;
5111 public MethodBuilder MethodBuilder {
5117 public Type DeclaringType {
5119 return declaring_type;
5123 public MethodData (InterfaceMemberBase member,
5124 int modifiers, MethodAttributes flags, IMethodData method)
5126 this.member = member;
5127 this.modifiers = modifiers;
5130 this.method = method;
5133 public MethodData (InterfaceMemberBase member,
5134 int modifiers, MethodAttributes flags,
5135 IMethodData method, MethodBuilder builder,
5136 GenericMethod generic, MethodInfo parent_method)
5137 : this (member, modifiers, flags, method)
5139 this.builder = builder;
5140 this.GenericMethod = generic;
5141 this.parent_method = parent_method;
5144 public bool Define (DeclSpace parent)
5146 string name = method.MethodName.Basename;
5147 string method_name = method.MethodName.FullName;
5149 TypeContainer container = parent.PartialContainer;
5151 PendingImplementation pending = container.PendingImplementations;
5152 if (pending != null){
5153 if (member is Indexer) // TODO: test it, but it should work without this IF
5154 implementing = pending.IsInterfaceIndexer (
5155 member.InterfaceType, method.ReturnType, method.ParameterInfo);
5157 implementing = pending.IsInterfaceMethod (
5158 member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
5160 if (member.InterfaceType != null){
5161 if (implementing == null){
5162 if (member is PropertyBase) {
5163 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
5164 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
5165 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
5168 Report.Error (539, method.Location,
5169 "`{0}.{1}' in explicit interface declaration is not a member of interface",
5170 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
5174 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
5175 Report.SymbolRelatedToPreviousError (implementing);
5176 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
5177 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
5181 method_name = member.FullName;
5183 if (implementing != null) {
5184 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
5185 if (prop_method == null) {
5186 if (TypeManager.IsSpecialMethod (implementing)) {
5187 Report.SymbolRelatedToPreviousError (implementing);
5188 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
5189 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
5190 implementing.Name.StartsWith ("get_") ? "get" : "set");
5192 } else if (implementing.DeclaringType.IsInterface) {
5193 if (!implementing.IsSpecialName) {
5194 Report.SymbolRelatedToPreviousError (implementing);
5195 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
5196 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
5199 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
5200 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
5201 Report.SymbolRelatedToPreviousError (implementing);
5202 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
5203 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
5212 // For implicit implementations, make sure we are public, for
5213 // explicit implementations, make sure we are private.
5215 if (implementing != null){
5217 // Setting null inside this block will trigger a more
5218 // verbose error reporting for missing interface implementations
5220 // The "candidate" function has been flagged already
5221 // but it wont get cleared
5223 if (member.IsExplicitImpl){
5224 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
5225 Report.SymbolRelatedToPreviousError (implementing);
5226 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
5227 method.GetSignatureForError ());
5231 if (implementing.DeclaringType.IsInterface) {
5233 // If this is an interface method implementation,
5234 // check for public accessibility
5236 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5238 implementing = null;
5240 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5241 // We may never be private.
5242 implementing = null;
5244 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5246 // We may be protected if we're overriding something.
5248 implementing = null;
5253 // Static is not allowed
5255 if ((modifiers & Modifiers.STATIC) != 0){
5256 implementing = null;
5261 // If implementing is still valid, set flags
5263 if (implementing != null){
5265 // When implementing interface methods, set NewSlot
5266 // unless, we are overwriting a method.
5268 if (implementing.DeclaringType.IsInterface){
5269 if ((modifiers & Modifiers.OVERRIDE) == 0)
5270 flags |= MethodAttributes.NewSlot;
5273 MethodAttributes.Virtual |
5274 MethodAttributes.HideBySig;
5276 // Set Final unless we're virtual, abstract or already overriding a method.
5277 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5278 flags |= MethodAttributes.Final;
5281 DefineMethodBuilder (container, method_name, method.ParameterInfo.Types);
5283 if (builder == null)
5286 if (container.CurrentType != null)
5287 declaring_type = container.CurrentType;
5289 declaring_type = container.TypeBuilder;
5291 if ((modifiers & Modifiers.UNSAFE) != 0)
5292 builder.InitLocals = false;
5295 if (implementing != null){
5297 // clear the pending implemntation flag
5299 if (member is Indexer) {
5300 pending.ImplementIndexer (
5301 member.InterfaceType, builder, method.ReturnType,
5302 method.ParameterInfo, member.IsExplicitImpl);
5304 pending.ImplementMethod (
5305 member.InterfaceType, name, method.ReturnType,
5306 method.ParameterInfo, member.IsExplicitImpl);
5308 if (member.IsExplicitImpl)
5309 container.TypeBuilder.DefineMethodOverride (
5310 builder, implementing);
5313 TypeManager.AddMethod (builder, method);
5315 if (GenericMethod != null) {
5316 bool is_override = member.IsExplicitImpl |
5317 ((modifiers & Modifiers.OVERRIDE) != 0);
5319 if (implementing != null)
5320 parent_method = implementing;
5322 EmitContext ec = method.CreateEmitContext (container, null);
5323 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5332 /// Create the MethodBuilder for the method
5334 void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
5336 if (builder == null) {
5337 builder = container.TypeBuilder.DefineMethod (
5338 method_name, flags, method.CallingConventions,
5339 method.ReturnType, ParameterTypes);
5343 #if GMCS_SOURCE && !MS_COMPATIBLE
5344 builder.SetGenericMethodSignature (
5345 flags, method.CallingConventions,
5346 method.ReturnType, ParameterTypes);
5353 public void Emit (DeclSpace parent)
5355 ToplevelBlock block = method.Block;
5359 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5361 ec = method.CreateEmitContext (parent, null);
5363 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5365 if (GenericMethod != null)
5366 GenericMethod.EmitAttributes ();
5368 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5371 // Handle destructors specially
5373 // FIXME: This code generates buggy code
5375 if (member is Destructor)
5376 EmitDestructor (ec, block);
5378 ec.EmitTopBlock (method, block);
5381 source.CloseMethod ();
5384 void EmitDestructor (EmitContext ec, ToplevelBlock block)
5386 ILGenerator ig = ec.ig;
5388 Label finish = ig.DefineLabel ();
5390 block.SetDestructor ();
5392 ig.BeginExceptionBlock ();
5393 ec.ReturnLabel = finish;
5394 ec.HasReturnLabel = true;
5395 ec.EmitTopBlock (method, block);
5397 // ig.MarkLabel (finish);
5398 ig.BeginFinallyBlock ();
5400 if (ec.ContainerType.BaseType != null) {
5401 Expression member_lookup = Expression.MemberLookup (
5402 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
5403 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
5405 if (member_lookup != null){
5406 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
5408 ig.Emit (OpCodes.Ldarg_0);
5409 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
5413 ig.EndExceptionBlock ();
5414 //ig.MarkLabel (ec.ReturnLabel);
5415 ig.Emit (OpCodes.Ret);
5419 // TODO: Should derive from MethodCore
5420 public class Destructor : Method {
5422 static string[] attribute_targets = new string [] { "method" };
5424 public Destructor (DeclSpace parent, Expression return_type, int mod,
5425 string name, Parameters parameters, Attributes attrs,
5427 : base (parent, null, return_type, mod, false, new MemberName (name, l),
5431 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5433 if (a.Type == TypeManager.conditional_attribute_type) {
5434 Error_ConditionalAttributeIsNotValid ();
5438 base.ApplyAttributeBuilder (a, cb);
5441 public override string GetSignatureForError ()
5443 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5446 public override string[] ValidAttributeTargets {
5448 return attribute_targets;
5453 abstract public class MemberBase : MemberCore {
5454 public Expression Type;
5455 public readonly DeclSpace ds;
5456 public readonly GenericMethod GenericMethod;
5459 // The type of this property / indexer / event
5461 protected Type member_type;
5462 public Type MemberType {
5464 if (member_type == null && Type != null) {
5465 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
5466 Type = Type.ResolveAsTypeTerminal (rc, false);
5468 member_type = Type.Type;
5476 // The constructor is only exposed to our children
5478 protected MemberBase (DeclSpace parent, GenericMethod generic,
5479 Expression type, int mod, int allowed_mod, int def_mod,
5480 MemberName name, Attributes attrs)
5481 : base (parent, name, attrs)
5483 this.ds = generic != null ? generic : (DeclSpace) parent;
5485 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5486 GenericMethod = generic;
5487 if (GenericMethod != null)
5488 GenericMethod.ModFlags = ModFlags;
5491 protected virtual bool CheckBase ()
5493 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.PartialContainer.Kind == Kind.Struct) {
5494 Report.Error (666, Location, "`{0}': new protected member declared in struct", GetSignatureForError ());
5498 if (Report.WarningLevel >= 4 &&
5499 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
5500 ((ModFlags & Modifiers.PROTECTED) != 0) &&
5501 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
5502 Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class", GetSignatureForError ());
5507 protected virtual bool DoDefine ()
5509 if (MemberType == null)
5512 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5513 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5514 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5515 GetSignatureForError (), Parent.GetSignatureForError ());
5519 // verify accessibility
5520 if (!IsAccessibleAs (MemberType)) {
5521 Report.SymbolRelatedToPreviousError (MemberType);
5522 if (this is Property)
5523 Report.Error (53, Location,
5524 "Inconsistent accessibility: property type `" +
5525 TypeManager.CSharpName (MemberType) + "' is less " +
5526 "accessible than property `" + GetSignatureForError () + "'");
5527 else if (this is Indexer)
5528 Report.Error (54, Location,
5529 "Inconsistent accessibility: indexer return type `" +
5530 TypeManager.CSharpName (MemberType) + "' is less " +
5531 "accessible than indexer `" + GetSignatureForError () + "'");
5532 else if (this is MethodCore) {
5533 if (this is Operator)
5534 Report.Error (56, Location,
5535 "Inconsistent accessibility: return type `" +
5536 TypeManager.CSharpName (MemberType) + "' is less " +
5537 "accessible than operator `" + GetSignatureForError () + "'");
5539 Report.Error (50, Location,
5540 "Inconsistent accessibility: return type `" +
5541 TypeManager.CSharpName (MemberType) + "' is less " +
5542 "accessible than method `" + GetSignatureForError () + "'");
5544 Report.Error (52, Location,
5545 "Inconsistent accessibility: field type `" +
5546 TypeManager.CSharpName (MemberType) + "' is less " +
5547 "accessible than field `" + GetSignatureForError () + "'");
5555 protected bool IsTypePermitted ()
5557 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5558 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5566 // Abstract class for all fields
5568 abstract public class FieldBase : MemberBase {
5569 public FieldBuilder FieldBuilder;
5570 public Status status;
5571 protected Expression initializer;
5574 public enum Status : byte {
5575 HAS_OFFSET = 4 // Used by FieldMember.
5578 static readonly string[] attribute_targets = new string [] { "field" };
5580 protected FieldBase (DeclSpace parent, Expression type, int mod,
5581 int allowed_mod, MemberName name, Attributes attrs)
5582 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5585 if ((mod & Modifiers.ABSTRACT) != 0)
5586 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5589 public override AttributeTargets AttributeTargets {
5591 return AttributeTargets.Field;
5595 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5597 if (a.Type == TypeManager.field_offset_attribute_type) {
5598 status |= Status.HAS_OFFSET;
5600 if (!Parent.PartialContainer.HasExplicitLayout) {
5601 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5605 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5606 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5612 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5613 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5618 if (a.Type == TypeManager.marshal_as_attr_type) {
5619 UnmanagedMarshal marshal = a.GetMarshal (this);
5620 if (marshal != null) {
5621 FieldBuilder.SetMarshal (marshal);
5626 if ((a.HasSecurityAttribute)) {
5627 a.Error_InvalidSecurityParent ();
5631 FieldBuilder.SetCustomAttribute (cb);
5634 protected override bool CheckBase ()
5636 if (!base.CheckBase ())
5639 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5640 if (conflict_symbol == null) {
5641 if ((ModFlags & Modifiers.NEW) != 0) {
5642 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5647 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5648 Report.SymbolRelatedToPreviousError (conflict_symbol);
5649 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5650 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5656 public override bool Define()
5658 if (MemberType == null || Type == null)
5661 if (TypeManager.IsGenericParameter (MemberType))
5664 if (MemberType == TypeManager.void_type) {
5665 // TODO: wrong location
5666 Expression.Error_VoidInvalidInTheContext (Location);
5670 if (MemberType.IsSealed && MemberType.IsAbstract) {
5671 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5681 if (!IsTypePermitted ())
5688 // Represents header string for documentation comment.
5690 public override string DocCommentHeader {
5691 get { return "F:"; }
5694 public override void Emit ()
5697 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0) {
5698 FieldBuilder.SetCustomAttribute (TypeManager.compiler_generated_attr);
5702 if (OptAttributes != null) {
5703 OptAttributes.Emit ();
5706 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5707 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5713 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5715 Report.SymbolRelatedToPreviousError (static_class);
5716 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5720 public Expression Initializer {
5722 if (value != null) {
5723 this.initializer = value;
5728 protected virtual bool IsFieldClsCompliant {
5730 if (FieldBuilder == null)
5733 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5737 public override string[] ValidAttributeTargets
5740 return attribute_targets;
5744 protected override bool VerifyClsCompliance ()
5746 if (!base.VerifyClsCompliance ())
5749 if (!IsFieldClsCompliant) {
5750 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5755 public void SetAssigned ()
5757 caching_flags |= Flags.IsAssigned;
5761 interface IFixedBuffer
5763 FieldInfo Element { get; }
5764 Type ElementType { get; }
5767 public class FixedFieldExternal: IFixedBuffer
5769 FieldInfo element_field;
5771 public FixedFieldExternal (FieldInfo fi)
5773 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5776 #region IFixedField Members
5778 public FieldInfo Element {
5780 return element_field;
5784 public Type ElementType {
5786 return element_field.FieldType;
5794 /// Fixed buffer implementation
5796 public class FixedField : FieldBase, IFixedBuffer
5798 public const string FixedElementName = "FixedElementField";
5799 static int GlobalCounter = 0;
5800 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5801 static FieldInfo[] fi;
5803 TypeBuilder fixed_buffer_type;
5804 FieldBuilder element;
5805 Expression size_expr;
5808 const int AllowedModifiers =
5811 Modifiers.PROTECTED |
5812 Modifiers.INTERNAL |
5815 public FixedField (DeclSpace parent, Expression type, int mod, string name,
5816 Expression size_expr, Attributes attrs, Location loc):
5817 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5819 if (RootContext.Version == LanguageVersion.ISO_1)
5820 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5822 this.size_expr = size_expr;
5825 public override bool Define()
5828 if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) != 0)
5829 Report.Warning (-23, 1, Location, "Only private or internal fixed sized buffers are supported by .NET 1.x");
5831 if (!Parent.IsInUnsafeScope)
5832 Expression.UnsafeError (Location);
5834 if (!base.Define ())
5837 if (!TypeManager.IsPrimitiveType (MemberType)) {
5838 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",
5839 GetSignatureForError ());
5842 // Create nested fixed buffer container
5843 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5844 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5845 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5847 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5848 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5850 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5851 Parent.MemberCache.AddMember (FieldBuilder, this);
5852 TypeManager.RegisterFieldBase (FieldBuilder, this);
5857 public override void Emit()
5859 if (Parent.PartialContainer.Kind != Kind.Struct) {
5860 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5861 GetSignatureForError ());
5864 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5865 Constant c = size_expr.ResolveAsConstant (ec, this);
5869 IntConstant buffer_size_const = c.ImplicitConversionRequired (TypeManager.int32_type, Location) as IntConstant;
5870 if (buffer_size_const == null)
5873 buffer_size = buffer_size_const.Value;
5875 if (buffer_size <= 0) {
5876 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5880 int type_size = Expression.GetTypeSize (MemberType);
5882 if (buffer_size > int.MaxValue / type_size) {
5883 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5884 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5888 buffer_size *= type_size;
5891 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5893 object[] fi_val = new object[1];
5894 fi_val [0] = buffer_size;
5896 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5897 ctor_args, fi, fi_val);
5898 fixed_buffer_type.SetCustomAttribute (cab);
5901 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object[] { MemberType, buffer_size } );
5902 FieldBuilder.SetCustomAttribute (cab);
5907 protected override bool IsFieldClsCompliant {
5913 #region IFixedField Members
5915 public FieldInfo Element {
5921 public Type ElementType {
5931 // The Field class is used to represents class/struct fields during parsing.
5933 public class Field : FieldBase {
5935 // Modifiers allowed in a class declaration
5937 const int AllowedModifiers =
5940 Modifiers.PROTECTED |
5941 Modifiers.INTERNAL |
5944 Modifiers.VOLATILE |
5948 public Field (DeclSpace parent, Expression type, int mod, string name,
5949 Attributes attrs, Location loc)
5950 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5955 bool CanBeVolatile ()
5957 if (TypeManager.IsReferenceType (MemberType))
5960 if (MemberType.IsEnum)
5963 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5964 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5965 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5966 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5967 MemberType == TypeManager.float_type)
5973 public override bool Define ()
5975 if (!base.Define ())
5978 if ((ModFlags & Modifiers.VOLATILE) != 0){
5979 if (!CanBeVolatile ()) {
5980 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5981 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5984 if ((ModFlags & Modifiers.READONLY) != 0){
5985 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5986 GetSignatureForError ());
5990 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5993 FieldBuilder = Parent.TypeBuilder.DefineField (
5994 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5996 Parent.MemberCache.AddMember (FieldBuilder, this);
5997 TypeManager.RegisterFieldBase (FieldBuilder, this);
5999 catch (ArgumentException) {
6000 Report.Warning (-24, 1, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
6004 if (initializer != null)
6005 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
6006 new FieldInitializer (FieldBuilder, initializer));
6008 if (Parent.PartialContainer.Kind == Kind.Struct && (fa & FieldAttributes.Static) == 0 &&
6009 MemberType == Parent.TypeBuilder && !TypeManager.IsBuiltinType (MemberType) && initializer == null) {
6010 Report.Error (523, Location, "Struct member `{0}' causes a cycle in the structure layout",
6011 GetSignatureForError ());
6018 protected override bool VerifyClsCompliance ()
6020 if (!base.VerifyClsCompliance ())
6023 if ((ModFlags & Modifiers.VOLATILE) != 0) {
6024 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
6032 // `set' and `get' accessors are represented with an Accessor.
6034 public class Accessor : IAnonymousHost {
6036 // Null if the accessor is empty, or a Block if not
6038 public const int AllowedModifiers =
6040 Modifiers.PROTECTED |
6041 Modifiers.INTERNAL |
6044 public ToplevelBlock Block;
6045 public Attributes Attributes;
6046 public Location Location;
6047 public int ModFlags;
6049 public ArrayList AnonymousMethods;
6051 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
6056 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
6059 public void SetYields ()
6064 public void AddAnonymousMethod (AnonymousMethodExpression ame)
6066 if (AnonymousMethods == null)
6067 AnonymousMethods = new ArrayList ();
6068 AnonymousMethods.Add (ame);
6072 // Ooouh Martin, templates are missing here.
6073 // When it will be possible move here a lot of child code and template method type.
6074 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
6075 protected MethodData method_data;
6076 protected ToplevelBlock block;
6077 protected ListDictionary declarative_security;
6079 // The accessor are created event if they are not wanted.
6080 // But we need them because their names are reserved.
6081 // Field says whether accessor will be emited or not
6082 public readonly bool IsDummy;
6084 protected readonly string prefix;
6086 ReturnParameter return_attributes;
6088 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
6089 : base (member.Parent, SetupName (prefix, member, member.Location), null)
6091 this.prefix = prefix;
6095 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
6097 : base (member.Parent, SetupName (prefix, member, accessor.Location),
6098 accessor.Attributes)
6100 this.prefix = prefix;
6101 this.block = accessor.Block;
6104 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
6106 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
6109 public void UpdateName (InterfaceMemberBase member)
6111 SetMemberName (SetupName (prefix, member, Location));
6114 #region IMethodData Members
6116 public abstract Iterator Iterator {
6120 public ToplevelBlock Block {
6130 public CallingConventions CallingConventions {
6132 return CallingConventions.Standard;
6136 public bool IsExcluded ()
6141 GenericMethod IMethodData.GenericMethod {
6147 public MemberName MethodName {
6153 public Type[] ParameterTypes {
6155 return ParameterInfo.Types;
6159 public abstract Parameters ParameterInfo { get ; }
6160 public abstract Type ReturnType { get; }
6161 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
6165 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
6167 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
6168 a.Type == TypeManager.conditional_attribute_type) {
6169 Report.Error (1667, a.Location,
6170 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6171 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6175 if (a.IsValidSecurityAttribute ()) {
6176 if (declarative_security == null)
6177 declarative_security = new ListDictionary ();
6178 a.ExtractSecurityPermissionSet (declarative_security);
6182 if (a.Target == AttributeTargets.Method) {
6183 method_data.MethodBuilder.SetCustomAttribute (cb);
6187 if (a.Target == AttributeTargets.ReturnValue) {
6188 if (return_attributes == null)
6189 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6191 return_attributes.ApplyAttributeBuilder (a, cb);
6195 ApplyToExtraTarget (a, cb);
6198 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
6200 throw new NotSupportedException ("You forgot to define special attribute target handling");
6203 // It is not supported for the accessors
6204 public sealed override bool Define()
6206 throw new NotSupportedException ();
6209 public void Emit (DeclSpace parent)
6211 EmitMethod (parent);
6214 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0)
6215 method_data.MethodBuilder.SetCustomAttribute (TypeManager.compiler_generated_attr);
6217 if (OptAttributes != null)
6218 OptAttributes.Emit ();
6220 if (declarative_security != null) {
6221 foreach (DictionaryEntry de in declarative_security) {
6222 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6229 protected virtual void EmitMethod (DeclSpace parent)
6231 method_data.Emit (parent);
6234 public override bool EnableOverloadChecks (MemberCore overload)
6236 // This can only happen with indexers and it will
6237 // be catched as indexer difference
6238 if (overload is AbstractPropertyEventMethod)
6241 if (overload is MethodCore) {
6242 caching_flags |= Flags.MethodOverloadsExist;
6248 public override bool IsClsComplianceRequired()
6253 public bool IsDuplicateImplementation (MethodCore method)
6255 if (!MemberName.Equals (method.MemberName))
6258 Type[] param_types = method.ParameterTypes;
6260 if (param_types == null || param_types.Length != ParameterTypes.Length)
6263 for (int i = 0; i < param_types.Length; i++)
6264 if (param_types [i] != ParameterTypes [i])
6267 Report.SymbolRelatedToPreviousError (method);
6268 Report.Error (82, Location, "A member `{0}' is already reserved",
6269 method.GetSignatureForError ());
6273 public override bool IsUsed
6283 public new Location Location {
6285 return base.Location;
6289 public virtual bool ResolveMembers ()
6295 // Represents header string for documentation comment.
6297 public override string DocCommentHeader {
6298 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6304 // Properties and Indexers both generate PropertyBuilders, we use this to share
6305 // their common bits.
6307 abstract public class PropertyBase : PropertyBasedMember {
6309 public class GetMethod : PropertyMethod
6311 static string[] attribute_targets = new string [] { "method", "return" };
6313 public GetMethod (PropertyBase method):
6314 base (method, "get_")
6318 public GetMethod (PropertyBase method, Accessor accessor):
6319 base (method, accessor, "get_")
6323 public override MethodBuilder Define (DeclSpace parent)
6325 if (!CheckForDuplications ())
6331 base.Define (parent);
6333 method_data = new MethodData (method, ModFlags, flags, this);
6335 if (!method_data.Define (parent))
6338 return method_data.MethodBuilder;
6341 public override Type ReturnType {
6343 return method.MemberType;
6347 public override Parameters ParameterInfo {
6349 return Parameters.EmptyReadOnlyParameters;
6353 public override string[] ValidAttributeTargets {
6355 return attribute_targets;
6360 public class SetMethod : PropertyMethod {
6362 static string[] attribute_targets = new string [] { "method", "param", "return" };
6363 ImplicitParameter param_attr;
6364 protected Parameters parameters;
6366 public SetMethod (PropertyBase method):
6367 base (method, "set_")
6371 public SetMethod (PropertyBase method, Accessor accessor):
6372 base (method, accessor, "set_")
6376 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6378 if (a.Target == AttributeTargets.Parameter) {
6379 if (param_attr == null)
6380 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6382 param_attr.ApplyAttributeBuilder (a, cb);
6386 base.ApplyAttributeBuilder (a, cb);
6389 public override Parameters ParameterInfo {
6391 if (parameters == null)
6392 DefineParameters ();
6397 protected virtual void DefineParameters ()
6399 parameters = Parameters.CreateFullyResolved (
6400 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location));
6403 public override MethodBuilder Define (DeclSpace parent)
6405 if (!CheckForDuplications ())
6411 base.Define (parent);
6413 method_data = new MethodData (method, ModFlags, flags, this);
6415 if (!method_data.Define (parent))
6418 return method_data.MethodBuilder;
6421 public override Type ReturnType {
6423 return TypeManager.void_type;
6427 public override string[] ValidAttributeTargets {
6429 return attribute_targets;
6434 static string[] attribute_targets = new string [] { "property" };
6436 public abstract class PropertyMethod : AbstractPropertyEventMethod
6438 protected readonly PropertyBase method;
6439 protected MethodAttributes flags;
6441 ArrayList anonymous_methods;
6444 public PropertyMethod (PropertyBase method, string prefix)
6445 : base (method, prefix)
6447 this.method = method;
6450 public PropertyMethod (PropertyBase method, Accessor accessor,
6452 : base (method, accessor, prefix)
6454 this.method = method;
6455 this.ModFlags = accessor.ModFlags;
6456 yields = accessor.Yields;
6457 anonymous_methods = accessor.AnonymousMethods;
6459 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6460 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6464 public override Iterator Iterator {
6465 get { return iterator; }
6468 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6470 if (a.IsInternalMethodImplAttribute) {
6471 method.is_external_implementation = true;
6474 base.ApplyAttributeBuilder (a, cb);
6477 public override AttributeTargets AttributeTargets {
6479 return AttributeTargets.Method;
6483 public override bool IsClsComplianceRequired ()
6485 return method.IsClsComplianceRequired ();
6488 public override bool ResolveMembers ()
6491 iterator = Iterator.CreateIterator (this, Parent, null, ModFlags);
6492 if (iterator == null)
6496 if (anonymous_methods != null) {
6497 foreach (AnonymousMethodExpression ame in anonymous_methods) {
6498 if (!ame.CreateAnonymousHelpers ())
6506 public virtual MethodBuilder Define (DeclSpace parent)
6508 TypeContainer container = parent.PartialContainer;
6511 // Check for custom access modifier
6513 if ((ModFlags & Modifiers.Accessibility) == 0) {
6514 ModFlags |= method.ModFlags;
6515 flags = method.flags;
6517 if (container.Kind == Kind.Interface)
6518 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6519 GetSignatureForError ());
6521 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6522 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6525 CheckModifiers (ModFlags);
6526 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6527 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6528 flags = Modifiers.MethodAttr (ModFlags);
6529 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6532 CheckAbstractAndExtern (block != null);
6536 public bool HasCustomAccessModifier
6539 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6543 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6545 return new EmitContext (method,
6546 ds, method.ds, method.Location, ig, ReturnType,
6547 method.ModFlags, false);
6550 public override ObsoleteAttribute GetObsoleteAttribute ()
6552 return method.GetObsoleteAttribute ();
6555 public override string GetSignatureForError()
6557 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6560 void CheckModifiers (int modflags)
6562 modflags &= Modifiers.Accessibility;
6564 int mflags = method.ModFlags & Modifiers.Accessibility;
6566 if ((mflags & Modifiers.PUBLIC) != 0) {
6567 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6569 else if ((mflags & Modifiers.PROTECTED) != 0) {
6570 if ((mflags & Modifiers.INTERNAL) != 0)
6571 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6573 flags |= Modifiers.PRIVATE;
6575 else if ((mflags & Modifiers.INTERNAL) != 0)
6576 flags |= Modifiers.PRIVATE;
6578 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6579 Report.Error (273, Location,
6580 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6581 GetSignatureForError (), method.GetSignatureForError ());
6585 protected bool CheckForDuplications ()
6587 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6590 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6594 public PropertyMethod Get, Set;
6595 public PropertyBuilder PropertyBuilder;
6596 public MethodBuilder GetBuilder, SetBuilder;
6598 protected bool define_set_first = false;
6600 public PropertyBase (DeclSpace parent, Expression type, int mod_flags,
6601 int allowed_mod, bool is_iface, MemberName name,
6602 Attributes attrs, bool define_set_first)
6603 : base (parent, null, type, mod_flags, allowed_mod, is_iface, name, attrs)
6605 this.define_set_first = define_set_first;
6608 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6610 if (a.HasSecurityAttribute) {
6611 a.Error_InvalidSecurityParent ();
6615 PropertyBuilder.SetCustomAttribute (cb);
6618 public override AttributeTargets AttributeTargets {
6620 return AttributeTargets.Property;
6624 public override bool Define ()
6629 if (!IsTypePermitted ())
6635 protected override bool DoDefine ()
6637 if (!base.DoDefine ())
6641 // Accessors modifiers check
6643 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6644 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6645 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6646 GetSignatureForError ());
6650 if ((Get.IsDummy || Set.IsDummy)
6651 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6652 Report.Error (276, Location,
6653 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6654 GetSignatureForError ());
6659 if (MemberType.IsGenericParameter)
6663 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6664 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6673 GetBuilder = Get.Define (Parent);
6674 return (Get.IsDummy) ? true : GetBuilder != null;
6677 bool DefineSet (bool define)
6682 SetBuilder = Set.Define (Parent);
6683 return (Set.IsDummy) ? true : SetBuilder != null;
6686 protected bool DefineAccessors ()
6688 return DefineSet (define_set_first) &&
6690 DefineSet (!define_set_first);
6693 protected abstract PropertyInfo ResolveBaseProperty ();
6695 // TODO: rename to Resolve......
6696 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6698 PropertyInfo base_property = ResolveBaseProperty ();
6699 if (base_property == null)
6702 base_ret_type = base_property.PropertyType;
6703 MethodInfo get_accessor = base_property.GetGetMethod (true);
6704 MethodInfo set_accessor = base_property.GetSetMethod (true);
6705 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6707 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6708 if (Get != null && !Get.IsDummy && get_accessor == null) {
6709 Report.SymbolRelatedToPreviousError (base_property);
6710 Report.Error (545, Location, "`{0}.get': cannot override because `{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6713 if (Set != null && !Set.IsDummy && set_accessor == null) {
6714 Report.SymbolRelatedToPreviousError (base_property);
6715 Report.Error (546, Location, "`{0}.set': cannot override because `{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6719 // Check base class accessors access
6722 // TODO: rewrite to reuse Get|Set.CheckAccessModifiers and share code there
6723 get_accessor_access = set_accessor_access = 0;
6724 if ((ModFlags & Modifiers.NEW) == 0) {
6725 if (get_accessor != null) {
6726 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6727 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6729 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6730 Error_CannotChangeAccessModifiers (get_accessor, get_accessor_access, ".get");
6733 if (set_accessor != null) {
6734 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6735 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6737 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6738 Error_CannotChangeAccessModifiers (set_accessor, set_accessor_access, ".set");
6743 // When one accessor does not exist and property hides base one
6744 // we need to propagate this upwards
6745 if (set_accessor == null)
6746 set_accessor = get_accessor;
6749 // Get the less restrictive access
6751 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6754 public override void Emit ()
6757 // The PropertyBuilder can be null for explicit implementations, in that
6758 // case, we do not actually emit the ".property", so there is nowhere to
6759 // put the attribute
6761 if (PropertyBuilder != null && OptAttributes != null)
6762 OptAttributes.Emit ();
6774 /// Tests whether accessors are not in collision with some method (CS0111)
6776 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6778 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6781 public override bool IsUsed
6787 return Get.IsUsed | Set.IsUsed;
6791 protected override void SetMemberName (MemberName new_name)
6793 base.SetMemberName (new_name);
6795 Get.UpdateName (this);
6796 Set.UpdateName (this);
6799 public override string[] ValidAttributeTargets {
6801 return attribute_targets;
6806 // Represents header string for documentation comment.
6808 public override string DocCommentHeader {
6809 get { return "P:"; }
6813 public class Property : PropertyBase {
6814 const int AllowedModifiers =
6817 Modifiers.PROTECTED |
6818 Modifiers.INTERNAL |
6822 Modifiers.OVERRIDE |
6823 Modifiers.ABSTRACT |
6826 Modifiers.METHOD_YIELDS |
6829 const int AllowedInterfaceModifiers =
6832 void CreateAutomaticProperty (Block block, Accessor get_block, Accessor set_block)
6835 Field field = new Field (
6837 Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & Modifiers.STATIC),
6838 "<" + Name + ">k__BackingField", null, Location);
6839 ((TypeContainer)Parent).AddField (field);
6842 get_block.Block = new ToplevelBlock (block, null, Location);
6843 Return r = new Return (new SimpleName(field.Name, Location), Location);
6844 get_block.Block.AddStatement (r);
6845 get_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6848 Parameters parameters = new Parameters (new Parameter (Type, "value", Parameter.Modifier.NONE, null, Location));
6849 set_block.Block = new ToplevelBlock (block, parameters, Location);
6850 Assign a = new Assign (new SimpleName(field.Name, Location), new SimpleName ("value", Location));
6851 set_block.Block.AddStatement (new StatementExpression(a));
6852 set_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6855 public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
6856 MemberName name, Attributes attrs, Accessor get_block,
6857 Accessor set_block, bool define_set_first)
6858 : this (parent, type, mod, is_iface, name, attrs, get_block, set_block,
6859 define_set_first, null)
6863 public Property (DeclSpace parent, Expression type, int mod, bool is_iface,
6864 MemberName name, Attributes attrs, Accessor get_block,
6865 Accessor set_block, bool define_set_first, Block current_block)
6866 : base (parent, type, mod,
6867 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6868 is_iface, name, attrs, define_set_first)
6870 if (!is_iface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6871 get_block != null && get_block.Block == null &&
6872 set_block != null && set_block.Block == null) {
6873 if (RootContext.Version <= LanguageVersion.ISO_2)
6874 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6876 CreateAutomaticProperty (current_block, get_block, set_block);
6879 if (get_block == null)
6880 Get = new GetMethod (this);
6882 Get = new GetMethod (this, get_block);
6884 if (set_block == null)
6885 Set = new SetMethod (this);
6887 Set = new SetMethod (this, set_block);
6890 public override bool Define ()
6892 if (!DoDefineBase ())
6895 if (!base.Define ())
6901 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6903 if (!DefineAccessors ())
6906 // FIXME - PropertyAttributes.HasDefault ?
6908 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6909 FullName, PropertyAttributes.None, MemberType, null);
6912 PropertyBuilder.SetGetMethod (GetBuilder);
6913 Parent.MemberCache.AddMember (GetBuilder, Get);
6917 PropertyBuilder.SetSetMethod (SetBuilder);
6918 Parent.MemberCache.AddMember (SetBuilder, Set);
6921 TypeManager.RegisterProperty (PropertyBuilder, this);
6922 Parent.MemberCache.AddMember (PropertyBuilder, this);
6926 protected override PropertyInfo ResolveBaseProperty ()
6928 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6929 Parent.TypeBuilder, Name, Parameters.EmptyReadOnlyParameters.Types, null, true) as PropertyInfo;
6934 /// Gigantic workaround for lameness in SRE follows :
6935 /// This class derives from EventInfo and attempts to basically
6936 /// wrap around the EventBuilder so that FindMembers can quickly
6937 /// return this in it search for members
6939 public class MyEventBuilder : EventInfo {
6942 // We use this to "point" to our Builder which is
6943 // not really a MemberInfo
6945 EventBuilder MyBuilder;
6948 // We "catch" and wrap these methods
6950 MethodInfo raise, remove, add;
6952 EventAttributes attributes;
6953 Type declaring_type, reflected_type, event_type;
6958 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6960 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6962 // And now store the values in our own fields.
6964 declaring_type = type_builder;
6966 reflected_type = type_builder;
6968 attributes = event_attr;
6971 this.event_type = event_type;
6975 // Methods that you have to override. Note that you only need
6976 // to "implement" the variants that take the argument (those are
6977 // the "abstract" methods, the others (GetAddMethod()) are
6980 public override MethodInfo GetAddMethod (bool nonPublic)
6985 public override MethodInfo GetRemoveMethod (bool nonPublic)
6990 public override MethodInfo GetRaiseMethod (bool nonPublic)
6996 // These methods make "MyEventInfo" look like a Builder
6998 public void SetRaiseMethod (MethodBuilder raiseMethod)
7000 raise = raiseMethod;
7001 MyBuilder.SetRaiseMethod (raiseMethod);
7004 public void SetRemoveOnMethod (MethodBuilder removeMethod)
7006 remove = removeMethod;
7007 MyBuilder.SetRemoveOnMethod (removeMethod);
7010 public void SetAddOnMethod (MethodBuilder addMethod)
7013 MyBuilder.SetAddOnMethod (addMethod);
7016 public void SetCustomAttribute (CustomAttributeBuilder cb)
7018 MyBuilder.SetCustomAttribute (cb);
7021 public override object [] GetCustomAttributes (bool inherit)
7023 // FIXME : There's nothing which can be seemingly done here because
7024 // we have no way of getting at the custom attribute objects of the
7029 public override object [] GetCustomAttributes (Type t, bool inherit)
7031 // FIXME : Same here !
7035 public override bool IsDefined (Type t, bool b)
7040 public override EventAttributes Attributes {
7046 public override string Name {
7052 public override Type DeclaringType {
7054 return declaring_type;
7058 public override Type ReflectedType {
7060 return reflected_type;
7064 public Type EventType {
7070 public void SetUsed ()
7072 if (my_event != null) {
7073 // my_event.SetAssigned ();
7074 my_event.SetMemberIsUsed ();
7080 /// For case when event is declared like property (with add and remove accessors).
7082 public class EventProperty: Event {
7083 abstract class AEventPropertyAccessor : AEventAccessor
7085 readonly ArrayList anonymous_methods;
7087 public AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
7088 base (method, accessor, prefix)
7090 this.anonymous_methods = accessor.AnonymousMethods;
7093 public override MethodBuilder Define (DeclSpace ds)
7095 CheckAbstractAndExtern (block != null);
7096 return base.Define (ds);
7099 public override string GetSignatureForError ()
7101 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
7104 public override bool ResolveMembers ()
7106 if (anonymous_methods == null)
7109 foreach (AnonymousMethodExpression ame in anonymous_methods) {
7110 if (!ame.CreateAnonymousHelpers ())
7119 sealed class AddDelegateMethod: AEventPropertyAccessor
7121 public AddDelegateMethod (Event method, Accessor accessor):
7122 base (method, accessor, "add_")
7126 protected override MethodInfo DelegateMethodInfo {
7128 return TypeManager.delegate_combine_delegate_delegate;
7133 sealed class RemoveDelegateMethod: AEventPropertyAccessor
7135 public RemoveDelegateMethod (Event method, Accessor accessor):
7136 base (method, accessor, "remove_")
7140 protected override MethodInfo DelegateMethodInfo {
7142 return TypeManager.delegate_remove_delegate_delegate;
7148 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
7150 public EventProperty (DeclSpace parent, Expression type, int mod_flags,
7151 bool is_iface, MemberName name,
7152 Attributes attrs, Accessor add, Accessor remove)
7153 : base (parent, type, mod_flags, is_iface, name, attrs)
7155 Add = new AddDelegateMethod (this, add);
7156 Remove = new RemoveDelegateMethod (this, remove);
7159 public override bool Define()
7161 if (!base.Define ())
7168 public override string[] ValidAttributeTargets {
7170 return attribute_targets;
7176 /// Event is declared like field.
7178 public class EventField : Event {
7179 abstract class EventFieldAccessor : AEventAccessor
7181 protected EventFieldAccessor (Event method, string prefix)
7182 : base (method, prefix)
7186 protected override void EmitMethod(DeclSpace parent)
7188 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7191 MethodBuilder mb = method_data.MethodBuilder;
7192 ILGenerator ig = mb.GetILGenerator ();
7194 // TODO: because we cannot use generics yet
7195 FieldInfo field_info = ((EventField)method).FieldBuilder;
7197 if (parent is Class) {
7198 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7201 if ((method.ModFlags & Modifiers.STATIC) != 0) {
7202 ig.Emit (OpCodes.Ldsfld, field_info);
7203 ig.Emit (OpCodes.Ldarg_0);
7204 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7205 ig.Emit (OpCodes.Castclass, method.MemberType);
7206 ig.Emit (OpCodes.Stsfld, field_info);
7208 ig.Emit (OpCodes.Ldarg_0);
7209 ig.Emit (OpCodes.Ldarg_0);
7210 ig.Emit (OpCodes.Ldfld, field_info);
7211 ig.Emit (OpCodes.Ldarg_1);
7212 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7213 ig.Emit (OpCodes.Castclass, method.MemberType);
7214 ig.Emit (OpCodes.Stfld, field_info);
7216 ig.Emit (OpCodes.Ret);
7220 sealed class AddDelegateMethod: EventFieldAccessor
7222 public AddDelegateMethod (Event method):
7223 base (method, "add_")
7227 protected override MethodInfo DelegateMethodInfo {
7229 return TypeManager.delegate_combine_delegate_delegate;
7234 sealed class RemoveDelegateMethod: EventFieldAccessor
7236 public RemoveDelegateMethod (Event method):
7237 base (method, "remove_")
7241 protected override MethodInfo DelegateMethodInfo {
7243 return TypeManager.delegate_remove_delegate_delegate;
7249 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7250 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7252 public FieldBuilder FieldBuilder;
7253 public Expression Initializer;
7255 public EventField (DeclSpace parent, Expression type, int mod_flags,
7256 bool is_iface, MemberName name,
7258 : base (parent, type, mod_flags, is_iface, name, attrs)
7260 Add = new AddDelegateMethod (this);
7261 Remove = new RemoveDelegateMethod (this);
7264 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7266 if (a.Target == AttributeTargets.Field) {
7267 FieldBuilder.SetCustomAttribute (cb);
7271 if (a.Target == AttributeTargets.Method) {
7272 int errors = Report.Errors;
7273 Add.ApplyAttributeBuilder (a, cb);
7274 if (errors == Report.Errors)
7275 Remove.ApplyAttributeBuilder (a, cb);
7279 base.ApplyAttributeBuilder (a, cb);
7282 public override bool Define()
7284 if (!base.Define ())
7290 // FIXME: We are unable to detect whether generic event is used because
7291 // we are using FieldExpr instead of EventExpr for event access in that
7292 // case. When this issue will be fixed this hack can be removed.
7293 if (TypeManager.IsGenericType (MemberType))
7296 if (Add.IsInterfaceImplementation)
7299 FieldBuilder = Parent.TypeBuilder.DefineField (
7301 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7302 TypeManager.RegisterEventField (EventBuilder, this);
7304 if (Initializer != null) {
7305 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
7306 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7307 GetSignatureForError ());
7311 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
7312 new FieldInitializer (FieldBuilder, Initializer));
7318 public override string[] ValidAttributeTargets
7321 return IsInterface ? attribute_targets_interface : attribute_targets;
7326 public abstract class Event : PropertyBasedMember {
7327 public abstract class AEventAccessor : AbstractPropertyEventMethod
7329 protected readonly Event method;
7330 ImplicitParameter param_attr;
7332 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7334 protected AEventAccessor (Event method, string prefix)
7335 : base (method, prefix)
7337 this.method = method;
7338 this.ModFlags = method.ModFlags;
7341 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7342 : base (method, accessor, prefix)
7344 this.method = method;
7345 this.ModFlags = method.ModFlags;
7348 public override Iterator Iterator {
7349 get { return null; }
7352 public bool IsInterfaceImplementation {
7353 get { return method_data.implementing != null; }
7356 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7358 if (a.IsInternalMethodImplAttribute) {
7359 method.is_external_implementation = true;
7362 base.ApplyAttributeBuilder (a, cb);
7365 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
7367 if (a.Target == AttributeTargets.Parameter) {
7368 if (param_attr == null)
7369 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7371 param_attr.ApplyAttributeBuilder (a, cb);
7375 base.ApplyAttributeBuilder (a, cb);
7378 public override AttributeTargets AttributeTargets {
7380 return AttributeTargets.Method;
7384 public override bool IsClsComplianceRequired ()
7386 return method.IsClsComplianceRequired ();
7389 public virtual MethodBuilder Define (DeclSpace parent)
7391 method_data = new MethodData (method, method.ModFlags,
7392 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7394 if (!method_data.Define (parent))
7397 MethodBuilder mb = method_data.MethodBuilder;
7398 ParameterInfo.ApplyAttributes (mb);
7402 protected abstract MethodInfo DelegateMethodInfo { get; }
7404 public override Type ReturnType {
7406 return TypeManager.void_type;
7410 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7412 return new EmitContext (
7413 ds, method.Parent, Location, ig, ReturnType,
7414 method.ModFlags, false);
7417 public override ObsoleteAttribute GetObsoleteAttribute ()
7419 return method.GetObsoleteAttribute ();
7422 public override string[] ValidAttributeTargets {
7424 return attribute_targets;
7428 public override Parameters ParameterInfo {
7430 return method.parameters;
7436 const int AllowedModifiers =
7439 Modifiers.PROTECTED |
7440 Modifiers.INTERNAL |
7445 Modifiers.OVERRIDE |
7447 Modifiers.ABSTRACT |
7450 const int AllowedInterfaceModifiers =
7453 public AEventAccessor Add, Remove;
7454 public MyEventBuilder EventBuilder;
7455 public MethodBuilder AddBuilder, RemoveBuilder;
7457 Parameters parameters;
7459 protected Event (DeclSpace parent, Expression type, int mod_flags,
7460 bool is_iface, MemberName name, Attributes attrs)
7461 : base (parent, null, type, mod_flags,
7462 is_iface ? AllowedInterfaceModifiers : AllowedModifiers, is_iface,
7467 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7469 if ((a.HasSecurityAttribute)) {
7470 a.Error_InvalidSecurityParent ();
7474 EventBuilder.SetCustomAttribute (cb);
7477 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7479 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7482 public override AttributeTargets AttributeTargets {
7484 return AttributeTargets.Event;
7488 public override bool Define ()
7490 if (!DoDefineBase ())
7496 if (!TypeManager.IsDelegateType (MemberType)) {
7497 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7501 parameters = Parameters.CreateFullyResolved (
7502 new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location));
7508 // Now define the accessors
7511 AddBuilder = Add.Define (Parent);
7512 if (AddBuilder == null)
7515 RemoveBuilder = Remove.Define (Parent);
7516 if (RemoveBuilder == null)
7519 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7520 EventBuilder.SetAddOnMethod (AddBuilder);
7521 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7523 Parent.MemberCache.AddMember (EventBuilder, this);
7524 Parent.MemberCache.AddMember (AddBuilder, Add);
7525 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7530 public override void Emit ()
7532 if (OptAttributes != null) {
7533 OptAttributes.Emit ();
7537 Remove.Emit (Parent);
7542 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7544 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7545 Parent.TypeBuilder, Name);
7550 ParameterData pd = TypeManager.GetParameterData (mi);
7551 base_ret_type = pd.ParameterType (0);
7556 // Represents header string for documentation comment.
7558 public override string DocCommentHeader {
7559 get { return "E:"; }
7564 public class Indexer : PropertyBase
7566 class GetIndexerMethod : GetMethod
7568 public GetIndexerMethod (PropertyBase method):
7573 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7574 base (method, accessor)
7578 public override bool EnableOverloadChecks (MemberCore overload)
7580 if (base.EnableOverloadChecks (overload)) {
7581 overload.caching_flags |= Flags.MethodOverloadsExist;
7588 public override Parameters ParameterInfo {
7590 return ((Indexer)method).parameters;
7595 class SetIndexerMethod: SetMethod
7597 public SetIndexerMethod (PropertyBase method):
7602 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7603 base (method, accessor)
7607 protected override void DefineParameters ()
7609 parameters = Parameters.MergeGenerated (((Indexer)method).parameters,
7610 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
7613 public override bool EnableOverloadChecks (MemberCore overload)
7615 if (base.EnableOverloadChecks (overload)) {
7616 overload.caching_flags |= Flags.MethodOverloadsExist;
7624 const int AllowedModifiers =
7627 Modifiers.PROTECTED |
7628 Modifiers.INTERNAL |
7632 Modifiers.OVERRIDE |
7637 const int AllowedInterfaceModifiers =
7640 public readonly Parameters parameters;
7642 public Indexer (DeclSpace parent, Expression type, MemberName name, int mod,
7643 bool is_iface, Parameters parameters, Attributes attrs,
7644 Accessor get_block, Accessor set_block, bool define_set_first)
7645 : base (parent, type, mod,
7646 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7647 is_iface, name, attrs, define_set_first)
7649 if (type == TypeManager.system_void_expr)
7650 Report.Error (620, name.Location, "An indexer return type cannot be `void'");
7652 this.parameters = parameters;
7654 if (get_block == null)
7655 Get = new GetIndexerMethod (this);
7657 Get = new GetIndexerMethod (this, get_block);
7659 if (set_block == null)
7660 Set = new SetIndexerMethod (this);
7662 Set = new SetIndexerMethod (this, set_block);
7665 public override bool Define ()
7667 if (!DoDefineBase ())
7670 if (!base.Define ())
7673 if (!DefineParameters (parameters))
7676 if (OptAttributes != null && TypeManager.indexer_name_type != null) {
7677 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
7678 if (indexer_attr != null) {
7679 // Remove the attribute from the list because it is not emitted
7680 OptAttributes.Attrs.Remove (indexer_attr);
7682 string name = indexer_attr.GetIndexerAttributeValue ();
7688 if (IsExplicitImpl) {
7689 Report.Error (415, indexer_attr.Location,
7690 "The `IndexerName' attribute is valid only on an " +
7691 "indexer that is not an explicit interface member declaration");
7695 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7696 Report.Error (609, indexer_attr.Location,
7697 "Cannot set the `IndexerName' attribute on an indexer marked override");
7703 if (InterfaceType != null) {
7704 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7705 if (base_IndexerName != Name)
7706 ShortName = base_IndexerName;
7709 if (!Parent.PartialContainer.AddMember (this) ||
7710 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7717 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
7718 if (!Parent.MemberCache.CheckExistingMembersOverloads (this, Name, parameters))
7722 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7724 if (!DefineAccessors ())
7728 // Setup iterator if we are one
7729 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
7730 Iterator iterator = Iterator.CreateIterator (Get, Parent, null, ModFlags);
7731 if (iterator == null)
7737 // Now name the parameters
7739 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7740 FullName, PropertyAttributes.None, MemberType, parameters.Types);
7743 PropertyBuilder.SetGetMethod (GetBuilder);
7744 Parent.MemberCache.AddMember (GetBuilder, Get);
7748 PropertyBuilder.SetSetMethod (SetBuilder);
7749 Parent.MemberCache.AddMember (SetBuilder, Set);
7752 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, parameters.Types);
7753 Parent.MemberCache.AddMember (PropertyBuilder, this);
7757 public override bool EnableOverloadChecks (MemberCore overload)
7759 if (overload is Indexer) {
7760 caching_flags |= Flags.MethodOverloadsExist;
7767 public override string GetDocCommentName (DeclSpace ds)
7769 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7772 public override string GetSignatureForError ()
7774 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7775 if (MemberName.Left != null) {
7777 sb.Append (MemberName.Left.GetSignatureForError ());
7780 sb.Append (".this");
7781 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7782 return sb.ToString ();
7785 protected override PropertyInfo ResolveBaseProperty ()
7787 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7788 Parent.TypeBuilder, Name, parameters.Types, null, true) as PropertyInfo;
7791 protected override bool VerifyClsCompliance ()
7793 if (!base.VerifyClsCompliance ())
7796 parameters.VerifyClsCompliance ();
7801 public class Operator : MethodOrOperator, IAnonymousHost {
7803 const int AllowedModifiers =
7809 public enum OpType : byte {
7819 // Unary and Binary operators
7842 // Implicit and Explicit
7846 // Just because of enum
7850 public readonly OpType OperatorType;
7852 public Operator (DeclSpace parent, OpType type, Expression ret_type,
7853 int mod_flags, Parameters parameters,
7854 ToplevelBlock block, Attributes attrs, Location loc)
7855 : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
7856 new MemberName ("op_" + type.ToString(), loc), attrs, parameters)
7858 OperatorType = type;
7862 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7864 if (a.Type == TypeManager.conditional_attribute_type) {
7865 Error_ConditionalAttributeIsNotValid ();
7869 base.ApplyAttributeBuilder (a, cb);
7872 public override bool Define ()
7874 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7875 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7876 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7880 if (!base.Define ())
7883 // imlicit and explicit operator of same types are not allowed
7884 if (OperatorType == OpType.Explicit)
7885 Parent.MemberCache.CheckExistingMembersOverloads (this, "op_Implicit", Parameters);
7886 else if (OperatorType == OpType.Implicit)
7887 Parent.MemberCache.CheckExistingMembersOverloads (this, "op_Explicit", Parameters);
7889 if (MemberType == TypeManager.void_type) {
7890 Report.Error (590, Location, "User-defined operators cannot return void");
7894 Type declaring_type = MethodData.DeclaringType;
7895 Type return_type = MemberType;
7896 Type first_arg_type = ParameterTypes [0];
7898 Type first_arg_type_unwrap = first_arg_type;
7899 if (TypeManager.IsNullableType (first_arg_type))
7900 first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
7902 Type return_type_unwrap = return_type;
7903 if (TypeManager.IsNullableType (return_type))
7904 return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
7907 // Rules for conversion operators
7909 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7910 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7911 Report.Error (555, Location,
7912 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7917 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7918 conv_type = first_arg_type;
7919 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7920 conv_type = return_type;
7922 Report.Error (556, Location,
7923 "User-defined conversion must convert to or from the enclosing type");
7928 // Because IsInterface and IsClass are not supported
7930 if (!TypeManager.IsGenericParameter (conv_type)) {
7931 if (conv_type.IsInterface) {
7932 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7933 GetSignatureForError ());
7937 if (conv_type.IsClass) {
7938 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7939 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7940 GetSignatureForError ());
7944 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7945 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7946 GetSignatureForError ());
7951 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7952 if (first_arg_type != declaring_type || ParameterTypes [1] != TypeManager.int32_type) {
7953 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");
7956 } else if (Parameters.Count == 1) {
7957 // Checks for Unary operators
7959 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7960 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7961 Report.Error (448, Location,
7962 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7965 if (first_arg_type != declaring_type) {
7967 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7972 if (first_arg_type != declaring_type){
7975 "The parameter of a unary operator must be the " +
7980 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7981 if (return_type != TypeManager.bool_type){
7984 "The return type of operator True or False " +
7991 // Checks for Binary operators
7993 if (first_arg_type != declaring_type &&
7994 ParameterTypes [1] != declaring_type){
7997 "One of the parameters of a binary operator must " +
7998 "be the containing type");
8006 protected override bool DoDefine ()
8008 if (!base.DoDefine ())
8011 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
8015 public override void Emit ()
8019 Parameters.ApplyAttributes (MethodBuilder);
8022 // abstract or extern methods have no bodies
8024 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
8028 if ((flags & MethodAttributes.PinvokeImpl) == 0)
8029 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
8031 ec = CreateEmitContext (Parent, null);
8033 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
8034 ec.EmitTopBlock (this, Block);
8037 source.CloseMethod ();
8042 // Operator cannot be override
8043 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
8048 public static string GetName (OpType ot)
8051 case OpType.LogicalNot:
8053 case OpType.OnesComplement:
8055 case OpType.Increment:
8057 case OpType.Decrement:
8063 case OpType.Addition:
8065 case OpType.Subtraction:
8067 case OpType.UnaryPlus:
8069 case OpType.UnaryNegation:
8071 case OpType.Multiply:
8073 case OpType.Division:
8075 case OpType.Modulus:
8077 case OpType.BitwiseAnd:
8079 case OpType.BitwiseOr:
8081 case OpType.ExclusiveOr:
8083 case OpType.LeftShift:
8085 case OpType.RightShift:
8087 case OpType.Equality:
8089 case OpType.Inequality:
8091 case OpType.GreaterThan:
8093 case OpType.LessThan:
8095 case OpType.GreaterThanOrEqual:
8097 case OpType.LessThanOrEqual:
8099 case OpType.Implicit:
8101 case OpType.Explicit:
8107 public OpType GetMatchingOperator ()
8109 switch (OperatorType) {
8110 case OpType.Equality:
8111 return OpType.Inequality;
8112 case OpType.Inequality:
8113 return OpType.Equality;
8115 return OpType.False;
8118 case OpType.GreaterThan:
8119 return OpType.LessThan;
8120 case OpType.LessThan:
8121 return OpType.GreaterThan;
8122 case OpType.GreaterThanOrEqual:
8123 return OpType.LessThanOrEqual;
8124 case OpType.LessThanOrEqual:
8125 return OpType.GreaterThanOrEqual;
8131 public static OpType GetOperatorType (string name)
8133 if (name.StartsWith ("op_")){
8134 for (int i = 0; i < Unary.oper_names.Length; ++i) {
8135 if (Unary.oper_names [i] == name)
8139 for (int i = 0; i < Binary.oper_names.Length; ++i) {
8140 if (Binary.oper_names [i] == name)
8147 public override string GetSignatureForError ()
8149 StringBuilder sb = new StringBuilder ();
8150 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
8151 sb.AppendFormat ("{0}.{1} operator {2}", Parent.GetSignatureForError (), GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type));
8154 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
8157 sb.Append (Parameters.GetSignatureForError ());
8158 return sb.ToString ();
8163 // This is used to compare method signatures
8165 struct MethodSignature {
8167 public Type RetType;
8168 public Type [] Parameters;
8171 /// This delegate is used to extract methods which have the
8172 /// same signature as the argument
8174 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
8176 public MethodSignature (string name, Type ret_type, Type [] parameters)
8181 if (parameters == null)
8182 Parameters = Type.EmptyTypes;
8184 Parameters = parameters;
8187 public override string ToString ()
8190 if (Parameters.Length != 0){
8191 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
8192 for (int i = 0; i < Parameters.Length; i++){
8193 sb.Append (Parameters [i]);
8194 if (i+1 < Parameters.Length)
8197 pars = sb.ToString ();
8200 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
8203 public override int GetHashCode ()
8205 return Name.GetHashCode ();
8208 public override bool Equals (Object o)
8210 MethodSignature other = (MethodSignature) o;
8212 if (other.Name != Name)
8215 if (other.RetType != RetType)
8218 if (Parameters == null){
8219 if (other.Parameters == null)
8224 if (other.Parameters == null)
8227 int c = Parameters.Length;
8228 if (other.Parameters.Length != c)
8231 for (int i = 0; i < c; i++)
8232 if (other.Parameters [i] != Parameters [i])
8238 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8240 MethodSignature sig = (MethodSignature) filter_criteria;
8242 if (m.Name != sig.Name)
8246 MethodInfo mi = m as MethodInfo;
8247 PropertyInfo pi = m as PropertyInfo;
8250 ReturnType = mi.ReturnType;
8251 else if (pi != null)
8252 ReturnType = pi.PropertyType;
8257 // we use sig.RetType == null to mean `do not check the
8258 // method return value.
8260 if (sig.RetType != null) {
8261 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8267 args = TypeManager.GetParameterData (mi).Types;
8269 args = TypeManager.GetArgumentTypes (pi);
8270 Type [] sigp = sig.Parameters;
8272 if (args.Length != sigp.Length)
8275 for (int i = args.Length - 1; i >= 0; i--)
8276 if (!TypeManager.IsEqual (args [i], sigp [i]))