2004-01-20 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / interface.cs
1 //
2 // interface.cs: Interface handler
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 #define CACHE
11 using System.Collections;
12 using System;
13 using System.IO;
14 using System.Reflection;
15 using System.Reflection.Emit;
16
17 namespace Mono.CSharp {
18
19         /// <summary>
20         ///   Interfaces
21         /// </summary>
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;
29
30                 const MethodAttributes property_attributes =
31                         MethodAttributes.Public |
32                         MethodAttributes.Abstract |
33                         MethodAttributes.HideBySig |
34                         MethodAttributes.NewSlot |
35                         MethodAttributes.SpecialName |
36                         MethodAttributes.Virtual;
37                 
38                 ArrayList bases;
39                 
40                 ArrayList defined_method;
41                 ArrayList defined_indexer;
42                 ArrayList defined_events;
43                 ArrayList defined_properties;
44
45                 ArrayList method_builders;
46                 ArrayList property_builders;
47                 ArrayList event_builders;
48                 
49                 Attributes OptAttributes;
50
51                 public string IndexerName;
52
53                 IMemberContainer parent_container;
54                 MemberCache member_cache;
55
56                 bool members_defined;
57
58                 // These will happen after the semantic analysis
59                 
60                 // Hashtable defined_indexers;
61                 // Hashtable defined_methods;
62                 
63                 /// <summary>
64                 ///   Modifiers allowed in a class declaration
65                 /// </summary>
66                 public const int AllowedModifiers =
67                         Modifiers.NEW       |
68                         Modifiers.PUBLIC    |
69                         Modifiers.PROTECTED |
70                         Modifiers.INTERNAL  |
71                         Modifiers.UNSAFE    |
72                         Modifiers.PRIVATE;
73
74                 public Interface (NamespaceEntry ns, TypeContainer parent, string name, int mod,
75                                   Attributes attrs, Location l)
76                         : base (ns, parent, name, l)
77                 {
78                         ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE, l);
79                         OptAttributes = attrs;
80                         
81                         method_builders = new ArrayList ();
82                         property_builders = new ArrayList ();
83                         event_builders = new ArrayList ();
84                 }
85
86                 public AdditionResult AddMethod (InterfaceMethod imethod)
87                 {
88                         string name = imethod.Name;
89                         Object value = defined_names [name];
90
91                         if (value != null){
92                                 if (!(value is InterfaceMethod))
93                                         return AdditionResult.NameExists;
94                         } 
95
96                         if (defined_method == null)
97                                 defined_method = new ArrayList ();
98
99                         defined_method.Add (imethod);
100                         if (value == null)
101                                 DefineName (name, imethod);
102                         
103                         return AdditionResult.Success;
104                 }
105
106                 public AdditionResult AddProperty (InterfaceProperty iprop)
107                 {
108                         AdditionResult res;
109                         string name = iprop.Name;
110
111                         if ((res = IsValid (name, name)) != AdditionResult.Success)
112                                 return res;
113
114                         DefineName (name, iprop);
115
116                         if (defined_properties == null)
117                                 defined_properties = new ArrayList ();
118
119                         defined_properties.Add (iprop);
120                         return AdditionResult.Success;
121                 }
122
123                 public AdditionResult AddEvent (InterfaceEvent ievent)
124                 {
125                         string name = ievent.Name;
126                         AdditionResult res;
127                         
128                         if ((res = IsValid (name, name)) != AdditionResult.Success)
129                                 return res;
130
131                         DefineName (name, ievent);
132
133                         if (defined_events == null)
134                                 defined_events = new ArrayList ();
135
136                         defined_events.Add (ievent);
137                         return AdditionResult.Success;
138                 }
139
140                 public bool AddIndexer (InterfaceIndexer iindexer)
141                 {
142                         if (defined_indexer == null)
143                                 defined_indexer = new ArrayList ();
144                         
145                         defined_indexer.Add (iindexer);
146                         return true;
147                 }
148                 
149                 public ArrayList InterfaceMethods {
150                         get {
151                                 return defined_method;
152                         }
153                 }
154
155                 public ArrayList InterfaceProperties {
156                         get {
157                                 return defined_properties;
158                         }
159                 }
160
161                 public ArrayList InterfaceEvents {
162                         get {
163                                 return defined_events;
164                         }
165                 }
166
167                 public ArrayList InterfaceIndexers {
168                         get {
169                                 return defined_indexer;
170                         }
171                 }
172
173                 public ArrayList Bases {
174                         get {
175                                 return bases;
176                         }
177
178                         set {
179                                 bases = value;
180                         }
181                 }
182
183                 public virtual TypeAttributes InterfaceAttr {
184                         get {
185                                 TypeAttributes x = TypeAttributes.Interface | TypeAttributes.Abstract;
186
187                                 if (IsTopLevel == false) {
188                                         
189                                         if ((ModFlags & Modifiers.PROTECTED) != 0
190                                             && (ModFlags & Modifiers.INTERNAL) != 0)
191                                                 x |= TypeAttributes.NestedFamORAssem;
192                                         else if ((ModFlags & Modifiers.PROTECTED) != 0)
193                                                 x |= TypeAttributes.NestedFamily;
194                                         else if ((ModFlags & Modifiers.INTERNAL) != 0)
195                                                 x |= TypeAttributes.NestedAssembly;
196                                         else if ((ModFlags & Modifiers.PUBLIC) != 0)
197                                                 x |= TypeAttributes.NestedPublic;
198                                         else
199                                                 x |= TypeAttributes.NestedPrivate;
200                                 } else {
201                                         if ((ModFlags & Modifiers.PUBLIC) != 0)
202                                                 x |= TypeAttributes.Public;
203                                         else if ((ModFlags & Modifiers.PRIVATE) != 0)
204                                                 x |= TypeAttributes.NotPublic;
205                                 }
206                                 
207                                 if ((ModFlags & Modifiers.ABSTRACT) != 0)
208                                         x |= TypeAttributes.Abstract;
209                                 
210                                 if ((ModFlags & Modifiers.SEALED) != 0)
211                                         x |= TypeAttributes.Sealed;
212
213                                 return x;
214                         }
215                 }
216                 
217                 void Error111 (InterfaceMemberBase ib)
218                 {
219                         Report.Error (
220                                 111,
221                                 "Interface `" + Name + "' already contains a definition with the " +
222                                 "same return value and parameter types for member `" + ib.Name + "'");
223                 }
224
225                 bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] types)
226                 {
227                         if (!TypeManager.RegisterMethod (mb, ip, types))
228                                 return false;
229
230                         method_builders.Add (mb);
231                         return true;
232                 }
233
234                 //
235                 // This might trigger a definition of the methods.  This happens only
236                 // with Attributes, as Attribute classes are processed before interfaces.
237                 // Ideally, we should make everything just define recursively in terms
238                 // of its dependencies.
239                 //
240                 public MethodInfo [] GetMethods (TypeContainer container)
241                 {
242                         int n = 0;
243                         
244                         if (!members_defined){
245                                 if (DefineMembers (container))
246                                         n = method_builders.Count;
247                         } else
248                                 n = method_builders.Count;
249                         
250                         MethodInfo [] mi = new MethodInfo [n];
251                         
252                         method_builders.CopyTo (mi, 0);
253
254                         return mi;
255                 }
256
257                 // Hack around System.Reflection as found everywhere else
258                 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
259                                                         MemberFilter filter, object criteria)
260                 {
261                         ArrayList members = new ArrayList ();
262
263                         if ((mt & MemberTypes.Method) != 0) {
264                                 foreach (MethodBuilder mb in method_builders)
265                                         if (filter (mb, criteria))
266                                                 members.Add (mb);
267                         }
268
269                         if ((mt & MemberTypes.Property) != 0) {
270                                 foreach (PropertyBuilder pb in property_builders)
271                                         if (filter (pb, criteria))
272                                                 members.Add (pb);
273                         }
274
275                         if ((mt & MemberTypes.Event) != 0) {
276                                 foreach (MyEventBuilder eb in event_builders)
277                                         if (filter (eb, criteria))
278                                                 members.Add (eb);
279                         }
280
281                         if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
282                                 MemberList parent_mi;
283                                 
284                                 parent_mi = TypeContainer.FindMembers (
285                                         TypeBuilder.BaseType, mt, bf, filter, criteria);
286
287                                 members.AddRange (parent_mi);
288                         }
289
290                         return new MemberList (members);
291                 }
292
293                 public override MemberCache MemberCache {
294                         get {
295                                 return member_cache;
296                         }
297                 }
298
299                 //
300                 // Populates the methods in the interface
301                 //
302                 void PopulateMethod (TypeContainer parent, DeclSpace decl_space, InterfaceMethod im)
303                 {
304                         Type return_type = im.ReturnType.Type;
305                         if (return_type == null)
306                                 return_type = this.ResolveType (im.ReturnType, false, im.Location);
307                         
308                         Type [] arg_types = im.ParameterTypes (this);
309                         MethodBuilder mb;
310
311                         if (return_type == null)
312                                 return;
313
314                         if (return_type.IsPointer && !UnsafeOK (this))
315                                 return;
316
317                         if (arg_types == null)
318                                 return;
319
320                         foreach (Type t in arg_types){
321
322                                 if (t == null)
323                                         return;
324                                 
325                                 if (t.IsPointer && !UnsafeOK (this))
326                                         return;
327                         }
328                         
329                         //
330                         // Create the method
331                         //
332                         mb = TypeBuilder.DefineMethod (
333                                 im.Name, interface_method_attributes,
334                                 return_type, arg_types);
335
336                         InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
337
338                         if (!RegisterMethod (mb, ip, arg_types)) {
339                                 Error111 (im);
340                                 return;
341                         }
342
343                         //
344                         // Labelling of parameters is taken care of
345                         // during the Emit phase via
346                         // MethodCore.LabelParameters method so I am
347                         // removing the old code here.
348                         //
349                         
350                         im.Builder = mb;
351                         
352                 }
353
354                 //
355                 // Populates the properties in the interface
356                 //
357                 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
358                 {
359                         PropertyBuilder pb;
360                         MethodBuilder get = null, set = null;
361                         ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
362                         if (ip.Type == null)
363                                 return;
364                         
365                         Type prop_type = ip.Type.Type;
366                         Type [] setter_args = new Type [1];
367
368                         if (prop_type == null)
369                                 return;
370
371                         if (prop_type.IsPointer && !UnsafeOK (this))
372                                 return;
373                         
374                         setter_args [0] = prop_type;
375
376                         //
377                         // FIXME: properties are missing the following
378                         // flags: hidebysig newslot specialname
379                         //
380                         pb = TypeBuilder.DefineProperty (
381                                 ip.Name, PropertyAttributes.None,
382                                 prop_type, null);
383
384                         if (ip.HasGet){
385                                 get = TypeBuilder.DefineMethod (
386                                         "get_" + ip.Name, property_attributes ,
387                                         prop_type, null);
388
389                                 //
390                                 // HACK because System.Reflection.Emit is lame
391                                 //
392                                 Type [] null_types = null;
393                                 InternalParameters inp = new InternalParameters
394                                         (null_types, Parameters.EmptyReadOnlyParameters);
395                                 
396                                 if (!RegisterMethod (get, inp, null)) {
397                                         Error111 (ip);
398                                         return;
399                                 }
400                                 
401                                 pb.SetGetMethod (get);
402                         }
403
404                         if (ip.HasSet){
405                                 setter_args [0] = prop_type;
406
407                                 set = TypeBuilder.DefineMethod (
408                                         "set_" + ip.Name, property_attributes,
409                                         TypeManager.void_type, setter_args);
410
411                                 set.DefineParameter (1, ParameterAttributes.None, "value");
412                                 pb.SetSetMethod (set);
413
414                                 //
415                                 // HACK because System.Reflection.Emit is lame
416                                 //
417                                 Parameter [] parms = new Parameter [1];
418                                 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
419                                 InternalParameters ipp = new InternalParameters (
420                                         this, new Parameters (parms, null, Location.Null));
421                                         
422                                 if (!RegisterMethod (set, ipp, setter_args)) {
423                                         Error111 (ip);
424                                         return;
425                                 }
426                         }
427
428                         TypeManager.RegisterProperty (pb, get, set);
429                         property_builders.Add (pb);
430                         ip.Builder = pb;
431                 }
432
433                 //
434                 // Populates the events in the interface
435                 //
436                 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
437                 {
438                         //
439                         // FIXME: We need to do this after delegates have been
440                         // declared or we declare them recursively.
441                         //
442                         MyEventBuilder eb;
443                         MethodBuilder add = null, remove = null;
444                         ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
445                         if (ie.Type == null)
446                                 return;
447                         
448                         Type event_type = ie.Type.Type;
449
450                         if (event_type == null)
451                                 return;
452
453                         if (event_type.IsPointer && !UnsafeOK (this))
454                                 return;
455
456                         Type [] parameters = new Type [1];
457                         parameters [0] = event_type;
458
459                         eb = new MyEventBuilder (null, TypeBuilder, ie.Name,
460                                                  EventAttributes.None, event_type);
461
462                         //
463                         // Now define the accessors
464                         //
465                         string add_name = "add_" + ie.Name;
466                         
467                         add = TypeBuilder.DefineMethod (
468                                 add_name, property_attributes, null, parameters);
469                         add.DefineParameter (1, ParameterAttributes.None, "value");
470                         eb.SetAddOnMethod (add);
471
472                         string remove_name = "remove_" + ie.Name;
473                         remove = TypeBuilder.DefineMethod (
474                                 remove_name, property_attributes, null, parameters);
475                         remove.DefineParameter (1, ParameterAttributes.None, "value");
476                         eb.SetRemoveOnMethod (remove);
477
478                         Parameter [] parms = new Parameter [1];
479                         parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
480                         InternalParameters ip = new InternalParameters (
481                                 this, new Parameters (parms, null, Location.Null));
482
483                         if (!RegisterMethod (add, ip, parameters)) {
484                                 Error111 (ie);
485                                 return;
486                         }
487                         
488                         if (!RegisterMethod (remove, ip, parameters)) {
489                                 Error111 (ie);
490                                 return;
491                         }
492
493                         TypeManager.RegisterEvent (eb, add, remove);
494                         event_builders.Add (eb);
495
496                         ie.Builder = eb;
497                 }
498
499                 //
500                 // Populates the indexers in the interface
501                 //
502                 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
503                 {
504                         PropertyBuilder pb;
505                         ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
506                         if (ii.Type == null)
507                                 return;
508                         
509                         Type prop_type = ii.Type.Type;
510                         Type [] arg_types = ii.ParameterTypes (this);
511                         Type [] value_arg_types;
512
513                         if (prop_type == null)
514                                 return;
515
516                         if (prop_type.IsPointer && !UnsafeOK (this))
517                                 return;
518                         
519                         //
520                         // Sets up the extra invisible `value' argument for setters.
521                         // 
522                         if (arg_types != null){
523                                 int count = arg_types.Length;
524                                 value_arg_types = new Type [count + 1];
525
526                                 arg_types.CopyTo (value_arg_types, 0);
527                                 value_arg_types [count] = prop_type;
528
529                                 foreach (Type t in arg_types){
530                                         if (t.IsPointer && !UnsafeOK (this))
531                                                 return;
532                                 }
533                         } else {
534                                 value_arg_types = new Type [1];
535
536                                 value_arg_types [1] = prop_type;
537                         }
538
539                         EmitContext ec = new EmitContext (parent, decl_space, Location, null,
540                                                           null, ModFlags, false);
541
542                         IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
543                         if (IndexerName == null)
544                                 IndexerName = "Item";
545                         
546                         pb = TypeBuilder.DefineProperty (
547                                 IndexerName, PropertyAttributes.None,
548                                 prop_type, arg_types);
549                         
550                         MethodBuilder set_item = null, get_item = null;
551                         if (ii.HasGet){
552                                 Parameter [] p = ii.Parameters.FixedParameters;
553                                 
554                                 get_item = TypeBuilder.DefineMethod (
555                                         "get_" + IndexerName, property_attributes,
556                                         prop_type, arg_types);
557                                 pb.SetGetMethod (get_item);
558                                 //
559                                 // HACK because System.Reflection.Emit is lame
560                                 //
561                                 InternalParameters ip = new InternalParameters (
562                                         arg_types, ii.Parameters);
563                                 
564                                 if (!RegisterMethod (get_item, ip, arg_types)) {
565                                         Error111 (ii);
566                                         return;
567                                 }
568
569                                 if (p != null){
570                                         for (int i = 0; i < p.Length; i++)
571                                                 get_item.DefineParameter (
572                                                         i + 1,
573                                                         p [i].Attributes, p [i].Name);
574                                 }
575                         }
576
577                         if (ii.HasSet){
578                                 Parameter [] p = ii.Parameters.FixedParameters;
579                                 Parameter [] pv;
580                                 int i = 0;
581                                 
582                                 pv = new Parameter [p.Length + 1];
583                                 p.CopyTo (pv, 0);
584                                 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
585                                 Parameters value_params = new Parameters (pv, null, Location.Null);
586                                 value_params.GetParameterInfo (decl_space);
587                                 
588                                 set_item = TypeBuilder.DefineMethod (
589                                         "set_" + IndexerName, property_attributes,
590                                         TypeManager.void_type, value_arg_types);
591                                 pb.SetSetMethod (set_item);
592                                 //
593                                 // HACK because System.Reflection.Emit is lame
594                                 //
595                                 InternalParameters ip = new InternalParameters (
596                                         value_arg_types, value_params);
597                                 if (!RegisterMethod (set_item, ip, value_arg_types)) {
598                                         Error111 (ii);
599                                         return;
600                                 }
601
602                                 if (p != null){
603                                         for (; i < p.Length; i++)
604                                                 set_item.DefineParameter (
605                                                         i + 1,
606                                                         p [i].Attributes, p [i].Name);
607                                 }
608                                 
609                                 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
610                         }
611
612                         property_builders.Add (pb);
613
614                         ii.Builder = pb;
615                 }
616
617                 /// <summary>
618                 ///   Performs the semantic analysis for all the interface members
619                 ///   that were declared
620                 /// </summary>
621                 bool SemanticAnalysis ()
622                 {
623                         Hashtable methods = new Hashtable ();
624
625                         
626                         if (defined_method != null){
627                                 foreach (InterfaceMethod im in defined_method){
628                                         string sig = im.GetSignature (this);
629                                         
630                                         //
631                                         // If there was an undefined Type on the signatures
632                                         // 
633                                         if (sig == null)
634                                                 continue;
635                                         
636                                         if (methods [sig] != null){
637                                                 Error111 (im);
638                                                 return false;
639                                         }
640                                 }
641                         }
642
643                         //
644                         // FIXME: Here I should check i
645                         // 
646                         return true;
647                 }
648
649                 TypeExpr GetInterfaceTypeByName (string name)
650                 {
651                         Type t = FindType (Location, name);
652
653                         if (t == null) {
654                                 Report.Error (246, Location, "The type or namespace `" + name +
655                                               "' could not be found");
656                                 return null;
657                         }
658                         
659                         if (t.IsInterface)
660                                 return new TypeExpression (t, Location);
661                                 
662                         string cause;
663                         
664                         if (t.IsValueType)
665                                 cause = "is a struct";
666                         else if (t.IsClass) 
667                                 cause = "is a class";
668                         else
669                                 cause = "Should not happen.";
670                         
671                         Report.Error (527, Location, "`"+name+"' " + cause +
672                                       ", need an interface instead");
673                         
674                         return null;
675                 }
676                 
677                 //
678                 // Returns the list of interfaces that this interface implements
679                 // Or null if it does not implement any interface.
680                 //
681                 // Sets the error boolean accoringly.
682                 //
683                 TypeExpr [] GetInterfaceBases (out bool error)
684                 {
685                         TypeExpr [] tbases;
686                         int i;
687
688                         error = false;
689                         if (Bases == null)
690                                 return null;
691                         
692                         tbases = new TypeExpr [Bases.Count];
693                         i = 0;
694
695                         foreach (string name in Bases){
696                                 TypeExpr t;
697
698                                 t = GetInterfaceTypeByName (name);
699                                 if (t == null){
700                                         error = true;
701                                         return null;
702                                 }
703
704                                 if (!t.AsAccessible (Parent, ModFlags))
705                                         Report.Error (61, Location,
706                                                       "Inconsistent accessibility: base interface `" +
707                                                       t.Name + "' is less accessible than interface `" +
708                                                       Name + "'");
709
710                                 tbases [i++] = t;
711                         }
712                         
713                         return TypeManager.ExpandInterfaces (tbases);
714                 }
715                 
716                 //
717                 // <summary>
718                 //  Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
719                 // </summary>
720                 //
721                 // TODO:
722                 //   Rework the way we recurse, because for recursive
723                 //   definitions of interfaces (A:B and B:A) we report the
724                 //   error twice, rather than once.  
725                 
726                 public override TypeBuilder DefineType ()
727                 {
728                         TypeExpr [] ifaces;
729                         bool error;
730
731                         if (TypeBuilder != null)
732                                 return TypeBuilder;
733                         
734                         if (InTransit)
735                                 return null;
736                         
737                         InTransit = true;
738                         
739                         EmitContext ec = new EmitContext (this, this, Location, null, null,
740                                                           ModFlags, false);
741
742                         ifaces = GetInterfaceBases (out error);
743
744                         if (error)
745                                 return null;
746
747                         if (IsTopLevel) {
748                                 if (TypeManager.NamespaceClash (Name, Location))
749                                         return null;
750                                 
751                                 ModuleBuilder builder = CodeGen.ModuleBuilder;
752
753                                 TypeBuilder = builder.DefineType (
754                                         Name,
755                                         InterfaceAttr,
756                                         (Type)null,   // Parent Type
757                                         null);
758                                 RootContext.RegisterOrder (this);
759                         } else {
760                                 TypeBuilder builder = Parent.TypeBuilder;
761
762                                 TypeBuilder = builder.DefineNestedType (
763                                         Basename,
764                                         InterfaceAttr,
765                                         (Type) null, //parent type
766                                         null);
767
768                                 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
769                                 tc.RegisterOrder (this);
770                         }
771
772                         if (ifaces != null) {
773                                 foreach (TypeExpr iface in ifaces) {
774                                         Type itype = iface.ResolveType (ec);
775                                         TypeBuilder.AddInterfaceImplementation (itype);
776                                 }
777                         }
778
779                         TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
780                         InTransit = false;
781
782                         return TypeBuilder;
783                 }
784
785                 //
786                 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
787                 // interface is consistent.  Either it is `Item' or it is the name defined by all the
788                 // indexers with the `IndexerName' attribute.
789                 //
790                 // Turns out that the IndexerNameAttribute is applied to each indexer,
791                 // but it is never emitted, instead a DefaultName attribute is attached
792                 // to the interface
793                 //
794                 void DefineIndexers (TypeContainer parent)
795                 {
796                         string interface_indexer_name = null;
797
798                         foreach (InterfaceIndexer ii in defined_indexer){
799
800                                 PopulateIndexer (parent, this, ii);
801
802                                 if (interface_indexer_name == null){
803                                         interface_indexer_name = IndexerName;
804                                         continue;
805                                 }
806                                 
807                                 if (IndexerName == interface_indexer_name)
808                                         continue;
809                                 
810                                 Report.Error (
811                                         668, "Two indexers have different names, " +
812                                         " you should use the same name for all your indexers");
813                         }
814                         if (interface_indexer_name == null)
815                                 interface_indexer_name = "Item";
816                         IndexerName = interface_indexer_name;
817                 }
818                 
819                 /// <summary>
820                 ///   Performs semantic analysis, and then generates the IL interfaces
821                 /// </summary>
822                 public override bool DefineMembers (TypeContainer parent)
823                 {
824                         if (members_defined)
825                                 return true;
826                         
827                         if (!SemanticAnalysis ())
828                                 return false;
829
830                         
831                         if (defined_method != null){
832                                 foreach (InterfaceMethod im in defined_method)
833                                         PopulateMethod (parent, this, im);
834                         }
835
836                         if (defined_properties != null){
837                                 foreach (InterfaceProperty ip in defined_properties)
838                                         PopulateProperty (parent, this, ip);
839                         }
840
841                         if (defined_events != null)
842                                 foreach (InterfaceEvent ie in defined_events)
843                                         PopulateEvent (parent, this, ie);
844
845                         if (defined_indexer != null) {
846                                 DefineIndexers (parent);
847
848                                 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
849                                         parent, IndexerName, ModFlags, Location);
850                                 if (cb != null)
851                                         TypeBuilder.SetCustomAttribute (cb);
852                         }
853
854 #if CACHE
855                         if (TypeBuilder.BaseType != null)
856                                 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
857
858                         member_cache = new MemberCache (this);
859 #endif
860                         members_defined = true;
861                         return true;
862                 }
863
864
865                 //
866                 // In the case of Interfaces, there is nothing to do here
867                 //
868                 public override bool Define (TypeContainer parent)
869                 {
870                         return true;
871                 }
872
873                 /// <summary>
874                 ///   Applies all the attributes.
875                 /// </summary>
876                 public void Emit (TypeContainer tc)
877                 {
878                         if (OptAttributes != null) {
879                                 EmitContext ec = new EmitContext (tc, this, Location, null, null,
880                                                                   ModFlags, false);
881                                 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
882                         }
883
884                         // Now emit attributes for each interface member
885                         if (defined_method != null) {
886                                 foreach (InterfaceMethod im in defined_method) {
887                                         EmitContext ec = new EmitContext (tc, this, Location, null,
888                                                                           im.ReturnType.Type, ModFlags, false);
889   
890                                         MethodCore.LabelParameters (ec, im.Builder,
891                                                                     im.Parameters,
892                                                                     im.OptAttributes,
893                                                                     Location);
894                                         
895                                         if (im.OptAttributes != null)
896                                                 Attribute.ApplyAttributes (ec, im.Builder,
897                                                                            im, im.OptAttributes);
898                                         
899                                 }
900                         }
901
902                         if (defined_properties != null) {
903                                 foreach (InterfaceProperty ip in defined_properties) {
904                                         EmitContext ec = new EmitContext (tc, this, Location, null,
905                                                                           null, ModFlags, false);
906                                         
907                                         if (ip.OptAttributes != null)
908                                                 Attribute.ApplyAttributes (ec, ip.Builder, ip, ip.OptAttributes);
909                                 }
910                         }
911
912                         if (defined_events != null) {
913                                 foreach (InterfaceEvent ie in defined_events) {
914                                         EmitContext ec = new EmitContext (tc, this, Location, null,
915                                                                           null, ModFlags, false);
916                                         
917                                         if (ie.OptAttributes != null)
918                                                 Attribute.ApplyAttributes (ec, ie.Builder, ie, ie.OptAttributes);
919                                 }
920                         }
921                 }
922
923                 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
924                                                                             string name,
925                                                                             int flags,
926                                                                             Location loc)
927                 {
928                         EmitContext ec = new EmitContext (parent, loc, null, null, flags);
929
930                         Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
931                                                                  ".ctor", MemberTypes.Constructor,
932                                                                  BindingFlags.Public | BindingFlags.Instance,
933                                                                  Location.Null);
934                         
935                         if (!(ml is MethodGroupExpr)) {
936                                 Console.WriteLine ("Internal error !!!!");
937                                 return null;
938                         }
939                         
940                         MethodGroupExpr mg = (MethodGroupExpr) ml;
941
942                         MethodBase constructor = mg.Methods [0];
943
944                         string [] vals = { name };
945
946                         CustomAttributeBuilder cb = null;
947                         try {
948                                 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
949                         } catch {
950                                 Report.Warning (-100, "Can not set the indexer default member attribute");
951                         }
952
953                         return cb;
954                 }
955
956                 //
957                 // IMemberContainer
958                 //
959
960                 string IMemberContainer.Name {
961                         get {
962                                 return Name;
963                         }
964                 }
965
966                 Type IMemberContainer.Type {
967                         get {
968                                 return TypeBuilder;
969                         }
970                 }
971
972                 IMemberContainer IMemberContainer.Parent {
973                         get {
974                                 return parent_container;
975                         }
976                 }
977
978                 MemberCache IMemberContainer.MemberCache {
979                         get {
980                                 return member_cache;
981                         }
982                 }
983
984                 bool IMemberContainer.IsInterface {
985                         get {
986                                 return true;
987                         }
988                 }
989
990                 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
991                 {
992                         // Interfaces only contain instance members.
993                         if ((bf & BindingFlags.Instance) == 0)
994                                 return MemberList.Empty;
995                         if ((bf & BindingFlags.Public) == 0)
996                                 return MemberList.Empty;
997
998                         ArrayList members = new ArrayList ();
999
1000                         if ((mt & MemberTypes.Method) != 0)
1001                                 members.AddRange (method_builders);
1002
1003                         if ((mt & MemberTypes.Property) != 0)
1004                                 members.AddRange (property_builders);
1005
1006                         if ((mt & MemberTypes.Event) != 0)
1007                                 members.AddRange (event_builders);
1008
1009                         return new MemberList (members);
1010                 }
1011         }
1012
1013         public class InterfaceMemberBase {
1014                 public readonly string Name;
1015                 public readonly bool IsNew;
1016                 public Attributes OptAttributes;
1017                 
1018                 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
1019                 {
1020                         Name = name;
1021                         IsNew = is_new;
1022                         OptAttributes = attrs;
1023                 }
1024         }
1025         
1026         public class InterfaceProperty : InterfaceMemberBase {
1027                 public readonly bool HasSet;
1028                 public readonly bool HasGet;
1029                 public readonly Location Location;
1030                 public Expression Type;
1031                 public PropertyBuilder Builder;
1032                 
1033                 public InterfaceProperty (Expression type, string name,
1034                                           bool is_new, bool has_get, bool has_set,
1035                                           Attributes attrs, Location loc)
1036                         : base (name, is_new, attrs)
1037                 {
1038                         Type = type;
1039                         HasGet = has_get;
1040                         HasSet = has_set;
1041                         Location = loc;
1042                 }
1043         }
1044
1045         public class InterfaceEvent : InterfaceMemberBase {
1046                 public readonly Location Location;
1047                 public Expression Type;
1048                 public MyEventBuilder Builder;
1049                 
1050                 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
1051                                        Location loc)
1052                         : base (name, is_new, attrs)
1053                 {
1054                         Type = type;
1055                         Location = loc;
1056                 }
1057         }
1058         
1059         public class InterfaceMethod : InterfaceMemberBase {
1060                 public Expression ReturnType;
1061                 public readonly Parameters Parameters;
1062                 public readonly Location Location;
1063                 public MethodBuilder Builder;
1064                 
1065                 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1066                                         Attributes attrs, Location l)
1067                         : base (name, is_new, attrs)
1068                 {
1069                         this.ReturnType = return_type;
1070                         this.Parameters = args;
1071                         Location = l;
1072                 }
1073
1074                 /// <summary>
1075                 ///   Returns the signature for this interface method
1076                 /// </summary>
1077                 public string GetSignature (DeclSpace ds)
1078                 {
1079                         ReturnType = ds.ResolveTypeExpr (ReturnType, false, Location);
1080                         if (ReturnType == null)
1081                                 return null;
1082                         
1083                         Type ret = ReturnType.Type;
1084                         string args = Parameters.GetSignature (ds);
1085
1086                         if ((ret == null) || (args == null))
1087                                 return null;
1088                         
1089                         return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1090                 }
1091
1092                 public Type [] ParameterTypes (DeclSpace ds)
1093                 {
1094                         return Parameters.GetParameterInfo (ds);
1095                 }
1096         }
1097
1098         public class InterfaceIndexer : InterfaceMemberBase {
1099                 public readonly bool HasGet, HasSet;
1100                 public readonly Parameters Parameters;
1101                 public readonly Location Location;
1102                 public Expression Type;
1103                 public PropertyBuilder Builder;
1104                 
1105                 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1106                                          bool is_new, Attributes attrs, Location loc)
1107                         : base ("", is_new, attrs)
1108                 {
1109                         Type = type;
1110                         Parameters = args;
1111                         HasGet = do_get;
1112                         HasSet = do_set;
1113                         Location = loc;
1114                 }
1115
1116                 public Type [] ParameterTypes (DeclSpace ds)
1117                 {
1118                         return Parameters.GetParameterInfo (ds);
1119                 }
1120         }
1121 }