2003-01-05 Sebastien Pouliot <spouliot@videotron.ca>
[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 = this.ResolveType (im.ReturnType, false, im.Location);
304                         Type [] arg_types = im.ParameterTypes (this);
305                         MethodBuilder mb;
306                         Parameter [] p;
307                         int i;
308
309                         if (return_type == null)
310                                 return;
311
312                         if (return_type.IsPointer && !UnsafeOK (this))
313                                 return;
314
315                         if (arg_types == null)
316                                 return;
317
318                         foreach (Type t in arg_types){
319
320                                 if (t == null)
321                                         return;
322                                 
323                                 if (t.IsPointer && !UnsafeOK (this))
324                                         return;
325                         }
326                         
327                         //
328                         // Create the method
329                         //
330                         mb = TypeBuilder.DefineMethod (
331                                 im.Name, interface_method_attributes,
332                                 return_type, arg_types);
333
334                         InternalParameters ip = new InternalParameters (arg_types, im.Parameters);
335                         
336                         if (!RegisterMethod (mb, ip, arg_types)) {
337                                 Error111 (im);
338                                 return;
339                         }
340
341                         //
342                         // Define each type attribute (in/out/ref) and
343                         // the argument names.
344                         //
345                         p = im.Parameters.FixedParameters;
346                         if (p != null){
347                                 for (i = 0; i < p.Length; i++)
348                                         mb.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
349
350                                 if (i != arg_types.Length)
351                                         Console.WriteLine ("Implement the type definition for params");
352                         }
353
354                         EmitContext ec = new EmitContext (parent, decl_space, Location, null,
355                                                           return_type, ModFlags, false);
356
357                         if (im.OptAttributes != null)
358                                 Attribute.ApplyAttributes (ec, mb, im, im.OptAttributes);
359                 }
360
361                 //
362                 // Populates the properties in the interface
363                 //
364                 void PopulateProperty (TypeContainer parent, DeclSpace decl_space, InterfaceProperty ip)
365                 {
366                         PropertyBuilder pb;
367                         MethodBuilder get = null, set = null;
368                         ip.Type = this.ResolveTypeExpr (ip.Type, false, ip.Location);
369                         if (ip.Type == null)
370                                 return;
371                         
372                         Type prop_type = ip.Type.Type;
373                         Type [] setter_args = new Type [1];
374
375                         if (prop_type == null)
376                                 return;
377
378                         if (prop_type.IsPointer && !UnsafeOK (this))
379                                 return;
380                         
381                         setter_args [0] = prop_type;
382
383                         //
384                         // FIXME: properties are missing the following
385                         // flags: hidebysig newslot specialname
386                         //
387                         pb = TypeBuilder.DefineProperty (
388                                 ip.Name, PropertyAttributes.None,
389                                 prop_type, null);
390
391                         if (ip.HasGet){
392                                 get = TypeBuilder.DefineMethod (
393                                         "get_" + ip.Name, property_attributes ,
394                                         prop_type, null);
395
396                                 //
397                                 // HACK because System.Reflection.Emit is lame
398                                 //
399                                 Type [] null_types = null;
400                                 InternalParameters inp = new InternalParameters
401                                         (null_types, Parameters.EmptyReadOnlyParameters);
402                                 
403                                 if (!RegisterMethod (get, inp, null)) {
404                                         Error111 (ip);
405                                         return;
406                                 }
407                                 
408                                 pb.SetGetMethod (get);
409                         }
410
411                         if (ip.HasSet){
412                                 setter_args [0] = prop_type;
413
414                                 set = TypeBuilder.DefineMethod (
415                                         "set_" + ip.Name, property_attributes,
416                                         TypeManager.void_type, setter_args);
417
418                                 set.DefineParameter (1, ParameterAttributes.None, "value");
419                                 pb.SetSetMethod (set);
420
421                                 //
422                                 // HACK because System.Reflection.Emit is lame
423                                 //
424                                 Parameter [] parms = new Parameter [1];
425                                 parms [0] = new Parameter (ip.Type, "value", Parameter.Modifier.NONE, null);
426                                 InternalParameters ipp = new InternalParameters (
427                                         this, new Parameters (parms, null, Location.Null));
428                                         
429                                 if (!RegisterMethod (set, ipp, setter_args)) {
430                                         Error111 (ip);
431                                         return;
432                                 }
433                         }
434
435                         EmitContext ec = new EmitContext (parent, decl_space, Location, null,
436                                                           null, ModFlags, false);
437
438                         if (ip.OptAttributes != null)
439                                 Attribute.ApplyAttributes (ec, pb, ip, ip.OptAttributes);
440
441                         TypeManager.RegisterProperty (pb, get, set);
442                         property_builders.Add (pb);
443                 }
444
445                 //
446                 // Populates the events in the interface
447                 //
448                 void PopulateEvent (TypeContainer parent, DeclSpace decl_space, InterfaceEvent ie)
449                 {
450                         //
451                         // FIXME: We need to do this after delegates have been
452                         // declared or we declare them recursively.
453                         //
454                         MyEventBuilder eb;
455                         MethodBuilder add = null, remove = null;
456                         ie.Type = this.ResolveTypeExpr (ie.Type, false, ie.Location);
457                         if (ie.Type == null)
458                                 return;
459                         
460                         Type event_type = ie.Type.Type;
461
462                         if (event_type == null)
463                                 return;
464
465                         if (event_type.IsPointer && !UnsafeOK (this))
466                                 return;
467
468                         Type [] parameters = new Type [1];
469                         parameters [0] = event_type;
470
471                         eb = new MyEventBuilder (null, TypeBuilder, ie.Name,
472                                                  EventAttributes.None, event_type);
473
474                         //
475                         // Now define the accessors
476                         //
477                         string add_name = "add_" + ie.Name;
478                         
479                         add = TypeBuilder.DefineMethod (
480                                 add_name, property_attributes, null, parameters);
481                         add.DefineParameter (1, ParameterAttributes.None, "value");
482                         eb.SetAddOnMethod (add);
483
484                         string remove_name = "remove_" + ie.Name;
485                         remove = TypeBuilder.DefineMethod (
486                                 remove_name, property_attributes, null, parameters);
487                         remove.DefineParameter (1, ParameterAttributes.None, "value");
488                         eb.SetRemoveOnMethod (remove);
489
490                         Parameter [] parms = new Parameter [1];
491                         parms [0] = new Parameter (ie.Type, "value", Parameter.Modifier.NONE, null);
492                         InternalParameters ip = new InternalParameters (
493                                 this, new Parameters (parms, null, Location.Null));
494
495                         if (!RegisterMethod (add, ip, parameters)) {
496                                 Error111 (ie);
497                                 return;
498                         }
499                         
500                         if (!RegisterMethod (remove, ip, parameters)) {
501                                 Error111 (ie);
502                                 return;
503                         }
504
505                         EmitContext ec = new EmitContext (parent, decl_space, Location, null,
506                                                           null, ModFlags, false);
507
508
509                         if (ie.OptAttributes != null)
510                                 Attribute.ApplyAttributes (ec, eb, ie, ie.OptAttributes);
511
512                         TypeManager.RegisterEvent (eb, add, remove);
513                         event_builders.Add (eb);
514                 }
515
516                 //
517                 // Populates the indexers in the interface
518                 //
519                 void PopulateIndexer (TypeContainer parent, DeclSpace decl_space, InterfaceIndexer ii)
520                 {
521                         PropertyBuilder pb;
522                         ii.Type = this.ResolveTypeExpr (ii.Type, false, ii.Location);
523                         if (ii.Type == null)
524                                 return;
525                         
526                         Type prop_type = ii.Type.Type;
527                         Type [] arg_types = ii.ParameterTypes (this);
528                         Type [] value_arg_types;
529
530                         if (prop_type == null)
531                                 return;
532
533                         if (prop_type.IsPointer && !UnsafeOK (this))
534                                 return;
535                         
536                         //
537                         // Sets up the extra invisible `value' argument for setters.
538                         // 
539                         if (arg_types != null){
540                                 int count = arg_types.Length;
541                                 value_arg_types = new Type [count + 1];
542
543                                 arg_types.CopyTo (value_arg_types, 0);
544                                 value_arg_types [count] = prop_type;
545
546                                 foreach (Type t in arg_types){
547                                         if (t.IsPointer && !UnsafeOK (this))
548                                                 return;
549                                 }
550                         } else {
551                                 value_arg_types = new Type [1];
552
553                                 value_arg_types [1] = prop_type;
554                         }
555
556                         EmitContext ec = new EmitContext (parent, decl_space, Location, null,
557                                                           null, ModFlags, false);
558
559                         IndexerName = Attribute.ScanForIndexerName (ec, ii.OptAttributes);
560                         if (IndexerName == null)
561                                 IndexerName = "Item";
562                         
563                         pb = TypeBuilder.DefineProperty (
564                                 IndexerName, PropertyAttributes.None,
565                                 prop_type, arg_types);
566                         
567                         MethodBuilder set_item = null, get_item = null;
568                         if (ii.HasGet){
569                                 Parameter [] p = ii.Parameters.FixedParameters;
570                                 
571                                 get_item = TypeBuilder.DefineMethod (
572                                         "get_" + IndexerName, property_attributes,
573                                         prop_type, arg_types);
574                                 pb.SetGetMethod (get_item);
575                                 //
576                                 // HACK because System.Reflection.Emit is lame
577                                 //
578                                 InternalParameters ip = new InternalParameters (
579                                         arg_types, ii.Parameters);
580                                 
581                                 if (!RegisterMethod (get_item, ip, arg_types)) {
582                                         Error111 (ii);
583                                         return;
584                                 }
585
586                                 if (p != null){
587                                         for (int i = 0; i < p.Length; i++)
588                                                 get_item.DefineParameter (
589                                                         i + 1,
590                                                         p [i].Attributes, p [i].Name);
591                                 }
592                         }
593
594                         if (ii.HasSet){
595                                 Parameter [] p = ii.Parameters.FixedParameters;
596                                 Parameter [] pv;
597                                 int i = 0;
598                                 
599                                 pv = new Parameter [p.Length + 1];
600                                 p.CopyTo (pv, 0);
601                                 pv [p.Length] = new Parameter (ii.Type, "value", Parameter.Modifier.NONE, null);
602                                 Parameters value_params = new Parameters (pv, null, Location.Null);
603                                 value_params.GetParameterInfo (decl_space);
604                                 
605                                 set_item = TypeBuilder.DefineMethod (
606                                         "set_" + IndexerName, property_attributes,
607                                         TypeManager.void_type, value_arg_types);
608                                 pb.SetSetMethod (set_item);
609                                 //
610                                 // HACK because System.Reflection.Emit is lame
611                                 //
612                                 InternalParameters ip = new InternalParameters (
613                                         value_arg_types, value_params);
614                                 if (!RegisterMethod (set_item, ip, value_arg_types)) {
615                                         Error111 (ii);
616                                         return;
617                                 }
618
619                                 if (p != null){
620                                         for (; i < p.Length; i++)
621                                                 set_item.DefineParameter (
622                                                         i + 1,
623                                                         p [i].Attributes, p [i].Name);
624                                 }
625                                 
626                                 set_item.DefineParameter (i + 1, ParameterAttributes.None, "value");
627                         }
628
629                         if (ii.OptAttributes != null)
630                                 Attribute.ApplyAttributes (ec, pb, ii, ii.OptAttributes);
631
632                         property_builders.Add (pb);
633                 }
634
635                 /// <summary>
636                 ///   Performs the semantic analysis for all the interface members
637                 ///   that were declared
638                 /// </summary>
639                 bool SemanticAnalysis ()
640                 {
641                         Hashtable methods = new Hashtable ();
642
643                         
644                         if (defined_method != null){
645                                 foreach (InterfaceMethod im in defined_method){
646                                         string sig = im.GetSignature (this);
647                                         
648                                         //
649                                         // If there was an undefined Type on the signatures
650                                         // 
651                                         if (sig == null)
652                                                 continue;
653                                         
654                                         if (methods [sig] != null){
655                                                 Error111 (im);
656                                                 return false;
657                                         }
658                                 }
659                         }
660
661                         //
662                         // FIXME: Here I should check i
663                         // 
664                         return true;
665                 }
666
667                 Type GetInterfaceTypeByName (string name)
668                 {
669                         Type t = FindType (Location, name);
670
671                         if (t == null) {
672                                 Report.Error (246, Location, "The type or namespace `" + name +
673                                               "' could not be found");
674                                 return null;
675                         }
676                         
677                         if (t.IsInterface)
678                                 return t;
679                                 
680                         string cause;
681                         
682                         if (t.IsValueType)
683                                 cause = "is a struct";
684                         else if (t.IsClass) 
685                                 cause = "is a class";
686                         else
687                                 cause = "Should not happen.";
688                         
689                         Report.Error (527, Location, "`"+name+"' " + cause +
690                                       ", need an interface instead");
691                         
692                         return null;
693                 }
694                 
695                 //
696                 // Returns the list of interfaces that this interface implements
697                 // Or null if it does not implement any interface.
698                 //
699                 // Sets the error boolean accoringly.
700                 //
701                 Type [] GetInterfaceBases (out bool error)
702                 {
703                         Type [] tbases;
704                         int i;
705
706                         error = false;
707                         if (Bases == null)
708                                 return null;
709                         
710                         tbases = new Type [Bases.Count];
711                         i = 0;
712
713                         foreach (string name in Bases){
714                                 Type t;
715
716                                 t = GetInterfaceTypeByName (name);
717                                 if (t == null){
718                                         error = true;
719                                         return null;
720                                 }
721
722                                 if (!Parent.AsAccessible (t, ModFlags))
723                                         Report.Error (61, Location,
724                                                       "Inconsistent accessibility: base interface `" +
725                                                       TypeManager.CSharpName (t) + "' is less " +
726                                                       "accessible than interface `" +
727                                                       Name + "'");
728
729                                 tbases [i++] = t;
730                         }
731                         
732                         return TypeManager.ExpandInterfaces (tbases);
733                 }
734                 
735                 //
736                 // <summary>
737                 //  Defines the Interface in the appropriate ModuleBuilder or TypeBuilder
738                 // </summary>
739                 //
740                 // TODO:
741                 //   Rework the way we recurse, because for recursive
742                 //   definitions of interfaces (A:B and B:A) we report the
743                 //   error twice, rather than once.  
744                 
745                 public override TypeBuilder DefineType ()
746                 {
747                         Type [] ifaces;
748                         bool error;
749
750                         if (TypeBuilder != null)
751                                 return TypeBuilder;
752                         
753                         if (InTransit)
754                                 return null;
755                         
756                         InTransit = true;
757                         
758                         ifaces = GetInterfaceBases (out error);
759
760                         if (error)
761                                 return null;
762
763                         if (IsTopLevel) {
764                                 ModuleBuilder builder = CodeGen.ModuleBuilder;
765
766                                 TypeBuilder = builder.DefineType (
767                                         Name,
768                                         InterfaceAttr,
769                                         (Type)null,   // Parent Type
770                                         ifaces);
771                                 RootContext.RegisterOrder (this);
772                         } else {
773                                 TypeBuilder builder = Parent.TypeBuilder;
774
775                                 TypeBuilder = builder.DefineNestedType (
776                                         Basename,
777                                         InterfaceAttr,
778                                         (Type) null, //parent type
779                                         ifaces);
780
781                                 TypeContainer tc = TypeManager.LookupTypeContainer (builder);
782                                 tc.RegisterOrder (this);
783                         }
784
785                         TypeManager.AddUserInterface (Name, TypeBuilder, this, ifaces);
786                         InTransit = false;
787
788                         return TypeBuilder;
789                 }
790
791                 //
792                 // Defines the indexers, and also verifies that the IndexerNameAttribute in the
793                 // interface is consistent.  Either it is `Item' or it is the name defined by all the
794                 // indexers with the `IndexerName' attribute.
795                 //
796                 // Turns out that the IndexerNameAttribute is applied to each indexer,
797                 // but it is never emitted, instead a DefaultName attribute is attached
798                 // to the interface
799                 //
800                 void DefineIndexers (TypeContainer parent)
801                 {
802                         string interface_indexer_name = null;
803
804                         foreach (InterfaceIndexer ii in defined_indexer){
805
806                                 PopulateIndexer (parent, this, ii);
807
808                                 if (interface_indexer_name == null){
809                                         interface_indexer_name = IndexerName;
810                                         continue;
811                                 }
812                                 
813                                 if (IndexerName == interface_indexer_name)
814                                         continue;
815                                 
816                                 Report.Error (
817                                         668, "Two indexers have different names, " +
818                                         " you should use the same name for all your indexers");
819                         }
820                         if (interface_indexer_name == null)
821                                 interface_indexer_name = "Item";
822                         IndexerName = interface_indexer_name;
823                 }
824                 
825                 /// <summary>
826                 ///   Performs semantic analysis, and then generates the IL interfaces
827                 /// </summary>
828                 public override bool DefineMembers (TypeContainer parent)
829                 {
830                         if (!SemanticAnalysis ())
831                                 return false;
832
833                         if (defined_method != null){
834                                 foreach (InterfaceMethod im in defined_method)
835                                         PopulateMethod (parent, this, im);
836                         }
837
838                         if (defined_properties != null){
839                                 foreach (InterfaceProperty ip in defined_properties)
840                                         PopulateProperty (parent, this, ip);
841                         }
842
843                         if (defined_events != null)
844                                 foreach (InterfaceEvent ie in defined_events)
845                                         PopulateEvent (parent, this, ie);
846
847                         if (defined_indexer != null) {
848                                 DefineIndexers (parent);
849
850                                 CustomAttributeBuilder cb = EmitDefaultMemberAttr (
851                                         parent, IndexerName, ModFlags, Location);
852                                 if (cb != null)
853                                         TypeBuilder.SetCustomAttribute (cb);
854                         }
855
856 #if CACHE
857                         if (TypeBuilder.BaseType != null)
858                                 parent_container = TypeManager.LookupMemberContainer (TypeBuilder.BaseType);
859
860                         member_cache = new MemberCache (this);
861 #endif
862                         members_defined = true;
863                         return true;
864                 }
865
866                 /// <summary>
867                 ///   Applies all the attributes.
868                 /// </summary>
869                 public override bool Define (TypeContainer parent)
870                 {
871                         if (OptAttributes != null) {
872                                 EmitContext ec = new EmitContext (parent, this, Location, null, null,
873                                                                   ModFlags, false);
874                                 Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
875                         }
876
877                         return true;
878                 }
879
880                 public static CustomAttributeBuilder EmitDefaultMemberAttr (TypeContainer parent,
881                                                                             string name,
882                                                                             int flags,
883                                                                             Location loc)
884                 {
885                         EmitContext ec = new EmitContext (parent, loc, null, null, flags);
886
887                         Expression ml = Expression.MemberLookup (ec, TypeManager.default_member_type,
888                                                                  ".ctor", MemberTypes.Constructor,
889                                                                  BindingFlags.Public | BindingFlags.Instance,
890                                                                  Location.Null);
891                         
892                         if (!(ml is MethodGroupExpr)) {
893                                 Console.WriteLine ("Internal error !!!!");
894                                 return null;
895                         }
896                         
897                         MethodGroupExpr mg = (MethodGroupExpr) ml;
898
899                         MethodBase constructor = mg.Methods [0];
900
901                         string [] vals = { name };
902
903                         CustomAttributeBuilder cb = null;
904                         try {
905                                 cb = new CustomAttributeBuilder ((ConstructorInfo) constructor, vals);
906                         } catch {
907                                 Report.Warning (-100, "Can not set the indexer default member attribute");
908                         }
909
910                         return cb;
911                 }
912
913                 //
914                 // IMemberContainer
915                 //
916
917                 string IMemberContainer.Name {
918                         get {
919                                 return Name;
920                         }
921                 }
922
923                 Type IMemberContainer.Type {
924                         get {
925                                 return TypeBuilder;
926                         }
927                 }
928
929                 IMemberContainer IMemberContainer.Parent {
930                         get {
931                                 return parent_container;
932                         }
933                 }
934
935                 MemberCache IMemberContainer.MemberCache {
936                         get {
937                                 return member_cache;
938                         }
939                 }
940
941                 bool IMemberContainer.IsInterface {
942                         get {
943                                 return true;
944                         }
945                 }
946
947                 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
948                 {
949                         // Interfaces only contain instance members.
950                         if ((bf & BindingFlags.Instance) == 0)
951                                 return MemberList.Empty;
952                         if ((bf & BindingFlags.Public) == 0)
953                                 return MemberList.Empty;
954
955                         ArrayList members = new ArrayList ();
956
957                         if ((mt & MemberTypes.Method) != 0)
958                                 members.AddRange (method_builders);
959
960                         if ((mt & MemberTypes.Property) != 0)
961                                 members.AddRange (property_builders);
962
963                         if ((mt & MemberTypes.Event) != 0)
964                                 members.AddRange (event_builders);
965
966                         return new MemberList (members);
967                 }
968         }
969
970         public class InterfaceMemberBase {
971                 public readonly string Name;
972                 public readonly bool IsNew;
973                 public Attributes OptAttributes;
974                 
975                 public InterfaceMemberBase (string name, bool is_new, Attributes attrs)
976                 {
977                         Name = name;
978                         IsNew = is_new;
979                         OptAttributes = attrs;
980                 }
981         }
982         
983         public class InterfaceProperty : InterfaceMemberBase {
984                 public readonly bool HasSet;
985                 public readonly bool HasGet;
986                 public readonly Location Location;
987                 public Expression Type;
988                 
989                 public InterfaceProperty (Expression type, string name,
990                                           bool is_new, bool has_get, bool has_set,
991                                           Attributes attrs, Location loc)
992                         : base (name, is_new, attrs)
993                 {
994                         Type = type;
995                         HasGet = has_get;
996                         HasSet = has_set;
997                         Location = loc;
998                 }
999         }
1000
1001         public class InterfaceEvent : InterfaceMemberBase {
1002                 public readonly Location Location;
1003                 public Expression Type;
1004                 
1005                 public InterfaceEvent (Expression type, string name, bool is_new, Attributes attrs,
1006                                        Location loc)
1007                         : base (name, is_new, attrs)
1008                 {
1009                         Type = type;
1010                         Location = loc;
1011                 }
1012         }
1013         
1014         public class InterfaceMethod : InterfaceMemberBase {
1015                 public readonly Expression ReturnType;
1016                 public readonly Parameters Parameters;
1017                 public readonly Location Location;
1018                 
1019                 public InterfaceMethod (Expression return_type, string name, bool is_new, Parameters args,
1020                                         Attributes attrs, Location l)
1021                         : base (name, is_new, attrs)
1022                 {
1023                         this.ReturnType = return_type;
1024                         this.Parameters = args;
1025                         Location = l;
1026                 }
1027
1028                 /// <summary>
1029                 ///   Returns the signature for this interface method
1030                 /// </summary>
1031                 public string GetSignature (DeclSpace ds)
1032                 {
1033                         Type ret = ds.ResolveType (ReturnType, false, Location);
1034                         string args = Parameters.GetSignature (ds);
1035
1036                         if ((ret == null) || (args == null))
1037                                 return null;
1038                         
1039                         return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
1040                 }
1041
1042                 public Type [] ParameterTypes (DeclSpace ds)
1043                 {
1044                         return Parameters.GetParameterInfo (ds);
1045                 }
1046         }
1047
1048         public class InterfaceIndexer : InterfaceMemberBase {
1049                 public readonly bool HasGet, HasSet;
1050                 public readonly Parameters Parameters;
1051                 public readonly Location Location;
1052                 public Expression Type;
1053                 
1054                 public InterfaceIndexer (Expression type, Parameters args, bool do_get, bool do_set,
1055                                          bool is_new, Attributes attrs, Location loc)
1056                         : base ("", is_new, attrs)
1057                 {
1058                         Type = type;
1059                         Parameters = args;
1060                         HasGet = do_get;
1061                         HasSet = do_set;
1062                         Location = loc;
1063                 }
1064
1065                 public Type [] ParameterTypes (DeclSpace ds)
1066                 {
1067                         return Parameters.GetParameterInfo (ds);
1068                 }
1069         }
1070 }