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 (next_part);
340 Error_MissingPartialModifier (tc);
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 if (Kind == Kind.Interface) {
403 Report.Error (541, mc.Location,
404 "`{0}': explicit interface declaration can only be declared in a class or struct",
405 mc.GetSignatureForError ());
408 ordered_explicit_member_list.Add (mc);
409 alist.Insert (0, mc);
411 ordered_member_list.Add (mc);
417 public void AddMethod (Method method)
419 MemberName mn = method.MemberName;
420 if (!AddToContainer (method, mn.IsGeneric ? mn.Basename : mn.MethodName))
424 methods = new MemberCoreArrayList ();
426 if (method.MemberName.Left != null)
427 AddMemberToList (method, methods, true);
429 AddMemberToList (method, methods, false);
432 public void AddConstructor (Constructor c)
434 if (c.Name != MemberName.Name) {
435 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
438 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
439 if (!AddToContainer (c, is_static ?
440 ConstructorBuilder.ConstructorName : ConstructorBuilder.TypeConstructorName))
443 if (is_static && c.Parameters.IsEmpty){
444 if (default_static_constructor != null) {
445 Report.SymbolRelatedToPreviousError (default_static_constructor);
446 Report.Error (111, c.Location,
447 "A member `{0}' is already defined. Rename this member or use different parameter types",
448 c.GetSignatureForError ());
452 default_static_constructor = c;
454 if (c.Parameters.IsEmpty)
455 default_constructor = c;
457 if (instance_constructors == null)
458 instance_constructors = new MemberCoreArrayList ();
460 instance_constructors.Add (c);
464 public bool AddField (FieldBase field)
466 if (!AddMember (field))
470 fields = new MemberCoreArrayList ();
474 if ((field.ModFlags & Modifiers.STATIC) != 0)
477 if (first_nonstatic_field == null) {
478 first_nonstatic_field = field;
482 if (Kind == Kind.Struct && first_nonstatic_field.Parent != field.Parent) {
483 Report.SymbolRelatedToPreviousError (first_nonstatic_field.Parent);
484 Report.Warning (282, 3, field.Location,
485 "struct instance field `{0}' found in different declaration from instance field `{1}'",
486 field.GetSignatureForError (), first_nonstatic_field.GetSignatureForError ());
491 public void AddProperty (Property prop)
493 if (!AddMember (prop) ||
494 !AddMember (prop.Get) || !AddMember (prop.Set))
497 if (properties == null)
498 properties = new MemberCoreArrayList ();
500 if (prop.MemberName.Left != null)
501 AddMemberToList (prop, properties, true);
503 AddMemberToList (prop, properties, false);
506 public void AddEvent (Event e)
511 if (e is EventProperty) {
512 if (!AddMember (e.Add))
515 if (!AddMember (e.Remove))
520 events = new MemberCoreArrayList ();
526 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
528 public void AddIndexer (Indexer i)
530 if (indexers == null)
531 indexers = new ArrayList ();
533 if (i.IsExplicitImpl)
534 AddMemberToList (i, indexers, true);
536 AddMemberToList (i, indexers, false);
539 public void AddOperator (Operator op)
544 if (operators == null)
545 operators = new OperatorArrayList (this);
550 public void AddCompilerGeneratedClass (CompilerGeneratedClass c)
552 Report.Debug (64, "ADD COMPILER GENERATED CLASS", this, c);
554 if (compiler_generated == null)
555 compiler_generated = new ArrayList ();
557 compiler_generated.Add (c);
560 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
562 if (a.Type == TypeManager.default_member_type) {
563 if (Indexers != null) {
564 Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
569 base.ApplyAttributeBuilder (a, cb);
572 public override AttributeTargets AttributeTargets {
574 throw new NotSupportedException ();
578 public ArrayList Types {
584 public MemberCoreArrayList Methods {
590 public ArrayList Constants {
596 protected Type BaseType {
598 return TypeBuilder.BaseType;
602 public ArrayList Fields {
608 public ArrayList InstanceConstructors {
610 return instance_constructors;
614 public ArrayList Properties {
620 public ArrayList Events {
626 public ArrayList Indexers {
632 public ArrayList Operators {
638 public ArrayList Delegates {
644 protected override TypeAttributes TypeAttr {
646 return Modifiers.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
650 public string IndexerName {
652 return indexers == null ? DefaultIndexerName : indexer_name;
656 public bool IsComImport {
658 if (OptAttributes == null || TypeManager.comimport_attr_type == null)
661 return OptAttributes.Contains (TypeManager.comimport_attr_type);
665 public virtual void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
667 if ((field.ModFlags & Modifiers.STATIC) != 0){
668 if (initialized_static_fields == null) {
669 PartialContainer.HasStaticFieldInitializer = true;
670 initialized_static_fields = new ArrayList (4);
673 initialized_static_fields.Add (expression);
675 if (initialized_fields == null)
676 initialized_fields = new ArrayList (4);
678 initialized_fields.Add (expression);
682 public void ResolveFieldInitializers (EmitContext ec)
684 // Field initializers are tricky for partial classes. They have to
685 // share same costructor (block) but they have they own resolve scope.
686 DeclSpace orig = ec.DeclContainer;
688 if (partial_parts != null) {
689 foreach (TypeContainer part in partial_parts) {
690 ec.DeclContainer = part;
691 part.DoResolveFieldInitializers (ec);
694 ec.DeclContainer = PartialContainer;
695 DoResolveFieldInitializers (ec);
696 ec.DeclContainer = orig;
699 void DoResolveFieldInitializers (EmitContext ec)
702 if (initialized_static_fields == null)
705 bool has_complex_initializer = false;
706 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
707 foreach (FieldInitializer fi in initialized_static_fields) {
708 fi.ResolveStatement (ec);
709 if (!fi.IsComplexInitializer)
712 has_complex_initializer = true;
715 // Need special check to not optimize code like this
716 // static int a = b = 5;
718 if (!has_complex_initializer && RootContext.Optimize) {
719 for (int i = 0; i < initialized_static_fields.Count; ++i) {
720 FieldInitializer fi = (FieldInitializer) initialized_static_fields [i];
721 if (fi.IsDefaultInitializer) {
722 initialized_static_fields.RemoveAt (i);
732 if (initialized_fields == null)
735 using (ec.Set (EmitContext.Flags.InFieldInitializer)) {
736 for (int i = 0; i < initialized_fields.Count; ++i) {
737 FieldInitializer fi = (FieldInitializer) initialized_fields [i];
738 fi.ResolveStatement (ec);
739 if (fi.IsDefaultInitializer && RootContext.Optimize) {
740 // Field is re-initialized to its default value => removed
741 initialized_fields.RemoveAt (i);
749 // Emits the instance field initializers
751 public bool EmitFieldInitializers (EmitContext ec)
753 if (partial_parts != null) {
754 foreach (TypeContainer part in partial_parts)
755 part.EmitFieldInitializers (ec);
761 fields = initialized_static_fields;
763 fields = initialized_fields;
769 foreach (FieldInitializer f in fields) {
770 f.EmitStatement (ec);
775 public override string DocComment {
787 public PendingImplementation PendingImplementations {
788 get { return pending; }
791 public override bool GetClsCompliantAttributeValue ()
793 if (PartialContainer != this)
794 return PartialContainer.GetClsCompliantAttributeValue ();
796 return base.GetClsCompliantAttributeValue ();
799 public virtual void AddBasesForPart (DeclSpace part, ArrayList bases)
801 // FIXME: get rid of partial_parts and store lists of bases of each part here
802 // assumed, not verified: 'part' is in 'partial_parts'
803 ((TypeContainer) part).type_bases = bases;
807 /// This function computes the Base class and also the
808 /// list of interfaces that the class or struct @c implements.
810 /// The return value is an array (might be null) of
811 /// interfaces implemented (as Types).
813 /// The @base_class argument is set to the base object or null
814 /// if this is `System.Object'.
816 protected virtual TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
819 if (type_bases == null)
822 int count = type_bases.Count;
823 TypeExpr [] ifaces = null;
824 for (int i = 0, j = 0; i < count; i++){
825 FullNamedExpression fne = (FullNamedExpression) type_bases [i];
828 // Standard ResolveAsTypeTerminal cannot be used in this case because
829 // it does ObsoleteAttribute and constraint checks which require
830 // base type to be set
832 TypeExpr fne_resolved = fne.ResolveAsBaseTerminal (this, false);
833 if (fne_resolved == null)
836 if (i == 0 && Kind == Kind.Class && !fne_resolved.Type.IsInterface) {
837 base_class = fne_resolved;
842 ifaces = new TypeExpr [count - i];
844 if (fne_resolved.Type.IsInterface) {
845 for (int ii = 0; ii < j; ++ii) {
846 if (TypeManager.IsEqual (fne_resolved.Type, ifaces [ii].Type)) {
847 Report.Error (528, Location, "`{0}' is already listed in interface list",
848 fne_resolved.GetSignatureForError ());
853 if (Kind == Kind.Interface && !fne_resolved.AsAccessible (this)) {
854 Report.Error (61, fne.Location,
855 "Inconsistent accessibility: base interface `{0}' is less accessible than interface `{1}'",
856 fne_resolved.GetSignatureForError (), GetSignatureForError ());
859 Report.SymbolRelatedToPreviousError (fne_resolved.Type);
860 if (Kind != Kind.Class) {
861 Report.Error (527, fne.Location, "Type `{0}' in interface list is not an interface", fne_resolved.GetSignatureForError ());
862 } else if (base_class != null)
863 Report.Error (1721, fne.Location, "`{0}': Classes cannot have multiple base classes (`{1}' and `{2}')",
864 GetSignatureForError (), base_class.GetSignatureForError (), fne_resolved.GetSignatureForError ());
866 Report.Error (1722, fne.Location, "`{0}': Base class `{1}' must be specified as first",
867 GetSignatureForError (), fne_resolved.GetSignatureForError ());
871 ifaces [j++] = fne_resolved;
877 TypeExpr[] GetNormalPartialBases (ref TypeExpr base_class)
879 ArrayList ifaces = new ArrayList (0);
880 if (iface_exprs != null)
881 ifaces.AddRange (iface_exprs);
883 foreach (TypeContainer part in partial_parts) {
884 TypeExpr new_base_class;
885 TypeExpr[] new_ifaces = part.ResolveBaseTypes (out new_base_class);
886 if (new_base_class != TypeManager.system_object_expr) {
887 if (base_class == TypeManager.system_object_expr)
888 base_class = new_base_class;
890 if (new_base_class != null && !new_base_class.Equals (base_class)) {
891 Report.SymbolRelatedToPreviousError (base_class.Location, "");
892 Report.Error (263, part.Location,
893 "Partial declarations of `{0}' must not specify different base classes",
894 part.GetSignatureForError ());
901 if (new_ifaces == null)
904 foreach (TypeExpr iface in new_ifaces) {
905 if (ifaces.Contains (iface))
912 if (ifaces.Count == 0)
915 return (TypeExpr[])ifaces.ToArray (typeof (TypeExpr));
918 bool CheckGenericInterfaces (Type[] ifaces)
921 ArrayList already_checked = new ArrayList ();
923 for (int i = 0; i < ifaces.Length; i++) {
924 Type iface = ifaces [i];
925 foreach (Type t in already_checked) {
929 Type[] inferred = new Type [CountTypeParameters];
930 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t, inferred, null))
933 Report.Error (695, Location,
934 "`{0}' cannot implement both `{1}' and `{2}' " +
935 "because they may unify for some type parameter substitutions",
936 TypeManager.CSharpName (TypeBuilder), TypeManager.CSharpName (iface),
937 TypeManager.CSharpName (t));
941 already_checked.Add (iface);
950 bool CreateTypeBuilder ()
953 Type default_parent = null;
954 if (Kind == Kind.Struct)
955 default_parent = TypeManager.value_type;
956 else if (Kind == Kind.Enum)
957 default_parent = TypeManager.enum_type;
960 // Sets .size to 1 for structs with no instance fields
962 int type_size = Kind == Kind.Struct && first_nonstatic_field == null ? 1 : 0;
965 if (TypeManager.NamespaceClash (Name, Location)) {
969 ModuleBuilder builder = CodeGen.Module.Builder;
970 TypeBuilder = builder.DefineType (
971 Name, TypeAttr, default_parent, type_size);
973 TypeBuilder builder = Parent.TypeBuilder;
975 TypeBuilder = builder.DefineNestedType (
976 Basename, TypeAttr, default_parent, type_size);
978 } catch (ArgumentException) {
979 Report.RuntimeMissingSupport (Location, "static classes");
983 TypeManager.AddUserType (this);
987 string[] param_names = new string [TypeParameters.Length];
988 for (int i = 0; i < TypeParameters.Length; i++)
989 param_names [i] = TypeParameters [i].Name;
991 GenericTypeParameterBuilder[] gen_params = TypeBuilder.DefineGenericParameters (param_names);
993 int offset = CountTypeParameters - CurrentTypeParameters.Length;
994 for (int i = offset; i < gen_params.Length; i++)
995 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1002 bool DefineBaseTypes ()
1004 iface_exprs = ResolveBaseTypes (out base_type);
1005 if (partial_parts != null) {
1006 iface_exprs = GetNormalPartialBases (ref base_type);
1010 // GetClassBases calls ResolveBaseTypeExpr() on the various type expressions involved,
1011 // which in turn should have called DefineType()s on base types if necessary.
1013 // None of the code below should trigger DefineType()s on classes that we depend on.
1014 // Thus, we are eligible to be on the topological sort `type_container_resolve_order'.
1016 // Let's do it as soon as possible, since code below can call DefineType() on classes
1017 // that depend on us to be populated before they are.
1019 if (!(this is CompilerGeneratedClass))
1020 RootContext.RegisterOrder (this);
1022 if (!CheckRecursiveDefinition (this))
1025 if (base_type != null && base_type.Type != null) {
1026 TypeBuilder.SetParent (base_type.Type);
1029 // add interfaces that were not added at type creation
1030 if (iface_exprs != null) {
1031 ifaces = TypeManager.ExpandInterfaces (iface_exprs);
1035 foreach (Type itype in ifaces)
1036 TypeBuilder.AddInterfaceImplementation (itype);
1038 if (!CheckGenericInterfaces (ifaces))
1041 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1048 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1050 public TypeBuilder CreateType ()
1052 if (TypeBuilder != null)
1058 if (!CreateTypeBuilder ()) {
1063 if (partial_parts != null) {
1064 foreach (TypeContainer part in partial_parts)
1065 part.TypeBuilder = TypeBuilder;
1068 if (Types != null) {
1069 foreach (TypeContainer tc in Types) {
1070 if (tc.CreateType () == null) {
1082 public override TypeBuilder DefineType ()
1089 type_defined = true;
1091 if (CreateType () == null) {
1096 if (!DefineBaseTypes ()) {
1101 if (!ResolveMembers ()) {
1106 if (!DefineNestedTypes ()) {
1114 bool ResolveMembers ()
1116 if (members_resolved)
1117 return members_resolved_ok;
1119 members_resolved_ok = DoResolveMembers ();
1120 members_resolved = true;
1122 return members_resolved_ok;
1125 protected virtual bool DoResolveMembers ()
1127 if (methods != null) {
1128 foreach (Method method in methods) {
1129 if (!method.ResolveMembers ())
1134 // TODO: this is not really needed
1135 if (operators != null) {
1136 foreach (Operator o in operators) {
1137 if (!o.ResolveMembers ())
1145 public override void SetParameterInfo (ArrayList constraints_list)
1147 base.SetParameterInfo (constraints_list);
1149 if (!is_generic || PartialContainer == this)
1152 TypeParameter[] tc_names = PartialContainer.TypeParameters;
1153 for (int i = 0; i < tc_names.Length; ++i) {
1154 if (tc_names [i].Name != type_params [i].Name) {
1155 Report.SymbolRelatedToPreviousError (PartialContainer.Location, "");
1156 Report.Error (264, Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
1157 GetSignatureForError ());
1163 void UpdateTypeParameterConstraints (TypeContainer part)
1165 TypeParameter[] current_params = CurrentTypeParameters;
1166 for (int i = 0; i < current_params.Length; i++) {
1167 Constraints c = part.CurrentTypeParameters [i].Constraints;
1171 if (current_params [i].UpdateConstraints (part, c))
1174 Report.SymbolRelatedToPreviousError (Location, "");
1175 Report.Error (265, part.Location,
1176 "Partial declarations of `{0}' have inconsistent constraints for type parameter `{1}'",
1177 GetSignatureForError (), current_params [i].GetSignatureForError ());
1181 public bool ResolveType ()
1183 if (!DoResolveType ())
1186 if (compiler_generated != null) {
1187 foreach (CompilerGeneratedClass c in compiler_generated)
1188 if (!c.ResolveType ())
1195 protected virtual bool DoResolveType ()
1197 if ((base_type != null) &&
1198 (base_type.ResolveAsTypeTerminal (this, false) == null)) {
1206 if (PartialContainer != this)
1207 throw new InternalErrorException ();
1209 TypeExpr current_type = null;
1211 foreach (TypeParameter type_param in CurrentTypeParameters) {
1212 if (!type_param.Resolve (this)) {
1218 if (partial_parts != null && is_generic) {
1219 foreach (TypeContainer part in partial_parts)
1220 UpdateTypeParameterConstraints (part);
1223 foreach (TypeParameter type_param in TypeParameters) {
1224 if (!type_param.DefineType (this)) {
1230 current_type = new ConstructedType (TypeBuilder, TypeParameters, Location);
1232 foreach (TypeParameter type_param in TypeParameters)
1233 if (!type_param.CheckDependencies ()) {
1238 if (current_type != null) {
1239 current_type = current_type.ResolveAsTypeTerminal (this, false);
1240 if (current_type == null) {
1245 CurrentType = current_type.Type;
1251 protected virtual bool DefineNestedTypes ()
1253 if (Types != null) {
1254 foreach (TypeContainer tc in Types)
1255 if (tc.DefineType () == null)
1259 if (Delegates != null) {
1260 foreach (Delegate d in Delegates)
1261 if (d.DefineType () == null)
1268 TypeContainer InTransit;
1270 protected bool CheckRecursiveDefinition (TypeContainer tc)
1272 if (InTransit != null) {
1273 Report.SymbolRelatedToPreviousError (this);
1274 if (this is Interface)
1276 529, tc.Location, "Inherited interface `{0}' causes a " +
1277 "cycle in the interface hierarchy of `{1}'",
1278 GetSignatureForError (), tc.GetSignatureForError ());
1281 146, tc.Location, "Circular base class dependency " +
1282 "involving `{0}' and `{1}'",
1283 tc.GetSignatureForError (), GetSignatureForError ());
1289 if (base_type != null && base_type.Type != null) {
1290 Type t = TypeManager.DropGenericTypeArguments (base_type.Type);
1291 TypeContainer ptc = TypeManager.LookupTypeContainer (t);
1292 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1296 if (iface_exprs != null) {
1297 foreach (TypeExpr iface in iface_exprs) {
1298 Type itype = TypeManager.DropGenericTypeArguments (iface.Type);
1299 TypeContainer ptc = TypeManager.LookupTypeContainer (itype);
1300 if ((ptc != null) && !ptc.CheckRecursiveDefinition (this))
1305 if (!IsTopLevel && !Parent.PartialContainer.CheckRecursiveDefinition (this))
1312 public static void Error_KeywordNotAllowed (Location loc)
1314 Report.Error (1530, loc, "Keyword `new' is not allowed on namespace elements");
1318 /// Populates our TypeBuilder with fields and methods
1320 public override bool DefineMembers ()
1322 if (members_defined)
1323 return members_defined_ok;
1325 if (!base.DefineMembers ())
1328 members_defined_ok = DoDefineMembers ();
1329 members_defined = true;
1331 return members_defined_ok;
1334 protected virtual bool DoDefineMembers ()
1336 if (iface_exprs != null) {
1337 foreach (TypeExpr iface in iface_exprs) {
1338 ObsoleteAttribute oa = AttributeTester.GetObsoleteAttribute (iface.Type);
1339 if ((oa != null) && !IsInObsoleteScope)
1340 AttributeTester.Report_ObsoleteMessage (
1341 oa, iface.GetSignatureForError (), Location);
1343 ConstructedType ct = iface as ConstructedType;
1344 if ((ct != null) && !ct.CheckConstraints (this))
1349 if (base_type != null) {
1350 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (base_type.Type);
1351 if (obsolete_attr != null && !IsInObsoleteScope)
1352 AttributeTester.Report_ObsoleteMessage (obsolete_attr, base_type.GetSignatureForError (), Location);
1354 ConstructedType ct = base_type as ConstructedType;
1355 if ((ct != null) && !ct.CheckConstraints (this))
1358 member_cache = new MemberCache (base_type.Type, this);
1359 } else if (Kind == Kind.Interface) {
1360 member_cache = new MemberCache (null, this);
1361 Type [] ifaces = TypeManager.GetInterfaces (TypeBuilder);
1362 for (int i = 0; i < ifaces.Length; ++i)
1363 member_cache.AddInterface (TypeManager.LookupMemberCache (ifaces [i]));
1365 member_cache = new MemberCache (null, this);
1369 foreach (TypeContainer tc in types)
1370 member_cache.AddNestedType (tc);
1372 if (delegates != null)
1373 foreach (Delegate d in delegates)
1374 member_cache.AddNestedType (d);
1376 if (partial_parts != null) {
1377 foreach (TypeContainer part in partial_parts)
1378 part.member_cache = member_cache;
1382 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Basename, false);
1383 if (conflict_symbol == null) {
1384 if ((ModFlags & Modifiers.NEW) != 0)
1385 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1387 if ((ModFlags & Modifiers.NEW) == 0) {
1388 Report.SymbolRelatedToPreviousError (conflict_symbol);
1389 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
1390 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
1395 DefineContainerMembers (constants);
1396 DefineContainerMembers (fields);
1398 if (Kind == Kind.Struct || Kind == Kind.Class) {
1399 pending = PendingImplementation.GetPendingImplementations (this);
1403 // Constructors are not in the defined_names array
1405 DefineContainerMembers (instance_constructors);
1407 DefineContainerMembers (events);
1408 DefineContainerMembers (ordered_explicit_member_list);
1409 DefineContainerMembers (ordered_member_list);
1411 DefineContainerMembers (operators);
1412 DefineContainerMembers (delegates);
1414 ComputeIndexerName();
1415 CheckEqualsAndGetHashCode();
1417 if (CurrentType != null) {
1418 GenericType = CurrentType;
1423 // FIXME: This hack is needed because member cache does not work
1424 // with generic types, we rely on runtime to inflate dynamic types.
1425 // TODO: This hack requires member cache refactoring to be removed
1427 if (TypeBuilder.IsGenericType)
1428 member_cache = new MemberCache (this);
1434 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1437 mcal.DefineContainerMembers ();
1440 protected virtual void ComputeIndexerName ()
1442 if (indexers == null)
1445 string class_indexer_name = null;
1448 // If there's both an explicit and an implicit interface implementation, the
1449 // explicit one actually implements the interface while the other one is just
1450 // a normal indexer. See bug #37714.
1453 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
1454 foreach (Indexer i in indexers) {
1455 if (i.InterfaceType != null) {
1456 if (seen_normal_indexers)
1457 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
1461 seen_normal_indexers = true;
1463 if (class_indexer_name == null) {
1464 class_indexer_name = i.ShortName;
1468 if (i.ShortName != class_indexer_name)
1469 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");
1472 if (class_indexer_name != null)
1473 indexer_name = class_indexer_name;
1476 protected virtual void EmitIndexerName ()
1478 if (!seen_normal_indexers)
1481 if (TypeManager.default_member_ctor == null) {
1482 if (TypeManager.default_member_type == null) {
1483 TypeManager.default_member_type = TypeManager.CoreLookupType (
1484 "System.Reflection", "DefaultMemberAttribute", Kind.Class, true);
1486 if (TypeManager.default_member_type == null)
1490 TypeManager.default_member_ctor = TypeManager.GetPredefinedConstructor (
1491 TypeManager.default_member_type, Location, TypeManager.string_type);
1493 if (TypeManager.default_member_ctor == null)
1497 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
1498 TypeBuilder.SetCustomAttribute (cb);
1501 protected virtual void CheckEqualsAndGetHashCode ()
1503 if (methods == null)
1506 if (HasEquals && !HasGetHashCode) {
1507 Report.Warning (659, 3, this.Location, "`{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", this.GetSignatureForError ());
1511 public override bool Define ()
1513 CheckProtectedModifier ();
1517 public MemberInfo FindBaseMemberWithSameName (string name, bool ignore_methods)
1519 return BaseCache == null ? null : BaseCache.FindMemberWithSameName (name, ignore_methods, null);
1523 /// This function is based by a delegate to the FindMembers routine
1525 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1531 /// This filter is used by FindMembers, and we just keep
1532 /// a global for the filter to `AlwaysAccept'
1534 static MemberFilter accepting_filter;
1537 static TypeContainer ()
1539 accepting_filter = new MemberFilter (AlwaysAccept);
1542 public MethodInfo[] GetMethods ()
1544 ArrayList members = new ArrayList ();
1548 if (methods != null) {
1549 int len = methods.Count;
1550 for (int i = 0; i < len; i++) {
1551 Method m = (Method) methods [i];
1553 members.Add (m.MethodBuilder);
1557 if (operators != null) {
1558 int len = operators.Count;
1559 for (int i = 0; i < len; i++) {
1560 Operator o = (Operator) operators [i];
1562 members.Add (o.MethodBuilder);
1566 if (properties != null) {
1567 int len = properties.Count;
1568 for (int i = 0; i < len; i++) {
1569 Property p = (Property) properties [i];
1571 if (p.GetBuilder != null)
1572 members.Add (p.GetBuilder);
1573 if (p.SetBuilder != null)
1574 members.Add (p.SetBuilder);
1578 if (indexers != null) {
1579 int len = indexers.Count;
1580 for (int i = 0; i < len; i++) {
1581 Indexer ix = (Indexer) indexers [i];
1583 if (ix.GetBuilder != null)
1584 members.Add (ix.GetBuilder);
1585 if (ix.SetBuilder != null)
1586 members.Add (ix.SetBuilder);
1590 if (events != null) {
1591 int len = events.Count;
1592 for (int i = 0; i < len; i++) {
1593 Event e = (Event) events [i];
1595 if (e.AddBuilder != null)
1596 members.Add (e.AddBuilder);
1597 if (e.RemoveBuilder != null)
1598 members.Add (e.RemoveBuilder);
1602 MethodInfo[] retMethods = new MethodInfo [members.Count];
1603 members.CopyTo (retMethods, 0);
1607 // Indicated whether container has StructLayout attribute set Explicit
1608 public bool HasExplicitLayout {
1609 get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
1610 set { caching_flags |= Flags.HasExplicitLayout; }
1614 // Return the nested type with name @name. Ensures that the nested type
1615 // is defined if necessary. Do _not_ use this when you have a MemberCache handy.
1617 public Type FindNestedType (string name)
1619 if (PartialContainer != this)
1620 throw new InternalErrorException ("should not happen");
1622 ArrayList [] lists = { types, delegates };
1624 for (int j = 0; j < lists.Length; ++j) {
1625 ArrayList list = lists [j];
1629 int len = list.Count;
1630 for (int i = 0; i < len; ++i) {
1631 DeclSpace ds = (DeclSpace) list [i];
1632 if (ds.Basename == name) {
1633 return ds.DefineType ();
1641 private void FindMembers_NestedTypes (int modflags,
1642 BindingFlags bf, MemberFilter filter, object criteria,
1643 ref ArrayList members)
1645 ArrayList [] lists = { types, delegates };
1647 for (int j = 0; j < lists.Length; ++j) {
1648 ArrayList list = lists [j];
1652 int len = list.Count;
1653 for (int i = 0; i < len; i++) {
1654 DeclSpace ds = (DeclSpace) list [i];
1656 if ((ds.ModFlags & modflags) == 0)
1659 TypeBuilder tb = ds.TypeBuilder;
1661 if (!(criteria is string) || ds.Basename.Equals (criteria))
1662 tb = ds.DefineType ();
1665 if (tb != null && (filter (tb, criteria) == true)) {
1666 if (members == null)
1667 members = new ArrayList ();
1676 /// This method returns the members of this type just like Type.FindMembers would
1677 /// Only, we need to use this for types which are _being_ defined because MS'
1678 /// implementation can't take care of that.
1681 // FIXME: return an empty static array instead of null, that cleans up
1682 // some code and is consistent with some coding conventions I just found
1686 // Notice that in various cases we check if our field is non-null,
1687 // something that would normally mean that there was a bug elsewhere.
1689 // The problem happens while we are defining p-invoke methods, as those
1690 // will trigger a FindMembers, but this happens before things are defined
1692 // Since the whole process is a no-op, it is fine to check for null here.
1694 // TODO: This approach will be one day completely removed, it's already
1695 // used at few places only
1698 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1699 MemberFilter filter, object criteria)
1701 ArrayList members = null;
1704 if ((bf & BindingFlags.Public) != 0)
1705 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1707 if ((bf & BindingFlags.NonPublic) != 0)
1708 modflags |= Modifiers.PRIVATE;
1710 int static_mask = 0, static_flags = 0;
1711 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1712 case BindingFlags.Static:
1713 static_mask = static_flags = Modifiers.STATIC;
1716 case BindingFlags.Instance:
1717 static_mask = Modifiers.STATIC;
1722 static_mask = static_flags = 0;
1726 Timer.StartTimer (TimerType.TcFindMembers);
1729 filter = accepting_filter;
1731 if ((mt & MemberTypes.Field) != 0) {
1732 if (fields != null) {
1733 int len = fields.Count;
1734 for (int i = 0; i < len; i++) {
1735 FieldBase f = (FieldBase) fields [i];
1737 if ((f.ModFlags & modflags) == 0)
1739 if ((f.ModFlags & static_mask) != static_flags)
1742 FieldBuilder fb = f.FieldBuilder;
1743 if (fb != null && filter (fb, criteria) == true) {
1744 if (members == null)
1745 members = new ArrayList ();
1752 if (constants != null) {
1753 int len = constants.Count;
1754 for (int i = 0; i < len; i++) {
1755 Const con = (Const) constants [i];
1757 if ((con.ModFlags & modflags) == 0)
1759 if ((con.ModFlags & static_mask) != static_flags)
1762 FieldBuilder fb = con.FieldBuilder;
1765 fb = con.FieldBuilder;
1767 if (fb != null && filter (fb, criteria) == true) {
1768 if (members == null)
1769 members = new ArrayList ();
1777 if ((mt & MemberTypes.Method) != 0) {
1778 if (methods != null) {
1779 int len = methods.Count;
1780 for (int i = 0; i < len; i++) {
1781 Method m = (Method) methods [i];
1783 if ((m.ModFlags & modflags) == 0)
1785 if ((m.ModFlags & static_mask) != static_flags)
1788 MethodBuilder mb = m.MethodBuilder;
1790 if (mb != null && filter (mb, criteria) == true) {
1791 if (members == null)
1792 members = new ArrayList ();
1799 if (operators != null) {
1800 int len = operators.Count;
1801 for (int i = 0; i < len; i++) {
1802 Operator o = (Operator) operators [i];
1804 if ((o.ModFlags & modflags) == 0)
1806 if ((o.ModFlags & static_mask) != static_flags)
1809 MethodBuilder ob = o.MethodBuilder;
1810 if (ob != null && filter (ob, criteria) == true) {
1811 if (members == null)
1812 members = new ArrayList ();
1819 if (events != null) {
1820 foreach (Event e in events) {
1821 if ((e.ModFlags & modflags) == 0)
1823 if ((e.ModFlags & static_mask) != static_flags)
1826 MethodBuilder b = e.AddBuilder;
1827 if (b != null && filter (b, criteria)) {
1828 if (members == null)
1829 members = new ArrayList (4);
1834 b = e.RemoveBuilder;
1835 if (b != null && filter (b, criteria)) {
1836 if (members == null)
1837 members = new ArrayList (4);
1844 if (properties != null) {
1845 int len = properties.Count;
1846 for (int i = 0; i < len; i++) {
1847 Property p = (Property) properties [i];
1849 if ((p.ModFlags & modflags) == 0)
1851 if ((p.ModFlags & static_mask) != static_flags)
1857 if (b != null && filter (b, criteria) == true) {
1858 if (members == null)
1859 members = new ArrayList ();
1865 if (b != null && filter (b, criteria) == true) {
1866 if (members == null)
1867 members = new ArrayList ();
1874 if (indexers != null) {
1875 int len = indexers.Count;
1876 for (int i = 0; i < len; i++) {
1877 Indexer ix = (Indexer) indexers [i];
1879 if ((ix.ModFlags & modflags) == 0)
1881 if ((ix.ModFlags & static_mask) != static_flags)
1887 if (b != null && filter (b, criteria) == true) {
1888 if (members == null)
1889 members = new ArrayList ();
1895 if (b != null && filter (b, criteria) == true) {
1896 if (members == null)
1897 members = new ArrayList ();
1905 if ((mt & MemberTypes.Event) != 0) {
1906 if (events != null) {
1907 int len = events.Count;
1908 for (int i = 0; i < len; i++) {
1909 Event e = (Event) events [i];
1911 if ((e.ModFlags & modflags) == 0)
1913 if ((e.ModFlags & static_mask) != static_flags)
1916 MemberInfo eb = e.EventBuilder;
1917 if (eb != null && filter (eb, criteria) == true) {
1918 if (members == null)
1919 members = new ArrayList ();
1921 members.Add (e.EventBuilder);
1927 if ((mt & MemberTypes.Property) != 0){
1928 if (properties != null) {
1929 int len = properties.Count;
1930 for (int i = 0; i < len; i++) {
1931 Property p = (Property) properties [i];
1933 if ((p.ModFlags & modflags) == 0)
1935 if ((p.ModFlags & static_mask) != static_flags)
1938 MemberInfo pb = p.PropertyBuilder;
1939 if (pb != null && filter (pb, criteria) == true) {
1940 if (members == null)
1941 members = new ArrayList ();
1943 members.Add (p.PropertyBuilder);
1948 if (indexers != null) {
1949 int len = indexers.Count;
1950 for (int i = 0; i < len; i++) {
1951 Indexer ix = (Indexer) indexers [i];
1953 if ((ix.ModFlags & modflags) == 0)
1955 if ((ix.ModFlags & static_mask) != static_flags)
1958 MemberInfo ib = ix.PropertyBuilder;
1959 if (ib != null && filter (ib, criteria) == true) {
1960 if (members == null)
1961 members = new ArrayList ();
1963 members.Add (ix.PropertyBuilder);
1969 if ((mt & MemberTypes.NestedType) != 0)
1970 FindMembers_NestedTypes (modflags, bf, filter, criteria, ref members);
1972 if ((mt & MemberTypes.Constructor) != 0){
1973 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
1974 int len = instance_constructors.Count;
1975 for (int i = 0; i < len; i++) {
1976 Constructor c = (Constructor) instance_constructors [i];
1978 ConstructorBuilder cb = c.ConstructorBuilder;
1979 if (cb != null && filter (cb, criteria) == true) {
1980 if (members == null)
1981 members = new ArrayList ();
1988 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
1989 ConstructorBuilder cb =
1990 default_static_constructor.ConstructorBuilder;
1992 if (cb != null && filter (cb, criteria) == true) {
1993 if (members == null)
1994 members = new ArrayList ();
2002 // Lookup members in base if requested.
2004 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2005 if (TypeBuilder.BaseType != null) {
2006 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2007 if (list.Count > 0) {
2008 if (members == null)
2009 members = new ArrayList ();
2011 members.AddRange (list);
2016 Timer.StopTimer (TimerType.TcFindMembers);
2018 if (members == null)
2019 return MemberList.Empty;
2021 return new MemberList (members);
2024 public override MemberCache MemberCache {
2026 return member_cache;
2030 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2031 MemberFilter filter, object criteria)
2033 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2036 return ds.FindMembers (mt, bf, filter, criteria);
2038 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2042 /// Emits the values for the constants
2044 public void EmitConstants ()
2046 if (constants != null)
2047 foreach (Const con in constants)
2052 static void CheckMemberUsage (MemberCoreArrayList al, string member_type)
2057 foreach (MemberCore mc in al) {
2058 if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2061 if (!mc.IsUsed && (mc.caching_flags & Flags.Excluded) == 0) {
2062 Report.Warning (169, 3, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ());
2067 public virtual void VerifyMembers ()
2070 // Check for internal or private fields that were never assigned
2072 if (Report.WarningLevel >= 3) {
2073 CheckMemberUsage (properties, "property");
2074 CheckMemberUsage (methods, "method");
2075 CheckMemberUsage (constants, "constant");
2077 if (fields != null){
2078 bool is_type_exposed = Kind == Kind.Struct || IsExposedFromAssembly ();
2079 foreach (FieldBase f in fields) {
2080 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) {
2081 if (is_type_exposed)
2084 f.SetMemberIsUsed ();
2088 if ((f.caching_flags & Flags.IsAssigned) == 0)
2089 Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ());
2092 const int error_code = 414;
2094 const int error_code = 169;
2096 Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used",
2097 f.GetSignatureForError ());
2103 // Only report 649 on level 4
2105 if (Report.WarningLevel < 4)
2108 if ((f.caching_flags & Flags.IsAssigned) != 0)
2111 Constant c = New.Constantify (f.MemberType);
2112 Report.Warning (649, 4, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'",
2113 f.GetSignatureForError (), c == null ? "null" : c.AsString ());
2119 // TODO: move to ClassOrStruct
2120 void EmitConstructors ()
2122 if (instance_constructors == null)
2125 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsComplianceRequired ()) {
2126 bool has_compliant_args = false;
2128 foreach (Constructor c in instance_constructors) {
2132 catch (Exception e) {
2133 throw new InternalErrorException (c, e);
2136 if (has_compliant_args)
2139 has_compliant_args = c.HasCompliantArgs;
2141 if (!has_compliant_args)
2142 Report.Warning (3015, 1, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2144 foreach (Constructor c in instance_constructors) {
2148 catch (Exception e) {
2149 throw new InternalErrorException (c, e);
2156 /// Emits the code, this step is performed after all
2157 /// the types, enumerations, constructors
2159 public virtual void EmitType ()
2161 if (OptAttributes != null)
2162 OptAttributes.Emit ();
2166 EmitConstructors ();
2168 // Can not continue if constants are broken
2170 if (Report.Errors > 0)
2173 if (default_static_constructor != null)
2174 default_static_constructor.Emit ();
2176 if (operators != null)
2177 foreach (Operator o in operators)
2180 if (properties != null)
2181 foreach (Property p in properties)
2184 if (indexers != null) {
2185 foreach (Indexer indx in indexers)
2190 if (events != null){
2191 foreach (Event e in Events)
2195 if (methods != null) {
2196 for (int i = 0; i < methods.Count; ++i)
2197 ((Method) methods [i]).Emit ();
2201 foreach (FieldBase f in fields)
2204 if (delegates != null) {
2205 foreach (Delegate d in Delegates) {
2210 if (pending != null)
2211 pending.VerifyPendingMethods ();
2213 if (Report.Errors > 0)
2216 if (compiler_generated != null) {
2217 for (int i = 0; i < compiler_generated.Count; ++i)
2218 ((CompilerGeneratedClass) compiler_generated [i]).EmitType ();
2222 public override void CloseType ()
2224 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2228 caching_flags |= Flags.CloseTypeCreated;
2229 TypeBuilder.CreateType ();
2230 } catch (TypeLoadException){
2232 // This is fine, the code still created the type
2234 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2235 // Console.WriteLine (e.Message);
2236 } catch (Exception e) {
2237 throw new InternalErrorException (this, e);
2241 foreach (TypeContainer tc in Types)
2242 if (tc.Kind == Kind.Struct)
2245 foreach (TypeContainer tc in Types)
2246 if (tc.Kind != Kind.Struct)
2250 if (Delegates != null)
2251 foreach (Delegate d in Delegates)
2254 if (compiler_generated != null)
2255 foreach (CompilerGeneratedClass c in compiler_generated)
2262 initialized_fields = null;
2263 initialized_static_fields = null;
2265 ordered_explicit_member_list = null;
2266 ordered_member_list = null;
2271 compiler_generated = null;
2272 default_constructor = null;
2273 default_static_constructor = null;
2275 OptAttributes = null;
2278 member_cache = null;
2282 // Performs the validation on a Method's modifiers (properties have
2283 // the same properties).
2285 public bool MethodModifiersValid (MemberCore mc)
2287 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2288 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2289 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2291 int flags = mc.ModFlags;
2294 // At most one of static, virtual or override
2296 if ((flags & Modifiers.STATIC) != 0){
2297 if ((flags & vao) != 0){
2298 Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract",
2299 mc.GetSignatureForError ());
2304 if (Kind == Kind.Struct){
2305 if ((flags & va) != 0){
2306 Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract");
2311 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2312 Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual",
2313 mc.GetSignatureForError ());
2318 // If the declaration includes the abstract modifier, then the
2319 // declaration does not include static, virtual or extern
2321 if ((flags & Modifiers.ABSTRACT) != 0){
2322 if ((flags & Modifiers.EXTERN) != 0){
2324 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ());
2328 if ((flags & Modifiers.SEALED) != 0) {
2329 Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ());
2333 if ((flags & Modifiers.VIRTUAL) != 0){
2334 Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ());
2338 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2339 Report.SymbolRelatedToPreviousError (this);
2340 Report.Error (513, mc.Location, "`{0}' is abstract but it is declared in the non-abstract class `{1}'",
2341 mc.GetSignatureForError (), GetSignatureForError ());
2346 if ((flags & Modifiers.PRIVATE) != 0){
2347 if ((flags & vao) != 0){
2348 Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ());
2353 if ((flags & Modifiers.SEALED) != 0){
2354 if ((flags & Modifiers.OVERRIDE) == 0){
2355 Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ());
2363 public Constructor DefaultStaticConstructor {
2364 get { return default_static_constructor; }
2367 protected override bool VerifyClsCompliance ()
2369 if (!base.VerifyClsCompliance ())
2374 Type base_type = TypeBuilder.BaseType;
2375 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2376 Report.Warning (3009, 1, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2383 /// Checks whether container name is CLS Compliant
2385 void VerifyClsName ()
2387 Hashtable base_members = base_cache == null ?
2389 base_cache.GetPublicMembers ();
2390 Hashtable this_members = new Hashtable ();
2392 foreach (DictionaryEntry entry in defined_names) {
2393 MemberCore mc = (MemberCore)entry.Value;
2394 if (!mc.IsClsComplianceRequired ())
2397 string name = (string) entry.Key;
2398 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2400 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2401 object found = base_members [lcase];
2402 if (found == null) {
2403 found = this_members [lcase];
2404 if (found == null) {
2405 this_members.Add (lcase, mc);
2410 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2413 if (found is MemberInfo) {
2414 if (basename == ((MemberInfo) found).Name)
2416 Report.SymbolRelatedToPreviousError ((MemberInfo) found);
2418 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2421 Report.Warning (3005, 1, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2427 /// Performs checks for an explicit interface implementation. First it
2428 /// checks whether the `interface_type' is a base inteface implementation.
2429 /// Then it checks whether `name' exists in the interface type.
2431 public bool VerifyImplements (InterfaceMemberBase mb)
2433 if (ifaces != null) {
2434 foreach (Type t in ifaces){
2435 if (TypeManager.IsEqual (t, mb.InterfaceType))
2440 Report.SymbolRelatedToPreviousError (mb.InterfaceType);
2441 Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'",
2442 mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType));
2446 public override Type LookupAnyGeneric (string typeName)
2448 if (types != null) {
2449 foreach (TypeContainer tc in types) {
2453 int pos = tc.Basename.LastIndexOf ('`');
2454 if (pos == typeName.Length && String.Compare (typeName, 0, tc.Basename, 0, pos) == 0)
2455 return tc.TypeBuilder;
2459 return base.LookupAnyGeneric (typeName);
2462 public void Mark_HasEquals ()
2464 cached_method |= CachedMethods.Equals;
2467 public void Mark_HasGetHashCode ()
2469 cached_method |= CachedMethods.GetHashCode;
2473 /// Method container contains Equals method
2475 public bool HasEquals {
2477 return (cached_method & CachedMethods.Equals) != 0;
2482 /// Method container contains GetHashCode method
2484 public bool HasGetHashCode {
2486 return (cached_method & CachedMethods.GetHashCode) != 0;
2490 public bool HasStaticFieldInitializer {
2492 return (cached_method & CachedMethods.HasStaticFieldInitializer) != 0;
2496 cached_method |= CachedMethods.HasStaticFieldInitializer;
2498 cached_method &= ~CachedMethods.HasStaticFieldInitializer;
2506 string IMemberContainer.Name {
2512 Type IMemberContainer.Type {
2518 bool IMemberContainer.IsInterface {
2520 return Kind == Kind.Interface;
2524 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2526 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2528 if (GenericType != null)
2529 return TypeManager.FindMembers (GenericType, mt, new_bf,
2532 return FindMembers (mt, new_bf, null, null);
2536 // Generates xml doc comments (if any), and if required,
2537 // handle warning report.
2539 internal override void GenerateDocComment (DeclSpace ds)
2541 DocUtil.GenerateTypeDocComment (this, ds);
2544 public override string DocCommentHeader {
2545 get { return "T:"; }
2548 public MemberCache BaseCache {
2550 if (base_cache != null)
2552 if (TypeBuilder.BaseType != null)
2553 base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
2554 if (TypeBuilder.IsInterface)
2555 base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder);
2561 public abstract class ClassOrStruct : TypeContainer {
2562 ListDictionary declarative_security;
2564 public ClassOrStruct (NamespaceEntry ns, DeclSpace parent,
2565 MemberName name, Attributes attrs, Kind kind)
2566 : base (ns, parent, name, attrs, kind)
2570 protected override bool AddToContainer (MemberCore symbol, string name)
2572 if (name == MemberName.Name) {
2573 if (symbol is TypeParameter) {
2574 Report.Error (694, symbol.Location,
2575 "Type parameter `{0}' has same name as " +
2576 "containing type, or method", name);
2580 Report.SymbolRelatedToPreviousError (this);
2581 Report.Error (542, symbol.Location, "`{0}': member names cannot be the same as their enclosing type",
2582 symbol.GetSignatureForError ());
2586 return base.AddToContainer (symbol, name);
2589 public override void VerifyMembers ()
2591 base.VerifyMembers ();
2593 if ((events != null) && Report.WarningLevel >= 3) {
2594 foreach (Event e in events){
2595 // Note: The event can be assigned from same class only, so we can report
2596 // this warning for all accessibility modes
2597 if ((e.caching_flags & Flags.IsUsed) == 0)
2598 Report.Warning (67, 3, e.Location, "The event `{0}' is never used", e.GetSignatureForError ());
2603 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2605 if (a.IsValidSecurityAttribute ()) {
2606 if (declarative_security == null)
2607 declarative_security = new ListDictionary ();
2609 a.ExtractSecurityPermissionSet (declarative_security);
2613 if (a.Type == TypeManager.struct_layout_attribute_type && a.GetLayoutKindValue () == LayoutKind.Explicit) {
2614 PartialContainer.HasExplicitLayout = true;
2617 base.ApplyAttributeBuilder (a, cb);
2621 /// Defines the default constructors
2623 protected void DefineDefaultConstructor (bool is_static)
2625 // The default instance constructor is public
2626 // If the class is abstract, the default constructor is protected
2627 // The default static constructor is private
2631 mods = Modifiers.STATIC | Modifiers.PRIVATE;
2633 mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC;
2636 Constructor c = new Constructor (this, MemberName.Name, mods,
2637 Parameters.EmptyReadOnlyParameters,
2638 new GeneratedBaseInitializer (Location),
2642 c.Block = new ToplevelBlock (null, Location);
2645 public override bool Define ()
2647 if (default_static_constructor == null && PartialContainer.HasStaticFieldInitializer)
2648 DefineDefaultConstructor (true);
2650 if (default_static_constructor != null)
2651 default_static_constructor.Define ();
2653 return base.Define ();
2656 public override void Emit ()
2660 if (declarative_security != null) {
2661 foreach (DictionaryEntry de in declarative_security) {
2662 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2667 public override ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
2669 return NamespaceEntry.LookupExtensionMethod (extensionType, this, name, loc);
2672 protected override TypeAttributes TypeAttr {
2674 if (default_static_constructor == null)
2675 return base.TypeAttr | TypeAttributes.BeforeFieldInit;
2677 return base.TypeAttr;
2683 // TODO: should be sealed
2684 public class Class : ClassOrStruct {
2685 const int AllowedModifiers =
2688 Modifiers.PROTECTED |
2689 Modifiers.INTERNAL |
2691 Modifiers.ABSTRACT |
2696 public const TypeAttributes StaticClassAttribute = TypeAttributes.Abstract | TypeAttributes.Sealed;
2698 public Class (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
2700 : base (ns, parent, name, attrs, Kind.Class)
2702 int accmods = Parent.Parent == null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
2703 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2705 if (IsStatic && RootContext.Version == LanguageVersion.ISO_1) {
2706 Report.FeatureIsNotAvailable (Location, "static classes");
2710 public override void AddBasesForPart (DeclSpace part, ArrayList bases)
2712 if (part.Name == "System.Object")
2713 Report.Error (537, part.Location,
2714 "The class System.Object cannot have a base class or implement an interface.");
2715 base.AddBasesForPart (part, bases);
2718 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2720 if (a.Type == TypeManager.attribute_usage_type) {
2721 if (!TypeManager.IsAttributeType (BaseType) &&
2722 TypeBuilder.FullName != "System.Attribute") {
2723 Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ());
2727 if (a.Type == TypeManager.conditional_attribute_type && !TypeManager.IsAttributeType (BaseType)) {
2728 Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes");
2732 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
2733 !attributes.Contains (TypeManager.guid_attr_type)) {
2734 a.Error_MissingGuidAttribute ();
2738 if (a.Type == TypeManager.extension_attribute_type) {
2739 a.Error_MisusedExtensionAttribute ();
2743 if (AttributeTester.IsAttributeExcluded (a.Type, Location))
2746 base.ApplyAttributeBuilder (a, cb);
2749 public override AttributeTargets AttributeTargets {
2751 return AttributeTargets.Class;
2755 protected override void DefineContainerMembers (MemberCoreArrayList list)
2761 base.DefineContainerMembers (list);
2765 foreach (MemberCore m in list) {
2766 if (m is Operator) {
2767 Report.Error (715, m.Location, "`{0}': Static classes cannot contain user-defined operators", m.GetSignatureForError ());
2771 if (m is Destructor) {
2772 Report.Error (711, m.Location, "`{0}': Static classes cannot contain destructor", GetSignatureForError ());
2776 if ((m.ModFlags & Modifiers.PROTECTED) != 0) {
2777 m.CheckProtectedModifier ();
2782 Report.Error (720, m.Location, "`{0}': cannot declare indexers in a static class", m.GetSignatureForError ());
2786 if ((m.ModFlags & Modifiers.STATIC) != 0 || m is Enum || m is Delegate)
2789 if (m is Constructor) {
2790 Report.Error (710, m.Location, "`{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2794 Method method = m as Method;
2795 if (method != null && method.Parameters.HasExtensionMethodType) {
2796 Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ());
2800 Report.Error (708, m.Location, "`{0}': cannot declare instance members in a static class", m.GetSignatureForError ());
2803 base.DefineContainerMembers (list);
2806 public override TypeBuilder DefineType ()
2808 if ((ModFlags & Modifiers.ABSTRACT) == Modifiers.ABSTRACT && (ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) != 0) {
2809 Report.Error (418, Location, "`{0}': an abstract class cannot be sealed or static", GetSignatureForError ());
2813 if ((ModFlags & (Modifiers.SEALED | Modifiers.STATIC)) == (Modifiers.SEALED | Modifiers.STATIC)) {
2814 Report.Error (441, Location, "`{0}': a class cannot be both static and sealed", GetSignatureForError ());
2818 return base.DefineType ();
2821 protected override bool DoDefineMembers ()
2823 if (InstanceConstructors == null && !IsStatic)
2824 DefineDefaultConstructor (false);
2826 return base.DoDefineMembers ();
2829 public override void Emit ()
2834 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
2835 TypeBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
2839 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2841 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2843 if (base_class == null) {
2844 if (RootContext.StdLib)
2845 base_class = TypeManager.system_object_expr;
2846 else if (Name != "System.Object")
2847 base_class = TypeManager.system_object_expr;
2849 if (Kind == Kind.Class && base_class is TypeParameterExpr){
2851 689, base_class.Location,
2852 "Cannot derive from `{0}' because it is a type parameter",
2853 base_class.GetSignatureForError ());
2857 if (IsGeneric && TypeManager.IsAttributeType (base_class.Type)) {
2858 Report.Error (698, base_class.Location,
2859 "A generic type cannot derive from `{0}' because it is an attribute class",
2860 base_class.GetSignatureForError ());
2863 if (base_class.IsSealed){
2864 Report.SymbolRelatedToPreviousError (base_class.Type);
2865 if (base_class.Type.IsAbstract) {
2866 Report.Error (709, Location, "`{0}': Cannot derive from static class `{1}'",
2867 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2869 Report.Error (509, Location, "`{0}': cannot derive from sealed class `{1}'",
2870 GetSignatureForError (), TypeManager.CSharpName (base_class.Type));
2875 if (!base_class.CanInheritFrom ()){
2876 Report.Error (644, Location, "`{0}' cannot derive from special class `{1}'",
2877 GetSignatureForError (), base_class.GetSignatureForError ());
2881 if (!base_class.AsAccessible (this)) {
2882 Report.SymbolRelatedToPreviousError (base_class.Type);
2883 Report.Error (60, Location, "Inconsistent accessibility: base class `{0}' is less accessible than class `{1}'",
2884 TypeManager.CSharpName (base_class.Type), GetSignatureForError ());
2888 if (PartialContainer.IsStaticClass) {
2889 if (base_class.Type != TypeManager.object_type) {
2890 Report.Error (713, Location, "Static class `{0}' cannot derive from type `{1}'. Static classes must derive from object",
2891 GetSignatureForError (), base_class.GetSignatureForError ());
2895 if (ifaces != null) {
2896 foreach (TypeExpr t in ifaces)
2897 Report.SymbolRelatedToPreviousError (t.Type);
2898 Report.Error (714, Location, "Static class `{0}' cannot implement interfaces", GetSignatureForError ());
2905 /// Search for at least one defined condition in ConditionalAttribute of attribute class
2906 /// Valid only for attribute classes.
2907 public bool IsExcluded ()
2909 if ((caching_flags & Flags.Excluded_Undetected) == 0)
2910 return (caching_flags & Flags.Excluded) != 0;
2912 caching_flags &= ~Flags.Excluded_Undetected;
2914 if (OptAttributes == null)
2917 if (TypeManager.conditional_attribute_type == null)
2920 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
2925 foreach (Attribute a in attrs) {
2926 string condition = a.GetConditionalAttributeValue ();
2927 if (Location.CompilationUnit.IsConditionalDefined (condition))
2931 caching_flags |= Flags.Excluded;
2937 return (ModFlags & Modifiers.STATIC) != 0;
2942 // FIXME: How do we deal with the user specifying a different
2945 protected override TypeAttributes TypeAttr {
2947 TypeAttributes ta = base.TypeAttr | TypeAttributes.AutoLayout | TypeAttributes.Class;
2949 ta |= StaticClassAttribute;
2955 public sealed class Struct : ClassOrStruct {
2957 // Modifiers allowed in a struct declaration
2959 const int AllowedModifiers =
2962 Modifiers.PROTECTED |
2963 Modifiers.INTERNAL |
2967 public Struct (NamespaceEntry ns, DeclSpace parent, MemberName name,
2968 int mod, Attributes attrs)
2969 : base (ns, parent, name, attrs, Kind.Struct)
2973 if (parent.Parent == null)
2974 accmods = Modifiers.INTERNAL;
2976 accmods = Modifiers.PRIVATE;
2978 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, Location);
2980 this.ModFlags |= Modifiers.SEALED;
2983 public override AttributeTargets AttributeTargets {
2985 return AttributeTargets.Struct;
2989 const TypeAttributes DefaultTypeAttributes =
2990 TypeAttributes.SequentialLayout |
2991 TypeAttributes.Sealed |
2992 TypeAttributes.BeforeFieldInit;
2995 protected override TypeExpr[] ResolveBaseTypes (out TypeExpr base_class)
2997 TypeExpr[] ifaces = base.ResolveBaseTypes (out base_class);
2999 // If we are compiling our runtime,
3000 // and we are defining ValueType, then our
3001 // base is `System.Object'.
3003 if (base_class == null) {
3004 if (!RootContext.StdLib && Name == "System.ValueType")
3005 base_class = TypeManager.system_object_expr;
3007 base_class = TypeManager.system_valuetype_expr;
3014 // FIXME: Allow the user to specify a different set of attributes
3015 // in some cases (Sealed for example is mandatory for a class,
3016 // but what SequentialLayout can be changed
3018 protected override TypeAttributes TypeAttr {
3020 return base.TypeAttr | DefaultTypeAttributes;
3024 public override void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
3026 if ((field.ModFlags & Modifiers.STATIC) == 0) {
3027 Report.Error (573, field.Location, "`{0}': Structs cannot have instance field initializers",
3028 field.GetSignatureForError ());
3031 base.RegisterFieldForInitialization (field, expression);
3039 public sealed class Interface : TypeContainer, IMemberContainer {
3042 /// Modifiers allowed in a class declaration
3044 public const int AllowedModifiers =
3047 Modifiers.PROTECTED |
3048 Modifiers.INTERNAL |
3052 public Interface (NamespaceEntry ns, DeclSpace parent, MemberName name, int mod,
3054 : base (ns, parent, name, attrs, Kind.Interface)
3058 if (parent.Parent == null)
3059 accmods = Modifiers.INTERNAL;
3061 accmods = Modifiers.PRIVATE;
3063 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, name.Location);
3066 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3068 if (a.Type == TypeManager.comimport_attr_type && TypeManager.guid_attr_type != null &&
3069 !attributes.Contains (TypeManager.guid_attr_type)) {
3070 a.Error_MissingGuidAttribute ();
3073 base.ApplyAttributeBuilder (a, cb);
3077 public override AttributeTargets AttributeTargets {
3079 return AttributeTargets.Interface;
3083 const TypeAttributes DefaultTypeAttributes =
3084 TypeAttributes.AutoLayout |
3085 TypeAttributes.Abstract |
3086 TypeAttributes.Interface;
3088 protected override TypeAttributes TypeAttr {
3090 return base.TypeAttr | DefaultTypeAttributes;
3094 protected override bool VerifyClsCompliance ()
3096 if (!base.VerifyClsCompliance ())
3099 if (ifaces != null) {
3100 foreach (Type t in ifaces) {
3101 if (AttributeTester.IsClsCompliant (t))
3104 Report.SymbolRelatedToPreviousError (t);
3105 Report.Warning (3027, 1, Location, "`{0}' is not CLS-compliant because base interface `{1}' is not CLS-compliant",
3106 GetSignatureForError (), TypeManager.CSharpName (t));
3114 // It is used as a base class for all property based members
3115 // This includes properties, indexers, and events
3116 public abstract class PropertyBasedMember : InterfaceMemberBase
3118 public PropertyBasedMember (DeclSpace parent, GenericMethod generic,
3119 FullNamedExpression type, int mod, int allowed_mod,
3120 MemberName name, Attributes attrs)
3121 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3125 protected override bool VerifyClsCompliance ()
3127 if (!base.VerifyClsCompliance ())
3130 if (!AttributeTester.IsClsCompliant (MemberType)) {
3131 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
3132 GetSignatureForError ());
3140 public abstract class MethodCore : InterfaceMemberBase
3142 public readonly Parameters Parameters;
3143 protected ToplevelBlock block;
3145 public MethodCore (DeclSpace parent, GenericMethod generic,
3146 FullNamedExpression type, int mod, int allowed_mod,
3147 MemberName name, Attributes attrs, Parameters parameters)
3148 : base (parent, generic, type, mod, allowed_mod, name, attrs)
3150 Parameters = parameters;
3154 // Returns the System.Type array for the parameters of this method
3156 public Type [] ParameterTypes {
3158 return Parameters.Types;
3162 public Parameters ParameterInfo
3169 public ToplevelBlock Block {
3179 protected override bool CheckBase ()
3181 // Check whether arguments were correct.
3182 if (!DefineParameters (Parameters))
3185 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
3186 if (!Parent.MemberCache.CheckExistingMembersOverloads (this,
3187 MemberName.IsGeneric ? MemberName.Basename : MemberName.MethodName, Parameters))
3190 // TODO: Find a better way how to check reserved accessors collision
3191 Method m = this as Method;
3193 if (!m.CheckForDuplications ())
3198 if (!base.CheckBase ())
3205 // Returns a string that represents the signature for this
3206 // member which should be used in XML documentation.
3208 public override string GetDocCommentName (DeclSpace ds)
3210 return DocUtil.GetMethodDocCommentName (this, Parameters, ds);
3214 // Raised (and passed an XmlElement that contains the comment)
3215 // when GenerateDocComment is writing documentation expectedly.
3217 // FIXME: with a few effort, it could be done with XmlReader,
3218 // that means removal of DOM use.
3220 internal override void OnGenerateDocComment (XmlElement el)
3222 DocUtil.OnMethodGenerateDocComment (this, el);
3226 // Represents header string for documentation comment.
3228 public override string DocCommentHeader
3230 get { return "M:"; }
3233 public override bool EnableOverloadChecks (MemberCore overload)
3235 if (overload is MethodCore || overload is AbstractPropertyEventMethod) {
3236 caching_flags |= Flags.MethodOverloadsExist;
3243 protected override bool VerifyClsCompliance ()
3245 if (!base.VerifyClsCompliance ())
3248 if (Parameters.HasArglist) {
3249 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
3252 if (!AttributeTester.IsClsCompliant (MemberType)) {
3253 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
3254 GetSignatureForError ());
3257 Parameters.VerifyClsCompliance ();
3263 public abstract class InterfaceMemberBase : MemberBase {
3265 // Whether this is an interface member.
3267 public bool IsInterface;
3270 // If true, this is an explicit interface implementation
3272 public bool IsExplicitImpl;
3274 protected bool is_external_implementation;
3277 // The interface type we are explicitly implementing
3279 public Type InterfaceType;
3282 // The method we're overriding if this is an override method.
3284 protected MethodInfo base_method;
3286 readonly int explicit_mod_flags;
3287 public MethodAttributes flags;
3289 public InterfaceMemberBase (DeclSpace parent, GenericMethod generic,
3290 FullNamedExpression type, int mod, int allowed_mod,
3291 MemberName name, Attributes attrs)
3292 : base (parent, generic, type, mod, allowed_mod, Modifiers.PRIVATE,
3295 IsInterface = parent.PartialContainer.Kind == Kind.Interface;
3296 IsExplicitImpl = (MemberName.Left != null);
3297 explicit_mod_flags = mod;
3300 protected override bool CheckBase ()
3302 if (!base.CheckBase ())
3308 // Is null for System.Object while compiling corlib and base interfaces
3309 if (Parent.PartialContainer.BaseCache == null) {
3310 if ((ModFlags & Modifiers.NEW) != 0) {
3311 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3316 Type base_ret_type = null;
3317 base_method = FindOutBaseMethod (ref base_ret_type);
3319 // method is override
3320 if (base_method != null) {
3321 if (!CheckMethodAgainstBase (base_ret_type))
3324 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3325 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (base_method);
3327 if (OptAttributes == null || TypeManager.obsolete_attribute_type == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3328 Report.SymbolRelatedToPreviousError (base_method);
3329 Report.Warning (672, 1, Location, "Member `{0}' overrides obsolete member `{1}'. Add the Obsolete attribute to `{0}'",
3330 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3333 if (OptAttributes != null && TypeManager.obsolete_attribute_type != null && OptAttributes.Contains (TypeManager.obsolete_attribute_type)) {
3334 Report.SymbolRelatedToPreviousError (base_method);
3335 Report.Warning (809, 1, Location, "Obsolete member `{0}' overrides non-obsolete member `{1}'",
3336 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3343 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, !((this is Event) || (this is Property)));
3344 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3345 if (conflict_symbol != null) {
3346 Report.SymbolRelatedToPreviousError (conflict_symbol);
3348 Report.Error (72, Location, "`{0}': cannot override because `{1}' is not an event", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3349 else if (this is PropertyBase)
3350 Report.Error (544, Location, "`{0}': cannot override because `{1}' is not a property", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3352 Report.Error (505, Location, "`{0}': cannot override because `{1}' is not a method", GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3354 Report.Error (115, Location, "`{0}' is marked as an override but no suitable {1} found to override",
3355 GetSignatureForError (), SimpleName.GetMemberType (this));
3360 if (conflict_symbol == null) {
3361 if ((ModFlags & Modifiers.NEW) != 0) {
3362 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
3367 if ((ModFlags & Modifiers.NEW) == 0) {
3368 if (this is Method && conflict_symbol is MethodBase)
3371 Report.SymbolRelatedToPreviousError (conflict_symbol);
3372 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3373 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
3380 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3381 // that have been defined.
3383 // `name' is the user visible name for reporting errors (this is used to
3384 // provide the right name regarding method names and properties)
3386 bool CheckMethodAgainstBase (Type base_method_type)
3390 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3391 if (!(base_method.IsAbstract || base_method.IsVirtual)){
3392 Report.Error (506, Location,
3393 "`{0}': cannot override inherited member `{1}' because it is not marked virtual, abstract or override",
3394 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3398 // Now we check that the overriden method is not final
3400 if (base_method.IsFinal) {
3401 Report.SymbolRelatedToPreviousError (base_method);
3402 Report.Error (239, Location, "`{0}': cannot override inherited member `{1}' because it is sealed",
3403 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3407 // Check that the permissions are not being changed
3409 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3410 MethodAttributes base_classp = base_method.Attributes & MethodAttributes.MemberAccessMask;
3412 if (!CheckAccessModifiers (thisp, base_classp, base_method)) {
3413 Error_CannotChangeAccessModifiers (Location, base_method, base_classp, null);
3417 if (!TypeManager.IsEqual (MemberType, TypeManager.TypeToCoreType (base_method_type))) {
3418 Report.SymbolRelatedToPreviousError (base_method);
3419 if (this is PropertyBasedMember) {
3420 Report.Error (1715, Location, "`{0}': type must be `{1}' to match overridden member `{2}'",
3421 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3424 Report.Error (508, Location, "`{0}': return type must be `{1}' to match overridden member `{2}'",
3425 GetSignatureForError (), TypeManager.CSharpName (base_method_type), TypeManager.CSharpSignature (base_method));
3431 if ((ModFlags & Modifiers.NEW) == 0) {
3432 if ((ModFlags & Modifiers.OVERRIDE) == 0 && Name != "Finalize") {
3433 ModFlags |= Modifiers.NEW;
3434 Report.SymbolRelatedToPreviousError (base_method);
3435 if (!IsInterface && (base_method.IsVirtual || base_method.IsAbstract)) {
3436 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",
3437 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3439 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
3440 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3444 if (base_method.IsAbstract && !IsInterface) {
3445 Report.SymbolRelatedToPreviousError (base_method);
3446 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
3447 GetSignatureForError (), TypeManager.CSharpSignature (base_method));
3455 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes base_classp, MethodInfo base_method)
3457 if ((base_classp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3459 // when overriding protected internal, the method can be declared
3460 // protected internal only within the same assembly or assembly
3461 // which has InternalsVisibleTo
3463 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3464 return TypeManager.IsThisOrFriendAssembly (base_method.DeclaringType.Assembly);
3465 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3467 // if it's not "protected internal", it must be "protected"
3471 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3473 // protected within the same assembly - an error
3476 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3477 (base_classp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3479 // protected ok, but other attributes differ - report an error
3485 return (thisp == base_classp);
3489 protected bool DefineParameters (Parameters parameters)
3491 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
3493 if (!parameters.Resolve (rc))
3497 for (int i = 0; i < parameters.Count; ++i) {
3498 Parameter p = parameters [i];
3499 if (p.CheckAccessibility (this))
3502 Type t = parameters.Types [i];
3503 Report.SymbolRelatedToPreviousError (t);
3504 if (this is Indexer)
3505 Report.Error (55, Location,
3506 "Inconsistent accessibility: parameter type `" +
3507 TypeManager.CSharpName (t) + "' is less " +
3508 "accessible than indexer `" + GetSignatureForError () + "'");
3509 else if (this is Operator)
3510 Report.Error (57, Location,
3511 "Inconsistent accessibility: parameter type `" +
3512 TypeManager.CSharpName (t) + "' is less " +
3513 "accessible than operator `" + GetSignatureForError () + "'");
3515 Report.Error (51, Location,
3516 "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'",
3517 TypeManager.CSharpName (t), GetSignatureForError ());
3523 protected override bool DoDefine()
3525 if (!base.DoDefine ())
3528 if (IsExplicitImpl) {
3529 TypeExpr texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3533 InterfaceType = texpr.Type;
3535 if (!InterfaceType.IsInterface) {
3536 Report.Error (538, Location, "`{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3540 if (!Parent.PartialContainer.VerifyImplements (this))
3547 protected bool DoDefineBase ()
3550 throw new InternalErrorException ();
3553 ModFlags = Modifiers.PUBLIC |
3554 Modifiers.ABSTRACT |
3555 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
3557 flags = MethodAttributes.Public |
3558 MethodAttributes.Abstract |
3559 MethodAttributes.HideBySig |
3560 MethodAttributes.NewSlot |
3561 MethodAttributes.Virtual;
3563 if (!Parent.PartialContainer.MethodModifiersValid (this))
3566 flags = Modifiers.MethodAttr (ModFlags);
3569 if (IsExplicitImpl) {
3570 TypeExpr iface_texpr = MemberName.Left.GetTypeExpression ().ResolveAsTypeTerminal (this, false);
3571 if (iface_texpr == null)
3574 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3575 Report.Error (754, Location, "A partial method `{0}' cannot explicitly implement an interface",
3576 GetSignatureForError ());
3580 InterfaceType = iface_texpr.Type;
3582 if (!InterfaceType.IsInterface) {
3583 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
3587 if (!Parent.PartialContainer.VerifyImplements (this))
3590 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
3596 public override void Emit()
3598 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
3599 // We are more strict than Microsoft and report CS0626 as error
3600 if ((ModFlags & Modifiers.EXTERN) != 0 && !is_external_implementation) {
3601 Report.Error (626, Location,
3602 "`{0}' is marked as an external but has no DllImport attribute. Consider adding a DllImport attribute to specify the external implementation",
3603 GetSignatureForError ());
3609 protected void Error_CannotChangeAccessModifiers (Location loc, MemberInfo base_method, MethodAttributes ma, string suffix)
3611 Report.SymbolRelatedToPreviousError (base_method);
3612 string base_name = TypeManager.GetFullNameSignature (base_method);
3613 string this_name = GetSignatureForError ();
3614 if (suffix != null) {
3615 base_name += suffix;
3616 this_name += suffix;
3619 Report.Error (507, loc, "`{0}': cannot change access modifiers when overriding `{1}' inherited member `{2}'",
3620 this_name, Modifiers.GetDescription (ma), base_name);
3623 protected static string Error722 {
3625 return "`{0}': static types cannot be used as return types";
3630 /// Gets base method and its return type
3632 protected abstract MethodInfo FindOutBaseMethod (ref Type base_ret_type);
3635 // The "short" name of this property / indexer / event. This is the
3636 // name without the explicit interface.
3638 public string ShortName
3640 get { return MemberName.Name; }
3641 set { SetMemberName (new MemberName (MemberName.Left, value, Location)); }
3644 public string GetFullName (MemberName name)
3646 if (!IsExplicitImpl)
3649 return InterfaceType.FullName.Replace ('+', '.') + "." + name.Name;
3652 protected override bool VerifyClsCompliance ()
3654 if (!base.VerifyClsCompliance ()) {
3655 if (IsInterface && HasClsCompliantAttribute && Parent.IsClsComplianceRequired ()) {
3656 Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
3659 if ((ModFlags & Modifiers.ABSTRACT) != 0 && Parent.TypeBuilder.IsClass && IsExposedFromAssembly () && Parent.IsClsComplianceRequired ()) {
3660 Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3665 if (GenericMethod != null)
3666 GenericMethod.VerifyClsCompliance ();
3671 public override bool IsUsed
3673 get { return IsExplicitImpl || base.IsUsed; }
3678 public abstract class MethodOrOperator : MethodCore, IMethodData
3680 public MethodBuilder MethodBuilder;
3681 ReturnParameter return_attributes;
3682 ListDictionary declarative_security;
3683 protected MethodData MethodData;
3685 static string[] attribute_targets = new string [] { "method", "return" };
3687 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, int mod,
3688 int allowed_mod, MemberName name,
3689 Attributes attrs, Parameters parameters)
3690 : base (parent, generic, type, mod, allowed_mod, name,
3695 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3697 if (a.Target == AttributeTargets.ReturnValue) {
3698 if (return_attributes == null)
3699 return_attributes = new ReturnParameter (MethodBuilder, Location);
3701 return_attributes.ApplyAttributeBuilder (a, cb);
3705 if (a.IsInternalMethodImplAttribute) {
3706 is_external_implementation = true;
3709 if (a.Type == TypeManager.dllimport_type) {
3710 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3711 if ((ModFlags & extern_static) != extern_static) {
3712 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3714 is_external_implementation = true;
3717 if (a.IsValidSecurityAttribute ()) {
3718 if (declarative_security == null)
3719 declarative_security = new ListDictionary ();
3720 a.ExtractSecurityPermissionSet (declarative_security);
3724 if (MethodBuilder != null)
3725 MethodBuilder.SetCustomAttribute (cb);
3728 public override AttributeTargets AttributeTargets {
3730 return AttributeTargets.Method;
3734 public virtual EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
3736 return new EmitContext (
3737 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
3740 protected bool DefineGenericMethod ()
3742 if (!DoDefineBase ())
3746 if (GenericMethod != null) {
3747 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
3748 if (!GenericMethod.Define (MethodBuilder))
3756 public bool ResolveMembers ()
3758 if (!DefineGenericMethod ())
3764 public override bool Define ()
3769 if (!CheckAbstractAndExtern (block != null))
3772 if ((ModFlags & Modifiers.PARTIAL) != 0) {
3773 for (int i = 0; i < Parameters.Count; ++i ) {
3774 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
3775 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
3776 GetSignatureForError ());
3785 if (block != null && block.IsIterator && !(Parent is IteratorStorey))
3786 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
3788 if (IsPartialDefinition) {
3789 caching_flags &= ~Flags.Excluded_Undetected;
3790 caching_flags |= Flags.Excluded;
3791 // Add to member cache only when a partial method implementation is not there
3792 if ((caching_flags & Flags.MethodOverloadsExist) == 0) {
3793 MethodBase mb = new PartialMethodDefinitionInfo (this);
3794 Parent.MemberCache.AddMember (mb, this);
3795 TypeManager.AddMethod (mb, this);
3801 MethodData = new MethodData (
3802 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
3804 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
3807 MethodBuilder = MethodData.MethodBuilder;
3810 if (MethodBuilder.IsGenericMethod)
3811 Parent.MemberCache.AddGenericMember (MethodBuilder, this);
3814 Parent.MemberCache.AddMember (MethodBuilder, this);
3816 if (!TypeManager.IsGenericParameter (MemberType)) {
3817 if (MemberType.IsAbstract && MemberType.IsSealed) {
3818 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3826 public override void Emit ()
3829 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
3830 MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
3831 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
3832 MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
3834 if (OptAttributes != null)
3835 OptAttributes.Emit ();
3837 if (declarative_security != null) {
3838 foreach (DictionaryEntry de in declarative_security) {
3839 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
3846 protected void Error_ConditionalAttributeIsNotValid ()
3848 Report.Error (577, Location,
3849 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
3850 GetSignatureForError ());
3853 public bool IsPartialDefinition {
3855 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
3859 public bool IsPartialImplementation {
3861 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
3865 public override string[] ValidAttributeTargets {
3867 return attribute_targets;
3871 #region IMethodData Members
3873 public CallingConventions CallingConventions {
3875 CallingConventions cc = Parameters.CallingConvention;
3877 if ((ModFlags & Modifiers.STATIC) == 0)
3878 cc |= CallingConventions.HasThis;
3880 // FIXME: How is `ExplicitThis' used in C#?
3886 public Type ReturnType {
3892 public MemberName MethodName {
3898 protected override bool CheckBase ()
3900 if (!base.CheckBase ())
3903 // TODO: Destructor should derive from MethodCore
3904 if (base_method != null && (ModFlags & Modifiers.OVERRIDE) != 0 && Name == "Finalize" &&
3905 base_method.DeclaringType == TypeManager.object_type && !(this is Destructor)) {
3906 Report.Error (249, Location, "Do not override object.Finalize. Instead, provide a destructor");
3914 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
3916 public bool IsExcluded () {
3917 if ((caching_flags & Flags.Excluded_Undetected) == 0)
3918 return (caching_flags & Flags.Excluded) != 0;
3920 caching_flags &= ~Flags.Excluded_Undetected;
3922 if (base_method == null) {
3923 if (OptAttributes == null)
3926 if (TypeManager.conditional_attribute_type == null)
3929 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type);
3934 foreach (Attribute a in attrs) {
3935 string condition = a.GetConditionalAttributeValue ();
3936 if (condition == null)
3939 if (Location.CompilationUnit.IsConditionalDefined (condition))
3943 caching_flags |= Flags.Excluded;
3947 IMethodData md = TypeManager.GetMethod (TypeManager.DropGenericMethodArguments (base_method));
3949 if (AttributeTester.IsConditionalMethodExcluded (base_method, Location)) {
3950 caching_flags |= Flags.Excluded;
3956 if (md.IsExcluded ()) {
3957 caching_flags |= Flags.Excluded;
3963 GenericMethod IMethodData.GenericMethod {
3965 return GenericMethod;
3969 public virtual void EmitExtraSymbolInfo (SourceMethod source)
3976 public class SourceMethod : IMethodDef
3979 SourceMethodBuilder builder;
3981 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
3983 this.method = method;
3985 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
3988 public string Name {
3989 get { return method.Name; }
3994 if (method is MethodBuilder)
3995 return ((MethodBuilder) method).GetToken ().Token;
3996 else if (method is ConstructorBuilder)
3997 return ((ConstructorBuilder) method).GetToken ().Token;
3999 throw new NotSupportedException ();
4003 public void CloseMethod ()
4005 SymbolWriter.CloseMethod ();
4008 public void SetRealMethodName (string name)
4010 if (builder != null)
4011 builder.SetRealMethodName (name);
4014 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
4016 if (!SymbolWriter.HasSymbolWriter)
4021 Location start_loc = block.StartLocation;
4022 if (start_loc.IsNull)
4025 ICompileUnit compile_unit = start_loc.CompilationUnit;
4026 if (compile_unit == null)
4029 return new SourceMethod (parent, method, compile_unit);
4033 public class Method : MethodOrOperator {
4036 /// Modifiers allowed in a class declaration
4038 const int AllowedModifiers =
4041 Modifiers.PROTECTED |
4042 Modifiers.INTERNAL |
4047 Modifiers.OVERRIDE |
4048 Modifiers.ABSTRACT |
4052 const int AllowedInterfaceModifiers =
4053 Modifiers.NEW | Modifiers.UNSAFE;
4055 public Method (DeclSpace parent, GenericMethod generic,
4056 FullNamedExpression return_type, int mod,
4057 MemberName name, Parameters parameters, Attributes attrs)
4058 : base (parent, generic, return_type, mod,
4059 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
4060 name, attrs, parameters)
4064 protected Method (DeclSpace parent, FullNamedExpression return_type, int mod, int amod,
4065 MemberName name, Parameters parameters, Attributes attrs)
4066 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
4070 public override string GetSignatureForError()
4072 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4075 void Error_DuplicateEntryPoint (MethodInfo b, Location location)
4077 Report.Error (17, location,
4078 "Program `{0}' has more than one entry point defined: `{1}'",
4079 CodeGen.FileName, TypeManager.CSharpSignature(b));
4082 bool IsEntryPoint ()
4084 if (ReturnType != TypeManager.void_type &&
4085 ReturnType != TypeManager.int32_type)
4088 if (Parameters.Count == 0)
4091 if (Parameters.Count > 1)
4094 Type t = Parameters.Types [0];
4095 return t.IsArray && t.GetArrayRank () == 1 &&
4096 TypeManager.GetElementType (t) == TypeManager.string_type &&
4097 (Parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
4100 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4102 if (a.Type == TypeManager.conditional_attribute_type) {
4103 if (IsExplicitImpl) {
4104 Error_ConditionalAttributeIsNotValid ();
4108 if (ReturnType != TypeManager.void_type) {
4109 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
4113 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
4114 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
4119 Report.Error (582, Location, "Conditional not valid on interface members");
4123 if (MethodData.implementing != null) {
4124 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
4125 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
4126 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
4130 for (int i = 0; i < Parameters.Count; ++i) {
4131 if (Parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
4132 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
4138 if (a.Type == TypeManager.extension_attribute_type) {
4139 a.Error_MisusedExtensionAttribute ();
4143 base.ApplyAttributeBuilder (a, cb);
4146 public bool CheckForDuplications ()
4148 ArrayList ar = Parent.PartialContainer.Properties;
4150 for (int i = 0; i < ar.Count; ++i) {
4151 PropertyBase pb = (PropertyBase) ar [i];
4152 if (pb.AreAccessorsDuplicateImplementation (this))
4157 ar = Parent.PartialContainer.Indexers;
4159 for (int i = 0; i < ar.Count; ++i) {
4160 PropertyBase pb = (PropertyBase) ar [i];
4161 if (pb.AreAccessorsDuplicateImplementation (this))
4172 public override bool Define ()
4174 if (!base.Define ())
4177 if (RootContext.StdLib && (ReturnType == TypeManager.arg_iterator_type || ReturnType == TypeManager.typed_reference_type)) {
4178 Error1599 (Location, ReturnType);
4182 if (ReturnType == TypeManager.void_type && Parameters.Count == 0 &&
4183 Name == "Finalize" && !(this is Destructor)) {
4184 Report.Warning (465, 1, Location, "Introducing a 'Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
4187 if (base_method != null && (ModFlags & Modifiers.NEW) == 0) {
4188 if (Parameters.Count == 1 && ParameterTypes [0] == TypeManager.object_type && Name == "Equals")
4189 Parent.PartialContainer.Mark_HasEquals ();
4190 else if (Parameters.IsEmpty && Name == "GetHashCode")
4191 Parent.PartialContainer.Mark_HasGetHashCode ();
4194 if ((ModFlags & Modifiers.STATIC) == 0)
4197 if (Parameters.HasExtensionMethodType) {
4198 if (Parent.IsStaticClass && !Parent.IsGeneric) {
4199 if (!Parent.IsTopLevel)
4200 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
4201 GetSignatureForError ());
4203 if (TypeManager.extension_attribute_type == null) {
4204 Report.Error (1110, Location,
4205 "`{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",
4206 GetSignatureForError ());
4207 } else if (TypeManager.extension_attribute_attr == null) {
4208 ConstructorInfo ci = TypeManager.GetPredefinedConstructor (
4209 TypeManager.extension_attribute_type, Location, System.Type.EmptyTypes);
4212 TypeManager.extension_attribute_attr = new CustomAttributeBuilder (ci, new object [0]);
4215 ModFlags |= Modifiers.METHOD_EXTENSION;
4216 Parent.ModFlags |= Modifiers.METHOD_EXTENSION;
4217 CodeGen.Assembly.HasExtensionMethods = true;
4219 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
4220 GetSignatureForError ());
4225 // This is used to track the Entry Point,
4227 if (RootContext.NeedsEntryPoint &&
4229 (RootContext.MainClass == null ||
4230 RootContext.MainClass == Parent.TypeBuilder.FullName)){
4231 if (IsEntryPoint ()) {
4233 if (RootContext.EntryPoint == null) {
4234 if (Parent.IsGeneric || MemberName.IsGeneric) {
4235 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
4236 GetSignatureForError ());
4238 IMethodData md = TypeManager.GetMethod (MethodBuilder);
4239 md.SetMemberIsUsed ();
4241 RootContext.EntryPoint = MethodBuilder;
4242 RootContext.EntryPointLocation = Location;
4245 Error_DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
4246 Error_DuplicateEntryPoint (MethodBuilder, Location);
4249 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
4250 GetSignatureForError ());
4260 public override void Emit ()
4263 Report.Debug (64, "METHOD EMIT", this, MethodBuilder, Location, Block, MethodData);
4264 if (IsPartialDefinition) {
4266 // Do attribute checks only when partial implementation does not exist
4268 if (MethodBuilder == null)
4274 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0)
4275 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
4276 GetSignatureForError ());
4278 MethodData.Emit (Parent);
4282 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
4283 MethodBuilder.SetCustomAttribute (TypeManager.extension_attribute_attr);
4289 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
4290 Location, MethodBuilder);
4295 public override bool EnableOverloadChecks (MemberCore overload)
4297 // TODO: It can be deleted when members will be defined in correct order
4298 if (overload is Operator)
4299 return overload.EnableOverloadChecks (this);
4301 if (overload is Indexer)
4304 return base.EnableOverloadChecks (overload);
4307 public static void Error1599 (Location loc, Type t)
4309 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
4312 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4314 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindMemberToOverride (
4315 Parent.TypeBuilder, Name, Parameters, GenericMethod, false);
4320 if (mi.IsSpecialName)
4323 base_ret_type = TypeManager.TypeToCoreType (mi.ReturnType);
4327 public void SetPartialDefinition (Method methodDefinition)
4329 caching_flags |= Flags.PartialDefinitionExists;
4330 methodDefinition.MethodBuilder = MethodBuilder;
4331 if (methodDefinition.attributes == null)
4334 if (attributes == null) {
4335 attributes = methodDefinition.attributes;
4337 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
4341 protected override bool VerifyClsCompliance ()
4343 if (!base.VerifyClsCompliance ())
4346 if (ParameterInfo.Count > 0) {
4347 ArrayList al = (ArrayList)Parent.PartialContainer.MemberCache.Members [Name];
4349 MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4356 public abstract class ConstructorInitializer : ExpressionStatement
4358 ArrayList argument_list;
4359 MethodGroupExpr base_constructor_group;
4361 public ConstructorInitializer (ArrayList argument_list, Location loc)
4363 this.argument_list = argument_list;
4367 public ArrayList Arguments {
4369 return argument_list;
4373 public override Expression CreateExpressionTree (EmitContext ec)
4375 throw new NotSupportedException ("ET");
4378 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4380 if (argument_list != null){
4381 foreach (Argument a in argument_list){
4382 if (!a.Resolve (ec, loc))
4387 if (this is ConstructorBaseInitializer) {
4388 if (ec.ContainerType.BaseType == null)
4391 type = ec.ContainerType.BaseType;
4392 if (ec.ContainerType.IsValueType) {
4393 Report.Error (522, loc,
4394 "`{0}': Struct constructors cannot call base constructors", TypeManager.CSharpSignature (caller_builder));
4399 // It is legal to have "this" initializers that take no arguments
4400 // in structs, they are just no-ops.
4402 // struct D { public D (int a) : this () {}
4404 if (ec.ContainerType.IsValueType && argument_list == null)
4407 type = ec.ContainerType;
4410 base_constructor_group = MemberLookupFinal (
4411 ec, null, type, ConstructorBuilder.ConstructorName, MemberTypes.Constructor,
4412 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4413 loc) as MethodGroupExpr;
4415 if (base_constructor_group == null)
4418 base_constructor_group = base_constructor_group.OverloadResolve (
4419 ec, ref argument_list, false, loc);
4421 if (base_constructor_group == null)
4424 ConstructorInfo base_ctor = (ConstructorInfo)base_constructor_group;
4426 if (base_ctor == caller_builder){
4427 Report.Error (516, loc, "Constructor `{0}' cannot call itself", TypeManager.CSharpSignature (caller_builder));
4433 public override Expression DoResolve (EmitContext ec)
4435 throw new NotSupportedException ();
4438 public override void Emit (EmitContext ec)
4440 // It can be null for static initializers
4441 if (base_constructor_group == null)
4446 base_constructor_group.InstanceExpression = ec.GetThis (loc);
4448 base_constructor_group.EmitCall (ec, argument_list);
4451 public override void EmitStatement (EmitContext ec)
4457 public class ConstructorBaseInitializer : ConstructorInitializer {
4458 public ConstructorBaseInitializer (ArrayList argument_list, Location l) :
4459 base (argument_list, l)
4464 class GeneratedBaseInitializer: ConstructorBaseInitializer {
4465 public GeneratedBaseInitializer (Location loc):
4471 public class ConstructorThisInitializer : ConstructorInitializer {
4472 public ConstructorThisInitializer (ArrayList argument_list, Location l) :
4473 base (argument_list, l)
4478 public class Constructor : MethodCore, IMethodData {
4479 public ConstructorBuilder ConstructorBuilder;
4480 public ConstructorInitializer Initializer;
4481 ListDictionary declarative_security;
4482 bool has_compliant_args;
4485 // Modifiers allowed for a constructor.
4487 public const int AllowedModifiers =
4489 Modifiers.PROTECTED |
4490 Modifiers.INTERNAL |
4496 static readonly string[] attribute_targets = new string [] { "method" };
4499 // The spec claims that static is not permitted, but
4500 // my very own code has static constructors.
4502 public Constructor (DeclSpace parent, string name, int mod, Parameters args,
4503 ConstructorInitializer init, Location loc)
4504 : base (parent, null, null, mod, AllowedModifiers,
4505 new MemberName (name, loc), null, args)
4510 public bool HasCompliantArgs {
4511 get { return has_compliant_args; }
4514 public override AttributeTargets AttributeTargets {
4515 get { return AttributeTargets.Constructor; }
4519 // Returns true if this is a default constructor
4521 public bool IsDefault ()
4523 if ((ModFlags & Modifiers.STATIC) != 0)
4524 return Parameters.IsEmpty;
4526 return Parameters.IsEmpty &&
4527 (Initializer is ConstructorBaseInitializer) &&
4528 (Initializer.Arguments == null);
4531 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4533 if (a.IsValidSecurityAttribute ()) {
4534 if (declarative_security == null) {
4535 declarative_security = new ListDictionary ();
4537 a.ExtractSecurityPermissionSet (declarative_security);
4541 if (a.IsInternalMethodImplAttribute) {
4542 is_external_implementation = true;
4545 ConstructorBuilder.SetCustomAttribute (cb);
4548 protected override bool CheckBase ()
4550 if ((ModFlags & Modifiers.STATIC) != 0) {
4551 if (!Parameters.IsEmpty) {
4552 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
4553 GetSignatureForError ());
4557 // the rest can be ignored
4561 // Check whether arguments were correct.
4562 if (!DefineParameters (Parameters))
4565 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
4566 Parent.MemberCache.CheckExistingMembersOverloads (this, ConstructorBuilder.ConstructorName,
4569 if (Parent.PartialContainer.Kind == Kind.Struct) {
4570 if (Parameters.Count == 0) {
4571 Report.Error (568, Location,
4572 "Structs cannot contain explicit parameterless constructors");
4577 CheckProtectedModifier ();
4583 // Creates the ConstructorBuilder
4585 public override bool Define ()
4587 if (ConstructorBuilder != null)
4590 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4591 MethodAttributes.SpecialName);
4593 if ((ModFlags & Modifiers.STATIC) != 0) {
4594 ca |= MethodAttributes.Static | MethodAttributes.Private;
4596 ca |= MethodAttributes.HideBySig;
4598 if ((ModFlags & Modifiers.PUBLIC) != 0)
4599 ca |= MethodAttributes.Public;
4600 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4601 if ((ModFlags & Modifiers.INTERNAL) != 0)
4602 ca |= MethodAttributes.FamORAssem;
4604 ca |= MethodAttributes.Family;
4605 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4606 ca |= MethodAttributes.Assembly;
4608 ca |= MethodAttributes.Private;
4611 if (!CheckAbstractAndExtern (block != null))
4614 // Check if arguments were correct.
4618 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4619 ca, CallingConventions,
4620 Parameters.GetEmitTypes ());
4622 if (Parent.PartialContainer.IsComImport) {
4623 if (!IsDefault ()) {
4624 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
4625 Parent.GetSignatureForError ());
4628 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
4631 Parent.MemberCache.AddMember (ConstructorBuilder, this);
4632 TypeManager.AddMethod (ConstructorBuilder, this);
4634 // It's here only to report an error
4635 if (block != null && block.IsIterator) {
4636 member_type = TypeManager.void_type;
4637 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
4646 public override void Emit ()
4648 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
4649 ConstructorBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
4651 if (OptAttributes != null)
4652 OptAttributes.Emit ();
4656 EmitContext ec = CreateEmitContext (null, null);
4659 // If we use a "this (...)" constructor initializer, then
4660 // do not emit field initializers, they are initialized in the other constructor
4662 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
4663 !(Initializer is ConstructorThisInitializer);
4665 if (emit_field_initializers)
4666 Parent.PartialContainer.ResolveFieldInitializers (ec);
4668 if (block != null) {
4669 // If this is a non-static `struct' constructor and doesn't have any
4670 // initializer, it must initialize all of the struct's fields.
4671 if ((Parent.PartialContainer.Kind == Kind.Struct) &&
4672 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4673 block.AddThisVariable (Parent, Location);
4675 if (!block.ResolveMeta (ec, ParameterInfo))
4678 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
4679 if (Parent.PartialContainer.Kind == Kind.Class && Initializer == null)
4680 Initializer = new GeneratedBaseInitializer (Location);
4683 // Spec mandates that Initializers will not have `this' access
4685 if (Initializer != null) {
4687 Initializer.Resolve (ConstructorBuilder, ec);
4688 ec.IsStatic = false;
4689 block.AddScopeStatement (new StatementExpression (Initializer));
4694 Parameters.ApplyAttributes (ConstructorBuilder);
4696 SourceMethod source = null;
4698 ec.OmitDebuggingInfo = true;
4700 source = SourceMethod.Create (Parent, ConstructorBuilder, block);
4702 bool unreachable = false;
4703 if (block != null) {
4704 if (!ec.ResolveTopBlock (null, block, ParameterInfo, this, out unreachable))
4707 ec.EmitMeta (block);
4709 if (Report.Errors > 0)
4712 if (emit_field_initializers)
4713 Parent.PartialContainer.EmitFieldInitializers (ec);
4715 ec.EmitResolvedTopBlock (block, unreachable);
4719 source.CloseMethod ();
4721 if (declarative_security != null) {
4722 foreach (DictionaryEntry de in declarative_security) {
4723 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4730 // Is never override
4731 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
4736 public override string GetSignatureForError()
4738 return base.GetSignatureForError () + Parameters.GetSignatureForError ();
4741 public override string[] ValidAttributeTargets {
4743 return attribute_targets;
4747 protected override bool VerifyClsCompliance ()
4749 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
4753 if (ParameterInfo.Count > 0) {
4754 ArrayList al = (ArrayList)Parent.MemberCache.Members [".ctor"];
4756 MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4758 if (TypeManager.IsSubclassOf (Parent.TypeBuilder, TypeManager.attribute_type)) {
4759 foreach (Type param in Parameters.Types) {
4760 if (param.IsArray) {
4766 has_compliant_args = true;
4770 #region IMethodData Members
4772 public System.Reflection.CallingConventions CallingConventions {
4774 CallingConventions cc = Parameters.CallingConvention;
4776 if (Parent.PartialContainer.Kind == Kind.Class)
4777 if ((ModFlags & Modifiers.STATIC) == 0)
4778 cc |= CallingConventions.HasThis;
4780 // FIXME: How is `ExplicitThis' used in C#?
4786 public MemberName MethodName {
4792 public Type ReturnType {
4798 public EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
4800 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4801 EmitContext ec = new EmitContext (this, Parent, Location, ig_, TypeManager.void_type, ModFlags, true);
4802 ec.CurrentBlock = block;
4806 public bool IsExcluded()
4811 GenericMethod IMethodData.GenericMethod {
4817 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
4824 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4826 public interface IMethodData
4828 CallingConventions CallingConventions { get; }
4829 Location Location { get; }
4830 MemberName MethodName { get; }
4831 Type ReturnType { get; }
4832 GenericMethod GenericMethod { get; }
4833 Parameters ParameterInfo { get; }
4835 Attributes OptAttributes { get; }
4836 ToplevelBlock Block { get; set; }
4838 EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
4839 ObsoleteAttribute GetObsoleteAttribute ();
4840 string GetSignatureForError ();
4842 bool IsClsComplianceRequired ();
4843 void SetMemberIsUsed ();
4844 void EmitExtraSymbolInfo (SourceMethod source);
4848 // Encapsulates most of the Method's state
4850 public class MethodData {
4852 static FieldInfo methodbuilder_attrs_field;
4854 public readonly IMethodData method;
4856 public readonly GenericMethod GenericMethod;
4859 // Are we implementing an interface ?
4861 public MethodInfo implementing;
4866 protected InterfaceMemberBase member;
4867 protected int modifiers;
4868 protected MethodAttributes flags;
4869 protected Type declaring_type;
4870 protected MethodInfo parent_method;
4872 MethodBuilder builder = null;
4873 public MethodBuilder MethodBuilder {
4879 public Type DeclaringType {
4881 return declaring_type;
4885 public MethodData (InterfaceMemberBase member,
4886 int modifiers, MethodAttributes flags, IMethodData method)
4888 this.member = member;
4889 this.modifiers = modifiers;
4892 this.method = method;
4895 public MethodData (InterfaceMemberBase member,
4896 int modifiers, MethodAttributes flags,
4897 IMethodData method, MethodBuilder builder,
4898 GenericMethod generic, MethodInfo parent_method)
4899 : this (member, modifiers, flags, method)
4901 this.builder = builder;
4902 this.GenericMethod = generic;
4903 this.parent_method = parent_method;
4906 public bool Define (DeclSpace parent, string method_full_name)
4908 string name = method.MethodName.Basename;
4910 TypeContainer container = parent.PartialContainer;
4912 PendingImplementation pending = container.PendingImplementations;
4913 if (pending != null){
4914 implementing = pending.IsInterfaceMethod (name, member.InterfaceType, this);
4916 if (member.InterfaceType != null){
4917 if (implementing == null){
4918 if (member is PropertyBase) {
4919 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
4920 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
4921 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
4924 Report.Error (539, method.Location,
4925 "`{0}.{1}' in explicit interface declaration is not a member of interface",
4926 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
4930 if (implementing.IsSpecialName && !(method is AbstractPropertyEventMethod)) {
4931 Report.SymbolRelatedToPreviousError (implementing);
4932 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
4933 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
4937 if (implementing != null) {
4938 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
4939 if (prop_method == null) {
4940 if (TypeManager.IsSpecialMethod (implementing)) {
4941 Report.SymbolRelatedToPreviousError (implementing);
4942 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}.{2}'",
4943 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing),
4944 implementing.Name.StartsWith ("get_") ? "get" : "set");
4946 } else if (implementing.DeclaringType.IsInterface) {
4947 if (!implementing.IsSpecialName) {
4948 Report.SymbolRelatedToPreviousError (implementing);
4949 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
4950 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
4953 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
4954 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
4955 Report.SymbolRelatedToPreviousError (implementing);
4956 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
4957 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing, true));
4966 // For implicit implementations, make sure we are public, for
4967 // explicit implementations, make sure we are private.
4969 if (implementing != null){
4971 // Setting null inside this block will trigger a more
4972 // verbose error reporting for missing interface implementations
4974 // The "candidate" function has been flagged already
4975 // but it wont get cleared
4977 if (member.IsExplicitImpl){
4978 if (method.ParameterInfo.HasParams && !TypeManager.GetParameterData (implementing).HasParams) {
4979 Report.SymbolRelatedToPreviousError (implementing);
4980 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
4981 method.GetSignatureForError ());
4985 if (implementing.DeclaringType.IsInterface) {
4987 // If this is an interface method implementation,
4988 // check for public accessibility
4990 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
4992 implementing = null;
4994 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4995 // We may never be private.
4996 implementing = null;
4998 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
5000 // We may be protected if we're overriding something.
5002 implementing = null;
5007 // Static is not allowed
5009 if ((modifiers & Modifiers.STATIC) != 0){
5010 implementing = null;
5015 // If implementing is still valid, set flags
5017 if (implementing != null){
5019 // When implementing interface methods, set NewSlot
5020 // unless, we are overwriting a method.
5022 if (implementing.DeclaringType.IsInterface){
5023 if ((modifiers & Modifiers.OVERRIDE) == 0)
5024 flags |= MethodAttributes.NewSlot;
5027 MethodAttributes.Virtual |
5028 MethodAttributes.HideBySig;
5030 // Set Final unless we're virtual, abstract or already overriding a method.
5031 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
5032 flags |= MethodAttributes.Final;
5035 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
5037 if (builder == null)
5040 if (container.CurrentType != null)
5041 declaring_type = container.CurrentType;
5043 declaring_type = container.TypeBuilder;
5045 if (implementing != null){
5047 // clear the pending implemntation flag
5049 pending.ImplementMethod (name, member.InterfaceType, this, member.IsExplicitImpl);
5051 if (member.IsExplicitImpl)
5052 container.TypeBuilder.DefineMethodOverride (
5053 builder, implementing);
5056 TypeManager.AddMethod (builder, method);
5058 if (GenericMethod != null) {
5059 bool is_override = member.IsExplicitImpl |
5060 ((modifiers & Modifiers.OVERRIDE) != 0);
5062 if (implementing != null)
5063 parent_method = implementing;
5065 EmitContext ec = method.CreateEmitContext (container, null);
5066 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
5075 /// Create the MethodBuilder for the method
5077 void DefineMethodBuilder (TypeContainer container, string method_name, Parameters param)
5079 if (builder == null) {
5080 builder = container.TypeBuilder.DefineMethod (
5081 method_name, flags, method.CallingConventions,
5082 method.ReturnType, param.GetEmitTypes ());
5088 // Generic method has been already defined to resolve method parameters
5089 // correctly when they use type parameters
5091 builder.SetParameters (param.GetEmitTypes ());
5092 builder.SetReturnType (method.ReturnType);
5094 if (builder.Attributes != flags) {
5096 if (methodbuilder_attrs_field == null)
5097 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
5098 methodbuilder_attrs_field.SetValue (builder, flags);
5100 Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
5104 throw new InternalErrorException ();
5111 public void Emit (DeclSpace parent)
5113 ToplevelBlock block = method.Block;
5117 ec = method.CreateEmitContext (parent, builder.GetILGenerator ());
5119 ec = method.CreateEmitContext (parent, null);
5121 method.ParameterInfo.ApplyAttributes (MethodBuilder);
5123 if (GenericMethod != null)
5124 GenericMethod.EmitAttributes ();
5126 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
5129 // Handle destructors specially
5131 // FIXME: This code generates buggy code
5133 if (member is Destructor)
5134 EmitDestructor (ec, block);
5136 ec.EmitTopBlock (method, block);
5138 if (source != null) {
5139 method.EmitExtraSymbolInfo (source);
5140 source.CloseMethod ();
5144 void EmitDestructor (EmitContext ec, ToplevelBlock block)
5146 ILGenerator ig = ec.ig;
5148 Label finish = ig.DefineLabel ();
5150 block.SetDestructor ();
5152 ig.BeginExceptionBlock ();
5153 ec.ReturnLabel = finish;
5154 ec.HasReturnLabel = true;
5155 ec.EmitTopBlock (method, block);
5157 // ig.MarkLabel (finish);
5158 ig.BeginFinallyBlock ();
5160 if (ec.ContainerType.BaseType != null) {
5161 Expression member_lookup = Expression.MemberLookup (
5162 ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
5163 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
5165 if (member_lookup != null){
5166 MethodGroupExpr base_destructor = ((MethodGroupExpr) member_lookup);
5168 ig.Emit (OpCodes.Ldarg_0);
5169 ig.Emit (OpCodes.Call, (MethodInfo) base_destructor.Methods [0]);
5173 ig.EndExceptionBlock ();
5174 //ig.MarkLabel (ec.ReturnLabel);
5175 ig.Emit (OpCodes.Ret);
5179 // TODO: Should derive from MethodCore
5180 public class Destructor : Method
5182 const int AllowedModifiers =
5186 static string[] attribute_targets = new string [] { "method" };
5188 public Destructor (DeclSpace parent, FullNamedExpression return_type, int mod,
5189 string name, Parameters parameters, Attributes attrs,
5191 : base (parent, return_type, mod, AllowedModifiers, new MemberName (name, l),
5194 ModFlags &= ~Modifiers.PRIVATE;
5195 if (!RootContext.StdLib && parent.Name == "System.Object")
5196 ModFlags |= Modifiers.PROTECTED | Modifiers.VIRTUAL;
5198 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
5201 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5203 if (a.Type == TypeManager.conditional_attribute_type) {
5204 Error_ConditionalAttributeIsNotValid ();
5208 base.ApplyAttributeBuilder (a, cb);
5211 public override string GetSignatureForError ()
5213 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
5216 public override string[] ValidAttributeTargets {
5218 return attribute_targets;
5223 abstract public class MemberBase : MemberCore {
5224 protected FullNamedExpression type_name;
5225 public readonly DeclSpace ds;
5226 public readonly GenericMethod GenericMethod;
5229 // The type of this property / indexer / event
5231 protected Type member_type;
5232 public Type MemberType {
5234 // TODO: Who wrote this, ResolveAsTypeTerminal can have side effects
5235 if (member_type == null && type_name != null) {
5236 IResolveContext rc = GenericMethod == null ? this : (IResolveContext)ds;
5237 type_name = type_name.ResolveAsTypeTerminal (rc, false);
5238 if (type_name != null) {
5239 member_type = type_name.Type;
5247 // The constructor is only exposed to our children
5249 protected MemberBase (DeclSpace parent, GenericMethod generic,
5250 FullNamedExpression type, int mod, int allowed_mod, int def_mod,
5251 MemberName name, Attributes attrs)
5252 : base (parent, name, attrs)
5254 this.ds = generic != null ? generic : (DeclSpace) parent;
5255 this.type_name = type;
5256 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, Location);
5257 GenericMethod = generic;
5258 if (GenericMethod != null)
5259 GenericMethod.ModFlags = ModFlags;
5262 protected virtual bool CheckBase ()
5264 CheckProtectedModifier ();
5269 protected virtual bool DoDefine ()
5271 if (MemberType == null)
5274 if ((Parent.ModFlags & Modifiers.SEALED) != 0 &&
5275 (ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0) {
5276 Report.Error (549, Location, "New virtual member `{0}' is declared in a sealed class `{1}'",
5277 GetSignatureForError (), Parent.GetSignatureForError ());
5281 // verify accessibility
5282 if (!IsAccessibleAs (MemberType)) {
5283 Report.SymbolRelatedToPreviousError (MemberType);
5284 if (this is Property)
5285 Report.Error (53, Location,
5286 "Inconsistent accessibility: property type `" +
5287 TypeManager.CSharpName (MemberType) + "' is less " +
5288 "accessible than property `" + GetSignatureForError () + "'");
5289 else if (this is Indexer)
5290 Report.Error (54, Location,
5291 "Inconsistent accessibility: indexer return type `" +
5292 TypeManager.CSharpName (MemberType) + "' is less " +
5293 "accessible than indexer `" + GetSignatureForError () + "'");
5294 else if (this is MethodCore) {
5295 if (this is Operator)
5296 Report.Error (56, Location,
5297 "Inconsistent accessibility: return type `" +
5298 TypeManager.CSharpName (MemberType) + "' is less " +
5299 "accessible than operator `" + GetSignatureForError () + "'");
5301 Report.Error (50, Location,
5302 "Inconsistent accessibility: return type `" +
5303 TypeManager.CSharpName (MemberType) + "' is less " +
5304 "accessible than method `" + GetSignatureForError () + "'");
5306 Report.Error (52, Location,
5307 "Inconsistent accessibility: field type `" +
5308 TypeManager.CSharpName (MemberType) + "' is less " +
5309 "accessible than field `" + GetSignatureForError () + "'");
5317 protected bool IsTypePermitted ()
5319 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5320 Report.Error (610, Location, "Field or property cannot be of type `{0}'", TypeManager.CSharpName (MemberType));
5328 // Abstract class for all fields
5330 abstract public class FieldBase : MemberBase {
5331 public FieldBuilder FieldBuilder;
5332 public Status status;
5333 protected Expression initializer;
5336 public enum Status : byte {
5337 HAS_OFFSET = 4 // Used by FieldMember.
5340 static readonly string[] attribute_targets = new string [] { "field" };
5342 protected FieldBase (DeclSpace parent, FullNamedExpression type, int mod,
5343 int allowed_mod, MemberName name, Attributes attrs)
5344 : base (parent, null, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE,
5347 if ((mod & Modifiers.ABSTRACT) != 0)
5348 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
5351 public override AttributeTargets AttributeTargets {
5353 return AttributeTargets.Field;
5357 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5359 if (a.Type == TypeManager.field_offset_attribute_type) {
5360 status |= Status.HAS_OFFSET;
5362 if (!Parent.PartialContainer.HasExplicitLayout) {
5363 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5367 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5368 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5374 if (a.Type == TypeManager.fixed_buffer_attr_type) {
5375 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
5381 if (a.Type == TypeManager.marshal_as_attr_type) {
5382 UnmanagedMarshal marshal = a.GetMarshal (this);
5383 if (marshal != null) {
5384 FieldBuilder.SetMarshal (marshal);
5389 if ((a.HasSecurityAttribute)) {
5390 a.Error_InvalidSecurityParent ();
5394 FieldBuilder.SetCustomAttribute (cb);
5397 protected override bool CheckBase ()
5399 if (!base.CheckBase ())
5402 MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
5403 if (conflict_symbol == null) {
5404 if ((ModFlags & Modifiers.NEW) != 0) {
5405 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
5410 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5411 Report.SymbolRelatedToPreviousError (conflict_symbol);
5412 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
5413 GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
5419 public override bool Define()
5421 if (MemberType == null || member_type == null)
5424 if (TypeManager.IsGenericParameter (MemberType))
5427 if (MemberType == TypeManager.void_type) {
5428 // TODO: wrong location
5429 Expression.Error_VoidInvalidInTheContext (Location);
5433 if (MemberType.IsSealed && MemberType.IsAbstract) {
5434 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType);
5444 if (!IsTypePermitted ())
5451 // Represents header string for documentation comment.
5453 public override string DocCommentHeader {
5454 get { return "F:"; }
5457 public override void Emit ()
5460 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
5461 FieldBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
5464 if (OptAttributes != null) {
5465 OptAttributes.Emit ();
5468 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
5469 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
5475 public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class)
5477 Report.SymbolRelatedToPreviousError (static_class);
5478 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
5482 public Expression Initializer {
5484 if (value != null) {
5485 this.initializer = value;
5490 protected virtual bool IsFieldClsCompliant {
5492 if (FieldBuilder == null)
5495 return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
5499 public override string[] ValidAttributeTargets
5502 return attribute_targets;
5506 protected override bool VerifyClsCompliance ()
5508 if (!base.VerifyClsCompliance ())
5511 if (!IsFieldClsCompliant) {
5512 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
5513 GetSignatureForError ());
5518 public void SetAssigned ()
5520 caching_flags |= Flags.IsAssigned;
5524 interface IFixedBuffer
5526 FieldInfo Element { get; }
5527 Type ElementType { get; }
5530 public class FixedFieldExternal: IFixedBuffer
5532 FieldInfo element_field;
5534 public FixedFieldExternal (FieldInfo fi)
5536 element_field = fi.FieldType.GetField (FixedField.FixedElementName);
5539 #region IFixedField Members
5541 public FieldInfo Element {
5543 return element_field;
5547 public Type ElementType {
5549 return element_field.FieldType;
5557 /// Fixed buffer implementation
5559 public class FixedField : FieldBase, IFixedBuffer
5561 public const string FixedElementName = "FixedElementField";
5562 static int GlobalCounter = 0;
5563 static object[] ctor_args = new object[] { (short)LayoutKind.Sequential };
5564 static FieldInfo[] fi;
5566 TypeBuilder fixed_buffer_type;
5567 FieldBuilder element;
5568 Expression size_expr;
5570 const int AllowedModifiers =
5573 Modifiers.PROTECTED |
5574 Modifiers.INTERNAL |
5577 public FixedField (DeclSpace parent, FullNamedExpression type, int mod, string name,
5578 Expression size_expr, Attributes attrs, Location loc):
5579 base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
5581 if (RootContext.Version == LanguageVersion.ISO_1)
5582 Report.FeatureIsNotAvailable (loc, "fixed size buffers");
5584 this.size_expr = size_expr;
5587 public override bool Define()
5589 if (!Parent.IsInUnsafeScope)
5590 Expression.UnsafeError (Location);
5592 if (!base.Define ())
5595 if (!TypeManager.IsPrimitiveType (MemberType)) {
5596 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",
5597 GetSignatureForError ());
5600 // Create nested fixed buffer container
5601 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
5602 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
5603 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
5605 element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
5606 RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
5608 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, Modifiers.FieldAttr (ModFlags));
5609 Parent.MemberCache.AddMember (FieldBuilder, this);
5610 TypeManager.RegisterFieldBase (FieldBuilder, this);
5615 public override void Emit()
5617 if (Parent.PartialContainer.Kind != Kind.Struct) {
5618 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
5619 GetSignatureForError ());
5622 EmitContext ec = new EmitContext (this, Parent, Location, null, TypeManager.void_type, ModFlags);
5623 Constant c = size_expr.ResolveAsConstant (ec, this);
5627 IntConstant buffer_size_const = c.ImplicitConversionRequired (ec, TypeManager.int32_type, Location) as IntConstant;
5628 if (buffer_size_const == null)
5631 int buffer_size = buffer_size_const.Value;
5633 if (buffer_size <= 0) {
5634 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
5638 int type_size = Expression.GetTypeSize (MemberType);
5640 if (buffer_size > int.MaxValue / type_size) {
5641 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
5642 GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
5646 buffer_size *= type_size;
5647 EmitFieldSize (buffer_size);
5651 void EmitFieldSize (int buffer_size)
5653 if (TypeManager.struct_layout_attribute_type == null) {
5654 TypeManager.struct_layout_attribute_type = TypeManager.CoreLookupType (
5655 "System.Runtime.InteropServices", "StructLayoutAttribute", Kind.Class, true);
5657 if (TypeManager.struct_layout_attribute_type == null)
5662 fi = new FieldInfo [] { TypeManager.struct_layout_attribute_type.GetField ("Size") };
5664 object [] fi_val = new object [] { buffer_size };
5666 if (TypeManager.struct_layout_attribute_ctor == null) {
5667 TypeManager.struct_layout_attribute_ctor = TypeManager.GetPredefinedConstructor (
5668 TypeManager.struct_layout_attribute_type, Location, TypeManager.short_type);
5669 if (TypeManager.struct_layout_attribute_ctor == null)
5673 CustomAttributeBuilder cab = new CustomAttributeBuilder (TypeManager.struct_layout_attribute_ctor,
5674 ctor_args, fi, fi_val);
5675 fixed_buffer_type.SetCustomAttribute (cab);
5678 // Don't emit FixedBufferAttribute attribute for private types
5680 if ((ModFlags & Modifiers.PRIVATE) != 0)
5683 if (TypeManager.fixed_buffer_attr_ctor == null) {
5684 if (TypeManager.fixed_buffer_attr_type == null) {
5685 TypeManager.fixed_buffer_attr_type = TypeManager.CoreLookupType (
5686 "System.Runtime.CompilerServices", "FixedBufferAttribute", Kind.Class, true);
5688 if (TypeManager.fixed_buffer_attr_type == null)
5692 TypeManager.fixed_buffer_attr_ctor = TypeManager.GetPredefinedConstructor (TypeManager.fixed_buffer_attr_type,
5693 Location, TypeManager.type_type, TypeManager.int32_type);
5695 if (TypeManager.fixed_buffer_attr_ctor == null)
5699 cab = new CustomAttributeBuilder (TypeManager.fixed_buffer_attr_ctor, new object [] { MemberType, buffer_size });
5700 FieldBuilder.SetCustomAttribute (cab);
5703 protected override bool IsFieldClsCompliant {
5709 #region IFixedField Members
5711 public FieldInfo Element {
5717 public Type ElementType {
5727 // The Field class is used to represents class/struct fields during parsing.
5729 public class Field : FieldBase {
5731 // Modifiers allowed in a class declaration
5733 const int AllowedModifiers =
5736 Modifiers.PROTECTED |
5737 Modifiers.INTERNAL |
5740 Modifiers.VOLATILE |
5744 public Field (DeclSpace parent, FullNamedExpression type, int mod, string name,
5745 Attributes attrs, Location loc)
5746 : base (parent, type, mod, AllowedModifiers, new MemberName (name, loc),
5751 bool CanBeVolatile ()
5753 if (TypeManager.IsReferenceType (MemberType))
5756 if (MemberType.IsEnum)
5759 if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
5760 MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
5761 MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
5762 MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
5763 MemberType == TypeManager.float_type)
5769 bool CheckStructLayout (Type type, bool isStatic)
5771 if (TypeManager.IsBuiltinType (type))
5775 if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
5779 if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
5780 if (!TypeManager.IsGenericType (type))
5783 foreach (Type t in TypeManager.GetTypeArguments (type)) {
5784 if (!CheckStructLayout (t, false))
5790 Report.Error (523, Location,
5791 "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
5792 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5796 public override bool Define ()
5798 if (!base.Define ())
5801 if ((ModFlags & Modifiers.VOLATILE) != 0){
5802 if (!CanBeVolatile ()) {
5803 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
5804 GetSignatureForError (), TypeManager.CSharpName (MemberType));
5807 if ((ModFlags & Modifiers.READONLY) != 0){
5808 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
5809 GetSignatureForError ());
5815 Type[] required_modifier = null;
5816 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5817 if (TypeManager.isvolatile_type == null)
5818 TypeManager.isvolatile_type = TypeManager.CoreLookupType (
5819 "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
5821 if (TypeManager.isvolatile_type != null)
5822 required_modifier = new Type [] { TypeManager.isvolatile_type };
5825 FieldBuilder = Parent.TypeBuilder.DefineField (
5826 Name, MemberType, required_modifier, null, Modifiers.FieldAttr (ModFlags));
5828 FieldBuilder = Parent.TypeBuilder.DefineField (
5829 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5831 Parent.MemberCache.AddMember (FieldBuilder, this);
5832 TypeManager.RegisterFieldBase (FieldBuilder, this);
5834 catch (ArgumentException) {
5835 Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
5839 if (initializer != null) {
5840 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
5841 new FieldInitializer (FieldBuilder, initializer));
5843 if (Parent.PartialContainer.Kind == Kind.Struct)
5844 CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
5850 public override string GetSignatureForError ()
5852 string s = base.GetSignatureForError ();
5853 if ((ModFlags & Modifiers.BACKING_FIELD) == 0)
5856 // Undecorate name mangling
5857 int l = s.LastIndexOf ('>');
5858 return s.Substring (0, l).Remove (s.LastIndexOf ('<'), 1);
5861 protected override bool VerifyClsCompliance ()
5863 if (!base.VerifyClsCompliance ())
5866 if ((ModFlags & Modifiers.VOLATILE) != 0) {
5867 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());
5875 // `set' and `get' accessors are represented with an Accessor.
5877 public class Accessor {
5879 // Null if the accessor is empty, or a Block if not
5881 public const int AllowedModifiers =
5883 Modifiers.PROTECTED |
5884 Modifiers.INTERNAL |
5887 public ToplevelBlock Block;
5888 public Attributes Attributes;
5889 public Location Location;
5890 public int ModFlags;
5891 public Parameters Parameters;
5893 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Parameters p, Location loc)
5899 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5903 // Ooouh Martin, templates are missing here.
5904 // When it will be possible move here a lot of child code and template method type.
5905 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
5906 protected MethodData method_data;
5907 protected ToplevelBlock block;
5908 protected ListDictionary declarative_security;
5910 // The accessor are created event if they are not wanted.
5911 // But we need them because their names are reserved.
5912 // Field says whether accessor will be emited or not
5913 public readonly bool IsDummy;
5915 protected readonly string prefix;
5917 ReturnParameter return_attributes;
5919 public AbstractPropertyEventMethod (PropertyBasedMember member, string prefix)
5920 : base (member.Parent, SetupName (prefix, member, member.Location), null)
5922 this.prefix = prefix;
5926 public AbstractPropertyEventMethod (InterfaceMemberBase member, Accessor accessor,
5928 : base (member.Parent, SetupName (prefix, member, accessor.Location),
5929 accessor.Attributes)
5931 this.prefix = prefix;
5932 this.block = accessor.Block;
5935 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
5937 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
5940 public void UpdateName (InterfaceMemberBase member)
5942 SetMemberName (SetupName (prefix, member, Location));
5945 #region IMethodData Members
5947 public ToplevelBlock Block {
5957 public CallingConventions CallingConventions {
5959 return CallingConventions.Standard;
5963 public bool IsExcluded ()
5968 GenericMethod IMethodData.GenericMethod {
5974 public MemberName MethodName {
5980 public Type[] ParameterTypes {
5982 return ParameterInfo.Types;
5986 public abstract Parameters ParameterInfo { get ; }
5987 public abstract Type ReturnType { get; }
5988 public abstract EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig);
5992 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5994 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5995 a.Type == TypeManager.conditional_attribute_type) {
5996 Report.Error (1667, a.Location,
5997 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
5998 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
6002 if (a.IsValidSecurityAttribute ()) {
6003 if (declarative_security == null)
6004 declarative_security = new ListDictionary ();
6005 a.ExtractSecurityPermissionSet (declarative_security);
6009 if (a.Target == AttributeTargets.Method) {
6010 method_data.MethodBuilder.SetCustomAttribute (cb);
6014 if (a.Target == AttributeTargets.ReturnValue) {
6015 if (return_attributes == null)
6016 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
6018 return_attributes.ApplyAttributeBuilder (a, cb);
6022 ApplyToExtraTarget (a, cb);
6025 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
6027 throw new NotSupportedException ("You forgot to define special attribute target handling");
6030 // It is not supported for the accessors
6031 public sealed override bool Define()
6033 throw new NotSupportedException ();
6036 public void Emit (DeclSpace parent)
6038 EmitMethod (parent);
6041 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
6042 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location));
6043 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
6044 method_data.MethodBuilder.SetCustomAttribute (TypeManager.GetDebuggerHiddenAttribute (Location));
6046 if (OptAttributes != null)
6047 OptAttributes.Emit ();
6049 if (declarative_security != null) {
6050 foreach (DictionaryEntry de in declarative_security) {
6051 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
6058 protected virtual void EmitMethod (DeclSpace parent)
6060 method_data.Emit (parent);
6063 public override bool EnableOverloadChecks (MemberCore overload)
6065 // This can only happen with indexers and it will
6066 // be catched as indexer difference
6067 if (overload is AbstractPropertyEventMethod)
6070 if (overload is MethodCore) {
6071 caching_flags |= Flags.MethodOverloadsExist;
6077 public override bool IsClsComplianceRequired()
6082 public bool IsDuplicateImplementation (MethodCore method)
6084 if (!MemberName.Equals (method.MemberName))
6087 Type[] param_types = method.ParameterTypes;
6089 if (param_types == null || param_types.Length != ParameterTypes.Length)
6092 for (int i = 0; i < param_types.Length; i++)
6093 if (param_types [i] != ParameterTypes [i])
6096 Report.SymbolRelatedToPreviousError (method);
6097 Report.Error (82, Location, "A member `{0}' is already reserved",
6098 method.GetSignatureForError ());
6102 public override bool IsUsed
6113 // Represents header string for documentation comment.
6115 public override string DocCommentHeader {
6116 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
6119 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
6124 // Properties and Indexers both generate PropertyBuilders, we use this to share
6125 // their common bits.
6127 abstract public class PropertyBase : PropertyBasedMember {
6129 public class GetMethod : PropertyMethod
6131 static string[] attribute_targets = new string [] { "method", "return" };
6133 public GetMethod (PropertyBase method):
6134 base (method, "get_")
6138 public GetMethod (PropertyBase method, Accessor accessor):
6139 base (method, accessor, "get_")
6143 public override MethodBuilder Define (DeclSpace parent)
6145 base.Define (parent);
6150 method_data = new MethodData (method, ModFlags, flags, this);
6152 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6155 return method_data.MethodBuilder;
6158 public override Type ReturnType {
6160 return method.MemberType;
6164 public override Parameters ParameterInfo {
6166 return Parameters.EmptyReadOnlyParameters;
6170 public override string[] ValidAttributeTargets {
6172 return attribute_targets;
6177 public class SetMethod : PropertyMethod {
6179 static string[] attribute_targets = new string [] { "method", "param", "return" };
6180 ImplicitParameter param_attr;
6181 protected Parameters parameters;
6183 public SetMethod (PropertyBase method) :
6184 base (method, "set_")
6186 parameters = new Parameters (
6187 new Parameter (method.type_name, "value", Parameter.Modifier.NONE, null, Location));
6190 public SetMethod (PropertyBase method, Accessor accessor):
6191 base (method, accessor, "set_")
6193 this.parameters = accessor.Parameters;
6196 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6198 if (a.Target == AttributeTargets.Parameter) {
6199 if (param_attr == null)
6200 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6202 param_attr.ApplyAttributeBuilder (a, cb);
6206 base.ApplyAttributeBuilder (a, cb);
6209 public override Parameters ParameterInfo {
6215 public override MethodBuilder Define (DeclSpace parent)
6217 parameters.Resolve (ResolveContext);
6218 base.Define (parent);
6223 method_data = new MethodData (method, ModFlags, flags, this);
6225 if (!method_data.Define (parent, method.GetFullName (MemberName)))
6228 return method_data.MethodBuilder;
6231 public override Type ReturnType {
6233 return TypeManager.void_type;
6237 public override string[] ValidAttributeTargets {
6239 return attribute_targets;
6244 static string[] attribute_targets = new string [] { "property" };
6246 public abstract class PropertyMethod : AbstractPropertyEventMethod
6248 protected readonly PropertyBase method;
6249 protected MethodAttributes flags;
6251 public PropertyMethod (PropertyBase method, string prefix)
6252 : base (method, prefix)
6254 this.method = method;
6255 this.ModFlags = method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE);
6258 public PropertyMethod (PropertyBase method, Accessor accessor,
6260 : base (method, accessor, prefix)
6262 this.method = method;
6263 this.ModFlags = accessor.ModFlags | (method.ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE));
6265 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6266 Report.FeatureIsNotAvailable (Location, "access modifiers on properties");
6270 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6272 if (a.IsInternalMethodImplAttribute) {
6273 method.is_external_implementation = true;
6276 base.ApplyAttributeBuilder (a, cb);
6279 public override AttributeTargets AttributeTargets {
6281 return AttributeTargets.Method;
6285 public override bool IsClsComplianceRequired ()
6287 return method.IsClsComplianceRequired ();
6290 public virtual MethodBuilder Define (DeclSpace parent)
6292 CheckForDuplications ();
6295 if (method.InterfaceType != null && method.IsExplicitImpl) {
6296 MethodInfo mi = parent.PartialContainer.PendingImplementations.IsInterfaceMethod (
6297 MethodName.Name, method.InterfaceType, new MethodData (method, ModFlags, flags, this));
6299 Report.SymbolRelatedToPreviousError (mi);
6300 Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'",
6301 method.GetSignatureForError (), TypeManager.CSharpSignature (mi, true));
6307 TypeContainer container = parent.PartialContainer;
6310 // Check for custom access modifier
6312 if ((ModFlags & Modifiers.Accessibility) == 0) {
6313 ModFlags |= method.ModFlags;
6314 flags = method.flags;
6316 if (container.Kind == Kind.Interface)
6317 Report.Error (275, Location, "`{0}': accessibility modifiers may not be used on accessors in an interface",
6318 GetSignatureForError ());
6320 if ((method.ModFlags & Modifiers.ABSTRACT) != 0 && (ModFlags & Modifiers.PRIVATE) != 0) {
6321 Report.Error (442, Location, "`{0}': abstract properties cannot have private accessors", GetSignatureForError ());
6324 CheckModifiers (ModFlags);
6325 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6326 ModFlags |= Modifiers.PROPERTY_CUSTOM;
6327 flags = Modifiers.MethodAttr (ModFlags);
6328 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6331 CheckAbstractAndExtern (block != null);
6333 if (block != null && block.IsIterator)
6334 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
6339 public bool HasCustomAccessModifier
6342 return (ModFlags & Modifiers.PROPERTY_CUSTOM) != 0;
6346 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
6348 return new EmitContext (this,
6349 ds, method.ds, method.Location, ig, ReturnType,
6350 method.ModFlags, false);
6353 public override ObsoleteAttribute GetObsoleteAttribute ()
6355 return method.GetObsoleteAttribute ();
6358 public override string GetSignatureForError()
6360 return method.GetSignatureForError () + '.' + prefix.Substring (0, 3);
6363 void CheckModifiers (int modflags)
6365 modflags &= Modifiers.Accessibility;
6367 int mflags = method.ModFlags & Modifiers.Accessibility;
6369 if ((mflags & Modifiers.PUBLIC) != 0) {
6370 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6372 else if ((mflags & Modifiers.PROTECTED) != 0) {
6373 if ((mflags & Modifiers.INTERNAL) != 0)
6374 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6376 flags |= Modifiers.PRIVATE;
6378 else if ((mflags & Modifiers.INTERNAL) != 0)
6379 flags |= Modifiers.PRIVATE;
6381 if ((mflags == modflags) || (modflags & (~flags)) != 0) {
6382 Report.Error (273, Location,
6383 "The accessibility modifier of the `{0}' accessor must be more restrictive than the modifier of the property or indexer `{1}'",
6384 GetSignatureForError (), method.GetSignatureForError ());
6388 protected bool CheckForDuplications ()
6390 if ((caching_flags & Flags.MethodOverloadsExist) == 0)
6393 return Parent.MemberCache.CheckExistingMembersOverloads (this, Name, ParameterInfo);
6397 public PropertyMethod Get, Set;
6398 public PropertyBuilder PropertyBuilder;
6399 public MethodBuilder GetBuilder, SetBuilder;
6401 protected bool define_set_first = false;
6403 public PropertyBase (DeclSpace parent, FullNamedExpression type, int mod_flags,
6404 int allowed_mod, MemberName name,
6405 Attributes attrs, bool define_set_first)
6406 : base (parent, null, type, mod_flags, allowed_mod, name, attrs)
6408 this.define_set_first = define_set_first;
6411 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6413 if (a.HasSecurityAttribute) {
6414 a.Error_InvalidSecurityParent ();
6418 PropertyBuilder.SetCustomAttribute (cb);
6421 public override AttributeTargets AttributeTargets {
6423 return AttributeTargets.Property;
6427 public override bool Define ()
6432 if (!IsTypePermitted ())
6438 protected override bool DoDefine ()
6440 if (!base.DoDefine ())
6444 // Accessors modifiers check
6446 if ((Get.ModFlags & Modifiers.Accessibility) != 0 &&
6447 (Set.ModFlags & Modifiers.Accessibility) != 0) {
6448 Report.Error (274, Location, "`{0}': Cannot specify accessibility modifiers for both accessors of the property or indexer",
6449 GetSignatureForError ());
6453 if ((ModFlags & Modifiers.OVERRIDE) == 0 &&
6454 (Get.IsDummy && (Set.ModFlags & Modifiers.Accessibility) != 0) ||
6455 (Set.IsDummy && (Get.ModFlags & Modifiers.Accessibility) != 0)) {
6456 Report.Error (276, Location,
6457 "`{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor",
6458 GetSignatureForError ());
6463 if (MemberType.IsGenericParameter)
6467 if ((MemberType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
6468 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6477 GetBuilder = Get.Define (Parent);
6478 return (Get.IsDummy) ? true : GetBuilder != null;
6481 bool DefineSet (bool define)
6486 SetBuilder = Set.Define (Parent);
6487 return (Set.IsDummy) ? true : SetBuilder != null;
6490 protected bool DefineAccessors ()
6492 return DefineSet (define_set_first) &&
6494 DefineSet (!define_set_first);
6497 protected abstract PropertyInfo ResolveBaseProperty ();
6499 // TODO: rename to Resolve......
6500 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
6502 PropertyInfo base_property = ResolveBaseProperty ();
6503 if (base_property == null)
6506 base_ret_type = base_property.PropertyType;
6507 MethodInfo get_accessor = base_property.GetGetMethod (true);
6508 MethodInfo set_accessor = base_property.GetSetMethod (true);
6509 MethodAttributes get_accessor_access = 0, set_accessor_access = 0;
6512 // Check base property accessors conflict
6514 if ((ModFlags & (Modifiers.OVERRIDE | Modifiers.NEW)) == Modifiers.OVERRIDE) {
6515 if (get_accessor == null) {
6516 if (Get != null && !Get.IsDummy) {
6517 Report.SymbolRelatedToPreviousError (base_property);
6518 Report.Error (545, Location,
6519 "`{0}.get': cannot override because `{1}' does not have an overridable get accessor",
6520 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6523 get_accessor_access = get_accessor.Attributes & MethodAttributes.MemberAccessMask;
6525 if (!Get.IsDummy && !CheckAccessModifiers (
6526 Modifiers.MethodAttr (Get.ModFlags) & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6527 Error_CannotChangeAccessModifiers (Get.Location, get_accessor, get_accessor_access, ".get");
6530 if (set_accessor == null) {
6531 if (Set != null && !Set.IsDummy) {
6532 Report.SymbolRelatedToPreviousError (base_property);
6533 Report.Error (546, Location,
6534 "`{0}.set': cannot override because `{1}' does not have an overridable set accessor",
6535 GetSignatureForError (), TypeManager.GetFullNameSignature (base_property));
6538 set_accessor_access = set_accessor.Attributes & MethodAttributes.MemberAccessMask;
6540 if (!Set.IsDummy && !CheckAccessModifiers (
6541 Modifiers.MethodAttr (Set.ModFlags) & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6542 Error_CannotChangeAccessModifiers (Set.Location, set_accessor, set_accessor_access, ".set");
6546 // When one accessor does not exist and property hides base one
6547 // we need to propagate this upwards
6548 if (set_accessor == null)
6549 set_accessor = get_accessor;
6552 // Get the less restrictive access
6554 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6557 public override void Emit ()
6560 // The PropertyBuilder can be null for explicit implementations, in that
6561 // case, we do not actually emit the ".property", so there is nowhere to
6562 // put the attribute
6564 if (PropertyBuilder != null && OptAttributes != null)
6565 OptAttributes.Emit ();
6577 /// Tests whether accessors are not in collision with some method (CS0111)
6579 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6581 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6584 public override bool IsUsed
6590 return Get.IsUsed | Set.IsUsed;
6594 protected override void SetMemberName (MemberName new_name)
6596 base.SetMemberName (new_name);
6598 Get.UpdateName (this);
6599 Set.UpdateName (this);
6602 public override string[] ValidAttributeTargets {
6604 return attribute_targets;
6609 // Represents header string for documentation comment.
6611 public override string DocCommentHeader {
6612 get { return "P:"; }
6616 public class Property : PropertyBase {
6617 const int AllowedModifiers =
6620 Modifiers.PROTECTED |
6621 Modifiers.INTERNAL |
6625 Modifiers.OVERRIDE |
6626 Modifiers.ABSTRACT |
6631 const int AllowedInterfaceModifiers =
6634 void CreateAutomaticProperty (Block block, Accessor get_block, Accessor set_block)
6637 Field field = new Field (
6639 Modifiers.BACKING_FIELD | Modifiers.PRIVATE | (ModFlags & (Modifiers.STATIC | Modifiers.UNSAFE)),
6640 "<" + Name + ">k__BackingField", null, Location);
6641 ((TypeContainer)Parent).PartialContainer.AddField (field);
6644 get_block.Block = new ToplevelBlock (block, null, Location);
6645 Return r = new Return (new SimpleName(field.Name, Location), Location);
6646 get_block.Block.AddStatement (r);
6647 get_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6650 set_block.Block = new ToplevelBlock (block, set_block.Parameters, Location);
6651 Assign a = new SimpleAssign (new SimpleName (field.Name, Location), new SimpleName ("value", Location));
6652 set_block.Block.AddStatement (new StatementExpression(a));
6653 set_block.ModFlags |= Modifiers.COMPILER_GENERATED;
6656 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6657 MemberName name, Attributes attrs, Accessor get_block,
6658 Accessor set_block, bool define_set_first)
6659 : this (parent, type, mod, name, attrs, get_block, set_block,
6660 define_set_first, null)
6664 public Property (DeclSpace parent, FullNamedExpression type, int mod,
6665 MemberName name, Attributes attrs, Accessor get_block,
6666 Accessor set_block, bool define_set_first, Block current_block)
6667 : base (parent, type, mod,
6668 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
6669 name, attrs, define_set_first)
6671 if (!IsInterface && (mod & (Modifiers.ABSTRACT | Modifiers.EXTERN)) == 0 &&
6672 get_block != null && get_block.Block == null &&
6673 set_block != null && set_block.Block == null) {
6674 if (RootContext.Version <= LanguageVersion.ISO_2)
6675 Report.FeatureIsNotAvailable (Location, "automatically implemented properties");
6677 CreateAutomaticProperty (current_block, get_block, set_block);
6680 if (get_block == null)
6681 Get = new GetMethod (this);
6683 Get = new GetMethod (this, get_block);
6685 if (set_block == null)
6686 Set = new SetMethod (this);
6688 Set = new SetMethod (this, set_block);
6691 public override bool Define ()
6693 if (!DoDefineBase ())
6696 if (!base.Define ())
6699 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6701 if (!DefineAccessors ())
6707 // FIXME - PropertyAttributes.HasDefault ?
6709 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6710 GetFullName (MemberName), PropertyAttributes.None, MemberType, null);
6713 PropertyBuilder.SetGetMethod (GetBuilder);
6714 Parent.MemberCache.AddMember (GetBuilder, Get);
6718 PropertyBuilder.SetSetMethod (SetBuilder);
6719 Parent.MemberCache.AddMember (SetBuilder, Set);
6722 TypeManager.RegisterProperty (PropertyBuilder, this);
6723 Parent.MemberCache.AddMember (PropertyBuilder, this);
6727 public override void Emit ()
6729 if (((Set.ModFlags | Get.ModFlags) & (Modifiers.STATIC | Modifiers.COMPILER_GENERATED)) == Modifiers.COMPILER_GENERATED && Parent.PartialContainer.HasExplicitLayout) {
6730 Report.Error (842, Location,
6731 "Automatically implemented property `{0}' cannot be used inside a type with an explicit StructLayout attribute",
6732 GetSignatureForError ());
6738 protected override PropertyInfo ResolveBaseProperty ()
6740 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
6741 Parent.TypeBuilder, Name, Parameters.EmptyReadOnlyParameters, null, true) as PropertyInfo;
6746 /// Gigantic workaround for lameness in SRE follows :
6747 /// This class derives from EventInfo and attempts to basically
6748 /// wrap around the EventBuilder so that FindMembers can quickly
6749 /// return this in it search for members
6751 public class MyEventBuilder : EventInfo {
6754 // We use this to "point" to our Builder which is
6755 // not really a MemberInfo
6757 EventBuilder MyBuilder;
6760 // We "catch" and wrap these methods
6762 MethodInfo raise, remove, add;
6764 EventAttributes attributes;
6765 Type declaring_type, reflected_type, event_type;
6770 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6772 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6774 // And now store the values in our own fields.
6776 declaring_type = type_builder;
6778 reflected_type = type_builder;
6780 attributes = event_attr;
6783 this.event_type = event_type;
6787 // Methods that you have to override. Note that you only need
6788 // to "implement" the variants that take the argument (those are
6789 // the "abstract" methods, the others (GetAddMethod()) are
6792 public override MethodInfo GetAddMethod (bool nonPublic)
6797 public override MethodInfo GetRemoveMethod (bool nonPublic)
6802 public override MethodInfo GetRaiseMethod (bool nonPublic)
6808 // These methods make "MyEventInfo" look like a Builder
6810 public void SetRaiseMethod (MethodBuilder raiseMethod)
6812 raise = raiseMethod;
6813 MyBuilder.SetRaiseMethod (raiseMethod);
6816 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6818 remove = removeMethod;
6819 MyBuilder.SetRemoveOnMethod (removeMethod);
6822 public void SetAddOnMethod (MethodBuilder addMethod)
6825 MyBuilder.SetAddOnMethod (addMethod);
6828 public void SetCustomAttribute (CustomAttributeBuilder cb)
6830 MyBuilder.SetCustomAttribute (cb);
6833 public override object [] GetCustomAttributes (bool inherit)
6835 // FIXME : There's nothing which can be seemingly done here because
6836 // we have no way of getting at the custom attribute objects of the
6841 public override object [] GetCustomAttributes (Type t, bool inherit)
6843 // FIXME : Same here !
6847 public override bool IsDefined (Type t, bool b)
6852 public override EventAttributes Attributes {
6858 public override string Name {
6864 public override Type DeclaringType {
6866 return declaring_type;
6870 public override Type ReflectedType {
6872 return reflected_type;
6876 public Type EventType {
6882 public void SetUsed ()
6884 if (my_event != null) {
6885 // my_event.SetAssigned ();
6886 my_event.SetMemberIsUsed ();
6892 /// For case when event is declared like property (with add and remove accessors).
6894 public class EventProperty: Event {
6895 abstract class AEventPropertyAccessor : AEventAccessor
6897 protected AEventPropertyAccessor (Event method, Accessor accessor, string prefix):
6898 base (method, accessor, prefix)
6902 public override MethodBuilder Define (DeclSpace ds)
6904 CheckAbstractAndExtern (block != null);
6905 return base.Define (ds);
6908 public override string GetSignatureForError ()
6910 return method.GetSignatureForError () + "." + prefix.Substring (0, prefix.Length - 1);
6914 sealed class AddDelegateMethod: AEventPropertyAccessor
6916 public AddDelegateMethod (Event method, Accessor accessor):
6917 base (method, accessor, "add_")
6921 protected override MethodInfo DelegateMethodInfo {
6923 return TypeManager.delegate_combine_delegate_delegate;
6928 sealed class RemoveDelegateMethod: AEventPropertyAccessor
6930 public RemoveDelegateMethod (Event method, Accessor accessor):
6931 base (method, accessor, "remove_")
6935 protected override MethodInfo DelegateMethodInfo {
6937 return TypeManager.delegate_remove_delegate_delegate;
6943 static readonly string[] attribute_targets = new string [] { "event" }; // "property" target was disabled for 2.0 version
6945 public EventProperty (DeclSpace parent, FullNamedExpression type, int mod_flags,
6947 Attributes attrs, Accessor add, Accessor remove)
6948 : base (parent, type, mod_flags, name, attrs)
6950 Add = new AddDelegateMethod (this, add);
6951 Remove = new RemoveDelegateMethod (this, remove);
6954 public override bool Define()
6956 if (!base.Define ())
6963 public override string[] ValidAttributeTargets {
6965 return attribute_targets;
6971 /// Event is declared like field.
6973 public class EventField : Event {
6974 abstract class EventFieldAccessor : AEventAccessor
6976 protected EventFieldAccessor (Event method, string prefix)
6977 : base (method, prefix)
6981 protected override void EmitMethod(DeclSpace parent)
6983 if ((method.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
6986 MethodBuilder mb = method_data.MethodBuilder;
6987 ILGenerator ig = mb.GetILGenerator ();
6989 // TODO: because we cannot use generics yet
6990 FieldInfo field_info = ((EventField)method).FieldBuilder;
6992 if (parent is Class) {
6993 mb.SetImplementationFlags (mb.GetMethodImplementationFlags () | MethodImplAttributes.Synchronized);
6996 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6997 ig.Emit (OpCodes.Ldsfld, field_info);
6998 ig.Emit (OpCodes.Ldarg_0);
6999 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7000 ig.Emit (OpCodes.Castclass, method.MemberType);
7001 ig.Emit (OpCodes.Stsfld, field_info);
7003 ig.Emit (OpCodes.Ldarg_0);
7004 ig.Emit (OpCodes.Ldarg_0);
7005 ig.Emit (OpCodes.Ldfld, field_info);
7006 ig.Emit (OpCodes.Ldarg_1);
7007 ig.Emit (OpCodes.Call, DelegateMethodInfo);
7008 ig.Emit (OpCodes.Castclass, method.MemberType);
7009 ig.Emit (OpCodes.Stfld, field_info);
7011 ig.Emit (OpCodes.Ret);
7015 sealed class AddDelegateMethod: EventFieldAccessor
7017 public AddDelegateMethod (Event method):
7018 base (method, "add_")
7022 protected override MethodInfo DelegateMethodInfo {
7024 return TypeManager.delegate_combine_delegate_delegate;
7029 sealed class RemoveDelegateMethod: EventFieldAccessor
7031 public RemoveDelegateMethod (Event method):
7032 base (method, "remove_")
7036 protected override MethodInfo DelegateMethodInfo {
7038 return TypeManager.delegate_remove_delegate_delegate;
7044 static readonly string[] attribute_targets = new string [] { "event", "field", "method" };
7045 static readonly string[] attribute_targets_interface = new string[] { "event", "method" };
7047 public FieldBuilder FieldBuilder;
7048 public Expression Initializer;
7050 public EventField (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7051 : base (parent, type, mod_flags, name, attrs)
7053 Add = new AddDelegateMethod (this);
7054 Remove = new RemoveDelegateMethod (this);
7057 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7059 if (a.Target == AttributeTargets.Field) {
7060 FieldBuilder.SetCustomAttribute (cb);
7064 if (a.Target == AttributeTargets.Method) {
7065 int errors = Report.Errors;
7066 Add.ApplyAttributeBuilder (a, cb);
7067 if (errors == Report.Errors)
7068 Remove.ApplyAttributeBuilder (a, cb);
7072 base.ApplyAttributeBuilder (a, cb);
7075 public override bool Define()
7077 if (!base.Define ())
7083 // FIXME: We are unable to detect whether generic event is used because
7084 // we are using FieldExpr instead of EventExpr for event access in that
7085 // case. When this issue will be fixed this hack can be removed.
7086 if (TypeManager.IsGenericType (MemberType))
7089 if (Add.IsInterfaceImplementation)
7092 FieldBuilder = Parent.TypeBuilder.DefineField (
7094 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7095 TypeManager.RegisterEventField (EventBuilder, this);
7097 if (Initializer != null) {
7098 if (((ModFlags & Modifiers.ABSTRACT) != 0)) {
7099 Report.Error (74, Location, "`{0}': abstract event cannot have an initializer",
7100 GetSignatureForError ());
7104 ((TypeContainer) Parent).RegisterFieldForInitialization (this,
7105 new FieldInitializer (FieldBuilder, Initializer));
7111 public override string[] ValidAttributeTargets
7114 return IsInterface ? attribute_targets_interface : attribute_targets;
7119 public abstract class Event : PropertyBasedMember {
7120 public abstract class AEventAccessor : AbstractPropertyEventMethod
7122 protected readonly Event method;
7123 ImplicitParameter param_attr;
7125 static readonly string[] attribute_targets = new string [] { "method", "param", "return" };
7127 protected AEventAccessor (Event method, string prefix)
7128 : base (method, prefix)
7130 this.method = method;
7131 this.ModFlags = method.ModFlags;
7134 protected AEventAccessor (Event method, Accessor accessor, string prefix)
7135 : base (method, accessor, prefix)
7137 this.method = method;
7138 this.ModFlags = method.ModFlags;
7141 public bool IsInterfaceImplementation {
7142 get { return method_data.implementing != null; }
7145 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7147 if (a.IsInternalMethodImplAttribute) {
7148 method.is_external_implementation = true;
7151 base.ApplyAttributeBuilder (a, cb);
7154 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
7156 if (a.Target == AttributeTargets.Parameter) {
7157 if (param_attr == null)
7158 param_attr = new ImplicitParameter (method_data.MethodBuilder);
7160 param_attr.ApplyAttributeBuilder (a, cb);
7164 base.ApplyAttributeBuilder (a, cb);
7167 public override AttributeTargets AttributeTargets {
7169 return AttributeTargets.Method;
7173 public override bool IsClsComplianceRequired ()
7175 return method.IsClsComplianceRequired ();
7178 public virtual MethodBuilder Define (DeclSpace parent)
7180 method_data = new MethodData (method, method.ModFlags,
7181 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
7183 if (!method_data.Define (parent, method.GetFullName (MemberName)))
7186 MethodBuilder mb = method_data.MethodBuilder;
7187 ParameterInfo.ApplyAttributes (mb);
7191 protected abstract MethodInfo DelegateMethodInfo { get; }
7193 public override Type ReturnType {
7195 return TypeManager.void_type;
7199 public override EmitContext CreateEmitContext (DeclSpace ds, ILGenerator ig)
7201 return new EmitContext (
7202 this, method.Parent, Location, ig, ReturnType,
7203 method.ModFlags, false);
7206 public override ObsoleteAttribute GetObsoleteAttribute ()
7208 return method.GetObsoleteAttribute ();
7211 public override string[] ValidAttributeTargets {
7213 return attribute_targets;
7217 public override Parameters ParameterInfo {
7219 return method.parameters;
7225 const int AllowedModifiers =
7228 Modifiers.PROTECTED |
7229 Modifiers.INTERNAL |
7234 Modifiers.OVERRIDE |
7236 Modifiers.ABSTRACT |
7239 const int AllowedInterfaceModifiers =
7242 public AEventAccessor Add, Remove;
7243 public MyEventBuilder EventBuilder;
7244 public MethodBuilder AddBuilder, RemoveBuilder;
7246 Parameters parameters;
7248 protected Event (DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, Attributes attrs)
7249 : base (parent, null, type, mod_flags,
7250 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7255 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7257 if ((a.HasSecurityAttribute)) {
7258 a.Error_InvalidSecurityParent ();
7262 EventBuilder.SetCustomAttribute (cb);
7265 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
7267 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
7270 public override AttributeTargets AttributeTargets {
7272 return AttributeTargets.Event;
7276 public override bool Define ()
7278 if (!DoDefineBase ())
7284 if (!TypeManager.IsDelegateType (MemberType)) {
7285 Report.Error (66, Location, "`{0}': event must be of a delegate type", GetSignatureForError ());
7289 parameters = Parameters.CreateFullyResolved (
7290 new Parameter (null, "value", Parameter.Modifier.NONE, null, Location), MemberType);
7295 if (TypeManager.delegate_combine_delegate_delegate == null) {
7296 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
7297 TypeManager.delegate_type, "Combine", Location,
7298 TypeManager.delegate_type, TypeManager.delegate_type);
7300 if (TypeManager.delegate_remove_delegate_delegate == null) {
7301 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
7302 TypeManager.delegate_type, "Remove", Location,
7303 TypeManager.delegate_type, TypeManager.delegate_type);
7307 // Now define the accessors
7310 AddBuilder = Add.Define (Parent);
7311 if (AddBuilder == null)
7314 RemoveBuilder = Remove.Define (Parent);
7315 if (RemoveBuilder == null)
7318 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, EventAttributes.None, MemberType);
7319 EventBuilder.SetAddOnMethod (AddBuilder);
7320 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7322 Parent.MemberCache.AddMember (EventBuilder, this);
7323 Parent.MemberCache.AddMember (AddBuilder, Add);
7324 Parent.MemberCache.AddMember (RemoveBuilder, Remove);
7329 public override void Emit ()
7331 if (OptAttributes != null) {
7332 OptAttributes.Emit ();
7336 Remove.Emit (Parent);
7341 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7343 MethodInfo mi = (MethodInfo) Parent.PartialContainer.BaseCache.FindBaseEvent (
7344 Parent.TypeBuilder, Name);
7349 AParametersCollection pd = TypeManager.GetParameterData (mi);
7350 base_ret_type = pd.Types [0];
7355 // Represents header string for documentation comment.
7357 public override string DocCommentHeader {
7358 get { return "E:"; }
7363 public class Indexer : PropertyBase
7365 public class GetIndexerMethod : GetMethod
7367 Parameters parameters;
7369 public GetIndexerMethod (Indexer method):
7372 this.parameters = method.parameters;
7375 public GetIndexerMethod (PropertyBase method, Accessor accessor):
7376 base (method, accessor)
7378 parameters = accessor.Parameters;
7381 public override MethodBuilder Define (DeclSpace parent)
7383 parameters.Resolve (ResolveContext);
7384 return base.Define (parent);
7387 public override bool EnableOverloadChecks (MemberCore overload)
7389 if (base.EnableOverloadChecks (overload)) {
7390 overload.caching_flags |= Flags.MethodOverloadsExist;
7397 public override Parameters ParameterInfo {
7404 public class SetIndexerMethod: SetMethod
7406 public SetIndexerMethod (Indexer method):
7409 parameters = Parameters.MergeGenerated (method.parameters, false, parameters [0], null);
7412 public SetIndexerMethod (PropertyBase method, Accessor accessor):
7413 base (method, accessor)
7415 parameters = method.Get.IsDummy ? accessor.Parameters : accessor.Parameters.Clone ();
7418 public override bool EnableOverloadChecks (MemberCore overload)
7420 if (base.EnableOverloadChecks (overload)) {
7421 overload.caching_flags |= Flags.MethodOverloadsExist;
7429 const int AllowedModifiers =
7432 Modifiers.PROTECTED |
7433 Modifiers.INTERNAL |
7437 Modifiers.OVERRIDE |
7442 const int AllowedInterfaceModifiers =
7445 public readonly Parameters parameters;
7447 public Indexer (DeclSpace parent, FullNamedExpression type, MemberName name, int mod,
7448 Parameters parameters, Attributes attrs,
7449 Accessor get_block, Accessor set_block, bool define_set_first)
7450 : base (parent, type, mod,
7451 parent.PartialContainer.Kind == Kind.Interface ? AllowedInterfaceModifiers : AllowedModifiers,
7452 name, attrs, define_set_first)
7454 this.parameters = parameters;
7456 if (get_block == null)
7457 Get = new GetIndexerMethod (this);
7459 Get = new GetIndexerMethod (this, get_block);
7461 if (set_block == null)
7462 Set = new SetIndexerMethod (this);
7464 Set = new SetIndexerMethod (this, set_block);
7467 public override bool Define ()
7469 if (!DoDefineBase ())
7472 if (!base.Define ())
7475 if (!DefineParameters (parameters))
7478 if (OptAttributes != null && TypeManager.indexer_name_type != null) {
7479 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type);
7480 if (indexer_attr != null) {
7481 // Remove the attribute from the list because it is not emitted
7482 OptAttributes.Attrs.Remove (indexer_attr);
7484 string name = indexer_attr.GetIndexerAttributeValue ();
7490 if (IsExplicitImpl) {
7491 Report.Error (415, indexer_attr.Location,
7492 "The `IndexerName' attribute is valid only on an " +
7493 "indexer that is not an explicit interface member declaration");
7497 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7498 Report.Error (609, indexer_attr.Location,
7499 "Cannot set the `IndexerName' attribute on an indexer marked override");
7505 if (InterfaceType != null) {
7506 string base_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7507 if (base_IndexerName != Name)
7508 ShortName = base_IndexerName;
7511 if (!Parent.PartialContainer.AddMember (this) ||
7512 !Parent.PartialContainer.AddMember (Get) || !Parent.PartialContainer.AddMember (Set))
7515 if ((caching_flags & Flags.MethodOverloadsExist) != 0) {
7516 if (!Parent.MemberCache.CheckExistingMembersOverloads (this, Name, parameters))
7520 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7522 if (!DefineAccessors ())
7529 // Now name the parameters
7531 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7532 GetFullName (MemberName), PropertyAttributes.None, MemberType, parameters.GetEmitTypes ());
7535 PropertyBuilder.SetGetMethod (GetBuilder);
7536 Parent.MemberCache.AddMember (GetBuilder, Get);
7540 PropertyBuilder.SetSetMethod (SetBuilder);
7541 Parent.MemberCache.AddMember (SetBuilder, Set);
7544 TypeManager.RegisterIndexer (PropertyBuilder, parameters);
7545 Parent.MemberCache.AddMember (PropertyBuilder, this);
7549 public override bool EnableOverloadChecks (MemberCore overload)
7551 if (overload is Indexer) {
7552 caching_flags |= Flags.MethodOverloadsExist;
7559 public override string GetDocCommentName (DeclSpace ds)
7561 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
7564 public override string GetSignatureForError ()
7566 StringBuilder sb = new StringBuilder (Parent.GetSignatureForError ());
7567 if (MemberName.Left != null) {
7569 sb.Append (MemberName.Left.GetSignatureForError ());
7572 sb.Append (".this");
7573 sb.Append (parameters.GetSignatureForError ().Replace ('(', '[').Replace (')', ']'));
7574 return sb.ToString ();
7577 protected override PropertyInfo ResolveBaseProperty ()
7579 return Parent.PartialContainer.BaseCache.FindMemberToOverride (
7580 Parent.TypeBuilder, Name, parameters, null, true) as PropertyInfo;
7583 protected override bool VerifyClsCompliance ()
7585 if (!base.VerifyClsCompliance ())
7588 parameters.VerifyClsCompliance ();
7593 public class Operator : MethodOrOperator {
7595 const int AllowedModifiers =
7601 public enum OpType : byte {
7611 // Unary and Binary operators
7634 // Implicit and Explicit
7638 // Just because of enum
7642 public readonly OpType OperatorType;
7644 static readonly string [] [] names;
7648 names = new string[(int)OpType.TOP][];
7649 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
7650 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
7651 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
7652 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
7653 names [(int) OpType.True] = new string [] { "true", "op_True" };
7654 names [(int) OpType.False] = new string [] { "false", "op_False" };
7655 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
7656 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
7657 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
7658 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
7659 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
7660 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
7661 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
7662 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
7663 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
7664 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
7665 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
7666 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
7667 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
7668 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
7669 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
7670 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
7671 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
7672 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
7673 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
7674 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
7677 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
7678 int mod_flags, Parameters parameters,
7679 ToplevelBlock block, Attributes attrs, Location loc)
7680 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
7681 new MemberName ("op_" + type.ToString(), loc), attrs, parameters)
7683 OperatorType = type;
7687 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7689 if (a.Type == TypeManager.conditional_attribute_type) {
7690 Error_ConditionalAttributeIsNotValid ();
7694 base.ApplyAttributeBuilder (a, cb);
7697 public override bool Define ()
7699 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7700 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7701 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
7705 if (!base.Define ())
7708 // imlicit and explicit operator of same types are not allowed
7709 if (OperatorType == OpType.Explicit)
7710 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), Parameters);
7711 else if (OperatorType == OpType.Implicit)
7712 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), Parameters);
7714 if (MemberType == TypeManager.void_type) {
7715 Report.Error (590, Location, "User-defined operators cannot return void");
7719 Type declaring_type = MethodData.DeclaringType;
7720 Type return_type = MemberType;
7721 Type first_arg_type = ParameterTypes [0];
7723 Type first_arg_type_unwrap = first_arg_type;
7724 if (TypeManager.IsNullableType (first_arg_type))
7725 first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
7727 Type return_type_unwrap = return_type;
7728 if (TypeManager.IsNullableType (return_type))
7729 return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
7732 // Rules for conversion operators
7734 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7735 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type){
7736 Report.Error (555, Location,
7737 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
7742 if (TypeManager.IsEqual (declaring_type, return_type) || declaring_type == return_type_unwrap) {
7743 conv_type = first_arg_type;
7744 } else if (TypeManager.IsEqual (declaring_type, first_arg_type) || declaring_type == first_arg_type_unwrap) {
7745 conv_type = return_type;
7747 Report.Error (556, Location,
7748 "User-defined conversion must convert to or from the enclosing type");
7753 // Because IsInterface and IsClass are not supported
7755 if (!TypeManager.IsGenericParameter (conv_type)) {
7756 if (conv_type.IsInterface) {
7757 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
7758 GetSignatureForError ());
7762 if (conv_type.IsClass) {
7763 if (TypeManager.IsSubclassOf (declaring_type, conv_type)) {
7764 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
7765 GetSignatureForError ());
7769 if (TypeManager.IsSubclassOf (conv_type, declaring_type)) {
7770 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
7771 GetSignatureForError ());
7776 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7777 if (first_arg_type != declaring_type || Parameters.Types [1] != TypeManager.int32_type) {
7778 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");
7781 } else if (Parameters.Count == 1) {
7782 // Checks for Unary operators
7784 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7785 if (return_type != declaring_type && !TypeManager.IsSubclassOf (return_type, declaring_type)) {
7786 Report.Error (448, Location,
7787 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7790 if (first_arg_type != declaring_type) {
7792 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7797 if (!TypeManager.IsEqual (first_arg_type_unwrap, declaring_type)){
7798 Report.Error (562, Location,
7799 "The parameter type of a unary operator must be the containing type");
7803 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7804 if (return_type != TypeManager.bool_type){
7807 "The return type of operator True or False " +
7814 // Checks for Binary operators
7816 if (first_arg_type != declaring_type &&
7817 Parameters.Types [1] != declaring_type){
7820 "One of the parameters of a binary operator must " +
7821 "be the containing type");
7829 protected override bool DoDefine ()
7831 if (!base.DoDefine ())
7834 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7838 public override void Emit ()
7842 Parameters.ApplyAttributes (MethodBuilder);
7845 // abstract or extern methods have no bodies
7847 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7851 if ((flags & MethodAttributes.PinvokeImpl) == 0)
7852 ec = CreateEmitContext (Parent, MethodBuilder.GetILGenerator ());
7854 ec = CreateEmitContext (Parent, null);
7856 SourceMethod source = SourceMethod.Create (Parent, MethodBuilder, Block);
7857 ec.EmitTopBlock (this, Block);
7860 source.CloseMethod ();
7865 // Operator cannot be override
7866 protected override MethodInfo FindOutBaseMethod (ref Type base_ret_type)
7871 public static string GetName (OpType ot)
7873 return names [(int) ot] [0];
7876 public static string GetName (string metadata_name)
7878 for (int i = 0; i < names.Length; ++i) {
7879 if (names [i] [1] == metadata_name)
7880 return names [i] [0];
7885 public static string GetMetadataName (OpType ot)
7887 return names [(int) ot] [1];
7890 public static string GetMetadataName (string name)
7892 for (int i = 0; i < names.Length; ++i) {
7893 if (names [i] [0] == name)
7894 return names [i] [1];
7899 public OpType GetMatchingOperator ()
7901 switch (OperatorType) {
7902 case OpType.Equality:
7903 return OpType.Inequality;
7904 case OpType.Inequality:
7905 return OpType.Equality;
7907 return OpType.False;
7910 case OpType.GreaterThan:
7911 return OpType.LessThan;
7912 case OpType.LessThan:
7913 return OpType.GreaterThan;
7914 case OpType.GreaterThanOrEqual:
7915 return OpType.LessThanOrEqual;
7916 case OpType.LessThanOrEqual:
7917 return OpType.GreaterThanOrEqual;
7923 public override string GetSignatureForError ()
7925 StringBuilder sb = new StringBuilder ();
7926 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7927 sb.AppendFormat ("{0}.{1} operator {2}",
7928 Parent.GetSignatureForError (), GetName (OperatorType), type_name.GetSignatureForError ());
7931 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
7934 sb.Append (Parameters.GetSignatureForError ());
7935 return sb.ToString ();
7940 // This is used to compare method signatures
7942 struct MethodSignature {
7944 public Type RetType;
7945 public Type [] Parameters;
7948 /// This delegate is used to extract methods which have the
7949 /// same signature as the argument
7951 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7953 public MethodSignature (string name, Type ret_type, Type [] parameters)
7958 if (parameters == null)
7959 Parameters = Type.EmptyTypes;
7961 Parameters = parameters;
7964 public override string ToString ()
7967 if (Parameters.Length != 0){
7968 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7969 for (int i = 0; i < Parameters.Length; i++){
7970 sb.Append (Parameters [i]);
7971 if (i+1 < Parameters.Length)
7974 pars = sb.ToString ();
7977 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7980 public override int GetHashCode ()
7982 return Name.GetHashCode ();
7985 public override bool Equals (Object o)
7987 MethodSignature other = (MethodSignature) o;
7989 if (other.Name != Name)
7992 if (other.RetType != RetType)
7995 if (Parameters == null){
7996 if (other.Parameters == null)
8001 if (other.Parameters == null)
8004 int c = Parameters.Length;
8005 if (other.Parameters.Length != c)
8008 for (int i = 0; i < c; i++)
8009 if (other.Parameters [i] != Parameters [i])
8015 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
8017 MethodSignature sig = (MethodSignature) filter_criteria;
8019 if (m.Name != sig.Name)
8023 MethodInfo mi = m as MethodInfo;
8024 PropertyInfo pi = m as PropertyInfo;
8027 ReturnType = mi.ReturnType;
8028 else if (pi != null)
8029 ReturnType = pi.PropertyType;
8034 // we use sig.RetType == null to mean `do not check the
8035 // method return value.
8037 if (sig.RetType != null) {
8038 if (!TypeManager.IsEqual (ReturnType, sig.RetType))
8044 args = TypeManager.GetParameterData (mi).Types;
8046 args = TypeManager.GetParameterData (pi).Types;
8047 Type [] sigp = sig.Parameters;
8049 if (args.Length != sigp.Length)
8052 for (int i = args.Length - 1; i >= 0; i--)
8053 if (!TypeManager.IsEqual (args [i], sigp [i]))