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