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);
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);
372 Type prop_type = ip.Type.Type;
373 Type [] setter_args = new Type [1];
375 if (prop_type == null)
378 if (prop_type.IsPointer && !UnsafeOK (this))
381 setter_args [0] = prop_type;
384 // FIXME: properties are missing the following
385 // flags: hidebysig newslot specialname
387 pb = TypeBuilder.DefineProperty (
388 ip.Name, PropertyAttributes.None,
392 get = TypeBuilder.DefineMethod (
393 "get_" + ip.Name, property_attributes ,
397 // HACK because System.Reflection.Emit is lame
399 Type [] null_types = null;
400 InternalParameters inp = new InternalParameters
401 (null_types, Parameters.EmptyReadOnlyParameters);
403 if (!RegisterMethod (get, inp, null)) {
408 pb.SetGetMethod (get);
412 setter_args [0] = prop_type;
414 set = TypeBuilder.DefineMethod (
415 "set_" + ip.Name, property_attributes,
416 TypeManager.void_type, setter_args);
418 set.DefineParameter (1, ParameterAttributes.None, "value");
419 pb.SetSetMethod (set);
422 // HACK because System.Reflection.Emit is lame
424 Parameter [] parms = new Parameter [1];
425 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
426 InternalParameters ipp = new InternalParameters (
427 this, new Parameters (parms, null, Location.Null));
429 if (!RegisterMethod (set, ipp, setter_args)) {
435 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
436 null, ModFlags, false);
438 if (ip.OptAttributes != null)
439 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes);
441 TypeManager.RegisterProperty (pb, get, set);
442 property_builders.Add (pb);
446 // Populates the events in the interface
448 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
451 // FIXME: We need to do this after delegates have been
452 // declared or we declare them recursively.
455 MethodBuilder add = null, remove = null;
456 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
460 Type event_type = ie.Type.Type;
462 if (event_type == null)
465 if (event_type.IsPointer && !UnsafeOK (this))
468 Type [] parameters = new Type [1];
469 parameters [0] = event_type;
471 eb = new MyEventBuilder (null, TypeBuilder, ie.Name,
472 EventAttributes.None, event_type);
475 // Now define the accessors
477 string add_name = "add_" + ie.Name;
479 add = TypeBuilder.DefineMethod (
480 add_name, property_attributes, null, parameters);
481 add.DefineParameter (1, ParameterAttributes.None, "value");
482 eb.SetAddOnMethod (add);
484 string remove_name = "remove_" + ie.Name;
485 remove = TypeBuilder.DefineMethod (
486 remove_name, property_attributes, null, parameters);
487 remove.DefineParameter (1, ParameterAttributes.None, "value");
488 eb.SetRemoveOnMethod (remove);
490 Parameter [] parms = new Parameter [1];
491 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
492 InternalParameters ip = new InternalParameters (
493 this, new Parameters (parms, null, Location.Null));
495 if (!RegisterMethod (add, ip, parameters)) {
500 if (!RegisterMethod (remove, ip, parameters)) {
505 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
506 null, ModFlags, false);
509 if (ie.OptAttributes != null)
510 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes);
512 TypeManager.RegisterEvent (eb, add, remove);
513 event_builders.Add (eb);
517 // Populates the indexers in the interface
519 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
522 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
526 Type prop_type = ii.Type.Type;
527 Type [] arg_types = ii.ParameterTypes (this);
528 Type [] value_arg_types;
530 if (prop_type == null)
533 if (prop_type.IsPointer && !UnsafeOK (this))
537 // Sets up the extra invisible `value' argument for setters.
539 if (arg_types != null){
540 int count = arg_types.Length;
541 value_arg_types = new Type [count + 1];
543 arg_types.CopyTo (value_arg_types, 0);
544 value_arg_types [count] = prop_type;
546 foreach (Type t in arg_types){
547 if (t.IsPointer && !UnsafeOK (this))
551 value_arg_types = new Type [1];
553 value_arg_types [1] = prop_type;
556 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
557 null, ModFlags, false);
559 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
560 if (IndexerName == null)
561 IndexerName = "Item";
563 pb = TypeBuilder.DefineProperty (
564 IndexerName, PropertyAttributes.None,
565 prop_type, arg_types);
567 MethodBuilder set_item = null, get_item = null;
569 Parameter [] p = ii.Parameters.FixedParameters;
571 get_item = TypeBuilder.DefineMethod (
572 "get_" + IndexerName, property_attributes,
573 prop_type, arg_types);
574 pb.SetGetMethod (get_item);
576 // HACK because System.Reflection.Emit is lame
578 InternalParameters ip = new InternalParameters (
579 arg_types, ii.Parameters);
581 if (!RegisterMethod (get_item, ip, arg_types)) {
587 for (int i = 0; i < p.Length; i++)
588 get_item.DefineParameter (
590 p [i].Attributes, p [i].Name);
595 Parameter [] p = ii.Parameters.FixedParameters;
599 pv = new Parameter [p.Length + 1];
601 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
602 Parameters value_params = new Parameters (pv, null, Location.Null);
603 value_params.GetParameterInfo (decl_space);
605 set_item = TypeBuilder.DefineMethod (
606 "set_" + IndexerName, property_attributes,
607 TypeManager.void_type, value_arg_types);
608 pb.SetSetMethod (set_item);
610 // HACK because System.Reflection.Emit is lame
612 InternalParameters ip = new InternalParameters (
613 value_arg_types, value_params);
614 if (!RegisterMethod (set_item, ip, value_arg_types)) {
620 for (; i < p.Length; i++)
621 set_item.DefineParameter (
623 p [i].Attributes, p [i].Name);
626 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
629 if (ii.OptAttributes != null)
630 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes);
632 property_builders.Add (pb);
636 /// Performs the semantic analysis for all the interface members
637 /// that were declared
639 bool SemanticAnalysis ()
641 Hashtable methods = new Hashtable ();
644 if (defined_method != null){
645 foreach (InterfaceMethod im in defined_method){
646 string sig = im.GetSignature (this);
649 // If there was an undefined Type on the signatures
654 if (methods [sig] != null){
662 // FIXME: Here I should check i
667 Type GetInterfaceTypeByName (string name)
669 Type t = FindType (Location, name);
672 Report.Error (246, Location, "The type or namespace `" + name +
673 "' could not be found");
683 cause = "is a struct";
685 cause = "is a class";
687 cause = "Should not happen.";
689 Report.Error (527, Location, "`"+name+"' " + cause +
690 ", need an interface instead");
696 // Returns the list of interfaces that this interface implements
697 // Or null if it does not implement any interface.
699 // Sets the error boolean accoringly.
701 Type [] GetInterfaceBases (out bool error)
710 tbases = new Type [Bases.Count];
713 foreach (string name in Bases){
716 t = GetInterfaceTypeByName (name);
722 if (!Parent.AsAccessible (t, ModFlags))
723 Report.Error (61, Location,
724 "Inconsistent accessibility: base interface `" +
725 TypeManager.CSharpName (t) + "' is less " +
726 "accessible than interface `" +
732 return TypeManager.ExpandInterfaces (tbases);
737 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
741 // Rework the way we recurse, because for recursive
742 // definitions of interfaces (A:B and B:A) we report the
743 // error twice, rather than once.
745 public override TypeBuilder DefineType ()
750 if (TypeBuilder != null)
758 ifaces = GetInterfaceBases (out error);
764 ModuleBuilder builder = CodeGen.ModuleBuilder;
766 TypeBuilder = builder.DefineType (
769 (Type)null, // Parent Type
771 RootContext.RegisterOrder (this);
773 TypeBuilder builder = Parent.TypeBuilder;
775 TypeBuilder = builder.DefineNestedType (
778 (Type) null, //parent type
781 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
782 tc.RegisterOrder (this);
785 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
792 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
793 // interface is consistent. Either it is `Item' or it is the name defined by all the
794 // indexers with the `IndexerName' attribute.
796 // Turns out that the IndexerNameAttribute is applied to each indexer,
797 // but it is never emitted, instead a DefaultName attribute is attached
800 void DefineIndexers (TypeContainer parent)
802 string interface_indexer_name = null;
804 foreach (InterfaceIndexer ii in defined_indexer){
806 PopulateIndexer (parent, this, ii);
808 if (interface_indexer_name == null){
809 interface_indexer_name = IndexerName;
813 if (IndexerName == interface_indexer_name)
817 668, "Two indexers have different names, " +
818 " you should use the same name for all your indexers");
820 if (interface_indexer_name == null)
821 interface_indexer_name = "Item";
822 IndexerName = interface_indexer_name;
826 /// Performs semantic analysis, and then generates the IL interfaces
828 public override bool DefineMembers (TypeContainer parent)
830 if (!SemanticAnalysis ())
833 if (defined_method != null){
834 foreach (InterfaceMethod im in defined_method)
835 PopulateMethod (parent, this, im);
838 if (defined_properties != null){
839 foreach (InterfaceProperty ip in defined_properties)
840 PopulateProperty (parent, this, ip);
843 if (defined_events != null)
844 foreach (InterfaceEvent ie in defined_events)
845 PopulateEvent (parent, this, ie);
847 if (defined_indexer != null) {
848 DefineIndexers (parent);
850 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
851 parent, IndexerName, ModFlags, Location);
853 TypeBuilder.SetCustomAttribute (cb);
857 if (TypeBuilder.BaseType != null)
858 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
860 member_cache = new MemberCache (this);
862 members_defined = true;
867 /// Applies all the attributes.
869 public override bool Define (TypeContainer parent)
871 if (OptAttributes != null) {
872 EmitContext ec = new EmitContext (parent, this, Location, null, null,
874 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
880 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
885 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
887 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
888 ".ctor", MemberTypes.Constructor,
889 BindingFlags.Public | BindingFlags.Instance,
892 if (!(ml is MethodGroupExpr)) {
893 Console.WriteLine ("Internal error !!!!");
897 MethodGroupExpr mg = (MethodGroupExpr) ml;
899 MethodBase constructor = mg.Methods [0];
901 string [] vals = { name };
903 CustomAttributeBuilder cb = null;
905 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
907 Report.Warning (-100, "Can not set the indexer default member attribute");
917 string IMemberContainer.Name {
923 Type IMemberContainer.Type {
929 IMemberContainer IMemberContainer.Parent {
931 return parent_container;
935 MemberCache IMemberContainer.MemberCache {
941 bool IMemberContainer.IsInterface {
947 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
949 // Interfaces only contain instance members.
950 if ((bf & BindingFlags.Instance) == 0)
951 return MemberList.Empty;
952 if ((bf & BindingFlags.Public) == 0)
953 return MemberList.Empty;
955 ArrayList members = new ArrayList ();
957 if ((mt & MemberTypes.Method) != 0)
958 members.AddRange (method_builders);
960 if ((mt & MemberTypes.Property) != 0)
961 members.AddRange (property_builders);
963 if ((mt & MemberTypes.Event) != 0)
964 members.AddRange (event_builders);
966 return new MemberList (members);
970 public class InterfaceMemberBase {
971 public readonly string Name;
972 public readonly bool IsNew;
973 public Attributes OptAttributes;
975 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
979 OptAttributes = attrs;
983 public class InterfaceProperty : InterfaceMemberBase {
984 public readonly bool HasSet;
985 public readonly bool HasGet;
986 public readonly Location Location;
987 public Expression Type;
989 public InterfaceProperty (Expression type, string name,
990 bool is_new, bool has_get, bool has_set,
991 Attributes attrs, Location loc)
992 : base (name, is_new, attrs)
1001 public class InterfaceEvent : InterfaceMemberBase {
1002 public readonly Location Location;
1003 public Expression Type;
1005 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
1007 : base (name, is_new, attrs)
1014 public class InterfaceMethod : InterfaceMemberBase {
1015 public readonly Expression ReturnType;
1016 public readonly Parameters Parameters;
1017 public readonly Location Location;
1019 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1020 Attributes attrs, Location l)
1021 : base (name, is_new, attrs)
1023 this.ReturnType = return_type;
1024 this.Parameters = args;
1029 /// Returns the signature for this interface method
1031 public string GetSignature (DeclSpace ds)
1033 Type ret = ds.ResolveType (ReturnType, false, Location);
1034 string args = Parameters.GetSignature (ds);
1036 if ((ret == null) || (args == null))
1039 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1042 public Type [] ParameterTypes (DeclSpace ds)
1044 return Parameters.GetParameterInfo (ds);
1048 public class InterfaceIndexer : InterfaceMemberBase {
1049 public readonly bool HasGet, HasSet;
1050 public readonly Parameters Parameters;
1051 public readonly Location Location;
1052 public Expression Type;
1054 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1055 bool is_new, Attributes attrs, Location loc)
1056 : base ("", is_new, attrs)
1065 public Type [] ParameterTypes (DeclSpace ds)
1067 return Parameters.GetParameterInfo (ds);