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, 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, 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 = im.ReturnType.Type;
304 if (return_type == null)
305 return_type = this.ResolveType (im.ReturnType, false, im.Location);
307 Type [] arg_types = im.ParameterTypes (this);
312 if (return_type == null)
315 if (return_type.IsPointer && !UnsafeOK (this))
318 if (arg_types == null)
321 foreach (Type t in arg_types){
326 if (t.IsPointer && !UnsafeOK (this))
333 mb = TypeBuilder.DefineMethod (
334 im.Name, interface_method_attributes,
335 return_type, arg_types);
337 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
339 if (!RegisterMethod (mb, ip, arg_types)) {
345 // Define each type attribute (in/out/ref) and
346 // the argument names.
348 p = im.Parameters.FixedParameters;
350 for (i = 0; i < p.Length; i++)
351 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
353 if (i != arg_types.Length)
354 Console.WriteLine ("Implement the type definition for params");
357 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
358 return_type, ModFlags, false);
360 if (im.OptAttributes != null)
361 Attribute.ApplyAttributes (ec, mb, im, im.OptAttributes);
365 // Populates the properties in the interface
367 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
370 MethodBuilder get = null, set = null;
371 ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
375 Type prop_type = ip.Type.Type;
376 Type [] setter_args = new Type [1];
378 if (prop_type == null)
381 if (prop_type.IsPointer && !UnsafeOK (this))
384 setter_args [0] = prop_type;
387 // FIXME: properties are missing the following
388 // flags: hidebysig newslot specialname
390 pb = TypeBuilder.DefineProperty (
391 ip.Name, PropertyAttributes.None,
395 get = TypeBuilder.DefineMethod (
396 "get_" + ip.Name, property_attributes ,
400 // HACK because System.Reflection.Emit is lame
402 Type [] null_types = null;
403 InternalParameters inp = new InternalParameters
404 (null_types, Parameters.EmptyReadOnlyParameters);
406 if (!RegisterMethod (get, inp, null)) {
411 pb.SetGetMethod (get);
415 setter_args [0] = prop_type;
417 set = TypeBuilder.DefineMethod (
418 "set_" + ip.Name, property_attributes,
419 TypeManager.void_type, setter_args);
421 set.DefineParameter (1, ParameterAttributes.None, "value");
422 pb.SetSetMethod (set);
425 // HACK because System.Reflection.Emit is lame
427 Parameter [] parms = new Parameter [1];
428 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
429 InternalParameters ipp = new InternalParameters (
430 this, new Parameters (parms, null, Location.Null));
432 if (!RegisterMethod (set, ipp, setter_args)) {
438 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
439 null, ModFlags, false);
441 if (ip.OptAttributes != null)
442 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes);
444 TypeManager.RegisterProperty (pb, get, set);
445 property_builders.Add (pb);
449 // Populates the events in the interface
451 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
454 // FIXME: We need to do this after delegates have been
455 // declared or we declare them recursively.
458 MethodBuilder add = null, remove = null;
459 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
463 Type event_type = ie.Type.Type;
465 if (event_type == null)
468 if (event_type.IsPointer && !UnsafeOK (this))
471 Type [] parameters = new Type [1];
472 parameters [0] = event_type;
474 eb = new MyEventBuilder (null, TypeBuilder, ie.Name,
475 EventAttributes.None, event_type);
478 // Now define the accessors
480 string add_name = "add_" + ie.Name;
482 add = TypeBuilder.DefineMethod (
483 add_name, property_attributes, null, parameters);
484 add.DefineParameter (1, ParameterAttributes.None, "value");
485 eb.SetAddOnMethod (add);
487 string remove_name = "remove_" + ie.Name;
488 remove = TypeBuilder.DefineMethod (
489 remove_name, property_attributes, null, parameters);
490 remove.DefineParameter (1, ParameterAttributes.None, "value");
491 eb.SetRemoveOnMethod (remove);
493 Parameter [] parms = new Parameter [1];
494 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
495 InternalParameters ip = new InternalParameters (
496 this, new Parameters (parms, null, Location.Null));
498 if (!RegisterMethod (add, ip, parameters)) {
503 if (!RegisterMethod (remove, ip, parameters)) {
508 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
509 null, ModFlags, false);
512 if (ie.OptAttributes != null)
513 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes);
515 TypeManager.RegisterEvent (eb, add, remove);
516 event_builders.Add (eb);
520 // Populates the indexers in the interface
522 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
525 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
529 Type prop_type = ii.Type.Type;
530 Type [] arg_types = ii.ParameterTypes (this);
531 Type [] value_arg_types;
533 if (prop_type == null)
536 if (prop_type.IsPointer && !UnsafeOK (this))
540 // Sets up the extra invisible `value' argument for setters.
542 if (arg_types != null){
543 int count = arg_types.Length;
544 value_arg_types = new Type [count + 1];
546 arg_types.CopyTo (value_arg_types, 0);
547 value_arg_types [count] = prop_type;
549 foreach (Type t in arg_types){
550 if (t.IsPointer && !UnsafeOK (this))
554 value_arg_types = new Type [1];
556 value_arg_types [1] = prop_type;
559 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
560 null, ModFlags, false);
562 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
563 if (IndexerName == null)
564 IndexerName = "Item";
566 pb = TypeBuilder.DefineProperty (
567 IndexerName, PropertyAttributes.None,
568 prop_type, arg_types);
570 MethodBuilder set_item = null, get_item = null;
572 Parameter [] p = ii.Parameters.FixedParameters;
574 get_item = TypeBuilder.DefineMethod (
575 "get_" + IndexerName, property_attributes,
576 prop_type, arg_types);
577 pb.SetGetMethod (get_item);
579 // HACK because System.Reflection.Emit is lame
581 InternalParameters ip = new InternalParameters (
582 arg_types, ii.Parameters);
584 if (!RegisterMethod (get_item, ip, arg_types)) {
590 for (int i = 0; i < p.Length; i++)
591 get_item.DefineParameter (
593 p [i].Attributes, p [i].Name);
598 Parameter [] p = ii.Parameters.FixedParameters;
602 pv = new Parameter [p.Length + 1];
604 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
605 Parameters value_params = new Parameters (pv, null, Location.Null);
606 value_params.GetParameterInfo (decl_space);
608 set_item = TypeBuilder.DefineMethod (
609 "set_" + IndexerName, property_attributes,
610 TypeManager.void_type, value_arg_types);
611 pb.SetSetMethod (set_item);
613 // HACK because System.Reflection.Emit is lame
615 InternalParameters ip = new InternalParameters (
616 value_arg_types, value_params);
617 if (!RegisterMethod (set_item, ip, value_arg_types)) {
623 for (; i < p.Length; i++)
624 set_item.DefineParameter (
626 p [i].Attributes, p [i].Name);
629 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
632 if (ii.OptAttributes != null)
633 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes);
635 property_builders.Add (pb);
639 /// Performs the semantic analysis for all the interface members
640 /// that were declared
642 bool SemanticAnalysis ()
644 Hashtable methods = new Hashtable ();
647 if (defined_method != null){
648 foreach (InterfaceMethod im in defined_method){
649 string sig = im.GetSignature (this);
652 // If there was an undefined Type on the signatures
657 if (methods [sig] != null){
665 // FIXME: Here I should check i
670 Type GetInterfaceTypeByName (string name)
672 Type t = FindType (Location, name);
675 Report.Error (246, Location, "The type or namespace `" + name +
676 "' could not be found");
686 cause = "is a struct";
688 cause = "is a class";
690 cause = "Should not happen.";
692 Report.Error (527, Location, "`"+name+"' " + cause +
693 ", need an interface instead");
699 // Returns the list of interfaces that this interface implements
700 // Or null if it does not implement any interface.
702 // Sets the error boolean accoringly.
704 Type [] GetInterfaceBases (out bool error)
713 tbases = new Type [Bases.Count];
716 foreach (string name in Bases){
719 t = GetInterfaceTypeByName (name);
725 if (!Parent.AsAccessible (t, ModFlags))
726 Report.Error (61, Location,
727 "Inconsistent accessibility: base interface `" +
728 TypeManager.CSharpName (t) + "' is less " +
729 "accessible than interface `" +
735 return TypeManager.ExpandInterfaces (tbases);
740 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
744 // Rework the way we recurse, because for recursive
745 // definitions of interfaces (A:B and B:A) we report the
746 // error twice, rather than once.
748 public override TypeBuilder DefineType ()
753 if (TypeBuilder != null)
761 ifaces = GetInterfaceBases (out error);
767 if (TypeManager.NamespaceClash (Name, Location))
770 ModuleBuilder builder = CodeGen.ModuleBuilder;
772 TypeBuilder = builder.DefineType (
775 (Type)null, // Parent Type
777 RootContext.RegisterOrder (this);
779 TypeBuilder builder = Parent.TypeBuilder;
781 TypeBuilder = builder.DefineNestedType (
784 (Type) null, //parent type
787 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
788 tc.RegisterOrder (this);
791 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
798 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
799 // interface is consistent. Either it is `Item' or it is the name defined by all the
800 // indexers with the `IndexerName' attribute.
802 // Turns out that the IndexerNameAttribute is applied to each indexer,
803 // but it is never emitted, instead a DefaultName attribute is attached
806 void DefineIndexers (TypeContainer parent)
808 string interface_indexer_name = null;
810 foreach (InterfaceIndexer ii in defined_indexer){
812 PopulateIndexer (parent, this, ii);
814 if (interface_indexer_name == null){
815 interface_indexer_name = IndexerName;
819 if (IndexerName == interface_indexer_name)
823 668, "Two indexers have different names, " +
824 " you should use the same name for all your indexers");
826 if (interface_indexer_name == null)
827 interface_indexer_name = "Item";
828 IndexerName = interface_indexer_name;
832 /// Performs semantic analysis, and then generates the IL interfaces
834 public override bool DefineMembers (TypeContainer parent)
839 if (!SemanticAnalysis ())
843 if (defined_method != null){
844 foreach (InterfaceMethod im in defined_method)
845 PopulateMethod (parent, this, im);
848 if (defined_properties != null){
849 foreach (InterfaceProperty ip in defined_properties)
850 PopulateProperty (parent, this, ip);
853 if (defined_events != null)
854 foreach (InterfaceEvent ie in defined_events)
855 PopulateEvent (parent, this, ie);
857 if (defined_indexer != null) {
858 DefineIndexers (parent);
860 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
861 parent, IndexerName, ModFlags, Location);
863 TypeBuilder.SetCustomAttribute (cb);
867 if (TypeBuilder.BaseType != null)
868 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
870 member_cache = new MemberCache (this);
872 members_defined = true;
878 // In the case of Interfaces, there is nothing to do here
880 public override bool Define (TypeContainer parent)
886 /// Applies all the attributes.
888 public void Emit (TypeContainer tc)
890 if (OptAttributes != null) {
891 EmitContext ec = new EmitContext (tc, this, Location, null, null,
893 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
897 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
902 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
904 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
905 ".ctor", MemberTypes.Constructor,
906 BindingFlags.Public | BindingFlags.Instance,
909 if (!(ml is MethodGroupExpr)) {
910 Console.WriteLine ("Internal error !!!!");
914 MethodGroupExpr mg = (MethodGroupExpr) ml;
916 MethodBase constructor = mg.Methods [0];
918 string [] vals = { name };
920 CustomAttributeBuilder cb = null;
922 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
924 Report.Warning (-100, "Can not set the indexer default member attribute");
934 string IMemberContainer.Name {
940 Type IMemberContainer.Type {
946 IMemberContainer IMemberContainer.Parent {
948 return parent_container;
952 MemberCache IMemberContainer.MemberCache {
958 bool IMemberContainer.IsInterface {
964 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
966 // Interfaces only contain instance members.
967 if ((bf & BindingFlags.Instance) == 0)
968 return MemberList.Empty;
969 if ((bf & BindingFlags.Public) == 0)
970 return MemberList.Empty;
972 ArrayList members = new ArrayList ();
974 if ((mt & MemberTypes.Method) != 0)
975 members.AddRange (method_builders);
977 if ((mt & MemberTypes.Property) != 0)
978 members.AddRange (property_builders);
980 if ((mt & MemberTypes.Event) != 0)
981 members.AddRange (event_builders);
983 return new MemberList (members);
987 public class InterfaceMemberBase {
988 public readonly string Name;
989 public readonly bool IsNew;
990 public Attributes OptAttributes;
992 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
996 OptAttributes = attrs;
1000 public class InterfaceProperty : InterfaceMemberBase {
1001 public readonly bool HasSet;
1002 public readonly bool HasGet;
1003 public readonly Location Location;
1004 public Expression Type;
1006 public InterfaceProperty (Expression type, string name,
1007 bool is_new, bool has_get, bool has_set,
1008 Attributes attrs, Location loc)
1009 : base (name, is_new, attrs)
1018 public class InterfaceEvent : InterfaceMemberBase {
1019 public readonly Location Location;
1020 public Expression Type;
1022 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
1024 : base (name, is_new, attrs)
1031 public class InterfaceMethod : InterfaceMemberBase {
1032 public Expression ReturnType;
1033 public readonly Parameters Parameters;
1034 public readonly Location Location;
1036 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1037 Attributes attrs, Location l)
1038 : base (name, is_new, attrs)
1040 this.ReturnType = return_type;
1041 this.Parameters = args;
1046 /// Returns the signature for this interface method
1048 public string GetSignature (DeclSpace ds)
1050 ReturnType = ds.ResolveTypeExpr (ReturnType, false, Location);
1051 if (ReturnType == null)
1054 Type ret = ReturnType.Type;
1055 string args = Parameters.GetSignature (ds);
1057 if ((ret == null) || (args == null))
1060 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1063 public Type [] ParameterTypes (DeclSpace ds)
1065 return Parameters.GetParameterInfo (ds);
1069 public class InterfaceIndexer : InterfaceMemberBase {
1070 public readonly bool HasGet, HasSet;
1071 public readonly Parameters Parameters;
1072 public readonly Location Location;
1073 public Expression Type;
1075 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1076 bool is_new, Attributes attrs, Location loc)
1077 : base ("", is_new, attrs)
1086 public Type [] ParameterTypes (DeclSpace ds)
1088 return Parameters.GetParameterInfo (ds);