2 // interface.cs: Interface handler
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
17 namespace Mono.CSharp {
22 public class Interface : DeclSpace, IMemberContainer {
23 const MethodAttributes interface_method_attributes =
24 MethodAttributes.Public |
25 MethodAttributes.Abstract |
26 MethodAttributes.HideBySig |
27 MethodAttributes.NewSlot |
28 MethodAttributes.Virtual;
30 const MethodAttributes property_attributes =
31 MethodAttributes.Public |
32 MethodAttributes.Abstract |
33 MethodAttributes.HideBySig |
34 MethodAttributes.NewSlot |
35 MethodAttributes.SpecialName |
36 MethodAttributes.Virtual;
40 ArrayList defined_method;
41 ArrayList defined_indexer;
42 ArrayList defined_events;
43 ArrayList defined_properties;
45 ArrayList method_builders;
46 ArrayList property_builders;
47 ArrayList event_builders;
49 Attributes OptAttributes;
51 public string IndexerName;
53 IMemberContainer parent_container;
54 MemberCache member_cache;
58 // These will happen after the semantic analysis
60 // Hashtable defined_indexers;
61 // Hashtable defined_methods;
64 /// Modifiers allowed in a class declaration
66 public const int AllowedModifiers =
74 public Interface (NamespaceEntry ns, TypeContainer parent, string name, int mod,
75 Attributes attrs, Location l)
76 : base (ns, parent, name, l)
78 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
79 OptAttributes = attrs;
81 method_builders = new ArrayList ();
82 property_builders = new ArrayList ();
83 event_builders = new ArrayList ();
86 public AdditionResult AddMethod (InterfaceMethod imethod)
88 string name = imethod.Name;
89 Object value = defined_names [name];
92 if (!(value is InterfaceMethod))
93 return AdditionResult.NameExists;
96 if (defined_method == null)
97 defined_method = new ArrayList ();
99 defined_method.Add (imethod);
101 DefineName (name, imethod);
103 return AdditionResult.Success;
106 public AdditionResult AddProperty (InterfaceProperty iprop)
109 string name = iprop.Name;
111 if ((res = IsValid (name, name)) != AdditionResult.Success)
114 DefineName (name, iprop);
116 if (defined_properties == null)
117 defined_properties = new ArrayList ();
119 defined_properties.Add (iprop);
120 return AdditionResult.Success;
123 public AdditionResult AddEvent (InterfaceEvent ievent)
125 string name = ievent.Name;
128 if ((res = IsValid (name, name)) != AdditionResult.Success)
131 DefineName (name, ievent);
133 if (defined_events == null)
134 defined_events = new ArrayList ();
136 defined_events.Add (ievent);
137 return AdditionResult.Success;
140 public bool AddIndexer (InterfaceIndexer iindexer)
142 if (defined_indexer == null)
143 defined_indexer = new ArrayList ();
145 defined_indexer.Add (iindexer);
149 public ArrayList InterfaceMethods {
151 return defined_method;
155 public ArrayList InterfaceProperties {
157 return defined_properties;
161 public ArrayList InterfaceEvents {
163 return defined_events;
167 public ArrayList InterfaceIndexers {
169 return defined_indexer;
173 public ArrayList Bases {
183 public virtual TypeAttributes InterfaceAttr {
185 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
187 if (IsTopLevel == false) {
189 if ((ModFlags & Modifiers.PROTECTED) != 0
190 && (ModFlags & Modifiers.INTERNAL) != 0)
191 x |= TypeAttributes.NestedFamORAssem;
192 else if ((ModFlags & Modifiers.PROTECTED) != 0)
193 x |= TypeAttributes.NestedFamily;
194 else if ((ModFlags & Modifiers.INTERNAL) != 0)
195 x |= TypeAttributes.NestedAssembly;
196 else if ((ModFlags & Modifiers.PUBLIC) != 0)
197 x |= TypeAttributes.NestedPublic;
199 x |= TypeAttributes.NestedPrivate;
201 if ((ModFlags & Modifiers.PUBLIC) != 0)
202 x |= TypeAttributes.Public;
203 else if ((ModFlags & Modifiers.PRIVATE) != 0)
204 x |= TypeAttributes.NotPublic;
207 if ((ModFlags & Modifiers.ABSTRACT) != 0)
208 x |= TypeAttributes.Abstract;
210 if ((ModFlags & Modifiers.SEALED) != 0)
211 x |= TypeAttributes.Sealed;
217 void Error111 (InterfaceMemberBase ib)
221 "Interface `" + Name + "' already contains a definition with the " +
222 "same return value and parameter types for member `" + ib.Name + "'");
225 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
227 if (!TypeManager.RegisterMethod (mb, ip, types))
230 method_builders.Add (mb);
235 // This might trigger a definition of the methods. This happens only
236 // with Attributes, as Attribute classes are processed before interfaces.
237 // Ideally, we should make everything just define recursively in terms
238 // of its dependencies.
240 public MethodInfo [] GetMethods (TypeContainer container)
244 if (!members_defined){
245 if (DefineMembers (container))
246 n = method_builders.Count;
248 n = method_builders.Count;
250 MethodInfo [] mi = new MethodInfo [n];
252 method_builders.CopyTo (mi, 0);
257 // Hack around System.Reflection as found everywhere else
258 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
259 MemberFilter filter, object criteria)
261 ArrayList members = new ArrayList ();
263 if ((mt & MemberTypes.Method) != 0) {
264 foreach (MethodBuilder mb in method_builders)
265 if (filter (mb, criteria))
269 if ((mt & MemberTypes.Property) != 0) {
270 foreach (PropertyBuilder pb in property_builders)
271 if (filter (pb, criteria))
275 if ((mt & MemberTypes.Event) != 0) {
276 foreach (MyEventBuilder eb in event_builders)
277 if (filter (eb, criteria))
281 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
282 MemberList parent_mi;
284 parent_mi = TypeContainer.FindMembers (
285 TypeBuilder.BaseType, mt, bf, filter, criteria);
287 members.AddRange (parent_mi);
290 return new MemberList (members);
293 public override MemberCache MemberCache {
300 // Populates the methods in the interface
302 void PopulateMethod (TypeContainer parent, DeclSpace decl_space, InterfaceMethod im)
304 Type return_type = im.ReturnType.Type;
305 if (return_type == null)
306 return_type = this.ResolveType (im.ReturnType, false, im.Location);
308 Type [] arg_types = im.ParameterTypes (this);
313 if (return_type == null)
316 if (return_type.IsPointer && !UnsafeOK (this))
319 if (arg_types == null)
322 foreach (Type t in arg_types){
327 if (t.IsPointer && !UnsafeOK (this))
334 mb = TypeBuilder.DefineMethod (
335 im.Name, interface_method_attributes,
336 return_type, arg_types);
338 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
340 if (!RegisterMethod (mb, ip, arg_types)) {
346 // Labelling of parameters is taken care of
347 // during the Emit phase via
348 // MethodCore.LabelParameters method so I am
349 // removing the old code here.
357 // Populates the properties in the interface
359 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
362 MethodBuilder get = null, set = null;
363 ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
367 Type prop_type = ip.Type.Type;
368 Type [] setter_args = new Type [1];
370 if (prop_type == null)
373 if (prop_type.IsPointer && !UnsafeOK (this))
376 setter_args [0] = prop_type;
379 // FIXME: properties are missing the following
380 // flags: hidebysig newslot specialname
382 pb = TypeBuilder.DefineProperty (
383 ip.Name, PropertyAttributes.None,
387 get = TypeBuilder.DefineMethod (
388 "get_" + ip.Name, property_attributes ,
392 // HACK because System.Reflection.Emit is lame
394 Type [] null_types = null;
395 InternalParameters inp = new InternalParameters
396 (null_types, Parameters.EmptyReadOnlyParameters);
398 if (!RegisterMethod (get, inp, null)) {
403 pb.SetGetMethod (get);
407 setter_args [0] = prop_type;
409 set = TypeBuilder.DefineMethod (
410 "set_" + ip.Name, property_attributes,
411 TypeManager.void_type, setter_args);
413 set.DefineParameter (1, ParameterAttributes.None, "value");
414 pb.SetSetMethod (set);
417 // HACK because System.Reflection.Emit is lame
419 Parameter [] parms = new Parameter [1];
420 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
421 InternalParameters ipp = new InternalParameters (
422 this, new Parameters (parms, null, Location.Null));
424 if (!RegisterMethod (set, ipp, setter_args)) {
430 TypeManager.RegisterProperty (pb, get, set);
431 property_builders.Add (pb);
436 // Populates the events in the interface
438 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
441 // FIXME: We need to do this after delegates have been
442 // declared or we declare them recursively.
445 MethodBuilder add = null, remove = null;
446 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
450 Type event_type = ie.Type.Type;
452 if (event_type == null)
455 if (event_type.IsPointer && !UnsafeOK (this))
458 Type [] parameters = new Type [1];
459 parameters [0] = event_type;
461 eb = new MyEventBuilder (null, TypeBuilder, ie.Name,
462 EventAttributes.None, event_type);
465 // Now define the accessors
467 string add_name = "add_" + ie.Name;
469 add = TypeBuilder.DefineMethod (
470 add_name, property_attributes, null, parameters);
471 add.DefineParameter (1, ParameterAttributes.None, "value");
472 eb.SetAddOnMethod (add);
474 string remove_name = "remove_" + ie.Name;
475 remove = TypeBuilder.DefineMethod (
476 remove_name, property_attributes, null, parameters);
477 remove.DefineParameter (1, ParameterAttributes.None, "value");
478 eb.SetRemoveOnMethod (remove);
480 Parameter [] parms = new Parameter [1];
481 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
482 InternalParameters ip = new InternalParameters (
483 this, new Parameters (parms, null, Location.Null));
485 if (!RegisterMethod (add, ip, parameters)) {
490 if (!RegisterMethod (remove, ip, parameters)) {
495 TypeManager.RegisterEvent (eb, add, remove);
496 event_builders.Add (eb);
502 // Populates the indexers in the interface
504 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
507 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
511 Type prop_type = ii.Type.Type;
512 Type [] arg_types = ii.ParameterTypes (this);
513 Type [] value_arg_types;
515 if (prop_type == null)
518 if (prop_type.IsPointer && !UnsafeOK (this))
522 // Sets up the extra invisible `value' argument for setters.
524 if (arg_types != null){
525 int count = arg_types.Length;
526 value_arg_types = new Type [count + 1];
528 arg_types.CopyTo (value_arg_types, 0);
529 value_arg_types [count] = prop_type;
531 foreach (Type t in arg_types){
532 if (t.IsPointer && !UnsafeOK (this))
536 value_arg_types = new Type [1];
538 value_arg_types [1] = prop_type;
541 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
542 null, ModFlags, false);
544 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
545 if (IndexerName == null)
546 IndexerName = "Item";
548 pb = TypeBuilder.DefineProperty (
549 IndexerName, PropertyAttributes.None,
550 prop_type, arg_types);
552 MethodBuilder set_item = null, get_item = null;
554 Parameter [] p = ii.Parameters.FixedParameters;
556 get_item = TypeBuilder.DefineMethod (
557 "get_" + IndexerName, property_attributes,
558 prop_type, arg_types);
559 pb.SetGetMethod (get_item);
561 // HACK because System.Reflection.Emit is lame
563 InternalParameters ip = new InternalParameters (
564 arg_types, ii.Parameters);
566 if (!RegisterMethod (get_item, ip, arg_types)) {
572 for (int i = 0; i < p.Length; i++)
573 get_item.DefineParameter (
575 p [i].Attributes, p [i].Name);
580 Parameter [] p = ii.Parameters.FixedParameters;
584 pv = new Parameter [p.Length + 1];
586 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
587 Parameters value_params = new Parameters (pv, null, Location.Null);
588 value_params.GetParameterInfo (decl_space);
590 set_item = TypeBuilder.DefineMethod (
591 "set_" + IndexerName, property_attributes,
592 TypeManager.void_type, value_arg_types);
593 pb.SetSetMethod (set_item);
595 // HACK because System.Reflection.Emit is lame
597 InternalParameters ip = new InternalParameters (
598 value_arg_types, value_params);
599 if (!RegisterMethod (set_item, ip, value_arg_types)) {
605 for (; i < p.Length; i++)
606 set_item.DefineParameter (
608 p [i].Attributes, p [i].Name);
611 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
614 property_builders.Add (pb);
620 /// Performs the semantic analysis for all the interface members
621 /// that were declared
623 bool SemanticAnalysis ()
625 Hashtable methods = new Hashtable ();
628 if (defined_method != null){
629 foreach (InterfaceMethod im in defined_method){
630 string sig = im.GetSignature (this);
633 // If there was an undefined Type on the signatures
638 if (methods [sig] != null){
646 // FIXME: Here I should check i
651 Type GetInterfaceTypeByName (Expression name)
653 Expression original = name;
654 name = ResolveTypeExpr (name, false, Location);
666 cause = "is a struct";
668 cause = "is a class";
670 cause = "Should not happen.";
672 Report.Error (527, Location, "`"+name+"' " + cause +
673 ", need an interface instead");
679 // Returns the list of interfaces that this interface implements
680 // Or null if it does not implement any interface.
682 // Sets the error boolean accoringly.
684 Type [] GetInterfaceBases (out bool error)
693 tbases = new Type [Bases.Count];
696 foreach (Expression name in Bases){
699 t = GetInterfaceTypeByName (name);
705 if (!Parent.AsAccessible (t, ModFlags))
706 Report.Error (61, Location,
707 "Inconsistent accessibility: base interface `" +
708 TypeManager.CSharpName (t) + "' is less " +
709 "accessible than interface `" +
715 return TypeManager.ExpandInterfaces (tbases);
720 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
724 // Rework the way we recurse, because for recursive
725 // definitions of interfaces (A:B and B:A) we report the
726 // error twice, rather than once.
728 public override TypeBuilder DefineType ()
733 if (TypeBuilder != null)
741 ifaces = GetInterfaceBases (out error);
747 foreach (TypeParameter type_param in TypeParameters)
748 if (!type_param.Resolve (this)) {
755 if (TypeManager.NamespaceClash (Name, Location))
758 ModuleBuilder builder = CodeGen.ModuleBuilder;
760 TypeBuilder = builder.DefineType (
763 (Type)null, // Parent Type
765 RootContext.RegisterOrder (this);
767 TypeBuilder builder = Parent.TypeBuilder;
769 TypeBuilder = builder.DefineNestedType (
772 (Type) null, //parent type
775 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
776 tc.RegisterOrder (this);
780 foreach (TypeParameter type_param in TypeParameters)
781 type_param.Define (TypeBuilder);
784 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
791 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
792 // interface is consistent. Either it is `Item' or it is the name defined by all the
793 // indexers with the `IndexerName' attribute.
795 // Turns out that the IndexerNameAttribute is applied to each indexer,
796 // but it is never emitted, instead a DefaultName attribute is attached
799 void DefineIndexers (TypeContainer parent)
801 string interface_indexer_name = null;
803 foreach (InterfaceIndexer ii in defined_indexer){
805 PopulateIndexer (parent, this, ii);
807 if (interface_indexer_name == null){
808 interface_indexer_name = IndexerName;
812 if (IndexerName == interface_indexer_name)
816 668, "Two indexers have different names, " +
817 " you should use the same name for all your indexers");
819 if (interface_indexer_name == null)
820 interface_indexer_name = "Item";
821 IndexerName = interface_indexer_name;
825 /// Performs semantic analysis, and then generates the IL interfaces
827 public override bool DefineMembers (TypeContainer parent)
832 if (!SemanticAnalysis ())
836 if (defined_method != null){
837 foreach (InterfaceMethod im in defined_method)
838 PopulateMethod (parent, this, im);
841 if (defined_properties != null){
842 foreach (InterfaceProperty ip in defined_properties)
843 PopulateProperty (parent, this, ip);
846 if (defined_events != null)
847 foreach (InterfaceEvent ie in defined_events)
848 PopulateEvent (parent, this, ie);
850 if (defined_indexer != null) {
851 DefineIndexers (parent);
853 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
854 parent, IndexerName, ModFlags, Location);
856 TypeBuilder.SetCustomAttribute (cb);
860 if (TypeBuilder.BaseType != null)
861 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
863 member_cache = new MemberCache (this);
865 members_defined = true;
871 // In the case of Interfaces, there is nothing to do here
873 public override bool Define (TypeContainer parent)
879 /// Applies all the attributes.
881 public void Emit (TypeContainer tc)
883 if (OptAttributes != null) {
884 EmitContext ec = new EmitContext (tc, this, Location, null, null,
886 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
889 // Now emit attributes for each interface member
890 if (defined_method != null) {
891 foreach (InterfaceMethod im in defined_method) {
892 EmitContext ec = new EmitContext (tc, this, Location, null,
893 im.ReturnType.Type, ModFlags, false);
895 MethodCore.LabelParameters (ec, im.Builder,
900 if (im.OptAttributes != null)
901 Attribute.ApplyAttributes (ec, im.Builder,
902 im, im.OptAttributes);
907 if (defined_properties != null) {
908 foreach (InterfaceProperty ip in defined_properties) {
909 EmitContext ec = new EmitContext (tc, this, Location, null,
910 null, ModFlags, false);
912 if (ip.OptAttributes != null)
913 Attribute.ApplyAttributes (ec, ip.Builder, ip, ip.OptAttributes);
917 if (defined_events != null) {
918 foreach (InterfaceEvent ie in defined_events) {
919 EmitContext ec = new EmitContext (tc, this, Location, null,
920 null, ModFlags, false);
922 if (ie.OptAttributes != null)
923 Attribute.ApplyAttributes (ec, ie.Builder, ie, ie.OptAttributes);
928 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
933 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
935 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
936 ".ctor", MemberTypes.Constructor,
937 BindingFlags.Public | BindingFlags.Instance,
940 if (!(ml is MethodGroupExpr)) {
941 Console.WriteLine ("Internal error !!!!");
945 MethodGroupExpr mg = (MethodGroupExpr) ml;
947 MethodBase constructor = mg.Methods [0];
949 string [] vals = { name };
951 CustomAttributeBuilder cb = null;
953 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
955 Report.Warning (-100, "Can not set the indexer default member attribute");
965 string IMemberContainer.Name {
971 Type IMemberContainer.Type {
977 IMemberContainer IMemberContainer.Parent {
979 return parent_container;
983 MemberCache IMemberContainer.MemberCache {
989 bool IMemberContainer.IsInterface {
995 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
997 // Interfaces only contain instance members.
998 if ((bf & BindingFlags.Instance) == 0)
999 return MemberList.Empty;
1000 if ((bf & BindingFlags.Public) == 0)
1001 return MemberList.Empty;
1003 ArrayList members = new ArrayList ();
1005 if ((mt & MemberTypes.Method) != 0)
1006 members.AddRange (method_builders);
1008 if ((mt & MemberTypes.Property) != 0)
1009 members.AddRange (property_builders);
1011 if ((mt & MemberTypes.Event) != 0)
1012 members.AddRange (event_builders);
1014 return new MemberList (members);
1018 public class InterfaceMemberBase {
1019 public readonly string Name;
1020 public readonly bool IsNew;
1021 public Attributes OptAttributes;
1023 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
1027 OptAttributes = attrs;
1031 public class InterfaceProperty : InterfaceMemberBase {
1032 public readonly bool HasSet;
1033 public readonly bool HasGet;
1034 public readonly Location Location;
1035 public Expression Type;
1036 public PropertyBuilder Builder;
1038 public InterfaceProperty (Expression type, string name,
1039 bool is_new, bool has_get, bool has_set,
1040 Attributes attrs, Location loc)
1041 : base (name, is_new, attrs)
1050 public class InterfaceEvent : InterfaceMemberBase {
1051 public readonly Location Location;
1052 public Expression Type;
1053 public MyEventBuilder Builder;
1055 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
1057 : base (name, is_new, attrs)
1064 public class InterfaceMethod : InterfaceMemberBase {
1065 public Expression ReturnType;
1066 public readonly Parameters Parameters;
1067 public readonly Location Location;
1068 public MethodBuilder Builder;
1070 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1071 Attributes attrs, Location l)
1072 : base (name, is_new, attrs)
1074 this.ReturnType = return_type;
1075 this.Parameters = args;
1080 /// Returns the signature for this interface method
1082 public string GetSignature (DeclSpace ds)
1084 ReturnType = ds.ResolveTypeExpr (ReturnType, false, Location);
1085 if (ReturnType == null)
1088 Type ret = ReturnType.Type;
1089 string args = Parameters.GetSignature (ds);
1091 if ((ret == null) || (args == null))
1094 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1097 public Type [] ParameterTypes (DeclSpace ds)
1099 return Parameters.GetParameterInfo (ds);
1103 public class InterfaceIndexer : InterfaceMemberBase {
1104 public readonly bool HasGet, HasSet;
1105 public readonly Parameters Parameters;
1106 public readonly Location Location;
1107 public Expression Type;
1108 public PropertyBuilder Builder;
1110 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1111 bool is_new, Attributes attrs, Location loc)
1112 : base ("", is_new, attrs)
1121 public Type [] ParameterTypes (DeclSpace ds)
1123 return Parameters.GetParameterInfo (ds);