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 (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
75 : base (parent, name, l)
77 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
78 OptAttributes = attrs;
80 method_builders = new ArrayList ();
81 property_builders = new ArrayList ();
82 event_builders = new ArrayList ();
85 public AdditionResult AddMethod (InterfaceMethod imethod)
87 string name = imethod.Name;
88 Object value = defined_names [name];
91 if (!(value is InterfaceMethod))
92 return AdditionResult.NameExists;
95 if (defined_method == null)
96 defined_method = new ArrayList ();
98 defined_method.Add (imethod);
100 DefineName (name, imethod);
102 return AdditionResult.Success;
105 public AdditionResult AddProperty (InterfaceProperty iprop)
108 string name = iprop.Name;
110 if ((res = IsValid (name)) != AdditionResult.Success)
113 DefineName (name, iprop);
115 if (defined_properties == null)
116 defined_properties = new ArrayList ();
118 defined_properties.Add (iprop);
119 return AdditionResult.Success;
122 public AdditionResult AddEvent (InterfaceEvent ievent)
124 string name = ievent.Name;
127 if ((res = IsValid (name)) != AdditionResult.Success)
130 DefineName (name, ievent);
132 if (defined_events == null)
133 defined_events = new ArrayList ();
135 defined_events.Add (ievent);
136 return AdditionResult.Success;
139 public bool AddIndexer (InterfaceIndexer iindexer)
141 if (defined_indexer == null)
142 defined_indexer = new ArrayList ();
144 defined_indexer.Add (iindexer);
148 public ArrayList InterfaceMethods {
150 return defined_method;
154 public ArrayList InterfaceProperties {
156 return defined_properties;
160 public ArrayList InterfaceEvents {
162 return defined_events;
166 public ArrayList InterfaceIndexers {
168 return defined_indexer;
172 public ArrayList Bases {
182 public virtual TypeAttributes InterfaceAttr {
184 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
186 if (IsTopLevel == false) {
188 if ((ModFlags & Modifiers.PROTECTED) != 0
189 && (ModFlags & Modifiers.INTERNAL) != 0)
190 x |= TypeAttributes.NestedFamORAssem;
191 else if ((ModFlags & Modifiers.PROTECTED) != 0)
192 x |= TypeAttributes.NestedFamily;
193 else if ((ModFlags & Modifiers.INTERNAL) != 0)
194 x |= TypeAttributes.NestedAssembly;
195 else if ((ModFlags & Modifiers.PUBLIC) != 0)
196 x |= TypeAttributes.NestedPublic;
198 x |= TypeAttributes.NestedPrivate;
200 if ((ModFlags & Modifiers.PUBLIC) != 0)
201 x |= TypeAttributes.Public;
202 else if ((ModFlags & Modifiers.PRIVATE) != 0)
203 x |= TypeAttributes.NotPublic;
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);
234 // This might trigger a definition of the methods. This happens only
235 // with Attributes, as Attribute classes are processed before interfaces.
236 // Ideally, we should make everything just define recursively in terms
237 // of its dependencies.
239 public MethodInfo [] GetMethods (TypeContainer container)
243 if (!members_defined){
244 if (DefineMembers (container))
245 n = method_builders.Count;
247 n = method_builders.Count;
249 MethodInfo [] mi = new MethodInfo [n];
251 method_builders.CopyTo (mi, 0);
256 // Hack around System.Reflection as found everywhere else
257 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
258 MemberFilter filter, object criteria)
260 ArrayList members = new ArrayList ();
262 if ((mt & MemberTypes.Method) != 0) {
263 foreach (MethodBuilder mb in method_builders)
264 if (filter (mb, criteria))
268 if ((mt & MemberTypes.Property) != 0) {
269 foreach (PropertyBuilder pb in property_builders)
270 if (filter (pb, criteria))
274 if ((mt & MemberTypes.Event) != 0) {
275 foreach (MyEventBuilder eb in event_builders)
276 if (filter (eb, criteria))
280 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
281 MemberList parent_mi;
283 parent_mi = TypeContainer.FindMembers (
284 TypeBuilder.BaseType, mt, bf, filter, criteria);
286 members.AddRange (parent_mi);
289 return new MemberList (members);
292 public override MemberCache MemberCache {
299 // Populates the methods in the interface
301 void PopulateMethod (TypeContainer parent, DeclSpace decl_space, InterfaceMethod im)
303 Type return_type = this.ResolveType (im.ReturnType, false, im.Location);
304 Type [] arg_types = im.ParameterTypes (this);
309 if (return_type == null)
312 if (return_type.IsPointer && !UnsafeOK (this))
315 if (arg_types == null)
318 foreach (Type t in arg_types){
323 if (t.IsPointer && !UnsafeOK (this))
330 mb = TypeBuilder.DefineMethod (
331 im.Name, interface_method_attributes,
332 return_type, arg_types);
334 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
336 if (!RegisterMethod (mb, ip, arg_types)) {
342 // Define each type attribute (in/out/ref) and
343 // the argument names.
345 p = im.Parameters.FixedParameters;
347 for (i = 0; i < p.Length; i++)
348 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
350 if (i != arg_types.Length)
351 Console.WriteLine ("Implement the type definition for params");
354 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
355 return_type, ModFlags, false);
357 if (im.OptAttributes != null)
358 Attribute.ApplyAttributes (ec, mb, im, im.OptAttributes, Location);
362 // Populates the properties in the interface
364 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
367 MethodBuilder get = null, set = null;
368 ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
369 Type prop_type = ip.Type.Type;
370 Type [] setter_args = new Type [1];
372 if (prop_type == null)
375 if (prop_type.IsPointer && !UnsafeOK (this))
378 setter_args [0] = prop_type;
381 // FIXME: properties are missing the following
382 // flags: hidebysig newslot specialname
384 pb = TypeBuilder.DefineProperty (
385 ip.Name, PropertyAttributes.None,
389 get = TypeBuilder.DefineMethod (
390 "get_" + ip.Name, property_attributes ,
394 // HACK because System.Reflection.Emit is lame
396 Type [] null_types = null;
397 InternalParameters inp = new InternalParameters
398 (null_types, Parameters.EmptyReadOnlyParameters);
400 if (!RegisterMethod (get, inp, null)) {
405 pb.SetGetMethod (get);
409 setter_args [0] = prop_type;
411 set = TypeBuilder.DefineMethod (
412 "set_" + ip.Name, property_attributes,
413 TypeManager.void_type, setter_args);
415 set.DefineParameter (1, ParameterAttributes.None, "value");
416 pb.SetSetMethod (set);
419 // HACK because System.Reflection.Emit is lame
421 Parameter [] parms = new Parameter [1];
422 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
423 InternalParameters ipp = new InternalParameters (
424 this, new Parameters (parms, null, Location.Null));
426 if (!RegisterMethod (set, ipp, setter_args)) {
432 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
433 null, ModFlags, false);
435 if (ip.OptAttributes != null)
436 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes, Location);
438 TypeManager.RegisterProperty (pb, get, set);
439 property_builders.Add (pb);
443 // Populates the events in the interface
445 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
448 // FIXME: We need to do this after delegates have been
449 // declared or we declare them recursively.
452 MethodBuilder add = null, remove = null;
453 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
454 Type event_type = ie.Type.Type;
456 if (event_type == null)
459 if (event_type.IsPointer && !UnsafeOK (this))
462 Type [] parameters = new Type [1];
463 parameters [0] = event_type;
465 eb = new MyEventBuilder (TypeBuilder, ie.Name,
466 EventAttributes.None, event_type);
469 // Now define the accessors
471 string add_name = "add_" + ie.Name;
473 add = TypeBuilder.DefineMethod (
474 add_name, property_attributes, null, parameters);
475 add.DefineParameter (1, ParameterAttributes.None, "value");
476 eb.SetAddOnMethod (add);
478 string remove_name = "remove_" + ie.Name;
479 remove = TypeBuilder.DefineMethod (
480 remove_name, property_attributes, null, parameters);
481 remove.DefineParameter (1, ParameterAttributes.None, "value");
482 eb.SetRemoveOnMethod (remove);
484 Parameter [] parms = new Parameter [1];
485 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
486 InternalParameters ip = new InternalParameters (
487 this, new Parameters (parms, null, Location.Null));
489 if (!RegisterMethod (add, ip, parameters)) {
494 if (!RegisterMethod (remove, ip, parameters)) {
499 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
500 null, ModFlags, false);
503 if (ie.OptAttributes != null)
504 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes, Location);
506 TypeManager.RegisterEvent (eb, add, remove);
507 event_builders.Add (eb);
511 // Populates the indexers in the interface
513 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
516 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
517 Type prop_type = ii.Type.Type;
518 Type [] arg_types = ii.ParameterTypes (this);
519 Type [] value_arg_types;
521 if (prop_type == null)
524 if (prop_type.IsPointer && !UnsafeOK (this))
528 // Sets up the extra invisible `value' argument for setters.
530 if (arg_types != null){
531 int count = arg_types.Length;
532 value_arg_types = new Type [count + 1];
534 arg_types.CopyTo (value_arg_types, 0);
535 value_arg_types [count] = prop_type;
537 foreach (Type t in arg_types){
538 if (t.IsPointer && !UnsafeOK (this))
542 value_arg_types = new Type [1];
544 value_arg_types [1] = prop_type;
547 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
548 null, ModFlags, false);
550 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
551 if (IndexerName == null)
552 IndexerName = "Item";
554 pb = TypeBuilder.DefineProperty (
555 IndexerName, PropertyAttributes.None,
556 prop_type, arg_types);
558 MethodBuilder set_item = null, get_item = null;
560 Parameter [] p = ii.Parameters.FixedParameters;
562 get_item = TypeBuilder.DefineMethod (
563 "get_" + IndexerName, property_attributes,
564 prop_type, arg_types);
565 pb.SetGetMethod (get_item);
567 // HACK because System.Reflection.Emit is lame
569 InternalParameters ip = new InternalParameters (
570 arg_types, ii.Parameters);
572 if (!RegisterMethod (get_item, ip, arg_types)) {
578 for (int i = 0; i < p.Length; i++)
579 get_item.DefineParameter (
581 p [i].Attributes, p [i].Name);
586 Parameter [] p = ii.Parameters.FixedParameters;
590 pv = new Parameter [p.Length + 1];
592 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
593 Parameters value_params = new Parameters (pv, null, Location.Null);
594 value_params.GetParameterInfo (decl_space);
596 set_item = TypeBuilder.DefineMethod (
597 "set_" + IndexerName, property_attributes,
598 TypeManager.void_type, value_arg_types);
599 pb.SetSetMethod (set_item);
601 // HACK because System.Reflection.Emit is lame
603 InternalParameters ip = new InternalParameters (
604 value_arg_types, value_params);
605 if (!RegisterMethod (set_item, ip, value_arg_types)) {
611 for (; i < p.Length; i++)
612 set_item.DefineParameter (
614 p [i].Attributes, p [i].Name);
617 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
620 if (ii.OptAttributes != null)
621 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes, Location);
623 property_builders.Add (pb);
627 /// Performs the semantic analysis for all the interface members
628 /// that were declared
630 bool SemanticAnalysis ()
632 Hashtable methods = new Hashtable ();
635 if (defined_method != null){
636 foreach (InterfaceMethod im in defined_method){
637 string sig = im.GetSignature (this);
640 // If there was an undefined Type on the signatures
645 if (methods [sig] != null){
653 // FIXME: Here I should check i
658 Type GetInterfaceTypeByName (string name)
660 Type t = FindType (Location, name);
663 Report.Error (246, Location, "The type or namespace `" + name +
664 "' could not be found");
674 cause = "is a struct";
676 cause = "is a class";
678 cause = "Should not happen.";
680 Report.Error (527, Location, "`"+name+"' " + cause +
681 ", need an interface instead");
687 // Returns the list of interfaces that this interface implements
688 // Or null if it does not implement any interface.
690 // Sets the error boolean accoringly.
692 Type [] GetInterfaceBases (out bool error)
701 tbases = new Type [Bases.Count];
704 foreach (string name in Bases){
707 t = GetInterfaceTypeByName (name);
713 if (!Parent.AsAccessible (t, ModFlags))
714 Report.Error (61, Location,
715 "Inconsistent accessibility: base interface `" +
716 TypeManager.CSharpName (t) + "' is less " +
717 "accessible than interface `" +
723 return TypeManager.ExpandInterfaces (tbases);
728 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
732 // Rework the way we recurse, because for recursive
733 // definitions of interfaces (A:B and B:A) we report the
734 // error twice, rather than once.
736 public override TypeBuilder DefineType ()
741 if (TypeBuilder != null)
749 ifaces = GetInterfaceBases (out error);
755 ModuleBuilder builder = CodeGen.ModuleBuilder;
757 TypeBuilder = builder.DefineType (
760 (Type)null, // Parent Type
762 RootContext.RegisterOrder (this);
764 TypeBuilder builder = Parent.TypeBuilder;
766 TypeBuilder = builder.DefineNestedType (
769 (Type) null, //parent type
772 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
773 tc.RegisterOrder (this);
776 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
783 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
784 // interface is consistent. Either it is `Item' or it is the name defined by all the
785 // indexers with the `IndexerName' attribute.
787 // Turns out that the IndexerNameAttribute is applied to each indexer,
788 // but it is never emitted, instead a DefaultName attribute is attached
791 void DefineIndexers (TypeContainer parent)
793 string interface_indexer_name = null;
795 foreach (InterfaceIndexer ii in defined_indexer){
797 PopulateIndexer (parent, this, ii);
799 if (interface_indexer_name == null){
800 interface_indexer_name = IndexerName;
804 if (IndexerName == interface_indexer_name)
808 668, "Two indexers have different names, " +
809 " you should use the same name for all your indexers");
811 if (interface_indexer_name == null)
812 interface_indexer_name = "Item";
813 IndexerName = interface_indexer_name;
817 /// Performs semantic analysis, and then generates the IL interfaces
819 public override bool DefineMembers (TypeContainer parent)
821 if (!SemanticAnalysis ())
824 if (defined_method != null){
825 foreach (InterfaceMethod im in defined_method)
826 PopulateMethod (parent, this, im);
829 if (defined_properties != null){
830 foreach (InterfaceProperty ip in defined_properties)
831 PopulateProperty (parent, this, ip);
834 if (defined_events != null)
835 foreach (InterfaceEvent ie in defined_events)
836 PopulateEvent (parent, this, ie);
838 if (defined_indexer != null) {
839 DefineIndexers (parent);
841 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
842 parent, IndexerName, ModFlags, Location);
844 TypeBuilder.SetCustomAttribute (cb);
848 if (TypeBuilder.BaseType != null)
849 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
851 member_cache = new MemberCache (this);
853 members_defined = true;
858 /// Applies all the attributes.
860 public override bool Define (TypeContainer parent)
862 if (OptAttributes != null) {
863 EmitContext ec = new EmitContext (parent, this, Location, null, null,
865 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
871 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
876 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
878 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
879 ".ctor", MemberTypes.Constructor,
880 BindingFlags.Public | BindingFlags.Instance,
883 if (!(ml is MethodGroupExpr)) {
884 Console.WriteLine ("Internal error !!!!");
888 MethodGroupExpr mg = (MethodGroupExpr) ml;
890 MethodBase constructor = mg.Methods [0];
892 string [] vals = { name };
894 CustomAttributeBuilder cb = null;
896 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
898 Report.Warning (-100, "Can not set the indexer default member attribute");
908 string IMemberContainer.Name {
914 Type IMemberContainer.Type {
920 IMemberContainer IMemberContainer.Parent {
922 return parent_container;
926 MemberCache IMemberContainer.MemberCache {
932 bool IMemberContainer.IsInterface {
938 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
940 // Interfaces only contain instance members.
941 if ((bf & BindingFlags.Instance) == 0)
942 return MemberList.Empty;
943 if ((bf & BindingFlags.Public) == 0)
944 return MemberList.Empty;
946 ArrayList members = new ArrayList ();
948 if ((mt & MemberTypes.Method) != 0)
949 members.AddRange (method_builders);
951 if ((mt & MemberTypes.Property) != 0)
952 members.AddRange (property_builders);
954 if ((mt & MemberTypes.Event) != 0)
955 members.AddRange (event_builders);
957 return new MemberList (members);
961 public class InterfaceMemberBase {
962 public readonly string Name;
963 public readonly bool IsNew;
964 public Attributes OptAttributes;
966 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
970 OptAttributes = attrs;
974 public class InterfaceProperty : InterfaceMemberBase {
975 public readonly bool HasSet;
976 public readonly bool HasGet;
977 public readonly Location Location;
978 public Expression Type;
980 public InterfaceProperty (Expression type, string name,
981 bool is_new, bool has_get, bool has_set,
982 Attributes attrs, Location loc)
983 : base (name, is_new, attrs)
992 public class InterfaceEvent : InterfaceMemberBase {
993 public readonly Location Location;
994 public Expression Type;
996 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
998 : base (name, is_new, attrs)
1005 public class InterfaceMethod : InterfaceMemberBase {
1006 public readonly Expression ReturnType;
1007 public readonly Parameters Parameters;
1008 public readonly Location Location;
1010 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1011 Attributes attrs, Location l)
1012 : base (name, is_new, attrs)
1014 this.ReturnType = return_type;
1015 this.Parameters = args;
1020 /// Returns the signature for this interface method
1022 public string GetSignature (DeclSpace ds)
1024 Type ret = ds.ResolveType (ReturnType, false, Location);
1025 string args = Parameters.GetSignature (ds);
1027 if ((ret == null) || (args == null))
1030 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1033 public Type [] ParameterTypes (DeclSpace ds)
1035 return Parameters.GetParameterInfo (ds);
1039 public class InterfaceIndexer : InterfaceMemberBase {
1040 public readonly bool HasGet, HasSet;
1041 public readonly Parameters Parameters;
1042 public readonly Location Location;
1043 public Expression Type;
1045 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1046 bool is_new, Attributes attrs, Location loc)
1047 : base ("", is_new, attrs)
1056 public Type [] ParameterTypes (DeclSpace ds)
1058 return Parameters.GetParameterInfo (ds);