**** Merged r41649-r41677 from MCS ****
[mono.git] / mcs / gmcs / generic.cs
1 //
2 // generic.cs: Generics support
3 //
4 // Authors: Martin Baulig (martin@ximian.com)
5 //          Miguel de Icaza (miguel@ximian.com)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
10 // (C) 2004 Novell, Inc
11 //
12 using System;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Globalization;
16 using System.Collections;
17 using System.Text;
18 using System.Text.RegularExpressions;
19         
20 namespace Mono.CSharp {
21
22         public abstract class GenericConstraints {
23                 public abstract GenericParameterAttributes Attributes {
24                         get;
25                 }
26
27                 public bool HasConstructorConstraint {
28                         get { return (Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
29                 }
30
31                 public bool HasReferenceTypeConstraint {
32                         get { return (Attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
33                 }
34
35                 public bool HasValueTypeConstraint {
36                         get { return (Attributes & GenericParameterAttributes.ValueTypeConstraint) != 0; }
37                 }
38
39                 public virtual bool HasClassConstraint {
40                         get { return ClassConstraint != null; }
41                 }
42
43                 public abstract Type ClassConstraint {
44                         get;
45                 }
46
47                 public abstract Type[] InterfaceConstraints {
48                         get;
49                 }
50
51                 public abstract Type EffectiveBaseClass {
52                         get;
53                 }
54
55                 // <summary>
56                 //   Returns whether the type parameter is "known to be a reference type".
57                 // </summary>
58                 public virtual bool IsReferenceType {
59                         get {
60                                 if (HasReferenceTypeConstraint)
61                                         return true;
62                                 if (HasValueTypeConstraint)
63                                         return false;
64
65                                 if (ClassConstraint != null) {
66                                         if (ClassConstraint.IsValueType)
67                                                 return false;
68
69                                         if (ClassConstraint != TypeManager.object_type)
70                                                 return true;
71                                 }
72
73                                 foreach (Type t in InterfaceConstraints) {
74                                         if (!t.IsGenericParameter)
75                                                 continue;
76
77                                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
78                                         if ((gc != null) && gc.IsReferenceType)
79                                                 return true;
80                                 }
81
82                                 return false;
83                         }
84                 }
85
86                 // <summary>
87                 //   Returns whether the type parameter is "known to be a value type".
88                 // </summary>
89                 public virtual bool IsValueType {
90                         get {
91                                 if (HasValueTypeConstraint)
92                                         return true;
93                                 if (HasReferenceTypeConstraint)
94                                         return false;
95
96                                 if (ClassConstraint != null) {
97                                         if (!ClassConstraint.IsValueType)
98                                                 return false;
99
100                                         if (ClassConstraint != TypeManager.value_type)
101                                                 return true;
102                                 }
103
104                                 foreach (Type t in InterfaceConstraints) {
105                                         if (!t.IsGenericParameter)
106                                                 continue;
107
108                                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
109                                         if ((gc != null) && gc.IsValueType)
110                                                 return true;
111                                 }
112
113                                 return false;
114                         }
115                 }
116         }
117
118         public enum SpecialConstraint
119         {
120                 Constructor,
121                 ReferenceType,
122                 ValueType
123         }
124
125         //
126         // Tracks the constraints for a type parameter
127         //
128         public class Constraints : GenericConstraints {
129                 string name;
130                 ArrayList constraints;
131                 Location loc;
132                 
133                 //
134                 // name is the identifier, constraints is an arraylist of
135                 // Expressions (with types) or `true' for the constructor constraint.
136                 // 
137                 public Constraints (string name, ArrayList constraints,
138                                     Location loc)
139                 {
140                         this.name = name;
141                         this.constraints = constraints;
142                         this.loc = loc;
143                 }
144
145                 public string TypeParameter {
146                         get {
147                                 return name;
148                         }
149                 }
150
151                 GenericParameterAttributes attrs;
152                 TypeExpr class_constraint;
153                 ArrayList iface_constraints;
154                 ArrayList type_param_constraints;
155                 int num_constraints, first_constraint;
156                 Type class_constraint_type;
157                 Type[] iface_constraint_types;
158                 Type effective_base_type;
159
160                 public bool Resolve (EmitContext ec)
161                 {
162                         iface_constraints = new ArrayList ();
163                         type_param_constraints = new ArrayList ();
164
165                         foreach (object obj in constraints) {
166                                 if (HasConstructorConstraint) {
167                                         Report.Error (401, loc,
168                                                       "The new() constraint must be last.");
169                                         return false;
170                                 }
171
172                                 if (obj is SpecialConstraint) {
173                                         SpecialConstraint sc = (SpecialConstraint) obj;
174
175                                         if (sc == SpecialConstraint.Constructor) {
176                                                 if (!HasValueTypeConstraint) {
177                                                         attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
178                                                         continue;
179                                                 }
180
181                                                 Report.Error (
182                                                         451, loc, "The new () constraint " +
183                                                         "cannot be used with the `struct' " +
184                                                         "constraint.");
185                                                 return false;
186                                         }
187
188                                         if ((num_constraints > 0) || HasReferenceTypeConstraint || HasValueTypeConstraint) {
189                                                 Report.Error (449, loc,
190                                                               "The `class' or `struct' " +
191                                                               "constraint must be first");
192                                                 return false;
193                                         }
194
195                                         if (sc == SpecialConstraint.ReferenceType)
196                                                 attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
197                                         else
198                                                 attrs |= GenericParameterAttributes.ValueTypeConstraint;
199                                         continue;
200                                 }
201
202                                 TypeExpr expr;
203                                 if (obj is ConstructedType) {
204                                         ConstructedType cexpr = (ConstructedType) obj;
205                                         if (!cexpr.ResolveConstructedType (ec))
206                                                 return false;
207                                         expr = cexpr;
208                                 } else
209                                         expr = ((Expression) obj).ResolveAsTypeTerminal (ec);
210
211                                 if (expr == null)
212                                         return false;
213
214                                 TypeParameterExpr texpr = expr as TypeParameterExpr;
215                                 if (texpr != null)
216                                         type_param_constraints.Add (expr);
217                                 else if (expr.IsInterface)
218                                         iface_constraints.Add (expr);
219                                 else if (class_constraint != null) {
220                                         Report.Error (406, loc,
221                                                       "`{0}': the class constraint for `{1}' " +
222                                                       "must come before any other constraints.",
223                                                       expr.Name, name);
224                                         return false;
225                                 } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
226                                         Report.Error (450, loc, "`{0}': cannot specify both " +
227                                                       "a constraint class and the `class' " +
228                                                       "or `struct' constraint.", expr.Name);
229                                         return false;
230                                 } else
231                                         class_constraint = expr;
232
233                                 num_constraints++;
234                         }
235
236                         return true;
237                 }
238
239                 bool CheckTypeParameterConstraints (TypeParameter tparam, Hashtable seen)
240                 {
241                         seen.Add (tparam, true);
242
243                         Constraints constraints = tparam.Constraints;
244                         if (constraints == null)
245                                 return true;
246
247                         if (constraints.HasValueTypeConstraint) {
248                                 Report.Error (456, loc, "Type parameter `{0}' has " +
249                                               "the `struct' constraint, so it cannot " +
250                                               "be used as a constraint for `{1}'",
251                                               tparam.Name, name);
252                                 return false;
253                         }
254
255                         if (constraints.type_param_constraints == null)
256                                 return true;
257
258                         foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
259                                 if (seen.Contains (expr.TypeParameter)) {
260                                         Report.Error (454, loc, "Circular constraint " +
261                                                       "dependency involving `{0}' and `{1}'",
262                                                       tparam.Name, expr.Name);
263                                         return false;
264                                 }
265
266                                 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
267                                         return false;
268                         }
269
270                         return true;
271                 }
272
273                 public bool ResolveTypes (EmitContext ec)
274                 {
275                         if (effective_base_type != null)
276                                 return true;
277
278                         foreach (object obj in constraints) {
279                                 ConstructedType cexpr = obj as ConstructedType;
280                                 if (cexpr == null)
281                                         continue;
282
283                                 if (!cexpr.CheckConstraints (ec))
284                                         return false;
285                         }
286
287                         foreach (TypeParameterExpr expr in type_param_constraints) {
288                                 Hashtable seen = new Hashtable ();
289                                 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
290                                         return false;
291                         }
292
293                         ArrayList list = new ArrayList ();
294
295                         foreach (TypeExpr iface_constraint in iface_constraints) {
296                                 foreach (Type type in list) {
297                                         if (!type.Equals (iface_constraint.Type))
298                                                 continue;
299
300                                         Report.Error (405, loc,
301                                                       "Duplicate constraint `{0}' for type " +
302                                                       "parameter `{1}'.", iface_constraint.Type,
303                                                       name);
304                                         return false;
305                                 }
306
307                                 list.Add (iface_constraint.Type);
308                         }
309
310                         foreach (TypeParameterExpr expr in type_param_constraints) {
311                                 foreach (Type type in list) {
312                                         if (!type.Equals (expr.Type))
313                                                 continue;
314
315                                         Report.Error (405, loc,
316                                                       "Duplicate constraint `{0}' for type " +
317                                                       "parameter `{1}'.", expr.Type, name);
318                                         return false;
319                                 }
320
321                                 list.Add (expr.Type);
322                         }
323
324                         iface_constraint_types = new Type [list.Count];
325                         list.CopyTo (iface_constraint_types, 0);
326
327                         if (class_constraint != null) {
328                                 class_constraint_type = class_constraint.Type;
329                                 if (class_constraint_type == null)
330                                         return false;
331
332                                 if (class_constraint_type.IsSealed) {
333                                         Report.Error (701, loc,
334                                                       "`{0}' is not a valid bound.  Bounds " +
335                                                       "must be interfaces or non sealed " +
336                                                       "classes", class_constraint_type);
337                                         return false;
338                                 }
339
340                                 if ((class_constraint_type == TypeManager.array_type) ||
341                                     (class_constraint_type == TypeManager.delegate_type) ||
342                                     (class_constraint_type == TypeManager.enum_type) ||
343                                     (class_constraint_type == TypeManager.value_type) ||
344                                     (class_constraint_type == TypeManager.object_type)) {
345                                         Report.Error (702, loc,
346                                                       "Bound cannot be special class `{0}'",
347                                                       class_constraint_type);
348                                         return false;
349                                 }
350                         }
351
352                         if (class_constraint_type != null)
353                                 effective_base_type = class_constraint_type;
354                         else if (HasValueTypeConstraint)
355                                 effective_base_type = TypeManager.value_type;
356                         else
357                                 effective_base_type = TypeManager.object_type;
358
359                         return true;
360                 }
361
362                 public bool CheckDependencies (EmitContext ec)
363                 {
364                         foreach (TypeParameterExpr expr in type_param_constraints) {
365                                 if (!CheckDependencies (expr.TypeParameter, ec))
366                                         return false;
367                         }
368
369                         return true;
370                 }
371
372                 bool CheckDependencies (TypeParameter tparam, EmitContext ec)
373                 {
374                         Constraints constraints = tparam.Constraints;
375                         if (constraints == null)
376                                 return true;
377
378                         if (HasValueTypeConstraint && constraints.HasClassConstraint) {
379                                 Report.Error (455, loc, "Type parameter `{0}' inherits " +
380                                               "conflicting constraints `{1}' and `{2}'",
381                                               name, constraints.ClassConstraint,
382                                               "System.ValueType");
383                                 return false;
384                         }
385
386                         if (HasClassConstraint && constraints.HasClassConstraint) {
387                                 Type t1 = ClassConstraint;
388                                 TypeExpr e1 = class_constraint;
389                                 Type t2 = constraints.ClassConstraint;
390                                 TypeExpr e2 = constraints.class_constraint;
391
392                                 if (!Convert.ImplicitReferenceConversionExists (ec, e1, t2) &&
393                                     !Convert.ImplicitReferenceConversionExists (ec, e2, t1)) {
394                                         Report.Error (455, loc,
395                                                       "Type parameter `{0}' inherits " +
396                                                       "conflicting constraints `{1}' and `{2}'",
397                                                       name, t1, t2);
398                                         return false;
399                                 }
400                         }
401
402                         if (constraints.type_param_constraints == null)
403                                 return true;
404
405                         foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
406                                 if (!CheckDependencies (expr.TypeParameter, ec))
407                                         return false;
408                         }
409
410                         return true;
411                 }
412
413                 public void Define (GenericTypeParameterBuilder type)
414                 {
415                         type.SetGenericParameterAttributes (attrs);
416                 }
417
418                 public override GenericParameterAttributes Attributes {
419                         get { return attrs; }
420                 }
421
422                 public override bool HasClassConstraint {
423                         get { return class_constraint != null; }
424                 }
425
426                 public override Type ClassConstraint {
427                         get { return class_constraint_type; }
428                 }
429
430                 public override Type[] InterfaceConstraints {
431                         get { return iface_constraint_types; }
432                 }
433
434                 public override Type EffectiveBaseClass {
435                         get { return effective_base_type; }
436                 }
437
438                 internal bool IsSubclassOf (Type t)
439                 {
440                         if ((class_constraint_type != null) &&
441                             class_constraint_type.IsSubclassOf (t))
442                                 return true;
443
444                         if (iface_constraint_types == null)
445                                 return false;
446
447                         foreach (Type iface in iface_constraint_types) {
448                                 if (TypeManager.IsSubclassOf (iface, t))
449                                         return true;
450                         }
451
452                         return false;
453                 }
454
455                 public bool CheckInterfaceMethod (EmitContext ec, GenericConstraints gc)
456                 {
457                         if (gc.Attributes != attrs)
458                                 return false;
459
460                         if (HasClassConstraint != gc.HasClassConstraint)
461                                 return false;
462                         if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
463                                 return false;
464
465                         int gc_icount = gc.InterfaceConstraints != null ?
466                                 gc.InterfaceConstraints.Length : 0;
467                         int icount = InterfaceConstraints != null ?
468                                 InterfaceConstraints.Length : 0;
469
470                         if (gc_icount != icount)
471                                 return false;
472
473                         foreach (Type iface in gc.InterfaceConstraints) {
474                                 bool ok = false;
475                                 foreach (Type check in InterfaceConstraints) {
476                                         if (TypeManager.IsEqual (iface, check)) {
477                                                 ok = true;
478                                                 break;
479                                         }
480                                 }
481
482                                 if (!ok)
483                                         return false;
484                         }
485
486                         return true;
487                 }
488         }
489
490         //
491         // This type represents a generic type parameter
492         //
493         public class TypeParameter : MemberCore, IMemberContainer {
494                 string name;
495                 GenericConstraints gc;
496                 Constraints constraints;
497                 Location loc;
498                 GenericTypeParameterBuilder type;
499
500                 public TypeParameter (TypeContainer parent, string name,
501                                       Constraints constraints, Location loc)
502                         : base (parent, new MemberName (name), null, loc)
503                 {
504                         this.name = name;
505                         this.constraints = constraints;
506                         this.loc = loc;
507                 }
508
509                 public GenericConstraints GenericConstraints {
510                         get {
511                                 return gc != null ? gc : constraints;
512                         }
513                 }
514
515                 public Constraints Constraints {
516                         get {
517                                 return constraints;
518                         }
519                 }
520
521                 public bool HasConstructorConstraint {
522                         get {
523                                 if (constraints != null)
524                                         return constraints.HasConstructorConstraint;
525
526                                 return false;
527                         }
528                 }
529
530                 public Type Type {
531                         get {
532                                 return type;
533                         }
534                 }
535
536                 public bool Resolve (DeclSpace ds)
537                 {
538                         if (constraints != null) {
539                                 if (!constraints.Resolve (ds.EmitContext)) {
540                                         constraints = null;
541                                         return false;
542                                 }
543                         }
544
545                         return true;
546                 }
547
548                 public void Define (GenericTypeParameterBuilder type)
549                 {
550                         if (this.type != null)
551                                 throw new InvalidOperationException ();
552
553                         this.type = type;
554                         TypeManager.AddTypeParameter (type, this);
555                 }
556
557                 public void DefineConstraints ()
558                 {
559                         if (constraints != null)
560                                 constraints.Define (type);
561                 }
562
563                 public bool ResolveType (EmitContext ec)
564                 {
565                         if (constraints != null) {
566                                 if (!constraints.ResolveTypes (ec)) {
567                                         constraints = null;
568                                         return false;
569                                 }
570                         }
571
572                         return true;
573                 }
574
575                 public bool DefineType (EmitContext ec)
576                 {
577                         return DefineType (ec, null, null, false);
578                 }
579
580                 public bool DefineType (EmitContext ec, MethodBuilder builder,
581                                         MethodInfo implementing, bool is_override)
582                 {
583                         if (!ResolveType (ec))
584                                 return false;
585
586                         if (implementing != null) {
587                                 if (is_override && (constraints != null)) {
588                                         Report.Error (
589                                                 460, loc, "Constraints for override and " +
590                                                 "explicit interface implementation methods " +
591                                                 "are inherited from the base method so they " +
592                                                 "cannot be specified directly");
593                                         return false;
594                                 }
595
596                                 MethodBase mb = implementing;
597                                 if (mb.Mono_IsInflatedMethod)
598                                         mb = mb.GetGenericMethodDefinition ();
599
600                                 int pos = type.GenericParameterPosition;
601                                 ParameterData pd = TypeManager.GetParameterData (mb);
602                                 GenericConstraints temp_gc = pd.GenericConstraints (pos);
603                                 Type mparam = mb.GetGenericArguments () [pos];
604
605                                 if (temp_gc != null)
606                                         gc = new InflatedConstraints (temp_gc, implementing.DeclaringType);
607                                 else if (constraints != null)
608                                         gc = new InflatedConstraints (constraints, implementing.DeclaringType);
609
610                                 bool ok = true;
611                                 if (constraints != null) {
612                                         if (temp_gc == null)
613                                                 ok = false;
614                                         else if (!constraints.CheckInterfaceMethod (ec, gc))
615                                                 ok = false;
616                                 } else {
617                                         if (!is_override && (temp_gc != null))
618                                                 ok = false;
619                                 }
620
621                                 if (!ok) {
622                                         Report.SymbolRelatedToPreviousError (implementing);
623
624                                         Report.Error (
625                                                 425, loc, "The constraints for type " +
626                                                 "parameter `{0}' of method `{1}' must match " +
627                                                 "the constraints for type parameter `{2}' " +
628                                                 "of interface method `{3}'.  Consider using " +
629                                                 "an explicit interface implementation instead",
630                                                 Name, TypeManager.CSharpSignature (builder),
631                                                 mparam, TypeManager.CSharpSignature (mb));
632                                         return false;
633                                 }
634                         } else {
635                                 gc = (GenericConstraints) constraints;
636                         }
637
638                         if (gc == null)
639                                 return true;
640
641                         if (gc.HasClassConstraint)
642                                 type.SetBaseTypeConstraint (gc.ClassConstraint);
643
644                         type.SetInterfaceConstraints (gc.InterfaceConstraints);
645                         TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
646
647                         return true;
648                 }
649
650                 public bool CheckDependencies (EmitContext ec)
651                 {
652                         if (constraints != null)
653                                 return constraints.CheckDependencies (ec);
654
655                         return true;
656                 }
657
658                 public bool UpdateConstraints (EmitContext ec, Constraints new_constraints)
659                 {
660                         //
661                         // We're used in partial generic type definitions.
662                         // If `check' is false, we just encountered the first ClassPart which has
663                         // constraints - they become our "real" constraints.
664                         // Otherwise we're called after the type parameters have already been defined
665                         // and check whether the constraints are the same in all parts.
666                         //
667                         if (type == null)
668                                 throw new InvalidOperationException ();
669
670                         if (constraints == null) {
671                                 new_constraints = constraints;
672                                 return true;
673                         } else if (new_constraints == null)
674                                 return true;
675
676                         if (!new_constraints.Resolve (ec))
677                                 return false;
678                         if (!new_constraints.ResolveTypes (ec))
679                                 return false;
680
681                         return constraints.CheckInterfaceMethod (ec, new_constraints);
682                 }
683
684                 public override string DocCommentHeader {
685                         get {
686                                 throw new InvalidOperationException (
687                                         "Unexpected attempt to get doc comment from " + this.GetType () + ".");
688                         }
689                 }
690
691                 //
692                 // MemberContainer
693                 //
694
695                 public override bool Define ()
696                 {
697                         return true;
698                 }
699
700                 protected override void VerifyObsoleteAttribute ()
701                 { }
702
703                 public override void ApplyAttributeBuilder (Attribute a,
704                                                             CustomAttributeBuilder cb)
705                 { }
706
707                 public override AttributeTargets AttributeTargets {
708                         get {
709                                 return (AttributeTargets) 0;
710                         }
711                 }
712
713                 public override string[] ValidAttributeTargets {
714                         get {
715                                 return new string [0];
716                         }
717                 }
718
719                 //
720                 // IMemberContainer
721                 //
722
723                 string IMemberContainer.Name {
724                         get { return Name; }
725                 }
726
727                 MemberCache IMemberContainer.BaseCache {
728                         get { return null; }
729                 }
730
731                 bool IMemberContainer.IsInterface {
732                         get { return true; }
733                 }
734
735                 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
736                 {
737                         return FindMembers (mt, bf, null, null);
738                 }
739
740                 MemberCache IMemberContainer.MemberCache {
741                         get { return null; }
742                 }
743
744                 public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
745                                                MemberFilter filter, object criteria)
746                 {
747                         if (constraints == null)
748                                 return MemberList.Empty;
749
750                         ArrayList members = new ArrayList ();
751
752                         GenericConstraints gc = (GenericConstraints) constraints;
753
754                         if (gc.HasClassConstraint) {
755                                 MemberList list = TypeManager.FindMembers (
756                                         gc.ClassConstraint, mt, bf, filter, criteria);
757
758                                 members.AddRange (list);
759                         }
760
761                         foreach (Type t in gc.InterfaceConstraints) {
762                                 MemberList list = TypeManager.FindMembers (
763                                         t, mt, bf, filter, criteria);
764
765                                 members.AddRange (list);
766                         }
767
768                         return new MemberList (members);
769                 }
770
771                 public bool IsSubclassOf (Type t)
772                 {
773                         if (type.Equals (t))
774                                 return true;
775
776                         if (constraints != null)
777                                 return constraints.IsSubclassOf (t);
778
779                         return false;
780                 }
781
782                 public override string ToString ()
783                 {
784                         return "TypeParameter[" + name + "]";
785                 }
786
787                 protected class InflatedConstraints : GenericConstraints
788                 {
789                         GenericConstraints gc;
790                         Type base_type;
791                         Type class_constraint;
792                         Type[] iface_constraints;
793                         Type[] dargs;
794                         Type declaring;
795
796                         public InflatedConstraints (GenericConstraints gc, Type declaring)
797                         {
798                                 this.gc = gc;
799                                 this.declaring = declaring;
800
801                                 dargs = TypeManager.GetTypeArguments (declaring);
802
803                                 ArrayList list = new ArrayList ();
804                                 if (gc.HasClassConstraint)
805                                         list.Add (inflate (gc.ClassConstraint));
806                                 foreach (Type iface in gc.InterfaceConstraints)
807                                         list.Add (inflate (iface));
808
809                                 bool has_class_constr = false;
810                                 if (list.Count > 0) {
811                                         Type first = (Type) list [0];
812                                         has_class_constr = !first.IsInterface && !first.IsGenericParameter;
813                                 }
814
815                                 if ((list.Count > 0) && has_class_constr) {
816                                         class_constraint = (Type) list [0];
817                                         iface_constraints = new Type [list.Count - 1];
818                                         list.CopyTo (1, iface_constraints, 0, list.Count - 1);
819                                 } else {
820                                         iface_constraints = new Type [list.Count];
821                                         list.CopyTo (iface_constraints, 0);
822                                 }
823
824                                 if (HasValueTypeConstraint)
825                                         base_type = TypeManager.value_type;
826                                 else if (class_constraint != null)
827                                         base_type = class_constraint;
828                                 else
829                                         base_type = TypeManager.object_type;
830                         }
831
832                         Type inflate (Type t)
833                         {
834                                 if (t == null)
835                                         return null;
836                                 if (t.IsGenericParameter)
837                                         return dargs [t.GenericParameterPosition];
838                                 if (t.IsGenericInstance) {
839                                         t = t.GetGenericTypeDefinition ();
840                                         t = t.BindGenericParameters (dargs);
841                                 }
842
843                                 return t;
844                         }
845
846                         public override GenericParameterAttributes Attributes {
847                                 get { return gc.Attributes; }
848                         }
849
850                         public override Type ClassConstraint {
851                                 get { return class_constraint; }
852                         }
853
854                         public override Type EffectiveBaseClass {
855                                 get { return base_type; }
856                         }
857
858                         public override Type[] InterfaceConstraints {
859                                 get { return iface_constraints; }
860                         }
861                 }
862         }
863
864         //
865         // This type represents a generic type parameter reference.
866         //
867         // These expressions are born in a fully resolved state.
868         //
869         public class TypeParameterExpr : TypeExpr {
870                 TypeParameter type_parameter;
871
872                 public override string Name {
873                         get {
874                                 return type_parameter.Name;
875                         }
876                 }
877
878                 public override string FullName {
879                         get {
880                                 return type_parameter.Name;
881                         }
882                 }
883
884                 public TypeParameter TypeParameter {
885                         get {
886                                 return type_parameter;
887                         }
888                 }
889                 
890                 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
891                 {
892                         this.type_parameter = type_parameter;
893                         this.loc = loc;
894                 }
895
896                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
897                 {
898                         type = type_parameter.Type;
899
900                         return this;
901                 }
902
903                 public override bool IsInterface {
904                         get { return false; }
905                 }
906
907                 public override bool CheckAccessLevel (DeclSpace ds)
908                 {
909                         return true;
910                 }
911
912                 public void Error_CannotUseAsUnmanagedType (Location loc)
913                 {
914                         Report.Error (-203, loc, "Can not use type parameter as unamanged type");
915                 }
916         }
917
918         public class TypeArguments {
919                 public readonly Location Location;
920                 ArrayList args;
921                 Type[] atypes;
922                 int dimension;
923                 bool has_type_args;
924                 bool created;
925                 
926                 public TypeArguments (Location loc)
927                 {
928                         args = new ArrayList ();
929                         this.Location = loc;
930                 }
931
932                 public TypeArguments (int dimension, Location loc)
933                 {
934                         this.dimension = dimension;
935                         this.Location = loc;
936                 }
937
938                 public void Add (Expression type)
939                 {
940                         if (created)
941                                 throw new InvalidOperationException ();
942
943                         args.Add (type);
944                 }
945
946                 public void Add (TypeArguments new_args)
947                 {
948                         if (created)
949                                 throw new InvalidOperationException ();
950
951                         args.AddRange (new_args.args);
952                 }
953
954                 public string[] GetDeclarations ()
955                 {
956                         string[] ret = new string [args.Count];
957                         for (int i = 0; i < args.Count; i++) {
958                                 SimpleName sn = args [i] as SimpleName;
959                                 if (sn != null) {
960                                         ret [i] = sn.Name;
961                                         continue;
962                                 }
963
964                                 Report.Error (81, Location, "Type parameter declaration " +
965                                               "must be an identifier not a type");
966                                 return null;
967                         }
968                         return ret;
969                 }
970
971                 public Type[] Arguments {
972                         get {
973                                 return atypes;
974                         }
975                 }
976
977                 public bool HasTypeArguments {
978                         get {
979                                 return has_type_args;
980                         }
981                 }
982
983                 public int Count {
984                         get {
985                                 if (dimension > 0)
986                                         return dimension;
987                                 else
988                                         return args.Count;
989                         }
990                 }
991
992                 public bool IsUnbound {
993                         get {
994                                 return dimension > 0;
995                         }
996                 }
997
998                 public override string ToString ()
999                 {
1000                         StringBuilder s = new StringBuilder ();
1001
1002                         int count = Count;
1003                         for (int i = 0; i < count; i++){
1004                                 //
1005                                 // FIXME: Use TypeManager.CSharpname once we have the type
1006                                 //
1007                                 if (args != null)
1008                                         s.Append (args [i].ToString ());
1009                                 if (i+1 < count)
1010                                         s.Append (",");
1011                         }
1012                         return s.ToString ();
1013                 }
1014
1015                 public bool Resolve (EmitContext ec)
1016                 {
1017                         int count = args.Count;
1018                         bool ok = true;
1019
1020                         atypes = new Type [count];
1021
1022                         for (int i = 0; i < count; i++){
1023                                 TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec);
1024                                 if (te == null) {
1025                                         ok = false;
1026                                         continue;
1027                                 }
1028                                 if (te is TypeParameterExpr)
1029                                         has_type_args = true;
1030
1031                                 if (te.Type.IsPointer) {
1032                                         Report.Error (306, Location, "The type `{0}' may not be used " +
1033                                                       "as a type argument.", TypeManager.CSharpName (te.Type));
1034                                         return false;
1035                                 }
1036
1037                                 atypes [i] = te.Type;
1038                         }
1039                         return ok;
1040                 }
1041         }
1042         
1043         public class ConstructedType : TypeExpr {
1044                 string full_name;
1045                 FullNamedExpression name;
1046                 TypeArguments args;
1047                 Type[] gen_params, atypes;
1048                 Type gt;
1049                 
1050                 public ConstructedType (FullNamedExpression fname, TypeArguments args, Location l)
1051                 {
1052                         loc = l;
1053                         this.name = fname;
1054                         this.args = args;
1055
1056                         eclass = ExprClass.Type;
1057                         full_name = name + "<" + args.ToString () + ">";
1058                 }
1059
1060                 protected ConstructedType (TypeArguments args, Location l)
1061                 {
1062                         loc = l;
1063                         this.args = args;
1064
1065                         eclass = ExprClass.Type;
1066                 }
1067
1068                 protected ConstructedType (TypeParameter[] type_params, Location l)
1069                 {
1070                         loc = l;
1071
1072                         args = new TypeArguments (l);
1073                         foreach (TypeParameter type_param in type_params)
1074                                 args.Add (new TypeParameterExpr (type_param, l));
1075
1076                         eclass = ExprClass.Type;
1077                 }
1078
1079                 public ConstructedType (Type t, TypeParameter[] type_params, Location l)
1080                         : this (type_params, l)
1081                 {
1082                         gt = t.GetGenericTypeDefinition ();
1083
1084                         this.name = new TypeExpression (gt, l);
1085                         full_name = gt.FullName + "<" + args.ToString () + ">";
1086                 }
1087
1088                 public ConstructedType (Type t, TypeArguments args, Location l)
1089                         : this (args, l)
1090                 {
1091                         gt = t.GetGenericTypeDefinition ();
1092
1093                         this.name = new TypeExpression (gt, l);
1094                         full_name = gt.FullName + "<" + args.ToString () + ">";
1095                 }
1096
1097                 public TypeArguments TypeArguments {
1098                         get { return args; }
1099                 }
1100
1101                 protected string DeclarationName {
1102                         get {
1103                                 StringBuilder sb = new StringBuilder ();
1104                                 sb.Append (gt.FullName);
1105                                 sb.Append ("<");
1106                                 for (int i = 0; i < gen_params.Length; i++) {
1107                                         if (i > 0)
1108                                                 sb.Append (",");
1109                                         sb.Append (gen_params [i]);
1110                                 }
1111                                 sb.Append (">");
1112                                 return sb.ToString ();
1113                         }
1114                 }
1115
1116                 protected bool CheckConstraint (EmitContext ec, Type ptype, Expression expr,
1117                                                 Type ctype)
1118                 {
1119                         if (TypeManager.HasGenericArguments (ctype)) {
1120                                 Type[] types = TypeManager.GetTypeArguments (ctype);
1121
1122                                 TypeArguments new_args = new TypeArguments (loc);
1123
1124                                 for (int i = 0; i < types.Length; i++) {
1125                                         Type t = types [i];
1126
1127                                         if (t.IsGenericParameter) {
1128                                                 int pos = t.GenericParameterPosition;
1129                                                 t = args.Arguments [pos];
1130                                         }
1131                                         new_args.Add (new TypeExpression (t, loc));
1132                                 }
1133
1134                                 TypeExpr ct = new ConstructedType (ctype, new_args, loc);
1135                                 if (ct.ResolveAsTypeTerminal (ec) == null)
1136                                         return false;
1137                                 ctype = ct.Type;
1138                         }
1139
1140                         return Convert.ImplicitStandardConversionExists (ec, expr, ctype);
1141                 }
1142
1143                 protected bool CheckConstraints (EmitContext ec, int index)
1144                 {
1145                         Type atype = atypes [index];
1146                         Type ptype = gen_params [index];
1147
1148                         if (atype == ptype)
1149                                 return true;
1150
1151                         Expression aexpr = new EmptyExpression (atype);
1152
1153                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1154                         if (gc == null)
1155                                 return true;
1156
1157                         bool is_class, is_struct;
1158                         if (atype.IsGenericParameter) {
1159                                 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1160                                 if (agc != null) {
1161                                         is_class = agc.HasReferenceTypeConstraint;
1162                                         is_struct = agc.HasValueTypeConstraint;
1163                                 } else {
1164                                         is_class = is_struct = false;
1165                                 }
1166                         } else {
1167                                 is_class = atype.IsClass;
1168                                 is_struct = atype.IsValueType;
1169                         }
1170
1171                         //
1172                         // First, check the `class' and `struct' constraints.
1173                         //
1174                         if (gc.HasReferenceTypeConstraint && !is_class) {
1175                                 Report.Error (452, loc, "The type `{0}' must be " +
1176                                               "a reference type in order to use it " +
1177                                               "as type parameter `{1}' in the " +
1178                                               "generic type or method `{2}'.",
1179                                               atype, ptype, DeclarationName);
1180                                 return false;
1181                         } else if (gc.HasValueTypeConstraint && !is_struct) {
1182                                 Report.Error (453, loc, "The type `{0}' must be " +
1183                                               "a value type in order to use it " +
1184                                               "as type parameter `{1}' in the " +
1185                                               "generic type or method `{2}'.",
1186                                               atype, ptype, DeclarationName);
1187                                 return false;
1188                         }
1189
1190                         //
1191                         // The class constraint comes next.
1192                         //
1193                         if (gc.HasClassConstraint) {
1194                                 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint)) {
1195                                         Report.Error (309, loc, "The type `{0}' must be " +
1196                                                       "convertible to `{1}' in order to " +
1197                                                       "use it as parameter `{2}' in the " +
1198                                                       "generic type or method `{3}'",
1199                                                       atype, gc.ClassConstraint, ptype, DeclarationName);
1200                                         return false;
1201                                 }
1202                         }
1203
1204                         //
1205                         // Now, check the interface constraints.
1206                         //
1207                         foreach (Type it in gc.InterfaceConstraints) {
1208                                 Type itype;
1209                                 if (it.IsGenericParameter)
1210                                         itype = atypes [it.GenericParameterPosition];
1211                                 else
1212                                         itype = it;
1213
1214                                 if (!CheckConstraint (ec, ptype, aexpr, itype)) {
1215                                         Report.Error (309, loc, "The type `{0}' must be " +
1216                                                       "convertible to `{1}' in order to " +
1217                                                       "use it as parameter `{2}' in the " +
1218                                                       "generic type or method `{3}'",
1219                                                       atype, itype, ptype, DeclarationName);
1220                                         return false;
1221                                 }
1222                         }
1223
1224                         //
1225                         // Finally, check the constructor constraint.
1226                         //
1227
1228                         if (!gc.HasConstructorConstraint)
1229                                 return true;
1230
1231                         if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
1232                                 return true;
1233
1234                         MethodGroupExpr mg = Expression.MemberLookup (
1235                                 ec, atype, ".ctor", MemberTypes.Constructor,
1236                                 BindingFlags.Public | BindingFlags.Instance |
1237                                 BindingFlags.DeclaredOnly, loc)
1238                                 as MethodGroupExpr;
1239
1240                         if (atype.IsAbstract || (mg == null) || !mg.IsInstance) {
1241                                 Report.Error (310, loc, "The type `{0}' must have a public " +
1242                                               "parameterless constructor in order to use it " +
1243                                               "as parameter `{1}' in the generic type or " +
1244                                               "method `{2}'", atype, ptype, DeclarationName);
1245                                 return false;
1246                         }
1247
1248                         return true;
1249                 }
1250
1251                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1252                 {
1253                         if (!ResolveConstructedType (ec))
1254                                 return null;
1255
1256                         return this;
1257                 }
1258
1259                 public bool CheckConstraints (EmitContext ec)
1260                 {
1261                         for (int i = 0; i < gen_params.Length; i++) {
1262                                 if (!CheckConstraints (ec, i))
1263                                         return false;
1264                         }
1265
1266                         return true;
1267                 }
1268
1269                 public override TypeExpr ResolveAsTypeTerminal (EmitContext ec)
1270                 {
1271                         if (base.ResolveAsTypeTerminal (ec) == null)
1272                                 return null;
1273
1274                         if (!CheckConstraints (ec))
1275                                 return null;
1276
1277                         return this;
1278                 }
1279
1280                 public bool ResolveConstructedType (EmitContext ec)
1281                 {
1282                         if (type != null)
1283                                 return true;
1284                         if (gt != null)
1285                                 return DoResolveType (ec);
1286
1287                         int num_args;
1288                         Type t = name.Type;
1289
1290                         if (t == null) {
1291                                 Report.Error (246, loc, "Cannot find type `{0}'<...>", Name);
1292                                 return false;
1293                         }
1294
1295                         num_args = TypeManager.GetNumberOfTypeArguments (t);
1296                         if (num_args == 0) {
1297                                 Report.Error (308, loc,
1298                                               "The non-generic type `{0}' cannot " +
1299                                               "be used with type arguments.",
1300                                               TypeManager.CSharpName (t));
1301                                 return false;
1302                         }
1303
1304                         gt = t.GetGenericTypeDefinition ();
1305                         return DoResolveType (ec);
1306                 }
1307
1308                 bool DoResolveType (EmitContext ec)
1309                 {
1310                         //
1311                         // Resolve the arguments.
1312                         //
1313                         if (args.Resolve (ec) == false)
1314                                 return false;
1315
1316                         gen_params = gt.GetGenericArguments ();
1317                         atypes = args.Arguments;
1318
1319                         if (atypes.Length != gen_params.Length) {
1320                                 Report.Error (305, loc,
1321                                               "Using the generic type `{0}' " +
1322                                               "requires {1} type arguments",
1323                                               TypeManager.GetFullName (gt),
1324                                               gen_params.Length);
1325                                 return false;
1326                         }
1327
1328                         //
1329                         // Now bind the parameters.
1330                         //
1331                         type = gt.BindGenericParameters (atypes);
1332                         return true;
1333                 }
1334
1335                 public Expression GetSimpleName (EmitContext ec)
1336                 {
1337                         return this;
1338                 }
1339
1340                 public override bool CheckAccessLevel (DeclSpace ds)
1341                 {
1342                         return ds.CheckAccessLevel (gt);
1343                 }
1344
1345                 public override bool AsAccessible (DeclSpace ds, int flags)
1346                 {
1347                         return ds.AsAccessible (gt, flags);
1348                 }
1349
1350                 public override bool IsClass {
1351                         get { return gt.IsClass; }
1352                 }
1353
1354                 public override bool IsValueType {
1355                         get { return gt.IsValueType; }
1356                 }
1357
1358                 public override bool IsInterface {
1359                         get { return gt.IsInterface; }
1360                 }
1361
1362                 public override bool IsSealed {
1363                         get { return gt.IsSealed; }
1364                 }
1365
1366                 public override bool Equals (object obj)
1367                 {
1368                         ConstructedType cobj = obj as ConstructedType;
1369                         if (cobj == null)
1370                                 return false;
1371
1372                         if ((type == null) || (cobj.type == null))
1373                                 return false;
1374
1375                         return type == cobj.type;
1376                 }
1377
1378                 public override int GetHashCode ()
1379                 {
1380                         return base.GetHashCode ();
1381                 }
1382
1383                 public override string Name {
1384                         get {
1385                                 return full_name;
1386                         }
1387                 }
1388
1389
1390                 public override string FullName {
1391                         get {
1392                                 return full_name;
1393                         }
1394                 }
1395         }
1396
1397         public class GenericMethod : DeclSpace
1398         {
1399                 public GenericMethod (NamespaceEntry ns, TypeContainer parent,
1400                                       MemberName name, Location l)
1401                         : base (ns, parent, name, null, l)
1402                 { }
1403
1404                 public override TypeBuilder DefineType ()
1405                 {
1406                         throw new Exception ();
1407                 }
1408
1409                 public override bool Define ()
1410                 {
1411                         for (int i = 0; i < TypeParameters.Length; i++)
1412                                 if (!TypeParameters [i].Resolve (Parent))
1413                                         return false;
1414
1415                         return true;
1416                 }
1417
1418                 public bool Define (MethodBuilder mb, Type return_type)
1419                 {
1420                         if (!Define ())
1421                                 return false;
1422
1423                         GenericTypeParameterBuilder[] gen_params;
1424                         string[] names = MemberName.TypeArguments.GetDeclarations ();
1425                         gen_params = mb.DefineGenericParameters (names);
1426                         for (int i = 0; i < TypeParameters.Length; i++)
1427                                 TypeParameters [i].Define (gen_params [i]);
1428
1429                         ec = new EmitContext (
1430                                 this, this, Location, null, return_type, ModFlags, false);
1431
1432                         for (int i = 0; i < TypeParameters.Length; i++) {
1433                                 if (!TypeParameters [i].ResolveType (ec))
1434                                         return false;
1435                         }
1436
1437                         return true;
1438                 }
1439
1440                 public bool DefineType (EmitContext ec, MethodBuilder mb,
1441                                         MethodInfo implementing, bool is_override)
1442                 {
1443                         for (int i = 0; i < TypeParameters.Length; i++)
1444                                 if (!TypeParameters [i].DefineType (
1445                                             ec, mb, implementing, is_override))
1446                                         return false;
1447
1448                         return true;
1449                 }
1450
1451                 public override bool DefineMembers (TypeContainer parent)
1452                 {
1453                         return true;
1454                 }
1455
1456                 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1457                                                         MemberFilter filter, object criteria)
1458                 {
1459                         throw new Exception ();
1460                 }               
1461
1462                 public override MemberCache MemberCache {
1463                         get {
1464                                 return null;
1465                         }
1466                 }
1467
1468                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1469                 {
1470                         // FIXME
1471                 }
1472
1473                 protected override void VerifyObsoleteAttribute()
1474                 {
1475                         // FIXME
1476                 }
1477
1478                 public override AttributeTargets AttributeTargets {
1479                         get {
1480                                 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1481                         }
1482                 }
1483
1484                 public override string DocCommentHeader {
1485                         get { return "M:"; }
1486                 }
1487         }
1488
1489         public class DefaultValueExpression : Expression
1490         {
1491                 Expression expr;
1492                 LocalTemporary temp_storage;
1493
1494                 public DefaultValueExpression (Expression expr, Location loc)
1495                 {
1496                         this.expr = expr;
1497                         this.loc = loc;
1498                 }
1499
1500                 public override Expression DoResolve (EmitContext ec)
1501                 {
1502                         TypeExpr texpr = expr.ResolveAsTypeTerminal (ec);
1503                         if (texpr == null)
1504                                 return null;
1505
1506                         type = texpr.Type;
1507                         if (type.IsGenericParameter || TypeManager.IsValueType (type))
1508                                 temp_storage = new LocalTemporary (ec, type);
1509
1510                         eclass = ExprClass.Variable;
1511                         return this;
1512                 }
1513
1514                 public override void Emit (EmitContext ec)
1515                 {
1516                         if (temp_storage != null) {
1517                                 temp_storage.AddressOf (ec, AddressOp.LoadStore);
1518                                 ec.ig.Emit (OpCodes.Initobj, type);
1519                                 temp_storage.Emit (ec);
1520                         } else
1521                                 ec.ig.Emit (OpCodes.Ldnull);
1522                 }
1523         }
1524
1525         public class NullableType : TypeExpr
1526         {
1527                 Expression underlying;
1528
1529                 public NullableType (Expression underlying, Location l)
1530                 {
1531                         this.underlying = underlying;
1532                         loc = l;
1533
1534                         eclass = ExprClass.Type;
1535                 }
1536
1537                 public NullableType (Type type, Location loc)
1538                         : this (new TypeExpression (type, loc), loc)
1539                 { }
1540
1541                 public override string Name {
1542                         get { return underlying.ToString () + "?"; }
1543                 }
1544
1545                 public override string FullName {
1546                         get { return underlying.ToString () + "?"; }
1547                 }
1548
1549                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1550                 {
1551                         TypeArguments args = new TypeArguments (loc);
1552                         args.Add (underlying);
1553
1554                         ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
1555                         return ctype.ResolveAsTypeTerminal (ec);
1556                 }
1557         }
1558
1559         public partial class TypeManager
1560         {
1561                 //
1562                 // A list of core types that the compiler requires or uses
1563                 //
1564                 static public Type new_constraint_attr_type;
1565                 static public Type activator_type;
1566                 static public Type generic_ienumerator_type;
1567                 static public Type generic_ienumerable_type;
1568                 static public Type generic_nullable_type;
1569
1570                 // <remarks>
1571                 //   Tracks the generic parameters.
1572                 // </remarks>
1573                 static PtrHashtable builder_to_type_param;
1574
1575                 //
1576                 // These methods are called by code generated by the compiler
1577                 //
1578                 static public MethodInfo activator_create_instance;
1579
1580                 static void InitGenerics ()
1581                 {
1582                         builder_to_type_param = new PtrHashtable ();
1583                 }
1584
1585                 static void CleanUpGenerics ()
1586                 {
1587                         builder_to_type_param = null;
1588                 }
1589
1590                 static void InitGenericCoreTypes ()
1591                 {
1592                         activator_type = CoreLookupType ("System.Activator");
1593                         new_constraint_attr_type = CoreLookupType (
1594                                 "System.Runtime.CompilerServices.NewConstraintAttribute");
1595
1596                         generic_ienumerator_type = CoreLookupType ("System.Collections.Generic.IEnumerator", 1);
1597                         generic_ienumerable_type = CoreLookupType ("System.Collections.Generic.IEnumerable", 1);
1598                         generic_nullable_type = CoreLookupType ("System.Nullable", 1);
1599                 }
1600
1601                 static void InitGenericCodeHelpers ()
1602                 {
1603                         // Activator
1604                         Type [] type_arg = { type_type };
1605                         activator_create_instance = GetMethod (
1606                                 activator_type, "CreateInstance", type_arg);
1607                 }
1608
1609                 static Type CoreLookupType (string name, int arity)
1610                 {
1611                         return CoreLookupType (MemberName.MakeName (name, arity));
1612                 }
1613
1614                 public static void AddTypeParameter (Type t, TypeParameter tparam)
1615                 {
1616                         if (!builder_to_type_param.Contains (t))
1617                                 builder_to_type_param.Add (t, tparam);
1618                 }
1619
1620                 public static TypeContainer LookupGenericTypeContainer (Type t)
1621                 {
1622                         while (t.IsGenericInstance)
1623                                 t = t.GetGenericTypeDefinition ();
1624
1625                         return LookupTypeContainer (t);
1626                 }
1627
1628                 public static TypeParameter LookupTypeParameter (Type t)
1629                 {
1630                         return (TypeParameter) builder_to_type_param [t];
1631                 }
1632
1633                 public static bool HasConstructorConstraint (Type t)
1634                 {
1635                         GenericConstraints gc = GetTypeParameterConstraints (t);
1636                         if (gc == null)
1637                                 return false;
1638
1639                         return (gc.Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
1640                 }
1641
1642                 public static GenericConstraints GetTypeParameterConstraints (Type t)
1643                 {
1644                         if (!t.IsGenericParameter)
1645                                 throw new InvalidOperationException ();
1646
1647                         TypeParameter tparam = LookupTypeParameter (t);
1648                         if (tparam != null)
1649                                 return tparam.GenericConstraints;
1650
1651                         return new ReflectionConstraints (t);
1652                 }
1653
1654                 public static bool IsGeneric (Type t)
1655                 {
1656                         DeclSpace ds = (DeclSpace) builder_to_declspace [t];
1657
1658                         return ds.IsGeneric;
1659                 }
1660
1661                 public static bool HasGenericArguments (Type t)
1662                 {
1663                         return GetNumberOfTypeArguments (t) > 0;
1664                 }
1665
1666                 public static int GetNumberOfTypeArguments (Type t)
1667                 {
1668                         DeclSpace tc = LookupDeclSpace (t);
1669                         if (tc != null)
1670                                 return tc.IsGeneric ? tc.CountTypeParameters : 0;
1671                         else
1672                                 return t.HasGenericArguments ? t.GetGenericArguments ().Length : 0;
1673                 }
1674
1675                 public static Type[] GetTypeArguments (Type t)
1676                 {
1677                         DeclSpace tc = LookupDeclSpace (t);
1678                         if (tc != null) {
1679                                 if (!tc.IsGeneric)
1680                                         return Type.EmptyTypes;
1681
1682                                 TypeParameter[] tparam = tc.TypeParameters;
1683                                 Type[] ret = new Type [tparam.Length];
1684                                 for (int i = 0; i < tparam.Length; i++) {
1685                                         ret [i] = tparam [i].Type;
1686                                         if (ret [i] == null)
1687                                                 throw new InternalErrorException ();
1688                                 }
1689
1690                                 return ret;
1691                         } else
1692                                 return t.GetGenericArguments ();
1693                 }
1694
1695                 //
1696                 // Whether `array' is an array of T and `enumerator' is `IEnumerable<T>'.
1697                 // For instance "string[]" -> "IEnumerable<string>".
1698                 //
1699                 public static bool IsIEnumerable (Type array, Type enumerator)
1700                 {
1701                         if (!array.IsArray || !enumerator.IsGenericInstance)
1702                                 return false;
1703
1704                         if (enumerator.GetGenericTypeDefinition () != generic_ienumerable_type)
1705                                 return false;
1706
1707                         Type[] args = GetTypeArguments (enumerator);
1708                         return args [0] == GetElementType (array);
1709                 }
1710
1711                 public static bool IsEqual (Type a, Type b)
1712                 {
1713                         if (a.Equals (b))
1714                                 return true;
1715
1716                         if ((a is TypeBuilder) && a.IsGenericTypeDefinition && b.IsGenericInstance) {
1717                                 //
1718                                 // `a' is a generic type definition's TypeBuilder and `b' is a
1719                                 // generic instance of the same type.
1720                                 //
1721                                 // Example:
1722                                 //
1723                                 // class Stack<T>
1724                                 // {
1725                                 //     void Test (Stack<T> stack) { }
1726                                 // }
1727                                 //
1728                                 // The first argument of `Test' will be the generic instance
1729                                 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1730                                 //
1731                                 //
1732                                 // We hit this via Closure.Filter() for gen-82.cs.
1733                                 //
1734                                 if (a != b.GetGenericTypeDefinition ())
1735                                         return false;
1736
1737                                 Type[] aparams = a.GetGenericArguments ();
1738                                 Type[] bparams = b.GetGenericArguments ();
1739
1740                                 if (aparams.Length != bparams.Length)
1741                                         return false;
1742
1743                                 for (int i = 0; i < aparams.Length; i++)
1744                                         if (!IsEqual (aparams [i], bparams [i]))
1745                                                 return false;
1746
1747                                 return true;
1748                         }
1749
1750                         if ((b is TypeBuilder) && b.IsGenericTypeDefinition && a.IsGenericInstance)
1751                                 return IsEqual (b, a);
1752
1753                         if (a.IsGenericParameter && b.IsGenericParameter) {
1754                                 if ((a.DeclaringMethod == null) || (b.DeclaringMethod == null))
1755                                         return false;
1756                                 return a.GenericParameterPosition == b.GenericParameterPosition;
1757                         }
1758
1759                         if (a.IsArray && b.IsArray) {
1760                                 if (a.GetArrayRank () != b.GetArrayRank ())
1761                                         return false;
1762                                 return IsEqual (a.GetElementType (), b.GetElementType ());
1763                         }
1764
1765                         if (a.IsGenericInstance && b.IsGenericInstance) {
1766                                 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1767                                         return false;
1768
1769                                 Type[] aargs = a.GetGenericArguments ();
1770                                 Type[] bargs = b.GetGenericArguments ();
1771
1772                                 if (aargs.Length != bargs.Length)
1773                                         return false;
1774
1775                                 for (int i = 0; i < aargs.Length; i++) {
1776                                         if (!IsEqual (aargs [i], bargs [i]))
1777                                                 return false;
1778                                 }
1779
1780                                 return true;
1781                         }
1782
1783                         return false;
1784                 }
1785
1786                 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_infered, Type[] method_infered)
1787                 {
1788                         if (a.IsGenericParameter) {
1789                                 //
1790                                 // If a is an array of a's type, they may never
1791                                 // become equal.
1792                                 //
1793                                 while (b.IsArray) {
1794                                         b = b.GetElementType ();
1795                                         if (a.Equals (b))
1796                                                 return false;
1797                                 }
1798
1799                                 //
1800                                 // If b is a generic parameter or an actual type,
1801                                 // they may become equal:
1802                                 //
1803                                 //    class X<T,U> : I<T>, I<U>
1804                                 //    class X<T> : I<T>, I<float>
1805                                 // 
1806                                 if (b.IsGenericParameter || !b.IsGenericInstance) {
1807                                         int pos = a.GenericParameterPosition;
1808                                         Type[] args = a.DeclaringMethod != null ? method_infered : class_infered;
1809                                         if (args [pos] == null) {
1810                                                 args [pos] = b;
1811                                                 return true;
1812                                         }
1813
1814                                         return args [pos] == a;
1815                                 }
1816
1817                                 //
1818                                 // We're now comparing a type parameter with a
1819                                 // generic instance.  They may become equal unless
1820                                 // the type parameter appears anywhere in the
1821                                 // generic instance:
1822                                 //
1823                                 //    class X<T,U> : I<T>, I<X<U>>
1824                                 //        -> error because you could instanciate it as
1825                                 //           X<X<int>,int>
1826                                 //
1827                                 //    class X<T> : I<T>, I<X<T>> -> ok
1828                                 //
1829
1830                                 Type[] bargs = GetTypeArguments (b);
1831                                 for (int i = 0; i < bargs.Length; i++) {
1832                                         if (a.Equals (bargs [i]))
1833                                                 return false;
1834                                 }
1835
1836                                 return true;
1837                         }
1838
1839                         if (b.IsGenericParameter)
1840                                 return MayBecomeEqualGenericTypes (b, a, class_infered, method_infered);
1841
1842                         //
1843                         // At this point, neither a nor b are a type parameter.
1844                         //
1845                         // If one of them is a generic instance, let
1846                         // MayBecomeEqualGenericInstances() compare them (if the
1847                         // other one is not a generic instance, they can never
1848                         // become equal).
1849                         //
1850
1851                         if (a.IsGenericInstance || b.IsGenericInstance)
1852                                 return MayBecomeEqualGenericInstances (a, b, class_infered, method_infered);
1853
1854                         //
1855                         // If both of them are arrays.
1856                         //
1857
1858                         if (a.IsArray && b.IsArray) {
1859                                 if (a.GetArrayRank () != b.GetArrayRank ())
1860                                         return false;
1861                         
1862                                 a = a.GetElementType ();
1863                                 b = b.GetElementType ();
1864
1865                                 return MayBecomeEqualGenericTypes (a, b, class_infered, method_infered);
1866                         }
1867
1868                         //
1869                         // Ok, two ordinary types.
1870                         //
1871
1872                         return a.Equals (b);
1873                 }
1874
1875                 //
1876                 // Checks whether two generic instances may become equal for some
1877                 // particular instantiation (26.3.1).
1878                 //
1879                 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1880                                                                    Type[] class_infered, Type[] method_infered)
1881                 {
1882                         if (!a.IsGenericInstance || !b.IsGenericInstance)
1883                                 return false;
1884                         if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1885                                 return false;
1886
1887                         return MayBecomeEqualGenericInstances (
1888                                 GetTypeArguments (a), GetTypeArguments (b), class_infered, method_infered);
1889                 }
1890
1891                 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1892                                                                    Type[] class_infered, Type[] method_infered)
1893                 {
1894                         if (aargs.Length != bargs.Length)
1895                                 return false;
1896
1897                         for (int i = 0; i < aargs.Length; i++) {
1898                                 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_infered, method_infered))
1899                                         return false;
1900                         }
1901
1902                         return true;
1903                 }
1904
1905                 public static bool IsEqualGenericInstance (Type type, Type parent)
1906                 {
1907                         int tcount = GetNumberOfTypeArguments (type);
1908                         int pcount = GetNumberOfTypeArguments (parent);
1909
1910                         if (type.IsGenericInstance)
1911                                 type = type.GetGenericTypeDefinition ();
1912                         if (parent.IsGenericInstance)
1913                                 parent = parent.GetGenericTypeDefinition ();
1914
1915                         if (tcount != pcount)
1916                                 return false;
1917
1918                         return type.Equals (parent);
1919                 }
1920
1921                 static public bool IsGenericMethod (MethodBase mb)
1922                 {
1923                         if (mb.DeclaringType is TypeBuilder) {
1924                                 IMethodData method = (IMethodData) builder_to_method [mb];
1925                                 if (method == null)
1926                                         return false;
1927
1928                                 return method.GenericMethod != null;
1929                         }
1930
1931                         return mb.IsGenericMethodDefinition;
1932                 }
1933
1934                 //
1935                 // Type inference.
1936                 //
1937
1938                 static bool InferType (Type pt, Type at, Type[] infered)
1939                 {
1940                         if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
1941                                 int pos = pt.GenericParameterPosition;
1942
1943                                 if (infered [pos] == null) {
1944                                         Type check = at;
1945                                         while (check.IsArray)
1946                                                 check = check.GetElementType ();
1947
1948                                         if (pt == check)
1949                                                 return false;
1950
1951                                         infered [pos] = at;
1952                                         return true;
1953                                 }
1954
1955                                 if (infered [pos] != at)
1956                                         return false;
1957
1958                                 return true;
1959                         }
1960
1961                         if (!pt.ContainsGenericParameters) {
1962                                 if (at.ContainsGenericParameters)
1963                                         return InferType (at, pt, infered);
1964                                 else
1965                                         return true;
1966                         }
1967
1968                         if (at.IsArray) {
1969                                 if (!pt.IsArray ||
1970                                     (at.GetArrayRank () != pt.GetArrayRank ()))
1971                                         return false;
1972
1973                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1974                         }
1975
1976                         if (pt.IsArray) {
1977                                 if (!at.IsArray ||
1978                                     (pt.GetArrayRank () != at.GetArrayRank ()))
1979                                         return false;
1980
1981                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1982                         }
1983
1984                         if (pt.IsByRef && at.IsByRef)
1985                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1986                         ArrayList list = new ArrayList ();
1987                         if (at.IsGenericInstance)
1988                                 list.Add (at);
1989                         else {
1990                                 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
1991                                         list.Add (bt);
1992
1993                                 list.AddRange (TypeManager.GetInterfaces (at));
1994                         }
1995
1996                         bool found_one = false;
1997
1998                         foreach (Type type in list) {
1999                                 if (!type.IsGenericInstance)
2000                                         continue;
2001
2002                                 Type[] infered_types = new Type [infered.Length];
2003
2004                                 if (!InferGenericInstance (pt, type, infered_types))
2005                                         continue;
2006
2007                                 for (int i = 0; i < infered_types.Length; i++) {
2008                                         if (infered [i] == null) {
2009                                                 infered [i] = infered_types [i];
2010                                                 continue;
2011                                         }
2012
2013                                         if (infered [i] != infered_types [i])
2014                                                 return false;
2015                                 }
2016
2017                                 found_one = true;
2018                         }
2019
2020                         return found_one;
2021                 }
2022
2023                 static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
2024                 {
2025                         Type[] at_args = at.GetGenericArguments ();
2026                         Type[] pt_args = pt.GetGenericArguments ();
2027
2028                         if (at_args.Length != pt_args.Length)
2029                                 return false;
2030
2031                         for (int i = 0; i < at_args.Length; i++) {
2032                                 if (!InferType (pt_args [i], at_args [i], infered_types))
2033                                         return false;
2034                         }
2035
2036                         for (int i = 0; i < infered_types.Length; i++) {
2037                                 if (infered_types [i] == null)
2038                                         return false;
2039                         }
2040
2041                         return true;
2042                 }
2043
2044                 public static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
2045                                                              ref MethodBase method)
2046                 {
2047                         if ((arguments == null) || !TypeManager.IsGenericMethod (method))
2048                                 return true;
2049
2050                         int arg_count;
2051                         
2052                         if (arguments == null)
2053                                 arg_count = 0;
2054                         else
2055                                 arg_count = arguments.Count;
2056                         
2057                         ParameterData pd = TypeManager.GetParameterData (method);
2058
2059                         int pd_count = pd.Count;
2060
2061                         if (pd_count == 0)
2062                                 return false;
2063                         
2064                         if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2065                                 return false;
2066                         
2067                         if (pd_count - 1 > arg_count)
2068                                 return false;
2069                         
2070                         if (pd_count == 1 && arg_count == 0)
2071                                 return true;
2072
2073                         Type[] method_args = method.GetGenericArguments ();
2074                         Type[] infered_types = new Type [method_args.Length];
2075
2076                         //
2077                         // If we have come this far, the case which
2078                         // remains is when the number of parameters is
2079                         // less than or equal to the argument count.
2080                         //
2081                         for (int i = 0; i < pd_count - 1; ++i) {
2082                                 Argument a = (Argument) arguments [i];
2083
2084                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2085                                         continue;
2086
2087                                 Type pt = pd.ParameterType (i);
2088                                 Type at = a.Type;
2089
2090                                 if (!InferType (pt, at, infered_types))
2091                                         return false;
2092                         }
2093
2094                         Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
2095
2096                         for (int i = pd_count - 1; i < arg_count; i++) {
2097                                 Argument a = (Argument) arguments [i];
2098
2099                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2100                                         continue;
2101
2102                                 if (!InferType (element_type, a.Type, infered_types))
2103                                         return false;
2104                         }
2105
2106                         for (int i = 0; i < infered_types.Length; i++)
2107                                 if (infered_types [i] == null)
2108                                         return false;
2109
2110                         method = method.BindGenericParameters (infered_types);
2111                         return true;
2112                 }
2113
2114                 public static bool InferTypeArguments (Type[] param_types, Type[] arg_types, Type[] infered_types)
2115                 {
2116                         if (infered_types == null)
2117                                 return false;
2118
2119                         for (int i = 0; i < arg_types.Length; i++) {
2120                                 if (arg_types [i] == null)
2121                                         continue;
2122
2123                                 if (!InferType (param_types [i], arg_types [i], infered_types))
2124                                         return false;
2125                         }
2126
2127                         for (int i = 0; i < infered_types.Length; i++)
2128                                 if (infered_types [i] == null)
2129                                         return false;
2130
2131                         return true;
2132                 }
2133
2134                 public static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
2135                                                        ref MethodBase method)
2136                 {
2137                         if (!TypeManager.IsGenericMethod (method))
2138                                 return true;
2139
2140                         int arg_count;
2141                         if (arguments != null)
2142                                 arg_count = arguments.Count;
2143                         else
2144                                 arg_count = 0;
2145
2146                         ParameterData pd = TypeManager.GetParameterData (method);
2147                         if (arg_count != pd.Count)
2148                                 return false;
2149
2150                         Type[] method_args = method.GetGenericArguments ();
2151
2152                         bool is_open = false;
2153                         for (int i = 0; i < method_args.Length; i++) {
2154                                 if (method_args [i].IsGenericParameter) {
2155                                         is_open = true;
2156                                         break;
2157                                 }
2158                         }
2159                         if (!is_open)
2160                                 return true;
2161
2162                         Type[] infered_types = new Type [method_args.Length];
2163
2164                         Type[] param_types = new Type [pd.Count];
2165                         Type[] arg_types = new Type [pd.Count];
2166
2167                         for (int i = 0; i < arg_count; i++) {
2168                                 param_types [i] = pd.ParameterType (i);
2169
2170                                 Argument a = (Argument) arguments [i];
2171                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2172                                         continue;
2173
2174                                 arg_types [i] = a.Type;
2175                         }
2176
2177                         if (!InferTypeArguments (param_types, arg_types, infered_types))
2178                                 return false;
2179
2180                         method = method.BindGenericParameters (infered_types);
2181                         return true;
2182                 }
2183
2184                 public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
2185                                                        ref MethodBase method)
2186                 {
2187                         if (!TypeManager.IsGenericMethod (method))
2188                                 return true;
2189
2190                         ParameterData pd = TypeManager.GetParameterData (method);
2191                         if (apd.Count != pd.Count)
2192                                 return false;
2193
2194                         Type[] method_args = method.GetGenericArguments ();
2195                         Type[] infered_types = new Type [method_args.Length];
2196
2197                         Type[] param_types = new Type [pd.Count];
2198                         Type[] arg_types = new Type [pd.Count];
2199
2200                         for (int i = 0; i < apd.Count; i++) {
2201                                 param_types [i] = pd.ParameterType (i);
2202                                 arg_types [i] = apd.ParameterType (i);
2203                         }
2204
2205                         if (!InferTypeArguments (param_types, arg_types, infered_types))
2206                                 return false;
2207
2208                         method = method.BindGenericParameters (infered_types);
2209                         return true;
2210                 }
2211
2212                 public static bool IsNullableType (Type t)
2213                 {
2214                         if (!t.IsGenericInstance)
2215                                 return false;
2216
2217                         Type gt = t.GetGenericTypeDefinition ();
2218                         return gt == generic_nullable_type;
2219                 }
2220         }
2221
2222         public abstract class Nullable
2223         {
2224                 protected sealed class NullableInfo
2225                 {
2226                         public readonly Type Type;
2227                         public readonly Type UnderlyingType;
2228                         public readonly MethodInfo HasValue;
2229                         public readonly MethodInfo Value;
2230                         public readonly ConstructorInfo Constructor;
2231
2232                         public NullableInfo (Type type)
2233                         {
2234                                 Type = type;
2235                                 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
2236
2237                                 PropertyInfo has_value_pi = type.GetProperty ("HasValue");
2238                                 PropertyInfo value_pi = type.GetProperty ("Value");
2239
2240                                 HasValue = has_value_pi.GetGetMethod (false);
2241                                 Value = value_pi.GetGetMethod (false);
2242                                 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
2243                         }
2244                 }
2245
2246                 protected class Unwrap : Expression, IMemoryLocation, IAssignMethod
2247                 {
2248                         Expression expr;
2249                         NullableInfo info;
2250
2251                         LocalTemporary temp;
2252                         bool has_temp;
2253
2254                         public Unwrap (Expression expr, Location loc)
2255                         {
2256                                 this.expr = expr;
2257                                 this.loc = loc;
2258                         }
2259
2260                         public override Expression DoResolve (EmitContext ec)
2261                         {
2262                                 expr = expr.Resolve (ec);
2263                                 if (expr == null)
2264                                         return null;
2265
2266                                 if (!(expr is IMemoryLocation))
2267                                         temp = new LocalTemporary (ec, expr.Type);
2268
2269                                 info = new NullableInfo (expr.Type);
2270                                 type = info.UnderlyingType;
2271                                 eclass = expr.eclass;
2272                                 return this;
2273                         }
2274
2275                         public override void Emit (EmitContext ec)
2276                         {
2277                                 AddressOf (ec, AddressOp.LoadStore);
2278                                 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
2279                         }
2280
2281                         public void EmitCheck (EmitContext ec)
2282                         {
2283                                 AddressOf (ec, AddressOp.LoadStore);
2284                                 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
2285                         }
2286
2287                         void create_temp (EmitContext ec)
2288                         {
2289                                 if ((temp != null) && !has_temp) {
2290                                         expr.Emit (ec);
2291                                         temp.Store (ec);
2292                                         has_temp = true;
2293                                 }
2294                         }
2295
2296                         public void AddressOf (EmitContext ec, AddressOp mode)
2297                         {
2298                                 create_temp (ec);
2299                                 if (temp != null)
2300                                         temp.AddressOf (ec, AddressOp.LoadStore);
2301                                 else
2302                                         ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
2303                         }
2304
2305                         public void Emit (EmitContext ec, bool leave_copy)
2306                         {
2307                                 create_temp (ec);
2308                                 if (leave_copy) {
2309                                         if (temp != null)
2310                                                 temp.Emit (ec);
2311                                         else
2312                                                 expr.Emit (ec);
2313                                 }
2314
2315                                 Emit (ec);
2316                         }
2317
2318                         public void EmitAssign (EmitContext ec, Expression source,
2319                                                 bool leave_copy, bool prepare_for_load)
2320                         {
2321                                 source.Emit (ec);
2322                                 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2323
2324                                 if (leave_copy)
2325                                         ec.ig.Emit (OpCodes.Dup);
2326
2327                                 Expression empty = new EmptyExpression (expr.Type);
2328                                 ((IAssignMethod) expr).EmitAssign (ec, empty, false, prepare_for_load);
2329                         }
2330                 }
2331
2332                 protected class Wrap : Expression
2333                 {
2334                         Expression expr;
2335                         NullableInfo info;
2336
2337                         public Wrap (Expression expr, Location loc)
2338                         {
2339                                 this.expr = expr;
2340                                 this.loc = loc;
2341                         }
2342
2343                         public override Expression DoResolve (EmitContext ec)
2344                         {
2345                                 expr = expr.Resolve (ec);
2346                                 if (expr == null)
2347                                         return null;
2348
2349                                 TypeExpr target_type = new NullableType (expr.Type, loc);
2350                                 target_type = target_type.ResolveAsTypeTerminal (ec);
2351                                 if (target_type == null)
2352                                         return null;
2353
2354                                 type = target_type.Type;
2355                                 info = new NullableInfo (type);
2356                                 eclass = ExprClass.Value;
2357                                 return this;
2358                         }
2359
2360                         public override void Emit (EmitContext ec)
2361                         {
2362                                 expr.Emit (ec);
2363                                 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2364                         }
2365                 }
2366
2367                 public class NullableLiteral : Expression, IMemoryLocation {
2368                         public NullableLiteral (Type target_type, Location loc)
2369                         {
2370                                 this.type = target_type;
2371                                 this.loc = loc;
2372
2373                                 eclass = ExprClass.Value;
2374                         }
2375                 
2376                         public override Expression DoResolve (EmitContext ec)
2377                         {
2378                                 return this;
2379                         }
2380
2381                         public override void Emit (EmitContext ec)
2382                         {
2383                                 LocalTemporary value_target = new LocalTemporary (ec, type);
2384
2385                                 value_target.AddressOf (ec, AddressOp.Store);
2386                                 ec.ig.Emit (OpCodes.Initobj, type);
2387                                 value_target.Emit (ec);
2388                         }
2389
2390                         public void AddressOf (EmitContext ec, AddressOp Mode)
2391                         {
2392                                 LocalTemporary value_target = new LocalTemporary (ec, type);
2393                                         
2394                                 value_target.AddressOf (ec, AddressOp.Store);
2395                                 ec.ig.Emit (OpCodes.Initobj, type);
2396                                 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
2397                         }
2398                 }
2399
2400                 public abstract class Lifted : Expression, IMemoryLocation
2401                 {
2402                         Expression expr, underlying, wrap, null_value;
2403                         Unwrap unwrap;
2404
2405                         protected Lifted (Expression expr, Location loc)
2406                         {
2407                                 this.expr = expr;
2408                                 this.loc = loc;
2409                         }
2410
2411                         public override Expression DoResolve (EmitContext ec)
2412                         {
2413                                 expr = expr.Resolve (ec);
2414                                 if (expr == null)
2415                                         return null;
2416
2417                                 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2418                                 if (unwrap == null)
2419                                         return null;
2420
2421                                 underlying = ResolveUnderlying (unwrap, ec);
2422                                 if (underlying == null)
2423                                         return null;
2424
2425                                 wrap = new Wrap (underlying, loc).Resolve (ec);
2426                                 if (wrap == null)
2427                                         return null;
2428
2429                                 null_value = new NullableLiteral (wrap.Type, loc).Resolve (ec);
2430                                 if (null_value == null)
2431                                         return null;
2432
2433                                 type = wrap.Type;
2434                                 eclass = ExprClass.Value;
2435                                 return this;
2436                         }
2437
2438                         protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
2439
2440                         public override void Emit (EmitContext ec)
2441                         {
2442                                 ILGenerator ig = ec.ig;
2443                                 Label is_null_label = ig.DefineLabel ();
2444                                 Label end_label = ig.DefineLabel ();
2445
2446                                 unwrap.EmitCheck (ec);
2447                                 ig.Emit (OpCodes.Brfalse, is_null_label);
2448
2449                                 wrap.Emit (ec);
2450                                 ig.Emit (OpCodes.Br, end_label);
2451
2452                                 ig.MarkLabel (is_null_label);
2453                                 null_value.Emit (ec);
2454
2455                                 ig.MarkLabel (end_label);
2456                         }
2457
2458                         public void AddressOf (EmitContext ec, AddressOp mode)
2459                         {
2460                                 unwrap.AddressOf (ec, mode);
2461                         }
2462                 }
2463
2464                 public class LiftedConversion : Lifted
2465                 {
2466                         public readonly bool IsUser;
2467                         public readonly bool IsExplicit;
2468                         public readonly Type TargetType;
2469
2470                         public LiftedConversion (Expression expr, Type target_type, bool is_user,
2471                                                  bool is_explicit, Location loc)
2472                                 : base (expr, loc)
2473                         {
2474                                 this.IsUser = is_user;
2475                                 this.IsExplicit = is_explicit;
2476                                 this.TargetType = target_type;
2477                         }
2478
2479                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2480                         {
2481                                 Type type = TypeManager.GetTypeArguments (TargetType) [0];
2482
2483                                 if (IsUser) {
2484                                         return Convert.UserDefinedConversion (ec, unwrap, type, loc, IsExplicit);
2485                                 } else {
2486                                         if (IsExplicit)
2487                                                 return Convert.ExplicitConversion (ec, unwrap, type, loc);
2488                                         else
2489                                                 return Convert.ImplicitConversion (ec, unwrap, type, loc);
2490                                 }
2491                         }
2492                 }
2493
2494                 public class LiftedUnaryOperator : Lifted
2495                 {
2496                         public readonly Unary.Operator Oper;
2497
2498                         public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
2499                                 : base (expr, loc)
2500                         {
2501                                 this.Oper = op;
2502                         }
2503
2504                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2505                         {
2506                                 return new Unary (Oper, unwrap, loc);
2507                         }
2508                 }
2509
2510                 public class LiftedConditional : Lifted
2511                 {
2512                         Expression expr, true_expr, false_expr;
2513                         Unwrap unwrap;
2514
2515                         public LiftedConditional (Expression expr, Expression true_expr, Expression false_expr,
2516                                                   Location loc)
2517                                 : base (expr, loc)
2518                         {
2519                                 this.true_expr = true_expr;
2520                                 this.false_expr = false_expr;
2521                         }
2522
2523                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2524                         {
2525                                 return new Conditional (unwrap, true_expr, false_expr, loc);
2526                         }
2527                 }
2528
2529                 public class LiftedBinaryOperator : Expression
2530                 {
2531                         public readonly Binary.Operator Oper;
2532
2533                         Expression left, right, underlying, null_value, bool_wrap;
2534                         Unwrap left_unwrap, right_unwrap;
2535                         bool is_equality, is_comparision, is_boolean;
2536
2537                         public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
2538                                                      Location loc)
2539                         {
2540                                 this.Oper = op;
2541                                 this.left = left;
2542                                 this.right = right;
2543                                 this.loc = loc;
2544                         }
2545
2546                         public override Expression DoResolve (EmitContext ec)
2547                         {
2548                                 if (TypeManager.IsNullableType (left.Type)) {
2549                                         left_unwrap = new Unwrap (left, loc);
2550                                         left = left_unwrap.Resolve (ec);
2551                                         if (left == null)
2552                                                 return null;
2553                                 }
2554
2555                                 if (TypeManager.IsNullableType (right.Type)) {
2556                                         right_unwrap = new Unwrap (right, loc);
2557                                         right = right_unwrap.Resolve (ec);
2558                                         if (right == null)
2559                                                 return null;
2560                                 }
2561
2562                                 if (((Oper == Binary.Operator.BitwiseAnd) || (Oper == Binary.Operator.BitwiseOr) ||
2563                                      (Oper == Binary.Operator.LogicalAnd) || (Oper == Binary.Operator.LogicalOr)) &&
2564                                     ((left.Type == TypeManager.bool_type) && (right.Type == TypeManager.bool_type))) {
2565                                         Expression empty = new EmptyExpression (TypeManager.bool_type);
2566                                         bool_wrap = new Wrap (empty, loc).Resolve (ec);
2567                                         null_value = new NullableLiteral (bool_wrap.Type, loc).Resolve (ec);
2568
2569                                         type = bool_wrap.Type;
2570                                         is_boolean = true;
2571                                 } else if ((Oper == Binary.Operator.Equality) || (Oper == Binary.Operator.Inequality)) {
2572                                         if (!(left is NullLiteral) && !(right is NullLiteral)) {
2573                                                 underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2574                                                 if (underlying == null)
2575                                                         return null;
2576                                         }
2577
2578                                         type = TypeManager.bool_type;
2579                                         is_equality = true;
2580                                 } else if ((Oper == Binary.Operator.LessThan) ||
2581                                            (Oper == Binary.Operator.GreaterThan) ||
2582                                            (Oper == Binary.Operator.LessThanOrEqual) ||
2583                                            (Oper == Binary.Operator.GreaterThanOrEqual)) {
2584                                         underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2585                                         if (underlying == null)
2586                                                 return null;
2587
2588                                         type = TypeManager.bool_type;
2589                                         is_comparision = true;
2590                                 } else {
2591                                         underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2592                                         if (underlying == null)
2593                                                 return null;
2594
2595                                         underlying = new Wrap (underlying, loc).Resolve (ec);
2596                                         if (underlying == null)
2597                                                 return null;
2598
2599                                         type = underlying.Type;
2600                                         null_value = new NullableLiteral (type, loc).Resolve (ec);
2601                                 }
2602
2603                                 eclass = ExprClass.Value;
2604                                 return this;
2605                         }
2606
2607                         void EmitBoolean (EmitContext ec)
2608                         {
2609                                 ILGenerator ig = ec.ig;
2610
2611                                 Label left_is_null_label = ig.DefineLabel ();
2612                                 Label right_is_null_label = ig.DefineLabel ();
2613                                 Label is_null_label = ig.DefineLabel ();
2614                                 Label wrap_label = ig.DefineLabel ();
2615                                 Label end_label = ig.DefineLabel ();
2616
2617                                 if (left_unwrap != null) {
2618                                         left_unwrap.EmitCheck (ec);
2619                                         ig.Emit (OpCodes.Brfalse, left_is_null_label);
2620                                 }
2621
2622                                 left.Emit (ec);
2623                                 ig.Emit (OpCodes.Dup);
2624                                 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
2625                                         ig.Emit (OpCodes.Brtrue, wrap_label);
2626                                 else
2627                                         ig.Emit (OpCodes.Brfalse, wrap_label);
2628
2629                                 if (right_unwrap != null) {
2630                                         right_unwrap.EmitCheck (ec);
2631                                         ig.Emit (OpCodes.Brfalse, right_is_null_label);
2632                                 }
2633
2634                                 if ((Oper == Binary.Operator.LogicalAnd) || (Oper == Binary.Operator.LogicalOr))
2635                                         ig.Emit (OpCodes.Pop);
2636
2637                                 right.Emit (ec);
2638                                 if (Oper == Binary.Operator.BitwiseOr)
2639                                         ig.Emit (OpCodes.Or);
2640                                 else if (Oper == Binary.Operator.BitwiseAnd)
2641                                         ig.Emit (OpCodes.And);
2642                                 ig.Emit (OpCodes.Br, wrap_label);
2643
2644                                 ig.MarkLabel (left_is_null_label);
2645                                 if (right_unwrap != null) {
2646                                         right_unwrap.EmitCheck (ec);
2647                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2648                                 }
2649
2650                                 right.Emit (ec);
2651                                 ig.Emit (OpCodes.Dup);
2652                                 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
2653                                         ig.Emit (OpCodes.Brtrue, wrap_label);
2654                                 else
2655                                         ig.Emit (OpCodes.Brfalse, wrap_label);
2656
2657                                 ig.MarkLabel (right_is_null_label);
2658                                 ig.Emit (OpCodes.Pop);
2659                                 ig.MarkLabel (is_null_label);
2660                                 null_value.Emit (ec);
2661                                 ig.Emit (OpCodes.Br, end_label);
2662
2663                                 ig.MarkLabel (wrap_label);
2664                                 ig.Emit (OpCodes.Nop);
2665                                 bool_wrap.Emit (ec);
2666                                 ig.Emit (OpCodes.Nop);
2667
2668                                 ig.MarkLabel (end_label);
2669                         }
2670
2671                         void EmitEquality (EmitContext ec)
2672                         {
2673                                 ILGenerator ig = ec.ig;
2674
2675                                 Label left_not_null_label = ig.DefineLabel ();
2676                                 Label false_label = ig.DefineLabel ();
2677                                 Label true_label = ig.DefineLabel ();
2678                                 Label end_label = ig.DefineLabel ();
2679
2680                                 if (left_unwrap != null) {
2681                                         left_unwrap.EmitCheck (ec);
2682                                         if (right is NullLiteral) {
2683                                                 if (Oper == Binary.Operator.Equality)
2684                                                         ig.Emit (OpCodes.Brfalse, true_label);
2685                                                 else
2686                                                         ig.Emit (OpCodes.Brfalse, false_label);
2687                                         } else if (right_unwrap != null) {
2688                                                 ig.Emit (OpCodes.Dup);
2689                                                 ig.Emit (OpCodes.Brtrue, left_not_null_label);
2690                                                 right_unwrap.EmitCheck (ec);
2691                                                 ig.Emit (OpCodes.Ceq);
2692                                                 if (Oper == Binary.Operator.Inequality) {
2693                                                         ig.Emit (OpCodes.Ldc_I4_0);
2694                                                         ig.Emit (OpCodes.Ceq);
2695                                                 }
2696                                                 ig.Emit (OpCodes.Br, end_label);
2697
2698                                                 ig.MarkLabel (left_not_null_label);
2699                                                 ig.Emit (OpCodes.Pop);
2700                                         } else {
2701                                                 if (Oper == Binary.Operator.Equality)
2702                                                         ig.Emit (OpCodes.Brfalse, false_label);
2703                                                 else
2704                                                         ig.Emit (OpCodes.Brfalse, true_label);
2705                                         }
2706                                 }
2707
2708                                 if (right_unwrap != null) {
2709                                         right_unwrap.EmitCheck (ec);
2710                                         if (left is NullLiteral) {
2711                                                 if (Oper == Binary.Operator.Equality)
2712                                                         ig.Emit (OpCodes.Brfalse, true_label);
2713                                                 else
2714                                                         ig.Emit (OpCodes.Brfalse, false_label);
2715                                         } else {
2716                                                 if (Oper == Binary.Operator.Equality)
2717                                                         ig.Emit (OpCodes.Brfalse, false_label);
2718                                                 else
2719                                                         ig.Emit (OpCodes.Brfalse, true_label);
2720                                         }
2721                                 }
2722
2723                                 bool left_is_null = left is NullLiteral;
2724                                 bool right_is_null = right is NullLiteral;
2725                                 if (left_is_null || right_is_null) {
2726                                         if (((Oper == Binary.Operator.Equality) && (left_is_null == right_is_null)) ||
2727                                             ((Oper == Binary.Operator.Inequality) && (left_is_null != right_is_null)))
2728                                                 ig.Emit (OpCodes.Br, true_label);
2729                                         else
2730                                                 ig.Emit (OpCodes.Br, false_label);
2731                                 } else {
2732                                         underlying.Emit (ec);
2733                                         ig.Emit (OpCodes.Br, end_label);
2734                                 }
2735
2736                                 ig.MarkLabel (false_label);
2737                                 ig.Emit (OpCodes.Ldc_I4_0);
2738                                 ig.Emit (OpCodes.Br, end_label);
2739
2740                                 ig.MarkLabel (true_label);
2741                                 ig.Emit (OpCodes.Ldc_I4_1);
2742
2743                                 ig.MarkLabel (end_label);
2744                         }
2745
2746                         void EmitComparision (EmitContext ec)
2747                         {
2748                                 ILGenerator ig = ec.ig;
2749
2750                                 Label is_null_label = ig.DefineLabel ();
2751                                 Label end_label = ig.DefineLabel ();
2752
2753                                 if (left_unwrap != null) {
2754                                         left_unwrap.EmitCheck (ec);
2755                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2756                                 }
2757
2758                                 if (right_unwrap != null) {
2759                                         right_unwrap.EmitCheck (ec);
2760                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2761                                 }
2762
2763                                 underlying.Emit (ec);
2764                                 ig.Emit (OpCodes.Br, end_label);
2765
2766                                 ig.MarkLabel (is_null_label);
2767                                 ig.Emit (OpCodes.Ldc_I4_0);
2768
2769                                 ig.MarkLabel (end_label);
2770                         }
2771
2772                         public override void Emit (EmitContext ec)
2773                         {
2774                                 if (is_boolean) {
2775                                         EmitBoolean (ec);
2776                                         return;
2777                                 } else if (is_equality) {
2778                                         EmitEquality (ec);
2779                                         return;
2780                                 } else if (is_comparision) {
2781                                         EmitComparision (ec);
2782                                         return;
2783                                 }
2784
2785                                 ILGenerator ig = ec.ig;
2786
2787                                 Label is_null_label = ig.DefineLabel ();
2788                                 Label end_label = ig.DefineLabel ();
2789
2790                                 if (left_unwrap != null) {
2791                                         left_unwrap.EmitCheck (ec);
2792                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2793                                 }
2794
2795                                 if (right_unwrap != null) {
2796                                         right_unwrap.EmitCheck (ec);
2797                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2798                                 }
2799
2800                                 underlying.Emit (ec);
2801                                 ig.Emit (OpCodes.Br, end_label);
2802
2803                                 ig.MarkLabel (is_null_label);
2804                                 null_value.Emit (ec);
2805
2806                                 ig.MarkLabel (end_label);
2807                         }
2808                 }
2809
2810                 public class OperatorTrueOrFalse : Expression
2811                 {
2812                         public readonly bool IsTrue;
2813
2814                         Expression expr;
2815                         Unwrap unwrap;
2816
2817                         public OperatorTrueOrFalse (Expression expr, bool is_true, Location loc)
2818                         {
2819                                 this.IsTrue = is_true;
2820                                 this.expr = expr;
2821                                 this.loc = loc;
2822                         }
2823
2824                         public override Expression DoResolve (EmitContext ec)
2825                         {
2826                                 unwrap = new Unwrap (expr, loc);
2827                                 expr = unwrap.Resolve (ec);
2828                                 if (expr == null)
2829                                         return null;
2830
2831                                 if (unwrap.Type != TypeManager.bool_type)
2832                                         return null;
2833
2834                                 type = TypeManager.bool_type;
2835                                 eclass = ExprClass.Value;
2836                                 return this;
2837                         }
2838
2839                         public override void Emit (EmitContext ec)
2840                         {
2841                                 ILGenerator ig = ec.ig;
2842
2843                                 Label is_null_label = ig.DefineLabel ();
2844                                 Label end_label = ig.DefineLabel ();
2845
2846                                 unwrap.EmitCheck (ec);
2847                                 ig.Emit (OpCodes.Brfalse, is_null_label);
2848
2849                                 unwrap.Emit (ec);
2850                                 if (!IsTrue) {
2851                                         ig.Emit (OpCodes.Ldc_I4_0);
2852                                         ig.Emit (OpCodes.Ceq);
2853                                 }
2854                                 ig.Emit (OpCodes.Br, end_label);
2855
2856                                 ig.MarkLabel (is_null_label);
2857                                 ig.Emit (OpCodes.Ldc_I4_0);
2858
2859                                 ig.MarkLabel (end_label);
2860                         }
2861                 }
2862
2863                 public class NullCoalescingOperator : Expression
2864                 {
2865                         Expression left, right;
2866                         Expression expr;
2867                         Unwrap unwrap;
2868
2869                         public NullCoalescingOperator (Expression left, Expression right, Location loc)
2870                         {
2871                                 this.left = left;
2872                                 this.right = right;
2873                                 this.loc = loc;
2874
2875                                 eclass = ExprClass.Value;
2876                         }
2877
2878                         public override Expression DoResolve (EmitContext ec)
2879                         {
2880                                 if (type != null)
2881                                         return this;
2882
2883                                 left = left.Resolve (ec);
2884                                 if (left == null)
2885                                         return null;
2886
2887                                 right = right.Resolve (ec);
2888                                 if (right == null)
2889                                         return null;
2890
2891                                 Type ltype = left.Type, rtype = right.Type;
2892
2893                                 if (!TypeManager.IsNullableType (ltype) && ltype.IsValueType) {
2894                                         Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2895                                         return null;
2896                                 }
2897
2898                                 if (TypeManager.IsNullableType (ltype)) {
2899                                         NullableInfo info = new NullableInfo (ltype);
2900
2901                                         unwrap = (Unwrap) new Unwrap (left, loc).Resolve (ec);
2902                                         if (unwrap == null)
2903                                                 return null;
2904
2905                                         expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
2906                                         if (expr != null) {
2907                                                 left = unwrap;
2908                                                 type = expr.Type;
2909                                                 return this;
2910                                         }
2911                                 }
2912
2913                                 expr = Convert.ImplicitConversion (ec, right, ltype, loc);
2914                                 if (expr != null) {
2915                                         type = expr.Type;
2916                                         return this;
2917                                 }
2918
2919                                 if (unwrap != null) {
2920                                         expr = Convert.ImplicitConversion (ec, unwrap, rtype, loc);
2921                                         if (expr != null) {
2922                                                 left = expr;
2923                                                 expr = right;
2924                                                 type = expr.Type;
2925                                                 return this;
2926                                         }
2927                                 }
2928
2929                                 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2930                                 return null;
2931                         }
2932
2933                         public override void Emit (EmitContext ec)
2934                         {
2935                                 ILGenerator ig = ec.ig;
2936
2937                                 Label is_null_label = ig.DefineLabel ();
2938                                 Label end_label = ig.DefineLabel ();
2939
2940                                 if (unwrap != null) {
2941                                         unwrap.EmitCheck (ec);
2942                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2943                                 }
2944
2945                                 left.Emit (ec);
2946                                 ig.Emit (OpCodes.Br, end_label);
2947
2948                                 ig.MarkLabel (is_null_label);
2949                                 expr.Emit (ec);
2950
2951                                 ig.MarkLabel (end_label);
2952                         }
2953                 }
2954
2955                 public class LiftedUnaryMutator : ExpressionStatement
2956                 {
2957                         public readonly UnaryMutator.Mode Mode;
2958                         Expression expr, null_value;
2959                         UnaryMutator underlying;
2960                         Unwrap unwrap;
2961
2962                         public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
2963                         {
2964                                 this.expr = expr;
2965                                 this.Mode = mode;
2966                                 this.loc = loc;
2967
2968                                 eclass = ExprClass.Value;
2969                         }
2970
2971                         public override Expression DoResolve (EmitContext ec)
2972                         {
2973                                 expr = expr.Resolve (ec);
2974                                 if (expr == null)
2975                                         return null;
2976
2977                                 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2978                                 if (unwrap == null)
2979                                         return null;
2980
2981                                 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
2982                                 if (underlying == null)
2983                                         return null;
2984
2985                                 null_value = new NullableLiteral (expr.Type, loc).Resolve (ec);
2986                                 if (null_value == null)
2987                                         return null;
2988
2989                                 type = expr.Type;
2990                                 return this;
2991                         }
2992
2993                         void DoEmit (EmitContext ec, bool is_expr)
2994                         {
2995                                 ILGenerator ig = ec.ig;
2996                                 Label is_null_label = ig.DefineLabel ();
2997                                 Label end_label = ig.DefineLabel ();
2998
2999                                 unwrap.EmitCheck (ec);
3000                                 ig.Emit (OpCodes.Brfalse, is_null_label);
3001
3002                                 if (is_expr)
3003                                         underlying.Emit (ec);
3004                                 else
3005                                         underlying.EmitStatement (ec);
3006                                 ig.Emit (OpCodes.Br, end_label);
3007
3008                                 ig.MarkLabel (is_null_label);
3009                                 if (is_expr)
3010                                         null_value.Emit (ec);
3011
3012                                 ig.MarkLabel (end_label);
3013                         }
3014
3015                         public override void Emit (EmitContext ec)
3016                         {
3017                                 DoEmit (ec, true);
3018                         }
3019
3020                         public override void EmitStatement (EmitContext ec)
3021                         {
3022                                 DoEmit (ec, false);
3023                         }
3024                 }
3025         }
3026 }