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