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, IMemberFinder {
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 // These will happen after the semantic analysis
55 // Hashtable defined_indexers;
56 // Hashtable defined_methods;
59 /// Modifiers allowed in a class declaration
61 public const int AllowedModifiers =
69 public Interface (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
70 : base (parent, name, l)
72 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
73 OptAttributes = attrs;
75 method_builders = new ArrayList ();
76 property_builders = new ArrayList ();
77 event_builders = new ArrayList ();
80 public AdditionResult AddMethod (InterfaceMethod imethod)
82 string name = imethod.Name;
83 Object value = defined_names [name];
86 if (!(value is InterfaceMethod))
87 return AdditionResult.NameExists;
90 if (defined_method == null)
91 defined_method = new ArrayList ();
93 defined_method.Add (imethod);
95 DefineName (name, imethod);
97 return AdditionResult.Success;
100 public AdditionResult AddProperty (InterfaceProperty iprop)
103 string name = iprop.Name;
105 if ((res = IsValid (name)) != AdditionResult.Success)
108 DefineName (name, iprop);
110 if (defined_properties == null)
111 defined_properties = new ArrayList ();
113 defined_properties.Add (iprop);
114 return AdditionResult.Success;
117 public AdditionResult AddEvent (InterfaceEvent ievent)
119 string name = ievent.Name;
122 if ((res = IsValid (name)) != AdditionResult.Success)
125 DefineName (name, ievent);
127 if (defined_events == null)
128 defined_events = new ArrayList ();
130 defined_events.Add (ievent);
131 return AdditionResult.Success;
134 public bool AddIndexer (InterfaceIndexer iindexer)
136 if (defined_indexer == null)
137 defined_indexer = new ArrayList ();
139 defined_indexer.Add (iindexer);
143 public ArrayList InterfaceMethods {
145 return defined_method;
149 public ArrayList InterfaceProperties {
151 return defined_properties;
155 public ArrayList InterfaceEvents {
157 return defined_events;
161 public ArrayList InterfaceIndexers {
163 return defined_indexer;
167 public ArrayList Bases {
177 public virtual TypeAttributes InterfaceAttr {
179 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
181 if (IsTopLevel == false) {
183 if ((ModFlags & Modifiers.PROTECTED) != 0
184 && (ModFlags & Modifiers.INTERNAL) != 0)
185 x |= TypeAttributes.NestedFamORAssem;
186 else if ((ModFlags & Modifiers.PROTECTED) != 0)
187 x |= TypeAttributes.NestedFamily;
188 else if ((ModFlags & Modifiers.INTERNAL) != 0)
189 x |= TypeAttributes.NestedAssembly;
190 else if ((ModFlags & Modifiers.PUBLIC) != 0)
191 x |= TypeAttributes.NestedPublic;
193 x |= TypeAttributes.NestedPrivate;
195 if ((ModFlags & Modifiers.PUBLIC) != 0)
196 x |= TypeAttributes.Public;
197 else if ((ModFlags & Modifiers.PRIVATE) != 0)
198 x |= TypeAttributes.NotPublic;
201 if ((ModFlags & Modifiers.ABSTRACT) != 0)
202 x |= TypeAttributes.Abstract;
204 if ((ModFlags & Modifiers.SEALED) != 0)
205 x |= TypeAttributes.Sealed;
211 void Error111 (InterfaceMemberBase ib)
215 "Interface `" + Name + "' already contains a definition with the " +
216 "same return value and parameter types for member `" + ib.Name + "'");
219 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
221 if (!TypeManager.RegisterMethod (mb, ip, types))
224 method_builders.Add (mb);
228 public MethodInfo [] GetMethods ()
230 int n = method_builders.Count;
231 MethodInfo [] mi = new MethodInfo [n];
233 method_builders.CopyTo (mi, 0);
238 // Hack around System.Reflection as found everywhere else
239 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
241 ArrayList members = new ArrayList ();
243 if ((mt & MemberTypes.Method) != 0) {
244 foreach (MethodBuilder mb in method_builders)
245 if (filter (mb, criteria))
249 if ((mt & MemberTypes.Property) != 0) {
250 foreach (PropertyBuilder pb in property_builders)
251 if (filter (pb, criteria))
255 if ((mt & MemberTypes.Event) != 0) {
256 foreach (MyEventBuilder eb in event_builders)
257 if (filter (eb, criteria))
261 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
262 MemberList parent_mi;
264 parent_mi = TypeContainer.FindMembers (
265 TypeBuilder.BaseType, mt, bf, filter, criteria);
267 members.AddRange (parent_mi);
270 return new MemberList (members);
274 // Populates the methods in the interface
276 void PopulateMethod (TypeContainer parent, DeclSpace decl_space, InterfaceMethod im)
278 Type return_type = this.ResolveType (im.ReturnType, false, im.Location);
279 Type [] arg_types = im.ParameterTypes (this);
284 if (return_type == null)
287 if (return_type.IsPointer && !UnsafeOK (this))
290 foreach (Type t in arg_types){
295 if (t.IsPointer && !UnsafeOK (this))
302 mb = TypeBuilder.DefineMethod (
303 im.Name, interface_method_attributes,
304 return_type, arg_types);
306 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
308 if (!RegisterMethod (mb, ip, arg_types)) {
314 // Define each type attribute (in/out/ref) and
315 // the argument names.
317 p = im.Parameters.FixedParameters;
319 for (i = 0; i < p.Length; i++)
320 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
322 if (i != arg_types.Length)
323 Console.WriteLine ("Implement the type definition for params");
326 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
327 return_type, ModFlags, false);
329 if (im.OptAttributes != null)
330 Attribute.ApplyAttributes (ec, mb, im, im.OptAttributes, Location);
334 // Populates the properties in the interface
336 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
339 MethodBuilder get = null, set = null;
340 ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
341 Type prop_type = ip.Type.Type;
342 Type [] setter_args = new Type [1];
344 if (prop_type == null)
347 if (prop_type.IsPointer && !UnsafeOK (this))
350 setter_args [0] = prop_type;
353 // FIXME: properties are missing the following
354 // flags: hidebysig newslot specialname
356 pb = TypeBuilder.DefineProperty (
357 ip.Name, PropertyAttributes.None,
361 get = TypeBuilder.DefineMethod (
362 "get_" + ip.Name, property_attributes ,
366 // HACK because System.Reflection.Emit is lame
368 Type [] null_types = null;
369 InternalParameters inp = new InternalParameters
370 (null_types, Parameters.EmptyReadOnlyParameters);
372 if (!RegisterMethod (get, inp, null)) {
377 pb.SetGetMethod (get);
381 setter_args [0] = prop_type;
383 set = TypeBuilder.DefineMethod (
384 "set_" + ip.Name, property_attributes,
385 TypeManager.void_type, setter_args);
387 set.DefineParameter (1, ParameterAttributes.None, "value");
388 pb.SetSetMethod (set);
391 // HACK because System.Reflection.Emit is lame
393 Parameter [] parms = new Parameter [1];
394 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
395 InternalParameters ipp = new InternalParameters (
396 this, new Parameters (parms, null, Location.Null));
398 if (!RegisterMethod (set, ipp, setter_args)) {
404 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
405 null, ModFlags, false);
407 if (ip.OptAttributes != null)
408 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes, Location);
410 TypeManager.RegisterProperty (pb, get, set);
411 property_builders.Add (pb);
415 // Populates the events in the interface
417 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
420 // FIXME: We need to do this after delegates have been
421 // declared or we declare them recursively.
424 MethodBuilder add = null, remove = null;
425 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
426 Type event_type = ie.Type.Type;
428 if (event_type == null)
431 if (event_type.IsPointer && !UnsafeOK (this))
434 Type [] parameters = new Type [1];
435 parameters [0] = event_type;
437 eb = new MyEventBuilder (TypeBuilder, ie.Name,
438 EventAttributes.None, event_type);
441 // Now define the accessors
443 string add_name = "add_" + ie.Name;
445 add = TypeBuilder.DefineMethod (
446 add_name, property_attributes, null, parameters);
447 add.DefineParameter (1, ParameterAttributes.None, "value");
448 eb.SetAddOnMethod (add);
450 string remove_name = "remove_" + ie.Name;
451 remove = TypeBuilder.DefineMethod (
452 remove_name, property_attributes, null, parameters);
453 remove.DefineParameter (1, ParameterAttributes.None, "value");
454 eb.SetRemoveOnMethod (remove);
456 Parameter [] parms = new Parameter [1];
457 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
458 InternalParameters ip = new InternalParameters (
459 this, new Parameters (parms, null, Location.Null));
461 if (!RegisterMethod (add, ip, parameters)) {
466 if (!RegisterMethod (remove, ip, parameters)) {
471 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
472 null, ModFlags, false);
475 if (ie.OptAttributes != null)
476 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes, Location);
478 TypeManager.RegisterEvent (eb, add, remove);
479 event_builders.Add (eb);
483 // Populates the indexers in the interface
485 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
488 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
489 Type prop_type = ii.Type.Type;
490 Type [] arg_types = ii.ParameterTypes (this);
491 Type [] value_arg_types;
493 if (prop_type == null)
496 if (prop_type.IsPointer && !UnsafeOK (this))
500 // Sets up the extra invisible `value' argument for setters.
502 if (arg_types != null){
503 int count = arg_types.Length;
504 value_arg_types = new Type [count + 1];
506 arg_types.CopyTo (value_arg_types, 0);
507 value_arg_types [count] = prop_type;
509 foreach (Type t in arg_types){
510 if (t.IsPointer && !UnsafeOK (this))
514 value_arg_types = new Type [1];
516 value_arg_types [1] = prop_type;
519 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
520 null, ModFlags, false);
522 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
523 if (IndexerName == null)
524 IndexerName = "Item";
526 pb = TypeBuilder.DefineProperty (
527 IndexerName, PropertyAttributes.None,
528 prop_type, arg_types);
530 MethodBuilder set_item = null, get_item = null;
532 Parameter [] p = ii.Parameters.FixedParameters;
534 get_item = TypeBuilder.DefineMethod (
535 "get_" + IndexerName, property_attributes,
536 prop_type, arg_types);
537 pb.SetGetMethod (get_item);
539 // HACK because System.Reflection.Emit is lame
541 InternalParameters ip = new InternalParameters (
542 arg_types, ii.Parameters);
544 if (!RegisterMethod (get_item, ip, arg_types)) {
550 for (int i = 0; i < p.Length; i++)
551 get_item.DefineParameter (
553 p [i].Attributes, p [i].Name);
558 Parameter [] p = ii.Parameters.FixedParameters;
562 pv = new Parameter [p.Length + 1];
564 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
565 Parameters value_params = new Parameters (pv, null, Location.Null);
566 value_params.GetParameterInfo (decl_space);
568 set_item = TypeBuilder.DefineMethod (
569 "set_" + IndexerName, property_attributes,
570 TypeManager.void_type, value_arg_types);
571 pb.SetSetMethod (set_item);
573 // HACK because System.Reflection.Emit is lame
575 InternalParameters ip = new InternalParameters (
576 value_arg_types, value_params);
577 if (!RegisterMethod (set_item, ip, value_arg_types)) {
583 for (; i < p.Length; i++)
584 set_item.DefineParameter (
586 p [i].Attributes, p [i].Name);
589 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
592 if (ii.OptAttributes != null)
593 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes, Location);
595 property_builders.Add (pb);
599 /// Performs the semantic analysis for all the interface members
600 /// that were declared
602 bool SemanticAnalysis ()
604 Hashtable methods = new Hashtable ();
607 if (defined_method != null){
608 foreach (InterfaceMethod im in defined_method){
609 string sig = im.GetSignature (this);
612 // If there was an undefined Type on the signatures
617 if (methods [sig] != null){
625 // FIXME: Here I should check i
630 Type GetInterfaceTypeByName (string name)
632 Type t = FindType (name);
635 Report.Error (246, Location, "The type or namespace `" + name +
636 "' could not be found");
646 cause = "is a struct";
648 cause = "is a class";
650 cause = "Should not happen.";
652 Report.Error (527, Location, "`"+name+"' " + cause +
653 ", need an interface instead");
659 // Returns the list of interfaces that this interface implements
660 // Or null if it does not implement any interface.
662 // Sets the error boolean accoringly.
664 Type [] GetInterfaceBases (out bool error)
673 tbases = new Type [Bases.Count];
676 foreach (string name in Bases){
679 t = GetInterfaceTypeByName (name);
685 if (!Parent.AsAccessible (t, ModFlags))
686 Report.Error (61, Location,
687 "Inconsistent accessibility: base interface `" +
688 TypeManager.CSharpName (t) + "' is less " +
689 "accessible than interface `" +
695 return TypeManager.ExpandInterfaces (tbases);
700 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
704 // Rework the way we recurse, because for recursive
705 // definitions of interfaces (A:B and B:A) we report the
706 // error twice, rather than once.
708 public override TypeBuilder DefineType ()
713 if (TypeBuilder != null)
721 ifaces = GetInterfaceBases (out error);
727 ModuleBuilder builder = CodeGen.ModuleBuilder;
729 TypeBuilder = builder.DefineType (
732 (Type)null, // Parent Type
734 RootContext.RegisterOrder (this);
736 TypeBuilder builder = Parent.TypeBuilder;
738 TypeBuilder = builder.DefineNestedType (
741 (Type) null, //parent type
744 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
745 tc.RegisterOrder (this);
748 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
755 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
756 // interface is consistent. Either it is `Item' or it is the name defined by all the
757 // indexers with the `IndexerName' attribute.
759 // Turns out that the IndexerNameAttribute is applied to each indexer,
760 // but it is never emitted, instead a DefaultName attribute is attached
763 void DefineIndexers (TypeContainer parent)
765 string interface_indexer_name = null;
767 foreach (InterfaceIndexer ii in defined_indexer){
769 PopulateIndexer (parent, this, ii);
771 if (interface_indexer_name == null){
772 interface_indexer_name = IndexerName;
776 if (IndexerName == interface_indexer_name)
780 668, "Two indexers have different names, " +
781 " you should use the same name for all your indexers");
783 if (interface_indexer_name == null)
784 interface_indexer_name = "Item";
785 IndexerName = interface_indexer_name;
789 /// Performs semantic analysis, and then generates the IL interfaces
791 public override bool Define (TypeContainer parent)
793 if (!SemanticAnalysis ())
796 if (defined_method != null){
797 foreach (InterfaceMethod im in defined_method)
798 PopulateMethod (parent, this, im);
801 if (defined_properties != null){
802 foreach (InterfaceProperty ip in defined_properties)
803 PopulateProperty (parent, this, ip);
806 if (defined_events != null)
807 foreach (InterfaceEvent ie in defined_events)
808 PopulateEvent (parent, this, ie);
810 if (defined_indexer != null) {
811 DefineIndexers (parent);
813 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
814 parent, IndexerName, ModFlags, Location);
816 TypeBuilder.SetCustomAttribute (cb);
819 if (OptAttributes != null) {
820 EmitContext ec = new EmitContext (parent, this, Location, null, null,
822 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
828 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
833 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
835 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
836 ".ctor", MemberTypes.Constructor,
837 BindingFlags.Public | BindingFlags.Instance,
840 if (!(ml is MethodGroupExpr)) {
841 Console.WriteLine ("Internal error !!!!");
845 MethodGroupExpr mg = (MethodGroupExpr) ml;
847 MethodBase constructor = mg.Methods [0];
849 string [] vals = { name };
851 CustomAttributeBuilder cb = null;
853 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
855 Report.Warning (-100, "Can not set the indexer default member attribute");
863 public class InterfaceMemberBase {
864 public readonly string Name;
865 public readonly bool IsNew;
866 public Attributes OptAttributes;
868 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
872 OptAttributes = attrs;
876 public class InterfaceProperty : InterfaceMemberBase {
877 public readonly bool HasSet;
878 public readonly bool HasGet;
879 public readonly Location Location;
880 public Expression Type;
882 public InterfaceProperty (Expression type, string name,
883 bool is_new, bool has_get, bool has_set,
884 Attributes attrs, Location loc)
885 : base (name, is_new, attrs)
894 public class InterfaceEvent : InterfaceMemberBase {
895 public readonly Location Location;
896 public Expression Type;
898 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
900 : base (name, is_new, attrs)
907 public class InterfaceMethod : InterfaceMemberBase {
908 public readonly Expression ReturnType;
909 public readonly Parameters Parameters;
910 public readonly Location Location;
912 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
913 Attributes attrs, Location l)
914 : base (name, is_new, attrs)
916 this.ReturnType = return_type;
917 this.Parameters = args;
922 /// Returns the signature for this interface method
924 public string GetSignature (DeclSpace ds)
926 Type ret = ds.ResolveType (ReturnType, false, Location);
927 string args = Parameters.GetSignature (ds);
929 if ((ret == null) || (args == null))
932 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
935 public Type [] ParameterTypes (DeclSpace ds)
937 return Parameters.GetParameterInfo (ds);
941 public class InterfaceIndexer : InterfaceMemberBase {
942 public readonly bool HasGet, HasSet;
943 public readonly Parameters Parameters;
944 public readonly Location Location;
945 public Expression Type;
947 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
948 bool is_new, Attributes attrs, Location loc)
949 : base ("", is_new, attrs)
958 public Type [] ParameterTypes (DeclSpace ds)
960 return Parameters.GetParameterInfo (ds);