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