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 {
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;
48 Attributes OptAttributes;
50 // These will happen after the semantic analysis
52 // Hashtable defined_indexers;
53 // Hashtable defined_methods;
56 /// Modifiers allowed in a class declaration
58 public const int AllowedModifiers =
66 public Interface (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
67 : base (parent, name, l)
69 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
70 OptAttributes = attrs;
72 method_builders = new ArrayList ();
73 property_builders = new ArrayList ();
76 public AdditionResult AddMethod (InterfaceMethod imethod)
78 string name = imethod.Name;
79 Object value = defined_names [name];
82 if (!(value is InterfaceMethod))
83 return AdditionResult.NameExists;
86 if (defined_method == null)
87 defined_method = new ArrayList ();
89 defined_method.Add (imethod);
91 DefineName (name, imethod);
93 return AdditionResult.Success;
96 public AdditionResult AddProperty (InterfaceProperty iprop)
99 string name = iprop.Name;
101 if ((res = IsValid (name)) != AdditionResult.Success)
104 DefineName (name, iprop);
106 if (defined_properties == null)
107 defined_properties = new ArrayList ();
109 defined_properties.Add (iprop);
110 return AdditionResult.Success;
113 public AdditionResult AddEvent (InterfaceEvent ievent)
115 string name = ievent.Name;
118 if ((res = IsValid (name)) != AdditionResult.Success)
121 DefineName (name, ievent);
123 if (defined_events == null)
124 defined_events = new ArrayList ();
126 defined_events.Add (ievent);
127 return AdditionResult.Success;
130 public bool AddIndexer (InterfaceIndexer iindexer)
132 if (defined_indexer == null)
133 defined_indexer = new ArrayList ();
135 defined_indexer.Add (iindexer);
139 public ArrayList InterfaceMethods {
141 return defined_method;
145 public ArrayList InterfaceProperties {
147 return defined_properties;
151 public ArrayList InterfaceEvents {
153 return defined_events;
157 public ArrayList InterfaceIndexers {
159 return defined_indexer;
163 public ArrayList Bases {
173 public bool IsTopLevel {
176 if (Parent.Parent == null)
183 public virtual TypeAttributes InterfaceAttr {
185 TypeAttributes x = 0;
187 if ((ModFlags & Modifiers.PUBLIC) != 0)
188 x |= TypeAttributes.Public;
189 else if ((ModFlags & Modifiers.PRIVATE) != 0)
190 x |= TypeAttributes.NotPublic;
192 if (IsTopLevel == false) {
194 if ((ModFlags & Modifiers.PROTECTED) != 0
195 && (ModFlags & Modifiers.INTERNAL) != 0)
196 x |= TypeAttributes.NestedFamORAssem;
198 if ((ModFlags & Modifiers.PROTECTED) != 0)
199 x |= TypeAttributes.NestedFamily;
201 if ((ModFlags & Modifiers.INTERNAL) != 0)
202 x |= TypeAttributes.NestedAssembly;
206 if ((ModFlags & Modifiers.ABSTRACT) != 0)
207 x |= TypeAttributes.Abstract;
209 if ((ModFlags & Modifiers.SEALED) != 0)
210 x |= TypeAttributes.Sealed;
216 void Error111 (InterfaceMemberBase ib)
220 "Interface `" + Name + "' already contains a definition with the " +
221 "same return value and parameter types for member `" + ib.Name + "'");
224 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
226 if (!TypeManager.RegisterMethod (mb, ip, types))
229 method_builders.Add (mb);
233 public MethodInfo [] GetMethods ()
235 int n = method_builders.Count;
236 MethodInfo [] mi = new MethodInfo [n];
238 method_builders.CopyTo (mi, 0);
243 // Hack around System.Reflection as found everywhere else
244 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
246 ArrayList members = new ArrayList ();
248 if ((mt & MemberTypes.Method) != 0) {
249 foreach (MethodBuilder mb in method_builders)
250 if (filter (mb, criteria))
254 if ((mt & MemberTypes.Property) != 0) {
255 foreach (PropertyBuilder pb in property_builders)
256 if (filter (pb, criteria))
260 // The rest of the cases, if any, are unhandled at present.
262 int count = members.Count;
265 MemberInfo [] mi = new MemberInfo [count];
266 members.CopyTo (mi, 0);
274 // Populates the methods in the interface
276 void PopulateMethod (InterfaceMethod im)
278 Type return_type = RootContext.LookupType (this, 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){
291 if (t.IsPointer && !UnsafeOK (this))
298 mb = TypeBuilder.DefineMethod (
299 im.Name, interface_method_attributes,
300 return_type, arg_types);
302 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
304 if (!RegisterMethod (mb, ip, arg_types)) {
310 // Define each type attribute (in/out/ref) and
311 // the argument names.
313 p = im.Parameters.FixedParameters;
315 for (i = 0; i < p.Length; i++)
316 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
318 if (i != arg_types.Length)
319 Console.WriteLine ("Implement the type definition for params");
324 // Populates the properties in the interface
326 void PopulateProperty (InterfaceProperty ip)
329 MethodBuilder get = null, set = null;
330 Type prop_type = RootContext.LookupType (this, ip.Type, false, ip.Location);
331 Type [] setter_args = new Type [1];
333 if (prop_type == null)
336 if (prop_type.IsPointer && !UnsafeOK (this))
339 setter_args [0] = prop_type;
342 // FIXME: properties are missing the following
343 // flags: hidebysig newslot specialname
345 pb = TypeBuilder.DefineProperty (
346 ip.Name, PropertyAttributes.None,
350 get = TypeBuilder.DefineMethod (
351 "get_" + ip.Name, property_attributes ,
355 // HACK because System.Reflection.Emit is lame
357 Type [] null_types = null;
358 InternalParameters inp = new InternalParameters
359 (null_types, Parameters.GetEmptyReadOnlyParameters ());
361 if (!RegisterMethod (get, inp, null)) {
366 pb.SetGetMethod (get);
370 setter_args [0] = prop_type;
372 set = TypeBuilder.DefineMethod (
373 "set_" + ip.Name, property_attributes,
374 TypeManager.void_type, setter_args);
376 set.DefineParameter (1, ParameterAttributes.None, "value");
377 pb.SetSetMethod (set);
380 // HACK because System.Reflection.Emit is lame
382 Parameter [] parms = new Parameter [1];
383 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
384 InternalParameters ipp = new InternalParameters (
385 Parent, new Parameters (parms, null, Location.Null));
387 if (!RegisterMethod (set, ipp, setter_args)) {
393 TypeManager.RegisterProperty (pb, get, set);
394 property_builders.Add (pb);
398 // Populates the events in the interface
400 void PopulateEvent (InterfaceEvent ie)
403 // FIXME: We need to do this after delegates have been
404 // declared or we declare them recursively.
409 // Populates the indexers in the interface
411 void PopulateIndexer (InterfaceIndexer ii)
414 Type prop_type = RootContext.LookupType (this, ii.Type, false, ii.Location);
415 Type [] arg_types = ii.ParameterTypes (this);
416 Type [] value_arg_types;
418 if (prop_type == null)
421 if (prop_type.IsPointer && !UnsafeOK (this))
425 // Sets up the extra invisible `value' argument for setters.
427 if (arg_types != null){
428 int count = arg_types.Length;
429 value_arg_types = new Type [count + 1];
431 arg_types.CopyTo (value_arg_types, 0);
432 value_arg_types [count] = prop_type;
434 foreach (Type t in arg_types){
435 if (t.IsPointer && !UnsafeOK (this))
439 value_arg_types = new Type [1];
441 value_arg_types [1] = prop_type;
444 pb = TypeBuilder.DefineProperty (
445 "Item", PropertyAttributes.None,
446 prop_type, arg_types);
449 MethodBuilder get_item;
450 Parameter [] p = ii.Parameters.FixedParameters;
452 get_item = TypeBuilder.DefineMethod (
453 "get_Item", property_attributes, prop_type, arg_types);
454 pb.SetGetMethod (get_item);
456 // HACK because System.Reflection.Emit is lame
458 InternalParameters ip = new InternalParameters (
459 arg_types, ii.Parameters);
461 if (!RegisterMethod (get_item, ip, arg_types)) {
467 for (int i = 0; i < p.Length; i++)
468 get_item.DefineParameter (
470 p [i].Attributes, p [i].Name);
475 Parameter [] p = ii.Parameters.FixedParameters;
476 MethodBuilder set_item;
479 set_item = TypeBuilder.DefineMethod (
480 "set_Item", property_attributes,
481 TypeManager.void_type, value_arg_types);
482 pb.SetSetMethod (set_item);
484 // HACK because System.Reflection.Emit is lame
486 InternalParameters ip = new InternalParameters (
487 value_arg_types, ii.Parameters);
488 if (!RegisterMethod (set_item, ip, value_arg_types)) {
494 for (; i < p.Length; i++)
495 set_item.DefineParameter (
497 p [i].Attributes, p [i].Name);
500 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
505 /// Performs the semantic analysis for all the interface members
506 /// that were declared
508 bool SemanticAnalysis ()
510 Hashtable methods = new Hashtable ();
513 if (defined_method != null){
514 foreach (InterfaceMethod im in defined_method){
515 string sig = im.GetSignature (this);
518 // If there was an undefined Type on the signatures
523 if (methods [sig] != null){
531 // FIXME: Here I should check i
537 // Returns the Type that represents the interface whose name
541 Type GetInterfaceTypeByName (object builder, string name)
544 Type t = RootContext.TypeManager.LookupType (name);
553 cause = "is a struct";
555 cause = "is a class";
557 cause = "Should not happen.";
559 Report.Error (527, Location, "`"+name+"' " + cause +
560 ", need an interface instead");
565 Tree tree = RootContext.Tree;
566 parent = (Interface) tree.Interfaces [name];
571 container = tree.Classes;
572 if (container != null && container [name] != null)
573 cause = "is a class";
575 container = tree.Structs;
577 if (container != null && container [name] != null)
578 cause = "is a struct";
582 Report.Error (246, Location, "Can not find type `"+name+"'");
584 Report.Error (527, Location, "`"+name+"' " + cause +
585 ", need an interface instead");
590 t = parent.DefineInterface (builder);
593 "Inherited interface `"+name+"' is circular");
601 // Returns the list of interfaces that this interface implements
602 // Or null if it does not implement any interface.
604 // Sets the error boolean accoringly.
606 Type [] GetInterfaceBases (object builder, out bool error)
615 tbases = new Type [Bases.Count];
618 foreach (string name in Bases){
621 t = GetInterfaceTypeByName (builder, name);
635 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
638 // Rework the way we recurse, because for recursive
639 // definitions of interfaces (A:B and B:A) we report the
640 // error twice, rather than once.
642 public TypeBuilder DefineInterface (object parent_builder)
652 ifaces = GetInterfaceBases (parent_builder, out error);
657 if (parent_builder is ModuleBuilder) {
658 ModuleBuilder builder = (ModuleBuilder) parent_builder;
660 TypeBuilder = builder.DefineType (Name,
661 TypeAttributes.Interface |
663 TypeAttributes.Abstract,
667 TypeBuilder builder = (System.Reflection.Emit.TypeBuilder) parent_builder;
669 TypeBuilder = builder.DefineNestedType (Basename,
670 TypeAttributes.Interface |
672 TypeAttributes.Abstract,
677 RootContext.TypeManager.AddUserInterface (Name, TypeBuilder, this);
678 RootContext.RegisterOrder (this);
686 /// Performs semantic analysis, and then generates the IL interfaces
688 public override bool Define (TypeContainer parent)
690 if (!SemanticAnalysis ())
693 if (defined_method != null){
694 foreach (InterfaceMethod im in defined_method)
698 if (defined_properties != null){
699 foreach (InterfaceProperty ip in defined_properties)
700 PopulateProperty (ip);
703 if (defined_events != null)
704 foreach (InterfaceEvent ie in defined_events)
707 if (defined_indexer != null) {
708 foreach (InterfaceIndexer ii in defined_indexer)
709 PopulateIndexer (ii);
711 CustomAttributeBuilder cb = EmitDefaultMemberAttr (parent, ModFlags, Location);
712 TypeBuilder.SetCustomAttribute (cb);
718 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent, int flags,
721 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
723 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
724 ".ctor", MemberTypes.Constructor,
725 BindingFlags.Public | BindingFlags.Instance,
728 if (!(ml is MethodGroupExpr)) {
729 Console.WriteLine ("Internal error !!!!");
733 MethodGroupExpr mg = (MethodGroupExpr) ml;
735 MethodBase constructor = mg.Methods [0];
737 string [] vals = { "Item" };
739 CustomAttributeBuilder cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
746 public class InterfaceMemberBase {
747 public readonly string Name;
748 public readonly bool IsNew;
749 public Attributes OptAttributes;
751 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
755 OptAttributes = attrs;
759 public class InterfaceProperty : InterfaceMemberBase {
760 public readonly bool HasSet;
761 public readonly bool HasGet;
762 public readonly string Type;
763 public readonly string type;
764 public readonly Location Location;
766 public InterfaceProperty (string type, string name,
767 bool is_new, bool has_get, bool has_set,
768 Attributes attrs, Location loc)
769 : base (name, is_new, attrs)
778 public class InterfaceEvent : InterfaceMemberBase {
779 public readonly string Type;
781 public InterfaceEvent (string type, string name, bool is_new, Attributes attrs)
782 : base (name, is_new, attrs)
788 public class InterfaceMethod : InterfaceMemberBase {
789 public readonly string ReturnType;
790 public readonly Parameters Parameters;
791 public readonly Location Location;
793 public InterfaceMethod (string return_type, string name, bool is_new, Parameters args,
794 Attributes attrs, Location l)
795 : base (name, is_new, attrs)
797 this.ReturnType = return_type;
798 this.Parameters = args;
803 /// Returns the signature for this interface method
805 public string GetSignature (DeclSpace ds)
807 Type ret = RootContext.LookupType (ds, ReturnType, false, Location);
808 string args = Parameters.GetSignature (ds);
810 if ((ret == null) || (args == null))
813 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
816 public Type [] ParameterTypes (DeclSpace ds)
818 return Parameters.GetParameterInfo (ds);
822 public class InterfaceIndexer : InterfaceMemberBase {
823 public readonly bool HasGet, HasSet;
824 public readonly Parameters Parameters;
825 public readonly string Type;
826 public readonly Location Location;
828 public InterfaceIndexer (string type, Parameters args, bool do_get, bool do_set,
829 bool is_new, Attributes attrs, Location loc)
830 : base ("", is_new, attrs)
839 public Type [] ParameterTypes (DeclSpace ds)
841 return Parameters.GetParameterInfo (ds);