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;
56 // These will happen after the semantic analysis
58 // Hashtable defined_indexers;
59 // Hashtable defined_methods;
62 /// Modifiers allowed in a class declaration
64 public const int AllowedModifiers =
72 public Interface (TypeContainer parent, string name, int mod, Attributes attrs, Location l)
73 : base (parent, name, l)
75 ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
76 OptAttributes = attrs;
78 method_builders = new ArrayList ();
79 property_builders = new ArrayList ();
80 event_builders = new ArrayList ();
83 public AdditionResult AddMethod (InterfaceMethod imethod)
85 string name = imethod.Name;
86 Object value = defined_names [name];
89 if (!(value is InterfaceMethod))
90 return AdditionResult.NameExists;
93 if (defined_method == null)
94 defined_method = new ArrayList ();
96 defined_method.Add (imethod);
98 DefineName (name, imethod);
100 return AdditionResult.Success;
103 public AdditionResult AddProperty (InterfaceProperty iprop)
106 string name = iprop.Name;
108 if ((res = IsValid (name)) != AdditionResult.Success)
111 DefineName (name, iprop);
113 if (defined_properties == null)
114 defined_properties = new ArrayList ();
116 defined_properties.Add (iprop);
117 return AdditionResult.Success;
120 public AdditionResult AddEvent (InterfaceEvent ievent)
122 string name = ievent.Name;
125 if ((res = IsValid (name)) != AdditionResult.Success)
128 DefineName (name, ievent);
130 if (defined_events == null)
131 defined_events = new ArrayList ();
133 defined_events.Add (ievent);
134 return AdditionResult.Success;
137 public bool AddIndexer (InterfaceIndexer iindexer)
139 if (defined_indexer == null)
140 defined_indexer = new ArrayList ();
142 defined_indexer.Add (iindexer);
146 public ArrayList InterfaceMethods {
148 return defined_method;
152 public ArrayList InterfaceProperties {
154 return defined_properties;
158 public ArrayList InterfaceEvents {
160 return defined_events;
164 public ArrayList InterfaceIndexers {
166 return defined_indexer;
170 public ArrayList Bases {
180 public virtual TypeAttributes InterfaceAttr {
182 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
184 if (IsTopLevel == false) {
186 if ((ModFlags & Modifiers.PROTECTED) != 0
187 && (ModFlags & Modifiers.INTERNAL) != 0)
188 x |= TypeAttributes.NestedFamORAssem;
189 else if ((ModFlags & Modifiers.PROTECTED) != 0)
190 x |= TypeAttributes.NestedFamily;
191 else if ((ModFlags & Modifiers.INTERNAL) != 0)
192 x |= TypeAttributes.NestedAssembly;
193 else if ((ModFlags & Modifiers.PUBLIC) != 0)
194 x |= TypeAttributes.NestedPublic;
196 x |= TypeAttributes.NestedPrivate;
198 if ((ModFlags & Modifiers.PUBLIC) != 0)
199 x |= TypeAttributes.Public;
200 else if ((ModFlags & Modifiers.PRIVATE) != 0)
201 x |= TypeAttributes.NotPublic;
204 if ((ModFlags & Modifiers.ABSTRACT) != 0)
205 x |= TypeAttributes.Abstract;
207 if ((ModFlags & Modifiers.SEALED) != 0)
208 x |= TypeAttributes.Sealed;
214 void Error111 (InterfaceMemberBase ib)
218 "Interface `" + Name + "' already contains a definition with the " +
219 "same return value and parameter types for member `" + ib.Name + "'");
222 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
224 if (!TypeManager.RegisterMethod (mb, ip, types))
227 method_builders.Add (mb);
231 public MethodInfo [] GetMethods ()
233 int n = method_builders.Count;
234 MethodInfo [] mi = new MethodInfo [n];
236 method_builders.CopyTo (mi, 0);
241 // Hack around System.Reflection as found everywhere else
242 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
243 MemberFilter filter, object criteria)
245 ArrayList members = new ArrayList ();
247 if ((mt & MemberTypes.Method) != 0) {
248 foreach (MethodBuilder mb in method_builders)
249 if (filter (mb, criteria))
253 if ((mt & MemberTypes.Property) != 0) {
254 foreach (PropertyBuilder pb in property_builders)
255 if (filter (pb, criteria))
259 if ((mt & MemberTypes.Event) != 0) {
260 foreach (MyEventBuilder eb in event_builders)
261 if (filter (eb, criteria))
265 if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
266 MemberList parent_mi;
268 parent_mi = TypeContainer.FindMembers (
269 TypeBuilder.BaseType, mt, bf, filter, criteria);
271 members.AddRange (parent_mi);
274 return new MemberList (members);
277 public override MemberCache MemberCache {
284 // Populates the methods in the interface
286 void PopulateMethod (TypeContainer parent, DeclSpace decl_space, InterfaceMethod im)
288 Type return_type = this.ResolveType (im.ReturnType, false, im.Location);
289 Type [] arg_types = im.ParameterTypes (this);
294 if (return_type == null)
297 if (return_type.IsPointer && !UnsafeOK (this))
300 foreach (Type t in arg_types){
305 if (t.IsPointer && !UnsafeOK (this))
312 mb = TypeBuilder.DefineMethod (
313 im.Name, interface_method_attributes,
314 return_type, arg_types);
316 InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
318 if (!RegisterMethod (mb, ip, arg_types)) {
324 // Define each type attribute (in/out/ref) and
325 // the argument names.
327 p = im.Parameters.FixedParameters;
329 for (i = 0; i < p.Length; i++)
330 mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
332 if (i != arg_types.Length)
333 Console.WriteLine ("Implement the type definition for params");
336 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
337 return_type, ModFlags, false);
339 if (im.OptAttributes != null)
340 Attribute.ApplyAttributes (ec, mb, im, im.OptAttributes, Location);
344 // Populates the properties in the interface
346 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
349 MethodBuilder get = null, set = null;
350 ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
351 Type prop_type = ip.Type.Type;
352 Type [] setter_args = new Type [1];
354 if (prop_type == null)
357 if (prop_type.IsPointer && !UnsafeOK (this))
360 setter_args [0] = prop_type;
363 // FIXME: properties are missing the following
364 // flags: hidebysig newslot specialname
366 pb = TypeBuilder.DefineProperty (
367 ip.Name, PropertyAttributes.None,
371 get = TypeBuilder.DefineMethod (
372 "get_" + ip.Name, property_attributes ,
376 // HACK because System.Reflection.Emit is lame
378 Type [] null_types = null;
379 InternalParameters inp = new InternalParameters
380 (null_types, Parameters.EmptyReadOnlyParameters);
382 if (!RegisterMethod (get, inp, null)) {
387 pb.SetGetMethod (get);
391 setter_args [0] = prop_type;
393 set = TypeBuilder.DefineMethod (
394 "set_" + ip.Name, property_attributes,
395 TypeManager.void_type, setter_args);
397 set.DefineParameter (1, ParameterAttributes.None, "value");
398 pb.SetSetMethod (set);
401 // HACK because System.Reflection.Emit is lame
403 Parameter [] parms = new Parameter [1];
404 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
405 InternalParameters ipp = new InternalParameters (
406 this, new Parameters (parms, null, Location.Null));
408 if (!RegisterMethod (set, ipp, setter_args)) {
414 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
415 null, ModFlags, false);
417 if (ip.OptAttributes != null)
418 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes, Location);
420 TypeManager.RegisterProperty (pb, get, set);
421 property_builders.Add (pb);
425 // Populates the events in the interface
427 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
430 // FIXME: We need to do this after delegates have been
431 // declared or we declare them recursively.
434 MethodBuilder add = null, remove = null;
435 ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
436 Type event_type = ie.Type.Type;
438 if (event_type == null)
441 if (event_type.IsPointer && !UnsafeOK (this))
444 Type [] parameters = new Type [1];
445 parameters [0] = event_type;
447 eb = new MyEventBuilder (TypeBuilder, ie.Name,
448 EventAttributes.None, event_type);
451 // Now define the accessors
453 string add_name = "add_" + ie.Name;
455 add = TypeBuilder.DefineMethod (
456 add_name, property_attributes, null, parameters);
457 add.DefineParameter (1, ParameterAttributes.None, "value");
458 eb.SetAddOnMethod (add);
460 string remove_name = "remove_" + ie.Name;
461 remove = TypeBuilder.DefineMethod (
462 remove_name, property_attributes, null, parameters);
463 remove.DefineParameter (1, ParameterAttributes.None, "value");
464 eb.SetRemoveOnMethod (remove);
466 Parameter [] parms = new Parameter [1];
467 parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
468 InternalParameters ip = new InternalParameters (
469 this, new Parameters (parms, null, Location.Null));
471 if (!RegisterMethod (add, ip, parameters)) {
476 if (!RegisterMethod (remove, ip, parameters)) {
481 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
482 null, ModFlags, false);
485 if (ie.OptAttributes != null)
486 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes, Location);
488 TypeManager.RegisterEvent (eb, add, remove);
489 event_builders.Add (eb);
493 // Populates the indexers in the interface
495 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
498 ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
499 Type prop_type = ii.Type.Type;
500 Type [] arg_types = ii.ParameterTypes (this);
501 Type [] value_arg_types;
503 if (prop_type == null)
506 if (prop_type.IsPointer && !UnsafeOK (this))
510 // Sets up the extra invisible `value' argument for setters.
512 if (arg_types != null){
513 int count = arg_types.Length;
514 value_arg_types = new Type [count + 1];
516 arg_types.CopyTo (value_arg_types, 0);
517 value_arg_types [count] = prop_type;
519 foreach (Type t in arg_types){
520 if (t.IsPointer && !UnsafeOK (this))
524 value_arg_types = new Type [1];
526 value_arg_types [1] = prop_type;
529 EmitContext ec = new EmitContext (parent, decl_space, Location, null,
530 null, ModFlags, false);
532 IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
533 if (IndexerName == null)
534 IndexerName = "Item";
536 pb = TypeBuilder.DefineProperty (
537 IndexerName, PropertyAttributes.None,
538 prop_type, arg_types);
540 MethodBuilder set_item = null, get_item = null;
542 Parameter [] p = ii.Parameters.FixedParameters;
544 get_item = TypeBuilder.DefineMethod (
545 "get_" + IndexerName, property_attributes,
546 prop_type, arg_types);
547 pb.SetGetMethod (get_item);
549 // HACK because System.Reflection.Emit is lame
551 InternalParameters ip = new InternalParameters (
552 arg_types, ii.Parameters);
554 if (!RegisterMethod (get_item, ip, arg_types)) {
560 for (int i = 0; i < p.Length; i++)
561 get_item.DefineParameter (
563 p [i].Attributes, p [i].Name);
568 Parameter [] p = ii.Parameters.FixedParameters;
572 pv = new Parameter [p.Length + 1];
574 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
575 Parameters value_params = new Parameters (pv, null, Location.Null);
576 value_params.GetParameterInfo (decl_space);
578 set_item = TypeBuilder.DefineMethod (
579 "set_" + IndexerName, property_attributes,
580 TypeManager.void_type, value_arg_types);
581 pb.SetSetMethod (set_item);
583 // HACK because System.Reflection.Emit is lame
585 InternalParameters ip = new InternalParameters (
586 value_arg_types, value_params);
587 if (!RegisterMethod (set_item, ip, value_arg_types)) {
593 for (; i < p.Length; i++)
594 set_item.DefineParameter (
596 p [i].Attributes, p [i].Name);
599 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
602 if (ii.OptAttributes != null)
603 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes, Location);
605 property_builders.Add (pb);
609 /// Performs the semantic analysis for all the interface members
610 /// that were declared
612 bool SemanticAnalysis ()
614 Hashtable methods = new Hashtable ();
617 if (defined_method != null){
618 foreach (InterfaceMethod im in defined_method){
619 string sig = im.GetSignature (this);
622 // If there was an undefined Type on the signatures
627 if (methods [sig] != null){
635 // FIXME: Here I should check i
640 Type GetInterfaceTypeByName (string name)
642 Type t = FindType (name);
645 Report.Error (246, Location, "The type or namespace `" + name +
646 "' could not be found");
656 cause = "is a struct";
658 cause = "is a class";
660 cause = "Should not happen.";
662 Report.Error (527, Location, "`"+name+"' " + cause +
663 ", need an interface instead");
669 // Returns the list of interfaces that this interface implements
670 // Or null if it does not implement any interface.
672 // Sets the error boolean accoringly.
674 Type [] GetInterfaceBases (out bool error)
683 tbases = new Type [Bases.Count];
686 foreach (string name in Bases){
689 t = GetInterfaceTypeByName (name);
695 if (!Parent.AsAccessible (t, ModFlags))
696 Report.Error (61, Location,
697 "Inconsistent accessibility: base interface `" +
698 TypeManager.CSharpName (t) + "' is less " +
699 "accessible than interface `" +
705 return TypeManager.ExpandInterfaces (tbases);
710 // Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
714 // Rework the way we recurse, because for recursive
715 // definitions of interfaces (A:B and B:A) we report the
716 // error twice, rather than once.
718 public override TypeBuilder DefineType ()
723 if (TypeBuilder != null)
731 ifaces = GetInterfaceBases (out error);
737 ModuleBuilder builder = CodeGen.ModuleBuilder;
739 TypeBuilder = builder.DefineType (
742 (Type)null, // Parent Type
744 RootContext.RegisterOrder (this);
746 TypeBuilder builder = Parent.TypeBuilder;
748 TypeBuilder = builder.DefineNestedType (
751 (Type) null, //parent type
754 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
755 tc.RegisterOrder (this);
758 TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
765 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
766 // interface is consistent. Either it is `Item' or it is the name defined by all the
767 // indexers with the `IndexerName' attribute.
769 // Turns out that the IndexerNameAttribute is applied to each indexer,
770 // but it is never emitted, instead a DefaultName attribute is attached
773 void DefineIndexers (TypeContainer parent)
775 string interface_indexer_name = null;
777 foreach (InterfaceIndexer ii in defined_indexer){
779 PopulateIndexer (parent, this, ii);
781 if (interface_indexer_name == null){
782 interface_indexer_name = IndexerName;
786 if (IndexerName == interface_indexer_name)
790 668, "Two indexers have different names, " +
791 " you should use the same name for all your indexers");
793 if (interface_indexer_name == null)
794 interface_indexer_name = "Item";
795 IndexerName = interface_indexer_name;
799 /// Performs semantic analysis, and then generates the IL interfaces
801 public override bool DefineMembers (TypeContainer parent)
803 if (!SemanticAnalysis ())
806 if (defined_method != null){
807 foreach (InterfaceMethod im in defined_method)
808 PopulateMethod (parent, this, im);
811 if (defined_properties != null){
812 foreach (InterfaceProperty ip in defined_properties)
813 PopulateProperty (parent, this, ip);
816 if (defined_events != null)
817 foreach (InterfaceEvent ie in defined_events)
818 PopulateEvent (parent, this, ie);
820 if (defined_indexer != null) {
821 DefineIndexers (parent);
823 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
824 parent, IndexerName, ModFlags, Location);
826 TypeBuilder.SetCustomAttribute (cb);
830 if (TypeBuilder.BaseType != null)
831 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
833 member_cache = new MemberCache (this);
840 /// Applies all the attributes.
842 public override bool Define (TypeContainer parent)
844 if (OptAttributes != null) {
845 EmitContext ec = new EmitContext (parent, this, Location, null, null,
847 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes, Location);
853 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
858 EmitContext ec = new EmitContext (parent, loc, null, null, flags);
860 Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
861 ".ctor", MemberTypes.Constructor,
862 BindingFlags.Public | BindingFlags.Instance,
865 if (!(ml is MethodGroupExpr)) {
866 Console.WriteLine ("Internal error !!!!");
870 MethodGroupExpr mg = (MethodGroupExpr) ml;
872 MethodBase constructor = mg.Methods [0];
874 string [] vals = { name };
876 CustomAttributeBuilder cb = null;
878 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
880 Report.Warning (-100, "Can not set the indexer default member attribute");
890 string IMemberContainer.Name {
896 Type IMemberContainer.Type {
902 IMemberContainer IMemberContainer.Parent {
904 return parent_container;
908 MemberCache IMemberContainer.MemberCache {
914 bool IMemberContainer.IsInterface {
920 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
922 // Interfaces only contain instance members.
923 if ((bf & BindingFlags.Instance) == 0)
924 return MemberList.Empty;
925 if ((bf & BindingFlags.Public) == 0)
926 return MemberList.Empty;
928 ArrayList members = new ArrayList ();
930 if ((mt & MemberTypes.Method) != 0)
931 members.AddRange (method_builders);
933 if ((mt & MemberTypes.Property) != 0)
934 members.AddRange (property_builders);
936 if ((mt & MemberTypes.Event) != 0)
937 members.AddRange (event_builders);
939 return new MemberList (members);
943 public class InterfaceMemberBase {
944 public readonly string Name;
945 public readonly bool IsNew;
946 public Attributes OptAttributes;
948 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
952 OptAttributes = attrs;
956 public class InterfaceProperty : InterfaceMemberBase {
957 public readonly bool HasSet;
958 public readonly bool HasGet;
959 public readonly Location Location;
960 public Expression Type;
962 public InterfaceProperty (Expression type, string name,
963 bool is_new, bool has_get, bool has_set,
964 Attributes attrs, Location loc)
965 : base (name, is_new, attrs)
974 public class InterfaceEvent : InterfaceMemberBase {
975 public readonly Location Location;
976 public Expression Type;
978 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
980 : base (name, is_new, attrs)
987 public class InterfaceMethod : InterfaceMemberBase {
988 public readonly Expression ReturnType;
989 public readonly Parameters Parameters;
990 public readonly Location Location;
992 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
993 Attributes attrs, Location l)
994 : base (name, is_new, attrs)
996 this.ReturnType = return_type;
997 this.Parameters = args;
1002 /// Returns the signature for this interface method
1004 public string GetSignature (DeclSpace ds)
1006 Type ret = ds.ResolveType (ReturnType, false, Location);
1007 string args = Parameters.GetSignature (ds);
1009 if ((ret == null) || (args == null))
1012 return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1015 public Type [] ParameterTypes (DeclSpace ds)
1017 return Parameters.GetParameterInfo (ds);
1021 public class InterfaceIndexer : InterfaceMemberBase {
1022 public readonly bool HasGet, HasSet;
1023 public readonly Parameters Parameters;
1024 public readonly Location Location;
1025 public Expression Type;
1027 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1028 bool is_new, Attributes attrs, Location loc)
1029 : base ("", is_new, attrs)
1038 public Type [] ParameterTypes (DeclSpace ds)
1040 return Parameters.GetParameterInfo (ds);