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 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.CompilerServices;
20 using System.Runtime.InteropServices;
21 using System.Security;
22 using System.Security.Permissions;
25 #if BOOTSTRAP_WITH_OLDLIB || NET_2_1
26 using XmlElement = System.Object;
31 using Mono.CompilerServices.SymbolWriter;
33 namespace Mono.CSharp {
45 /// This is the base class for structs and classes.
47 public abstract class TypeContainer : DeclSpace, IMemberContainer {
49 public class MemberCoreArrayList: ArrayList
52 /// Defines the MemberCore objects that are in this array
54 public virtual void DefineContainerMembers ()
56 foreach (MemberCore mc in this) {
59 } catch (Exception e) {
60 throw new InternalErrorException (mc, e);
65 public virtual void Emit ()
67 foreach (MemberCore mc in this)
72 public class OperatorArrayList: MemberCoreArrayList
74 TypeContainer container;
76 public OperatorArrayList (TypeContainer container)
78 this.container = container;
82 // Checks that some operators come in pairs:
88 // They are matched based on the return type and the argument types
90 void CheckPairedOperators ()
92 bool has_equality_or_inequality = false;
93 Operator[] operators = (Operator[]) ToArray (typeof (Operator));
94 bool [] has_pair = new bool [operators.Length];
96 for (int i = 0; i < Count; ++i) {
97 if (operators [i] == null)
100 Operator o_a = operators [i];
101 Operator.OpType o_type = o_a.OperatorType;
102 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
103 has_equality_or_inequality = true;
105 Operator.OpType matching_type = o_a.GetMatchingOperator ();
106 if (matching_type == Operator.OpType.TOP) {
107 operators [i] = null;
111 for (int ii = 0; ii < Count; ++ii) {
112 Operator o_b = operators [ii];
113 if (o_b == null || o_b.OperatorType != matching_type)
116 if (!TypeManager.IsEqual (o_a.ReturnType, o_b.ReturnType))
119 if (!TypeManager.IsEqual (o_a.ParameterTypes, o_b.ParameterTypes))
122 operators [i] = null;
125 // Used to ignore duplicate user conversions
127 has_pair [ii] = true;
131 for (int i = 0; i < Count; ++i) {
132 if (operators [i] == null || has_pair [i])
135 Operator o = operators [i];
136 Report.Error (216, o.Location,
137 "The operator `{0}' requires a matching operator `{1}' to also be defined",
138 o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
141 if (has_equality_or_inequality && Report.WarningLevel > 2) {
142 if (container.Methods == null || !container.HasEquals)
143 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
145 if (container.Methods == null || !container.HasGetHashCode)
146 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
150 public override void DefineContainerMembers ()
152 base.DefineContainerMembers ();
153 CheckPairedOperators ();
158 // Different context is needed when resolving type container base
159 // types. Type names come from the parent scope but type parameter
160 // names from the container scope.
162 struct BaseContext : IResolveContext
166 public BaseContext (TypeContainer tc)
171 #region IResolveContext Members
173 public DeclSpace DeclContainer {
174 get { return tc.Parent; }
177 public bool IsInObsoleteScope {
178 get { return tc.IsInObsoleteScope; }
181 public bool IsInUnsafeScope {
182 get { return tc.IsInUnsafeScope; }
185 public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
187 return tc.Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
190 public DeclSpace GenericDeclContainer {
191 get { return tc.GenericDeclContainer; }
201 GetHashCode = 1 << 1,
202 HasStaticFieldInitializer = 1 << 2
206 // Whether this is a struct, class or interface
207 public readonly Kind Kind;
209 // Holds a list of classes and structures
210 protected ArrayList types;
212 MemberCoreArrayList ordered_explicit_member_list;
213 MemberCoreArrayList ordered_member_list;
215 // Holds the list of properties
216 MemberCoreArrayList properties;
218 // Holds the list of delegates
219 MemberCoreArrayList delegates;
221 // Holds the list of constructors
222 protected MemberCoreArrayList instance_constructors;
224 // Holds the list of fields
225 protected MemberCoreArrayList fields;
227 // Holds a list of fields that have initializers
228 protected ArrayList initialized_fields;
230 // Holds a list of static fields that have initializers
231 protected ArrayList initialized_static_fields;
233 // Holds the list of constants
234 protected MemberCoreArrayList constants;
236 // Holds the methods.
237 MemberCoreArrayList methods;
240 protected MemberCoreArrayList events;
242 // Holds the indexers
245 // Holds the operators
246 MemberCoreArrayList operators;
248 // Holds the compiler generated classes
249 ArrayList compiler_generated;
252 // Pointers to the default constructor and the default static constructor
254 protected Constructor default_constructor;
255 protected Constructor default_static_constructor;
258 // Points to the first non-static field added to the container.
260 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
261 // and the first one's as good as any.
263 FieldBase first_nonstatic_field = null;
266 // This one is computed after we can distinguish interfaces
267 // from classes from the arraylist `type_bases'
270 TypeExpr[] iface_exprs;
272 GenericTypeParameterBuilder[] nested_gen_params;
274 protected ArrayList type_bases;
276 protected bool members_defined;
277 bool members_defined_ok;
279 // The interfaces we implement.
280 protected Type[] ifaces;
282 // The base member cache and our member cache
283 MemberCache base_cache;
284 protected MemberCache member_cache;
286 public const string DefaultIndexerName = "Item";
288 private bool seen_normal_indexers = false;
289 private string indexer_name = DefaultIndexerName;
290 protected bool requires_delayed_unmanagedtype_check;
292 private CachedMethods cached_method;
294 ArrayList partial_parts;
297 /// The pending methods that need to be implemented
298 // (interfaces or abstract methods)
300 PendingImplementation pending;
302 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
303 Attributes attrs, Kind kind)
304 : base (ns, parent, name, attrs)
306 if (parent != null && parent.NamespaceEntry != ns)
307 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
310 this.PartialContainer = this;
313 public bool AddMember (MemberCore symbol)
315 return AddToContainer (symbol, symbol.MemberName.Basename);
318 protected virtual bool AddMemberType (DeclSpace ds)
320 return AddToContainer (ds, ds.Basename);
323 protected virtual void RemoveMemberType (DeclSpace ds)
325 RemoveFromContainer (ds.Basename);
328 public void AddConstant (Const constant)
330 if (!AddMember (constant))
333 if (constants == null)
334 constants = new MemberCoreArrayList ();
336 constants.Add (constant);
339 public TypeContainer AddTypeContainer (TypeContainer tc)
341 if (!AddMemberType (tc))
345 types = new MemberCoreArrayList ();
351 public virtual TypeContainer AddPartial (TypeContainer next_part)
353 return AddPartial (next_part, next_part.Basename);
356 protected TypeContainer AddPartial (TypeContainer next_part, string name)
358 next_part.ModFlags |= Modifiers.PARTIAL;
359 TypeContainer tc = defined_names [name] as TypeContainer;
362 return AddTypeContainer (next_part);
364 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
365 Report.SymbolRelatedToPreviousError (next_part);
366 Error_MissingPartialModifier (tc);
369 if (tc.Kind != next_part.Kind) {
370 Report.SymbolRelatedToPreviousError (tc);
371 Report.Error (261, next_part.Location,
372 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
373 next_part.GetSignatureForError ());
376 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
377 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
378 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
379 Report.SymbolRelatedToPreviousError (tc);
380 Report.Error (262, next_part.Location,
381 "Partial declarations of `{0}' have conflicting accessibility modifiers",
382 next_part.GetSignatureForError ());
385 if (tc.partial_parts == null)
386 tc.partial_parts = new ArrayList (1);
388 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
389 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
390 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
391 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
392 tc.ModFlags |= next_part.ModFlags;
394 tc.ModFlags |= next_part.ModFlags;
397 if (next_part.attributes != null) {
398 if (tc.attributes == null)
399 tc.attributes = next_part.attributes;
401 tc.attributes.AddAttributes (next_part.attributes.Attrs);
404 next_part.PartialContainer = tc;
405 tc.partial_parts.Add (next_part);
409 public virtual void RemoveTypeContainer (TypeContainer next_part)
412 types.Remove (next_part);
413 RemoveMemberType (next_part);
416 public void AddDelegate (Delegate d)
418 if (!AddMemberType (d))
421 if (delegates == null)
422 delegates = new MemberCoreArrayList ();
427 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
429 if (ordered_explicit_member_list == null) {
430 ordered_explicit_member_list = new MemberCoreArrayList ();
431 ordered_member_list = new MemberCoreArrayList ();
435 if (Kind == Kind.Interface) {
436 Report.Error (541, mc.Location,
437 "`{0}': explicit interface declaration can only be declared in a class or struct",
438 mc.GetSignatureForError ());
441 ordered_explicit_member_list.Add (mc);
442 alist.Insert (0, mc);
444 ordered_member_list.Add (mc);
450 public void AddMethod (MethodOrOperator method)
452 if (!AddToContainer (method, method.MemberName.Basename))
456 methods = new MemberCoreArrayList ();
458 if (method.MemberName.Left != null)
459 AddMemberToList (method, methods, true);
461 AddMemberToList (method, methods, false);
464 public void AddConstructor (Constructor c)
466 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
467 if (!AddToContainer (c, is_static ?
468 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
471 if (is_static && c.Parameters.IsEmpty){
472 if (default_static_constructor != null) {
473 Report.SymbolRelatedToPreviousError (default_static_constructor);
474 Report.Error (111, c.Location,
475 "A member `{0}' is already defined. Rename this member or use different parameter types",
476 c.GetSignatureForError ());
480 default_static_constructor = c;
482 if (c.Parameters.IsEmpty)
483 default_constructor = c;
485 if (instance_constructors == null)
486 instance_constructors = new MemberCoreArrayList ();
488 instance_constructors.Add (c);
492 public bool AddField (FieldBase field)
494 if (!AddMember (field))
498 fields = new MemberCoreArrayList ();
502 if ((field.ModFlags & Modifiers.STATIC) != 0)
505 if (first_nonstatic_field == null) {
506 first_nonstatic_field = field;
510 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
511 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
512 Report.Warning (282, 3, field.Location,
513 "struct instance field `{0}' found in different declaration from instance field `{1}'",
514 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
519 public void AddProperty (Property prop)
521 if (!AddMember (prop) ||
522 !AddMember (prop.Get) || !AddMember (prop.Set))
525 if (properties == null)
526 properties = new MemberCoreArrayList ();
528 if (prop.MemberName.Left != null)
529 AddMemberToList (prop, properties, true);
531 AddMemberToList (prop, properties, false);
534 public void AddEvent (Event e)
539 if (e is EventProperty) {
540 if (!AddMember (e.Add))
543 if (!AddMember (e.Remove))
548 events = new MemberCoreArrayList ();
554 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
556 public void AddIndexer (Indexer i)
558 if (indexers == null)
559 indexers = new ArrayList ();
561 if (i.IsExplicitImpl)
562 AddMemberToList (i, indexers, true);
564 AddMemberToList (i, indexers, false);
567 public void AddOperator (Operator op)
572 if (operators == null)
573 operators = new OperatorArrayList (this);
578 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
580 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
582 if (compiler_generated == null)
583 compiler_generated = new ArrayList ();
585 compiler_generated.Add (c);
588 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
590 if (a.Type == pa.DefaultMember) {
591 if (Indexers != null) {
592 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
597 base.ApplyAttributeBuilder (a, cb, pa);
600 public override AttributeTargets AttributeTargets {
602 throw new NotSupportedException ();
606 public ArrayList Types {
612 public MemberCoreArrayList Methods {
618 public ArrayList Constants {
624 protected Type BaseType {
626 return TypeBuilder.BaseType;
630 public ArrayList Fields {
636 public ArrayList InstanceConstructors {
638 return instance_constructors;
642 public ArrayList Properties {
648 public ArrayList Events {
654 public ArrayList Indexers {
660 public ArrayList Operators {
666 public ArrayList Delegates {
672 protected override TypeAttributes TypeAttr {
674 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
678 public string IndexerName {
680 return indexers == null ? DefaultIndexerName : indexer_name;
684 public bool IsComImport {
686 if (OptAttributes == null)
689 return OptAttributes.Contains (PredefinedAttributes.Get.ComImport);
693 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
695 if ((field.ModFlags & Modifiers.STATIC) != 0){
696 if (initialized_static_fields == null) {
697 PartialContainer.HasStaticFieldInitializer = true;
698 initialized_static_fields = new ArrayList (4);
701 initialized_static_fields.Add (expression);
703 if (initialized_fields == null)
704 initialized_fields = new ArrayList (4);
706 initialized_fields.Add (expression);
710 public void ResolveFieldInitializers (EmitContext ec)
712 if (partial_parts != null) {
713 foreach (TypeContainer part in partial_parts) {
714 part.DoResolveFieldInitializers (ec);
717 DoResolveFieldInitializers (ec);
720 void DoResolveFieldInitializers (EmitContext ec)
723 if (initialized_static_fields == null)
726 bool has_complex_initializer = !RootContext.Optimize;
728 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
729 for (i = 0; i < initialized_static_fields.Count; ++i) {
730 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
731 ExpressionStatement s = fi.ResolveStatement (ec);
733 s = EmptyExpressionStatement.Instance;
734 } else if (fi.IsComplexInitializer) {
735 has_complex_initializer |= true;
741 for (i = 0; i < initialized_static_fields.Count; ++i) {
742 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
744 // Need special check to not optimize code like this
745 // static int a = b = 5;
748 if (!has_complex_initializer && fi.IsDefaultInitializer)
751 ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
757 if (initialized_fields == null)
760 for (int i = 0; i < initialized_fields.Count; ++i) {
761 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
762 ExpressionStatement s = fi.ResolveStatement (ec);
767 // Field is re-initialized to its default value => removed
769 if (fi.IsDefaultInitializer && RootContext.Optimize)
772 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
776 public override string DocComment {
788 public PendingImplementation PendingImplementations {
789 get { return pending; }
792 public override bool GetClsCompliantAttributeValue ()
794 if (PartialContainer != this)
795 return PartialContainer.GetClsCompliantAttributeValue ();
797 return base.GetClsCompliantAttributeValue ();
800 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
802 // FIXME: get rid of partial_parts and store lists of bases of each part here
803 // assumed, not verified: 'part' is in 'partial_parts'
804 ((TypeContainer) part).type_bases = bases;
808 /// This function computes the Base class and also the
809 /// list of interfaces that the class or struct @c implements.
811 /// The return value is an array (might be null) of
812 /// interfaces implemented (as Types).
814 /// The @base_class argument is set to the base object or null
815 /// if this is `System.Object'.
817 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
820 if (type_bases == null)
823 int count = type_bases.Count;
824 TypeExpr [] ifaces = null;
825 IResolveContext base_context = new BaseContext (this);
826 for (int i = 0, j = 0; i < count; i++){
827 FullNamedExpression fne = (FullNamedExpression) type_bases [i];
830 // Standard ResolveAsTypeTerminal cannot be used in this case because
831 // it does ObsoleteAttribute and constraint checks which require
832 // base type to be set
834 TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (base_context, false);
835 if (fne_resolved == null)
838 if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
839 if (fne_resolved is DynamicTypeExpr)
840 Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
841 GetSignatureForError ());
843 base_class = fne_resolved;
848 ifaces = new TypeExpr [count - i];
850 if (fne_resolved.Type.IsInterface) {
851 for (int ii = 0; ii < j; ++ii) {
852 if (TypeManager.IsEqual (fne_resolved.Type, ifaces [ii].Type)) {
853 Report.Error (528, Location, "`{0}' is already listed in interface list",
854 fne_resolved.GetSignatureForError ());
859 if (Kind == Kind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
860 Report.Error (61, fne.Location,
861 "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
862 fne_resolved.GetSignatureForError (), GetSignatureForError ());
865 Report.SymbolRelatedToPreviousError (fne_resolved.Type);
866 if (Kind != Kind.Class) {
867 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
868 } else if (base_class != null)
869 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
870 GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
872 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
873 GetSignatureForError (), fne_resolved.GetSignatureForError ());
877 ifaces [j++] = fne_resolved;
883 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
885 ArrayList ifaces = new ArrayList (0);
886 if (iface_exprs != null)
887 ifaces.AddRange (iface_exprs);
889 foreach (TypeContainer part in partial_parts) {
890 TypeExpr new_base_class;
891 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
892 if (new_base_class != TypeManager.system_object_expr) {
893 if (base_class == TypeManager.system_object_expr)
894 base_class = new_base_class;
896 if (new_base_class != null && !new_base_class.Equals (base_class)) {
897 Report.SymbolRelatedToPreviousError (base_class.Location, "");
898 Report.Error (263, part.Location,
899 "Partial declarations of `{0}' must not specify different base classes",
900 part.GetSignatureForError ());
907 if (new_ifaces == null)
910 foreach (TypeExpr iface in new_ifaces) {
911 if (ifaces.Contains (iface))
918 if (ifaces.Count == 0)
921 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
924 bool CheckGenericInterfaces (Type[] ifaces)
927 ArrayList already_checked = new ArrayList ();
929 for (int i = 0; i < ifaces.Length; i++) {
930 Type iface = ifaces [i];
931 foreach (Type t in already_checked) {
935 Type[] inferred = new Type [CountTypeParameters];
936 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
939 Report.Error (695, Location,
940 "`{0}' cannot implement both `{1}' and `{2}' " +
941 "because they may unify for some type parameter substitutions",
942 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
943 TypeManager.CSharpName (t));
947 already_checked.Add (iface);
956 bool CreateTypeBuilder ()
959 Type default_parent = null;
960 if (Kind == Kind.Struct)
961 default_parent = TypeManager.value_type;
962 else if (Kind == Kind.Enum)
963 default_parent = TypeManager.enum_type;
966 // Sets .size to 1 for structs with no instance fields
968 int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
971 if (TypeManager.NamespaceClash (Name, Location)) {
975 ModuleBuilder builder = Module.Builder;
976 TypeBuilder = builder.DefineType (
977 Name, TypeAttr, default_parent, type_size);
979 TypeBuilder builder = Parent.TypeBuilder;
981 TypeBuilder = builder.DefineNestedType (
982 Basename, TypeAttr, default_parent, type_size);
984 } catch (ArgumentException) {
985 Report.RuntimeMissingSupport (Location, "static classes");
989 TypeManager.AddUserType (this);
992 string[] param_names = new string [TypeParameters.Length];
993 for (int i = 0; i < TypeParameters.Length; i++)
994 param_names [i] = TypeParameters [i].Name;
997 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
999 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1001 nested_gen_params = new GenericTypeParameterBuilder [offset];
1002 Array.Copy (gen_params, nested_gen_params, offset);
1005 for (int i = offset; i < gen_params.Length; i++)
1006 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1008 nested_gen_params = null;
1009 throw new NotSupportedException ();
1016 bool DefineBaseTypes ()
1018 iface_exprs = ResolveBaseTypes (out base_type);
1019 if (partial_parts != null) {
1020 iface_exprs = GetNormalPartialBases (ref base_type);
1024 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1025 // which in turn should have called DefineType()s on base types if necessary.
1027 // None of the code below should trigger DefineType()s on classes that we depend on.
1028 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1030 // Let's do it as soon as possible, since code below can call DefineType() on classes
1031 // that depend on us to be populated before they are.
1033 if (!(this is CompilerGeneratedClass))
1034 RootContext.RegisterOrder (this);
1036 if (!CheckRecursiveDefinition (this))
1039 if (base_type != null && base_type.Type != null) {
1040 TypeBuilder.SetParent (base_type.Type);
1043 // add interfaces that were not added at type creation
1044 if (iface_exprs != null) {
1045 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1049 foreach (Type itype in ifaces)
1050 TypeBuilder.AddInterfaceImplementation (itype);
1052 if (!CheckGenericInterfaces (ifaces))
1055 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1062 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1064 public TypeBuilder CreateType ()
1066 if (TypeBuilder != null)
1072 if (!CreateTypeBuilder ()) {
1077 if (partial_parts != null) {
1078 foreach (TypeContainer part in partial_parts)
1079 part.TypeBuilder = TypeBuilder;
1082 if (Types != null) {
1083 foreach (TypeContainer tc in Types) {
1084 if (tc.CreateType () == null) {
1096 public override TypeBuilder DefineType ()
1103 type_defined = true;
1105 if (CreateType () == null) {
1110 if (!DefineBaseTypes ()) {
1115 if (!DefineNestedTypes ()) {
1123 public override void SetParameterInfo (ArrayList constraints_list)
1125 base.SetParameterInfo (constraints_list);
1127 if (!is_generic || PartialContainer == this)
1130 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1131 for (int i = 0; i < tc_names.Length; ++i) {
1132 if (tc_names [i].Name != type_params [i].Name) {
1133 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1134 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1135 GetSignatureForError ());
1141 void UpdateTypeParameterConstraints (TypeContainer part)
1143 TypeParameter[] current_params = CurrentTypeParameters;
1144 for (int i = 0; i < current_params.Length; i++) {
1145 Constraints c = part.CurrentTypeParameters [i].Constraints;
1149 if (current_params [i].UpdateConstraints (part, c))
1152 Report.SymbolRelatedToPreviousError (Location, "");
1153 Report.Error (265, part.Location,
1154 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1155 GetSignatureForError (), current_params [i].GetSignatureForError ());
1159 public bool ResolveType ()
1161 if (!DoResolveType ())
1164 if (compiler_generated != null) {
1165 foreach (CompilerGeneratedClass c in compiler_generated)
1166 if (!c.ResolveType ())
1173 protected virtual bool DoResolveType ()
1178 if (PartialContainer != this)
1179 throw new InternalErrorException ();
1181 TypeExpr current_type = null;
1183 foreach (TypeParameter type_param in CurrentTypeParameters) {
1184 if (!type_param.Resolve (this)) {
1190 if (partial_parts != null && is_generic) {
1191 foreach (TypeContainer part in partial_parts)
1192 UpdateTypeParameterConstraints (part);
1195 for (int i = 0; i < TypeParameters.Length; ++i) {
1197 // FIXME: Same should be done for delegates
1198 // TODO: Quite ugly way how to propagate constraints to
1201 if (nested_gen_params != null && i < nested_gen_params.Length) {
1202 TypeParameters [i].SetConstraints (nested_gen_params [i]);
1204 if (!TypeParameters [i].DefineType (this)) {
1211 // TODO: Very strange, why not simple make generic type from
1212 // current type parameters
1213 current_type = new GenericTypeExpr (this, Location);
1214 current_type = current_type.ResolveAsTypeTerminal (this, false);
1215 if (current_type == null) {
1220 CurrentType = current_type.Type;
1224 protected virtual bool DefineNestedTypes ()
1226 if (Types != null) {
1227 foreach (TypeContainer tc in Types)
1228 if (tc.DefineType () == null)
1232 if (Delegates != null) {
1233 foreach (Delegate d in Delegates)
1234 if (d.DefineType () == null)
1241 TypeContainer InTransit;
1243 protected bool CheckRecursiveDefinition (TypeContainer tc)
1245 if (InTransit != null) {
1246 Report.SymbolRelatedToPreviousError (this);
1247 if (this is Interface)
1249 529, tc.Location, "Inherited interface `{0}' causes a " +
1250 "cycle in the interface hierarchy of `{1}'",
1251 GetSignatureForError (), tc.GetSignatureForError ());
1254 146, tc.Location, "Circular base class dependency " +
1255 "involving `{0}' and `{1}'",
1256 tc.GetSignatureForError (), GetSignatureForError ());
1262 if (base_type != null && base_type.Type != null) {
1263 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1264 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1265 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1269 if (iface_exprs != null) {
1270 foreach (TypeExpr iface in iface_exprs) {
1271 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1272 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1273 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1278 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1286 /// Populates our TypeBuilder with fields and methods
1288 public override bool Define ()
1290 if (members_defined)
1291 return members_defined_ok;
1293 members_defined_ok = DoDefineMembers ();
1294 members_defined = true;
1296 return members_defined_ok;
1299 protected virtual bool DoDefineMembers ()
1301 if (iface_exprs != null) {
1302 foreach (TypeExpr iface in iface_exprs) {
1303 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
1304 if ((oa != null) && !IsInObsoleteScope)
1305 AttributeTester.Report_ObsoleteMessage (
1306 oa, iface.GetSignatureForError (), Location);
1308 GenericTypeExpr ct = iface as GenericTypeExpr;
1310 // TODO: passing `this' is wrong, should be base type iface instead
1311 TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
1313 if (!ct.CheckConstraints (this))
1319 if (base_type != null) {
1320 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1321 if (obsolete_attr != null && !IsInObsoleteScope)
1322 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1324 GenericTypeExpr ct = base_type as GenericTypeExpr;
1325 if ((ct != null) && !ct.CheckConstraints (this))
1328 TypeContainer baseContainer = TypeManager.LookupTypeContainer(base_type.Type);
1329 if (baseContainer != null)
1330 baseContainer.Define();
1332 member_cache = new MemberCache (base_type.Type, this);
1333 } else if (Kind == Kind.Interface) {
1334 member_cache = new MemberCache (null, this);
1335 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1336 for (int i = 0; i < ifaces.Length; ++i)
1337 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1339 member_cache = new MemberCache (null, this);
1343 foreach (TypeContainer tc in types)
1344 member_cache.AddNestedType (tc);
1346 if (delegates != null)
1347 foreach (Delegate d in delegates)
1348 member_cache.AddNestedType (d);
1350 if (partial_parts != null) {
1351 foreach (TypeContainer part in partial_parts)
1352 part.member_cache = member_cache;
1356 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1357 if (conflict_symbol == null) {
1358 if ((ModFlags & Modifiers.NEW) != 0)
1359 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1361 if ((ModFlags & Modifiers.NEW) == 0) {
1362 Report.SymbolRelatedToPreviousError (conflict_symbol);
1363 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1364 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1369 DefineContainerMembers (constants);
1370 DefineContainerMembers (fields);
1372 if (Kind == Kind.Struct || Kind == Kind.Class) {
1373 pending = PendingImplementation.GetPendingImplementations (this);
1375 if (requires_delayed_unmanagedtype_check) {
1376 requires_delayed_unmanagedtype_check = false;
1377 foreach (FieldBase f in fields) {
1378 if (f.MemberType != null && f.MemberType.IsPointer)
1379 TypeManager.VerifyUnManaged (f.MemberType, f.Location);
1385 // Constructors are not in the defined_names array
1387 DefineContainerMembers (instance_constructors);
1389 DefineContainerMembers (events);
1390 DefineContainerMembers (ordered_explicit_member_list);
1391 DefineContainerMembers (ordered_member_list);
1393 DefineContainerMembers (operators);
1394 DefineContainerMembers (delegates);
1396 ComputeIndexerName();
1397 CheckEqualsAndGetHashCode();
1399 if (CurrentType != null) {
1400 GenericType = CurrentType;
1404 // FIXME: This hack is needed because member cache does not work
1405 // with generic types, we rely on runtime to inflate dynamic types.
1406 // TODO: This hack requires member cache refactoring to be removed
1408 if (TypeManager.IsGenericType (TypeBuilder))
1409 member_cache = new MemberCache (this);
1414 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1417 mcal.DefineContainerMembers ();
1420 protected virtual void ComputeIndexerName ()
1422 if (indexers == null)
1425 string class_indexer_name = null;
1428 // If there's both an explicit and an implicit interface implementation, the
1429 // explicit one actually implements the interface while the other one is just
1430 // a normal indexer. See bug #37714.
1433 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1434 foreach (Indexer i in indexers) {
1435 if (i.InterfaceType != null) {
1436 if (seen_normal_indexers)
1437 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1441 seen_normal_indexers = true;
1443 if (class_indexer_name == null) {
1444 class_indexer_name = i.ShortName;
1448 if (i.ShortName != class_indexer_name)
1449 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");
1452 if (class_indexer_name != null)
1453 indexer_name = class_indexer_name;
1456 protected virtual void EmitIndexerName ()
1458 if (!seen_normal_indexers)
1461 PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
1462 if (pa.Constructor == null &&
1463 !pa.ResolveConstructor (Location, TypeManager.string_type))
1466 CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { IndexerName });
1467 TypeBuilder.SetCustomAttribute (cb);
1470 protected virtual void CheckEqualsAndGetHashCode ()
1472 if (methods == null)
1475 if (HasEquals && !HasGetHashCode) {
1476 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1480 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1482 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1486 /// This function is based by a delegate to the FindMembers routine
1488 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1494 /// This filter is used by FindMembers, and we just keep
1495 /// a global for the filter to `AlwaysAccept'
1497 static MemberFilter accepting_filter;
1500 static TypeContainer ()
1502 accepting_filter = new MemberFilter (AlwaysAccept);
1505 public MethodInfo[] GetMethods ()
1507 ArrayList members = new ArrayList ();
1511 if (methods != null) {
1512 int len = methods.Count;
1513 for (int i = 0; i < len; i++) {
1514 Method m = (Method) methods [i];
1516 members.Add (m.MethodBuilder);
1520 if (operators != null) {
1521 int len = operators.Count;
1522 for (int i = 0; i < len; i++) {
1523 Operator o = (Operator) operators [i];
1525 members.Add (o.MethodBuilder);
1529 if (properties != null) {
1530 int len = properties.Count;
1531 for (int i = 0; i < len; i++) {
1532 Property p = (Property) properties [i];
1534 if (p.GetBuilder != null)
1535 members.Add (p.GetBuilder);
1536 if (p.SetBuilder != null)
1537 members.Add (p.SetBuilder);
1541 if (indexers != null) {
1542 int len = indexers.Count;
1543 for (int i = 0; i < len; i++) {
1544 Indexer ix = (Indexer) indexers [i];
1546 if (ix.GetBuilder != null)
1547 members.Add (ix.GetBuilder);
1548 if (ix.SetBuilder != null)
1549 members.Add (ix.SetBuilder);
1553 if (events != null) {
1554 int len = events.Count;
1555 for (int i = 0; i < len; i++) {
1556 Event e = (Event) events [i];
1558 if (e.AddBuilder != null)
1559 members.Add (e.AddBuilder);
1560 if (e.RemoveBuilder != null)
1561 members.Add (e.RemoveBuilder);
1565 MethodInfo[] retMethods = new MethodInfo [members.Count];
1566 members.CopyTo (retMethods, 0);
1570 // Indicated whether container has StructLayout attribute set Explicit
1571 public bool HasExplicitLayout {
1572 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1573 set { caching_flags |= Flags.HasExplicitLayout; }
1576 public bool HasStructLayout {
1577 get { return (caching_flags & Flags.HasStructLayout) != 0; }
1578 set { caching_flags |= Flags.HasStructLayout; }
1582 // Return the nested type with name @name. Ensures that the nested type
1583 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1585 public Type FindNestedType (string name)
1587 if (PartialContainer != this)
1588 throw new InternalErrorException ("should not happen");
1590 ArrayList [] lists = { types, delegates };
1592 for (int j = 0; j < lists.Length; ++j) {
1593 ArrayList list = lists [j];
1597 int len = list.Count;
1598 for (int i = 0; i < len; ++i) {
1599 DeclSpace ds = (DeclSpace) list [i];
1600 if (ds.Basename == name) {
1601 return ds.DefineType ();
1609 private void FindMembers_NestedTypes (int modflags,
1610 BindingFlags bf, MemberFilter filter, object criteria,
1611 ref ArrayList members)
1613 ArrayList [] lists = { types, delegates };
1615 for (int j = 0; j < lists.Length; ++j) {
1616 ArrayList list = lists [j];
1620 int len = list.Count;
1621 for (int i = 0; i < len; i++) {
1622 DeclSpace ds = (DeclSpace) list [i];
1624 if ((ds.ModFlags & modflags) == 0)
1627 TypeBuilder tb = ds.TypeBuilder;
1629 if (!(criteria is string) || ds.Basename.Equals (criteria))
1630 tb = ds.DefineType ();
1633 if (tb != null && (filter (tb, criteria) == true)) {
1634 if (members == null)
1635 members = new ArrayList ();
1644 /// This method returns the members of this type just like Type.FindMembers would
1645 /// Only, we need to use this for types which are _being_ defined because MS'
1646 /// implementation can't take care of that.
1649 // FIXME: return an empty static array instead of null, that cleans up
1650 // some code and is consistent with some coding conventions I just found
1654 // Notice that in various cases we check if our field is non-null,
1655 // something that would normally mean that there was a bug elsewhere.
1657 // The problem happens while we are defining p-invoke methods, as those
1658 // will trigger a FindMembers, but this happens before things are defined
1660 // Since the whole process is a no-op, it is fine to check for null here.
1662 // TODO: This approach will be one day completely removed, it's already
1663 // used at few places only
1666 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1667 MemberFilter filter, object criteria)
1669 ArrayList members = null;
1672 if ((bf & BindingFlags.Public) != 0)
1673 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1675 if ((bf & BindingFlags.NonPublic) != 0)
1676 modflags |= Modifiers.PRIVATE;
1678 int static_mask = 0, static_flags = 0;
1679 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1680 case BindingFlags.Static:
1681 static_mask = static_flags = Modifiers.STATIC;
1684 case BindingFlags.Instance:
1685 static_mask = Modifiers.STATIC;
1690 static_mask = static_flags = 0;
1694 Timer.StartTimer (TimerType.TcFindMembers);
1697 filter = accepting_filter;
1699 if ((mt & MemberTypes.Field) != 0) {
1700 if (fields != null) {
1701 int len = fields.Count;
1702 for (int i = 0; i < len; i++) {
1703 FieldBase f = (FieldBase) fields [i];
1705 if ((f.ModFlags & modflags) == 0)
1707 if ((f.ModFlags & static_mask) != static_flags)
1710 FieldBuilder fb = f.FieldBuilder;
1711 if (fb != null && filter (fb, criteria) == true) {
1712 if (members == null)
1713 members = new ArrayList ();
1720 if (constants != null) {
1721 int len = constants.Count;
1722 for (int i = 0; i < len; i++) {
1723 Const con = (Const) constants [i];
1725 if ((con.ModFlags & modflags) == 0)
1727 if ((con.ModFlags & static_mask) != static_flags)
1730 FieldBuilder fb = con.FieldBuilder;
1733 fb = con.FieldBuilder;
1735 if (fb != null && filter (fb, criteria) == true) {
1736 if (members == null)
1737 members = new ArrayList ();
1745 if ((mt & MemberTypes.Method) != 0) {
1746 if (methods != null) {
1747 int len = methods.Count;
1748 for (int i = 0; i < len; i++) {
1749 MethodOrOperator m = (MethodOrOperator) methods [i];
1751 if ((m.ModFlags & modflags) == 0)
1753 if ((m.ModFlags & static_mask) != static_flags)
1756 MethodBuilder mb = m.MethodBuilder;
1758 if (mb != null && filter (mb, criteria) == true) {
1759 if (members == null)
1760 members = new ArrayList ();
1767 if (operators != null) {
1768 int len = operators.Count;
1769 for (int i = 0; i < len; i++) {
1770 Operator o = (Operator) operators [i];
1772 if ((o.ModFlags & modflags) == 0)
1774 if ((o.ModFlags & static_mask) != static_flags)
1777 MethodBuilder ob = o.MethodBuilder;
1778 if (ob != null && filter (ob, criteria) == true) {
1779 if (members == null)
1780 members = new ArrayList ();
1787 if (events != null) {
1788 foreach (Event e in events) {
1789 if ((e.ModFlags & modflags) == 0)
1791 if ((e.ModFlags & static_mask) != static_flags)
1794 MethodBuilder b = e.AddBuilder;
1795 if (b != null && filter (b, criteria)) {
1796 if (members == null)
1797 members = new ArrayList (4);
1802 b = e.RemoveBuilder;
1803 if (b != null && filter (b, criteria)) {
1804 if (members == null)
1805 members = new ArrayList (4);
1812 if (properties != null) {
1813 int len = properties.Count;
1814 for (int i = 0; i < len; i++) {
1815 Property p = (Property) properties [i];
1817 if ((p.ModFlags & modflags) == 0)
1819 if ((p.ModFlags & static_mask) != static_flags)
1825 if (b != null && filter (b, criteria) == true) {
1826 if (members == null)
1827 members = new ArrayList ();
1833 if (b != null && filter (b, criteria) == true) {
1834 if (members == null)
1835 members = new ArrayList ();
1842 if (indexers != null) {
1843 int len = indexers.Count;
1844 for (int i = 0; i < len; i++) {
1845 Indexer ix = (Indexer) indexers [i];
1847 if ((ix.ModFlags & modflags) == 0)
1849 if ((ix.ModFlags & static_mask) != static_flags)
1855 if (b != null && filter (b, criteria) == true) {
1856 if (members == null)
1857 members = new ArrayList ();
1863 if (b != null && filter (b, criteria) == true) {
1864 if (members == null)
1865 members = new ArrayList ();
1873 if ((mt & MemberTypes.Event) != 0) {
1874 if (events != null) {
1875 int len = events.Count;
1876 for (int i = 0; i < len; i++) {
1877 Event e = (Event) events [i];
1879 if ((e.ModFlags & modflags) == 0)
1881 if ((e.ModFlags & static_mask) != static_flags)
1884 MemberInfo eb = e.EventBuilder;
1885 if (eb != null && filter (eb, criteria) == true) {
1886 if (members == null)
1887 members = new ArrayList ();
1889 members.Add (e.EventBuilder);
1895 if ((mt & MemberTypes.Property) != 0){
1896 if (properties != null) {
1897 int len = properties.Count;
1898 for (int i = 0; i < len; i++) {
1899 Property p = (Property) properties [i];
1901 if ((p.ModFlags & modflags) == 0)
1903 if ((p.ModFlags & static_mask) != static_flags)
1906 MemberInfo pb = p.PropertyBuilder;
1907 if (pb != null && filter (pb, criteria) == true) {
1908 if (members == null)
1909 members = new ArrayList ();
1911 members.Add (p.PropertyBuilder);
1916 if (indexers != null) {
1917 int len = indexers.Count;
1918 for (int i = 0; i < len; i++) {
1919 Indexer ix = (Indexer) indexers [i];
1921 if ((ix.ModFlags & modflags) == 0)
1923 if ((ix.ModFlags & static_mask) != static_flags)
1926 MemberInfo ib = ix.PropertyBuilder;
1927 if (ib != null && filter (ib, criteria) == true) {
1928 if (members == null)
1929 members = new ArrayList ();
1931 members.Add (ix.PropertyBuilder);
1937 if ((mt & MemberTypes.NestedType) != 0)
1938 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1940 if ((mt & MemberTypes.Constructor) != 0){
1941 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1942 int len = instance_constructors.Count;
1943 for (int i = 0; i < len; i++) {
1944 Constructor c = (Constructor) instance_constructors [i];
1946 ConstructorBuilder cb = c.ConstructorBuilder;
1947 if (cb != null && filter (cb, criteria) == true) {
1948 if (members == null)
1949 members = new ArrayList ();
1956 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1957 ConstructorBuilder cb =
1958 default_static_constructor.ConstructorBuilder;
1960 if (cb != null && filter (cb, criteria) == true) {
1961 if (members == null)
1962 members = new ArrayList ();
1970 // Lookup members in base if requested.
1972 if ((bf & BindingFlags.DeclaredOnly) == 0) {
1973 if (TypeBuilder.BaseType != null) {
1974 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1975 if (list.Count > 0) {
1976 if (members == null)
1977 members = new ArrayList ();
1979 members.AddRange (list);
1984 Timer.StopTimer (TimerType.TcFindMembers);
1986 if (members == null)
1987 return MemberList.Empty;
1989 return new MemberList (members);
1992 public override MemberCache MemberCache {
1994 return member_cache;
1998 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1999 MemberFilter filter, object criteria)
2001 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2004 return ds.FindMembers (mt, bf, filter, criteria);
2006 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2010 /// Emits the values for the constants
2012 public void EmitConstants ()
2014 if (constants != null)
2015 foreach (Const con in constants)
2020 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2025 foreach (MemberCore mc in al) {
2026 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2029 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2030 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2035 public virtual void VerifyMembers ()
2038 // Check for internal or private fields that were never assigned
2040 if (Report.WarningLevel >= 3) {
2041 CheckMemberUsage (properties, "property");
2042 CheckMemberUsage (methods, "method");
2043 CheckMemberUsage (constants, "constant");
2045 if (fields != null){
2046 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2047 foreach (FieldBase f in fields) {
2048 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2049 if (is_type_exposed)
2052 f.SetMemberIsUsed ();
2056 if ((f.caching_flags & Flags.IsAssigned) == 0)
2057 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2060 const int error_code = 414;
2062 const int error_code = 169;
2064 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2065 f.GetSignatureForError ());
2071 // Only report 649 on level 4
2073 if (Report.WarningLevel < 4)
2076 if ((f.caching_flags & Flags.IsAssigned) != 0)
2080 // Don't be pendatic over serializable attributes
2082 if (f.OptAttributes != null || PartialContainer.HasStructLayout)
2085 Constant c = New.Constantify (f.MemberType);
2086 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2087 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2093 // TODO: move to ClassOrStruct
2094 void EmitConstructors ()
2096 if (instance_constructors == null)
2099 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2100 bool has_compliant_args = false;
2102 foreach (Constructor c in instance_constructors) {
2106 catch (Exception e) {
2107 throw new InternalErrorException (c, e);
2110 if (has_compliant_args)
2113 has_compliant_args = c.HasCompliantArgs;
2115 if (!has_compliant_args)
2116 Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2118 foreach (Constructor c in instance_constructors) {
2122 catch (Exception e) {
2123 throw new InternalErrorException (c, e);
2130 /// Emits the code, this step is performed after all
2131 /// the types, enumerations, constructors
2133 public virtual void EmitType ()
2135 if (OptAttributes != null)
2136 OptAttributes.Emit ();
2140 EmitConstructors ();
2142 // Can not continue if constants are broken
2144 if (Report.Errors > 0)
2147 if (default_static_constructor != null)
2148 default_static_constructor.Emit ();
2150 if (operators != null)
2151 foreach (Operator o in operators)
2154 if (properties != null)
2155 foreach (Property p in properties)
2158 if (indexers != null) {
2159 foreach (Indexer indx in indexers)
2164 if (events != null){
2165 foreach (Event e in Events)
2169 if (methods != null) {
2170 for (int i = 0; i < methods.Count; ++i)
2171 ((MethodOrOperator) methods [i]).Emit ();
2175 foreach (FieldBase f in fields)
2178 if (delegates != null) {
2179 foreach (Delegate d in Delegates) {
2184 if (pending != null)
2185 pending.VerifyPendingMethods ();
2187 if (Report.Errors > 0)
2190 if (compiler_generated != null) {
2191 for (int i = 0; i < compiler_generated.Count; ++i)
2192 ((CompilerGeneratedClass) compiler_generated [i]).EmitType ();
2196 public override void CloseType ()
2198 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2202 caching_flags |= Flags.CloseTypeCreated;
2203 TypeBuilder.CreateType ();
2204 } catch (TypeLoadException){
2206 // This is fine, the code still created the type
2208 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2209 // Console.WriteLine (e.Message);
2210 } catch (Exception e) {
2211 throw new InternalErrorException (this, e);
2215 foreach (TypeContainer tc in Types)
2216 if (tc.Kind == Kind.Struct)
2219 foreach (TypeContainer tc in Types)
2220 if (tc.Kind != Kind.Struct)
2224 if (Delegates != null)
2225 foreach (Delegate d in Delegates)
2228 if (compiler_generated != null)
2229 foreach (CompilerGeneratedClass c in compiler_generated)
2232 PartialContainer = null;
2237 initialized_fields = null;
2238 initialized_static_fields = null;
2240 ordered_explicit_member_list = null;
2241 ordered_member_list = null;
2246 compiler_generated = null;
2247 default_constructor = null;
2248 default_static_constructor = null;
2250 OptAttributes = null;
2253 member_cache = null;
2257 // Performs the validation on a Method's modifiers (properties have
2258 // the same properties).
2260 public bool MethodModifiersValid (MemberCore mc)
2262 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2263 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2264 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2266 int flags = mc.ModFlags;
2269 // At most one of static, virtual or override
2271 if ((flags & Modifiers.STATIC) != 0){
2272 if ((flags & vao) != 0){
2273 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2274 mc.GetSignatureForError ());
2279 if (Kind == Kind.Struct){
2280 if ((flags & va) != 0){
2281 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2286 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2287 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2288 mc.GetSignatureForError ());
2293 // If the declaration includes the abstract modifier, then the
2294 // declaration does not include static, virtual or extern
2296 if ((flags & Modifiers.ABSTRACT) != 0){
2297 if ((flags & Modifiers.EXTERN) != 0){
2299 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2303 if ((flags & Modifiers.SEALED) != 0) {
2304 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2308 if ((flags & Modifiers.VIRTUAL) != 0){
2309 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2313 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2314 Report.SymbolRelatedToPreviousError (this);
2315 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2316 mc.GetSignatureForError (), GetSignatureForError ());
2321 if ((flags & Modifiers.PRIVATE) != 0){
2322 if ((flags & vao) != 0){
2323 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2328 if ((flags & Modifiers.SEALED) != 0){
2329 if ((flags & Modifiers.OVERRIDE) == 0){
2330 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2338 public Constructor DefaultStaticConstructor {
2339 get { return default_static_constructor; }
2342 protected override bool VerifyClsCompliance ()
2344 if (!base.VerifyClsCompliance ())
2349 Type base_type = TypeBuilder.BaseType;
2350 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2351 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2358 /// Checks whether container name is CLS Compliant
2360 void VerifyClsName ()
2362 Hashtable base_members = base_cache == null ?
2364 base_cache.GetPublicMembers ();
2365 Hashtable this_members = new Hashtable ();
2367 foreach (DictionaryEntry entry in defined_names) {
2368 MemberCore mc = (MemberCore)entry.Value;
2369 if (!mc.IsClsComplianceRequired ())
2372 string name = (string) entry.Key;
2373 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2375 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2376 object found = base_members [lcase];
2377 if (found == null) {
2378 found = this_members [lcase];
2379 if (found == null) {
2380 this_members.Add (lcase, mc);
2385 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2388 if (found is MemberInfo) {
2389 if (basename == ((MemberInfo) found).Name)
2391 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2393 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2396 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2402 /// Performs checks for an explicit interface implementation. First it
2403 /// checks whether the `interface_type' is a base inteface implementation.
2404 /// Then it checks whether `name' exists in the interface type.
2406 public bool VerifyImplements (InterfaceMemberBase mb)
2408 if (ifaces != null) {
2409 foreach (Type t in ifaces){
2410 if (TypeManager.IsEqual (t, mb.InterfaceType))
2415 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2416 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2417 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2421 public override Type LookupAnyGeneric (string typeName)
2423 if (types != null) {
2424 foreach (TypeContainer tc in types) {
2428 int pos = tc.Basename.LastIndexOf ('`');
2429 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2430 return tc.TypeBuilder;
2434 return base.LookupAnyGeneric (typeName);
2437 public void Mark_HasEquals ()
2439 cached_method |= CachedMethods.Equals;
2442 public void Mark_HasGetHashCode ()
2444 cached_method |= CachedMethods.GetHashCode;
2448 /// Method container contains Equals method
2450 public bool HasEquals {
2452 return (cached_method & CachedMethods.Equals) != 0;
2457 /// Method container contains GetHashCode method
2459 public bool HasGetHashCode {
2461 return (cached_method & CachedMethods.GetHashCode) != 0;
2465 public bool HasStaticFieldInitializer {
2467 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2471 cached_method |= CachedMethods.HasStaticFieldInitializer;
2473 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2481 string IMemberContainer.Name {
2487 Type IMemberContainer.Type {
2493 bool IMemberContainer.IsInterface {
2495 return Kind == Kind.Interface;
2499 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2501 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2503 if (GenericType != null)
2504 return TypeManager.FindMembers (GenericType, mt, new_bf,
2507 return FindMembers (mt, new_bf, null, null);
2511 // Generates xml doc comments (if any), and if required,
2512 // handle warning report.
2514 internal override void GenerateDocComment (DeclSpace ds)
2516 DocUtil.GenerateTypeDocComment (this, ds);
2519 public override string DocCommentHeader {
2520 get { return "T:"; }
2523 public MemberCache BaseCache {
2525 if (base_cache != null)
2527 if (TypeBuilder.BaseType != null)
2528 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2529 if (TypeBuilder.IsInterface)
2530 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2536 public abstract class ClassOrStruct : TypeContainer {
2537 ListDictionary declarative_security;
2539 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2540 MemberName name, Attributes attrs, Kind kind)
2541 : base (ns, parent, name, attrs, kind)
2545 protected override bool AddToContainer (MemberCore symbol, string name)
2547 if (name == MemberName.Name) {
2548 if (symbol is TypeParameter) {
2549 Report.Error (694, symbol.Location,
2550 "Type parameter `{0}' has same name as containing type, or method",
2551 symbol.GetSignatureForError ());
2555 InterfaceMemberBase imb = symbol as InterfaceMemberBase;
2556 if (imb == null || !imb.IsExplicitImpl) {
2557 Report.SymbolRelatedToPreviousError (this);
2558 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2559 symbol.GetSignatureForError ());
2564 return base.AddToContainer (symbol, name);
2567 public override void VerifyMembers ()
2569 base.VerifyMembers ();
2571 if ((events != null) && Report.WarningLevel >= 3) {
2572 foreach (Event e in events){
2573 // Note: The event can be assigned from same class only, so we can report
2574 // this warning for all accessibility modes
2575 if ((e.caching_flags & Flags.IsUsed) == 0)
2576 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2581 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2583 if (a.IsValidSecurityAttribute ()) {
2584 if (declarative_security == null)
2585 declarative_security = new ListDictionary ();
2587 a.ExtractSecurityPermissionSet (declarative_security);
2591 if (a.Type == pa.StructLayout) {
2592 PartialContainer.HasStructLayout = true;
2594 if (a.GetLayoutKindValue () == LayoutKind.Explicit)
2595 PartialContainer.HasExplicitLayout = true;
2598 base.ApplyAttributeBuilder (a, cb, pa);
2602 /// Defines the default constructors
2604 protected void DefineDefaultConstructor (bool is_static)
2606 // The default instance constructor is public
2607 // If the class is abstract, the default constructor is protected
2608 // The default static constructor is private
2612 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2614 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2617 Constructor c = new Constructor (this, MemberName.Name, mods,
2618 null, ParametersCompiled.EmptyReadOnlyParameters,
2619 new GeneratedBaseInitializer (Location),
2623 c.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
2626 public override bool Define ()
2628 CheckProtectedModifier ();
2632 if (default_static_constructor != null)
2633 default_static_constructor.Define ();
2638 public override void Emit ()
2640 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer) {
2641 DefineDefaultConstructor (true);
2642 default_static_constructor.Define ();
2647 if (declarative_security != null) {
2648 foreach (DictionaryEntry de in declarative_security) {
2649 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2654 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2656 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
2659 protected override TypeAttributes TypeAttr {
2661 if (default_static_constructor == null)
2662 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2664 return base.TypeAttr;
2670 // TODO: should be sealed
2671 public class Class : ClassOrStruct {
2672 const int AllowedModifiers =
2675 Modifiers.PROTECTED |
2676 Modifiers.INTERNAL |
2678 Modifiers.ABSTRACT |
2683 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2685 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2687 : base (ns, parent, name, attrs, Kind.Class)
2689 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2690 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2692 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2693 Report.FeatureIsNotAvailable (Location, "static classes");
2697 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2699 if (part.Name == "System.Object")
2700 Report.Error (537, part.Location,
2701 "The class System.Object cannot have a base class or implement an interface.");
2702 base.AddBasesForPart (part, bases);
2705 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2707 if (a.Type == pa.AttributeUsage) {
2708 if (!TypeManager.IsAttributeType (BaseType) &&
2709 TypeBuilder.FullName != "System.Attribute") {
2710 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2714 if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) {
2715 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2719 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2720 a.Error_MissingGuidAttribute ();
2724 if (a.Type == pa.Extension) {
2725 a.Error_MisusedExtensionAttribute ();
2729 if (AttributeTester.IsAttributeExcluded (a.Type, Location))
2732 base.ApplyAttributeBuilder (a, cb, pa);
2735 public override AttributeTargets AttributeTargets {
2737 return AttributeTargets.Class;
2741 protected override void DefineContainerMembers (MemberCoreArrayList list)
2747 base.DefineContainerMembers (list);
2751 foreach (MemberCore m in list) {
2752 if (m is Operator) {
2753 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2757 if (m is Destructor) {
2758 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2763 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2767 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2770 if (m is Constructor) {
2771 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2775 Method method = m as Method;
2776 if (method != null && method.Parameters.HasExtensionMethodType) {
2777 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2781 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2784 base.DefineContainerMembers (list);
2787 public override bool Define ()
2789 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2790 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2793 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2794 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2797 return base.Define ();
2800 protected override bool DoDefineMembers ()
2802 if (InstanceConstructors == null && !IsStatic)
2803 DefineDefaultConstructor (false);
2805 return base.DoDefineMembers ();
2808 public override void Emit ()
2812 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2813 PredefinedAttributes.Get.Extension.EmitAttribute (TypeBuilder);
2816 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2818 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2820 if (base_class == null) {
2821 if (RootContext.StdLib)
2822 base_class = TypeManager.system_object_expr;
2823 else if (Name != "System.Object")
2824 base_class = TypeManager.system_object_expr;
2826 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2828 689, base_class.Location,
2829 "Cannot derive from `{0}' because it is a type parameter",
2830 base_class.GetSignatureForError ());
2834 if (IsGeneric && TypeManager.IsAttributeType (base_class.Type)) {
2835 Report.Error (698, base_class.Location,
2836 "A generic type cannot derive from `{0}' because it is an attribute class",
2837 base_class.GetSignatureForError ());
2840 if (base_class.IsSealed){
2841 Report.SymbolRelatedToPreviousError (base_class.Type);
2842 if (base_class.Type.IsAbstract) {
2843 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2844 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2846 Report.Error (509, Location, "`{0}': cannot derive from sealed type `{1}'",
2847 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2852 if (!base_class.CanInheritFrom ()){
2853 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2854 GetSignatureForError (), base_class.GetSignatureForError ());
2858 if (!IsAccessibleAs (base_class.Type)) {
2859 Report.SymbolRelatedToPreviousError (base_class.Type);
2860 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2861 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2865 if (PartialContainer.IsStaticClass) {
2866 if (base_class.Type != TypeManager.object_type) {
2867 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2868 GetSignatureForError (), base_class.GetSignatureForError ());
2872 if (ifaces != null) {
2873 foreach (TypeExpr t in ifaces)
2874 Report.SymbolRelatedToPreviousError (t.Type);
2875 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2882 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2883 /// Valid only for attribute classes.
2884 public bool IsExcluded ()
2886 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2887 return (caching_flags & Flags.Excluded) != 0;
2889 caching_flags &= ~Flags.Excluded_Undetected;
2891 if (OptAttributes == null)
2894 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
2898 foreach (Attribute a in attrs) {
2899 string condition = a.GetConditionalAttributeValue ();
2900 if (Location.CompilationUnit.IsConditionalDefined (condition))
2904 caching_flags |= Flags.Excluded;
2910 return (ModFlags & Modifiers.STATIC) != 0;
2915 // FIXME: How do we deal with the user specifying a different
2918 protected override TypeAttributes TypeAttr {
2920 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2922 ta |= StaticClassAttribute;
2928 public sealed class Struct : ClassOrStruct {
2930 bool is_unmanaged, has_unmanaged_check_done;
2933 // Modifiers allowed in a struct declaration
2935 const int AllowedModifiers =
2938 Modifiers.PROTECTED |
2939 Modifiers.INTERNAL |
2943 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2944 int mod, Attributes attrs)
2945 : base (ns, parent, name, attrs, Kind.Struct)
2949 if (parent.Parent == null)
2950 accmods = Modifiers.INTERNAL;
2952 accmods = Modifiers.PRIVATE;
2954 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2956 this.ModFlags |= Modifiers.SEALED;
2959 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2961 base.ApplyAttributeBuilder (a, cb, pa);
2964 // When struct constains fixed fixed and struct layout has explicitly
2965 // set CharSet, its value has to be propagated to compiler generated
2966 // fixed field types
2968 if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
2969 for (int i = 0; i < Fields.Count; ++i) {
2970 FixedField ff = Fields [i] as FixedField;
2972 ff.SetCharSet (TypeBuilder.Attributes);
2977 public override AttributeTargets AttributeTargets {
2979 return AttributeTargets.Struct;
2983 public override bool IsUnmanagedType ()
2988 if (requires_delayed_unmanagedtype_check)
2991 if (has_unmanaged_check_done)
2992 return is_unmanaged;
2994 has_unmanaged_check_done = true;
2996 foreach (FieldBase f in fields) {
2997 if ((f.ModFlags & Modifiers.STATIC) != 0)
3000 // It can happen when recursive unmanaged types are defined
3001 // struct S { S* s; }
3002 Type mt = f.MemberType;
3004 has_unmanaged_check_done = false;
3005 requires_delayed_unmanagedtype_check = true;
3009 // TODO: Remove when pointer types are under mcs control
3010 while (mt.IsPointer)
3011 mt = TypeManager.GetElementType (mt);
3012 if (TypeManager.IsEqual (mt, TypeBuilder))
3015 if (TypeManager.IsUnmanagedType (mt))
3021 is_unmanaged = true;
3025 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
3027 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
3029 // If we are compiling our runtime,
3030 // and we are defining ValueType, then our
3031 // base is `System.Object'.
3033 if (base_class == null) {
3034 if (!RootContext.StdLib && Name == "System.ValueType")
3035 base_class = TypeManager.system_object_expr;
3037 base_class = TypeManager.system_valuetype_expr;
3044 // FIXME: Allow the user to specify a different set of attributes
3045 // in some cases (Sealed for example is mandatory for a class,
3046 // but what SequentialLayout can be changed
3048 protected override TypeAttributes TypeAttr {
3050 const TypeAttributes DefaultTypeAttributes =
3051 TypeAttributes.SequentialLayout |
3052 TypeAttributes.Sealed;
3054 return base.TypeAttr | DefaultTypeAttributes;
3058 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3060 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3061 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3062 field.GetSignatureForError ());
3065 base.RegisterFieldForInitialization (field, expression);
3073 public sealed class Interface : TypeContainer, IMemberContainer {
3076 /// Modifiers allowed in a class declaration
3078 public const int AllowedModifiers =
3081 Modifiers.PROTECTED |
3082 Modifiers.INTERNAL |
3086 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3088 : base (ns, parent, name, attrs, Kind.Interface)
3092 if (parent.Parent == null)
3093 accmods = Modifiers.INTERNAL;
3095 accmods = Modifiers.PRIVATE;
3097 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3100 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3102 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
3103 a.Error_MissingGuidAttribute ();
3107 base.ApplyAttributeBuilder (a, cb, pa);
3111 public override AttributeTargets AttributeTargets {
3113 return AttributeTargets.Interface;
3117 protected override TypeAttributes TypeAttr {
3119 const TypeAttributes DefaultTypeAttributes =
3120 TypeAttributes.AutoLayout |
3121 TypeAttributes.Abstract |
3122 TypeAttributes.Interface;
3124 return base.TypeAttr | DefaultTypeAttributes;
3128 protected override bool VerifyClsCompliance ()
3130 if (!base.VerifyClsCompliance ())
3133 if (ifaces != null) {
3134 foreach (Type t in ifaces) {
3135 if (AttributeTester.IsClsCompliant (t))
3138 Report.SymbolRelatedToPreviousError (t);
3139 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3140 GetSignatureForError (), TypeManager.CSharpName (t));
3148 // It is used as a base class for all property based members
3149 // This includes properties, indexers, and events
3150 public abstract class PropertyBasedMember : InterfaceMemberBase
3152 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3153 FullNamedExpression type, int mod, int allowed_mod,
3154 MemberName name, Attributes attrs)
3155 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3159 protected override bool VerifyClsCompliance ()
3161 if (!base.VerifyClsCompliance ())
3164 if (!AttributeTester.IsClsCompliant (MemberType)) {
3165 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
3166 GetSignatureForError ());
3174 public abstract class MethodCore : InterfaceMemberBase
3176 public readonly ParametersCompiled Parameters;
3177 protected ToplevelBlock block;
3179 public MethodCore (DeclSpace parent, GenericMethod generic,
3180 FullNamedExpression type, int mod, int allowed_mod,
3181 MemberName name, Attributes attrs, ParametersCompiled parameters)
3182 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3184 Parameters = parameters;
3188 // Returns the System.Type array for the parameters of this method
3190 public Type [] ParameterTypes {
3192 return Parameters.Types;
3196 public ParametersCompiled ParameterInfo {
3202 public ToplevelBlock Block {
3212 public CallingConventions CallingConventions {
3214 CallingConventions cc = Parameters.CallingConvention;
3216 if ((ModFlags & Modifiers.STATIC) == 0)
3217 cc |= CallingConventions.HasThis;
3219 // FIXME: How is `ExplicitThis' used in C#?
3225 protected override bool CheckBase ()
3227 // Check whether arguments were correct.
3228 if (!DefineParameters (Parameters))
3231 return base.CheckBase ();
3235 // Returns a string that represents the signature for this
3236 // member which should be used in XML documentation.
3238 public override string GetDocCommentName (DeclSpace ds)
3240 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3244 // Raised (and passed an XmlElement that contains the comment)
3245 // when GenerateDocComment is writing documentation expectedly.
3247 // FIXME: with a few effort, it could be done with XmlReader,
3248 // that means removal of DOM use.
3250 internal override void OnGenerateDocComment (XmlElement el)
3252 DocUtil.OnMethodGenerateDocComment (this, el);
3256 // Represents header string for documentation comment.
3258 public override string DocCommentHeader
3260 get { return "M:"; }
3263 public override bool EnableOverloadChecks (MemberCore overload)
3265 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3266 caching_flags |= Flags.MethodOverloadsExist;
3270 return base.EnableOverloadChecks (overload);
3273 protected override bool VerifyClsCompliance ()
3275 if (!base.VerifyClsCompliance ())
3278 if (Parameters.HasArglist) {
3279 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
3282 if (!AttributeTester.IsClsCompliant (MemberType)) {
3283 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
3284 GetSignatureForError ());
3287 Parameters.VerifyClsCompliance ();
3293 public abstract class InterfaceMemberBase : MemberBase {
3295 // Whether this is an interface member.
3297 public bool IsInterface;
3300 // If true, this is an explicit interface implementation
3302 public bool IsExplicitImpl;
3304 protected bool is_external_implementation;
3307 // The interface type we are explicitly implementing
3309 public Type InterfaceType;
3312 // The method we're overriding if this is an override method.
3314 protected MethodInfo base_method;
3316 readonly int explicit_mod_flags;
3317 public MethodAttributes flags;
3319 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3320 FullNamedExpression type, int mod, int allowed_mod,
3321 MemberName name, Attributes attrs)
3322 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3325 IsInterface = parent.PartialContainer.Kind == Kind.Interface;
3326 IsExplicitImpl = (MemberName.Left != null);
3327 explicit_mod_flags = mod;
3330 protected override bool CheckBase ()
3332 if (!base.CheckBase ())
3335 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
3336 CheckForDuplications ();
3341 // Is null for System.Object while compiling corlib and base interfaces
3342 if (Parent.PartialContainer.BaseCache == null) {
3343 if ((ModFlags & Modifiers.NEW) != 0) {
3344 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3349 Type base_ret_type = null;
3350 base_method = FindOutBaseMethod (ref base_ret_type);
3352 // method is override
3353 if (base_method != null) {
3354 if (!CheckMethodAgainstBase (base_ret_type))
3357 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3358 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3360 if (OptAttributes == null || !OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3361 Report.SymbolRelatedToPreviousError (base_method);
3362 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3363 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3366 if (OptAttributes != null && OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3367 Report.SymbolRelatedToPreviousError (base_method);
3368 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3369 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3376 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3377 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3378 if (conflict_symbol != null) {
3379 Report.SymbolRelatedToPreviousError (conflict_symbol);
3381 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3382 else if (this is PropertyBase)
3383 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3385 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3387 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3388 GetSignatureForError (), SimpleName.GetMemberType (this));
3393 if (conflict_symbol == null) {
3394 if ((ModFlags & Modifiers.NEW) != 0) {
3395 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3400 if ((ModFlags & Modifiers.NEW) == 0) {
3401 if (this is MethodOrOperator && conflict_symbol.MemberType == MemberTypes.Method)
3404 Report.SymbolRelatedToPreviousError (conflict_symbol);
3405 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3406 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3412 protected virtual bool CheckForDuplications ()
3414 return Parent.MemberCache.CheckExistingMembersOverloads (
3415 this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters);
3419 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3420 // that have been defined.
3422 // `name' is the user visible name for reporting errors (this is used to
3423 // provide the right name regarding method names and properties)
3425 bool CheckMethodAgainstBase (Type base_method_type)
3429 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3430 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3431 Report.SymbolRelatedToPreviousError (base_method);
3432 Report.Error (506, Location,
3433 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3434 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3438 // Now we check that the overriden method is not final
3440 if (base_method.IsFinal) {
3441 Report.SymbolRelatedToPreviousError (base_method);
3442 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3443 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3447 // Check that the permissions are not being changed
3449 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3450 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3452 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3453 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3457 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3458 Report.SymbolRelatedToPreviousError (base_method);
3459 if (this is PropertyBasedMember) {
3460 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3461 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3464 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3465 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3471 if ((ModFlags & Modifiers.NEW) == 0) {
3472 if ((ModFlags & Modifiers.OVERRIDE) == 0) {
3473 ModFlags |= Modifiers.NEW;
3474 Report.SymbolRelatedToPreviousError (base_method);
3475 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3476 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",
3477 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3478 if (base_method.IsAbstract){
3479 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3480 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3484 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3485 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3489 if (base_method.IsAbstract && !IsInterface) {
3490 Report.SymbolRelatedToPreviousError (base_method);
3491 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3492 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3500 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3502 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3504 // when overriding protected internal, the method can be declared
3505 // protected internal only within the same assembly or assembly
3506 // which has InternalsVisibleTo
3508 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3509 return TypeManager.IsThisOrFriendAssembly (base_method.DeclaringType.Assembly);
3510 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3512 // if it's not "protected internal", it must be "protected"
3516 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3518 // protected within the same assembly - an error
3521 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3522 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3524 // protected ok, but other attributes differ - report an error
3530 return (thisp == base_classp);
3534 public override bool Define ()
3537 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3538 Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3540 flags = MethodAttributes.Public |
3541 MethodAttributes.Abstract |
3542 MethodAttributes.HideBySig |
3543 MethodAttributes.NewSlot |
3544 MethodAttributes.Virtual;
3546 Parent.PartialContainer.MethodModifiersValid (this);
3548 flags = Modifiers.MethodAttr (ModFlags);
3551 if (IsExplicitImpl) {
3552 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3553 if (iface_texpr == null)
3556 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3557 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3558 GetSignatureForError ());
3561 InterfaceType = iface_texpr.Type;
3563 if (!InterfaceType.IsInterface) {
3564 Report.SymbolRelatedToPreviousError (InterfaceType);
3565 Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3566 TypeManager.CSharpName (InterfaceType));
3568 Parent.PartialContainer.VerifyImplements (this);
3571 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3574 return base.Define ();
3577 protected bool DefineParameters (ParametersCompiled parameters)
3579 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3581 if (!parameters.Resolve (rc))
3585 for (int i = 0; i < parameters.Count; ++i) {
3586 Parameter p = parameters [i];
3588 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3589 p.Warning_UselessOptionalParameter ();
3591 if (p.CheckAccessibility (this))
3594 Type t = parameters.Types [i];
3595 Report.SymbolRelatedToPreviousError (t);
3596 if (this is Indexer)
3597 Report.Error (55, Location,
3598 "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3599 TypeManager.CSharpName (t), GetSignatureForError ());
3600 else if (this is Operator)
3601 Report.Error (57, Location,
3602 "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3603 TypeManager.CSharpName (t), GetSignatureForError ());
3605 Report.Error (51, Location,
3606 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3607 TypeManager.CSharpName (t), GetSignatureForError ());
3613 public override void Emit()
3615 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3616 // We are more strict than csc and report this as an error because SRE does not allow emit that
3617 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3618 if (this is Constructor) {
3619 Report.Error (824, Location,
3620 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3622 Report.Error (626, Location,
3623 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3624 GetSignatureForError ());
3631 public override bool EnableOverloadChecks (MemberCore overload)
3634 // Two members can differ in their explicit interface
3635 // type parameter only
3637 InterfaceMemberBase imb = overload as InterfaceMemberBase;
3638 if (imb != null && imb.IsExplicitImpl) {
3639 if (IsExplicitImpl) {
3640 caching_flags |= Flags.MethodOverloadsExist;
3645 return IsExplicitImpl;
3648 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3650 Report.SymbolRelatedToPreviousError (base_method);
3651 string base_name = TypeManager.GetFullNameSignature (base_method);
3652 string this_name = GetSignatureForError ();
3653 if (suffix != null) {
3654 base_name += suffix;
3655 this_name += suffix;
3658 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3659 this_name, Modifiers.GetDescription (ma), base_name);
3662 protected static string Error722 {
3664 return "`{0}': static types cannot be used as return types";
3669 /// Gets base method and its return type
3671 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3674 // The "short" name of this property / indexer / event. This is the
3675 // name without the explicit interface.
3677 public string ShortName
3679 get { return MemberName.Name; }
3680 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3684 // Returns full metadata method name
3686 public string GetFullName (MemberName name)
3688 if (!IsExplicitImpl)
3692 // When dealing with explicit members a full interface type
3693 // name is added to member name to avoid possible name conflicts
3695 // We use CSharpName which gets us full name with benefit of
3696 // replacing predefined names which saves some space and name
3699 return TypeManager.CSharpName (InterfaceType) + "." + name.Name;
3702 protected override bool VerifyClsCompliance ()
3704 if (!base.VerifyClsCompliance ()) {
3705 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3706 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3709 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3710 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3715 if (GenericMethod != null)
3716 GenericMethod.VerifyClsCompliance ();
3721 public override bool IsUsed
3723 get { return IsExplicitImpl || base.IsUsed; }
3728 public abstract class MethodOrOperator : MethodCore, IMethodData
3730 public MethodBuilder MethodBuilder;
3731 ReturnParameter return_attributes;
3732 ListDictionary declarative_security;
3733 protected MethodData MethodData;
3735 static string[] attribute_targets = new string [] { "method", "return" };
3737 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3738 int allowed_mod, MemberName name,
3739 Attributes attrs, ParametersCompiled parameters)
3740 : base (parent, generic, type, mod, allowed_mod, name,
3745 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3747 if (a.Target == AttributeTargets.ReturnValue) {
3748 if (return_attributes == null)
3749 return_attributes = new ReturnParameter (MethodBuilder, Location);
3751 return_attributes.ApplyAttributeBuilder (a, cb, pa);
3755 if (a.IsInternalMethodImplAttribute) {
3756 is_external_implementation = true;
3759 if (a.Type == pa.DllImport) {
3760 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3761 if ((ModFlags & extern_static) != extern_static) {
3762 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3764 is_external_implementation = true;
3767 if (a.IsValidSecurityAttribute ()) {
3768 if (declarative_security == null)
3769 declarative_security = new ListDictionary ();
3770 a.ExtractSecurityPermissionSet (declarative_security);
3774 if (MethodBuilder != null)
3775 MethodBuilder.SetCustomAttribute (cb);
3778 public override AttributeTargets AttributeTargets {
3780 return AttributeTargets.Method;
3784 protected override bool CheckForDuplications ()
3786 string name = GetFullName (MemberName);
3787 if (MemberName.IsGeneric)
3788 name = MemberName.MakeName (name, MemberName.TypeArguments);
3790 return Parent.MemberCache.CheckExistingMembersOverloads (this, name, Parameters);
3793 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3795 return new EmitContext (
3796 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3799 protected override bool ResolveMemberType ()
3802 if (GenericMethod != null) {
3803 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3804 if (!GenericMethod.Define (this))
3809 return base.ResolveMemberType ();
3812 public override bool Define ()
3814 if (!base.Define ())
3820 if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
3822 // Current method is turned into automatically generated
3823 // wrapper which creates an instance of iterator
3825 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
3826 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
3829 if (IsPartialDefinition) {
3830 caching_flags &= ~Flags.Excluded_Undetected;
3831 caching_flags |= Flags.Excluded;
3832 // Add to member cache only when a partial method implementation is not there
3833 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3834 MethodBase mb = new PartialMethodDefinitionInfo (this);
3835 Parent.MemberCache.AddMember (mb, this);
3836 TypeManager.AddMethod (mb, this);
3842 MethodData = new MethodData (
3843 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3845 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
3848 MethodBuilder = MethodData.MethodBuilder;
3850 if (TypeManager.IsGenericMethod (MethodBuilder))
3851 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3853 Parent.MemberCache.AddMember (MethodBuilder, this);
3858 protected override void DoMemberTypeIndependentChecks ()
3860 base.DoMemberTypeIndependentChecks ();
3862 CheckAbstractAndExtern (block != null);
3864 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3865 for (int i = 0; i < Parameters.Count; ++i) {
3866 IParameterData p = Parameters.FixedParameters [i];
3867 if (p.ModFlags == Parameter.Modifier.OUT) {
3868 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3869 GetSignatureForError ());
3872 if (p.HasDefaultValue && IsPartialImplementation)
3873 ((Parameter) p).Warning_UselessOptionalParameter ();
3878 protected override void DoMemberTypeDependentChecks ()
3880 base.DoMemberTypeDependentChecks ();
3882 if (!TypeManager.IsGenericParameter (MemberType)) {
3883 if (MemberType.IsAbstract && MemberType.IsSealed) {
3884 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3889 public override void Emit ()
3891 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
3892 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
3893 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3894 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
3896 if (TypeManager.IsDynamicType (ReturnType)) {
3897 return_attributes = new ReturnParameter (MethodBuilder, Location);
3898 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
3901 if (OptAttributes != null)
3902 OptAttributes.Emit ();
3904 if (declarative_security != null) {
3905 foreach (DictionaryEntry de in declarative_security) {
3906 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3910 if (MethodData != null)
3911 MethodData.Emit (Parent);
3919 protected void Error_ConditionalAttributeIsNotValid ()
3921 Report.Error (577, Location,
3922 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3923 GetSignatureForError ());
3926 public bool IsPartialDefinition {
3928 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3932 public bool IsPartialImplementation {
3934 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3938 public override string[] ValidAttributeTargets {
3940 return attribute_targets;
3944 #region IMethodData Members
3946 public Type ReturnType {
3952 public MemberName MethodName {
3959 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3961 public bool IsExcluded () {
3962 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3963 return (caching_flags & Flags.Excluded) != 0;
3965 caching_flags &= ~Flags.Excluded_Undetected;
3967 if (base_method == null) {
3968 if (OptAttributes == null)
3971 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
3976 foreach (Attribute a in attrs) {
3977 string condition = a.GetConditionalAttributeValue ();
3978 if (condition == null)
3981 if (Location.CompilationUnit.IsConditionalDefined (condition))
3985 caching_flags |= Flags.Excluded;
3989 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
3991 if (AttributeTester.IsConditionalMethodExcluded (base_method, Location)) {
3992 caching_flags |= Flags.Excluded;
3998 if (md.IsExcluded ()) {
3999 caching_flags |= Flags.Excluded;
4005 GenericMethod IMethodData.GenericMethod {
4007 return GenericMethod;
4011 public virtual void EmitExtraSymbolInfo (SourceMethod source)
4018 public class SourceMethod : IMethodDef
4021 SourceMethodBuilder builder;
4023 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
4025 this.method = method;
4027 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
4030 public string Name {
4031 get { return method.Name; }
4036 if (method is MethodBuilder)
4037 return ((MethodBuilder) method).GetToken ().Token;
4038 else if (method is ConstructorBuilder)
4039 return ((ConstructorBuilder) method).GetToken ().Token;
4041 throw new NotSupportedException ();
4045 public void CloseMethod ()
4047 SymbolWriter.CloseMethod ();
4050 public void SetRealMethodName (string name)
4052 if (builder != null)
4053 builder.SetRealMethodName (name);
4056 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4058 if (!SymbolWriter.HasSymbolWriter)
4063 Location start_loc = block.StartLocation;
4064 if (start_loc.IsNull)
4067 ICompileUnit compile_unit = start_loc.CompilationUnit;
4068 if (compile_unit == null)
4071 return new SourceMethod (parent, method, compile_unit);
4075 public class Method : MethodOrOperator {
4078 /// Modifiers allowed in a class declaration
4080 const int AllowedModifiers =
4083 Modifiers.PROTECTED |
4084 Modifiers.INTERNAL |
4089 Modifiers.OVERRIDE |
4090 Modifiers.ABSTRACT |
4094 const int AllowedInterfaceModifiers =
4095 Modifiers.NEW | Modifiers.UNSAFE;
4097 Method partialMethodImplementation;
4099 public Method (DeclSpace parent, GenericMethod generic,
4100 FullNamedExpression return_type, int mod,
4101 MemberName name, ParametersCompiled parameters, Attributes attrs)
4102 : base (parent, generic, return_type, mod,
4103 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
4104 name, attrs, parameters)
4108 protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
4109 MemberName name, ParametersCompiled parameters, Attributes attrs)
4110 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
4114 public override string GetSignatureForError()
4116 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4119 static void Error_DuplicateEntryPoint (Method b)
4121 Report.Error (17, b.Location,
4122 "Program `{0}' has more than one entry point defined: `{1}'",
4123 CodeGen.FileName, b.GetSignatureForError ());
4126 bool IsEntryPoint ()
4128 if (ReturnType != TypeManager.void_type &&
4129 ReturnType != TypeManager.int32_type)
4132 if (Parameters.Count == 0)
4135 if (Parameters.Count > 1)
4138 Type t = Parameters.Types [0];
4139 return t.IsArray && t.GetArrayRank () == 1 &&
4140 TypeManager.GetElementType (t) == TypeManager.string_type &&
4141 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4144 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4146 if (a.Type == pa.Conditional) {
4147 if (IsExplicitImpl) {
4148 Error_ConditionalAttributeIsNotValid ();
4152 if (ReturnType != TypeManager.void_type) {
4153 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4157 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4158 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4163 Report.Error (582, Location, "Conditional not valid on interface members");
4167 if (MethodData.implementing != null) {
4168 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4169 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4170 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4174 for (int i = 0; i < Parameters.Count; ++i) {
4175 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
4176 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4182 if (a.Type == pa.Extension) {
4183 a.Error_MisusedExtensionAttribute ();
4187 base.ApplyAttributeBuilder (a, cb, pa);
4190 protected override bool CheckForDuplications ()
4192 if (!base.CheckForDuplications ())
4195 ArrayList ar = Parent.PartialContainer.Properties;
4197 for (int i = 0; i < ar.Count; ++i) {
4198 PropertyBase pb = (PropertyBase) ar [i];
4199 if (pb.AreAccessorsDuplicateImplementation (this))
4204 ar = Parent.PartialContainer.Indexers;
4206 for (int i = 0; i < ar.Count; ++i) {
4207 PropertyBase pb = (PropertyBase) ar [i];
4208 if (pb.AreAccessorsDuplicateImplementation (this))
4216 protected override bool CheckBase ()
4218 if (!base.CheckBase ())
4221 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == Destructor.MetadataName) {
4222 Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
4223 TypeManager.CSharpSignature (base_method));
4232 public override bool Define ()
4234 if (type_name == TypeManager.system_void_expr && Parameters.IsEmpty && Name == Destructor.MetadataName) {
4235 Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4238 if (!base.Define ())
4241 if (partialMethodImplementation != null && IsPartialDefinition)
4242 MethodBuilder = partialMethodImplementation.MethodBuilder;
4244 if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
4245 Error1599 (Location, ReturnType);
4249 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4250 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4251 Parent.PartialContainer.Mark_HasEquals ();
4252 else if (Parameters.IsEmpty && Name == "GetHashCode")
4253 Parent.PartialContainer.Mark_HasGetHashCode ();
4256 if ((ModFlags & Modifiers.STATIC) == 0)
4259 if (Parameters.HasExtensionMethodType) {
4260 if (Parent.PartialContainer.IsStaticClass && !Parent.IsGeneric) {
4261 if (!Parent.IsTopLevel)
4262 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4263 GetSignatureForError ());
4265 PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
4266 if (!pa.IsDefined) {
4267 Report.Error (1110, Location,
4268 "`{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",
4269 GetSignatureForError ());
4272 ModFlags |= Modifiers.METHOD_EXTENSION;
4273 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
4274 CodeGen.Assembly.HasExtensionMethods = true;
4276 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4277 GetSignatureForError ());
4282 // This is used to track the Entry Point,
4284 if (RootContext.NeedsEntryPoint &&
4286 (RootContext.MainClass == null ||
4287 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4288 if (IsEntryPoint ()) {
4290 if (RootContext.EntryPoint == null) {
4291 if (Parent.IsGeneric || MemberName.IsGeneric) {
4292 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4293 GetSignatureForError ());
4296 RootContext.EntryPoint = this;
4299 Error_DuplicateEntryPoint (RootContext.EntryPoint);
4300 Error_DuplicateEntryPoint (this);
4303 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4304 GetSignatureForError ());
4314 public override void Emit ()
4317 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4318 if (IsPartialDefinition) {
4320 // Use partial method implementation builder for partial method declaration attributes
4322 if (partialMethodImplementation != null) {
4323 MethodBuilder = partialMethodImplementation.MethodBuilder;
4326 } else if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
4327 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4328 GetSignatureForError ());
4333 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4334 PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
4336 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4337 Location, MethodBuilder);
4342 public override bool EnableOverloadChecks (MemberCore overload)
4344 // TODO: It can be deleted when members will be defined in correct order
4345 if (overload is Operator)
4346 return overload.EnableOverloadChecks (this);
4348 if (overload is Indexer)
4351 return base.EnableOverloadChecks (overload);
4354 public static void Error1599 (Location loc, Type t)
4356 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4359 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4361 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4362 Parent.TypeBuilder, Name, Parameters, GenericMethod, false);
4367 if (mi.IsSpecialName)
4370 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4374 public void SetPartialDefinition (Method methodDefinition)
4376 caching_flags |= Flags.PartialDefinitionExists;
4377 methodDefinition.partialMethodImplementation = this;
4379 for (int i = 0; i < methodDefinition.Parameters.Count; ++i ) {
4380 Parameters [i].DefaultValue = methodDefinition.Parameters [i].DefaultValue;
4383 if (methodDefinition.attributes == null)
4386 if (attributes == null) {
4387 attributes = methodDefinition.attributes;
4389 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4393 protected override bool VerifyClsCompliance ()
4395 if (!base.VerifyClsCompliance ())
4398 if (!Parameters.IsEmpty) {
4399 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4401 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4408 public abstract class ConstructorInitializer : ExpressionStatement
4410 Arguments argument_list;
4411 MethodGroupExpr base_constructor_group;
4413 public ConstructorInitializer (Arguments argument_list, Location loc)
4415 this.argument_list = argument_list;
4419 public Arguments Arguments {
4421 return argument_list;
4425 public override Expression CreateExpressionTree (EmitContext ec)
4427 throw new NotSupportedException ("ET");
4430 public ExpressionStatement Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4432 if (argument_list != null) {
4434 argument_list.Resolve (ec, out dynamic);
4436 SimpleName ctor = new SimpleName (ConstructorBuilder.ConstructorName, loc);
4437 return new DynamicInvocation (ctor, argument_list, loc).Resolve (ec) as ExpressionStatement;
4441 if (this is ConstructorBaseInitializer) {
4442 if (ec.ContainerType.BaseType == null)
4445 type = ec.ContainerType.BaseType;
4446 if (TypeManager.IsStruct (ec.ContainerType)) {
4447 Report.Error (522, loc,
4448 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4453 // It is legal to have "this" initializers that take no arguments
4454 // in structs, they are just no-ops.
4456 // struct D { public D (int a) : this () {}
4458 if (TypeManager.IsStruct (ec.ContainerType) && argument_list == null)
4461 type = ec.ContainerType;
4464 base_constructor_group = MemberLookupFinal (
4465 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4466 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4467 loc) as MethodGroupExpr;
4469 if (base_constructor_group == null)
4472 base_constructor_group = base_constructor_group.OverloadResolve (
4473 ec, ref argument_list, false, loc);
4475 if (base_constructor_group == null)
4478 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4480 if (base_ctor == caller_builder){
4481 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4487 public override Expression DoResolve (EmitContext ec)
4489 throw new NotSupportedException ();
4492 public override void Emit (EmitContext ec)
4494 // It can be null for static initializers
4495 if (base_constructor_group == null)
4500 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4502 base_constructor_group.EmitCall (ec, argument_list);
4505 public override void EmitStatement (EmitContext ec)
4511 public class ConstructorBaseInitializer : ConstructorInitializer {
4512 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
4513 base (argument_list, l)
4518 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4519 public GeneratedBaseInitializer (Location loc):
4525 public class ConstructorThisInitializer : ConstructorInitializer {
4526 public ConstructorThisInitializer (Arguments argument_list, Location l) :
4527 base (argument_list, l)
4532 public class Constructor : MethodCore, IMethodData {
4533 public ConstructorBuilder ConstructorBuilder;
4534 public ConstructorInitializer Initializer;
4535 ListDictionary declarative_security;
4536 bool has_compliant_args;
4539 // Modifiers allowed for a constructor.
4541 public const int AllowedModifiers =
4543 Modifiers.PROTECTED |
4544 Modifiers.INTERNAL |
4550 static readonly string[] attribute_targets = new string [] { "method" };
4553 // The spec claims that static is not permitted, but
4554 // my very own code has static constructors.
4556 public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, ParametersCompiled args,
4557 ConstructorInitializer init, Location loc)
4558 : base (parent, null, null, mod, AllowedModifiers,
4559 new MemberName (name, loc), attrs, args)
4564 public bool HasCompliantArgs {
4565 get { return has_compliant_args; }
4568 public override AttributeTargets AttributeTargets {
4569 get { return AttributeTargets.Constructor; }
4573 // Returns true if this is a default constructor
4575 public bool IsDefault ()
4577 if ((ModFlags & Modifiers.STATIC) != 0)
4578 return Parameters.IsEmpty;
4580 return Parameters.IsEmpty &&
4581 (Initializer is ConstructorBaseInitializer) &&
4582 (Initializer.Arguments == null);
4585 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4587 if (a.IsValidSecurityAttribute ()) {
4588 if (declarative_security == null) {
4589 declarative_security = new ListDictionary ();
4591 a.ExtractSecurityPermissionSet (declarative_security);
4595 if (a.IsInternalMethodImplAttribute) {
4596 is_external_implementation = true;
4599 ConstructorBuilder.SetCustomAttribute (cb);
4602 protected override bool CheckBase ()
4604 if ((ModFlags & Modifiers.STATIC) != 0) {
4605 if (!Parameters.IsEmpty) {
4606 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4607 GetSignatureForError ());
4611 // the rest can be ignored
4615 // Check whether arguments were correct.
4616 if (!DefineParameters (Parameters))
4619 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4620 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorInfo.ConstructorName,
4623 if (Parent.PartialContainer.Kind == Kind.Struct) {
4624 if (Parameters.Count == 0) {
4625 Report.Error (568, Location,
4626 "Structs cannot contain explicit parameterless constructors");
4631 CheckProtectedModifier ();
4637 // Creates the ConstructorBuilder
4639 public override bool Define ()
4641 if (ConstructorBuilder != null)
4644 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4645 MethodAttributes.SpecialName);
4647 if ((ModFlags & Modifiers.STATIC) != 0) {
4648 ca |= MethodAttributes.Static | MethodAttributes.Private;
4650 ca |= MethodAttributes.HideBySig;
4652 if ((ModFlags & Modifiers.PUBLIC) != 0)
4653 ca |= MethodAttributes.Public;
4654 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4655 if ((ModFlags & Modifiers.INTERNAL) != 0)
4656 ca |= MethodAttributes.FamORAssem;
4658 ca |= MethodAttributes.Family;
4659 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4660 ca |= MethodAttributes.Assembly;
4662 ca |= MethodAttributes.Private;
4665 if (!CheckAbstractAndExtern (block != null))
4668 // Check if arguments were correct.
4672 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4673 ca, CallingConventions,
4674 Parameters.GetEmitTypes ());
4676 if (Parent.PartialContainer.IsComImport) {
4677 if (!IsDefault ()) {
4678 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4679 Parent.GetSignatureForError ());
4681 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4684 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4685 TypeManager.AddMethod (ConstructorBuilder, this);
4687 // It's here only to report an error
4688 if (block != null && block.IsIterator) {
4689 member_type = TypeManager.void_type;
4690 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
4699 public override void Emit ()
4701 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4702 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
4704 if (OptAttributes != null)
4705 OptAttributes.Emit ();
4709 EmitContext ec = CreateEmitContext (null, null);
4712 // If we use a "this (...)" constructor initializer, then
4713 // do not emit field initializers, they are initialized in the other constructor
4715 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4716 !(Initializer is ConstructorThisInitializer);
4718 if (emit_field_initializers)
4719 Parent.PartialContainer.ResolveFieldInitializers (ec);
4721 if (block != null) {
4722 // If this is a non-static `struct' constructor and doesn't have any
4723 // initializer, it must initialize all of the struct's fields.
4724 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4725 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4726 block.AddThisVariable (Parent, Location);
4728 if (!block.ResolveMeta (ec, Parameters))
4731 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4732 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4733 Initializer = new GeneratedBaseInitializer (Location);
4736 // Spec mandates that Initializers will not have `this' access
4738 if (Initializer != null) {
4740 ExpressionStatement expr = Initializer.Resolve (ConstructorBuilder, ec);
4741 ec.IsStatic = false;
4742 block.AddScopeStatement (new StatementExpression (expr));
4747 Parameters.ApplyAttributes (ConstructorBuilder);
4749 SourceMethod source = null;
4751 ec.OmitDebuggingInfo = true;
4753 source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4755 bool unreachable = false;
4756 if (block != null) {
4757 if (!ec.ResolveTopBlock (null, block, Parameters, this, out unreachable))
4760 ec.EmitMeta (block);
4762 if (Report.Errors > 0)
4765 ec.EmitResolvedTopBlock (block, unreachable);
4769 source.CloseMethod ();
4771 if (declarative_security != null) {
4772 foreach (DictionaryEntry de in declarative_security) {
4773 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4780 // Is never override
4781 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4786 public override string GetSignatureForError()
4788 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4791 public override string[] ValidAttributeTargets {
4793 return attribute_targets;
4797 protected override bool VerifyClsCompliance ()
4799 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4803 if (!Parameters.IsEmpty) {
4804 ArrayList al = (ArrayList)Parent.MemberCache.Members [ConstructorInfo.ConstructorName];
4806 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4808 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4809 foreach (Type param in Parameters.Types) {
4810 if (param.IsArray) {
4816 has_compliant_args = true;
4820 #region IMethodData Members
4822 public MemberName MethodName {
4828 public Type ReturnType {
4834 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4836 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4837 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
4838 ec.CurrentBlock = block;
4842 public bool IsExcluded()
4847 GenericMethod IMethodData.GenericMethod {
4853 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4860 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4862 public interface IMethodData
4864 CallingConventions CallingConventions { get; }
4865 Location Location { get; }
4866 MemberName MethodName { get; }
4867 Type ReturnType { get; }
4868 GenericMethod GenericMethod { get; }
4869 ParametersCompiled ParameterInfo { get; }
4871 Attributes OptAttributes { get; }
4872 ToplevelBlock Block { get; set; }
4874 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4875 ObsoleteAttribute GetObsoleteAttribute ();
4876 string GetSignatureForError ();
4878 bool IsClsComplianceRequired ();
4879 void SetMemberIsUsed ();
4880 void EmitExtraSymbolInfo (SourceMethod source);
4884 // Encapsulates most of the Method's state
4886 public class MethodData {
4887 static FieldInfo methodbuilder_attrs_field;
4888 public readonly IMethodData method;
4890 public readonly GenericMethod GenericMethod;
4893 // Are we implementing an interface ?
4895 public MethodInfo implementing;
4900 protected InterfaceMemberBase member;
4901 protected int modifiers;
4902 protected MethodAttributes flags;
4903 protected Type declaring_type;
4904 protected MethodInfo parent_method;
4906 MethodBuilder builder = null;
4907 public MethodBuilder MethodBuilder {
4913 public Type DeclaringType {
4915 return declaring_type;
4919 public MethodData (InterfaceMemberBase member,
4920 int modifiers, MethodAttributes flags, IMethodData method)
4922 this.member = member;
4923 this.modifiers = modifiers;
4926 this.method = method;
4929 public MethodData (InterfaceMemberBase member,
4930 int modifiers, MethodAttributes flags,
4931 IMethodData method, MethodBuilder builder,
4932 GenericMethod generic, MethodInfo parent_method)
4933 : this (member, modifiers, flags, method)
4935 this.builder = builder;
4936 this.GenericMethod = generic;
4937 this.parent_method = parent_method;
4940 public bool Define (DeclSpace parent, string method_full_name)
4942 string name = method.MethodName.Basename;
4944 TypeContainer container = parent.PartialContainer;
4946 PendingImplementation pending = container.PendingImplementations;
4947 if (pending != null){
4948 implementing = pending.IsInterfaceMethod (name, member.InterfaceType, this);
4950 if (member.InterfaceType != null){
4951 if (implementing == null){
4952 if (member is PropertyBase) {
4953 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4954 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4955 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4958 Report.Error (539, method.Location,
4959 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4960 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4964 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4965 Report.SymbolRelatedToPreviousError (implementing);
4966 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4967 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4971 if (implementing != null) {
4972 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4973 if (prop_method == null) {
4974 if (TypeManager.IsSpecialMethod (implementing)) {
4975 Report.SymbolRelatedToPreviousError (implementing);
4976 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
4977 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
4978 implementing.Name.StartsWith ("get_") ? "get" : "set");
4980 } else if (implementing.DeclaringType.IsInterface) {
4981 if (!implementing.IsSpecialName) {
4982 Report.SymbolRelatedToPreviousError (implementing);
4983 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4984 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4987 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4988 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4989 Report.SymbolRelatedToPreviousError (implementing);
4990 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4991 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
5000 // For implicit implementations, make sure we are public, for
5001 // explicit implementations, make sure we are private.
5003 if (implementing != null){
5005 // Setting null inside this block will trigger a more
5006 // verbose error reporting for missing interface implementations
5008 // The "candidate" function has been flagged already
5009 // but it wont get cleared
5011 if (member.IsExplicitImpl){
5012 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
5013 Report.SymbolRelatedToPreviousError (implementing);
5014 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
5015 method.GetSignatureForError ());
5019 if (implementing.DeclaringType.IsInterface) {
5021 // If this is an interface method implementation,
5022 // check for public accessibility
5024 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5026 implementing = null;
5028 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5029 // We may never be private.
5030 implementing = null;
5032 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5034 // We may be protected if we're overriding something.
5036 implementing = null;
5041 // Static is not allowed
5043 if ((modifiers & Modifiers.STATIC) != 0){
5044 implementing = null;
5049 // If implementing is still valid, set flags
5051 if (implementing != null){
5053 // When implementing interface methods, set NewSlot
5054 // unless, we are overwriting a method.
5056 if (implementing.DeclaringType.IsInterface){
5057 if ((modifiers & Modifiers.OVERRIDE) == 0)
5058 flags |= MethodAttributes.NewSlot;
5061 MethodAttributes.Virtual |
5062 MethodAttributes.HideBySig;
5064 // Set Final unless we're virtual, abstract or already overriding a method.
5065 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5066 flags |= MethodAttributes.Final;
5069 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
5071 if (builder == null)
5074 if (container.CurrentType != null)
5075 declaring_type = container.CurrentType;
5077 declaring_type = container.TypeBuilder;
5079 if (implementing != null && member.IsExplicitImpl) {
5080 container.TypeBuilder.DefineMethodOverride (builder, implementing);
5083 TypeManager.AddMethod (builder, method);
5085 if (GenericMethod != null) {
5086 bool is_override = member.IsExplicitImpl |
5087 ((modifiers & Modifiers.OVERRIDE) != 0);
5089 if (implementing != null)
5090 parent_method = implementing;
5092 EmitContext ec = method.CreateEmitContext (container, null);
5093 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5102 /// Create the MethodBuilder for the method
5104 void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
5106 if (builder == null) {
5107 builder = container.TypeBuilder.DefineMethod (
5108 method_name, flags, method.CallingConventions,
5109 TypeManager.TypeToReflectionType (method.ReturnType),
5110 param.GetEmitTypes ());
5116 // Generic method has been already defined to resolve method parameters
5117 // correctly when they use type parameters
5119 builder.SetParameters (param.GetEmitTypes ());
5120 builder.SetReturnType (method.ReturnType);
5122 if (builder.Attributes != flags) {
5124 if (methodbuilder_attrs_field == null)
5125 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5126 methodbuilder_attrs_field.SetValue (builder, flags);
5128 Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5136 public void Emit (DeclSpace parent)
5138 ToplevelBlock block = method.Block;
5142 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5144 ec = method.CreateEmitContext (parent, null);
5146 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5148 if (GenericMethod != null)
5149 GenericMethod.EmitAttributes ();
5152 // clear the pending implementation flag
5154 if (implementing != null)
5155 parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName.Basename,
5156 member.InterfaceType, this, member.IsExplicitImpl);
5158 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5160 ec.EmitTopBlock (method, block);
5162 if (source != null) {
5163 method.EmitExtraSymbolInfo (source);
5164 source.CloseMethod ();
5169 public class Destructor : MethodOrOperator
5171 const int AllowedModifiers =
5175 static readonly string[] attribute_targets = new string [] { "method" };
5177 public static readonly string MetadataName = "Finalize";
5179 public Destructor (DeclSpace parent, int mod, ParametersCompiled parameters, Attributes attrs, Location l)
5180 : base (parent, null, TypeManager.system_void_expr, mod, AllowedModifiers,
5181 new MemberName (MetadataName, l), attrs, parameters)
5183 ModFlags &= ~Modifiers.PRIVATE;
5184 ModFlags |= Modifiers.PROTECTED;
5187 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5189 if (a.Type == pa.Conditional) {
5190 Error_ConditionalAttributeIsNotValid ();
5194 base.ApplyAttributeBuilder (a, cb, pa);
5197 protected override bool CheckBase ()
5199 flags |= MethodAttributes.Virtual;
5201 if (!base.CheckBase ())
5204 if (Parent.PartialContainer.BaseCache == null)
5207 Type base_type = Parent.PartialContainer.BaseCache.Container.Type;
5208 if (base_type != null && Block != null) {
5209 MethodGroupExpr method_expr = Expression.MethodLookup (Parent.TypeBuilder, base_type, MetadataName, Location);
5210 if (method_expr == null)
5211 throw new NotImplementedException ();
5213 method_expr.IsBase = true;
5214 method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.TypeBuilder, Location);
5216 ToplevelBlock new_block = new ToplevelBlock (Block.StartLocation);
5217 new_block.EndLocation = Block.EndLocation;
5219 Block finaly_block = new ExplicitBlock (new_block, Location, Location);
5220 Block try_block = new Block (new_block, block);
5223 // 0-size arguments to avoid CS0250 error
5224 // TODO: Should use AddScopeStatement or something else which emits correct
5227 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
5228 new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
5236 public override string GetSignatureForError ()
5238 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5241 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
5246 public override string[] ValidAttributeTargets {
5248 return attribute_targets;
5253 public abstract class MemberBase : MemberCore
5255 protected FullNamedExpression type_name;
5256 protected Type member_type;
5258 public readonly DeclSpace ds;
5259 public readonly GenericMethod GenericMethod;
5261 protected MemberBase (DeclSpace parent, GenericMethod generic,
5262 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5263 MemberName name, Attributes attrs)
5264 : base (parent, name, attrs)
5266 this.ds = generic != null ? generic : (DeclSpace) parent;
5267 this.type_name = type;
5268 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5269 GenericMethod = generic;
5270 if (GenericMethod != null)
5271 GenericMethod.ModFlags = ModFlags;
5275 // Main member define entry
5277 public override bool Define ()
5279 DoMemberTypeIndependentChecks ();
5282 // Returns false only when type resolution failed
5284 if (!ResolveMemberType ())
5287 DoMemberTypeDependentChecks ();
5292 // Any type_name independent checks
5294 protected virtual void DoMemberTypeIndependentChecks ()
5296 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5297 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5298 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5299 GetSignatureForError (), Parent.GetSignatureForError ());
5304 // Any type_name dependent checks
5306 protected virtual void DoMemberTypeDependentChecks ()
5308 // verify accessibility
5309 if (!IsAccessibleAs (MemberType)) {
5310 Report.SymbolRelatedToPreviousError (MemberType);
5311 if (this is Property)
5312 Report.Error (53, Location,
5313 "Inconsistent accessibility: property type `" +
5314 TypeManager.CSharpName (MemberType) + "' is less " +
5315 "accessible than property `" + GetSignatureForError () + "'");
5316 else if (this is Indexer)
5317 Report.Error (54, Location,
5318 "Inconsistent accessibility: indexer return type `" +
5319 TypeManager.CSharpName (MemberType) + "' is less " +
5320 "accessible than indexer `" + GetSignatureForError () + "'");
5321 else if (this is MethodCore) {
5322 if (this is Operator)
5323 Report.Error (56, Location,
5324 "Inconsistent accessibility: return type `" +
5325 TypeManager.CSharpName (MemberType) + "' is less " +
5326 "accessible than operator `" + GetSignatureForError () + "'");
5328 Report.Error (50, Location,
5329 "Inconsistent accessibility: return type `" +
5330 TypeManager.CSharpName (MemberType) + "' is less " +
5331 "accessible than method `" + GetSignatureForError () + "'");
5333 Report.Error (52, Location,
5334 "Inconsistent accessibility: field type `" +
5335 TypeManager.CSharpName (MemberType) + "' is less " +
5336 "accessible than field `" + GetSignatureForError () + "'");
5340 Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
5341 TypeManager.CheckTypeVariance (MemberType, variance, this);
5344 protected bool IsTypePermitted ()
5346 if (TypeManager.IsSpecialType (MemberType)) {
5347 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5353 protected virtual bool CheckBase ()
5355 CheckProtectedModifier ();
5360 public Type MemberType {
5361 get { return member_type; }
5364 protected virtual bool ResolveMemberType ()
5366 if (member_type != null)
5367 throw new InternalErrorException ("Multi-resolve");
5369 IResolveContext rc = GenericMethod == null ? this : (IResolveContext) ds;
5370 TypeExpr te = type_name.ResolveAsTypeTerminal (rc, false);
5375 // Replace original type name, error reporting can use fully resolved name
5379 member_type = te.Type;
5385 // Abstract class for all fields
5387 abstract public class FieldBase : MemberBase {
5388 public FieldBuilder FieldBuilder;
5389 public Status status;
5390 protected Expression initializer;
5393 public enum Status : byte {
5394 HAS_OFFSET = 4 // Used by FieldMember.
5397 static readonly string[] attribute_targets = new string [] { "field" };
5399 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5400 int allowed_mod, MemberName name, Attributes attrs)
5401 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5404 if ((mod & Modifiers.ABSTRACT) != 0)
5405 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5408 public override AttributeTargets AttributeTargets {
5410 return AttributeTargets.Field;
5414 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5416 if (a.Type == pa.FieldOffset) {
5417 status |= Status.HAS_OFFSET;
5419 if (!Parent.PartialContainer.HasExplicitLayout) {
5420 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5424 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5425 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5431 if (a.Type == pa.FixedBuffer) {
5432 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5438 if (a.Type == pa.MarshalAs) {
5439 UnmanagedMarshal marshal = a.GetMarshal (this);
5440 if (marshal != null) {
5441 FieldBuilder.SetMarshal (marshal);
5446 if ((a.HasSecurityAttribute)) {
5447 a.Error_InvalidSecurityParent ();
5451 FieldBuilder.SetCustomAttribute (cb);
5454 protected override bool CheckBase ()
5456 if (!base.CheckBase ())
5459 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5460 if (conflict_symbol == null) {
5461 if ((ModFlags & Modifiers.NEW) != 0) {
5462 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5467 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
5468 Report.SymbolRelatedToPreviousError (conflict_symbol);
5469 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5470 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5476 protected override void DoMemberTypeDependentChecks ()
5478 base.DoMemberTypeDependentChecks ();
5480 if (TypeManager.IsGenericParameter (MemberType))
5483 if (MemberType.IsSealed && MemberType.IsAbstract) {
5484 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5492 // Represents header string for documentation comment.
5494 public override string DocCommentHeader {
5495 get { return "F:"; }
5498 public override void Emit ()
5500 if (TypeManager.IsDynamicType (member_type))
5501 PredefinedAttributes.Get.Dynamic.EmitAttribute (FieldBuilder);
5503 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
5504 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
5506 if (OptAttributes != null) {
5507 OptAttributes.Emit ();
5510 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5511 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
5517 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5519 Report.SymbolRelatedToPreviousError (static_class);
5520 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5524 public Expression Initializer {
5526 if (value != null) {
5527 this.initializer = value;
5532 protected virtual bool IsFieldClsCompliant {
5534 if (FieldBuilder == null)
5537 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5541 public override string[] ValidAttributeTargets
5544 return attribute_targets;
5548 protected override bool VerifyClsCompliance ()
5550 if (!base.VerifyClsCompliance ())
5553 if (!IsFieldClsCompliant) {
5554 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
5555 GetSignatureForError ());
5560 public void SetAssigned ()
5562 caching_flags |= Flags.IsAssigned;
5566 interface IFixedBuffer
5568 FieldInfo Element { get; }
5569 Type ElementType { get; }
5572 public class FixedFieldExternal: IFixedBuffer
5574 FieldInfo element_field;
5576 public FixedFieldExternal (FieldInfo fi)
5578 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5581 #region IFixedField Members
5583 public FieldInfo Element {
5585 return element_field;
5589 public Type ElementType {
5591 return element_field.FieldType;
5599 /// Fixed buffer implementation
5601 public class FixedField : FieldBase, IFixedBuffer
5603 public const string FixedElementName = "FixedElementField";
5604 static int GlobalCounter = 0;
5605 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5606 static FieldInfo[] fi;
5608 TypeBuilder fixed_buffer_type;
5609 FieldBuilder element;
5610 Expression size_expr;
5612 const int AllowedModifiers =
5615 Modifiers.PROTECTED |
5616 Modifiers.INTERNAL |
5619 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5620 Expression size_expr, Attributes attrs, Location loc):
5621 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5623 if (RootContext.Version < LanguageVersion.ISO_2)
5624 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5626 this.size_expr = size_expr;
5629 public override bool Define()
5631 if (!base.Define ())
5634 if (!TypeManager.IsPrimitiveType (MemberType)) {
5635 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",
5636 GetSignatureForError ());
5639 // Create nested fixed buffer container
5640 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5641 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
5642 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5644 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5645 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5647 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5648 Parent.MemberCache.AddMember (FieldBuilder, this);
5649 TypeManager.RegisterFieldBase (FieldBuilder, this);
5654 protected override void DoMemberTypeIndependentChecks ()
5656 base.DoMemberTypeIndependentChecks ();
5658 if (!Parent.IsInUnsafeScope)
5659 Expression.UnsafeError (Location);
5661 if (Parent.PartialContainer.Kind != Kind.Struct) {
5662 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5663 GetSignatureForError ());
5667 public override void Emit()
5669 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5670 Constant c = size_expr.ResolveAsConstant (ec, this);
5674 IntConstant buffer_size_const = c.ImplicitConversionRequired (ec, TypeManager.int32_type, Location) as IntConstant;
5675 if (buffer_size_const == null)
5678 int buffer_size = buffer_size_const.Value;
5680 if (buffer_size <= 0) {
5681 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5685 int type_size = Expression.GetTypeSize (MemberType);
5687 if (buffer_size > int.MaxValue / type_size) {
5688 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5689 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5693 buffer_size *= type_size;
5694 EmitFieldSize (buffer_size);
5696 PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
5701 void EmitFieldSize (int buffer_size)
5703 CustomAttributeBuilder cab;
5704 PredefinedAttribute pa;
5706 pa = PredefinedAttributes.Get.StructLayout;
5707 if (pa.Constructor == null &&
5708 !pa.ResolveConstructor (Location, TypeManager.short_type))
5711 // TODO: It's not cleared
5713 fi = new FieldInfo[] { pa.Type.GetField ("Size") };
5715 object[] fi_val = new object[] { buffer_size };
5716 cab = new CustomAttributeBuilder (pa.Constructor,
5717 ctor_args, fi, fi_val);
5718 fixed_buffer_type.SetCustomAttribute (cab);
5721 // Don't emit FixedBufferAttribute attribute for private types
5723 if ((ModFlags & Modifiers.PRIVATE) != 0)
5726 pa = PredefinedAttributes.Get.FixedBuffer;
5727 if (pa.Constructor == null &&
5728 !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
5731 cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
5732 FieldBuilder.SetCustomAttribute (cab);
5735 protected override bool IsFieldClsCompliant {
5741 public void SetCharSet (TypeAttributes ta)
5743 TypeAttributes cta = fixed_buffer_type.Attributes;
5744 if ((cta & TypeAttributes.UnicodeClass) != (ta & TypeAttributes.UnicodeClass))
5745 SetTypeBuilderCharSet ((cta & ~TypeAttributes.AutoClass) | TypeAttributes.UnicodeClass);
5746 else if ((cta & TypeAttributes.AutoClass) != (ta & TypeAttributes.AutoClass))
5747 SetTypeBuilderCharSet ((cta & ~TypeAttributes.UnicodeClass) | TypeAttributes.AutoClass);
5748 else if (cta == 0 && ta != 0)
5749 SetTypeBuilderCharSet (cta & ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass));
5752 void SetTypeBuilderCharSet (TypeAttributes ta)
5754 MethodInfo mi = typeof (TypeBuilder).GetMethod ("SetCharSet", BindingFlags.Instance | BindingFlags.NonPublic);
5756 Report.RuntimeMissingSupport (Location, "TypeBuilder::SetCharSet");
5758 mi.Invoke (fixed_buffer_type, new object [] { ta });
5762 #region IFixedField Members
5764 public FieldInfo Element {
5770 public Type ElementType {
5780 // The Field class is used to represents class/struct fields during parsing.
5782 public class Field : FieldBase {
5784 // Modifiers allowed in a class declaration
5786 const int AllowedModifiers =
5789 Modifiers.PROTECTED |
5790 Modifiers.INTERNAL |
5793 Modifiers.VOLATILE |
5797 public Field (DeclSpace parent, FullNamedExpression type, int mod, MemberName name,
5799 : base (parent, type, mod, AllowedModifiers, name, attrs)
5803 bool CanBeVolatile ()
5805 if (TypeManager.IsReferenceType (MemberType))
5808 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5809 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5810 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5811 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5812 MemberType == TypeManager.float_type ||
5813 MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
5816 if (TypeManager.IsEnumType (MemberType))
5822 bool CheckStructLayout (Type type, bool isStatic)
5824 if (TypeManager.IsBuiltinType (type))
5828 if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
5832 if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
5833 if (!TypeManager.IsGenericType (type))
5836 foreach (Type t in TypeManager.GetTypeArguments (type)) {
5837 if (!CheckStructLayout (t, false))
5843 Report.Error (523, Location,
5844 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
5845 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5849 public override bool Define ()
5851 if (!base.Define ())
5856 Type[] required_modifier = null;
5857 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5858 if (TypeManager.isvolatile_type == null)
5859 TypeManager.isvolatile_type = TypeManager.CoreLookupType (
5860 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5862 if (TypeManager.isvolatile_type != null)
5863 required_modifier = new Type [] { TypeManager.isvolatile_type };
5866 FieldBuilder = Parent.TypeBuilder.DefineField (
5867 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5869 FieldBuilder = Parent.TypeBuilder.DefineField (
5870 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5872 // Don't cache inaccessible fields
5873 if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
5874 Parent.MemberCache.AddMember (FieldBuilder, this);
5877 TypeManager.RegisterFieldBase (FieldBuilder, this);
5879 catch (ArgumentException) {
5880 Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
5884 if (initializer != null) {
5885 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5886 new FieldInitializer (FieldBuilder, initializer, this));
5888 if (Parent.PartialContainer.Kind == Kind.Struct)
5889 CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
5895 protected override void DoMemberTypeDependentChecks ()
5897 base.DoMemberTypeDependentChecks ();
5899 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5900 if (!CanBeVolatile ()) {
5901 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5902 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5905 if ((ModFlags & Modifiers.READONLY) != 0) {
5906 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5907 GetSignatureForError ());
5912 protected override bool VerifyClsCompliance ()
5914 if (!base.VerifyClsCompliance ())
5917 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5918 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5926 // `set' and `get' accessors are represented with an Accessor.
5928 public class Accessor {
5930 // Null if the accessor is empty, or a Block if not
5932 public const int AllowedModifiers =
5934 Modifiers.PROTECTED |
5935 Modifiers.INTERNAL |
5938 public ToplevelBlock Block;
5939 public Attributes Attributes;
5940 public Location Location;
5941 public int ModFlags;
5942 public ParametersCompiled Parameters;
5944 public Accessor (ToplevelBlock b, int mod, Attributes attrs, ParametersCompiled p, Location loc)
5950 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5954 // Ooouh Martin, templates are missing here.
5955 // When it will be possible move here a lot of child code and template method type.
5956 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5957 protected MethodData method_data;
5958 protected ToplevelBlock block;
5959 protected ListDictionary declarative_security;
5961 // The accessor are created even if they are not wanted.
5962 // But we need them because their names are reserved.
5963 // Field says whether accessor will be emited or not
5964 public readonly bool IsDummy;
5966 protected readonly string prefix;
5968 ReturnParameter return_attributes;
5970 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
5971 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5973 this.prefix = prefix;
5977 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
5979 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5980 accessor.Attributes)
5982 this.prefix = prefix;
5983 this.block = accessor.Block;
5986 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
5988 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5991 public void UpdateName (InterfaceMemberBase member)
5993 SetMemberName (SetupName (prefix, member, Location));
5996 #region IMethodData Members
5998 public ToplevelBlock Block {
6008 public CallingConventions CallingConventions {
6010 return CallingConventions.Standard;
6014 public bool IsExcluded ()
6019 GenericMethod IMethodData.GenericMethod {
6025 public MemberName MethodName {
6031 public Type[] ParameterTypes {
6033 return ParameterInfo.Types;
6037 public abstract ParametersCompiled ParameterInfo { get ; }
6038 public abstract Type ReturnType { get; }
6039 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
6043 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6045 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
6046 Report.Error (1667, a.Location,
6047 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6048 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6052 if (a.IsValidSecurityAttribute ()) {
6053 if (declarative_security == null)
6054 declarative_security = new ListDictionary ();
6055 a.ExtractSecurityPermissionSet (declarative_security);
6059 if (a.Target == AttributeTargets.Method) {
6060 method_data.MethodBuilder.SetCustomAttribute (cb);
6064 if (a.Target == AttributeTargets.ReturnValue) {
6065 if (return_attributes == null)
6066 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6068 return_attributes.ApplyAttributeBuilder (a, cb, pa);
6072 ApplyToExtraTarget (a, cb, pa);
6075 protected virtual void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6077 throw new NotSupportedException ("You forgot to define special attribute target handling");
6080 // It is not supported for the accessors
6081 public sealed override bool Define()
6083 throw new NotSupportedException ();
6086 public virtual void Emit (DeclSpace parent)
6088 method_data.Emit (parent);
6090 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
6091 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
6092 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6093 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
6095 if (TypeManager.IsDynamicType (ReturnType)) {
6096 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6097 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
6100 if (OptAttributes != null)
6101 OptAttributes.Emit ();
6103 if (declarative_security != null) {
6104 foreach (DictionaryEntry de in declarative_security) {
6105 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6112 public override bool EnableOverloadChecks (MemberCore overload)
6114 // This can only happen with indexers and it will
6115 // be catched as indexer difference
6116 if (overload is AbstractPropertyEventMethod)
6119 if (overload is MethodCore) {
6120 caching_flags |= Flags.MethodOverloadsExist;
6126 public override bool IsClsComplianceRequired()
6131 public bool IsDuplicateImplementation (MethodCore method)
6133 if (!MemberName.Equals (method.MemberName))
6136 Type[] param_types = method.ParameterTypes;
6138 if (param_types == null || param_types.Length != ParameterTypes.Length)
6141 for (int i = 0; i < param_types.Length; i++)
6142 if (param_types [i] != ParameterTypes [i])
6145 Report.SymbolRelatedToPreviousError (method);
6146 Report.Error (82, Location, "A member `{0}' is already reserved",
6147 method.GetSignatureForError ());
6151 public override bool IsUsed
6162 // Represents header string for documentation comment.
6164 public override string DocCommentHeader {
6165 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6168 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6173 // Properties and Indexers both generate PropertyBuilders, we use this to share
6174 // their common bits.
6176 abstract public class PropertyBase : PropertyBasedMember {
6178 public class GetMethod : PropertyMethod
6180 static string[] attribute_targets = new string [] { "method", "return" };
6182 public GetMethod (PropertyBase method):
6183 base (method, "get_")
6187 public GetMethod (PropertyBase method, Accessor accessor):
6188 base (method, accessor, "get_")
6192 public override MethodBuilder Define (DeclSpace parent)
6194 base.Define (parent);
6199 method_data = new MethodData (method, ModFlags, flags, this);
6201 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6204 return method_data.MethodBuilder;
6207 public override Type ReturnType {
6209 return method.MemberType;
6213 public override ParametersCompiled ParameterInfo {
6215 return ParametersCompiled.EmptyReadOnlyParameters;
6219 public override string[] ValidAttributeTargets {
6221 return attribute_targets;
6226 public class SetMethod : PropertyMethod {
6228 static string[] attribute_targets = new string [] { "method", "param", "return" };
6229 ImplicitParameter param_attr;
6230 protected ParametersCompiled parameters;
6232 public SetMethod (PropertyBase method) :
6233 base (method, "set_")
6235 parameters = new ParametersCompiled (
6236 new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
6239 public SetMethod (PropertyBase method, Accessor accessor):
6240 base (method, accessor, "set_")
6242 this.parameters = accessor.Parameters;
6245 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6247 if (a.Target == AttributeTargets.Parameter) {
6248 if (param_attr == null)
6249 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6251 param_attr.ApplyAttributeBuilder (a, cb, pa);
6255 base.ApplyAttributeBuilder (a, cb, pa);
6258 public override ParametersCompiled ParameterInfo {
6264 public override MethodBuilder Define (DeclSpace parent)
6266 parameters.Resolve (this);
6268 base.Define (parent);
6273 method_data = new MethodData (method, ModFlags, flags, this);
6275 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6278 return method_data.MethodBuilder;
6281 public override Type ReturnType {
6283 return TypeManager.void_type;
6287 public override string[] ValidAttributeTargets {
6289 return attribute_targets;
6294 static string[] attribute_targets = new string [] { "property" };
6296 public abstract class PropertyMethod : AbstractPropertyEventMethod
6298 protected readonly PropertyBase method;
6299 protected MethodAttributes flags;
6301 public PropertyMethod (PropertyBase method, string prefix)
6302 : base (method, prefix)
6304 this.method = method;
6305 this.ModFlags = method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE);
6308 public PropertyMethod (PropertyBase method, Accessor accessor,
6310 : base (method, accessor, prefix)
6312 this.method = method;
6313 this.ModFlags = accessor.ModFlags | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
6315 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6316 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6320 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6322 if (a.IsInternalMethodImplAttribute) {
6323 method.is_external_implementation = true;
6326 base.ApplyAttributeBuilder (a, cb, pa);
6329 public override AttributeTargets AttributeTargets {
6331 return AttributeTargets.Method;
6335 public override bool IsClsComplianceRequired ()
6337 return method.IsClsComplianceRequired ();
6340 public virtual MethodBuilder Define (DeclSpace parent)
6342 CheckForDuplications ();
6345 if (method.InterfaceType != null && parent.PartialContainer.PendingImplementations != null) {
6346 MethodInfo mi = parent.PartialContainer.PendingImplementations.IsInterfaceMethod (
6347 MethodName.Name, method.InterfaceType, new MethodData (method, ModFlags, flags, this));
6349 Report.SymbolRelatedToPreviousError (mi);
6350 Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
6351 method.GetSignatureForError (), TypeManager.CSharpSignature (mi, true));
6357 TypeContainer container = parent.PartialContainer;
6360 // Check for custom access modifier
6362 if ((ModFlags & Modifiers.Accessibility) == 0) {
6363 ModFlags |= method.ModFlags;
6364 flags = method.flags;
6366 if (container.Kind == Kind.Interface)
6367 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6368 GetSignatureForError ());
6370 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6371 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6374 CheckModifiers (ModFlags);
6375 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6376 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6377 flags = Modifiers.MethodAttr (ModFlags);
6378 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6381 CheckAbstractAndExtern (block != null);
6383 if (block != null && block.IsIterator)
6384 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
6389 public bool HasCustomAccessModifier {
6391 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6395 public PropertyBase Property {
6401 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6403 return new EmitContext (this,
6404 ds, method.ds, method.Location, ig, ReturnType,
6405 method.ModFlags, false);
6408 public override ObsoleteAttribute GetObsoleteAttribute ()
6410 return method.GetObsoleteAttribute ();
6413 public override string GetSignatureForError()
6415 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6418 void CheckModifiers (int modflags)
6420 modflags &= Modifiers.Accessibility;
6422 int mflags = method.ModFlags & Modifiers.Accessibility;
6424 if ((mflags & Modifiers.PUBLIC) != 0) {
6425 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6427 else if ((mflags & Modifiers.PROTECTED) != 0) {
6428 if ((mflags & Modifiers.INTERNAL) != 0)
6429 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6431 flags |= Modifiers.PRIVATE;
6433 else if ((mflags & Modifiers.INTERNAL) != 0)
6434 flags |= Modifiers.PRIVATE;
6436 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6437 Report.Error (273, Location,
6438 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6439 GetSignatureForError (), method.GetSignatureForError ());
6443 protected bool CheckForDuplications ()
6445 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6448 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6452 public PropertyMethod Get, Set;
6453 public PropertyBuilder PropertyBuilder;
6454 public MethodBuilder GetBuilder, SetBuilder;
6456 protected bool define_set_first = false;
6458 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6459 int allowed_mod, MemberName name,
6460 Attributes attrs, bool define_set_first)
6461 : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
6463 this.define_set_first = define_set_first;
6466 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6468 if (a.HasSecurityAttribute) {
6469 a.Error_InvalidSecurityParent ();
6473 PropertyBuilder.SetCustomAttribute (cb);
6476 public override AttributeTargets AttributeTargets {
6478 return AttributeTargets.Property;
6482 protected override void DoMemberTypeDependentChecks ()
6484 base.DoMemberTypeDependentChecks ();
6488 if (MemberType.IsGenericParameter)
6492 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6493 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6497 protected override void DoMemberTypeIndependentChecks ()
6499 base.DoMemberTypeIndependentChecks ();
6502 // Accessors modifiers check
6504 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6505 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6506 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6507 GetSignatureForError ());
6510 if ((ModFlags & Modifiers.OVERRIDE) == 0 &&
6511 (Get.IsDummy && (Set.ModFlags & Modifiers.Accessibility) != 0) ||
6512 (Set.IsDummy && (Get.ModFlags & Modifiers.Accessibility) != 0)) {
6513 Report.Error (276, Location,
6514 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6515 GetSignatureForError ());
6521 GetBuilder = Get.Define (Parent);
6522 return (Get.IsDummy) ? true : GetBuilder != null;
6525 bool DefineSet (bool define)
6530 SetBuilder = Set.Define (Parent);
6531 return (Set.IsDummy) ? true : SetBuilder != null;
6534 protected bool DefineAccessors ()
6536 return DefineSet (define_set_first) &&
6538 DefineSet (!define_set_first);
6541 protected abstract PropertyInfo ResolveBaseProperty ();
6543 // TODO: rename to Resolve......
6544 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6546 PropertyInfo base_property = ResolveBaseProperty ();
6547 if (base_property == null)
6550 base_ret_type = base_property.PropertyType;
6551 MethodInfo get_accessor = base_property.GetGetMethod (true);
6552 MethodInfo set_accessor = base_property.GetSetMethod (true);
6553 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6556 // Check base property accessors conflict
6558 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6559 if (get_accessor == null) {
6560 if (Get != null && !Get.IsDummy) {
6561 Report.SymbolRelatedToPreviousError (base_property);
6562 Report.Error (545, Location,
6563 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6564 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6567 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6569 if (!Get.IsDummy && !CheckAccessModifiers (
6570 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6571 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6574 if (set_accessor == null) {
6575 if (Set != null && !Set.IsDummy) {
6576 Report.SymbolRelatedToPreviousError (base_property);
6577 Report.Error (546, Location,
6578 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6579 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6582 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6584 if (!Set.IsDummy && !CheckAccessModifiers (
6585 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6586 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6590 // When one accessor does not exist and property hides base one
6591 // we need to propagate this upwards
6592 if (set_accessor == null)
6593 set_accessor = get_accessor;
6596 // Get the less restrictive access
6598 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6601 public override void Emit ()
6604 // The PropertyBuilder can be null for explicit implementations, in that
6605 // case, we do not actually emit the ".property", so there is nowhere to
6606 // put the attribute
6608 if (PropertyBuilder != null) {
6609 if (OptAttributes != null)
6610 OptAttributes.Emit ();
6612 if (TypeManager.IsDynamicType (member_type))
6613 PredefinedAttributes.Get.Dynamic.EmitAttribute (PropertyBuilder);
6626 /// Tests whether accessors are not in collision with some method (CS0111)
6628 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6630 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6633 public override bool IsUsed
6639 return Get.IsUsed | Set.IsUsed;
6643 protected override void SetMemberName (MemberName new_name)
6645 base.SetMemberName (new_name);
6647 Get.UpdateName (this);
6648 Set.UpdateName (this);
6651 public override string[] ValidAttributeTargets {
6653 return attribute_targets;
6658 // Represents header string for documentation comment.
6660 public override string DocCommentHeader {
6661 get { return "P:"; }
6665 public class Property : PropertyBase
6667 public sealed class BackingField : Field
6669 readonly Property property;
6671 public BackingField (Property p)
6672 : base (p.Parent, p.type_name,
6673 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6674 new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
6679 public override string GetSignatureForError ()
6681 return property.GetSignatureForError ();
6685 const int AllowedModifiers =
6688 Modifiers.PROTECTED |
6689 Modifiers.INTERNAL |
6693 Modifiers.OVERRIDE |
6694 Modifiers.ABSTRACT |
6699 const int AllowedInterfaceModifiers =
6702 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6703 MemberName name, Attributes attrs, Accessor get_block,
6704 Accessor set_block, bool define_set_first)
6705 : this (parent, type, mod, name, attrs, get_block, set_block,
6706 define_set_first, null)
6710 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6711 MemberName name, Attributes attrs, Accessor get_block,
6712 Accessor set_block, bool define_set_first, Block current_block)
6713 : base (parent, type, mod,
6714 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
6715 name, attrs, define_set_first)
6717 if (get_block == null)
6718 Get = new GetMethod (this);
6720 Get = new GetMethod (this, get_block);
6722 if (set_block == null)
6723 Set = new SetMethod (this);
6725 Set = new SetMethod (this, set_block);
6727 if (!IsInterface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6728 get_block != null && get_block.Block == null &&
6729 set_block != null && set_block.Block == null) {
6730 if (RootContext.Version <= LanguageVersion.ISO_2)
6731 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6733 Get.ModFlags |= Modifiers.COMPILER_GENERATED;
6734 Set.ModFlags |= Modifiers.COMPILER_GENERATED;
6738 void CreateAutomaticProperty ()
6740 // Create backing field
6741 Field field = new BackingField (this);
6742 if (!field.Define ())
6745 Parent.PartialContainer.AddField (field);
6747 FieldExpr fe = new FieldExpr (field.FieldBuilder, Location);
6748 if ((field.ModFlags & Modifiers.STATIC) == 0)
6749 fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
6752 Get.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
6753 Return r = new Return (fe, Location);
6754 Get.Block.AddStatement (r);
6757 Set.Block = new ToplevelBlock (Set.ParameterInfo, Location);
6758 Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
6759 Set.Block.AddStatement (new StatementExpression (a));
6762 public override bool Define ()
6764 if (!base.Define ())
6767 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6769 if ((Get.ModFlags & Modifiers.COMPILER_GENERATED) != 0)
6770 CreateAutomaticProperty ();
6772 if (!DefineAccessors ())
6778 // FIXME - PropertyAttributes.HasDefault ?
6780 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6781 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6784 PropertyBuilder.SetGetMethod (GetBuilder);
6785 Parent.MemberCache.AddMember (GetBuilder, Get);
6789 PropertyBuilder.SetSetMethod (SetBuilder);
6790 Parent.MemberCache.AddMember (SetBuilder, Set);
6793 TypeManager.RegisterProperty (PropertyBuilder, this);
6794 Parent.MemberCache.AddMember (PropertyBuilder, this);
6798 public override void Emit ()
6800 if (((Set.ModFlags | Get.ModFlags) & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
6801 Report.Error (842, Location,
6802 "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
6803 GetSignatureForError ());
6809 protected override PropertyInfo ResolveBaseProperty ()
6811 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6812 Parent.TypeBuilder, Name, ParametersCompiled.EmptyReadOnlyParameters, null, true) as PropertyInfo;
6817 /// Gigantic workaround for lameness in SRE follows :
6818 /// This class derives from EventInfo and attempts to basically
6819 /// wrap around the EventBuilder so that FindMembers can quickly
6820 /// return this in it search for members
6822 public class MyEventBuilder : EventInfo {
6825 // We use this to "point" to our Builder which is
6826 // not really a MemberInfo
6828 EventBuilder MyBuilder;
6831 // We "catch" and wrap these methods
6833 MethodInfo raise, remove, add;
6835 EventAttributes attributes;
6836 Type declaring_type, reflected_type, event_type;
6841 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6843 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6845 // And now store the values in our own fields.
6847 declaring_type = type_builder;
6849 reflected_type = type_builder;
6851 attributes = event_attr;
6854 this.event_type = event_type;
6858 // Methods that you have to override. Note that you only need
6859 // to "implement" the variants that take the argument (those are
6860 // the "abstract" methods, the others (GetAddMethod()) are
6863 public override MethodInfo GetAddMethod (bool nonPublic)
6868 public override MethodInfo GetRemoveMethod (bool nonPublic)
6873 public override MethodInfo GetRaiseMethod (bool nonPublic)
6879 // These methods make "MyEventInfo" look like a Builder
6881 public void SetRaiseMethod (MethodBuilder raiseMethod)
6883 raise = raiseMethod;
6884 MyBuilder.SetRaiseMethod (raiseMethod);
6887 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6889 remove = removeMethod;
6890 MyBuilder.SetRemoveOnMethod (removeMethod);
6893 public void SetAddOnMethod (MethodBuilder addMethod)
6896 MyBuilder.SetAddOnMethod (addMethod);
6899 public void SetCustomAttribute (CustomAttributeBuilder cb)
6901 MyBuilder.SetCustomAttribute (cb);
6904 public override object [] GetCustomAttributes (bool inherit)
6906 // FIXME : There's nothing which can be seemingly done here because
6907 // we have no way of getting at the custom attribute objects of the
6912 public override object [] GetCustomAttributes (Type t, bool inherit)
6914 // FIXME : Same here !
6918 public override bool IsDefined (Type t, bool b)
6923 public override EventAttributes Attributes {
6929 public override string Name {
6935 public override Type DeclaringType {
6937 return declaring_type;
6941 public override Type ReflectedType {
6943 return reflected_type;
6947 public Type EventType {
6953 public void SetUsed ()
6955 if (my_event != null) {
6956 // my_event.SetAssigned ();
6957 my_event.SetMemberIsUsed ();
6963 /// For case when event is declared like property (with add and remove accessors).
6965 public class EventProperty: Event {
6966 abstract class AEventPropertyAccessor : AEventAccessor
6968 protected AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
6969 base (method, accessor, prefix)
6973 public override MethodBuilder Define (DeclSpace ds)
6975 CheckAbstractAndExtern (block != null);
6976 return base.Define (ds);
6979 public override string GetSignatureForError ()
6981 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
6985 sealed class AddDelegateMethod: AEventPropertyAccessor
6987 public AddDelegateMethod (Event method, Accessor accessor):
6988 base (method, accessor, AddPrefix)
6993 sealed class RemoveDelegateMethod: AEventPropertyAccessor
6995 public RemoveDelegateMethod (Event method, Accessor accessor):
6996 base (method, accessor, RemovePrefix)
7002 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
7004 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
7006 Attributes attrs, Accessor add, Accessor remove)
7007 : base (parent, type, mod_flags, name, attrs)
7009 Add = new AddDelegateMethod (this, add);
7010 Remove = new RemoveDelegateMethod (this, remove);
7013 public override bool Define()
7015 if (!base.Define ())
7022 public override string[] ValidAttributeTargets {
7024 return attribute_targets;
7030 /// Event is declared like field.
7032 public class EventField : Event {
7033 abstract class EventFieldAccessor : AEventAccessor
7035 protected EventFieldAccessor (Event method, string prefix)
7036 : base (method, prefix)
7040 public override void Emit (DeclSpace parent)
7042 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
7043 if (parent is Class) {
7044 MethodBuilder mb = method_data.MethodBuilder;
7045 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7048 // TODO: because we cannot use generics yet
7049 FieldInfo field_info = ((EventField) method).BackingField.FieldBuilder;
7050 FieldExpr f_expr = new FieldExpr (field_info, Location);
7051 if ((method.ModFlags & Modifiers.STATIC) == 0)
7052 f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.FieldType, Location);
7054 block = new ToplevelBlock (ParameterInfo, Location);
7055 block.AddStatement (new StatementExpression (
7056 new CompoundAssign (Operation,
7058 block.GetParameterReference (ParameterInfo[0].Name, Location))));
7064 protected abstract Binary.Operator Operation { get; }
7067 sealed class AddDelegateMethod: EventFieldAccessor
7069 public AddDelegateMethod (Event method):
7070 base (method, AddPrefix)
7074 protected override Binary.Operator Operation {
7075 get { return Binary.Operator.Addition; }
7079 sealed class RemoveDelegateMethod: EventFieldAccessor
7081 public RemoveDelegateMethod (Event method):
7082 base (method, RemovePrefix)
7086 protected override Binary.Operator Operation {
7087 get { return Binary.Operator.Subtraction; }
7092 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7093 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7095 public Field BackingField;
7096 public Expression Initializer;
7098 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7099 : base (parent, type, mod_flags, name, attrs)
7101 Add = new AddDelegateMethod (this);
7102 Remove = new RemoveDelegateMethod (this);
7105 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7107 if (a.Target == AttributeTargets.Field) {
7108 BackingField.ApplyAttributeBuilder (a, cb, pa);
7112 if (a.Target == AttributeTargets.Method) {
7113 int errors = Report.Errors;
7114 Add.ApplyAttributeBuilder (a, cb, pa);
7115 if (errors == Report.Errors)
7116 Remove.ApplyAttributeBuilder (a, cb, pa);
7120 base.ApplyAttributeBuilder (a, cb, pa);
7123 public override bool Define()
7125 if (!base.Define ())
7128 if (Initializer != null && (ModFlags & Modifiers.ABSTRACT) != 0) {
7129 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7130 GetSignatureForError ());
7133 if (!HasBackingField) {
7138 // FIXME: We are unable to detect whether generic event is used because
7139 // we are using FieldExpr instead of EventExpr for event access in that
7140 // case. When this issue will be fixed this hack can be removed.
7141 if (TypeManager.IsGenericType (MemberType))
7144 if (Add.IsInterfaceImplementation)
7147 TypeManager.RegisterEventField (EventBuilder, this);
7149 BackingField = new Field (Parent,
7150 new TypeExpression (MemberType, Location),
7151 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
7154 Parent.PartialContainer.AddField (BackingField);
7155 BackingField.Initializer = Initializer;
7156 BackingField.ModFlags &= ~Modifiers.COMPILER_GENERATED;
7158 // Call define because we passed fields definition
7159 return BackingField.Define ();
7162 bool HasBackingField {
7164 return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
7168 public override string[] ValidAttributeTargets
7171 return HasBackingField ? attribute_targets : attribute_targets_interface;
7176 public abstract class Event : PropertyBasedMember {
7177 public abstract class AEventAccessor : AbstractPropertyEventMethod
7179 protected readonly Event method;
7180 ImplicitParameter param_attr;
7182 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7184 public const string AddPrefix = "add_";
7185 public const string RemovePrefix = "remove_";
7187 protected AEventAccessor (Event method, string prefix)
7188 : base (method, prefix)
7190 this.method = method;
7191 this.ModFlags = method.ModFlags;
7194 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7195 : base (method, accessor, prefix)
7197 this.method = method;
7198 this.ModFlags = method.ModFlags;
7201 public bool IsInterfaceImplementation {
7202 get { return method_data.implementing != null; }
7205 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7207 if (a.IsInternalMethodImplAttribute) {
7208 method.is_external_implementation = true;
7211 base.ApplyAttributeBuilder (a, cb, pa);
7214 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7216 if (a.Target == AttributeTargets.Parameter) {
7217 if (param_attr == null)
7218 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7220 param_attr.ApplyAttributeBuilder (a, cb, pa);
7224 base.ApplyAttributeBuilder (a, cb, pa);
7227 public override AttributeTargets AttributeTargets {
7229 return AttributeTargets.Method;
7233 public override bool IsClsComplianceRequired ()
7235 return method.IsClsComplianceRequired ();
7238 public virtual MethodBuilder Define (DeclSpace parent)
7240 method_data = new MethodData (method, method.ModFlags,
7241 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7243 if (!method_data.Define (parent, method.GetFullName (MemberName)))
7246 MethodBuilder mb = method_data.MethodBuilder;
7247 ParameterInfo.ApplyAttributes (mb);
7251 public override Type ReturnType {
7253 return TypeManager.void_type;
7257 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7259 return new EmitContext (
7260 this, method.Parent, Location, ig, ReturnType,
7261 method.ModFlags, false);
7264 public override ObsoleteAttribute GetObsoleteAttribute ()
7266 return method.GetObsoleteAttribute ();
7269 public override string[] ValidAttributeTargets {
7271 return attribute_targets;
7275 public override ParametersCompiled ParameterInfo {
7277 return method.parameters;
7283 const int AllowedModifiers =
7286 Modifiers.PROTECTED |
7287 Modifiers.INTERNAL |
7292 Modifiers.OVERRIDE |
7294 Modifiers.ABSTRACT |
7297 const int AllowedInterfaceModifiers =
7300 public AEventAccessor Add, Remove;
7301 public MyEventBuilder EventBuilder;
7302 public MethodBuilder AddBuilder, RemoveBuilder;
7304 ParametersCompiled parameters;
7306 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7307 : base (parent, null, type, mod_flags,
7308 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7313 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7315 if ((a.HasSecurityAttribute)) {
7316 a.Error_InvalidSecurityParent ();
7320 EventBuilder.SetCustomAttribute (cb);
7323 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7325 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7328 public override AttributeTargets AttributeTargets {
7330 return AttributeTargets.Event;
7334 public override bool Define ()
7336 if (!base.Define ())
7339 if (!TypeManager.IsDelegateType (MemberType)) {
7340 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7343 parameters = ParametersCompiled.CreateFullyResolved (
7344 new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
7349 if (TypeManager.delegate_combine_delegate_delegate == null) {
7350 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7351 TypeManager.delegate_type, "Combine", Location,
7352 TypeManager.delegate_type, TypeManager.delegate_type);
7354 if (TypeManager.delegate_remove_delegate_delegate == null) {
7355 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7356 TypeManager.delegate_type, "Remove", Location,
7357 TypeManager.delegate_type, TypeManager.delegate_type);
7361 // Now define the accessors
7364 AddBuilder = Add.Define (Parent);
7365 if (AddBuilder == null)
7368 RemoveBuilder = Remove.Define (Parent);
7369 if (RemoveBuilder == null)
7372 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7373 EventBuilder.SetAddOnMethod (AddBuilder);
7374 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7376 Parent.MemberCache.AddMember (EventBuilder, this);
7377 Parent.MemberCache.AddMember (AddBuilder, Add);
7378 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7383 public override void Emit ()
7385 if (OptAttributes != null) {
7386 OptAttributes.Emit ();
7390 Remove.Emit (Parent);
7395 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7397 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7398 Parent.TypeBuilder, Name);
7403 AParametersCollection pd = TypeManager.GetParameterData (mi);
7404 base_ret_type = pd.Types [0];
7409 // Represents header string for documentation comment.
7411 public override string DocCommentHeader {
7412 get { return "E:"; }
7417 public class Indexer : PropertyBase
7419 public class GetIndexerMethod : GetMethod
7421 ParametersCompiled parameters;
7423 public GetIndexerMethod (Indexer method):
7426 this.parameters = method.parameters;
7429 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7430 base (method, accessor)
7432 parameters = accessor.Parameters;
7435 public override MethodBuilder Define (DeclSpace parent)
7437 parameters.Resolve (this);
7438 return base.Define (parent);
7441 public override bool EnableOverloadChecks (MemberCore overload)
7443 if (base.EnableOverloadChecks (overload)) {
7444 overload.caching_flags |= Flags.MethodOverloadsExist;
7451 public override ParametersCompiled ParameterInfo {
7458 public class SetIndexerMethod: SetMethod
7460 public SetIndexerMethod (Indexer method):
7463 parameters = ParametersCompiled.MergeGenerated (method.parameters, false, parameters [0], null);
7466 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7467 base (method, accessor)
7469 parameters = method.Get.IsDummy ? accessor.Parameters : accessor.Parameters.Clone ();
7472 public override bool EnableOverloadChecks (MemberCore overload)
7474 if (base.EnableOverloadChecks (overload)) {
7475 overload.caching_flags |= Flags.MethodOverloadsExist;
7483 const int AllowedModifiers =
7486 Modifiers.PROTECTED |
7487 Modifiers.INTERNAL |
7491 Modifiers.OVERRIDE |
7496 const int AllowedInterfaceModifiers =
7499 public readonly ParametersCompiled parameters;
7501 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7502 ParametersCompiled parameters, Attributes attrs,
7503 Accessor get_block, Accessor set_block, bool define_set_first)
7504 : base (parent, type, mod,
7505 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7506 name, attrs, define_set_first)
7508 this.parameters = parameters;
7510 if (get_block == null)
7511 Get = new GetIndexerMethod (this);
7513 Get = new GetIndexerMethod (this, get_block);
7515 if (set_block == null)
7516 Set = new SetIndexerMethod (this);
7518 Set = new SetIndexerMethod (this, set_block);
7521 protected override bool CheckForDuplications ()
7523 return Parent.MemberCache.CheckExistingMembersOverloads (this, GetFullName (MemberName), parameters);
7526 public override bool Define ()
7528 if (!base.Define ())
7531 if (!DefineParameters (parameters))
7534 if (OptAttributes != null) {
7535 Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
7536 if (indexer_attr != null) {
7537 // Remove the attribute from the list because it is not emitted
7538 OptAttributes.Attrs.Remove (indexer_attr);
7540 string name = indexer_attr.GetIndexerAttributeValue ();
7546 if (IsExplicitImpl) {
7547 Report.Error (415, indexer_attr.Location,
7548 "The `IndexerName' attribute is valid only on an " +
7549 "indexer that is not an explicit interface member declaration");
7553 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7554 Report.Error (609, indexer_attr.Location,
7555 "Cannot set the `IndexerName' attribute on an indexer marked override");
7561 if (InterfaceType != null) {
7562 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7563 if (base_IndexerName != Name)
7564 ShortName = base_IndexerName;
7567 if (!Parent.PartialContainer.AddMember (this) ||
7568 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7571 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7573 if (!DefineAccessors ())
7580 // Now name the parameters
7582 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7583 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.GetEmitTypes ());
7586 PropertyBuilder.SetGetMethod (GetBuilder);
7587 Parent.MemberCache.AddMember (GetBuilder, Get);
7591 PropertyBuilder.SetSetMethod (SetBuilder);
7592 Parent.MemberCache.AddMember (SetBuilder, Set);
7595 TypeManager.RegisterIndexer (PropertyBuilder, parameters);
7596 Parent.MemberCache.AddMember (PropertyBuilder, this);
7600 public override bool EnableOverloadChecks (MemberCore overload)
7602 if (overload is Indexer) {
7603 caching_flags |= Flags.MethodOverloadsExist;
7607 return base.EnableOverloadChecks (overload);
7610 public override string GetDocCommentName (DeclSpace ds)
7612 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7615 public override string GetSignatureForError ()
7617 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7618 if (MemberName.Left != null) {
7620 sb.Append (MemberName.Left.GetSignatureForError ());
7623 sb.Append (".this");
7624 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7625 return sb.ToString ();
7628 protected override PropertyInfo ResolveBaseProperty ()
7630 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7631 Parent.TypeBuilder, Name, parameters, null, true) as PropertyInfo;
7634 protected override bool VerifyClsCompliance ()
7636 if (!base.VerifyClsCompliance ())
7639 parameters.VerifyClsCompliance ();
7644 public class Operator : MethodOrOperator {
7646 const int AllowedModifiers =
7652 public enum OpType : byte {
7662 // Unary and Binary operators
7685 // Implicit and Explicit
7689 // Just because of enum
7693 public readonly OpType OperatorType;
7695 static readonly string [] [] names;
7699 names = new string[(int)OpType.TOP][];
7700 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7701 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7702 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7703 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7704 names [(int) OpType.True] = new string [] { "true", "op_True" };
7705 names [(int) OpType.False] = new string [] { "false", "op_False" };
7706 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7707 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7708 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7709 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7710 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7711 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7712 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7713 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7714 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7715 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7716 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7717 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7718 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7719 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7720 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7721 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7722 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7723 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7724 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7725 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7728 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7729 int mod_flags, ParametersCompiled parameters,
7730 ToplevelBlock block, Attributes attrs, Location loc)
7731 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
7732 new MemberName (GetMetadataName (type), loc), attrs, parameters)
7734 OperatorType = type;
7738 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7740 if (a.Type == pa.Conditional) {
7741 Error_ConditionalAttributeIsNotValid ();
7745 base.ApplyAttributeBuilder (a, cb, pa);
7748 public override bool Define ()
7750 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7751 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7752 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7755 if (!base.Define ())
7758 // imlicit and explicit operator of same types are not allowed
7759 if (OperatorType == OpType.Explicit)
7760 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
7761 else if (OperatorType == OpType.Implicit)
7762 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
7764 Type declaring_type = MethodData.DeclaringType;
7765 Type return_type = MemberType;
7766 Type first_arg_type = ParameterTypes [0];
7768 Type first_arg_type_unwrap = first_arg_type;
7769 if (TypeManager.IsNullableType (first_arg_type))
7770 first_arg_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (first_arg_type) [0]);
7772 Type return_type_unwrap = return_type;
7773 if (TypeManager.IsNullableType (return_type))
7774 return_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (return_type) [0]);
7776 if (TypeManager.IsDynamicType (return_type) || TypeManager.IsDynamicType (first_arg_type)) {
7777 Report.Error (1964, Location,
7778 "User-defined operator `{0}' cannot convert to or from the dynamic type",
7779 GetSignatureForError ());
7785 // Rules for conversion operators
7787 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7788 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7789 Report.Error (555, Location,
7790 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7795 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7796 conv_type = first_arg_type;
7797 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7798 conv_type = return_type;
7800 Report.Error (556, Location,
7801 "User-defined conversion must convert to or from the enclosing type");
7806 // Because IsInterface and IsClass are not supported
7808 if (!TypeManager.IsGenericParameter (conv_type)) {
7809 if (conv_type.IsInterface) {
7810 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7811 GetSignatureForError ());
7815 if (conv_type.IsClass) {
7816 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7817 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7818 GetSignatureForError ());
7822 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7823 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7824 GetSignatureForError ());
7829 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7830 if (first_arg_type != declaring_type || Parameters.Types [1] != TypeManager.int32_type) {
7831 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");
7834 } else if (Parameters.Count == 1) {
7835 // Checks for Unary operators
7837 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7838 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7839 Report.Error (448, Location,
7840 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7843 if (first_arg_type != declaring_type) {
7845 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7850 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7851 Report.Error (562, Location,
7852 "The parameter type of a unary operator must be the containing type");
7856 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7857 if (return_type != TypeManager.bool_type){
7860 "The return type of operator True or False " +
7867 // Checks for Binary operators
7869 if (first_arg_type != declaring_type &&
7870 Parameters.Types [1] != declaring_type){
7873 "One of the parameters of a binary operator must " +
7874 "be the containing type");
7882 protected override bool ResolveMemberType ()
7884 if (!base.ResolveMemberType ())
7887 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7891 // Operator cannot be override
7892 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7897 public static string GetName (OpType ot)
7899 return names [(int) ot] [0];
7902 public static string GetName (string metadata_name)
7904 for (int i = 0; i < names.Length; ++i) {
7905 if (names [i] [1] == metadata_name)
7906 return names [i] [0];
7911 public static string GetMetadataName (OpType ot)
7913 return names [(int) ot] [1];
7916 public static string GetMetadataName (string name)
7918 for (int i = 0; i < names.Length; ++i) {
7919 if (names [i] [0] == name)
7920 return names [i] [1];
7925 public OpType GetMatchingOperator ()
7927 switch (OperatorType) {
7928 case OpType.Equality:
7929 return OpType.Inequality;
7930 case OpType.Inequality:
7931 return OpType.Equality;
7933 return OpType.False;
7936 case OpType.GreaterThan:
7937 return OpType.LessThan;
7938 case OpType.LessThan:
7939 return OpType.GreaterThan;
7940 case OpType.GreaterThanOrEqual:
7941 return OpType.LessThanOrEqual;
7942 case OpType.LessThanOrEqual:
7943 return OpType.GreaterThanOrEqual;
7949 public override string GetSignatureForError ()
7951 StringBuilder sb = new StringBuilder ();
7952 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7953 sb.AppendFormat ("{0}.{1} operator {2}",
7954 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
7957 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7960 sb.Append (Parameters.GetSignatureForError ());
7961 return sb.ToString ();
7966 // This is used to compare method signatures
7968 struct MethodSignature {
7970 public Type RetType;
7971 public Type [] Parameters;
7974 /// This delegate is used to extract methods which have the
7975 /// same signature as the argument
7977 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7979 public MethodSignature (string name, Type ret_type, Type [] parameters)
7984 if (parameters == null)
7985 Parameters = Type.EmptyTypes;
7987 Parameters = parameters;
7990 public override string ToString ()
7993 if (Parameters.Length != 0){
7994 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7995 for (int i = 0; i < Parameters.Length; i++){
7996 sb.Append (Parameters [i]);
7997 if (i+1 < Parameters.Length)
8000 pars = sb.ToString ();
8003 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
8006 public override int GetHashCode ()
8008 return Name.GetHashCode ();
8011 public override bool Equals (Object o)
8013 MethodSignature other = (MethodSignature) o;
8015 if (other.Name != Name)
8018 if (other.RetType != RetType)
8021 if (Parameters == null){
8022 if (other.Parameters == null)
8027 if (other.Parameters == null)
8030 int c = Parameters.Length;
8031 if (other.Parameters.Length != c)
8034 for (int i = 0; i < c; i++)
8035 if (other.Parameters [i] != Parameters [i])
8041 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8043 MethodSignature sig = (MethodSignature) filter_criteria;
8045 if (m.Name != sig.Name)
8049 MethodInfo mi = m as MethodInfo;
8050 PropertyInfo pi = m as PropertyInfo;
8053 ReturnType = mi.ReturnType;
8054 else if (pi != null)
8055 ReturnType = pi.PropertyType;
8060 // we use sig.RetType == null to mean `do not check the
8061 // method return value.
8063 if (sig.RetType != null) {
8064 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8070 args = TypeManager.GetParameterData (mi).Types;
8072 args = TypeManager.GetParameterData (pi).Types;
8073 Type [] sigp = sig.Parameters;
8075 if (args.Length != sigp.Length)
8078 for (int i = args.Length - 1; i >= 0; i--)
8079 if (!TypeManager.IsEqual (args [i], sigp [i]))