2 // class.cs: Class and Struct handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
14 // 2002-10-11 Miguel de Icaza <miguel@ximian.com>
16 // * class.cs: Following the comment from 2002-09-26 to AddMethod, I
17 // have fixed a remaining problem: not every AddXXXX was adding a
18 // fully qualified name.
20 // Now everyone registers a fully qualified name in the DeclSpace as
21 // being defined instead of the partial name.
23 // Downsides: we are slower than we need to be due to the excess
24 // copies and the names being registered this way.
26 // The reason for this is that we currently depend (on the corlib
27 // bootstrap for instance) that types are fully qualified, because
28 // we dump all the types in the namespace, and we should really have
29 // types inserted into the proper namespace, so we can only store the
30 // basenames in the defined_names array.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Security;
41 using System.Security.Permissions;
44 #if BOOTSTRAP_WITH_OLDLIB || NET_2_1
45 using XmlElement = System.Object;
50 using Mono.CompilerServices.SymbolWriter;
52 namespace Mono.CSharp {
64 /// This is the base class for structs and classes.
66 public abstract class TypeContainer : DeclSpace, IMemberContainer {
68 public class MemberCoreArrayList: ArrayList
71 /// Defines the MemberCore objects that are in this array
73 public virtual void DefineContainerMembers ()
75 foreach (MemberCore mc in this) {
78 } catch (Exception e) {
79 throw new InternalErrorException (mc, e);
84 public virtual void Emit ()
86 foreach (MemberCore mc in this)
91 public class OperatorArrayList: MemberCoreArrayList
93 TypeContainer container;
95 public OperatorArrayList (TypeContainer container)
97 this.container = container;
101 // Checks that some operators come in pairs:
107 // They are matched based on the return type and the argument types
109 void CheckPairedOperators ()
111 bool has_equality_or_inequality = false;
112 Operator[] operators = (Operator[]) ToArray (typeof (Operator));
113 bool [] has_pair = new bool [operators.Length];
115 for (int i = 0; i < Count; ++i) {
116 if (operators [i] == null)
119 Operator o_a = operators [i];
120 Operator.OpType o_type = o_a.OperatorType;
121 if (o_type == Operator.OpType.Equality || o_type == Operator.OpType.Inequality)
122 has_equality_or_inequality = true;
124 Operator.OpType matching_type = o_a.GetMatchingOperator ();
125 if (matching_type == Operator.OpType.TOP) {
126 operators [i] = null;
130 for (int ii = 0; ii < Count; ++ii) {
131 Operator o_b = operators [ii];
132 if (o_b == null || o_b.OperatorType != matching_type)
135 if (!TypeManager.IsEqual (o_a.ReturnType, o_b.ReturnType))
138 if (!TypeManager.IsEqual (o_a.ParameterTypes, o_b.ParameterTypes))
141 operators [i] = null;
144 // Used to ignore duplicate user conversions
146 has_pair [ii] = true;
150 for (int i = 0; i < Count; ++i) {
151 if (operators [i] == null || has_pair [i])
154 Operator o = operators [i];
155 Report.Error (216, o.Location,
156 "The operator `{0}' requires a matching operator `{1}' to also be defined",
157 o.GetSignatureForError (), Operator.GetName (o.GetMatchingOperator ()));
160 if (has_equality_or_inequality && Report.WarningLevel > 2) {
161 if (container.Methods == null || !container.HasEquals)
162 Report.Warning (660, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
164 if (container.Methods == null || !container.HasGetHashCode)
165 Report.Warning (661, 2, container.Location, "`{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
169 public override void DefineContainerMembers ()
171 base.DefineContainerMembers ();
172 CheckPairedOperators ();
180 GetHashCode = 1 << 1,
181 HasStaticFieldInitializer = 1 << 2
185 // Whether this is a struct, class or interface
186 public readonly Kind Kind;
188 // Holds a list of classes and structures
189 protected ArrayList types;
191 MemberCoreArrayList ordered_explicit_member_list;
192 MemberCoreArrayList ordered_member_list;
194 // Holds the list of properties
195 MemberCoreArrayList properties;
197 // Holds the list of delegates
198 MemberCoreArrayList delegates;
200 // Holds the list of constructors
201 protected MemberCoreArrayList instance_constructors;
203 // Holds the list of fields
204 MemberCoreArrayList fields;
206 // Holds a list of fields that have initializers
207 protected ArrayList initialized_fields;
209 // Holds a list of static fields that have initializers
210 protected ArrayList initialized_static_fields;
212 // Holds the list of constants
213 protected MemberCoreArrayList constants;
215 // Holds the methods.
216 MemberCoreArrayList methods;
219 protected MemberCoreArrayList events;
221 // Holds the indexers
224 // Holds the operators
225 MemberCoreArrayList operators;
227 // Holds the compiler generated classes
228 ArrayList compiler_generated;
231 // Pointers to the default constructor and the default static constructor
233 protected Constructor default_constructor;
234 protected Constructor default_static_constructor;
237 // Points to the first non-static field added to the container.
239 // This is an arbitrary choice. We are interested in looking at _some_ non-static field,
240 // and the first one's as good as any.
242 FieldBase first_nonstatic_field = null;
245 // This one is computed after we can distinguish interfaces
246 // from classes from the arraylist `type_bases'
249 TypeExpr[] iface_exprs;
252 protected ArrayList type_bases;
254 bool members_resolved;
255 bool members_resolved_ok;
256 protected bool members_defined;
257 bool members_defined_ok;
259 // The interfaces we implement.
260 protected Type[] ifaces;
262 // The base member cache and our member cache
263 MemberCache base_cache;
264 protected MemberCache member_cache;
266 public const string DefaultIndexerName = "Item";
268 private bool seen_normal_indexers = false;
269 private string indexer_name = DefaultIndexerName;
271 private CachedMethods cached_method;
273 ArrayList partial_parts;
276 /// The pending methods that need to be implemented
277 // (interfaces or abstract methods)
279 PendingImplementation pending;
281 public TypeContainer (NamespaceEntry ns, DeclSpace parent, MemberName name,
282 Attributes attrs, Kind kind)
283 : base (ns, parent, name, attrs)
285 if (parent != null && parent.NamespaceEntry != ns)
286 throw new InternalErrorException ("A nested type should be in the same NamespaceEntry as its enclosing class");
289 this.PartialContainer = this;
292 public bool AddMember (MemberCore symbol)
294 return AddToContainer (symbol, symbol.MemberName.MethodName);
297 protected virtual bool AddMemberType (DeclSpace ds)
299 return AddToContainer (ds, ds.Basename);
302 public void AddConstant (Const constant)
304 if (!AddMember (constant))
307 if (constants == null)
308 constants = new MemberCoreArrayList ();
310 constants.Add (constant);
313 public TypeContainer AddTypeContainer (TypeContainer tc)
315 if (!AddMemberType (tc))
319 types = new MemberCoreArrayList ();
325 public virtual TypeContainer AddPartial (TypeContainer next_part)
327 return AddPartial (next_part, next_part.Basename);
330 protected TypeContainer AddPartial (TypeContainer next_part, string name)
332 next_part.ModFlags |= Modifiers.PARTIAL;
333 TypeContainer tc = defined_names [name] as TypeContainer;
336 return AddTypeContainer (next_part);
338 if ((tc.ModFlags & Modifiers.PARTIAL) == 0) {
339 Report.SymbolRelatedToPreviousError (tc);
340 Error_MissingPartialModifier (next_part);
343 if (tc.Kind != next_part.Kind) {
344 Report.SymbolRelatedToPreviousError (tc);
345 Report.Error (261, next_part.Location,
346 "Partial declarations of `{0}' must be all classes, all structs or all interfaces",
347 next_part.GetSignatureForError ());
350 if ((tc.ModFlags & Modifiers.Accessibility) != (next_part.ModFlags & Modifiers.Accessibility) &&
351 ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0 &&
352 (next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) == 0)) {
353 Report.SymbolRelatedToPreviousError (tc);
354 Report.Error (262, next_part.Location,
355 "Partial declarations of `{0}' have conflicting accessibility modifiers",
356 next_part.GetSignatureForError ());
359 if (tc.partial_parts == null)
360 tc.partial_parts = new ArrayList (1);
362 if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
363 tc.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
364 } else if ((tc.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFER) != 0) {
365 tc.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFER | Modifiers.Accessibility);
366 tc.ModFlags |= next_part.ModFlags;
368 tc.ModFlags |= next_part.ModFlags;
371 if (next_part.attributes != null) {
372 if (tc.attributes == null)
373 tc.attributes = next_part.attributes;
375 tc.attributes.AddAttributes (next_part.attributes.Attrs);
378 next_part.PartialContainer = tc;
379 tc.partial_parts.Add (next_part);
383 public void AddDelegate (Delegate d)
385 if (!AddMemberType (d))
388 if (delegates == null)
389 delegates = new MemberCoreArrayList ();
394 private void AddMemberToList (MemberCore mc, ArrayList alist, bool isexplicit)
396 if (ordered_explicit_member_list == null) {
397 ordered_explicit_member_list = new MemberCoreArrayList ();
398 ordered_member_list = new MemberCoreArrayList ();
402 ordered_explicit_member_list.Add (mc);
403 alist.Insert (0, mc);
406 ordered_member_list.Add (mc);
412 public void AddMethod (Method method)
414 MemberName mn = method.MemberName;
415 if (!AddToContainer (method, mn.IsGeneric ? mn.Basename : mn.MethodName))
419 methods = new MemberCoreArrayList ();
421 if (method.MemberName.Left != null)
422 AddMemberToList (method, methods, true);
424 AddMemberToList (method, methods, false);
428 // Do not use this method: use AddMethod.
430 // This is only used by iterators.
432 public void AppendMethod (Method method)
434 if (!AddMember (method))
438 methods = new MemberCoreArrayList ();
440 AddMemberToList (method, methods, false);
443 public void AddConstructor (Constructor c)
445 if (c.Name != MemberName.Name) {
446 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
449 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
450 if (!AddToContainer (c, is_static ?
451 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
454 if (is_static && c.Parameters.Empty){
455 if (default_static_constructor != null) {
456 Report.SymbolRelatedToPreviousError (default_static_constructor);
457 Report.Error (111, c.Location,
458 "A member `{0}' is already defined. Rename this member or use different parameter types",
459 c.GetSignatureForError ());
463 default_static_constructor = c;
465 if (c.Parameters.Empty)
466 default_constructor = c;
468 if (instance_constructors == null)
469 instance_constructors = new MemberCoreArrayList ();
471 instance_constructors.Add (c);
475 public bool AddField (FieldBase field)
477 if (!AddMember (field))
481 fields = new MemberCoreArrayList ();
485 if ((field.ModFlags & Modifiers.STATIC) != 0)
488 if (first_nonstatic_field == null) {
489 first_nonstatic_field = field;
493 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
494 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
495 Report.Warning (282, 3, field.Location,
496 "struct instance field `{0}' found in different declaration from instance field `{1}'",
497 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
502 public void AddProperty (Property prop)
504 if (!AddMember (prop) ||
505 !AddMember (prop.Get) || !AddMember (prop.Set))
508 if (properties == null)
509 properties = new MemberCoreArrayList ();
511 if (prop.MemberName.Left != null)
512 AddMemberToList (prop, properties, true);
514 AddMemberToList (prop, properties, false);
517 public void AddEvent (Event e)
522 if (e is EventProperty) {
523 if (!AddMember (e.Add))
526 if (!AddMember (e.Remove))
531 events = new MemberCoreArrayList ();
537 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
539 public void AddIndexer (Indexer i)
541 if (indexers == null)
542 indexers = new ArrayList ();
544 if (i.IsExplicitImpl)
545 AddMemberToList (i, indexers, true);
547 AddMemberToList (i, indexers, false);
550 public void AddOperator (Operator op)
555 if (operators == null)
556 operators = new OperatorArrayList (this);
561 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
563 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
565 if (compiler_generated == null)
566 compiler_generated = new ArrayList ();
568 compiler_generated.Add (c);
571 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
573 if (a.Type == TypeManager.default_member_type) {
574 if (Indexers != null) {
575 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
580 base.ApplyAttributeBuilder (a, cb);
583 public override AttributeTargets AttributeTargets {
585 throw new NotSupportedException ();
589 public ArrayList Types {
595 public MemberCoreArrayList Methods {
601 public ArrayList Constants {
607 protected Type BaseType {
609 return TypeBuilder.BaseType;
613 public ArrayList Fields {
619 public ArrayList InstanceConstructors {
621 return instance_constructors;
625 public ArrayList Properties {
631 public ArrayList Events {
637 public ArrayList Indexers {
643 public ArrayList Operators {
649 public ArrayList Delegates {
655 protected override TypeAttributes TypeAttr {
657 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
661 public string IndexerName {
663 return indexers == null ? DefaultIndexerName : indexer_name;
667 public bool IsComImport {
669 if (OptAttributes == null || TypeManager.comimport_attr_type == null)
672 return OptAttributes.Contains (TypeManager.comimport_attr_type);
676 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
678 if ((field.ModFlags & Modifiers.STATIC) != 0){
679 if (initialized_static_fields == null) {
680 PartialContainer.HasStaticFieldInitializer = true;
681 initialized_static_fields = new ArrayList (4);
684 initialized_static_fields.Add (expression);
686 if (initialized_fields == null)
687 initialized_fields = new ArrayList (4);
689 initialized_fields.Add (expression);
693 public void ResolveFieldInitializers (EmitContext ec)
695 // Field initializers are tricky for partial classes. They have to
696 // share same costructor (block) but they have they own resolve scope.
697 DeclSpace orig = ec.DeclContainer;
699 if (partial_parts != null) {
700 foreach (TypeContainer part in partial_parts) {
701 ec.DeclContainer = part;
702 part.DoResolveFieldInitializers (ec);
705 ec.DeclContainer = PartialContainer;
706 DoResolveFieldInitializers (ec);
707 ec.DeclContainer = orig;
710 void DoResolveFieldInitializers (EmitContext ec)
713 if (initialized_static_fields == null)
716 bool has_complex_initializer = false;
717 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
718 foreach (FieldInitializer fi in initialized_static_fields) {
719 fi.ResolveStatement (ec);
720 if (!fi.IsComplexInitializer)
723 has_complex_initializer = true;
726 // Need special check to not optimize code like this
727 // static int a = b = 5;
729 if (!has_complex_initializer && RootContext.Optimize) {
730 for (int i = 0; i < initialized_static_fields.Count; ++i) {
731 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
732 if (fi.IsDefaultInitializer) {
733 initialized_static_fields.RemoveAt (i);
743 if (initialized_fields == null)
746 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
747 for (int i = 0; i < initialized_fields.Count; ++i) {
748 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
749 fi.ResolveStatement (ec);
750 if (fi.IsDefaultInitializer && RootContext.Optimize) {
751 // Field is re-initialized to its default value => removed
752 initialized_fields.RemoveAt (i);
760 // Emits the instance field initializers
762 public bool EmitFieldInitializers (EmitContext ec)
764 if (partial_parts != null) {
765 foreach (TypeContainer part in partial_parts)
766 part.EmitFieldInitializers (ec);
772 fields = initialized_static_fields;
774 fields = initialized_fields;
780 foreach (FieldInitializer f in fields) {
781 f.EmitStatement (ec);
786 public override string DocComment {
798 public PendingImplementation PendingImplementations {
799 get { return pending; }
802 public override bool GetClsCompliantAttributeValue ()
804 if (PartialContainer != this)
805 return PartialContainer.GetClsCompliantAttributeValue ();
807 return base.GetClsCompliantAttributeValue ();
810 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
812 // FIXME: get rid of partial_parts and store lists of bases of each part here
813 // assumed, not verified: 'part' is in 'partial_parts'
814 ((TypeContainer) part).type_bases = bases;
817 TypeExpr[] GetNormalBases (out TypeExpr base_class)
820 if (type_bases == null)
823 int count = type_bases.Count;
826 if (Kind == Kind.Class){
827 TypeExpr name = ((Expression) type_bases [0]).ResolveAsBaseTerminal (this, false);
833 if (!name.IsInterface) {
834 // base_class could be a class, struct, enum, delegate.
835 // This is validated in GetClassBases.
841 TypeExpr [] ifaces = new TypeExpr [count-start];
843 for (i = start, j = 0; i < count; i++, j++){
844 TypeExpr resolved = ((Expression) type_bases [i]).ResolveAsBaseTerminal (this, false);
845 if (resolved == null) {
849 ifaces [j] = resolved;
852 return ifaces.Length == 0 ? null : ifaces;
856 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
858 ArrayList ifaces = new ArrayList (0);
859 if (iface_exprs != null)
860 ifaces.AddRange (iface_exprs);
862 foreach (TypeContainer part in partial_parts) {
863 TypeExpr new_base_class;
864 TypeExpr[] new_ifaces = part.GetClassBases (out new_base_class);
865 if (new_base_class != TypeManager.system_object_expr) {
866 if (base_class == TypeManager.system_object_expr)
867 base_class = new_base_class;
869 if (new_base_class != null && !new_base_class.Equals (base_class)) {
870 Report.SymbolRelatedToPreviousError (base_class.Location, "");
871 Report.Error (263, part.Location,
872 "Partial declarations of `{0}' must not specify different base classes",
873 part.GetSignatureForError ());
880 if (new_ifaces == null)
883 foreach (TypeExpr iface in new_ifaces) {
884 if (ifaces.Contains (iface))
891 if (ifaces.Count == 0)
894 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
898 /// This function computes the Base class and also the
899 /// list of interfaces that the class or struct @c implements.
901 /// The return value is an array (might be null) of
902 /// interfaces implemented (as Types).
904 /// The @base_class argument is set to the base object or null
905 /// if this is `System.Object'.
907 public virtual TypeExpr [] GetClassBases (out TypeExpr base_class)
909 TypeExpr[] ifaces = GetNormalBases (out base_class);
914 int count = ifaces.Length;
916 for (int i = 0; i < count; i++) {
917 TypeExpr iface = (TypeExpr) ifaces [i];
919 if (!iface.IsInterface) {
920 if (Kind != Kind.Class) {
921 // TODO: location of symbol related ....
922 Error_TypeInListIsNotInterface (Location, iface.GetSignatureForError ());
924 else if (base_class != null)
925 Report.Error (1721, Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
926 GetSignatureForError (), base_class.GetSignatureForError (), iface.GetSignatureForError ());
928 Report.Error (1722, Location, "`{0}': Base class `{1}' must be specified as first",
929 GetSignatureForError (), iface.GetSignatureForError ());
934 for (int x = 0; x < i; x++) {
935 if (iface.Equals (ifaces [x])) {
936 Report.Error (528, Location,
937 "`{0}' is already listed in " +
938 "interface list", iface.GetSignatureForError ());
943 if ((Kind == Kind.Interface) &&
944 !iface.AsAccessible (this)) {
945 Report.Error (61, Location,
946 "Inconsistent accessibility: base " +
947 "interface `{0}' is less accessible " +
948 "than interface `{1}'", iface.GetSignatureForError (),
956 bool CheckGenericInterfaces (Type[] ifaces)
959 ArrayList already_checked = new ArrayList ();
961 for (int i = 0; i < ifaces.Length; i++) {
962 Type iface = ifaces [i];
963 foreach (Type t in already_checked) {
967 Type[] inferred = new Type [CountTypeParameters];
968 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
971 Report.Error (695, Location,
972 "`{0}' cannot implement both `{1}' and `{2}' " +
973 "because they may unify for some type parameter substitutions",
974 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
975 TypeManager.CSharpName (t));
979 already_checked.Add (iface);
988 protected void Error_TypeInListIsNotInterface (Location loc, string type)
990 Report.Error (527, loc, "Type `{0}' in interface list is not an interface", type);
993 bool CreateTypeBuilder ()
996 Type default_parent = null;
997 if (Kind == Kind.Struct)
998 default_parent = TypeManager.value_type;
999 else if (Kind == Kind.Enum)
1000 default_parent = TypeManager.enum_type;
1003 if (TypeManager.NamespaceClash (Name, Location)) {
1007 ModuleBuilder builder = CodeGen.Module.Builder;
1008 TypeBuilder = builder.DefineType (
1009 Name, TypeAttr, default_parent, null);
1011 TypeBuilder builder = Parent.TypeBuilder;
1013 TypeBuilder = builder.DefineNestedType (
1014 Basename, TypeAttr, default_parent, null);
1016 } catch (ArgumentException) {
1017 Report.RuntimeMissingSupport (Location, "static classes");
1021 TypeManager.AddUserType (this);
1025 string[] param_names = new string [TypeParameters.Length];
1026 for (int i = 0; i < TypeParameters.Length; i++)
1027 param_names [i] = TypeParameters [i].Name;
1029 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
1031 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1032 for (int i = offset; i < gen_params.Length; i++)
1033 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1040 bool DefineBaseTypes ()
1042 iface_exprs = GetClassBases (out base_type);
1043 if (partial_parts != null) {
1044 iface_exprs = GetNormalPartialBases (ref base_type);
1048 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1049 // which in turn should have called DefineType()s on base types if necessary.
1051 // None of the code below should trigger DefineType()s on classes that we depend on.
1052 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1054 // Let's do it as soon as possible, since code below can call DefineType() on classes
1055 // that depend on us to be populated before they are.
1057 if (!(this is CompilerGeneratedClass))
1058 RootContext.RegisterOrder (this);
1060 if (IsGeneric && base_type != null && TypeManager.IsAttributeType (base_type.Type)) {
1061 Report.Error (698, base_type.Location,
1062 "A generic type cannot derive from `{0}' because it is an attribute class",
1063 base_type.GetSignatureForError ());
1067 if (!CheckRecursiveDefinition (this))
1070 if (base_type != null && base_type.Type != null) {
1071 TypeBuilder.SetParent (base_type.Type);
1073 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1074 if (obsolete_attr != null && !IsInObsoleteScope)
1075 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1078 // add interfaces that were not added at type creation
1079 if (iface_exprs != null) {
1080 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1084 foreach (Type itype in ifaces)
1085 TypeBuilder.AddInterfaceImplementation (itype);
1087 foreach (TypeExpr ie in iface_exprs) {
1088 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (ie.Type);
1089 if ((oa != null) && !IsInObsoleteScope)
1090 AttributeTester.Report_ObsoleteMessage (
1091 oa, ie.GetSignatureForError (), Location);
1094 if (!CheckGenericInterfaces (ifaces))
1097 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1104 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1106 public TypeBuilder CreateType ()
1108 if (TypeBuilder != null)
1114 if (!CreateTypeBuilder ()) {
1119 if (partial_parts != null) {
1120 foreach (TypeContainer part in partial_parts)
1121 part.TypeBuilder = TypeBuilder;
1124 if (Types != null) {
1125 foreach (TypeContainer tc in Types) {
1126 if (tc.CreateType () == null) {
1138 public override TypeBuilder DefineType ()
1145 type_defined = true;
1147 if (CreateType () == null) {
1152 if (!DefineBaseTypes ()) {
1157 if (!ResolveMembers ()) {
1162 if (!DefineNestedTypes ()) {
1170 bool ResolveMembers ()
1172 if (members_resolved)
1173 return members_resolved_ok;
1175 members_resolved_ok = DoResolveMembers ();
1176 members_resolved = true;
1178 return members_resolved_ok;
1181 protected virtual bool DoResolveMembers ()
1183 if (methods != null) {
1184 foreach (Method method in methods) {
1185 if (!method.ResolveMembers ())
1190 // TODO: this is not really needed
1191 if (operators != null) {
1192 foreach (Operator o in operators) {
1193 if (!o.ResolveMembers ())
1201 public override void SetParameterInfo (ArrayList constraints_list)
1203 base.SetParameterInfo (constraints_list);
1205 if (!is_generic || PartialContainer == this)
1208 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1209 for (int i = 0; i < tc_names.Length; ++i) {
1210 if (tc_names [i].Name != type_params [i].Name) {
1211 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1212 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1213 GetSignatureForError ());
1219 void UpdateTypeParameterConstraints (TypeContainer part)
1221 TypeParameter[] current_params = CurrentTypeParameters;
1222 for (int i = 0; i < current_params.Length; i++) {
1223 Constraints c = part.CurrentTypeParameters [i].Constraints;
1227 if (current_params [i].UpdateConstraints (part, c))
1230 Report.SymbolRelatedToPreviousError (Location, "");
1231 Report.Error (265, part.Location,
1232 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1233 GetSignatureForError (), current_params [i].GetSignatureForError ());
1237 public bool ResolveType ()
1239 if (!DoResolveType ())
1242 if (compiler_generated != null) {
1243 foreach (CompilerGeneratedClass c in compiler_generated)
1244 if (!c.ResolveType ())
1251 protected virtual bool DoResolveType ()
1253 if ((base_type != null) &&
1254 (base_type.ResolveAsTypeTerminal (this, false) == null)) {
1262 if (PartialContainer != this)
1263 throw new InternalErrorException ();
1265 TypeExpr current_type = null;
1267 foreach (TypeParameter type_param in CurrentTypeParameters) {
1268 if (!type_param.Resolve (this)) {
1274 if (partial_parts != null && is_generic) {
1275 foreach (TypeContainer part in partial_parts)
1276 UpdateTypeParameterConstraints (part);
1279 foreach (TypeParameter type_param in TypeParameters) {
1280 if (!type_param.DefineType (this)) {
1286 current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
1288 foreach (TypeParameter type_param in TypeParameters)
1289 if (!type_param.CheckDependencies ()) {
1294 if (current_type != null) {
1295 current_type = current_type.ResolveAsTypeTerminal (this, false);
1296 if (current_type == null) {
1301 CurrentType = current_type.Type;
1307 protected virtual bool DefineNestedTypes ()
1309 if (Types != null) {
1310 foreach (TypeContainer tc in Types)
1311 if (tc.DefineType () == null)
1315 if (Delegates != null) {
1316 foreach (Delegate d in Delegates)
1317 if (d.DefineType () == null)
1324 TypeContainer InTransit;
1326 protected bool CheckRecursiveDefinition (TypeContainer tc)
1328 if (InTransit != null) {
1329 Report.SymbolRelatedToPreviousError (this);
1330 if (this is Interface)
1332 529, tc.Location, "Inherited interface `{0}' causes a " +
1333 "cycle in the interface hierarchy of `{1}'",
1334 GetSignatureForError (), tc.GetSignatureForError ());
1337 146, tc.Location, "Circular base class dependency " +
1338 "involving `{0}' and `{1}'",
1339 tc.GetSignatureForError (), GetSignatureForError ());
1345 if (base_type != null && base_type.Type != null) {
1346 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1347 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1348 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1352 if (iface_exprs != null) {
1353 foreach (TypeExpr iface in iface_exprs) {
1354 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1355 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1356 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1361 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1368 public static void Error_KeywordNotAllowed (Location loc)
1370 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1374 /// Populates our TypeBuilder with fields and methods
1376 public override bool DefineMembers ()
1378 if (members_defined)
1379 return members_defined_ok;
1381 if (!base.DefineMembers ())
1384 members_defined_ok = DoDefineMembers ();
1385 members_defined = true;
1387 return members_defined_ok;
1390 protected virtual bool DoDefineMembers ()
1392 if (iface_exprs != null) {
1393 foreach (TypeExpr iface in iface_exprs) {
1394 ConstructedType ct = iface as ConstructedType;
1395 if ((ct != null) && !ct.CheckConstraints (this))
1400 if (base_type != null) {
1401 ConstructedType ct = base_type as ConstructedType;
1402 if ((ct != null) && !ct.CheckConstraints (this))
1405 member_cache = new MemberCache (base_type.Type, this);
1406 } else if (Kind == Kind.Interface) {
1407 member_cache = new MemberCache (null, this);
1408 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1409 for (int i = 0; i < ifaces.Length; ++i)
1410 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1412 member_cache = new MemberCache (null, this);
1416 foreach (TypeContainer tc in types)
1417 member_cache.AddNestedType (tc);
1419 if (delegates != null)
1420 foreach (Delegate d in delegates)
1421 member_cache.AddNestedType (d);
1423 if (partial_parts != null) {
1424 foreach (TypeContainer part in partial_parts)
1425 part.member_cache = member_cache;
1429 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1430 if (conflict_symbol == null) {
1431 if ((ModFlags & Modifiers.NEW) != 0)
1432 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1434 if ((ModFlags & Modifiers.NEW) == 0) {
1435 Report.SymbolRelatedToPreviousError (conflict_symbol);
1436 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1437 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1442 DefineContainerMembers (constants);
1443 DefineContainerMembers (fields);
1445 if (Kind == Kind.Struct || Kind == Kind.Class) {
1446 pending = PendingImplementation.GetPendingImplementations (this);
1450 // Constructors are not in the defined_names array
1452 DefineContainerMembers (instance_constructors);
1454 DefineContainerMembers (events);
1455 DefineContainerMembers (ordered_explicit_member_list);
1456 DefineContainerMembers (ordered_member_list);
1458 DefineContainerMembers (operators);
1459 DefineContainerMembers (delegates);
1461 ComputeIndexerName();
1462 CheckEqualsAndGetHashCode();
1464 if (CurrentType != null) {
1465 GenericType = CurrentType;
1470 // FIXME: This hack is needed because member cache does not work
1471 // with generic types, we rely on runtime to inflate dynamic types.
1472 // TODO: This hack requires member cache refactoring to be removed
1474 if (TypeBuilder.IsGenericType)
1475 member_cache = new MemberCache (this);
1481 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1484 mcal.DefineContainerMembers ();
1487 protected virtual void ComputeIndexerName ()
1489 if (indexers == null)
1492 string class_indexer_name = null;
1495 // If there's both an explicit and an implicit interface implementation, the
1496 // explicit one actually implements the interface while the other one is just
1497 // a normal indexer. See bug #37714.
1500 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1501 foreach (Indexer i in indexers) {
1502 if (i.InterfaceType != null) {
1503 if (seen_normal_indexers)
1504 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1508 seen_normal_indexers = true;
1510 if (class_indexer_name == null) {
1511 class_indexer_name = i.ShortName;
1515 if (i.ShortName != class_indexer_name)
1516 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");
1519 if (class_indexer_name != null)
1520 indexer_name = class_indexer_name;
1523 protected virtual void EmitIndexerName ()
1525 if (!seen_normal_indexers)
1528 if (TypeManager.default_member_ctor == null) {
1529 if (TypeManager.default_member_type == null) {
1530 TypeManager.default_member_type = TypeManager.CoreLookupType (
1531 "System.Reflection", "DefaultMemberAttribute", Kind.Class, true);
1533 if (TypeManager.default_member_type == null)
1537 TypeManager.default_member_ctor = TypeManager.GetPredefinedConstructor (
1538 TypeManager.default_member_type, Location, TypeManager.string_type);
1540 if (TypeManager.default_member_ctor == null)
1544 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
1545 TypeBuilder.SetCustomAttribute (cb);
1548 protected virtual void CheckEqualsAndGetHashCode ()
1550 if (methods == null)
1553 if (HasEquals && !HasGetHashCode) {
1554 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1558 public override bool Define ()
1560 CheckProtectedModifier ();
1564 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1566 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1570 /// This function is based by a delegate to the FindMembers routine
1572 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1578 /// This filter is used by FindMembers, and we just keep
1579 /// a global for the filter to `AlwaysAccept'
1581 static MemberFilter accepting_filter;
1584 static TypeContainer ()
1586 accepting_filter = new MemberFilter (AlwaysAccept);
1589 public MethodInfo[] GetMethods ()
1591 ArrayList members = new ArrayList ();
1595 if (methods != null) {
1596 int len = methods.Count;
1597 for (int i = 0; i < len; i++) {
1598 Method m = (Method) methods [i];
1600 members.Add (m.MethodBuilder);
1604 if (operators != null) {
1605 int len = operators.Count;
1606 for (int i = 0; i < len; i++) {
1607 Operator o = (Operator) operators [i];
1609 members.Add (o.MethodBuilder);
1613 if (properties != null) {
1614 int len = properties.Count;
1615 for (int i = 0; i < len; i++) {
1616 Property p = (Property) properties [i];
1618 if (p.GetBuilder != null)
1619 members.Add (p.GetBuilder);
1620 if (p.SetBuilder != null)
1621 members.Add (p.SetBuilder);
1625 if (indexers != null) {
1626 int len = indexers.Count;
1627 for (int i = 0; i < len; i++) {
1628 Indexer ix = (Indexer) indexers [i];
1630 if (ix.GetBuilder != null)
1631 members.Add (ix.GetBuilder);
1632 if (ix.SetBuilder != null)
1633 members.Add (ix.SetBuilder);
1637 if (events != null) {
1638 int len = events.Count;
1639 for (int i = 0; i < len; i++) {
1640 Event e = (Event) events [i];
1642 if (e.AddBuilder != null)
1643 members.Add (e.AddBuilder);
1644 if (e.RemoveBuilder != null)
1645 members.Add (e.RemoveBuilder);
1649 MethodInfo[] retMethods = new MethodInfo [members.Count];
1650 members.CopyTo (retMethods, 0);
1654 // Indicated whether container has StructLayout attribute set Explicit
1655 public bool HasExplicitLayout {
1656 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1657 set { caching_flags |= Flags.HasExplicitLayout; }
1661 // Return the nested type with name @name. Ensures that the nested type
1662 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1664 public Type FindNestedType (string name)
1666 if (PartialContainer != this)
1667 throw new InternalErrorException ("should not happen");
1669 ArrayList [] lists = { types, delegates };
1671 for (int j = 0; j < lists.Length; ++j) {
1672 ArrayList list = lists [j];
1676 int len = list.Count;
1677 for (int i = 0; i < len; ++i) {
1678 DeclSpace ds = (DeclSpace) list [i];
1679 if (ds.Basename == name) {
1680 return ds.DefineType ();
1688 private void FindMembers_NestedTypes (int modflags,
1689 BindingFlags bf, MemberFilter filter, object criteria,
1690 ref ArrayList members)
1692 ArrayList [] lists = { types, delegates };
1694 for (int j = 0; j < lists.Length; ++j) {
1695 ArrayList list = lists [j];
1699 int len = list.Count;
1700 for (int i = 0; i < len; i++) {
1701 DeclSpace ds = (DeclSpace) list [i];
1703 if ((ds.ModFlags & modflags) == 0)
1706 TypeBuilder tb = ds.TypeBuilder;
1708 if (!(criteria is string) || ds.Basename.Equals (criteria))
1709 tb = ds.DefineType ();
1712 if (tb != null && (filter (tb, criteria) == true)) {
1713 if (members == null)
1714 members = new ArrayList ();
1723 /// This method returns the members of this type just like Type.FindMembers would
1724 /// Only, we need to use this for types which are _being_ defined because MS'
1725 /// implementation can't take care of that.
1728 // FIXME: return an empty static array instead of null, that cleans up
1729 // some code and is consistent with some coding conventions I just found
1733 // Notice that in various cases we check if our field is non-null,
1734 // something that would normally mean that there was a bug elsewhere.
1736 // The problem happens while we are defining p-invoke methods, as those
1737 // will trigger a FindMembers, but this happens before things are defined
1739 // Since the whole process is a no-op, it is fine to check for null here.
1741 // TODO: This approach will be one day completely removed, it's already
1742 // used at few places only
1745 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1746 MemberFilter filter, object criteria)
1748 ArrayList members = null;
1751 if ((bf & BindingFlags.Public) != 0)
1752 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1754 if ((bf & BindingFlags.NonPublic) != 0)
1755 modflags |= Modifiers.PRIVATE;
1757 int static_mask = 0, static_flags = 0;
1758 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1759 case BindingFlags.Static:
1760 static_mask = static_flags = Modifiers.STATIC;
1763 case BindingFlags.Instance:
1764 static_mask = Modifiers.STATIC;
1769 static_mask = static_flags = 0;
1773 Timer.StartTimer (TimerType.TcFindMembers);
1776 filter = accepting_filter;
1778 if ((mt & MemberTypes.Field) != 0) {
1779 if (fields != null) {
1780 int len = fields.Count;
1781 for (int i = 0; i < len; i++) {
1782 FieldBase f = (FieldBase) fields [i];
1784 if ((f.ModFlags & modflags) == 0)
1786 if ((f.ModFlags & static_mask) != static_flags)
1789 FieldBuilder fb = f.FieldBuilder;
1790 if (fb != null && filter (fb, criteria) == true) {
1791 if (members == null)
1792 members = new ArrayList ();
1799 if (constants != null) {
1800 int len = constants.Count;
1801 for (int i = 0; i < len; i++) {
1802 Const con = (Const) constants [i];
1804 if ((con.ModFlags & modflags) == 0)
1806 if ((con.ModFlags & static_mask) != static_flags)
1809 FieldBuilder fb = con.FieldBuilder;
1812 fb = con.FieldBuilder;
1814 if (fb != null && filter (fb, criteria) == true) {
1815 if (members == null)
1816 members = new ArrayList ();
1824 if ((mt & MemberTypes.Method) != 0) {
1825 if (methods != null) {
1826 int len = methods.Count;
1827 for (int i = 0; i < len; i++) {
1828 Method m = (Method) methods [i];
1830 if ((m.ModFlags & modflags) == 0)
1832 if ((m.ModFlags & static_mask) != static_flags)
1835 MethodBuilder mb = m.MethodBuilder;
1837 if (mb != null && filter (mb, criteria) == true) {
1838 if (members == null)
1839 members = new ArrayList ();
1846 if (operators != null) {
1847 int len = operators.Count;
1848 for (int i = 0; i < len; i++) {
1849 Operator o = (Operator) operators [i];
1851 if ((o.ModFlags & modflags) == 0)
1853 if ((o.ModFlags & static_mask) != static_flags)
1856 MethodBuilder ob = o.MethodBuilder;
1857 if (ob != null && filter (ob, criteria) == true) {
1858 if (members == null)
1859 members = new ArrayList ();
1866 if (events != null) {
1867 foreach (Event e in events) {
1868 if ((e.ModFlags & modflags) == 0)
1870 if ((e.ModFlags & static_mask) != static_flags)
1873 MethodBuilder b = e.AddBuilder;
1874 if (b != null && filter (b, criteria)) {
1875 if (members == null)
1876 members = new ArrayList (4);
1881 b = e.RemoveBuilder;
1882 if (b != null && filter (b, criteria)) {
1883 if (members == null)
1884 members = new ArrayList (4);
1891 if (properties != null) {
1892 int len = properties.Count;
1893 for (int i = 0; i < len; i++) {
1894 Property p = (Property) properties [i];
1896 if ((p.ModFlags & modflags) == 0)
1898 if ((p.ModFlags & static_mask) != static_flags)
1904 if (b != null && filter (b, criteria) == true) {
1905 if (members == null)
1906 members = new ArrayList ();
1912 if (b != null && filter (b, criteria) == true) {
1913 if (members == null)
1914 members = new ArrayList ();
1921 if (indexers != null) {
1922 int len = indexers.Count;
1923 for (int i = 0; i < len; i++) {
1924 Indexer ix = (Indexer) indexers [i];
1926 if ((ix.ModFlags & modflags) == 0)
1928 if ((ix.ModFlags & static_mask) != static_flags)
1934 if (b != null && filter (b, criteria) == true) {
1935 if (members == null)
1936 members = new ArrayList ();
1942 if (b != null && filter (b, criteria) == true) {
1943 if (members == null)
1944 members = new ArrayList ();
1952 if ((mt & MemberTypes.Event) != 0) {
1953 if (events != null) {
1954 int len = events.Count;
1955 for (int i = 0; i < len; i++) {
1956 Event e = (Event) events [i];
1958 if ((e.ModFlags & modflags) == 0)
1960 if ((e.ModFlags & static_mask) != static_flags)
1963 MemberInfo eb = e.EventBuilder;
1964 if (eb != null && filter (eb, criteria) == true) {
1965 if (members == null)
1966 members = new ArrayList ();
1968 members.Add (e.EventBuilder);
1974 if ((mt & MemberTypes.Property) != 0){
1975 if (properties != null) {
1976 int len = properties.Count;
1977 for (int i = 0; i < len; i++) {
1978 Property p = (Property) properties [i];
1980 if ((p.ModFlags & modflags) == 0)
1982 if ((p.ModFlags & static_mask) != static_flags)
1985 MemberInfo pb = p.PropertyBuilder;
1986 if (pb != null && filter (pb, criteria) == true) {
1987 if (members == null)
1988 members = new ArrayList ();
1990 members.Add (p.PropertyBuilder);
1995 if (indexers != null) {
1996 int len = indexers.Count;
1997 for (int i = 0; i < len; i++) {
1998 Indexer ix = (Indexer) indexers [i];
2000 if ((ix.ModFlags & modflags) == 0)
2002 if ((ix.ModFlags & static_mask) != static_flags)
2005 MemberInfo ib = ix.PropertyBuilder;
2006 if (ib != null && filter (ib, criteria) == true) {
2007 if (members == null)
2008 members = new ArrayList ();
2010 members.Add (ix.PropertyBuilder);
2016 if ((mt & MemberTypes.NestedType) != 0)
2017 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
2019 if ((mt & MemberTypes.Constructor) != 0){
2020 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
2021 int len = instance_constructors.Count;
2022 for (int i = 0; i < len; i++) {
2023 Constructor c = (Constructor) instance_constructors [i];
2025 ConstructorBuilder cb = c.ConstructorBuilder;
2026 if (cb != null && filter (cb, criteria) == true) {
2027 if (members == null)
2028 members = new ArrayList ();
2035 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2036 ConstructorBuilder cb =
2037 default_static_constructor.ConstructorBuilder;
2039 if (cb != null && filter (cb, criteria) == true) {
2040 if (members == null)
2041 members = new ArrayList ();
2049 // Lookup members in base if requested.
2051 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2052 if (TypeBuilder.BaseType != null) {
2053 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2054 if (list.Count > 0) {
2055 if (members == null)
2056 members = new ArrayList ();
2058 members.AddRange (list);
2063 Timer.StopTimer (TimerType.TcFindMembers);
2065 if (members == null)
2066 return MemberList.Empty;
2068 return new MemberList (members);
2071 public override MemberCache MemberCache {
2073 return member_cache;
2077 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2078 MemberFilter filter, object criteria)
2080 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2083 return ds.FindMembers (mt, bf, filter, criteria);
2085 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2089 /// Emits the values for the constants
2091 public void EmitConstants ()
2093 if (constants != null)
2094 foreach (Const con in constants)
2099 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2104 foreach (MemberCore mc in al) {
2105 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2108 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2109 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2114 public virtual void VerifyMembers ()
2117 // Check for internal or private fields that were never assigned
2119 if (Report.WarningLevel >= 3) {
2120 CheckMemberUsage (properties, "property");
2121 CheckMemberUsage (methods, "method");
2122 CheckMemberUsage (constants, "constant");
2124 if (fields != null){
2125 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2126 foreach (FieldBase f in fields) {
2127 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2128 if (is_type_exposed)
2131 f.SetMemberIsUsed ();
2135 if ((f.caching_flags & Flags.IsAssigned) == 0)
2136 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2139 const int error_code = 414;
2141 const int error_code = 169;
2143 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2144 f.GetSignatureForError ());
2150 // Only report 649 on level 4
2152 if (Report.WarningLevel < 4)
2155 if ((f.caching_flags & Flags.IsAssigned) != 0)
2158 Constant c = New.Constantify (f.MemberType);
2159 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2160 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2166 // TODO: move to ClassOrStruct
2167 void EmitConstructors ()
2169 if (instance_constructors == null)
2172 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2173 bool has_compliant_args = false;
2175 foreach (Constructor c in instance_constructors) {
2179 catch (Exception e) {
2180 throw new InternalErrorException (c, e);
2183 if (has_compliant_args)
2186 has_compliant_args = c.HasCompliantArgs;
2188 if (!has_compliant_args)
2189 Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2191 foreach (Constructor c in instance_constructors) {
2195 catch (Exception e) {
2196 throw new InternalErrorException (c, e);
2203 /// Emits the code, this step is performed after all
2204 /// the types, enumerations, constructors
2206 public virtual void EmitType ()
2208 if (OptAttributes != null)
2209 OptAttributes.Emit ();
2212 // Structs with no fields need to have at least one byte.
2213 // The right thing would be to set the PackingSize in a DefineType
2214 // but there are no functions that allow interfaces *and* the size to
2218 if (Kind == Kind.Struct && first_nonstatic_field == null){
2219 FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
2220 FieldAttributes.Private);
2222 if (HasExplicitLayout){
2223 object [] ctor_args = new object [] { 0 };
2225 if (TypeManager.field_offset_attribute_ctor == null) {
2227 if (TypeManager.field_offset_attribute_type == null) {
2228 TypeManager.field_offset_attribute_type = TypeManager.CoreLookupType (
2229 "System.Runtime.InteropServices", "FieldOffsetAttribute", Kind.Class, true);
2232 TypeManager.field_offset_attribute_ctor = TypeManager.GetPredefinedConstructor (
2233 TypeManager.field_offset_attribute_type, Location, TypeManager.int32_type);
2236 CustomAttributeBuilder cba = new CustomAttributeBuilder (
2237 TypeManager.field_offset_attribute_ctor, ctor_args);
2238 fb.SetCustomAttribute (cba);
2244 EmitConstructors ();
2246 // Can not continue if constants are broken
2248 if (Report.Errors > 0)
2251 if (default_static_constructor != null)
2252 default_static_constructor.Emit ();
2254 if (operators != null)
2255 foreach (Operator o in operators)
2258 if (properties != null)
2259 foreach (Property p in properties)
2262 if (indexers != null) {
2263 foreach (Indexer indx in indexers)
2268 if (methods != null) {
2269 for (int i = 0; i < methods.Count; ++i)
2270 ((Method) methods [i]).Emit ();
2274 foreach (FieldBase f in fields)
2277 if (events != null){
2278 foreach (Event e in Events)
2282 if (delegates != null) {
2283 foreach (Delegate d in Delegates) {
2288 if (pending != null)
2289 if (pending.VerifyPendingMethods ())
2292 if (Report.Errors > 0)
2295 if (compiler_generated != null) {
2296 foreach (CompilerGeneratedClass c in compiler_generated)
2301 public override void CloseType ()
2303 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2307 caching_flags |= Flags.CloseTypeCreated;
2308 TypeBuilder.CreateType ();
2309 } catch (TypeLoadException){
2311 // This is fine, the code still created the type
2313 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2314 // Console.WriteLine (e.Message);
2315 } catch (Exception e) {
2316 throw new InternalErrorException (this, e);
2320 foreach (TypeContainer tc in Types)
2321 if (tc.Kind == Kind.Struct)
2324 foreach (TypeContainer tc in Types)
2325 if (tc.Kind != Kind.Struct)
2329 if (Delegates != null)
2330 foreach (Delegate d in Delegates)
2333 if (compiler_generated != null)
2334 foreach (CompilerGeneratedClass c in compiler_generated)
2341 initialized_fields = null;
2342 initialized_static_fields = null;
2344 ordered_explicit_member_list = null;
2345 ordered_member_list = null;
2350 compiler_generated = null;
2351 default_constructor = null;
2352 default_static_constructor = null;
2354 OptAttributes = null;
2357 member_cache = null;
2361 // Performs the validation on a Method's modifiers (properties have
2362 // the same properties).
2364 public bool MethodModifiersValid (MemberCore mc)
2366 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2367 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2368 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2370 int flags = mc.ModFlags;
2373 // At most one of static, virtual or override
2375 if ((flags & Modifiers.STATIC) != 0){
2376 if ((flags & vao) != 0){
2377 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2378 mc.GetSignatureForError ());
2383 if (Kind == Kind.Struct){
2384 if ((flags & va) != 0){
2385 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2390 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2391 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2392 mc.GetSignatureForError ());
2397 // If the declaration includes the abstract modifier, then the
2398 // declaration does not include static, virtual or extern
2400 if ((flags & Modifiers.ABSTRACT) != 0){
2401 if ((flags & Modifiers.EXTERN) != 0){
2403 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2407 if ((flags & Modifiers.SEALED) != 0) {
2408 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2412 if ((flags & Modifiers.VIRTUAL) != 0){
2413 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2417 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2418 Report.SymbolRelatedToPreviousError (this);
2419 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2420 mc.GetSignatureForError (), GetSignatureForError ());
2425 if ((flags & Modifiers.PRIVATE) != 0){
2426 if ((flags & vao) != 0){
2427 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2432 if ((flags & Modifiers.SEALED) != 0){
2433 if ((flags & Modifiers.OVERRIDE) == 0){
2434 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2442 public Constructor DefaultStaticConstructor {
2443 get { return default_static_constructor; }
2446 protected override bool VerifyClsCompliance ()
2448 if (!base.VerifyClsCompliance ())
2453 Type base_type = TypeBuilder.BaseType;
2454 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2455 Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2462 /// Checks whether container name is CLS Compliant
2464 void VerifyClsName ()
2466 Hashtable base_members = base_cache == null ?
2468 base_cache.GetPublicMembers ();
2469 Hashtable this_members = new Hashtable ();
2471 foreach (DictionaryEntry entry in defined_names) {
2472 MemberCore mc = (MemberCore)entry.Value;
2473 if (!mc.IsClsComplianceRequired ())
2476 string name = (string) entry.Key;
2477 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2479 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2480 object found = base_members [lcase];
2481 if (found == null) {
2482 found = this_members [lcase];
2483 if (found == null) {
2484 this_members.Add (lcase, mc);
2489 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2492 if (found is MemberInfo) {
2493 if (basename == ((MemberInfo) found).Name)
2495 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2497 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2500 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2502 Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2509 /// Performs checks for an explicit interface implementation. First it
2510 /// checks whether the `interface_type' is a base inteface implementation.
2511 /// Then it checks whether `name' exists in the interface type.
2513 public bool VerifyImplements (InterfaceMemberBase mb)
2515 if (ifaces != null) {
2516 foreach (Type t in ifaces){
2517 if (TypeManager.IsEqual (t, mb.InterfaceType))
2522 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2523 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2524 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2528 public override Type LookupAnyGeneric (string typeName)
2530 if (types != null) {
2531 foreach (TypeContainer tc in types) {
2535 int pos = tc.Basename.LastIndexOf ('`');
2536 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2537 return tc.TypeBuilder;
2541 return base.LookupAnyGeneric (typeName);
2544 public void Mark_HasEquals ()
2546 cached_method |= CachedMethods.Equals;
2549 public void Mark_HasGetHashCode ()
2551 cached_method |= CachedMethods.GetHashCode;
2555 /// Method container contains Equals method
2557 public bool HasEquals {
2559 return (cached_method & CachedMethods.Equals) != 0;
2564 /// Method container contains GetHashCode method
2566 public bool HasGetHashCode {
2568 return (cached_method & CachedMethods.GetHashCode) != 0;
2572 public bool HasStaticFieldInitializer {
2574 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2578 cached_method |= CachedMethods.HasStaticFieldInitializer;
2580 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2588 string IMemberContainer.Name {
2594 Type IMemberContainer.Type {
2600 bool IMemberContainer.IsInterface {
2602 return Kind == Kind.Interface;
2606 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2608 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2610 if (GenericType != null)
2611 return TypeManager.FindMembers (GenericType, mt, new_bf,
2614 return FindMembers (mt, new_bf, null, null);
2618 // Generates xml doc comments (if any), and if required,
2619 // handle warning report.
2621 internal override void GenerateDocComment (DeclSpace ds)
2623 DocUtil.GenerateTypeDocComment (this, ds);
2626 public override string DocCommentHeader {
2627 get { return "T:"; }
2630 public MemberCache BaseCache {
2632 if (base_cache != null)
2634 if (TypeBuilder.BaseType != null)
2635 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2636 if (TypeBuilder.IsInterface)
2637 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2643 public abstract class ClassOrStruct : TypeContainer {
2644 ListDictionary declarative_security;
2646 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2647 MemberName name, Attributes attrs, Kind kind)
2648 : base (ns, parent, name, attrs, kind)
2652 protected override bool AddToContainer (MemberCore symbol, string name)
2654 if (name == MemberName.Name) {
2655 if (symbol is TypeParameter) {
2656 Report.Error (694, symbol.Location,
2657 "Type parameter `{0}' has same name as " +
2658 "containing type, or method", name);
2662 Report.SymbolRelatedToPreviousError (this);
2663 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2664 symbol.GetSignatureForError ());
2668 return base.AddToContainer (symbol, name);
2671 public override void VerifyMembers ()
2673 base.VerifyMembers ();
2675 if ((events != null) && Report.WarningLevel >= 3) {
2676 foreach (Event e in events){
2677 // Note: The event can be assigned from same class only, so we can report
2678 // this warning for all accessibility modes
2679 if ((e.caching_flags & Flags.IsUsed) == 0)
2680 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2685 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2687 if (a.IsValidSecurityAttribute ()) {
2688 if (declarative_security == null)
2689 declarative_security = new ListDictionary ();
2691 a.ExtractSecurityPermissionSet (declarative_security);
2695 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2696 PartialContainer.HasExplicitLayout = true;
2699 base.ApplyAttributeBuilder (a, cb);
2703 /// Defines the default constructors
2705 protected void DefineDefaultConstructor (bool is_static)
2707 // The default instance constructor is public
2708 // If the class is abstract, the default constructor is protected
2709 // The default static constructor is private
2713 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2715 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2718 Constructor c = new Constructor (this, MemberName.Name, mods,
2719 Parameters.EmptyReadOnlyParameters,
2720 new GeneratedBaseInitializer (Location),
2724 c.Block = new ToplevelBlock (null, Location);
2727 public override bool Define ()
2729 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer)
2730 DefineDefaultConstructor (true);
2732 if (default_static_constructor != null)
2733 default_static_constructor.Define ();
2735 return base.Define ();
2738 public override void Emit ()
2742 if (declarative_security != null) {
2743 foreach (DictionaryEntry de in declarative_security) {
2744 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2749 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2751 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
2754 protected override TypeAttributes TypeAttr {
2756 if (default_static_constructor == null)
2757 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2759 return base.TypeAttr;
2765 // TODO: should be sealed
2766 public class Class : ClassOrStruct {
2767 const int AllowedModifiers =
2770 Modifiers.PROTECTED |
2771 Modifiers.INTERNAL |
2773 Modifiers.ABSTRACT |
2778 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2780 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2782 : base (ns, parent, name, attrs, Kind.Class)
2784 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2785 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2787 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2788 Report.FeatureIsNotAvailable (Location, "static classes");
2792 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2794 if (part.Name == "System.Object")
2795 Report.Error (537, part.Location,
2796 "The class System.Object cannot have a base class or implement an interface.");
2797 base.AddBasesForPart (part, bases);
2800 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2802 if (a.Type == TypeManager.attribute_usage_type) {
2803 if (!TypeManager.IsAttributeType (BaseType) &&
2804 TypeBuilder.FullName != "System.Attribute") {
2805 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2809 if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
2810 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2814 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
2815 !attributes.Contains (TypeManager.guid_attr_type)) {
2816 a.Error_MissingGuidAttribute ();
2820 if (a.Type == TypeManager.extension_attribute_type) {
2821 a.Error_MisusedExtensionAttribute ();
2825 if (AttributeTester.IsAttributeExcluded (a.Type))
2828 base.ApplyAttributeBuilder (a, cb);
2831 public override AttributeTargets AttributeTargets {
2833 return AttributeTargets.Class;
2837 protected override void DefineContainerMembers (MemberCoreArrayList list)
2843 base.DefineContainerMembers (list);
2847 foreach (MemberCore m in list) {
2848 if (m is Operator) {
2849 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2853 if (m is Destructor) {
2854 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2858 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2859 m.CheckProtectedModifier ();
2864 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2868 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2871 if (m is Constructor) {
2872 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2876 Method method = m as Method;
2877 if (method != null && method.Parameters.HasExtensionMethodType) {
2878 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2882 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2885 base.DefineContainerMembers (list);
2888 public override TypeBuilder DefineType ()
2890 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2891 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2895 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2896 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2900 return base.DefineType ();
2903 protected override bool DoDefineMembers ()
2905 if (InstanceConstructors == null && !IsStatic)
2906 DefineDefaultConstructor (false);
2908 return base.DoDefineMembers ();
2911 public override void Emit ()
2916 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2917 TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
2921 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
2923 TypeExpr[] ifaces = base.GetClassBases (out base_class);
2925 if (base_class == null) {
2926 if (RootContext.StdLib)
2927 base_class = TypeManager.system_object_expr;
2928 else if (Name != "System.Object")
2929 base_class = TypeManager.system_object_expr;
2931 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2933 689, base_class.Location,
2934 "Cannot derive from `{0}' because it is a type parameter",
2935 base_class.GetSignatureForError ());
2939 if (base_class.IsSealed){
2940 Report.SymbolRelatedToPreviousError (base_class.Type);
2941 if (base_class.Type.IsAbstract) {
2942 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2943 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2945 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2946 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2951 if (!base_class.CanInheritFrom ()){
2952 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2953 GetSignatureForError (), base_class.GetSignatureForError ());
2957 if (!base_class.AsAccessible (this)) {
2958 Report.SymbolRelatedToPreviousError (base_class.Type);
2959 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2960 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2964 if (PartialContainer.IsStaticClass) {
2965 if (base_class.Type != TypeManager.object_type) {
2966 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2967 GetSignatureForError (), base_class.GetSignatureForError ());
2971 if (ifaces != null) {
2972 foreach (TypeExpr t in ifaces)
2973 Report.SymbolRelatedToPreviousError (t.Type);
2974 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2981 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2982 /// Valid only for attribute classes.
2983 public bool IsExcluded ()
2985 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2986 return (caching_flags & Flags.Excluded) != 0;
2988 caching_flags &= ~Flags.Excluded_Undetected;
2990 if (OptAttributes == null)
2993 if (TypeManager.conditional_attribute_type == null)
2996 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3001 foreach (Attribute a in attrs) {
3002 string condition = a.GetConditionalAttributeValue ();
3003 if (RootContext.AllDefines.Contains (condition))
3007 caching_flags |= Flags.Excluded;
3013 return (ModFlags & Modifiers.STATIC) != 0;
3018 // FIXME: How do we deal with the user specifying a different
3021 protected override TypeAttributes TypeAttr {
3023 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
3025 ta |= StaticClassAttribute;
3031 public sealed class Struct : ClassOrStruct {
3033 // Modifiers allowed in a struct declaration
3035 const int AllowedModifiers =
3038 Modifiers.PROTECTED |
3039 Modifiers.INTERNAL |
3043 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
3044 int mod, Attributes attrs)
3045 : base (ns, parent, name, attrs, Kind.Struct)
3049 if (parent.Parent == null)
3050 accmods = Modifiers.INTERNAL;
3052 accmods = Modifiers.PRIVATE;
3054 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
3056 this.ModFlags |= Modifiers.SEALED;
3059 public override AttributeTargets AttributeTargets {
3061 return AttributeTargets.Struct;
3065 const TypeAttributes DefaultTypeAttributes =
3066 TypeAttributes.SequentialLayout |
3067 TypeAttributes.Sealed |
3068 TypeAttributes.BeforeFieldInit;
3071 public override TypeExpr[] GetClassBases (out TypeExpr base_class)
3073 TypeExpr[] ifaces = base.GetClassBases (out base_class);
3075 // If we are compiling our runtime,
3076 // and we are defining ValueType, then our
3077 // base is `System.Object'.
3079 if (base_class == null) {
3080 if (!RootContext.StdLib && Name == "System.ValueType")
3081 base_class = TypeManager.system_object_expr;
3083 base_class = TypeManager.system_valuetype_expr;
3090 // FIXME: Allow the user to specify a different set of attributes
3091 // in some cases (Sealed for example is mandatory for a class,
3092 // but what SequentialLayout can be changed
3094 protected override TypeAttributes TypeAttr {
3096 return base.TypeAttr | DefaultTypeAttributes;
3100 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3102 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3103 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3104 field.GetSignatureForError ());
3107 base.RegisterFieldForInitialization (field, expression);
3115 public sealed class Interface : TypeContainer, IMemberContainer {
3118 /// Modifiers allowed in a class declaration
3120 public const int AllowedModifiers =
3123 Modifiers.PROTECTED |
3124 Modifiers.INTERNAL |
3128 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3130 : base (ns, parent, name, attrs, Kind.Interface)
3134 if (parent.Parent == null)
3135 accmods = Modifiers.INTERNAL;
3137 accmods = Modifiers.PRIVATE;
3139 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3142 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3144 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
3145 !attributes.Contains (TypeManager.guid_attr_type)) {
3146 a.Error_MissingGuidAttribute ();
3149 base.ApplyAttributeBuilder (a, cb);
3153 public override AttributeTargets AttributeTargets {
3155 return AttributeTargets.Interface;
3159 const TypeAttributes DefaultTypeAttributes =
3160 TypeAttributes.AutoLayout |
3161 TypeAttributes.Abstract |
3162 TypeAttributes.Interface;
3164 protected override TypeAttributes TypeAttr {
3166 return base.TypeAttr | DefaultTypeAttributes;
3170 protected override bool VerifyClsCompliance ()
3172 if (!base.VerifyClsCompliance ())
3175 if (ifaces != null) {
3176 foreach (Type t in ifaces) {
3177 if (AttributeTester.IsClsCompliant (t))
3180 Report.SymbolRelatedToPreviousError (t);
3181 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3182 GetSignatureForError (), TypeManager.CSharpName (t));
3190 // It is used as a base class for all property based members
3191 // This includes properties, indexers, and events
3192 public abstract class PropertyBasedMember : InterfaceMemberBase
3194 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3195 FullNamedExpression type, int mod, int allowed_mod, bool is_iface,
3196 MemberName name, Attributes attrs)
3197 : base (parent, generic, type, mod, allowed_mod, is_iface, name, attrs)
3201 protected override bool VerifyClsCompliance ()
3203 if (!base.VerifyClsCompliance ())
3206 if (!AttributeTester.IsClsCompliant (MemberType)) {
3207 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant",
3208 GetSignatureForError ());
3216 public abstract class MethodCore : InterfaceMemberBase
3218 public readonly Parameters Parameters;
3219 protected ToplevelBlock block;
3221 public MethodCore (DeclSpace parent, GenericMethod generic,
3222 FullNamedExpression type, int mod, int allowed_mod, bool is_iface,
3223 MemberName name, Attributes attrs, Parameters parameters)
3224 : base (parent, generic, type, mod, allowed_mod, is_iface, name, attrs)
3226 Parameters = parameters;
3230 // Returns the System.Type array for the parameters of this method
3232 public Type [] ParameterTypes {
3234 return Parameters.Types;
3238 public Parameters ParameterInfo
3245 public ToplevelBlock Block {
3255 protected override bool CheckBase ()
3257 // Check whether arguments were correct.
3258 if (!DefineParameters (Parameters))
3261 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
3262 if (!Parent.MemberCache.CheckExistingMembersOverloads (this,
3263 MemberName.IsGeneric ? MemberName.Basename : MemberName.MethodName, Parameters))
3266 // TODO: Find a better way how to check reserved accessors collision
3267 Method m = this as Method;
3269 if (!m.CheckForDuplications ())
3274 if (!base.CheckBase ())
3281 // Returns a string that represents the signature for this
3282 // member which should be used in XML documentation.
3284 public override string GetDocCommentName (DeclSpace ds)
3286 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3290 // Raised (and passed an XmlElement that contains the comment)
3291 // when GenerateDocComment is writing documentation expectedly.
3293 // FIXME: with a few effort, it could be done with XmlReader,
3294 // that means removal of DOM use.
3296 internal override void OnGenerateDocComment (XmlElement el)
3298 DocUtil.OnMethodGenerateDocComment (this, el);
3302 // Represents header string for documentation comment.
3304 public override string DocCommentHeader
3306 get { return "M:"; }
3309 public override bool EnableOverloadChecks (MemberCore overload)
3311 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3312 caching_flags |= Flags.MethodOverloadsExist;
3319 public virtual void SetYields ()
3321 ModFlags |= Modifiers.METHOD_YIELDS;
3324 protected override bool VerifyClsCompliance ()
3326 if (!base.VerifyClsCompliance ())
3329 if (Parameters.HasArglist) {
3330 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3333 if (!AttributeTester.IsClsCompliant (MemberType)) {
3334 Report.Error (3002, Location, "Return type of `{0}' is not CLS-compliant",
3335 GetSignatureForError ());
3338 Parameters.VerifyClsCompliance ();
3344 public abstract class InterfaceMemberBase : MemberBase {
3346 // Whether this is an interface member.
3348 public bool IsInterface;
3351 // If true, this is an explicit interface implementation
3353 public bool IsExplicitImpl;
3355 protected bool is_external_implementation;
3358 // The interface type we are explicitly implementing
3360 public Type InterfaceType;
3363 // The method we're overriding if this is an override method.
3365 protected MethodInfo base_method;
3367 readonly int explicit_mod_flags;
3368 public MethodAttributes flags;
3370 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3371 FullNamedExpression type, int mod, int allowed_mod, bool is_iface,
3372 MemberName name, Attributes attrs)
3373 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3376 IsInterface = is_iface;
3377 IsExplicitImpl = (MemberName.Left != null);
3378 explicit_mod_flags = mod;
3381 protected override bool CheckBase ()
3383 if (!base.CheckBase ())
3389 // Is null for System.Object while compiling corlib and base interfaces
3390 if (Parent.PartialContainer.BaseCache == null) {
3391 if ((ModFlags & Modifiers.NEW) != 0) {
3392 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3397 Type base_ret_type = null;
3398 base_method = FindOutBaseMethod (ref base_ret_type);
3400 // method is override
3401 if (base_method != null) {
3402 if (!CheckMethodAgainstBase (base_ret_type))
3405 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3406 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3408 if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3409 Report.SymbolRelatedToPreviousError (base_method);
3410 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3411 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3414 if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3415 Report.SymbolRelatedToPreviousError (base_method);
3416 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3417 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3424 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3425 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3426 if (conflict_symbol != null) {
3427 Report.SymbolRelatedToPreviousError (conflict_symbol);
3429 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3430 else if (this is PropertyBase)
3431 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3433 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3435 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3436 GetSignatureForError (), SimpleName.GetMemberType (this));
3441 if (conflict_symbol == null) {
3442 if ((ModFlags & Modifiers.NEW) != 0) {
3443 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3448 if ((ModFlags & Modifiers.NEW) == 0) {
3449 if (this is Method && conflict_symbol is MethodBase)
3452 Report.SymbolRelatedToPreviousError (conflict_symbol);
3453 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3454 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3461 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3462 // that have been defined.
3464 // `name' is the user visible name for reporting errors (this is used to
3465 // provide the right name regarding method names and properties)
3467 bool CheckMethodAgainstBase (Type base_method_type)
3471 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3472 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3473 Report.Error (506, Location,
3474 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3475 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3479 // Now we check that the overriden method is not final
3481 if (base_method.IsFinal) {
3482 Report.SymbolRelatedToPreviousError (base_method);
3483 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3484 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3488 // Check that the permissions are not being changed
3490 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3491 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3493 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3494 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3498 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3499 Report.SymbolRelatedToPreviousError (base_method);
3500 if (this is PropertyBasedMember) {
3501 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3502 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3505 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3506 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3512 if ((ModFlags & Modifiers.NEW) == 0) {
3513 if ((ModFlags & Modifiers.OVERRIDE) == 0 && Name != "Finalize") {
3514 ModFlags |= Modifiers.NEW;
3515 Report.SymbolRelatedToPreviousError (base_method);
3516 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3517 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",
3518 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3520 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3521 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3525 if (base_method.IsAbstract && !IsInterface) {
3526 Report.SymbolRelatedToPreviousError (base_method);
3527 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3528 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3536 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3538 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3540 // when overriding protected internal, the method can be declared
3541 // protected internal only within the same assembly or assembly
3542 // which has InternalsVisibleTo
3544 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3545 return TypeManager.IsThisOrFriendAssembly (base_method.DeclaringType.Assembly);
3546 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3548 // if it's not "protected internal", it must be "protected"
3552 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3554 // protected within the same assembly - an error
3557 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3558 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3560 // protected ok, but other attributes differ - report an error
3566 return (thisp == base_classp);
3570 protected bool DefineParameters (Parameters parameters)
3572 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3574 if (!parameters.Resolve (rc))
3578 foreach (Parameter p in parameters.FixedParameters) {
3579 if (p.CheckAccessibility (this))
3582 Report.SymbolRelatedToPreviousError (p.ParameterType);
3583 if (this is Indexer)
3584 Report.Error (55, Location,
3585 "Inconsistent accessibility: parameter type `" +
3586 TypeManager.CSharpName (p.ParameterType) + "' is less " +
3587 "accessible than indexer `" + GetSignatureForError () + "'");
3588 else if (this is Operator)
3589 Report.Error (57, Location,
3590 "Inconsistent accessibility: parameter type `" +
3591 TypeManager.CSharpName (p.ParameterType) + "' is less " +
3592 "accessible than operator `" + GetSignatureForError () + "'");
3594 Report.Error (51, Location,
3595 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3596 TypeManager.CSharpName (p.ParameterType), GetSignatureForError ());
3602 protected override bool DoDefine()
3604 if (!base.DoDefine ())
3607 if (IsExplicitImpl) {
3608 TypeExpr texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3612 InterfaceType = texpr.Type;
3614 if (!InterfaceType.IsInterface) {
3615 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3619 if (!Parent.PartialContainer.VerifyImplements (this))
3626 protected bool DoDefineBase ()
3629 throw new InternalErrorException ();
3632 ModFlags = Modifiers.PUBLIC |
3633 Modifiers.ABSTRACT |
3634 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
3636 flags = MethodAttributes.Public |
3637 MethodAttributes.Abstract |
3638 MethodAttributes.HideBySig |
3639 MethodAttributes.NewSlot |
3640 MethodAttributes.Virtual;
3642 if (!Parent.PartialContainer.MethodModifiersValid (this))
3645 flags = Modifiers.MethodAttr (ModFlags);
3648 if (IsExplicitImpl) {
3649 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3650 if (iface_texpr == null)
3653 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3654 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3655 GetSignatureForError ());
3659 InterfaceType = iface_texpr.Type;
3661 if (!InterfaceType.IsInterface) {
3662 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3666 if (!Parent.PartialContainer.VerifyImplements (this))
3669 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3675 public override void Emit()
3677 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3678 // We are more strict than Microsoft and report CS0626 as error
3679 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3680 Report.Error (626, Location,
3681 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3682 GetSignatureForError ());
3688 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3690 Report.SymbolRelatedToPreviousError (base_method);
3691 string base_name = TypeManager.GetFullNameSignature (base_method);
3692 string this_name = GetSignatureForError ();
3693 if (suffix != null) {
3694 base_name += suffix;
3695 this_name += suffix;
3698 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3699 this_name, Modifiers.GetDescription (ma), base_name);
3702 protected static string Error722 {
3704 return "`{0}': static types cannot be used as return types";
3709 /// Gets base method and its return type
3711 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3714 // The "short" name of this property / indexer / event. This is the
3715 // name without the explicit interface.
3717 public string ShortName
3719 get { return MemberName.Name; }
3720 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3723 public string GetFullName (MemberName name)
3725 if (!IsExplicitImpl)
3728 return InterfaceType.FullName.Replace ('+', '.') + "." + name.Name;
3731 protected override bool VerifyClsCompliance ()
3733 if (!base.VerifyClsCompliance ()) {
3734 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3735 Report.Error (3010, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3738 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3739 Report.Error (3011, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3744 if (GenericMethod != null)
3745 GenericMethod.VerifyClsCompliance ();
3750 public override bool IsUsed
3752 get { return IsExplicitImpl || base.IsUsed; }
3757 public abstract class MethodOrOperator : MethodCore, IMethodData
3759 public MethodBuilder MethodBuilder;
3760 ReturnParameter return_attributes;
3761 ListDictionary declarative_security;
3762 protected MethodData MethodData;
3764 static string[] attribute_targets = new string [] { "method", "return" };
3766 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3767 int allowed_mod, bool is_interface, MemberName name,
3768 Attributes attrs, Parameters parameters)
3769 : base (parent, generic, type, mod, allowed_mod, is_interface, name,
3774 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3776 if (a.Target == AttributeTargets.ReturnValue) {
3777 if (return_attributes == null)
3778 return_attributes = new ReturnParameter (MethodBuilder, Location);
3780 return_attributes.ApplyAttributeBuilder (a, cb);
3784 if (a.IsInternalMethodImplAttribute) {
3785 is_external_implementation = true;
3788 if (a.Type == TypeManager.dllimport_type) {
3789 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3790 if ((ModFlags & extern_static) != extern_static) {
3791 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3793 is_external_implementation = true;
3796 if (a.IsValidSecurityAttribute ()) {
3797 if (declarative_security == null)
3798 declarative_security = new ListDictionary ();
3799 a.ExtractSecurityPermissionSet (declarative_security);
3803 if (MethodBuilder != null)
3804 MethodBuilder.SetCustomAttribute (cb);
3807 public override AttributeTargets AttributeTargets {
3809 return AttributeTargets.Method;
3813 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3815 return new EmitContext (
3816 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3819 protected bool DefineGenericMethod ()
3821 if (!DoDefineBase ())
3825 if (GenericMethod != null) {
3826 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3827 if (!GenericMethod.Define (MethodBuilder, block))
3835 public bool ResolveMembers ()
3837 if (!DefineGenericMethod ())
3843 public override bool Define ()
3848 if (!CheckAbstractAndExtern (block != null))
3851 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3852 for (int i = 0; i < Parameters.Count; ++i ) {
3853 if (Parameters.ParameterModifier (i) == Parameter.Modifier.OUT) {
3854 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3855 GetSignatureForError ());
3864 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0)
3865 Iterator.CreateIterator (this, Parent, ModFlags);
3867 if (IsPartialDefinition) {
3868 caching_flags &= ~Flags.Excluded_Undetected;
3869 caching_flags |= Flags.Excluded;
3870 // Add to member cache only when a partial method implementation is not there
3871 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3872 MethodBase mb = new PartialMethodDefinitionInfo (this);
3873 Parent.MemberCache.AddMember (mb, this);
3874 TypeManager.AddMethod (mb, this);
3880 MethodData = new MethodData (
3881 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3883 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
3886 MethodBuilder = MethodData.MethodBuilder;
3889 if (MethodBuilder.IsGenericMethod)
3890 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3893 Parent.MemberCache.AddMember (MethodBuilder, this);
3895 if (!TypeManager.IsGenericParameter (MemberType)) {
3896 if (MemberType.IsAbstract && MemberType.IsSealed) {
3897 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3905 public override void Emit ()
3908 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0)
3909 MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
3910 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3911 MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
3913 if (OptAttributes != null)
3914 OptAttributes.Emit ();
3916 if (declarative_security != null) {
3917 foreach (DictionaryEntry de in declarative_security) {
3918 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3925 protected void Error_ConditionalAttributeIsNotValid ()
3927 Report.Error (577, Location,
3928 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3929 GetSignatureForError ());
3932 public bool IsPartialDefinition {
3934 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3938 public bool IsPartialImplementation {
3940 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3944 public override string[] ValidAttributeTargets {
3946 return attribute_targets;
3950 #region IMethodData Members
3952 public CallingConventions CallingConventions {
3954 CallingConventions cc = Parameters.CallingConvention;
3956 if ((ModFlags & Modifiers.STATIC) == 0)
3957 cc |= CallingConventions.HasThis;
3959 // FIXME: How is `ExplicitThis' used in C#?
3965 public Type ReturnType {
3971 public MemberName MethodName {
3977 protected override bool CheckBase ()
3979 if (!base.CheckBase ())
3982 // TODO: Destructor should derive from MethodCore
3983 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3984 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3985 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3993 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3995 public bool IsExcluded () {
3996 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3997 return (caching_flags & Flags.Excluded) != 0;
3999 caching_flags &= ~Flags.Excluded_Undetected;
4001 if (base_method == null) {
4002 if (OptAttributes == null)
4005 if (TypeManager.conditional_attribute_type == null)
4008 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
4013 foreach (Attribute a in attrs) {
4014 string condition = a.GetConditionalAttributeValue ();
4015 if (condition == null)
4018 if (RootContext.AllDefines.Contains (condition))
4022 caching_flags |= Flags.Excluded;
4026 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
4028 if (AttributeTester.IsConditionalMethodExcluded (base_method)) {
4029 caching_flags |= Flags.Excluded;
4035 if (md.IsExcluded ()) {
4036 caching_flags |= Flags.Excluded;
4042 GenericMethod IMethodData.GenericMethod {
4044 return GenericMethod;
4048 public virtual void EmitExtraSymbolInfo (SourceMethod source)
4055 public class SourceMethod : IMethodDef
4059 SourceMethodBuilder builder;
4061 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
4063 this.parent = parent;
4064 this.method = method;
4066 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
4069 public string Name {
4070 get { return method.Name; }
4075 if (method is MethodBuilder)
4076 return ((MethodBuilder) method).GetToken ().Token;
4077 else if (method is ConstructorBuilder)
4078 return ((ConstructorBuilder) method).GetToken ().Token;
4080 throw new NotSupportedException ();
4084 public void CloseMethod ()
4086 SymbolWriter.CloseMethod ();
4089 public void SetRealMethodName (string name)
4091 if (builder != null)
4092 builder.SetRealMethodName (name);
4095 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4097 if (!SymbolWriter.HasSymbolWriter)
4102 Location start_loc = block.StartLocation;
4103 if (start_loc.IsNull)
4106 ICompileUnit compile_unit = start_loc.CompilationUnit;
4107 if (compile_unit == null)
4110 return new SourceMethod (parent, method, compile_unit);
4114 public class Method : MethodOrOperator, IAnonymousHost {
4117 /// Modifiers allowed in a class declaration
4119 const int AllowedModifiers =
4122 Modifiers.PROTECTED |
4123 Modifiers.INTERNAL |
4128 Modifiers.OVERRIDE |
4129 Modifiers.ABSTRACT |
4131 Modifiers.METHOD_YIELDS |
4134 const int AllowedInterfaceModifiers =
4135 Modifiers.NEW | Modifiers.UNSAFE;
4138 // return_type can be "null" for VOID values.
4140 public Method (DeclSpace parent, GenericMethod generic,
4141 FullNamedExpression return_type, int mod, bool is_iface,
4142 MemberName name, Parameters parameters, Attributes attrs)
4143 : base (parent, generic, return_type, mod,
4144 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
4145 is_iface, name, attrs, parameters)
4149 public override string GetSignatureForError()
4151 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4154 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
4156 Report.Error (17, location,
4157 "Program `{0}' has more than one entry point defined: `{1}'",
4158 CodeGen.FileName, TypeManager.CSharpSignature(b));
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.ParameterType (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 void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4181 if (a.Type == TypeManager.conditional_attribute_type) {
4182 if (IsExplicitImpl) {
4183 Error_ConditionalAttributeIsNotValid ();
4187 if (ReturnType != TypeManager.void_type) {
4188 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4192 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4193 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4198 Report.Error (582, Location, "Conditional not valid on interface members");
4202 if (MethodData.implementing != null) {
4203 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4204 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4205 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4209 for (int i = 0; i < Parameters.Count; ++i) {
4210 if ((Parameters.ParameterModifier (i) & Parameter.Modifier.OUTMASK) != 0) {
4211 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4217 if (a.Type == TypeManager.extension_attribute_type) {
4218 a.Error_MisusedExtensionAttribute ();
4222 base.ApplyAttributeBuilder (a, cb);
4225 public bool CheckForDuplications ()
4227 ArrayList ar = Parent.PartialContainer.Properties;
4229 for (int i = 0; i < ar.Count; ++i) {
4230 PropertyBase pb = (PropertyBase) ar [i];
4231 if (pb.AreAccessorsDuplicateImplementation (this))
4236 ar = Parent.PartialContainer.Indexers;
4238 for (int i = 0; i < ar.Count; ++i) {
4239 PropertyBase pb = (PropertyBase) ar [i];
4240 if (pb.AreAccessorsDuplicateImplementation (this))
4251 public override bool Define ()
4253 if (!base.Define ())
4256 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
4257 Error1599 (Location, ReturnType);
4261 if (ReturnType == TypeManager.void_type && ParameterTypes.Length == 0 &&
4262 Name == "Finalize" && !(this is Destructor)) {
4263 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4266 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4267 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4268 Parent.PartialContainer.Mark_HasEquals ();
4269 else if (Parameters.Empty && Name == "GetHashCode")
4270 Parent.PartialContainer.Mark_HasGetHashCode ();
4273 if ((ModFlags & Modifiers.STATIC) == 0)
4276 if (Parameters.HasExtensionMethodType) {
4277 if (Parent.IsStaticClass && !Parent.IsGeneric) {
4278 if (!Parent.IsTopLevel)
4279 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4280 GetSignatureForError ());
4282 if (TypeManager.extension_attribute_type == null) {
4283 Report.Error (1110, Location,
4284 "`{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",
4285 GetSignatureForError ());
4286 } else if (TypeManager.extension_attribute_attr == null) {
4287 ConstructorInfo ci = TypeManager.GetPredefinedConstructor (
4288 TypeManager.extension_attribute_type, Location, System.Type.EmptyTypes);
4291 TypeManager.extension_attribute_attr = new CustomAttributeBuilder (ci, new object [0]);
4294 ModFlags |= Modifiers.METHOD_EXTENSION;
4295 Parent.ModFlags |= Modifiers.METHOD_EXTENSION;
4296 CodeGen.Assembly.HasExtensionMethods = true;
4298 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4299 GetSignatureForError ());
4304 // This is used to track the Entry Point,
4306 if (RootContext.NeedsEntryPoint &&
4308 (RootContext.MainClass == null ||
4309 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4310 if (IsEntryPoint ()) {
4312 if (RootContext.EntryPoint == null) {
4313 if (Parent.IsGeneric || MemberName.IsGeneric) {
4314 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4315 GetSignatureForError ());
4317 IMethodData md = TypeManager.GetMethod (MethodBuilder);
4318 md.SetMemberIsUsed ();
4320 RootContext.EntryPoint = MethodBuilder;
4321 RootContext.EntryPointLocation = Location;
4324 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
4325 Error_DuplicateEntryPoint (MethodBuilder, Location);
4328 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4329 GetSignatureForError ());
4339 public override void Emit ()
4342 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4343 if (IsPartialDefinition) {
4345 // Do attribute checks only when partial implementation does not exist
4347 if (MethodBuilder == null)
4353 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
4354 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4355 GetSignatureForError ());
4357 MethodData.Emit (Parent);
4361 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4362 MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
4368 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4369 Location, MethodBuilder);
4374 public override bool EnableOverloadChecks (MemberCore overload)
4376 // TODO: It can be deleted when members will be defined in correct order
4377 if (overload is Operator)
4378 return overload.EnableOverloadChecks (this);
4380 if (overload is Indexer)
4383 return base.EnableOverloadChecks (overload);
4386 public static void Error1599 (Location loc, Type t)
4388 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4391 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4393 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4394 Parent.TypeBuilder, Name, ParameterTypes, GenericMethod, false);
4399 if (mi.IsSpecialName)
4402 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4406 public void SetPartialDefinition (Method methodDefinition)
4408 caching_flags |= Flags.PartialDefinitionExists;
4409 methodDefinition.MethodBuilder = MethodBuilder;
4410 if (methodDefinition.attributes == null)
4413 if (attributes == null) {
4414 attributes = methodDefinition.attributes;
4416 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4420 protected override bool VerifyClsCompliance ()
4422 if (!base.VerifyClsCompliance ())
4425 if (ParameterInfo.Count > 0) {
4426 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4428 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4435 public abstract class ConstructorInitializer : ExpressionStatement
4437 ArrayList argument_list;
4438 MethodGroupExpr base_constructor_group;
4440 public ConstructorInitializer (ArrayList argument_list, Location loc)
4442 this.argument_list = argument_list;
4446 public ArrayList Arguments {
4448 return argument_list;
4452 public override Expression CreateExpressionTree (EmitContext ec)
4454 throw new NotSupportedException ("ET");
4457 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4459 if (argument_list != null){
4460 foreach (Argument a in argument_list){
4461 if (!a.Resolve (ec, loc))
4466 if (this is ConstructorBaseInitializer) {
4467 if (ec.ContainerType.BaseType == null)
4470 type = ec.ContainerType.BaseType;
4471 if (ec.ContainerType.IsValueType) {
4472 Report.Error (522, loc,
4473 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4478 // It is legal to have "this" initializers that take no arguments
4479 // in structs, they are just no-ops.
4481 // struct D { public D (int a) : this () {}
4483 if (ec.ContainerType.IsValueType && argument_list == null)
4486 type = ec.ContainerType;
4489 base_constructor_group = MemberLookupFinal (
4490 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4491 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4492 loc) as MethodGroupExpr;
4494 if (base_constructor_group == null)
4497 base_constructor_group = base_constructor_group.OverloadResolve (
4498 ec, ref argument_list, false, loc);
4500 if (base_constructor_group == null)
4503 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4505 if (base_ctor == caller_builder){
4506 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4512 public override Expression DoResolve (EmitContext ec)
4514 throw new NotSupportedException ();
4517 public override void Emit (EmitContext ec)
4519 // It can be null for static initializers
4520 if (base_constructor_group == null)
4523 ec.Mark (loc, false);
4525 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4527 base_constructor_group.EmitCall (ec, argument_list);
4530 public override void EmitStatement (EmitContext ec)
4536 public class ConstructorBaseInitializer : ConstructorInitializer {
4537 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4538 base (argument_list, l)
4543 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4544 public GeneratedBaseInitializer (Location loc):
4550 public class ConstructorThisInitializer : ConstructorInitializer {
4551 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4552 base (argument_list, l)
4557 public class Constructor : MethodCore, IMethodData, IAnonymousHost {
4558 public ConstructorBuilder ConstructorBuilder;
4559 public ConstructorInitializer Initializer;
4560 ListDictionary declarative_security;
4561 bool has_compliant_args;
4564 // Modifiers allowed for a constructor.
4566 public const int AllowedModifiers =
4568 Modifiers.PROTECTED |
4569 Modifiers.INTERNAL |
4575 static readonly string[] attribute_targets = new string [] { "method" };
4578 // The spec claims that static is not permitted, but
4579 // my very own code has static constructors.
4581 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4582 ConstructorInitializer init, Location loc)
4583 : base (parent, null, null, mod, AllowedModifiers, false,
4584 new MemberName (name, loc), null, args)
4589 public bool HasCompliantArgs {
4590 get { return has_compliant_args; }
4593 public override AttributeTargets AttributeTargets {
4594 get { return AttributeTargets.Constructor; }
4598 // Returns true if this is a default constructor
4600 public bool IsDefault ()
4602 if ((ModFlags & Modifiers.STATIC) != 0)
4603 return Parameters.Empty;
4605 return Parameters.Empty &&
4606 (Initializer is ConstructorBaseInitializer) &&
4607 (Initializer.Arguments == null);
4610 public bool IsCompilerGenerated {
4611 get { return Initializer is GeneratedBaseInitializer; }
4614 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4616 if (a.IsValidSecurityAttribute ()) {
4617 if (declarative_security == null) {
4618 declarative_security = new ListDictionary ();
4620 a.ExtractSecurityPermissionSet (declarative_security);
4624 if (a.IsInternalMethodImplAttribute) {
4625 is_external_implementation = true;
4628 ConstructorBuilder.SetCustomAttribute (cb);
4631 protected override bool CheckBase ()
4633 if ((ModFlags & Modifiers.STATIC) != 0) {
4634 if (!Parameters.Empty) {
4635 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4636 GetSignatureForError ());
4640 // the rest can be ignored
4644 // Check whether arguments were correct.
4645 if (!DefineParameters (Parameters))
4648 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4649 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorBuilder.ConstructorName,
4652 if (Parent.PartialContainer.Kind == Kind.Struct) {
4653 if (ParameterTypes.Length == 0) {
4654 Report.Error (568, Location,
4655 "Structs cannot contain explicit parameterless constructors");
4660 CheckProtectedModifier ();
4666 // Creates the ConstructorBuilder
4668 public override bool Define ()
4670 if (ConstructorBuilder != null)
4673 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4674 MethodAttributes.SpecialName);
4676 if ((ModFlags & Modifiers.STATIC) != 0) {
4677 ca |= MethodAttributes.Static | MethodAttributes.Private;
4679 ca |= MethodAttributes.HideBySig;
4681 if ((ModFlags & Modifiers.PUBLIC) != 0)
4682 ca |= MethodAttributes.Public;
4683 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4684 if ((ModFlags & Modifiers.INTERNAL) != 0)
4685 ca |= MethodAttributes.FamORAssem;
4687 ca |= MethodAttributes.Family;
4688 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4689 ca |= MethodAttributes.Assembly;
4691 ca |= MethodAttributes.Private;
4694 if (!CheckAbstractAndExtern (block != null))
4697 // Check if arguments were correct.
4701 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4702 ca, CallingConventions,
4705 if ((ModFlags & Modifiers.UNSAFE) != 0)
4706 ConstructorBuilder.InitLocals = false;
4708 if (Parent.PartialContainer.IsComImport) {
4709 if (!IsDefault ()) {
4710 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4711 Parent.GetSignatureForError ());
4714 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4717 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4718 TypeManager.AddMethod (ConstructorBuilder, this);
4720 // It's here only to report an error
4721 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0) {
4722 member_type = TypeManager.void_type;
4723 Iterator.CreateIterator (this, Parent, ModFlags);
4732 public override void Emit ()
4734 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4735 ConstructorBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
4737 if (OptAttributes != null)
4738 OptAttributes.Emit ();
4742 EmitContext ec = CreateEmitContext (null, null);
4745 // If we use a "this (...)" constructor initializer, then
4746 // do not emit field initializers, they are initialized in the other constructor
4748 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4749 !(Initializer is ConstructorThisInitializer);
4751 if (emit_field_initializers)
4752 Parent.PartialContainer.ResolveFieldInitializers (ec);
4754 if (block != null) {
4755 // If this is a non-static `struct' constructor and doesn't have any
4756 // initializer, it must initialize all of the struct's fields.
4757 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4758 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4759 block.AddThisVariable (Parent, Location);
4761 if (!block.ResolveMeta (ec, ParameterInfo))
4764 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4765 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4766 Initializer = new GeneratedBaseInitializer (Location);
4769 // Spec mandates that Initializers will not have `this' access
4771 if (Initializer != null) {
4773 Initializer.Resolve (ConstructorBuilder, ec);
4774 ec.IsStatic = false;
4775 block.AddScopeStatement (new StatementExpression (Initializer));
4780 Parameters.ApplyAttributes (ConstructorBuilder);
4782 SourceMethod source = null;
4784 ec.OmitDebuggingInfo = true;
4786 source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4788 bool unreachable = false;
4789 if (block != null) {
4790 if (!ec.ResolveTopBlock (null, block, ParameterInfo, this, out unreachable))
4793 ec.EmitMeta (block);
4795 if (Report.Errors > 0)
4798 if (emit_field_initializers)
4799 Parent.PartialContainer.EmitFieldInitializers (ec);
4801 ec.EmitResolvedTopBlock (block, unreachable);
4805 source.CloseMethod ();
4807 if (declarative_security != null) {
4808 foreach (DictionaryEntry de in declarative_security) {
4809 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4816 // Is never override
4817 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4822 public override string GetSignatureForError()
4824 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4827 public override string[] ValidAttributeTargets {
4829 return attribute_targets;
4833 protected override bool VerifyClsCompliance ()
4835 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4839 if (ParameterInfo.Count > 0) {
4840 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4842 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4844 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4845 foreach (Type param in ParameterTypes) {
4846 if (param.IsArray) {
4852 has_compliant_args = true;
4856 #region IMethodData Members
4858 public System.Reflection.CallingConventions CallingConventions {
4860 CallingConventions cc = Parameters.CallingConvention;
4862 if (Parent.PartialContainer.Kind == Kind.Class)
4863 if ((ModFlags & Modifiers.STATIC) == 0)
4864 cc |= CallingConventions.HasThis;
4866 // FIXME: How is `ExplicitThis' used in C#?
4872 public MemberName MethodName {
4878 public Type ReturnType {
4884 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4886 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4887 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
4888 ec.CurrentBlock = block;
4892 public bool IsExcluded()
4897 GenericMethod IMethodData.GenericMethod {
4903 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4910 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4912 public interface IMethodData
4914 CallingConventions CallingConventions { get; }
4915 Location Location { get; }
4916 MemberName MethodName { get; }
4917 Type ReturnType { get; }
4918 GenericMethod GenericMethod { get; }
4919 Parameters ParameterInfo { get; }
4921 Attributes OptAttributes { get; }
4922 ToplevelBlock Block { get; set; }
4924 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4925 ObsoleteAttribute GetObsoleteAttribute ();
4926 string GetSignatureForError ();
4928 bool IsClsComplianceRequired ();
4929 void SetMemberIsUsed ();
4930 void EmitExtraSymbolInfo (SourceMethod source);
4934 // Encapsulates most of the Method's state
4936 public class MethodData {
4938 static FieldInfo methodbuilder_attrs_field;
4940 readonly IMethodData method;
4942 public readonly GenericMethod GenericMethod;
4945 // Are we implementing an interface ?
4947 public MethodInfo implementing;
4952 protected InterfaceMemberBase member;
4953 protected int modifiers;
4954 protected MethodAttributes flags;
4955 protected Type declaring_type;
4956 protected MethodInfo parent_method;
4958 MethodBuilder builder = null;
4959 public MethodBuilder MethodBuilder {
4965 public Type DeclaringType {
4967 return declaring_type;
4971 public MethodData (InterfaceMemberBase member,
4972 int modifiers, MethodAttributes flags, IMethodData method)
4974 this.member = member;
4975 this.modifiers = modifiers;
4978 this.method = method;
4981 public MethodData (InterfaceMemberBase member,
4982 int modifiers, MethodAttributes flags,
4983 IMethodData method, MethodBuilder builder,
4984 GenericMethod generic, MethodInfo parent_method)
4985 : this (member, modifiers, flags, method)
4987 this.builder = builder;
4988 this.GenericMethod = generic;
4989 this.parent_method = parent_method;
4992 public bool Define (DeclSpace parent, string method_full_name)
4994 string name = method.MethodName.Basename;
4996 TypeContainer container = parent.PartialContainer;
4998 PendingImplementation pending = container.PendingImplementations;
4999 if (pending != null){
5000 if (member is Indexer) // TODO: test it, but it should work without this IF
5001 implementing = pending.IsInterfaceIndexer (
5002 member.InterfaceType, method.ReturnType, method.ParameterInfo);
5004 implementing = pending.IsInterfaceMethod (
5005 member.InterfaceType, name, method.ReturnType, method.ParameterInfo);
5007 if (member.InterfaceType != null){
5008 if (implementing == null){
5009 if (member is PropertyBase) {
5010 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
5011 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
5012 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
5015 Report.Error (539, method.Location,
5016 "`{0}.{1}' in explicit interface declaration is not a member of interface",
5017 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
5021 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
5022 Report.SymbolRelatedToPreviousError (implementing);
5023 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
5024 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
5028 if (implementing != null) {
5029 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
5030 if (prop_method == null) {
5031 if (TypeManager.IsSpecialMethod (implementing)) {
5032 Report.SymbolRelatedToPreviousError (implementing);
5033 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
5034 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
5035 implementing.Name.StartsWith ("get_") ? "get" : "set");
5037 } else if (implementing.DeclaringType.IsInterface) {
5038 if (!implementing.IsSpecialName) {
5039 Report.SymbolRelatedToPreviousError (implementing);
5040 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
5041 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
5044 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
5045 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
5046 Report.SymbolRelatedToPreviousError (implementing);
5047 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
5048 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
5057 // For implicit implementations, make sure we are public, for
5058 // explicit implementations, make sure we are private.
5060 if (implementing != null){
5062 // Setting null inside this block will trigger a more
5063 // verbose error reporting for missing interface implementations
5065 // The "candidate" function has been flagged already
5066 // but it wont get cleared
5068 if (member.IsExplicitImpl){
5069 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
5070 Report.SymbolRelatedToPreviousError (implementing);
5071 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
5072 method.GetSignatureForError ());
5076 if (implementing.DeclaringType.IsInterface) {
5078 // If this is an interface method implementation,
5079 // check for public accessibility
5081 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
5083 implementing = null;
5085 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
5086 // We may never be private.
5087 implementing = null;
5089 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5091 // We may be protected if we're overriding something.
5093 implementing = null;
5098 // Static is not allowed
5100 if ((modifiers & Modifiers.STATIC) != 0){
5101 implementing = null;
5106 // If implementing is still valid, set flags
5108 if (implementing != null){
5110 // When implementing interface methods, set NewSlot
5111 // unless, we are overwriting a method.
5113 if (implementing.DeclaringType.IsInterface){
5114 if ((modifiers & Modifiers.OVERRIDE) == 0)
5115 flags |= MethodAttributes.NewSlot;
5118 MethodAttributes.Virtual |
5119 MethodAttributes.HideBySig;
5121 // Set Final unless we're virtual, abstract or already overriding a method.
5122 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5123 flags |= MethodAttributes.Final;
5126 DefineMethodBuilder (container, method_full_name, method.ParameterInfo.Types);
5128 if (builder == null)
5131 if (container.CurrentType != null)
5132 declaring_type = container.CurrentType;
5134 declaring_type = container.TypeBuilder;
5136 if ((modifiers & Modifiers.UNSAFE) != 0)
5137 builder.InitLocals = false;
5140 if (implementing != null){
5142 // clear the pending implemntation flag
5144 if (member is Indexer) {
5145 pending.ImplementIndexer (
5146 member.InterfaceType, builder, method.ReturnType,
5147 method.ParameterInfo, member.IsExplicitImpl);
5149 pending.ImplementMethod (
5150 member.InterfaceType, name, method.ReturnType,
5151 method.ParameterInfo, member.IsExplicitImpl);
5153 if (member.IsExplicitImpl)
5154 container.TypeBuilder.DefineMethodOverride (
5155 builder, implementing);
5158 TypeManager.AddMethod (builder, method);
5160 if (GenericMethod != null) {
5161 bool is_override = member.IsExplicitImpl |
5162 ((modifiers & Modifiers.OVERRIDE) != 0);
5164 if (implementing != null)
5165 parent_method = implementing;
5167 EmitContext ec = method.CreateEmitContext (container, null);
5168 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5177 /// Create the MethodBuilder for the method
5179 void DefineMethodBuilder (TypeContainer container, string method_name, Type[] ParameterTypes)
5181 if (builder == null) {
5182 builder = container.TypeBuilder.DefineMethod (
5183 method_name, flags, method.CallingConventions,
5184 method.ReturnType, ParameterTypes);
5190 // Generic method has been already defined to resolve method parameters
5191 // correctly when they use type parameters
5193 builder.SetParameters (ParameterTypes);
5194 builder.SetReturnType (method.ReturnType);
5196 if (builder.Attributes != flags) {
5198 if (methodbuilder_attrs_field == null)
5199 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5200 methodbuilder_attrs_field.SetValue (builder, flags);
5202 Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5206 throw new InternalErrorException ();
5213 public void Emit (DeclSpace parent)
5215 ToplevelBlock block = method.Block;
5219 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5221 ec = method.CreateEmitContext (parent, null);
5223 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5225 if (GenericMethod != null)
5226 GenericMethod.EmitAttributes ();
5228 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5231 // Handle destructors specially
5233 // FIXME: This code generates buggy code
5235 if (member is Destructor)
5236 EmitDestructor (ec, block);
5238 ec.EmitTopBlock (method, block);
5240 if (source != null) {
5241 method.EmitExtraSymbolInfo (source);
5242 source.CloseMethod ();
5246 void EmitDestructor (EmitContext ec, ToplevelBlock block)
5248 ILGenerator ig = ec.ig;
5250 Label finish = ig.DefineLabel ();
5252 block.SetDestructor ();
5254 ig.BeginExceptionBlock ();
5255 ec.ReturnLabel = finish;
5256 ec.HasReturnLabel = true;
5257 ec.EmitTopBlock (method, block);
5259 // ig.MarkLabel (finish);
5260 ig.BeginFinallyBlock ();
5262 if (ec.ContainerType.BaseType != null) {
5263 Expression member_lookup = Expression.MemberLookup (
5264 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
5265 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
5267 if (member_lookup != null){
5268 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
5270 ig.Emit (OpCodes.Ldarg_0);
5271 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
5275 ig.EndExceptionBlock ();
5276 //ig.MarkLabel (ec.ReturnLabel);
5277 ig.Emit (OpCodes.Ret);
5281 // TODO: Should derive from MethodCore
5282 public class Destructor : Method {
5284 static string[] attribute_targets = new string [] { "method" };
5286 public Destructor (DeclSpace parent, FullNamedExpression return_type, int mod,
5287 string name, Parameters parameters, Attributes attrs,
5289 : base (parent, null, return_type, mod, false, new MemberName (name, l),
5293 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5295 if (a.Type == TypeManager.conditional_attribute_type) {
5296 Error_ConditionalAttributeIsNotValid ();
5300 base.ApplyAttributeBuilder (a, cb);
5303 public override string GetSignatureForError ()
5305 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5308 public override string[] ValidAttributeTargets {
5310 return attribute_targets;
5315 abstract public class MemberBase : MemberCore {
5316 protected FullNamedExpression type_name;
5317 public readonly DeclSpace ds;
5318 public readonly GenericMethod GenericMethod;
5321 // The type of this property / indexer / event
5323 protected Type member_type;
5324 public Type MemberType {
5326 // TODO: Who wrote this, ResolveAsTypeTerminal can have side effects
5327 if (member_type == null && type_name != null) {
5328 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
5329 type_name = type_name.ResolveAsTypeTerminal (rc, false);
5330 if (type_name != null) {
5331 member_type = type_name.Type;
5339 // The constructor is only exposed to our children
5341 protected MemberBase (DeclSpace parent, GenericMethod generic,
5342 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5343 MemberName name, Attributes attrs)
5344 : base (parent, name, attrs)
5346 this.ds = generic != null ? generic : (DeclSpace) parent;
5347 this.type_name = type;
5348 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5349 GenericMethod = generic;
5350 if (GenericMethod != null)
5351 GenericMethod.ModFlags = ModFlags;
5354 protected virtual bool CheckBase ()
5356 CheckProtectedModifier ();
5361 protected virtual bool DoDefine ()
5363 if (MemberType == null)
5366 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5367 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5368 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5369 GetSignatureForError (), Parent.GetSignatureForError ());
5373 // verify accessibility
5374 if (!IsAccessibleAs (MemberType)) {
5375 Report.SymbolRelatedToPreviousError (MemberType);
5376 if (this is Property)
5377 Report.Error (53, Location,
5378 "Inconsistent accessibility: property type `" +
5379 TypeManager.CSharpName (MemberType) + "' is less " +
5380 "accessible than property `" + GetSignatureForError () + "'");
5381 else if (this is Indexer)
5382 Report.Error (54, Location,
5383 "Inconsistent accessibility: indexer return type `" +
5384 TypeManager.CSharpName (MemberType) + "' is less " +
5385 "accessible than indexer `" + GetSignatureForError () + "'");
5386 else if (this is MethodCore) {
5387 if (this is Operator)
5388 Report.Error (56, Location,
5389 "Inconsistent accessibility: return type `" +
5390 TypeManager.CSharpName (MemberType) + "' is less " +
5391 "accessible than operator `" + GetSignatureForError () + "'");
5393 Report.Error (50, Location,
5394 "Inconsistent accessibility: return type `" +
5395 TypeManager.CSharpName (MemberType) + "' is less " +
5396 "accessible than method `" + GetSignatureForError () + "'");
5398 Report.Error (52, Location,
5399 "Inconsistent accessibility: field type `" +
5400 TypeManager.CSharpName (MemberType) + "' is less " +
5401 "accessible than field `" + GetSignatureForError () + "'");
5409 protected bool IsTypePermitted ()
5411 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5412 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5420 // Abstract class for all fields
5422 abstract public class FieldBase : MemberBase {
5423 public FieldBuilder FieldBuilder;
5424 public Status status;
5425 protected Expression initializer;
5428 public enum Status : byte {
5429 HAS_OFFSET = 4 // Used by FieldMember.
5432 static readonly string[] attribute_targets = new string [] { "field" };
5434 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5435 int allowed_mod, MemberName name, Attributes attrs)
5436 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5439 if ((mod & Modifiers.ABSTRACT) != 0)
5440 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5443 public override AttributeTargets AttributeTargets {
5445 return AttributeTargets.Field;
5449 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5451 if (a.Type == TypeManager.field_offset_attribute_type) {
5452 status |= Status.HAS_OFFSET;
5454 if (!Parent.PartialContainer.HasExplicitLayout) {
5455 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5459 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5460 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5466 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5467 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5472 if (a.Type == TypeManager.marshal_as_attr_type) {
5473 UnmanagedMarshal marshal = a.GetMarshal (this);
5474 if (marshal != null) {
5475 FieldBuilder.SetMarshal (marshal);
5480 if ((a.HasSecurityAttribute)) {
5481 a.Error_InvalidSecurityParent ();
5485 FieldBuilder.SetCustomAttribute (cb);
5488 protected override bool CheckBase ()
5490 if (!base.CheckBase ())
5493 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5494 if (conflict_symbol == null) {
5495 if ((ModFlags & Modifiers.NEW) != 0) {
5496 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5501 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5502 Report.SymbolRelatedToPreviousError (conflict_symbol);
5503 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5504 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5510 public override bool Define()
5512 if (MemberType == null || member_type == null)
5515 if (TypeManager.IsGenericParameter (MemberType))
5518 if (MemberType == TypeManager.void_type) {
5519 // TODO: wrong location
5520 Expression.Error_VoidInvalidInTheContext (Location);
5524 if (MemberType.IsSealed && MemberType.IsAbstract) {
5525 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5535 if (!IsTypePermitted ())
5542 // Represents header string for documentation comment.
5544 public override string DocCommentHeader {
5545 get { return "F:"; }
5548 public override void Emit ()
5551 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0) {
5552 FieldBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
5556 if (OptAttributes != null) {
5557 OptAttributes.Emit ();
5560 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5561 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5567 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5569 Report.SymbolRelatedToPreviousError (static_class);
5570 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5574 public Expression Initializer {
5576 if (value != null) {
5577 this.initializer = value;
5582 protected virtual bool IsFieldClsCompliant {
5584 if (FieldBuilder == null)
5587 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5591 public override string[] ValidAttributeTargets
5594 return attribute_targets;
5598 protected override bool VerifyClsCompliance ()
5600 if (!base.VerifyClsCompliance ())
5603 if (!IsFieldClsCompliant) {
5604 Report.Error (3003, Location, "Type of `{0}' is not CLS-compliant", GetSignatureForError ());
5609 public void SetAssigned ()
5611 caching_flags |= Flags.IsAssigned;
5615 interface IFixedBuffer
5617 FieldInfo Element { get; }
5618 Type ElementType { get; }
5621 public class FixedFieldExternal: IFixedBuffer
5623 FieldInfo element_field;
5625 public FixedFieldExternal (FieldInfo fi)
5627 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5630 #region IFixedField Members
5632 public FieldInfo Element {
5634 return element_field;
5638 public Type ElementType {
5640 return element_field.FieldType;
5648 /// Fixed buffer implementation
5650 public class FixedField : FieldBase, IFixedBuffer
5652 public const string FixedElementName = "FixedElementField";
5653 static int GlobalCounter = 0;
5654 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5655 static FieldInfo[] fi;
5657 TypeBuilder fixed_buffer_type;
5658 FieldBuilder element;
5659 Expression size_expr;
5661 const int AllowedModifiers =
5664 Modifiers.PROTECTED |
5665 Modifiers.INTERNAL |
5668 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5669 Expression size_expr, Attributes attrs, Location loc):
5670 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5672 if (RootContext.Version == LanguageVersion.ISO_1)
5673 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5675 this.size_expr = size_expr;
5678 public override bool Define()
5680 if (!Parent.IsInUnsafeScope)
5681 Expression.UnsafeError (Location);
5683 if (!base.Define ())
5686 if (!TypeManager.IsPrimitiveType (MemberType)) {
5687 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",
5688 GetSignatureForError ());
5691 // Create nested fixed buffer container
5692 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5693 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5694 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5696 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5697 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5699 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5700 Parent.MemberCache.AddMember (FieldBuilder, this);
5701 TypeManager.RegisterFieldBase (FieldBuilder, this);
5706 public override void Emit()
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 ());
5713 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5714 Constant c = size_expr.ResolveAsConstant (ec, this);
5718 IntConstant buffer_size_const = c.ImplicitConversionRequired (TypeManager.int32_type, Location) as IntConstant;
5719 if (buffer_size_const == null)
5722 int buffer_size = buffer_size_const.Value;
5724 if (buffer_size <= 0) {
5725 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5729 int type_size = Expression.GetTypeSize (MemberType);
5731 if (buffer_size > int.MaxValue / type_size) {
5732 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5733 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5737 buffer_size *= type_size;
5738 EmitFieldSize (buffer_size);
5742 void EmitFieldSize (int buffer_size)
5744 if (TypeManager.struct_layout_attribute_type == null) {
5745 TypeManager.struct_layout_attribute_type = TypeManager.CoreLookupType (
5746 "System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, true);
5748 if (TypeManager.struct_layout_attribute_type == null)
5753 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5755 object [] fi_val = new object [] { buffer_size };
5757 if (TypeManager.struct_layout_attribute_ctor == null) {
5758 TypeManager.struct_layout_attribute_ctor = TypeManager.GetPredefinedConstructor (
5759 TypeManager.struct_layout_attribute_type, Location, TypeManager.short_type);
5760 if (TypeManager.struct_layout_attribute_ctor == null)
5764 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5765 ctor_args, fi, fi_val);
5766 fixed_buffer_type.SetCustomAttribute (cab);
5769 // Don't emit FixedBufferAttribute attribute for private types
5771 if ((ModFlags & Modifiers.PRIVATE) != 0)
5774 if (TypeManager.fixed_buffer_attr_ctor == null) {
5775 if (TypeManager.fixed_buffer_attr_type == null) {
5776 TypeManager.fixed_buffer_attr_type = TypeManager.CoreLookupType (
5777 "System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, true);
5779 if (TypeManager.fixed_buffer_attr_type == null)
5783 TypeManager.fixed_buffer_attr_ctor = TypeManager.GetPredefinedConstructor (TypeManager.fixed_buffer_attr_type,
5784 Location, TypeManager.type_type, TypeManager.int32_type);
5786 if (TypeManager.fixed_buffer_attr_ctor == null)
5790 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object [] { MemberType, buffer_size });
5791 FieldBuilder.SetCustomAttribute (cab);
5794 protected override bool IsFieldClsCompliant {
5800 #region IFixedField Members
5802 public FieldInfo Element {
5808 public Type ElementType {
5818 // The Field class is used to represents class/struct fields during parsing.
5820 public class Field : FieldBase {
5822 // Modifiers allowed in a class declaration
5824 const int AllowedModifiers =
5827 Modifiers.PROTECTED |
5828 Modifiers.INTERNAL |
5831 Modifiers.VOLATILE |
5835 public Field (DeclSpace parent, FullNamedExpression type, int mod, string name,
5836 Attributes attrs, Location loc)
5837 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5842 bool CanBeVolatile ()
5844 if (TypeManager.IsReferenceType (MemberType))
5847 if (MemberType.IsEnum)
5850 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5851 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5852 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5853 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5854 MemberType == TypeManager.float_type)
5860 public override bool Define ()
5862 if (!base.Define ())
5865 if ((ModFlags & Modifiers.VOLATILE) != 0){
5866 if (!CanBeVolatile ()) {
5867 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5868 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5871 if ((ModFlags & Modifiers.READONLY) != 0){
5872 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5873 GetSignatureForError ());
5877 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5881 Type[] required_modifier = null;
5882 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5883 if (TypeManager.isvolatile_type == null)
5884 TypeManager.isvolatile_type = TypeManager.CoreLookupType (
5885 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5887 if (TypeManager.isvolatile_type != null)
5888 required_modifier = new Type [] { TypeManager.isvolatile_type };
5891 FieldBuilder = Parent.TypeBuilder.DefineField (
5892 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5894 FieldBuilder = Parent.TypeBuilder.DefineField (
5895 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5897 Parent.MemberCache.AddMember (FieldBuilder, this);
5898 TypeManager.RegisterFieldBase (FieldBuilder, this);
5900 catch (ArgumentException) {
5901 Report.Warning (-24, 1, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5905 if (initializer != null)
5906 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5907 new FieldInitializer (FieldBuilder, initializer));
5909 if (Parent.PartialContainer.Kind == Kind.Struct && (fa & FieldAttributes.Static) == 0 &&
5910 MemberType == Parent.TypeBuilder && !TypeManager.IsBuiltinType (MemberType) && initializer == null) {
5911 Report.Error (523, Location, "Struct member `{0}' causes a cycle in the structure layout",
5912 GetSignatureForError ());
5919 public override string GetSignatureForError ()
5921 string s = base.GetSignatureForError ();
5922 if ((ModFlags & Modifiers.BACKING_FIELD) == 0)
5925 // Undecorate name mangling
5926 int l = s.LastIndexOf ('>');
5927 return s.Substring (0, l).Remove (s.LastIndexOf ('<'), 1);
5930 protected override bool VerifyClsCompliance ()
5932 if (!base.VerifyClsCompliance ())
5935 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5936 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5944 // `set' and `get' accessors are represented with an Accessor.
5946 public class Accessor : IAnonymousHost {
5948 // Null if the accessor is empty, or a Block if not
5950 public const int AllowedModifiers =
5952 Modifiers.PROTECTED |
5953 Modifiers.INTERNAL |
5956 public ToplevelBlock Block;
5957 public Attributes Attributes;
5958 public Location Location;
5959 public int ModFlags;
5962 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5967 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5970 public void SetYields ()
5976 // Ooouh Martin, templates are missing here.
5977 // When it will be possible move here a lot of child code and template method type.
5978 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5979 protected MethodData method_data;
5980 protected ToplevelBlock block;
5981 protected ListDictionary declarative_security;
5983 // The accessor are created event if they are not wanted.
5984 // But we need them because their names are reserved.
5985 // Field says whether accessor will be emited or not
5986 public readonly bool IsDummy;
5988 protected readonly string prefix;
5990 ReturnParameter return_attributes;
5992 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
5993 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5995 this.prefix = prefix;
5999 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
6001 : base (member.Parent, SetupName (prefix, member, accessor.Location),
6002 accessor.Attributes)
6004 this.prefix = prefix;
6005 this.block = accessor.Block;
6008 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
6010 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
6013 public void UpdateName (InterfaceMemberBase member)
6015 SetMemberName (SetupName (prefix, member, Location));
6018 #region IMethodData Members
6020 public ToplevelBlock Block {
6030 public CallingConventions CallingConventions {
6032 return CallingConventions.Standard;
6036 public bool IsExcluded ()
6041 GenericMethod IMethodData.GenericMethod {
6047 public MemberName MethodName {
6053 public Type[] ParameterTypes {
6055 return ParameterInfo.Types;
6059 public abstract Parameters ParameterInfo { get ; }
6060 public abstract Type ReturnType { get; }
6061 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
6065 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
6067 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
6068 a.Type == TypeManager.conditional_attribute_type) {
6069 Report.Error (1667, a.Location,
6070 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
6071 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6075 if (a.IsValidSecurityAttribute ()) {
6076 if (declarative_security == null)
6077 declarative_security = new ListDictionary ();
6078 a.ExtractSecurityPermissionSet (declarative_security);
6082 if (a.Target == AttributeTargets.Method) {
6083 method_data.MethodBuilder.SetCustomAttribute (cb);
6087 if (a.Target == AttributeTargets.ReturnValue) {
6088 if (return_attributes == null)
6089 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6091 return_attributes.ApplyAttributeBuilder (a, cb);
6095 ApplyToExtraTarget (a, cb);
6098 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
6100 throw new NotSupportedException ("You forgot to define special attribute target handling");
6103 // It is not supported for the accessors
6104 public sealed override bool Define()
6106 throw new NotSupportedException ();
6109 public void Emit (DeclSpace parent)
6111 EmitMethod (parent);
6114 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0)
6115 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
6116 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6117 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
6119 if (OptAttributes != null)
6120 OptAttributes.Emit ();
6122 if (declarative_security != null) {
6123 foreach (DictionaryEntry de in declarative_security) {
6124 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6131 protected virtual void EmitMethod (DeclSpace parent)
6133 method_data.Emit (parent);
6136 public override bool EnableOverloadChecks (MemberCore overload)
6138 // This can only happen with indexers and it will
6139 // be catched as indexer difference
6140 if (overload is AbstractPropertyEventMethod)
6143 if (overload is MethodCore) {
6144 caching_flags |= Flags.MethodOverloadsExist;
6150 public override bool IsClsComplianceRequired()
6155 public bool IsDuplicateImplementation (MethodCore method)
6157 if (!MemberName.Equals (method.MemberName))
6160 Type[] param_types = method.ParameterTypes;
6162 if (param_types == null || param_types.Length != ParameterTypes.Length)
6165 for (int i = 0; i < param_types.Length; i++)
6166 if (param_types [i] != ParameterTypes [i])
6169 Report.SymbolRelatedToPreviousError (method);
6170 Report.Error (82, Location, "A member `{0}' is already reserved",
6171 method.GetSignatureForError ());
6175 public override bool IsUsed
6186 // Represents header string for documentation comment.
6188 public override string DocCommentHeader {
6189 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6192 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6197 // Properties and Indexers both generate PropertyBuilders, we use this to share
6198 // their common bits.
6200 abstract public class PropertyBase : PropertyBasedMember {
6202 public class GetMethod : PropertyMethod
6204 static string[] attribute_targets = new string [] { "method", "return" };
6206 public GetMethod (PropertyBase method):
6207 base (method, "get_")
6211 public GetMethod (PropertyBase method, Accessor accessor):
6212 base (method, accessor, "get_")
6216 public override MethodBuilder Define (DeclSpace parent)
6218 if (!CheckForDuplications ())
6224 base.Define (parent);
6226 method_data = new MethodData (method, ModFlags, flags, this);
6228 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6231 return method_data.MethodBuilder;
6234 public override Type ReturnType {
6236 return method.MemberType;
6240 public override Parameters ParameterInfo {
6242 return Parameters.EmptyReadOnlyParameters;
6246 public override string[] ValidAttributeTargets {
6248 return attribute_targets;
6253 public class SetMethod : PropertyMethod {
6255 static string[] attribute_targets = new string [] { "method", "param", "return" };
6256 ImplicitParameter param_attr;
6257 protected Parameters parameters;
6259 public SetMethod (PropertyBase method):
6260 base (method, "set_")
6264 public SetMethod (PropertyBase method, Accessor accessor):
6265 base (method, accessor, "set_")
6269 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6271 if (a.Target == AttributeTargets.Parameter) {
6272 if (param_attr == null)
6273 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6275 param_attr.ApplyAttributeBuilder (a, cb);
6279 base.ApplyAttributeBuilder (a, cb);
6282 public override Parameters ParameterInfo {
6284 if (parameters == null)
6285 DefineParameters ();
6290 protected virtual void DefineParameters ()
6292 parameters = Parameters.CreateFullyResolved (
6293 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, Location));
6296 public override MethodBuilder Define (DeclSpace parent)
6298 if (!CheckForDuplications ())
6304 base.Define (parent);
6306 method_data = new MethodData (method, ModFlags, flags, this);
6308 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6311 return method_data.MethodBuilder;
6314 public override Type ReturnType {
6316 return TypeManager.void_type;
6320 public override string[] ValidAttributeTargets {
6322 return attribute_targets;
6327 static string[] attribute_targets = new string [] { "property" };
6329 public abstract class PropertyMethod : AbstractPropertyEventMethod
6331 protected readonly PropertyBase method;
6332 protected MethodAttributes flags;
6335 public PropertyMethod (PropertyBase method, string prefix)
6336 : base (method, prefix)
6338 this.method = method;
6341 public PropertyMethod (PropertyBase method, Accessor accessor,
6343 : base (method, accessor, prefix)
6345 this.method = method;
6346 this.ModFlags = accessor.ModFlags;
6347 yields = accessor.Yields;
6349 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6350 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6354 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6356 if (a.IsInternalMethodImplAttribute) {
6357 method.is_external_implementation = true;
6360 base.ApplyAttributeBuilder (a, cb);
6363 public override AttributeTargets AttributeTargets {
6365 return AttributeTargets.Method;
6369 public override bool IsClsComplianceRequired ()
6371 return method.IsClsComplianceRequired ();
6374 public virtual MethodBuilder Define (DeclSpace parent)
6376 TypeContainer container = parent.PartialContainer;
6379 // Check for custom access modifier
6381 if ((ModFlags & Modifiers.Accessibility) == 0) {
6382 ModFlags |= method.ModFlags;
6383 flags = method.flags;
6385 if (container.Kind == Kind.Interface)
6386 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6387 GetSignatureForError ());
6389 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6390 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6393 CheckModifiers (ModFlags);
6394 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6395 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6396 flags = Modifiers.MethodAttr (ModFlags);
6397 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6400 CheckAbstractAndExtern (block != null);
6403 Iterator.CreateIterator (this, Parent, ModFlags);
6408 public bool HasCustomAccessModifier
6411 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6415 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6417 return new EmitContext (method,
6418 ds, method.ds, method.Location, ig, ReturnType,
6419 method.ModFlags, false);
6422 public override ObsoleteAttribute GetObsoleteAttribute ()
6424 return method.GetObsoleteAttribute ();
6427 public override string GetSignatureForError()
6429 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6432 void CheckModifiers (int modflags)
6434 modflags &= Modifiers.Accessibility;
6436 int mflags = method.ModFlags & Modifiers.Accessibility;
6438 if ((mflags & Modifiers.PUBLIC) != 0) {
6439 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6441 else if ((mflags & Modifiers.PROTECTED) != 0) {
6442 if ((mflags & Modifiers.INTERNAL) != 0)
6443 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6445 flags |= Modifiers.PRIVATE;
6447 else if ((mflags & Modifiers.INTERNAL) != 0)
6448 flags |= Modifiers.PRIVATE;
6450 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6451 Report.Error (273, Location,
6452 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6453 GetSignatureForError (), method.GetSignatureForError ());
6457 protected bool CheckForDuplications ()
6459 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6462 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6466 public PropertyMethod Get, Set;
6467 public PropertyBuilder PropertyBuilder;
6468 public MethodBuilder GetBuilder, SetBuilder;
6470 protected bool define_set_first = false;
6472 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6473 int allowed_mod, bool is_iface, MemberName name,
6474 Attributes attrs, bool define_set_first)
6475 : base (parent, null, type, mod_flags, allowed_mod, is_iface, name, attrs)
6477 this.define_set_first = define_set_first;
6480 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6482 if (a.HasSecurityAttribute) {
6483 a.Error_InvalidSecurityParent ();
6487 PropertyBuilder.SetCustomAttribute (cb);
6490 public override AttributeTargets AttributeTargets {
6492 return AttributeTargets.Property;
6496 public override bool Define ()
6501 if (!IsTypePermitted ())
6507 protected override bool DoDefine ()
6509 if (!base.DoDefine ())
6513 // Accessors modifiers check
6515 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6516 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6517 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6518 GetSignatureForError ());
6522 if ((Get.IsDummy || Set.IsDummy)
6523 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6524 Report.Error (276, Location,
6525 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6526 GetSignatureForError ());
6531 if (MemberType.IsGenericParameter)
6535 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6536 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6545 GetBuilder = Get.Define (Parent);
6546 return (Get.IsDummy) ? true : GetBuilder != null;
6549 bool DefineSet (bool define)
6554 SetBuilder = Set.Define (Parent);
6555 return (Set.IsDummy) ? true : SetBuilder != null;
6558 protected bool DefineAccessors ()
6560 return DefineSet (define_set_first) &&
6562 DefineSet (!define_set_first);
6565 protected abstract PropertyInfo ResolveBaseProperty ();
6567 // TODO: rename to Resolve......
6568 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6570 PropertyInfo base_property = ResolveBaseProperty ();
6571 if (base_property == null)
6574 base_ret_type = base_property.PropertyType;
6575 MethodInfo get_accessor = base_property.GetGetMethod (true);
6576 MethodInfo set_accessor = base_property.GetSetMethod (true);
6577 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6580 // Check base property accessors conflict
6582 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6583 if (get_accessor == null) {
6584 if (Get != null && !Get.IsDummy) {
6585 Report.SymbolRelatedToPreviousError (base_property);
6586 Report.Error (545, Location,
6587 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6588 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6591 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6593 if (!Get.IsDummy && !CheckAccessModifiers (
6594 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6595 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6598 if (set_accessor == null) {
6599 if (Set != null && !Set.IsDummy) {
6600 Report.SymbolRelatedToPreviousError (base_property);
6601 Report.Error (546, Location,
6602 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6603 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6606 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6608 if (!Set.IsDummy && !CheckAccessModifiers (
6609 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6610 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6614 // When one accessor does not exist and property hides base one
6615 // we need to propagate this upwards
6616 if (set_accessor == null)
6617 set_accessor = get_accessor;
6620 // Get the less restrictive access
6622 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6625 public override void Emit ()
6628 // The PropertyBuilder can be null for explicit implementations, in that
6629 // case, we do not actually emit the ".property", so there is nowhere to
6630 // put the attribute
6632 if (PropertyBuilder != null && OptAttributes != null)
6633 OptAttributes.Emit ();
6645 /// Tests whether accessors are not in collision with some method (CS0111)
6647 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6649 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6652 public override bool IsUsed
6658 return Get.IsUsed | Set.IsUsed;
6662 protected override void SetMemberName (MemberName new_name)
6664 base.SetMemberName (new_name);
6666 Get.UpdateName (this);
6667 Set.UpdateName (this);
6670 public override string[] ValidAttributeTargets {
6672 return attribute_targets;
6677 // Represents header string for documentation comment.
6679 public override string DocCommentHeader {
6680 get { return "P:"; }
6684 public class Property : PropertyBase {
6685 const int AllowedModifiers =
6688 Modifiers.PROTECTED |
6689 Modifiers.INTERNAL |
6693 Modifiers.OVERRIDE |
6694 Modifiers.ABSTRACT |
6697 Modifiers.METHOD_YIELDS |
6700 const int AllowedInterfaceModifiers =
6703 void CreateAutomaticProperty (Block block, Accessor get_block, Accessor set_block)
6706 Field field = new Field (
6708 Modifiers.BACKING_FIELD | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6709 "<" + Name + ">k__BackingField", null, Location);
6710 ((TypeContainer)Parent).PartialContainer.AddField (field);
6713 get_block.Block = new ToplevelBlock (block, null, Location);
6714 Return r = new Return (new SimpleName(field.Name, Location), Location);
6715 get_block.Block.AddStatement (r);
6716 get_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6719 Parameters parameters = new Parameters (new Parameter (type_name, "value", Parameter.Modifier.NONE, null, Location));
6720 set_block.Block = new ToplevelBlock (block, parameters, Location);
6721 Assign a = new SimpleAssign (new SimpleName (field.Name, Location), new SimpleName ("value", Location));
6722 set_block.Block.AddStatement (new StatementExpression(a));
6723 set_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6726 public Property (DeclSpace parent, FullNamedExpression type, int mod, bool is_iface,
6727 MemberName name, Attributes attrs, Accessor get_block,
6728 Accessor set_block, bool define_set_first)
6729 : this (parent, type, mod, is_iface, name, attrs, get_block, set_block,
6730 define_set_first, null)
6734 public Property (DeclSpace parent, FullNamedExpression type, int mod, bool is_iface,
6735 MemberName name, Attributes attrs, Accessor get_block,
6736 Accessor set_block, bool define_set_first, Block current_block)
6737 : base (parent, type, mod,
6738 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6739 is_iface, name, attrs, define_set_first)
6741 if (!is_iface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6742 get_block != null && get_block.Block == null &&
6743 set_block != null && set_block.Block == null) {
6744 if (RootContext.Version <= LanguageVersion.ISO_2)
6745 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6747 CreateAutomaticProperty (current_block, get_block, set_block);
6750 if (get_block == null)
6751 Get = new GetMethod (this);
6753 Get = new GetMethod (this, get_block);
6755 if (set_block == null)
6756 Set = new SetMethod (this);
6758 Set = new SetMethod (this, set_block);
6761 public override bool Define ()
6763 if (!DoDefineBase ())
6766 if (!base.Define ())
6769 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6771 if (!DefineAccessors ())
6777 // FIXME - PropertyAttributes.HasDefault ?
6779 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6780 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6783 PropertyBuilder.SetGetMethod (GetBuilder);
6784 Parent.MemberCache.AddMember (GetBuilder, Get);
6788 PropertyBuilder.SetSetMethod (SetBuilder);
6789 Parent.MemberCache.AddMember (SetBuilder, Set);
6792 TypeManager.RegisterProperty (PropertyBuilder, this);
6793 Parent.MemberCache.AddMember (PropertyBuilder, this);
6797 protected override PropertyInfo ResolveBaseProperty ()
6799 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6800 Parent.TypeBuilder, Name, Parameters.EmptyReadOnlyParameters.Types, null, true) as PropertyInfo;
6805 /// Gigantic workaround for lameness in SRE follows :
6806 /// This class derives from EventInfo and attempts to basically
6807 /// wrap around the EventBuilder so that FindMembers can quickly
6808 /// return this in it search for members
6810 public class MyEventBuilder : EventInfo {
6813 // We use this to "point" to our Builder which is
6814 // not really a MemberInfo
6816 EventBuilder MyBuilder;
6819 // We "catch" and wrap these methods
6821 MethodInfo raise, remove, add;
6823 EventAttributes attributes;
6824 Type declaring_type, reflected_type, event_type;
6829 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6831 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6833 // And now store the values in our own fields.
6835 declaring_type = type_builder;
6837 reflected_type = type_builder;
6839 attributes = event_attr;
6842 this.event_type = event_type;
6846 // Methods that you have to override. Note that you only need
6847 // to "implement" the variants that take the argument (those are
6848 // the "abstract" methods, the others (GetAddMethod()) are
6851 public override MethodInfo GetAddMethod (bool nonPublic)
6856 public override MethodInfo GetRemoveMethod (bool nonPublic)
6861 public override MethodInfo GetRaiseMethod (bool nonPublic)
6867 // These methods make "MyEventInfo" look like a Builder
6869 public void SetRaiseMethod (MethodBuilder raiseMethod)
6871 raise = raiseMethod;
6872 MyBuilder.SetRaiseMethod (raiseMethod);
6875 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6877 remove = removeMethod;
6878 MyBuilder.SetRemoveOnMethod (removeMethod);
6881 public void SetAddOnMethod (MethodBuilder addMethod)
6884 MyBuilder.SetAddOnMethod (addMethod);
6887 public void SetCustomAttribute (CustomAttributeBuilder cb)
6889 MyBuilder.SetCustomAttribute (cb);
6892 public override object [] GetCustomAttributes (bool inherit)
6894 // FIXME : There's nothing which can be seemingly done here because
6895 // we have no way of getting at the custom attribute objects of the
6900 public override object [] GetCustomAttributes (Type t, bool inherit)
6902 // FIXME : Same here !
6906 public override bool IsDefined (Type t, bool b)
6911 public override EventAttributes Attributes {
6917 public override string Name {
6923 public override Type DeclaringType {
6925 return declaring_type;
6929 public override Type ReflectedType {
6931 return reflected_type;
6935 public Type EventType {
6941 public void SetUsed ()
6943 if (my_event != null) {
6944 // my_event.SetAssigned ();
6945 my_event.SetMemberIsUsed ();
6951 /// For case when event is declared like property (with add and remove accessors).
6953 public class EventProperty: Event {
6954 abstract class AEventPropertyAccessor : AEventAccessor
6956 public AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
6957 base (method, accessor, prefix)
6961 public override MethodBuilder Define (DeclSpace ds)
6963 CheckAbstractAndExtern (block != null);
6964 return base.Define (ds);
6967 public override string GetSignatureForError ()
6969 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
6973 sealed class AddDelegateMethod: AEventPropertyAccessor
6975 public AddDelegateMethod (Event method, Accessor accessor):
6976 base (method, accessor, "add_")
6980 protected override MethodInfo DelegateMethodInfo {
6982 return TypeManager.delegate_combine_delegate_delegate;
6987 sealed class RemoveDelegateMethod: AEventPropertyAccessor
6989 public RemoveDelegateMethod (Event method, Accessor accessor):
6990 base (method, accessor, "remove_")
6994 protected override MethodInfo DelegateMethodInfo {
6996 return TypeManager.delegate_remove_delegate_delegate;
7002 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
7004 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
7005 bool is_iface, MemberName name,
7006 Attributes attrs, Accessor add, Accessor remove)
7007 : base (parent, type, mod_flags, is_iface, name, attrs)
7009 Add = new AddDelegateMethod (this, add);
7010 Remove = new RemoveDelegateMethod (this, remove);
7013 public override bool Define()
7015 if (!base.Define ())
7022 public override string[] ValidAttributeTargets {
7024 return attribute_targets;
7030 /// Event is declared like field.
7032 public class EventField : Event {
7033 abstract class EventFieldAccessor : AEventAccessor
7035 protected EventFieldAccessor (Event method, string prefix)
7036 : base (method, prefix)
7040 protected override void EmitMethod(DeclSpace parent)
7042 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7045 MethodBuilder mb = method_data.MethodBuilder;
7046 ILGenerator ig = mb.GetILGenerator ();
7048 // TODO: because we cannot use generics yet
7049 FieldInfo field_info = ((EventField)method).FieldBuilder;
7051 if (parent is Class) {
7052 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
7055 if ((method.ModFlags & Modifiers.STATIC) != 0) {
7056 ig.Emit (OpCodes.Ldsfld, field_info);
7057 ig.Emit (OpCodes.Ldarg_0);
7058 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7059 ig.Emit (OpCodes.Castclass, method.MemberType);
7060 ig.Emit (OpCodes.Stsfld, field_info);
7062 ig.Emit (OpCodes.Ldarg_0);
7063 ig.Emit (OpCodes.Ldarg_0);
7064 ig.Emit (OpCodes.Ldfld, field_info);
7065 ig.Emit (OpCodes.Ldarg_1);
7066 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7067 ig.Emit (OpCodes.Castclass, method.MemberType);
7068 ig.Emit (OpCodes.Stfld, field_info);
7070 ig.Emit (OpCodes.Ret);
7074 sealed class AddDelegateMethod: EventFieldAccessor
7076 public AddDelegateMethod (Event method):
7077 base (method, "add_")
7081 protected override MethodInfo DelegateMethodInfo {
7083 return TypeManager.delegate_combine_delegate_delegate;
7088 sealed class RemoveDelegateMethod: EventFieldAccessor
7090 public RemoveDelegateMethod (Event method):
7091 base (method, "remove_")
7095 protected override MethodInfo DelegateMethodInfo {
7097 return TypeManager.delegate_remove_delegate_delegate;
7103 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7104 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7106 public FieldBuilder FieldBuilder;
7107 public Expression Initializer;
7109 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags,
7110 bool is_iface, MemberName name,
7112 : base (parent, type, mod_flags, is_iface, name, attrs)
7114 Add = new AddDelegateMethod (this);
7115 Remove = new RemoveDelegateMethod (this);
7118 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7120 if (a.Target == AttributeTargets.Field) {
7121 FieldBuilder.SetCustomAttribute (cb);
7125 if (a.Target == AttributeTargets.Method) {
7126 int errors = Report.Errors;
7127 Add.ApplyAttributeBuilder (a, cb);
7128 if (errors == Report.Errors)
7129 Remove.ApplyAttributeBuilder (a, cb);
7133 base.ApplyAttributeBuilder (a, cb);
7136 public override bool Define()
7138 if (!base.Define ())
7144 // FIXME: We are unable to detect whether generic event is used because
7145 // we are using FieldExpr instead of EventExpr for event access in that
7146 // case. When this issue will be fixed this hack can be removed.
7147 if (TypeManager.IsGenericType (MemberType))
7150 if (Add.IsInterfaceImplementation)
7153 FieldBuilder = Parent.TypeBuilder.DefineField (
7155 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7156 TypeManager.RegisterEventField (EventBuilder, this);
7158 if (Initializer != null) {
7159 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
7160 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7161 GetSignatureForError ());
7165 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
7166 new FieldInitializer (FieldBuilder, Initializer));
7172 public override string[] ValidAttributeTargets
7175 return IsInterface ? attribute_targets_interface : attribute_targets;
7180 public abstract class Event : PropertyBasedMember {
7181 public abstract class AEventAccessor : AbstractPropertyEventMethod
7183 protected readonly Event method;
7184 ImplicitParameter param_attr;
7186 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7188 protected AEventAccessor (Event method, string prefix)
7189 : base (method, prefix)
7191 this.method = method;
7192 this.ModFlags = method.ModFlags;
7195 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7196 : base (method, accessor, prefix)
7198 this.method = method;
7199 this.ModFlags = method.ModFlags;
7202 public bool IsInterfaceImplementation {
7203 get { return method_data.implementing != null; }
7206 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7208 if (a.IsInternalMethodImplAttribute) {
7209 method.is_external_implementation = true;
7212 base.ApplyAttributeBuilder (a, cb);
7215 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
7217 if (a.Target == AttributeTargets.Parameter) {
7218 if (param_attr == null)
7219 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7221 param_attr.ApplyAttributeBuilder (a, cb);
7225 base.ApplyAttributeBuilder (a, cb);
7228 public override AttributeTargets AttributeTargets {
7230 return AttributeTargets.Method;
7234 public override bool IsClsComplianceRequired ()
7236 return method.IsClsComplianceRequired ();
7239 public virtual MethodBuilder Define (DeclSpace parent)
7241 method_data = new MethodData (method, method.ModFlags,
7242 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7244 if (!method_data.Define (parent, method.GetFullName (MemberName)))
7247 MethodBuilder mb = method_data.MethodBuilder;
7248 ParameterInfo.ApplyAttributes (mb);
7252 protected abstract MethodInfo DelegateMethodInfo { get; }
7254 public override Type ReturnType {
7256 return TypeManager.void_type;
7260 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7262 return new EmitContext (
7263 ds, method.Parent, Location, ig, ReturnType,
7264 method.ModFlags, false);
7267 public override ObsoleteAttribute GetObsoleteAttribute ()
7269 return method.GetObsoleteAttribute ();
7272 public override string[] ValidAttributeTargets {
7274 return attribute_targets;
7278 public override Parameters ParameterInfo {
7280 return method.parameters;
7286 const int AllowedModifiers =
7289 Modifiers.PROTECTED |
7290 Modifiers.INTERNAL |
7295 Modifiers.OVERRIDE |
7297 Modifiers.ABSTRACT |
7300 const int AllowedInterfaceModifiers =
7303 public AEventAccessor Add, Remove;
7304 public MyEventBuilder EventBuilder;
7305 public MethodBuilder AddBuilder, RemoveBuilder;
7307 Parameters parameters;
7309 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags,
7310 bool is_iface, MemberName name, Attributes attrs)
7311 : base (parent, null, type, mod_flags,
7312 is_iface ? AllowedInterfaceModifiers : AllowedModifiers, is_iface,
7317 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7319 if ((a.HasSecurityAttribute)) {
7320 a.Error_InvalidSecurityParent ();
7324 EventBuilder.SetCustomAttribute (cb);
7327 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7329 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7332 public override AttributeTargets AttributeTargets {
7334 return AttributeTargets.Event;
7338 public override bool Define ()
7340 if (!DoDefineBase ())
7346 if (!TypeManager.IsDelegateType (MemberType)) {
7347 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7351 parameters = Parameters.CreateFullyResolved (
7352 new Parameter (MemberType, "value", Parameter.Modifier.NONE, null, Location));
7357 if (TypeManager.delegate_combine_delegate_delegate == null) {
7358 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7359 TypeManager.delegate_type, "Combine", Location,
7360 TypeManager.delegate_type, TypeManager.delegate_type);
7362 if (TypeManager.delegate_remove_delegate_delegate == null) {
7363 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7364 TypeManager.delegate_type, "Remove", Location,
7365 TypeManager.delegate_type, TypeManager.delegate_type);
7369 // Now define the accessors
7372 AddBuilder = Add.Define (Parent);
7373 if (AddBuilder == null)
7376 RemoveBuilder = Remove.Define (Parent);
7377 if (RemoveBuilder == null)
7380 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7381 EventBuilder.SetAddOnMethod (AddBuilder);
7382 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7384 Parent.MemberCache.AddMember (EventBuilder, this);
7385 Parent.MemberCache.AddMember (AddBuilder, Add);
7386 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7391 public override void Emit ()
7393 if (OptAttributes != null) {
7394 OptAttributes.Emit ();
7398 Remove.Emit (Parent);
7403 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7405 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7406 Parent.TypeBuilder, Name);
7411 ParameterData pd = TypeManager.GetParameterData (mi);
7412 base_ret_type = pd.ParameterType (0);
7417 // Represents header string for documentation comment.
7419 public override string DocCommentHeader {
7420 get { return "E:"; }
7425 public class Indexer : PropertyBase
7427 public class GetIndexerMethod : GetMethod
7429 Parameters parameters;
7431 public GetIndexerMethod (PropertyBase method):
7436 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7437 base (method, accessor)
7441 public override MethodBuilder Define (DeclSpace parent)
7444 // Clone indexer accessor parameters for localized capturing
7446 parameters = ((Indexer) method).parameters.Clone ();
7447 return base.Define (parent);
7450 public override bool EnableOverloadChecks (MemberCore overload)
7452 if (base.EnableOverloadChecks (overload)) {
7453 overload.caching_flags |= Flags.MethodOverloadsExist;
7460 public override Parameters ParameterInfo {
7467 public class SetIndexerMethod: SetMethod
7469 public SetIndexerMethod (PropertyBase method):
7474 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7475 base (method, accessor)
7479 protected override void DefineParameters ()
7481 parameters = Parameters.MergeGenerated (((Indexer)method).parameters,
7482 new Parameter (method.MemberType, "value", Parameter.Modifier.NONE, null, method.Location));
7485 public override bool EnableOverloadChecks (MemberCore overload)
7487 if (base.EnableOverloadChecks (overload)) {
7488 overload.caching_flags |= Flags.MethodOverloadsExist;
7496 const int AllowedModifiers =
7499 Modifiers.PROTECTED |
7500 Modifiers.INTERNAL |
7504 Modifiers.OVERRIDE |
7509 const int AllowedInterfaceModifiers =
7512 public readonly Parameters parameters;
7514 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7515 bool is_iface, Parameters parameters, Attributes attrs,
7516 Accessor get_block, Accessor set_block, bool define_set_first)
7517 : base (parent, type, mod,
7518 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7519 is_iface, name, attrs, define_set_first)
7521 if (type == TypeManager.system_void_expr)
7522 Report.Error (620, name.Location, "An indexer return type cannot be `void'");
7524 this.parameters = parameters;
7526 if (get_block == null)
7527 Get = new GetIndexerMethod (this);
7529 Get = new GetIndexerMethod (this, get_block);
7531 if (set_block == null)
7532 Set = new SetIndexerMethod (this);
7534 Set = new SetIndexerMethod (this, set_block);
7537 public override bool Define ()
7539 if (!DoDefineBase ())
7542 if (!base.Define ())
7545 if (!DefineParameters (parameters))
7548 if (OptAttributes != null && TypeManager.indexer_name_type != null) {
7549 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
7550 if (indexer_attr != null) {
7551 // Remove the attribute from the list because it is not emitted
7552 OptAttributes.Attrs.Remove (indexer_attr);
7554 string name = indexer_attr.GetIndexerAttributeValue ();
7560 if (IsExplicitImpl) {
7561 Report.Error (415, indexer_attr.Location,
7562 "The `IndexerName' attribute is valid only on an " +
7563 "indexer that is not an explicit interface member declaration");
7567 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7568 Report.Error (609, indexer_attr.Location,
7569 "Cannot set the `IndexerName' attribute on an indexer marked override");
7575 if (InterfaceType != null) {
7576 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7577 if (base_IndexerName != Name)
7578 ShortName = base_IndexerName;
7581 if (!Parent.PartialContainer.AddMember (this) ||
7582 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7585 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
7586 if (!Parent.MemberCache.CheckExistingMembersOverloads (this, Name, parameters))
7590 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7592 if (!DefineAccessors ())
7599 // Now name the parameters
7601 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7602 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.Types);
7605 PropertyBuilder.SetGetMethod (GetBuilder);
7606 Parent.MemberCache.AddMember (GetBuilder, Get);
7610 PropertyBuilder.SetSetMethod (SetBuilder);
7611 Parent.MemberCache.AddMember (SetBuilder, Set);
7614 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, parameters.Types);
7615 Parent.MemberCache.AddMember (PropertyBuilder, this);
7619 public override bool EnableOverloadChecks (MemberCore overload)
7621 if (overload is Indexer) {
7622 caching_flags |= Flags.MethodOverloadsExist;
7629 public override string GetDocCommentName (DeclSpace ds)
7631 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7634 public override string GetSignatureForError ()
7636 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7637 if (MemberName.Left != null) {
7639 sb.Append (MemberName.Left.GetSignatureForError ());
7642 sb.Append (".this");
7643 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7644 return sb.ToString ();
7647 protected override PropertyInfo ResolveBaseProperty ()
7649 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7650 Parent.TypeBuilder, Name, parameters.Types, null, true) as PropertyInfo;
7653 protected override bool VerifyClsCompliance ()
7655 if (!base.VerifyClsCompliance ())
7658 parameters.VerifyClsCompliance ();
7663 public class Operator : MethodOrOperator, IAnonymousHost {
7665 const int AllowedModifiers =
7671 public enum OpType : byte {
7681 // Unary and Binary operators
7704 // Implicit and Explicit
7708 // Just because of enum
7712 public readonly OpType OperatorType;
7714 static readonly string [] [] names;
7718 names = new string[(int)OpType.TOP][];
7719 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7720 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7721 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7722 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7723 names [(int) OpType.True] = new string [] { "true", "op_True" };
7724 names [(int) OpType.False] = new string [] { "false", "op_False" };
7725 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7726 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7727 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7728 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7729 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7730 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7731 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7732 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7733 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7734 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7735 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7736 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7737 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7738 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7739 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7740 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7741 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7742 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7743 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7744 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7747 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7748 int mod_flags, Parameters parameters,
7749 ToplevelBlock block, Attributes attrs, Location loc)
7750 : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
7751 new MemberName ("op_" + type.ToString(), loc), attrs, parameters)
7753 OperatorType = type;
7757 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7759 if (a.Type == TypeManager.conditional_attribute_type) {
7760 Error_ConditionalAttributeIsNotValid ();
7764 base.ApplyAttributeBuilder (a, cb);
7767 public override bool Define ()
7769 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7770 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7771 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7775 if (!base.Define ())
7778 // imlicit and explicit operator of same types are not allowed
7779 if (OperatorType == OpType.Explicit)
7780 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
7781 else if (OperatorType == OpType.Implicit)
7782 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
7784 if (MemberType == TypeManager.void_type) {
7785 Report.Error (590, Location, "User-defined operators cannot return void");
7789 Type declaring_type = MethodData.DeclaringType;
7790 Type return_type = MemberType;
7791 Type first_arg_type = ParameterTypes [0];
7793 Type first_arg_type_unwrap = first_arg_type;
7794 if (TypeManager.IsNullableType (first_arg_type))
7795 first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
7797 Type return_type_unwrap = return_type;
7798 if (TypeManager.IsNullableType (return_type))
7799 return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
7802 // Rules for conversion operators
7804 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7805 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7806 Report.Error (555, Location,
7807 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7812 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7813 conv_type = first_arg_type;
7814 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7815 conv_type = return_type;
7817 Report.Error (556, Location,
7818 "User-defined conversion must convert to or from the enclosing type");
7823 // Because IsInterface and IsClass are not supported
7825 if (!TypeManager.IsGenericParameter (conv_type)) {
7826 if (conv_type.IsInterface) {
7827 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7828 GetSignatureForError ());
7832 if (conv_type.IsClass) {
7833 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7834 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7835 GetSignatureForError ());
7839 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7840 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7841 GetSignatureForError ());
7846 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7847 if (first_arg_type != declaring_type || ParameterTypes [1] != TypeManager.int32_type) {
7848 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");
7851 } else if (Parameters.Count == 1) {
7852 // Checks for Unary operators
7854 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7855 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7856 Report.Error (448, Location,
7857 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7860 if (first_arg_type != declaring_type) {
7862 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7867 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7868 Report.Error (562, Location,
7869 "The parameter type of a unary operator must be the containing type");
7873 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7874 if (return_type != TypeManager.bool_type){
7877 "The return type of operator True or False " +
7884 // Checks for Binary operators
7886 if (first_arg_type != declaring_type &&
7887 ParameterTypes [1] != declaring_type){
7890 "One of the parameters of a binary operator must " +
7891 "be the containing type");
7899 protected override bool DoDefine ()
7901 if (!base.DoDefine ())
7904 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7908 public override void Emit ()
7912 Parameters.ApplyAttributes (MethodBuilder);
7915 // abstract or extern methods have no bodies
7917 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7921 if ((flags & MethodAttributes.PinvokeImpl) == 0)
7922 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
7924 ec = CreateEmitContext (Parent, null);
7926 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
7927 ec.EmitTopBlock (this, Block);
7930 source.CloseMethod ();
7935 // Operator cannot be override
7936 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7941 public static string GetName (OpType ot)
7943 return names [(int) ot] [0];
7946 public static string GetName (string metadata_name)
7948 for (int i = 0; i < names.Length; ++i) {
7949 if (names [i] [1] == metadata_name)
7950 return names [i] [0];
7955 public static string GetMetadataName (OpType ot)
7957 return names [(int) ot] [1];
7960 public static string GetMetadataName (string name)
7962 for (int i = 0; i < names.Length; ++i) {
7963 if (names [i] [0] == name)
7964 return names [i] [1];
7969 public OpType GetMatchingOperator ()
7971 switch (OperatorType) {
7972 case OpType.Equality:
7973 return OpType.Inequality;
7974 case OpType.Inequality:
7975 return OpType.Equality;
7977 return OpType.False;
7980 case OpType.GreaterThan:
7981 return OpType.LessThan;
7982 case OpType.LessThan:
7983 return OpType.GreaterThan;
7984 case OpType.GreaterThanOrEqual:
7985 return OpType.LessThanOrEqual;
7986 case OpType.LessThanOrEqual:
7987 return OpType.GreaterThanOrEqual;
7993 public override string GetSignatureForError ()
7995 StringBuilder sb = new StringBuilder ();
7996 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7997 sb.AppendFormat ("{0}.{1} operator {2}",
7998 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
8001 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
8004 sb.Append (Parameters.GetSignatureForError ());
8005 return sb.ToString ();
8010 // This is used to compare method signatures
8012 struct MethodSignature {
8014 public Type RetType;
8015 public Type [] Parameters;
8018 /// This delegate is used to extract methods which have the
8019 /// same signature as the argument
8021 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
8023 public MethodSignature (string name, Type ret_type, Type [] parameters)
8028 if (parameters == null)
8029 Parameters = Type.EmptyTypes;
8031 Parameters = parameters;
8034 public override string ToString ()
8037 if (Parameters.Length != 0){
8038 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
8039 for (int i = 0; i < Parameters.Length; i++){
8040 sb.Append (Parameters [i]);
8041 if (i+1 < Parameters.Length)
8044 pars = sb.ToString ();
8047 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
8050 public override int GetHashCode ()
8052 return Name.GetHashCode ();
8055 public override bool Equals (Object o)
8057 MethodSignature other = (MethodSignature) o;
8059 if (other.Name != Name)
8062 if (other.RetType != RetType)
8065 if (Parameters == null){
8066 if (other.Parameters == null)
8071 if (other.Parameters == null)
8074 int c = Parameters.Length;
8075 if (other.Parameters.Length != c)
8078 for (int i = 0; i < c; i++)
8079 if (other.Parameters [i] != Parameters [i])
8085 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8087 MethodSignature sig = (MethodSignature) filter_criteria;
8089 if (m.Name != sig.Name)
8093 MethodInfo mi = m as MethodInfo;
8094 PropertyInfo pi = m as PropertyInfo;
8097 ReturnType = mi.ReturnType;
8098 else if (pi != null)
8099 ReturnType = pi.PropertyType;
8104 // we use sig.RetType == null to mean `do not check the
8105 // method return value.
8107 if (sig.RetType != null) {
8108 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8114 args = TypeManager.GetParameterData (mi).Types;
8116 args = TypeManager.GetArgumentTypes (pi);
8117 Type [] sigp = sig.Parameters;
8119 if (args.Length != sigp.Length)
8122 for (int i = args.Length - 1; i >= 0; i--)
8123 if (!TypeManager.IsEqual (args [i], sigp [i]))