2 // class.cs: Class and Struct handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@gnome.org)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 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.
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.Reflection;
39 using System.Reflection.Emit;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42 using System.Security;
43 using System.Security.Permissions;
46 using Mono.CompilerServices.SymbolWriter;
48 namespace Mono.CSharp {
58 /// This is the base class for structs and classes.
60 public abstract class TypeContainer : DeclSpace, IMemberContainer {
62 public class MemberCoreArrayList: ArrayList
65 /// Defines the MemberCore objects that are in this array
67 public virtual void DefineContainerMembers ()
69 foreach (MemberCore mc in this) {
74 public virtual void Emit ()
76 foreach (MemberCore mc in this)
81 public class MethodArrayList: MemberCoreArrayList
89 CachedMethods cached_method;
90 TypeContainer container;
92 public MethodArrayList (TypeContainer container)
94 this.container = container;
98 /// Method container contains Equals method
100 public bool HasEquals {
102 cached_method |= CachedMethods.Equals;
106 return (cached_method & CachedMethods.Equals) != 0;
111 /// Method container contains GetHashCode method
113 public bool HasGetHashCode {
115 cached_method |= CachedMethods.GetHashCode;
119 return (cached_method & CachedMethods.GetHashCode) != 0;
123 public override void DefineContainerMembers ()
125 base.DefineContainerMembers ();
127 if ((RootContext.WarningLevel >= 3) && HasEquals && !HasGetHashCode) {
128 Report.Warning (659, container.Location, "'{0}' overrides Object.Equals(object) but does not override Object.GetHashCode()", container.GetSignatureForError ());
134 public sealed class IndexerArrayList: MemberCoreArrayList
137 /// The indexer name for this container
139 public string IndexerName = DefaultIndexerName;
141 bool seen_normal_indexers = false;
143 TypeContainer container;
145 public IndexerArrayList (TypeContainer container)
147 this.container = container;
151 /// Defines the indexers, and also verifies that the IndexerNameAttribute in the
152 /// class is consistent. Either it is `Item' or it is the name defined by all the
153 /// indexers with the `IndexerName' attribute.
155 /// Turns out that the IndexerNameAttribute is applied to each indexer,
156 /// but it is never emitted, instead a DefaultMember attribute is attached
159 public override void DefineContainerMembers()
161 base.DefineContainerMembers ();
163 string class_indexer_name = null;
166 // If there's both an explicit and an implicit interface implementation, the
167 // explicit one actually implements the interface while the other one is just
168 // a normal indexer. See bug #37714.
171 // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
172 foreach (Indexer i in this) {
173 if (i.InterfaceType != null) {
174 if (seen_normal_indexers)
175 throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
179 seen_normal_indexers = true;
181 if (class_indexer_name == null) {
182 class_indexer_name = i.ShortName;
186 if (i.ShortName != class_indexer_name)
187 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");
190 if (class_indexer_name != null)
191 IndexerName = class_indexer_name;
194 public override void Emit ()
198 if (!seen_normal_indexers)
201 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.default_member_ctor, new string [] { IndexerName });
202 container.TypeBuilder.SetCustomAttribute (cb);
206 public class OperatorArrayList: MemberCoreArrayList
208 TypeContainer container;
210 public OperatorArrayList (TypeContainer container)
212 this.container = container;
216 // Operator pair checking
221 public Type ret_type;
222 public Type type1, type2;
224 public Operator.OpType ot;
226 public OperatorEntry (int f, Operator o)
230 ret_type = o.OperatorMethod.ReturnType;
231 Type [] pt = o.OperatorMethod.ParameterTypes;
238 public override int GetHashCode ()
240 return ret_type.GetHashCode ();
243 public override bool Equals (object o)
245 OperatorEntry other = (OperatorEntry) o;
247 if (other.ret_type != ret_type)
249 if (other.type1 != type1)
251 if (other.type2 != type2)
258 // Checks that some operators come in pairs:
264 // They are matched based on the return type and the argument types
266 void CheckPairedOperators ()
268 Hashtable pairs = new Hashtable (null, null);
269 Operator true_op = null;
270 Operator false_op = null;
271 bool has_equality_or_inequality = false;
273 // Register all the operators we care about.
274 foreach (Operator op in this){
277 switch (op.OperatorType){
278 case Operator.OpType.Equality:
280 has_equality_or_inequality = true;
282 case Operator.OpType.Inequality:
284 has_equality_or_inequality = true;
287 case Operator.OpType.True:
290 case Operator.OpType.False:
294 case Operator.OpType.GreaterThan:
296 case Operator.OpType.LessThan:
299 case Operator.OpType.GreaterThanOrEqual:
301 case Operator.OpType.LessThanOrEqual:
307 OperatorEntry oe = new OperatorEntry (reg, op);
309 object o = pairs [oe];
313 oe = (OperatorEntry) o;
318 if (true_op != null){
319 if (false_op == null)
320 Report.Error (216, true_op.Location, "operator true requires a matching operator false");
321 } else if (false_op != null)
322 Report.Error (216, false_op.Location, "operator false requires a matching operator true");
325 // Look for the mistakes.
327 foreach (DictionaryEntry de in pairs){
328 OperatorEntry oe = (OperatorEntry) de.Key;
335 case Operator.OpType.Equality:
338 case Operator.OpType.Inequality:
341 case Operator.OpType.GreaterThan:
344 case Operator.OpType.LessThan:
347 case Operator.OpType.GreaterThanOrEqual:
350 case Operator.OpType.LessThanOrEqual:
354 Report.Error (216, oe.op.Location,
355 "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
358 if (has_equality_or_inequality && (RootContext.WarningLevel > 2)) {
359 if (container.Methods == null || !container.Methods.HasEquals)
360 Report.Warning (660, container.Location, "'{0}' defines operator == or operator != but does not override Object.Equals(object o)", container.GetSignatureForError ());
362 if (container.Methods == null || !container.Methods.HasGetHashCode)
363 Report.Warning (661, container.Location, "'{0}' defines operator == or operator != but does not override Object.GetHashCode()", container.GetSignatureForError ());
367 public override void DefineContainerMembers ()
369 base.DefineContainerMembers ();
370 CheckPairedOperators ();
375 // Whether this is a struct, class or interface
376 public readonly Kind Kind;
378 // Holds a list of classes and structures
381 // Holds the list of properties
382 MemberCoreArrayList properties;
384 // Holds the list of enumerations
385 MemberCoreArrayList enums;
387 // Holds the list of delegates
388 MemberCoreArrayList delegates;
390 // Holds the list of constructors
391 protected MemberCoreArrayList instance_constructors;
393 // Holds the list of fields
394 MemberCoreArrayList fields;
396 // Holds a list of fields that have initializers
397 protected ArrayList initialized_fields;
399 // Holds a list of static fields that have initializers
400 protected ArrayList initialized_static_fields;
402 // Holds the list of constants
403 MemberCoreArrayList constants;
406 MemberCoreArrayList interfaces;
408 // Holds the methods.
409 MethodArrayList methods;
412 protected MemberCoreArrayList events;
414 // Holds the indexers
415 IndexerArrayList indexers;
417 // Holds the operators
418 MemberCoreArrayList operators;
420 // Holds the iterators
423 // Holds the parts of a partial class;
427 // Pointers to the default constructor and the default static constructor
429 protected Constructor default_constructor;
430 protected Constructor default_static_constructor;
433 // Whether we have at least one non-static field
435 bool have_nonstatic_fields = false;
438 // This one is computed after we can distinguish interfaces
439 // from classes from the arraylist `type_bases'
441 string base_class_name;
442 TypeExpr parent_type;
444 ArrayList type_bases;
446 bool members_defined;
447 bool members_defined_ok;
449 // The interfaces we implement.
450 protected Type[] ifaces;
451 protected Type ptype;
453 // The parent member cache and our member cache
454 MemberCache parent_cache;
455 MemberCache member_cache;
457 public const string DefaultIndexerName = "Item";
461 public TypeContainer (NamespaceEntry ns, TypeContainer parent, MemberName name,
462 Attributes attrs, Kind kind, Location l)
463 : base (ns, parent, name, attrs, l)
467 types = new ArrayList ();
469 base_class_name = null;
472 public bool AddToMemberContainer (MemberCore symbol, bool is_method)
474 return AddToContainer (symbol, is_method, String.Concat (Name, '.', symbol.Name), symbol.Name);
477 bool AddToTypeContainer (DeclSpace ds)
479 return AddToContainer (ds, false, ds.Name, ds.Basename);
482 public void AddConstant (Const constant)
484 if (!AddToMemberContainer (constant, false))
487 if (constants == null)
488 constants = new MemberCoreArrayList ();
490 constants.Add (constant);
493 public void AddEnum (Mono.CSharp.Enum e)
495 if (!AddToTypeContainer (e))
499 enums = new MemberCoreArrayList ();
504 public void AddClassOrStruct (TypeContainer c)
506 if (!AddToTypeContainer (c))
512 public void AddDelegate (Delegate d)
514 if (!AddToTypeContainer (d))
517 if (delegates == null)
518 delegates = new MemberCoreArrayList ();
523 public void AddMethod (Method method)
525 if (!AddToMemberContainer (method, true))
529 methods = new MethodArrayList (this);
531 if (method.Name.IndexOf ('.') != -1)
532 methods.Insert (0, method);
534 methods.Add (method);
537 public void AddConstructor (Constructor c)
539 if (c.Name != Basename) {
540 Report.Error (1520, c.Location, "Class, struct, or interface method must have a return type");
543 bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
546 if (default_static_constructor != null) {
547 Report.SymbolRelatedToPreviousError (default_static_constructor);
548 Report.Error (111, c.Location, "Type '{0}' already defines a member " +
549 "called '{1}' with the same parameter types", Name, c.Name);
553 default_static_constructor = c;
556 if (default_constructor != null) {
557 Report.SymbolRelatedToPreviousError (default_constructor);
558 Report.Error (111, c.Location, "Type '{0}' already defines a member " +
559 "called '{1}' with the same parameter types", Name, c.Name);
562 default_constructor = c;
565 if (instance_constructors == null)
566 instance_constructors = new MemberCoreArrayList ();
568 instance_constructors.Add (c);
572 public void AddInterface (TypeContainer iface)
574 if (!AddToTypeContainer (iface))
577 if (interfaces == null) {
578 interfaces = new MemberCoreArrayList ();
581 interfaces.Add (iface);
584 public void AddField (Field field)
586 if (!AddToMemberContainer (field, false))
590 fields = new MemberCoreArrayList ();
594 if (field.HasInitializer){
595 if ((field.ModFlags & Modifiers.STATIC) != 0){
596 if (initialized_static_fields == null)
597 initialized_static_fields = new ArrayList ();
599 initialized_static_fields.Add (field);
602 if (initialized_fields == null)
603 initialized_fields = new ArrayList ();
605 initialized_fields.Add (field);
609 if ((field.ModFlags & Modifiers.STATIC) == 0)
610 have_nonstatic_fields = true;
613 public void AddProperty (Property prop)
615 if (!AddToMemberContainer (prop, false) ||
616 !AddToMemberContainer (prop.Get, true) || !AddToMemberContainer (prop.Set, true))
619 if (properties == null)
620 properties = new MemberCoreArrayList ();
622 if (prop.Name.IndexOf ('.') != -1)
623 properties.Insert (0, prop);
625 properties.Add (prop);
628 public void AddEvent (Event e)
630 if (!AddToMemberContainer (e, false))
633 if (e is EventProperty) {
634 if (!AddToMemberContainer (e.Add, true))
637 if (!AddToMemberContainer (e.Remove, true))
642 events = new MemberCoreArrayList ();
648 /// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
650 public void AddIndexer (Indexer i)
652 if (indexers == null)
653 indexers = new IndexerArrayList (this);
655 if (i.IsExplicitImpl)
656 indexers.Insert (0, i);
661 public void AddOperator (Operator op)
663 if (!AddToMemberContainer (op, true))
666 if (operators == null)
667 operators = new OperatorArrayList (this);
672 public void AddIterator (Iterator i)
674 if (iterators == null)
675 iterators = new ArrayList ();
680 public void AddType (TypeContainer tc)
685 public void AddPart (ClassPart part)
688 parts = new ArrayList ();
693 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
695 if (a.Type == TypeManager.default_member_type) {
696 if (Indexers != null) {
697 Report.Error (646, a.Location,
698 "Cannot specify the DefaultMember attribute on" +
699 " a type containing an indexer");
704 base.ApplyAttributeBuilder (a, cb);
707 public override AttributeTargets AttributeTargets {
709 throw new NotSupportedException ();
713 public ArrayList Types {
719 public MethodArrayList Methods {
725 public ArrayList Constants {
731 public ArrayList Interfaces {
737 public ArrayList Iterators {
745 return base_class_name;
749 public ArrayList Bases {
759 public ArrayList Fields {
765 public ArrayList InstanceConstructors {
767 return instance_constructors;
771 public ArrayList Properties {
777 public ArrayList Events {
783 public ArrayList Enums {
789 public ArrayList Indexers {
795 public ArrayList Operators {
801 public ArrayList Delegates {
807 public ArrayList Parts {
813 public virtual TypeAttributes TypeAttr {
815 return Modifiers.TypeAttr (ModFlags, this);
819 public string IndexerName {
821 return indexers == null ? DefaultIndexerName : indexers.IndexerName;
826 // Emits the instance field initializers
828 public bool EmitFieldInitializers (EmitContext ec)
831 Expression instance_expr;
834 fields = initialized_static_fields;
835 instance_expr = null;
837 fields = initialized_fields;
838 instance_expr = new This (Location.Null).Resolve (ec);
844 foreach (Field f in fields){
845 Expression e = f.GetInitializerExpression (ec);
849 Location l = f.Location;
850 FieldExpr fe = new FieldExpr (f.FieldBuilder, l);
851 fe.InstanceExpression = instance_expr;
852 fe.IsFieldInitializer = true;
854 ExpressionStatement a = new Assign (fe, e, l);
856 a = a.ResolveStatement (ec);
860 a.EmitStatement (ec);
867 // Defines the default constructors
869 protected void DefineDefaultConstructor (bool is_static)
873 // The default constructor is public
874 // If the class is abstract, the default constructor is protected
875 // The default static constructor is private
877 int mods = Modifiers.PUBLIC;
879 mods = Modifiers.STATIC | Modifiers.PRIVATE;
880 else if ((ModFlags & Modifiers.ABSTRACT) != 0)
881 mods = Modifiers.PROTECTED;
883 c = new Constructor (this, Basename, mods, Parameters.EmptyReadOnlyParameters,
884 new ConstructorBaseInitializer (
885 null, Parameters.EmptyReadOnlyParameters,
891 c.Block = new ToplevelBlock (null, Location);
896 /// The pending methods that need to be implemented
897 // (interfaces or abstract methods)
899 public PendingImplementation Pending;
901 public abstract void Register ();
903 public abstract PendingImplementation GetPendingImplementations ();
905 TypeExpr[] GetPartialBases (out TypeExpr parent, out bool error)
907 ArrayList ifaces = new ArrayList ();
910 Location parent_loc = Location.Null;
912 foreach (ClassPart part in parts) {
914 TypeExpr[] new_ifaces;
916 new_ifaces = part.GetClassBases (out new_parent, out error);
920 if ((parent != null) && (new_parent != null) &&
921 !parent.Equals (new_parent)) {
922 Report.Error (263, part.Location,
923 "Partial declarations of `{0}' must " +
924 "not specify different base classes",
927 if (!Location.IsNull (parent_loc))
928 Report.LocationOfPreviousError (parent_loc);
934 if ((parent == null) && (new_parent != null)) {
936 parent_loc = part.Location;
939 if (new_ifaces == null)
942 foreach (TypeExpr iface in new_ifaces) {
944 foreach (TypeExpr old_iface in ifaces) {
945 if (old_iface.Equals (iface)) {
958 TypeExpr[] retval = new TypeExpr [ifaces.Count];
959 ifaces.CopyTo (retval, 0);
963 TypeExpr[] GetNormalBases (out TypeExpr parent, out bool error)
967 int count = Bases.Count;
970 if (Kind == Kind.Class){
971 TypeExpr name = ResolveTypeExpr ((Expression) Bases [0], Location);
988 TypeExpr [] ifaces = new TypeExpr [count-start];
990 for (i = start, j = 0; i < count; i++, j++){
991 Expression name = (Expression) Bases [i];
992 TypeExpr resolved = ResolveTypeExpr (name, Location);
993 if (resolved == null) {
998 ifaces [j] = resolved;
1006 /// This function computes the Base class and also the
1007 /// list of interfaces that the class or struct @c implements.
1009 /// The return value is an array (might be null) of
1010 /// interfaces implemented (as Types).
1012 /// The @parent argument is set to the parent object or null
1013 /// if this is `System.Object'.
1015 TypeExpr [] GetClassBases (out TypeExpr parent, out bool error)
1024 ifaces = GetPartialBases (out parent, out error);
1025 else if (Bases == null){
1029 ifaces = GetNormalBases (out parent, out error);
1034 if ((parent != null) && (Kind == Kind.Class)){
1035 if (parent is TypeParameterExpr){
1037 689, parent.Location,
1038 "Type parameter `{0}' can not be used as a " +
1039 "base class or interface", parent.Name);
1044 if (IsGeneric && parent.IsAttribute){
1046 698, parent.Location,
1047 "A generic type cannot derive from `{0}' " +
1048 "because it is an attribute class",
1054 if (parent.IsSealed){
1056 Report.SymbolRelatedToPreviousError (parent.Type);
1057 if (parent.Type.IsAbstract) {
1058 Report.Error (709, Location, "'{0}': Cannot derive from static class", GetSignatureForError ());
1060 Report.Error (509, Location, "'{0}': Cannot derive from sealed class", GetSignatureForError ());
1065 if (!parent.CanInheritFrom ()){
1066 Report.Error (644, Location,
1067 "`{0}' cannot inherit from special class `{1}'",
1068 Name, parent_type.Name);
1073 if (!parent.AsAccessible (this, ModFlags)) {
1074 Report.SymbolRelatedToPreviousError (parent.Type);
1075 Report.Error (60, Location, "Inconsistent accessibility: base class '{0}' is less accessible than class '{1}'",
1076 TypeManager.CSharpName (parent.Type), GetSignatureForError ());
1081 base_class_name = parent.Name;
1086 int count = ifaces != null ? ifaces.Length : 0;
1088 for (i = 0; i < count; i++) {
1089 TypeExpr iface = (TypeExpr) ifaces [i];
1091 if ((Kind != Kind.Class) && !iface.IsInterface){
1092 string what = Kind == Kind.Struct ?
1093 "Struct" : "Interface";
1095 Report.Error (527, Location,
1096 "In {0} `{1}', type `{2}' is not "+
1097 "an interface", what, Name, iface.Name);
1102 if (iface.IsClass) {
1103 Report.Error (1722, Location,
1104 "In Class `{0}', `{1}' is not " +
1105 "an interface, a base class must be listed first", Name, iface.Name);
1110 for (int x = 0; x < i; x++) {
1111 if (iface.Equals (ifaces [x])) {
1112 Report.Error (528, Location,
1113 "`{0}' is already listed in " +
1114 "interface list", iface.Name);
1120 if ((Kind == Kind.Interface) &&
1121 !iface.AsAccessible (Parent, ModFlags))
1122 Report.Error (61, Location,
1123 "Inconsistent accessibility: base " +
1124 "interface `{0}' is less accessible " +
1125 "than interface `{1}'", iface.Name,
1132 bool CheckGenericInterfaces (Type[] ifaces)
1134 ArrayList already_checked = new ArrayList ();
1136 for (int i = 0; i < ifaces.Length; i++) {
1137 Type iface = ifaces [i];
1138 foreach (Type t in already_checked) {
1142 if (!TypeManager.MayBecomeEqualGenericInstances (iface, t))
1147 "`{0}' cannot implement both `{1}' and `{2}' " +
1148 "because they may unify for some type " +
1149 "parameter substitutions",
1150 TypeManager.GetFullName (TypeBuilder),
1155 already_checked.Add (iface);
1164 // Defines the type in the appropriate ModuleBuilder or TypeBuilder.
1166 public override TypeBuilder DefineType ()
1173 if (TypeBuilder != null)
1176 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
1178 TypeAttributes type_attributes = TypeAttr;
1182 if (TypeManager.NamespaceClash (Name, Location)) {
1187 ModuleBuilder builder = CodeGen.Module.Builder;
1188 TypeBuilder = builder.DefineType (
1189 Name, type_attributes, null, null);
1191 TypeBuilder builder;
1192 if (Parent.TypeBuilder != null)
1193 builder = Parent.TypeBuilder;
1195 builder = Parent.DefineType ();
1197 if (builder == null) {
1202 TypeBuilder = builder.DefineNestedType (
1203 MemberName.Basename, type_attributes,
1207 catch (ArgumentException) {
1208 Report.RuntimeMissingSupport (Location, "static classes");
1212 TypeManager.AddUserType (Name, TypeBuilder, this);
1214 TypeExpr current_type = null;
1217 string[] param_names = new string [TypeParameters.Length];
1218 for (int i = 0; i < TypeParameters.Length; i++)
1219 param_names [i] = TypeParameters [i].Name;
1221 GenericTypeParameterBuilder[] gen_params;
1222 gen_params = TypeBuilder.DefineGenericParameters (param_names);
1224 int offset = CountTypeParameters - CurrentTypeParameters.Length;
1225 for (int i = offset; i < gen_params.Length; i++)
1226 CurrentTypeParameters [i - offset].Define (gen_params [i]);
1228 foreach (TypeParameter type_param in CurrentTypeParameters) {
1229 if (!type_param.Resolve (this)) {
1235 for (int i = offset; i < gen_params.Length; i++)
1236 CurrentTypeParameters [i - offset].DefineConstraints ();
1238 current_type = new ConstructedType (Name, TypeParameters, Location);
1242 foreach (TypeParameter type_param in TypeParameters)
1243 if (!type_param.DefineType (ec)) {
1249 if ((Kind == Kind.Struct) && TypeManager.value_type == null)
1250 throw new Exception ();
1252 TypeExpr[] iface_exprs = GetClassBases (out parent_type, out error);
1256 if (parent_type == null) {
1257 if (Kind == Kind.Class){
1258 if (RootContext.StdLib)
1259 parent_type = TypeManager.system_object_expr;
1260 else if (Name != "System.Object")
1261 parent_type = TypeManager.system_object_expr;
1262 } else if (Kind == Kind.Struct){
1264 // If we are compiling our runtime,
1265 // and we are defining ValueType, then our
1266 // parent is `System.Object'.
1268 if (!RootContext.StdLib && Name == "System.ValueType")
1269 parent_type = TypeManager.system_object_expr;
1270 else if (Kind == Kind.Struct)
1271 parent_type = TypeManager.system_valuetype_expr;
1275 if (parent_type != null) {
1276 parent_type = parent_type.ResolveAsTypeTerminal (ec);
1277 if (parent_type == null) {
1282 ptype = parent_type.Type;
1285 if (!CheckRecursiveDefinition ()) {
1291 TypeBuilder.SetParent (ptype);
1294 // Structs with no fields need to have at least one byte.
1295 // The right thing would be to set the PackingSize in a DefineType
1296 // but there are no functions that allow interfaces *and* the size to
1300 if ((Kind == Kind.Struct) && !have_nonstatic_fields){
1301 TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
1302 FieldAttributes.Private);
1305 // add interfaces that were not added at type creation
1306 if (iface_exprs != null) {
1307 ifaces = TypeManager.ExpandInterfaces (ec, iface_exprs);
1308 if (ifaces == null) {
1313 foreach (Type itype in ifaces)
1314 TypeBuilder.AddInterfaceImplementation (itype);
1316 if (!CheckGenericInterfaces (ifaces)) {
1321 TypeManager.RegisterBuilder (TypeBuilder, ifaces);
1325 foreach (TypeParameter type_param in TypeParameters)
1326 if (!type_param.CheckDependencies (ec)) {
1332 if (current_type != null) {
1333 current_type = current_type.ResolveAsTypeTerminal (ec);
1334 if (current_type == null) {
1339 CurrentType = current_type.Type;
1343 // Finish the setup for the EmitContext
1345 ec.ContainerType = TypeBuilder;
1347 if ((parent_type != null) && parent_type.IsAttribute) {
1348 RootContext.RegisterAttribute (this);
1349 } else if (!(this is Iterator))
1350 RootContext.RegisterOrder (this);
1352 if (!DefineNestedTypes ()) {
1360 protected virtual bool DefineNestedTypes ()
1362 if (Interfaces != null) {
1363 foreach (TypeContainer iface in Interfaces)
1364 if (iface.DefineType () == null)
1368 if (Types != null) {
1369 foreach (TypeContainer tc in Types)
1370 if (tc.DefineType () == null)
1374 if (Delegates != null) {
1375 foreach (Delegate d in Delegates)
1376 if (d.DefineType () == null)
1380 if (Enums != null) {
1381 foreach (Enum en in Enums)
1382 if (en.DefineType () == null)
1386 if (Parts != null) {
1387 foreach (ClassPart part in Parts) {
1388 part.TypeBuilder = TypeBuilder;
1389 part.parent_type = parent_type;
1390 part.ec = new EmitContext (part, Mono.CSharp.Location.Null, null, null, ModFlags);
1397 protected bool CheckRecursiveDefinition ()
1400 Report.Error (146, Location,
1401 "Class definition is circular: `{0}'",
1402 GetSignatureForError ());
1409 Type parent = ptype;
1410 if (parent != null) {
1411 if (parent.IsGenericInstance)
1412 parent = parent.GetGenericTypeDefinition ();
1414 TypeContainer ptc = TypeManager.LookupTypeContainer (parent);
1415 if ((ptc != null) && !ptc.CheckRecursiveDefinition ())
1423 static void Error_KeywordNotAllowed (Location loc)
1425 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
1429 /// Populates our TypeBuilder with fields and methods
1431 public override bool DefineMembers (TypeContainer container)
1433 if (members_defined)
1434 return members_defined_ok;
1436 members_defined_ok = DoDefineMembers ();
1437 members_defined = true;
1439 return members_defined_ok;
1442 protected virtual bool DoDefineMembers ()
1445 // We need to be able to use the member cache while we are checking/defining
1447 if (TypeBuilder.BaseType != null)
1448 parent_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType);
1450 if (TypeBuilder.IsInterface)
1451 parent_cache = TypeManager.LookupParentInterfacesCache (TypeBuilder);
1454 if ((ModFlags & Modifiers.NEW) != 0)
1455 Error_KeywordNotAllowed (Location);
1457 // HACK: missing implemenation
1458 // This is not fully functional. Better way how to handle this is to have recursive definition of containers
1459 // instead of flat as we have now.
1460 // Now we are not able to check inner attribute class because its parent had not been defined.
1462 // TODO: remove this if
1463 if (Parent.MemberCache != null) {
1464 MemberInfo conflict_symbol = Parent.MemberCache.FindMemberWithSameName (Basename, false, TypeBuilder);
1465 if (conflict_symbol == null) {
1466 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0))
1467 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
1469 if ((ModFlags & Modifiers.NEW) == 0) {
1470 Report.SymbolRelatedToPreviousError (conflict_symbol);
1471 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError ());
1477 DefineContainerMembers (constants);
1478 DefineContainerMembers (fields);
1480 if ((Kind == Kind.Class) && !(this is ClassPart) && !(this is StaticClass)){
1481 if ((instance_constructors == null) &&
1482 !(this is StaticClass)) {
1483 if (default_constructor == null)
1484 DefineDefaultConstructor (false);
1487 if (initialized_static_fields != null &&
1488 default_static_constructor == null)
1489 DefineDefaultConstructor (true);
1492 if (Kind == Kind.Struct){
1494 // Structs can not have initialized instance
1497 if (initialized_static_fields != null &&
1498 default_static_constructor == null)
1499 DefineDefaultConstructor (true);
1501 if (initialized_fields != null)
1502 ReportStructInitializedInstanceError ();
1505 Pending = GetPendingImplementations ();
1507 if (parts != null) {
1508 foreach (ClassPart part in parts) {
1509 if (!part.DefineMembers (this))
1515 // Constructors are not in the defined_names array
1517 DefineContainerMembers (instance_constructors);
1519 if (default_static_constructor != null)
1520 default_static_constructor.Define ();
1522 DefineContainerMembers (properties);
1523 DefineContainerMembers (events);
1524 DefineContainerMembers (indexers);
1525 DefineContainerMembers (methods);
1526 DefineContainerMembers (operators);
1527 DefineContainerMembers (enums);
1528 DefineContainerMembers (delegates);
1530 if (CurrentType != null) {
1531 GenericType = CurrentType;
1533 ec.ContainerType = GenericType;
1538 if (!(this is ClassPart))
1539 member_cache = new MemberCache (this);
1542 if (parts != null) {
1543 foreach (ClassPart part in parts)
1544 part.member_cache = member_cache;
1547 if (iterators != null) {
1548 foreach (Iterator iterator in iterators) {
1549 if (iterator.DefineType () == null)
1553 foreach (Iterator iterator in iterators) {
1554 if (!iterator.DefineMembers (this))
1562 void ReportStructInitializedInstanceError ()
1564 string n = TypeBuilder.FullName;
1566 foreach (Field f in initialized_fields){
1569 "`" + n + "." + f.Name + "': can not have " +
1570 "instance field initializers in structs");
1574 protected virtual void DefineContainerMembers (MemberCoreArrayList mcal)
1577 mcal.DefineContainerMembers ();
1580 public override bool Define ()
1582 if (parts != null) {
1583 foreach (ClassPart part in parts) {
1584 if (!part.Define ())
1592 public MemberInfo FindMemberWithSameName (string name, bool ignore_methods)
1594 return ParentCache.FindMemberWithSameName (name, ignore_methods, null);
1598 /// This function is based by a delegate to the FindMembers routine
1600 static bool AlwaysAccept (MemberInfo m, object filterCriteria)
1606 /// This filter is used by FindMembers, and we just keep
1607 /// a global for the filter to `AlwaysAccept'
1609 static MemberFilter accepting_filter;
1612 static TypeContainer ()
1614 accepting_filter = new MemberFilter (AlwaysAccept);
1617 public MethodInfo[] GetMethods ()
1619 ArrayList members = new ArrayList ();
1621 DefineMembers (null);
1623 if (methods != null) {
1624 int len = methods.Count;
1625 for (int i = 0; i < len; i++) {
1626 Method m = (Method) methods [i];
1628 members.Add (m.MethodBuilder);
1632 if (operators != null) {
1633 int len = operators.Count;
1634 for (int i = 0; i < len; i++) {
1635 Operator o = (Operator) operators [i];
1637 members.Add (o.OperatorMethodBuilder);
1641 if (properties != null) {
1642 int len = properties.Count;
1643 for (int i = 0; i < len; i++) {
1644 Property p = (Property) properties [i];
1646 if (p.GetBuilder != null)
1647 members.Add (p.GetBuilder);
1648 if (p.SetBuilder != null)
1649 members.Add (p.SetBuilder);
1653 if (indexers != null) {
1654 int len = indexers.Count;
1655 for (int i = 0; i < len; i++) {
1656 Indexer ix = (Indexer) indexers [i];
1658 if (ix.GetBuilder != null)
1659 members.Add (ix.GetBuilder);
1660 if (ix.SetBuilder != null)
1661 members.Add (ix.SetBuilder);
1665 if (events != null) {
1666 int len = events.Count;
1667 for (int i = 0; i < len; i++) {
1668 Event e = (Event) events [i];
1670 if (e.AddBuilder != null)
1671 members.Add (e.AddBuilder);
1672 if (e.RemoveBuilder != null)
1673 members.Add (e.RemoveBuilder);
1677 MethodInfo[] retMethods = new MethodInfo [members.Count];
1678 members.CopyTo (retMethods, 0);
1682 // Indicated whether container has StructLayout attribute set Explicit
1683 public virtual bool HasExplicitLayout {
1690 /// This method returns the members of this type just like Type.FindMembers would
1691 /// Only, we need to use this for types which are _being_ defined because MS'
1692 /// implementation can't take care of that.
1695 // FIXME: return an empty static array instead of null, that cleans up
1696 // some code and is consistent with some coding conventions I just found
1700 // Notice that in various cases we check if our field is non-null,
1701 // something that would normally mean that there was a bug elsewhere.
1703 // The problem happens while we are defining p-invoke methods, as those
1704 // will trigger a FindMembers, but this happens before things are defined
1706 // Since the whole process is a no-op, it is fine to check for null here.
1708 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1709 MemberFilter filter, object criteria)
1711 ArrayList members = null;
1714 if ((bf & BindingFlags.Public) != 0)
1715 modflags |= Modifiers.PUBLIC | Modifiers.PROTECTED |
1717 if ((bf & BindingFlags.NonPublic) != 0)
1718 modflags |= Modifiers.PRIVATE;
1720 int static_mask = 0, static_flags = 0;
1721 switch (bf & (BindingFlags.Static | BindingFlags.Instance)) {
1722 case BindingFlags.Static:
1723 static_mask = static_flags = Modifiers.STATIC;
1726 case BindingFlags.Instance:
1727 static_mask = Modifiers.STATIC;
1732 static_mask = static_flags = 0;
1736 Timer.StartTimer (TimerType.TcFindMembers);
1739 filter = accepting_filter;
1741 if ((mt & MemberTypes.Field) != 0) {
1742 if (fields != null) {
1743 int len = fields.Count;
1744 for (int i = 0; i < len; i++) {
1745 Field f = (Field) fields [i];
1747 if ((f.ModFlags & modflags) == 0)
1749 if ((f.ModFlags & static_mask) != static_flags)
1752 FieldBuilder fb = f.FieldBuilder;
1753 if (fb != null && filter (fb, criteria) == true) {
1754 if (members == null)
1755 members = new ArrayList ();
1762 if (constants != null) {
1763 int len = constants.Count;
1764 for (int i = 0; i < len; i++) {
1765 Const con = (Const) constants [i];
1767 if ((con.ModFlags & modflags) == 0)
1769 if ((con.ModFlags & static_mask) != static_flags)
1772 FieldBuilder fb = con.FieldBuilder;
1773 if (fb != null && filter (fb, criteria) == true) {
1774 if (members == null)
1775 members = new ArrayList ();
1783 if ((mt & MemberTypes.Method) != 0) {
1784 if (methods != null) {
1785 int len = methods.Count;
1786 for (int i = 0; i < len; i++) {
1787 Method m = (Method) methods [i];
1789 if ((m.ModFlags & modflags) == 0)
1791 if ((m.ModFlags & static_mask) != static_flags)
1794 MethodBuilder mb = m.MethodBuilder;
1796 if (mb != null && filter (mb, criteria) == true) {
1797 if (members == null)
1798 members = new ArrayList ();
1805 if (operators != null) {
1806 int len = operators.Count;
1807 for (int i = 0; i < len; i++) {
1808 Operator o = (Operator) operators [i];
1810 if ((o.ModFlags & modflags) == 0)
1812 if ((o.ModFlags & static_mask) != static_flags)
1815 MethodBuilder ob = o.OperatorMethodBuilder;
1816 if (ob != null && filter (ob, criteria) == true) {
1817 if (members == null)
1818 members = new ArrayList ();
1825 if (properties != null) {
1826 int len = properties.Count;
1827 for (int i = 0; i < len; i++) {
1828 Property p = (Property) properties [i];
1830 if ((p.ModFlags & modflags) == 0)
1832 if ((p.ModFlags & static_mask) != static_flags)
1838 if (b != null && filter (b, criteria) == true) {
1839 if (members == null)
1840 members = new ArrayList ();
1846 if (b != null && filter (b, criteria) == true) {
1847 if (members == null)
1848 members = new ArrayList ();
1855 if (indexers != null) {
1856 int len = indexers.Count;
1857 for (int i = 0; i < len; i++) {
1858 Indexer ix = (Indexer) indexers [i];
1860 if ((ix.ModFlags & modflags) == 0)
1862 if ((ix.ModFlags & static_mask) != static_flags)
1868 if (b != null && filter (b, criteria) == true) {
1869 if (members == null)
1870 members = new ArrayList ();
1876 if (b != null && filter (b, criteria) == true) {
1877 if (members == null)
1878 members = new ArrayList ();
1886 if ((mt & MemberTypes.Event) != 0) {
1887 if (events != null) {
1888 int len = events.Count;
1889 for (int i = 0; i < len; i++) {
1890 Event e = (Event) events [i];
1892 if ((e.ModFlags & modflags) == 0)
1894 if ((e.ModFlags & static_mask) != static_flags)
1897 MemberInfo eb = e.EventBuilder;
1898 if (eb != null && filter (eb, criteria) == true) {
1899 if (members == null)
1900 members = new ArrayList ();
1902 members.Add (e.EventBuilder);
1908 if ((mt & MemberTypes.Property) != 0){
1909 if (properties != null) {
1910 int len = properties.Count;
1911 for (int i = 0; i < len; i++) {
1912 Property p = (Property) properties [i];
1914 if ((p.ModFlags & modflags) == 0)
1916 if ((p.ModFlags & static_mask) != static_flags)
1919 MemberInfo pb = p.PropertyBuilder;
1920 if (pb != null && filter (pb, criteria) == true) {
1921 if (members == null)
1922 members = new ArrayList ();
1924 members.Add (p.PropertyBuilder);
1929 if (indexers != null) {
1930 int len = indexers.Count;
1931 for (int i = 0; i < len; i++) {
1932 Indexer ix = (Indexer) indexers [i];
1934 if ((ix.ModFlags & modflags) == 0)
1936 if ((ix.ModFlags & static_mask) != static_flags)
1939 MemberInfo ib = ix.PropertyBuilder;
1940 if (ib != null && filter (ib, criteria) == true) {
1941 if (members == null)
1942 members = new ArrayList ();
1944 members.Add (ix.PropertyBuilder);
1950 if ((mt & MemberTypes.NestedType) != 0) {
1951 if (types != null) {
1952 int len = types.Count;
1953 for (int i = 0; i < len; i++) {
1954 TypeContainer t = (TypeContainer) types [i];
1956 if ((t.ModFlags & modflags) == 0)
1959 TypeBuilder tb = t.TypeBuilder;
1960 if (tb != null && (filter (tb, criteria) == true)) {
1961 if (members == null)
1962 members = new ArrayList ();
1969 if (enums != null) {
1970 int len = enums.Count;
1971 for (int i = 0; i < len; i++) {
1972 Enum en = (Enum) enums [i];
1974 if ((en.ModFlags & modflags) == 0)
1977 TypeBuilder tb = en.TypeBuilder;
1978 if (tb != null && (filter (tb, criteria) == true)) {
1979 if (members == null)
1980 members = new ArrayList ();
1987 if (delegates != null) {
1988 int len = delegates.Count;
1989 for (int i = 0; i < len; i++) {
1990 Delegate d = (Delegate) delegates [i];
1992 if ((d.ModFlags & modflags) == 0)
1995 TypeBuilder tb = d.TypeBuilder;
1996 if (tb != null && (filter (tb, criteria) == true)) {
1997 if (members == null)
1998 members = new ArrayList ();
2005 if (interfaces != null) {
2006 int len = interfaces.Count;
2007 for (int i = 0; i < len; i++) {
2008 TypeContainer iface = (TypeContainer) interfaces [i];
2010 if ((iface.ModFlags & modflags) == 0)
2013 TypeBuilder tb = iface.TypeBuilder;
2014 if (tb != null && (filter (tb, criteria) == true)) {
2015 if (members == null)
2016 members = new ArrayList ();
2024 if ((mt & MemberTypes.Constructor) != 0){
2025 if (((bf & BindingFlags.Instance) != 0) && (instance_constructors != null)){
2026 int len = instance_constructors.Count;
2027 for (int i = 0; i < len; i++) {
2028 Constructor c = (Constructor) instance_constructors [i];
2030 ConstructorBuilder cb = c.ConstructorBuilder;
2031 if (cb != null && filter (cb, criteria) == true) {
2032 if (members == null)
2033 members = new ArrayList ();
2040 if (((bf & BindingFlags.Static) != 0) && (default_static_constructor != null)){
2041 ConstructorBuilder cb =
2042 default_static_constructor.ConstructorBuilder;
2044 if (cb != null && filter (cb, criteria) == true) {
2045 if (members == null)
2046 members = new ArrayList ();
2054 // Lookup members in parent if requested.
2056 if ((bf & BindingFlags.DeclaredOnly) == 0) {
2057 if (TypeBuilder.BaseType != null) {
2058 MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
2059 if (list.Count > 0) {
2060 if (members == null)
2061 members = new ArrayList ();
2063 members.AddRange (list);
2069 Timer.StopTimer (TimerType.TcFindMembers);
2071 if (members == null)
2072 return MemberList.Empty;
2074 return new MemberList (members);
2077 public override MemberCache MemberCache {
2079 return member_cache;
2083 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
2084 MemberFilter filter, object criteria)
2086 DeclSpace ds = TypeManager.LookupDeclSpace (t);
2089 return ds.FindMembers (mt, bf, filter, criteria);
2091 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
2095 // FindMethods will look for methods not only in the type `t', but in
2096 // any interfaces implemented by the type.
2098 public static MethodInfo [] FindMethods (Type t, BindingFlags bf,
2099 MemberFilter filter, object criteria)
2105 /// Emits the values for the constants
2107 public void EmitConstants ()
2109 if (constants != null)
2110 foreach (Const con in constants)
2115 protected virtual void VerifyMembers (EmitContext ec)
2118 // Check for internal or private fields that were never assigned
2120 if (RootContext.WarningLevel >= 3) {
2121 if (fields != null){
2122 foreach (Field f in fields) {
2123 if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
2126 if ((f.status & Field.Status.USED) == 0){
2127 Report.Warning (169, f.Location, "The private field '{0}' is never used", f.GetSignatureForError ());
2132 // Only report 649 on level 4
2134 if (RootContext.WarningLevel < 4)
2137 if ((f.status & Field.Status.ASSIGNED) != 0)
2140 Report.Warning (649, f.Location, "Field '{0}' is never assigned to, and will always have its default value '{1}'", f.GetSignatureForError (), "");
2147 /// Emits the code, this step is performed after all
2148 /// the types, enumerations, constructors
2150 public void EmitType ()
2152 if (OptAttributes != null)
2153 OptAttributes.Emit (ec, this);
2157 if (instance_constructors != null) {
2158 if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) {
2159 bool has_compliant_args = false;
2161 foreach (Constructor c in instance_constructors) {
2164 if (has_compliant_args)
2167 has_compliant_args = c.HasCompliantArgs;
2169 if (!has_compliant_args)
2170 Report.Error (3015, Location, "'{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ());
2172 foreach (Constructor c in instance_constructors)
2179 if (default_static_constructor != null)
2180 default_static_constructor.Emit ();
2182 if (methods != null)
2183 foreach (Method m in methods)
2186 if (operators != null)
2187 foreach (Operator o in operators)
2190 if (properties != null)
2191 foreach (Property p in properties)
2194 if (indexers != null){
2199 foreach (Field f in fields)
2202 if (events != null){
2203 foreach (Event e in Events)
2207 if (delegates != null) {
2208 foreach (Delegate d in Delegates) {
2213 if (enums != null) {
2214 foreach (Enum e in enums) {
2219 if (parts != null) {
2220 foreach (ClassPart part in parts)
2224 if ((Pending != null) && !(this is ClassPart))
2225 if (Pending.VerifyPendingMethods ())
2230 if (iterators != null)
2231 foreach (Iterator iterator in iterators)
2232 iterator.EmitType ();
2234 // if (types != null)
2235 // foreach (TypeContainer tc in types)
2239 public override void CloseType ()
2241 if ((caching_flags & Flags.CloseTypeCreated) != 0)
2245 caching_flags |= Flags.CloseTypeCreated;
2246 TypeBuilder.CreateType ();
2247 } catch (TypeLoadException){
2249 // This is fine, the code still created the type
2251 // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);
2252 // Console.WriteLine (e.Message);
2254 Console.WriteLine ("In type: " + Name);
2259 foreach (Enum en in Enums)
2263 foreach (TypeContainer tc in Types)
2264 if (tc.Kind == Kind.Struct)
2267 foreach (TypeContainer tc in Types)
2268 if (tc.Kind != Kind.Struct)
2272 if (Delegates != null)
2273 foreach (Delegate d in Delegates)
2276 if (Iterators != null)
2277 foreach (Iterator i in Iterators)
2285 initialized_fields = null;
2286 initialized_static_fields = null;
2295 default_constructor = null;
2296 default_static_constructor = null;
2298 OptAttributes = null;
2300 parent_cache = null;
2301 member_cache = null;
2304 // TODO: make it obsolete and use GetSignatureForError
2305 public string MakeName (string n)
2307 return "`" + Name + "." + n + "'";
2311 // Performs the validation on a Method's modifiers (properties have
2312 // the same properties).
2314 public bool MethodModifiersValid (int flags, string n, Location loc)
2316 const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE);
2317 const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT);
2318 const int nv = (Modifiers.NEW | Modifiers.VIRTUAL);
2322 // At most one of static, virtual or override
2324 if ((flags & Modifiers.STATIC) != 0){
2325 if ((flags & vao) != 0){
2327 112, loc, "static method " + MakeName (n) + "can not be marked " +
2328 "as virtual, abstract or override");
2333 if (Kind == Kind.Struct){
2334 if ((flags & va) != 0){
2335 Modifiers.Error_InvalidModifier (loc, "virtual or abstract");
2340 if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){
2342 113, loc, MakeName (n) +
2343 " marked as override cannot be marked as new or virtual");
2348 // If the declaration includes the abstract modifier, then the
2349 // declaration does not include static, virtual or extern
2351 if ((flags & Modifiers.ABSTRACT) != 0){
2352 if ((flags & Modifiers.EXTERN) != 0){
2354 180, loc, MakeName (n) + " can not be both abstract and extern");
2358 if ((flags & Modifiers.VIRTUAL) != 0){
2360 503, loc, MakeName (n) + " can not be both abstract and virtual");
2364 if ((ModFlags & Modifiers.ABSTRACT) == 0){
2366 513, loc, MakeName (n) +
2367 " is abstract but its container class is not");
2373 if ((flags & Modifiers.PRIVATE) != 0){
2374 if ((flags & vao) != 0){
2376 621, loc, MakeName (n) +
2377 " virtual or abstract members can not be private");
2382 if ((flags & Modifiers.SEALED) != 0){
2383 if ((flags & Modifiers.OVERRIDE) == 0){
2385 238, loc, MakeName (n) +
2386 " cannot be sealed because it is not an override");
2394 public bool UserDefinedStaticConstructor {
2396 return default_static_constructor != null;
2400 public Constructor DefaultStaticConstructor {
2401 get { return default_static_constructor; }
2404 protected override bool VerifyClsCompliance (DeclSpace ds)
2406 if (!base.VerifyClsCompliance (ds))
2411 Type base_type = TypeBuilder.BaseType;
2412 if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) {
2413 Report.Error (3009, Location, "'{0}': base type '{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type));
2420 /// Checks whether container name is CLS Compliant
2422 void VerifyClsName ()
2424 Hashtable parent_members = parent_cache == null ?
2426 parent_cache.GetPublicMembers ();
2427 Hashtable this_members = new Hashtable ();
2429 foreach (DictionaryEntry entry in defined_names) {
2430 MemberCore mc = (MemberCore)entry.Value;
2431 if (!mc.IsClsCompliaceRequired (this))
2434 string name = (string)entry.Key;
2435 string basename = name.Substring (name.LastIndexOf ('.') + 1);
2437 string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture);
2438 object found = parent_members [lcase];
2439 if (found == null) {
2440 found = this_members [lcase];
2441 if (found == null) {
2442 this_members.Add (lcase, mc);
2447 if ((mc.ModFlags & Modifiers.OVERRIDE) != 0)
2450 if (found is MemberInfo) {
2451 if (basename == ((MemberInfo)found).Name)
2453 Report.SymbolRelatedToPreviousError ((MemberInfo)found);
2455 Report.SymbolRelatedToPreviousError ((MemberCore) found);
2457 Report.Error (3005, mc.Location, "Identifier '{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ());
2463 /// Performs checks for an explicit interface implementation. First it
2464 /// checks whether the `interface_type' is a base inteface implementation.
2465 /// Then it checks whether `name' exists in the interface type.
2467 public virtual bool VerifyImplements (Type interface_type, string full,
2468 string name, Location loc)
2472 if (ifaces != null){
2473 foreach (Type t in ifaces){
2474 if (t == interface_type){
2482 Report.Error (540, "`" + full + "': containing class does not implement interface `" + interface_type.FullName + "'");
2489 protected override void VerifyObsoleteAttribute()
2491 CheckUsageOfObsoleteAttribute (TypeBuilder.BaseType);
2496 foreach (Type iface in ifaces) {
2497 CheckUsageOfObsoleteAttribute (iface);
2505 string IMemberContainer.Name {
2511 Type IMemberContainer.Type {
2517 MemberCache IMemberContainer.MemberCache {
2519 return member_cache;
2523 bool IMemberContainer.IsInterface {
2525 return Kind == Kind.Interface;
2529 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
2531 BindingFlags new_bf = bf | BindingFlags.DeclaredOnly;
2533 if (GenericType != null)
2534 return TypeManager.FindMembers (GenericType, mt, new_bf,
2537 return FindMembers (mt, new_bf, null, null);
2541 // Generates xml doc comments (if any), and if required,
2542 // handle warning report.
2544 internal override void GenerateDocComment (DeclSpace ds)
2546 DocUtil.GenerateTypeDocComment (this, ds);
2549 public override string DocCommentHeader {
2550 get { return "T:"; }
2553 public virtual MemberCache ParentCache {
2555 return parent_cache;
2561 public class PartialContainer : TypeContainer {
2563 public readonly Namespace Namespace;
2564 public readonly int OriginalModFlags;
2565 public readonly int AllowedModifiers;
2566 public readonly TypeAttributes DefaultTypeAttributes;
2568 static PartialContainer Create (NamespaceEntry ns, TypeContainer parent,
2569 MemberName member_name, int mod_flags, Kind kind,
2572 PartialContainer pc;
2573 string full_name = member_name.GetName (true);
2574 DeclSpace ds = (DeclSpace) RootContext.Tree.Decls [full_name];
2576 pc = ds as PartialContainer;
2580 260, ds.Location, "Missing partial modifier " +
2581 "on declaration of type `{0}'; another " +
2582 "partial implementation of this type exists",
2583 member_name.GetTypeName());
2585 Report.LocationOfPreviousError (loc);
2589 if (pc.Kind != kind) {
2591 261, loc, "Partial declarations of `{0}' " +
2592 "must be all classes, all structs or " +
2593 "all interfaces", member_name.GetTypeName ());
2597 if (pc.OriginalModFlags != mod_flags) {
2599 262, loc, "Partial declarations of `{0}' " +
2600 "have conflicting accessibility modifiers",
2601 member_name.GetTypeName ());
2608 pc = new PartialContainer (ns, parent, member_name, mod_flags, kind, loc);
2609 RootContext.Tree.RecordDecl (full_name, pc);
2610 parent.AddType (pc);
2615 public static ClassPart CreatePart (NamespaceEntry ns, TypeContainer parent,
2616 MemberName name, int mod, Attributes attrs,
2617 Kind kind, Location loc)
2619 PartialContainer pc = Create (ns, parent, name, mod, kind, loc);
2621 // An error occured; create a dummy container, but don't
2623 pc = new PartialContainer (ns, parent, name, mod, kind, loc);
2626 ClassPart part = new ClassPart (ns, pc, mod, attrs, kind, loc);
2631 protected PartialContainer (NamespaceEntry ns, TypeContainer parent,
2632 MemberName name, int mod, Kind kind, Location l)
2633 : base (ns, parent, name, null, kind, l)
2635 this.Namespace = ns.NS;
2639 AllowedModifiers = Class.AllowedModifiers;
2640 DefaultTypeAttributes = Class.DefaultTypeAttributes;
2644 AllowedModifiers = Struct.AllowedModifiers;
2645 DefaultTypeAttributes = Struct.DefaultTypeAttributes;
2648 case Kind.Interface:
2649 AllowedModifiers = Interface.AllowedModifiers;
2650 DefaultTypeAttributes = Interface.DefaultTypeAttributes;
2654 throw new InvalidOperationException ();
2658 if (parent.Parent == null)
2659 accmods = Modifiers.INTERNAL;
2661 accmods = Modifiers.PRIVATE;
2663 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2664 this.OriginalModFlags = mod;
2667 public override void Register ()
2669 if (Kind == Kind.Interface)
2670 Parent.AddInterface (this);
2671 else if (Kind == Kind.Class || Kind == Kind.Struct)
2672 Parent.AddClassOrStruct (this);
2674 throw new InvalidOperationException ();
2677 public override PendingImplementation GetPendingImplementations ()
2679 return PendingImplementation.GetPendingImplementations (this);
2682 public ClassPart AddPart (NamespaceEntry ns, int mod, Attributes attrs,
2685 ClassPart part = new ClassPart (ns, this, mod, attrs, Kind, l);
2690 public override TypeAttributes TypeAttr {
2692 return base.TypeAttr | DefaultTypeAttributes;
2697 public class ClassPart : TypeContainer, IMemberContainer {
2698 public readonly PartialContainer PartialContainer;
2699 public readonly bool IsPartial;
2701 public ClassPart (NamespaceEntry ns, PartialContainer parent,
2702 int mod, Attributes attrs, Kind kind, Location l)
2703 : base (ns, parent.Parent, parent.MemberName, attrs, kind, l)
2705 this.PartialContainer = parent;
2706 this.IsPartial = true;
2709 if (parent.Parent == null)
2710 accmods = Modifiers.INTERNAL;
2712 accmods = Modifiers.PRIVATE;
2714 this.ModFlags = Modifiers.Check (
2715 parent.AllowedModifiers, mod, accmods, l);
2718 public override void Register ()
2722 public override PendingImplementation GetPendingImplementations ()
2724 return PartialContainer.Pending;
2727 public override bool VerifyImplements (Type interface_type, string full,
2728 string name, Location loc)
2730 return PartialContainer.VerifyImplements (
2731 interface_type, full, name, loc);
2734 public override MemberCache ParentCache {
2736 return PartialContainer.ParentCache;
2741 public abstract class ClassOrStruct : TypeContainer {
2742 bool hasExplicitLayout = false;
2743 ListDictionary declarative_security;
2745 public ClassOrStruct (NamespaceEntry ns, TypeContainer parent,
2746 MemberName name, Attributes attrs, Kind kind,
2748 : base (ns, parent, name, attrs, kind, l)
2752 public override PendingImplementation GetPendingImplementations ()
2754 return PendingImplementation.GetPendingImplementations (this);
2757 public override bool HasExplicitLayout {
2759 return hasExplicitLayout;
2763 protected override void VerifyMembers (EmitContext ec)
2765 base.VerifyMembers (ec);
2767 if ((events != null) && (RootContext.WarningLevel >= 3)) {
2768 foreach (Event e in events){
2770 Report.Warning (67, e.Location, "The event '{0}' is never used", e.GetSignatureForError ());
2775 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
2777 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
2778 if (declarative_security == null)
2779 declarative_security = new ListDictionary ();
2781 a.ExtractSecurityPermissionSet (declarative_security);
2785 if (a.Type == TypeManager.struct_layout_attribute_type
2786 && (LayoutKind) a.GetPositionalValue (0) == LayoutKind.Explicit)
2787 hasExplicitLayout = true;
2789 base.ApplyAttributeBuilder (a, cb);
2792 public override void Emit()
2796 if (declarative_security != null) {
2797 foreach (DictionaryEntry de in declarative_security) {
2798 TypeBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
2803 public override void Register ()
2805 Parent.AddClassOrStruct (this);
2810 /// Class handles static classes declaration
2812 public sealed class StaticClass: Class {
2813 public StaticClass (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2814 Attributes attrs, Location l)
2815 : base (ns, parent, name, mod & ~Modifiers.STATIC, attrs, l)
2817 if (RootContext.Version == LanguageVersion.ISO_1) {
2818 Report.FeatureIsNotStandardized (l, "static classes");
2819 Environment.Exit (1);
2823 protected override void DefineContainerMembers (MemberCoreArrayList list)
2828 foreach (MemberCore m in list) {
2829 if (m is Operator) {
2830 Report.Error (715, m.Location, "'{0}': static classes cannot contain user-defined operators", m.GetSignatureForError (this));
2834 if ((m.ModFlags & Modifiers.STATIC) != 0)
2837 if (m is Constructor) {
2838 Report.Error (710, m.Location, "'{0}': Static classes cannot have instance constructors", GetSignatureForError ());
2842 if (m is Destructor) {
2843 Report.Error (711, m.Location, "'{0}': Static class cannot contain destructor", GetSignatureForError ());
2846 Report.Error (708, m.Location, "'{0}': cannot declare instance members in a static class", m.GetSignatureForError (this));
2849 base.DefineContainerMembers (list);
2852 public override TypeBuilder DefineType()
2854 TypeBuilder tb = base.DefineType ();
2858 if ((ptype != null) && (ptype != TypeManager.object_type)) {
2861 "Static class '{0}' cannot derive from type '{1}'. " +
2862 "Static classes must derive from object",
2863 GetSignatureForError (), ptype);
2867 if (ifaces != null) {
2868 foreach (Type t in ifaces)
2869 Report.SymbolRelatedToPreviousError (t);
2872 "'{0}': static classes cannot implement interfaces",
2873 GetSignatureForError ());
2878 public override TypeAttributes TypeAttr {
2880 return base.TypeAttr | TypeAttributes.Abstract | TypeAttributes.Sealed;
2885 public class Class : ClassOrStruct {
2887 // Modifiers allowed in a class declaration
2889 public const int AllowedModifiers =
2892 Modifiers.PROTECTED |
2893 Modifiers.INTERNAL |
2895 Modifiers.ABSTRACT |
2899 // Information in the case we are an attribute type
2900 AttributeUsageAttribute attribute_usage;
2902 public Class (NamespaceEntry ns, TypeContainer parent, MemberName name, int mod,
2903 Attributes attrs, Location l)
2904 : base (ns, parent, name, attrs, Kind.Class, l)
2908 if (parent.Parent == null)
2909 accmods = Modifiers.INTERNAL;
2911 accmods = Modifiers.PRIVATE;
2913 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2914 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.SEALED)) == (Modifiers.ABSTRACT | Modifiers.SEALED)) {
2915 Report.Error (502, Location, "'{0}' cannot be both abstract and sealed", GetSignatureForError ());
2918 attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
2921 public override AttributeTargets AttributeTargets {
2923 return AttributeTargets.Class;
2927 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
2929 if (a.UsageAttribute != null) {
2930 if (ptype != TypeManager.attribute_type &&
2931 !ptype.IsSubclassOf (TypeManager.attribute_type) &&
2932 TypeBuilder.FullName != "System.Attribute") {
2933 Report.Error (641, a.Location, "Attribute '{0}' is only valid on classes derived from System.Attribute", a.Name);
2935 attribute_usage = a.UsageAttribute;
2938 base.ApplyAttributeBuilder (a, cb);
2941 public AttributeUsageAttribute AttributeUsage {
2943 return attribute_usage;
2947 public const TypeAttributes DefaultTypeAttributes =
2948 TypeAttributes.AutoLayout | TypeAttributes.Class;
2951 // FIXME: How do we deal with the user specifying a different
2954 public override TypeAttributes TypeAttr {
2956 return base.TypeAttr | DefaultTypeAttributes;
2961 public class Struct : ClassOrStruct {
2963 // Modifiers allowed in a struct declaration
2965 public const int AllowedModifiers =
2968 Modifiers.PROTECTED |
2969 Modifiers.INTERNAL |
2973 public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
2974 int mod, Attributes attrs, Location l)
2975 : base (ns, parent, name, attrs, Kind.Struct, l)
2979 if (parent.Parent == null)
2980 accmods = Modifiers.INTERNAL;
2982 accmods = Modifiers.PRIVATE;
2984 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
2986 this.ModFlags |= Modifiers.SEALED;
2989 public override AttributeTargets AttributeTargets {
2991 return AttributeTargets.Struct;
2995 public const TypeAttributes DefaultTypeAttributes =
2996 TypeAttributes.SequentialLayout |
2997 TypeAttributes.Sealed |
2998 TypeAttributes.BeforeFieldInit;
3001 // FIXME: Allow the user to specify a different set of attributes
3002 // in some cases (Sealed for example is mandatory for a class,
3003 // but what SequentialLayout can be changed
3005 public override TypeAttributes TypeAttr {
3007 return base.TypeAttr | DefaultTypeAttributes;
3015 public class Interface : TypeContainer, IMemberContainer {
3017 /// Modifiers allowed in a class declaration
3019 public const int AllowedModifiers =
3022 Modifiers.PROTECTED |
3023 Modifiers.INTERNAL |
3027 public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name,
3028 int mod, Attributes attrs, Location l)
3029 : base (ns, parent, name, attrs, Kind.Interface, l)
3033 if (parent.Parent == null)
3034 accmods = Modifiers.INTERNAL;
3036 accmods = Modifiers.PRIVATE;
3038 this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
3041 public override void Register ()
3043 Parent.AddInterface (this);
3046 public override PendingImplementation GetPendingImplementations ()
3051 public override AttributeTargets AttributeTargets {
3053 return AttributeTargets.Interface;
3057 public const TypeAttributes DefaultTypeAttributes =
3058 TypeAttributes.AutoLayout |
3059 TypeAttributes.Abstract |
3060 TypeAttributes.Interface;
3062 public override TypeAttributes TypeAttr {
3064 return base.TypeAttr | DefaultTypeAttributes;
3069 public abstract class MethodCore : MemberBase {
3070 public readonly Parameters Parameters;
3071 public readonly GenericMethod GenericMethod;
3072 public readonly DeclSpace ds;
3073 protected ToplevelBlock block;
3076 // Parameters, cached for semantic analysis.
3078 protected InternalParameters parameter_info;
3079 protected Type [] parameter_types;
3081 // Whether this is an operator method.
3082 public bool IsOperator;
3085 // The method we're overriding if this is an override method.
3087 protected MethodInfo parent_method = null;
3089 static string[] attribute_targets = new string [] { "method", "return" };
3091 public MethodCore (TypeContainer parent, GenericMethod generic,
3092 Expression type, int mod, int allowed_mod, bool is_iface,
3093 MemberName name, Attributes attrs, Parameters parameters,
3095 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE, name,
3098 Parameters = parameters;
3099 IsInterface = is_iface;
3100 this.GenericMethod = generic;
3102 if (generic != null)
3109 // Returns the System.Type array for the parameters of this method
3111 public Type [] ParameterTypes {
3113 return parameter_types;
3117 public InternalParameters ParameterInfo
3120 return parameter_info;
3124 public ToplevelBlock Block {
3134 protected override bool CheckBase ()
3136 if (!base.CheckBase ())
3139 // Check whether arguments were correct.
3140 if (!DoDefineParameters ())
3143 if ((caching_flags & Flags.TestMethodDuplication) != 0 && !CheckForDuplications ())
3149 // Is null for System.Object while compiling corlib and base interfaces
3150 if (Parent.ParentCache == null) {
3151 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3152 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3157 Type parent_ret_type = null;
3158 parent_method = FindOutParentMethod (Parent, ref parent_ret_type);
3160 // method is override
3161 if (parent_method != null) {
3163 if (!CheckMethodAgainstBase ())
3166 if ((ModFlags & Modifiers.NEW) == 0) {
3167 if (!MemberType.Equals (TypeManager.TypeToCoreType (parent_ret_type))) {
3168 Report.SymbolRelatedToPreviousError (parent_method);
3169 Report.Error (508, Location, GetSignatureForError (Parent) + ": cannot " +
3170 "change return type when overriding inherited member");
3174 if (parent_method.IsAbstract && !IsInterface) {
3175 Report.SymbolRelatedToPreviousError (parent_method);
3176 Report.Error (533, Location, "'{0}' hides inherited abstract member", GetSignatureForError (Parent));
3181 if (parent_method.IsSpecialName && !(this is PropertyBase)) {
3182 Report.Error (561, Location, "'{0}': cannot override '{1}' because it is a special compiler-generated method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (parent_method));
3186 if (RootContext.WarningLevel > 2) {
3187 if (Name == "Equals" && parameter_types.Length == 1 && parameter_types [0] == TypeManager.object_type)
3188 Parent.Methods.HasEquals = true;
3189 else if (Name == "GetHashCode" && parameter_types.Length == 0)
3190 Parent.Methods.HasGetHashCode = true;
3193 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (parent_method);
3195 EmitContext ec = new EmitContext (this.Parent, this.Parent, Location, null, null, ModFlags, false);
3196 if (OptAttributes == null || !OptAttributes.Contains (TypeManager.obsolete_attribute_type, ec)) {
3197 Report.SymbolRelatedToPreviousError (parent_method);
3198 Report.Warning (672, 1, Location, "Member '{0}' overrides obsolete member. Add the Obsolete attribute to '{0}'", GetSignatureForError (Parent));
3204 MemberInfo conflict_symbol = Parent.FindMemberWithSameName (Name, !(this is Property));
3205 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3206 if (conflict_symbol != null) {
3207 Report.SymbolRelatedToPreviousError (conflict_symbol);
3208 if (this is PropertyBase)
3209 Report.Error (544, Location, "'{0}': cannot override because '{1}' is not a property", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
3211 Report.Error (505, Location, "'{0}': cannot override because '{1}' is not a method", GetSignatureForError (Parent), TypeManager.GetFullNameSignature (conflict_symbol));
3213 Report.Error (115, Location, "'{0}': no suitable methods found to override", GetSignatureForError (Parent));
3217 if (conflict_symbol == null) {
3218 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
3219 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
3224 if ((ModFlags & Modifiers.NEW) == 0) {
3225 if (this is Method && conflict_symbol is MethodBase)
3228 Report.SymbolRelatedToPreviousError (conflict_symbol);
3229 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3237 // Performs various checks on the MethodInfo `mb' regarding the modifier flags
3238 // that have been defined.
3240 // `name' is the user visible name for reporting errors (this is used to
3241 // provide the right name regarding method names and properties)
3243 bool CheckMethodAgainstBase ()
3247 // TODO: replace with GetSignatureForError
3248 string name = parent_method.DeclaringType.Name + "." + parent_method.Name;
3250 if ((ModFlags & Modifiers.OVERRIDE) != 0){
3251 if (!(parent_method.IsAbstract || parent_method.IsVirtual)){
3253 506, Location, Parent.MakeName (Name) +
3254 ": cannot override inherited member `" +
3255 name + "' because it is not " +
3256 "virtual, abstract or override");
3260 // Now we check that the overriden method is not final
3262 if (parent_method.IsFinal) {
3263 // This happens when implementing interface methods.
3264 if (parent_method.IsHideBySig && parent_method.IsVirtual) {
3266 506, Location, Parent.MakeName (Name) +
3267 ": cannot override inherited member `" +
3268 name + "' because it is not " +
3269 "virtual, abstract or override");
3271 Report.Error (239, Location, Parent.MakeName (Name) + " : cannot " +
3272 "override inherited member `" + name +
3273 "' because it is sealed.");
3277 // Check that the permissions are not being changed
3279 MethodAttributes thisp = flags & MethodAttributes.MemberAccessMask;
3280 MethodAttributes parentp = parent_method.Attributes & MethodAttributes.MemberAccessMask;
3282 if (!CheckAccessModifiers (thisp, parentp, parent_method)) {
3283 Error_CannotChangeAccessModifiers (Parent, parent_method, name);
3288 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0 && Name != "Finalize") {
3289 ModFlags |= Modifiers.NEW;
3290 Report.SymbolRelatedToPreviousError (parent_method);
3291 if (!IsInterface && (parent_method.IsVirtual || parent_method.IsAbstract)) {
3292 if (RootContext.WarningLevel >= 2)
3293 Report.Warning (114, Location, "'{0}' hides inherited member '{1}'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword", GetSignatureForError (Parent), TypeManager.CSharpSignature (parent_method));
3295 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
3301 protected bool CheckAccessModifiers (MethodAttributes thisp, MethodAttributes parentp, MethodInfo base_method)
3303 if ((parentp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3305 // when overriding protected internal, the method can be declared
3306 // protected internal only within the same assembly
3309 if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
3310 if (Parent.TypeBuilder.Assembly != base_method.DeclaringType.Assembly){
3312 // assemblies differ - report an error
3316 } else if (thisp != parentp) {
3318 // same assembly, but other attributes differ - report an error
3323 } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
3325 // if it's not "protected internal", it must be "protected"
3329 } else if (Parent.TypeBuilder.Assembly == base_method.DeclaringType.Assembly) {
3331 // protected within the same assembly - an error
3334 } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) !=
3335 (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
3337 // protected ok, but other attributes differ - report an error
3343 return (thisp == parentp);
3347 void Error_CannotChangeAccessModifiers (TypeContainer parent, MethodInfo parent_method, string name)
3350 // FIXME: report the old/new permissions?
3353 507, Location, parent.MakeName (Name) +
3354 ": can't change the access modifiers when overriding inherited " +
3355 "member `" + name + "'");
3358 protected static string Error722 {
3360 return "'{0}': static types cannot be used as return types";
3365 /// For custom member duplication search in a container
3367 protected abstract bool CheckForDuplications ();
3370 /// Gets parent method and its return type
3372 protected abstract MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type);
3374 protected virtual bool DoDefineParameters ()
3376 EmitContext ec = ds.EmitContext;
3378 throw new InternalErrorException ("DoDefineParameters invoked too early");
3380 bool old_unsafe = ec.InUnsafe;
3381 ec.InUnsafe = InUnsafe;
3382 // Check if arguments were correct
3383 parameter_types = Parameters.GetParameterInfo (ec);
3384 ec.InUnsafe = old_unsafe;
3386 if ((parameter_types == null) ||
3387 !CheckParameters (ds, parameter_types))
3390 TypeParameter[] tparam = ds.IsGeneric ? ds.TypeParameters : null;
3391 parameter_info = new InternalParameters (parameter_types, Parameters, tparam);
3393 Parameter array_param = Parameters.ArrayParameter;
3394 if ((array_param != null) &&
3395 (!array_param.ParameterType.IsArray ||
3396 (array_param.ParameterType.GetArrayRank () != 1))) {
3397 Report.Error (225, Location, "params parameter has to be a single dimensional array");
3404 void error_425 (Type old, Type t, string name)
3406 Report.Error (425, Location,
3407 "The constraints of type parameter `{0}' " +
3408 "of method `{1}' must match the constraints for " +
3409 "type parameter `{2}' of method `{3}'",
3410 TypeManager.CSharpName (old), Name,
3411 TypeManager.CSharpName (t), name);
3414 protected override bool CheckGenericOverride (MethodInfo method, string name)
3416 ParameterData pd = Invocation.GetParameterData (method);
3418 for (int i = 0; i < ParameterTypes.Length; i++) {
3419 GenericConstraints ogc = pd.GenericConstraints (i);
3420 GenericConstraints gc = ParameterInfo.GenericConstraints (i);
3422 if ((gc == null) && (ogc == null))
3425 Type ot = pd.ParameterType (i);
3426 Type t = ParameterTypes [i];
3428 if (!((gc != null) && (ogc != null))) {
3429 error_425 (ot, t, name);
3433 if ((gc.Attributes != ogc.Attributes) ||
3434 (gc.HasClassConstraint != ogc.HasClassConstraint)) {
3435 error_425 (ot, t, name);
3439 if (ogc.HasClassConstraint &&
3440 !ogc.ClassConstraint.Equals (gc.ClassConstraint)) {
3441 error_425 (ot, t, name);
3445 Type[] oct = ogc.InterfaceConstraints;
3446 Type[] ct = gc.InterfaceConstraints;
3448 if (oct.Length != ct.Length) {
3449 error_425 (ot, t, name);
3453 for (int j = 0; j < oct.Length; j++)
3454 if (!oct [j].Equals (ct [j])) {
3455 error_425 (ot, t, name);
3463 public override string[] ValidAttributeTargets {
3465 return attribute_targets;
3469 protected override bool VerifyClsCompliance (DeclSpace ds)
3471 if (!base.VerifyClsCompliance (ds)) {
3472 if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
3473 Report.Error (3011, Location, "'{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
3478 if (Parameters.HasArglist) {
3479 Report.Error (3000, Location, "Methods with variable arguments are not CLS-compliant");
3482 if (!AttributeTester.IsClsCompliant (MemberType)) {
3483 Report.Error (3002, Location, "Return type of '{0}' is not CLS-compliant", GetSignatureForError ());
3486 AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
3491 protected bool IsDuplicateImplementation (MethodCore method)
3493 if ((method == this) ||
3494 (method.MemberName.GetTypeName () != MemberName.GetTypeName ()))
3497 Type[] param_types = method.ParameterTypes;
3498 if (param_types == null)
3501 if (param_types.Length != ParameterTypes.Length)
3504 int type_params = 0;
3505 if (GenericMethod != null)
3506 type_params = GenericMethod.CountTypeParameters;
3508 int m_type_params = 0;
3509 if (method.GenericMethod != null)
3510 m_type_params = method.GenericMethod.CountTypeParameters;
3512 if (type_params != m_type_params)
3518 Type[] infered_types;
3519 if (type_params > 0)
3520 infered_types = new Type [type_params];
3522 infered_types = null;
3524 may_unify = Invocation.InferTypeArguments (
3525 param_types, ParameterTypes, ref infered_types);
3528 if (type_params > 0)
3529 infered_types = new Type [type_params];
3531 infered_types = null;
3533 may_unify = Invocation.InferTypeArguments (
3534 ParameterTypes, param_types, ref infered_types);
3537 for (int i = 0; i < param_types.Length; i++) {
3538 if (param_types [i] != ParameterTypes [i])
3542 // TODO: make operator compatible with MethodCore to avoid this
3543 if (this is Operator && method is Operator) {
3544 if (MemberType != method.MemberType)
3550 // Try to report 663: method only differs on out/ref
3552 ParameterData info = ParameterInfo;
3553 ParameterData other_info = method.ParameterInfo;
3554 for (int i = 0; i < info.Count; i++){
3555 if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
3556 Report.Error (663, Location,
3557 "Overload method only differs " +
3558 "in parameter modifier");
3563 Report.SymbolRelatedToPreviousError (method);
3564 Report.Error (111, Location, "Type '{0}' already defines a member called '{1}' with the same parameter types", Parent.Name, Name);
3566 } else if (may_unify) {
3567 Report.Error (408, Location,
3568 "`{0}' cannot define overload members that " +
3569 "may unify for some type parameter substitutions",
3578 // Returns a string that represents the signature for this
3579 // member which should be used in XML documentation.
3581 public override string GetDocCommentName (DeclSpace ds)
3583 return DocUtil.GetMethodDocCommentName (this, ds);
3587 // Raised (and passed an XmlElement that contains the comment)
3588 // when GenerateDocComment is writing documentation expectedly.
3590 // FIXME: with a few effort, it could be done with XmlReader,
3591 // that means removal of DOM use.
3593 internal override void OnGenerateDocComment (DeclSpace ds, XmlElement el)
3595 DocUtil.OnMethodGenerateDocComment (this, ds, el);
3599 // Represents header string for documentation comment.
3601 public override string DocCommentHeader {
3602 get { return "M:"; }
3605 protected override void VerifyObsoleteAttribute()
3607 base.VerifyObsoleteAttribute ();
3609 if (parameter_types == null)
3612 foreach (Type type in parameter_types) {
3613 CheckUsageOfObsoleteAttribute (type);
3618 public class SourceMethod : ISourceMethod
3620 TypeContainer container;
3623 protected SourceMethod (TypeContainer container, MethodBase builder,
3624 ISourceFile file, Location start, Location end)
3626 this.container = container;
3627 this.builder = builder;
3629 CodeGen.SymbolWriter.OpenMethod (
3630 file, this, start.Row, 0, end.Row, 0);
3633 public string Name {
3634 get { return builder.Name; }
3637 public int NamespaceID {
3638 get { return container.NamespaceEntry.SymbolFileID; }
3643 if (builder is MethodBuilder)
3644 return ((MethodBuilder) builder).GetToken ().Token;
3645 else if (builder is ConstructorBuilder)
3646 return ((ConstructorBuilder) builder).GetToken ().Token;
3648 throw new NotSupportedException ();
3652 public void CloseMethod ()
3654 if (CodeGen.SymbolWriter != null)
3655 CodeGen.SymbolWriter.CloseMethod ();
3658 public static SourceMethod Create (TypeContainer parent,
3659 MethodBase builder, Block block)
3661 if (CodeGen.SymbolWriter == null)
3666 Location start_loc = block.StartLocation;
3667 if (Location.IsNull (start_loc))
3670 Location end_loc = block.EndLocation;
3671 if (Location.IsNull (end_loc))
3674 ISourceFile file = start_loc.SourceFile;
3678 return new SourceMethod (
3679 parent, builder, file, start_loc, end_loc);
3683 public class Method : MethodCore, IIteratorContainer, IMethodData {
3684 public MethodBuilder MethodBuilder;
3685 public MethodData MethodData;
3686 ReturnParameter return_attributes;
3687 ListDictionary declarative_security;
3690 /// Modifiers allowed in a class declaration
3692 const int AllowedModifiers =
3695 Modifiers.PROTECTED |
3696 Modifiers.INTERNAL |
3701 Modifiers.OVERRIDE |
3702 Modifiers.ABSTRACT |
3704 Modifiers.METHOD_YIELDS |
3707 const int AllowedInterfaceModifiers =
3708 Modifiers.NEW | Modifiers.UNSAFE;
3711 // return_type can be "null" for VOID values.
3713 public Method (TypeContainer parent, GenericMethod generic,
3714 Expression return_type, int mod, bool is_iface,
3715 MemberName name, Parameters parameters, Attributes attrs,
3717 : base (parent, generic, return_type, mod,
3718 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
3719 is_iface, name, attrs, parameters, l)
3723 public override AttributeTargets AttributeTargets {
3725 return AttributeTargets.Method;
3729 public override string GetSignatureForError()
3731 if (MethodBuilder == null) {
3732 return GetSignatureForError (Parent);
3734 return TypeManager.CSharpSignature (MethodBuilder);
3738 /// Use this method when MethodBuilder is null
3740 public override string GetSignatureForError (TypeContainer tc)
3742 // TODO: get params from somewhere
3743 if (parameter_info == null)
3744 return base.GetSignatureForError (tc);
3746 // TODO: move to parameters
3747 System.Text.StringBuilder args = new System.Text.StringBuilder ();
3748 if (parameter_info.Parameters.FixedParameters != null) {
3749 for (int i = 0; i < parameter_info.Parameters.FixedParameters.Length; ++i) {
3750 Parameter p = parameter_info.Parameters.FixedParameters [i];
3751 args.Append (p.GetSignatureForError ());
3753 if (i < parameter_info.Parameters.FixedParameters.Length - 1)
3758 return String.Concat (base.GetSignatureForError (tc), "(", args.ToString (), ")");
3761 void DuplicateEntryPoint (MethodInfo b, Location location)
3765 "Program `" + CodeGen.FileName +
3766 "' has more than one entry point defined: `" +
3767 TypeManager.CSharpSignature(b) + "'");
3770 public bool IsEntryPoint (MethodBuilder b, InternalParameters pinfo)
3772 if (b.ReturnType != TypeManager.void_type &&
3773 b.ReturnType != TypeManager.int32_type)
3776 if (pinfo.Count == 0)
3779 if (pinfo.Count > 1)
3782 Type t = pinfo.ParameterType(0);
3784 (t.GetArrayRank() == 1) &&
3785 (TypeManager.GetElementType(t) == TypeManager.string_type) &&
3786 (pinfo.ParameterModifier(0) == Parameter.Modifier.NONE))
3792 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
3794 if (a.Target == AttributeTargets.ReturnValue) {
3795 if (return_attributes == null)
3796 return_attributes = new ReturnParameter (MethodBuilder, Location);
3798 return_attributes.ApplyAttributeBuilder (a, cb);
3802 if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall) {
3803 MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
3806 if (a.Type == TypeManager.dllimport_type) {
3807 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
3808 if ((ModFlags & extern_static) != extern_static) {
3809 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
3815 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
3816 if (declarative_security == null)
3817 declarative_security = new ListDictionary ();
3818 a.ExtractSecurityPermissionSet (declarative_security);
3822 if (a.Type == TypeManager.conditional_attribute_type) {
3823 if (IsOperator || IsExplicitImpl) {
3824 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
3828 if (ReturnType != TypeManager.void_type) {
3829 Report.Error (578, Location, "Conditional not valid on '{0}' because its return new ErrorData ( type is not void", GetSignatureForError ());
3833 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
3834 Report.Error (243, Location, "Conditional not valid on '{0}' because it is an override method", GetSignatureForError ());
3839 Report.Error (582, Location, "Conditional not valid on interface members");
3843 if (MethodData.IsImplementing) {
3844 Report.Error (629, Location, "Conditional member '{0}' cannot implement interface member", GetSignatureForError ());
3848 for (int i = 0; i < parameter_info.Count; ++i) {
3849 if ((parameter_info.ParameterModifier (i) & Parameter.Modifier.OUT) != 0) {
3850 Report.Error (685, Location, "Conditional method '{0}' cannot have an out parameter", GetSignatureForError ());
3856 MethodBuilder.SetCustomAttribute (cb);
3859 protected override bool CheckForDuplications ()
3861 ArrayList ar = Parent.Methods;
3863 int arLen = ar.Count;
3865 for (int i = 0; i < arLen; i++) {
3866 Method m = (Method) ar [i];
3867 if (IsDuplicateImplementation (m))
3872 ar = Parent.Properties;
3874 for (int i = 0; i < ar.Count; ++i) {
3875 PropertyBase pb = (PropertyBase) ar [i];
3876 if (pb.AreAccessorsDuplicateImplementation (this))
3881 ar = Parent.Indexers;
3883 for (int i = 0; i < ar.Count; ++i) {
3884 PropertyBase pb = (PropertyBase) ar [i];
3885 if (pb.AreAccessorsDuplicateImplementation (this))
3892 for (int i = 0; i < ar.Count; ++i) {
3893 Event ev = (Event) ar [i];
3894 if (ev.AreAccessorsDuplicateImplementation (this))
3905 public override bool Define ()
3907 if (!DoDefineBase ())
3910 MethodBuilder mb = null;
3911 if (GenericMethod != null) {
3912 string mname = MemberName.GetMethodName ();
3913 mb = Parent.TypeBuilder.DefineGenericMethod (mname, flags);
3914 if (!GenericMethod.Define (mb, ReturnType))
3925 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
3927 MethodData = new MethodData (this, ParameterInfo, ModFlags, flags,
3928 this, mb, GenericMethod, parent_method);
3930 if (!MethodData.Define (Parent))
3934 // Setup iterator if we are one
3936 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
3937 Iterator iterator = new Iterator (
3938 Parent, Name, MemberType, ParameterTypes,
3939 ParameterInfo, ModFlags, block, Location);
3941 if (!iterator.DefineIterator ())
3944 block = iterator.Block;
3947 MethodBuilder = MethodData.MethodBuilder;
3950 // This is used to track the Entry Point,
3952 if (Name == "Main" &&
3953 ((ModFlags & Modifiers.STATIC) != 0) && RootContext.NeedsEntryPoint &&
3954 (RootContext.MainClass == null ||
3955 RootContext.MainClass == Parent.TypeBuilder.FullName)){
3956 if (IsEntryPoint (MethodBuilder, ParameterInfo)) {
3957 if (RootContext.EntryPoint == null) {
3958 if (Parent.IsGeneric){
3959 Report.Error (-201, Location,
3960 "Entry point can not be defined in a generic class");
3963 RootContext.EntryPoint = MethodBuilder;
3964 RootContext.EntryPointLocation = Location;
3966 DuplicateEntryPoint (RootContext.EntryPoint, RootContext.EntryPointLocation);
3967 DuplicateEntryPoint (MethodBuilder, Location);
3970 if (RootContext.WarningLevel >= 4)
3971 Report.Warning (28, Location, "'{0}' has the wrong signature to be an entry point", TypeManager.CSharpSignature(MethodBuilder) );
3975 if (MemberType.IsAbstract && MemberType.IsSealed) {
3976 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
3986 public override void Emit ()
3988 MethodData.Emit (Parent, this);
3991 if (declarative_security != null) {
3992 foreach (DictionaryEntry de in declarative_security) {
3993 MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4001 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
4003 MethodInfo mi = (MethodInfo) container.ParentCache.FindMemberToOverride (
4004 container.TypeBuilder, Name, ParameterTypes, false);
4009 parent_ret_type = mi.ReturnType;
4013 protected override bool VerifyClsCompliance(DeclSpace ds)
4015 if (!base.VerifyClsCompliance (ds))
4018 if (parameter_types.Length > 0) {
4019 ArrayList al = (ArrayList)ds.MemberCache.Members [Name];
4021 ds.MemberCache.VerifyClsParameterConflict (al, this, MethodBuilder);
4028 void IIteratorContainer.SetYields ()
4030 ModFlags |= Modifiers.METHOD_YIELDS;
4033 #region IMethodData Members
4035 public CallingConventions CallingConventions {
4037 CallingConventions cc = Parameters.GetCallingConvention ();
4038 if (Parameters.HasArglist)
4039 block.HasVarargs = true;
4042 if ((ModFlags & Modifiers.STATIC) == 0)
4043 cc |= CallingConventions.HasThis;
4045 // FIXME: How is `ExplicitThis' used in C#?
4051 public Type ReturnType {
4057 public MemberName MethodName {
4063 public new Location Location {
4065 return base.Location;
4069 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4071 return new EmitContext (
4072 tc, ds, Location, ig, ReturnType, ModFlags, false);
4075 public ObsoleteAttribute GetObsoleteAttribute ()
4077 return GetObsoleteAttribute (ds);
4081 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
4083 public bool IsExcluded (EmitContext ec)
4085 if ((caching_flags & Flags.Excluded_Undetected) == 0)
4086 return (caching_flags & Flags.Excluded) != 0;
4088 caching_flags &= ~Flags.Excluded_Undetected;
4090 if (parent_method == null) {
4091 if (OptAttributes == null)
4094 Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
4099 foreach (Attribute a in attrs) {
4100 string condition = a.GetConditionalAttributeValue (ds);
4101 if (RootContext.AllDefines.Contains (condition))
4105 caching_flags |= Flags.Excluded;
4109 IMethodData md = TypeManager.GetMethod (parent_method);
4111 if (AttributeTester.IsConditionalMethodExcluded (parent_method)) {
4112 caching_flags |= Flags.Excluded;
4118 if (md.IsExcluded (ec)) {
4119 caching_flags |= Flags.Excluded;
4125 GenericMethod IMethodData.GenericMethod {
4127 return GenericMethod;
4134 public abstract class ConstructorInitializer {
4135 ArrayList argument_list;
4136 protected ConstructorInfo parent_constructor;
4137 Parameters parameters;
4140 public ConstructorInitializer (ArrayList argument_list, Parameters parameters,
4143 this.argument_list = argument_list;
4144 this.parameters = parameters;
4148 public ArrayList Arguments {
4150 return argument_list;
4154 public bool Resolve (ConstructorBuilder caller_builder, EmitContext ec)
4156 Expression parent_constructor_group;
4159 ec.CurrentBlock = new ToplevelBlock (Block.Flags.Implicit, parameters, loc);
4161 if (argument_list != null){
4162 foreach (Argument a in argument_list){
4163 if (!a.Resolve (ec, loc))
4167 ec.CurrentBlock = null;
4169 if (this is ConstructorBaseInitializer) {
4170 if (ec.ContainerType.BaseType == null)
4173 t = ec.ContainerType.BaseType;
4174 if (ec.ContainerType.IsValueType) {
4175 Report.Error (522, loc,
4176 "structs cannot call base class constructors");
4180 t = ec.ContainerType;
4182 parent_constructor_group = Expression.MemberLookup (
4183 ec, t, ".ctor", MemberTypes.Constructor,
4184 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4187 if (parent_constructor_group == null){
4188 parent_constructor_group = Expression.MemberLookup (
4189 ec, t, ".ctor", MemberTypes.Constructor,
4190 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
4193 if (parent_constructor_group != null)
4195 112, loc, "`{0}.{1}' is inaccessible due to " +
4196 "its protection level", t.FullName, t.Name);
4199 1501, loc, "Can not find a constructor for " +
4200 "this argument list");
4204 parent_constructor = (ConstructorInfo) Invocation.OverloadResolve (
4205 ec, (MethodGroupExpr) parent_constructor_group, argument_list,
4208 if (parent_constructor == null){
4209 Report.Error (1501, loc,
4210 "Can not find a constructor for this argument list");
4214 if (parent_constructor == caller_builder){
4215 Report.Error (516, String.Format ("Constructor `{0}' can not call itself", TypeManager.CSharpSignature (caller_builder)));
4222 public void Emit (EmitContext ec)
4224 if (parent_constructor != null){
4225 ec.Mark (loc, false);
4227 Invocation.EmitCall (ec, true, true, null, parent_constructor, argument_list, loc);
4229 Invocation.EmitCall (ec, true, false, ec.GetThis (loc), parent_constructor, argument_list, loc);
4234 /// Method search for base ctor. (We do not cache it).
4236 Constructor GetOverloadedConstructor (TypeContainer tc)
4238 if (tc.InstanceConstructors == null)
4241 foreach (Constructor c in tc.InstanceConstructors) {
4242 if (Arguments == null) {
4243 if (c.ParameterTypes.Length == 0)
4251 int count = c.ParameterInfo.Count;
4253 c.ParameterInfo.ParameterModifier (count - 1) == Parameter.Modifier.PARAMS) {
4254 for (int i = 0; i < count-1; i++)
4255 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4260 if (c.ParameterTypes.Length != Arguments.Count)
4263 for (int i = 0; i < Arguments.Count; ++i)
4264 if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
4279 //TODO: implement caching when it will be necessary
4280 public virtual void CheckObsoleteAttribute (TypeContainer tc, Location loc)
4282 Constructor ctor = GetOverloadedConstructor (tc);
4286 ObsoleteAttribute oa = ctor.GetObsoleteAttribute (tc);
4290 AttributeTester.Report_ObsoleteMessage (oa, ctor.GetSignatureForError (), loc);
4294 public class ConstructorBaseInitializer : ConstructorInitializer {
4295 public ConstructorBaseInitializer (ArrayList argument_list, Parameters pars, Location l) :
4296 base (argument_list, pars, l)
4300 public override void CheckObsoleteAttribute(TypeContainer tc, Location loc) {
4301 if (parent_constructor == null)
4304 TypeContainer type_ds = TypeManager.LookupTypeContainer (tc.TypeBuilder.BaseType);
4305 if (type_ds == null) {
4306 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (parent_constructor);
4309 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (parent_constructor), loc);
4314 base.CheckObsoleteAttribute (type_ds, loc);
4319 public class ConstructorThisInitializer : ConstructorInitializer {
4320 public ConstructorThisInitializer (ArrayList argument_list, Parameters pars, Location l) :
4321 base (argument_list, pars, l)
4326 public class Constructor : MethodCore, IMethodData {
4327 public ConstructorBuilder ConstructorBuilder;
4328 public ConstructorInitializer Initializer;
4329 ListDictionary declarative_security;
4332 // Modifiers allowed for a constructor.
4334 public const int AllowedModifiers =
4336 Modifiers.PROTECTED |
4337 Modifiers.INTERNAL |
4343 bool has_compliant_args = false;
4345 // The spec claims that static is not permitted, but
4346 // my very own code has static constructors.
4348 public Constructor (TypeContainer ds, string name, int mod, Parameters args,
4349 ConstructorInitializer init, Location l)
4350 : base (ds, null, null, mod, AllowedModifiers, false,
4351 new MemberName (name), null, args, l)
4356 public override string GetSignatureForError()
4358 if (ConstructorBuilder == null)
4359 return GetSignatureForError (Parent);
4361 return TypeManager.CSharpSignature (ConstructorBuilder);
4364 public bool HasCompliantArgs {
4366 return has_compliant_args;
4370 public override AttributeTargets AttributeTargets {
4372 return AttributeTargets.Constructor;
4378 // Returns true if this is a default constructor
4380 public bool IsDefault ()
4382 if ((ModFlags & Modifiers.STATIC) != 0)
4383 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4384 (Parameters.ArrayParameter == null ? true : Parameters.Empty);
4387 return (Parameters.FixedParameters == null ? true : Parameters.Empty) &&
4388 (Parameters.ArrayParameter == null ? true : Parameters.Empty) &&
4389 (Initializer is ConstructorBaseInitializer) &&
4390 (Initializer.Arguments == null);
4393 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
4395 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
4396 if (declarative_security == null) {
4397 declarative_security = new ListDictionary ();
4399 a.ExtractSecurityPermissionSet (declarative_security);
4403 ConstructorBuilder.SetCustomAttribute (cb);
4406 protected override bool CheckForDuplications ()
4408 ArrayList ar = Parent.InstanceConstructors;
4410 int arLen = ar.Count;
4412 for (int i = 0; i < arLen; i++) {
4413 Constructor m = (Constructor) ar [i];
4414 if (IsDuplicateImplementation (m))
4421 protected override bool CheckBase ()
4423 // Check whether arguments were correct.
4424 if (!DoDefineParameters ())
4427 // TODO: skip the rest for generated ctor
4428 if ((ModFlags & Modifiers.STATIC) != 0)
4431 if (!CheckForDuplications ())
4434 if (Parent.Kind == Kind.Struct) {
4435 if (ParameterTypes.Length == 0) {
4436 Report.Error (568, Location,
4437 "Structs can not contain explicit parameterless " +
4442 if ((ModFlags & Modifiers.PROTECTED) != 0) {
4443 Report.Error (666, Location, "Protected member in struct declaration");
4448 if ((RootContext.WarningLevel >= 4) && ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.PROTECTED) != 0)) {
4449 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
4456 // Creates the ConstructorBuilder
4458 public override bool Define ()
4460 MethodAttributes ca = (MethodAttributes.RTSpecialName |
4461 MethodAttributes.SpecialName);
4463 if ((ModFlags & Modifiers.STATIC) != 0){
4464 ca |= MethodAttributes.Static | MethodAttributes.Private;
4466 ca |= MethodAttributes.HideBySig;
4468 if ((ModFlags & Modifiers.PUBLIC) != 0)
4469 ca |= MethodAttributes.Public;
4470 else if ((ModFlags & Modifiers.PROTECTED) != 0){
4471 if ((ModFlags & Modifiers.INTERNAL) != 0)
4472 ca |= MethodAttributes.FamORAssem;
4474 ca |= MethodAttributes.Family;
4475 } else if ((ModFlags & Modifiers.INTERNAL) != 0)
4476 ca |= MethodAttributes.Assembly;
4477 else if (IsDefault ())
4478 ca |= MethodAttributes.Public;
4480 ca |= MethodAttributes.Private;
4483 // Check if arguments were correct.
4487 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
4488 ca, CallingConventions,
4491 if ((ModFlags & Modifiers.UNSAFE) != 0)
4492 ConstructorBuilder.InitLocals = false;
4494 TypeManager.AddMethod (ConstructorBuilder, this);
4497 // HACK because System.Reflection.Emit is lame
4499 TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes);
4507 public override void Emit ()
4509 EmitContext ec = CreateEmitContext (null, null);
4512 // extern methods have no bodies
4514 if ((ModFlags & Modifiers.EXTERN) != 0) {
4515 if ((block != null) && ((ModFlags & Modifiers.EXTERN) != 0)) {
4517 179, Location, "External constructor `" +
4518 TypeManager.CSharpSignature (ConstructorBuilder) +
4519 "' can not have a body");
4522 } else if (block == null) {
4524 501, Location, "Constructor `" +
4525 TypeManager.CSharpSignature (ConstructorBuilder) +
4526 "' must declare a body since it is not marked extern");
4530 if ((ModFlags & Modifiers.STATIC) == 0){
4531 if (Parent.Kind == Kind.Class && Initializer == null)
4532 Initializer = new ConstructorBaseInitializer (
4533 null, Parameters.EmptyReadOnlyParameters, Location);
4537 // Spec mandates that Initializers will not have
4541 if (Initializer != null && !Initializer.Resolve (ConstructorBuilder, ec))
4543 ec.IsStatic = false;
4546 Parameters.LabelParameters (ec, ConstructorBuilder, Location);
4548 SourceMethod source = SourceMethod.Create (
4549 Parent, ConstructorBuilder, block);
4552 // Classes can have base initializers and instance field initializers.
4554 if (Parent.Kind == Kind.Class){
4555 if ((ModFlags & Modifiers.STATIC) == 0){
4558 // If we use a "this (...)" constructor initializer, then
4559 // do not emit field initializers, they are initialized in the other constructor
4561 if (!(Initializer != null && Initializer is ConstructorThisInitializer))
4562 Parent.EmitFieldInitializers (ec);
4565 if (Initializer != null) {
4566 if (GetObsoleteAttribute (Parent) == null && Parent.GetObsoleteAttribute (Parent.Parent) == null)
4567 Initializer.CheckObsoleteAttribute (Parent, Location);
4569 ec.TestObsoleteMethodUsage = false;
4570 Initializer.Emit (ec);
4573 if ((ModFlags & Modifiers.STATIC) != 0)
4574 Parent.EmitFieldInitializers (ec);
4576 if (OptAttributes != null)
4577 OptAttributes.Emit (ec, this);
4579 // If this is a non-static `struct' constructor and doesn't have any
4580 // initializer, it must initialize all of the struct's fields.
4581 if ((Parent.Kind == Kind.Struct) &&
4582 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
4583 Block.AddThisVariable (Parent, Location);
4585 ec.EmitTopBlock (block, ParameterInfo, Location);
4588 source.CloseMethod ();
4592 if (declarative_security != null) {
4593 foreach (DictionaryEntry de in declarative_security) {
4594 ConstructorBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
4601 // Is never override
4602 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
4607 protected override bool VerifyClsCompliance (DeclSpace ds)
4609 if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
4613 if (parameter_types.Length > 0) {
4614 ArrayList al = (ArrayList)ds.MemberCache.Members [".ctor"];
4616 ds.MemberCache.VerifyClsParameterConflict (al, this, ConstructorBuilder);
4618 if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
4619 foreach (Type param in parameter_types) {
4620 if (param.IsArray) {
4626 has_compliant_args = true;
4630 #region IMethodData Members
4632 public System.Reflection.CallingConventions CallingConventions {
4634 CallingConventions cc = Parameters.GetCallingConvention ();
4636 if (Parent.Kind == Kind.Class)
4637 if ((ModFlags & Modifiers.STATIC) == 0)
4638 cc |= CallingConventions.HasThis;
4640 // FIXME: How is `ExplicitThis' used in C#?
4646 public new Location Location {
4648 return base.Location;
4652 public MemberName MethodName {
4658 public Type ReturnType {
4664 public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
4666 ILGenerator ig_ = ConstructorBuilder.GetILGenerator ();
4667 return new EmitContext (Parent, Location, ig_, null, ModFlags, true);
4670 public ObsoleteAttribute GetObsoleteAttribute ()
4675 public bool IsExcluded(EmitContext ec)
4680 GenericMethod IMethodData.GenericMethod {
4690 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
4692 public interface IMethodData
4694 CallingConventions CallingConventions { get; }
4695 Location Location { get; }
4696 MemberName MethodName { get; }
4697 Type[] ParameterTypes { get; }
4698 Type ReturnType { get; }
4699 GenericMethod GenericMethod { get; }
4701 Attributes OptAttributes { get; }
4702 ToplevelBlock Block { get; }
4704 EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
4705 ObsoleteAttribute GetObsoleteAttribute ();
4706 string GetSignatureForError (TypeContainer tc);
4707 bool IsExcluded (EmitContext ec);
4708 bool IsClsCompliaceRequired (DeclSpace ds);
4712 // Encapsulates most of the Method's state
4714 public class MethodData {
4716 readonly IMethodData method;
4719 // The return type of this method
4721 public readonly GenericMethod GenericMethod;
4722 public readonly InternalParameters ParameterInfo;
4725 // Are we implementing an interface ?
4727 public bool IsImplementing = false;
4732 protected MemberBase member;
4733 protected int modifiers;
4734 protected MethodAttributes flags;
4735 protected Type declaring_type;
4736 protected MethodInfo parent_method;
4740 MethodBuilder builder = null;
4741 public MethodBuilder MethodBuilder {
4747 public Type DeclaringType {
4749 return declaring_type;
4753 public MethodData (MemberBase member, InternalParameters parameters,
4754 int modifiers, MethodAttributes flags, IMethodData method)
4756 this.member = member;
4757 this.ParameterInfo = parameters;
4758 this.modifiers = modifiers;
4761 this.method = method;
4764 public MethodData (MemberBase member, InternalParameters parameters,
4765 int modifiers, MethodAttributes flags,
4766 IMethodData method, MethodBuilder builder,
4767 GenericMethod generic, MethodInfo parent_method)
4768 : this (member, parameters, modifiers, flags, method)
4770 this.builder = builder;
4771 this.GenericMethod = generic;
4772 this.parent_method = parent_method;
4775 static string RemoveArity (string name)
4778 StringBuilder sb = new StringBuilder ();
4779 while (start < name.Length) {
4780 int pos = name.IndexOf ('`', start);
4782 sb.Append (name.Substring (start));
4786 sb.Append (name.Substring (start, pos-start));
4789 while ((pos < name.Length) && Char.IsNumber (name [pos]))
4795 return sb.ToString ();
4798 public bool Define (TypeContainer container)
4800 MethodInfo implementing = null;
4803 if (member.IsExplicitImpl)
4804 prefix = member.InterfaceType.FullName + ".";
4808 string name = method.MethodName.Basename;
4809 string method_name = prefix + name;
4811 Type[] ParameterTypes = method.ParameterTypes;
4813 if (container.Pending != null){
4814 if (member is Indexer)
4815 implementing = container.Pending.IsInterfaceIndexer (
4816 member.InterfaceType, method.ReturnType, ParameterTypes);
4818 implementing = container.Pending.IsInterfaceMethod (
4819 member.InterfaceType, name, method.ReturnType, ParameterTypes);
4821 if (member.InterfaceType != null){
4822 if (implementing == null){
4823 Report.Error (539, method.Location,
4824 "'{0}' in explicit interface declaration is not an interface", method_name);
4831 // For implicit implementations, make sure we are public, for
4832 // explicit implementations, make sure we are private.
4834 if (implementing != null){
4836 // Setting null inside this block will trigger a more
4837 // verbose error reporting for missing interface implementations
4839 // The "candidate" function has been flagged already
4840 // but it wont get cleared
4842 if (member.IsExplicitImpl){
4843 if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
4844 Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
4845 implementing = null;
4847 } else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
4848 if (TypeManager.IsInterfaceType (implementing.DeclaringType)){
4850 // If this is an interface method implementation,
4851 // check for public accessibility
4853 implementing = null;
4854 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
4855 // We may never be private.
4856 implementing = null;
4857 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
4859 // We may be protected if we're overriding something.
4861 implementing = null;
4866 // Static is not allowed
4868 if ((modifiers & Modifiers.STATIC) != 0){
4869 implementing = null;
4870 Modifiers.Error_InvalidModifier (method.Location, "static");
4875 // If implementing is still valid, set flags
4877 if (implementing != null){
4879 // When implementing interface methods, set NewSlot
4880 // unless, we are overwriting a method.
4882 if (implementing.DeclaringType.IsInterface){
4883 if ((modifiers & Modifiers.OVERRIDE) == 0)
4884 flags |= MethodAttributes.NewSlot;
4887 MethodAttributes.Virtual |
4888 MethodAttributes.HideBySig;
4890 // Set Final unless we're virtual, abstract or already overriding a method.
4891 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
4892 flags |= MethodAttributes.Final;
4894 IsImplementing = true;
4897 EmitContext ec = method.CreateEmitContext (container, null);
4899 DefineMethodBuilder (ec, container, method_name, ParameterTypes);
4901 if (builder == null)
4904 if (container.CurrentType != null)
4905 declaring_type = container.CurrentType;
4907 declaring_type = container.TypeBuilder;
4909 if ((modifiers & Modifiers.UNSAFE) != 0)
4910 builder.InitLocals = false;
4912 if (IsImplementing){
4914 // clear the pending implemntation flag
4916 if (member is Indexer) {
4917 container.Pending.ImplementIndexer (
4918 member.InterfaceType, builder, method.ReturnType,
4919 ParameterTypes, member.IsExplicitImpl);
4921 container.Pending.ImplementMethod (
4922 member.InterfaceType, name, method.ReturnType,
4923 ParameterTypes, member.IsExplicitImpl);
4925 if (member.IsExplicitImpl)
4926 container.TypeBuilder.DefineMethodOverride (
4927 builder, implementing);
4931 TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes);
4932 TypeManager.AddMethod (builder, method);
4934 if (GenericMethod != null) {
4935 bool is_override = member.IsExplicitImpl |
4936 ((modifiers & Modifiers.OVERRIDE) != 0);
4938 if (implementing != null)
4939 parent_method = implementing;
4941 if (!GenericMethod.DefineType (ec, builder, parent_method, is_override))
4949 /// Create the MethodBuilder for the method
4951 void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
4953 const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
4955 if ((modifiers & extern_static) == extern_static) {
4957 if (method.OptAttributes != null) {
4958 Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
4959 if (dllimport_attribute != null) {
4960 flags |= MethodAttributes.PinvokeImpl;
4961 builder = dllimport_attribute.DefinePInvokeMethod (
4962 ec, container.TypeBuilder, method_name, flags,
4963 method.ReturnType, ParameterTypes);
4969 // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
4970 // We are more strict than Microsoft and report CS0626 like error
4971 if (method.OptAttributes == null ||
4972 !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
4973 Report.Error (626, method.Location, "Method, operator, or accessor '{0}' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation", method.GetSignatureForError (container));
4978 if (builder == null)
4979 builder = container.TypeBuilder.DefineMethod (
4980 method_name, flags, method.CallingConventions,
4981 method.ReturnType, ParameterTypes);
4983 builder.SetGenericMethodSignature (
4984 flags, method.CallingConventions,
4985 method.ReturnType, ParameterTypes);
4991 public void Emit (TypeContainer container, Attributable kind)
4994 if ((flags & MethodAttributes.PinvokeImpl) == 0)
4995 ec = method.CreateEmitContext (container, builder.GetILGenerator ());
4997 ec = method.CreateEmitContext (container, null);
4999 if (method.GetObsoleteAttribute () != null || container.GetObsoleteAttribute (container.Parent) != null)
5000 ec.TestObsoleteMethodUsage = false;
5002 Location loc = method.Location;
5003 Attributes OptAttributes = method.OptAttributes;
5005 if (OptAttributes != null)
5006 OptAttributes.Emit (ec, kind);
5008 if (member is MethodCore)
5009 ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
5011 SymbolWriter sw = CodeGen.SymbolWriter;
5012 ToplevelBlock block = method.Block;
5015 // abstract or extern methods have no bodies
5017 if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
5022 // abstract or extern methods have no bodies.
5024 if ((modifiers & Modifiers.ABSTRACT) != 0)
5026 500, method.Location, "Abstract method `" +
5027 TypeManager.CSharpSignature (builder) +
5028 "' can not have a body");
5030 if ((modifiers & Modifiers.EXTERN) != 0)
5032 179, method.Location, "External method `" +
5033 TypeManager.CSharpSignature (builder) +
5034 "' can not have a body");
5040 // Methods must have a body unless they're extern or abstract
5042 if (block == null) {
5044 501, method.Location, "Method `" +
5045 TypeManager.CSharpSignature (builder) +
5046 "' must declare a body since it is not marked " +
5047 "abstract or extern");
5051 SourceMethod source = SourceMethod.Create (
5052 container, MethodBuilder, method.Block);
5055 // Handle destructors specially
5057 // FIXME: This code generates buggy code
5059 if (member is Destructor)
5060 EmitDestructor (ec, block);
5062 ec.EmitTopBlock (block, ParameterInfo, loc);
5065 source.CloseMethod ();
5068 void EmitDestructor (EmitContext ec, ToplevelBlock block)
5070 ILGenerator ig = ec.ig;
5072 Label finish = ig.DefineLabel ();
5074 block.SetDestructor ();
5076 ig.BeginExceptionBlock ();
5077 ec.ReturnLabel = finish;
5078 ec.HasReturnLabel = true;
5079 ec.EmitTopBlock (block, null, method.Location);
5081 // ig.MarkLabel (finish);
5082 ig.BeginFinallyBlock ();
5084 if (ec.ContainerType.BaseType != null) {
5085 Expression member_lookup = Expression.MemberLookup (
5086 ec, ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
5087 "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
5089 if (member_lookup != null){
5090 MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
5092 ig.Emit (OpCodes.Ldarg_0);
5093 ig.Emit (OpCodes.Call, (MethodInfo) parent_destructor.Methods [0]);
5097 ig.EndExceptionBlock ();
5098 //ig.MarkLabel (ec.ReturnLabel);
5099 ig.Emit (OpCodes.Ret);
5103 public class Destructor : Method {
5105 public Destructor (TypeContainer ds, Expression return_type, int mod, string name,
5106 Parameters parameters, Attributes attrs, Location l)
5107 : base (ds, null, return_type, mod, false, new MemberName (name),
5108 parameters, attrs, l)
5111 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5113 if (a.Type == TypeManager.conditional_attribute_type) {
5114 Report.Error (577, Location, "Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation", GetSignatureForError ());
5118 base.ApplyAttributeBuilder (a, cb);
5122 abstract public class MemberBase : MemberCore {
5123 public Expression Type;
5125 public MethodAttributes flags;
5127 protected readonly int explicit_mod_flags;
5130 // The "short" name of this property / indexer / event. This is the
5131 // name without the explicit interface.
5133 public string ShortName;
5136 // The type of this property / indexer / event
5138 public Type MemberType;
5141 // If true, this is an explicit interface implementation
5143 public bool IsExplicitImpl = false;
5146 // The name of the interface we are explicitly implementing
5148 public MemberName ExplicitInterfaceName = null;
5151 // Whether this is an interface member.
5153 public bool IsInterface;
5156 // If true, the interface type we are explicitly implementing
5158 public Type InterfaceType = null;
5161 // The constructor is only exposed to our children
5163 protected MemberBase (TypeContainer parent, Expression type, int mod,
5164 int allowed_mod, int def_mod, MemberName name,
5165 Attributes attrs, Location loc)
5166 : base (parent, name, attrs, loc)
5168 explicit_mod_flags = mod;
5170 ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
5172 // Check for explicit interface implementation
5173 if (MemberName.Left != null) {
5174 ExplicitInterfaceName = MemberName.Left;
5175 ShortName = MemberName.Name;
5176 IsExplicitImpl = true;
5181 protected virtual bool CheckBase ()
5183 if ((ModFlags & Modifiers.PROTECTED) != 0 && Parent.Kind == Kind.Struct) {
5184 Report.Error (666, Location, "Protected member in struct declaration");
5188 if ((RootContext.WarningLevel >= 4) &&
5189 ((Parent.ModFlags & Modifiers.SEALED) != 0) &&
5190 ((ModFlags & Modifiers.PROTECTED) != 0) &&
5191 ((ModFlags & Modifiers.OVERRIDE) == 0) && (Name != "Finalize")) {
5192 Report.Warning (628, Location, "'{0}': new protected member declared in sealed class", GetSignatureForError (Parent));
5198 protected abstract bool CheckGenericOverride (MethodInfo method, string name);
5200 protected virtual bool CheckParameters (DeclSpace ds, Type [] parameters)
5204 foreach (Type partype in parameters){
5205 if (partype == TypeManager.void_type) {
5207 1547, Location, "Keyword 'void' cannot " +
5208 "be used in this context");
5212 if (partype.IsPointer){
5215 if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (partype), Location))
5219 if (ds.AsAccessible (partype, ModFlags))
5222 if (this is Indexer)
5223 Report.Error (55, Location,
5224 "Inconsistent accessibility: parameter type `" +
5225 TypeManager.CSharpName (partype) + "' is less " +
5226 "accessible than indexer `" + Name + "'");
5227 else if ((this is Method) && ((Method) this).IsOperator)
5228 Report.Error (57, Location,
5229 "Inconsistent accessibility: parameter type `" +
5230 TypeManager.CSharpName (partype) + "' is less " +
5231 "accessible than operator `" + Name + "'");
5233 Report.Error (51, Location,
5234 "Inconsistent accessibility: parameter type `" +
5235 TypeManager.CSharpName (partype) + "' is less " +
5236 "accessible than method `" + Name + "'");
5243 protected virtual bool DoDefineBase ()
5245 EmitContext ec = Parent.EmitContext;
5247 throw new InternalErrorException ("MemberBase.DoDefine called too early");
5250 throw new InternalErrorException ();
5253 ModFlags = Modifiers.PUBLIC |
5254 Modifiers.ABSTRACT |
5255 Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE) | (ModFlags & Modifiers.NEW);
5257 flags = MethodAttributes.Public |
5258 MethodAttributes.Abstract |
5259 MethodAttributes.HideBySig |
5260 MethodAttributes.NewSlot |
5261 MethodAttributes.Virtual;
5263 if (!Parent.MethodModifiersValid (ModFlags, Name, Location))
5266 flags = Modifiers.MethodAttr (ModFlags);
5272 protected virtual bool DoDefine (DeclSpace decl)
5274 EmitContext ec = decl.EmitContext;
5276 throw new InternalErrorException ("MemberBase.DoDefine called too early");
5278 ec.InUnsafe = InUnsafe;
5280 // Lookup Type, verify validity
5281 bool old_unsafe = ec.InUnsafe;
5282 ec.InUnsafe = InUnsafe;
5283 TypeExpr texpr = Type.ResolveAsTypeTerminal (ec);
5284 ec.InUnsafe = old_unsafe;
5289 MemberType = texpr.Type;
5291 if ((Parent.ModFlags & Modifiers.SEALED) != 0){
5292 if ((ModFlags & (Modifiers.VIRTUAL|Modifiers.ABSTRACT)) != 0){
5293 Report.Error (549, Location, "Virtual method can not be contained in sealed class");
5298 // verify accessibility
5299 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5300 if (this is Property)
5301 Report.Error (53, Location,
5302 "Inconsistent accessibility: property type `" +
5303 TypeManager.CSharpName (MemberType) + "' is less " +
5304 "accessible than property `" + Name + "'");
5305 else if (this is Indexer)
5306 Report.Error (54, Location,
5307 "Inconsistent accessibility: indexer return type `" +
5308 TypeManager.CSharpName (MemberType) + "' is less " +
5309 "accessible than indexer `" + Name + "'");
5310 else if (this is MethodCore) {
5311 if (this is Operator)
5312 Report.Error (56, Location,
5313 "Inconsistent accessibility: return type `" +
5314 TypeManager.CSharpName (MemberType) + "' is less " +
5315 "accessible than operator `" + Name + "'");
5317 Report.Error (50, Location,
5318 "Inconsistent accessibility: return type `" +
5319 TypeManager.CSharpName (MemberType) + "' is less " +
5320 "accessible than method `" + Name + "'");
5322 Report.Error (52, Location,
5323 "Inconsistent accessibility: field type `" +
5324 TypeManager.CSharpName (MemberType) + "' is less " +
5325 "accessible than field `" + Name + "'");
5330 if (MemberType.IsPointer && !UnsafeOK (Parent))
5333 if (IsExplicitImpl) {
5334 Expression expr = ExplicitInterfaceName.GetTypeExpression (Location);
5335 TypeExpr iface_texpr = expr.ResolveAsTypeTerminal (ec);
5336 if (iface_texpr == null)
5339 InterfaceType = iface_texpr.Type;
5341 if (!InterfaceType.IsInterface) {
5342 Report.Error (538, Location, "'{0}' in explicit interface declaration is not an interface", TypeManager.CSharpName (InterfaceType));
5346 if (!Parent.VerifyImplements (InterfaceType, ShortName, Name, Location))
5349 Modifiers.Check (Modifiers.AllowedExplicitImplFlags, explicit_mod_flags, 0, Location);
5356 /// The name of the member can be changed during definition (see IndexerName attribute)
5358 protected virtual void UpdateMemberName ()
5360 MemberName.Name = ShortName;
5363 public override string GetSignatureForError (TypeContainer tc)
5365 return String.Concat (tc.Name, '.', base.GetSignatureForError (tc));
5368 protected bool IsTypePermitted ()
5370 if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
5371 Report.Error (610, Location, "Field or property cannot be of type '{0}'", TypeManager.CSharpName (MemberType));
5377 protected override bool VerifyClsCompliance(DeclSpace ds)
5379 if (base.VerifyClsCompliance (ds)) {
5383 if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
5384 Report.Error (3010, Location, "'{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
5389 protected override void VerifyObsoleteAttribute()
5391 CheckUsageOfObsoleteAttribute (MemberType);
5396 // Fields and Events both generate FieldBuilders, we use this to share
5397 // their common bits. This is also used to flag usage of the field
5399 abstract public class FieldBase : MemberBase {
5400 public FieldBuilder FieldBuilder;
5401 public Status status;
5404 public enum Status : byte {
5407 HAS_OFFSET = 4 // Used by FieldMember.
5410 static string[] attribute_targets = new string [] { "field" };
5413 /// Symbol with same name in parent class/struct
5415 public MemberInfo conflict_symbol;
5418 // The constructor is only exposed to our children
5420 protected FieldBase (TypeContainer parent, Expression type, int mod,
5421 int allowed_mod, MemberName name, object init,
5422 Attributes attrs, Location loc)
5423 : base (parent, type, mod, allowed_mod, Modifiers.PRIVATE,
5429 public override AttributeTargets AttributeTargets {
5431 return AttributeTargets.Field;
5435 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
5437 if (a.Type == TypeManager.marshal_as_attr_type) {
5438 UnmanagedMarshal marshal = a.GetMarshal (this);
5439 if (marshal != null) {
5440 FieldBuilder.SetMarshal (marshal);
5445 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
5446 a.Error_InvalidSecurityParent ();
5450 FieldBuilder.SetCustomAttribute (cb);
5454 // Whether this field has an initializer.
5456 public bool HasInitializer {
5458 return init != null;
5462 protected readonly Object init;
5464 Expression init_expr;
5465 bool init_expr_initialized = false;
5467 protected override bool CheckGenericOverride (MethodInfo method, string name)
5473 // Resolves and returns the field initializer.
5475 public Expression GetInitializerExpression (EmitContext ec)
5477 if (init_expr_initialized)
5481 if (init is Expression)
5482 e = (Expression) init;
5484 e = new ArrayCreation (Type, "", (ArrayList)init, Location);
5486 ec.IsFieldInitializer = true;
5487 e = e.DoResolve (ec);
5488 ec.IsFieldInitializer = false;
5491 init_expr_initialized = true;
5496 protected override bool CheckBase ()
5498 if (!base.CheckBase ())
5505 conflict_symbol = Parent.FindMemberWithSameName (Name, false);
5506 if (conflict_symbol == null) {
5507 if ((RootContext.WarningLevel >= 4) && ((ModFlags & Modifiers.NEW) != 0)) {
5508 Report.Warning (109, Location, "The member '{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError (Parent));
5513 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0) {
5514 Report.SymbolRelatedToPreviousError (conflict_symbol);
5515 Report.Warning (108, Location, "The keyword new is required on '{0}' because it hides inherited member", GetSignatureForError (Parent));
5521 protected override bool DoDefine (DeclSpace ds)
5523 if (!base.DoDefine (ds))
5526 if (MemberType == TypeManager.void_type) {
5527 Report.Error (1547, Location,
5528 "Keyword 'void' cannot be used in this context");
5534 public override string GetSignatureForError ()
5536 if (FieldBuilder == null) {
5537 return base.GetSignatureForError (Parent);
5539 return TypeManager.GetFullNameSignature (FieldBuilder);
5542 public override string[] ValidAttributeTargets {
5544 return attribute_targets;
5548 protected override bool VerifyClsCompliance (DeclSpace ds)
5550 if (!base.VerifyClsCompliance (ds))
5553 if (FieldBuilder == null) {
5557 if (!AttributeTester.IsClsCompliant (FieldBuilder.FieldType)) {
5558 Report.Error (3003, Location, "Type of '{0}' is not CLS-compliant", GetSignatureForError ());
5564 public void SetAssigned ()
5566 status |= Status.ASSIGNED;
5570 public abstract class FieldMember: FieldBase
5574 protected FieldMember (TypeContainer parent, Expression type, int mod,
5575 int allowed_mod, MemberName name, object init, Attributes attrs, Location loc)
5576 : base (parent, type, mod, allowed_mod, name, init, attrs, loc)
5580 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5582 if (a.Type == TypeManager.field_offset_attribute_type)
5584 status |= Status.HAS_OFFSET;
5586 if (!Parent.HasExplicitLayout) {
5587 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
5591 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
5592 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
5596 base.ApplyAttributeBuilder (a, cb);
5600 public override bool Define()
5602 EmitContext ec = Parent.EmitContext;
5604 throw new InternalErrorException ("FieldMember.Define called too early");
5606 bool old_unsafe = ec.InUnsafe;
5607 ec.InUnsafe = InUnsafe;
5608 TypeExpr texpr = Type.ResolveAsTypeTerminal (ec);
5609 ec.InUnsafe = old_unsafe;
5613 MemberType = texpr.Type;
5618 if (!Parent.AsAccessible (MemberType, ModFlags)) {
5619 Report.Error (52, Location,
5620 "Inconsistent accessibility: field type `" +
5621 TypeManager.CSharpName (MemberType) + "' is less " +
5622 "accessible than field `" + Name + "'");
5626 if (!IsTypePermitted ())
5629 if (MemberType.IsPointer && !UnsafeOK (Parent))
5635 public override void Emit ()
5637 if (Parent.HasExplicitLayout && ((status & Status.HAS_OFFSET) == 0) && (ModFlags & Modifiers.STATIC) == 0) {
5638 Report.Error (625, Location, "'{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute.", GetSignatureForError ());
5645 // Represents header string for documentation comment.
5647 public override string DocCommentHeader {
5648 get { return "F:"; }
5653 // The Field class is used to represents class/struct fields during parsing.
5655 public class Field : FieldMember {
5657 // Modifiers allowed in a class declaration
5659 const int AllowedModifiers =
5662 Modifiers.PROTECTED |
5663 Modifiers.INTERNAL |
5666 Modifiers.VOLATILE |
5670 public Field (TypeContainer parent, Expression type, int mod, string name,
5671 Object expr_or_array_init, Attributes attrs, Location loc)
5672 : base (parent, type, mod, AllowedModifiers, new MemberName (name),
5673 expr_or_array_init, attrs, loc)
5677 public override bool Define ()
5679 if (!base.Define ())
5682 if (RootContext.WarningLevel > 1){
5683 Type ptype = Parent.TypeBuilder.BaseType;
5685 // ptype is only null for System.Object while compiling corlib.
5687 TypeContainer.FindMembers (
5688 ptype, MemberTypes.Method,
5689 BindingFlags.Public |
5690 BindingFlags.Static | BindingFlags.Instance,
5691 System.Type.FilterName, Name);
5695 if ((ModFlags & Modifiers.VOLATILE) != 0){
5696 if (!MemberType.IsClass){
5697 Type vt = MemberType;
5699 if (TypeManager.IsEnumType (vt))
5700 vt = TypeManager.EnumToUnderlying (MemberType);
5702 if (!((vt == TypeManager.bool_type) ||
5703 (vt == TypeManager.sbyte_type) ||
5704 (vt == TypeManager.byte_type) ||
5705 (vt == TypeManager.short_type) ||
5706 (vt == TypeManager.ushort_type) ||
5707 (vt == TypeManager.int32_type) ||
5708 (vt == TypeManager.uint32_type) ||
5709 (vt == TypeManager.char_type) ||
5710 (vt == TypeManager.float_type) ||
5711 (!vt.IsValueType))){
5713 677, Location, Parent.MakeName (Name) +
5714 " A volatile field can not be of type `" +
5715 TypeManager.CSharpName (vt) + "'");
5720 if ((ModFlags & Modifiers.READONLY) != 0){
5723 "A field can not be both volatile and readonly");
5728 FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
5730 if (Parent.Kind == Kind.Struct &&
5731 ((fa & FieldAttributes.Static) == 0) &&
5732 MemberType == Parent.TypeBuilder &&
5733 !TypeManager.IsBuiltinType (MemberType)){
5734 Report.Error (523, Location, "Struct member `" + Parent.Name + "." + Name +
5735 "' causes a cycle in the structure layout");
5740 FieldBuilder = Parent.TypeBuilder.DefineField (
5741 Name, MemberType, Modifiers.FieldAttr (ModFlags));
5743 TypeManager.RegisterFieldBase (FieldBuilder, this);
5745 catch (ArgumentException) {
5746 Report.Warning (-24, Location, "The Microsoft runtime is unable to use [void|void*] as a field type, try using the Mono runtime.");
5753 public override void Emit ()
5755 if (OptAttributes != null) {
5756 EmitContext ec = new EmitContext (
5757 Parent, Location, null, FieldBuilder.FieldType,
5759 OptAttributes.Emit (ec, this);
5767 // `set' and `get' accessors are represented with an Accessor.
5769 public class Accessor {
5771 // Null if the accessor is empty, or a Block if not
5773 public const int AllowedModifiers =
5775 Modifiers.PROTECTED |
5776 Modifiers.INTERNAL |
5779 public ToplevelBlock Block;
5780 public Attributes Attributes;
5781 public Location Location;
5782 public int ModFlags;
5784 public Accessor (ToplevelBlock b, int mod, Attributes attrs, Location loc)
5789 ModFlags = Modifiers.Check (AllowedModifiers, mod, 0, loc);
5794 // Ooouh Martin, templates are missing here.
5795 // When it will be possible move here a lot of child code and template method type.
5796 public abstract class AbstractPropertyEventMethod: MemberCore, IMethodData {
5797 protected MethodData method_data;
5798 protected ToplevelBlock block;
5799 protected ListDictionary declarative_security;
5801 // The accessor are created event if they are not wanted.
5802 // But we need them because their names are reserved.
5803 // Field says whether accessor will be emited or not
5804 public readonly bool IsDummy;
5806 protected readonly string prefix;
5808 ReturnParameter return_attributes;
5810 public AbstractPropertyEventMethod (MemberBase member, string prefix)
5811 : base (null, SetupName (prefix, member), null, member.Location)
5813 this.prefix = prefix;
5817 public AbstractPropertyEventMethod (MemberBase member, Accessor accessor,
5819 : base (null, SetupName (prefix, member),
5820 accessor.Attributes, accessor.Location)
5822 this.prefix = prefix;
5823 this.block = accessor.Block;
5826 static MemberName SetupName (string prefix, MemberBase member)
5828 MemberName name = member.MemberName.Clone ();
5829 name.Name = prefix + member.ShortName;
5833 public void UpdateName (MemberBase member)
5835 MemberName.Name = prefix + member.ShortName;
5838 #region IMethodData Members
5840 public ToplevelBlock Block {
5850 public CallingConventions CallingConventions {
5852 return CallingConventions.Standard;
5856 public bool IsExcluded (EmitContext ec)
5861 GenericMethod IMethodData.GenericMethod {
5867 public MemberName MethodName {
5873 public abstract ObsoleteAttribute GetObsoleteAttribute ();
5874 public abstract Type[] ParameterTypes { get; }
5875 public abstract Type ReturnType { get; }
5876 public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
5880 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
5882 if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
5883 a.Type == TypeManager.conditional_attribute_type) {
5884 Report.Error (1667, a.Location, "'{0}' is not valid on property or event accessors. It is valid on {1} declarations only", TypeManager.CSharpName (a.Type), a.GetValidTargets ());
5888 if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
5889 if (declarative_security == null)
5890 declarative_security = new ListDictionary ();
5891 a.ExtractSecurityPermissionSet (declarative_security);
5895 if (a.Target == AttributeTargets.Method) {
5896 method_data.MethodBuilder.SetCustomAttribute (cb);
5900 if (a.Target == AttributeTargets.ReturnValue) {
5901 if (return_attributes == null)
5902 return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
5904 return_attributes.ApplyAttributeBuilder (a, cb);
5908 ApplyToExtraTarget (a, cb);
5911 virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
5913 System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
5916 public override bool Define()
5918 throw new NotSupportedException ();
5921 public virtual void Emit (TypeContainer container)
5923 method_data.Emit (container, this);
5925 if (declarative_security != null) {
5926 foreach (DictionaryEntry de in declarative_security) {
5927 method_data.MethodBuilder.AddDeclarativeSecurity ((SecurityAction)de.Key, (PermissionSet)de.Value);
5934 public override bool IsClsCompliaceRequired(DeclSpace ds)
5939 public bool IsDuplicateImplementation (MethodCore method)
5941 if (Name != method.Name)
5944 Type[] param_types = method.ParameterTypes;
5946 if (param_types.Length != ParameterTypes.Length)
5949 for (int i = 0; i < param_types.Length; i++)
5950 if (param_types [i] != ParameterTypes [i])
5953 Report.SymbolRelatedToPreviousError (method);
5954 Report.Error (111, Location, "Type '{0}' already defines a member called '{1}' with " +
5955 "the same parameter types", Parent.Name, Name);
5959 public new Location Location {
5961 return base.Location;
5966 // Represents header string for documentation comment.
5968 public override string DocCommentHeader {
5969 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
5972 protected override void VerifyObsoleteAttribute()
5979 // Properties and Indexers both generate PropertyBuilders, we use this to share
5980 // their common bits.
5982 abstract public class PropertyBase : MethodCore {
5984 public class GetMethod: PropertyMethod
5986 static string[] attribute_targets = new string [] { "method", "return" };
5988 public GetMethod (MethodCore method):
5989 base (method, "get_")
5993 public GetMethod (MethodCore method, Accessor accessor):
5994 base (method, accessor, "get_")
5998 public override MethodBuilder Define(TypeContainer container)
6000 base.Define (container);
6002 method_data = new MethodData (method, method.ParameterInfo, ModFlags, flags, this);
6004 if (!method_data.Define (container))
6007 return method_data.MethodBuilder;
6010 public override string GetSignatureForError (TypeContainer tc)
6012 return String.Concat (base.GetSignatureForError (tc), ".get");
6015 public override Type ReturnType {
6017 return method.MemberType;
6021 public override string[] ValidAttributeTargets {
6023 return attribute_targets;
6028 public class SetMethod: PropertyMethod {
6030 static string[] attribute_targets = new string [] { "method", "param", "return" };
6031 ImplicitParameter param_attr;
6033 public SetMethod (MethodCore method):
6034 base (method, "set_")
6038 public SetMethod (MethodCore method, Accessor accessor):
6039 base (method, accessor, "set_")
6043 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6045 if (a.Target == AttributeTargets.Parameter) {
6046 if (param_attr == null)
6047 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6049 param_attr.ApplyAttributeBuilder (a, cb);
6053 base.ApplyAttributeBuilder (a, cb);
6056 protected virtual InternalParameters GetParameterInfo (EmitContext ec)
6058 Parameter [] parms = new Parameter [1];
6059 parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null);
6060 Parameters parameters = new Parameters (parms, null, method.Location);
6061 Type [] types = parameters.GetParameterInfo (ec);
6062 return new InternalParameters (types, parameters);
6065 public override MethodBuilder Define(TypeContainer container)
6067 if (container.EmitContext == null)
6068 throw new InternalErrorException ("SetMethod.Define called too early");
6070 base.Define (container);
6072 method_data = new MethodData (method, GetParameterInfo (container.EmitContext), ModFlags, flags, this);
6074 if (!method_data.Define (container))
6077 return method_data.MethodBuilder;
6080 public override string GetSignatureForError (TypeContainer tc)
6082 return String.Concat (base.GetSignatureForError (tc), ".set");
6085 public override Type[] ParameterTypes {
6087 return new Type[] { method.MemberType };
6091 public override Type ReturnType {
6093 return TypeManager.void_type;
6097 public override string[] ValidAttributeTargets {
6099 return attribute_targets;
6104 static string[] attribute_targets = new string [] { "property" };
6106 public abstract class PropertyMethod: AbstractPropertyEventMethod {
6107 protected readonly MethodCore method;
6108 protected MethodAttributes flags;
6110 public PropertyMethod (MethodCore method, string prefix)
6111 : base (method, prefix)
6113 this.method = method;
6116 public PropertyMethod (MethodCore method, Accessor accessor, string prefix)
6117 : base (method, accessor, prefix)
6119 this.method = method;
6120 this.ModFlags = accessor.ModFlags;
6122 if (accessor.ModFlags != 0 && RootContext.Version == LanguageVersion.ISO_1) {
6123 Report.FeatureIsNotStandardized (Location, "accessor modifiers");
6124 Environment.Exit (1);
6128 public override AttributeTargets AttributeTargets {
6130 return AttributeTargets.Method;
6134 public override bool IsClsCompliaceRequired(DeclSpace ds)
6136 return method.IsClsCompliaceRequired (ds);
6139 public InternalParameters ParameterInfo
6142 return method_data.ParameterInfo;
6146 public virtual MethodBuilder Define (TypeContainer container)
6149 // Check for custom access modifier
6151 if (ModFlags == 0) {
6152 ModFlags = method.ModFlags;
6153 flags = method.flags;
6155 CheckModifiers (container, ModFlags);
6156 ModFlags |= (method.ModFlags & (~Modifiers.Accessibility));
6157 flags = Modifiers.MethodAttr (ModFlags);
6158 flags |= (method.flags & (~MethodAttributes.MemberAccessMask));
6165 public override Type[] ParameterTypes {
6167 return TypeManager.NoTypes;
6171 public override EmitContext CreateEmitContext (TypeContainer tc,
6174 return new EmitContext (
6175 tc, method.ds, method.Location, ig, ReturnType,
6176 method.ModFlags, false);
6179 public override ObsoleteAttribute GetObsoleteAttribute ()
6181 return method.GetObsoleteAttribute (method.ds);
6184 public override string GetSignatureForError (TypeContainer tc)
6186 return String.Concat (tc.Name, '.', method.Name);
6189 void CheckModifiers (TypeContainer container, int modflags)
6192 int mflags = method.ModFlags & Modifiers.Accessibility;
6194 if ((mflags & Modifiers.PUBLIC) != 0) {
6195 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL | Modifiers.PRIVATE;
6197 else if ((mflags & Modifiers.PROTECTED) != 0) {
6198 if ((mflags & Modifiers.INTERNAL) != 0)
6199 flags |= Modifiers.PROTECTED | Modifiers.INTERNAL;
6201 flags |= Modifiers.PRIVATE;
6203 else if ((mflags & Modifiers.INTERNAL) != 0)
6204 flags |= Modifiers.PRIVATE;
6206 if ((mflags == modflags) || (modflags & (~flags)) != 0)
6207 Report.Error (273, Location, "{0}: accessibility modifier must be more restrictive than the property or indexer",
6208 GetSignatureForError (container));
6212 public PropertyMethod Get, Set;
6213 public PropertyBuilder PropertyBuilder;
6214 public MethodBuilder GetBuilder, SetBuilder;
6216 protected EmitContext ec;
6218 public PropertyBase (TypeContainer parent, Expression type, int mod_flags,
6219 int allowed_mod, bool is_iface, MemberName name,
6220 Parameters parameters, Attributes attrs,
6222 : base (parent, null, type, mod_flags, allowed_mod, is_iface, name,
6223 attrs, parameters, loc)
6227 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6229 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6230 a.Error_InvalidSecurityParent ();
6234 PropertyBuilder.SetCustomAttribute (cb);
6237 public override AttributeTargets AttributeTargets {
6239 return AttributeTargets.Property;
6243 public override bool Define ()
6245 if (!DoDefine (Parent))
6248 if (!IsTypePermitted ())
6254 protected override bool DoDefine (DeclSpace ds)
6256 if (!base.DoDefine (ds))
6260 // Accessors modifiers check
6262 if (Get.ModFlags != 0 && Set.ModFlags != 0) {
6263 Report.Error (274, Location, "'{0}': cannot specify accessibility modifiers for both accessors of the property or indexer.",
6264 GetSignatureForError ());
6268 if ((Get.IsDummy || Set.IsDummy)
6269 && (Get.ModFlags != 0 || Set.ModFlags != 0) && (ModFlags & Modifiers.OVERRIDE) == 0) {
6270 Report.Error (276, Location,
6271 "'{0}': accessibility modifiers on accessors may only be used if the property or indexer has both a get and a set accessor.",
6272 GetSignatureForError ());
6276 if (MemberType.IsAbstract && MemberType.IsSealed) {
6277 Report.Error (722, Location, Error722, TypeManager.CSharpName (MemberType));
6281 ec = new EmitContext (Parent, Location, null, MemberType, ModFlags);
6285 public override string GetSignatureForError()
6287 if (PropertyBuilder == null)
6288 return GetSignatureForError (Parent);
6290 return TypeManager.CSharpSignature (PropertyBuilder, false);
6294 protected override bool CheckForDuplications ()
6296 ArrayList ar = Parent.Indexers;
6298 int arLen = ar.Count;
6300 for (int i = 0; i < arLen; i++) {
6301 Indexer m = (Indexer) ar [i];
6302 if (IsDuplicateImplementation (m))
6307 ar = Parent.Properties;
6309 int arLen = ar.Count;
6311 for (int i = 0; i < arLen; i++) {
6312 Property m = (Property) ar [i];
6313 if (IsDuplicateImplementation (m))
6321 // TODO: rename to Resolve......
6322 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
6324 PropertyInfo parent_property = container.ParentCache.FindMemberToOverride (
6325 container.TypeBuilder, Name, ParameterTypes, true) as PropertyInfo;
6327 if (parent_property == null)
6330 parent_ret_type = parent_property.PropertyType;
6331 MethodInfo get_accessor = parent_property.GetGetMethod (true);
6332 MethodInfo set_accessor = parent_property.GetSetMethod (true);
6333 MethodAttributes get_accessor_access, set_accessor_access;
6335 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
6336 if (Get != null && !Get.IsDummy && get_accessor == null) {
6337 Report.SymbolRelatedToPreviousError (parent_property);
6338 Report.Error (545, Location, "'{0}': cannot override because '{1}' does not have an overridable get accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
6341 if (Set != null && !Set.IsDummy && set_accessor == null) {
6342 Report.SymbolRelatedToPreviousError (parent_property);
6343 Report.Error (546, Location, "'{0}': cannot override because '{1}' does not have an overridable set accessor", GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
6348 // Check parent accessors access
6350 get_accessor_access = set_accessor_access = 0;
6351 if ((ModFlags & Modifiers.NEW) == 0) {
6352 if (get_accessor != null) {
6353 MethodAttributes get_flags = Modifiers.MethodAttr (Get.ModFlags != 0 ? Get.ModFlags : ModFlags);
6354 get_accessor_access = (get_accessor.Attributes & MethodAttributes.MemberAccessMask);
6356 if (!Get.IsDummy && !CheckAccessModifiers (get_flags & MethodAttributes.MemberAccessMask, get_accessor_access, get_accessor))
6357 Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
6358 GetSignatureForError (), TypeManager.GetFullNameSignature (parent_property));
6361 if (set_accessor != null) {
6362 MethodAttributes set_flags = Modifiers.MethodAttr (Set.ModFlags != 0 ? Set.ModFlags : ModFlags);
6363 set_accessor_access = (set_accessor.Attributes & MethodAttributes.MemberAccessMask);
6365 if (!Set.IsDummy && !CheckAccessModifiers (set_flags & MethodAttributes.MemberAccessMask, set_accessor_access, set_accessor))
6366 Report.Error (507, Location, "'{0}' can't change the access modifiers when overriding inherited member '{1}'",
6367 GetSignatureForError (container), TypeManager.GetFullNameSignature (parent_property));
6372 // Get the less restrictive access
6374 return get_accessor_access > set_accessor_access ? get_accessor : set_accessor;
6377 public override void Emit ()
6380 // The PropertyBuilder can be null for explicit implementations, in that
6381 // case, we do not actually emit the ".property", so there is nowhere to
6382 // put the attribute
6384 if (PropertyBuilder != null && OptAttributes != null)
6385 OptAttributes.Emit (ec, this);
6397 /// Tests whether accessors are not in collision with some method (CS0111)
6399 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6401 return Get.IsDuplicateImplementation (mc) || Set.IsDuplicateImplementation (mc);
6404 protected override void UpdateMemberName ()
6406 base.UpdateMemberName ();
6408 Get.UpdateName (this);
6409 Set.UpdateName (this);
6413 public override string[] ValidAttributeTargets {
6415 return attribute_targets;
6420 // Represents header string for documentation comment.
6422 public override string DocCommentHeader {
6423 get { return "P:"; }
6427 public class Property : PropertyBase, IIteratorContainer {
6428 const int AllowedModifiers =
6431 Modifiers.PROTECTED |
6432 Modifiers.INTERNAL |
6436 Modifiers.OVERRIDE |
6437 Modifiers.ABSTRACT |
6440 Modifiers.METHOD_YIELDS |
6443 const int AllowedInterfaceModifiers =
6446 public Property (TypeContainer parent, Expression type, int mod_flags,
6447 bool is_iface, MemberName name, Attributes attrs,
6448 Accessor get_block, Accessor set_block, Location loc)
6449 : base (parent, type, mod_flags,
6450 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6451 is_iface, name, Parameters.EmptyReadOnlyParameters, attrs,
6454 if (get_block == null)
6455 Get = new GetMethod (this);
6457 Get = new GetMethod (this, get_block);
6459 if (set_block == null)
6460 Set = new SetMethod (this);
6462 Set = new SetMethod (this, set_block);
6465 public override bool Define ()
6467 if (!DoDefineBase ())
6470 if (!base.Define ())
6476 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
6480 GetBuilder = Get.Define (Parent);
6481 if (GetBuilder == null)
6485 // Setup iterator if we are one
6487 if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
6488 Iterator iterator = new Iterator (
6489 Parent, "get", MemberType,
6490 TypeManager.NoTypes, Get.ParameterInfo,
6491 ModFlags, Get.Block, Location);
6493 if (!iterator.DefineIterator ())
6495 Get.Block = iterator.Block;
6500 SetBuilder = Set.Define (Parent);
6501 if (SetBuilder == null)
6504 SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
6507 // FIXME - PropertyAttributes.HasDefault ?
6509 PropertyAttributes prop_attr = PropertyAttributes.None;
6511 prop_attr |= PropertyAttributes.RTSpecialName |
6512 PropertyAttributes.SpecialName;
6514 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
6515 Name, prop_attr, MemberType, null);
6518 PropertyBuilder.SetGetMethod (GetBuilder);
6521 PropertyBuilder.SetSetMethod (SetBuilder);
6523 TypeManager.RegisterProperty (PropertyBuilder, GetBuilder, SetBuilder);
6527 public void SetYields ()
6529 ModFlags |= Modifiers.METHOD_YIELDS;
6534 /// Gigantic workaround for lameness in SRE follows :
6535 /// This class derives from EventInfo and attempts to basically
6536 /// wrap around the EventBuilder so that FindMembers can quickly
6537 /// return this in it search for members
6539 public class MyEventBuilder : EventInfo {
6542 // We use this to "point" to our Builder which is
6543 // not really a MemberInfo
6545 EventBuilder MyBuilder;
6548 // We "catch" and wrap these methods
6550 MethodInfo raise, remove, add;
6552 EventAttributes attributes;
6553 Type declaring_type, reflected_type, event_type;
6558 public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
6560 MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
6562 // And now store the values in our own fields.
6564 declaring_type = type_builder;
6566 reflected_type = type_builder;
6568 attributes = event_attr;
6571 this.event_type = event_type;
6575 // Methods that you have to override. Note that you only need
6576 // to "implement" the variants that take the argument (those are
6577 // the "abstract" methods, the others (GetAddMethod()) are
6580 public override MethodInfo GetAddMethod (bool nonPublic)
6585 public override MethodInfo GetRemoveMethod (bool nonPublic)
6590 public override MethodInfo GetRaiseMethod (bool nonPublic)
6596 // These methods make "MyEventInfo" look like a Builder
6598 public void SetRaiseMethod (MethodBuilder raiseMethod)
6600 raise = raiseMethod;
6601 MyBuilder.SetRaiseMethod (raiseMethod);
6604 public void SetRemoveOnMethod (MethodBuilder removeMethod)
6606 remove = removeMethod;
6607 MyBuilder.SetRemoveOnMethod (removeMethod);
6610 public void SetAddOnMethod (MethodBuilder addMethod)
6613 MyBuilder.SetAddOnMethod (addMethod);
6616 public void SetCustomAttribute (CustomAttributeBuilder cb)
6618 MyBuilder.SetCustomAttribute (cb);
6621 public override object [] GetCustomAttributes (bool inherit)
6623 // FIXME : There's nothing which can be seemingly done here because
6624 // we have no way of getting at the custom attribute objects of the
6629 public override object [] GetCustomAttributes (Type t, bool inherit)
6631 // FIXME : Same here !
6635 public override bool IsDefined (Type t, bool b)
6640 public override EventAttributes Attributes {
6646 public override string Name {
6652 public override Type DeclaringType {
6654 return declaring_type;
6658 public override Type ReflectedType {
6660 return reflected_type;
6664 public Type EventType {
6670 public void SetUsed ()
6672 if (my_event != null)
6673 my_event.status = (FieldBase.Status.ASSIGNED | FieldBase.Status.USED);
6678 /// For case when event is declared like property (with add and remove accessors).
6680 public class EventProperty: Event {
6682 static string[] attribute_targets = new string [] { "event", "property" };
6684 public EventProperty (TypeContainer parent, Expression type, int mod_flags,
6685 bool is_iface, MemberName name, Object init,
6686 Attributes attrs, Accessor add, Accessor remove,
6688 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6690 Add = new AddDelegateMethod (this, add);
6691 Remove = new RemoveDelegateMethod (this, remove);
6693 // For this event syntax we don't report error CS0067
6694 // because it is hard to do it.
6698 public override string[] ValidAttributeTargets {
6700 return attribute_targets;
6706 /// Event is declared like field.
6708 public class EventField: Event {
6710 static string[] attribute_targets = new string [] { "event", "field", "method" };
6711 static string[] attribute_targets_interface = new string[] { "event", "method" };
6713 public EventField (TypeContainer parent, Expression type, int mod_flags,
6714 bool is_iface, MemberName name, Object init,
6715 Attributes attrs, Location loc)
6716 : base (parent, type, mod_flags, is_iface, name, init, attrs, loc)
6718 Add = new AddDelegateMethod (this);
6719 Remove = new RemoveDelegateMethod (this);
6722 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6724 if (a.Target == AttributeTargets.Field) {
6725 FieldBuilder.SetCustomAttribute (cb);
6729 if (a.Target == AttributeTargets.Method) {
6730 Add.ApplyAttributeBuilder (a, cb);
6731 Remove.ApplyAttributeBuilder (a, cb);
6735 base.ApplyAttributeBuilder (a, cb);
6738 public override string[] ValidAttributeTargets {
6740 return IsInterface ? attribute_targets_interface : attribute_targets;
6745 public abstract class Event : FieldBase {
6747 protected sealed class AddDelegateMethod: DelegateMethod
6750 public AddDelegateMethod (Event method):
6751 base (method, "add_")
6755 public AddDelegateMethod (Event method, Accessor accessor):
6756 base (method, accessor, "add_")
6760 protected override MethodInfo DelegateMethodInfo {
6762 return TypeManager.delegate_combine_delegate_delegate;
6768 protected sealed class RemoveDelegateMethod: DelegateMethod
6770 public RemoveDelegateMethod (Event method):
6771 base (method, "remove_")
6775 public RemoveDelegateMethod (Event method, Accessor accessor):
6776 base (method, accessor, "remove_")
6780 protected override MethodInfo DelegateMethodInfo {
6782 return TypeManager.delegate_remove_delegate_delegate;
6788 public abstract class DelegateMethod: AbstractPropertyEventMethod
6790 protected readonly Event method;
6791 ImplicitParameter param_attr;
6793 static string[] attribute_targets = new string [] { "method", "param", "return" };
6795 public DelegateMethod (Event method, string prefix)
6796 : base (method, prefix)
6798 this.method = method;
6801 public DelegateMethod (Event method, Accessor accessor, string prefix)
6802 : base (method, accessor, prefix)
6804 this.method = method;
6807 protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
6809 if (a.Target == AttributeTargets.Parameter) {
6810 if (param_attr == null)
6811 param_attr = new ImplicitParameter (method_data.MethodBuilder);
6813 param_attr.ApplyAttributeBuilder (a, cb);
6817 base.ApplyAttributeBuilder (a, cb);
6820 public override AttributeTargets AttributeTargets {
6822 return AttributeTargets.Method;
6826 public override bool IsClsCompliaceRequired(DeclSpace ds)
6828 return method.IsClsCompliaceRequired (ds);
6831 public MethodBuilder Define (TypeContainer container, InternalParameters ip)
6833 method_data = new MethodData (method, ip, method.ModFlags,
6834 method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
6836 if (!method_data.Define (container))
6839 MethodBuilder mb = method_data.MethodBuilder;
6840 mb.DefineParameter (1, ParameterAttributes.None, "value");
6845 public override void Emit (TypeContainer tc)
6847 if (block != null) {
6852 ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
6853 EmitContext ec = CreateEmitContext (tc, ig);
6854 FieldInfo field_info = (FieldInfo)method.FieldBuilder;
6856 method_data.MethodBuilder.SetImplementationFlags (MethodImplAttributes.Synchronized);
6857 if ((method.ModFlags & Modifiers.STATIC) != 0) {
6858 ig.Emit (OpCodes.Ldsfld, field_info);
6859 ig.Emit (OpCodes.Ldarg_0);
6860 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6861 ig.Emit (OpCodes.Castclass, method.MemberType);
6862 ig.Emit (OpCodes.Stsfld, field_info);
6864 ig.Emit (OpCodes.Ldarg_0);
6865 ig.Emit (OpCodes.Ldarg_0);
6866 ig.Emit (OpCodes.Ldfld, field_info);
6867 ig.Emit (OpCodes.Ldarg_1);
6868 ig.Emit (OpCodes.Call, DelegateMethodInfo);
6869 ig.Emit (OpCodes.Castclass, method.MemberType);
6870 ig.Emit (OpCodes.Stfld, field_info);
6872 ig.Emit (OpCodes.Ret);
6875 protected abstract MethodInfo DelegateMethodInfo { get; }
6877 public override Type[] ParameterTypes {
6879 return new Type[] { method.MemberType };
6883 public override Type ReturnType {
6885 return TypeManager.void_type;
6889 public override EmitContext CreateEmitContext (TypeContainer tc,
6892 return new EmitContext (
6893 tc, method.Parent, Location, ig, ReturnType,
6894 method.ModFlags, false);
6897 public override string GetSignatureForError (TypeContainer tc)
6899 return String.Concat (tc.Name, '.', method.Name);
6902 public override ObsoleteAttribute GetObsoleteAttribute ()
6904 return method.GetObsoleteAttribute (method.Parent);
6907 public override string[] ValidAttributeTargets {
6909 return attribute_targets;
6915 const int AllowedModifiers =
6918 Modifiers.PROTECTED |
6919 Modifiers.INTERNAL |
6924 Modifiers.OVERRIDE |
6928 const int AllowedInterfaceModifiers =
6931 public DelegateMethod Add, Remove;
6932 public MyEventBuilder EventBuilder;
6933 public MethodBuilder AddBuilder, RemoveBuilder;
6935 MethodData AddData, RemoveData;
6937 public Event (TypeContainer parent, Expression type, int mod_flags,
6938 bool is_iface, MemberName name, Object init, Attributes attrs,
6940 : base (parent, type, mod_flags,
6941 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
6942 name, init, attrs, loc)
6944 IsInterface = is_iface;
6947 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
6949 if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
6950 a.Error_InvalidSecurityParent ();
6954 EventBuilder.SetCustomAttribute (cb);
6957 public bool AreAccessorsDuplicateImplementation (MethodCore mc)
6959 return Add.IsDuplicateImplementation (mc) || Remove.IsDuplicateImplementation (mc);
6962 public override AttributeTargets AttributeTargets {
6964 return AttributeTargets.Event;
6968 public override bool Define ()
6970 EventAttributes e_attr;
6971 e_attr = EventAttributes.None;
6973 if (!DoDefineBase ())
6976 if (!DoDefine (Parent))
6979 if (init != null && ((ModFlags & Modifiers.ABSTRACT) != 0)){
6980 Report.Error (74, Location, "'" + Parent.Name + "." + Name +
6981 "': abstract event can not have an initializer");
6985 if (!TypeManager.IsDelegateType (MemberType)) {
6986 Report.Error (66, Location, "'" + Parent.Name + "." + Name +
6987 "' : event must be of a delegate type");
6991 EmitContext ec = Parent.EmitContext;
6993 throw new InternalErrorException ("Event.Define called too early?");
6994 bool old_unsafe = ec.InUnsafe;
6995 ec.InUnsafe = InUnsafe;
6997 Parameter [] parms = new Parameter [1];
6998 parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
6999 Parameters parameters = new Parameters (parms, null, Location);
7000 Type [] types = parameters.GetParameterInfo (ec);
7001 InternalParameters ip = new InternalParameters (types, parameters);
7003 ec.InUnsafe = old_unsafe;
7009 // Now define the accessors
7012 AddBuilder = Add.Define (Parent, ip);
7013 if (AddBuilder == null)
7016 RemoveBuilder = Remove.Define (Parent, ip);
7017 if (RemoveBuilder == null)
7020 EventBuilder = new MyEventBuilder (this, Parent.TypeBuilder, Name, e_attr, MemberType);
7022 if (Add.Block == null && Remove.Block == null && !IsInterface) {
7023 FieldBuilder = Parent.TypeBuilder.DefineField (
7025 FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
7026 TypeManager.RegisterPrivateFieldOfEvent (
7027 (EventInfo) EventBuilder, FieldBuilder);
7028 TypeManager.RegisterFieldBase (FieldBuilder, this);
7031 EventBuilder.SetAddOnMethod (AddBuilder);
7032 EventBuilder.SetRemoveOnMethod (RemoveBuilder);
7034 TypeManager.RegisterEvent (EventBuilder, AddBuilder, RemoveBuilder);
7038 protected override bool CheckBase ()
7040 if (!base.CheckBase ())
7043 if (conflict_symbol != null && (ModFlags & Modifiers.NEW) == 0) {
7044 if (!(conflict_symbol is EventInfo)) {
7045 Report.SymbolRelatedToPreviousError (conflict_symbol);
7046 Report.Error (72, Location, "Event '{0}' can override only event", GetSignatureForError (Parent));
7054 public override void Emit ()
7056 if (OptAttributes != null) {
7057 EmitContext ec = new EmitContext (
7058 Parent, Location, null, MemberType, ModFlags);
7059 OptAttributes.Emit (ec, this);
7064 Remove.Emit (Parent);
7070 public override string GetSignatureForError ()
7072 if (EventBuilder == null)
7073 return base.GetSignatureForError (Parent);
7075 return TypeManager.GetFullNameSignature (EventBuilder);
7079 // Represents header string for documentation comment.
7081 public override string DocCommentHeader {
7082 get { return "E:"; }
7087 public class Indexer : PropertyBase {
7089 class GetIndexerMethod: GetMethod
7091 public GetIndexerMethod (MethodCore method):
7096 public GetIndexerMethod (MethodCore method, Accessor accessor):
7097 base (method, accessor)
7101 public override Type[] ParameterTypes {
7103 return method.ParameterTypes;
7108 class SetIndexerMethod: SetMethod
7110 readonly Parameters parameters;
7112 public SetIndexerMethod (MethodCore method):
7117 public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
7118 base (method, accessor)
7120 this.parameters = parameters;
7123 public override Type[] ParameterTypes {
7125 int top = method.ParameterTypes.Length;
7126 Type [] set_pars = new Type [top + 1];
7127 method.ParameterTypes.CopyTo (set_pars, 0);
7128 set_pars [top] = method.MemberType;
7133 protected override InternalParameters GetParameterInfo (EmitContext ec)
7135 Parameter [] fixed_parms = parameters.FixedParameters;
7137 if (fixed_parms == null){
7138 throw new Exception ("We currently do not support only array arguments in an indexer at: " + method.Location);
7139 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7140 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7142 // Here is the problem: the `value' parameter has
7143 // to come *after* the array parameter in the declaration
7145 // X (object [] x, Type value)
7148 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7149 // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
7153 Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
7155 fixed_parms.CopyTo (tmp, 0);
7156 tmp [fixed_parms.Length] = new Parameter (
7157 method.Type, "value", Parameter.Modifier.NONE, null);
7159 Parameters set_formal_params = new Parameters (tmp, null, method.Location);
7160 Type [] types = set_formal_params.GetParameterInfo (ec);
7162 return new InternalParameters (types, set_formal_params);
7166 const int AllowedModifiers =
7169 Modifiers.PROTECTED |
7170 Modifiers.INTERNAL |
7174 Modifiers.OVERRIDE |
7179 const int AllowedInterfaceModifiers =
7183 // Are we implementing an interface ?
7185 public Indexer (TypeContainer parent, Expression type, MemberName name, int mod,
7186 bool is_iface, Parameters parameters, Attributes attrs,
7187 Accessor get_block, Accessor set_block, Location loc)
7188 : base (parent, type, mod,
7189 is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
7190 is_iface, name, parameters, attrs, loc)
7192 if (get_block == null)
7193 Get = new GetIndexerMethod (this);
7195 Get = new GetIndexerMethod (this, get_block);
7197 if (set_block == null)
7198 Set = new SetIndexerMethod (this);
7200 Set = new SetIndexerMethod (this, parameters, set_block);
7203 public override bool Define ()
7205 PropertyAttributes prop_attr =
7206 PropertyAttributes.RTSpecialName |
7207 PropertyAttributes.SpecialName;
7209 if (!DoDefineBase ())
7212 if (!base.Define ())
7215 if (OptAttributes != null) {
7216 Attribute indexer_attr = OptAttributes.Search (TypeManager.indexer_name_type, ec);
7217 if (indexer_attr != null) {
7218 // Remove the attribute from the list because it is not emitted
7219 OptAttributes.Attrs.Remove (indexer_attr);
7221 ShortName = indexer_attr.GetIndexerAttributeValue (ec);
7223 if (IsExplicitImpl) {
7224 Report.Error (415, indexer_attr.Location,
7225 "The 'IndexerName' attribute is valid only on an" +
7226 "indexer that is not an explicit interface member declaration");
7230 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
7231 Report.Error (609, indexer_attr.Location,
7232 "Cannot set the 'IndexerName' attribute on an indexer marked override");
7236 if (!Tokenizer.IsValidIdentifier (ShortName)) {
7237 Report.Error (633, indexer_attr.Location,
7238 "The argument to the 'IndexerName' attribute must be a valid identifier");
7242 UpdateMemberName ();
7246 if (InterfaceType != null) {
7247 string parent_IndexerName = TypeManager.IndexerPropertyName (InterfaceType);
7248 if (parent_IndexerName != Name)
7249 ShortName = parent_IndexerName;
7250 UpdateMemberName ();
7253 if (!Parent.AddToMemberContainer (this, true) ||
7254 !Parent.AddToMemberContainer (Get, true) || !Parent.AddToMemberContainer (Set, true))
7260 flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
7262 GetBuilder = Get.Define (Parent);
7263 if (GetBuilder == null)
7268 SetBuilder = Set.Define (Parent);
7269 if (SetBuilder == null)
7274 // Now name the parameters
7276 Parameter [] p = Parameters.FixedParameters;
7278 if ((p [0].ModFlags & Parameter.Modifier.ISBYREF) != 0) {
7279 Report.Error (631, Location, "ref and out are not valid in this context");
7285 for (i = 0; i < p.Length; ++i) {
7287 GetBuilder.DefineParameter (
7288 i + 1, p [i].Attributes, p [i].Name);
7291 SetBuilder.DefineParameter (
7292 i + 1, p [i].Attributes, p [i].Name);
7296 SetBuilder.DefineParameter (
7297 i + 1, ParameterAttributes.None, "value");
7299 if (i != ParameterTypes.Length) {
7300 Parameter array_param = Parameters.ArrayParameter;
7302 SetBuilder.DefineParameter (
7303 i + 1, array_param.Attributes, array_param.Name);
7307 PropertyBuilder = Parent.TypeBuilder.DefineProperty (
7308 Name, prop_attr, MemberType, ParameterTypes);
7311 PropertyBuilder.SetGetMethod (GetBuilder);
7314 PropertyBuilder.SetSetMethod (SetBuilder);
7316 TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder, ParameterTypes);
7321 public override string GetSignatureForError ()
7323 if (PropertyBuilder == null)
7324 return GetSignatureForError (Parent);
7326 return TypeManager.CSharpSignature (PropertyBuilder, true);
7329 public override string GetSignatureForError(TypeContainer tc)
7331 return String.Concat (tc.Name, ".this[", Parameters.FixedParameters [0].TypeName.ToString (), ']');
7335 public class Operator : MethodCore, IIteratorContainer {
7337 const int AllowedModifiers =
7343 public enum OpType : byte {
7353 // Unary and Binary operators
7376 // Implicit and Explicit
7381 public readonly OpType OperatorType;
7382 public MethodBuilder OperatorMethodBuilder;
7384 public Method OperatorMethod;
7386 static string[] attribute_targets = new string [] { "method", "return" };
7388 public Operator (TypeContainer parent, OpType type, Expression ret_type,
7389 int mod_flags, Parameters parameters,
7390 ToplevelBlock block, Attributes attrs, Location loc)
7391 : base (parent, null, ret_type, mod_flags, AllowedModifiers, false,
7392 new MemberName ("op_" + type), attrs, parameters, loc)
7394 OperatorType = type;
7398 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
7400 OperatorMethod.ApplyAttributeBuilder (a, cb);
7403 public override AttributeTargets AttributeTargets {
7405 return AttributeTargets.Method;
7409 protected override bool CheckGenericOverride (MethodInfo method, string name)
7414 protected override bool CheckForDuplications()
7416 ArrayList ar = Parent.Operators;
7418 int arLen = ar.Count;
7420 for (int i = 0; i < arLen; i++) {
7421 Operator o = (Operator) ar [i];
7422 if (IsDuplicateImplementation (o))
7427 ar = Parent.Methods;
7429 int arLen = ar.Count;
7431 for (int i = 0; i < arLen; i++) {
7432 Method m = (Method) ar [i];
7433 if (IsDuplicateImplementation (m))
7441 public override bool Define ()
7443 const int RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
7444 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
7445 Report.Error (558, Location, "User defined operators '{0}' must be declared static and public", GetSignatureForError (Parent));
7452 OperatorMethod = new Method (
7453 Parent, null, Type, ModFlags, false, MemberName,
7454 Parameters, OptAttributes, Location);
7456 OperatorMethod.Block = Block;
7457 OperatorMethod.IsOperator = true;
7458 OperatorMethod.flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
7459 OperatorMethod.Define ();
7461 if (OperatorMethod.MethodBuilder == null)
7464 OperatorMethodBuilder = OperatorMethod.MethodBuilder;
7466 parameter_types = OperatorMethod.ParameterTypes;
7467 Type declaring_type = OperatorMethod.MethodData.DeclaringType;
7468 Type return_type = OperatorMethod.ReturnType;
7469 Type first_arg_type = parameter_types [0];
7474 // Rules for conversion operators
7476 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
7477 if (first_arg_type == return_type && first_arg_type == declaring_type){
7480 "User-defined conversion cannot take an object of the " +
7481 "enclosing type and convert to an object of the enclosing" +
7486 if (first_arg_type != declaring_type && return_type != declaring_type){
7489 "User-defined conversion must convert to or from the " +
7494 if (first_arg_type == TypeManager.object_type ||
7495 return_type == TypeManager.object_type){
7498 "User-defined conversion cannot convert to or from " +
7503 if (first_arg_type.IsInterface || return_type.IsInterface){
7506 "User-defined conversion cannot convert to or from an " +
7511 if (first_arg_type.IsSubclassOf (return_type)
7512 || return_type.IsSubclassOf (first_arg_type)){
7513 if (declaring_type.IsSubclassOf (return_type)) {
7514 Report.Error (553, Location, "'{0}' : user defined conversion to/from base class", GetSignatureForError ());
7517 Report.Error (554, Location, "'{0}' : user defined conversion to/from derived class", GetSignatureForError ());
7520 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
7521 if (first_arg_type != declaring_type || parameter_types [1] != TypeManager.int32_type) {
7522 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");
7525 } else if (Parameters.FixedParameters.Length == 1) {
7526 // Checks for Unary operators
7528 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
7529 if (return_type != declaring_type && !return_type.IsSubclassOf (declaring_type)) {
7530 Report.Error (448, Location,
7531 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
7534 if (first_arg_type != declaring_type){
7536 559, Location, "The parameter type for ++ or -- operator must be the containing type");
7541 if (first_arg_type != declaring_type){
7544 "The parameter of a unary operator must be the " +
7549 if (OperatorType == OpType.True || OperatorType == OpType.False) {
7550 if (return_type != TypeManager.bool_type){
7553 "The return type of operator True or False " +
7560 // Checks for Binary operators
7562 if (first_arg_type != declaring_type &&
7563 parameter_types [1] != declaring_type){
7566 "One of the parameters of a binary operator must " +
7567 "be the containing type");
7575 public override void Emit ()
7578 // abstract or extern methods have no bodies
7580 if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7583 OperatorMethod.Emit ();
7587 // Operator cannot be override
7588 protected override MethodInfo FindOutParentMethod (TypeContainer container, ref Type parent_ret_type)
7593 public static string GetName (OpType ot)
7596 case OpType.LogicalNot:
7598 case OpType.OnesComplement:
7600 case OpType.Increment:
7602 case OpType.Decrement:
7608 case OpType.Addition:
7610 case OpType.Subtraction:
7612 case OpType.UnaryPlus:
7614 case OpType.UnaryNegation:
7616 case OpType.Multiply:
7618 case OpType.Division:
7620 case OpType.Modulus:
7622 case OpType.BitwiseAnd:
7624 case OpType.BitwiseOr:
7626 case OpType.ExclusiveOr:
7628 case OpType.LeftShift:
7630 case OpType.RightShift:
7632 case OpType.Equality:
7634 case OpType.Inequality:
7636 case OpType.GreaterThan:
7638 case OpType.LessThan:
7640 case OpType.GreaterThanOrEqual:
7642 case OpType.LessThanOrEqual:
7644 case OpType.Implicit:
7646 case OpType.Explicit:
7652 public override string GetSignatureForError (TypeContainer tc)
7654 StringBuilder sb = new StringBuilder ();
7655 sb.AppendFormat ("{0}.operator {1} {2}({3}", tc.Name, GetName (OperatorType), Type.Type == null ? Type.ToString () : TypeManager.CSharpName (Type.Type),
7656 Parameters.FixedParameters [0].GetSignatureForError ());
7658 if (Parameters.FixedParameters.Length > 1) {
7660 sb.Append (Parameters.FixedParameters [1].GetSignatureForError ());
7663 return sb.ToString ();
7666 public override string GetSignatureForError ()
7671 public override string ToString ()
7673 if (OperatorMethod == null)
7676 Type return_type = OperatorMethod.ReturnType;
7677 Type [] param_types = OperatorMethod.ParameterTypes;
7679 if (Parameters.FixedParameters.Length == 1)
7680 return String.Format (
7681 "{0} operator {1}({2})",
7682 TypeManager.CSharpName (return_type),
7683 GetName (OperatorType),
7686 return String.Format (
7687 "{0} operator {1}({2}, {3})",
7688 TypeManager.CSharpName (return_type),
7689 GetName (OperatorType),
7690 param_types [0], param_types [1]);
7693 public override string[] ValidAttributeTargets {
7695 return attribute_targets;
7699 public void SetYields ()
7701 ModFlags |= Modifiers.METHOD_YIELDS;
7706 // This is used to compare method signatures
7708 struct MethodSignature {
7710 public Type RetType;
7711 public Type [] Parameters;
7714 /// This delegate is used to extract methods which have the
7715 /// same signature as the argument
7717 public static MemberFilter method_signature_filter = new MemberFilter (MemberSignatureCompare);
7719 public MethodSignature (string name, Type ret_type, Type [] parameters)
7724 if (parameters == null)
7725 Parameters = TypeManager.NoTypes;
7727 Parameters = parameters;
7730 public override string ToString ()
7733 if (Parameters.Length != 0){
7734 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
7735 for (int i = 0; i < Parameters.Length; i++){
7736 sb.Append (Parameters [i]);
7737 if (i+1 < Parameters.Length)
7740 pars = sb.ToString ();
7743 return String.Format ("{0} {1} ({2})", RetType, Name, pars);
7746 public override int GetHashCode ()
7748 return Name.GetHashCode ();
7751 public override bool Equals (Object o)
7753 MethodSignature other = (MethodSignature) o;
7755 if (other.Name != Name)
7758 if (other.RetType != RetType)
7761 if (Parameters == null){
7762 if (other.Parameters == null)
7767 if (other.Parameters == null)
7770 int c = Parameters.Length;
7771 if (other.Parameters.Length != c)
7774 for (int i = 0; i < c; i++)
7775 if (other.Parameters [i] != Parameters [i])
7781 static bool MemberSignatureCompare (MemberInfo m, object filter_criteria)
7783 MethodSignature sig = (MethodSignature) filter_criteria;
7785 if (m.Name != sig.Name)
7789 MethodInfo mi = m as MethodInfo;
7790 PropertyInfo pi = m as PropertyInfo;
7793 ReturnType = mi.ReturnType;
7794 else if (pi != null)
7795 ReturnType = pi.PropertyType;
7800 // we use sig.RetType == null to mean `do not check the
7801 // method return value.
7803 if (sig.RetType != null)
7804 if (ReturnType != sig.RetType)
7809 args = TypeManager.GetArgumentTypes (mi);
7811 args = TypeManager.GetArgumentTypes (pi);
7812 Type [] sigp = sig.Parameters;
7814 if (args.Length != sigp.Length)
7817 for (int i = args.Length; i > 0; ){
7819 if (args [i] != sigp [i])