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;
19 public class Interface : DeclSpace {
20 const MethodAttributes interface_method_attributes =
21 MethodAttributes.Public |
22 MethodAttributes.Abstract |
23 MethodAttributes.HideBySig |
24 MethodAttributes.NewSlot |
25 MethodAttributes.Virtual;
27 const MethodAttributes property_attributes =
28 MethodAttributes.Public |
29 MethodAttributes.Abstract |
30 MethodAttributes.HideBySig |
31 MethodAttributes.NewSlot |
32 MethodAttributes.SpecialName |
33 MethodAttributes.Virtual;
38 ArrayList defined_method;
39 ArrayList defined_indexer;
40 ArrayList defined_events;
41 ArrayList defined_properties;
43 ArrayList method_builders;
44 ArrayList property_builders;
48 Attributes OptAttributes;
50 public readonly RootContext RootContext;
52 // These will happen after the semantic analysis
54 // Hashtable defined_indexers;
55 // Hashtable defined_methods;
58 // Modifiers allowed in a class declaration
60 public const int AllowedModifiers =
67 public Interface (RootContext rc, TypeContainer parent, string name, int mod, Attributes attrs, Location l)
70 this.mod_flags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PUBLIC);
72 OptAttributes = attrs;
75 method_builders = new ArrayList ();
76 property_builders = new ArrayList ();
79 public AdditionResult AddMethod (InterfaceMethod imethod)
81 string name = imethod.Name;
82 Object value = defined_names [name];
85 if (!(value is InterfaceMethod))
86 return AdditionResult.NameExists;
89 if (defined_method == null)
90 defined_method = new ArrayList ();
92 defined_method.Add (imethod);
94 DefineName (name, imethod);
96 return AdditionResult.Success;
99 public AdditionResult AddProperty (InterfaceProperty iprop)
102 string name = iprop.Name;
104 if ((res = IsValid (name)) != AdditionResult.Success)
107 DefineName (name, iprop);
109 if (defined_properties == null)
110 defined_properties = new ArrayList ();
112 defined_properties.Add (iprop);
113 return AdditionResult.Success;
116 public AdditionResult AddEvent (InterfaceEvent ievent)
118 string name = ievent.Name;
121 if ((res = IsValid (name)) != AdditionResult.Success)
124 DefineName (name, ievent);
126 if (defined_events == null)
127 defined_events = new ArrayList ();
129 defined_events.Add (ievent);
130 return AdditionResult.Success;
133 public bool AddIndexer (InterfaceIndexer iindexer)
135 if (defined_indexer == null)
136 defined_indexer = new ArrayList ();
138 defined_indexer.Add (iindexer);
142 public ArrayList InterfaceMethods {
144 return defined_method;
148 public ArrayList InterfaceProperties {
150 return defined_properties;
154 public ArrayList InterfaceEvents {
156 return defined_events;
160 public ArrayList InterfaceIndexers {
162 return defined_indexer;
166 public int ModFlags {
172 public ArrayList Bases {
182 public bool IsTopLevel {
185 if (parent.Parent == null)
192 public virtual TypeAttributes InterfaceAttr {
194 TypeAttributes x = 0;
196 if ((mod_flags & Modifiers.PUBLIC) != 0)
197 x |= TypeAttributes.Public;
198 else if ((mod_flags & Modifiers.PRIVATE) != 0)
199 x |= TypeAttributes.NotPublic;
201 if (IsTopLevel == false) {
203 if ((mod_flags & Modifiers.PROTECTED) != 0
204 && (mod_flags & Modifiers.INTERNAL) != 0)
205 x |= TypeAttributes.NestedFamORAssem;
207 if ((mod_flags & Modifiers.PROTECTED) != 0)
208 x |= TypeAttributes.NestedFamily;
210 if ((mod_flags & Modifiers.INTERNAL) != 0)
211 x |= TypeAttributes.NestedAssembly;
215 if ((mod_flags & Modifiers.ABSTRACT) != 0)
216 x |= TypeAttributes.Abstract;
218 if ((mod_flags & Modifiers.SEALED) != 0)
219 x |= TypeAttributes.Sealed;
225 void Error111 (InterfaceMethod im)
229 "Interface `" + Name + "' already contains a definition with the " +
230 "same return value and paramenter types for method `" + im.Name + "'");
233 void RegisterMethod (MethodBase mb, Type [] types)
235 TypeManager.RegisterMethod (mb, types);
236 method_builders.Add (mb);
239 public MethodInfo [] GetMethods ()
241 int n = method_builders.Count;
242 MethodInfo [] mi = new MethodInfo [n];
244 method_builders.CopyTo (mi, 0);
249 // Hack around System.Reflection as found everywhere else
250 public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
252 ArrayList members = new ArrayList ();
254 if ((mt & MemberTypes.Method) != 0) {
255 foreach (MethodBuilder mb in method_builders)
256 if (filter (mb, criteria))
260 if ((mt & MemberTypes.Property) != 0) {
261 foreach (PropertyBuilder pb in property_builders)
262 if (filter (pb, criteria))
266 // The rest of the cases, if any, are unhandled at present.
268 int count = members.Count;
271 MemberInfo [] mi = new MemberInfo [count];
272 members.CopyTo (mi, 0);
281 // Populates the methods in the interface
283 void PopulateMethod (InterfaceMethod im)
285 Type return_type = parent.LookupType (im.ReturnType, true);
286 Type [] arg_types = im.ParameterTypes (parent);
294 mb = TypeBuilder.DefineMethod (
295 im.Name, interface_method_attributes,
296 return_type, arg_types);
298 RegisterMethod (mb, arg_types);
301 // Define each type attribute (in/out/ref) and
302 // the argument names.
304 p = im.Parameters.FixedParameters;
306 for (i = 0; i < p.Length; i++)
307 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
309 if (i != arg_types.Length)
310 Console.WriteLine ("Implement the type definition for params");
315 // Populates the properties in the interface
317 void PopulateProperty (InterfaceProperty ip)
321 Type prop_type = parent.LookupType (ip.Type, true);
322 Type [] setter_args = new Type [1];
324 setter_args [0] = prop_type;
327 // FIXME: properties are missing the following
328 // flags: hidebysig newslot specialname
330 pb = TypeBuilder.DefineProperty (
331 ip.Name, PropertyAttributes.None,
335 mb = TypeBuilder.DefineMethod (
336 "get_" + ip.Name, property_attributes ,
340 // HACK because System.Reflection.Emit is lame
342 RegisterMethod (mb, null);
344 pb.SetGetMethod (mb);
348 setter_args [0] = prop_type;
350 mb = TypeBuilder.DefineMethod (
351 "set_" + ip.Name, property_attributes,
354 mb.DefineParameter (1, ParameterAttributes.None, "value");
355 pb.SetSetMethod (mb);
358 // HACK because System.Reflection.Emit is lame
360 RegisterMethod (mb, setter_args);
363 property_builders.Add (pb);
367 // Populates the events in the interface
369 void PopulateEvent (InterfaceEvent ie)
372 // FIXME: We need to do this after delegates have been
373 // declared or we declare them recursively.
378 // Populates the indexers in the interface
380 void PopulateIndexer (InterfaceIndexer ii)
383 Type prop_type = parent.LookupType (ii.Type, true);
384 Type [] arg_types = ii.ParameterTypes (parent);
385 Type [] value_arg_types;
388 // Sets up the extra invisible `value' argument for setters.
390 if (arg_types != null){
391 int count = arg_types.Length;
392 value_arg_types = new Type [count + 1];
394 arg_types.CopyTo (value_arg_types, 0);
395 value_arg_types [count] = prop_type;
397 value_arg_types = new Type [1];
399 value_arg_types [1] = prop_type;
402 pb = TypeBuilder.DefineProperty (
403 "Item", PropertyAttributes.None,
404 prop_type, arg_types);
407 MethodBuilder get_item;
408 Parameter [] p = ii.Parameters.FixedParameters;
410 get_item = TypeBuilder.DefineMethod (
411 "get_Item", property_attributes, prop_type, arg_types);
412 pb.SetGetMethod (get_item);
414 // HACK because System.Reflection.Emit is lame
416 RegisterMethod (get_item, arg_types);
419 for (int i = 0; i < p.Length; i++)
420 get_item.DefineParameter (
422 p [i].Attributes, p [i].Name);
427 Parameter [] p = ii.Parameters.FixedParameters;
428 MethodBuilder set_item;
431 set_item = TypeBuilder.DefineMethod (
432 "set_Item", property_attributes, null, value_arg_types);
433 pb.SetSetMethod (set_item);
435 // HACK because System.Reflection.Emit is lame
437 RegisterMethod (set_item, value_arg_types);
440 for (; i < p.Length; i++)
441 set_item.DefineParameter (
443 p [i].Attributes, p [i].Name);
445 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
450 // Performs the semantic analysis for all the interface members
451 // that were declared
453 bool SemanticAnalysis ()
455 Hashtable methods = new Hashtable ();
458 if (defined_method != null){
459 foreach (InterfaceMethod im in defined_method){
460 string sig = im.GetSignature (parent);
463 // If there was an undefined Type on the signatures
468 if (methods [sig] != null){
476 // FIXME: Here I should check i
482 // Returns the Type that represents the interface whose name
486 Type GetInterfaceTypeByName (object builder, string name)
489 Type t = RootContext.TypeManager.LookupType (name);
499 cause = "is a struct";
501 cause = "is a class";
503 cause = "Should not happen.";
505 Report.Error (527, "`"+name+"' " + cause + ", need an interface instead");
510 Tree tree = RootContext.Tree;
511 parent = (Interface) tree.Interfaces [name];
513 string cause = "is undefined";
515 if (tree.Classes [name] != null)
516 cause = "is a class";
517 else if (tree.Structs [name] != null)
518 cause = "is a struct";
520 Report.Error (527, "`"+name+"' " + cause + ", need an interface instead");
524 t = parent.DefineInterface (builder);
527 "Inherited interface `"+name+"' is circular");
535 // Returns the list of interfaces that this interface implements
536 // Or null if it does not implement any interface.
538 // Sets the error boolean accoringly.
540 Type [] GetInterfaceBases (object builder, out bool error)
549 tbases = new Type [Bases.Count];
552 foreach (string name in Bases){
555 t = GetInterfaceTypeByName (builder, name);
569 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
572 // Rework the way we recurse, because for recursive
573 // definitions of interfaces (A:B and B:A) we report the
574 // error twice, rather than once.
576 public TypeBuilder DefineInterface (object parent_builder)
586 ifaces = GetInterfaceBases (parent_builder, out error);
591 if (parent_builder is ModuleBuilder) {
592 ModuleBuilder builder = (ModuleBuilder) parent_builder;
594 TypeBuilder = builder.DefineType (Name,
595 TypeAttributes.Interface |
597 TypeAttributes.Abstract,
601 TypeBuilder builder = (TypeBuilder) parent_builder;
603 TypeBuilder = builder.DefineNestedType (Name,
604 TypeAttributes.Interface |
606 TypeAttributes.Abstract,
611 RootContext.TypeManager.AddUserInterface (Name, TypeBuilder, this);
619 // Performs semantic analysis, and then generates the IL interfaces
621 public void Populate ()
623 if (!SemanticAnalysis ())
626 if (defined_method != null){
627 foreach (InterfaceMethod im in defined_method)
631 if (defined_properties != null){
632 foreach (InterfaceProperty ip in defined_properties)
633 PopulateProperty (ip);
636 if (defined_events != null)
637 foreach (InterfaceEvent ie in defined_events)
640 if (defined_indexer != null)
641 foreach (InterfaceIndexer ii in defined_indexer)
642 PopulateIndexer (ii);
645 public void CloseType ()
647 TypeBuilder.CreateType ();
652 public class InterfaceMemberBase {
653 public readonly string Name;
654 public readonly bool IsNew;
655 public Attributes OptAttributes;
657 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
661 OptAttributes = attrs;
665 public class InterfaceProperty : InterfaceMemberBase {
666 public readonly bool HasSet;
667 public readonly bool HasGet;
668 public readonly string Type;
669 public readonly string type;
671 public InterfaceProperty (string type, string name,
672 bool is_new, bool has_get, bool has_set, Attributes attrs)
673 : base (name, is_new, attrs)
681 public class InterfaceEvent : InterfaceMemberBase {
682 public readonly string Type;
684 public InterfaceEvent (string type, string name, bool is_new, Attributes attrs)
685 : base (name, is_new, attrs)
691 public class InterfaceMethod : InterfaceMemberBase {
692 public readonly string ReturnType;
693 public readonly Parameters Parameters;
695 public InterfaceMethod (string return_type, string name, bool is_new, Parameters args, Attributes attrs)
696 : base (name, is_new, attrs)
698 this.ReturnType = return_type;
699 this.Parameters = args;
703 // Returns the signature for this interface method
705 public string GetSignature (TypeContainer tc)
707 Type ret = tc.LookupType (ReturnType, false);
708 string args = Parameters.GetSignature (tc);
710 if ((ret == null) || (args == null))
713 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
716 public Type [] ParameterTypes (TypeContainer tc)
718 return Parameters.GetParameterInfo (tc);
722 public class InterfaceIndexer : InterfaceMemberBase {
723 public readonly bool HasGet, HasSet;
724 public readonly Parameters Parameters;
725 public readonly string Type;
727 public InterfaceIndexer (string type, Parameters args, bool do_get, bool do_set, bool is_new,
729 : base ("", is_new, attrs)
737 public Type [] ParameterTypes (TypeContainer tc)
739 return Parameters.GetParameterInfo (tc);