2 // class.cs: Class and Struct handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.CompilerServices;
20 using System.Runtime.InteropServices;
21 using System.Security;
22 using System.Security.Permissions;
26 using XmlElement = System.Object;
31 using Mono.CompilerServices.SymbolWriter;
33 namespace Mono.CSharp {
45 /// This is the base class for structs and classes.
47 public abstract class TypeContainer : DeclSpace, IMemberContainer {
49 public class MemberCoreArrayList: ArrayList
52 /// Defines the MemberCore objects that are in this array
54 public virtual void DefineContainerMembers ()
56 foreach (MemberCore mc in this) {
59 } catch (Exception e) {
60 throw new InternalErrorException (mc, e);
65 public virtual void Emit ()
67 foreach (MemberCore mc in this)
72 public class OperatorArrayList: MemberCoreArrayList
74 TypeContainer container;
76 public OperatorArrayList (TypeContainer container)
78 this.container = container;
82 // Checks that some operators come in pairs:
88 // They are matched based on the return type and the argument types
90 void CheckPairedOperators ()
92 bool has_equality_or_inequality = false;
93 Operator[] operators = (Operator[]) ToArray (typeof (Operator));
94 bool [] has_pair = new bool [operators.Length];
96 for (int i = 0; i < Count; ++i) {
97 if (operators [i] == null)
100 Operator o_a = operators [i];
101 Operator.OpType o_type = o_a.OperatorType;
102 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
103 has_equality_or_inequality = true;
105 Operator.OpType matching_type = o_a.GetMatchingOperator ();
106 if (matching_type == Operator.OpType.TOP) {
107 operators [i] = null;
111 for (int ii = 0; ii < Count; ++ii) {
112 Operator o_b = operators [ii];
113 if (o_b == null || o_b.OperatorType != matching_type)
116 if (!TypeManager.IsEqual (o_a.ReturnType, o_b.ReturnType))
119 if (!TypeManager.IsEqual (o_a.ParameterTypes, o_b.ParameterTypes))
122 operators [i] = null;
125 // Used to ignore duplicate user conversions
127 has_pair [ii] = true;
131 for (int i = 0; i < Count; ++i) {
132 if (operators [i] == null || has_pair [i])
135 Operator o = operators [i];
136 container.Report.Error (216, o.Location,
137 "The operator `{0}' requires a matching operator `{1}' to also be defined",
138 o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
141 if (has_equality_or_inequality && container.Report.WarningLevel > 2) {
142 if (container.Methods == null || !container.HasEquals)
143 container.Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
145 if (container.Methods == null || !container.HasGetHashCode)
146 container.Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
150 public override void DefineContainerMembers ()
152 base.DefineContainerMembers ();
153 CheckPairedOperators ();
158 // Different context is needed when resolving type container base
159 // types. Type names come from the parent scope but type parameter
160 // names from the container scope.
162 struct BaseContext : IMemberContext
166 public BaseContext (TypeContainer tc)
171 #region IMemberContext Members
173 public CompilerContext Compiler {
174 get { return tc.Compiler; }
177 public Type CurrentType {
178 get { return tc.Parent.CurrentType; }
181 public TypeParameter[] CurrentTypeParameters {
182 get { return tc.PartialContainer.CurrentTypeParameters; }
185 public TypeContainer CurrentTypeDefinition {
186 get { return tc.Parent.CurrentTypeDefinition; }
189 public bool IsObsolete {
190 get { return tc.IsObsolete; }
193 public bool IsUnsafe {
194 get { return tc.IsUnsafe; }
197 public bool IsStatic {
198 get { return tc.IsStatic; }
201 public string GetSignatureForError ()
203 throw new NotImplementedException ();
206 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
211 public FullNamedExpression LookupNamespaceAlias (string name)
213 return tc.Parent.LookupNamespaceAlias (name);
216 public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
218 TypeParameter[] tp = CurrentTypeParameters;
220 TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
222 return new TypeParameterExpr (t, loc);
225 return tc.Parent.LookupNamespaceOrType (name, loc, ignore_cs0104);
235 GetHashCode = 1 << 1,
236 HasStaticFieldInitializer = 1 << 2
240 // Whether this is a struct, class or interface
241 public readonly Kind Kind;
243 // Holds a list of classes and structures
244 protected ArrayList types;
246 MemberCoreArrayList ordered_explicit_member_list;
247 MemberCoreArrayList ordered_member_list;
249 // Holds the list of properties
250 MemberCoreArrayList properties;
252 // Holds the list of delegates
253 MemberCoreArrayList delegates;
255 // Holds the list of constructors
256 protected MemberCoreArrayList instance_constructors;
258 // Holds the list of fields
259 protected MemberCoreArrayList fields;
261 // Holds a list of fields that have initializers
262 protected ArrayList initialized_fields;
264 // Holds a list of static fields that have initializers
265 protected ArrayList initialized_static_fields;
267 // Holds the list of constants
268 protected MemberCoreArrayList constants;
270 // Holds the methods.
271 MemberCoreArrayList methods;
274 protected MemberCoreArrayList events;
276 // Holds the indexers
279 // Holds the operators
280 MemberCoreArrayList operators;
282 // Holds the compiler generated classes
283 ArrayList compiler_generated;
286 // Pointers to the default constructor and the default static constructor
288 protected Constructor default_constructor;
289 protected Constructor default_static_constructor;
292 // Points to the first non-static field added to the container.
294 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
295 // and the first one's as good as any.
297 FieldBase first_nonstatic_field = null;
300 // This one is computed after we can distinguish interfaces
301 // from classes from the arraylist `type_bases'
304 TypeExpr[] iface_exprs;
306 GenericTypeParameterBuilder[] nested_gen_params;
308 protected ArrayList type_bases;
310 protected bool members_defined;
311 bool members_defined_ok;
313 // The interfaces we implement.
314 protected Type[] ifaces;
316 // The base member cache and our member cache
317 MemberCache base_cache;
318 protected MemberCache member_cache;
320 public const string DefaultIndexerName = "Item";
322 private bool seen_normal_indexers = false;
323 private string indexer_name = DefaultIndexerName;
324 protected bool requires_delayed_unmanagedtype_check;
326 private CachedMethods cached_method;
328 ArrayList partial_parts;
331 /// The pending methods that need to be implemented
332 // (interfaces or abstract methods)
334 PendingImplementation pending;
336 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
337 Attributes attrs, Kind kind)
338 : base (ns, parent, name, attrs)
340 if (parent != null && parent.NamespaceEntry != ns)
341 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
344 this.PartialContainer = this;
347 public bool AddMember (MemberCore symbol)
349 return AddToContainer (symbol, symbol.MemberName.Basename);
352 protected virtual bool AddMemberType (DeclSpace ds)
354 return AddToContainer (ds, ds.Basename);
357 protected virtual void RemoveMemberType (DeclSpace ds)
359 RemoveFromContainer (ds.Basename);
362 public void AddConstant (Const constant)
364 if (!AddMember (constant))
367 if (constants == null)
368 constants = new MemberCoreArrayList ();
370 constants.Add (constant);
373 public TypeContainer AddTypeContainer (TypeContainer tc)
375 if (!AddMemberType (tc))
379 types = new MemberCoreArrayList ();
385 public virtual TypeContainer AddPartial (TypeContainer next_part)
387 return AddPartial (next_part, next_part.Basename);
390 protected TypeContainer AddPartial (TypeContainer next_part, string name)
392 next_part.ModFlags |= Modifiers.PARTIAL;
393 TypeContainer tc = defined_names [name] as TypeContainer;
396 return AddTypeContainer (next_part);
398 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
399 Report.SymbolRelatedToPreviousError (next_part);
400 Error_MissingPartialModifier (tc);
403 if (tc.Kind != next_part.Kind) {
404 Report.SymbolRelatedToPreviousError (tc);
405 Report.Error (261, next_part.Location,
406 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
407 next_part.GetSignatureForError ());
410 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
411 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
412 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
413 Report.SymbolRelatedToPreviousError (tc);
414 Report.Error (262, next_part.Location,
415 "Partial declarations of `{0}' have conflicting accessibility modifiers",
416 next_part.GetSignatureForError ());
419 if (tc.partial_parts == null)
420 tc.partial_parts = new ArrayList (1);
422 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
423 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
424 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
425 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
426 tc.ModFlags |= next_part.ModFlags;
428 tc.ModFlags |= next_part.ModFlags;
431 if (next_part.attributes != null) {
432 if (tc.attributes == null)
433 tc.attributes = next_part.attributes;
435 tc.attributes.AddAttributes (next_part.attributes.Attrs);
438 next_part.PartialContainer = tc;
439 tc.partial_parts.Add (next_part);
443 public virtual void RemoveTypeContainer (TypeContainer next_part)
446 types.Remove (next_part);
447 RemoveMemberType (next_part);
450 public void AddDelegate (Delegate d)
452 if (!AddMemberType (d))
455 if (delegates == null)
456 delegates = new MemberCoreArrayList ();
461 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
463 if (ordered_explicit_member_list == null) {
464 ordered_explicit_member_list = new MemberCoreArrayList ();
465 ordered_member_list = new MemberCoreArrayList ();
469 if (Kind == Kind.Interface) {
470 Report.Error (541, mc.Location,
471 "`{0}': explicit interface declaration can only be declared in a class or struct",
472 mc.GetSignatureForError ());
475 ordered_explicit_member_list.Add (mc);
476 alist.Insert (0, mc);
478 ordered_member_list.Add (mc);
484 public void AddMethod (MethodOrOperator method)
486 if (!AddToContainer (method, method.MemberName.Basename))
490 methods = new MemberCoreArrayList ();
492 if (method.MemberName.Left != null)
493 AddMemberToList (method, methods, true);
495 AddMemberToList (method, methods, false);
498 public void AddConstructor (Constructor c)
500 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
501 if (!AddToContainer (c, is_static ?
502 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
505 if (is_static && c.Parameters.IsEmpty){
506 if (default_static_constructor != null) {
507 Report.SymbolRelatedToPreviousError (default_static_constructor);
508 Report.Error (111, c.Location,
509 "A member `{0}' is already defined. Rename this member or use different parameter types",
510 c.GetSignatureForError ());
514 default_static_constructor = c;
516 if (c.Parameters.IsEmpty)
517 default_constructor = c;
519 if (instance_constructors == null)
520 instance_constructors = new MemberCoreArrayList ();
522 instance_constructors.Add (c);
526 public bool AddField (FieldBase field)
528 if (!AddMember (field))
532 fields = new MemberCoreArrayList ();
536 if ((field.ModFlags & Modifiers.STATIC) != 0)
539 if (first_nonstatic_field == null) {
540 first_nonstatic_field = field;
544 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
545 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
546 Report.Warning (282, 3, field.Location,
547 "struct instance field `{0}' found in different declaration from instance field `{1}'",
548 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
553 public void AddProperty (Property prop)
555 if (!AddMember (prop) ||
556 !AddMember (prop.Get) || !AddMember (prop.Set))
559 if (properties == null)
560 properties = new MemberCoreArrayList ();
562 if (prop.MemberName.Left != null)
563 AddMemberToList (prop, properties, true);
565 AddMemberToList (prop, properties, false);
568 public void AddEvent (Event e)
573 if (e is EventProperty) {
574 if (!AddMember (e.Add))
577 if (!AddMember (e.Remove))
582 events = new MemberCoreArrayList ();
588 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
590 public void AddIndexer (Indexer i)
592 if (indexers == null)
593 indexers = new ArrayList ();
595 if (i.IsExplicitImpl)
596 AddMemberToList (i, indexers, true);
598 AddMemberToList (i, indexers, false);
601 public void AddOperator (Operator op)
606 if (operators == null)
607 operators = new OperatorArrayList (this);
612 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
614 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
616 if (compiler_generated == null)
617 compiler_generated = new ArrayList ();
619 compiler_generated.Add (c);
622 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
624 if (a.Type == pa.DefaultMember) {
625 if (Indexers != null) {
626 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
631 base.ApplyAttributeBuilder (a, cb, pa);
634 public override AttributeTargets AttributeTargets {
636 throw new NotSupportedException ();
640 public ArrayList Types {
646 public MemberCoreArrayList Methods {
652 public ArrayList Constants {
658 protected Type BaseType {
660 return TypeBuilder.BaseType;
664 public ArrayList Fields {
670 public ArrayList InstanceConstructors {
672 return instance_constructors;
676 public ArrayList Properties {
682 public ArrayList Events {
688 public ArrayList Indexers {
694 public ArrayList Operators {
700 public ArrayList Delegates {
706 protected override TypeAttributes TypeAttr {
708 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
712 public string IndexerName {
714 return indexers == null ? DefaultIndexerName : indexer_name;
718 public bool IsComImport {
720 if (OptAttributes == null)
723 return OptAttributes.Contains (PredefinedAttributes.Get.ComImport);
727 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
729 if ((field.ModFlags & Modifiers.STATIC) != 0){
730 if (initialized_static_fields == null) {
731 PartialContainer.HasStaticFieldInitializer = true;
732 initialized_static_fields = new ArrayList (4);
735 initialized_static_fields.Add (expression);
737 if (initialized_fields == null)
738 initialized_fields = new ArrayList (4);
740 initialized_fields.Add (expression);
744 public void ResolveFieldInitializers (BlockContext ec)
746 if (partial_parts != null) {
747 foreach (TypeContainer part in partial_parts) {
748 part.DoResolveFieldInitializers (ec);
751 DoResolveFieldInitializers (ec);
754 void DoResolveFieldInitializers (BlockContext ec)
757 if (initialized_static_fields == null)
760 bool has_complex_initializer = !RootContext.Optimize;
762 ExpressionStatement [] init = new ExpressionStatement [initialized_static_fields.Count];
763 for (i = 0; i < initialized_static_fields.Count; ++i) {
764 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
765 ExpressionStatement s = fi.ResolveStatement (ec);
767 s = EmptyExpressionStatement.Instance;
768 } else if (fi.IsComplexInitializer) {
769 has_complex_initializer |= true;
775 for (i = 0; i < initialized_static_fields.Count; ++i) {
776 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
778 // Need special check to not optimize code like this
779 // static int a = b = 5;
782 if (!has_complex_initializer && fi.IsDefaultInitializer)
785 ec.CurrentBlock.AddScopeStatement (new StatementExpression (init [i]));
791 if (initialized_fields == null)
794 for (int i = 0; i < initialized_fields.Count; ++i) {
795 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
796 ExpressionStatement s = fi.ResolveStatement (ec);
801 // Field is re-initialized to its default value => removed
803 if (fi.IsDefaultInitializer && RootContext.Optimize)
806 ec.CurrentBlock.AddScopeStatement (new StatementExpression (s));
810 public override string DocComment {
822 public PendingImplementation PendingImplementations {
823 get { return pending; }
826 public override bool GetClsCompliantAttributeValue ()
828 if (PartialContainer != this)
829 return PartialContainer.GetClsCompliantAttributeValue ();
831 return base.GetClsCompliantAttributeValue ();
834 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
836 // FIXME: get rid of partial_parts and store lists of bases of each part here
837 // assumed, not verified: 'part' is in 'partial_parts'
838 ((TypeContainer) part).type_bases = bases;
842 /// This function computes the Base class and also the
843 /// list of interfaces that the class or struct @c implements.
845 /// The return value is an array (might be null) of
846 /// interfaces implemented (as Types).
848 /// The @base_class argument is set to the base object or null
849 /// if this is `System.Object'.
851 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
854 if (type_bases == null)
857 int count = type_bases.Count;
858 TypeExpr [] ifaces = null;
859 IMemberContext base_context = new BaseContext (this);
860 for (int i = 0, j = 0; i < count; i++){
861 FullNamedExpression fne = (FullNamedExpression) type_bases [i];
864 // Standard ResolveAsTypeTerminal cannot be used in this case because
865 // it does ObsoleteAttribute and constraint checks which require
866 // base type to be set
868 TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (base_context, false);
869 if (fne_resolved == null)
872 if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
873 if (fne_resolved is DynamicTypeExpr)
874 Report.Error (1965, Location, "Class `{0}' cannot derive from the dynamic type",
875 GetSignatureForError ());
877 base_class = fne_resolved;
882 ifaces = new TypeExpr [count - i];
884 if (fne_resolved.Type.IsInterface) {
885 for (int ii = 0; ii < j; ++ii) {
886 if (TypeManager.IsEqual (fne_resolved.Type, ifaces [ii].Type)) {
887 Report.Error (528, Location, "`{0}' is already listed in interface list",
888 fne_resolved.GetSignatureForError ());
893 if (Kind == Kind.Interface && !IsAccessibleAs (fne_resolved.Type)) {
894 Report.Error (61, fne.Location,
895 "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
896 fne_resolved.GetSignatureForError (), GetSignatureForError ());
899 Report.SymbolRelatedToPreviousError (fne_resolved.Type);
900 if (Kind != Kind.Class) {
901 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
902 } else if (base_class != null)
903 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
904 GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
906 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
907 GetSignatureForError (), fne_resolved.GetSignatureForError ());
911 ifaces [j++] = fne_resolved;
917 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
919 ArrayList ifaces = new ArrayList (0);
920 if (iface_exprs != null)
921 ifaces.AddRange (iface_exprs);
923 foreach (TypeContainer part in partial_parts) {
924 TypeExpr new_base_class;
925 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
926 if (new_base_class != TypeManager.system_object_expr) {
927 if (base_class == TypeManager.system_object_expr)
928 base_class = new_base_class;
930 if (new_base_class != null && !TypeManager.IsEqual (new_base_class.Type, base_class.Type)) {
931 Report.SymbolRelatedToPreviousError (base_class.Location, "");
932 Report.Error (263, part.Location,
933 "Partial declarations of `{0}' must not specify different base classes",
934 part.GetSignatureForError ());
941 if (new_ifaces == null)
944 foreach (TypeExpr iface in new_ifaces) {
945 if (ifaces.Contains (iface))
952 if (ifaces.Count == 0)
955 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
958 bool CheckGenericInterfaces (Type[] ifaces)
960 ArrayList already_checked = new ArrayList ();
962 for (int i = 0; i < ifaces.Length; i++) {
963 Type iface = ifaces [i];
964 foreach (Type t in already_checked) {
968 Type[] inferred = new Type [CountTypeParameters];
969 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
972 Report.Error (695, Location,
973 "`{0}' cannot implement both `{1}' and `{2}' " +
974 "because they may unify for some type parameter substitutions",
975 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
976 TypeManager.CSharpName (t));
980 already_checked.Add (iface);
988 bool CreateTypeBuilder ()
991 Type default_parent = null;
992 if (Kind == Kind.Struct)
993 default_parent = TypeManager.value_type;
994 else if (Kind == Kind.Enum)
995 default_parent = TypeManager.enum_type;
996 else if (Kind == Kind.Delegate)
997 default_parent = TypeManager.multicast_delegate_type;
1000 // Sets .size to 1 for structs with no instance fields
1002 int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
1005 if (GlobalRootNamespace.Instance.IsNamespace (Name)) {
1006 Report.Error (519, Location, "`{0}' clashes with a predefined namespace", Name);
1010 ModuleBuilder builder = Module.Compiled.Builder;
1011 TypeBuilder = builder.DefineType (
1012 Name, TypeAttr, default_parent, type_size);
1014 TypeBuilder builder = Parent.TypeBuilder;
1016 TypeBuilder = builder.DefineNestedType (
1017 Basename, TypeAttr, default_parent, type_size);
1019 } catch (ArgumentException) {
1020 Report.RuntimeMissingSupport (Location, "static classes");
1024 TypeManager.AddUserType (this);
1027 string[] param_names = new string [TypeParameters.Length];
1028 for (int i = 0; i < TypeParameters.Length; i++)
1029 param_names [i] = TypeParameters [i].Name;
1031 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
1033 int offset = CountTypeParameters;
1034 if (CurrentTypeParameters != null)
1035 offset -= CurrentTypeParameters.Length;
1038 nested_gen_params = new GenericTypeParameterBuilder [offset];
1039 Array.Copy (gen_params, nested_gen_params, offset);
1042 for (int i = offset; i < gen_params.Length; i++)
1043 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1049 bool DefineBaseTypes ()
1051 iface_exprs = ResolveBaseTypes (out base_type);
1052 if (partial_parts != null) {
1053 iface_exprs = GetNormalPartialBases (ref base_type);
1057 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1058 // which in turn should have called DefineType()s on base types if necessary.
1060 // None of the code below should trigger DefineType()s on classes that we depend on.
1061 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1063 // Let's do it as soon as possible, since code below can call DefineType() on classes
1064 // that depend on us to be populated before they are.
1066 if (!(this is CompilerGeneratedClass) && !(this is Delegate))
1067 RootContext.RegisterOrder (this);
1069 if (!CheckRecursiveDefinition (this))
1072 if (base_type != null && base_type.Type != null) {
1073 TypeBuilder.SetParent (base_type.Type);
1076 // add interfaces that were not added at type creation
1077 if (iface_exprs != null) {
1078 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1082 foreach (Type itype in ifaces)
1083 TypeBuilder.AddInterfaceImplementation (itype);
1085 if (!CheckGenericInterfaces (ifaces))
1088 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1095 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1097 public TypeBuilder CreateType ()
1099 if (TypeBuilder != null)
1105 if (!CreateTypeBuilder ()) {
1110 if (partial_parts != null) {
1111 foreach (TypeContainer part in partial_parts)
1112 part.TypeBuilder = TypeBuilder;
1115 if (Types != null) {
1116 foreach (TypeContainer tc in Types) {
1117 if (tc.CreateType () == null) {
1129 public override TypeBuilder DefineType ()
1136 type_defined = true;
1138 if (CreateType () == null) {
1143 if (!DefineBaseTypes ()) {
1148 if (!DefineNestedTypes ()) {
1156 public override void SetParameterInfo (ArrayList constraints_list)
1158 base.SetParameterInfo (constraints_list);
1160 if (!is_generic || PartialContainer == this)
1163 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1164 for (int i = 0; i < tc_names.Length; ++i) {
1165 if (tc_names [i].Name != type_params [i].Name) {
1166 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1167 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1168 GetSignatureForError ());
1174 void UpdateTypeParameterConstraints (TypeContainer part)
1176 TypeParameter[] current_params = type_params;
1177 for (int i = 0; i < current_params.Length; i++) {
1178 Constraints c = part.type_params [i].Constraints;
1182 if (current_params [i].UpdateConstraints (part, c))
1185 Report.SymbolRelatedToPreviousError (Location, "");
1186 Report.Error (265, part.Location,
1187 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1188 GetSignatureForError (), current_params [i].GetSignatureForError ());
1192 public bool ResolveType ()
1194 if (!DoResolveType ())
1197 if (compiler_generated != null) {
1198 foreach (CompilerGeneratedClass c in compiler_generated)
1199 if (!c.ResolveType ())
1206 protected virtual bool DoResolveType ()
1211 if (PartialContainer != this)
1212 throw new InternalErrorException ();
1214 TypeExpr current_type = null;
1215 if (CurrentTypeParameters != null) {
1216 foreach (TypeParameter type_param in CurrentTypeParameters) {
1217 if (!type_param.Resolve (this)) {
1223 if (partial_parts != null) {
1224 foreach (TypeContainer part in partial_parts)
1225 UpdateTypeParameterConstraints (part);
1229 for (int i = 0; i < TypeParameters.Length; ++i) {
1231 // FIXME: Same should be done for delegates
1232 // TODO: Quite ugly way how to propagate constraints to
1235 if (nested_gen_params != null && i < nested_gen_params.Length) {
1236 TypeParameters [i].SetConstraints (nested_gen_params [i]);
1238 if (!TypeParameters [i].DefineType (this)) {
1245 // TODO: Very strange, why not simple make generic type from
1246 // current type parameters
1247 current_type = new GenericTypeExpr (this, Location);
1248 current_type = current_type.ResolveAsTypeTerminal (this, false);
1249 if (current_type == null) {
1254 currentType = current_type.Type;
1258 protected virtual bool DefineNestedTypes ()
1260 if (Types != null) {
1261 foreach (TypeContainer tc in Types)
1262 if (tc.DefineType () == null)
1266 if (Delegates != null) {
1267 foreach (Delegate d in Delegates)
1268 if (d.DefineType () == null)
1275 TypeContainer InTransit;
1277 protected bool CheckRecursiveDefinition (TypeContainer tc)
1279 if (InTransit != null) {
1280 Report.SymbolRelatedToPreviousError (this);
1281 if (this is Interface)
1283 529, tc.Location, "Inherited interface `{0}' causes a " +
1284 "cycle in the interface hierarchy of `{1}'",
1285 GetSignatureForError (), tc.GetSignatureForError ());
1288 146, tc.Location, "Circular base class dependency " +
1289 "involving `{0}' and `{1}'",
1290 tc.GetSignatureForError (), GetSignatureForError ());
1296 if (base_type != null && base_type.Type != null) {
1297 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1298 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1299 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1303 if (iface_exprs != null) {
1304 foreach (TypeExpr iface in iface_exprs) {
1305 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1306 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1307 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1312 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1319 public override TypeParameter[] CurrentTypeParameters {
1321 return PartialContainer.type_params;
1326 /// Populates our TypeBuilder with fields and methods
1328 public override bool Define ()
1330 if (members_defined)
1331 return members_defined_ok;
1333 members_defined_ok = DoDefineMembers ();
1334 members_defined = true;
1336 return members_defined_ok;
1339 protected virtual bool DoDefineMembers ()
1341 if (iface_exprs != null) {
1342 foreach (TypeExpr iface in iface_exprs) {
1343 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
1344 if ((oa != null) && !IsObsolete)
1345 AttributeTester.Report_ObsoleteMessage (
1346 oa, iface.GetSignatureForError (), Location, Report);
1348 GenericTypeExpr ct = iface as GenericTypeExpr;
1350 // TODO: passing `this' is wrong, should be base type iface instead
1351 TypeManager.CheckTypeVariance (ct.Type, Variance.Covariant, this);
1353 if (!ct.CheckConstraints (this))
1359 if (base_type != null) {
1360 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1361 if (obsolete_attr != null && !IsObsolete)
1362 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location, Report);
1364 GenericTypeExpr ct = base_type as GenericTypeExpr;
1365 if ((ct != null) && !ct.CheckConstraints (this))
1368 TypeContainer baseContainer = TypeManager.LookupTypeContainer(base_type.Type);
1369 if (baseContainer != null)
1370 baseContainer.Define();
1372 member_cache = new MemberCache (base_type.Type, this);
1373 } else if (Kind == Kind.Interface) {
1374 member_cache = new MemberCache (null, this);
1375 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1376 for (int i = 0; i < ifaces.Length; ++i)
1377 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1379 member_cache = new MemberCache (null, this);
1383 foreach (TypeContainer tc in types)
1384 member_cache.AddNestedType (tc);
1386 if (delegates != null)
1387 foreach (Delegate d in delegates)
1388 member_cache.AddNestedType (d);
1390 if (partial_parts != null) {
1391 foreach (TypeContainer part in partial_parts)
1392 part.member_cache = member_cache;
1396 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1397 if (conflict_symbol == null) {
1398 if ((ModFlags & Modifiers.NEW) != 0)
1399 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1401 if ((ModFlags & Modifiers.NEW) == 0) {
1402 Report.SymbolRelatedToPreviousError (conflict_symbol);
1403 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1404 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1409 DefineContainerMembers (constants);
1410 DefineContainerMembers (fields);
1412 if (Kind == Kind.Struct || Kind == Kind.Class) {
1413 pending = PendingImplementation.GetPendingImplementations (this);
1415 if (requires_delayed_unmanagedtype_check) {
1416 requires_delayed_unmanagedtype_check = false;
1417 foreach (FieldBase f in fields) {
1418 if (f.MemberType != null && f.MemberType.IsPointer)
1419 TypeManager.VerifyUnManaged (f.MemberType, f.Location);
1425 // Constructors are not in the defined_names array
1427 DefineContainerMembers (instance_constructors);
1429 DefineContainerMembers (events);
1430 DefineContainerMembers (ordered_explicit_member_list);
1431 DefineContainerMembers (ordered_member_list);
1433 DefineContainerMembers (operators);
1434 DefineContainerMembers (delegates);
1436 ComputeIndexerName();
1437 CheckEqualsAndGetHashCode();
1439 if (CurrentType != null) {
1440 GenericType = CurrentType;
1444 // FIXME: This hack is needed because member cache does not work
1445 // with generic types, we rely on runtime to inflate dynamic types.
1446 // TODO: This hack requires member cache refactoring to be removed
1448 if (TypeManager.IsGenericType (TypeBuilder))
1449 member_cache = new MemberCache (this);
1454 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1457 mcal.DefineContainerMembers ();
1460 protected virtual void ComputeIndexerName ()
1462 if (indexers == null)
1465 string class_indexer_name = null;
1468 // If there's both an explicit and an implicit interface implementation, the
1469 // explicit one actually implements the interface while the other one is just
1470 // a normal indexer. See bug #37714.
1473 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1474 foreach (Indexer i in indexers) {
1475 if (i.InterfaceType != null) {
1476 if (seen_normal_indexers)
1477 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1481 seen_normal_indexers = true;
1483 if (class_indexer_name == null) {
1484 class_indexer_name = i.ShortName;
1488 if (i.ShortName != class_indexer_name)
1489 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");
1492 if (class_indexer_name != null)
1493 indexer_name = class_indexer_name;
1496 protected virtual void EmitIndexerName ()
1498 if (!seen_normal_indexers)
1501 PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
1502 if (pa.Constructor == null &&
1503 !pa.ResolveConstructor (Location, TypeManager.string_type))
1506 CustomAttributeBuilder cb = new CustomAttributeBuilder (pa.Constructor, new string [] { IndexerName });
1507 TypeBuilder.SetCustomAttribute (cb);
1510 protected virtual void CheckEqualsAndGetHashCode ()
1512 if (methods == null)
1515 if (HasEquals && !HasGetHashCode) {
1516 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1520 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1522 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1526 /// This function is based by a delegate to the FindMembers routine
1528 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1534 /// This filter is used by FindMembers, and we just keep
1535 /// a global for the filter to `AlwaysAccept'
1537 static MemberFilter accepting_filter;
1540 static TypeContainer ()
1542 accepting_filter = new MemberFilter (AlwaysAccept);
1545 public MethodInfo[] GetMethods ()
1547 ArrayList members = new ArrayList ();
1551 if (methods != null) {
1552 int len = methods.Count;
1553 for (int i = 0; i < len; i++) {
1554 Method m = (Method) methods [i];
1556 members.Add (m.MethodBuilder);
1560 if (operators != null) {
1561 int len = operators.Count;
1562 for (int i = 0; i < len; i++) {
1563 Operator o = (Operator) operators [i];
1565 members.Add (o.MethodBuilder);
1569 if (properties != null) {
1570 int len = properties.Count;
1571 for (int i = 0; i < len; i++) {
1572 Property p = (Property) properties [i];
1574 if (p.GetBuilder != null)
1575 members.Add (p.GetBuilder);
1576 if (p.SetBuilder != null)
1577 members.Add (p.SetBuilder);
1581 if (indexers != null) {
1582 int len = indexers.Count;
1583 for (int i = 0; i < len; i++) {
1584 Indexer ix = (Indexer) indexers [i];
1586 if (ix.GetBuilder != null)
1587 members.Add (ix.GetBuilder);
1588 if (ix.SetBuilder != null)
1589 members.Add (ix.SetBuilder);
1593 if (events != null) {
1594 int len = events.Count;
1595 for (int i = 0; i < len; i++) {
1596 Event e = (Event) events [i];
1598 if (e.AddBuilder != null)
1599 members.Add (e.AddBuilder);
1600 if (e.RemoveBuilder != null)
1601 members.Add (e.RemoveBuilder);
1605 MethodInfo[] retMethods = new MethodInfo [members.Count];
1606 members.CopyTo (retMethods, 0);
1610 // Indicated whether container has StructLayout attribute set Explicit
1611 public bool HasExplicitLayout {
1612 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1613 set { caching_flags |= Flags.HasExplicitLayout; }
1616 public bool HasStructLayout {
1617 get { return (caching_flags & Flags.HasStructLayout) != 0; }
1618 set { caching_flags |= Flags.HasStructLayout; }
1622 // Return the nested type with name @name. Ensures that the nested type
1623 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1625 public Type FindNestedType (string name)
1627 if (PartialContainer != this)
1628 throw new InternalErrorException ("should not happen");
1630 ArrayList [] lists = { types, delegates };
1632 for (int j = 0; j < lists.Length; ++j) {
1633 ArrayList list = lists [j];
1637 int len = list.Count;
1638 for (int i = 0; i < len; ++i) {
1639 DeclSpace ds = (DeclSpace) list [i];
1640 if (ds.Basename == name) {
1641 return ds.DefineType ();
1649 private void FindMembers_NestedTypes (int modflags,
1650 BindingFlags bf, MemberFilter filter, object criteria,
1651 ref ArrayList members)
1653 ArrayList [] lists = { types, delegates };
1655 for (int j = 0; j < lists.Length; ++j) {
1656 ArrayList list = lists [j];
1660 int len = list.Count;
1661 for (int i = 0; i < len; i++) {
1662 DeclSpace ds = (DeclSpace) list [i];
1664 if ((ds.ModFlags & modflags) == 0)
1667 TypeBuilder tb = ds.TypeBuilder;
1669 if (!(criteria is string) || ds.Basename.Equals (criteria))
1670 tb = ds.DefineType ();
1673 if (tb != null && (filter (tb, criteria) == true)) {
1674 if (members == null)
1675 members = new ArrayList ();
1684 /// This method returns the members of this type just like Type.FindMembers would
1685 /// Only, we need to use this for types which are _being_ defined because MS'
1686 /// implementation can't take care of that.
1689 // FIXME: return an empty static array instead of null, that cleans up
1690 // some code and is consistent with some coding conventions I just found
1694 // Notice that in various cases we check if our field is non-null,
1695 // something that would normally mean that there was a bug elsewhere.
1697 // The problem happens while we are defining p-invoke methods, as those
1698 // will trigger a FindMembers, but this happens before things are defined
1700 // Since the whole process is a no-op, it is fine to check for null here.
1702 // TODO: This approach will be one day completely removed, it's already
1703 // used at few places only
1706 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1707 MemberFilter filter, object criteria)
1709 ArrayList members = null;
1712 if ((bf & BindingFlags.Public) != 0)
1713 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1715 if ((bf & BindingFlags.NonPublic) != 0)
1716 modflags |= Modifiers.PRIVATE;
1718 int static_mask = 0, static_flags = 0;
1719 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1720 case BindingFlags.Static:
1721 static_mask = static_flags = Modifiers.STATIC;
1724 case BindingFlags.Instance:
1725 static_mask = Modifiers.STATIC;
1730 static_mask = static_flags = 0;
1734 Timer.StartTimer (TimerType.TcFindMembers);
1737 filter = accepting_filter;
1739 if ((mt & MemberTypes.Field) != 0) {
1740 if (fields != null) {
1741 int len = fields.Count;
1742 for (int i = 0; i < len; i++) {
1743 FieldBase f = (FieldBase) fields [i];
1745 if ((f.ModFlags & modflags) == 0)
1747 if ((f.ModFlags & static_mask) != static_flags)
1750 FieldBuilder fb = f.FieldBuilder;
1751 if (fb != null && filter (fb, criteria) == true) {
1752 if (members == null)
1753 members = new ArrayList ();
1760 if (constants != null) {
1761 int len = constants.Count;
1762 for (int i = 0; i < len; i++) {
1763 Const con = (Const) constants [i];
1765 if ((con.ModFlags & modflags) == 0)
1767 if ((con.ModFlags & static_mask) != static_flags)
1770 FieldBuilder fb = con.FieldBuilder;
1773 fb = con.FieldBuilder;
1775 if (fb != null && filter (fb, criteria) == true) {
1776 if (members == null)
1777 members = new ArrayList ();
1785 if ((mt & MemberTypes.Method) != 0) {
1786 if (methods != null) {
1787 int len = methods.Count;
1788 for (int i = 0; i < len; i++) {
1789 MethodOrOperator m = (MethodOrOperator) methods [i];
1791 if ((m.ModFlags & modflags) == 0)
1793 if ((m.ModFlags & static_mask) != static_flags)
1796 MethodBuilder mb = m.MethodBuilder;
1798 if (mb != null && filter (mb, criteria) == true) {
1799 if (members == null)
1800 members = new ArrayList ();
1807 if (operators != null) {
1808 int len = operators.Count;
1809 for (int i = 0; i < len; i++) {
1810 Operator o = (Operator) operators [i];
1812 if ((o.ModFlags & modflags) == 0)
1814 if ((o.ModFlags & static_mask) != static_flags)
1817 MethodBuilder ob = o.MethodBuilder;
1818 if (ob != null && filter (ob, criteria) == true) {
1819 if (members == null)
1820 members = new ArrayList ();
1827 if (events != null) {
1828 foreach (Event e in events) {
1829 if ((e.ModFlags & modflags) == 0)
1831 if ((e.ModFlags & static_mask) != static_flags)
1834 MethodBuilder b = e.AddBuilder;
1835 if (b != null && filter (b, criteria)) {
1836 if (members == null)
1837 members = new ArrayList (4);
1842 b = e.RemoveBuilder;
1843 if (b != null && filter (b, criteria)) {
1844 if (members == null)
1845 members = new ArrayList (4);
1852 if (properties != null) {
1853 int len = properties.Count;
1854 for (int i = 0; i < len; i++) {
1855 Property p = (Property) properties [i];
1857 if ((p.ModFlags & modflags) == 0)
1859 if ((p.ModFlags & static_mask) != static_flags)
1865 if (b != null && filter (b, criteria) == true) {
1866 if (members == null)
1867 members = new ArrayList ();
1873 if (b != null && filter (b, criteria) == true) {
1874 if (members == null)
1875 members = new ArrayList ();
1882 if (indexers != null) {
1883 int len = indexers.Count;
1884 for (int i = 0; i < len; i++) {
1885 Indexer ix = (Indexer) indexers [i];
1887 if ((ix.ModFlags & modflags) == 0)
1889 if ((ix.ModFlags & static_mask) != static_flags)
1895 if (b != null && filter (b, criteria) == true) {
1896 if (members == null)
1897 members = new ArrayList ();
1903 if (b != null && filter (b, criteria) == true) {
1904 if (members == null)
1905 members = new ArrayList ();
1913 if ((mt & MemberTypes.Event) != 0) {
1914 if (events != null) {
1915 int len = events.Count;
1916 for (int i = 0; i < len; i++) {
1917 Event e = (Event) events [i];
1919 if ((e.ModFlags & modflags) == 0)
1921 if ((e.ModFlags & static_mask) != static_flags)
1924 MemberInfo eb = e.EventBuilder;
1925 if (eb != null && filter (eb, criteria) == true) {
1926 if (members == null)
1927 members = new ArrayList ();
1929 members.Add (e.EventBuilder);
1935 if ((mt & MemberTypes.Property) != 0){
1936 if (properties != null) {
1937 int len = properties.Count;
1938 for (int i = 0; i < len; i++) {
1939 Property p = (Property) properties [i];
1941 if ((p.ModFlags & modflags) == 0)
1943 if ((p.ModFlags & static_mask) != static_flags)
1946 MemberInfo pb = p.PropertyBuilder;
1947 if (pb != null && filter (pb, criteria) == true) {
1948 if (members == null)
1949 members = new ArrayList ();
1951 members.Add (p.PropertyBuilder);
1956 if (indexers != null) {
1957 int len = indexers.Count;
1958 for (int i = 0; i < len; i++) {
1959 Indexer ix = (Indexer) indexers [i];
1961 if ((ix.ModFlags & modflags) == 0)
1963 if ((ix.ModFlags & static_mask) != static_flags)
1966 MemberInfo ib = ix.PropertyBuilder;
1967 if (ib != null && filter (ib, criteria) == true) {
1968 if (members == null)
1969 members = new ArrayList ();
1971 members.Add (ix.PropertyBuilder);
1977 if ((mt & MemberTypes.NestedType) != 0)
1978 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1980 if ((mt & MemberTypes.Constructor) != 0){
1981 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1982 int len = instance_constructors.Count;
1983 for (int i = 0; i < len; i++) {
1984 Constructor c = (Constructor) instance_constructors [i];
1986 ConstructorBuilder cb = c.ConstructorBuilder;
1987 if (cb != null && filter (cb, criteria) == true) {
1988 if (members == null)
1989 members = new ArrayList ();
1996 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1997 ConstructorBuilder cb =
1998 default_static_constructor.ConstructorBuilder;
2000 if (cb != null && filter (cb, criteria) == true) {
2001 if (members == null)
2002 members = new ArrayList ();
2010 // Lookup members in base if requested.
2012 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2013 if (TypeBuilder.BaseType != null) {
2014 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2015 if (list.Count > 0) {
2016 if (members == null)
2017 members = new ArrayList ();
2019 members.AddRange (list);
2024 Timer.StopTimer (TimerType.TcFindMembers);
2026 if (members == null)
2027 return MemberList.Empty;
2029 return new MemberList (members);
2032 public override MemberCache MemberCache {
2034 return member_cache;
2038 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2039 MemberFilter filter, object criteria)
2041 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2044 return ds.FindMembers (mt, bf, filter, criteria);
2046 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2050 /// Emits the values for the constants
2052 public void EmitConstants ()
2054 if (constants != null)
2055 foreach (Const con in constants)
2060 void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2065 foreach (MemberCore mc in al) {
2066 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2069 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2070 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2075 public virtual void VerifyMembers ()
2078 // Check for internal or private fields that were never assigned
2080 if (Report.WarningLevel >= 3) {
2081 CheckMemberUsage (properties, "property");
2082 CheckMemberUsage (methods, "method");
2083 CheckMemberUsage (constants, "constant");
2085 if (fields != null){
2086 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2087 foreach (FieldBase f in fields) {
2088 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2089 if (is_type_exposed)
2092 f.SetMemberIsUsed ();
2096 if ((f.caching_flags & Flags.IsAssigned) == 0)
2097 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2099 Report.Warning (414, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2100 f.GetSignatureForError ());
2106 // Only report 649 on level 4
2108 if (Report.WarningLevel < 4)
2111 if ((f.caching_flags & Flags.IsAssigned) != 0)
2115 // Don't be pendatic over serializable attributes
2117 if (f.OptAttributes != null || PartialContainer.HasStructLayout)
2120 Constant c = New.Constantify (f.MemberType);
2121 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2122 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2128 // TODO: move to ClassOrStruct
2129 void EmitConstructors ()
2131 if (instance_constructors == null)
2134 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2135 bool has_compliant_args = false;
2137 foreach (Constructor c in instance_constructors) {
2141 catch (Exception e) {
2142 throw new InternalErrorException (c, e);
2145 if (has_compliant_args)
2148 has_compliant_args = c.HasCompliantArgs;
2150 if (!has_compliant_args)
2151 Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2153 foreach (Constructor c in instance_constructors) {
2157 catch (Exception e) {
2158 throw new InternalErrorException (c, e);
2165 /// Emits the code, this step is performed after all
2166 /// the types, enumerations, constructors
2168 public virtual void EmitType ()
2170 if (OptAttributes != null)
2171 OptAttributes.Emit ();
2175 EmitConstructors ();
2177 // Can not continue if constants are broken
2179 if (Report.Errors > 0)
2182 if (default_static_constructor != null)
2183 default_static_constructor.Emit ();
2185 if (operators != null)
2186 foreach (Operator o in operators)
2189 if (properties != null)
2190 foreach (Property p in properties)
2193 if (indexers != null) {
2194 foreach (Indexer indx in indexers)
2199 if (events != null){
2200 foreach (Event e in Events)
2204 if (methods != null) {
2205 for (int i = 0; i < methods.Count; ++i)
2206 ((MethodOrOperator) methods [i]).Emit ();
2210 foreach (FieldBase f in fields)
2213 if (delegates != null) {
2214 foreach (Delegate d in Delegates) {
2219 if (pending != null)
2220 pending.VerifyPendingMethods (Report);
2222 if (Report.Errors > 0)
2225 if (compiler_generated != null) {
2226 for (int i = 0; i < compiler_generated.Count; ++i)
2227 ((CompilerGeneratedClass) compiler_generated [i]).EmitType ();
2231 public override void CloseType ()
2233 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2237 caching_flags |= Flags.CloseTypeCreated;
2238 TypeBuilder.CreateType ();
2239 } catch (TypeLoadException){
2241 // This is fine, the code still created the type
2243 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2244 // Console.WriteLine (e.Message);
2245 } catch (Exception e) {
2246 throw new InternalErrorException (this, e);
2250 foreach (TypeContainer tc in Types)
2251 if (tc.Kind == Kind.Struct)
2254 foreach (TypeContainer tc in Types)
2255 if (tc.Kind != Kind.Struct)
2259 if (Delegates != null)
2260 foreach (Delegate d in Delegates)
2263 if (compiler_generated != null)
2264 foreach (CompilerGeneratedClass c in compiler_generated)
2267 PartialContainer = null;
2272 initialized_fields = null;
2273 initialized_static_fields = null;
2275 ordered_explicit_member_list = null;
2276 ordered_member_list = null;
2281 compiler_generated = null;
2282 default_constructor = null;
2283 default_static_constructor = null;
2285 OptAttributes = null;
2288 member_cache = null;
2292 // Performs the validation on a Method's modifiers (properties have
2293 // the same properties).
2295 public bool MethodModifiersValid (MemberCore mc)
2297 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2298 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2299 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2301 int flags = mc.ModFlags;
2304 // At most one of static, virtual or override
2306 if ((flags & Modifiers.STATIC) != 0){
2307 if ((flags & vao) != 0){
2308 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2309 mc.GetSignatureForError ());
2314 if (Kind == Kind.Struct){
2315 if ((flags & va) != 0){
2316 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract", Report);
2321 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2322 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2323 mc.GetSignatureForError ());
2328 // If the declaration includes the abstract modifier, then the
2329 // declaration does not include static, virtual or extern
2331 if ((flags & Modifiers.ABSTRACT) != 0){
2332 if ((flags & Modifiers.EXTERN) != 0){
2334 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2338 if ((flags & Modifiers.SEALED) != 0) {
2339 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2343 if ((flags & Modifiers.VIRTUAL) != 0){
2344 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2348 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2349 Report.SymbolRelatedToPreviousError (this);
2350 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2351 mc.GetSignatureForError (), GetSignatureForError ());
2356 if ((flags & Modifiers.PRIVATE) != 0){
2357 if ((flags & vao) != 0){
2358 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2363 if ((flags & Modifiers.SEALED) != 0){
2364 if ((flags & Modifiers.OVERRIDE) == 0){
2365 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2373 public Constructor DefaultStaticConstructor {
2374 get { return default_static_constructor; }
2377 protected override bool VerifyClsCompliance ()
2379 if (!base.VerifyClsCompliance ())
2384 Type base_type = TypeBuilder.BaseType;
2385 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2386 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2393 /// Checks whether container name is CLS Compliant
2395 void VerifyClsName ()
2397 Hashtable base_members = base_cache == null ?
2399 base_cache.GetPublicMembers ();
2400 Hashtable this_members = new Hashtable ();
2402 foreach (DictionaryEntry entry in defined_names) {
2403 MemberCore mc = (MemberCore)entry.Value;
2404 if (!mc.IsClsComplianceRequired ())
2407 string name = (string) entry.Key;
2408 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2410 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2411 object found = base_members [lcase];
2412 if (found == null) {
2413 found = this_members [lcase];
2414 if (found == null) {
2415 this_members.Add (lcase, mc);
2420 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2423 if (found is MemberInfo) {
2424 if (basename == ((MemberInfo) found).Name)
2426 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2428 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2431 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2437 /// Performs checks for an explicit interface implementation. First it
2438 /// checks whether the `interface_type' is a base inteface implementation.
2439 /// Then it checks whether `name' exists in the interface type.
2441 public bool VerifyImplements (InterfaceMemberBase mb)
2443 if (ifaces != null) {
2444 foreach (Type t in ifaces){
2445 if (TypeManager.IsEqual (t, mb.InterfaceType))
2450 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2451 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2452 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2456 public override Type LookupAnyGeneric (string typeName)
2458 if (types != null) {
2459 foreach (TypeContainer tc in types) {
2463 int pos = tc.Basename.LastIndexOf ('`');
2464 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2465 return tc.TypeBuilder;
2469 return base.LookupAnyGeneric (typeName);
2472 public void Mark_HasEquals ()
2474 cached_method |= CachedMethods.Equals;
2477 public void Mark_HasGetHashCode ()
2479 cached_method |= CachedMethods.GetHashCode;
2483 /// Method container contains Equals method
2485 public bool HasEquals {
2487 return (cached_method & CachedMethods.Equals) != 0;
2492 /// Method container contains GetHashCode method
2494 public bool HasGetHashCode {
2496 return (cached_method & CachedMethods.GetHashCode) != 0;
2500 public bool HasStaticFieldInitializer {
2502 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2506 cached_method |= CachedMethods.HasStaticFieldInitializer;
2508 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2516 string IMemberContainer.Name {
2522 Type IMemberContainer.Type {
2528 bool IMemberContainer.IsInterface {
2530 return Kind == Kind.Interface;
2534 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2536 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2538 if (GenericType != null)
2539 return TypeManager.FindMembers (GenericType, mt, new_bf,
2542 return FindMembers (mt, new_bf, null, null);
2546 // Generates xml doc comments (if any), and if required,
2547 // handle warning report.
2549 internal override void GenerateDocComment (DeclSpace ds)
2551 DocUtil.GenerateTypeDocComment (this, ds, Report);
2554 public override string DocCommentHeader {
2555 get { return "T:"; }
2558 public MemberCache BaseCache {
2560 if (base_cache != null)
2562 if (TypeBuilder.BaseType != null)
2563 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2564 if (TypeBuilder.IsInterface)
2565 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2571 public abstract class ClassOrStruct : TypeContainer {
2572 ListDictionary declarative_security;
2574 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2575 MemberName name, Attributes attrs, Kind kind)
2576 : base (ns, parent, name, attrs, kind)
2580 protected override bool AddToContainer (MemberCore symbol, string name)
2582 if (name == MemberName.Name) {
2583 if (symbol is TypeParameter) {
2584 Report.Error (694, symbol.Location,
2585 "Type parameter `{0}' has same name as containing type, or method",
2586 symbol.GetSignatureForError ());
2590 InterfaceMemberBase imb = symbol as InterfaceMemberBase;
2591 if (imb == null || !imb.IsExplicitImpl) {
2592 Report.SymbolRelatedToPreviousError (this);
2593 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2594 symbol.GetSignatureForError ());
2599 return base.AddToContainer (symbol, name);
2602 public override void VerifyMembers ()
2604 base.VerifyMembers ();
2606 if ((events != null) && Report.WarningLevel >= 3) {
2607 foreach (Event e in events){
2608 // Note: The event can be assigned from same class only, so we can report
2609 // this warning for all accessibility modes
2610 if ((e.caching_flags & Flags.IsUsed) == 0)
2611 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2616 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2618 if (a.IsValidSecurityAttribute ()) {
2619 if (declarative_security == null)
2620 declarative_security = new ListDictionary ();
2622 a.ExtractSecurityPermissionSet (declarative_security);
2626 if (a.Type == pa.StructLayout) {
2627 PartialContainer.HasStructLayout = true;
2629 if (a.GetLayoutKindValue () == LayoutKind.Explicit)
2630 PartialContainer.HasExplicitLayout = true;
2633 base.ApplyAttributeBuilder (a, cb, pa);
2637 /// Defines the default constructors
2639 protected void DefineDefaultConstructor (bool is_static)
2641 // The default instance constructor is public
2642 // If the class is abstract, the default constructor is protected
2643 // The default static constructor is private
2647 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2649 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2652 Constructor c = new Constructor (this, MemberName.Name, mods,
2653 null, ParametersCompiled.EmptyReadOnlyParameters,
2654 new GeneratedBaseInitializer (Location),
2658 c.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
2661 public override bool Define ()
2663 CheckProtectedModifier ();
2667 if (default_static_constructor != null)
2668 default_static_constructor.Define ();
2673 public override void Emit ()
2675 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer) {
2676 DefineDefaultConstructor (true);
2677 default_static_constructor.Define ();
2682 if (declarative_security != null) {
2683 foreach (DictionaryEntry de in declarative_security) {
2684 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2689 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2691 DeclSpace top_level = Parent;
2692 if (top_level != null) {
2693 while (top_level.Parent != null)
2694 top_level = top_level.Parent;
2696 ArrayList candidates = NamespaceEntry.NS.LookupExtensionMethod (extensionType, this, name);
2697 if (candidates != null)
2698 return new ExtensionMethodGroupExpr (candidates, NamespaceEntry, extensionType, loc);
2701 return NamespaceEntry.LookupExtensionMethod (extensionType, name, loc);
2704 protected override TypeAttributes TypeAttr {
2706 if (default_static_constructor == null)
2707 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2709 return base.TypeAttr;
2715 // TODO: should be sealed
2716 public class Class : ClassOrStruct {
2717 const int AllowedModifiers =
2720 Modifiers.PROTECTED |
2721 Modifiers.INTERNAL |
2723 Modifiers.ABSTRACT |
2728 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2730 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2732 : base (ns, parent, name, attrs, Kind.Class)
2734 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2735 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location, Report);
2737 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2738 Report.FeatureIsNotAvailable (Location, "static classes");
2742 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2744 if (part.Name == "System.Object")
2745 Report.Error (537, part.Location,
2746 "The class System.Object cannot have a base class or implement an interface.");
2747 base.AddBasesForPart (part, bases);
2750 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
2752 if (a.Type == pa.AttributeUsage) {
2753 if (!TypeManager.IsAttributeType (BaseType) &&
2754 TypeBuilder.FullName != "System.Attribute") {
2755 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2759 if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) {
2760 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2764 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
2765 a.Error_MissingGuidAttribute ();
2769 if (a.Type == pa.Extension) {
2770 a.Error_MisusedExtensionAttribute ();
2774 if (AttributeTester.IsAttributeExcluded (a.Type, Location))
2777 base.ApplyAttributeBuilder (a, cb, pa);
2780 public override AttributeTargets AttributeTargets {
2782 return AttributeTargets.Class;
2786 protected override void DefineContainerMembers (MemberCoreArrayList list)
2792 base.DefineContainerMembers (list);
2796 foreach (MemberCore m in list) {
2797 if (m is Operator) {
2798 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2802 if (m is Destructor) {
2803 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2808 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2812 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2815 if (m is Constructor) {
2816 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2820 Method method = m as Method;
2821 if (method != null && method.Parameters.HasExtensionMethodType) {
2822 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2826 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2829 base.DefineContainerMembers (list);
2832 public override bool Define ()
2834 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2835 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2838 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2839 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2842 return base.Define ();
2845 protected override bool DoDefineMembers ()
2847 if (InstanceConstructors == null && !IsStatic)
2848 DefineDefaultConstructor (false);
2850 return base.DoDefineMembers ();
2853 public override void Emit ()
2857 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2858 PredefinedAttributes.Get.Extension.EmitAttribute (TypeBuilder);
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 && TypeManager.IsGenericParameter (base_class.Type)){
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 type `{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 (!IsAccessibleAs (base_class.Type)) {
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 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
2943 foreach (Attribute a in attrs) {
2944 string condition = a.GetConditionalAttributeValue ();
2945 if (Location.CompilationUnit.IsConditionalDefined (condition))
2949 caching_flags |= Flags.Excluded;
2954 // FIXME: How do we deal with the user specifying a different
2957 protected override TypeAttributes TypeAttr {
2959 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2961 ta |= StaticClassAttribute;
2967 public sealed class Struct : ClassOrStruct {
2969 bool is_unmanaged, has_unmanaged_check_done;
2972 // Modifiers allowed in a struct declaration
2974 const int AllowedModifiers =
2977 Modifiers.PROTECTED |
2978 Modifiers.INTERNAL |
2982 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2983 int mod, Attributes attrs)
2984 : base (ns, parent, name, attrs, Kind.Struct)
2988 if (parent.Parent == null)
2989 accmods = Modifiers.INTERNAL;
2991 accmods = Modifiers.PRIVATE;
2993 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location, Report);
2995 this.ModFlags |= Modifiers.SEALED;
2998 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3000 base.ApplyAttributeBuilder (a, cb, pa);
3003 // When struct constains fixed fixed and struct layout has explicitly
3004 // set CharSet, its value has to be propagated to compiler generated
3005 // fixed field types
3007 if (a.Type == pa.StructLayout && Fields != null && a.HasField ("CharSet")) {
3008 for (int i = 0; i < Fields.Count; ++i) {
3009 FixedField ff = Fields [i] as FixedField;
3011 ff.SetCharSet (TypeBuilder.Attributes);
3016 public override AttributeTargets AttributeTargets {
3018 return AttributeTargets.Struct;
3022 public override bool IsUnmanagedType ()
3027 if (requires_delayed_unmanagedtype_check)
3030 if (has_unmanaged_check_done)
3031 return is_unmanaged;
3033 has_unmanaged_check_done = true;
3035 foreach (FieldBase f in fields) {
3036 if ((f.ModFlags & Modifiers.STATIC) != 0)
3039 // It can happen when recursive unmanaged types are defined
3040 // struct S { S* s; }
3041 Type mt = f.MemberType;
3043 has_unmanaged_check_done = false;
3044 requires_delayed_unmanagedtype_check = true;
3048 // TODO: Remove when pointer types are under mcs control
3049 while (mt.IsPointer)
3050 mt = TypeManager.GetElementType (mt);
3051 if (TypeManager.IsEqual (mt, TypeBuilder))
3054 if (TypeManager.IsUnmanagedType (mt))
3060 is_unmanaged = true;
3064 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
3066 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
3068 // If we are compiling our runtime,
3069 // and we are defining ValueType, then our
3070 // base is `System.Object'.
3072 if (base_class == null) {
3073 if (!RootContext.StdLib && Name == "System.ValueType")
3074 base_class = TypeManager.system_object_expr;
3076 base_class = TypeManager.system_valuetype_expr;
3083 // FIXME: Allow the user to specify a different set of attributes
3084 // in some cases (Sealed for example is mandatory for a class,
3085 // but what SequentialLayout can be changed
3087 protected override TypeAttributes TypeAttr {
3089 const TypeAttributes DefaultTypeAttributes =
3090 TypeAttributes.SequentialLayout |
3091 TypeAttributes.Sealed;
3093 return base.TypeAttr | DefaultTypeAttributes;
3097 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3099 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3100 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3101 field.GetSignatureForError ());
3104 base.RegisterFieldForInitialization (field, expression);
3112 public sealed class Interface : TypeContainer, IMemberContainer {
3115 /// Modifiers allowed in a class declaration
3117 public const int AllowedModifiers =
3120 Modifiers.PROTECTED |
3121 Modifiers.INTERNAL |
3125 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3127 : base (ns, parent, name, attrs, Kind.Interface)
3131 if (parent.Parent == null)
3132 accmods = Modifiers.INTERNAL;
3134 accmods = Modifiers.PRIVATE;
3136 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location, Report);
3139 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3141 if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) {
3142 a.Error_MissingGuidAttribute ();
3146 base.ApplyAttributeBuilder (a, cb, pa);
3150 public override AttributeTargets AttributeTargets {
3152 return AttributeTargets.Interface;
3156 protected override TypeAttributes TypeAttr {
3158 const TypeAttributes DefaultTypeAttributes =
3159 TypeAttributes.AutoLayout |
3160 TypeAttributes.Abstract |
3161 TypeAttributes.Interface;
3163 return base.TypeAttr | DefaultTypeAttributes;
3167 protected override bool VerifyClsCompliance ()
3169 if (!base.VerifyClsCompliance ())
3172 if (ifaces != null) {
3173 foreach (Type t in ifaces) {
3174 if (AttributeTester.IsClsCompliant (t))
3177 Report.SymbolRelatedToPreviousError (t);
3178 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3179 GetSignatureForError (), TypeManager.CSharpName (t));
3187 // It is used as a base class for all property based members
3188 // This includes properties, indexers, and events
3189 public abstract class PropertyBasedMember : InterfaceMemberBase
3191 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3192 FullNamedExpression type, int mod, int allowed_mod,
3193 MemberName name, Attributes attrs)
3194 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3198 protected override bool VerifyClsCompliance ()
3200 if (!base.VerifyClsCompliance ())
3203 if (!AttributeTester.IsClsCompliant (MemberType)) {
3204 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
3205 GetSignatureForError ());
3213 public abstract class MethodCore : InterfaceMemberBase
3215 public readonly ParametersCompiled Parameters;
3216 protected ToplevelBlock block;
3218 public MethodCore (DeclSpace parent, GenericMethod generic,
3219 FullNamedExpression type, int mod, int allowed_mod,
3220 MemberName name, Attributes attrs, ParametersCompiled parameters)
3221 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3223 Parameters = parameters;
3227 // Returns the System.Type array for the parameters of this method
3229 public Type [] ParameterTypes {
3231 return Parameters.Types;
3235 public ParametersCompiled ParameterInfo {
3241 public ToplevelBlock Block {
3251 public CallingConventions CallingConventions {
3253 CallingConventions cc = Parameters.CallingConvention;
3255 if ((ModFlags & Modifiers.STATIC) == 0)
3256 cc |= CallingConventions.HasThis;
3258 // FIXME: How is `ExplicitThis' used in C#?
3264 protected override bool CheckBase ()
3266 // Check whether arguments were correct.
3267 if (!DefineParameters (Parameters))
3270 return base.CheckBase ();
3274 // Returns a string that represents the signature for this
3275 // member which should be used in XML documentation.
3277 public override string GetDocCommentName (DeclSpace ds)
3279 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3283 // Raised (and passed an XmlElement that contains the comment)
3284 // when GenerateDocComment is writing documentation expectedly.
3286 // FIXME: with a few effort, it could be done with XmlReader,
3287 // that means removal of DOM use.
3289 internal override void OnGenerateDocComment (XmlElement el)
3291 DocUtil.OnMethodGenerateDocComment (this, el, Report);
3295 // Represents header string for documentation comment.
3297 public override string DocCommentHeader
3299 get { return "M:"; }
3302 public override bool EnableOverloadChecks (MemberCore overload)
3304 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3305 caching_flags |= Flags.MethodOverloadsExist;
3309 return base.EnableOverloadChecks (overload);
3312 protected override bool VerifyClsCompliance ()
3314 if (!base.VerifyClsCompliance ())
3317 if (Parameters.HasArglist) {
3318 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
3321 if (!AttributeTester.IsClsCompliant (MemberType)) {
3322 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
3323 GetSignatureForError ());
3326 Parameters.VerifyClsCompliance ();
3332 public abstract class InterfaceMemberBase : MemberBase {
3334 // Whether this is an interface member.
3336 public bool IsInterface;
3339 // If true, this is an explicit interface implementation
3341 public bool IsExplicitImpl;
3343 protected bool is_external_implementation;
3346 // The interface type we are explicitly implementing
3348 public Type InterfaceType;
3351 // The method we're overriding if this is an override method.
3353 protected MethodInfo base_method;
3355 readonly int explicit_mod_flags;
3356 public MethodAttributes flags;
3358 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3359 FullNamedExpression type, int mod, int allowed_mod,
3360 MemberName name, Attributes attrs)
3361 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3364 IsInterface = parent.PartialContainer.Kind == Kind.Interface;
3365 IsExplicitImpl = (MemberName.Left != null);
3366 explicit_mod_flags = mod;
3369 protected override bool CheckBase ()
3371 if (!base.CheckBase ())
3374 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
3375 CheckForDuplications ();
3380 // Is null for System.Object while compiling corlib and base interfaces
3381 if (Parent.PartialContainer.BaseCache == null) {
3382 if ((ModFlags & Modifiers.NEW) != 0) {
3383 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3388 Type base_ret_type = null;
3389 base_method = FindOutBaseMethod (ref base_ret_type);
3391 // method is override
3392 if (base_method != null) {
3393 if (!CheckMethodAgainstBase (base_ret_type))
3396 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3397 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3399 if (OptAttributes == null || !OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3400 Report.SymbolRelatedToPreviousError (base_method);
3401 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3402 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3405 if (OptAttributes != null && OptAttributes.Contains (PredefinedAttributes.Get.Obsolete)) {
3406 Report.SymbolRelatedToPreviousError (base_method);
3407 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3408 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3415 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3416 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3417 if (conflict_symbol != null) {
3418 Report.SymbolRelatedToPreviousError (conflict_symbol);
3420 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3421 else if (this is PropertyBase)
3422 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3424 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3426 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3427 GetSignatureForError (), SimpleName.GetMemberType (this));
3432 if (conflict_symbol == null) {
3433 if ((ModFlags & Modifiers.NEW) != 0) {
3434 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3439 if ((ModFlags & Modifiers.NEW) == 0) {
3440 if (this is MethodOrOperator && conflict_symbol.MemberType == MemberTypes.Method)
3443 Report.SymbolRelatedToPreviousError (conflict_symbol);
3444 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3445 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3451 protected virtual bool CheckForDuplications ()
3453 return Parent.MemberCache.CheckExistingMembersOverloads (
3454 this, GetFullName (MemberName), ParametersCompiled.EmptyReadOnlyParameters, Report);
3458 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3459 // that have been defined.
3461 // `name' is the user visible name for reporting errors (this is used to
3462 // provide the right name regarding method names and properties)
3464 bool CheckMethodAgainstBase (Type base_method_type)
3468 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3469 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3470 Report.SymbolRelatedToPreviousError (base_method);
3471 Report.Error (506, Location,
3472 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3473 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3477 // Now we check that the overriden method is not final
3479 if (base_method.IsFinal) {
3480 Report.SymbolRelatedToPreviousError (base_method);
3481 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3482 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3486 // Check that the permissions are not being changed
3488 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3489 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3491 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3492 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3496 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3497 Report.SymbolRelatedToPreviousError (base_method);
3498 if (this is PropertyBasedMember) {
3499 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3500 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3503 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3504 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3510 if ((ModFlags & Modifiers.NEW) == 0) {
3511 if ((ModFlags & Modifiers.OVERRIDE) == 0) {
3512 ModFlags |= Modifiers.NEW;
3513 Report.SymbolRelatedToPreviousError (base_method);
3514 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3515 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",
3516 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3517 if (base_method.IsAbstract){
3518 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3519 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3523 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3524 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3528 if (base_method.IsAbstract && !IsInterface) {
3529 Report.SymbolRelatedToPreviousError (base_method);
3530 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3531 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3539 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3541 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3543 // when overriding protected internal, the method can be declared
3544 // protected internal only within the same assembly or assembly
3545 // which has InternalsVisibleTo
3547 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3548 return TypeManager.IsThisOrFriendAssembly (Parent.Module.Assembly, base_method.DeclaringType.Assembly);
3549 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3551 // if it's not "protected internal", it must be "protected"
3555 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3557 // protected within the same assembly - an error
3560 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3561 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3563 // protected ok, but other attributes differ - report an error
3569 return (thisp == base_classp);
3573 public override bool Define ()
3576 ModFlags = Modifiers.PUBLIC | Modifiers.ABSTRACT |
3577 Modifiers.VIRTUAL | (ModFlags & (Modifiers.UNSAFE | Modifiers.NEW));
3579 flags = MethodAttributes.Public |
3580 MethodAttributes.Abstract |
3581 MethodAttributes.HideBySig |
3582 MethodAttributes.NewSlot |
3583 MethodAttributes.Virtual;
3585 Parent.PartialContainer.MethodModifiersValid (this);
3587 flags = Modifiers.MethodAttr (ModFlags);
3590 if (IsExplicitImpl) {
3591 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3592 if (iface_texpr == null)
3595 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3596 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3597 GetSignatureForError ());
3600 InterfaceType = iface_texpr.Type;
3602 if (!InterfaceType.IsInterface) {
3603 Report.SymbolRelatedToPreviousError (InterfaceType);
3604 Report.Error (538, Location, "The type `{0}' in explicit interface declaration is not an interface",
3605 TypeManager.CSharpName (InterfaceType));
3607 Parent.PartialContainer.VerifyImplements (this);
3610 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location, Report);
3613 return base.Define ();
3616 protected bool DefineParameters (ParametersCompiled parameters)
3618 if (!parameters.Resolve (this))
3622 for (int i = 0; i < parameters.Count; ++i) {
3623 Parameter p = parameters [i];
3625 if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1)))
3626 p.Warning_UselessOptionalParameter (Report);
3628 if (p.CheckAccessibility (this))
3631 Type t = parameters.Types [i];
3632 Report.SymbolRelatedToPreviousError (t);
3633 if (this is Indexer)
3634 Report.Error (55, Location,
3635 "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'",
3636 TypeManager.CSharpName (t), GetSignatureForError ());
3637 else if (this is Operator)
3638 Report.Error (57, Location,
3639 "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'",
3640 TypeManager.CSharpName (t), GetSignatureForError ());
3642 Report.Error (51, Location,
3643 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3644 TypeManager.CSharpName (t), GetSignatureForError ());
3650 public override void Emit()
3652 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3653 // We are more strict than csc and report this as an error because SRE does not allow emit that
3654 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3655 if (this is Constructor) {
3656 Report.Error (824, Location,
3657 "Constructor `{0}' is marked `external' but has no external implementation specified", GetSignatureForError ());
3659 Report.Error (626, Location,
3660 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3661 GetSignatureForError ());
3668 public override bool EnableOverloadChecks (MemberCore overload)
3671 // Two members can differ in their explicit interface
3672 // type parameter only
3674 InterfaceMemberBase imb = overload as InterfaceMemberBase;
3675 if (imb != null && imb.IsExplicitImpl) {
3676 if (IsExplicitImpl) {
3677 caching_flags |= Flags.MethodOverloadsExist;
3682 return IsExplicitImpl;
3685 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3687 Report.SymbolRelatedToPreviousError (base_method);
3688 string base_name = TypeManager.GetFullNameSignature (base_method);
3689 string this_name = GetSignatureForError ();
3690 if (suffix != null) {
3691 base_name += suffix;
3692 this_name += suffix;
3695 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3696 this_name, Modifiers.GetDescription (ma), base_name);
3699 protected static string Error722 {
3701 return "`{0}': static types cannot be used as return types";
3706 /// Gets base method and its return type
3708 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3711 // The "short" name of this property / indexer / event. This is the
3712 // name without the explicit interface.
3714 public string ShortName
3716 get { return MemberName.Name; }
3717 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3721 // Returns full metadata method name
3723 public string GetFullName (MemberName name)
3725 if (!IsExplicitImpl)
3729 // When dealing with explicit members a full interface type
3730 // name is added to member name to avoid possible name conflicts
3732 // We use CSharpName which gets us full name with benefit of
3733 // replacing predefined names which saves some space and name
3736 return TypeManager.CSharpName (InterfaceType) + "." + name.Name;
3739 protected override bool VerifyClsCompliance ()
3741 if (!base.VerifyClsCompliance ()) {
3742 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3743 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3746 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3747 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3752 if (GenericMethod != null)
3753 GenericMethod.VerifyClsCompliance ();
3758 public override bool IsUsed
3760 get { return IsExplicitImpl || base.IsUsed; }
3765 public abstract class MethodOrOperator : MethodCore, IMethodData
3767 public MethodBuilder MethodBuilder;
3768 ReturnParameter return_attributes;
3769 ListDictionary declarative_security;
3770 protected MethodData MethodData;
3772 static string[] attribute_targets = new string [] { "method", "return" };
3774 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3775 int allowed_mod, MemberName name,
3776 Attributes attrs, ParametersCompiled parameters)
3777 : base (parent, generic, type, mod, allowed_mod, name,
3782 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
3784 if (a.Target == AttributeTargets.ReturnValue) {
3785 if (return_attributes == null)
3786 return_attributes = new ReturnParameter (MethodBuilder, Location);
3788 return_attributes.ApplyAttributeBuilder (a, cb, pa);
3792 if (a.IsInternalMethodImplAttribute) {
3793 is_external_implementation = true;
3796 if (a.Type == pa.DllImport) {
3797 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3798 if ((ModFlags & extern_static) != extern_static) {
3799 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3801 is_external_implementation = true;
3804 if (a.IsValidSecurityAttribute ()) {
3805 if (declarative_security == null)
3806 declarative_security = new ListDictionary ();
3807 a.ExtractSecurityPermissionSet (declarative_security);
3811 if (MethodBuilder != null)
3812 MethodBuilder.SetCustomAttribute (cb);
3815 public override AttributeTargets AttributeTargets {
3817 return AttributeTargets.Method;
3821 protected override bool CheckForDuplications ()
3823 string name = GetFullName (MemberName);
3824 if (MemberName.IsGeneric)
3825 name = MemberName.MakeName (name, MemberName.TypeArguments);
3827 return Parent.MemberCache.CheckExistingMembersOverloads (this, name, Parameters, Report);
3830 public virtual EmitContext CreateEmitContext (ILGenerator ig)
3832 return new EmitContext (
3833 this, ig, MemberType);
3836 protected override bool ResolveMemberType ()
3838 if (GenericMethod != null) {
3839 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3840 if (!GenericMethod.Define (this))
3844 return base.ResolveMemberType ();
3847 public override bool Define ()
3849 if (!base.Define ())
3855 if (block != null && block.IsIterator && !(Parent is IteratorStorey)) {
3857 // Current method is turned into automatically generated
3858 // wrapper which creates an instance of iterator
3860 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
3861 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
3864 if (IsPartialDefinition) {
3865 caching_flags &= ~Flags.Excluded_Undetected;
3866 caching_flags |= Flags.Excluded;
3867 // Add to member cache only when a partial method implementation is not there
3868 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3869 MethodBase mb = new PartialMethodDefinitionInfo (this);
3870 Parent.MemberCache.AddMember (mb, this);
3871 TypeManager.AddMethod (mb, this);
3877 MethodData = new MethodData (
3878 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3880 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName), Report))
3883 MethodBuilder = MethodData.MethodBuilder;
3885 if (TypeManager.IsGenericMethod (MethodBuilder))
3886 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3888 Parent.MemberCache.AddMember (MethodBuilder, this);
3893 protected override void DoMemberTypeIndependentChecks ()
3895 base.DoMemberTypeIndependentChecks ();
3897 CheckAbstractAndExtern (block != null);
3899 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3900 for (int i = 0; i < Parameters.Count; ++i) {
3901 IParameterData p = Parameters.FixedParameters [i];
3902 if (p.ModFlags == Parameter.Modifier.OUT) {
3903 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3904 GetSignatureForError ());
3907 if (p.HasDefaultValue && IsPartialImplementation)
3908 ((Parameter) p).Warning_UselessOptionalParameter (Report);
3913 protected override void DoMemberTypeDependentChecks ()
3915 base.DoMemberTypeDependentChecks ();
3917 if (!TypeManager.IsGenericParameter (MemberType)) {
3918 if (MemberType.IsAbstract && MemberType.IsSealed) {
3919 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3924 public override void Emit ()
3926 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
3927 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (MethodBuilder);
3928 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3929 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (MethodBuilder);
3931 if (TypeManager.IsDynamicType (ReturnType)) {
3932 return_attributes = new ReturnParameter (MethodBuilder, Location);
3933 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
3936 if (OptAttributes != null)
3937 OptAttributes.Emit ();
3939 if (declarative_security != null) {
3940 foreach (DictionaryEntry de in declarative_security) {
3941 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3945 if (MethodData != null)
3946 MethodData.Emit (Parent);
3954 protected void Error_ConditionalAttributeIsNotValid ()
3956 Report.Error (577, Location,
3957 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3958 GetSignatureForError ());
3961 public bool IsPartialDefinition {
3963 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3967 public bool IsPartialImplementation {
3969 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3973 public override string[] ValidAttributeTargets {
3975 return attribute_targets;
3979 #region IMethodData Members
3981 public Type ReturnType {
3987 public MemberName MethodName {
3994 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3996 public bool IsExcluded () {
3997 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3998 return (caching_flags & Flags.Excluded) != 0;
4000 caching_flags &= ~Flags.Excluded_Undetected;
4002 if (base_method == null) {
4003 if (OptAttributes == null)
4006 Attribute[] attrs = OptAttributes.SearchMulti (PredefinedAttributes.Get.Conditional);
4011 foreach (Attribute a in attrs) {
4012 string condition = a.GetConditionalAttributeValue ();
4013 if (condition == null)
4016 if (Location.CompilationUnit.IsConditionalDefined (condition))
4020 caching_flags |= Flags.Excluded;
4024 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
4026 if (AttributeTester.IsConditionalMethodExcluded (base_method, Location)) {
4027 caching_flags |= Flags.Excluded;
4033 if (md.IsExcluded ()) {
4034 caching_flags |= Flags.Excluded;
4040 GenericMethod IMethodData.GenericMethod {
4042 return GenericMethod;
4046 public virtual void EmitExtraSymbolInfo (SourceMethod source)
4053 public class SourceMethod : IMethodDef
4056 SourceMethodBuilder builder;
4058 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
4060 this.method = method;
4062 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
4065 public string Name {
4066 get { return method.Name; }
4071 if (method is MethodBuilder)
4072 return ((MethodBuilder) method).GetToken ().Token;
4073 else if (method is ConstructorBuilder)
4074 return ((ConstructorBuilder) method).GetToken ().Token;
4076 throw new NotSupportedException ();
4080 public void CloseMethod ()
4082 SymbolWriter.CloseMethod ();
4085 public void SetRealMethodName (string name)
4087 if (builder != null)
4088 builder.SetRealMethodName (name);
4091 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4093 if (!SymbolWriter.HasSymbolWriter)
4098 Location start_loc = block.StartLocation;
4099 if (start_loc.IsNull)
4102 ICompileUnit compile_unit = start_loc.CompilationUnit;
4103 if (compile_unit == null)
4106 return new SourceMethod (parent, method, compile_unit);
4110 public class Method : MethodOrOperator {
4113 /// Modifiers allowed in a class declaration
4115 const int AllowedModifiers =
4118 Modifiers.PROTECTED |
4119 Modifiers.INTERNAL |
4124 Modifiers.OVERRIDE |
4125 Modifiers.ABSTRACT |
4129 const int AllowedInterfaceModifiers =
4130 Modifiers.NEW | Modifiers.UNSAFE;
4132 Method partialMethodImplementation;
4134 public Method (DeclSpace parent, GenericMethod generic,
4135 FullNamedExpression return_type, int mod,
4136 MemberName name, ParametersCompiled parameters, Attributes attrs)
4137 : base (parent, generic, return_type, mod,
4138 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
4139 name, attrs, parameters)
4143 protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
4144 MemberName name, ParametersCompiled parameters, Attributes attrs)
4145 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
4149 public override string GetSignatureForError()
4151 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4154 void Error_DuplicateEntryPoint (Method b)
4156 Report.Error (17, b.Location,
4157 "Program `{0}' has more than one entry point defined: `{1}'",
4158 CodeGen.FileName, b.GetSignatureForError ());
4161 bool IsEntryPoint ()
4163 if (ReturnType != TypeManager.void_type &&
4164 ReturnType != TypeManager.int32_type)
4167 if (Parameters.Count == 0)
4170 if (Parameters.Count > 1)
4173 Type t = Parameters.Types [0];
4174 return t.IsArray && t.GetArrayRank () == 1 &&
4175 TypeManager.GetElementType (t) == TypeManager.string_type &&
4176 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4179 public override FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
4181 TypeParameter[] tp = CurrentTypeParameters;
4183 TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
4185 return new TypeParameterExpr (t, loc);
4188 return base.LookupNamespaceOrType (name, loc, ignore_cs0104);
4191 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4193 if (a.Type == pa.Conditional) {
4194 if (IsExplicitImpl) {
4195 Error_ConditionalAttributeIsNotValid ();
4199 if (ReturnType != TypeManager.void_type) {
4200 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4204 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4205 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4210 Report.Error (582, Location, "Conditional not valid on interface members");
4214 if (MethodData.implementing != null) {
4215 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4216 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4217 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4221 for (int i = 0; i < Parameters.Count; ++i) {
4222 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
4223 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4229 if (a.Type == pa.Extension) {
4230 a.Error_MisusedExtensionAttribute ();
4234 base.ApplyAttributeBuilder (a, cb, pa);
4237 protected override bool CheckForDuplications ()
4239 if (!base.CheckForDuplications ())
4242 ArrayList ar = Parent.PartialContainer.Properties;
4244 for (int i = 0; i < ar.Count; ++i) {
4245 PropertyBase pb = (PropertyBase) ar [i];
4246 if (pb.AreAccessorsDuplicateImplementation (this))
4251 ar = Parent.PartialContainer.Indexers;
4253 for (int i = 0; i < ar.Count; ++i) {
4254 PropertyBase pb = (PropertyBase) ar [i];
4255 if (pb.AreAccessorsDuplicateImplementation (this))
4263 protected override bool CheckBase ()
4265 if (!base.CheckBase ())
4268 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == Destructor.MetadataName) {
4269 Report.Error (249, Location, "Do not override `{0}'. Use destructor syntax instead",
4270 TypeManager.CSharpSignature (base_method));
4276 public override TypeParameter[] CurrentTypeParameters {
4278 if (GenericMethod != null)
4279 return GenericMethod.CurrentTypeParameters;
4288 public override bool Define ()
4290 if (type_name == TypeManager.system_void_expr && Parameters.IsEmpty && Name == Destructor.MetadataName) {
4291 Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4294 if (!base.Define ())
4297 if (partialMethodImplementation != null && IsPartialDefinition)
4298 MethodBuilder = partialMethodImplementation.MethodBuilder;
4300 if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
4301 Error1599 (Location, ReturnType, Report);
4305 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4306 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4307 Parent.PartialContainer.Mark_HasEquals ();
4308 else if (Parameters.IsEmpty && Name == "GetHashCode")
4309 Parent.PartialContainer.Mark_HasGetHashCode ();
4312 if ((ModFlags & Modifiers.STATIC) == 0)
4315 if (Parameters.HasExtensionMethodType) {
4316 if (Parent.PartialContainer.IsStaticClass && !Parent.IsGeneric) {
4317 if (!Parent.IsTopLevel)
4318 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4319 GetSignatureForError ());
4321 PredefinedAttribute pa = PredefinedAttributes.Get.Extension;
4322 if (!pa.IsDefined) {
4323 Report.Error (1110, Location,
4324 "`{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",
4325 GetSignatureForError ());
4328 ModFlags |= Modifiers.METHOD_EXTENSION;
4329 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
4330 CodeGen.Assembly.HasExtensionMethods = true;
4332 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4333 GetSignatureForError ());
4338 // This is used to track the Entry Point,
4340 if (RootContext.NeedsEntryPoint &&
4342 (RootContext.MainClass == null ||
4343 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4344 if (IsEntryPoint ()) {
4346 if (RootContext.EntryPoint == null) {
4347 if (Parent.IsGeneric || MemberName.IsGeneric) {
4348 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4349 GetSignatureForError ());
4352 RootContext.EntryPoint = this;
4355 Error_DuplicateEntryPoint (RootContext.EntryPoint);
4356 Error_DuplicateEntryPoint (this);
4359 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4360 GetSignatureForError ());
4370 public override void Emit ()
4373 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4374 if (IsPartialDefinition) {
4376 // Use partial method implementation builder for partial method declaration attributes
4378 if (partialMethodImplementation != null) {
4379 MethodBuilder = partialMethodImplementation.MethodBuilder;
4382 } else if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
4383 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4384 GetSignatureForError ());
4389 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4390 PredefinedAttributes.Get.Extension.EmitAttribute (MethodBuilder);
4392 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4393 Location, MethodBuilder);
4398 public override bool EnableOverloadChecks (MemberCore overload)
4400 // TODO: It can be deleted when members will be defined in correct order
4401 if (overload is Operator)
4402 return overload.EnableOverloadChecks (this);
4404 if (overload is Indexer)
4407 return base.EnableOverloadChecks (overload);
4410 public static void Error1599 (Location loc, Type t, Report Report)
4412 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4415 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4417 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4418 Parent.TypeBuilder, Name, Parameters, GenericMethod, false);
4423 if (mi.IsSpecialName)
4426 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4430 public void SetPartialDefinition (Method methodDefinition)
4432 caching_flags |= Flags.PartialDefinitionExists;
4433 methodDefinition.partialMethodImplementation = this;
4435 for (int i = 0; i < methodDefinition.Parameters.Count; ++i ) {
4436 Parameters [i].DefaultValue = methodDefinition.Parameters [i].DefaultValue;
4439 if (methodDefinition.attributes == null)
4442 if (attributes == null) {
4443 attributes = methodDefinition.attributes;
4445 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4449 protected override bool VerifyClsCompliance ()
4451 if (!base.VerifyClsCompliance ())
4454 if (!Parameters.IsEmpty) {
4455 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4457 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder, Report);
4464 public abstract class ConstructorInitializer : ExpressionStatement
4466 Arguments argument_list;
4467 MethodGroupExpr base_constructor_group;
4469 public ConstructorInitializer (Arguments argument_list, Location loc)
4471 this.argument_list = argument_list;
4475 public Arguments Arguments {
4477 return argument_list;
4481 public override Expression CreateExpressionTree (ResolveContext ec)
4483 throw new NotSupportedException ("ET");
4486 public override Expression DoResolve (ResolveContext ec)
4488 eclass = ExprClass.Value;
4490 // TODO: ec.GetSignatureForError ()
4491 ConstructorBuilder caller_builder = ((Constructor) ec.MemberContext).ConstructorBuilder;
4493 if (argument_list != null) {
4497 // Spec mandates that constructor initializer will not have `this' access
4499 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
4500 argument_list.Resolve (ec, out dynamic);
4504 SimpleName ctor = new SimpleName (ConstructorBuilder.ConstructorName, loc);
4505 return new DynamicInvocation (ctor, argument_list, loc).Resolve (ec) as ExpressionStatement;
4509 type = ec.CurrentType;
4510 if (this is ConstructorBaseInitializer) {
4511 if (ec.CurrentType.BaseType == null)
4514 type = ec.CurrentType.BaseType;
4515 if (TypeManager.IsStruct (ec.CurrentType)) {
4516 ec.Report.Error (522, loc,
4517 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4522 // It is legal to have "this" initializers that take no arguments
4523 // in structs, they are just no-ops.
4525 // struct D { public D (int a) : this () {}
4527 if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
4531 base_constructor_group = MemberLookupFinal (
4532 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4533 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4534 loc) as MethodGroupExpr;
4536 if (base_constructor_group == null)
4539 base_constructor_group = base_constructor_group.OverloadResolve (
4540 ec, ref argument_list, false, loc);
4542 if (base_constructor_group == null)
4546 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4548 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4550 if (base_ctor == caller_builder){
4551 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4557 public override void Emit (EmitContext ec)
4559 // It can be null for static initializers
4560 if (base_constructor_group == null)
4565 base_constructor_group.EmitCall (ec, argument_list);
4568 public override void EmitStatement (EmitContext ec)
4574 public class ConstructorBaseInitializer : ConstructorInitializer {
4575 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
4576 base (argument_list, l)
4581 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4582 public GeneratedBaseInitializer (Location loc):
4588 public class ConstructorThisInitializer : ConstructorInitializer {
4589 public ConstructorThisInitializer (Arguments argument_list, Location l) :
4590 base (argument_list, l)
4595 public class Constructor : MethodCore, IMethodData {
4596 public ConstructorBuilder ConstructorBuilder;
4597 public ConstructorInitializer Initializer;
4598 ListDictionary declarative_security;
4599 bool has_compliant_args;
4602 // Modifiers allowed for a constructor.
4604 public const int AllowedModifiers =
4606 Modifiers.PROTECTED |
4607 Modifiers.INTERNAL |
4613 static readonly string[] attribute_targets = new string [] { "method" };
4616 // The spec claims that static is not permitted, but
4617 // my very own code has static constructors.
4619 public Constructor (DeclSpace parent, string name, int mod, Attributes attrs, ParametersCompiled args,
4620 ConstructorInitializer init, Location loc)
4621 : base (parent, null, null, mod, AllowedModifiers,
4622 new MemberName (name, loc), attrs, args)
4627 public bool HasCompliantArgs {
4628 get { return has_compliant_args; }
4631 public override AttributeTargets AttributeTargets {
4632 get { return AttributeTargets.Constructor; }
4636 // Returns true if this is a default constructor
4638 public bool IsDefault ()
4640 if ((ModFlags & Modifiers.STATIC) != 0)
4641 return Parameters.IsEmpty;
4643 return Parameters.IsEmpty &&
4644 (Initializer is ConstructorBaseInitializer) &&
4645 (Initializer.Arguments == null);
4648 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
4650 if (a.IsValidSecurityAttribute ()) {
4651 if (declarative_security == null) {
4652 declarative_security = new ListDictionary ();
4654 a.ExtractSecurityPermissionSet (declarative_security);
4658 if (a.IsInternalMethodImplAttribute) {
4659 is_external_implementation = true;
4662 ConstructorBuilder.SetCustomAttribute (cb);
4665 protected override bool CheckBase ()
4667 if ((ModFlags & Modifiers.STATIC) != 0) {
4668 if (!Parameters.IsEmpty) {
4669 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4670 GetSignatureForError ());
4674 // the rest can be ignored
4678 // Check whether arguments were correct.
4679 if (!DefineParameters (Parameters))
4682 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4683 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorInfo.ConstructorName,
4684 Parameters, Report);
4686 if (Parent.PartialContainer.Kind == Kind.Struct) {
4687 if (Parameters.Count == 0) {
4688 Report.Error (568, Location,
4689 "Structs cannot contain explicit parameterless constructors");
4694 CheckProtectedModifier ();
4700 // Creates the ConstructorBuilder
4702 public override bool Define ()
4704 if (ConstructorBuilder != null)
4707 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4708 MethodAttributes.SpecialName);
4710 if ((ModFlags & Modifiers.STATIC) != 0) {
4711 ca |= MethodAttributes.Static | MethodAttributes.Private;
4713 ca |= MethodAttributes.HideBySig;
4715 if ((ModFlags & Modifiers.PUBLIC) != 0)
4716 ca |= MethodAttributes.Public;
4717 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4718 if ((ModFlags & Modifiers.INTERNAL) != 0)
4719 ca |= MethodAttributes.FamORAssem;
4721 ca |= MethodAttributes.Family;
4722 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4723 ca |= MethodAttributes.Assembly;
4725 ca |= MethodAttributes.Private;
4728 if (!CheckAbstractAndExtern (block != null))
4731 // Check if arguments were correct.
4735 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4736 ca, CallingConventions,
4737 Parameters.GetEmitTypes ());
4739 if (Parent.PartialContainer.IsComImport) {
4740 if (!IsDefault ()) {
4741 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4742 Parent.GetSignatureForError ());
4744 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4747 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4748 TypeManager.AddMethod (ConstructorBuilder, this);
4750 // It's here only to report an error
4751 if (block != null && block.IsIterator) {
4752 member_type = TypeManager.void_type;
4753 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
4762 public override void Emit ()
4764 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4765 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (ConstructorBuilder);
4767 if (OptAttributes != null)
4768 OptAttributes.Emit ();
4773 // If we use a "this (...)" constructor initializer, then
4774 // do not emit field initializers, they are initialized in the other constructor
4776 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4777 !(Initializer is ConstructorThisInitializer);
4779 BlockContext bc = new BlockContext (this, block, TypeManager.void_type);
4780 bc.Set (ResolveContext.Options.ConstructorScope);
4782 if (emit_field_initializers)
4783 Parent.PartialContainer.ResolveFieldInitializers (bc);
4785 if (block != null) {
4786 // If this is a non-static `struct' constructor and doesn't have any
4787 // initializer, it must initialize all of the struct's fields.
4788 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4789 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4790 block.AddThisVariable (Parent, Location);
4792 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4793 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4794 Initializer = new GeneratedBaseInitializer (Location);
4796 if (Initializer != null) {
4797 block.AddScopeStatement (new StatementExpression (Initializer));
4802 Parameters.ApplyAttributes (ConstructorBuilder);
4804 SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4806 if (block != null) {
4807 if (block.Resolve (null, bc, Parameters, this)) {
4808 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
4809 ec.With (EmitContext.Options.ConstructorScope, true);
4811 if (!ec.HasReturnLabel && bc.HasReturnLabel) {
4812 ec.ReturnLabel = bc.ReturnLabel;
4813 ec.HasReturnLabel = true;
4821 source.CloseMethod ();
4823 if (declarative_security != null) {
4824 foreach (DictionaryEntry de in declarative_security) {
4825 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4832 // Is never override
4833 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4838 public override string GetSignatureForError()
4840 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4843 public override string[] ValidAttributeTargets {
4845 return attribute_targets;
4849 protected override bool VerifyClsCompliance ()
4851 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4855 if (!Parameters.IsEmpty) {
4856 ArrayList al = (ArrayList)Parent.MemberCache.Members [ConstructorInfo.ConstructorName];
4858 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder, Report);
4860 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4861 foreach (Type param in Parameters.Types) {
4862 if (param.IsArray) {
4868 has_compliant_args = true;
4872 #region IMethodData Members
4874 public MemberName MethodName {
4880 public Type ReturnType {
4886 public EmitContext CreateEmitContext (ILGenerator ig)
4888 throw new NotImplementedException ();
4891 public bool IsExcluded()
4896 GenericMethod IMethodData.GenericMethod {
4902 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4909 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4911 public interface IMethodData
4913 CallingConventions CallingConventions { get; }
4914 Location Location { get; }
4915 MemberName MethodName { get; }
4916 Type ReturnType { get; }
4917 GenericMethod GenericMethod { get; }
4918 ParametersCompiled ParameterInfo { get; }
4920 Attributes OptAttributes { get; }
4921 ToplevelBlock Block { get; set; }
4923 EmitContext CreateEmitContext (ILGenerator ig);
4924 ObsoleteAttribute GetObsoleteAttribute ();
4925 string GetSignatureForError ();
4927 bool IsClsComplianceRequired ();
4928 void SetMemberIsUsed ();
4929 void EmitExtraSymbolInfo (SourceMethod source);
4933 // Encapsulates most of the Method's state
4935 public class MethodData {
4936 static FieldInfo methodbuilder_attrs_field;
4937 public readonly IMethodData method;
4939 public readonly GenericMethod GenericMethod;
4942 // Are we implementing an interface ?
4944 public MethodInfo implementing;
4949 protected InterfaceMemberBase member;
4950 protected int modifiers;
4951 protected MethodAttributes flags;
4952 protected Type declaring_type;
4953 protected MethodInfo parent_method;
4955 MethodBuilder builder = null;
4956 public MethodBuilder MethodBuilder {
4962 public Type DeclaringType {
4964 return declaring_type;
4968 public MethodData (InterfaceMemberBase member,
4969 int modifiers, MethodAttributes flags, IMethodData method)
4971 this.member = member;
4972 this.modifiers = modifiers;
4975 this.method = method;
4978 public MethodData (InterfaceMemberBase member,
4979 int modifiers, MethodAttributes flags,
4980 IMethodData method, MethodBuilder builder,
4981 GenericMethod generic, MethodInfo parent_method)
4982 : this (member, modifiers, flags, method)
4984 this.builder = builder;
4985 this.GenericMethod = generic;
4986 this.parent_method = parent_method;
4989 public bool Define (DeclSpace parent, string method_full_name, Report Report)
4991 string name = method.MethodName.Basename;
4993 TypeContainer container = parent.PartialContainer;
4995 PendingImplementation pending = container.PendingImplementations;
4996 if (pending != null){
4997 implementing = pending.IsInterfaceMethod (name, member.InterfaceType, this);
4999 if (member.InterfaceType != null){
5000 if (implementing == null){
5001 if (member is PropertyBase) {
5002 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
5003 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
5004 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
5007 Report.Error (539, method.Location,
5008 "`{0}.{1}' in explicit interface declaration is not a member of interface",
5009 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
5013 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
5014 Report.SymbolRelatedToPreviousError (implementing);
5015 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
5016 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
5020 if (implementing != null) {
5021 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
5022 if (prop_method == null) {
5023 if (TypeManager.IsSpecialMethod (implementing)) {
5024 Report.SymbolRelatedToPreviousError (implementing);
5025 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
5026 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
5027 implementing.Name.StartsWith ("get_") ? "get" : "set");
5029 } else if (implementing.DeclaringType.IsInterface) {
5030 if (!implementing.IsSpecialName) {
5031 Report.SymbolRelatedToPreviousError (implementing);
5032 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
5033 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
5036 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
5037 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
5038 Report.SymbolRelatedToPreviousError (implementing);
5039 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
5040 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
5049 // For implicit implementations, make sure we are public, for
5050 // explicit implementations, make sure we are private.
5052 if (implementing != null){
5054 // Setting null inside this block will trigger a more
5055 // verbose error reporting for missing interface implementations
5057 // The "candidate" function has been flagged already
5058 // but it wont get cleared
5060 if (member.IsExplicitImpl){
5061 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
5062 Report.SymbolRelatedToPreviousError (implementing);
5063 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
5064 method.GetSignatureForError ());
5068 if (implementing.DeclaringType.IsInterface) {
5070 // If this is an interface method implementation,
5071 // check for public accessibility
5073 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5075 implementing = null;
5077 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5078 // We may never be private.
5079 implementing = null;
5081 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5083 // We may be protected if we're overriding something.
5085 implementing = null;
5090 // Static is not allowed
5092 if ((modifiers & Modifiers.STATIC) != 0){
5093 implementing = null;
5098 // If implementing is still valid, set flags
5100 if (implementing != null){
5102 // When implementing interface methods, set NewSlot
5103 // unless, we are overwriting a method.
5105 if (implementing.DeclaringType.IsInterface){
5106 if ((modifiers & Modifiers.OVERRIDE) == 0)
5107 flags |= MethodAttributes.NewSlot;
5110 MethodAttributes.Virtual |
5111 MethodAttributes.HideBySig;
5113 // Set Final unless we're virtual, abstract or already overriding a method.
5114 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5115 flags |= MethodAttributes.Final;
5118 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
5120 if (builder == null)
5123 if (container.CurrentType != null)
5124 declaring_type = container.CurrentType;
5126 declaring_type = container.TypeBuilder;
5128 if (implementing != null && member.IsExplicitImpl) {
5129 container.TypeBuilder.DefineMethodOverride (builder, implementing);
5132 TypeManager.AddMethod (builder, method);
5134 if (GenericMethod != null) {
5135 bool is_override = member.IsExplicitImpl |
5136 ((modifiers & Modifiers.OVERRIDE) != 0);
5138 if (implementing != null)
5139 parent_method = implementing;
5141 if (!GenericMethod.DefineType (GenericMethod, builder, parent_method, is_override))
5150 /// Create the MethodBuilder for the method
5152 void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
5154 if (builder == null) {
5155 builder = container.TypeBuilder.DefineMethod (
5156 method_name, flags, method.CallingConventions,
5157 TypeManager.TypeToReflectionType (method.ReturnType),
5158 param.GetEmitTypes ());
5163 // Generic method has been already defined to resolve method parameters
5164 // correctly when they use type parameters
5166 builder.SetParameters (param.GetEmitTypes ());
5167 builder.SetReturnType (method.ReturnType);
5168 if (builder.Attributes != flags) {
5170 if (methodbuilder_attrs_field == null)
5171 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5172 methodbuilder_attrs_field.SetValue (builder, flags);
5174 RootContext.ToplevelTypes.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5182 public void Emit (DeclSpace parent)
5184 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5186 if (GenericMethod != null)
5187 GenericMethod.EmitAttributes ();
5190 // clear the pending implementation flag
5192 if (implementing != null)
5193 parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName.Basename,
5194 member.InterfaceType, this, member.IsExplicitImpl);
5196 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5198 ToplevelBlock block = method.Block;
5199 if (block != null) {
5200 BlockContext bc = new BlockContext ((IMemberContext) method, block, method.ReturnType);
5201 if (block.Resolve (null, bc, method.ParameterInfo, method)) {
5202 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
5203 if (!ec.HasReturnLabel && bc.HasReturnLabel) {
5204 ec.ReturnLabel = bc.ReturnLabel;
5205 ec.HasReturnLabel = true;
5212 if (source != null) {
5213 method.EmitExtraSymbolInfo (source);
5214 source.CloseMethod ();
5219 public class Destructor : MethodOrOperator
5221 const int AllowedModifiers =
5225 static readonly string[] attribute_targets = new string [] { "method" };
5227 public static readonly string MetadataName = "Finalize";
5229 public Destructor (DeclSpace parent, int mod, ParametersCompiled parameters, Attributes attrs, Location l)
5230 : base (parent, null, TypeManager.system_void_expr, mod, AllowedModifiers,
5231 new MemberName (MetadataName, l), attrs, parameters)
5233 ModFlags &= ~Modifiers.PRIVATE;
5234 ModFlags |= Modifiers.PROTECTED;
5237 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5239 if (a.Type == pa.Conditional) {
5240 Error_ConditionalAttributeIsNotValid ();
5244 base.ApplyAttributeBuilder (a, cb, pa);
5247 protected override bool CheckBase ()
5249 flags |= MethodAttributes.Virtual;
5251 if (!base.CheckBase ())
5254 if (Parent.PartialContainer.BaseCache == null)
5257 Type base_type = Parent.PartialContainer.BaseCache.Container.Type;
5258 if (base_type != null && Block != null) {
5259 MethodGroupExpr method_expr = Expression.MethodLookup (Parent.Module.Compiler, Parent.TypeBuilder, base_type, MetadataName, Location);
5260 if (method_expr == null)
5261 throw new NotImplementedException ();
5263 method_expr.IsBase = true;
5264 method_expr.InstanceExpression = new CompilerGeneratedThis (Parent.TypeBuilder, Location);
5266 ToplevelBlock new_block = new ToplevelBlock (Compiler, Block.StartLocation);
5267 new_block.EndLocation = Block.EndLocation;
5269 Block finaly_block = new ExplicitBlock (new_block, Location, Location);
5270 Block try_block = new Block (new_block, block);
5273 // 0-size arguments to avoid CS0250 error
5274 // TODO: Should use AddScopeStatement or something else which emits correct
5277 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
5278 new_block.AddStatement (new TryFinally (try_block, finaly_block, Location));
5286 public override string GetSignatureForError ()
5288 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5291 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
5296 public override string[] ValidAttributeTargets {
5298 return attribute_targets;
5303 public abstract class MemberBase : MemberCore
5305 protected FullNamedExpression type_name;
5306 protected Type member_type;
5308 public readonly DeclSpace ds;
5309 public readonly GenericMethod GenericMethod;
5311 protected MemberBase (DeclSpace parent, GenericMethod generic,
5312 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5313 MemberName name, Attributes attrs)
5314 : base (parent, name, attrs)
5316 this.ds = generic != null ? generic : (DeclSpace) parent;
5317 this.type_name = type;
5318 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location, Report);
5319 GenericMethod = generic;
5320 if (GenericMethod != null)
5321 GenericMethod.ModFlags = ModFlags;
5325 // Main member define entry
5327 public override bool Define ()
5329 DoMemberTypeIndependentChecks ();
5332 // Returns false only when type resolution failed
5334 if (!ResolveMemberType ())
5337 DoMemberTypeDependentChecks ();
5342 // Any type_name independent checks
5344 protected virtual void DoMemberTypeIndependentChecks ()
5346 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5347 (ModFlags & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5348 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5349 GetSignatureForError (), Parent.GetSignatureForError ());
5354 // Any type_name dependent checks
5356 protected virtual void DoMemberTypeDependentChecks ()
5358 // verify accessibility
5359 if (!IsAccessibleAs (MemberType)) {
5360 Report.SymbolRelatedToPreviousError (MemberType);
5361 if (this is Property)
5362 Report.Error (53, Location,
5363 "Inconsistent accessibility: property type `" +
5364 TypeManager.CSharpName (MemberType) + "' is less " +
5365 "accessible than property `" + GetSignatureForError () + "'");
5366 else if (this is Indexer)
5367 Report.Error (54, Location,
5368 "Inconsistent accessibility: indexer return type `" +
5369 TypeManager.CSharpName (MemberType) + "' is less " +
5370 "accessible than indexer `" + GetSignatureForError () + "'");
5371 else if (this is MethodCore) {
5372 if (this is Operator)
5373 Report.Error (56, Location,
5374 "Inconsistent accessibility: return type `" +
5375 TypeManager.CSharpName (MemberType) + "' is less " +
5376 "accessible than operator `" + GetSignatureForError () + "'");
5378 Report.Error (50, Location,
5379 "Inconsistent accessibility: return type `" +
5380 TypeManager.CSharpName (MemberType) + "' is less " +
5381 "accessible than method `" + GetSignatureForError () + "'");
5383 Report.Error (52, Location,
5384 "Inconsistent accessibility: field type `" +
5385 TypeManager.CSharpName (MemberType) + "' is less " +
5386 "accessible than field `" + GetSignatureForError () + "'");
5390 Variance variance = this is Event ? Variance.Contravariant : Variance.Covariant;
5391 TypeManager.CheckTypeVariance (MemberType, variance, this);
5394 protected bool IsTypePermitted ()
5396 if (TypeManager.IsSpecialType (MemberType)) {
5397 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5403 protected virtual bool CheckBase ()
5405 CheckProtectedModifier ();
5410 public Type MemberType {
5411 get { return member_type; }
5414 protected virtual bool ResolveMemberType ()
5416 if (member_type != null)
5417 throw new InternalErrorException ("Multi-resolve");
5419 TypeExpr te = type_name.ResolveAsTypeTerminal (this, false);
5424 // Replace original type name, error reporting can use fully resolved name
5428 member_type = te.Type;
5434 // Abstract class for all fields
5436 abstract public class FieldBase : MemberBase {
5437 public FieldBuilder FieldBuilder;
5438 public Status status;
5439 protected Expression initializer;
5442 public enum Status : byte {
5443 HAS_OFFSET = 4 // Used by FieldMember.
5446 static readonly string[] attribute_targets = new string [] { "field" };
5448 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5449 int allowed_mod, MemberName name, Attributes attrs)
5450 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5453 if ((mod & Modifiers.ABSTRACT) != 0)
5454 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5457 public override AttributeTargets AttributeTargets {
5459 return AttributeTargets.Field;
5463 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
5465 if (a.Type == pa.FieldOffset) {
5466 status |= Status.HAS_OFFSET;
5468 if (!Parent.PartialContainer.HasExplicitLayout) {
5469 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5473 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5474 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5479 if (a.Type == pa.FixedBuffer) {
5480 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5485 if (a.Type == pa.MarshalAs) {
5486 UnmanagedMarshal marshal = a.GetMarshal (this);
5487 if (marshal != null) {
5488 FieldBuilder.SetMarshal (marshal);
5493 if ((a.HasSecurityAttribute)) {
5494 a.Error_InvalidSecurityParent ();
5498 FieldBuilder.SetCustomAttribute (cb);
5501 protected override bool CheckBase ()
5503 if (!base.CheckBase ())
5506 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5507 if (conflict_symbol == null) {
5508 if ((ModFlags & Modifiers.NEW) != 0) {
5509 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5514 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
5515 Report.SymbolRelatedToPreviousError (conflict_symbol);
5516 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5517 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5523 protected override void DoMemberTypeDependentChecks ()
5525 base.DoMemberTypeDependentChecks ();
5527 if (TypeManager.IsGenericParameter (MemberType))
5530 if (MemberType.IsSealed && MemberType.IsAbstract) {
5531 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType, Report);
5539 // Represents header string for documentation comment.
5541 public override string DocCommentHeader {
5542 get { return "F:"; }
5545 public override void Emit ()
5547 if (TypeManager.IsDynamicType (member_type))
5548 PredefinedAttributes.Get.Dynamic.EmitAttribute (FieldBuilder);
5550 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
5551 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
5553 if (OptAttributes != null) {
5554 OptAttributes.Emit ();
5557 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5558 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
5564 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class, Report Report)
5566 Report.SymbolRelatedToPreviousError (static_class);
5567 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5571 public Expression Initializer {
5573 if (value != null) {
5574 this.initializer = value;
5579 protected virtual bool IsFieldClsCompliant {
5581 if (FieldBuilder == null)
5584 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5588 public override string[] ValidAttributeTargets
5591 return attribute_targets;
5595 protected override bool VerifyClsCompliance ()
5597 if (!base.VerifyClsCompliance ())
5600 if (!IsFieldClsCompliant) {
5601 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
5602 GetSignatureForError ());
5607 public void SetAssigned ()
5609 caching_flags |= Flags.IsAssigned;
5613 interface IFixedBuffer
5615 FieldInfo Element { get; }
5616 Type ElementType { get; }
5619 public class FixedFieldExternal: IFixedBuffer
5621 FieldInfo element_field;
5623 public FixedFieldExternal (FieldInfo fi)
5625 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5628 #region IFixedField Members
5630 public FieldInfo Element {
5632 return element_field;
5636 public Type ElementType {
5638 return element_field.FieldType;
5646 /// Fixed buffer implementation
5648 public class FixedField : FieldBase, IFixedBuffer
5650 public const string FixedElementName = "FixedElementField";
5651 static int GlobalCounter = 0;
5652 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5653 static FieldInfo[] fi;
5655 TypeBuilder fixed_buffer_type;
5656 FieldBuilder element;
5657 Expression size_expr;
5659 const int AllowedModifiers =
5662 Modifiers.PROTECTED |
5663 Modifiers.INTERNAL |
5666 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5667 Expression size_expr, Attributes attrs, Location loc):
5668 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5670 if (RootContext.Version < LanguageVersion.ISO_2)
5671 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5673 this.size_expr = size_expr;
5676 public override bool Define()
5678 if (!base.Define ())
5681 if (!TypeManager.IsPrimitiveType (MemberType)) {
5682 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",
5683 GetSignatureForError ());
5686 // Create nested fixed buffer container
5687 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5688 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
5689 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5691 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5692 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5694 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5695 Parent.MemberCache.AddMember (FieldBuilder, this);
5696 TypeManager.RegisterFieldBase (FieldBuilder, this);
5701 protected override void DoMemberTypeIndependentChecks ()
5703 base.DoMemberTypeIndependentChecks ();
5705 if (!Parent.IsUnsafe)
5706 Expression.UnsafeError (Report, Location);
5708 if (Parent.PartialContainer.Kind != Kind.Struct) {
5709 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5710 GetSignatureForError ());
5714 public override void Emit()
5716 ResolveContext rc = new ResolveContext (this);
5717 Constant c = size_expr.ResolveAsConstant (rc, this);
5721 IntConstant buffer_size_const = c.ImplicitConversionRequired (rc, TypeManager.int32_type, Location) as IntConstant;
5722 if (buffer_size_const == null)
5725 int buffer_size = buffer_size_const.Value;
5727 if (buffer_size <= 0) {
5728 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5732 int type_size = Expression.GetTypeSize (MemberType);
5734 if (buffer_size > int.MaxValue / type_size) {
5735 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5736 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5740 buffer_size *= type_size;
5741 EmitFieldSize (buffer_size);
5743 PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
5748 void EmitFieldSize (int buffer_size)
5750 CustomAttributeBuilder cab;
5751 PredefinedAttribute pa;
5753 pa = PredefinedAttributes.Get.StructLayout;
5754 if (pa.Constructor == null &&
5755 !pa.ResolveConstructor (Location, TypeManager.short_type))
5758 // TODO: It's not cleared
5760 fi = new FieldInfo[] { pa.Type.GetField ("Size") };
5762 object[] fi_val = new object[] { buffer_size };
5763 cab = new CustomAttributeBuilder (pa.Constructor,
5764 ctor_args, fi, fi_val);
5765 fixed_buffer_type.SetCustomAttribute (cab);
5768 // Don't emit FixedBufferAttribute attribute for private types
5770 if ((ModFlags & Modifiers.PRIVATE) != 0)
5773 pa = PredefinedAttributes.Get.FixedBuffer;
5774 if (pa.Constructor == null &&
5775 !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
5778 cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
5779 FieldBuilder.SetCustomAttribute (cab);
5782 protected override bool IsFieldClsCompliant {
5788 public void SetCharSet (TypeAttributes ta)
5790 TypeAttributes cta = fixed_buffer_type.Attributes;
5791 if ((cta & TypeAttributes.UnicodeClass) != (ta & TypeAttributes.UnicodeClass))
5792 SetTypeBuilderCharSet ((cta & ~TypeAttributes.AutoClass) | TypeAttributes.UnicodeClass);
5793 else if ((cta & TypeAttributes.AutoClass) != (ta & TypeAttributes.AutoClass))
5794 SetTypeBuilderCharSet ((cta & ~TypeAttributes.UnicodeClass) | TypeAttributes.AutoClass);
5795 else if (cta == 0 && ta != 0)
5796 SetTypeBuilderCharSet (cta & ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass));
5799 void SetTypeBuilderCharSet (TypeAttributes ta)
5801 MethodInfo mi = typeof (TypeBuilder).GetMethod ("SetCharSet", BindingFlags.Instance | BindingFlags.NonPublic);
5803 Report.RuntimeMissingSupport (Location, "TypeBuilder::SetCharSet");
5805 mi.Invoke (fixed_buffer_type, new object [] { ta });
5809 #region IFixedField Members
5811 public FieldInfo Element {
5817 public Type ElementType {
5827 // The Field class is used to represents class/struct fields during parsing.
5829 public class Field : FieldBase {
5831 // Modifiers allowed in a class declaration
5833 const int AllowedModifiers =
5836 Modifiers.PROTECTED |
5837 Modifiers.INTERNAL |
5840 Modifiers.VOLATILE |
5844 public Field (DeclSpace parent, FullNamedExpression type, int mod, MemberName name,
5846 : base (parent, type, mod, AllowedModifiers, name, attrs)
5850 bool CanBeVolatile ()
5852 if (TypeManager.IsReferenceType (MemberType))
5855 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5856 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5857 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5858 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5859 MemberType == TypeManager.float_type ||
5860 MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
5863 if (TypeManager.IsEnumType (MemberType))
5869 bool CheckStructLayout (Type type, bool isStatic)
5871 if (TypeManager.IsBuiltinType (type))
5875 if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
5879 if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
5880 if (!TypeManager.IsGenericType (type))
5883 foreach (Type t in TypeManager.GetTypeArguments (type)) {
5884 if (!CheckStructLayout (t, false))
5890 Report.Error (523, Location,
5891 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
5892 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5896 public override bool Define ()
5898 if (!base.Define ())
5902 Type[] required_modifier = null;
5903 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5904 if (TypeManager.isvolatile_type == null)
5905 TypeManager.isvolatile_type = TypeManager.CoreLookupType (Compiler,
5906 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5908 if (TypeManager.isvolatile_type != null)
5909 required_modifier = new Type [] { TypeManager.isvolatile_type };
5912 FieldBuilder = Parent.TypeBuilder.DefineField (
5913 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5915 // Don't cache inaccessible fields
5916 if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
5917 Parent.MemberCache.AddMember (FieldBuilder, this);
5920 TypeManager.RegisterFieldBase (FieldBuilder, this);
5922 catch (ArgumentException) {
5923 Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
5927 if (initializer != null) {
5928 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5929 new FieldInitializer (FieldBuilder, initializer, this));
5931 if (Parent.PartialContainer.Kind == Kind.Struct)
5932 CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
5938 protected override void DoMemberTypeDependentChecks ()
5940 base.DoMemberTypeDependentChecks ();
5942 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5943 if (!CanBeVolatile ()) {
5944 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5945 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5948 if ((ModFlags & Modifiers.READONLY) != 0) {
5949 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5950 GetSignatureForError ());
5955 protected override bool VerifyClsCompliance ()
5957 if (!base.VerifyClsCompliance ())
5960 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5961 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5969 // `set' and `get' accessors are represented with an Accessor.
5971 public class Accessor {
5973 // Null if the accessor is empty, or a Block if not
5975 public const int AllowedModifiers =
5977 Modifiers.PROTECTED |
5978 Modifiers.INTERNAL |
5981 public ToplevelBlock Block;
5982 public Attributes Attributes;
5983 public Location Location;
5984 public int ModFlags;
5985 public ParametersCompiled Parameters;
5987 public Accessor (ToplevelBlock b, int mod, Attributes attrs, ParametersCompiled p, Location loc)
5993 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc, RootContext.ToplevelTypes.Compiler.Report);
5997 // Ooouh Martin, templates are missing here.
5998 // When it will be possible move here a lot of child code and template method type.
5999 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
6000 protected MethodData method_data;
6001 protected ToplevelBlock block;
6002 protected ListDictionary declarative_security;
6004 // The accessor are created even if they are not wanted.
6005 // But we need them because their names are reserved.
6006 // Field says whether accessor will be emited or not
6007 public readonly bool IsDummy;
6009 protected readonly string prefix;
6011 ReturnParameter return_attributes;
6013 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
6014 : base (member.Parent, SetupName (prefix, member, member.Location), null)
6016 this.prefix = prefix;
6020 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
6022 : base (member.Parent, SetupName (prefix, member, accessor.Location),
6023 accessor.Attributes)
6025 this.prefix = prefix;
6026 this.block = accessor.Block;
6029 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
6031 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
6034 public void UpdateName (InterfaceMemberBase member)
6036 SetMemberName (SetupName (prefix, member, Location));
6039 #region IMethodData Members
6041 public ToplevelBlock Block {
6051 public CallingConventions CallingConventions {
6053 return CallingConventions.Standard;
6057 public EmitContext CreateEmitContext (ILGenerator ig)
6059 return new EmitContext (this, ig, ReturnType);
6062 public bool IsExcluded ()
6067 GenericMethod IMethodData.GenericMethod {
6073 public MemberName MethodName {
6079 public Type[] ParameterTypes {
6081 return ParameterInfo.Types;
6085 public abstract ParametersCompiled ParameterInfo { get ; }
6086 public abstract Type ReturnType { get; }
6090 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6092 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
6093 Report.Error (1667, a.Location,
6094 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6095 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6099 if (a.IsValidSecurityAttribute ()) {
6100 if (declarative_security == null)
6101 declarative_security = new ListDictionary ();
6102 a.ExtractSecurityPermissionSet (declarative_security);
6106 if (a.Target == AttributeTargets.Method) {
6107 method_data.MethodBuilder.SetCustomAttribute (cb);
6111 if (a.Target == AttributeTargets.ReturnValue) {
6112 if (return_attributes == null)
6113 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6115 return_attributes.ApplyAttributeBuilder (a, cb, pa);
6119 ApplyToExtraTarget (a, cb, pa);
6122 protected virtual void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6124 throw new NotSupportedException ("You forgot to define special attribute target handling");
6127 // It is not supported for the accessors
6128 public sealed override bool Define()
6130 throw new NotSupportedException ();
6133 public virtual void Emit (DeclSpace parent)
6135 method_data.Emit (parent);
6137 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
6138 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
6139 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6140 PredefinedAttributes.Get.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
6142 if (TypeManager.IsDynamicType (ReturnType)) {
6143 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6144 return_attributes.EmitPredefined (PredefinedAttributes.Get.Dynamic, Location);
6147 if (OptAttributes != null)
6148 OptAttributes.Emit ();
6150 if (declarative_security != null) {
6151 foreach (DictionaryEntry de in declarative_security) {
6152 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6159 public override bool EnableOverloadChecks (MemberCore overload)
6161 // This can only happen with indexers and it will
6162 // be catched as indexer difference
6163 if (overload is AbstractPropertyEventMethod)
6166 if (overload is MethodCore) {
6167 caching_flags |= Flags.MethodOverloadsExist;
6173 public override bool IsClsComplianceRequired()
6178 public bool IsDuplicateImplementation (MethodCore method)
6180 if (!MemberName.Equals (method.MemberName))
6183 Type[] param_types = method.ParameterTypes;
6185 if (param_types == null || param_types.Length != ParameterTypes.Length)
6188 for (int i = 0; i < param_types.Length; i++)
6189 if (param_types [i] != ParameterTypes [i])
6192 Report.SymbolRelatedToPreviousError (method);
6193 Report.Error (82, Location, "A member `{0}' is already reserved",
6194 method.GetSignatureForError ());
6198 public override bool IsUsed
6209 // Represents header string for documentation comment.
6211 public override string DocCommentHeader {
6212 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6215 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6220 // Properties and Indexers both generate PropertyBuilders, we use this to share
6221 // their common bits.
6223 abstract public class PropertyBase : PropertyBasedMember {
6225 public class GetMethod : PropertyMethod
6227 static string[] attribute_targets = new string [] { "method", "return" };
6229 public GetMethod (PropertyBase method):
6230 base (method, "get_")
6234 public GetMethod (PropertyBase method, Accessor accessor):
6235 base (method, accessor, "get_")
6239 public override MethodBuilder Define (DeclSpace parent)
6241 base.Define (parent);
6246 method_data = new MethodData (method, ModFlags, flags, this);
6248 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
6251 return method_data.MethodBuilder;
6254 public override Type ReturnType {
6256 return method.MemberType;
6260 public override ParametersCompiled ParameterInfo {
6262 return ParametersCompiled.EmptyReadOnlyParameters;
6266 public override string[] ValidAttributeTargets {
6268 return attribute_targets;
6273 public class SetMethod : PropertyMethod {
6275 static string[] attribute_targets = new string [] { "method", "param", "return" };
6276 ImplicitParameter param_attr;
6277 protected ParametersCompiled parameters;
6279 public SetMethod (PropertyBase method) :
6280 base (method, "set_")
6282 parameters = new ParametersCompiled (
6283 new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
6286 public SetMethod (PropertyBase method, Accessor accessor):
6287 base (method, accessor, "set_")
6289 this.parameters = accessor.Parameters;
6292 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6294 if (a.Target == AttributeTargets.Parameter) {
6295 if (param_attr == null)
6296 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6298 param_attr.ApplyAttributeBuilder (a, cb, pa);
6302 base.ApplyAttributeBuilder (a, cb, pa);
6305 public override ParametersCompiled ParameterInfo {
6311 public override MethodBuilder Define (DeclSpace parent)
6313 parameters.Resolve (this);
6315 base.Define (parent);
6320 method_data = new MethodData (method, ModFlags, flags, this);
6322 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
6325 return method_data.MethodBuilder;
6328 public override Type ReturnType {
6330 return TypeManager.void_type;
6334 public override string[] ValidAttributeTargets {
6336 return attribute_targets;
6341 static string[] attribute_targets = new string [] { "property" };
6343 public abstract class PropertyMethod : AbstractPropertyEventMethod
6345 protected readonly PropertyBase method;
6346 protected MethodAttributes flags;
6348 public PropertyMethod (PropertyBase method, string prefix)
6349 : base (method, prefix)
6351 this.method = method;
6352 this.ModFlags = method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE);
6355 public PropertyMethod (PropertyBase method, Accessor accessor,
6357 : base (method, accessor, prefix)
6359 this.method = method;
6360 this.ModFlags = accessor.ModFlags | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
6362 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6363 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6367 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6369 if (a.IsInternalMethodImplAttribute) {
6370 method.is_external_implementation = true;
6373 base.ApplyAttributeBuilder (a, cb, pa);
6376 public override AttributeTargets AttributeTargets {
6378 return AttributeTargets.Method;
6382 public override bool IsClsComplianceRequired ()
6384 return method.IsClsComplianceRequired ();
6387 public virtual MethodBuilder Define (DeclSpace parent)
6389 CheckForDuplications ();
6392 if (method.InterfaceType != null && parent.PartialContainer.PendingImplementations != null) {
6393 MethodInfo mi = parent.PartialContainer.PendingImplementations.IsInterfaceMethod (
6394 MethodName.Name, method.InterfaceType, new MethodData (method, ModFlags, flags, this));
6396 Report.SymbolRelatedToPreviousError (mi);
6397 Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
6398 method.GetSignatureForError (), TypeManager.CSharpSignature (mi, true));
6404 TypeContainer container = parent.PartialContainer;
6407 // Check for custom access modifier
6409 if ((ModFlags & Modifiers.Accessibility) == 0) {
6410 ModFlags |= method.ModFlags;
6411 flags = method.flags;
6413 if (container.Kind == Kind.Interface)
6414 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6415 GetSignatureForError ());
6417 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6418 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6421 CheckModifiers (ModFlags);
6422 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6423 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6424 flags = Modifiers.MethodAttr (ModFlags);
6425 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6428 CheckAbstractAndExtern (block != null);
6429 CheckProtectedModifier ();
6431 if (block != null && block.IsIterator)
6432 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
6437 public bool HasCustomAccessModifier {
6439 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6443 public PropertyBase Property {
6449 public override ObsoleteAttribute GetObsoleteAttribute ()
6451 return method.GetObsoleteAttribute ();
6454 public override string GetSignatureForError()
6456 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6459 void CheckModifiers (int modflags)
6461 modflags &= Modifiers.Accessibility;
6463 int mflags = method.ModFlags & Modifiers.Accessibility;
6465 if ((mflags & Modifiers.PUBLIC) != 0) {
6466 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6468 else if ((mflags & Modifiers.PROTECTED) != 0) {
6469 if ((mflags & Modifiers.INTERNAL) != 0)
6470 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6472 flags |= Modifiers.PRIVATE;
6474 else if ((mflags & Modifiers.INTERNAL) != 0)
6475 flags |= Modifiers.PRIVATE;
6477 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6478 Report.Error (273, Location,
6479 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6480 GetSignatureForError (), method.GetSignatureForError ());
6484 protected bool CheckForDuplications ()
6486 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6489 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo, Report);
6493 public PropertyMethod Get, Set;
6494 public PropertyBuilder PropertyBuilder;
6495 public MethodBuilder GetBuilder, SetBuilder;
6497 protected bool define_set_first = false;
6499 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6500 int allowed_mod, MemberName name,
6501 Attributes attrs, bool define_set_first)
6502 : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
6504 this.define_set_first = define_set_first;
6507 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
6509 if (a.HasSecurityAttribute) {
6510 a.Error_InvalidSecurityParent ();
6514 PropertyBuilder.SetCustomAttribute (cb);
6517 public override AttributeTargets AttributeTargets {
6519 return AttributeTargets.Property;
6523 protected override void DoMemberTypeDependentChecks ()
6525 base.DoMemberTypeDependentChecks ();
6529 if (MemberType.IsGenericParameter)
6533 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6534 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6538 protected override void DoMemberTypeIndependentChecks ()
6540 base.DoMemberTypeIndependentChecks ();
6543 // Accessors modifiers check
6545 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6546 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6547 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6548 GetSignatureForError ());
6551 if ((ModFlags & Modifiers.OVERRIDE) == 0 &&
6552 (Get.IsDummy && (Set.ModFlags & Modifiers.Accessibility) != 0) ||
6553 (Set.IsDummy && (Get.ModFlags & Modifiers.Accessibility) != 0)) {
6554 Report.Error (276, Location,
6555 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6556 GetSignatureForError ());
6562 GetBuilder = Get.Define (Parent);
6563 return (Get.IsDummy) ? true : GetBuilder != null;
6566 bool DefineSet (bool define)
6571 SetBuilder = Set.Define (Parent);
6572 return (Set.IsDummy) ? true : SetBuilder != null;
6575 protected bool DefineAccessors ()
6577 return DefineSet (define_set_first) &&
6579 DefineSet (!define_set_first);
6582 protected abstract PropertyInfo ResolveBaseProperty ();
6584 // TODO: rename to Resolve......
6585 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6587 PropertyInfo base_property = ResolveBaseProperty ();
6588 if (base_property == null)
6591 base_ret_type = base_property.PropertyType;
6592 MethodInfo get_accessor = base_property.GetGetMethod (true);
6593 MethodInfo set_accessor = base_property.GetSetMethod (true);
6594 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6597 // Check base property accessors conflict
6599 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6600 if (get_accessor == null) {
6601 if (Get != null && !Get.IsDummy) {
6602 Report.SymbolRelatedToPreviousError (base_property);
6603 Report.Error (545, Location,
6604 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6605 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6608 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6610 if (!Get.IsDummy && !CheckAccessModifiers (
6611 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6612 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6615 if (set_accessor == null) {
6616 if (Set != null && !Set.IsDummy) {
6617 Report.SymbolRelatedToPreviousError (base_property);
6618 Report.Error (546, Location,
6619 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6620 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6623 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6625 if (!Set.IsDummy && !CheckAccessModifiers (
6626 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6627 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6631 // When one accessor does not exist and property hides base one
6632 // we need to propagate this upwards
6633 if (set_accessor == null)
6634 set_accessor = get_accessor;
6637 // Get the less restrictive access
6639 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6642 public override void Emit ()
6645 // The PropertyBuilder can be null for explicit implementations, in that
6646 // case, we do not actually emit the ".property", so there is nowhere to
6647 // put the attribute
6649 if (PropertyBuilder != null) {
6650 if (OptAttributes != null)
6651 OptAttributes.Emit ();
6653 if (TypeManager.IsDynamicType (member_type))
6654 PredefinedAttributes.Get.Dynamic.EmitAttribute (PropertyBuilder);
6667 /// Tests whether accessors are not in collision with some method (CS0111)
6669 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6671 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6674 public override bool IsUsed
6680 return Get.IsUsed | Set.IsUsed;
6684 protected override void SetMemberName (MemberName new_name)
6686 base.SetMemberName (new_name);
6688 Get.UpdateName (this);
6689 Set.UpdateName (this);
6692 public override string[] ValidAttributeTargets {
6694 return attribute_targets;
6699 // Represents header string for documentation comment.
6701 public override string DocCommentHeader {
6702 get { return "P:"; }
6706 public class Property : PropertyBase
6708 public sealed class BackingField : Field
6710 readonly Property property;
6712 public BackingField (Property p)
6713 : base (p.Parent, p.type_name,
6714 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (p.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6715 new MemberName ("<" + p.GetFullName (p.MemberName) + ">k__BackingField", p.Location), null)
6720 public override string GetSignatureForError ()
6722 return property.GetSignatureForError ();
6726 const int AllowedModifiers =
6729 Modifiers.PROTECTED |
6730 Modifiers.INTERNAL |
6734 Modifiers.OVERRIDE |
6735 Modifiers.ABSTRACT |
6740 const int AllowedInterfaceModifiers =
6743 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6744 MemberName name, Attributes attrs, Accessor get_block,
6745 Accessor set_block, bool define_set_first)
6746 : this (parent, type, mod, name, attrs, get_block, set_block,
6747 define_set_first, null)
6751 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6752 MemberName name, Attributes attrs, Accessor get_block,
6753 Accessor set_block, bool define_set_first, Block current_block)
6754 : base (parent, type, mod,
6755 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
6756 name, attrs, define_set_first)
6758 if (get_block == null)
6759 Get = new GetMethod (this);
6761 Get = new GetMethod (this, get_block);
6763 if (set_block == null)
6764 Set = new SetMethod (this);
6766 Set = new SetMethod (this, set_block);
6768 if (!IsInterface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6769 get_block != null && get_block.Block == null &&
6770 set_block != null && set_block.Block == null) {
6771 if (RootContext.Version <= LanguageVersion.ISO_2)
6772 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6774 Get.ModFlags |= Modifiers.COMPILER_GENERATED;
6775 Set.ModFlags |= Modifiers.COMPILER_GENERATED;
6779 void CreateAutomaticProperty ()
6781 // Create backing field
6782 Field field = new BackingField (this);
6783 if (!field.Define ())
6786 Parent.PartialContainer.AddField (field);
6788 FieldExpr fe = new FieldExpr (field.FieldBuilder, Location);
6789 if ((field.ModFlags & Modifiers.STATIC) == 0)
6790 fe.InstanceExpression = new CompilerGeneratedThis (fe.Type, Location);
6793 Get.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
6794 Return r = new Return (fe, Location);
6795 Get.Block.AddStatement (r);
6798 Set.Block = new ToplevelBlock (Compiler, Set.ParameterInfo, Location);
6799 Assign a = new SimpleAssign (fe, new SimpleName ("value", Location));
6800 Set.Block.AddStatement (new StatementExpression (a));
6803 public override bool Define ()
6805 if (!base.Define ())
6808 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6810 if ((Get.ModFlags & Modifiers.COMPILER_GENERATED) != 0)
6811 CreateAutomaticProperty ();
6813 if (!DefineAccessors ())
6819 // FIXME - PropertyAttributes.HasDefault ?
6821 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6822 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6825 PropertyBuilder.SetGetMethod (GetBuilder);
6826 Parent.MemberCache.AddMember (GetBuilder, Get);
6830 PropertyBuilder.SetSetMethod (SetBuilder);
6831 Parent.MemberCache.AddMember (SetBuilder, Set);
6834 TypeManager.RegisterProperty (PropertyBuilder, this);
6835 Parent.MemberCache.AddMember (PropertyBuilder, this);
6839 public override void Emit ()
6841 if (((Set.ModFlags | Get.ModFlags) & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
6842 Report.Error (842, Location,
6843 "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
6844 GetSignatureForError ());
6850 protected override PropertyInfo ResolveBaseProperty ()
6852 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6853 Parent.TypeBuilder, Name, ParametersCompiled.EmptyReadOnlyParameters, null, true) as PropertyInfo;
6858 /// Gigantic workaround for lameness in SRE follows :
6859 /// This class derives from EventInfo and attempts to basically
6860 /// wrap around the EventBuilder so that FindMembers can quickly
6861 /// return this in it search for members
6863 public class MyEventBuilder : EventInfo {
6866 // We use this to "point" to our Builder which is
6867 // not really a MemberInfo
6869 EventBuilder MyBuilder;
6872 // We "catch" and wrap these methods
6874 MethodInfo raise, remove, add;
6876 EventAttributes attributes;
6877 Type declaring_type, reflected_type, event_type;
6882 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6884 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6886 // And now store the values in our own fields.
6888 declaring_type = type_builder;
6890 reflected_type = type_builder;
6892 attributes = event_attr;
6895 this.event_type = event_type;
6899 // Methods that you have to override. Note that you only need
6900 // to "implement" the variants that take the argument (those are
6901 // the "abstract" methods, the others (GetAddMethod()) are
6904 public override MethodInfo GetAddMethod (bool nonPublic)
6909 public override MethodInfo GetRemoveMethod (bool nonPublic)
6914 public override MethodInfo GetRaiseMethod (bool nonPublic)
6920 // These methods make "MyEventInfo" look like a Builder
6922 public void SetRaiseMethod (MethodBuilder raiseMethod)
6924 raise = raiseMethod;
6925 MyBuilder.SetRaiseMethod (raiseMethod);
6928 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6930 remove = removeMethod;
6931 MyBuilder.SetRemoveOnMethod (removeMethod);
6934 public void SetAddOnMethod (MethodBuilder addMethod)
6937 MyBuilder.SetAddOnMethod (addMethod);
6940 public void SetCustomAttribute (CustomAttributeBuilder cb)
6942 MyBuilder.SetCustomAttribute (cb);
6945 public override object [] GetCustomAttributes (bool inherit)
6947 // FIXME : There's nothing which can be seemingly done here because
6948 // we have no way of getting at the custom attribute objects of the
6953 public override object [] GetCustomAttributes (Type t, bool inherit)
6955 // FIXME : Same here !
6959 public override bool IsDefined (Type t, bool b)
6964 public override EventAttributes Attributes {
6970 public override string Name {
6976 public override Type DeclaringType {
6978 return declaring_type;
6982 public override Type ReflectedType {
6984 return reflected_type;
6988 public Type EventType {
6994 public void SetUsed ()
6996 if (my_event != null) {
6997 // my_event.SetAssigned ();
6998 my_event.SetMemberIsUsed ();
7004 /// For case when event is declared like property (with add and remove accessors).
7006 public class EventProperty: Event {
7007 abstract class AEventPropertyAccessor : AEventAccessor
7009 protected AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
7010 base (method, accessor, prefix)
7014 public override MethodBuilder Define (DeclSpace ds)
7016 CheckAbstractAndExtern (block != null);
7017 return base.Define (ds);
7020 public override string GetSignatureForError ()
7022 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
7026 sealed class AddDelegateMethod: AEventPropertyAccessor
7028 public AddDelegateMethod (Event method, Accessor accessor):
7029 base (method, accessor, AddPrefix)
7034 sealed class RemoveDelegateMethod: AEventPropertyAccessor
7036 public RemoveDelegateMethod (Event method, Accessor accessor):
7037 base (method, accessor, RemovePrefix)
7043 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
7045 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
7047 Attributes attrs, Accessor add, Accessor remove)
7048 : base (parent, type, mod_flags, name, attrs)
7050 Add = new AddDelegateMethod (this, add);
7051 Remove = new RemoveDelegateMethod (this, remove);
7054 public override bool Define()
7056 if (!base.Define ())
7063 public override string[] ValidAttributeTargets {
7065 return attribute_targets;
7071 /// Event is declared like field.
7073 public class EventField : Event {
7074 abstract class EventFieldAccessor : AEventAccessor
7076 protected EventFieldAccessor (Event method, string prefix)
7077 : base (method, prefix)
7081 public override void Emit (DeclSpace parent)
7083 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0) {
7084 if (parent is Class) {
7085 MethodBuilder mb = method_data.MethodBuilder;
7086 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7089 // TODO: because we cannot use generics yet
7090 FieldInfo field_info = ((EventField) method).BackingField.FieldBuilder;
7091 FieldExpr f_expr = new FieldExpr (field_info, Location);
7092 if ((method.ModFlags & Modifiers.STATIC) == 0)
7093 f_expr.InstanceExpression = new CompilerGeneratedThis (field_info.FieldType, Location);
7095 block = new ToplevelBlock (Compiler, ParameterInfo, Location);
7096 block.AddStatement (new StatementExpression (
7097 new CompoundAssign (Operation,
7099 block.GetParameterReference (ParameterInfo[0].Name, Location))));
7105 protected abstract Binary.Operator Operation { get; }
7108 sealed class AddDelegateMethod: EventFieldAccessor
7110 public AddDelegateMethod (Event method):
7111 base (method, AddPrefix)
7115 protected override Binary.Operator Operation {
7116 get { return Binary.Operator.Addition; }
7120 sealed class RemoveDelegateMethod: EventFieldAccessor
7122 public RemoveDelegateMethod (Event method):
7123 base (method, RemovePrefix)
7127 protected override Binary.Operator Operation {
7128 get { return Binary.Operator.Subtraction; }
7133 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7134 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7136 public Field BackingField;
7137 public Expression Initializer;
7139 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7140 : base (parent, type, mod_flags, name, attrs)
7142 Add = new AddDelegateMethod (this);
7143 Remove = new RemoveDelegateMethod (this);
7146 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7148 if (a.Target == AttributeTargets.Field) {
7149 BackingField.ApplyAttributeBuilder (a, cb, pa);
7153 if (a.Target == AttributeTargets.Method) {
7154 int errors = Report.Errors;
7155 Add.ApplyAttributeBuilder (a, cb, pa);
7156 if (errors == Report.Errors)
7157 Remove.ApplyAttributeBuilder (a, cb, pa);
7161 base.ApplyAttributeBuilder (a, cb, pa);
7164 public override bool Define()
7166 if (!base.Define ())
7169 if (Initializer != null && (ModFlags & Modifiers.ABSTRACT) != 0) {
7170 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7171 GetSignatureForError ());
7174 if (!HasBackingField) {
7179 // FIXME: We are unable to detect whether generic event is used because
7180 // we are using FieldExpr instead of EventExpr for event access in that
7181 // case. When this issue will be fixed this hack can be removed.
7182 if (TypeManager.IsGenericType (MemberType))
7185 if (Add.IsInterfaceImplementation)
7188 TypeManager.RegisterEventField (EventBuilder, this);
7190 BackingField = new Field (Parent,
7191 new TypeExpression (MemberType, Location),
7192 Modifiers.BACKING_FIELD | Modifiers.COMPILER_GENERATED | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
7195 Parent.PartialContainer.AddField (BackingField);
7196 BackingField.Initializer = Initializer;
7197 BackingField.ModFlags &= ~Modifiers.COMPILER_GENERATED;
7199 // Call define because we passed fields definition
7200 return BackingField.Define ();
7203 bool HasBackingField {
7205 return !IsInterface && (ModFlags & Modifiers.ABSTRACT) == 0;
7209 public override string[] ValidAttributeTargets
7212 return HasBackingField ? attribute_targets : attribute_targets_interface;
7217 public abstract class Event : PropertyBasedMember {
7218 public abstract class AEventAccessor : AbstractPropertyEventMethod
7220 protected readonly Event method;
7221 ImplicitParameter param_attr;
7223 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7225 public const string AddPrefix = "add_";
7226 public const string RemovePrefix = "remove_";
7228 protected AEventAccessor (Event method, string prefix)
7229 : base (method, prefix)
7231 this.method = method;
7232 this.ModFlags = method.ModFlags;
7235 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7236 : base (method, accessor, prefix)
7238 this.method = method;
7239 this.ModFlags = method.ModFlags;
7242 public bool IsInterfaceImplementation {
7243 get { return method_data.implementing != null; }
7246 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7248 if (a.IsInternalMethodImplAttribute) {
7249 method.is_external_implementation = true;
7252 base.ApplyAttributeBuilder (a, cb, pa);
7255 protected override void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7257 if (a.Target == AttributeTargets.Parameter) {
7258 if (param_attr == null)
7259 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7261 param_attr.ApplyAttributeBuilder (a, cb, pa);
7265 base.ApplyAttributeBuilder (a, cb, pa);
7268 public override AttributeTargets AttributeTargets {
7270 return AttributeTargets.Method;
7274 public override bool IsClsComplianceRequired ()
7276 return method.IsClsComplianceRequired ();
7279 public virtual MethodBuilder Define (DeclSpace parent)
7281 method_data = new MethodData (method, method.ModFlags,
7282 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7284 if (!method_data.Define (parent, method.GetFullName (MemberName), Report))
7287 MethodBuilder mb = method_data.MethodBuilder;
7288 ParameterInfo.ApplyAttributes (mb);
7292 public override Type ReturnType {
7294 return TypeManager.void_type;
7298 public override ObsoleteAttribute GetObsoleteAttribute ()
7300 return method.GetObsoleteAttribute ();
7303 public override string[] ValidAttributeTargets {
7305 return attribute_targets;
7309 public override ParametersCompiled ParameterInfo {
7311 return method.parameters;
7317 const int AllowedModifiers =
7320 Modifiers.PROTECTED |
7321 Modifiers.INTERNAL |
7326 Modifiers.OVERRIDE |
7328 Modifiers.ABSTRACT |
7331 const int AllowedInterfaceModifiers =
7334 public AEventAccessor Add, Remove;
7335 public MyEventBuilder EventBuilder;
7336 public MethodBuilder AddBuilder, RemoveBuilder;
7338 ParametersCompiled parameters;
7340 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7341 : base (parent, null, type, mod_flags,
7342 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7347 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7349 if ((a.HasSecurityAttribute)) {
7350 a.Error_InvalidSecurityParent ();
7354 EventBuilder.SetCustomAttribute (cb);
7357 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7359 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7362 public override AttributeTargets AttributeTargets {
7364 return AttributeTargets.Event;
7368 public override bool Define ()
7370 if (!base.Define ())
7373 if (!TypeManager.IsDelegateType (MemberType)) {
7374 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7377 parameters = ParametersCompiled.CreateFullyResolved (
7378 new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
7383 if (TypeManager.delegate_combine_delegate_delegate == null) {
7384 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7385 TypeManager.delegate_type, "Combine", Location,
7386 TypeManager.delegate_type, TypeManager.delegate_type);
7388 if (TypeManager.delegate_remove_delegate_delegate == null) {
7389 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7390 TypeManager.delegate_type, "Remove", Location,
7391 TypeManager.delegate_type, TypeManager.delegate_type);
7395 // Now define the accessors
7398 AddBuilder = Add.Define (Parent);
7399 if (AddBuilder == null)
7402 RemoveBuilder = Remove.Define (Parent);
7403 if (RemoveBuilder == null)
7406 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7407 EventBuilder.SetAddOnMethod (AddBuilder);
7408 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7410 Parent.MemberCache.AddMember (EventBuilder, this);
7411 Parent.MemberCache.AddMember (AddBuilder, Add);
7412 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7417 public override void Emit ()
7419 if (OptAttributes != null) {
7420 OptAttributes.Emit ();
7424 Remove.Emit (Parent);
7429 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7431 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7432 Parent.TypeBuilder, Name);
7437 AParametersCollection pd = TypeManager.GetParameterData (mi);
7438 base_ret_type = pd.Types [0];
7443 // Represents header string for documentation comment.
7445 public override string DocCommentHeader {
7446 get { return "E:"; }
7451 public class Indexer : PropertyBase
7453 public class GetIndexerMethod : GetMethod
7455 ParametersCompiled parameters;
7457 public GetIndexerMethod (Indexer method):
7460 this.parameters = method.parameters;
7463 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7464 base (method, accessor)
7466 parameters = accessor.Parameters;
7469 public override MethodBuilder Define (DeclSpace parent)
7471 parameters.Resolve (this);
7472 return base.Define (parent);
7475 public override bool EnableOverloadChecks (MemberCore overload)
7477 if (base.EnableOverloadChecks (overload)) {
7478 overload.caching_flags |= Flags.MethodOverloadsExist;
7485 public override ParametersCompiled ParameterInfo {
7492 public class SetIndexerMethod: SetMethod
7494 public SetIndexerMethod (Indexer method):
7497 parameters = ParametersCompiled.MergeGenerated (method.parameters, false, parameters [0], null);
7500 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7501 base (method, accessor)
7503 parameters = method.Get.IsDummy ? accessor.Parameters : accessor.Parameters.Clone ();
7506 public override bool EnableOverloadChecks (MemberCore overload)
7508 if (base.EnableOverloadChecks (overload)) {
7509 overload.caching_flags |= Flags.MethodOverloadsExist;
7517 const int AllowedModifiers =
7520 Modifiers.PROTECTED |
7521 Modifiers.INTERNAL |
7525 Modifiers.OVERRIDE |
7530 const int AllowedInterfaceModifiers =
7533 public readonly ParametersCompiled parameters;
7535 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7536 ParametersCompiled parameters, Attributes attrs,
7537 Accessor get_block, Accessor set_block, bool define_set_first)
7538 : base (parent, type, mod,
7539 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7540 name, attrs, define_set_first)
7542 this.parameters = parameters;
7544 if (get_block == null)
7545 Get = new GetIndexerMethod (this);
7547 Get = new GetIndexerMethod (this, get_block);
7549 if (set_block == null)
7550 Set = new SetIndexerMethod (this);
7552 Set = new SetIndexerMethod (this, set_block);
7555 protected override bool CheckForDuplications ()
7557 return Parent.MemberCache.CheckExistingMembersOverloads (this, GetFullName (MemberName), parameters, Report);
7560 public override bool Define ()
7562 if (!base.Define ())
7565 if (!DefineParameters (parameters))
7568 if (OptAttributes != null) {
7569 Attribute indexer_attr = OptAttributes.Search (PredefinedAttributes.Get.IndexerName);
7570 if (indexer_attr != null) {
7571 // Remove the attribute from the list because it is not emitted
7572 OptAttributes.Attrs.Remove (indexer_attr);
7574 string name = indexer_attr.GetIndexerAttributeValue ();
7580 if (IsExplicitImpl) {
7581 Report.Error (415, indexer_attr.Location,
7582 "The `IndexerName' attribute is valid only on an " +
7583 "indexer that is not an explicit interface member declaration");
7587 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7588 Report.Error (609, indexer_attr.Location,
7589 "Cannot set the `IndexerName' attribute on an indexer marked override");
7595 if (InterfaceType != null) {
7596 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7597 if (base_IndexerName != Name)
7598 ShortName = base_IndexerName;
7601 if (!Parent.PartialContainer.AddMember (this) ||
7602 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7605 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7607 if (!DefineAccessors ())
7614 // Now name the parameters
7616 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7617 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.GetEmitTypes ());
7620 PropertyBuilder.SetGetMethod (GetBuilder);
7621 Parent.MemberCache.AddMember (GetBuilder, Get);
7625 PropertyBuilder.SetSetMethod (SetBuilder);
7626 Parent.MemberCache.AddMember (SetBuilder, Set);
7629 TypeManager.RegisterIndexer (PropertyBuilder, parameters);
7630 Parent.MemberCache.AddMember (PropertyBuilder, this);
7634 public override bool EnableOverloadChecks (MemberCore overload)
7636 if (overload is Indexer) {
7637 caching_flags |= Flags.MethodOverloadsExist;
7641 return base.EnableOverloadChecks (overload);
7644 public override string GetDocCommentName (DeclSpace ds)
7646 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7649 public override string GetSignatureForError ()
7651 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7652 if (MemberName.Left != null) {
7654 sb.Append (MemberName.Left.GetSignatureForError ());
7657 sb.Append (".this");
7658 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7659 return sb.ToString ();
7662 protected override PropertyInfo ResolveBaseProperty ()
7664 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7665 Parent.TypeBuilder, Name, parameters, null, true) as PropertyInfo;
7668 protected override bool VerifyClsCompliance ()
7670 if (!base.VerifyClsCompliance ())
7673 parameters.VerifyClsCompliance ();
7678 public class Operator : MethodOrOperator {
7680 const int AllowedModifiers =
7686 public enum OpType : byte {
7696 // Unary and Binary operators
7719 // Implicit and Explicit
7723 // Just because of enum
7727 public readonly OpType OperatorType;
7729 static readonly string [] [] names;
7733 names = new string[(int)OpType.TOP][];
7734 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7735 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7736 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7737 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7738 names [(int) OpType.True] = new string [] { "true", "op_True" };
7739 names [(int) OpType.False] = new string [] { "false", "op_False" };
7740 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7741 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7742 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7743 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7744 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7745 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7746 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7747 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7748 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7749 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7750 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7751 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7752 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7753 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7754 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7755 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7756 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7757 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7758 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7759 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7762 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7763 int mod_flags, ParametersCompiled parameters,
7764 ToplevelBlock block, Attributes attrs, Location loc)
7765 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
7766 new MemberName (GetMetadataName (type), loc), attrs, parameters)
7768 OperatorType = type;
7772 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
7774 if (a.Type == pa.Conditional) {
7775 Error_ConditionalAttributeIsNotValid ();
7779 base.ApplyAttributeBuilder (a, cb, pa);
7782 public override bool Define ()
7784 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7785 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7786 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7789 if (!base.Define ())
7792 // imlicit and explicit operator of same types are not allowed
7793 if (OperatorType == OpType.Explicit)
7794 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters, Report);
7795 else if (OperatorType == OpType.Implicit)
7796 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters, Report);
7798 Type declaring_type = MethodData.DeclaringType;
7799 Type return_type = MemberType;
7800 Type first_arg_type = ParameterTypes [0];
7802 Type first_arg_type_unwrap = first_arg_type;
7803 if (TypeManager.IsNullableType (first_arg_type))
7804 first_arg_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (first_arg_type) [0]);
7806 Type return_type_unwrap = return_type;
7807 if (TypeManager.IsNullableType (return_type))
7808 return_type_unwrap = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (return_type) [0]);
7810 if (TypeManager.IsDynamicType (return_type) || TypeManager.IsDynamicType (first_arg_type)) {
7811 Report.Error (1964, Location,
7812 "User-defined operator `{0}' cannot convert to or from the dynamic type",
7813 GetSignatureForError ());
7819 // Rules for conversion operators
7821 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7822 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7823 Report.Error (555, Location,
7824 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7829 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7830 conv_type = first_arg_type;
7831 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7832 conv_type = return_type;
7834 Report.Error (556, Location,
7835 "User-defined conversion must convert to or from the enclosing type");
7840 // Because IsInterface and IsClass are not supported
7842 if (!TypeManager.IsGenericParameter (conv_type)) {
7843 if (conv_type.IsInterface) {
7844 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7845 GetSignatureForError ());
7849 if (conv_type.IsClass) {
7850 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7851 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7852 GetSignatureForError ());
7856 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7857 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7858 GetSignatureForError ());
7863 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7864 if (first_arg_type != declaring_type || Parameters.Types [1] != TypeManager.int32_type) {
7865 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");
7868 } else if (Parameters.Count == 1) {
7869 // Checks for Unary operators
7871 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7872 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7873 Report.Error (448, Location,
7874 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7877 if (first_arg_type != declaring_type) {
7879 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7884 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7885 Report.Error (562, Location,
7886 "The parameter type of a unary operator must be the containing type");
7890 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7891 if (return_type != TypeManager.bool_type){
7894 "The return type of operator True or False " +
7901 // Checks for Binary operators
7903 if (first_arg_type != declaring_type &&
7904 Parameters.Types [1] != declaring_type){
7907 "One of the parameters of a binary operator must " +
7908 "be the containing type");
7916 protected override bool ResolveMemberType ()
7918 if (!base.ResolveMemberType ())
7921 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7925 // Operator cannot be override
7926 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7931 public static string GetName (OpType ot)
7933 return names [(int) ot] [0];
7936 public static string GetName (string metadata_name)
7938 for (int i = 0; i < names.Length; ++i) {
7939 if (names [i] [1] == metadata_name)
7940 return names [i] [0];
7945 public static string GetMetadataName (OpType ot)
7947 return names [(int) ot] [1];
7950 public static string GetMetadataName (string name)
7952 for (int i = 0; i < names.Length; ++i) {
7953 if (names [i] [0] == name)
7954 return names [i] [1];
7959 public OpType GetMatchingOperator ()
7961 switch (OperatorType) {
7962 case OpType.Equality:
7963 return OpType.Inequality;
7964 case OpType.Inequality:
7965 return OpType.Equality;
7967 return OpType.False;
7970 case OpType.GreaterThan:
7971 return OpType.LessThan;
7972 case OpType.LessThan:
7973 return OpType.GreaterThan;
7974 case OpType.GreaterThanOrEqual:
7975 return OpType.LessThanOrEqual;
7976 case OpType.LessThanOrEqual:
7977 return OpType.GreaterThanOrEqual;
7983 public override string GetSignatureForError ()
7985 StringBuilder sb = new StringBuilder ();
7986 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7987 sb.AppendFormat ("{0}.{1} operator {2}",
7988 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
7991 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7994 sb.Append (Parameters.GetSignatureForError ());
7995 return sb.ToString ();
8000 // This is used to compare method signatures
8002 struct MethodSignature {
8004 public Type RetType;
8005 public Type [] Parameters;
8008 /// This delegate is used to extract methods which have the
8009 /// same signature as the argument
8011 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
8013 public MethodSignature (string name, Type ret_type, Type [] parameters)
8018 if (parameters == null)
8019 Parameters = Type.EmptyTypes;
8021 Parameters = parameters;
8024 public override string ToString ()
8027 if (Parameters.Length != 0){
8028 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
8029 for (int i = 0; i < Parameters.Length; i++){
8030 sb.Append (Parameters [i]);
8031 if (i+1 < Parameters.Length)
8034 pars = sb.ToString ();
8037 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
8040 public override int GetHashCode ()
8042 return Name.GetHashCode ();
8045 public override bool Equals (Object o)
8047 MethodSignature other = (MethodSignature) o;
8049 if (other.Name != Name)
8052 if (other.RetType != RetType)
8055 if (Parameters == null){
8056 if (other.Parameters == null)
8061 if (other.Parameters == null)
8064 int c = Parameters.Length;
8065 if (other.Parameters.Length != c)
8068 for (int i = 0; i < c; i++)
8069 if (other.Parameters [i] != Parameters [i])
8075 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8077 MethodSignature sig = (MethodSignature) filter_criteria;
8079 if (m.Name != sig.Name)
8083 MethodInfo mi = m as MethodInfo;
8084 PropertyInfo pi = m as PropertyInfo;
8087 ReturnType = mi.ReturnType;
8088 else if (pi != null)
8089 ReturnType = pi.PropertyType;
8094 // we use sig.RetType == null to mean `do not check the
8095 // method return value.
8097 if (sig.RetType != null) {
8098 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8104 args = TypeManager.GetParameterData (mi).Types;
8106 args = TypeManager.GetParameterData (pi).Types;
8107 Type [] sigp = sig.Parameters;
8109 if (args.Length != sigp.Length)
8112 for (int i = args.Length - 1; i >= 0; i--)
8113 if (!TypeManager.IsEqual (args [i], sigp [i]))