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 virtual TypeAttributes InterfaceAttr {
175 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
177 if (IsTopLevel == false) {
179 if ((ModFlags & Modifiers.PROTECTED) != 0
180 && (ModFlags & Modifiers.INTERNAL) != 0)
181 x |= TypeAttributes.NestedFamORAssem;
182 else if ((ModFlags & Modifiers.PROTECTED) != 0)
183 x |= TypeAttributes.NestedFamily;
184 else if ((ModFlags & Modifiers.INTERNAL) != 0)
185 x |= TypeAttributes.NestedAssembly;
186 else if ((ModFlags & Modifiers.PUBLIC) != 0)
187 x |= TypeAttributes.NestedPublic;
189 x |= TypeAttributes.NestedPrivate;
191 if ((ModFlags & Modifiers.PUBLIC) != 0)
192 x |= TypeAttributes.Public;
193 else if ((ModFlags & Modifiers.PRIVATE) != 0)
194 x |= TypeAttributes.NotPublic;
197 if ((ModFlags & Modifiers.ABSTRACT) != 0)
198 x |= TypeAttributes.Abstract;
200 if ((ModFlags & Modifiers.SEALED) != 0)
201 x |= TypeAttributes.Sealed;
207 void Error111 (InterfaceMemberBase ib)
211 "Interface `" + Name + "' already contains a definition with the " +
212 "same return value and parameter types for member `" + ib.Name + "'");
215 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
217 if (!TypeManager.RegisterMethod (mb, ip, types))
220 method_builders.Add (mb);
224 public MethodInfo [] GetMethods ()
226 int n = method_builders.Count;
227 MethodInfo [] mi = new MethodInfo [n];
229 method_builders.CopyTo (mi, 0);
234 // Hack around System.Reflection as found everywhere else
235 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
237 ArrayList members = new ArrayList ();
239 if ((mt & MemberTypes.Method) != 0) {
240 foreach (MethodBuilder mb in method_builders)
241 if (filter (mb, criteria))
245 if ((mt & MemberTypes.Property) != 0) {
246 foreach (PropertyBuilder pb in property_builders)
247 if (filter (pb, criteria))
251 if ((bf & BindingFlags.DeclaredOnly) == 0){
252 MemberInfo [] parent_mi;
254 parent_mi = TypeContainer.FindMembers (
255 TypeBuilder.BaseType, mt, bf, filter, criteria);
257 if (parent_mi != null)
258 members.AddRange (parent_mi);
261 // The rest of the cases, if any, are unhandled at present.
263 int count = members.Count;
266 MemberInfo [] mi = new MemberInfo [count];
267 members.CopyTo (mi, 0);
275 // Populates the methods in the interface
277 void PopulateMethod (InterfaceMethod im)
279 Type return_type = RootContext.LookupType (this, im.ReturnType, false, im.Location);
280 Type [] arg_types = im.ParameterTypes (this);
285 if (return_type == null)
288 if (return_type.IsPointer && !UnsafeOK (this))
291 foreach (Type t in arg_types){
296 if (t.IsPointer && !UnsafeOK (this))
303 mb = TypeBuilder.DefineMethod (
304 im.Name, interface_method_attributes,
305 return_type, arg_types);
307 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
309 if (!RegisterMethod (mb, ip, arg_types)) {
315 // Define each type attribute (in/out/ref) and
316 // the argument names.
318 p = im.Parameters.FixedParameters;
320 for (i = 0; i < p.Length; i++)
321 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
323 if (i != arg_types.Length)
324 Console.WriteLine ("Implement the type definition for params");
329 // Populates the properties in the interface
331 void PopulateProperty (InterfaceProperty ip)
334 MethodBuilder get = null, set = null;
335 Type prop_type = RootContext.LookupType (this, ip.Type, false, ip.Location);
336 Type [] setter_args = new Type [1];
338 if (prop_type == null)
341 if (prop_type.IsPointer && !UnsafeOK (this))
344 setter_args [0] = prop_type;
347 // FIXME: properties are missing the following
348 // flags: hidebysig newslot specialname
350 pb = TypeBuilder.DefineProperty (
351 ip.Name, PropertyAttributes.None,
355 get = TypeBuilder.DefineMethod (
356 "get_" + ip.Name, property_attributes ,
360 // HACK because System.Reflection.Emit is lame
362 Type [] null_types = null;
363 InternalParameters inp = new InternalParameters
364 (null_types, Parameters.GetEmptyReadOnlyParameters ());
366 if (!RegisterMethod (get, inp, null)) {
371 pb.SetGetMethod (get);
375 setter_args [0] = prop_type;
377 set = TypeBuilder.DefineMethod (
378 "set_" + ip.Name, property_attributes,
379 TypeManager.void_type, setter_args);
381 set.DefineParameter (1, ParameterAttributes.None, "value");
382 pb.SetSetMethod (set);
385 // HACK because System.Reflection.Emit is lame
387 Parameter [] parms = new Parameter [1];
388 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
389 InternalParameters ipp = new InternalParameters (
390 this, new Parameters (parms, null, Location.Null));
392 if (!RegisterMethod (set, ipp, setter_args)) {
398 TypeManager.RegisterProperty (pb, get, set);
399 property_builders.Add (pb);
403 // Populates the events in the interface
405 void PopulateEvent (InterfaceEvent ie)
408 // FIXME: We need to do this after delegates have been
409 // declared or we declare them recursively.
414 // Populates the indexers in the interface
416 void PopulateIndexer (InterfaceIndexer ii)
419 Type prop_type = RootContext.LookupType (this, ii.Type, false, ii.Location);
420 Type [] arg_types = ii.ParameterTypes (this);
421 Type [] value_arg_types;
423 if (prop_type == null)
426 if (prop_type.IsPointer && !UnsafeOK (this))
430 // Sets up the extra invisible `value' argument for setters.
432 if (arg_types != null){
433 int count = arg_types.Length;
434 value_arg_types = new Type [count + 1];
436 arg_types.CopyTo (value_arg_types, 0);
437 value_arg_types [count] = prop_type;
439 foreach (Type t in arg_types){
440 if (t.IsPointer && !UnsafeOK (this))
444 value_arg_types = new Type [1];
446 value_arg_types [1] = prop_type;
449 pb = TypeBuilder.DefineProperty (
450 "Item", PropertyAttributes.None,
451 prop_type, arg_types);
454 MethodBuilder get_item;
455 Parameter [] p = ii.Parameters.FixedParameters;
457 get_item = TypeBuilder.DefineMethod (
458 "get_Item", property_attributes, prop_type, arg_types);
459 pb.SetGetMethod (get_item);
461 // HACK because System.Reflection.Emit is lame
463 InternalParameters ip = new InternalParameters (
464 arg_types, ii.Parameters);
466 if (!RegisterMethod (get_item, ip, arg_types)) {
472 for (int i = 0; i < p.Length; i++)
473 get_item.DefineParameter (
475 p [i].Attributes, p [i].Name);
480 Parameter [] p = ii.Parameters.FixedParameters;
481 MethodBuilder set_item;
484 set_item = TypeBuilder.DefineMethod (
485 "set_Item", property_attributes,
486 TypeManager.void_type, value_arg_types);
487 pb.SetSetMethod (set_item);
489 // HACK because System.Reflection.Emit is lame
491 InternalParameters ip = new InternalParameters (
492 value_arg_types, ii.Parameters);
493 if (!RegisterMethod (set_item, ip, value_arg_types)) {
499 for (; i < p.Length; i++)
500 set_item.DefineParameter (
502 p [i].Attributes, p [i].Name);
505 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
510 /// Performs the semantic analysis for all the interface members
511 /// that were declared
513 bool SemanticAnalysis ()
515 Hashtable methods = new Hashtable ();
518 if (defined_method != null){
519 foreach (InterfaceMethod im in defined_method){
520 string sig = im.GetSignature (this);
523 // If there was an undefined Type on the signatures
528 if (methods [sig] != null){
536 // FIXME: Here I should check i
541 Type GetInterfaceTypeByName (string name)
543 Type t = FindType (name);
554 cause = "is a struct";
556 cause = "is a class";
558 cause = "Should not happen.";
560 Report.Error (527, Location, "`"+name+"' " + cause +
561 ", need an interface instead");
567 // Returns the list of interfaces that this interface implements
568 // Or null if it does not implement any interface.
570 // Sets the error boolean accoringly.
572 Type [] GetInterfaceBases (out bool error)
581 tbases = new Type [Bases.Count];
584 foreach (string name in Bases){
587 t = GetInterfaceTypeByName (name);
601 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
605 // Rework the way we recurse, because for recursive
606 // definitions of interfaces (A:B and B:A) we report the
607 // error twice, rather than once.
609 public override TypeBuilder DefineType ()
614 if (TypeBuilder != null)
622 ifaces = GetInterfaceBases (out error);
628 ModuleBuilder builder = CodeGen.ModuleBuilder;
630 TypeBuilder = builder.DefineType (
633 (Type)null, // Parent Type
635 RootContext.RegisterOrder (this);
637 TypeBuilder builder = Parent.TypeBuilder;
639 TypeBuilder = builder.DefineNestedType (
642 (Type) null, //parent type
645 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
646 tc.RegisterOrder (this);
649 TypeManager.AddUserInterface (Name, TypeBuilder, this);
656 /// Performs semantic analysis, and then generates the IL interfaces
658 public override bool Define (TypeContainer parent)
660 if (!SemanticAnalysis ())
663 if (defined_method != null){
664 foreach (InterfaceMethod im in defined_method)
668 if (defined_properties != null){
669 foreach (InterfaceProperty ip in defined_properties)
670 PopulateProperty (ip);
673 if (defined_events != null)
674 foreach (InterfaceEvent ie in defined_events)
677 if (defined_indexer != null) {
678 foreach (InterfaceIndexer ii in defined_indexer)
679 PopulateIndexer (ii);
681 CustomAttributeBuilder cb = EmitDefaultMemberAttr (parent, ModFlags, Location);
682 TypeBuilder.SetCustomAttribute (cb);
688 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent, int flags,
691 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
693 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
694 ".ctor", MemberTypes.Constructor,
695 BindingFlags.Public | BindingFlags.Instance,
698 if (!(ml is MethodGroupExpr)) {
699 Console.WriteLine ("Internal error !!!!");
703 MethodGroupExpr mg = (MethodGroupExpr) ml;
705 MethodBase constructor = mg.Methods [0];
707 string [] vals = { "Item" };
709 CustomAttributeBuilder cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
716 public class InterfaceMemberBase {
717 public readonly string Name;
718 public readonly bool IsNew;
719 public Attributes OptAttributes;
721 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
725 OptAttributes = attrs;
729 public class InterfaceProperty : InterfaceMemberBase {
730 public readonly bool HasSet;
731 public readonly bool HasGet;
732 public readonly string Type;
733 public readonly string type;
734 public readonly Location Location;
736 public InterfaceProperty (string type, string name,
737 bool is_new, bool has_get, bool has_set,
738 Attributes attrs, Location loc)
739 : base (name, is_new, attrs)
748 public class InterfaceEvent : InterfaceMemberBase {
749 public readonly string Type;
751 public InterfaceEvent (string type, string name, bool is_new, Attributes attrs)
752 : base (name, is_new, attrs)
758 public class InterfaceMethod : InterfaceMemberBase {
759 public readonly string ReturnType;
760 public readonly Parameters Parameters;
761 public readonly Location Location;
763 public InterfaceMethod (string return_type, string name, bool is_new, Parameters args,
764 Attributes attrs, Location l)
765 : base (name, is_new, attrs)
767 this.ReturnType = return_type;
768 this.Parameters = args;
773 /// Returns the signature for this interface method
775 public string GetSignature (DeclSpace ds)
777 Type ret = RootContext.LookupType (ds, ReturnType, false, Location);
778 string args = Parameters.GetSignature (ds);
780 if ((ret == null) || (args == null))
783 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
786 public Type [] ParameterTypes (DeclSpace ds)
788 return Parameters.GetParameterInfo (ds);
792 public class InterfaceIndexer : InterfaceMemberBase {
793 public readonly bool HasGet, HasSet;
794 public readonly Parameters Parameters;
795 public readonly string Type;
796 public readonly Location Location;
798 public InterfaceIndexer (string type, Parameters args, bool do_get, bool do_set,
799 bool is_new, Attributes attrs, Location loc)
800 : base ("", is_new, attrs)
809 public Type [] ParameterTypes (DeclSpace ds)
811 return Parameters.GetParameterInfo (ds);