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 ();
161 GetHashCode = 1 << 1,
162 HasStaticFieldInitializer = 1 << 2
166 // Whether this is a struct, class or interface
167 public readonly Kind Kind;
169 // Holds a list of classes and structures
170 protected ArrayList types;
172 MemberCoreArrayList ordered_explicit_member_list;
173 MemberCoreArrayList ordered_member_list;
175 // Holds the list of properties
176 MemberCoreArrayList properties;
178 // Holds the list of delegates
179 MemberCoreArrayList delegates;
181 // Holds the list of constructors
182 protected MemberCoreArrayList instance_constructors;
184 // Holds the list of fields
185 protected MemberCoreArrayList fields;
187 // Holds a list of fields that have initializers
188 protected ArrayList initialized_fields;
190 // Holds a list of static fields that have initializers
191 protected ArrayList initialized_static_fields;
193 // Holds the list of constants
194 protected MemberCoreArrayList constants;
196 // Holds the methods.
197 MemberCoreArrayList methods;
200 protected MemberCoreArrayList events;
202 // Holds the indexers
205 // Holds the operators
206 MemberCoreArrayList operators;
208 // Holds the compiler generated classes
209 ArrayList compiler_generated;
212 // Pointers to the default constructor and the default static constructor
214 protected Constructor default_constructor;
215 protected Constructor default_static_constructor;
218 // Points to the first non-static field added to the container.
220 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
221 // and the first one's as good as any.
223 FieldBase first_nonstatic_field = null;
226 // This one is computed after we can distinguish interfaces
227 // from classes from the arraylist `type_bases'
230 TypeExpr[] iface_exprs;
232 GenericTypeParameterBuilder[] nested_gen_params;
234 protected ArrayList type_bases;
236 protected bool members_defined;
237 bool members_defined_ok;
239 // The interfaces we implement.
240 protected Type[] ifaces;
242 // The base member cache and our member cache
243 MemberCache base_cache;
244 protected MemberCache member_cache;
246 public const string DefaultIndexerName = "Item";
248 private bool seen_normal_indexers = false;
249 private string indexer_name = DefaultIndexerName;
250 protected bool requires_delayed_unmanagedtype_check;
252 private CachedMethods cached_method;
254 ArrayList partial_parts;
257 /// The pending methods that need to be implemented
258 // (interfaces or abstract methods)
260 PendingImplementation pending;
262 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
263 Attributes attrs, Kind kind)
264 : base (ns, parent, name, attrs)
266 if (parent != null && parent.NamespaceEntry != ns)
267 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
270 this.PartialContainer = this;
273 public bool AddMember (MemberCore symbol)
275 return AddToContainer (symbol, symbol.MemberName.Basename);
278 protected virtual bool AddMemberType (DeclSpace ds)
280 return AddToContainer (ds, ds.Basename);
283 protected virtual void RemoveMemberType (DeclSpace ds)
285 RemoveFromContainer (ds.Basename);
288 public void AddConstant (Const constant)
290 if (!AddMember (constant))
293 if (constants == null)
294 constants = new MemberCoreArrayList ();
296 constants.Add (constant);
299 public TypeContainer AddTypeContainer (TypeContainer tc)
301 if (!AddMemberType (tc))
305 types = new MemberCoreArrayList ();
311 public virtual TypeContainer AddPartial (TypeContainer next_part)
313 return AddPartial (next_part, next_part.Basename);
316 protected TypeContainer AddPartial (TypeContainer next_part, string name)
318 next_part.ModFlags |= Modifiers.PARTIAL;
319 TypeContainer tc = defined_names [name] as TypeContainer;
322 return AddTypeContainer (next_part);
324 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
325 Report.SymbolRelatedToPreviousError (next_part);
326 Error_MissingPartialModifier (tc);
329 if (tc.Kind != next_part.Kind) {
330 Report.SymbolRelatedToPreviousError (tc);
331 Report.Error (261, next_part.Location,
332 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
333 next_part.GetSignatureForError ());
336 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
337 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
338 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
339 Report.SymbolRelatedToPreviousError (tc);
340 Report.Error (262, next_part.Location,
341 "Partial declarations of `{0}' have conflicting accessibility modifiers",
342 next_part.GetSignatureForError ());
345 if (tc.partial_parts == null)
346 tc.partial_parts = new ArrayList (1);
348 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
349 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
350 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
351 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
352 tc.ModFlags |= next_part.ModFlags;
354 tc.ModFlags |= next_part.ModFlags;
357 if (next_part.attributes != null) {
358 if (tc.attributes == null)
359 tc.attributes = next_part.attributes;
361 tc.attributes.AddAttributes (next_part.attributes.Attrs);
364 next_part.PartialContainer = tc;
365 tc.partial_parts.Add (next_part);
369 public virtual void RemoveTypeContainer (TypeContainer next_part)
372 types.Remove (next_part);
373 RemoveMemberType (next_part);
376 public void AddDelegate (Delegate d)
378 if (!AddMemberType (d))
381 if (delegates == null)
382 delegates = new MemberCoreArrayList ();
387 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
389 if (ordered_explicit_member_list == null) {
390 ordered_explicit_member_list = new MemberCoreArrayList ();
391 ordered_member_list = new MemberCoreArrayList ();
395 if (Kind == Kind.Interface) {
396 Report.Error (541, mc.Location,
397 "`{0}': explicit interface declaration can only be declared in a class or struct",
398 mc.GetSignatureForError ());
401 ordered_explicit_member_list.Add (mc);
402 alist.Insert (0, mc);
404 ordered_member_list.Add (mc);
410 public void AddMethod (MethodOrOperator method)
412 if (!AddToContainer (method, method.MemberName.Basename))
416 methods = new MemberCoreArrayList ();
418 if (method.MemberName.Left != null)
419 AddMemberToList (method, methods, true);
421 AddMemberToList (method, methods, false);
424 public void AddConstructor (Constructor c)
426 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
427 if (!AddToContainer (c, is_static ?
428 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
431 if (is_static && c.Parameters.IsEmpty){
432 if (default_static_constructor != null) {
433 Report.SymbolRelatedToPreviousError (default_static_constructor);
434 Report.Error (111, c.Location,
435 "A member `{0}' is already defined. Rename this member or use different parameter types",
436 c.GetSignatureForError ());
440 default_static_constructor = c;
442 if (c.Parameters.IsEmpty)
443 default_constructor = c;
445 if (instance_constructors == null)
446 instance_constructors = new MemberCoreArrayList ();
448 instance_constructors.Add (c);
452 public bool AddField (FieldBase field)
454 if (!AddMember (field))
458 fields = new MemberCoreArrayList ();
462 if ((field.ModFlags & Modifiers.STATIC) != 0)
465 if (first_nonstatic_field == null) {
466 first_nonstatic_field = field;
470 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
471 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
472 Report.Warning (282, 3, field.Location,
473 "struct instance field `{0}' found in different declaration from instance field `{1}'",
474 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
479 public void AddProperty (Property prop)
481 if (!AddMember (prop) ||
482 !AddMember (prop.Get) || !AddMember (prop.Set))
485 if (properties == null)
486 properties = new MemberCoreArrayList ();
488 if (prop.MemberName.Left != null)
489 AddMemberToList (prop, properties, true);
491 AddMemberToList (prop, properties, false);
494 public void AddEvent (Event e)
499 if (e is EventProperty) {
500 if (!AddMember (e.Add))
503 if (!AddMember (e.Remove))
508 events = new MemberCoreArrayList ();
514 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
516 public void AddIndexer (Indexer i)
518 if (indexers == null)
519 indexers = new ArrayList ();
521 if (i.IsExplicitImpl)
522 AddMemberToList (i, indexers, true);
524 AddMemberToList (i, indexers, false);
527 public void AddOperator (Operator op)
532 if (operators == null)
533 operators = new OperatorArrayList (this);
538 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
540 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
542 if (compiler_generated == null)
543 compiler_generated = new ArrayList ();
545 compiler_generated.Add (c);
548 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
550 if (a.Type == pa.DefaultMember) {
551 if (Indexers != null) {
552 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
557 base.ApplyAttributeBuilder (a, cb, pa);
560 public override AttributeTargets AttributeTargets {
562 throw new NotSupportedException ();
566 public ArrayList Types {
572 public MemberCoreArrayList Methods {
578 public ArrayList Constants {
584 protected Type BaseType {
586 return TypeBuilder.BaseType;
590 public ArrayList Fields {
596 public ArrayList InstanceConstructors {
598 return instance_constructors;
602 public ArrayList Properties {
608 public ArrayList Events {
614 public ArrayList Indexers {
620 public ArrayList Operators {
626 public ArrayList Delegates {
632 protected override TypeAttributes TypeAttr {
634 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
638 public string IndexerName {
640 return indexers == null ? DefaultIndexerName : indexer_name;
644 public bool IsComImport {
646 if (OptAttributes == null)
649 return OptAttributes.Contains (PredefinedAttributes.Get.ComImport);
653 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
655 if ((field.ModFlags & Modifiers.STATIC) != 0){
656 if (initialized_static_fields == null) {
657 PartialContainer.HasStaticFieldInitializer = true;
658 initialized_static_fields = new ArrayList (4);
661 initialized_static_fields.Add (expression);
663 if (initialized_fields == null)
664 initialized_fields = new ArrayList (4);
666 initialized_fields.Add (expression);
670 public void ResolveFieldInitializers (EmitContext ec)
672 if (partial_parts != null) {
673 foreach (TypeContainer part in partial_parts) {
674 part.DoResolveFieldInitializers (ec);
677 DoResolveFieldInitializers (ec);
680 void DoResolveFieldInitializers (EmitContext ec)
683 if (initialized_static_fields == null)
686 bool has_complex_initializer = !RootContext.Optimize;
688 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
689 for (i = 0; i < initialized_static_fields.Count; ++i) {
690 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
691 ExpressionStatement s = fi.ResolveStatement (ec);
693 s = EmptyExpressionStatement.Instance;
694 } else if (fi.IsComplexInitializer) {
695 has_complex_initializer |= true;
701 for (i = 0; i < initialized_static_fields.Count; ++i) {
702 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
704 // Need special check to not optimize code like this
705 // static int a = b = 5;
708 if (!has_complex_initializer && fi.IsDefaultInitializer)
711 ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
717 if (initialized_fields == null)
720 for (int i = 0; i < initialized_fields.Count; ++i) {
721 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
722 ExpressionStatement s = fi.ResolveStatement (ec);
727 // Field is re-initialized to its default value => removed
729 if (fi.IsDefaultInitializer && RootContext.Optimize)
732 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
737 // Emits the instance field initializers
739 public bool EmitFieldInitializers (EmitContext ec)
741 if (partial_parts != null) {
742 foreach (TypeContainer part in partial_parts)
743 part.EmitFieldInitializers (ec);
749 fields = initialized_static_fields;
751 fields = initialized_fields;
757 foreach (FieldInitializer f in fields) {
758 f.EmitStatement (ec);
763 public override string DocComment {
775 public PendingImplementation PendingImplementations {
776 get { return pending; }
779 public override bool GetClsCompliantAttributeValue ()
781 if (PartialContainer != this)
782 return PartialContainer.GetClsCompliantAttributeValue ();
784 return base.GetClsCompliantAttributeValue ();
787 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
789 // FIXME: get rid of partial_parts and store lists of bases of each part here
790 // assumed, not verified: 'part' is in 'partial_parts'
791 ((TypeContainer) part).type_bases = bases;
795 /// This function computes the Base class and also the
796 /// list of interfaces that the class or struct @c implements.
798 /// The return value is an array (might be null) of
799 /// interfaces implemented (as Types).
801 /// The @base_class argument is set to the base object or null
802 /// if this is `System.Object'.
804 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
807 if (type_bases == null)
810 int count = type_bases.Count;
811 TypeExpr [] ifaces = null;
812 for (int i = 0, j = 0; i < count; i++){
813 FullNamedExpression fne = (FullNamedExpression) type_bases [i];
816 // Standard ResolveAsTypeTerminal cannot be used in this case because
817 // it does ObsoleteAttribute and constraint checks which require
818 // base type to be set
820 TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (this, false);
821 if (fne_resolved == null)
824 if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
825 base_class = fne_resolved;
830 ifaces = new TypeExpr [count - i];
832 if (fne_resolved.Type.IsInterface) {
833 for (int ii = 0; ii < j; ++ii) {
834 if (TypeManager.IsEqual (fne_resolved.Type, ifaces [ii].Type)) {
835 Report.Error (528, Location, "`{0}' is already listed in interface list",
836 fne_resolved.GetSignatureForError ());
841 if (Kind == Kind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
842 Report.Error (61, fne.Location,
843 "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
844 fne_resolved.GetSignatureForError (), GetSignatureForError ());
847 Report.SymbolRelatedToPreviousError (fne_resolved.Type);
848 if (Kind != Kind.Class) {
849 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
850 } else if (base_class != null)
851 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
852 GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
854 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
855 GetSignatureForError (), fne_resolved.GetSignatureForError ());
859 ifaces [j++] = fne_resolved;
865 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
867 ArrayList ifaces = new ArrayList (0);
868 if (iface_exprs != null)
869 ifaces.AddRange (iface_exprs);
871 foreach (TypeContainer part in partial_parts) {
872 TypeExpr new_base_class;
873 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
874 if (new_base_class != TypeManager.system_object_expr) {
875 if (base_class == TypeManager.system_object_expr)
876 base_class = new_base_class;
878 if (new_base_class != null && !new_base_class.Equals (base_class)) {
879 Report.SymbolRelatedToPreviousError (base_class.Location, "");
880 Report.Error (263, part.Location,
881 "Partial declarations of `{0}' must not specify different base classes",
882 part.GetSignatureForError ());
889 if (new_ifaces == null)
892 foreach (TypeExpr iface in new_ifaces) {
893 if (ifaces.Contains (iface))
900 if (ifaces.Count == 0)
903 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
906 bool CheckGenericInterfaces (Type[] ifaces)
909 ArrayList already_checked = new ArrayList ();
911 for (int i = 0; i < ifaces.Length; i++) {
912 Type iface = ifaces [i];
913 foreach (Type t in already_checked) {
917 Type[] inferred = new Type [CountTypeParameters];
918 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
921 Report.Error (695, Location,
922 "`{0}' cannot implement both `{1}' and `{2}' " +
923 "because they may unify for some type parameter substitutions",
924 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
925 TypeManager.CSharpName (t));
929 already_checked.Add (iface);
938 bool CreateTypeBuilder ()
941 Type default_parent = null;
942 if (Kind == Kind.Struct)
943 default_parent = TypeManager.value_type;
944 else if (Kind == Kind.Enum)
945 default_parent = TypeManager.enum_type;
948 // Sets .size to 1 for structs with no instance fields
950 int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
953 if (TypeManager.NamespaceClash (Name, Location)) {
957 ModuleBuilder builder = Module.Builder;
958 TypeBuilder = builder.DefineType (
959 Name, TypeAttr, default_parent, type_size);
961 TypeBuilder builder = Parent.TypeBuilder;
963 TypeBuilder = builder.DefineNestedType (
964 Basename, TypeAttr, default_parent, type_size);
966 } catch (ArgumentException) {
967 Report.RuntimeMissingSupport (Location, "static classes");
971 TypeManager.AddUserType (this);
974 string[] param_names = new string [TypeParameters.Length];
975 for (int i = 0; i < TypeParameters.Length; i++)
976 param_names [i] = TypeParameters [i].Name;
979 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
981 int offset = CountTypeParameters - CurrentTypeParameters.Length;
983 nested_gen_params = new GenericTypeParameterBuilder [offset];
984 Array.Copy (gen_params, nested_gen_params, offset);
987 for (int i = offset; i < gen_params.Length; i++)
988 CurrentTypeParameters [i - offset].Define (gen_params [i]);
990 nested_gen_params = null;
991 throw new NotSupportedException ();
998 bool DefineBaseTypes ()
1000 iface_exprs = ResolveBaseTypes (out base_type);
1001 if (partial_parts != null) {
1002 iface_exprs = GetNormalPartialBases (ref base_type);
1006 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1007 // which in turn should have called DefineType()s on base types if necessary.
1009 // None of the code below should trigger DefineType()s on classes that we depend on.
1010 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1012 // Let's do it as soon as possible, since code below can call DefineType() on classes
1013 // that depend on us to be populated before they are.
1015 if (!(this is CompilerGeneratedClass))
1016 RootContext.RegisterOrder (this);
1018 if (!CheckRecursiveDefinition (this))
1021 if (base_type != null && base_type.Type != null) {
1022 TypeBuilder.SetParent (base_type.Type);
1025 // add interfaces that were not added at type creation
1026 if (iface_exprs != null) {
1027 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1031 foreach (Type itype in ifaces)
1032 TypeBuilder.AddInterfaceImplementation (itype);
1034 if (!CheckGenericInterfaces (ifaces))
1037 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1044 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1046 public TypeBuilder CreateType ()
1048 if (TypeBuilder != null)
1054 if (!CreateTypeBuilder ()) {
1059 if (partial_parts != null) {
1060 foreach (TypeContainer part in partial_parts)
1061 part.TypeBuilder = TypeBuilder;
1064 if (Types != null) {
1065 foreach (TypeContainer tc in Types) {
1066 if (tc.CreateType () == null) {
1078 public override TypeBuilder DefineType ()
1085 type_defined = true;
1087 if (CreateType () == null) {
1092 if (!DefineBaseTypes ()) {
1097 if (!DefineNestedTypes ()) {
1105 public override void SetParameterInfo (ArrayList constraints_list)
1107 base.SetParameterInfo (constraints_list);
1109 if (!is_generic || PartialContainer == this)
1112 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1113 for (int i = 0; i < tc_names.Length; ++i) {
1114 if (tc_names [i].Name != type_params [i].Name) {
1115 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1116 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1117 GetSignatureForError ());
1123 void UpdateTypeParameterConstraints (TypeContainer part)
1125 TypeParameter[] current_params = CurrentTypeParameters;
1126 for (int i = 0; i < current_params.Length; i++) {
1127 Constraints c = part.CurrentTypeParameters [i].Constraints;
1131 if (current_params [i].UpdateConstraints (part, c))
1134 Report.SymbolRelatedToPreviousError (Location, "");
1135 Report.Error (265, part.Location,
1136 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1137 GetSignatureForError (), current_params [i].GetSignatureForError ());
1141 public bool ResolveType ()
1143 if (!DoResolveType ())
1146 if (compiler_generated != null) {
1147 foreach (CompilerGeneratedClass c in compiler_generated)
1148 if (!c.ResolveType ())
1155 protected virtual bool DoResolveType ()
1160 if (PartialContainer != this)
1161 throw new InternalErrorException ();
1163 TypeExpr current_type = null;
1165 foreach (TypeParameter type_param in CurrentTypeParameters) {
1166 if (!type_param.Resolve (this)) {
1172 if (partial_parts != null && is_generic) {
1173 foreach (TypeContainer part in partial_parts)
1174 UpdateTypeParameterConstraints (part);
1177 for (int i = 0; i < TypeParameters.Length; ++i) {
1179 // FIXME: Same should be done for delegates
1180 // TODO: Quite ugly way how to propagate constraints to
1183 if (nested_gen_params != null && i < nested_gen_params.Length) {
1184 TypeParameters [i].SetConstraints (nested_gen_params [i]);
1186 if (!TypeParameters [i].DefineType (this)) {
1193 // TODO: Very strange, why not simple make generic type from
1194 // current type parameters
1195 current_type = new GenericTypeExpr (this, Location);
1196 current_type = current_type.ResolveAsTypeTerminal (this, false);
1197 if (current_type == null) {
1202 CurrentType = current_type.Type;
1206 protected virtual bool DefineNestedTypes ()
1208 if (Types != null) {
1209 foreach (TypeContainer tc in Types)
1210 if (tc.DefineType () == null)
1214 if (Delegates != null) {
1215 foreach (Delegate d in Delegates)
1216 if (d.DefineType () == null)
1223 TypeContainer InTransit;
1225 protected bool CheckRecursiveDefinition (TypeContainer tc)
1227 if (InTransit != null) {
1228 Report.SymbolRelatedToPreviousError (this);
1229 if (this is Interface)
1231 529, tc.Location, "Inherited interface `{0}' causes a " +
1232 "cycle in the interface hierarchy of `{1}'",
1233 GetSignatureForError (), tc.GetSignatureForError ());
1236 146, tc.Location, "Circular base class dependency " +
1237 "involving `{0}' and `{1}'",
1238 tc.GetSignatureForError (), GetSignatureForError ());
1244 if (base_type != null && base_type.Type != null) {
1245 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1246 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1247 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1251 if (iface_exprs != null) {
1252 foreach (TypeExpr iface in iface_exprs) {
1253 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1254 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1255 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1260 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1268 /// Populates our TypeBuilder with fields and methods
1270 public override bool Define ()
1272 if (members_defined)
1273 return members_defined_ok;
1275 members_defined_ok = DoDefineMembers ();
1276 members_defined = true;
1278 return members_defined_ok;
1281 protected virtual bool DoDefineMembers ()
1283 if (iface_exprs != null) {
1284 foreach (TypeExpr iface in iface_exprs) {
1285 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
1286 if ((oa != null) && !IsInObsoleteScope)
1287 AttributeTester.Report_ObsoleteMessage (
1288 oa, iface.GetSignatureForError (), Location);
1290 GenericTypeExpr ct = iface as GenericTypeExpr;
1292 // TODO: passing `this' is wrong, should be base type iface instead
1293 TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
1295 if (!ct.CheckConstraints (this))
1301 if (base_type != null) {
1302 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1303 if (obsolete_attr != null && !IsInObsoleteScope)
1304 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1306 GenericTypeExpr ct = base_type as GenericTypeExpr;
1307 if ((ct != null) && !ct.CheckConstraints (this))
1310 TypeContainer baseContainer = TypeManager.LookupTypeContainer(base_type.Type);
1311 if (baseContainer != null)
1312 baseContainer.Define();
1314 member_cache = new MemberCache (base_type.Type, this);
1315 } else if (Kind == Kind.Interface) {
1316 member_cache = new MemberCache (null, this);
1317 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1318 for (int i = 0; i < ifaces.Length; ++i)
1319 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1321 member_cache = new MemberCache (null, this);
1325 foreach (TypeContainer tc in types)
1326 member_cache.AddNestedType (tc);
1328 if (delegates != null)
1329 foreach (Delegate d in delegates)
1330 member_cache.AddNestedType (d);
1332 if (partial_parts != null) {
1333 foreach (TypeContainer part in partial_parts)
1334 part.member_cache = member_cache;
1338 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1339 if (conflict_symbol == null) {
1340 if ((ModFlags & Modifiers.NEW) != 0)
1341 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1343 if ((ModFlags & Modifiers.NEW) == 0) {
1344 Report.SymbolRelatedToPreviousError (conflict_symbol);
1345 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1346 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1351 DefineContainerMembers (constants);
1352 DefineContainerMembers (fields);
1354 if (Kind == Kind.Struct || Kind == Kind.Class) {
1355 pending = PendingImplementation.GetPendingImplementations (this);
1357 if (requires_delayed_unmanagedtype_check) {
1358 requires_delayed_unmanagedtype_check = false;
1359 foreach (FieldBase f in fields) {
1360 if (f.MemberType != null && f.MemberType.IsPointer)
1361 TypeManager.VerifyUnManaged (f.MemberType, f.Location);
1367 // Constructors are not in the defined_names array
1369 DefineContainerMembers (instance_constructors);
1371 DefineContainerMembers (events);
1372 DefineContainerMembers (ordered_explicit_member_list);
1373 DefineContainerMembers (ordered_member_list);
1375 DefineContainerMembers (operators);
1376 DefineContainerMembers (delegates);
1378 ComputeIndexerName();
1379 CheckEqualsAndGetHashCode();
1381 if (CurrentType != null) {
1382 GenericType = CurrentType;
1386 // FIXME: This hack is needed because member cache does not work
1387 // with generic types, we rely on runtime to inflate dynamic types.
1388 // TODO: This hack requires member cache refactoring to be removed
1390 if (TypeManager.IsGenericType (TypeBuilder))
1391 member_cache = new MemberCache (this);
1396 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1399 mcal.DefineContainerMembers ();
1402 protected virtual void ComputeIndexerName ()
1404 if (indexers == null)
1407 string class_indexer_name = null;
1410 // If there's both an explicit and an implicit interface implementation, the
1411 // explicit one actually implements the interface while the other one is just
1412 // a normal indexer. See bug #37714.
1415 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1416 foreach (Indexer i in indexers) {
1417 if (i.InterfaceType != null) {
1418 if (seen_normal_indexers)
1419 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1423 seen_normal_indexers = true;
1425 if (class_indexer_name == null) {
1426 class_indexer_name = i.ShortName;
1430 if (i.ShortName != class_indexer_name)
1431 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");
1434 if (class_indexer_name != null)
1435 indexer_name = class_indexer_name;
1438 protected virtual void EmitIndexerName ()
1440 if (!seen_normal_indexers)
1443 PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
1444 if (pa.Constructor == null &&
1445 !pa.ResolveConstructor (Location, TypeManager.string_type))
1448 CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { IndexerName });
1449 TypeBuilder.SetCustomAttribute (cb);
1452 protected virtual void CheckEqualsAndGetHashCode ()
1454 if (methods == null)
1457 if (HasEquals && !HasGetHashCode) {
1458 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1462 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1464 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1468 /// This function is based by a delegate to the FindMembers routine
1470 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1476 /// This filter is used by FindMembers, and we just keep
1477 /// a global for the filter to `AlwaysAccept'
1479 static MemberFilter accepting_filter;
1482 static TypeContainer ()
1484 accepting_filter = new MemberFilter (AlwaysAccept);
1487 public MethodInfo[] GetMethods ()
1489 ArrayList members = new ArrayList ();
1493 if (methods != null) {
1494 int len = methods.Count;
1495 for (int i = 0; i < len; i++) {
1496 Method m = (Method) methods [i];
1498 members.Add (m.MethodBuilder);
1502 if (operators != null) {
1503 int len = operators.Count;
1504 for (int i = 0; i < len; i++) {
1505 Operator o = (Operator) operators [i];
1507 members.Add (o.MethodBuilder);
1511 if (properties != null) {
1512 int len = properties.Count;
1513 for (int i = 0; i < len; i++) {
1514 Property p = (Property) properties [i];
1516 if (p.GetBuilder != null)
1517 members.Add (p.GetBuilder);
1518 if (p.SetBuilder != null)
1519 members.Add (p.SetBuilder);
1523 if (indexers != null) {
1524 int len = indexers.Count;
1525 for (int i = 0; i < len; i++) {
1526 Indexer ix = (Indexer) indexers [i];
1528 if (ix.GetBuilder != null)
1529 members.Add (ix.GetBuilder);
1530 if (ix.SetBuilder != null)
1531 members.Add (ix.SetBuilder);
1535 if (events != null) {
1536 int len = events.Count;
1537 for (int i = 0; i < len; i++) {
1538 Event e = (Event) events [i];
1540 if (e.AddBuilder != null)
1541 members.Add (e.AddBuilder);
1542 if (e.RemoveBuilder != null)
1543 members.Add (e.RemoveBuilder);
1547 MethodInfo[] retMethods = new MethodInfo [members.Count];
1548 members.CopyTo (retMethods, 0);
1552 // Indicated whether container has StructLayout attribute set Explicit
1553 public bool HasExplicitLayout {
1554 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1555 set { caching_flags |= Flags.HasExplicitLayout; }
1558 public bool HasStructLayout {
1559 get { return (caching_flags & Flags.HasStructLayout) != 0; }
1560 set { caching_flags |= Flags.HasStructLayout; }
1564 // Return the nested type with name @name. Ensures that the nested type
1565 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1567 public Type FindNestedType (string name)
1569 if (PartialContainer != this)
1570 throw new InternalErrorException ("should not happen");
1572 ArrayList [] lists = { types, delegates };
1574 for (int j = 0; j < lists.Length; ++j) {
1575 ArrayList list = lists [j];
1579 int len = list.Count;
1580 for (int i = 0; i < len; ++i) {
1581 DeclSpace ds = (DeclSpace) list [i];
1582 if (ds.Basename == name) {
1583 return ds.DefineType ();
1591 private void FindMembers_NestedTypes (int modflags,
1592 BindingFlags bf, MemberFilter filter, object criteria,
1593 ref ArrayList members)
1595 ArrayList [] lists = { types, delegates };
1597 for (int j = 0; j < lists.Length; ++j) {
1598 ArrayList list = lists [j];
1602 int len = list.Count;
1603 for (int i = 0; i < len; i++) {
1604 DeclSpace ds = (DeclSpace) list [i];
1606 if ((ds.ModFlags & modflags) == 0)
1609 TypeBuilder tb = ds.TypeBuilder;
1611 if (!(criteria is string) || ds.Basename.Equals (criteria))
1612 tb = ds.DefineType ();
1615 if (tb != null && (filter (tb, criteria) == true)) {
1616 if (members == null)
1617 members = new ArrayList ();
1626 /// This method returns the members of this type just like Type.FindMembers would
1627 /// Only, we need to use this for types which are _being_ defined because MS'
1628 /// implementation can't take care of that.
1631 // FIXME: return an empty static array instead of null, that cleans up
1632 // some code and is consistent with some coding conventions I just found
1636 // Notice that in various cases we check if our field is non-null,
1637 // something that would normally mean that there was a bug elsewhere.
1639 // The problem happens while we are defining p-invoke methods, as those
1640 // will trigger a FindMembers, but this happens before things are defined
1642 // Since the whole process is a no-op, it is fine to check for null here.
1644 // TODO: This approach will be one day completely removed, it's already
1645 // used at few places only
1648 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1649 MemberFilter filter, object criteria)
1651 ArrayList members = null;
1654 if ((bf & BindingFlags.Public) != 0)
1655 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1657 if ((bf & BindingFlags.NonPublic) != 0)
1658 modflags |= Modifiers.PRIVATE;
1660 int static_mask = 0, static_flags = 0;
1661 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1662 case BindingFlags.Static:
1663 static_mask = static_flags = Modifiers.STATIC;
1666 case BindingFlags.Instance:
1667 static_mask = Modifiers.STATIC;
1672 static_mask = static_flags = 0;
1676 Timer.StartTimer (TimerType.TcFindMembers);
1679 filter = accepting_filter;
1681 if ((mt & MemberTypes.Field) != 0) {
1682 if (fields != null) {
1683 int len = fields.Count;
1684 for (int i = 0; i < len; i++) {
1685 FieldBase f = (FieldBase) fields [i];
1687 if ((f.ModFlags & modflags) == 0)
1689 if ((f.ModFlags & static_mask) != static_flags)
1692 FieldBuilder fb = f.FieldBuilder;
1693 if (fb != null && filter (fb, criteria) == true) {
1694 if (members == null)
1695 members = new ArrayList ();
1702 if (constants != null) {
1703 int len = constants.Count;
1704 for (int i = 0; i < len; i++) {
1705 Const con = (Const) constants [i];
1707 if ((con.ModFlags & modflags) == 0)
1709 if ((con.ModFlags & static_mask) != static_flags)
1712 FieldBuilder fb = con.FieldBuilder;
1715 fb = con.FieldBuilder;
1717 if (fb != null && filter (fb, criteria) == true) {
1718 if (members == null)
1719 members = new ArrayList ();
1727 if ((mt & MemberTypes.Method) != 0) {
1728 if (methods != null) {
1729 int len = methods.Count;
1730 for (int i = 0; i < len; i++) {
1731 MethodOrOperator m = (MethodOrOperator) methods [i];
1733 if ((m.ModFlags & modflags) == 0)
1735 if ((m.ModFlags & static_mask) != static_flags)
1738 MethodBuilder mb = m.MethodBuilder;
1740 if (mb != null && filter (mb, criteria) == true) {
1741 if (members == null)
1742 members = new ArrayList ();
1749 if (operators != null) {
1750 int len = operators.Count;
1751 for (int i = 0; i < len; i++) {
1752 Operator o = (Operator) operators [i];
1754 if ((o.ModFlags & modflags) == 0)
1756 if ((o.ModFlags & static_mask) != static_flags)
1759 MethodBuilder ob = o.MethodBuilder;
1760 if (ob != null && filter (ob, criteria) == true) {
1761 if (members == null)
1762 members = new ArrayList ();
1769 if (events != null) {
1770 foreach (Event e in events) {
1771 if ((e.ModFlags & modflags) == 0)
1773 if ((e.ModFlags & static_mask) != static_flags)
1776 MethodBuilder b = e.AddBuilder;
1777 if (b != null && filter (b, criteria)) {
1778 if (members == null)
1779 members = new ArrayList (4);
1784 b = e.RemoveBuilder;
1785 if (b != null && filter (b, criteria)) {
1786 if (members == null)
1787 members = new ArrayList (4);
1794 if (properties != null) {
1795 int len = properties.Count;
1796 for (int i = 0; i < len; i++) {
1797 Property p = (Property) properties [i];
1799 if ((p.ModFlags & modflags) == 0)
1801 if ((p.ModFlags & static_mask) != static_flags)
1807 if (b != null && filter (b, criteria) == true) {
1808 if (members == null)
1809 members = new ArrayList ();
1815 if (b != null && filter (b, criteria) == true) {
1816 if (members == null)
1817 members = new ArrayList ();
1824 if (indexers != null) {
1825 int len = indexers.Count;
1826 for (int i = 0; i < len; i++) {
1827 Indexer ix = (Indexer) indexers [i];
1829 if ((ix.ModFlags & modflags) == 0)
1831 if ((ix.ModFlags & static_mask) != static_flags)
1837 if (b != null && filter (b, criteria) == true) {
1838 if (members == null)
1839 members = new ArrayList ();
1845 if (b != null && filter (b, criteria) == true) {
1846 if (members == null)
1847 members = new ArrayList ();
1855 if ((mt & MemberTypes.Event) != 0) {
1856 if (events != null) {
1857 int len = events.Count;
1858 for (int i = 0; i < len; i++) {
1859 Event e = (Event) events [i];
1861 if ((e.ModFlags & modflags) == 0)
1863 if ((e.ModFlags & static_mask) != static_flags)
1866 MemberInfo eb = e.EventBuilder;
1867 if (eb != null && filter (eb, criteria) == true) {
1868 if (members == null)
1869 members = new ArrayList ();
1871 members.Add (e.EventBuilder);
1877 if ((mt & MemberTypes.Property) != 0){
1878 if (properties != null) {
1879 int len = properties.Count;
1880 for (int i = 0; i < len; i++) {
1881 Property p = (Property) properties [i];
1883 if ((p.ModFlags & modflags) == 0)
1885 if ((p.ModFlags & static_mask) != static_flags)
1888 MemberInfo pb = p.PropertyBuilder;
1889 if (pb != null && filter (pb, criteria) == true) {
1890 if (members == null)
1891 members = new ArrayList ();
1893 members.Add (p.PropertyBuilder);
1898 if (indexers != null) {
1899 int len = indexers.Count;
1900 for (int i = 0; i < len; i++) {
1901 Indexer ix = (Indexer) indexers [i];
1903 if ((ix.ModFlags & modflags) == 0)
1905 if ((ix.ModFlags & static_mask) != static_flags)
1908 MemberInfo ib = ix.PropertyBuilder;
1909 if (ib != null && filter (ib, criteria) == true) {
1910 if (members == null)
1911 members = new ArrayList ();
1913 members.Add (ix.PropertyBuilder);
1919 if ((mt & MemberTypes.NestedType) != 0)
1920 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1922 if ((mt & MemberTypes.Constructor) != 0){
1923 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1924 int len = instance_constructors.Count;
1925 for (int i = 0; i < len; i++) {
1926 Constructor c = (Constructor) instance_constructors [i];
1928 ConstructorBuilder cb = c.ConstructorBuilder;
1929 if (cb != null && filter (cb, criteria) == true) {
1930 if (members == null)
1931 members = new ArrayList ();
1938 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1939 ConstructorBuilder cb =
1940 default_static_constructor.ConstructorBuilder;
1942 if (cb != null && filter (cb, criteria) == true) {
1943 if (members == null)
1944 members = new ArrayList ();
1952 // Lookup members in base if requested.
1954 if ((bf & BindingFlags.DeclaredOnly) == 0) {
1955 if (TypeBuilder.BaseType != null) {
1956 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
1957 if (list.Count > 0) {
1958 if (members == null)
1959 members = new ArrayList ();
1961 members.AddRange (list);
1966 Timer.StopTimer (TimerType.TcFindMembers);
1968 if (members == null)
1969 return MemberList.Empty;
1971 return new MemberList (members);
1974 public override MemberCache MemberCache {
1976 return member_cache;
1980 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1981 MemberFilter filter, object criteria)
1983 DeclSpace ds = TypeManager.LookupDeclSpace (t);
1986 return ds.FindMembers (mt, bf, filter, criteria);
1988 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1992 /// Emits the values for the constants
1994 public void EmitConstants ()
1996 if (constants != null)
1997 foreach (Const con in constants)
2002 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2007 foreach (MemberCore mc in al) {
2008 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2011 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2012 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2017 public virtual void VerifyMembers ()
2020 // Check for internal or private fields that were never assigned
2022 if (Report.WarningLevel >= 3) {
2023 CheckMemberUsage (properties, "property");
2024 CheckMemberUsage (methods, "method");
2025 CheckMemberUsage (constants, "constant");
2027 if (fields != null){
2028 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2029 foreach (FieldBase f in fields) {
2030 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2031 if (is_type_exposed)
2034 f.SetMemberIsUsed ();
2038 if ((f.caching_flags & Flags.IsAssigned) == 0)
2039 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2042 const int error_code = 414;
2044 const int error_code = 169;
2046 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2047 f.GetSignatureForError ());
2053 // Only report 649 on level 4
2055 if (Report.WarningLevel < 4)
2058 if ((f.caching_flags & Flags.IsAssigned) != 0)
2062 // Don't be pendatic over serializable attributes
2064 if (f.OptAttributes != null || PartialContainer.HasStructLayout)
2067 Constant c = New.Constantify (f.MemberType);
2068 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2069 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2075 // TODO: move to ClassOrStruct
2076 void EmitConstructors ()
2078 if (instance_constructors == null)
2081 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2082 bool has_compliant_args = false;
2084 foreach (Constructor c in instance_constructors) {
2088 catch (Exception e) {
2089 throw new InternalErrorException (c, e);
2092 if (has_compliant_args)
2095 has_compliant_args = c.HasCompliantArgs;
2097 if (!has_compliant_args)
2098 Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2100 foreach (Constructor c in instance_constructors) {
2104 catch (Exception e) {
2105 throw new InternalErrorException (c, e);
2112 /// Emits the code, this step is performed after all
2113 /// the types, enumerations, constructors
2115 public virtual void EmitType ()
2117 if (OptAttributes != null)
2118 OptAttributes.Emit ();
2122 EmitConstructors ();
2124 // Can not continue if constants are broken
2126 if (Report.Errors > 0)
2129 if (default_static_constructor != null)
2130 default_static_constructor.Emit ();
2132 if (operators != null)
2133 foreach (Operator o in operators)
2136 if (properties != null)
2137 foreach (Property p in properties)
2140 if (indexers != null) {
2141 foreach (Indexer indx in indexers)
2146 if (events != null){
2147 foreach (Event e in Events)
2151 if (methods != null) {
2152 for (int i = 0; i < methods.Count; ++i)
2153 ((MethodOrOperator) methods [i]).Emit ();
2157 foreach (FieldBase f in fields)
2160 if (delegates != null) {
2161 foreach (Delegate d in Delegates) {
2166 if (pending != null)
2167 pending.VerifyPendingMethods ();
2169 if (Report.Errors > 0)
2172 if (compiler_generated != null) {
2173 for (int i = 0; i < compiler_generated.Count; ++i)
2174 ((CompilerGeneratedClass) compiler_generated [i]).EmitType ();
2178 public override void CloseType ()
2180 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2184 caching_flags |= Flags.CloseTypeCreated;
2185 TypeBuilder.CreateType ();
2186 } catch (TypeLoadException){
2188 // This is fine, the code still created the type
2190 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2191 // Console.WriteLine (e.Message);
2192 } catch (Exception e) {
2193 throw new InternalErrorException (this, e);
2197 foreach (TypeContainer tc in Types)
2198 if (tc.Kind == Kind.Struct)
2201 foreach (TypeContainer tc in Types)
2202 if (tc.Kind != Kind.Struct)
2206 if (Delegates != null)
2207 foreach (Delegate d in Delegates)
2210 if (compiler_generated != null)
2211 foreach (CompilerGeneratedClass c in compiler_generated)
2214 PartialContainer = null;
2219 initialized_fields = null;
2220 initialized_static_fields = null;
2222 ordered_explicit_member_list = null;
2223 ordered_member_list = null;
2228 compiler_generated = null;
2229 default_constructor = null;
2230 default_static_constructor = null;
2232 OptAttributes = null;
2235 member_cache = null;
2239 // Performs the validation on a Method's modifiers (properties have
2240 // the same properties).
2242 public bool MethodModifiersValid (MemberCore mc)
2244 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2245 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2246 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2248 int flags = mc.ModFlags;
2251 // At most one of static, virtual or override
2253 if ((flags & Modifiers.STATIC) != 0){
2254 if ((flags & vao) != 0){
2255 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2256 mc.GetSignatureForError ());
2261 if (Kind == Kind.Struct){
2262 if ((flags & va) != 0){
2263 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2268 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2269 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2270 mc.GetSignatureForError ());
2275 // If the declaration includes the abstract modifier, then the
2276 // declaration does not include static, virtual or extern
2278 if ((flags & Modifiers.ABSTRACT) != 0){
2279 if ((flags & Modifiers.EXTERN) != 0){
2281 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2285 if ((flags & Modifiers.SEALED) != 0) {
2286 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2290 if ((flags & Modifiers.VIRTUAL) != 0){
2291 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2295 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2296 Report.SymbolRelatedToPreviousError (this);
2297 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2298 mc.GetSignatureForError (), GetSignatureForError ());
2303 if ((flags & Modifiers.PRIVATE) != 0){
2304 if ((flags & vao) != 0){
2305 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2310 if ((flags & Modifiers.SEALED) != 0){
2311 if ((flags & Modifiers.OVERRIDE) == 0){
2312 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2320 public Constructor DefaultStaticConstructor {
2321 get { return default_static_constructor; }
2324 protected override bool VerifyClsCompliance ()
2326 if (!base.VerifyClsCompliance ())
2331 Type base_type = TypeBuilder.BaseType;
2332 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2333 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2340 /// Checks whether container name is CLS Compliant
2342 void VerifyClsName ()
2344 Hashtable base_members = base_cache == null ?
2346 base_cache.GetPublicMembers ();
2347 Hashtable this_members = new Hashtable ();
2349 foreach (DictionaryEntry entry in defined_names) {
2350 MemberCore mc = (MemberCore)entry.Value;
2351 if (!mc.IsClsComplianceRequired ())
2354 string name = (string) entry.Key;
2355 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2357 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2358 object found = base_members [lcase];
2359 if (found == null) {
2360 found = this_members [lcase];
2361 if (found == null) {
2362 this_members.Add (lcase, mc);
2367 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2370 if (found is MemberInfo) {
2371 if (basename == ((MemberInfo) found).Name)
2373 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2375 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2378 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2384 /// Performs checks for an explicit interface implementation. First it
2385 /// checks whether the `interface_type' is a base inteface implementation.
2386 /// Then it checks whether `name' exists in the interface type.
2388 public bool VerifyImplements (InterfaceMemberBase mb)
2390 if (ifaces != null) {
2391 foreach (Type t in ifaces){
2392 if (TypeManager.IsEqual (t, mb.InterfaceType))
2397 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2398 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2399 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2403 public override Type LookupAnyGeneric (string typeName)
2405 if (types != null) {
2406 foreach (TypeContainer tc in types) {
2410 int pos = tc.Basename.LastIndexOf ('`');
2411 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2412 return tc.TypeBuilder;
2416 return base.LookupAnyGeneric (typeName);
2419 public void Mark_HasEquals ()
2421 cached_method |= CachedMethods.Equals;
2424 public void Mark_HasGetHashCode ()
2426 cached_method |= CachedMethods.GetHashCode;
2430 /// Method container contains Equals method
2432 public bool HasEquals {
2434 return (cached_method & CachedMethods.Equals) != 0;
2439 /// Method container contains GetHashCode method
2441 public bool HasGetHashCode {
2443 return (cached_method & CachedMethods.GetHashCode) != 0;
2447 public bool HasStaticFieldInitializer {
2449 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2453 cached_method |= CachedMethods.HasStaticFieldInitializer;
2455 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2463 string IMemberContainer.Name {
2469 Type IMemberContainer.Type {
2475 bool IMemberContainer.IsInterface {
2477 return Kind == Kind.Interface;
2481 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2483 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2485 if (GenericType != null)
2486 return TypeManager.FindMembers (GenericType, mt, new_bf,
2489 return FindMembers (mt, new_bf, null, null);
2493 // Generates xml doc comments (if any), and if required,
2494 // handle warning report.
2496 internal override void GenerateDocComment (DeclSpace ds)
2498 DocUtil.GenerateTypeDocComment (this, ds);
2501 public override string DocCommentHeader {
2502 get { return "T:"; }
2505 public MemberCache BaseCache {
2507 if (base_cache != null)
2509 if (TypeBuilder.BaseType != null)
2510 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2511 if (TypeBuilder.IsInterface)
2512 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2518 public abstract class ClassOrStruct : TypeContainer {
2519 ListDictionary declarative_security;
2521 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2522 MemberName name, Attributes attrs, Kind kind)
2523 : base (ns, parent, name, attrs, kind)
2527 protected override bool AddToContainer (MemberCore symbol, string name)
2529 if (name == MemberName.Name) {
2530 if (symbol is TypeParameter) {
2531 Report.Error (694, symbol.Location,
2532 "Type parameter `{0}' has same name as containing type, or method",
2533 symbol.GetSignatureForError ());
2537 InterfaceMemberBase imb = symbol as InterfaceMemberBase;
2538 if (imb == null || !imb.IsExplicitImpl) {
2539 Report.SymbolRelatedToPreviousError (this);
2540 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2541 symbol.GetSignatureForError ());
2546 return base.AddToContainer (symbol, name);
2549 public override void VerifyMembers ()
2551 base.VerifyMembers ();
2553 if ((events != null) && Report.WarningLevel >= 3) {
2554 foreach (Event e in events){
2555 // Note: The event can be assigned from same class only, so we can report
2556 // this warning for all accessibility modes
2557 if ((e.caching_flags & Flags.IsUsed) == 0)
2558 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2563 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2565 if (a.IsValidSecurityAttribute ()) {
2566 if (declarative_security == null)
2567 declarative_security = new ListDictionary ();
2569 a.ExtractSecurityPermissionSet (declarative_security);
2573 if (a.Type == pa.StructLayout) {
2574 PartialContainer.HasStructLayout = true;
2576 if (a.GetLayoutKindValue () == LayoutKind.Explicit)
2577 PartialContainer.HasExplicitLayout = true;
2580 base.ApplyAttributeBuilder (a, cb, pa);
2584 /// Defines the default constructors
2586 protected void DefineDefaultConstructor (bool is_static)
2588 // The default instance constructor is public
2589 // If the class is abstract, the default constructor is protected
2590 // The default static constructor is private
2594 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2596 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2599 Constructor c = new Constructor (this, MemberName.Name, mods,
2600 null, ParametersCompiled.EmptyReadOnlyParameters,
2601 new GeneratedBaseInitializer (Location),
2605 c.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
2608 public override bool Define ()
2610 CheckProtectedModifier ();
2614 if (default_static_constructor != null)
2615 default_static_constructor.Define ();
2620 public override void Emit ()
2622 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer) {
2623 DefineDefaultConstructor (true);
2624 default_static_constructor.Define ();
2629 if (declarative_security != null) {
2630 foreach (DictionaryEntry de in declarative_security) {
2631 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2636 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2638 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
2641 protected override TypeAttributes TypeAttr {
2643 if (default_static_constructor == null)
2644 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2646 return base.TypeAttr;
2652 // TODO: should be sealed
2653 public class Class : ClassOrStruct {
2654 const int AllowedModifiers =
2657 Modifiers.PROTECTED |
2658 Modifiers.INTERNAL |
2660 Modifiers.ABSTRACT |
2665 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2667 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2669 : base (ns, parent, name, attrs, Kind.Class)
2671 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2672 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2674 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2675 Report.FeatureIsNotAvailable (Location, "static classes");
2679 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2681 if (part.Name == "System.Object")
2682 Report.Error (537, part.Location,
2683 "The class System.Object cannot have a base class or implement an interface.");
2684 base.AddBasesForPart (part, bases);
2687 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2689 if (a.Type == pa.AttributeUsage) {
2690 if (!TypeManager.IsAttributeType (BaseType) &&
2691 TypeBuilder.FullName != "System.Attribute") {
2692 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2696 if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) {
2697 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2701 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2702 a.Error_MissingGuidAttribute ();
2706 if (a.Type == pa.Extension) {
2707 a.Error_MisusedExtensionAttribute ();
2711 if (AttributeTester.IsAttributeExcluded (a.Type, Location))
2714 base.ApplyAttributeBuilder (a, cb, pa);
2717 public override AttributeTargets AttributeTargets {
2719 return AttributeTargets.Class;
2723 protected override void DefineContainerMembers (MemberCoreArrayList list)
2729 base.DefineContainerMembers (list);
2733 foreach (MemberCore m in list) {
2734 if (m is Operator) {
2735 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2739 if (m is Destructor) {
2740 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2745 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2749 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2752 if (m is Constructor) {
2753 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2757 Method method = m as Method;
2758 if (method != null && method.Parameters.HasExtensionMethodType) {
2759 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2763 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2766 base.DefineContainerMembers (list);
2769 public override bool Define ()
2771 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2772 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2775 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2776 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2779 return base.Define ();
2782 protected override bool DoDefineMembers ()
2784 if (InstanceConstructors == null && !IsStatic)
2785 DefineDefaultConstructor (false);
2787 return base.DoDefineMembers ();
2790 public override void Emit ()
2794 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2795 PredefinedAttributes.Get.Extension.EmitAttribute (TypeBuilder);
2798 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2800 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2802 if (base_class == null) {
2803 if (RootContext.StdLib)
2804 base_class = TypeManager.system_object_expr;
2805 else if (Name != "System.Object")
2806 base_class = TypeManager.system_object_expr;
2808 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2810 689, base_class.Location,
2811 "Cannot derive from `{0}' because it is a type parameter",
2812 base_class.GetSignatureForError ());
2816 if (IsGeneric && TypeManager.IsAttributeType (base_class.Type)) {
2817 Report.Error (698, base_class.Location,
2818 "A generic type cannot derive from `{0}' because it is an attribute class",
2819 base_class.GetSignatureForError ());
2822 if (base_class.IsSealed){
2823 Report.SymbolRelatedToPreviousError (base_class.Type);
2824 if (base_class.Type.IsAbstract) {
2825 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2826 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2828 Report.Error (509, Location, "`{0}': cannot derive from sealed type `{1}'",
2829 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2834 if (!base_class.CanInheritFrom ()){
2835 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2836 GetSignatureForError (), base_class.GetSignatureForError ());
2840 if (!IsAccessibleAs (base_class.Type)) {
2841 Report.SymbolRelatedToPreviousError (base_class.Type);
2842 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2843 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2847 if (PartialContainer.IsStaticClass) {
2848 if (base_class.Type != TypeManager.object_type) {
2849 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2850 GetSignatureForError (), base_class.GetSignatureForError ());
2854 if (ifaces != null) {
2855 foreach (TypeExpr t in ifaces)
2856 Report.SymbolRelatedToPreviousError (t.Type);
2857 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2864 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2865 /// Valid only for attribute classes.
2866 public bool IsExcluded ()
2868 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2869 return (caching_flags & Flags.Excluded) != 0;
2871 caching_flags &= ~Flags.Excluded_Undetected;
2873 if (OptAttributes == null)
2876 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
2880 foreach (Attribute a in attrs) {
2881 string condition = a.GetConditionalAttributeValue ();
2882 if (Location.CompilationUnit.IsConditionalDefined (condition))
2886 caching_flags |= Flags.Excluded;
2892 return (ModFlags & Modifiers.STATIC) != 0;
2897 // FIXME: How do we deal with the user specifying a different
2900 protected override TypeAttributes TypeAttr {
2902 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2904 ta |= StaticClassAttribute;
2910 public sealed class Struct : ClassOrStruct {
2912 bool is_unmanaged, has_unmanaged_check_done;
2915 // Modifiers allowed in a struct declaration
2917 const int AllowedModifiers =
2920 Modifiers.PROTECTED |
2921 Modifiers.INTERNAL |
2925 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2926 int mod, Attributes attrs)
2927 : base (ns, parent, name, attrs, Kind.Struct)
2931 if (parent.Parent == null)
2932 accmods = Modifiers.INTERNAL;
2934 accmods = Modifiers.PRIVATE;
2936 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2938 this.ModFlags |= Modifiers.SEALED;
2941 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2943 base.ApplyAttributeBuilder (a, cb, pa);
2946 // When struct constains fixed fixed and struct layout has explicitly
2947 // set CharSet, its value has to be propagated to compiler generated
2948 // fixed field types
2950 if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
2951 for (int i = 0; i < Fields.Count; ++i) {
2952 FixedField ff = Fields [i] as FixedField;
2954 ff.SetCharSet (TypeBuilder.Attributes);
2959 public override AttributeTargets AttributeTargets {
2961 return AttributeTargets.Struct;
2965 public override bool IsUnmanagedType ()
2970 if (requires_delayed_unmanagedtype_check)
2973 if (has_unmanaged_check_done)
2974 return is_unmanaged;
2976 has_unmanaged_check_done = true;
2978 foreach (FieldBase f in fields) {
2979 if ((f.ModFlags & Modifiers.STATIC) != 0)
2982 // It can happen when recursive unmanaged types are defined
2983 // struct S { S* s; }
2984 Type mt = f.MemberType;
2986 has_unmanaged_check_done = false;
2987 requires_delayed_unmanagedtype_check = true;
2991 // TODO: Remove when pointer types are under mcs control
2992 while (mt.IsPointer)
2993 mt = TypeManager.GetElementType (mt);
2994 if (TypeManager.IsEqual (mt, TypeBuilder))
2997 if (TypeManager.IsUnmanagedType (mt))
3003 is_unmanaged = true;
3007 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
3009 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
3011 // If we are compiling our runtime,
3012 // and we are defining ValueType, then our
3013 // base is `System.Object'.
3015 if (base_class == null) {
3016 if (!RootContext.StdLib && Name == "System.ValueType")
3017 base_class = TypeManager.system_object_expr;
3019 base_class = TypeManager.system_valuetype_expr;
3026 // FIXME: Allow the user to specify a different set of attributes
3027 // in some cases (Sealed for example is mandatory for a class,
3028 // but what SequentialLayout can be changed
3030 protected override TypeAttributes TypeAttr {
3032 const TypeAttributes DefaultTypeAttributes =
3033 TypeAttributes.SequentialLayout |
3034 TypeAttributes.Sealed;
3036 return base.TypeAttr | DefaultTypeAttributes;
3040 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3042 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3043 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3044 field.GetSignatureForError ());
3047 base.RegisterFieldForInitialization (field, expression);
3055 public sealed class Interface : TypeContainer, IMemberContainer {
3058 /// Modifiers allowed in a class declaration
3060 public const int AllowedModifiers =
3063 Modifiers.PROTECTED |
3064 Modifiers.INTERNAL |
3068 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3070 : base (ns, parent, name, attrs, Kind.Interface)
3074 if (parent.Parent == null)
3075 accmods = Modifiers.INTERNAL;
3077 accmods = Modifiers.PRIVATE;
3079 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3082 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3084 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
3085 a.Error_MissingGuidAttribute ();
3089 base.ApplyAttributeBuilder (a, cb, pa);
3093 public override AttributeTargets AttributeTargets {
3095 return AttributeTargets.Interface;
3099 protected override TypeAttributes TypeAttr {
3101 const TypeAttributes DefaultTypeAttributes =
3102 TypeAttributes.AutoLayout |
3103 TypeAttributes.Abstract |
3104 TypeAttributes.Interface;
3106 return base.TypeAttr | DefaultTypeAttributes;
3110 protected override bool VerifyClsCompliance ()
3112 if (!base.VerifyClsCompliance ())
3115 if (ifaces != null) {
3116 foreach (Type t in ifaces) {
3117 if (AttributeTester.IsClsCompliant (t))
3120 Report.SymbolRelatedToPreviousError (t);
3121 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3122 GetSignatureForError (), TypeManager.CSharpName (t));
3130 // It is used as a base class for all property based members
3131 // This includes properties, indexers, and events
3132 public abstract class PropertyBasedMember : InterfaceMemberBase
3134 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3135 FullNamedExpression type, int mod, int allowed_mod,
3136 MemberName name, Attributes attrs)
3137 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3141 protected override bool VerifyClsCompliance ()
3143 if (!base.VerifyClsCompliance ())
3146 if (!AttributeTester.IsClsCompliant (MemberType)) {
3147 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
3148 GetSignatureForError ());
3156 public abstract class MethodCore : InterfaceMemberBase
3158 public readonly ParametersCompiled Parameters;
3159 protected ToplevelBlock block;
3161 public MethodCore (DeclSpace parent, GenericMethod generic,
3162 FullNamedExpression type, int mod, int allowed_mod,
3163 MemberName name, Attributes attrs, ParametersCompiled parameters)
3164 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3166 Parameters = parameters;
3170 // Returns the System.Type array for the parameters of this method
3172 public Type [] ParameterTypes {
3174 return Parameters.Types;
3178 public ParametersCompiled ParameterInfo {
3184 public ToplevelBlock Block {
3194 public CallingConventions CallingConventions {
3196 CallingConventions cc = Parameters.CallingConvention;
3198 if ((ModFlags & Modifiers.STATIC) == 0)
3199 cc |= CallingConventions.HasThis;
3201 // FIXME: How is `ExplicitThis' used in C#?
3207 protected override bool CheckBase ()
3209 // Check whether arguments were correct.
3210 if (!DefineParameters (Parameters))
3213 return base.CheckBase ();
3217 // Returns a string that represents the signature for this
3218 // member which should be used in XML documentation.
3220 public override string GetDocCommentName (DeclSpace ds)
3222 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3226 // Raised (and passed an XmlElement that contains the comment)
3227 // when GenerateDocComment is writing documentation expectedly.
3229 // FIXME: with a few effort, it could be done with XmlReader,
3230 // that means removal of DOM use.
3232 internal override void OnGenerateDocComment (XmlElement el)
3234 DocUtil.OnMethodGenerateDocComment (this, el);
3238 // Represents header string for documentation comment.
3240 public override string DocCommentHeader
3242 get { return "M:"; }
3245 public override bool EnableOverloadChecks (MemberCore overload)
3247 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3248 caching_flags |= Flags.MethodOverloadsExist;
3252 return base.EnableOverloadChecks (overload);
3255 protected override bool VerifyClsCompliance ()
3257 if (!base.VerifyClsCompliance ())
3260 if (Parameters.HasArglist) {
3261 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
3264 if (!AttributeTester.IsClsCompliant (MemberType)) {
3265 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
3266 GetSignatureForError ());
3269 Parameters.VerifyClsCompliance ();
3275 public abstract class InterfaceMemberBase : MemberBase {
3277 // Whether this is an interface member.
3279 public bool IsInterface;
3282 // If true, this is an explicit interface implementation
3284 public bool IsExplicitImpl;
3286 protected bool is_external_implementation;
3289 // The interface type we are explicitly implementing
3291 public Type InterfaceType;
3294 // The method we're overriding if this is an override method.
3296 protected MethodInfo base_method;
3298 readonly int explicit_mod_flags;
3299 public MethodAttributes flags;
3301 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3302 FullNamedExpression type, int mod, int allowed_mod,
3303 MemberName name, Attributes attrs)
3304 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3307 IsInterface = parent.PartialContainer.Kind == Kind.Interface;
3308 IsExplicitImpl = (MemberName.Left != null);
3309 explicit_mod_flags = mod;
3312 protected override bool CheckBase ()
3314 if (!base.CheckBase ())
3317 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
3318 CheckForDuplications ();
3323 // Is null for System.Object while compiling corlib and base interfaces
3324 if (Parent.PartialContainer.BaseCache == null) {
3325 if ((ModFlags & Modifiers.NEW) != 0) {
3326 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3331 Type base_ret_type = null;
3332 base_method = FindOutBaseMethod (ref base_ret_type);
3334 // method is override
3335 if (base_method != null) {
3336 if (!CheckMethodAgainstBase (base_ret_type))
3339 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3340 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3342 if (OptAttributes == null || !OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3343 Report.SymbolRelatedToPreviousError (base_method);
3344 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3345 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3348 if (OptAttributes != null && OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3349 Report.SymbolRelatedToPreviousError (base_method);
3350 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3351 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3358 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3359 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3360 if (conflict_symbol != null) {
3361 Report.SymbolRelatedToPreviousError (conflict_symbol);
3363 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3364 else if (this is PropertyBase)
3365 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3367 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3369 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3370 GetSignatureForError (), SimpleName.GetMemberType (this));
3375 if (conflict_symbol == null) {
3376 if ((ModFlags & Modifiers.NEW) != 0) {
3377 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3382 if ((ModFlags & Modifiers.NEW) == 0) {
3383 if (this is MethodOrOperator && conflict_symbol.MemberType == MemberTypes.Method)
3386 Report.SymbolRelatedToPreviousError (conflict_symbol);
3387 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3388 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3394 protected virtual bool CheckForDuplications ()
3396 return Parent.MemberCache.CheckExistingMembersOverloads (
3397 this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters);
3401 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3402 // that have been defined.
3404 // `name' is the user visible name for reporting errors (this is used to
3405 // provide the right name regarding method names and properties)
3407 bool CheckMethodAgainstBase (Type base_method_type)
3411 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3412 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3413 Report.SymbolRelatedToPreviousError (base_method);
3414 Report.Error (506, Location,
3415 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3416 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3420 // Now we check that the overriden method is not final
3422 if (base_method.IsFinal) {
3423 Report.SymbolRelatedToPreviousError (base_method);
3424 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3425 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3429 // Check that the permissions are not being changed
3431 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3432 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3434 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3435 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3439 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3440 Report.SymbolRelatedToPreviousError (base_method);
3441 if (this is PropertyBasedMember) {
3442 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3443 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3446 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3447 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3453 if ((ModFlags & Modifiers.NEW) == 0) {
3454 if ((ModFlags & Modifiers.OVERRIDE) == 0) {
3455 ModFlags |= Modifiers.NEW;
3456 Report.SymbolRelatedToPreviousError (base_method);
3457 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3458 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",
3459 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3461 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3462 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3466 if (base_method.IsAbstract && !IsInterface) {
3467 Report.SymbolRelatedToPreviousError (base_method);
3468 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3469 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3477 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3479 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3481 // when overriding protected internal, the method can be declared
3482 // protected internal only within the same assembly or assembly
3483 // which has InternalsVisibleTo
3485 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3486 return TypeManager.IsThisOrFriendAssembly (base_method.DeclaringType.Assembly);
3487 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3489 // if it's not "protected internal", it must be "protected"
3493 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3495 // protected within the same assembly - an error
3498 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3499 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3501 // protected ok, but other attributes differ - report an error
3507 return (thisp == base_classp);
3511 public override bool Define ()
3514 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3515 Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3517 flags = MethodAttributes.Public |
3518 MethodAttributes.Abstract |
3519 MethodAttributes.HideBySig |
3520 MethodAttributes.NewSlot |
3521 MethodAttributes.Virtual;
3523 Parent.PartialContainer.MethodModifiersValid (this);
3525 flags = Modifiers.MethodAttr (ModFlags);
3528 if (IsExplicitImpl) {
3529 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3530 if (iface_texpr == null)
3533 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3534 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3535 GetSignatureForError ());
3538 InterfaceType = iface_texpr.Type;
3540 if (!InterfaceType.IsInterface) {
3541 Report.SymbolRelatedToPreviousError (InterfaceType);
3542 Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3543 TypeManager.CSharpName (InterfaceType));
3545 Parent.PartialContainer.VerifyImplements (this);
3548 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3551 return base.Define ();
3554 protected bool DefineParameters (ParametersCompiled parameters)
3556 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3558 if (!parameters.Resolve (rc))
3562 for (int i = 0; i < parameters.Count; ++i) {
3563 Parameter p = parameters [i];
3565 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3566 p.Warning_UselessOptionalParameter ();
3568 if (p.CheckAccessibility (this))
3571 Type t = parameters.Types [i];
3572 Report.SymbolRelatedToPreviousError (t);
3573 if (this is Indexer)
3574 Report.Error (55, Location,
3575 "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3576 TypeManager.CSharpName (t), GetSignatureForError ());
3577 else if (this is Operator)
3578 Report.Error (57, Location,
3579 "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3580 TypeManager.CSharpName (t), GetSignatureForError ());
3582 Report.Error (51, Location,
3583 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3584 TypeManager.CSharpName (t), GetSignatureForError ());
3590 public override void Emit()
3592 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3593 // We are more strict than csc and report this as an error because SRE does not allow emit that
3594 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3595 if (this is Constructor) {
3596 Report.Error (824, Location,
3597 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3599 Report.Error (626, Location,
3600 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3601 GetSignatureForError ());
3608 public override bool EnableOverloadChecks (MemberCore overload)
3611 // Two members can differ in their explicit interface
3612 // type parameter only
3614 InterfaceMemberBase imb = overload as InterfaceMemberBase;
3615 if (imb != null && imb.IsExplicitImpl) {
3616 if (IsExplicitImpl) {
3617 caching_flags |= Flags.MethodOverloadsExist;
3622 return IsExplicitImpl;
3625 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3627 Report.SymbolRelatedToPreviousError (base_method);
3628 string base_name = TypeManager.GetFullNameSignature (base_method);
3629 string this_name = GetSignatureForError ();
3630 if (suffix != null) {
3631 base_name += suffix;
3632 this_name += suffix;
3635 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3636 this_name, Modifiers.GetDescription (ma), base_name);
3639 protected static string Error722 {
3641 return "`{0}': static types cannot be used as return types";
3646 /// Gets base method and its return type
3648 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3651 // The "short" name of this property / indexer / event. This is the
3652 // name without the explicit interface.
3654 public string ShortName
3656 get { return MemberName.Name; }
3657 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3661 // Returns full metadata method name
3663 public string GetFullName (MemberName name)
3665 if (!IsExplicitImpl)
3669 // When dealing with explicit members a full interface type
3670 // name is added to member name to avoid possible name conflicts
3672 // We use CSharpName which gets us full name with benefit of
3673 // replacing predefined names which saves some space and name
3676 return TypeManager.CSharpName (InterfaceType) + "." + name.Name;
3679 protected override bool VerifyClsCompliance ()
3681 if (!base.VerifyClsCompliance ()) {
3682 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3683 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3686 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3687 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3692 if (GenericMethod != null)
3693 GenericMethod.VerifyClsCompliance ();
3698 public override bool IsUsed
3700 get { return IsExplicitImpl || base.IsUsed; }
3705 public abstract class MethodOrOperator : MethodCore, IMethodData
3707 public MethodBuilder MethodBuilder;
3708 ReturnParameter return_attributes;
3709 ListDictionary declarative_security;
3710 protected MethodData MethodData;
3712 static string[] attribute_targets = new string [] { "method", "return" };
3714 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3715 int allowed_mod, MemberName name,
3716 Attributes attrs, ParametersCompiled parameters)
3717 : base (parent, generic, type, mod, allowed_mod, name,
3722 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3724 if (a.Target == AttributeTargets.ReturnValue) {
3725 if (return_attributes == null)
3726 return_attributes = new ReturnParameter (MethodBuilder, Location);
3728 return_attributes.ApplyAttributeBuilder (a, cb, pa);
3732 if (a.IsInternalMethodImplAttribute) {
3733 is_external_implementation = true;
3736 if (a.Type == pa.DllImport) {
3737 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3738 if ((ModFlags & extern_static) != extern_static) {
3739 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3741 is_external_implementation = true;
3744 if (a.IsValidSecurityAttribute ()) {
3745 if (declarative_security == null)
3746 declarative_security = new ListDictionary ();
3747 a.ExtractSecurityPermissionSet (declarative_security);
3751 if (MethodBuilder != null)
3752 MethodBuilder.SetCustomAttribute (cb);
3755 public override AttributeTargets AttributeTargets {
3757 return AttributeTargets.Method;
3761 protected override bool CheckForDuplications ()
3763 string name = GetFullName (MemberName);
3764 if (MemberName.IsGeneric)
3765 name = MemberName.MakeName (name, MemberName.TypeArguments);
3767 return Parent.MemberCache.CheckExistingMembersOverloads (this, name, Parameters);
3770 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3772 return new EmitContext (
3773 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3776 protected override bool ResolveMemberType ()
3779 if (GenericMethod != null) {
3780 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3781 if (!GenericMethod.Define (this))
3786 return base.ResolveMemberType ();
3789 public override bool Define ()
3791 if (!base.Define ())
3797 if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
3799 // Current method is turned into automatically generated
3800 // wrapper which creates an instance of iterator
3802 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
3803 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
3806 if (IsPartialDefinition) {
3807 caching_flags &= ~Flags.Excluded_Undetected;
3808 caching_flags |= Flags.Excluded;
3809 // Add to member cache only when a partial method implementation is not there
3810 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3811 MethodBase mb = new PartialMethodDefinitionInfo (this);
3812 Parent.MemberCache.AddMember (mb, this);
3813 TypeManager.AddMethod (mb, this);
3819 MethodData = new MethodData (
3820 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3822 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
3825 MethodBuilder = MethodData.MethodBuilder;
3827 if (TypeManager.IsGenericMethod (MethodBuilder))
3828 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3830 Parent.MemberCache.AddMember (MethodBuilder, this);
3835 protected override void DoMemberTypeIndependentChecks ()
3837 base.DoMemberTypeIndependentChecks ();
3839 CheckAbstractAndExtern (block != null);
3841 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3842 for (int i = 0; i < Parameters.Count; ++i) {
3843 IParameterData p = Parameters.FixedParameters [i];
3844 if (p.ModFlags == Parameter.Modifier.OUT) {
3845 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3846 GetSignatureForError ());
3849 if (p.HasDefaultValue && IsPartialImplementation)
3850 ((Parameter) p).Warning_UselessOptionalParameter ();
3855 protected override void DoMemberTypeDependentChecks ()
3857 base.DoMemberTypeDependentChecks ();
3859 if (!TypeManager.IsGenericParameter (MemberType)) {
3860 if (MemberType.IsAbstract && MemberType.IsSealed) {
3861 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3866 public override void Emit ()
3868 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
3869 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
3870 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3871 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
3873 if (TypeManager.IsDynamicType (ReturnType)) {
3874 return_attributes = new ReturnParameter (MethodBuilder, Location);
3875 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
3878 if (OptAttributes != null)
3879 OptAttributes.Emit ();
3881 if (declarative_security != null) {
3882 foreach (DictionaryEntry de in declarative_security) {
3883 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3887 if (MethodData != null)
3888 MethodData.Emit (Parent);
3896 protected void Error_ConditionalAttributeIsNotValid ()
3898 Report.Error (577, Location,
3899 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3900 GetSignatureForError ());
3903 public bool IsPartialDefinition {
3905 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3909 public bool IsPartialImplementation {
3911 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3915 public override string[] ValidAttributeTargets {
3917 return attribute_targets;
3921 #region IMethodData Members
3923 public Type ReturnType {
3929 public MemberName MethodName {
3936 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3938 public bool IsExcluded () {
3939 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3940 return (caching_flags & Flags.Excluded) != 0;
3942 caching_flags &= ~Flags.Excluded_Undetected;
3944 if (base_method == null) {
3945 if (OptAttributes == null)
3948 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
3953 foreach (Attribute a in attrs) {
3954 string condition = a.GetConditionalAttributeValue ();
3955 if (condition == null)
3958 if (Location.CompilationUnit.IsConditionalDefined (condition))
3962 caching_flags |= Flags.Excluded;
3966 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
3968 if (AttributeTester.IsConditionalMethodExcluded (base_method, Location)) {
3969 caching_flags |= Flags.Excluded;
3975 if (md.IsExcluded ()) {
3976 caching_flags |= Flags.Excluded;
3982 GenericMethod IMethodData.GenericMethod {
3984 return GenericMethod;
3988 public virtual void EmitExtraSymbolInfo (SourceMethod source)
3995 public class SourceMethod : IMethodDef
3998 SourceMethodBuilder builder;
4000 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
4002 this.method = method;
4004 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
4007 public string Name {
4008 get { return method.Name; }
4013 if (method is MethodBuilder)
4014 return ((MethodBuilder) method).GetToken ().Token;
4015 else if (method is ConstructorBuilder)
4016 return ((ConstructorBuilder) method).GetToken ().Token;
4018 throw new NotSupportedException ();
4022 public void CloseMethod ()
4024 SymbolWriter.CloseMethod ();
4027 public void SetRealMethodName (string name)
4029 if (builder != null)
4030 builder.SetRealMethodName (name);
4033 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4035 if (!SymbolWriter.HasSymbolWriter)
4040 Location start_loc = block.StartLocation;
4041 if (start_loc.IsNull)
4044 ICompileUnit compile_unit = start_loc.CompilationUnit;
4045 if (compile_unit == null)
4048 return new SourceMethod (parent, method, compile_unit);
4052 public class Method : MethodOrOperator {
4055 /// Modifiers allowed in a class declaration
4057 const int AllowedModifiers =
4060 Modifiers.PROTECTED |
4061 Modifiers.INTERNAL |
4066 Modifiers.OVERRIDE |
4067 Modifiers.ABSTRACT |
4071 const int AllowedInterfaceModifiers =
4072 Modifiers.NEW | Modifiers.UNSAFE;
4074 public Method (DeclSpace parent, GenericMethod generic,
4075 FullNamedExpression return_type, int mod,
4076 MemberName name, ParametersCompiled parameters, Attributes attrs)
4077 : base (parent, generic, return_type, mod,
4078 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
4079 name, attrs, parameters)
4083 protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
4084 MemberName name, ParametersCompiled parameters, Attributes attrs)
4085 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
4089 public override string GetSignatureForError()
4091 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4094 static void Error_DuplicateEntryPoint (Method b)
4096 Report.Error (17, b.Location,
4097 "Program `{0}' has more than one entry point defined: `{1}'",
4098 CodeGen.FileName, b.GetSignatureForError ());
4101 bool IsEntryPoint ()
4103 if (ReturnType != TypeManager.void_type &&
4104 ReturnType != TypeManager.int32_type)
4107 if (Parameters.Count == 0)
4110 if (Parameters.Count > 1)
4113 Type t = Parameters.Types [0];
4114 return t.IsArray && t.GetArrayRank () == 1 &&
4115 TypeManager.GetElementType (t) == TypeManager.string_type &&
4116 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4119 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4121 if (a.Type == pa.Conditional) {
4122 if (IsExplicitImpl) {
4123 Error_ConditionalAttributeIsNotValid ();
4127 if (ReturnType != TypeManager.void_type) {
4128 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4132 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4133 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4138 Report.Error (582, Location, "Conditional not valid on interface members");
4142 if (MethodData.implementing != null) {
4143 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4144 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4145 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4149 for (int i = 0; i < Parameters.Count; ++i) {
4150 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
4151 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4157 if (a.Type == pa.Extension) {
4158 a.Error_MisusedExtensionAttribute ();
4162 base.ApplyAttributeBuilder (a, cb, pa);
4165 protected override bool CheckForDuplications ()
4167 if (!base.CheckForDuplications ())
4170 ArrayList ar = Parent.PartialContainer.Properties;
4172 for (int i = 0; i < ar.Count; ++i) {
4173 PropertyBase pb = (PropertyBase) ar [i];
4174 if (pb.AreAccessorsDuplicateImplementation (this))
4179 ar = Parent.PartialContainer.Indexers;
4181 for (int i = 0; i < ar.Count; ++i) {
4182 PropertyBase pb = (PropertyBase) ar [i];
4183 if (pb.AreAccessorsDuplicateImplementation (this))
4191 protected override bool CheckBase ()
4193 if (!base.CheckBase ())
4196 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == Destructor.MetadataName) {
4197 Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
4198 TypeManager.CSharpSignature (base_method));
4207 public override bool Define ()
4209 if (type_name == TypeManager.system_void_expr && Parameters.IsEmpty && Name == Destructor.MetadataName) {
4210 Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4213 if (!base.Define ())
4216 if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
4217 Error1599 (Location, ReturnType);
4221 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4222 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4223 Parent.PartialContainer.Mark_HasEquals ();
4224 else if (Parameters.IsEmpty && Name == "GetHashCode")
4225 Parent.PartialContainer.Mark_HasGetHashCode ();
4228 if ((ModFlags & Modifiers.STATIC) == 0)
4231 if (Parameters.HasExtensionMethodType) {
4232 if (Parent.PartialContainer.IsStaticClass && !Parent.IsGeneric) {
4233 if (!Parent.IsTopLevel)
4234 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4235 GetSignatureForError ());
4237 PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
4238 if (!pa.IsDefined) {
4239 Report.Error (1110, Location,
4240 "`{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",
4241 GetSignatureForError ());
4244 ModFlags |= Modifiers.METHOD_EXTENSION;
4245 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
4246 CodeGen.Assembly.HasExtensionMethods = true;
4248 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4249 GetSignatureForError ());
4254 // This is used to track the Entry Point,
4256 if (RootContext.NeedsEntryPoint &&
4258 (RootContext.MainClass == null ||
4259 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4260 if (IsEntryPoint ()) {
4262 if (RootContext.EntryPoint == null) {
4263 if (Parent.IsGeneric || MemberName.IsGeneric) {
4264 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4265 GetSignatureForError ());
4268 RootContext.EntryPoint = this;
4271 Error_DuplicateEntryPoint (RootContext.EntryPoint);
4272 Error_DuplicateEntryPoint (this);
4275 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4276 GetSignatureForError ());
4286 public override void Emit ()
4289 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4290 if (IsPartialDefinition) {
4292 // Do attribute checks only when partial implementation does not exist
4294 if (MethodBuilder == null)
4300 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
4301 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4302 GetSignatureForError ());
4306 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4307 PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
4309 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4310 Location, MethodBuilder);
4315 public override bool EnableOverloadChecks (MemberCore overload)
4317 // TODO: It can be deleted when members will be defined in correct order
4318 if (overload is Operator)
4319 return overload.EnableOverloadChecks (this);
4321 if (overload is Indexer)
4324 return base.EnableOverloadChecks (overload);
4327 public static void Error1599 (Location loc, Type t)
4329 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4332 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4334 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4335 Parent.TypeBuilder, Name, Parameters, GenericMethod, false);
4340 if (mi.IsSpecialName)
4343 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4347 public void SetPartialDefinition (Method methodDefinition)
4349 caching_flags |= Flags.PartialDefinitionExists;
4350 methodDefinition.MethodBuilder = MethodBuilder;
4352 for (int i = 0; i < methodDefinition.Parameters.Count; ++i ) {
4353 Parameters [i].DefaultValue = methodDefinition.Parameters [i].DefaultValue;
4356 if (methodDefinition.attributes == null)
4359 if (attributes == null) {
4360 attributes = methodDefinition.attributes;
4361 attributes.AttachTo (this);
4363 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4367 protected override bool VerifyClsCompliance ()
4369 if (!base.VerifyClsCompliance ())
4372 if (!Parameters.IsEmpty) {
4373 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4375 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4382 public abstract class ConstructorInitializer : ExpressionStatement
4384 Arguments argument_list;
4385 MethodGroupExpr base_constructor_group;
4387 public ConstructorInitializer (Arguments argument_list, Location loc)
4389 this.argument_list = argument_list;
4393 public Arguments Arguments {
4395 return argument_list;
4399 public override Expression CreateExpressionTree (EmitContext ec)
4401 throw new NotSupportedException ("ET");
4404 public ExpressionStatement Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4406 if (argument_list != null) {
4408 argument_list.Resolve (ec, out dynamic);
4410 SimpleName ctor = new SimpleName (ConstructorBuilder.ConstructorName, loc);
4411 return new DynamicInvocation (ctor, argument_list, loc).Resolve (ec) as ExpressionStatement;
4415 if (this is ConstructorBaseInitializer) {
4416 if (ec.ContainerType.BaseType == null)
4419 type = ec.ContainerType.BaseType;
4420 if (TypeManager.IsStruct (ec.ContainerType)) {
4421 Report.Error (522, loc,
4422 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4427 // It is legal to have "this" initializers that take no arguments
4428 // in structs, they are just no-ops.
4430 // struct D { public D (int a) : this () {}
4432 if (TypeManager.IsStruct (ec.ContainerType) && argument_list == null)
4435 type = ec.ContainerType;
4438 base_constructor_group = MemberLookupFinal (
4439 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4440 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4441 loc) as MethodGroupExpr;
4443 if (base_constructor_group == null)
4446 base_constructor_group = base_constructor_group.OverloadResolve (
4447 ec, ref argument_list, false, loc);
4449 if (base_constructor_group == null)
4452 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4454 if (base_ctor == caller_builder){
4455 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4461 public override Expression DoResolve (EmitContext ec)
4463 throw new NotSupportedException ();
4466 public override void Emit (EmitContext ec)
4468 // It can be null for static initializers
4469 if (base_constructor_group == null)
4474 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4476 base_constructor_group.EmitCall (ec, argument_list);
4479 public override void EmitStatement (EmitContext ec)
4485 public class ConstructorBaseInitializer : ConstructorInitializer {
4486 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
4487 base (argument_list, l)
4492 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4493 public GeneratedBaseInitializer (Location loc):
4499 public class ConstructorThisInitializer : ConstructorInitializer {
4500 public ConstructorThisInitializer (Arguments argument_list, Location l) :
4501 base (argument_list, l)
4506 public class Constructor : MethodCore, IMethodData {
4507 public ConstructorBuilder ConstructorBuilder;
4508 public ConstructorInitializer Initializer;
4509 ListDictionary declarative_security;
4510 bool has_compliant_args;
4513 // Modifiers allowed for a constructor.
4515 public const int AllowedModifiers =
4517 Modifiers.PROTECTED |
4518 Modifiers.INTERNAL |
4524 static readonly string[] attribute_targets = new string [] { "method" };
4527 // The spec claims that static is not permitted, but
4528 // my very own code has static constructors.
4530 public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, ParametersCompiled args,
4531 ConstructorInitializer init, Location loc)
4532 : base (parent, null, null, mod, AllowedModifiers,
4533 new MemberName (name, loc), attrs, args)
4538 public bool HasCompliantArgs {
4539 get { return has_compliant_args; }
4542 public override AttributeTargets AttributeTargets {
4543 get { return AttributeTargets.Constructor; }
4547 // Returns true if this is a default constructor
4549 public bool IsDefault ()
4551 if ((ModFlags & Modifiers.STATIC) != 0)
4552 return Parameters.IsEmpty;
4554 return Parameters.IsEmpty &&
4555 (Initializer is ConstructorBaseInitializer) &&
4556 (Initializer.Arguments == null);
4559 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4561 if (a.IsValidSecurityAttribute ()) {
4562 if (declarative_security == null) {
4563 declarative_security = new ListDictionary ();
4565 a.ExtractSecurityPermissionSet (declarative_security);
4569 if (a.IsInternalMethodImplAttribute) {
4570 is_external_implementation = true;
4573 ConstructorBuilder.SetCustomAttribute (cb);
4576 protected override bool CheckBase ()
4578 if ((ModFlags & Modifiers.STATIC) != 0) {
4579 if (!Parameters.IsEmpty) {
4580 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4581 GetSignatureForError ());
4585 // the rest can be ignored
4589 // Check whether arguments were correct.
4590 if (!DefineParameters (Parameters))
4593 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4594 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorInfo.ConstructorName,
4597 if (Parent.PartialContainer.Kind == Kind.Struct) {
4598 if (Parameters.Count == 0) {
4599 Report.Error (568, Location,
4600 "Structs cannot contain explicit parameterless constructors");
4605 CheckProtectedModifier ();
4611 // Creates the ConstructorBuilder
4613 public override bool Define ()
4615 if (ConstructorBuilder != null)
4618 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4619 MethodAttributes.SpecialName);
4621 if ((ModFlags & Modifiers.STATIC) != 0) {
4622 ca |= MethodAttributes.Static | MethodAttributes.Private;
4624 ca |= MethodAttributes.HideBySig;
4626 if ((ModFlags & Modifiers.PUBLIC) != 0)
4627 ca |= MethodAttributes.Public;
4628 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4629 if ((ModFlags & Modifiers.INTERNAL) != 0)
4630 ca |= MethodAttributes.FamORAssem;
4632 ca |= MethodAttributes.Family;
4633 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4634 ca |= MethodAttributes.Assembly;
4636 ca |= MethodAttributes.Private;
4639 if (!CheckAbstractAndExtern (block != null))
4642 // Check if arguments were correct.
4646 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4647 ca, CallingConventions,
4648 Parameters.GetEmitTypes ());
4650 if (Parent.PartialContainer.IsComImport) {
4651 if (!IsDefault ()) {
4652 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4653 Parent.GetSignatureForError ());
4655 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4658 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4659 TypeManager.AddMethod (ConstructorBuilder, this);
4661 // It's here only to report an error
4662 if (block != null && block.IsIterator) {
4663 member_type = TypeManager.void_type;
4664 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
4673 public override void Emit ()
4675 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4676 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
4678 if (OptAttributes != null)
4679 OptAttributes.Emit ();
4683 EmitContext ec = CreateEmitContext (null, null);
4686 // If we use a "this (...)" constructor initializer, then
4687 // do not emit field initializers, they are initialized in the other constructor
4689 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4690 !(Initializer is ConstructorThisInitializer);
4692 if (emit_field_initializers)
4693 Parent.PartialContainer.ResolveFieldInitializers (ec);
4695 if (block != null) {
4696 // If this is a non-static `struct' constructor and doesn't have any
4697 // initializer, it must initialize all of the struct's fields.
4698 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4699 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4700 block.AddThisVariable (Parent, Location);
4702 if (!block.ResolveMeta (ec, Parameters))
4705 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4706 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4707 Initializer = new GeneratedBaseInitializer (Location);
4710 // Spec mandates that Initializers will not have `this' access
4712 if (Initializer != null) {
4714 ExpressionStatement expr = Initializer.Resolve (ConstructorBuilder, ec);
4715 ec.IsStatic = false;
4716 block.AddScopeStatement (new StatementExpression (expr));
4721 Parameters.ApplyAttributes (ConstructorBuilder);
4723 SourceMethod source = null;
4725 ec.OmitDebuggingInfo = true;
4727 source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4729 bool unreachable = false;
4730 if (block != null) {
4731 if (!ec.ResolveTopBlock (null, block, Parameters, this, out unreachable))
4734 ec.EmitMeta (block);
4736 if (Report.Errors > 0)
4739 ec.EmitResolvedTopBlock (block, unreachable);
4743 source.CloseMethod ();
4745 if (declarative_security != null) {
4746 foreach (DictionaryEntry de in declarative_security) {
4747 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4754 // Is never override
4755 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4760 public override string GetSignatureForError()
4762 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4765 public override string[] ValidAttributeTargets {
4767 return attribute_targets;
4771 protected override bool VerifyClsCompliance ()
4773 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4777 if (!Parameters.IsEmpty) {
4778 ArrayList al = (ArrayList)Parent.MemberCache.Members [ConstructorInfo.ConstructorName];
4780 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4782 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4783 foreach (Type param in Parameters.Types) {
4784 if (param.IsArray) {
4790 has_compliant_args = true;
4794 #region IMethodData Members
4796 public MemberName MethodName {
4802 public Type ReturnType {
4808 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4810 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4811 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
4812 ec.CurrentBlock = block;
4816 public bool IsExcluded()
4821 GenericMethod IMethodData.GenericMethod {
4827 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4834 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4836 public interface IMethodData
4838 CallingConventions CallingConventions { get; }
4839 Location Location { get; }
4840 MemberName MethodName { get; }
4841 Type ReturnType { get; }
4842 GenericMethod GenericMethod { get; }
4843 ParametersCompiled ParameterInfo { get; }
4845 Attributes OptAttributes { get; }
4846 ToplevelBlock Block { get; set; }
4848 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4849 ObsoleteAttribute GetObsoleteAttribute ();
4850 string GetSignatureForError ();
4852 bool IsClsComplianceRequired ();
4853 void SetMemberIsUsed ();
4854 void EmitExtraSymbolInfo (SourceMethod source);
4858 // Encapsulates most of the Method's state
4860 public class MethodData {
4861 static FieldInfo methodbuilder_attrs_field;
4862 public readonly IMethodData method;
4864 public readonly GenericMethod GenericMethod;
4867 // Are we implementing an interface ?
4869 public MethodInfo implementing;
4874 protected InterfaceMemberBase member;
4875 protected int modifiers;
4876 protected MethodAttributes flags;
4877 protected Type declaring_type;
4878 protected MethodInfo parent_method;
4880 MethodBuilder builder = null;
4881 public MethodBuilder MethodBuilder {
4887 public Type DeclaringType {
4889 return declaring_type;
4893 public MethodData (InterfaceMemberBase member,
4894 int modifiers, MethodAttributes flags, IMethodData method)
4896 this.member = member;
4897 this.modifiers = modifiers;
4900 this.method = method;
4903 public MethodData (InterfaceMemberBase member,
4904 int modifiers, MethodAttributes flags,
4905 IMethodData method, MethodBuilder builder,
4906 GenericMethod generic, MethodInfo parent_method)
4907 : this (member, modifiers, flags, method)
4909 this.builder = builder;
4910 this.GenericMethod = generic;
4911 this.parent_method = parent_method;
4914 public bool Define (DeclSpace parent, string method_full_name)
4916 string name = method.MethodName.Basename;
4918 TypeContainer container = parent.PartialContainer;
4920 PendingImplementation pending = container.PendingImplementations;
4921 if (pending != null){
4922 implementing = pending.IsInterfaceMethod (name, member.InterfaceType, this);
4924 if (member.InterfaceType != null){
4925 if (implementing == null){
4926 if (member is PropertyBase) {
4927 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4928 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4929 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4932 Report.Error (539, method.Location,
4933 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4934 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4938 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4939 Report.SymbolRelatedToPreviousError (implementing);
4940 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4941 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4945 if (implementing != null) {
4946 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4947 if (prop_method == null) {
4948 if (TypeManager.IsSpecialMethod (implementing)) {
4949 Report.SymbolRelatedToPreviousError (implementing);
4950 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
4951 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
4952 implementing.Name.StartsWith ("get_") ? "get" : "set");
4954 } else if (implementing.DeclaringType.IsInterface) {
4955 if (!implementing.IsSpecialName) {
4956 Report.SymbolRelatedToPreviousError (implementing);
4957 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4958 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4961 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4962 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4963 Report.SymbolRelatedToPreviousError (implementing);
4964 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4965 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4974 // For implicit implementations, make sure we are public, for
4975 // explicit implementations, make sure we are private.
4977 if (implementing != null){
4979 // Setting null inside this block will trigger a more
4980 // verbose error reporting for missing interface implementations
4982 // The "candidate" function has been flagged already
4983 // but it wont get cleared
4985 if (member.IsExplicitImpl){
4986 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
4987 Report.SymbolRelatedToPreviousError (implementing);
4988 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
4989 method.GetSignatureForError ());
4993 if (implementing.DeclaringType.IsInterface) {
4995 // If this is an interface method implementation,
4996 // check for public accessibility
4998 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5000 implementing = null;
5002 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5003 // We may never be private.
5004 implementing = null;
5006 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5008 // We may be protected if we're overriding something.
5010 implementing = null;
5015 // Static is not allowed
5017 if ((modifiers & Modifiers.STATIC) != 0){
5018 implementing = null;
5023 // If implementing is still valid, set flags
5025 if (implementing != null){
5027 // When implementing interface methods, set NewSlot
5028 // unless, we are overwriting a method.
5030 if (implementing.DeclaringType.IsInterface){
5031 if ((modifiers & Modifiers.OVERRIDE) == 0)
5032 flags |= MethodAttributes.NewSlot;
5035 MethodAttributes.Virtual |
5036 MethodAttributes.HideBySig;
5038 // Set Final unless we're virtual, abstract or already overriding a method.
5039 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5040 flags |= MethodAttributes.Final;
5043 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
5045 if (builder == null)
5048 if (container.CurrentType != null)
5049 declaring_type = container.CurrentType;
5051 declaring_type = container.TypeBuilder;
5053 if (implementing != null && member.IsExplicitImpl) {
5054 container.TypeBuilder.DefineMethodOverride (builder, implementing);
5057 TypeManager.AddMethod (builder, method);
5059 if (GenericMethod != null) {
5060 bool is_override = member.IsExplicitImpl |
5061 ((modifiers & Modifiers.OVERRIDE) != 0);
5063 if (implementing != null)
5064 parent_method = implementing;
5066 EmitContext ec = method.CreateEmitContext (container, null);
5067 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5076 /// Create the MethodBuilder for the method
5078 void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
5080 if (builder == null) {
5081 builder = container.TypeBuilder.DefineMethod (
5082 method_name, flags, method.CallingConventions,
5083 TypeManager.TypeToReflectionType (method.ReturnType),
5084 param.GetEmitTypes ());
5090 // Generic method has been already defined to resolve method parameters
5091 // correctly when they use type parameters
5093 builder.SetParameters (param.GetEmitTypes ());
5094 builder.SetReturnType (method.ReturnType);
5096 if (builder.Attributes != flags) {
5098 if (methodbuilder_attrs_field == null)
5099 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5100 methodbuilder_attrs_field.SetValue (builder, flags);
5102 Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5110 public void Emit (DeclSpace parent)
5112 ToplevelBlock block = method.Block;
5116 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5118 ec = method.CreateEmitContext (parent, null);
5120 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5122 if (GenericMethod != null)
5123 GenericMethod.EmitAttributes ();
5126 // clear the pending implementation flag
5128 if (implementing != null)
5129 parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName.Basename,
5130 member.InterfaceType, this, member.IsExplicitImpl);
5132 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5134 ec.EmitTopBlock (method, block);
5136 if (source != null) {
5137 method.EmitExtraSymbolInfo (source);
5138 source.CloseMethod ();
5143 public class Destructor : MethodOrOperator
5145 const int AllowedModifiers =
5149 static readonly string[] attribute_targets = new string [] { "method" };
5151 public static readonly string MetadataName = "Finalize";
5153 public Destructor (DeclSpace parent, int mod, ParametersCompiled parameters, Attributes attrs, Location l)
5154 : base (parent, null, TypeManager.system_void_expr, mod, AllowedModifiers,
5155 new MemberName (MetadataName, l), attrs, parameters)
5157 ModFlags &= ~Modifiers.PRIVATE;
5158 ModFlags |= Modifiers.PROTECTED;
5161 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5163 if (a.Type == pa.Conditional) {
5164 Error_ConditionalAttributeIsNotValid ();
5168 base.ApplyAttributeBuilder (a, cb, pa);
5171 protected override bool CheckBase ()
5173 flags |= MethodAttributes.Virtual;
5175 if (!base.CheckBase ())
5178 if (Parent.PartialContainer.BaseCache == null)
5181 Type base_type = Parent.PartialContainer.BaseCache.Container.Type;
5182 if (base_type != null && Block != null) {
5183 MethodGroupExpr method_expr = Expression.MethodLookup (Parent.TypeBuilder, base_type, MetadataName, Location);
5184 if (method_expr == null)
5185 throw new NotImplementedException ();
5187 method_expr.IsBase = true;
5188 method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.TypeBuilder, Location);
5190 ToplevelBlock new_block = new ToplevelBlock (Block.StartLocation);
5191 new_block.EndLocation = Block.EndLocation;
5193 Block finaly_block = new ExplicitBlock (new_block, Location, Location);
5194 Block try_block = new Block (new_block, block);
5197 // 0-size arguments to avoid CS0250 error
5198 // TODO: Should use AddScopeStatement or something else which emits correct
5201 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
5202 new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
5210 public override string GetSignatureForError ()
5212 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5215 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
5220 public override string[] ValidAttributeTargets {
5222 return attribute_targets;
5227 public abstract class MemberBase : MemberCore
5229 protected FullNamedExpression type_name;
5230 protected Type member_type;
5232 public readonly DeclSpace ds;
5233 public readonly GenericMethod GenericMethod;
5235 protected MemberBase (DeclSpace parent, GenericMethod generic,
5236 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5237 MemberName name, Attributes attrs)
5238 : base (parent, name, attrs)
5240 this.ds = generic != null ? generic : (DeclSpace) parent;
5241 this.type_name = type;
5242 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5243 GenericMethod = generic;
5244 if (GenericMethod != null)
5245 GenericMethod.ModFlags = ModFlags;
5249 // Main member define entry
5251 public override bool Define ()
5253 DoMemberTypeIndependentChecks ();
5256 // Returns false only when type resolution failed
5258 if (!ResolveMemberType ())
5261 DoMemberTypeDependentChecks ();
5266 // Any type_name independent checks
5268 protected virtual void DoMemberTypeIndependentChecks ()
5270 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5271 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5272 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5273 GetSignatureForError (), Parent.GetSignatureForError ());
5278 // Any type_name dependent checks
5280 protected virtual void DoMemberTypeDependentChecks ()
5282 // verify accessibility
5283 if (!IsAccessibleAs (MemberType)) {
5284 Report.SymbolRelatedToPreviousError (MemberType);
5285 if (this is Property)
5286 Report.Error (53, Location,
5287 "Inconsistent accessibility: property type `" +
5288 TypeManager.CSharpName (MemberType) + "' is less " +
5289 "accessible than property `" + GetSignatureForError () + "'");
5290 else if (this is Indexer)
5291 Report.Error (54, Location,
5292 "Inconsistent accessibility: indexer return type `" +
5293 TypeManager.CSharpName (MemberType) + "' is less " +
5294 "accessible than indexer `" + GetSignatureForError () + "'");
5295 else if (this is MethodCore) {
5296 if (this is Operator)
5297 Report.Error (56, Location,
5298 "Inconsistent accessibility: return type `" +
5299 TypeManager.CSharpName (MemberType) + "' is less " +
5300 "accessible than operator `" + GetSignatureForError () + "'");
5302 Report.Error (50, Location,
5303 "Inconsistent accessibility: return type `" +
5304 TypeManager.CSharpName (MemberType) + "' is less " +
5305 "accessible than method `" + GetSignatureForError () + "'");
5307 Report.Error (52, Location,
5308 "Inconsistent accessibility: field type `" +
5309 TypeManager.CSharpName (MemberType) + "' is less " +
5310 "accessible than field `" + GetSignatureForError () + "'");
5314 Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
5315 TypeManager.CheckTypeVariance (MemberType, variance, this);
5318 protected bool IsTypePermitted ()
5320 if (TypeManager.IsSpecialType (MemberType)) {
5321 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5327 protected virtual bool CheckBase ()
5329 CheckProtectedModifier ();
5334 public Type MemberType {
5335 get { return member_type; }
5338 protected virtual bool ResolveMemberType ()
5340 if (member_type != null)
5341 throw new InternalErrorException ("Multi-resolve");
5343 IResolveContext rc = GenericMethod == null ? this : (IResolveContext) ds;
5344 TypeExpr te = type_name.ResolveAsTypeTerminal (rc, false);
5349 // Replace original type name, error reporting can use fully resolved name
5353 member_type = te.Type;
5359 // Abstract class for all fields
5361 abstract public class FieldBase : MemberBase {
5362 public FieldBuilder FieldBuilder;
5363 public Status status;
5364 protected Expression initializer;
5367 public enum Status : byte {
5368 HAS_OFFSET = 4 // Used by FieldMember.
5371 static readonly string[] attribute_targets = new string [] { "field" };
5373 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5374 int allowed_mod, MemberName name, Attributes attrs)
5375 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5378 if ((mod & Modifiers.ABSTRACT) != 0)
5379 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5382 public override AttributeTargets AttributeTargets {
5384 return AttributeTargets.Field;
5388 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5390 if (a.Type == pa.FieldOffset) {
5391 status |= Status.HAS_OFFSET;
5393 if (!Parent.PartialContainer.HasExplicitLayout) {
5394 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5398 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5399 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5405 if (a.Type == pa.FixedBuffer) {
5406 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5412 if (a.Type == pa.MarshalAs) {
5413 UnmanagedMarshal marshal = a.GetMarshal (this);
5414 if (marshal != null) {
5415 FieldBuilder.SetMarshal (marshal);
5420 if ((a.HasSecurityAttribute)) {
5421 a.Error_InvalidSecurityParent ();
5425 FieldBuilder.SetCustomAttribute (cb);
5428 protected override bool CheckBase ()
5430 if (!base.CheckBase ())
5433 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5434 if (conflict_symbol == null) {
5435 if ((ModFlags & Modifiers.NEW) != 0) {
5436 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5441 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
5442 Report.SymbolRelatedToPreviousError (conflict_symbol);
5443 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5444 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5450 protected override void DoMemberTypeDependentChecks ()
5452 base.DoMemberTypeDependentChecks ();
5454 if (TypeManager.IsGenericParameter (MemberType))
5457 if (MemberType.IsSealed && MemberType.IsAbstract) {
5458 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5466 // Represents header string for documentation comment.
5468 public override string DocCommentHeader {
5469 get { return "F:"; }
5472 public override void Emit ()
5474 if (TypeManager.IsDynamicType (member_type))
5475 PredefinedAttributes.Get.Dynamic.EmitAttribute (FieldBuilder);
5477 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
5478 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
5480 if (OptAttributes != null) {
5481 OptAttributes.Emit ();
5484 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5485 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
5491 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5493 Report.SymbolRelatedToPreviousError (static_class);
5494 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5498 public Expression Initializer {
5500 if (value != null) {
5501 this.initializer = value;
5506 protected virtual bool IsFieldClsCompliant {
5508 if (FieldBuilder == null)
5511 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5515 public override string[] ValidAttributeTargets
5518 return attribute_targets;
5522 protected override bool VerifyClsCompliance ()
5524 if (!base.VerifyClsCompliance ())
5527 if (!IsFieldClsCompliant) {
5528 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
5529 GetSignatureForError ());
5534 public void SetAssigned ()
5536 caching_flags |= Flags.IsAssigned;
5540 interface IFixedBuffer
5542 FieldInfo Element { get; }
5543 Type ElementType { get; }
5546 public class FixedFieldExternal: IFixedBuffer
5548 FieldInfo element_field;
5550 public FixedFieldExternal (FieldInfo fi)
5552 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5555 #region IFixedField Members
5557 public FieldInfo Element {
5559 return element_field;
5563 public Type ElementType {
5565 return element_field.FieldType;
5573 /// Fixed buffer implementation
5575 public class FixedField : FieldBase, IFixedBuffer
5577 public const string FixedElementName = "FixedElementField";
5578 static int GlobalCounter = 0;
5579 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5580 static FieldInfo[] fi;
5582 TypeBuilder fixed_buffer_type;
5583 FieldBuilder element;
5584 Expression size_expr;
5586 const int AllowedModifiers =
5589 Modifiers.PROTECTED |
5590 Modifiers.INTERNAL |
5593 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5594 Expression size_expr, Attributes attrs, Location loc):
5595 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5597 if (RootContext.Version < LanguageVersion.ISO_2)
5598 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5600 this.size_expr = size_expr;
5603 public override bool Define()
5605 if (!base.Define ())
5608 if (!TypeManager.IsPrimitiveType (MemberType)) {
5609 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",
5610 GetSignatureForError ());
5613 // Create nested fixed buffer container
5614 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5615 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
5616 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5618 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5619 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5621 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5622 Parent.MemberCache.AddMember (FieldBuilder, this);
5623 TypeManager.RegisterFieldBase (FieldBuilder, this);
5628 protected override void DoMemberTypeIndependentChecks ()
5630 base.DoMemberTypeIndependentChecks ();
5632 if (!Parent.IsInUnsafeScope)
5633 Expression.UnsafeError (Location);
5635 if (Parent.PartialContainer.Kind != Kind.Struct) {
5636 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5637 GetSignatureForError ());
5641 public override void Emit()
5643 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5644 Constant c = size_expr.ResolveAsConstant (ec, this);
5648 IntConstant buffer_size_const = c.ImplicitConversionRequired (ec, TypeManager.int32_type, Location) as IntConstant;
5649 if (buffer_size_const == null)
5652 int buffer_size = buffer_size_const.Value;
5654 if (buffer_size <= 0) {
5655 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5659 int type_size = Expression.GetTypeSize (MemberType);
5661 if (buffer_size > int.MaxValue / type_size) {
5662 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5663 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5667 buffer_size *= type_size;
5668 EmitFieldSize (buffer_size);
5670 PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
5675 void EmitFieldSize (int buffer_size)
5677 CustomAttributeBuilder cab;
5678 PredefinedAttribute pa;
5680 pa = PredefinedAttributes.Get.StructLayout;
5681 if (pa.Constructor == null &&
5682 !pa.ResolveConstructor (Location, TypeManager.short_type))
5685 // TODO: It's not cleared
5687 fi = new FieldInfo[] { pa.Type.GetField ("Size") };
5689 object[] fi_val = new object[] { buffer_size };
5690 cab = new CustomAttributeBuilder (pa.Constructor,
5691 ctor_args, fi, fi_val);
5692 fixed_buffer_type.SetCustomAttribute (cab);
5695 // Don't emit FixedBufferAttribute attribute for private types
5697 if ((ModFlags & Modifiers.PRIVATE) != 0)
5700 pa = PredefinedAttributes.Get.FixedBuffer;
5701 if (pa.Constructor == null &&
5702 !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
5705 cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
5706 FieldBuilder.SetCustomAttribute (cab);
5709 protected override bool IsFieldClsCompliant {
5715 public void SetCharSet (TypeAttributes ta)
5717 TypeAttributes cta = fixed_buffer_type.Attributes;
5718 if ((cta & TypeAttributes.UnicodeClass) != (ta & TypeAttributes.UnicodeClass))
5719 SetTypeBuilderCharSet ((cta & ~TypeAttributes.AutoClass) | TypeAttributes.UnicodeClass);
5720 else if ((cta & TypeAttributes.AutoClass) != (ta & TypeAttributes.AutoClass))
5721 SetTypeBuilderCharSet ((cta & ~TypeAttributes.UnicodeClass) | TypeAttributes.AutoClass);
5722 else if (cta == 0 && ta != 0)
5723 SetTypeBuilderCharSet (cta & ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass));
5726 void SetTypeBuilderCharSet (TypeAttributes ta)
5728 MethodInfo mi = typeof (TypeBuilder).GetMethod ("SetCharSet", BindingFlags.Instance | BindingFlags.NonPublic);
5730 Report.RuntimeMissingSupport (Location, "TypeBuilder::SetCharSet");
5732 mi.Invoke (fixed_buffer_type, new object [] { ta });
5736 #region IFixedField Members
5738 public FieldInfo Element {
5744 public Type ElementType {
5754 // The Field class is used to represents class/struct fields during parsing.
5756 public class Field : FieldBase {
5758 // Modifiers allowed in a class declaration
5760 const int AllowedModifiers =
5763 Modifiers.PROTECTED |
5764 Modifiers.INTERNAL |
5767 Modifiers.VOLATILE |
5771 public Field (DeclSpace parent, FullNamedExpression type, int mod, MemberName name,
5773 : base (parent, type, mod, AllowedModifiers, name, attrs)
5777 bool CanBeVolatile ()
5779 if (TypeManager.IsReferenceType (MemberType))
5782 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5783 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5784 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5785 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5786 MemberType == TypeManager.float_type ||
5787 MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
5790 if (TypeManager.IsEnumType (MemberType))
5796 bool CheckStructLayout (Type type, bool isStatic)
5798 if (TypeManager.IsBuiltinType (type))
5802 if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
5806 if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
5807 if (!TypeManager.IsGenericType (type))
5810 foreach (Type t in TypeManager.GetTypeArguments (type)) {
5811 if (!CheckStructLayout (t, false))
5817 Report.Error (523, Location,
5818 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
5819 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5823 public override bool Define ()
5825 if (!base.Define ())
5830 Type[] required_modifier = null;
5831 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5832 if (TypeManager.isvolatile_type == null)
5833 TypeManager.isvolatile_type = TypeManager.CoreLookupType (
5834 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5836 if (TypeManager.isvolatile_type != null)
5837 required_modifier = new Type [] { TypeManager.isvolatile_type };
5840 FieldBuilder = Parent.TypeBuilder.DefineField (
5841 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5843 FieldBuilder = Parent.TypeBuilder.DefineField (
5844 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5846 // Don't cache inaccessible fields
5847 if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
5848 Parent.MemberCache.AddMember (FieldBuilder, this);
5851 TypeManager.RegisterFieldBase (FieldBuilder, this);
5853 catch (ArgumentException) {
5854 Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
5858 if (initializer != null) {
5859 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5860 new FieldInitializer (FieldBuilder, initializer, this));
5862 if (Parent.PartialContainer.Kind == Kind.Struct)
5863 CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
5869 protected override void DoMemberTypeDependentChecks ()
5871 base.DoMemberTypeDependentChecks ();
5873 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5874 if (!CanBeVolatile ()) {
5875 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5876 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5879 if ((ModFlags & Modifiers.READONLY) != 0) {
5880 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5881 GetSignatureForError ());
5886 protected override bool VerifyClsCompliance ()
5888 if (!base.VerifyClsCompliance ())
5891 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5892 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5900 // `set' and `get' accessors are represented with an Accessor.
5902 public class Accessor {
5904 // Null if the accessor is empty, or a Block if not
5906 public const int AllowedModifiers =
5908 Modifiers.PROTECTED |
5909 Modifiers.INTERNAL |
5912 public ToplevelBlock Block;
5913 public Attributes Attributes;
5914 public Location Location;
5915 public int ModFlags;
5916 public ParametersCompiled Parameters;
5918 public Accessor (ToplevelBlock b, int mod, Attributes attrs, ParametersCompiled p, Location loc)
5924 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5928 // Ooouh Martin, templates are missing here.
5929 // When it will be possible move here a lot of child code and template method type.
5930 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5931 protected MethodData method_data;
5932 protected ToplevelBlock block;
5933 protected ListDictionary declarative_security;
5935 // The accessor are created even if they are not wanted.
5936 // But we need them because their names are reserved.
5937 // Field says whether accessor will be emited or not
5938 public readonly bool IsDummy;
5940 protected readonly string prefix;
5942 ReturnParameter return_attributes;
5944 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
5945 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5947 this.prefix = prefix;
5951 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
5953 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5954 accessor.Attributes)
5956 this.prefix = prefix;
5957 this.block = accessor.Block;
5960 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
5962 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5965 public void UpdateName (InterfaceMemberBase member)
5967 SetMemberName (SetupName (prefix, member, Location));
5970 #region IMethodData Members
5972 public ToplevelBlock Block {
5982 public CallingConventions CallingConventions {
5984 return CallingConventions.Standard;
5988 public bool IsExcluded ()
5993 GenericMethod IMethodData.GenericMethod {
5999 public MemberName MethodName {
6005 public Type[] ParameterTypes {
6007 return ParameterInfo.Types;
6011 public abstract ParametersCompiled ParameterInfo { get ; }
6012 public abstract Type ReturnType { get; }
6013 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
6017 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6019 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
6020 Report.Error (1667, a.Location,
6021 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6022 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6026 if (a.IsValidSecurityAttribute ()) {
6027 if (declarative_security == null)
6028 declarative_security = new ListDictionary ();
6029 a.ExtractSecurityPermissionSet (declarative_security);
6033 if (a.Target == AttributeTargets.Method) {
6034 method_data.MethodBuilder.SetCustomAttribute (cb);
6038 if (a.Target == AttributeTargets.ReturnValue) {
6039 if (return_attributes == null)
6040 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6042 return_attributes.ApplyAttributeBuilder (a, cb, pa);
6046 ApplyToExtraTarget (a, cb, pa);
6049 protected virtual void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6051 throw new NotSupportedException ("You forgot to define special attribute target handling");
6054 // It is not supported for the accessors
6055 public sealed override bool Define()
6057 throw new NotSupportedException ();
6060 public virtual void Emit (DeclSpace parent)
6062 method_data.Emit (parent);
6064 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
6065 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
6066 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6067 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
6069 if (TypeManager.IsDynamicType (ReturnType)) {
6070 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6071 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
6074 if (OptAttributes != null)
6075 OptAttributes.Emit ();
6077 if (declarative_security != null) {
6078 foreach (DictionaryEntry de in declarative_security) {
6079 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6086 public override bool EnableOverloadChecks (MemberCore overload)
6088 // This can only happen with indexers and it will
6089 // be catched as indexer difference
6090 if (overload is AbstractPropertyEventMethod)
6093 if (overload is MethodCore) {
6094 caching_flags |= Flags.MethodOverloadsExist;
6100 public override bool IsClsComplianceRequired()
6105 public bool IsDuplicateImplementation (MethodCore method)
6107 if (!MemberName.Equals (method.MemberName))
6110 Type[] param_types = method.ParameterTypes;
6112 if (param_types == null || param_types.Length != ParameterTypes.Length)
6115 for (int i = 0; i < param_types.Length; i++)
6116 if (param_types [i] != ParameterTypes [i])
6119 Report.SymbolRelatedToPreviousError (method);
6120 Report.Error (82, Location, "A member `{0}' is already reserved",
6121 method.GetSignatureForError ());
6125 public override bool IsUsed
6136 // Represents header string for documentation comment.
6138 public override string DocCommentHeader {
6139 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6142 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6147 // Properties and Indexers both generate PropertyBuilders, we use this to share
6148 // their common bits.
6150 abstract public class PropertyBase : PropertyBasedMember {
6152 public class GetMethod : PropertyMethod
6154 static string[] attribute_targets = new string [] { "method", "return" };
6156 public GetMethod (PropertyBase method):
6157 base (method, "get_")
6161 public GetMethod (PropertyBase method, Accessor accessor):
6162 base (method, accessor, "get_")
6166 public override MethodBuilder Define (DeclSpace parent)
6168 base.Define (parent);
6173 method_data = new MethodData (method, ModFlags, flags, this);
6175 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6178 return method_data.MethodBuilder;
6181 public override Type ReturnType {
6183 return method.MemberType;
6187 public override ParametersCompiled ParameterInfo {
6189 return ParametersCompiled.EmptyReadOnlyParameters;
6193 public override string[] ValidAttributeTargets {
6195 return attribute_targets;
6200 public class SetMethod : PropertyMethod {
6202 static string[] attribute_targets = new string [] { "method", "param", "return" };
6203 ImplicitParameter param_attr;
6204 protected ParametersCompiled parameters;
6206 public SetMethod (PropertyBase method) :
6207 base (method, "set_")
6209 parameters = new ParametersCompiled (
6210 new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
6213 public SetMethod (PropertyBase method, Accessor accessor):
6214 base (method, accessor, "set_")
6216 this.parameters = accessor.Parameters;
6219 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6221 if (a.Target == AttributeTargets.Parameter) {
6222 if (param_attr == null)
6223 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6225 param_attr.ApplyAttributeBuilder (a, cb, pa);
6229 base.ApplyAttributeBuilder (a, cb, pa);
6232 public override ParametersCompiled ParameterInfo {
6238 public override MethodBuilder Define (DeclSpace parent)
6240 parameters.Resolve (ResolveContext);
6242 base.Define (parent);
6247 method_data = new MethodData (method, ModFlags, flags, this);
6249 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6252 return method_data.MethodBuilder;
6255 public override Type ReturnType {
6257 return TypeManager.void_type;
6261 public override string[] ValidAttributeTargets {
6263 return attribute_targets;
6268 static string[] attribute_targets = new string [] { "property" };
6270 public abstract class PropertyMethod : AbstractPropertyEventMethod
6272 protected readonly PropertyBase method;
6273 protected MethodAttributes flags;
6275 public PropertyMethod (PropertyBase method, string prefix)
6276 : base (method, prefix)
6278 this.method = method;
6279 this.ModFlags = method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE);
6282 public PropertyMethod (PropertyBase method, Accessor accessor,
6284 : base (method, accessor, prefix)
6286 this.method = method;
6287 this.ModFlags = accessor.ModFlags | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
6289 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6290 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6294 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6296 if (a.IsInternalMethodImplAttribute) {
6297 method.is_external_implementation = true;
6300 base.ApplyAttributeBuilder (a, cb, pa);
6303 public override AttributeTargets AttributeTargets {
6305 return AttributeTargets.Method;
6309 public override bool IsClsComplianceRequired ()
6311 return method.IsClsComplianceRequired ();
6314 public virtual MethodBuilder Define (DeclSpace parent)
6316 CheckForDuplications ();
6319 if (method.InterfaceType != null && parent.PartialContainer.PendingImplementations != null) {
6320 MethodInfo mi = parent.PartialContainer.PendingImplementations.IsInterfaceMethod (
6321 MethodName.Name, method.InterfaceType, new MethodData (method, ModFlags, flags, this));
6323 Report.SymbolRelatedToPreviousError (mi);
6324 Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
6325 method.GetSignatureForError (), TypeManager.CSharpSignature (mi, true));
6331 TypeContainer container = parent.PartialContainer;
6334 // Check for custom access modifier
6336 if ((ModFlags & Modifiers.Accessibility) == 0) {
6337 ModFlags |= method.ModFlags;
6338 flags = method.flags;
6340 if (container.Kind == Kind.Interface)
6341 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6342 GetSignatureForError ());
6344 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6345 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6348 CheckModifiers (ModFlags);
6349 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6350 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6351 flags = Modifiers.MethodAttr (ModFlags);
6352 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6355 CheckAbstractAndExtern (block != null);
6357 if (block != null && block.IsIterator)
6358 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
6363 public bool HasCustomAccessModifier {
6365 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6369 public PropertyBase Property {
6375 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6377 return new EmitContext (this,
6378 ds, method.ds, method.Location, ig, ReturnType,
6379 method.ModFlags, false);
6382 public override ObsoleteAttribute GetObsoleteAttribute ()
6384 return method.GetObsoleteAttribute ();
6387 public override string GetSignatureForError()
6389 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6392 void CheckModifiers (int modflags)
6394 modflags &= Modifiers.Accessibility;
6396 int mflags = method.ModFlags & Modifiers.Accessibility;
6398 if ((mflags & Modifiers.PUBLIC) != 0) {
6399 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6401 else if ((mflags & Modifiers.PROTECTED) != 0) {
6402 if ((mflags & Modifiers.INTERNAL) != 0)
6403 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6405 flags |= Modifiers.PRIVATE;
6407 else if ((mflags & Modifiers.INTERNAL) != 0)
6408 flags |= Modifiers.PRIVATE;
6410 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6411 Report.Error (273, Location,
6412 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6413 GetSignatureForError (), method.GetSignatureForError ());
6417 protected bool CheckForDuplications ()
6419 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6422 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6426 public PropertyMethod Get, Set;
6427 public PropertyBuilder PropertyBuilder;
6428 public MethodBuilder GetBuilder, SetBuilder;
6430 protected bool define_set_first = false;
6432 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6433 int allowed_mod, MemberName name,
6434 Attributes attrs, bool define_set_first)
6435 : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
6437 this.define_set_first = define_set_first;
6440 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6442 if (a.HasSecurityAttribute) {
6443 a.Error_InvalidSecurityParent ();
6447 PropertyBuilder.SetCustomAttribute (cb);
6450 public override AttributeTargets AttributeTargets {
6452 return AttributeTargets.Property;
6456 protected override void DoMemberTypeDependentChecks ()
6458 base.DoMemberTypeDependentChecks ();
6462 if (MemberType.IsGenericParameter)
6466 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6467 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6471 protected override void DoMemberTypeIndependentChecks ()
6473 base.DoMemberTypeIndependentChecks ();
6476 // Accessors modifiers check
6478 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6479 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6480 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6481 GetSignatureForError ());
6484 if ((ModFlags & Modifiers.OVERRIDE) == 0 &&
6485 (Get.IsDummy && (Set.ModFlags & Modifiers.Accessibility) != 0) ||
6486 (Set.IsDummy && (Get.ModFlags & Modifiers.Accessibility) != 0)) {
6487 Report.Error (276, Location,
6488 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6489 GetSignatureForError ());
6495 GetBuilder = Get.Define (Parent);
6496 return (Get.IsDummy) ? true : GetBuilder != null;
6499 bool DefineSet (bool define)
6504 SetBuilder = Set.Define (Parent);
6505 return (Set.IsDummy) ? true : SetBuilder != null;
6508 protected bool DefineAccessors ()
6510 return DefineSet (define_set_first) &&
6512 DefineSet (!define_set_first);
6515 protected abstract PropertyInfo ResolveBaseProperty ();
6517 // TODO: rename to Resolve......
6518 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6520 PropertyInfo base_property = ResolveBaseProperty ();
6521 if (base_property == null)
6524 base_ret_type = base_property.PropertyType;
6525 MethodInfo get_accessor = base_property.GetGetMethod (true);
6526 MethodInfo set_accessor = base_property.GetSetMethod (true);
6527 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6530 // Check base property accessors conflict
6532 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6533 if (get_accessor == null) {
6534 if (Get != null && !Get.IsDummy) {
6535 Report.SymbolRelatedToPreviousError (base_property);
6536 Report.Error (545, Location,
6537 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6538 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6541 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6543 if (!Get.IsDummy && !CheckAccessModifiers (
6544 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6545 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6548 if (set_accessor == null) {
6549 if (Set != null && !Set.IsDummy) {
6550 Report.SymbolRelatedToPreviousError (base_property);
6551 Report.Error (546, Location,
6552 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6553 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6556 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6558 if (!Set.IsDummy && !CheckAccessModifiers (
6559 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6560 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6564 // When one accessor does not exist and property hides base one
6565 // we need to propagate this upwards
6566 if (set_accessor == null)
6567 set_accessor = get_accessor;
6570 // Get the less restrictive access
6572 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6575 public override void Emit ()
6578 // The PropertyBuilder can be null for explicit implementations, in that
6579 // case, we do not actually emit the ".property", so there is nowhere to
6580 // put the attribute
6582 if (PropertyBuilder != null) {
6583 if (OptAttributes != null)
6584 OptAttributes.Emit ();
6586 if (TypeManager.IsDynamicType (member_type))
6587 PredefinedAttributes.Get.Dynamic.EmitAttribute (PropertyBuilder);
6600 /// Tests whether accessors are not in collision with some method (CS0111)
6602 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6604 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6607 public override bool IsUsed
6613 return Get.IsUsed | Set.IsUsed;
6617 protected override void SetMemberName (MemberName new_name)
6619 base.SetMemberName (new_name);
6621 Get.UpdateName (this);
6622 Set.UpdateName (this);
6625 public override string[] ValidAttributeTargets {
6627 return attribute_targets;
6632 // Represents header string for documentation comment.
6634 public override string DocCommentHeader {
6635 get { return "P:"; }
6639 public class Property : PropertyBase
6641 public sealed class BackingField : Field
6643 readonly Property property;
6645 public BackingField (Property p)
6646 : base (p.Parent, p.type_name,
6647 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6648 new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
6653 public override string GetSignatureForError ()
6655 return property.GetSignatureForError ();
6659 const int AllowedModifiers =
6662 Modifiers.PROTECTED |
6663 Modifiers.INTERNAL |
6667 Modifiers.OVERRIDE |
6668 Modifiers.ABSTRACT |
6673 const int AllowedInterfaceModifiers =
6676 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6677 MemberName name, Attributes attrs, Accessor get_block,
6678 Accessor set_block, bool define_set_first)
6679 : this (parent, type, mod, name, attrs, get_block, set_block,
6680 define_set_first, null)
6684 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6685 MemberName name, Attributes attrs, Accessor get_block,
6686 Accessor set_block, bool define_set_first, Block current_block)
6687 : base (parent, type, mod,
6688 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
6689 name, attrs, define_set_first)
6691 if (get_block == null)
6692 Get = new GetMethod (this);
6694 Get = new GetMethod (this, get_block);
6696 if (set_block == null)
6697 Set = new SetMethod (this);
6699 Set = new SetMethod (this, set_block);
6701 if (!IsInterface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6702 get_block != null && get_block.Block == null &&
6703 set_block != null && set_block.Block == null) {
6704 if (RootContext.Version <= LanguageVersion.ISO_2)
6705 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6707 Get.ModFlags |= Modifiers.COMPILER_GENERATED;
6708 Set.ModFlags |= Modifiers.COMPILER_GENERATED;
6712 void CreateAutomaticProperty ()
6714 // Create backing field
6715 Field field = new BackingField (this);
6716 if (!field.Define ())
6719 Parent.PartialContainer.AddField (field);
6721 FieldExpr fe = new FieldExpr (field.FieldBuilder, Location);
6722 if ((field.ModFlags & Modifiers.STATIC) == 0)
6723 fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
6726 Get.Block = new ToplevelBlock (ParametersCompiled.EmptyReadOnlyParameters, Location);
6727 Return r = new Return (fe, Location);
6728 Get.Block.AddStatement (r);
6731 Set.Block = new ToplevelBlock (Set.ParameterInfo, Location);
6732 Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
6733 Set.Block.AddStatement (new StatementExpression (a));
6736 public override bool Define ()
6738 if (!base.Define ())
6741 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6743 if ((Get.ModFlags & Modifiers.COMPILER_GENERATED) != 0)
6744 CreateAutomaticProperty ();
6746 if (!DefineAccessors ())
6752 // FIXME - PropertyAttributes.HasDefault ?
6754 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6755 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6758 PropertyBuilder.SetGetMethod (GetBuilder);
6759 Parent.MemberCache.AddMember (GetBuilder, Get);
6763 PropertyBuilder.SetSetMethod (SetBuilder);
6764 Parent.MemberCache.AddMember (SetBuilder, Set);
6767 TypeManager.RegisterProperty (PropertyBuilder, this);
6768 Parent.MemberCache.AddMember (PropertyBuilder, this);
6772 public override void Emit ()
6774 if (((Set.ModFlags | Get.ModFlags) & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
6775 Report.Error (842, Location,
6776 "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
6777 GetSignatureForError ());
6783 protected override PropertyInfo ResolveBaseProperty ()
6785 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6786 Parent.TypeBuilder, Name, ParametersCompiled.EmptyReadOnlyParameters, null, true) as PropertyInfo;
6791 /// Gigantic workaround for lameness in SRE follows :
6792 /// This class derives from EventInfo and attempts to basically
6793 /// wrap around the EventBuilder so that FindMembers can quickly
6794 /// return this in it search for members
6796 public class MyEventBuilder : EventInfo {
6799 // We use this to "point" to our Builder which is
6800 // not really a MemberInfo
6802 EventBuilder MyBuilder;
6805 // We "catch" and wrap these methods
6807 MethodInfo raise, remove, add;
6809 EventAttributes attributes;
6810 Type declaring_type, reflected_type, event_type;
6815 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6817 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6819 // And now store the values in our own fields.
6821 declaring_type = type_builder;
6823 reflected_type = type_builder;
6825 attributes = event_attr;
6828 this.event_type = event_type;
6832 // Methods that you have to override. Note that you only need
6833 // to "implement" the variants that take the argument (those are
6834 // the "abstract" methods, the others (GetAddMethod()) are
6837 public override MethodInfo GetAddMethod (bool nonPublic)
6842 public override MethodInfo GetRemoveMethod (bool nonPublic)
6847 public override MethodInfo GetRaiseMethod (bool nonPublic)
6853 // These methods make "MyEventInfo" look like a Builder
6855 public void SetRaiseMethod (MethodBuilder raiseMethod)
6857 raise = raiseMethod;
6858 MyBuilder.SetRaiseMethod (raiseMethod);
6861 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6863 remove = removeMethod;
6864 MyBuilder.SetRemoveOnMethod (removeMethod);
6867 public void SetAddOnMethod (MethodBuilder addMethod)
6870 MyBuilder.SetAddOnMethod (addMethod);
6873 public void SetCustomAttribute (CustomAttributeBuilder cb)
6875 MyBuilder.SetCustomAttribute (cb);
6878 public override object [] GetCustomAttributes (bool inherit)
6880 // FIXME : There's nothing which can be seemingly done here because
6881 // we have no way of getting at the custom attribute objects of the
6886 public override object [] GetCustomAttributes (Type t, bool inherit)
6888 // FIXME : Same here !
6892 public override bool IsDefined (Type t, bool b)
6897 public override EventAttributes Attributes {
6903 public override string Name {
6909 public override Type DeclaringType {
6911 return declaring_type;
6915 public override Type ReflectedType {
6917 return reflected_type;
6921 public Type EventType {
6927 public void SetUsed ()
6929 if (my_event != null) {
6930 // my_event.SetAssigned ();
6931 my_event.SetMemberIsUsed ();
6937 /// For case when event is declared like property (with add and remove accessors).
6939 public class EventProperty: Event {
6940 abstract class AEventPropertyAccessor : AEventAccessor
6942 protected AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
6943 base (method, accessor, prefix)
6947 public override MethodBuilder Define (DeclSpace ds)
6949 CheckAbstractAndExtern (block != null);
6950 return base.Define (ds);
6953 public override string GetSignatureForError ()
6955 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
6959 sealed class AddDelegateMethod: AEventPropertyAccessor
6961 public AddDelegateMethod (Event method, Accessor accessor):
6962 base (method, accessor, AddPrefix)
6967 sealed class RemoveDelegateMethod: AEventPropertyAccessor
6969 public RemoveDelegateMethod (Event method, Accessor accessor):
6970 base (method, accessor, RemovePrefix)
6976 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6978 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
6980 Attributes attrs, Accessor add, Accessor remove)
6981 : base (parent, type, mod_flags, name, attrs)
6983 Add = new AddDelegateMethod (this, add);
6984 Remove = new RemoveDelegateMethod (this, remove);
6987 public override bool Define()
6989 if (!base.Define ())
6996 public override string[] ValidAttributeTargets {
6998 return attribute_targets;
7004 /// Event is declared like field.
7006 public class EventField : Event {
7007 abstract class EventFieldAccessor : AEventAccessor
7009 protected EventFieldAccessor (Event method, string prefix)
7010 : base (method, prefix)
7014 public override void Emit (DeclSpace parent)
7016 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
7017 if (parent is Class) {
7018 MethodBuilder mb = method_data.MethodBuilder;
7019 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7022 // TODO: because we cannot use generics yet
7023 FieldInfo field_info = ((EventField) method).BackingField.FieldBuilder;
7024 FieldExpr f_expr = new FieldExpr (field_info, Location);
7025 if ((method.ModFlags & Modifiers.STATIC) == 0)
7026 f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.FieldType, Location);
7028 block = new ToplevelBlock (ParameterInfo, Location);
7029 block.AddStatement (new StatementExpression (
7030 new CompoundAssign (Operation,
7032 block.GetParameterReference (ParameterInfo[0].Name, Location))));
7038 protected abstract Binary.Operator Operation { get; }
7041 sealed class AddDelegateMethod: EventFieldAccessor
7043 public AddDelegateMethod (Event method):
7044 base (method, AddPrefix)
7048 protected override Binary.Operator Operation {
7049 get { return Binary.Operator.Addition; }
7053 sealed class RemoveDelegateMethod: EventFieldAccessor
7055 public RemoveDelegateMethod (Event method):
7056 base (method, RemovePrefix)
7060 protected override Binary.Operator Operation {
7061 get { return Binary.Operator.Subtraction; }
7066 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7067 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7069 public Field BackingField;
7070 public Expression Initializer;
7072 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7073 : base (parent, type, mod_flags, name, attrs)
7075 Add = new AddDelegateMethod (this);
7076 Remove = new RemoveDelegateMethod (this);
7079 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7081 if (a.Target == AttributeTargets.Field) {
7082 BackingField.ApplyAttributeBuilder (a, cb, pa);
7086 if (a.Target == AttributeTargets.Method) {
7087 int errors = Report.Errors;
7088 Add.ApplyAttributeBuilder (a, cb, pa);
7089 if (errors == Report.Errors)
7090 Remove.ApplyAttributeBuilder (a, cb, pa);
7094 base.ApplyAttributeBuilder (a, cb, pa);
7097 public override bool Define()
7099 if (!base.Define ())
7102 if (Initializer != null && (ModFlags & Modifiers.ABSTRACT) != 0) {
7103 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7104 GetSignatureForError ());
7107 if (!HasBackingField) {
7112 // FIXME: We are unable to detect whether generic event is used because
7113 // we are using FieldExpr instead of EventExpr for event access in that
7114 // case. When this issue will be fixed this hack can be removed.
7115 if (TypeManager.IsGenericType (MemberType))
7118 if (Add.IsInterfaceImplementation)
7121 TypeManager.RegisterEventField (EventBuilder, this);
7123 BackingField = new Field (Parent,
7124 new TypeExpression (MemberType, Location),
7125 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
7128 Parent.PartialContainer.AddField (BackingField);
7129 BackingField.Initializer = Initializer;
7130 BackingField.ModFlags &= ~Modifiers.COMPILER_GENERATED;
7132 // Call define because we passed fields definition
7133 return BackingField.Define ();
7136 bool HasBackingField {
7138 return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
7142 public override string[] ValidAttributeTargets
7145 return HasBackingField ? attribute_targets : attribute_targets_interface;
7150 public abstract class Event : PropertyBasedMember {
7151 public abstract class AEventAccessor : AbstractPropertyEventMethod
7153 protected readonly Event method;
7154 ImplicitParameter param_attr;
7156 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7158 public const string AddPrefix = "add_";
7159 public const string RemovePrefix = "remove_";
7161 protected AEventAccessor (Event method, string prefix)
7162 : base (method, prefix)
7164 this.method = method;
7165 this.ModFlags = method.ModFlags;
7168 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7169 : base (method, accessor, prefix)
7171 this.method = method;
7172 this.ModFlags = method.ModFlags;
7175 public bool IsInterfaceImplementation {
7176 get { return method_data.implementing != null; }
7179 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7181 if (a.IsInternalMethodImplAttribute) {
7182 method.is_external_implementation = true;
7185 base.ApplyAttributeBuilder (a, cb, pa);
7188 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7190 if (a.Target == AttributeTargets.Parameter) {
7191 if (param_attr == null)
7192 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7194 param_attr.ApplyAttributeBuilder (a, cb, pa);
7198 base.ApplyAttributeBuilder (a, cb, pa);
7201 public override AttributeTargets AttributeTargets {
7203 return AttributeTargets.Method;
7207 public override bool IsClsComplianceRequired ()
7209 return method.IsClsComplianceRequired ();
7212 public virtual MethodBuilder Define (DeclSpace parent)
7214 method_data = new MethodData (method, method.ModFlags,
7215 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7217 if (!method_data.Define (parent, method.GetFullName (MemberName)))
7220 MethodBuilder mb = method_data.MethodBuilder;
7221 ParameterInfo.ApplyAttributes (mb);
7225 public override Type ReturnType {
7227 return TypeManager.void_type;
7231 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7233 return new EmitContext (
7234 this, method.Parent, Location, ig, ReturnType,
7235 method.ModFlags, false);
7238 public override ObsoleteAttribute GetObsoleteAttribute ()
7240 return method.GetObsoleteAttribute ();
7243 public override string[] ValidAttributeTargets {
7245 return attribute_targets;
7249 public override ParametersCompiled ParameterInfo {
7251 return method.parameters;
7257 const int AllowedModifiers =
7260 Modifiers.PROTECTED |
7261 Modifiers.INTERNAL |
7266 Modifiers.OVERRIDE |
7268 Modifiers.ABSTRACT |
7271 const int AllowedInterfaceModifiers =
7274 public AEventAccessor Add, Remove;
7275 public MyEventBuilder EventBuilder;
7276 public MethodBuilder AddBuilder, RemoveBuilder;
7278 ParametersCompiled parameters;
7280 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7281 : base (parent, null, type, mod_flags,
7282 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7287 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7289 if ((a.HasSecurityAttribute)) {
7290 a.Error_InvalidSecurityParent ();
7294 EventBuilder.SetCustomAttribute (cb);
7297 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7299 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7302 public override AttributeTargets AttributeTargets {
7304 return AttributeTargets.Event;
7308 public override bool Define ()
7310 if (!base.Define ())
7313 if (!TypeManager.IsDelegateType (MemberType)) {
7314 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7317 parameters = ParametersCompiled.CreateFullyResolved (
7318 new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
7323 if (TypeManager.delegate_combine_delegate_delegate == null) {
7324 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7325 TypeManager.delegate_type, "Combine", Location,
7326 TypeManager.delegate_type, TypeManager.delegate_type);
7328 if (TypeManager.delegate_remove_delegate_delegate == null) {
7329 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7330 TypeManager.delegate_type, "Remove", Location,
7331 TypeManager.delegate_type, TypeManager.delegate_type);
7335 // Now define the accessors
7338 AddBuilder = Add.Define (Parent);
7339 if (AddBuilder == null)
7342 RemoveBuilder = Remove.Define (Parent);
7343 if (RemoveBuilder == null)
7346 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7347 EventBuilder.SetAddOnMethod (AddBuilder);
7348 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7350 Parent.MemberCache.AddMember (EventBuilder, this);
7351 Parent.MemberCache.AddMember (AddBuilder, Add);
7352 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7357 public override void Emit ()
7359 if (OptAttributes != null) {
7360 OptAttributes.Emit ();
7364 Remove.Emit (Parent);
7369 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7371 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7372 Parent.TypeBuilder, Name);
7377 AParametersCollection pd = TypeManager.GetParameterData (mi);
7378 base_ret_type = pd.Types [0];
7383 // Represents header string for documentation comment.
7385 public override string DocCommentHeader {
7386 get { return "E:"; }
7391 public class Indexer : PropertyBase
7393 public class GetIndexerMethod : GetMethod
7395 ParametersCompiled parameters;
7397 public GetIndexerMethod (Indexer method):
7400 this.parameters = method.parameters;
7403 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7404 base (method, accessor)
7406 parameters = accessor.Parameters;
7409 public override MethodBuilder Define (DeclSpace parent)
7411 parameters.Resolve (ResolveContext);
7412 return base.Define (parent);
7415 public override bool EnableOverloadChecks (MemberCore overload)
7417 if (base.EnableOverloadChecks (overload)) {
7418 overload.caching_flags |= Flags.MethodOverloadsExist;
7425 public override ParametersCompiled ParameterInfo {
7432 public class SetIndexerMethod: SetMethod
7434 public SetIndexerMethod (Indexer method):
7437 parameters = ParametersCompiled.MergeGenerated (method.parameters, false, parameters [0], null);
7440 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7441 base (method, accessor)
7443 parameters = method.Get.IsDummy ? accessor.Parameters : accessor.Parameters.Clone ();
7446 public override bool EnableOverloadChecks (MemberCore overload)
7448 if (base.EnableOverloadChecks (overload)) {
7449 overload.caching_flags |= Flags.MethodOverloadsExist;
7457 const int AllowedModifiers =
7460 Modifiers.PROTECTED |
7461 Modifiers.INTERNAL |
7465 Modifiers.OVERRIDE |
7470 const int AllowedInterfaceModifiers =
7473 public readonly ParametersCompiled parameters;
7475 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7476 ParametersCompiled parameters, Attributes attrs,
7477 Accessor get_block, Accessor set_block, bool define_set_first)
7478 : base (parent, type, mod,
7479 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7480 name, attrs, define_set_first)
7482 this.parameters = parameters;
7484 if (get_block == null)
7485 Get = new GetIndexerMethod (this);
7487 Get = new GetIndexerMethod (this, get_block);
7489 if (set_block == null)
7490 Set = new SetIndexerMethod (this);
7492 Set = new SetIndexerMethod (this, set_block);
7495 protected override bool CheckForDuplications ()
7497 return Parent.MemberCache.CheckExistingMembersOverloads (this, GetFullName (MemberName), parameters);
7500 public override bool Define ()
7502 if (!base.Define ())
7505 if (!DefineParameters (parameters))
7508 if (OptAttributes != null) {
7509 Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
7510 if (indexer_attr != null) {
7511 // Remove the attribute from the list because it is not emitted
7512 OptAttributes.Attrs.Remove (indexer_attr);
7514 string name = indexer_attr.GetIndexerAttributeValue ();
7520 if (IsExplicitImpl) {
7521 Report.Error (415, indexer_attr.Location,
7522 "The `IndexerName' attribute is valid only on an " +
7523 "indexer that is not an explicit interface member declaration");
7527 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7528 Report.Error (609, indexer_attr.Location,
7529 "Cannot set the `IndexerName' attribute on an indexer marked override");
7535 if (InterfaceType != null) {
7536 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7537 if (base_IndexerName != Name)
7538 ShortName = base_IndexerName;
7541 if (!Parent.PartialContainer.AddMember (this) ||
7542 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7545 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7547 if (!DefineAccessors ())
7554 // Now name the parameters
7556 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7557 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.GetEmitTypes ());
7560 PropertyBuilder.SetGetMethod (GetBuilder);
7561 Parent.MemberCache.AddMember (GetBuilder, Get);
7565 PropertyBuilder.SetSetMethod (SetBuilder);
7566 Parent.MemberCache.AddMember (SetBuilder, Set);
7569 TypeManager.RegisterIndexer (PropertyBuilder, parameters);
7570 Parent.MemberCache.AddMember (PropertyBuilder, this);
7574 public override bool EnableOverloadChecks (MemberCore overload)
7576 if (overload is Indexer) {
7577 caching_flags |= Flags.MethodOverloadsExist;
7581 return base.EnableOverloadChecks (overload);
7584 public override string GetDocCommentName (DeclSpace ds)
7586 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7589 public override string GetSignatureForError ()
7591 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7592 if (MemberName.Left != null) {
7594 sb.Append (MemberName.Left.GetSignatureForError ());
7597 sb.Append (".this");
7598 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7599 return sb.ToString ();
7602 protected override PropertyInfo ResolveBaseProperty ()
7604 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7605 Parent.TypeBuilder, Name, parameters, null, true) as PropertyInfo;
7608 protected override bool VerifyClsCompliance ()
7610 if (!base.VerifyClsCompliance ())
7613 parameters.VerifyClsCompliance ();
7618 public class Operator : MethodOrOperator {
7620 const int AllowedModifiers =
7626 public enum OpType : byte {
7636 // Unary and Binary operators
7659 // Implicit and Explicit
7663 // Just because of enum
7667 public readonly OpType OperatorType;
7669 static readonly string [] [] names;
7673 names = new string[(int)OpType.TOP][];
7674 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7675 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7676 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7677 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7678 names [(int) OpType.True] = new string [] { "true", "op_True" };
7679 names [(int) OpType.False] = new string [] { "false", "op_False" };
7680 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7681 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7682 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7683 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7684 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7685 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7686 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7687 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7688 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7689 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7690 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7691 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7692 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7693 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7694 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7695 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7696 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7697 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7698 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7699 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7702 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7703 int mod_flags, ParametersCompiled parameters,
7704 ToplevelBlock block, Attributes attrs, Location loc)
7705 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
7706 new MemberName (GetMetadataName (type), loc), attrs, parameters)
7708 OperatorType = type;
7712 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7714 if (a.Type == pa.Conditional) {
7715 Error_ConditionalAttributeIsNotValid ();
7719 base.ApplyAttributeBuilder (a, cb, pa);
7722 public override bool Define ()
7724 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7725 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7726 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7729 if (!base.Define ())
7732 // imlicit and explicit operator of same types are not allowed
7733 if (OperatorType == OpType.Explicit)
7734 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
7735 else if (OperatorType == OpType.Implicit)
7736 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
7738 Type declaring_type = MethodData.DeclaringType;
7739 Type return_type = MemberType;
7740 Type first_arg_type = ParameterTypes [0];
7742 Type first_arg_type_unwrap = first_arg_type;
7743 if (TypeManager.IsNullableType (first_arg_type))
7744 first_arg_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (first_arg_type) [0]);
7746 Type return_type_unwrap = return_type;
7747 if (TypeManager.IsNullableType (return_type))
7748 return_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (return_type) [0]);
7751 // Rules for conversion operators
7753 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7754 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7755 Report.Error (555, Location,
7756 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7761 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7762 conv_type = first_arg_type;
7763 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7764 conv_type = return_type;
7766 Report.Error (556, Location,
7767 "User-defined conversion must convert to or from the enclosing type");
7772 // Because IsInterface and IsClass are not supported
7774 if (!TypeManager.IsGenericParameter (conv_type)) {
7775 if (conv_type.IsInterface) {
7776 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7777 GetSignatureForError ());
7781 if (conv_type.IsClass) {
7782 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7783 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7784 GetSignatureForError ());
7788 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7789 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7790 GetSignatureForError ());
7795 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7796 if (first_arg_type != declaring_type || Parameters.Types [1] != TypeManager.int32_type) {
7797 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");
7800 } else if (Parameters.Count == 1) {
7801 // Checks for Unary operators
7803 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7804 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7805 Report.Error (448, Location,
7806 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7809 if (first_arg_type != declaring_type) {
7811 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7816 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7817 Report.Error (562, Location,
7818 "The parameter type of a unary operator must be the containing type");
7822 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7823 if (return_type != TypeManager.bool_type){
7826 "The return type of operator True or False " +
7833 // Checks for Binary operators
7835 if (first_arg_type != declaring_type &&
7836 Parameters.Types [1] != declaring_type){
7839 "One of the parameters of a binary operator must " +
7840 "be the containing type");
7848 protected override bool ResolveMemberType ()
7850 if (!base.ResolveMemberType ())
7853 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7857 // Operator cannot be override
7858 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7863 public static string GetName (OpType ot)
7865 return names [(int) ot] [0];
7868 public static string GetName (string metadata_name)
7870 for (int i = 0; i < names.Length; ++i) {
7871 if (names [i] [1] == metadata_name)
7872 return names [i] [0];
7877 public static string GetMetadataName (OpType ot)
7879 return names [(int) ot] [1];
7882 public static string GetMetadataName (string name)
7884 for (int i = 0; i < names.Length; ++i) {
7885 if (names [i] [0] == name)
7886 return names [i] [1];
7891 public OpType GetMatchingOperator ()
7893 switch (OperatorType) {
7894 case OpType.Equality:
7895 return OpType.Inequality;
7896 case OpType.Inequality:
7897 return OpType.Equality;
7899 return OpType.False;
7902 case OpType.GreaterThan:
7903 return OpType.LessThan;
7904 case OpType.LessThan:
7905 return OpType.GreaterThan;
7906 case OpType.GreaterThanOrEqual:
7907 return OpType.LessThanOrEqual;
7908 case OpType.LessThanOrEqual:
7909 return OpType.GreaterThanOrEqual;
7915 public override string GetSignatureForError ()
7917 StringBuilder sb = new StringBuilder ();
7918 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7919 sb.AppendFormat ("{0}.{1} operator {2}",
7920 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
7923 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7926 sb.Append (Parameters.GetSignatureForError ());
7927 return sb.ToString ();
7932 // This is used to compare method signatures
7934 struct MethodSignature {
7936 public Type RetType;
7937 public Type [] Parameters;
7940 /// This delegate is used to extract methods which have the
7941 /// same signature as the argument
7943 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7945 public MethodSignature (string name, Type ret_type, Type [] parameters)
7950 if (parameters == null)
7951 Parameters = Type.EmptyTypes;
7953 Parameters = parameters;
7956 public override string ToString ()
7959 if (Parameters.Length != 0){
7960 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7961 for (int i = 0; i < Parameters.Length; i++){
7962 sb.Append (Parameters [i]);
7963 if (i+1 < Parameters.Length)
7966 pars = sb.ToString ();
7969 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7972 public override int GetHashCode ()
7974 return Name.GetHashCode ();
7977 public override bool Equals (Object o)
7979 MethodSignature other = (MethodSignature) o;
7981 if (other.Name != Name)
7984 if (other.RetType != RetType)
7987 if (Parameters == null){
7988 if (other.Parameters == null)
7993 if (other.Parameters == null)
7996 int c = Parameters.Length;
7997 if (other.Parameters.Length != c)
8000 for (int i = 0; i < c; i++)
8001 if (other.Parameters [i] != Parameters [i])
8007 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8009 MethodSignature sig = (MethodSignature) filter_criteria;
8011 if (m.Name != sig.Name)
8015 MethodInfo mi = m as MethodInfo;
8016 PropertyInfo pi = m as PropertyInfo;
8019 ReturnType = mi.ReturnType;
8020 else if (pi != null)
8021 ReturnType = pi.PropertyType;
8026 // we use sig.RetType == null to mean `do not check the
8027 // method return value.
8029 if (sig.RetType != null) {
8030 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8036 args = TypeManager.GetParameterData (mi).Types;
8038 args = TypeManager.GetParameterData (pi).Types;
8039 Type [] sigp = sig.Parameters;
8041 if (args.Length != sigp.Length)
8044 for (int i = args.Length - 1; i >= 0; i--)
8045 if (!TypeManager.IsEqual (args [i], sigp [i]))