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
14 // 2002-10-11 Miguel de Icaza <miguel@ximian.com>
16 // * class.cs: Following the comment from 2002-09-26 to AddMethod, I
17 // have fixed a remaining problem: not every AddXXXX was adding a
18 // fully qualified name.
20 // Now everyone registers a fully qualified name in the DeclSpace as
21 // being defined instead of the partial name.
23 // Downsides: we are slower than we need to be due to the excess
24 // copies and the names being registered this way.
26 // The reason for this is that we currently depend (on the corlib
27 // bootstrap for instance) that types are fully qualified, because
28 // we dump all the types in the namespace, and we should really have
29 // types inserted into the proper namespace, so we can only store the
30 // basenames in the defined_names array.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Security;
41 using System.Security.Permissions;
44 #if BOOTSTRAP_WITH_OLDLIB || NET_2_1
45 using XmlElement = System.Object;
50 using Mono.CompilerServices.SymbolWriter;
52 namespace Mono.CSharp {
64 /// This is the base class for structs and classes.
66 public abstract class TypeContainer : DeclSpace, IMemberContainer {
68 public class MemberCoreArrayList: ArrayList
71 /// Defines the MemberCore objects that are in this array
73 public virtual void DefineContainerMembers ()
75 foreach (MemberCore mc in this) {
78 } catch (Exception e) {
79 throw new InternalErrorException (mc, e);
84 public virtual void Emit ()
86 foreach (MemberCore mc in this)
91 public class OperatorArrayList: MemberCoreArrayList
93 TypeContainer container;
95 public OperatorArrayList (TypeContainer container)
97 this.container = container;
101 // Checks that some operators come in pairs:
107 // They are matched based on the return type and the argument types
109 void CheckPairedOperators ()
111 bool has_equality_or_inequality = false;
112 Operator[] operators = (Operator[]) ToArray (typeof (Operator));
113 bool [] has_pair = new bool [operators.Length];
115 for (int i = 0; i < Count; ++i) {
116 if (operators [i] == null)
119 Operator o_a = operators [i];
120 Operator.OpType o_type = o_a.OperatorType;
121 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
122 has_equality_or_inequality = true;
124 Operator.OpType matching_type = o_a.GetMatchingOperator ();
125 if (matching_type == Operator.OpType.TOP) {
126 operators [i] = null;
130 for (int ii = 0; ii < Count; ++ii) {
131 Operator o_b = operators [ii];
132 if (o_b == null || o_b.OperatorType != matching_type)
135 if (!TypeManager.IsEqual (o_a.ReturnType, o_b.ReturnType))
138 if (!TypeManager.IsEqual (o_a.ParameterTypes, o_b.ParameterTypes))
141 operators [i] = null;
144 // Used to ignore duplicate user conversions
146 has_pair [ii] = true;
150 for (int i = 0; i < Count; ++i) {
151 if (operators [i] == null || has_pair [i])
154 Operator o = operators [i];
155 Report.Error (216, o.Location,
156 "The operator `{0}' requires a matching operator `{1}' to also be defined",
157 o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
160 if (has_equality_or_inequality && Report.WarningLevel > 2) {
161 if (container.Methods == null || !container.HasEquals)
162 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
164 if (container.Methods == null || !container.HasGetHashCode)
165 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
169 public override void DefineContainerMembers ()
171 base.DefineContainerMembers ();
172 CheckPairedOperators ();
180 GetHashCode = 1 << 1,
181 HasStaticFieldInitializer = 1 << 2
185 // Whether this is a struct, class or interface
186 public readonly Kind Kind;
188 // Holds a list of classes and structures
189 protected ArrayList types;
191 MemberCoreArrayList ordered_explicit_member_list;
192 MemberCoreArrayList ordered_member_list;
194 // Holds the list of properties
195 MemberCoreArrayList properties;
197 // Holds the list of delegates
198 MemberCoreArrayList delegates;
200 // Holds the list of constructors
201 protected MemberCoreArrayList instance_constructors;
203 // Holds the list of fields
204 MemberCoreArrayList fields;
206 // Holds a list of fields that have initializers
207 protected ArrayList initialized_fields;
209 // Holds a list of static fields that have initializers
210 protected ArrayList initialized_static_fields;
212 // Holds the list of constants
213 protected MemberCoreArrayList constants;
215 // Holds the methods.
216 MemberCoreArrayList methods;
219 protected MemberCoreArrayList events;
221 // Holds the indexers
224 // Holds the operators
225 MemberCoreArrayList operators;
227 // Holds the compiler generated classes
228 ArrayList compiler_generated;
231 // Pointers to the default constructor and the default static constructor
233 protected Constructor default_constructor;
234 protected Constructor default_static_constructor;
237 // Points to the first non-static field added to the container.
239 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
240 // and the first one's as good as any.
242 FieldBase first_nonstatic_field = null;
245 // This one is computed after we can distinguish interfaces
246 // from classes from the arraylist `type_bases'
249 TypeExpr[] iface_exprs;
252 protected ArrayList type_bases;
254 bool members_resolved;
255 bool members_resolved_ok;
256 protected bool members_defined;
257 bool members_defined_ok;
259 // The interfaces we implement.
260 protected Type[] ifaces;
262 // The base member cache and our member cache
263 MemberCache base_cache;
264 protected MemberCache member_cache;
266 public const string DefaultIndexerName = "Item";
268 private bool seen_normal_indexers = false;
269 private string indexer_name = DefaultIndexerName;
271 private CachedMethods cached_method;
273 ArrayList partial_parts;
276 /// The pending methods that need to be implemented
277 // (interfaces or abstract methods)
279 PendingImplementation pending;
281 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
282 Attributes attrs, Kind kind)
283 : base (ns, parent, name, attrs)
285 if (parent != null && parent.NamespaceEntry != ns)
286 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
289 this.PartialContainer = this;
292 public bool AddMember (MemberCore symbol)
294 return AddToContainer (symbol, symbol.MemberName.MethodName);
297 protected virtual bool AddMemberType (DeclSpace ds)
299 return AddToContainer (ds, ds.Basename);
302 protected virtual void RemoveMemberType (DeclSpace ds)
304 RemoveFromContainer (ds.Basename);
307 public void AddConstant (Const constant)
309 if (!AddMember (constant))
312 if (constants == null)
313 constants = new MemberCoreArrayList ();
315 constants.Add (constant);
318 public TypeContainer AddTypeContainer (TypeContainer tc)
320 if (!AddMemberType (tc))
324 types = new MemberCoreArrayList ();
330 public virtual TypeContainer AddPartial (TypeContainer next_part)
332 return AddPartial (next_part, next_part.Basename);
335 protected TypeContainer AddPartial (TypeContainer next_part, string name)
337 next_part.ModFlags |= Modifiers.PARTIAL;
338 TypeContainer tc = defined_names [name] as TypeContainer;
341 return AddTypeContainer (next_part);
343 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
344 Report.SymbolRelatedToPreviousError (next_part);
345 Error_MissingPartialModifier (tc);
348 if (tc.Kind != next_part.Kind) {
349 Report.SymbolRelatedToPreviousError (tc);
350 Report.Error (261, next_part.Location,
351 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
352 next_part.GetSignatureForError ());
355 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
356 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
357 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
358 Report.SymbolRelatedToPreviousError (tc);
359 Report.Error (262, next_part.Location,
360 "Partial declarations of `{0}' have conflicting accessibility modifiers",
361 next_part.GetSignatureForError ());
364 if (tc.partial_parts == null)
365 tc.partial_parts = new ArrayList (1);
367 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
368 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
369 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
370 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
371 tc.ModFlags |= next_part.ModFlags;
373 tc.ModFlags |= next_part.ModFlags;
376 if (next_part.attributes != null) {
377 if (tc.attributes == null)
378 tc.attributes = next_part.attributes;
380 tc.attributes.AddAttributes (next_part.attributes.Attrs);
383 next_part.PartialContainer = tc;
384 tc.partial_parts.Add (next_part);
388 public virtual void RemoveTypeContainer (TypeContainer next_part)
391 types.Remove (next_part);
392 RemoveMemberType (next_part);
395 public void AddDelegate (Delegate d)
397 if (!AddMemberType (d))
400 if (delegates == null)
401 delegates = new MemberCoreArrayList ();
406 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
408 if (ordered_explicit_member_list == null) {
409 ordered_explicit_member_list = new MemberCoreArrayList ();
410 ordered_member_list = new MemberCoreArrayList ();
414 if (Kind == Kind.Interface) {
415 Report.Error (541, mc.Location,
416 "`{0}': explicit interface declaration can only be declared in a class or struct",
417 mc.GetSignatureForError ());
420 ordered_explicit_member_list.Add (mc);
421 alist.Insert (0, mc);
423 ordered_member_list.Add (mc);
429 public void AddMethod (Method method)
431 MemberName mn = method.MemberName;
432 if (!AddToContainer (method, mn.IsGeneric ? mn.Basename : mn.MethodName))
436 methods = new MemberCoreArrayList ();
438 if (method.MemberName.Left != null)
439 AddMemberToList (method, methods, true);
441 AddMemberToList (method, methods, false);
444 public void AddConstructor (Constructor c)
446 if (c.Name != MemberName.Name) {
447 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
450 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
451 if (!AddToContainer (c, is_static ?
452 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
455 if (is_static && c.Parameters.IsEmpty){
456 if (default_static_constructor != null) {
457 Report.SymbolRelatedToPreviousError (default_static_constructor);
458 Report.Error (111, c.Location,
459 "A member `{0}' is already defined. Rename this member or use different parameter types",
460 c.GetSignatureForError ());
464 default_static_constructor = c;
466 if (c.Parameters.IsEmpty)
467 default_constructor = c;
469 if (instance_constructors == null)
470 instance_constructors = new MemberCoreArrayList ();
472 instance_constructors.Add (c);
476 public bool AddField (FieldBase field)
478 if (!AddMember (field))
482 fields = new MemberCoreArrayList ();
486 if ((field.ModFlags & Modifiers.STATIC) != 0)
489 if (first_nonstatic_field == null) {
490 first_nonstatic_field = field;
494 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
495 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
496 Report.Warning (282, 3, field.Location,
497 "struct instance field `{0}' found in different declaration from instance field `{1}'",
498 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
503 public void AddProperty (Property prop)
505 if (!AddMember (prop) ||
506 !AddMember (prop.Get) || !AddMember (prop.Set))
509 if (properties == null)
510 properties = new MemberCoreArrayList ();
512 if (prop.MemberName.Left != null)
513 AddMemberToList (prop, properties, true);
515 AddMemberToList (prop, properties, false);
518 public void AddEvent (Event e)
523 if (e is EventProperty) {
524 if (!AddMember (e.Add))
527 if (!AddMember (e.Remove))
532 events = new MemberCoreArrayList ();
538 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
540 public void AddIndexer (Indexer i)
542 if (indexers == null)
543 indexers = new ArrayList ();
545 if (i.IsExplicitImpl)
546 AddMemberToList (i, indexers, true);
548 AddMemberToList (i, indexers, false);
551 public void AddOperator (Operator op)
556 if (operators == null)
557 operators = new OperatorArrayList (this);
562 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
564 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
566 if (compiler_generated == null)
567 compiler_generated = new ArrayList ();
569 compiler_generated.Add (c);
572 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
574 if (a.Type == TypeManager.default_member_type) {
575 if (Indexers != null) {
576 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
581 base.ApplyAttributeBuilder (a, cb);
584 public override AttributeTargets AttributeTargets {
586 throw new NotSupportedException ();
590 public ArrayList Types {
596 public MemberCoreArrayList Methods {
602 public ArrayList Constants {
608 protected Type BaseType {
610 return TypeBuilder.BaseType;
614 public ArrayList Fields {
620 public ArrayList InstanceConstructors {
622 return instance_constructors;
626 public ArrayList Properties {
632 public ArrayList Events {
638 public ArrayList Indexers {
644 public ArrayList Operators {
650 public ArrayList Delegates {
656 protected override TypeAttributes TypeAttr {
658 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
662 public string IndexerName {
664 return indexers == null ? DefaultIndexerName : indexer_name;
668 public bool IsComImport {
670 if (OptAttributes == null || TypeManager.comimport_attr_type == null)
673 return OptAttributes.Contains (TypeManager.comimport_attr_type);
677 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
679 if ((field.ModFlags & Modifiers.STATIC) != 0){
680 if (initialized_static_fields == null) {
681 PartialContainer.HasStaticFieldInitializer = true;
682 initialized_static_fields = new ArrayList (4);
685 initialized_static_fields.Add (expression);
687 if (initialized_fields == null)
688 initialized_fields = new ArrayList (4);
690 initialized_fields.Add (expression);
694 public void ResolveFieldInitializers (EmitContext ec)
696 // Field initializers are tricky for partial classes. They have to
697 // share same costructor (block) but they have they own resolve scope.
698 DeclSpace orig = ec.DeclContainer;
700 if (partial_parts != null) {
701 foreach (TypeContainer part in partial_parts) {
702 ec.DeclContainer = part;
703 part.DoResolveFieldInitializers (ec);
706 ec.DeclContainer = PartialContainer;
707 DoResolveFieldInitializers (ec);
708 ec.DeclContainer = orig;
711 void DoResolveFieldInitializers (EmitContext ec)
714 if (initialized_static_fields == null)
717 bool has_complex_initializer = !RootContext.Optimize;
718 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
720 ExpressionStatement[] init = new ExpressionStatement [initialized_static_fields.Count];
721 for (i = 0; i < initialized_static_fields.Count; ++i) {
722 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
723 ExpressionStatement s = fi.ResolveStatement (ec);
725 s = EmptyExpressionStatement.Instance;
726 } else if (fi.IsComplexInitializer) {
727 has_complex_initializer |= true;
733 for (i = 0; i < initialized_static_fields.Count; ++i) {
734 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
736 // Need special check to not optimize code like this
737 // static int a = b = 5;
740 if (!has_complex_initializer && fi.IsDefaultInitializer)
743 ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
750 if (initialized_fields == null)
753 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
754 for (int i = 0; i < initialized_fields.Count; ++i) {
755 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
756 ExpressionStatement s = fi.ResolveStatement (ec);
761 // Field is re-initialized to its default value => removed
763 if (fi.IsDefaultInitializer && RootContext.Optimize)
766 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
772 // Emits the instance field initializers
774 public bool EmitFieldInitializers (EmitContext ec)
776 if (partial_parts != null) {
777 foreach (TypeContainer part in partial_parts)
778 part.EmitFieldInitializers (ec);
784 fields = initialized_static_fields;
786 fields = initialized_fields;
792 foreach (FieldInitializer f in fields) {
793 f.EmitStatement (ec);
798 public override string DocComment {
810 public PendingImplementation PendingImplementations {
811 get { return pending; }
814 public override bool GetClsCompliantAttributeValue ()
816 if (PartialContainer != this)
817 return PartialContainer.GetClsCompliantAttributeValue ();
819 return base.GetClsCompliantAttributeValue ();
822 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
824 // FIXME: get rid of partial_parts and store lists of bases of each part here
825 // assumed, not verified: 'part' is in 'partial_parts'
826 ((TypeContainer) part).type_bases = bases;
830 /// This function computes the Base class and also the
831 /// list of interfaces that the class or struct @c implements.
833 /// The return value is an array (might be null) of
834 /// interfaces implemented (as Types).
836 /// The @base_class argument is set to the base object or null
837 /// if this is `System.Object'.
839 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
842 if (type_bases == null)
845 int count = type_bases.Count;
846 TypeExpr [] ifaces = null;
847 for (int i = 0, j = 0; i < count; i++){
848 FullNamedExpression fne = (FullNamedExpression) type_bases [i];
851 // Standard ResolveAsTypeTerminal cannot be used in this case because
852 // it does ObsoleteAttribute and constraint checks which require
853 // base type to be set
855 TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (this, false);
856 if (fne_resolved == null)
859 if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
860 base_class = fne_resolved;
865 ifaces = new TypeExpr [count - i];
867 if (fne_resolved.Type.IsInterface) {
868 for (int ii = 0; ii < j; ++ii) {
869 if (TypeManager.IsEqual (fne_resolved.Type, ifaces [ii].Type)) {
870 Report.Error (528, Location, "`{0}' is already listed in interface list",
871 fne_resolved.GetSignatureForError ());
876 if (Kind == Kind.Interface && !fne_resolved.AsAccessible (this)) {
877 Report.Error (61, fne.Location,
878 "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
879 fne_resolved.GetSignatureForError (), GetSignatureForError ());
882 Report.SymbolRelatedToPreviousError (fne_resolved.Type);
883 if (Kind != Kind.Class) {
884 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
885 } else if (base_class != null)
886 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
887 GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
889 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
890 GetSignatureForError (), fne_resolved.GetSignatureForError ());
894 ifaces [j++] = fne_resolved;
900 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
902 ArrayList ifaces = new ArrayList (0);
903 if (iface_exprs != null)
904 ifaces.AddRange (iface_exprs);
906 foreach (TypeContainer part in partial_parts) {
907 TypeExpr new_base_class;
908 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
909 if (new_base_class != TypeManager.system_object_expr) {
910 if (base_class == TypeManager.system_object_expr)
911 base_class = new_base_class;
913 if (new_base_class != null && !new_base_class.Equals (base_class)) {
914 Report.SymbolRelatedToPreviousError (base_class.Location, "");
915 Report.Error (263, part.Location,
916 "Partial declarations of `{0}' must not specify different base classes",
917 part.GetSignatureForError ());
924 if (new_ifaces == null)
927 foreach (TypeExpr iface in new_ifaces) {
928 if (ifaces.Contains (iface))
935 if (ifaces.Count == 0)
938 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
941 bool CheckGenericInterfaces (Type[] ifaces)
944 ArrayList already_checked = new ArrayList ();
946 for (int i = 0; i < ifaces.Length; i++) {
947 Type iface = ifaces [i];
948 foreach (Type t in already_checked) {
952 Type[] inferred = new Type [CountTypeParameters];
953 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
956 Report.Error (695, Location,
957 "`{0}' cannot implement both `{1}' and `{2}' " +
958 "because they may unify for some type parameter substitutions",
959 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
960 TypeManager.CSharpName (t));
964 already_checked.Add (iface);
973 bool CreateTypeBuilder ()
976 Type default_parent = null;
977 if (Kind == Kind.Struct)
978 default_parent = TypeManager.value_type;
979 else if (Kind == Kind.Enum)
980 default_parent = TypeManager.enum_type;
983 // Sets .size to 1 for structs with no instance fields
985 int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
988 if (TypeManager.NamespaceClash (Name, Location)) {
992 ModuleBuilder builder = CodeGen.Module.Builder;
993 TypeBuilder = builder.DefineType (
994 Name, TypeAttr, default_parent, type_size);
996 TypeBuilder builder = Parent.TypeBuilder;
998 TypeBuilder = builder.DefineNestedType (
999 Basename, TypeAttr, default_parent, type_size);
1001 } catch (ArgumentException) {
1002 Report.RuntimeMissingSupport (Location, "static classes");
1006 TypeManager.AddUserType (this);
1010 string[] param_names = new string [TypeParameters.Length];
1011 for (int i = 0; i < TypeParameters.Length; i++)
1012 param_names [i] = TypeParameters [i].Name;
1014 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
1016 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1017 for (int i = offset; i < gen_params.Length; i++)
1018 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1025 bool DefineBaseTypes ()
1027 iface_exprs = ResolveBaseTypes (out base_type);
1028 if (partial_parts != null) {
1029 iface_exprs = GetNormalPartialBases (ref base_type);
1033 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1034 // which in turn should have called DefineType()s on base types if necessary.
1036 // None of the code below should trigger DefineType()s on classes that we depend on.
1037 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1039 // Let's do it as soon as possible, since code below can call DefineType() on classes
1040 // that depend on us to be populated before they are.
1042 if (!(this is CompilerGeneratedClass))
1043 RootContext.RegisterOrder (this);
1045 if (!CheckRecursiveDefinition (this))
1048 if (base_type != null && base_type.Type != null) {
1049 TypeBuilder.SetParent (base_type.Type);
1052 // add interfaces that were not added at type creation
1053 if (iface_exprs != null) {
1054 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1058 foreach (Type itype in ifaces)
1059 TypeBuilder.AddInterfaceImplementation (itype);
1061 if (!CheckGenericInterfaces (ifaces))
1064 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1071 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1073 public TypeBuilder CreateType ()
1075 if (TypeBuilder != null)
1081 if (!CreateTypeBuilder ()) {
1086 if (partial_parts != null) {
1087 foreach (TypeContainer part in partial_parts)
1088 part.TypeBuilder = TypeBuilder;
1091 if (Types != null) {
1092 foreach (TypeContainer tc in Types) {
1093 if (tc.CreateType () == null) {
1105 public override TypeBuilder DefineType ()
1112 type_defined = true;
1114 if (CreateType () == null) {
1119 if (!DefineBaseTypes ()) {
1124 if (!ResolveMembers ()) {
1129 if (!DefineNestedTypes ()) {
1137 bool ResolveMembers ()
1139 if (members_resolved)
1140 return members_resolved_ok;
1142 members_resolved_ok = DoResolveMembers ();
1143 members_resolved = true;
1145 return members_resolved_ok;
1148 protected virtual bool DoResolveMembers ()
1150 if (methods != null) {
1151 foreach (Method method in methods) {
1152 if (!method.ResolveMembers ())
1157 // TODO: this is not really needed
1158 if (operators != null) {
1159 foreach (Operator o in operators) {
1160 if (!o.ResolveMembers ())
1168 public override void SetParameterInfo (ArrayList constraints_list)
1170 base.SetParameterInfo (constraints_list);
1172 if (!is_generic || PartialContainer == this)
1175 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1176 for (int i = 0; i < tc_names.Length; ++i) {
1177 if (tc_names [i].Name != type_params [i].Name) {
1178 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1179 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1180 GetSignatureForError ());
1186 void UpdateTypeParameterConstraints (TypeContainer part)
1188 TypeParameter[] current_params = CurrentTypeParameters;
1189 for (int i = 0; i < current_params.Length; i++) {
1190 Constraints c = part.CurrentTypeParameters [i].Constraints;
1194 if (current_params [i].UpdateConstraints (part, c))
1197 Report.SymbolRelatedToPreviousError (Location, "");
1198 Report.Error (265, part.Location,
1199 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1200 GetSignatureForError (), current_params [i].GetSignatureForError ());
1204 public bool ResolveType ()
1206 if (!DoResolveType ())
1209 if (compiler_generated != null) {
1210 foreach (CompilerGeneratedClass c in compiler_generated)
1211 if (!c.ResolveType ())
1218 protected virtual bool DoResolveType ()
1220 if ((base_type != null) &&
1221 (base_type.ResolveAsTypeTerminal (this, false) == null)) {
1229 if (PartialContainer != this)
1230 throw new InternalErrorException ();
1232 TypeExpr current_type = null;
1234 foreach (TypeParameter type_param in CurrentTypeParameters) {
1235 if (!type_param.Resolve (this)) {
1241 if (partial_parts != null && is_generic) {
1242 foreach (TypeContainer part in partial_parts)
1243 UpdateTypeParameterConstraints (part);
1246 foreach (TypeParameter type_param in TypeParameters) {
1247 if (!type_param.DefineType (this)) {
1253 current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
1254 current_type = current_type.ResolveAsTypeTerminal (this, false);
1255 if (current_type == null) {
1260 CurrentType = current_type.Type;
1264 protected virtual bool DefineNestedTypes ()
1266 if (Types != null) {
1267 foreach (TypeContainer tc in Types)
1268 if (tc.DefineType () == null)
1272 if (Delegates != null) {
1273 foreach (Delegate d in Delegates)
1274 if (d.DefineType () == null)
1281 TypeContainer InTransit;
1283 protected bool CheckRecursiveDefinition (TypeContainer tc)
1285 if (InTransit != null) {
1286 Report.SymbolRelatedToPreviousError (this);
1287 if (this is Interface)
1289 529, tc.Location, "Inherited interface `{0}' causes a " +
1290 "cycle in the interface hierarchy of `{1}'",
1291 GetSignatureForError (), tc.GetSignatureForError ());
1294 146, tc.Location, "Circular base class dependency " +
1295 "involving `{0}' and `{1}'",
1296 tc.GetSignatureForError (), GetSignatureForError ());
1302 if (base_type != null && base_type.Type != null) {
1303 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1304 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1305 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1309 if (iface_exprs != null) {
1310 foreach (TypeExpr iface in iface_exprs) {
1311 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1312 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1313 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1318 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1325 public static void Error_KeywordNotAllowed (Location loc)
1327 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1331 /// Populates our TypeBuilder with fields and methods
1333 public override bool DefineMembers ()
1335 if (members_defined)
1336 return members_defined_ok;
1338 if (!base.DefineMembers ())
1341 members_defined_ok = DoDefineMembers ();
1342 members_defined = true;
1344 return members_defined_ok;
1347 protected virtual bool DoDefineMembers ()
1349 if (iface_exprs != null) {
1350 foreach (TypeExpr iface in iface_exprs) {
1351 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
1352 if ((oa != null) && !IsInObsoleteScope)
1353 AttributeTester.Report_ObsoleteMessage (
1354 oa, iface.GetSignatureForError (), Location);
1356 ConstructedType ct = iface as ConstructedType;
1357 if ((ct != null) && !ct.CheckConstraints (this))
1362 if (base_type != null) {
1363 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1364 if (obsolete_attr != null && !IsInObsoleteScope)
1365 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1367 ConstructedType ct = base_type as ConstructedType;
1368 if ((ct != null) && !ct.CheckConstraints (this))
1371 member_cache = new MemberCache (base_type.Type, this);
1372 } else if (Kind == Kind.Interface) {
1373 member_cache = new MemberCache (null, this);
1374 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1375 for (int i = 0; i < ifaces.Length; ++i)
1376 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1378 member_cache = new MemberCache (null, this);
1382 foreach (TypeContainer tc in types)
1383 member_cache.AddNestedType (tc);
1385 if (delegates != null)
1386 foreach (Delegate d in delegates)
1387 member_cache.AddNestedType (d);
1389 if (partial_parts != null) {
1390 foreach (TypeContainer part in partial_parts)
1391 part.member_cache = member_cache;
1395 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1396 if (conflict_symbol == null) {
1397 if ((ModFlags & Modifiers.NEW) != 0)
1398 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1400 if ((ModFlags & Modifiers.NEW) == 0) {
1401 Report.SymbolRelatedToPreviousError (conflict_symbol);
1402 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1403 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1408 DefineContainerMembers (constants);
1409 DefineContainerMembers (fields);
1411 if (Kind == Kind.Struct || Kind == Kind.Class) {
1412 pending = PendingImplementation.GetPendingImplementations (this);
1416 // Constructors are not in the defined_names array
1418 DefineContainerMembers (instance_constructors);
1420 DefineContainerMembers (events);
1421 DefineContainerMembers (ordered_explicit_member_list);
1422 DefineContainerMembers (ordered_member_list);
1424 DefineContainerMembers (operators);
1425 DefineContainerMembers (delegates);
1427 ComputeIndexerName();
1428 CheckEqualsAndGetHashCode();
1430 if (CurrentType != null) {
1431 GenericType = CurrentType;
1436 // FIXME: This hack is needed because member cache does not work
1437 // with generic types, we rely on runtime to inflate dynamic types.
1438 // TODO: This hack requires member cache refactoring to be removed
1440 if (TypeBuilder.IsGenericType)
1441 member_cache = new MemberCache (this);
1447 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1450 mcal.DefineContainerMembers ();
1453 protected virtual void ComputeIndexerName ()
1455 if (indexers == null)
1458 string class_indexer_name = null;
1461 // If there's both an explicit and an implicit interface implementation, the
1462 // explicit one actually implements the interface while the other one is just
1463 // a normal indexer. See bug #37714.
1466 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1467 foreach (Indexer i in indexers) {
1468 if (i.InterfaceType != null) {
1469 if (seen_normal_indexers)
1470 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1474 seen_normal_indexers = true;
1476 if (class_indexer_name == null) {
1477 class_indexer_name = i.ShortName;
1481 if (i.ShortName != class_indexer_name)
1482 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");
1485 if (class_indexer_name != null)
1486 indexer_name = class_indexer_name;
1489 protected virtual void EmitIndexerName ()
1491 if (!seen_normal_indexers)
1494 if (TypeManager.default_member_ctor == null) {
1495 if (TypeManager.default_member_type == null) {
1496 TypeManager.default_member_type = TypeManager.CoreLookupType (
1497 "System.Reflection", "DefaultMemberAttribute", Kind.Class, true);
1499 if (TypeManager.default_member_type == null)
1503 TypeManager.default_member_ctor = TypeManager.GetPredefinedConstructor (
1504 TypeManager.default_member_type, Location, TypeManager.string_type);
1506 if (TypeManager.default_member_ctor == null)
1510 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
1511 TypeBuilder.SetCustomAttribute (cb);
1514 protected virtual void CheckEqualsAndGetHashCode ()
1516 if (methods == null)
1519 if (HasEquals && !HasGetHashCode) {
1520 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1524 public override bool Define ()
1526 CheckProtectedModifier ();
1530 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1532 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1536 /// This function is based by a delegate to the FindMembers routine
1538 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1544 /// This filter is used by FindMembers, and we just keep
1545 /// a global for the filter to `AlwaysAccept'
1547 static MemberFilter accepting_filter;
1550 static TypeContainer ()
1552 accepting_filter = new MemberFilter (AlwaysAccept);
1555 public MethodInfo[] GetMethods ()
1557 ArrayList members = new ArrayList ();
1561 if (methods != null) {
1562 int len = methods.Count;
1563 for (int i = 0; i < len; i++) {
1564 Method m = (Method) methods [i];
1566 members.Add (m.MethodBuilder);
1570 if (operators != null) {
1571 int len = operators.Count;
1572 for (int i = 0; i < len; i++) {
1573 Operator o = (Operator) operators [i];
1575 members.Add (o.MethodBuilder);
1579 if (properties != null) {
1580 int len = properties.Count;
1581 for (int i = 0; i < len; i++) {
1582 Property p = (Property) properties [i];
1584 if (p.GetBuilder != null)
1585 members.Add (p.GetBuilder);
1586 if (p.SetBuilder != null)
1587 members.Add (p.SetBuilder);
1591 if (indexers != null) {
1592 int len = indexers.Count;
1593 for (int i = 0; i < len; i++) {
1594 Indexer ix = (Indexer) indexers [i];
1596 if (ix.GetBuilder != null)
1597 members.Add (ix.GetBuilder);
1598 if (ix.SetBuilder != null)
1599 members.Add (ix.SetBuilder);
1603 if (events != null) {
1604 int len = events.Count;
1605 for (int i = 0; i < len; i++) {
1606 Event e = (Event) events [i];
1608 if (e.AddBuilder != null)
1609 members.Add (e.AddBuilder);
1610 if (e.RemoveBuilder != null)
1611 members.Add (e.RemoveBuilder);
1615 MethodInfo[] retMethods = new MethodInfo [members.Count];
1616 members.CopyTo (retMethods, 0);
1620 // Indicated whether container has StructLayout attribute set Explicit
1621 public bool HasExplicitLayout {
1622 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1623 set { caching_flags |= Flags.HasExplicitLayout; }
1626 public bool HasStructLayout {
1627 get { return (caching_flags & Flags.HasStructLayout) != 0; }
1628 set { caching_flags |= Flags.HasStructLayout; }
1632 // Return the nested type with name @name. Ensures that the nested type
1633 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1635 public Type FindNestedType (string name)
1637 if (PartialContainer != this)
1638 throw new InternalErrorException ("should not happen");
1640 ArrayList [] lists = { types, delegates };
1642 for (int j = 0; j < lists.Length; ++j) {
1643 ArrayList list = lists [j];
1647 int len = list.Count;
1648 for (int i = 0; i < len; ++i) {
1649 DeclSpace ds = (DeclSpace) list [i];
1650 if (ds.Basename == name) {
1651 return ds.DefineType ();
1659 private void FindMembers_NestedTypes (int modflags,
1660 BindingFlags bf, MemberFilter filter, object criteria,
1661 ref ArrayList members)
1663 ArrayList [] lists = { types, delegates };
1665 for (int j = 0; j < lists.Length; ++j) {
1666 ArrayList list = lists [j];
1670 int len = list.Count;
1671 for (int i = 0; i < len; i++) {
1672 DeclSpace ds = (DeclSpace) list [i];
1674 if ((ds.ModFlags & modflags) == 0)
1677 TypeBuilder tb = ds.TypeBuilder;
1679 if (!(criteria is string) || ds.Basename.Equals (criteria))
1680 tb = ds.DefineType ();
1683 if (tb != null && (filter (tb, criteria) == true)) {
1684 if (members == null)
1685 members = new ArrayList ();
1694 /// This method returns the members of this type just like Type.FindMembers would
1695 /// Only, we need to use this for types which are _being_ defined because MS'
1696 /// implementation can't take care of that.
1699 // FIXME: return an empty static array instead of null, that cleans up
1700 // some code and is consistent with some coding conventions I just found
1704 // Notice that in various cases we check if our field is non-null,
1705 // something that would normally mean that there was a bug elsewhere.
1707 // The problem happens while we are defining p-invoke methods, as those
1708 // will trigger a FindMembers, but this happens before things are defined
1710 // Since the whole process is a no-op, it is fine to check for null here.
1712 // TODO: This approach will be one day completely removed, it's already
1713 // used at few places only
1716 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1717 MemberFilter filter, object criteria)
1719 ArrayList members = null;
1722 if ((bf & BindingFlags.Public) != 0)
1723 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1725 if ((bf & BindingFlags.NonPublic) != 0)
1726 modflags |= Modifiers.PRIVATE;
1728 int static_mask = 0, static_flags = 0;
1729 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1730 case BindingFlags.Static:
1731 static_mask = static_flags = Modifiers.STATIC;
1734 case BindingFlags.Instance:
1735 static_mask = Modifiers.STATIC;
1740 static_mask = static_flags = 0;
1744 Timer.StartTimer (TimerType.TcFindMembers);
1747 filter = accepting_filter;
1749 if ((mt & MemberTypes.Field) != 0) {
1750 if (fields != null) {
1751 int len = fields.Count;
1752 for (int i = 0; i < len; i++) {
1753 FieldBase f = (FieldBase) fields [i];
1755 if ((f.ModFlags & modflags) == 0)
1757 if ((f.ModFlags & static_mask) != static_flags)
1760 FieldBuilder fb = f.FieldBuilder;
1761 if (fb != null && filter (fb, criteria) == true) {
1762 if (members == null)
1763 members = new ArrayList ();
1770 if (constants != null) {
1771 int len = constants.Count;
1772 for (int i = 0; i < len; i++) {
1773 Const con = (Const) constants [i];
1775 if ((con.ModFlags & modflags) == 0)
1777 if ((con.ModFlags & static_mask) != static_flags)
1780 FieldBuilder fb = con.FieldBuilder;
1783 fb = con.FieldBuilder;
1785 if (fb != null && filter (fb, criteria) == true) {
1786 if (members == null)
1787 members = new ArrayList ();
1795 if ((mt & MemberTypes.Method) != 0) {
1796 if (methods != null) {
1797 int len = methods.Count;
1798 for (int i = 0; i < len; i++) {
1799 Method m = (Method) methods [i];
1801 if ((m.ModFlags & modflags) == 0)
1803 if ((m.ModFlags & static_mask) != static_flags)
1806 MethodBuilder mb = m.MethodBuilder;
1808 if (mb != null && filter (mb, criteria) == true) {
1809 if (members == null)
1810 members = new ArrayList ();
1817 if (operators != null) {
1818 int len = operators.Count;
1819 for (int i = 0; i < len; i++) {
1820 Operator o = (Operator) operators [i];
1822 if ((o.ModFlags & modflags) == 0)
1824 if ((o.ModFlags & static_mask) != static_flags)
1827 MethodBuilder ob = o.MethodBuilder;
1828 if (ob != null && filter (ob, criteria) == true) {
1829 if (members == null)
1830 members = new ArrayList ();
1837 if (events != null) {
1838 foreach (Event e in events) {
1839 if ((e.ModFlags & modflags) == 0)
1841 if ((e.ModFlags & static_mask) != static_flags)
1844 MethodBuilder b = e.AddBuilder;
1845 if (b != null && filter (b, criteria)) {
1846 if (members == null)
1847 members = new ArrayList (4);
1852 b = e.RemoveBuilder;
1853 if (b != null && filter (b, criteria)) {
1854 if (members == null)
1855 members = new ArrayList (4);
1862 if (properties != null) {
1863 int len = properties.Count;
1864 for (int i = 0; i < len; i++) {
1865 Property p = (Property) properties [i];
1867 if ((p.ModFlags & modflags) == 0)
1869 if ((p.ModFlags & static_mask) != static_flags)
1875 if (b != null && filter (b, criteria) == true) {
1876 if (members == null)
1877 members = new ArrayList ();
1883 if (b != null && filter (b, criteria) == true) {
1884 if (members == null)
1885 members = new ArrayList ();
1892 if (indexers != null) {
1893 int len = indexers.Count;
1894 for (int i = 0; i < len; i++) {
1895 Indexer ix = (Indexer) indexers [i];
1897 if ((ix.ModFlags & modflags) == 0)
1899 if ((ix.ModFlags & static_mask) != static_flags)
1905 if (b != null && filter (b, criteria) == true) {
1906 if (members == null)
1907 members = new ArrayList ();
1913 if (b != null && filter (b, criteria) == true) {
1914 if (members == null)
1915 members = new ArrayList ();
1923 if ((mt & MemberTypes.Event) != 0) {
1924 if (events != null) {
1925 int len = events.Count;
1926 for (int i = 0; i < len; i++) {
1927 Event e = (Event) events [i];
1929 if ((e.ModFlags & modflags) == 0)
1931 if ((e.ModFlags & static_mask) != static_flags)
1934 MemberInfo eb = e.EventBuilder;
1935 if (eb != null && filter (eb, criteria) == true) {
1936 if (members == null)
1937 members = new ArrayList ();
1939 members.Add (e.EventBuilder);
1945 if ((mt & MemberTypes.Property) != 0){
1946 if (properties != null) {
1947 int len = properties.Count;
1948 for (int i = 0; i < len; i++) {
1949 Property p = (Property) properties [i];
1951 if ((p.ModFlags & modflags) == 0)
1953 if ((p.ModFlags & static_mask) != static_flags)
1956 MemberInfo pb = p.PropertyBuilder;
1957 if (pb != null && filter (pb, criteria) == true) {
1958 if (members == null)
1959 members = new ArrayList ();
1961 members.Add (p.PropertyBuilder);
1966 if (indexers != null) {
1967 int len = indexers.Count;
1968 for (int i = 0; i < len; i++) {
1969 Indexer ix = (Indexer) indexers [i];
1971 if ((ix.ModFlags & modflags) == 0)
1973 if ((ix.ModFlags & static_mask) != static_flags)
1976 MemberInfo ib = ix.PropertyBuilder;
1977 if (ib != null && filter (ib, criteria) == true) {
1978 if (members == null)
1979 members = new ArrayList ();
1981 members.Add (ix.PropertyBuilder);
1987 if ((mt & MemberTypes.NestedType) != 0)
1988 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1990 if ((mt & MemberTypes.Constructor) != 0){
1991 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1992 int len = instance_constructors.Count;
1993 for (int i = 0; i < len; i++) {
1994 Constructor c = (Constructor) instance_constructors [i];
1996 ConstructorBuilder cb = c.ConstructorBuilder;
1997 if (cb != null && filter (cb, criteria) == true) {
1998 if (members == null)
1999 members = new ArrayList ();
2006 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2007 ConstructorBuilder cb =
2008 default_static_constructor.ConstructorBuilder;
2010 if (cb != null && filter (cb, criteria) == true) {
2011 if (members == null)
2012 members = new ArrayList ();
2020 // Lookup members in base if requested.
2022 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2023 if (TypeBuilder.BaseType != null) {
2024 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2025 if (list.Count > 0) {
2026 if (members == null)
2027 members = new ArrayList ();
2029 members.AddRange (list);
2034 Timer.StopTimer (TimerType.TcFindMembers);
2036 if (members == null)
2037 return MemberList.Empty;
2039 return new MemberList (members);
2042 public override MemberCache MemberCache {
2044 return member_cache;
2048 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2049 MemberFilter filter, object criteria)
2051 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2054 return ds.FindMembers (mt, bf, filter, criteria);
2056 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2060 /// Emits the values for the constants
2062 public void EmitConstants ()
2064 if (constants != null)
2065 foreach (Const con in constants)
2070 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2075 foreach (MemberCore mc in al) {
2076 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2079 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2080 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2085 public virtual void VerifyMembers ()
2088 // Check for internal or private fields that were never assigned
2090 if (Report.WarningLevel >= 3) {
2091 CheckMemberUsage (properties, "property");
2092 CheckMemberUsage (methods, "method");
2093 CheckMemberUsage (constants, "constant");
2095 if (fields != null){
2096 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2097 foreach (FieldBase f in fields) {
2098 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2099 if (is_type_exposed)
2102 f.SetMemberIsUsed ();
2106 if ((f.caching_flags & Flags.IsAssigned) == 0)
2107 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2110 const int error_code = 414;
2112 const int error_code = 169;
2114 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2115 f.GetSignatureForError ());
2121 // Only report 649 on level 4
2123 if (Report.WarningLevel < 4)
2126 if ((f.caching_flags & Flags.IsAssigned) != 0)
2130 // Don't be pendatic over serializable attributes
2132 if (f.OptAttributes != null || PartialContainer.HasStructLayout)
2135 Constant c = New.Constantify (f.MemberType);
2136 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2137 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2143 // TODO: move to ClassOrStruct
2144 void EmitConstructors ()
2146 if (instance_constructors == null)
2149 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2150 bool has_compliant_args = false;
2152 foreach (Constructor c in instance_constructors) {
2156 catch (Exception e) {
2157 throw new InternalErrorException (c, e);
2160 if (has_compliant_args)
2163 has_compliant_args = c.HasCompliantArgs;
2165 if (!has_compliant_args)
2166 Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2168 foreach (Constructor c in instance_constructors) {
2172 catch (Exception e) {
2173 throw new InternalErrorException (c, e);
2180 /// Emits the code, this step is performed after all
2181 /// the types, enumerations, constructors
2183 public virtual void EmitType ()
2185 if (OptAttributes != null)
2186 OptAttributes.Emit ();
2190 EmitConstructors ();
2192 // Can not continue if constants are broken
2194 if (Report.Errors > 0)
2197 if (default_static_constructor != null)
2198 default_static_constructor.Emit ();
2200 if (operators != null)
2201 foreach (Operator o in operators)
2204 if (properties != null)
2205 foreach (Property p in properties)
2208 if (indexers != null) {
2209 foreach (Indexer indx in indexers)
2214 if (events != null){
2215 foreach (Event e in Events)
2219 if (methods != null) {
2220 for (int i = 0; i < methods.Count; ++i)
2221 ((Method) methods [i]).Emit ();
2225 foreach (FieldBase f in fields)
2228 if (delegates != null) {
2229 foreach (Delegate d in Delegates) {
2234 if (pending != null)
2235 pending.VerifyPendingMethods ();
2237 if (Report.Errors > 0)
2240 if (compiler_generated != null) {
2241 for (int i = 0; i < compiler_generated.Count; ++i)
2242 ((CompilerGeneratedClass) compiler_generated [i]).EmitType ();
2246 public override void CloseType ()
2248 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2252 caching_flags |= Flags.CloseTypeCreated;
2253 TypeBuilder.CreateType ();
2254 } catch (TypeLoadException){
2256 // This is fine, the code still created the type
2258 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2259 // Console.WriteLine (e.Message);
2260 } catch (Exception e) {
2261 throw new InternalErrorException (this, e);
2265 foreach (TypeContainer tc in Types)
2266 if (tc.Kind == Kind.Struct)
2269 foreach (TypeContainer tc in Types)
2270 if (tc.Kind != Kind.Struct)
2274 if (Delegates != null)
2275 foreach (Delegate d in Delegates)
2278 if (compiler_generated != null)
2279 foreach (CompilerGeneratedClass c in compiler_generated)
2286 initialized_fields = null;
2287 initialized_static_fields = null;
2289 ordered_explicit_member_list = null;
2290 ordered_member_list = null;
2295 compiler_generated = null;
2296 default_constructor = null;
2297 default_static_constructor = null;
2299 OptAttributes = null;
2302 member_cache = null;
2306 // Performs the validation on a Method's modifiers (properties have
2307 // the same properties).
2309 public bool MethodModifiersValid (MemberCore mc)
2311 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2312 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2313 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2315 int flags = mc.ModFlags;
2318 // At most one of static, virtual or override
2320 if ((flags & Modifiers.STATIC) != 0){
2321 if ((flags & vao) != 0){
2322 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2323 mc.GetSignatureForError ());
2328 if (Kind == Kind.Struct){
2329 if ((flags & va) != 0){
2330 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2335 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2336 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2337 mc.GetSignatureForError ());
2342 // If the declaration includes the abstract modifier, then the
2343 // declaration does not include static, virtual or extern
2345 if ((flags & Modifiers.ABSTRACT) != 0){
2346 if ((flags & Modifiers.EXTERN) != 0){
2348 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2352 if ((flags & Modifiers.SEALED) != 0) {
2353 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2357 if ((flags & Modifiers.VIRTUAL) != 0){
2358 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2362 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2363 Report.SymbolRelatedToPreviousError (this);
2364 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2365 mc.GetSignatureForError (), GetSignatureForError ());
2370 if ((flags & Modifiers.PRIVATE) != 0){
2371 if ((flags & vao) != 0){
2372 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2377 if ((flags & Modifiers.SEALED) != 0){
2378 if ((flags & Modifiers.OVERRIDE) == 0){
2379 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2387 public Constructor DefaultStaticConstructor {
2388 get { return default_static_constructor; }
2391 protected override bool VerifyClsCompliance ()
2393 if (!base.VerifyClsCompliance ())
2398 Type base_type = TypeBuilder.BaseType;
2399 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2400 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2407 /// Checks whether container name is CLS Compliant
2409 void VerifyClsName ()
2411 Hashtable base_members = base_cache == null ?
2413 base_cache.GetPublicMembers ();
2414 Hashtable this_members = new Hashtable ();
2416 foreach (DictionaryEntry entry in defined_names) {
2417 MemberCore mc = (MemberCore)entry.Value;
2418 if (!mc.IsClsComplianceRequired ())
2421 string name = (string) entry.Key;
2422 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2424 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2425 object found = base_members [lcase];
2426 if (found == null) {
2427 found = this_members [lcase];
2428 if (found == null) {
2429 this_members.Add (lcase, mc);
2434 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2437 if (found is MemberInfo) {
2438 if (basename == ((MemberInfo) found).Name)
2440 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2442 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2445 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2451 /// Performs checks for an explicit interface implementation. First it
2452 /// checks whether the `interface_type' is a base inteface implementation.
2453 /// Then it checks whether `name' exists in the interface type.
2455 public bool VerifyImplements (InterfaceMemberBase mb)
2457 if (ifaces != null) {
2458 foreach (Type t in ifaces){
2459 if (TypeManager.IsEqual (t, mb.InterfaceType))
2464 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2465 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2466 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2470 public override Type LookupAnyGeneric (string typeName)
2472 if (types != null) {
2473 foreach (TypeContainer tc in types) {
2477 int pos = tc.Basename.LastIndexOf ('`');
2478 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2479 return tc.TypeBuilder;
2483 return base.LookupAnyGeneric (typeName);
2486 public void Mark_HasEquals ()
2488 cached_method |= CachedMethods.Equals;
2491 public void Mark_HasGetHashCode ()
2493 cached_method |= CachedMethods.GetHashCode;
2497 /// Method container contains Equals method
2499 public bool HasEquals {
2501 return (cached_method & CachedMethods.Equals) != 0;
2506 /// Method container contains GetHashCode method
2508 public bool HasGetHashCode {
2510 return (cached_method & CachedMethods.GetHashCode) != 0;
2514 public bool HasStaticFieldInitializer {
2516 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2520 cached_method |= CachedMethods.HasStaticFieldInitializer;
2522 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2530 string IMemberContainer.Name {
2536 Type IMemberContainer.Type {
2542 bool IMemberContainer.IsInterface {
2544 return Kind == Kind.Interface;
2548 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2550 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2552 if (GenericType != null)
2553 return TypeManager.FindMembers (GenericType, mt, new_bf,
2556 return FindMembers (mt, new_bf, null, null);
2560 // Generates xml doc comments (if any), and if required,
2561 // handle warning report.
2563 internal override void GenerateDocComment (DeclSpace ds)
2565 DocUtil.GenerateTypeDocComment (this, ds);
2568 public override string DocCommentHeader {
2569 get { return "T:"; }
2572 public MemberCache BaseCache {
2574 if (base_cache != null)
2576 if (TypeBuilder.BaseType != null)
2577 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2578 if (TypeBuilder.IsInterface)
2579 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2585 public abstract class ClassOrStruct : TypeContainer {
2586 ListDictionary declarative_security;
2588 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2589 MemberName name, Attributes attrs, Kind kind)
2590 : base (ns, parent, name, attrs, kind)
2594 protected override bool AddToContainer (MemberCore symbol, string name)
2596 if (name == MemberName.Name) {
2597 if (symbol is TypeParameter) {
2598 Report.Error (694, symbol.Location,
2599 "Type parameter `{0}' has same name as " +
2600 "containing type, or method", name);
2604 Report.SymbolRelatedToPreviousError (this);
2605 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2606 symbol.GetSignatureForError ());
2610 return base.AddToContainer (symbol, name);
2613 public override void VerifyMembers ()
2615 base.VerifyMembers ();
2617 if ((events != null) && Report.WarningLevel >= 3) {
2618 foreach (Event e in events){
2619 // Note: The event can be assigned from same class only, so we can report
2620 // this warning for all accessibility modes
2621 if ((e.caching_flags & Flags.IsUsed) == 0)
2622 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2627 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2629 if (a.IsValidSecurityAttribute ()) {
2630 if (declarative_security == null)
2631 declarative_security = new ListDictionary ();
2633 a.ExtractSecurityPermissionSet (declarative_security);
2637 if (a.Type == TypeManager.struct_layout_attribute_type) {
2638 PartialContainer.HasStructLayout = true;
2640 if (a.GetLayoutKindValue () == LayoutKind.Explicit)
2641 PartialContainer.HasExplicitLayout = true;
2644 base.ApplyAttributeBuilder (a, cb);
2648 /// Defines the default constructors
2650 protected void DefineDefaultConstructor (bool is_static)
2652 // The default instance constructor is public
2653 // If the class is abstract, the default constructor is protected
2654 // The default static constructor is private
2658 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2660 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2663 Constructor c = new Constructor (this, MemberName.Name, mods,
2664 Parameters.EmptyReadOnlyParameters,
2665 new GeneratedBaseInitializer (Location),
2669 c.Block = new ToplevelBlock (null, Location);
2672 public override bool Define ()
2674 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer)
2675 DefineDefaultConstructor (true);
2677 if (default_static_constructor != null)
2678 default_static_constructor.Define ();
2680 return base.Define ();
2683 public override void Emit ()
2687 if (declarative_security != null) {
2688 foreach (DictionaryEntry de in declarative_security) {
2689 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2694 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2696 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
2699 protected override TypeAttributes TypeAttr {
2701 if (default_static_constructor == null)
2702 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2704 return base.TypeAttr;
2710 // TODO: should be sealed
2711 public class Class : ClassOrStruct {
2712 const int AllowedModifiers =
2715 Modifiers.PROTECTED |
2716 Modifiers.INTERNAL |
2718 Modifiers.ABSTRACT |
2723 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2725 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2727 : base (ns, parent, name, attrs, Kind.Class)
2729 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2730 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2732 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2733 Report.FeatureIsNotAvailable (Location, "static classes");
2737 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2739 if (part.Name == "System.Object")
2740 Report.Error (537, part.Location,
2741 "The class System.Object cannot have a base class or implement an interface.");
2742 base.AddBasesForPart (part, bases);
2745 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2747 if (a.Type == TypeManager.attribute_usage_type) {
2748 if (!TypeManager.IsAttributeType (BaseType) &&
2749 TypeBuilder.FullName != "System.Attribute") {
2750 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2754 if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
2755 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2759 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
2760 !attributes.Contains (TypeManager.guid_attr_type)) {
2761 a.Error_MissingGuidAttribute ();
2765 if (a.Type == TypeManager.extension_attribute_type) {
2766 a.Error_MisusedExtensionAttribute ();
2770 if (AttributeTester.IsAttributeExcluded (a.Type, Location))
2773 base.ApplyAttributeBuilder (a, cb);
2776 public override AttributeTargets AttributeTargets {
2778 return AttributeTargets.Class;
2782 protected override void DefineContainerMembers (MemberCoreArrayList list)
2788 base.DefineContainerMembers (list);
2792 foreach (MemberCore m in list) {
2793 if (m is Operator) {
2794 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2798 if (m is Destructor) {
2799 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2804 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2808 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2811 if (m is Constructor) {
2812 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2816 Method method = m as Method;
2817 if (method != null && method.Parameters.HasExtensionMethodType) {
2818 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2822 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2825 base.DefineContainerMembers (list);
2828 public override TypeBuilder DefineType ()
2830 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2831 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2835 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2836 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2840 return base.DefineType ();
2843 protected override bool DoDefineMembers ()
2845 if (InstanceConstructors == null && !IsStatic)
2846 DefineDefaultConstructor (false);
2848 return base.DoDefineMembers ();
2851 public override void Emit ()
2856 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2857 TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
2861 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2863 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2865 if (base_class == null) {
2866 if (RootContext.StdLib)
2867 base_class = TypeManager.system_object_expr;
2868 else if (Name != "System.Object")
2869 base_class = TypeManager.system_object_expr;
2871 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2873 689, base_class.Location,
2874 "Cannot derive from `{0}' because it is a type parameter",
2875 base_class.GetSignatureForError ());
2879 if (IsGeneric && TypeManager.IsAttributeType (base_class.Type)) {
2880 Report.Error (698, base_class.Location,
2881 "A generic type cannot derive from `{0}' because it is an attribute class",
2882 base_class.GetSignatureForError ());
2885 if (base_class.IsSealed){
2886 Report.SymbolRelatedToPreviousError (base_class.Type);
2887 if (base_class.Type.IsAbstract) {
2888 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2889 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2891 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2892 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2897 if (!base_class.CanInheritFrom ()){
2898 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2899 GetSignatureForError (), base_class.GetSignatureForError ());
2903 if (!base_class.AsAccessible (this)) {
2904 Report.SymbolRelatedToPreviousError (base_class.Type);
2905 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2906 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2910 if (PartialContainer.IsStaticClass) {
2911 if (base_class.Type != TypeManager.object_type) {
2912 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2913 GetSignatureForError (), base_class.GetSignatureForError ());
2917 if (ifaces != null) {
2918 foreach (TypeExpr t in ifaces)
2919 Report.SymbolRelatedToPreviousError (t.Type);
2920 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2927 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2928 /// Valid only for attribute classes.
2929 public bool IsExcluded ()
2931 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2932 return (caching_flags & Flags.Excluded) != 0;
2934 caching_flags &= ~Flags.Excluded_Undetected;
2936 if (OptAttributes == null)
2939 if (TypeManager.conditional_attribute_type == null)
2942 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
2947 foreach (Attribute a in attrs) {
2948 string condition = a.GetConditionalAttributeValue ();
2949 if (Location.CompilationUnit.IsConditionalDefined (condition))
2953 caching_flags |= Flags.Excluded;
2959 return (ModFlags & Modifiers.STATIC) != 0;
2964 // FIXME: How do we deal with the user specifying a different
2967 protected override TypeAttributes TypeAttr {
2969 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2971 ta |= StaticClassAttribute;
2977 public sealed class Struct : ClassOrStruct {
2979 // Modifiers allowed in a struct declaration
2981 const int AllowedModifiers =
2984 Modifiers.PROTECTED |
2985 Modifiers.INTERNAL |
2989 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2990 int mod, Attributes attrs)
2991 : base (ns, parent, name, attrs, Kind.Struct)
2995 if (parent.Parent == null)
2996 accmods = Modifiers.INTERNAL;
2998 accmods = Modifiers.PRIVATE;
3000 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
3002 this.ModFlags |= Modifiers.SEALED;
3005 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3007 base.ApplyAttributeBuilder (a, cb);
3010 // When struct constains fixed fixed and struct layout has explicitly
3011 // set CharSet, its value has to be propagated to compiler generated
3012 // fixed field types
3014 if (a.Type == TypeManager.struct_layout_attribute_type && Fields != null && a.HasField ("CharSet")) {
3015 for (int i = 0; i < Fields.Count; ++i) {
3016 FixedField ff = Fields [i] as FixedField;
3018 ff.SetCharSet (TypeBuilder.Attributes);
3023 public override AttributeTargets AttributeTargets {
3025 return AttributeTargets.Struct;
3029 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
3031 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
3033 // If we are compiling our runtime,
3034 // and we are defining ValueType, then our
3035 // base is `System.Object'.
3037 if (base_class == null) {
3038 if (!RootContext.StdLib && Name == "System.ValueType")
3039 base_class = TypeManager.system_object_expr;
3041 base_class = TypeManager.system_valuetype_expr;
3048 // FIXME: Allow the user to specify a different set of attributes
3049 // in some cases (Sealed for example is mandatory for a class,
3050 // but what SequentialLayout can be changed
3052 protected override TypeAttributes TypeAttr {
3054 const TypeAttributes DefaultTypeAttributes =
3055 TypeAttributes.SequentialLayout |
3056 TypeAttributes.Sealed |
3057 TypeAttributes.BeforeFieldInit;
3059 return base.TypeAttr | DefaultTypeAttributes;
3063 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3065 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3066 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3067 field.GetSignatureForError ());
3070 base.RegisterFieldForInitialization (field, expression);
3078 public sealed class Interface : TypeContainer, IMemberContainer {
3081 /// Modifiers allowed in a class declaration
3083 public const int AllowedModifiers =
3086 Modifiers.PROTECTED |
3087 Modifiers.INTERNAL |
3091 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3093 : base (ns, parent, name, attrs, Kind.Interface)
3097 if (parent.Parent == null)
3098 accmods = Modifiers.INTERNAL;
3100 accmods = Modifiers.PRIVATE;
3102 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3105 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3107 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
3108 !attributes.Contains (TypeManager.guid_attr_type)) {
3109 a.Error_MissingGuidAttribute ();
3112 base.ApplyAttributeBuilder (a, cb);
3116 public override AttributeTargets AttributeTargets {
3118 return AttributeTargets.Interface;
3122 const TypeAttributes DefaultTypeAttributes =
3123 TypeAttributes.AutoLayout |
3124 TypeAttributes.Abstract |
3125 TypeAttributes.Interface;
3127 protected override TypeAttributes TypeAttr {
3129 return base.TypeAttr | DefaultTypeAttributes;
3133 protected override bool VerifyClsCompliance ()
3135 if (!base.VerifyClsCompliance ())
3138 if (ifaces != null) {
3139 foreach (Type t in ifaces) {
3140 if (AttributeTester.IsClsCompliant (t))
3143 Report.SymbolRelatedToPreviousError (t);
3144 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3145 GetSignatureForError (), TypeManager.CSharpName (t));
3153 // It is used as a base class for all property based members
3154 // This includes properties, indexers, and events
3155 public abstract class PropertyBasedMember : InterfaceMemberBase
3157 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3158 FullNamedExpression type, int mod, int allowed_mod,
3159 MemberName name, Attributes attrs)
3160 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3164 protected override bool VerifyClsCompliance ()
3166 if (!base.VerifyClsCompliance ())
3169 if (!AttributeTester.IsClsCompliant (MemberType)) {
3170 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
3171 GetSignatureForError ());
3179 public abstract class MethodCore : InterfaceMemberBase
3181 public readonly Parameters Parameters;
3182 protected ToplevelBlock block;
3184 public MethodCore (DeclSpace parent, GenericMethod generic,
3185 FullNamedExpression type, int mod, int allowed_mod,
3186 MemberName name, Attributes attrs, Parameters parameters)
3187 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3189 Parameters = parameters;
3193 // Returns the System.Type array for the parameters of this method
3195 public Type [] ParameterTypes {
3197 return Parameters.Types;
3201 public Parameters ParameterInfo
3208 public ToplevelBlock Block {
3218 protected override bool CheckBase ()
3220 // Check whether arguments were correct.
3221 if (!DefineParameters (Parameters))
3224 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
3225 if (!Parent.MemberCache.CheckExistingMembersOverloads (this,
3226 MemberName.IsGeneric ? MemberName.Basename : MemberName.MethodName, Parameters))
3229 // TODO: Find a better way how to check reserved accessors collision
3230 Method m = this as Method;
3232 if (!m.CheckForDuplications ())
3237 if (!base.CheckBase ())
3244 // Returns a string that represents the signature for this
3245 // member which should be used in XML documentation.
3247 public override string GetDocCommentName (DeclSpace ds)
3249 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3253 // Raised (and passed an XmlElement that contains the comment)
3254 // when GenerateDocComment is writing documentation expectedly.
3256 // FIXME: with a few effort, it could be done with XmlReader,
3257 // that means removal of DOM use.
3259 internal override void OnGenerateDocComment (XmlElement el)
3261 DocUtil.OnMethodGenerateDocComment (this, el);
3265 // Represents header string for documentation comment.
3267 public override string DocCommentHeader
3269 get { return "M:"; }
3272 public override bool EnableOverloadChecks (MemberCore overload)
3274 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3275 caching_flags |= Flags.MethodOverloadsExist;
3282 protected override bool VerifyClsCompliance ()
3284 if (!base.VerifyClsCompliance ())
3287 if (Parameters.HasArglist) {
3288 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
3291 if (!AttributeTester.IsClsCompliant (MemberType)) {
3292 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
3293 GetSignatureForError ());
3296 Parameters.VerifyClsCompliance ();
3302 public abstract class InterfaceMemberBase : MemberBase {
3304 // Whether this is an interface member.
3306 public bool IsInterface;
3309 // If true, this is an explicit interface implementation
3311 public bool IsExplicitImpl;
3313 protected bool is_external_implementation;
3316 // The interface type we are explicitly implementing
3318 public Type InterfaceType;
3321 // The method we're overriding if this is an override method.
3323 protected MethodInfo base_method;
3325 readonly int explicit_mod_flags;
3326 public MethodAttributes flags;
3328 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3329 FullNamedExpression type, int mod, int allowed_mod,
3330 MemberName name, Attributes attrs)
3331 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3334 IsInterface = parent.PartialContainer.Kind == Kind.Interface;
3335 IsExplicitImpl = (MemberName.Left != null);
3336 explicit_mod_flags = mod;
3339 protected override bool CheckBase ()
3341 if (!base.CheckBase ())
3347 // Is null for System.Object while compiling corlib and base interfaces
3348 if (Parent.PartialContainer.BaseCache == null) {
3349 if ((ModFlags & Modifiers.NEW) != 0) {
3350 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3355 Type base_ret_type = null;
3356 base_method = FindOutBaseMethod (ref base_ret_type);
3358 // method is override
3359 if (base_method != null) {
3360 if (!CheckMethodAgainstBase (base_ret_type))
3363 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3364 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3366 if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3367 Report.SymbolRelatedToPreviousError (base_method);
3368 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3369 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3372 if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3373 Report.SymbolRelatedToPreviousError (base_method);
3374 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3375 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3382 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3383 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3384 if (conflict_symbol != null) {
3385 Report.SymbolRelatedToPreviousError (conflict_symbol);
3387 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3388 else if (this is PropertyBase)
3389 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3391 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3393 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3394 GetSignatureForError (), SimpleName.GetMemberType (this));
3399 if (conflict_symbol == null) {
3400 if ((ModFlags & Modifiers.NEW) != 0) {
3401 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3406 if ((ModFlags & Modifiers.NEW) == 0) {
3407 if (this is MethodOrOperator && conflict_symbol.MemberType == MemberTypes.Method)
3410 Report.SymbolRelatedToPreviousError (conflict_symbol);
3411 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3412 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3419 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3420 // that have been defined.
3422 // `name' is the user visible name for reporting errors (this is used to
3423 // provide the right name regarding method names and properties)
3425 bool CheckMethodAgainstBase (Type base_method_type)
3429 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3430 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3431 Report.Error (506, Location,
3432 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3433 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3437 // Now we check that the overriden method is not final
3439 if (base_method.IsFinal) {
3440 Report.SymbolRelatedToPreviousError (base_method);
3441 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3442 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3446 // Check that the permissions are not being changed
3448 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3449 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3451 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3452 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3456 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3457 Report.SymbolRelatedToPreviousError (base_method);
3458 if (this is PropertyBasedMember) {
3459 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3460 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3463 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3464 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3470 if ((ModFlags & Modifiers.NEW) == 0) {
3471 if ((ModFlags & Modifiers.OVERRIDE) == 0 && Name != "Finalize") {
3472 ModFlags |= Modifiers.NEW;
3473 Report.SymbolRelatedToPreviousError (base_method);
3474 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3475 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",
3476 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3478 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3479 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3483 if (base_method.IsAbstract && !IsInterface) {
3484 Report.SymbolRelatedToPreviousError (base_method);
3485 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3486 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3494 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3496 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3498 // when overriding protected internal, the method can be declared
3499 // protected internal only within the same assembly or assembly
3500 // which has InternalsVisibleTo
3502 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3503 return TypeManager.IsThisOrFriendAssembly (base_method.DeclaringType.Assembly);
3504 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3506 // if it's not "protected internal", it must be "protected"
3510 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3512 // protected within the same assembly - an error
3515 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3516 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3518 // protected ok, but other attributes differ - report an error
3524 return (thisp == base_classp);
3528 protected bool DefineParameters (Parameters parameters)
3530 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3532 if (!parameters.Resolve (rc))
3536 for (int i = 0; i < parameters.Count; ++i) {
3537 Parameter p = parameters [i];
3538 if (p.CheckAccessibility (this))
3541 Type t = parameters.Types [i];
3542 Report.SymbolRelatedToPreviousError (t);
3543 if (this is Indexer)
3544 Report.Error (55, Location,
3545 "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3546 TypeManager.CSharpName (t), GetSignatureForError ());
3547 else if (this is Operator)
3548 Report.Error (57, Location,
3549 "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3550 TypeManager.CSharpName (t), GetSignatureForError ());
3552 Report.Error (51, Location,
3553 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3554 TypeManager.CSharpName (t), GetSignatureForError ());
3560 protected override bool DoDefine()
3562 if (!base.DoDefine ())
3565 if (IsExplicitImpl) {
3566 TypeExpr texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3570 InterfaceType = texpr.Type;
3572 if (!InterfaceType.IsInterface) {
3573 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3577 if (!Parent.PartialContainer.VerifyImplements (this))
3584 protected bool DoDefineBase ()
3587 throw new InternalErrorException ();
3590 ModFlags = Modifiers.PUBLIC |
3591 Modifiers.ABSTRACT |
3592 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
3594 flags = MethodAttributes.Public |
3595 MethodAttributes.Abstract |
3596 MethodAttributes.HideBySig |
3597 MethodAttributes.NewSlot |
3598 MethodAttributes.Virtual;
3600 if (!Parent.PartialContainer.MethodModifiersValid (this))
3603 flags = Modifiers.MethodAttr (ModFlags);
3606 if (IsExplicitImpl) {
3607 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3608 if (iface_texpr == null)
3611 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3612 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3613 GetSignatureForError ());
3617 InterfaceType = iface_texpr.Type;
3619 if (!InterfaceType.IsInterface) {
3620 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3624 if (!Parent.PartialContainer.VerifyImplements (this))
3627 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3633 public override void Emit()
3635 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3636 // We are more strict than Microsoft and report CS0626 as error
3637 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3638 Report.Error (626, Location,
3639 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3640 GetSignatureForError ());
3646 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3648 Report.SymbolRelatedToPreviousError (base_method);
3649 string base_name = TypeManager.GetFullNameSignature (base_method);
3650 string this_name = GetSignatureForError ();
3651 if (suffix != null) {
3652 base_name += suffix;
3653 this_name += suffix;
3656 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3657 this_name, Modifiers.GetDescription (ma), base_name);
3660 protected static string Error722 {
3662 return "`{0}': static types cannot be used as return types";
3667 /// Gets base method and its return type
3669 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3672 // The "short" name of this property / indexer / event. This is the
3673 // name without the explicit interface.
3675 public string ShortName
3677 get { return MemberName.Name; }
3678 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3681 public string GetFullName (MemberName name)
3683 if (!IsExplicitImpl)
3686 return InterfaceType.FullName.Replace ('+', '.') + "." + name.Name;
3689 protected override bool VerifyClsCompliance ()
3691 if (!base.VerifyClsCompliance ()) {
3692 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3693 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3696 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3697 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3702 if (GenericMethod != null)
3703 GenericMethod.VerifyClsCompliance ();
3708 public override bool IsUsed
3710 get { return IsExplicitImpl || base.IsUsed; }
3715 public abstract class MethodOrOperator : MethodCore, IMethodData
3717 public MethodBuilder MethodBuilder;
3718 ReturnParameter return_attributes;
3719 ListDictionary declarative_security;
3720 protected MethodData MethodData;
3722 static string[] attribute_targets = new string [] { "method", "return" };
3724 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3725 int allowed_mod, MemberName name,
3726 Attributes attrs, Parameters parameters)
3727 : base (parent, generic, type, mod, allowed_mod, name,
3732 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3734 if (a.Target == AttributeTargets.ReturnValue) {
3735 if (return_attributes == null)
3736 return_attributes = new ReturnParameter (MethodBuilder, Location);
3738 return_attributes.ApplyAttributeBuilder (a, cb);
3742 if (a.IsInternalMethodImplAttribute) {
3743 is_external_implementation = true;
3746 if (a.Type == TypeManager.dllimport_type) {
3747 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3748 if ((ModFlags & extern_static) != extern_static) {
3749 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3751 is_external_implementation = true;
3754 if (a.IsValidSecurityAttribute ()) {
3755 if (declarative_security == null)
3756 declarative_security = new ListDictionary ();
3757 a.ExtractSecurityPermissionSet (declarative_security);
3761 if (MethodBuilder != null)
3762 MethodBuilder.SetCustomAttribute (cb);
3765 public override AttributeTargets AttributeTargets {
3767 return AttributeTargets.Method;
3771 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3773 return new EmitContext (
3774 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3777 protected bool DefineGenericMethod ()
3779 if (!DoDefineBase ())
3783 if (GenericMethod != null) {
3784 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3785 if (!GenericMethod.Define (MethodBuilder))
3793 public bool ResolveMembers ()
3795 if (!DefineGenericMethod ())
3801 public override bool Define ()
3806 if (!CheckAbstractAndExtern (block != null))
3809 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3810 for (int i = 0; i < Parameters.Count; ++i ) {
3811 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
3812 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3813 GetSignatureForError ());
3822 if (block != null && block.IsIterator && !(Parent is IteratorStorey))
3823 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
3825 if (IsPartialDefinition) {
3826 caching_flags &= ~Flags.Excluded_Undetected;
3827 caching_flags |= Flags.Excluded;
3828 // Add to member cache only when a partial method implementation is not there
3829 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3830 MethodBase mb = new PartialMethodDefinitionInfo (this);
3831 Parent.MemberCache.AddMember (mb, this);
3832 TypeManager.AddMethod (mb, this);
3838 MethodData = new MethodData (
3839 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3841 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
3844 MethodBuilder = MethodData.MethodBuilder;
3847 if (MethodBuilder.IsGenericMethod)
3848 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3851 Parent.MemberCache.AddMember (MethodBuilder, this);
3853 if (!TypeManager.IsGenericParameter (MemberType)) {
3854 if (MemberType.IsAbstract && MemberType.IsSealed) {
3855 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3863 public override void Emit ()
3866 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
3867 MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
3868 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3869 MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
3871 if (OptAttributes != null)
3872 OptAttributes.Emit ();
3874 if (declarative_security != null) {
3875 foreach (DictionaryEntry de in declarative_security) {
3876 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3883 protected void Error_ConditionalAttributeIsNotValid ()
3885 Report.Error (577, Location,
3886 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3887 GetSignatureForError ());
3890 public bool IsPartialDefinition {
3892 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3896 public bool IsPartialImplementation {
3898 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3902 public override string[] ValidAttributeTargets {
3904 return attribute_targets;
3908 #region IMethodData Members
3910 public CallingConventions CallingConventions {
3912 CallingConventions cc = Parameters.CallingConvention;
3914 if ((ModFlags & Modifiers.STATIC) == 0)
3915 cc |= CallingConventions.HasThis;
3917 // FIXME: How is `ExplicitThis' used in C#?
3923 public Type ReturnType {
3929 public MemberName MethodName {
3935 protected override bool CheckBase ()
3937 if (!base.CheckBase ())
3940 // TODO: Destructor should derive from MethodCore
3941 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3942 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3943 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3951 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3953 public bool IsExcluded () {
3954 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3955 return (caching_flags & Flags.Excluded) != 0;
3957 caching_flags &= ~Flags.Excluded_Undetected;
3959 if (base_method == null) {
3960 if (OptAttributes == null)
3963 if (TypeManager.conditional_attribute_type == null)
3966 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3971 foreach (Attribute a in attrs) {
3972 string condition = a.GetConditionalAttributeValue ();
3973 if (condition == null)
3976 if (Location.CompilationUnit.IsConditionalDefined (condition))
3980 caching_flags |= Flags.Excluded;
3984 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
3986 if (AttributeTester.IsConditionalMethodExcluded (base_method, Location)) {
3987 caching_flags |= Flags.Excluded;
3993 if (md.IsExcluded ()) {
3994 caching_flags |= Flags.Excluded;
4000 GenericMethod IMethodData.GenericMethod {
4002 return GenericMethod;
4006 public virtual void EmitExtraSymbolInfo (SourceMethod source)
4013 public class SourceMethod : IMethodDef
4016 SourceMethodBuilder builder;
4018 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
4020 this.method = method;
4022 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
4025 public string Name {
4026 get { return method.Name; }
4031 if (method is MethodBuilder)
4032 return ((MethodBuilder) method).GetToken ().Token;
4033 else if (method is ConstructorBuilder)
4034 return ((ConstructorBuilder) method).GetToken ().Token;
4036 throw new NotSupportedException ();
4040 public void CloseMethod ()
4042 SymbolWriter.CloseMethod ();
4045 public void SetRealMethodName (string name)
4047 if (builder != null)
4048 builder.SetRealMethodName (name);
4051 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4053 if (!SymbolWriter.HasSymbolWriter)
4058 Location start_loc = block.StartLocation;
4059 if (start_loc.IsNull)
4062 ICompileUnit compile_unit = start_loc.CompilationUnit;
4063 if (compile_unit == null)
4066 return new SourceMethod (parent, method, compile_unit);
4070 public class Method : MethodOrOperator {
4073 /// Modifiers allowed in a class declaration
4075 const int AllowedModifiers =
4078 Modifiers.PROTECTED |
4079 Modifiers.INTERNAL |
4084 Modifiers.OVERRIDE |
4085 Modifiers.ABSTRACT |
4089 const int AllowedInterfaceModifiers =
4090 Modifiers.NEW | Modifiers.UNSAFE;
4092 public Method (DeclSpace parent, GenericMethod generic,
4093 FullNamedExpression return_type, int mod,
4094 MemberName name, Parameters parameters, Attributes attrs)
4095 : base (parent, generic, return_type, mod,
4096 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
4097 name, attrs, parameters)
4101 protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
4102 MemberName name, Parameters parameters, Attributes attrs)
4103 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
4107 public override string GetSignatureForError()
4109 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4112 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
4114 Report.Error (17, location,
4115 "Program `{0}' has more than one entry point defined: `{1}'",
4116 CodeGen.FileName, TypeManager.CSharpSignature(b));
4119 bool IsEntryPoint ()
4121 if (ReturnType != TypeManager.void_type &&
4122 ReturnType != TypeManager.int32_type)
4125 if (Parameters.Count == 0)
4128 if (Parameters.Count > 1)
4131 Type t = Parameters.Types [0];
4132 return t.IsArray && t.GetArrayRank () == 1 &&
4133 TypeManager.GetElementType (t) == TypeManager.string_type &&
4134 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4137 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4139 if (a.Type == TypeManager.conditional_attribute_type) {
4140 if (IsExplicitImpl) {
4141 Error_ConditionalAttributeIsNotValid ();
4145 if (ReturnType != TypeManager.void_type) {
4146 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4150 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4151 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4156 Report.Error (582, Location, "Conditional not valid on interface members");
4160 if (MethodData.implementing != null) {
4161 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4162 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4163 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4167 for (int i = 0; i < Parameters.Count; ++i) {
4168 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
4169 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4175 if (a.Type == TypeManager.extension_attribute_type) {
4176 a.Error_MisusedExtensionAttribute ();
4180 base.ApplyAttributeBuilder (a, cb);
4183 public bool CheckForDuplications ()
4185 ArrayList ar = Parent.PartialContainer.Properties;
4187 for (int i = 0; i < ar.Count; ++i) {
4188 PropertyBase pb = (PropertyBase) ar [i];
4189 if (pb.AreAccessorsDuplicateImplementation (this))
4194 ar = Parent.PartialContainer.Indexers;
4196 for (int i = 0; i < ar.Count; ++i) {
4197 PropertyBase pb = (PropertyBase) ar [i];
4198 if (pb.AreAccessorsDuplicateImplementation (this))
4209 public override bool Define ()
4211 if (!base.Define ())
4214 if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
4215 Error1599 (Location, ReturnType);
4219 if (ReturnType == TypeManager.void_type && Parameters.Count == 0 &&
4220 Name == "Finalize" && !(this is Destructor)) {
4221 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4224 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4225 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4226 Parent.PartialContainer.Mark_HasEquals ();
4227 else if (Parameters.IsEmpty && Name == "GetHashCode")
4228 Parent.PartialContainer.Mark_HasGetHashCode ();
4231 if ((ModFlags & Modifiers.STATIC) == 0)
4234 if (Parameters.HasExtensionMethodType) {
4235 if (Parent.IsStaticClass && !Parent.IsGeneric) {
4236 if (!Parent.IsTopLevel)
4237 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4238 GetSignatureForError ());
4240 if (TypeManager.extension_attribute_type == null) {
4241 Report.Error (1110, Location,
4242 "`{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",
4243 GetSignatureForError ());
4244 } else if (TypeManager.extension_attribute_attr == null) {
4245 ConstructorInfo ci = TypeManager.GetPredefinedConstructor (
4246 TypeManager.extension_attribute_type, Location, System.Type.EmptyTypes);
4249 TypeManager.extension_attribute_attr = new CustomAttributeBuilder (ci, new object [0]);
4252 ModFlags |= Modifiers.METHOD_EXTENSION;
4253 Parent.ModFlags |= Modifiers.METHOD_EXTENSION;
4254 CodeGen.Assembly.HasExtensionMethods = true;
4256 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4257 GetSignatureForError ());
4262 // This is used to track the Entry Point,
4264 if (RootContext.NeedsEntryPoint &&
4266 (RootContext.MainClass == null ||
4267 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4268 if (IsEntryPoint ()) {
4270 if (RootContext.EntryPoint == null) {
4271 if (Parent.IsGeneric || MemberName.IsGeneric) {
4272 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4273 GetSignatureForError ());
4275 IMethodData md = TypeManager.GetMethod (MethodBuilder);
4276 md.SetMemberIsUsed ();
4278 RootContext.EntryPoint = MethodBuilder;
4279 RootContext.EntryPointLocation = Location;
4282 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
4283 Error_DuplicateEntryPoint (MethodBuilder, Location);
4286 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4287 GetSignatureForError ());
4297 public override void Emit ()
4300 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4301 if (IsPartialDefinition) {
4303 // Do attribute checks only when partial implementation does not exist
4305 if (MethodBuilder == null)
4311 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
4312 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4313 GetSignatureForError ());
4315 MethodData.Emit (Parent);
4319 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4320 MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
4326 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4327 Location, MethodBuilder);
4332 public override bool EnableOverloadChecks (MemberCore overload)
4334 // TODO: It can be deleted when members will be defined in correct order
4335 if (overload is Operator)
4336 return overload.EnableOverloadChecks (this);
4338 if (overload is Indexer)
4341 return base.EnableOverloadChecks (overload);
4344 public static void Error1599 (Location loc, Type t)
4346 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4349 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4351 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4352 Parent.TypeBuilder, Name, Parameters, GenericMethod, false);
4357 if (mi.IsSpecialName)
4360 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4364 public void SetPartialDefinition (Method methodDefinition)
4366 caching_flags |= Flags.PartialDefinitionExists;
4367 methodDefinition.MethodBuilder = MethodBuilder;
4368 if (methodDefinition.attributes == null)
4371 if (attributes == null) {
4372 attributes = methodDefinition.attributes;
4374 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4378 protected override bool VerifyClsCompliance ()
4380 if (!base.VerifyClsCompliance ())
4383 if (ParameterInfo.Count > 0) {
4384 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4386 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4393 public abstract class ConstructorInitializer : ExpressionStatement
4395 ArrayList argument_list;
4396 MethodGroupExpr base_constructor_group;
4398 public ConstructorInitializer (ArrayList argument_list, Location loc)
4400 this.argument_list = argument_list;
4404 public ArrayList Arguments {
4406 return argument_list;
4410 public override Expression CreateExpressionTree (EmitContext ec)
4412 throw new NotSupportedException ("ET");
4415 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4417 if (argument_list != null){
4418 foreach (Argument a in argument_list){
4419 if (!a.Resolve (ec, loc))
4424 if (this is ConstructorBaseInitializer) {
4425 if (ec.ContainerType.BaseType == null)
4428 type = ec.ContainerType.BaseType;
4429 if (ec.ContainerType.IsValueType) {
4430 Report.Error (522, loc,
4431 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4436 // It is legal to have "this" initializers that take no arguments
4437 // in structs, they are just no-ops.
4439 // struct D { public D (int a) : this () {}
4441 if (ec.ContainerType.IsValueType && argument_list == null)
4444 type = ec.ContainerType;
4447 base_constructor_group = MemberLookupFinal (
4448 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4449 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4450 loc) as MethodGroupExpr;
4452 if (base_constructor_group == null)
4455 base_constructor_group = base_constructor_group.OverloadResolve (
4456 ec, ref argument_list, false, loc);
4458 if (base_constructor_group == null)
4461 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4463 if (base_ctor == caller_builder){
4464 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4470 public override Expression DoResolve (EmitContext ec)
4472 throw new NotSupportedException ();
4475 public override void Emit (EmitContext ec)
4477 // It can be null for static initializers
4478 if (base_constructor_group == null)
4483 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4485 base_constructor_group.EmitCall (ec, argument_list);
4488 public override void EmitStatement (EmitContext ec)
4494 public class ConstructorBaseInitializer : ConstructorInitializer {
4495 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4496 base (argument_list, l)
4501 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4502 public GeneratedBaseInitializer (Location loc):
4508 public class ConstructorThisInitializer : ConstructorInitializer {
4509 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4510 base (argument_list, l)
4515 public class Constructor : MethodCore, IMethodData {
4516 public ConstructorBuilder ConstructorBuilder;
4517 public ConstructorInitializer Initializer;
4518 ListDictionary declarative_security;
4519 bool has_compliant_args;
4522 // Modifiers allowed for a constructor.
4524 public const int AllowedModifiers =
4526 Modifiers.PROTECTED |
4527 Modifiers.INTERNAL |
4533 static readonly string[] attribute_targets = new string [] { "method" };
4536 // The spec claims that static is not permitted, but
4537 // my very own code has static constructors.
4539 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4540 ConstructorInitializer init, Location loc)
4541 : base (parent, null, null, mod, AllowedModifiers,
4542 new MemberName (name, loc), null, args)
4547 public bool HasCompliantArgs {
4548 get { return has_compliant_args; }
4551 public override AttributeTargets AttributeTargets {
4552 get { return AttributeTargets.Constructor; }
4556 // Returns true if this is a default constructor
4558 public bool IsDefault ()
4560 if ((ModFlags & Modifiers.STATIC) != 0)
4561 return Parameters.IsEmpty;
4563 return Parameters.IsEmpty &&
4564 (Initializer is ConstructorBaseInitializer) &&
4565 (Initializer.Arguments == null);
4568 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4570 if (a.IsValidSecurityAttribute ()) {
4571 if (declarative_security == null) {
4572 declarative_security = new ListDictionary ();
4574 a.ExtractSecurityPermissionSet (declarative_security);
4578 if (a.IsInternalMethodImplAttribute) {
4579 is_external_implementation = true;
4582 ConstructorBuilder.SetCustomAttribute (cb);
4585 protected override bool CheckBase ()
4587 if ((ModFlags & Modifiers.STATIC) != 0) {
4588 if (!Parameters.IsEmpty) {
4589 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4590 GetSignatureForError ());
4594 // the rest can be ignored
4598 // Check whether arguments were correct.
4599 if (!DefineParameters (Parameters))
4602 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4603 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorBuilder.ConstructorName,
4606 if (Parent.PartialContainer.Kind == Kind.Struct) {
4607 if (Parameters.Count == 0) {
4608 Report.Error (568, Location,
4609 "Structs cannot contain explicit parameterless constructors");
4614 CheckProtectedModifier ();
4620 // Creates the ConstructorBuilder
4622 public override bool Define ()
4624 if (ConstructorBuilder != null)
4627 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4628 MethodAttributes.SpecialName);
4630 if ((ModFlags & Modifiers.STATIC) != 0) {
4631 ca |= MethodAttributes.Static | MethodAttributes.Private;
4633 ca |= MethodAttributes.HideBySig;
4635 if ((ModFlags & Modifiers.PUBLIC) != 0)
4636 ca |= MethodAttributes.Public;
4637 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4638 if ((ModFlags & Modifiers.INTERNAL) != 0)
4639 ca |= MethodAttributes.FamORAssem;
4641 ca |= MethodAttributes.Family;
4642 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4643 ca |= MethodAttributes.Assembly;
4645 ca |= MethodAttributes.Private;
4648 if (!CheckAbstractAndExtern (block != null))
4651 // Check if arguments were correct.
4655 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4656 ca, CallingConventions,
4657 Parameters.GetEmitTypes ());
4659 if (Parent.PartialContainer.IsComImport) {
4660 if (!IsDefault ()) {
4661 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4662 Parent.GetSignatureForError ());
4665 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4668 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4669 TypeManager.AddMethod (ConstructorBuilder, this);
4671 // It's here only to report an error
4672 if (block != null && block.IsIterator) {
4673 member_type = TypeManager.void_type;
4674 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
4683 public override void Emit ()
4685 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4686 ConstructorBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
4688 if (OptAttributes != null)
4689 OptAttributes.Emit ();
4693 EmitContext ec = CreateEmitContext (null, null);
4696 // If we use a "this (...)" constructor initializer, then
4697 // do not emit field initializers, they are initialized in the other constructor
4699 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4700 !(Initializer is ConstructorThisInitializer);
4702 if (emit_field_initializers)
4703 Parent.PartialContainer.ResolveFieldInitializers (ec);
4705 if (block != null) {
4706 // If this is a non-static `struct' constructor and doesn't have any
4707 // initializer, it must initialize all of the struct's fields.
4708 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4709 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4710 block.AddThisVariable (Parent, Location);
4712 if (!block.ResolveMeta (ec, ParameterInfo))
4715 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4716 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4717 Initializer = new GeneratedBaseInitializer (Location);
4720 // Spec mandates that Initializers will not have `this' access
4722 if (Initializer != null) {
4724 Initializer.Resolve (ConstructorBuilder, ec);
4725 ec.IsStatic = false;
4726 block.AddScopeStatement (new StatementExpression (Initializer));
4731 Parameters.ApplyAttributes (ConstructorBuilder);
4733 SourceMethod source = null;
4735 ec.OmitDebuggingInfo = true;
4737 source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4739 bool unreachable = false;
4740 if (block != null) {
4741 if (!ec.ResolveTopBlock (null, block, ParameterInfo, this, out unreachable))
4744 ec.EmitMeta (block);
4746 if (Report.Errors > 0)
4749 ec.EmitResolvedTopBlock (block, unreachable);
4753 source.CloseMethod ();
4755 if (declarative_security != null) {
4756 foreach (DictionaryEntry de in declarative_security) {
4757 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4764 // Is never override
4765 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4770 public override string GetSignatureForError()
4772 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4775 public override string[] ValidAttributeTargets {
4777 return attribute_targets;
4781 protected override bool VerifyClsCompliance ()
4783 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4787 if (ParameterInfo.Count > 0) {
4788 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4790 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4792 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4793 foreach (Type param in Parameters.Types) {
4794 if (param.IsArray) {
4800 has_compliant_args = true;
4804 #region IMethodData Members
4806 public System.Reflection.CallingConventions CallingConventions {
4808 CallingConventions cc = Parameters.CallingConvention;
4810 if (Parent.PartialContainer.Kind == Kind.Class)
4811 if ((ModFlags & Modifiers.STATIC) == 0)
4812 cc |= CallingConventions.HasThis;
4814 // FIXME: How is `ExplicitThis' used in C#?
4820 public MemberName MethodName {
4826 public Type ReturnType {
4832 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4834 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4835 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
4836 ec.CurrentBlock = block;
4840 public bool IsExcluded()
4845 GenericMethod IMethodData.GenericMethod {
4851 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4858 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4860 public interface IMethodData
4862 CallingConventions CallingConventions { get; }
4863 Location Location { get; }
4864 MemberName MethodName { get; }
4865 Type ReturnType { get; }
4866 GenericMethod GenericMethod { get; }
4867 Parameters ParameterInfo { get; }
4869 Attributes OptAttributes { get; }
4870 ToplevelBlock Block { get; set; }
4872 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4873 ObsoleteAttribute GetObsoleteAttribute ();
4874 string GetSignatureForError ();
4876 bool IsClsComplianceRequired ();
4877 void SetMemberIsUsed ();
4878 void EmitExtraSymbolInfo (SourceMethod source);
4882 // Encapsulates most of the Method's state
4884 public class MethodData {
4886 static FieldInfo methodbuilder_attrs_field;
4888 public readonly IMethodData method;
4890 public readonly GenericMethod GenericMethod;
4893 // Are we implementing an interface ?
4895 public MethodInfo implementing;
4900 protected InterfaceMemberBase member;
4901 protected int modifiers;
4902 protected MethodAttributes flags;
4903 protected Type declaring_type;
4904 protected MethodInfo parent_method;
4906 MethodBuilder builder = null;
4907 public MethodBuilder MethodBuilder {
4913 public Type DeclaringType {
4915 return declaring_type;
4919 public MethodData (InterfaceMemberBase member,
4920 int modifiers, MethodAttributes flags, IMethodData method)
4922 this.member = member;
4923 this.modifiers = modifiers;
4926 this.method = method;
4929 public MethodData (InterfaceMemberBase member,
4930 int modifiers, MethodAttributes flags,
4931 IMethodData method, MethodBuilder builder,
4932 GenericMethod generic, MethodInfo parent_method)
4933 : this (member, modifiers, flags, method)
4935 this.builder = builder;
4936 this.GenericMethod = generic;
4937 this.parent_method = parent_method;
4940 public bool Define (DeclSpace parent, string method_full_name)
4942 string name = method.MethodName.Basename;
4944 TypeContainer container = parent.PartialContainer;
4946 PendingImplementation pending = container.PendingImplementations;
4947 if (pending != null){
4948 implementing = pending.IsInterfaceMethod (name, member.InterfaceType, this);
4950 if (member.InterfaceType != null){
4951 if (implementing == null){
4952 if (member is PropertyBase) {
4953 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4954 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4955 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4958 Report.Error (539, method.Location,
4959 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4960 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4964 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4965 Report.SymbolRelatedToPreviousError (implementing);
4966 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4967 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4971 if (implementing != null) {
4972 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4973 if (prop_method == null) {
4974 if (TypeManager.IsSpecialMethod (implementing)) {
4975 Report.SymbolRelatedToPreviousError (implementing);
4976 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
4977 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
4978 implementing.Name.StartsWith ("get_") ? "get" : "set");
4980 } else if (implementing.DeclaringType.IsInterface) {
4981 if (!implementing.IsSpecialName) {
4982 Report.SymbolRelatedToPreviousError (implementing);
4983 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4984 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4987 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4988 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4989 Report.SymbolRelatedToPreviousError (implementing);
4990 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4991 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
5000 // For implicit implementations, make sure we are public, for
5001 // explicit implementations, make sure we are private.
5003 if (implementing != null){
5005 // Setting null inside this block will trigger a more
5006 // verbose error reporting for missing interface implementations
5008 // The "candidate" function has been flagged already
5009 // but it wont get cleared
5011 if (member.IsExplicitImpl){
5012 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
5013 Report.SymbolRelatedToPreviousError (implementing);
5014 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
5015 method.GetSignatureForError ());
5019 if (implementing.DeclaringType.IsInterface) {
5021 // If this is an interface method implementation,
5022 // check for public accessibility
5024 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5026 implementing = null;
5028 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5029 // We may never be private.
5030 implementing = null;
5032 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5034 // We may be protected if we're overriding something.
5036 implementing = null;
5041 // Static is not allowed
5043 if ((modifiers & Modifiers.STATIC) != 0){
5044 implementing = null;
5049 // If implementing is still valid, set flags
5051 if (implementing != null){
5053 // When implementing interface methods, set NewSlot
5054 // unless, we are overwriting a method.
5056 if (implementing.DeclaringType.IsInterface){
5057 if ((modifiers & Modifiers.OVERRIDE) == 0)
5058 flags |= MethodAttributes.NewSlot;
5061 MethodAttributes.Virtual |
5062 MethodAttributes.HideBySig;
5064 // Set Final unless we're virtual, abstract or already overriding a method.
5065 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5066 flags |= MethodAttributes.Final;
5069 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
5071 if (builder == null)
5074 if (container.CurrentType != null)
5075 declaring_type = container.CurrentType;
5077 declaring_type = container.TypeBuilder;
5079 if (implementing != null){
5081 // clear the pending implemntation flag
5083 pending.ImplementMethod (name, member.InterfaceType, this, member.IsExplicitImpl);
5085 if (member.IsExplicitImpl)
5086 container.TypeBuilder.DefineMethodOverride (
5087 builder, implementing);
5090 TypeManager.AddMethod (builder, method);
5092 if (GenericMethod != null) {
5093 bool is_override = member.IsExplicitImpl |
5094 ((modifiers & Modifiers.OVERRIDE) != 0);
5096 if (implementing != null)
5097 parent_method = implementing;
5099 EmitContext ec = method.CreateEmitContext (container, null);
5100 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5109 /// Create the MethodBuilder for the method
5111 void DefineMethodBuilder (TypeContainer container, string method_name, Parameters param)
5113 if (builder == null) {
5114 builder = container.TypeBuilder.DefineMethod (
5115 method_name, flags, method.CallingConventions,
5116 method.ReturnType, param.GetEmitTypes ());
5122 // Generic method has been already defined to resolve method parameters
5123 // correctly when they use type parameters
5125 builder.SetParameters (param.GetEmitTypes ());
5126 builder.SetReturnType (method.ReturnType);
5128 if (builder.Attributes != flags) {
5130 if (methodbuilder_attrs_field == null)
5131 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5132 methodbuilder_attrs_field.SetValue (builder, flags);
5134 Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5138 throw new InternalErrorException ();
5145 public void Emit (DeclSpace parent)
5147 ToplevelBlock block = method.Block;
5151 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5153 ec = method.CreateEmitContext (parent, null);
5155 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5157 if (GenericMethod != null)
5158 GenericMethod.EmitAttributes ();
5160 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5163 // Handle destructors specially
5165 // FIXME: This code generates buggy code
5167 if (member is Destructor)
5168 EmitDestructor (ec, block);
5170 ec.EmitTopBlock (method, block);
5172 if (source != null) {
5173 method.EmitExtraSymbolInfo (source);
5174 source.CloseMethod ();
5178 void EmitDestructor (EmitContext ec, ToplevelBlock block)
5180 ILGenerator ig = ec.ig;
5182 Label finish = ig.DefineLabel ();
5184 block.SetDestructor ();
5186 ig.BeginExceptionBlock ();
5187 ec.ReturnLabel = finish;
5188 ec.HasReturnLabel = true;
5189 ec.EmitTopBlock (method, block);
5191 // ig.MarkLabel (finish);
5192 ig.BeginFinallyBlock ();
5194 if (ec.ContainerType.BaseType != null) {
5195 Expression member_lookup = Expression.MemberLookup (
5196 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
5197 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
5199 if (member_lookup != null){
5200 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
5202 ig.Emit (OpCodes.Ldarg_0);
5203 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
5207 ig.EndExceptionBlock ();
5208 //ig.MarkLabel (ec.ReturnLabel);
5209 ig.Emit (OpCodes.Ret);
5213 // TODO: Should derive from MethodCore
5214 public class Destructor : Method
5216 const int AllowedModifiers =
5220 static string[] attribute_targets = new string [] { "method" };
5222 public Destructor (DeclSpace parent, FullNamedExpression return_type, int mod,
5223 string name, Parameters parameters, Attributes attrs,
5225 : base (parent, return_type, mod, AllowedModifiers, new MemberName (name, l),
5228 ModFlags &= ~Modifiers.PRIVATE;
5229 if (!RootContext.StdLib && parent.Name == "System.Object")
5230 ModFlags |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
5232 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
5235 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5237 if (a.Type == TypeManager.conditional_attribute_type) {
5238 Error_ConditionalAttributeIsNotValid ();
5242 base.ApplyAttributeBuilder (a, cb);
5245 public override string GetSignatureForError ()
5247 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5250 public override string[] ValidAttributeTargets {
5252 return attribute_targets;
5257 abstract public class MemberBase : MemberCore {
5258 protected FullNamedExpression type_name;
5259 public readonly DeclSpace ds;
5260 public readonly GenericMethod GenericMethod;
5263 // The type of this property / indexer / event
5265 protected Type member_type;
5266 public Type MemberType {
5268 // TODO: Who wrote this, ResolveAsTypeTerminal can have side effects
5269 if (member_type == null && type_name != null) {
5270 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
5271 type_name = type_name.ResolveAsTypeTerminal (rc, false);
5272 if (type_name != null) {
5273 member_type = type_name.Type;
5281 // The constructor is only exposed to our children
5283 protected MemberBase (DeclSpace parent, GenericMethod generic,
5284 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5285 MemberName name, Attributes attrs)
5286 : base (parent, name, attrs)
5288 this.ds = generic != null ? generic : (DeclSpace) parent;
5289 this.type_name = type;
5290 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5291 GenericMethod = generic;
5292 if (GenericMethod != null)
5293 GenericMethod.ModFlags = ModFlags;
5296 protected virtual bool CheckBase ()
5298 CheckProtectedModifier ();
5303 protected virtual bool DoDefine ()
5305 if (MemberType == null)
5308 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5309 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5310 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5311 GetSignatureForError (), Parent.GetSignatureForError ());
5315 // verify accessibility
5316 if (!IsAccessibleAs (MemberType)) {
5317 Report.SymbolRelatedToPreviousError (MemberType);
5318 if (this is Property)
5319 Report.Error (53, Location,
5320 "Inconsistent accessibility: property type `" +
5321 TypeManager.CSharpName (MemberType) + "' is less " +
5322 "accessible than property `" + GetSignatureForError () + "'");
5323 else if (this is Indexer)
5324 Report.Error (54, Location,
5325 "Inconsistent accessibility: indexer return type `" +
5326 TypeManager.CSharpName (MemberType) + "' is less " +
5327 "accessible than indexer `" + GetSignatureForError () + "'");
5328 else if (this is MethodCore) {
5329 if (this is Operator)
5330 Report.Error (56, Location,
5331 "Inconsistent accessibility: return type `" +
5332 TypeManager.CSharpName (MemberType) + "' is less " +
5333 "accessible than operator `" + GetSignatureForError () + "'");
5335 Report.Error (50, Location,
5336 "Inconsistent accessibility: return type `" +
5337 TypeManager.CSharpName (MemberType) + "' is less " +
5338 "accessible than method `" + GetSignatureForError () + "'");
5340 Report.Error (52, Location,
5341 "Inconsistent accessibility: field type `" +
5342 TypeManager.CSharpName (MemberType) + "' is less " +
5343 "accessible than field `" + GetSignatureForError () + "'");
5351 protected bool IsTypePermitted ()
5353 if (TypeManager.IsSpecialType (MemberType)) {
5354 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5362 // Abstract class for all fields
5364 abstract public class FieldBase : MemberBase {
5365 public FieldBuilder FieldBuilder;
5366 public Status status;
5367 protected Expression initializer;
5370 public enum Status : byte {
5371 HAS_OFFSET = 4 // Used by FieldMember.
5374 static readonly string[] attribute_targets = new string [] { "field" };
5376 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5377 int allowed_mod, MemberName name, Attributes attrs)
5378 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5381 if ((mod & Modifiers.ABSTRACT) != 0)
5382 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5385 public override AttributeTargets AttributeTargets {
5387 return AttributeTargets.Field;
5391 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5393 if (a.Type == TypeManager.field_offset_attribute_type) {
5394 status |= Status.HAS_OFFSET;
5396 if (!Parent.PartialContainer.HasExplicitLayout) {
5397 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5401 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5402 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5408 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5409 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5415 if (a.Type == TypeManager.marshal_as_attr_type) {
5416 UnmanagedMarshal marshal = a.GetMarshal (this);
5417 if (marshal != null) {
5418 FieldBuilder.SetMarshal (marshal);
5423 if ((a.HasSecurityAttribute)) {
5424 a.Error_InvalidSecurityParent ();
5428 FieldBuilder.SetCustomAttribute (cb);
5431 protected override bool CheckBase ()
5433 if (!base.CheckBase ())
5436 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5437 if (conflict_symbol == null) {
5438 if ((ModFlags & Modifiers.NEW) != 0) {
5439 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5444 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5445 Report.SymbolRelatedToPreviousError (conflict_symbol);
5446 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5447 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5453 public override bool Define()
5455 if (MemberType == null || member_type == null)
5458 if (TypeManager.IsGenericParameter (MemberType))
5461 if (MemberType.IsSealed && MemberType.IsAbstract) {
5462 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5472 if (!IsTypePermitted ())
5479 // Represents header string for documentation comment.
5481 public override string DocCommentHeader {
5482 get { return "F:"; }
5485 public override void Emit ()
5488 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
5489 FieldBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
5492 if (OptAttributes != null) {
5493 OptAttributes.Emit ();
5496 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5497 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
5503 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5505 Report.SymbolRelatedToPreviousError (static_class);
5506 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5510 public Expression Initializer {
5512 if (value != null) {
5513 this.initializer = value;
5518 protected virtual bool IsFieldClsCompliant {
5520 if (FieldBuilder == null)
5523 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5527 public override string[] ValidAttributeTargets
5530 return attribute_targets;
5534 protected override bool VerifyClsCompliance ()
5536 if (!base.VerifyClsCompliance ())
5539 if (!IsFieldClsCompliant) {
5540 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
5541 GetSignatureForError ());
5546 public void SetAssigned ()
5548 caching_flags |= Flags.IsAssigned;
5552 interface IFixedBuffer
5554 FieldInfo Element { get; }
5555 Type ElementType { get; }
5558 public class FixedFieldExternal: IFixedBuffer
5560 FieldInfo element_field;
5562 public FixedFieldExternal (FieldInfo fi)
5564 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5567 #region IFixedField Members
5569 public FieldInfo Element {
5571 return element_field;
5575 public Type ElementType {
5577 return element_field.FieldType;
5585 /// Fixed buffer implementation
5587 public class FixedField : FieldBase, IFixedBuffer
5589 public const string FixedElementName = "FixedElementField";
5590 static int GlobalCounter = 0;
5591 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5592 static FieldInfo[] fi;
5594 TypeBuilder fixed_buffer_type;
5595 FieldBuilder element;
5596 Expression size_expr;
5598 const int AllowedModifiers =
5601 Modifiers.PROTECTED |
5602 Modifiers.INTERNAL |
5605 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5606 Expression size_expr, Attributes attrs, Location loc):
5607 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5609 if (RootContext.Version == LanguageVersion.ISO_1)
5610 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5612 this.size_expr = size_expr;
5615 public override bool Define()
5617 if (!Parent.IsInUnsafeScope)
5618 Expression.UnsafeError (Location);
5620 if (!base.Define ())
5623 if (!TypeManager.IsPrimitiveType (MemberType)) {
5624 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",
5625 GetSignatureForError ());
5628 // Create nested fixed buffer container
5629 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5630 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, CodeGen.Module.DefaultCharSetType |
5631 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5633 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5634 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5636 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5637 Parent.MemberCache.AddMember (FieldBuilder, this);
5638 TypeManager.RegisterFieldBase (FieldBuilder, this);
5643 public override void Emit()
5645 if (Parent.PartialContainer.Kind != Kind.Struct) {
5646 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5647 GetSignatureForError ());
5650 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5651 Constant c = size_expr.ResolveAsConstant (ec, this);
5655 IntConstant buffer_size_const = c.ImplicitConversionRequired (ec, TypeManager.int32_type, Location) as IntConstant;
5656 if (buffer_size_const == null)
5659 int buffer_size = buffer_size_const.Value;
5661 if (buffer_size <= 0) {
5662 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5666 int type_size = Expression.GetTypeSize (MemberType);
5668 if (buffer_size > int.MaxValue / type_size) {
5669 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5670 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5674 buffer_size *= type_size;
5675 EmitFieldSize (buffer_size);
5679 // Emit compiler generated fixed buffer type attribute
5681 CustomAttributeBuilder cab = TypeManager.unsafe_value_type_attr;
5683 Type attr_type = TypeManager.CoreLookupType (
5684 "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute", Kind.Class, true);
5686 if (attr_type != null) {
5687 ConstructorInfo ci = TypeManager.GetPredefinedConstructor (attr_type, Location);
5689 cab = new CustomAttributeBuilder (ci, new object [0]);
5690 TypeManager.unsafe_value_type_attr = cab;
5696 fixed_buffer_type.SetCustomAttribute (cab);
5701 void EmitFieldSize (int buffer_size)
5703 if (TypeManager.struct_layout_attribute_type == null) {
5704 TypeManager.struct_layout_attribute_type = TypeManager.CoreLookupType (
5705 "System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, true);
5707 if (TypeManager.struct_layout_attribute_type == null)
5712 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5714 object [] fi_val = new object [] { buffer_size };
5716 if (TypeManager.struct_layout_attribute_ctor == null) {
5717 TypeManager.struct_layout_attribute_ctor = TypeManager.GetPredefinedConstructor (
5718 TypeManager.struct_layout_attribute_type, Location, TypeManager.short_type);
5719 if (TypeManager.struct_layout_attribute_ctor == null)
5723 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5724 ctor_args, fi, fi_val);
5725 fixed_buffer_type.SetCustomAttribute (cab);
5728 // Don't emit FixedBufferAttribute attribute for private types
5730 if ((ModFlags & Modifiers.PRIVATE) != 0)
5733 if (TypeManager.fixed_buffer_attr_ctor == null) {
5734 if (TypeManager.fixed_buffer_attr_type == null) {
5735 TypeManager.fixed_buffer_attr_type = TypeManager.CoreLookupType (
5736 "System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, true);
5738 if (TypeManager.fixed_buffer_attr_type == null)
5742 TypeManager.fixed_buffer_attr_ctor = TypeManager.GetPredefinedConstructor (TypeManager.fixed_buffer_attr_type,
5743 Location, TypeManager.type_type, TypeManager.int32_type);
5745 if (TypeManager.fixed_buffer_attr_ctor == null)
5749 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object [] { MemberType, buffer_size });
5750 FieldBuilder.SetCustomAttribute (cab);
5753 protected override bool IsFieldClsCompliant {
5759 public void SetCharSet (TypeAttributes ta)
5761 TypeAttributes cta = fixed_buffer_type.Attributes;
5762 if ((cta & TypeAttributes.UnicodeClass) != (ta & TypeAttributes.UnicodeClass))
5763 SetTypeBuilderCharSet ((cta & ~TypeAttributes.AutoClass) | TypeAttributes.UnicodeClass);
5764 else if ((cta & TypeAttributes.AutoClass) != (ta & TypeAttributes.AutoClass))
5765 SetTypeBuilderCharSet ((cta & ~TypeAttributes.UnicodeClass) | TypeAttributes.AutoClass);
5766 else if (cta == 0 && ta != 0)
5767 SetTypeBuilderCharSet (cta & ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass));
5770 void SetTypeBuilderCharSet (TypeAttributes ta)
5772 MethodInfo mi = typeof (TypeBuilder).GetMethod ("SetCharSet", BindingFlags.Instance | BindingFlags.NonPublic);
5774 Report.RuntimeMissingSupport (Location, "TypeBuilder::SetCharSet");
5776 mi.Invoke (fixed_buffer_type, new object [] { ta });
5780 #region IFixedField Members
5782 public FieldInfo Element {
5788 public Type ElementType {
5798 // The Field class is used to represents class/struct fields during parsing.
5800 public class Field : FieldBase {
5802 // Modifiers allowed in a class declaration
5804 const int AllowedModifiers =
5807 Modifiers.PROTECTED |
5808 Modifiers.INTERNAL |
5811 Modifiers.VOLATILE |
5815 public Field (DeclSpace parent, FullNamedExpression type, int mod, string name,
5816 Attributes attrs, Location loc)
5817 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5822 bool CanBeVolatile ()
5824 if (TypeManager.IsReferenceType (MemberType))
5827 if (MemberType.IsEnum)
5830 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5831 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5832 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5833 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5834 MemberType == TypeManager.float_type)
5840 bool CheckStructLayout (Type type, bool isStatic)
5842 if (TypeManager.IsBuiltinType (type))
5846 if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
5850 if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
5851 if (!TypeManager.IsGenericType (type))
5854 foreach (Type t in TypeManager.GetTypeArguments (type)) {
5855 if (!CheckStructLayout (t, false))
5861 Report.Error (523, Location,
5862 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
5863 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5867 public override bool Define ()
5869 if (!base.Define ())
5872 if ((ModFlags & Modifiers.VOLATILE) != 0){
5873 if (!CanBeVolatile ()) {
5874 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5875 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5878 if ((ModFlags & Modifiers.READONLY) != 0){
5879 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5880 GetSignatureForError ());
5886 Type[] required_modifier = null;
5887 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5888 if (TypeManager.isvolatile_type == null)
5889 TypeManager.isvolatile_type = TypeManager.CoreLookupType (
5890 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5892 if (TypeManager.isvolatile_type != null)
5893 required_modifier = new Type [] { TypeManager.isvolatile_type };
5896 FieldBuilder = Parent.TypeBuilder.DefineField (
5897 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5899 FieldBuilder = Parent.TypeBuilder.DefineField (
5900 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5902 Parent.MemberCache.AddMember (FieldBuilder, this);
5903 TypeManager.RegisterFieldBase (FieldBuilder, this);
5905 catch (ArgumentException) {
5906 Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
5910 if (initializer != null) {
5911 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5912 new FieldInitializer (FieldBuilder, initializer));
5914 if (Parent.PartialContainer.Kind == Kind.Struct)
5915 CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
5921 public override string GetSignatureForError ()
5923 string s = base.GetSignatureForError ();
5924 if ((ModFlags & Modifiers.BACKING_FIELD) == 0)
5927 // Undecorate name mangling
5928 int l = s.LastIndexOf ('>');
5929 return s.Substring (0, l).Remove (s.LastIndexOf ('<'), 1);
5932 protected override bool VerifyClsCompliance ()
5934 if (!base.VerifyClsCompliance ())
5937 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5938 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5946 // `set' and `get' accessors are represented with an Accessor.
5948 public class Accessor {
5950 // Null if the accessor is empty, or a Block if not
5952 public const int AllowedModifiers =
5954 Modifiers.PROTECTED |
5955 Modifiers.INTERNAL |
5958 public ToplevelBlock Block;
5959 public Attributes Attributes;
5960 public Location Location;
5961 public int ModFlags;
5962 public Parameters Parameters;
5964 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Parameters p, Location loc)
5970 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5974 // Ooouh Martin, templates are missing here.
5975 // When it will be possible move here a lot of child code and template method type.
5976 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5977 protected MethodData method_data;
5978 protected ToplevelBlock block;
5979 protected ListDictionary declarative_security;
5981 // The accessor are created event if they are not wanted.
5982 // But we need them because their names are reserved.
5983 // Field says whether accessor will be emited or not
5984 public readonly bool IsDummy;
5986 protected readonly string prefix;
5988 ReturnParameter return_attributes;
5990 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
5991 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5993 this.prefix = prefix;
5997 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
5999 : base (member.Parent, SetupName (prefix, member, accessor.Location),
6000 accessor.Attributes)
6002 this.prefix = prefix;
6003 this.block = accessor.Block;
6006 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
6008 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
6011 public void UpdateName (InterfaceMemberBase member)
6013 SetMemberName (SetupName (prefix, member, Location));
6016 #region IMethodData Members
6018 public ToplevelBlock Block {
6028 public CallingConventions CallingConventions {
6030 return CallingConventions.Standard;
6034 public bool IsExcluded ()
6039 GenericMethod IMethodData.GenericMethod {
6045 public MemberName MethodName {
6051 public Type[] ParameterTypes {
6053 return ParameterInfo.Types;
6057 public abstract Parameters ParameterInfo { get ; }
6058 public abstract Type ReturnType { get; }
6059 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
6063 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
6065 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
6066 a.Type == TypeManager.conditional_attribute_type) {
6067 Report.Error (1667, a.Location,
6068 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6069 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6073 if (a.IsValidSecurityAttribute ()) {
6074 if (declarative_security == null)
6075 declarative_security = new ListDictionary ();
6076 a.ExtractSecurityPermissionSet (declarative_security);
6080 if (a.Target == AttributeTargets.Method) {
6081 method_data.MethodBuilder.SetCustomAttribute (cb);
6085 if (a.Target == AttributeTargets.ReturnValue) {
6086 if (return_attributes == null)
6087 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6089 return_attributes.ApplyAttributeBuilder (a, cb);
6093 ApplyToExtraTarget (a, cb);
6096 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
6098 throw new NotSupportedException ("You forgot to define special attribute target handling");
6101 // It is not supported for the accessors
6102 public sealed override bool Define()
6104 throw new NotSupportedException ();
6107 public void Emit (DeclSpace parent)
6109 EmitMethod (parent);
6112 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
6113 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
6114 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6115 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
6117 if (OptAttributes != null)
6118 OptAttributes.Emit ();
6120 if (declarative_security != null) {
6121 foreach (DictionaryEntry de in declarative_security) {
6122 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6129 protected virtual void EmitMethod (DeclSpace parent)
6131 method_data.Emit (parent);
6134 public override bool EnableOverloadChecks (MemberCore overload)
6136 // This can only happen with indexers and it will
6137 // be catched as indexer difference
6138 if (overload is AbstractPropertyEventMethod)
6141 if (overload is MethodCore) {
6142 caching_flags |= Flags.MethodOverloadsExist;
6148 public override bool IsClsComplianceRequired()
6153 public bool IsDuplicateImplementation (MethodCore method)
6155 if (!MemberName.Equals (method.MemberName))
6158 Type[] param_types = method.ParameterTypes;
6160 if (param_types == null || param_types.Length != ParameterTypes.Length)
6163 for (int i = 0; i < param_types.Length; i++)
6164 if (param_types [i] != ParameterTypes [i])
6167 Report.SymbolRelatedToPreviousError (method);
6168 Report.Error (82, Location, "A member `{0}' is already reserved",
6169 method.GetSignatureForError ());
6173 public override bool IsUsed
6184 // Represents header string for documentation comment.
6186 public override string DocCommentHeader {
6187 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6190 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6195 // Properties and Indexers both generate PropertyBuilders, we use this to share
6196 // their common bits.
6198 abstract public class PropertyBase : PropertyBasedMember {
6200 public class GetMethod : PropertyMethod
6202 static string[] attribute_targets = new string [] { "method", "return" };
6204 public GetMethod (PropertyBase method):
6205 base (method, "get_")
6209 public GetMethod (PropertyBase method, Accessor accessor):
6210 base (method, accessor, "get_")
6214 public override MethodBuilder Define (DeclSpace parent)
6216 base.Define (parent);
6221 method_data = new MethodData (method, ModFlags, flags, this);
6223 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6226 return method_data.MethodBuilder;
6229 public override Type ReturnType {
6231 return method.MemberType;
6235 public override Parameters ParameterInfo {
6237 return Parameters.EmptyReadOnlyParameters;
6241 public override string[] ValidAttributeTargets {
6243 return attribute_targets;
6248 public class SetMethod : PropertyMethod {
6250 static string[] attribute_targets = new string [] { "method", "param", "return" };
6251 ImplicitParameter param_attr;
6252 protected Parameters parameters;
6254 public SetMethod (PropertyBase method) :
6255 base (method, "set_")
6257 parameters = new Parameters (
6258 new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
6261 public SetMethod (PropertyBase method, Accessor accessor):
6262 base (method, accessor, "set_")
6264 this.parameters = accessor.Parameters;
6267 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6269 if (a.Target == AttributeTargets.Parameter) {
6270 if (param_attr == null)
6271 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6273 param_attr.ApplyAttributeBuilder (a, cb);
6277 base.ApplyAttributeBuilder (a, cb);
6280 public override Parameters ParameterInfo {
6286 public override MethodBuilder Define (DeclSpace parent)
6288 parameters.Resolve (ResolveContext);
6289 base.Define (parent);
6294 method_data = new MethodData (method, ModFlags, flags, this);
6296 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6299 return method_data.MethodBuilder;
6302 public override Type ReturnType {
6304 return TypeManager.void_type;
6308 public override string[] ValidAttributeTargets {
6310 return attribute_targets;
6315 static string[] attribute_targets = new string [] { "property" };
6317 public abstract class PropertyMethod : AbstractPropertyEventMethod
6319 protected readonly PropertyBase method;
6320 protected MethodAttributes flags;
6322 public PropertyMethod (PropertyBase method, string prefix)
6323 : base (method, prefix)
6325 this.method = method;
6326 this.ModFlags = method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE);
6329 public PropertyMethod (PropertyBase method, Accessor accessor,
6331 : base (method, accessor, prefix)
6333 this.method = method;
6334 this.ModFlags = accessor.ModFlags | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
6336 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6337 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6341 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6343 if (a.IsInternalMethodImplAttribute) {
6344 method.is_external_implementation = true;
6347 base.ApplyAttributeBuilder (a, cb);
6350 public override AttributeTargets AttributeTargets {
6352 return AttributeTargets.Method;
6356 public override bool IsClsComplianceRequired ()
6358 return method.IsClsComplianceRequired ();
6361 public virtual MethodBuilder Define (DeclSpace parent)
6363 CheckForDuplications ();
6366 if (method.InterfaceType != null && method.IsExplicitImpl) {
6367 MethodInfo mi = parent.PartialContainer.PendingImplementations.IsInterfaceMethod (
6368 MethodName.Name, method.InterfaceType, new MethodData (method, ModFlags, flags, this));
6370 Report.SymbolRelatedToPreviousError (mi);
6371 Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
6372 method.GetSignatureForError (), TypeManager.CSharpSignature (mi, true));
6378 TypeContainer container = parent.PartialContainer;
6381 // Check for custom access modifier
6383 if ((ModFlags & Modifiers.Accessibility) == 0) {
6384 ModFlags |= method.ModFlags;
6385 flags = method.flags;
6387 if (container.Kind == Kind.Interface)
6388 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6389 GetSignatureForError ());
6391 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6392 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6395 CheckModifiers (ModFlags);
6396 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6397 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6398 flags = Modifiers.MethodAttr (ModFlags);
6399 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6402 CheckAbstractAndExtern (block != null);
6404 if (block != null && block.IsIterator)
6405 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
6410 public bool HasCustomAccessModifier
6413 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6417 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6419 return new EmitContext (this,
6420 ds, method.ds, method.Location, ig, ReturnType,
6421 method.ModFlags, false);
6424 public override ObsoleteAttribute GetObsoleteAttribute ()
6426 return method.GetObsoleteAttribute ();
6429 public override string GetSignatureForError()
6431 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6434 void CheckModifiers (int modflags)
6436 modflags &= Modifiers.Accessibility;
6438 int mflags = method.ModFlags & Modifiers.Accessibility;
6440 if ((mflags & Modifiers.PUBLIC) != 0) {
6441 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6443 else if ((mflags & Modifiers.PROTECTED) != 0) {
6444 if ((mflags & Modifiers.INTERNAL) != 0)
6445 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6447 flags |= Modifiers.PRIVATE;
6449 else if ((mflags & Modifiers.INTERNAL) != 0)
6450 flags |= Modifiers.PRIVATE;
6452 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6453 Report.Error (273, Location,
6454 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6455 GetSignatureForError (), method.GetSignatureForError ());
6459 protected bool CheckForDuplications ()
6461 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6464 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6468 public PropertyMethod Get, Set;
6469 public PropertyBuilder PropertyBuilder;
6470 public MethodBuilder GetBuilder, SetBuilder;
6472 protected bool define_set_first = false;
6474 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6475 int allowed_mod, MemberName name,
6476 Attributes attrs, bool define_set_first)
6477 : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
6479 this.define_set_first = define_set_first;
6482 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6484 if (a.HasSecurityAttribute) {
6485 a.Error_InvalidSecurityParent ();
6489 PropertyBuilder.SetCustomAttribute (cb);
6492 public override AttributeTargets AttributeTargets {
6494 return AttributeTargets.Property;
6498 public override bool Define ()
6503 if (!IsTypePermitted ())
6509 protected override bool DoDefine ()
6511 if (!base.DoDefine ())
6515 // Accessors modifiers check
6517 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6518 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6519 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6520 GetSignatureForError ());
6524 if ((ModFlags & Modifiers.OVERRIDE) == 0 &&
6525 (Get.IsDummy && (Set.ModFlags & Modifiers.Accessibility) != 0) ||
6526 (Set.IsDummy && (Get.ModFlags & Modifiers.Accessibility) != 0)) {
6527 Report.Error (276, Location,
6528 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6529 GetSignatureForError ());
6534 if (MemberType.IsGenericParameter)
6538 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6539 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6548 GetBuilder = Get.Define (Parent);
6549 return (Get.IsDummy) ? true : GetBuilder != null;
6552 bool DefineSet (bool define)
6557 SetBuilder = Set.Define (Parent);
6558 return (Set.IsDummy) ? true : SetBuilder != null;
6561 protected bool DefineAccessors ()
6563 return DefineSet (define_set_first) &&
6565 DefineSet (!define_set_first);
6568 protected abstract PropertyInfo ResolveBaseProperty ();
6570 // TODO: rename to Resolve......
6571 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6573 PropertyInfo base_property = ResolveBaseProperty ();
6574 if (base_property == null)
6577 base_ret_type = base_property.PropertyType;
6578 MethodInfo get_accessor = base_property.GetGetMethod (true);
6579 MethodInfo set_accessor = base_property.GetSetMethod (true);
6580 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6583 // Check base property accessors conflict
6585 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6586 if (get_accessor == null) {
6587 if (Get != null && !Get.IsDummy) {
6588 Report.SymbolRelatedToPreviousError (base_property);
6589 Report.Error (545, Location,
6590 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6591 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6594 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6596 if (!Get.IsDummy && !CheckAccessModifiers (
6597 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6598 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6601 if (set_accessor == null) {
6602 if (Set != null && !Set.IsDummy) {
6603 Report.SymbolRelatedToPreviousError (base_property);
6604 Report.Error (546, Location,
6605 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6606 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6609 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6611 if (!Set.IsDummy && !CheckAccessModifiers (
6612 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6613 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6617 // When one accessor does not exist and property hides base one
6618 // we need to propagate this upwards
6619 if (set_accessor == null)
6620 set_accessor = get_accessor;
6623 // Get the less restrictive access
6625 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6628 public override void Emit ()
6631 // The PropertyBuilder can be null for explicit implementations, in that
6632 // case, we do not actually emit the ".property", so there is nowhere to
6633 // put the attribute
6635 if (PropertyBuilder != null && OptAttributes != null)
6636 OptAttributes.Emit ();
6648 /// Tests whether accessors are not in collision with some method (CS0111)
6650 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6652 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6655 public override bool IsUsed
6661 return Get.IsUsed | Set.IsUsed;
6665 protected override void SetMemberName (MemberName new_name)
6667 base.SetMemberName (new_name);
6669 Get.UpdateName (this);
6670 Set.UpdateName (this);
6673 public override string[] ValidAttributeTargets {
6675 return attribute_targets;
6680 // Represents header string for documentation comment.
6682 public override string DocCommentHeader {
6683 get { return "P:"; }
6687 public class Property : PropertyBase {
6688 const int AllowedModifiers =
6691 Modifiers.PROTECTED |
6692 Modifiers.INTERNAL |
6696 Modifiers.OVERRIDE |
6697 Modifiers.ABSTRACT |
6702 const int AllowedInterfaceModifiers =
6705 void CreateAutomaticProperty (Block block, Accessor get_block, Accessor set_block)
6708 Field field = new Field (
6710 Modifiers.BACKING_FIELD | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6711 "<" + Name + ">k__BackingField", null, Location);
6712 ((TypeContainer)Parent).PartialContainer.AddField (field);
6715 get_block.Block = new ToplevelBlock (block, null, Location);
6716 Return r = new Return (new SimpleName(field.Name, Location), Location);
6717 get_block.Block.AddStatement (r);
6718 get_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6721 set_block.Block = new ToplevelBlock (block, set_block.Parameters, Location);
6722 Assign a = new SimpleAssign (new SimpleName (field.Name, Location), new SimpleName ("value", Location));
6723 set_block.Block.AddStatement (new StatementExpression(a));
6724 set_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6727 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6728 MemberName name, Attributes attrs, Accessor get_block,
6729 Accessor set_block, bool define_set_first)
6730 : this (parent, type, mod, name, attrs, get_block, set_block,
6731 define_set_first, null)
6735 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6736 MemberName name, Attributes attrs, Accessor get_block,
6737 Accessor set_block, bool define_set_first, Block current_block)
6738 : base (parent, type, mod,
6739 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
6740 name, attrs, define_set_first)
6742 if (!IsInterface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6743 get_block != null && get_block.Block == null &&
6744 set_block != null && set_block.Block == null) {
6745 if (RootContext.Version <= LanguageVersion.ISO_2)
6746 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6748 CreateAutomaticProperty (current_block, get_block, set_block);
6751 if (get_block == null)
6752 Get = new GetMethod (this);
6754 Get = new GetMethod (this, get_block);
6756 if (set_block == null)
6757 Set = new SetMethod (this);
6759 Set = new SetMethod (this, set_block);
6762 public override bool Define ()
6764 if (!DoDefineBase ())
6767 if (!base.Define ())
6770 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6772 if (!DefineAccessors ())
6778 // FIXME - PropertyAttributes.HasDefault ?
6780 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6781 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6784 PropertyBuilder.SetGetMethod (GetBuilder);
6785 Parent.MemberCache.AddMember (GetBuilder, Get);
6789 PropertyBuilder.SetSetMethod (SetBuilder);
6790 Parent.MemberCache.AddMember (SetBuilder, Set);
6793 TypeManager.RegisterProperty (PropertyBuilder, this);
6794 Parent.MemberCache.AddMember (PropertyBuilder, this);
6798 public override void Emit ()
6800 if (((Set.ModFlags | Get.ModFlags) & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
6801 Report.Error (842, Location,
6802 "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
6803 GetSignatureForError ());
6809 protected override PropertyInfo ResolveBaseProperty ()
6811 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6812 Parent.TypeBuilder, Name, Parameters.EmptyReadOnlyParameters, null, true) as PropertyInfo;
6817 /// Gigantic workaround for lameness in SRE follows :
6818 /// This class derives from EventInfo and attempts to basically
6819 /// wrap around the EventBuilder so that FindMembers can quickly
6820 /// return this in it search for members
6822 public class MyEventBuilder : EventInfo {
6825 // We use this to "point" to our Builder which is
6826 // not really a MemberInfo
6828 EventBuilder MyBuilder;
6831 // We "catch" and wrap these methods
6833 MethodInfo raise, remove, add;
6835 EventAttributes attributes;
6836 Type declaring_type, reflected_type, event_type;
6841 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6843 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6845 // And now store the values in our own fields.
6847 declaring_type = type_builder;
6849 reflected_type = type_builder;
6851 attributes = event_attr;
6854 this.event_type = event_type;
6858 // Methods that you have to override. Note that you only need
6859 // to "implement" the variants that take the argument (those are
6860 // the "abstract" methods, the others (GetAddMethod()) are
6863 public override MethodInfo GetAddMethod (bool nonPublic)
6868 public override MethodInfo GetRemoveMethod (bool nonPublic)
6873 public override MethodInfo GetRaiseMethod (bool nonPublic)
6879 // These methods make "MyEventInfo" look like a Builder
6881 public void SetRaiseMethod (MethodBuilder raiseMethod)
6883 raise = raiseMethod;
6884 MyBuilder.SetRaiseMethod (raiseMethod);
6887 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6889 remove = removeMethod;
6890 MyBuilder.SetRemoveOnMethod (removeMethod);
6893 public void SetAddOnMethod (MethodBuilder addMethod)
6896 MyBuilder.SetAddOnMethod (addMethod);
6899 public void SetCustomAttribute (CustomAttributeBuilder cb)
6901 MyBuilder.SetCustomAttribute (cb);
6904 public override object [] GetCustomAttributes (bool inherit)
6906 // FIXME : There's nothing which can be seemingly done here because
6907 // we have no way of getting at the custom attribute objects of the
6912 public override object [] GetCustomAttributes (Type t, bool inherit)
6914 // FIXME : Same here !
6918 public override bool IsDefined (Type t, bool b)
6923 public override EventAttributes Attributes {
6929 public override string Name {
6935 public override Type DeclaringType {
6937 return declaring_type;
6941 public override Type ReflectedType {
6943 return reflected_type;
6947 public Type EventType {
6953 public void SetUsed ()
6955 if (my_event != null) {
6956 // my_event.SetAssigned ();
6957 my_event.SetMemberIsUsed ();
6963 /// For case when event is declared like property (with add and remove accessors).
6965 public class EventProperty: Event {
6966 abstract class AEventPropertyAccessor : AEventAccessor
6968 protected AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
6969 base (method, accessor, prefix)
6973 public override MethodBuilder Define (DeclSpace ds)
6975 CheckAbstractAndExtern (block != null);
6976 return base.Define (ds);
6979 public override string GetSignatureForError ()
6981 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
6985 sealed class AddDelegateMethod: AEventPropertyAccessor
6987 public AddDelegateMethod (Event method, Accessor accessor):
6988 base (method, accessor, "add_")
6992 protected override MethodInfo DelegateMethodInfo {
6994 return TypeManager.delegate_combine_delegate_delegate;
6999 sealed class RemoveDelegateMethod: AEventPropertyAccessor
7001 public RemoveDelegateMethod (Event method, Accessor accessor):
7002 base (method, accessor, "remove_")
7006 protected override MethodInfo DelegateMethodInfo {
7008 return TypeManager.delegate_remove_delegate_delegate;
7014 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
7016 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
7018 Attributes attrs, Accessor add, Accessor remove)
7019 : base (parent, type, mod_flags, name, attrs)
7021 Add = new AddDelegateMethod (this, add);
7022 Remove = new RemoveDelegateMethod (this, remove);
7025 public override bool Define()
7027 if (!base.Define ())
7034 public override string[] ValidAttributeTargets {
7036 return attribute_targets;
7042 /// Event is declared like field.
7044 public class EventField : Event {
7045 abstract class EventFieldAccessor : AEventAccessor
7047 protected EventFieldAccessor (Event method, string prefix)
7048 : base (method, prefix)
7052 protected override void EmitMethod(DeclSpace parent)
7054 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7057 MethodBuilder mb = method_data.MethodBuilder;
7058 ILGenerator ig = mb.GetILGenerator ();
7060 // TODO: because we cannot use generics yet
7061 FieldInfo field_info = ((EventField)method).FieldBuilder;
7063 if (parent is Class) {
7064 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7067 if ((method.ModFlags & Modifiers.STATIC) != 0) {
7068 ig.Emit (OpCodes.Ldsfld, field_info);
7069 ig.Emit (OpCodes.Ldarg_0);
7070 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7071 ig.Emit (OpCodes.Castclass, method.MemberType);
7072 ig.Emit (OpCodes.Stsfld, field_info);
7074 ig.Emit (OpCodes.Ldarg_0);
7075 ig.Emit (OpCodes.Ldarg_0);
7076 ig.Emit (OpCodes.Ldfld, field_info);
7077 ig.Emit (OpCodes.Ldarg_1);
7078 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7079 ig.Emit (OpCodes.Castclass, method.MemberType);
7080 ig.Emit (OpCodes.Stfld, field_info);
7082 ig.Emit (OpCodes.Ret);
7086 sealed class AddDelegateMethod: EventFieldAccessor
7088 public AddDelegateMethod (Event method):
7089 base (method, "add_")
7093 protected override MethodInfo DelegateMethodInfo {
7095 return TypeManager.delegate_combine_delegate_delegate;
7100 sealed class RemoveDelegateMethod: EventFieldAccessor
7102 public RemoveDelegateMethod (Event method):
7103 base (method, "remove_")
7107 protected override MethodInfo DelegateMethodInfo {
7109 return TypeManager.delegate_remove_delegate_delegate;
7115 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7116 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7118 public FieldBuilder FieldBuilder;
7119 public Expression Initializer;
7121 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7122 : base (parent, type, mod_flags, name, attrs)
7124 Add = new AddDelegateMethod (this);
7125 Remove = new RemoveDelegateMethod (this);
7128 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7130 if (a.Target == AttributeTargets.Field) {
7131 FieldBuilder.SetCustomAttribute (cb);
7135 if (a.Target == AttributeTargets.Method) {
7136 int errors = Report.Errors;
7137 Add.ApplyAttributeBuilder (a, cb);
7138 if (errors == Report.Errors)
7139 Remove.ApplyAttributeBuilder (a, cb);
7143 base.ApplyAttributeBuilder (a, cb);
7146 public override bool Define()
7148 if (!base.Define ())
7154 // FIXME: We are unable to detect whether generic event is used because
7155 // we are using FieldExpr instead of EventExpr for event access in that
7156 // case. When this issue will be fixed this hack can be removed.
7157 if (TypeManager.IsGenericType (MemberType))
7160 if (Add.IsInterfaceImplementation)
7163 FieldBuilder = Parent.TypeBuilder.DefineField (
7165 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7166 TypeManager.RegisterEventField (EventBuilder, this);
7168 if (Initializer != null) {
7169 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
7170 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7171 GetSignatureForError ());
7175 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
7176 new FieldInitializer (FieldBuilder, Initializer));
7182 public override string[] ValidAttributeTargets
7185 return IsInterface ? attribute_targets_interface : attribute_targets;
7190 public abstract class Event : PropertyBasedMember {
7191 public abstract class AEventAccessor : AbstractPropertyEventMethod
7193 protected readonly Event method;
7194 ImplicitParameter param_attr;
7196 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7198 protected AEventAccessor (Event method, string prefix)
7199 : base (method, prefix)
7201 this.method = method;
7202 this.ModFlags = method.ModFlags;
7205 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7206 : base (method, accessor, prefix)
7208 this.method = method;
7209 this.ModFlags = method.ModFlags;
7212 public bool IsInterfaceImplementation {
7213 get { return method_data.implementing != null; }
7216 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7218 if (a.IsInternalMethodImplAttribute) {
7219 method.is_external_implementation = true;
7222 base.ApplyAttributeBuilder (a, cb);
7225 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
7227 if (a.Target == AttributeTargets.Parameter) {
7228 if (param_attr == null)
7229 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7231 param_attr.ApplyAttributeBuilder (a, cb);
7235 base.ApplyAttributeBuilder (a, cb);
7238 public override AttributeTargets AttributeTargets {
7240 return AttributeTargets.Method;
7244 public override bool IsClsComplianceRequired ()
7246 return method.IsClsComplianceRequired ();
7249 public virtual MethodBuilder Define (DeclSpace parent)
7251 method_data = new MethodData (method, method.ModFlags,
7252 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7254 if (!method_data.Define (parent, method.GetFullName (MemberName)))
7257 MethodBuilder mb = method_data.MethodBuilder;
7258 ParameterInfo.ApplyAttributes (mb);
7262 protected abstract MethodInfo DelegateMethodInfo { get; }
7264 public override Type ReturnType {
7266 return TypeManager.void_type;
7270 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7272 return new EmitContext (
7273 this, method.Parent, Location, ig, ReturnType,
7274 method.ModFlags, false);
7277 public override ObsoleteAttribute GetObsoleteAttribute ()
7279 return method.GetObsoleteAttribute ();
7282 public override string[] ValidAttributeTargets {
7284 return attribute_targets;
7288 public override Parameters ParameterInfo {
7290 return method.parameters;
7296 const int AllowedModifiers =
7299 Modifiers.PROTECTED |
7300 Modifiers.INTERNAL |
7305 Modifiers.OVERRIDE |
7307 Modifiers.ABSTRACT |
7310 const int AllowedInterfaceModifiers =
7313 public AEventAccessor Add, Remove;
7314 public MyEventBuilder EventBuilder;
7315 public MethodBuilder AddBuilder, RemoveBuilder;
7317 Parameters parameters;
7319 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7320 : base (parent, null, type, mod_flags,
7321 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7326 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7328 if ((a.HasSecurityAttribute)) {
7329 a.Error_InvalidSecurityParent ();
7333 EventBuilder.SetCustomAttribute (cb);
7336 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7338 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7341 public override AttributeTargets AttributeTargets {
7343 return AttributeTargets.Event;
7347 public override bool Define ()
7349 if (!DoDefineBase ())
7355 if (!TypeManager.IsDelegateType (MemberType)) {
7356 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7360 parameters = Parameters.CreateFullyResolved (
7361 new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
7366 if (TypeManager.delegate_combine_delegate_delegate == null) {
7367 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7368 TypeManager.delegate_type, "Combine", Location,
7369 TypeManager.delegate_type, TypeManager.delegate_type);
7371 if (TypeManager.delegate_remove_delegate_delegate == null) {
7372 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7373 TypeManager.delegate_type, "Remove", Location,
7374 TypeManager.delegate_type, TypeManager.delegate_type);
7378 // Now define the accessors
7381 AddBuilder = Add.Define (Parent);
7382 if (AddBuilder == null)
7385 RemoveBuilder = Remove.Define (Parent);
7386 if (RemoveBuilder == null)
7389 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7390 EventBuilder.SetAddOnMethod (AddBuilder);
7391 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7393 Parent.MemberCache.AddMember (EventBuilder, this);
7394 Parent.MemberCache.AddMember (AddBuilder, Add);
7395 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7400 public override void Emit ()
7402 if (OptAttributes != null) {
7403 OptAttributes.Emit ();
7407 Remove.Emit (Parent);
7412 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7414 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7415 Parent.TypeBuilder, Name);
7420 AParametersCollection pd = TypeManager.GetParameterData (mi);
7421 base_ret_type = pd.Types [0];
7426 // Represents header string for documentation comment.
7428 public override string DocCommentHeader {
7429 get { return "E:"; }
7434 public class Indexer : PropertyBase
7436 public class GetIndexerMethod : GetMethod
7438 Parameters parameters;
7440 public GetIndexerMethod (Indexer method):
7443 this.parameters = method.parameters;
7446 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7447 base (method, accessor)
7449 parameters = accessor.Parameters;
7452 public override MethodBuilder Define (DeclSpace parent)
7454 parameters.Resolve (ResolveContext);
7455 return base.Define (parent);
7458 public override bool EnableOverloadChecks (MemberCore overload)
7460 if (base.EnableOverloadChecks (overload)) {
7461 overload.caching_flags |= Flags.MethodOverloadsExist;
7468 public override Parameters ParameterInfo {
7475 public class SetIndexerMethod: SetMethod
7477 public SetIndexerMethod (Indexer method):
7480 parameters = Parameters.MergeGenerated (method.parameters, false, parameters [0], null);
7483 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7484 base (method, accessor)
7486 parameters = method.Get.IsDummy ? accessor.Parameters : accessor.Parameters.Clone ();
7489 public override bool EnableOverloadChecks (MemberCore overload)
7491 if (base.EnableOverloadChecks (overload)) {
7492 overload.caching_flags |= Flags.MethodOverloadsExist;
7500 const int AllowedModifiers =
7503 Modifiers.PROTECTED |
7504 Modifiers.INTERNAL |
7508 Modifiers.OVERRIDE |
7513 const int AllowedInterfaceModifiers =
7516 public readonly Parameters parameters;
7518 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7519 Parameters parameters, Attributes attrs,
7520 Accessor get_block, Accessor set_block, bool define_set_first)
7521 : base (parent, type, mod,
7522 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7523 name, attrs, define_set_first)
7525 this.parameters = parameters;
7527 if (get_block == null)
7528 Get = new GetIndexerMethod (this);
7530 Get = new GetIndexerMethod (this, get_block);
7532 if (set_block == null)
7533 Set = new SetIndexerMethod (this);
7535 Set = new SetIndexerMethod (this, set_block);
7538 public override bool Define ()
7540 if (!DoDefineBase ())
7543 if (!base.Define ())
7546 if (!DefineParameters (parameters))
7549 if (OptAttributes != null && TypeManager.indexer_name_type != null) {
7550 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
7551 if (indexer_attr != null) {
7552 // Remove the attribute from the list because it is not emitted
7553 OptAttributes.Attrs.Remove (indexer_attr);
7555 string name = indexer_attr.GetIndexerAttributeValue ();
7561 if (IsExplicitImpl) {
7562 Report.Error (415, indexer_attr.Location,
7563 "The `IndexerName' attribute is valid only on an " +
7564 "indexer that is not an explicit interface member declaration");
7568 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7569 Report.Error (609, indexer_attr.Location,
7570 "Cannot set the `IndexerName' attribute on an indexer marked override");
7576 if (InterfaceType != null) {
7577 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7578 if (base_IndexerName != Name)
7579 ShortName = base_IndexerName;
7582 if (!Parent.PartialContainer.AddMember (this) ||
7583 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7586 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
7587 if (!Parent.MemberCache.CheckExistingMembersOverloads (this, Name, parameters))
7591 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7593 if (!DefineAccessors ())
7600 // Now name the parameters
7602 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7603 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.GetEmitTypes ());
7606 PropertyBuilder.SetGetMethod (GetBuilder);
7607 Parent.MemberCache.AddMember (GetBuilder, Get);
7611 PropertyBuilder.SetSetMethod (SetBuilder);
7612 Parent.MemberCache.AddMember (SetBuilder, Set);
7615 TypeManager.RegisterIndexer (PropertyBuilder, parameters);
7616 Parent.MemberCache.AddMember (PropertyBuilder, this);
7620 public override bool EnableOverloadChecks (MemberCore overload)
7622 if (overload is Indexer) {
7623 caching_flags |= Flags.MethodOverloadsExist;
7630 public override string GetDocCommentName (DeclSpace ds)
7632 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7635 public override string GetSignatureForError ()
7637 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7638 if (MemberName.Left != null) {
7640 sb.Append (MemberName.Left.GetSignatureForError ());
7643 sb.Append (".this");
7644 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7645 return sb.ToString ();
7648 protected override PropertyInfo ResolveBaseProperty ()
7650 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7651 Parent.TypeBuilder, Name, parameters, null, true) as PropertyInfo;
7654 protected override bool VerifyClsCompliance ()
7656 if (!base.VerifyClsCompliance ())
7659 parameters.VerifyClsCompliance ();
7664 public class Operator : MethodOrOperator {
7666 const int AllowedModifiers =
7672 public enum OpType : byte {
7682 // Unary and Binary operators
7705 // Implicit and Explicit
7709 // Just because of enum
7713 public readonly OpType OperatorType;
7715 static readonly string [] [] names;
7719 names = new string[(int)OpType.TOP][];
7720 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7721 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7722 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7723 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7724 names [(int) OpType.True] = new string [] { "true", "op_True" };
7725 names [(int) OpType.False] = new string [] { "false", "op_False" };
7726 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7727 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7728 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7729 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7730 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7731 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7732 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7733 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7734 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7735 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7736 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7737 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7738 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7739 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7740 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7741 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7742 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7743 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7744 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7745 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7748 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7749 int mod_flags, Parameters parameters,
7750 ToplevelBlock block, Attributes attrs, Location loc)
7751 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
7752 new MemberName ("op_" + type.ToString(), loc), attrs, parameters)
7754 OperatorType = type;
7758 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7760 if (a.Type == TypeManager.conditional_attribute_type) {
7761 Error_ConditionalAttributeIsNotValid ();
7765 base.ApplyAttributeBuilder (a, cb);
7768 public override bool Define ()
7770 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7771 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7772 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7776 if (!base.Define ())
7779 // imlicit and explicit operator of same types are not allowed
7780 if (OperatorType == OpType.Explicit)
7781 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
7782 else if (OperatorType == OpType.Implicit)
7783 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
7785 Type declaring_type = MethodData.DeclaringType;
7786 Type return_type = MemberType;
7787 Type first_arg_type = ParameterTypes [0];
7789 Type first_arg_type_unwrap = first_arg_type;
7790 if (TypeManager.IsNullableType (first_arg_type))
7791 first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
7793 Type return_type_unwrap = return_type;
7794 if (TypeManager.IsNullableType (return_type))
7795 return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
7798 // Rules for conversion operators
7800 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7801 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7802 Report.Error (555, Location,
7803 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7808 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7809 conv_type = first_arg_type;
7810 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7811 conv_type = return_type;
7813 Report.Error (556, Location,
7814 "User-defined conversion must convert to or from the enclosing type");
7819 // Because IsInterface and IsClass are not supported
7821 if (!TypeManager.IsGenericParameter (conv_type)) {
7822 if (conv_type.IsInterface) {
7823 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7824 GetSignatureForError ());
7828 if (conv_type.IsClass) {
7829 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7830 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7831 GetSignatureForError ());
7835 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7836 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7837 GetSignatureForError ());
7842 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7843 if (first_arg_type != declaring_type || Parameters.Types [1] != TypeManager.int32_type) {
7844 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");
7847 } else if (Parameters.Count == 1) {
7848 // Checks for Unary operators
7850 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7851 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7852 Report.Error (448, Location,
7853 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7856 if (first_arg_type != declaring_type) {
7858 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7863 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7864 Report.Error (562, Location,
7865 "The parameter type of a unary operator must be the containing type");
7869 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7870 if (return_type != TypeManager.bool_type){
7873 "The return type of operator True or False " +
7880 // Checks for Binary operators
7882 if (first_arg_type != declaring_type &&
7883 Parameters.Types [1] != declaring_type){
7886 "One of the parameters of a binary operator must " +
7887 "be the containing type");
7895 protected override bool DoDefine ()
7897 if (!base.DoDefine ())
7900 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7904 public override void Emit ()
7908 Parameters.ApplyAttributes (MethodBuilder);
7911 // abstract or extern methods have no bodies
7913 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7917 if ((flags & MethodAttributes.PinvokeImpl) == 0)
7918 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
7920 ec = CreateEmitContext (Parent, null);
7922 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
7923 ec.EmitTopBlock (this, Block);
7926 source.CloseMethod ();
7931 // Operator cannot be override
7932 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7937 public static string GetName (OpType ot)
7939 return names [(int) ot] [0];
7942 public static string GetName (string metadata_name)
7944 for (int i = 0; i < names.Length; ++i) {
7945 if (names [i] [1] == metadata_name)
7946 return names [i] [0];
7951 public static string GetMetadataName (OpType ot)
7953 return names [(int) ot] [1];
7956 public static string GetMetadataName (string name)
7958 for (int i = 0; i < names.Length; ++i) {
7959 if (names [i] [0] == name)
7960 return names [i] [1];
7965 public OpType GetMatchingOperator ()
7967 switch (OperatorType) {
7968 case OpType.Equality:
7969 return OpType.Inequality;
7970 case OpType.Inequality:
7971 return OpType.Equality;
7973 return OpType.False;
7976 case OpType.GreaterThan:
7977 return OpType.LessThan;
7978 case OpType.LessThan:
7979 return OpType.GreaterThan;
7980 case OpType.GreaterThanOrEqual:
7981 return OpType.LessThanOrEqual;
7982 case OpType.LessThanOrEqual:
7983 return OpType.GreaterThanOrEqual;
7989 public override string GetSignatureForError ()
7991 StringBuilder sb = new StringBuilder ();
7992 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7993 sb.AppendFormat ("{0}.{1} operator {2}",
7994 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
7997 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
8000 sb.Append (Parameters.GetSignatureForError ());
8001 return sb.ToString ();
8006 // This is used to compare method signatures
8008 struct MethodSignature {
8010 public Type RetType;
8011 public Type [] Parameters;
8014 /// This delegate is used to extract methods which have the
8015 /// same signature as the argument
8017 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
8019 public MethodSignature (string name, Type ret_type, Type [] parameters)
8024 if (parameters == null)
8025 Parameters = Type.EmptyTypes;
8027 Parameters = parameters;
8030 public override string ToString ()
8033 if (Parameters.Length != 0){
8034 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
8035 for (int i = 0; i < Parameters.Length; i++){
8036 sb.Append (Parameters [i]);
8037 if (i+1 < Parameters.Length)
8040 pars = sb.ToString ();
8043 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
8046 public override int GetHashCode ()
8048 return Name.GetHashCode ();
8051 public override bool Equals (Object o)
8053 MethodSignature other = (MethodSignature) o;
8055 if (other.Name != Name)
8058 if (other.RetType != RetType)
8061 if (Parameters == null){
8062 if (other.Parameters == null)
8067 if (other.Parameters == null)
8070 int c = Parameters.Length;
8071 if (other.Parameters.Length != c)
8074 for (int i = 0; i < c; i++)
8075 if (other.Parameters [i] != Parameters [i])
8081 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8083 MethodSignature sig = (MethodSignature) filter_criteria;
8085 if (m.Name != sig.Name)
8089 MethodInfo mi = m as MethodInfo;
8090 PropertyInfo pi = m as PropertyInfo;
8093 ReturnType = mi.ReturnType;
8094 else if (pi != null)
8095 ReturnType = pi.PropertyType;
8100 // we use sig.RetType == null to mean `do not check the
8101 // method return value.
8103 if (sig.RetType != null) {
8104 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8110 args = TypeManager.GetParameterData (mi).Types;
8112 args = TypeManager.GetParameterData (pi).Types;
8113 Type [] sigp = sig.Parameters;
8115 if (args.Length != sigp.Length)
8118 for (int i = args.Length - 1; i >= 0; i--)
8119 if (!TypeManager.IsEqual (args [i], sigp [i]))