**** Merged r41518 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, bool check)
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 (!check) {
668                                 if (type != null)
669                                         throw new InvalidOperationException ();
670                                 constraints = new_constraints;
671                                 return true;
672                         }
673
674                         if (type == null)
675                                 throw new InvalidOperationException ();
676
677                         if (constraints == null)
678                                 return new_constraints == null;
679                         else if (new_constraints == null)
680                                 return false;
681
682                         if (!new_constraints.Resolve (ec))
683                                 return false;
684                         if (!new_constraints.ResolveTypes (ec))
685                                 return false;
686
687                         return constraints.CheckInterfaceMethod (ec, new_constraints);
688                 }
689
690                 public override string DocCommentHeader {
691                         get {
692                                 throw new InvalidOperationException (
693                                         "Unexpected attempt to get doc comment from " + this.GetType () + ".");
694                         }
695                 }
696
697                 //
698                 // MemberContainer
699                 //
700
701                 public override bool Define ()
702                 {
703                         return true;
704                 }
705
706                 protected override void VerifyObsoleteAttribute ()
707                 { }
708
709                 public override void ApplyAttributeBuilder (Attribute a,
710                                                             CustomAttributeBuilder cb)
711                 { }
712
713                 public override AttributeTargets AttributeTargets {
714                         get {
715                                 return (AttributeTargets) 0;
716                         }
717                 }
718
719                 public override string[] ValidAttributeTargets {
720                         get {
721                                 return new string [0];
722                         }
723                 }
724
725                 //
726                 // IMemberContainer
727                 //
728
729                 string IMemberContainer.Name {
730                         get { return Name; }
731                 }
732
733                 MemberCache IMemberContainer.BaseCache {
734                         get { return null; }
735                 }
736
737                 bool IMemberContainer.IsInterface {
738                         get { return true; }
739                 }
740
741                 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
742                 {
743                         return FindMembers (mt, bf, null, null);
744                 }
745
746                 MemberCache IMemberContainer.MemberCache {
747                         get { return null; }
748                 }
749
750                 public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
751                                                MemberFilter filter, object criteria)
752                 {
753                         if (constraints == null)
754                                 return MemberList.Empty;
755
756                         ArrayList members = new ArrayList ();
757
758                         GenericConstraints gc = (GenericConstraints) constraints;
759
760                         if (gc.HasClassConstraint) {
761                                 MemberList list = TypeManager.FindMembers (
762                                         gc.ClassConstraint, mt, bf, filter, criteria);
763
764                                 members.AddRange (list);
765                         }
766
767                         foreach (Type t in gc.InterfaceConstraints) {
768                                 MemberList list = TypeManager.FindMembers (
769                                         t, mt, bf, filter, criteria);
770
771                                 members.AddRange (list);
772                         }
773
774                         return new MemberList (members);
775                 }
776
777                 public bool IsSubclassOf (Type t)
778                 {
779                         if (type.Equals (t))
780                                 return true;
781
782                         if (constraints != null)
783                                 return constraints.IsSubclassOf (t);
784
785                         return false;
786                 }
787
788                 public override string ToString ()
789                 {
790                         return "TypeParameter[" + name + "]";
791                 }
792
793                 protected class InflatedConstraints : GenericConstraints
794                 {
795                         GenericConstraints gc;
796                         Type base_type;
797                         Type class_constraint;
798                         Type[] iface_constraints;
799                         Type[] dargs;
800                         Type declaring;
801
802                         public InflatedConstraints (GenericConstraints gc, Type declaring)
803                         {
804                                 this.gc = gc;
805                                 this.declaring = declaring;
806
807                                 dargs = TypeManager.GetTypeArguments (declaring);
808
809                                 ArrayList list = new ArrayList ();
810                                 if (gc.HasClassConstraint)
811                                         list.Add (inflate (gc.ClassConstraint));
812                                 foreach (Type iface in gc.InterfaceConstraints)
813                                         list.Add (inflate (iface));
814
815                                 bool has_class_constr = false;
816                                 if (list.Count > 0) {
817                                         Type first = (Type) list [0];
818                                         has_class_constr = !first.IsInterface && !first.IsGenericParameter;
819                                 }
820
821                                 if ((list.Count > 0) && has_class_constr) {
822                                         class_constraint = (Type) list [0];
823                                         iface_constraints = new Type [list.Count - 1];
824                                         list.CopyTo (1, iface_constraints, 0, list.Count - 1);
825                                 } else {
826                                         iface_constraints = new Type [list.Count];
827                                         list.CopyTo (iface_constraints, 0);
828                                 }
829
830                                 if (HasValueTypeConstraint)
831                                         base_type = TypeManager.value_type;
832                                 else if (class_constraint != null)
833                                         base_type = class_constraint;
834                                 else
835                                         base_type = TypeManager.object_type;
836                         }
837
838                         Type inflate (Type t)
839                         {
840                                 if (t == null)
841                                         return null;
842                                 if (t.IsGenericParameter)
843                                         return dargs [t.GenericParameterPosition];
844                                 if (t.IsGenericInstance) {
845                                         t = t.GetGenericTypeDefinition ();
846                                         t = t.BindGenericParameters (dargs);
847                                 }
848
849                                 return t;
850                         }
851
852                         public override GenericParameterAttributes Attributes {
853                                 get { return gc.Attributes; }
854                         }
855
856                         public override Type ClassConstraint {
857                                 get { return class_constraint; }
858                         }
859
860                         public override Type EffectiveBaseClass {
861                                 get { return base_type; }
862                         }
863
864                         public override Type[] InterfaceConstraints {
865                                 get { return iface_constraints; }
866                         }
867                 }
868         }
869
870         //
871         // This type represents a generic type parameter reference.
872         //
873         // These expressions are born in a fully resolved state.
874         //
875         public class TypeParameterExpr : TypeExpr {
876                 TypeParameter type_parameter;
877
878                 public override string Name {
879                         get {
880                                 return type_parameter.Name;
881                         }
882                 }
883
884                 public override string FullName {
885                         get {
886                                 return type_parameter.Name;
887                         }
888                 }
889
890                 public TypeParameter TypeParameter {
891                         get {
892                                 return type_parameter;
893                         }
894                 }
895                 
896                 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
897                 {
898                         this.type_parameter = type_parameter;
899                         this.loc = loc;
900                 }
901
902                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
903                 {
904                         type = type_parameter.Type;
905
906                         return this;
907                 }
908
909                 public override bool IsInterface {
910                         get { return false; }
911                 }
912
913                 public override bool CheckAccessLevel (DeclSpace ds)
914                 {
915                         return true;
916                 }
917
918                 public void Error_CannotUseAsUnmanagedType (Location loc)
919                 {
920                         Report.Error (-203, loc, "Can not use type parameter as unamanged type");
921                 }
922         }
923
924         public class TypeArguments {
925                 public readonly Location Location;
926                 ArrayList args;
927                 Type[] atypes;
928                 int dimension;
929                 bool has_type_args;
930                 bool created;
931                 
932                 public TypeArguments (Location loc)
933                 {
934                         args = new ArrayList ();
935                         this.Location = loc;
936                 }
937
938                 public TypeArguments (int dimension, Location loc)
939                 {
940                         this.dimension = dimension;
941                         this.Location = loc;
942                 }
943
944                 public void Add (Expression type)
945                 {
946                         if (created)
947                                 throw new InvalidOperationException ();
948
949                         args.Add (type);
950                 }
951
952                 public void Add (TypeArguments new_args)
953                 {
954                         if (created)
955                                 throw new InvalidOperationException ();
956
957                         args.AddRange (new_args.args);
958                 }
959
960                 public string[] GetDeclarations ()
961                 {
962                         string[] ret = new string [args.Count];
963                         for (int i = 0; i < args.Count; i++) {
964                                 SimpleName sn = args [i] as SimpleName;
965                                 if (sn != null) {
966                                         ret [i] = sn.Name;
967                                         continue;
968                                 }
969
970                                 Report.Error (81, Location, "Type parameter declaration " +
971                                               "must be an identifier not a type");
972                                 return null;
973                         }
974                         return ret;
975                 }
976
977                 public Type[] Arguments {
978                         get {
979                                 return atypes;
980                         }
981                 }
982
983                 public bool HasTypeArguments {
984                         get {
985                                 return has_type_args;
986                         }
987                 }
988
989                 public int Count {
990                         get {
991                                 if (dimension > 0)
992                                         return dimension;
993                                 else
994                                         return args.Count;
995                         }
996                 }
997
998                 public bool IsUnbound {
999                         get {
1000                                 return dimension > 0;
1001                         }
1002                 }
1003
1004                 public override string ToString ()
1005                 {
1006                         StringBuilder s = new StringBuilder ();
1007
1008                         int count = Count;
1009                         for (int i = 0; i < count; i++){
1010                                 //
1011                                 // FIXME: Use TypeManager.CSharpname once we have the type
1012                                 //
1013                                 if (args != null)
1014                                         s.Append (args [i].ToString ());
1015                                 if (i+1 < count)
1016                                         s.Append (",");
1017                         }
1018                         return s.ToString ();
1019                 }
1020
1021                 public bool Resolve (EmitContext ec)
1022                 {
1023                         int count = args.Count;
1024                         bool ok = true;
1025
1026                         atypes = new Type [count];
1027
1028                         for (int i = 0; i < count; i++){
1029                                 TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec);
1030                                 if (te == null) {
1031                                         ok = false;
1032                                         continue;
1033                                 }
1034                                 if (te is TypeParameterExpr)
1035                                         has_type_args = true;
1036
1037                                 if (te.Type.IsPointer) {
1038                                         Report.Error (306, Location, "The type `{0}' may not be used " +
1039                                                       "as a type argument.", TypeManager.CSharpName (te.Type));
1040                                         return false;
1041                                 }
1042
1043                                 atypes [i] = te.Type;
1044                         }
1045                         return ok;
1046                 }
1047         }
1048         
1049         public class ConstructedType : TypeExpr {
1050                 string full_name;
1051                 FullNamedExpression name;
1052                 TypeArguments args;
1053                 Type[] gen_params, atypes;
1054                 Type gt;
1055                 
1056                 public ConstructedType (FullNamedExpression fname, TypeArguments args, Location l)
1057                 {
1058                         loc = l;
1059                         this.name = fname;
1060                         this.args = args;
1061
1062                         eclass = ExprClass.Type;
1063                         full_name = name + "<" + args.ToString () + ">";
1064                 }
1065
1066                 protected ConstructedType (TypeArguments args, Location l)
1067                 {
1068                         loc = l;
1069                         this.args = args;
1070
1071                         eclass = ExprClass.Type;
1072                 }
1073
1074                 protected ConstructedType (TypeParameter[] type_params, Location l)
1075                 {
1076                         loc = l;
1077
1078                         args = new TypeArguments (l);
1079                         foreach (TypeParameter type_param in type_params)
1080                                 args.Add (new TypeParameterExpr (type_param, l));
1081
1082                         eclass = ExprClass.Type;
1083                 }
1084
1085                 public ConstructedType (Type t, TypeParameter[] type_params, Location l)
1086                         : this (type_params, l)
1087                 {
1088                         gt = t.GetGenericTypeDefinition ();
1089
1090                         this.name = new TypeExpression (gt, l);
1091                         full_name = gt.FullName + "<" + args.ToString () + ">";
1092                 }
1093
1094                 public ConstructedType (Type t, TypeArguments args, Location l)
1095                         : this (args, l)
1096                 {
1097                         gt = t.GetGenericTypeDefinition ();
1098
1099                         this.name = new TypeExpression (gt, l);
1100                         full_name = gt.FullName + "<" + args.ToString () + ">";
1101                 }
1102
1103                 public TypeArguments TypeArguments {
1104                         get { return args; }
1105                 }
1106
1107                 protected string DeclarationName {
1108                         get {
1109                                 StringBuilder sb = new StringBuilder ();
1110                                 sb.Append (gt.FullName);
1111                                 sb.Append ("<");
1112                                 for (int i = 0; i < gen_params.Length; i++) {
1113                                         if (i > 0)
1114                                                 sb.Append (",");
1115                                         sb.Append (gen_params [i]);
1116                                 }
1117                                 sb.Append (">");
1118                                 return sb.ToString ();
1119                         }
1120                 }
1121
1122                 protected bool CheckConstraint (EmitContext ec, Type ptype, Expression expr,
1123                                                 Type ctype)
1124                 {
1125                         if (TypeManager.HasGenericArguments (ctype)) {
1126                                 Type[] types = TypeManager.GetTypeArguments (ctype);
1127
1128                                 TypeArguments new_args = new TypeArguments (loc);
1129
1130                                 for (int i = 0; i < types.Length; i++) {
1131                                         Type t = types [i];
1132
1133                                         if (t.IsGenericParameter) {
1134                                                 int pos = t.GenericParameterPosition;
1135                                                 t = args.Arguments [pos];
1136                                         }
1137                                         new_args.Add (new TypeExpression (t, loc));
1138                                 }
1139
1140                                 TypeExpr ct = new ConstructedType (ctype, new_args, loc);
1141                                 if (ct.ResolveAsTypeTerminal (ec) == null)
1142                                         return false;
1143                                 ctype = ct.Type;
1144                         }
1145
1146                         return Convert.ImplicitStandardConversionExists (ec, expr, ctype);
1147                 }
1148
1149                 protected bool CheckConstraints (EmitContext ec, int index)
1150                 {
1151                         Type atype = atypes [index];
1152                         Type ptype = gen_params [index];
1153
1154                         if (atype == ptype)
1155                                 return true;
1156
1157                         Expression aexpr = new EmptyExpression (atype);
1158
1159                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1160                         if (gc == null)
1161                                 return true;
1162
1163                         bool is_class, is_struct;
1164                         if (atype.IsGenericParameter) {
1165                                 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1166                                 if (agc != null) {
1167                                         is_class = agc.HasReferenceTypeConstraint;
1168                                         is_struct = agc.HasValueTypeConstraint;
1169                                 } else {
1170                                         is_class = is_struct = false;
1171                                 }
1172                         } else {
1173                                 is_class = atype.IsClass;
1174                                 is_struct = atype.IsValueType;
1175                         }
1176
1177                         //
1178                         // First, check the `class' and `struct' constraints.
1179                         //
1180                         if (gc.HasReferenceTypeConstraint && !is_class) {
1181                                 Report.Error (452, loc, "The type `{0}' must be " +
1182                                               "a reference type in order to use it " +
1183                                               "as type parameter `{1}' in the " +
1184                                               "generic type or method `{2}'.",
1185                                               atype, ptype, DeclarationName);
1186                                 return false;
1187                         } else if (gc.HasValueTypeConstraint && !is_struct) {
1188                                 Report.Error (453, loc, "The type `{0}' must be " +
1189                                               "a value type in order to use it " +
1190                                               "as type parameter `{1}' in the " +
1191                                               "generic type or method `{2}'.",
1192                                               atype, ptype, DeclarationName);
1193                                 return false;
1194                         }
1195
1196                         //
1197                         // The class constraint comes next.
1198                         //
1199                         if (gc.HasClassConstraint) {
1200                                 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint)) {
1201                                         Report.Error (309, loc, "The type `{0}' must be " +
1202                                                       "convertible to `{1}' in order to " +
1203                                                       "use it as parameter `{2}' in the " +
1204                                                       "generic type or method `{3}'",
1205                                                       atype, gc.ClassConstraint, ptype, DeclarationName);
1206                                         return false;
1207                                 }
1208                         }
1209
1210                         //
1211                         // Now, check the interface constraints.
1212                         //
1213                         foreach (Type it in gc.InterfaceConstraints) {
1214                                 Type itype;
1215                                 if (it.IsGenericParameter)
1216                                         itype = atypes [it.GenericParameterPosition];
1217                                 else
1218                                         itype = it;
1219
1220                                 if (!CheckConstraint (ec, ptype, aexpr, itype)) {
1221                                         Report.Error (309, loc, "The type `{0}' must be " +
1222                                                       "convertible to `{1}' in order to " +
1223                                                       "use it as parameter `{2}' in the " +
1224                                                       "generic type or method `{3}'",
1225                                                       atype, itype, ptype, DeclarationName);
1226                                         return false;
1227                                 }
1228                         }
1229
1230                         //
1231                         // Finally, check the constructor constraint.
1232                         //
1233
1234                         if (!gc.HasConstructorConstraint)
1235                                 return true;
1236
1237                         if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
1238                                 return true;
1239
1240                         MethodGroupExpr mg = Expression.MemberLookup (
1241                                 ec, atype, ".ctor", MemberTypes.Constructor,
1242                                 BindingFlags.Public | BindingFlags.Instance |
1243                                 BindingFlags.DeclaredOnly, loc)
1244                                 as MethodGroupExpr;
1245
1246                         if (atype.IsAbstract || (mg == null) || !mg.IsInstance) {
1247                                 Report.Error (310, loc, "The type `{0}' must have a public " +
1248                                               "parameterless constructor in order to use it " +
1249                                               "as parameter `{1}' in the generic type or " +
1250                                               "method `{2}'", atype, ptype, DeclarationName);
1251                                 return false;
1252                         }
1253
1254                         return true;
1255                 }
1256
1257                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1258                 {
1259                         if (!ResolveConstructedType (ec))
1260                                 return null;
1261
1262                         return this;
1263                 }
1264
1265                 public bool CheckConstraints (EmitContext ec)
1266                 {
1267                         for (int i = 0; i < gen_params.Length; i++) {
1268                                 if (!CheckConstraints (ec, i))
1269                                         return false;
1270                         }
1271
1272                         return true;
1273                 }
1274
1275                 public override TypeExpr ResolveAsTypeTerminal (EmitContext ec)
1276                 {
1277                         if (base.ResolveAsTypeTerminal (ec) == null)
1278                                 return null;
1279
1280                         if (!CheckConstraints (ec))
1281                                 return null;
1282
1283                         return this;
1284                 }
1285
1286                 public bool ResolveConstructedType (EmitContext ec)
1287                 {
1288                         if (type != null)
1289                                 return true;
1290                         if (gt != null)
1291                                 return DoResolveType (ec);
1292
1293                         int num_args;
1294                         Type t = name.Type;
1295
1296                         if (t == null) {
1297                                 Report.Error (246, loc, "Cannot find type `{0}'<...>", Name);
1298                                 return false;
1299                         }
1300
1301                         num_args = TypeManager.GetNumberOfTypeArguments (t);
1302                         if (num_args == 0) {
1303                                 Report.Error (308, loc,
1304                                               "The non-generic type `{0}' cannot " +
1305                                               "be used with type arguments.",
1306                                               TypeManager.CSharpName (t));
1307                                 return false;
1308                         }
1309
1310                         gt = t.GetGenericTypeDefinition ();
1311                         return DoResolveType (ec);
1312                 }
1313
1314                 bool DoResolveType (EmitContext ec)
1315                 {
1316                         //
1317                         // Resolve the arguments.
1318                         //
1319                         if (args.Resolve (ec) == false)
1320                                 return false;
1321
1322                         gen_params = gt.GetGenericArguments ();
1323                         atypes = args.Arguments;
1324
1325                         if (atypes.Length != gen_params.Length) {
1326                                 Report.Error (305, loc,
1327                                               "Using the generic type `{0}' " +
1328                                               "requires {1} type arguments",
1329                                               TypeManager.GetFullName (gt),
1330                                               gen_params.Length);
1331                                 return false;
1332                         }
1333
1334                         //
1335                         // Now bind the parameters.
1336                         //
1337                         type = gt.BindGenericParameters (atypes);
1338                         return true;
1339                 }
1340
1341                 public Expression GetSimpleName (EmitContext ec)
1342                 {
1343                         return this;
1344                 }
1345
1346                 public override bool CheckAccessLevel (DeclSpace ds)
1347                 {
1348                         return ds.CheckAccessLevel (gt);
1349                 }
1350
1351                 public override bool AsAccessible (DeclSpace ds, int flags)
1352                 {
1353                         return ds.AsAccessible (gt, flags);
1354                 }
1355
1356                 public override bool IsClass {
1357                         get { return gt.IsClass; }
1358                 }
1359
1360                 public override bool IsValueType {
1361                         get { return gt.IsValueType; }
1362                 }
1363
1364                 public override bool IsInterface {
1365                         get { return gt.IsInterface; }
1366                 }
1367
1368                 public override bool IsSealed {
1369                         get { return gt.IsSealed; }
1370                 }
1371
1372                 public override bool Equals (object obj)
1373                 {
1374                         ConstructedType cobj = obj as ConstructedType;
1375                         if (cobj == null)
1376                                 return false;
1377
1378                         if ((type == null) || (cobj.type == null))
1379                                 return false;
1380
1381                         return type == cobj.type;
1382                 }
1383
1384                 public override int GetHashCode ()
1385                 {
1386                         return base.GetHashCode ();
1387                 }
1388
1389                 public override string Name {
1390                         get {
1391                                 return full_name;
1392                         }
1393                 }
1394
1395
1396                 public override string FullName {
1397                         get {
1398                                 return full_name;
1399                         }
1400                 }
1401         }
1402
1403         public class GenericMethod : DeclSpace
1404         {
1405                 public GenericMethod (NamespaceEntry ns, TypeContainer parent,
1406                                       MemberName name, Location l)
1407                         : base (ns, parent, name, null, l)
1408                 { }
1409
1410                 public override TypeBuilder DefineType ()
1411                 {
1412                         throw new Exception ();
1413                 }
1414
1415                 public override bool Define ()
1416                 {
1417                         for (int i = 0; i < TypeParameters.Length; i++)
1418                                 if (!TypeParameters [i].Resolve (Parent))
1419                                         return false;
1420
1421                         return true;
1422                 }
1423
1424                 public bool Define (MethodBuilder mb, Type return_type)
1425                 {
1426                         if (!Define ())
1427                                 return false;
1428
1429                         GenericTypeParameterBuilder[] gen_params;
1430                         string[] names = MemberName.TypeArguments.GetDeclarations ();
1431                         gen_params = mb.DefineGenericParameters (names);
1432                         for (int i = 0; i < TypeParameters.Length; i++)
1433                                 TypeParameters [i].Define (gen_params [i]);
1434
1435                         ec = new EmitContext (
1436                                 this, this, Location, null, return_type, ModFlags, false);
1437
1438                         for (int i = 0; i < TypeParameters.Length; i++) {
1439                                 if (!TypeParameters [i].ResolveType (ec))
1440                                         return false;
1441                         }
1442
1443                         return true;
1444                 }
1445
1446                 public bool DefineType (EmitContext ec, MethodBuilder mb,
1447                                         MethodInfo implementing, bool is_override)
1448                 {
1449                         for (int i = 0; i < TypeParameters.Length; i++)
1450                                 if (!TypeParameters [i].DefineType (
1451                                             ec, mb, implementing, is_override))
1452                                         return false;
1453
1454                         return true;
1455                 }
1456
1457                 public override bool DefineMembers (TypeContainer parent)
1458                 {
1459                         return true;
1460                 }
1461
1462                 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1463                                                         MemberFilter filter, object criteria)
1464                 {
1465                         throw new Exception ();
1466                 }               
1467
1468                 public override MemberCache MemberCache {
1469                         get {
1470                                 return null;
1471                         }
1472                 }
1473
1474                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1475                 {
1476                         // FIXME
1477                 }
1478
1479                 protected override void VerifyObsoleteAttribute()
1480                 {
1481                         // FIXME
1482                 }
1483
1484                 public override AttributeTargets AttributeTargets {
1485                         get {
1486                                 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1487                         }
1488                 }
1489
1490                 public override string DocCommentHeader {
1491                         get { return "M:"; }
1492                 }
1493         }
1494
1495         public class DefaultValueExpression : Expression
1496         {
1497                 Expression expr;
1498                 LocalTemporary temp_storage;
1499
1500                 public DefaultValueExpression (Expression expr, Location loc)
1501                 {
1502                         this.expr = expr;
1503                         this.loc = loc;
1504                 }
1505
1506                 public override Expression DoResolve (EmitContext ec)
1507                 {
1508                         TypeExpr texpr = expr.ResolveAsTypeTerminal (ec);
1509                         if (texpr == null)
1510                                 return null;
1511
1512                         type = texpr.Type;
1513                         if (type.IsGenericParameter || TypeManager.IsValueType (type))
1514                                 temp_storage = new LocalTemporary (ec, type);
1515
1516                         eclass = ExprClass.Variable;
1517                         return this;
1518                 }
1519
1520                 public override void Emit (EmitContext ec)
1521                 {
1522                         if (temp_storage != null) {
1523                                 temp_storage.AddressOf (ec, AddressOp.LoadStore);
1524                                 ec.ig.Emit (OpCodes.Initobj, type);
1525                                 temp_storage.Emit (ec);
1526                         } else
1527                                 ec.ig.Emit (OpCodes.Ldnull);
1528                 }
1529         }
1530
1531         public class NullableType : TypeExpr
1532         {
1533                 Expression underlying;
1534
1535                 public NullableType (Expression underlying, Location l)
1536                 {
1537                         this.underlying = underlying;
1538                         loc = l;
1539
1540                         eclass = ExprClass.Type;
1541                 }
1542
1543                 public NullableType (Type type, Location loc)
1544                         : this (new TypeExpression (type, loc), loc)
1545                 { }
1546
1547                 public override string Name {
1548                         get { return underlying.ToString () + "?"; }
1549                 }
1550
1551                 public override string FullName {
1552                         get { return underlying.ToString () + "?"; }
1553                 }
1554
1555                 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1556                 {
1557                         TypeArguments args = new TypeArguments (loc);
1558                         args.Add (underlying);
1559
1560                         ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
1561                         return ctype.ResolveAsTypeTerminal (ec);
1562                 }
1563         }
1564
1565         public partial class TypeManager
1566         {
1567                 //
1568                 // A list of core types that the compiler requires or uses
1569                 //
1570                 static public Type new_constraint_attr_type;
1571                 static public Type activator_type;
1572                 static public Type generic_ienumerator_type;
1573                 static public Type generic_ienumerable_type;
1574                 static public Type generic_nullable_type;
1575
1576                 // <remarks>
1577                 //   Tracks the generic parameters.
1578                 // </remarks>
1579                 static PtrHashtable builder_to_type_param;
1580
1581                 //
1582                 // These methods are called by code generated by the compiler
1583                 //
1584                 static public MethodInfo activator_create_instance;
1585
1586                 static void InitGenerics ()
1587                 {
1588                         builder_to_type_param = new PtrHashtable ();
1589                 }
1590
1591                 static void CleanUpGenerics ()
1592                 {
1593                         builder_to_type_param = null;
1594                 }
1595
1596                 static void InitGenericCoreTypes ()
1597                 {
1598                         activator_type = CoreLookupType ("System.Activator");
1599                         new_constraint_attr_type = CoreLookupType (
1600                                 "System.Runtime.CompilerServices.NewConstraintAttribute");
1601
1602                         generic_ienumerator_type = CoreLookupType ("System.Collections.Generic.IEnumerator", 1);
1603                         generic_ienumerable_type = CoreLookupType ("System.Collections.Generic.IEnumerable", 1);
1604                         generic_nullable_type = CoreLookupType ("System.Nullable", 1);
1605                 }
1606
1607                 static void InitGenericCodeHelpers ()
1608                 {
1609                         // Activator
1610                         Type [] type_arg = { type_type };
1611                         activator_create_instance = GetMethod (
1612                                 activator_type, "CreateInstance", type_arg);
1613                 }
1614
1615                 static Type CoreLookupType (string name, int arity)
1616                 {
1617                         return CoreLookupType (MemberName.MakeName (name, arity));
1618                 }
1619
1620                 public static void AddTypeParameter (Type t, TypeParameter tparam)
1621                 {
1622                         if (!builder_to_type_param.Contains (t))
1623                                 builder_to_type_param.Add (t, tparam);
1624                 }
1625
1626                 public static TypeContainer LookupGenericTypeContainer (Type t)
1627                 {
1628                         while (t.IsGenericInstance)
1629                                 t = t.GetGenericTypeDefinition ();
1630
1631                         return LookupTypeContainer (t);
1632                 }
1633
1634                 public static TypeParameter LookupTypeParameter (Type t)
1635                 {
1636                         return (TypeParameter) builder_to_type_param [t];
1637                 }
1638
1639                 public static bool HasConstructorConstraint (Type t)
1640                 {
1641                         GenericConstraints gc = GetTypeParameterConstraints (t);
1642                         if (gc == null)
1643                                 return false;
1644
1645                         return (gc.Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
1646                 }
1647
1648                 public static GenericConstraints GetTypeParameterConstraints (Type t)
1649                 {
1650                         if (!t.IsGenericParameter)
1651                                 throw new InvalidOperationException ();
1652
1653                         TypeParameter tparam = LookupTypeParameter (t);
1654                         if (tparam != null)
1655                                 return tparam.GenericConstraints;
1656
1657                         return new ReflectionConstraints (t);
1658                 }
1659
1660                 public static bool IsGeneric (Type t)
1661                 {
1662                         DeclSpace ds = (DeclSpace) builder_to_declspace [t];
1663
1664                         return ds.IsGeneric;
1665                 }
1666
1667                 public static bool HasGenericArguments (Type t)
1668                 {
1669                         return GetNumberOfTypeArguments (t) > 0;
1670                 }
1671
1672                 public static int GetNumberOfTypeArguments (Type t)
1673                 {
1674                         DeclSpace tc = LookupDeclSpace (t);
1675                         if (tc != null)
1676                                 return tc.IsGeneric ? tc.CountTypeParameters : 0;
1677                         else
1678                                 return t.HasGenericArguments ? t.GetGenericArguments ().Length : 0;
1679                 }
1680
1681                 public static Type[] GetTypeArguments (Type t)
1682                 {
1683                         DeclSpace tc = LookupDeclSpace (t);
1684                         if (tc != null) {
1685                                 if (!tc.IsGeneric)
1686                                         return Type.EmptyTypes;
1687
1688                                 TypeParameter[] tparam = tc.TypeParameters;
1689                                 Type[] ret = new Type [tparam.Length];
1690                                 for (int i = 0; i < tparam.Length; i++) {
1691                                         ret [i] = tparam [i].Type;
1692                                         if (ret [i] == null)
1693                                                 throw new InternalErrorException ();
1694                                 }
1695
1696                                 return ret;
1697                         } else
1698                                 return t.GetGenericArguments ();
1699                 }
1700
1701                 //
1702                 // Whether `array' is an array of T and `enumerator' is `IEnumerable<T>'.
1703                 // For instance "string[]" -> "IEnumerable<string>".
1704                 //
1705                 public static bool IsIEnumerable (Type array, Type enumerator)
1706                 {
1707                         if (!array.IsArray || !enumerator.IsGenericInstance)
1708                                 return false;
1709
1710                         if (enumerator.GetGenericTypeDefinition () != generic_ienumerable_type)
1711                                 return false;
1712
1713                         Type[] args = GetTypeArguments (enumerator);
1714                         return args [0] == GetElementType (array);
1715                 }
1716
1717                 public static bool IsEqual (Type a, Type b)
1718                 {
1719                         if (a.Equals (b))
1720                                 return true;
1721
1722                         if ((a is TypeBuilder) && a.IsGenericTypeDefinition && b.IsGenericInstance) {
1723                                 //
1724                                 // `a' is a generic type definition's TypeBuilder and `b' is a
1725                                 // generic instance of the same type.
1726                                 //
1727                                 // Example:
1728                                 //
1729                                 // class Stack<T>
1730                                 // {
1731                                 //     void Test (Stack<T> stack) { }
1732                                 // }
1733                                 //
1734                                 // The first argument of `Test' will be the generic instance
1735                                 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1736                                 //
1737                                 //
1738                                 // We hit this via Closure.Filter() for gen-82.cs.
1739                                 //
1740                                 if (a != b.GetGenericTypeDefinition ())
1741                                         return false;
1742
1743                                 Type[] aparams = a.GetGenericArguments ();
1744                                 Type[] bparams = b.GetGenericArguments ();
1745
1746                                 if (aparams.Length != bparams.Length)
1747                                         return false;
1748
1749                                 for (int i = 0; i < aparams.Length; i++)
1750                                         if (!IsEqual (aparams [i], bparams [i]))
1751                                                 return false;
1752
1753                                 return true;
1754                         }
1755
1756                         if ((b is TypeBuilder) && b.IsGenericTypeDefinition && a.IsGenericInstance)
1757                                 return IsEqual (b, a);
1758
1759                         if (a.IsGenericParameter && b.IsGenericParameter) {
1760                                 if ((a.DeclaringMethod == null) || (b.DeclaringMethod == null))
1761                                         return false;
1762                                 return a.GenericParameterPosition == b.GenericParameterPosition;
1763                         }
1764
1765                         if (a.IsArray && b.IsArray) {
1766                                 if (a.GetArrayRank () != b.GetArrayRank ())
1767                                         return false;
1768                                 return IsEqual (a.GetElementType (), b.GetElementType ());
1769                         }
1770
1771                         if (a.IsGenericInstance && b.IsGenericInstance) {
1772                                 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1773                                         return false;
1774
1775                                 Type[] aargs = a.GetGenericArguments ();
1776                                 Type[] bargs = b.GetGenericArguments ();
1777
1778                                 if (aargs.Length != bargs.Length)
1779                                         return false;
1780
1781                                 for (int i = 0; i < aargs.Length; i++) {
1782                                         if (!IsEqual (aargs [i], bargs [i]))
1783                                                 return false;
1784                                 }
1785
1786                                 return true;
1787                         }
1788
1789                         return false;
1790                 }
1791
1792                 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_infered, Type[] method_infered)
1793                 {
1794                         if (a.IsGenericParameter) {
1795                                 //
1796                                 // If a is an array of a's type, they may never
1797                                 // become equal.
1798                                 //
1799                                 while (b.IsArray) {
1800                                         b = b.GetElementType ();
1801                                         if (a.Equals (b))
1802                                                 return false;
1803                                 }
1804
1805                                 //
1806                                 // If b is a generic parameter or an actual type,
1807                                 // they may become equal:
1808                                 //
1809                                 //    class X<T,U> : I<T>, I<U>
1810                                 //    class X<T> : I<T>, I<float>
1811                                 // 
1812                                 if (b.IsGenericParameter || !b.IsGenericInstance) {
1813                                         int pos = a.GenericParameterPosition;
1814                                         Type[] args = a.DeclaringMethod != null ? method_infered : class_infered;
1815                                         if (args [pos] == null) {
1816                                                 args [pos] = b;
1817                                                 return true;
1818                                         }
1819
1820                                         return args [pos] == a;
1821                                 }
1822
1823                                 //
1824                                 // We're now comparing a type parameter with a
1825                                 // generic instance.  They may become equal unless
1826                                 // the type parameter appears anywhere in the
1827                                 // generic instance:
1828                                 //
1829                                 //    class X<T,U> : I<T>, I<X<U>>
1830                                 //        -> error because you could instanciate it as
1831                                 //           X<X<int>,int>
1832                                 //
1833                                 //    class X<T> : I<T>, I<X<T>> -> ok
1834                                 //
1835
1836                                 Type[] bargs = GetTypeArguments (b);
1837                                 for (int i = 0; i < bargs.Length; i++) {
1838                                         if (a.Equals (bargs [i]))
1839                                                 return false;
1840                                 }
1841
1842                                 return true;
1843                         }
1844
1845                         if (b.IsGenericParameter)
1846                                 return MayBecomeEqualGenericTypes (b, a, class_infered, method_infered);
1847
1848                         //
1849                         // At this point, neither a nor b are a type parameter.
1850                         //
1851                         // If one of them is a generic instance, let
1852                         // MayBecomeEqualGenericInstances() compare them (if the
1853                         // other one is not a generic instance, they can never
1854                         // become equal).
1855                         //
1856
1857                         if (a.IsGenericInstance || b.IsGenericInstance)
1858                                 return MayBecomeEqualGenericInstances (a, b, class_infered, method_infered);
1859
1860                         //
1861                         // If both of them are arrays.
1862                         //
1863
1864                         if (a.IsArray && b.IsArray) {
1865                                 if (a.GetArrayRank () != b.GetArrayRank ())
1866                                         return false;
1867                         
1868                                 a = a.GetElementType ();
1869                                 b = b.GetElementType ();
1870
1871                                 return MayBecomeEqualGenericTypes (a, b, class_infered, method_infered);
1872                         }
1873
1874                         //
1875                         // Ok, two ordinary types.
1876                         //
1877
1878                         return a.Equals (b);
1879                 }
1880
1881                 //
1882                 // Checks whether two generic instances may become equal for some
1883                 // particular instantiation (26.3.1).
1884                 //
1885                 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1886                                                                    Type[] class_infered, Type[] method_infered)
1887                 {
1888                         if (!a.IsGenericInstance || !b.IsGenericInstance)
1889                                 return false;
1890                         if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1891                                 return false;
1892
1893                         return MayBecomeEqualGenericInstances (
1894                                 GetTypeArguments (a), GetTypeArguments (b), class_infered, method_infered);
1895                 }
1896
1897                 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1898                                                                    Type[] class_infered, Type[] method_infered)
1899                 {
1900                         if (aargs.Length != bargs.Length)
1901                                 return false;
1902
1903                         for (int i = 0; i < aargs.Length; i++) {
1904                                 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_infered, method_infered))
1905                                         return false;
1906                         }
1907
1908                         return true;
1909                 }
1910
1911                 public static bool IsEqualGenericInstance (Type type, Type parent)
1912                 {
1913                         int tcount = GetNumberOfTypeArguments (type);
1914                         int pcount = GetNumberOfTypeArguments (parent);
1915
1916                         if (type.IsGenericInstance)
1917                                 type = type.GetGenericTypeDefinition ();
1918                         if (parent.IsGenericInstance)
1919                                 parent = parent.GetGenericTypeDefinition ();
1920
1921                         if (tcount != pcount)
1922                                 return false;
1923
1924                         return type.Equals (parent);
1925                 }
1926
1927                 static public bool IsGenericMethod (MethodBase mb)
1928                 {
1929                         if (mb.DeclaringType is TypeBuilder) {
1930                                 IMethodData method = (IMethodData) builder_to_method [mb];
1931                                 if (method == null)
1932                                         return false;
1933
1934                                 return method.GenericMethod != null;
1935                         }
1936
1937                         return mb.IsGenericMethodDefinition;
1938                 }
1939
1940                 //
1941                 // Type inference.
1942                 //
1943
1944                 static bool InferType (Type pt, Type at, Type[] infered)
1945                 {
1946                         if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
1947                                 int pos = pt.GenericParameterPosition;
1948
1949                                 if (infered [pos] == null) {
1950                                         Type check = at;
1951                                         while (check.IsArray)
1952                                                 check = check.GetElementType ();
1953
1954                                         if (pt == check)
1955                                                 return false;
1956
1957                                         infered [pos] = at;
1958                                         return true;
1959                                 }
1960
1961                                 if (infered [pos] != at)
1962                                         return false;
1963
1964                                 return true;
1965                         }
1966
1967                         if (!pt.ContainsGenericParameters) {
1968                                 if (at.ContainsGenericParameters)
1969                                         return InferType (at, pt, infered);
1970                                 else
1971                                         return true;
1972                         }
1973
1974                         if (at.IsArray) {
1975                                 if (!pt.IsArray ||
1976                                     (at.GetArrayRank () != pt.GetArrayRank ()))
1977                                         return false;
1978
1979                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1980                         }
1981
1982                         if (pt.IsArray) {
1983                                 if (!at.IsArray ||
1984                                     (pt.GetArrayRank () != at.GetArrayRank ()))
1985                                         return false;
1986
1987                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1988                         }
1989
1990                         if (pt.IsByRef && at.IsByRef)
1991                                 return InferType (pt.GetElementType (), at.GetElementType (), infered);
1992                         ArrayList list = new ArrayList ();
1993                         if (at.IsGenericInstance)
1994                                 list.Add (at);
1995                         else {
1996                                 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
1997                                         list.Add (bt);
1998
1999                                 list.AddRange (TypeManager.GetInterfaces (at));
2000                         }
2001
2002                         bool found_one = false;
2003
2004                         foreach (Type type in list) {
2005                                 if (!type.IsGenericInstance)
2006                                         continue;
2007
2008                                 Type[] infered_types = new Type [infered.Length];
2009
2010                                 if (!InferGenericInstance (pt, type, infered_types))
2011                                         continue;
2012
2013                                 for (int i = 0; i < infered_types.Length; i++) {
2014                                         if (infered [i] == null) {
2015                                                 infered [i] = infered_types [i];
2016                                                 continue;
2017                                         }
2018
2019                                         if (infered [i] != infered_types [i])
2020                                                 return false;
2021                                 }
2022
2023                                 found_one = true;
2024                         }
2025
2026                         return found_one;
2027                 }
2028
2029                 static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
2030                 {
2031                         Type[] at_args = at.GetGenericArguments ();
2032                         Type[] pt_args = pt.GetGenericArguments ();
2033
2034                         if (at_args.Length != pt_args.Length)
2035                                 return false;
2036
2037                         for (int i = 0; i < at_args.Length; i++) {
2038                                 if (!InferType (pt_args [i], at_args [i], infered_types))
2039                                         return false;
2040                         }
2041
2042                         for (int i = 0; i < infered_types.Length; i++) {
2043                                 if (infered_types [i] == null)
2044                                         return false;
2045                         }
2046
2047                         return true;
2048                 }
2049
2050                 public static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
2051                                                              ref MethodBase method)
2052                 {
2053                         if ((arguments == null) || !TypeManager.IsGenericMethod (method))
2054                                 return true;
2055
2056                         int arg_count;
2057                         
2058                         if (arguments == null)
2059                                 arg_count = 0;
2060                         else
2061                                 arg_count = arguments.Count;
2062                         
2063                         ParameterData pd = TypeManager.GetParameterData (method);
2064
2065                         int pd_count = pd.Count;
2066
2067                         if (pd_count == 0)
2068                                 return false;
2069                         
2070                         if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2071                                 return false;
2072                         
2073                         if (pd_count - 1 > arg_count)
2074                                 return false;
2075                         
2076                         if (pd_count == 1 && arg_count == 0)
2077                                 return true;
2078
2079                         Type[] method_args = method.GetGenericArguments ();
2080                         Type[] infered_types = new Type [method_args.Length];
2081
2082                         //
2083                         // If we have come this far, the case which
2084                         // remains is when the number of parameters is
2085                         // less than or equal to the argument count.
2086                         //
2087                         for (int i = 0; i < pd_count - 1; ++i) {
2088                                 Argument a = (Argument) arguments [i];
2089
2090                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2091                                         continue;
2092
2093                                 Type pt = pd.ParameterType (i);
2094                                 Type at = a.Type;
2095
2096                                 if (!InferType (pt, at, infered_types))
2097                                         return false;
2098                         }
2099
2100                         Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
2101
2102                         for (int i = pd_count - 1; i < arg_count; i++) {
2103                                 Argument a = (Argument) arguments [i];
2104
2105                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2106                                         continue;
2107
2108                                 if (!InferType (element_type, a.Type, infered_types))
2109                                         return false;
2110                         }
2111
2112                         for (int i = 0; i < infered_types.Length; i++)
2113                                 if (infered_types [i] == null)
2114                                         return false;
2115
2116                         method = method.BindGenericParameters (infered_types);
2117                         return true;
2118                 }
2119
2120                 public static bool InferTypeArguments (Type[] param_types, Type[] arg_types, Type[] infered_types)
2121                 {
2122                         if (infered_types == null)
2123                                 return false;
2124
2125                         for (int i = 0; i < arg_types.Length; i++) {
2126                                 if (arg_types [i] == null)
2127                                         continue;
2128
2129                                 if (!InferType (param_types [i], arg_types [i], infered_types))
2130                                         return false;
2131                         }
2132
2133                         for (int i = 0; i < infered_types.Length; i++)
2134                                 if (infered_types [i] == null)
2135                                         return false;
2136
2137                         return true;
2138                 }
2139
2140                 public static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
2141                                                        ref MethodBase method)
2142                 {
2143                         if (!TypeManager.IsGenericMethod (method))
2144                                 return true;
2145
2146                         int arg_count;
2147                         if (arguments != null)
2148                                 arg_count = arguments.Count;
2149                         else
2150                                 arg_count = 0;
2151
2152                         ParameterData pd = TypeManager.GetParameterData (method);
2153                         if (arg_count != pd.Count)
2154                                 return false;
2155
2156                         Type[] method_args = method.GetGenericArguments ();
2157
2158                         bool is_open = false;
2159                         for (int i = 0; i < method_args.Length; i++) {
2160                                 if (method_args [i].IsGenericParameter) {
2161                                         is_open = true;
2162                                         break;
2163                                 }
2164                         }
2165                         if (!is_open)
2166                                 return true;
2167
2168                         Type[] infered_types = new Type [method_args.Length];
2169
2170                         Type[] param_types = new Type [pd.Count];
2171                         Type[] arg_types = new Type [pd.Count];
2172
2173                         for (int i = 0; i < arg_count; i++) {
2174                                 param_types [i] = pd.ParameterType (i);
2175
2176                                 Argument a = (Argument) arguments [i];
2177                                 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2178                                         continue;
2179
2180                                 arg_types [i] = a.Type;
2181                         }
2182
2183                         if (!InferTypeArguments (param_types, arg_types, infered_types))
2184                                 return false;
2185
2186                         method = method.BindGenericParameters (infered_types);
2187                         return true;
2188                 }
2189
2190                 public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
2191                                                        ref MethodBase method)
2192                 {
2193                         if (!TypeManager.IsGenericMethod (method))
2194                                 return true;
2195
2196                         ParameterData pd = TypeManager.GetParameterData (method);
2197                         if (apd.Count != pd.Count)
2198                                 return false;
2199
2200                         Type[] method_args = method.GetGenericArguments ();
2201                         Type[] infered_types = new Type [method_args.Length];
2202
2203                         Type[] param_types = new Type [pd.Count];
2204                         Type[] arg_types = new Type [pd.Count];
2205
2206                         for (int i = 0; i < apd.Count; i++) {
2207                                 param_types [i] = pd.ParameterType (i);
2208                                 arg_types [i] = apd.ParameterType (i);
2209                         }
2210
2211                         if (!InferTypeArguments (param_types, arg_types, infered_types))
2212                                 return false;
2213
2214                         method = method.BindGenericParameters (infered_types);
2215                         return true;
2216                 }
2217
2218                 public static bool IsNullableType (Type t)
2219                 {
2220                         if (!t.IsGenericInstance)
2221                                 return false;
2222
2223                         Type gt = t.GetGenericTypeDefinition ();
2224                         return gt == generic_nullable_type;
2225                 }
2226         }
2227
2228         public abstract class Nullable
2229         {
2230                 protected sealed class NullableInfo
2231                 {
2232                         public readonly Type Type;
2233                         public readonly Type UnderlyingType;
2234                         public readonly MethodInfo HasValue;
2235                         public readonly MethodInfo Value;
2236                         public readonly ConstructorInfo Constructor;
2237
2238                         public NullableInfo (Type type)
2239                         {
2240                                 Type = type;
2241                                 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
2242
2243                                 PropertyInfo has_value_pi = type.GetProperty ("HasValue");
2244                                 PropertyInfo value_pi = type.GetProperty ("Value");
2245
2246                                 HasValue = has_value_pi.GetGetMethod (false);
2247                                 Value = value_pi.GetGetMethod (false);
2248                                 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
2249                         }
2250                 }
2251
2252                 protected class Unwrap : Expression, IMemoryLocation, IAssignMethod
2253                 {
2254                         Expression expr;
2255                         NullableInfo info;
2256
2257                         LocalTemporary temp;
2258                         bool has_temp;
2259
2260                         public Unwrap (Expression expr, Location loc)
2261                         {
2262                                 this.expr = expr;
2263                                 this.loc = loc;
2264                         }
2265
2266                         public override Expression DoResolve (EmitContext ec)
2267                         {
2268                                 expr = expr.Resolve (ec);
2269                                 if (expr == null)
2270                                         return null;
2271
2272                                 if (!(expr is IMemoryLocation))
2273                                         temp = new LocalTemporary (ec, expr.Type);
2274
2275                                 info = new NullableInfo (expr.Type);
2276                                 type = info.UnderlyingType;
2277                                 eclass = expr.eclass;
2278                                 return this;
2279                         }
2280
2281                         public override void Emit (EmitContext ec)
2282                         {
2283                                 AddressOf (ec, AddressOp.LoadStore);
2284                                 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
2285                         }
2286
2287                         public void EmitCheck (EmitContext ec)
2288                         {
2289                                 AddressOf (ec, AddressOp.LoadStore);
2290                                 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
2291                         }
2292
2293                         void create_temp (EmitContext ec)
2294                         {
2295                                 if ((temp != null) && !has_temp) {
2296                                         expr.Emit (ec);
2297                                         temp.Store (ec);
2298                                         has_temp = true;
2299                                 }
2300                         }
2301
2302                         public void AddressOf (EmitContext ec, AddressOp mode)
2303                         {
2304                                 create_temp (ec);
2305                                 if (temp != null)
2306                                         temp.AddressOf (ec, AddressOp.LoadStore);
2307                                 else
2308                                         ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
2309                         }
2310
2311                         public void Emit (EmitContext ec, bool leave_copy)
2312                         {
2313                                 create_temp (ec);
2314                                 if (leave_copy) {
2315                                         if (temp != null)
2316                                                 temp.Emit (ec);
2317                                         else
2318                                                 expr.Emit (ec);
2319                                 }
2320
2321                                 Emit (ec);
2322                         }
2323
2324                         public void EmitAssign (EmitContext ec, Expression source,
2325                                                 bool leave_copy, bool prepare_for_load)
2326                         {
2327                                 source.Emit (ec);
2328                                 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2329
2330                                 if (leave_copy)
2331                                         ec.ig.Emit (OpCodes.Dup);
2332
2333                                 Expression empty = new EmptyExpression (expr.Type);
2334                                 ((IAssignMethod) expr).EmitAssign (ec, empty, false, prepare_for_load);
2335                         }
2336                 }
2337
2338                 protected class Wrap : Expression
2339                 {
2340                         Expression expr;
2341                         NullableInfo info;
2342
2343                         public Wrap (Expression expr, Location loc)
2344                         {
2345                                 this.expr = expr;
2346                                 this.loc = loc;
2347                         }
2348
2349                         public override Expression DoResolve (EmitContext ec)
2350                         {
2351                                 expr = expr.Resolve (ec);
2352                                 if (expr == null)
2353                                         return null;
2354
2355                                 TypeExpr target_type = new NullableType (expr.Type, loc);
2356                                 target_type = target_type.ResolveAsTypeTerminal (ec);
2357                                 if (target_type == null)
2358                                         return null;
2359
2360                                 type = target_type.Type;
2361                                 info = new NullableInfo (type);
2362                                 eclass = ExprClass.Value;
2363                                 return this;
2364                         }
2365
2366                         public override void Emit (EmitContext ec)
2367                         {
2368                                 expr.Emit (ec);
2369                                 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2370                         }
2371                 }
2372
2373                 public class NullableLiteral : Expression, IMemoryLocation {
2374                         public NullableLiteral (Type target_type, Location loc)
2375                         {
2376                                 this.type = target_type;
2377                                 this.loc = loc;
2378
2379                                 eclass = ExprClass.Value;
2380                         }
2381                 
2382                         public override Expression DoResolve (EmitContext ec)
2383                         {
2384                                 return this;
2385                         }
2386
2387                         public override void Emit (EmitContext ec)
2388                         {
2389                                 LocalTemporary value_target = new LocalTemporary (ec, type);
2390
2391                                 value_target.AddressOf (ec, AddressOp.Store);
2392                                 ec.ig.Emit (OpCodes.Initobj, type);
2393                                 value_target.Emit (ec);
2394                         }
2395
2396                         public void AddressOf (EmitContext ec, AddressOp Mode)
2397                         {
2398                                 LocalTemporary value_target = new LocalTemporary (ec, type);
2399                                         
2400                                 value_target.AddressOf (ec, AddressOp.Store);
2401                                 ec.ig.Emit (OpCodes.Initobj, type);
2402                                 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
2403                         }
2404                 }
2405
2406                 public abstract class Lifted : Expression, IMemoryLocation
2407                 {
2408                         Expression expr, underlying, wrap, null_value;
2409                         Unwrap unwrap;
2410
2411                         protected Lifted (Expression expr, Location loc)
2412                         {
2413                                 this.expr = expr;
2414                                 this.loc = loc;
2415                         }
2416
2417                         public override Expression DoResolve (EmitContext ec)
2418                         {
2419                                 expr = expr.Resolve (ec);
2420                                 if (expr == null)
2421                                         return null;
2422
2423                                 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2424                                 if (unwrap == null)
2425                                         return null;
2426
2427                                 underlying = ResolveUnderlying (unwrap, ec);
2428                                 if (underlying == null)
2429                                         return null;
2430
2431                                 wrap = new Wrap (underlying, loc).Resolve (ec);
2432                                 if (wrap == null)
2433                                         return null;
2434
2435                                 null_value = new NullableLiteral (wrap.Type, loc).Resolve (ec);
2436                                 if (null_value == null)
2437                                         return null;
2438
2439                                 type = wrap.Type;
2440                                 eclass = ExprClass.Value;
2441                                 return this;
2442                         }
2443
2444                         protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
2445
2446                         public override void Emit (EmitContext ec)
2447                         {
2448                                 ILGenerator ig = ec.ig;
2449                                 Label is_null_label = ig.DefineLabel ();
2450                                 Label end_label = ig.DefineLabel ();
2451
2452                                 unwrap.EmitCheck (ec);
2453                                 ig.Emit (OpCodes.Brfalse, is_null_label);
2454
2455                                 wrap.Emit (ec);
2456                                 ig.Emit (OpCodes.Br, end_label);
2457
2458                                 ig.MarkLabel (is_null_label);
2459                                 null_value.Emit (ec);
2460
2461                                 ig.MarkLabel (end_label);
2462                         }
2463
2464                         public void AddressOf (EmitContext ec, AddressOp mode)
2465                         {
2466                                 unwrap.AddressOf (ec, mode);
2467                         }
2468                 }
2469
2470                 public class LiftedConversion : Lifted
2471                 {
2472                         public readonly bool IsUser;
2473                         public readonly bool IsExplicit;
2474                         public readonly Type TargetType;
2475
2476                         public LiftedConversion (Expression expr, Type target_type, bool is_user,
2477                                                  bool is_explicit, Location loc)
2478                                 : base (expr, loc)
2479                         {
2480                                 this.IsUser = is_user;
2481                                 this.IsExplicit = is_explicit;
2482                                 this.TargetType = target_type;
2483                         }
2484
2485                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2486                         {
2487                                 Type type = TypeManager.GetTypeArguments (TargetType) [0];
2488
2489                                 if (IsUser) {
2490                                         return Convert.UserDefinedConversion (ec, unwrap, type, loc, IsExplicit);
2491                                 } else {
2492                                         if (IsExplicit)
2493                                                 return Convert.ExplicitConversion (ec, unwrap, type, loc);
2494                                         else
2495                                                 return Convert.ImplicitConversion (ec, unwrap, type, loc);
2496                                 }
2497                         }
2498                 }
2499
2500                 public class LiftedUnaryOperator : Lifted
2501                 {
2502                         public readonly Unary.Operator Oper;
2503
2504                         public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
2505                                 : base (expr, loc)
2506                         {
2507                                 this.Oper = op;
2508                         }
2509
2510                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2511                         {
2512                                 return new Unary (Oper, unwrap, loc);
2513                         }
2514                 }
2515
2516                 public class LiftedConditional : Lifted
2517                 {
2518                         Expression expr, true_expr, false_expr;
2519                         Unwrap unwrap;
2520
2521                         public LiftedConditional (Expression expr, Expression true_expr, Expression false_expr,
2522                                                   Location loc)
2523                                 : base (expr, loc)
2524                         {
2525                                 this.true_expr = true_expr;
2526                                 this.false_expr = false_expr;
2527                         }
2528
2529                         protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2530                         {
2531                                 return new Conditional (unwrap, true_expr, false_expr, loc);
2532                         }
2533                 }
2534
2535                 public class LiftedBinaryOperator : Expression
2536                 {
2537                         public readonly Binary.Operator Oper;
2538
2539                         Expression left, right, underlying, null_value, bool_wrap;
2540                         Unwrap left_unwrap, right_unwrap;
2541                         bool is_equality, is_comparision, is_boolean;
2542
2543                         public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
2544                                                      Location loc)
2545                         {
2546                                 this.Oper = op;
2547                                 this.left = left;
2548                                 this.right = right;
2549                                 this.loc = loc;
2550                         }
2551
2552                         public override Expression DoResolve (EmitContext ec)
2553                         {
2554                                 if (TypeManager.IsNullableType (left.Type)) {
2555                                         left_unwrap = new Unwrap (left, loc);
2556                                         left = left_unwrap.Resolve (ec);
2557                                         if (left == null)
2558                                                 return null;
2559                                 }
2560
2561                                 if (TypeManager.IsNullableType (right.Type)) {
2562                                         right_unwrap = new Unwrap (right, loc);
2563                                         right = right_unwrap.Resolve (ec);
2564                                         if (right == null)
2565                                                 return null;
2566                                 }
2567
2568                                 if (((Oper == Binary.Operator.BitwiseAnd) || (Oper == Binary.Operator.BitwiseOr) ||
2569                                      (Oper == Binary.Operator.LogicalAnd) || (Oper == Binary.Operator.LogicalOr)) &&
2570                                     ((left.Type == TypeManager.bool_type) && (right.Type == TypeManager.bool_type))) {
2571                                         Expression empty = new EmptyExpression (TypeManager.bool_type);
2572                                         bool_wrap = new Wrap (empty, loc).Resolve (ec);
2573                                         null_value = new NullableLiteral (bool_wrap.Type, loc).Resolve (ec);
2574
2575                                         type = bool_wrap.Type;
2576                                         is_boolean = true;
2577                                 } else if ((Oper == Binary.Operator.Equality) || (Oper == Binary.Operator.Inequality)) {
2578                                         if (!(left is NullLiteral) && !(right is NullLiteral)) {
2579                                                 underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2580                                                 if (underlying == null)
2581                                                         return null;
2582                                         }
2583
2584                                         type = TypeManager.bool_type;
2585                                         is_equality = true;
2586                                 } else if ((Oper == Binary.Operator.LessThan) ||
2587                                            (Oper == Binary.Operator.GreaterThan) ||
2588                                            (Oper == Binary.Operator.LessThanOrEqual) ||
2589                                            (Oper == Binary.Operator.GreaterThanOrEqual)) {
2590                                         underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2591                                         if (underlying == null)
2592                                                 return null;
2593
2594                                         type = TypeManager.bool_type;
2595                                         is_comparision = true;
2596                                 } else {
2597                                         underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2598                                         if (underlying == null)
2599                                                 return null;
2600
2601                                         underlying = new Wrap (underlying, loc).Resolve (ec);
2602                                         if (underlying == null)
2603                                                 return null;
2604
2605                                         type = underlying.Type;
2606                                         null_value = new NullableLiteral (type, loc).Resolve (ec);
2607                                 }
2608
2609                                 eclass = ExprClass.Value;
2610                                 return this;
2611                         }
2612
2613                         void EmitBoolean (EmitContext ec)
2614                         {
2615                                 ILGenerator ig = ec.ig;
2616
2617                                 Label left_is_null_label = ig.DefineLabel ();
2618                                 Label right_is_null_label = ig.DefineLabel ();
2619                                 Label is_null_label = ig.DefineLabel ();
2620                                 Label wrap_label = ig.DefineLabel ();
2621                                 Label end_label = ig.DefineLabel ();
2622
2623                                 if (left_unwrap != null) {
2624                                         left_unwrap.EmitCheck (ec);
2625                                         ig.Emit (OpCodes.Brfalse, left_is_null_label);
2626                                 }
2627
2628                                 left.Emit (ec);
2629                                 ig.Emit (OpCodes.Dup);
2630                                 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
2631                                         ig.Emit (OpCodes.Brtrue, wrap_label);
2632                                 else
2633                                         ig.Emit (OpCodes.Brfalse, wrap_label);
2634
2635                                 if (right_unwrap != null) {
2636                                         right_unwrap.EmitCheck (ec);
2637                                         ig.Emit (OpCodes.Brfalse, right_is_null_label);
2638                                 }
2639
2640                                 if ((Oper == Binary.Operator.LogicalAnd) || (Oper == Binary.Operator.LogicalOr))
2641                                         ig.Emit (OpCodes.Pop);
2642
2643                                 right.Emit (ec);
2644                                 if (Oper == Binary.Operator.BitwiseOr)
2645                                         ig.Emit (OpCodes.Or);
2646                                 else if (Oper == Binary.Operator.BitwiseAnd)
2647                                         ig.Emit (OpCodes.And);
2648                                 ig.Emit (OpCodes.Br, wrap_label);
2649
2650                                 ig.MarkLabel (left_is_null_label);
2651                                 if (right_unwrap != null) {
2652                                         right_unwrap.EmitCheck (ec);
2653                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2654                                 }
2655
2656                                 right.Emit (ec);
2657                                 ig.Emit (OpCodes.Dup);
2658                                 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOr))
2659                                         ig.Emit (OpCodes.Brtrue, wrap_label);
2660                                 else
2661                                         ig.Emit (OpCodes.Brfalse, wrap_label);
2662
2663                                 ig.MarkLabel (right_is_null_label);
2664                                 ig.Emit (OpCodes.Pop);
2665                                 ig.MarkLabel (is_null_label);
2666                                 null_value.Emit (ec);
2667                                 ig.Emit (OpCodes.Br, end_label);
2668
2669                                 ig.MarkLabel (wrap_label);
2670                                 ig.Emit (OpCodes.Nop);
2671                                 bool_wrap.Emit (ec);
2672                                 ig.Emit (OpCodes.Nop);
2673
2674                                 ig.MarkLabel (end_label);
2675                         }
2676
2677                         void EmitEquality (EmitContext ec)
2678                         {
2679                                 ILGenerator ig = ec.ig;
2680
2681                                 Label left_not_null_label = ig.DefineLabel ();
2682                                 Label false_label = ig.DefineLabel ();
2683                                 Label true_label = ig.DefineLabel ();
2684                                 Label end_label = ig.DefineLabel ();
2685
2686                                 if (left_unwrap != null) {
2687                                         left_unwrap.EmitCheck (ec);
2688                                         if (right is NullLiteral) {
2689                                                 if (Oper == Binary.Operator.Equality)
2690                                                         ig.Emit (OpCodes.Brfalse, true_label);
2691                                                 else
2692                                                         ig.Emit (OpCodes.Brfalse, false_label);
2693                                         } else if (right_unwrap != null) {
2694                                                 ig.Emit (OpCodes.Dup);
2695                                                 ig.Emit (OpCodes.Brtrue, left_not_null_label);
2696                                                 right_unwrap.EmitCheck (ec);
2697                                                 ig.Emit (OpCodes.Ceq);
2698                                                 if (Oper == Binary.Operator.Inequality) {
2699                                                         ig.Emit (OpCodes.Ldc_I4_0);
2700                                                         ig.Emit (OpCodes.Ceq);
2701                                                 }
2702                                                 ig.Emit (OpCodes.Br, end_label);
2703
2704                                                 ig.MarkLabel (left_not_null_label);
2705                                                 ig.Emit (OpCodes.Pop);
2706                                         } else {
2707                                                 if (Oper == Binary.Operator.Equality)
2708                                                         ig.Emit (OpCodes.Brfalse, false_label);
2709                                                 else
2710                                                         ig.Emit (OpCodes.Brfalse, true_label);
2711                                         }
2712                                 }
2713
2714                                 if (right_unwrap != null) {
2715                                         right_unwrap.EmitCheck (ec);
2716                                         if (left is NullLiteral) {
2717                                                 if (Oper == Binary.Operator.Equality)
2718                                                         ig.Emit (OpCodes.Brfalse, true_label);
2719                                                 else
2720                                                         ig.Emit (OpCodes.Brfalse, false_label);
2721                                         } else {
2722                                                 if (Oper == Binary.Operator.Equality)
2723                                                         ig.Emit (OpCodes.Brfalse, false_label);
2724                                                 else
2725                                                         ig.Emit (OpCodes.Brfalse, true_label);
2726                                         }
2727                                 }
2728
2729                                 bool left_is_null = left is NullLiteral;
2730                                 bool right_is_null = right is NullLiteral;
2731                                 if (left_is_null || right_is_null) {
2732                                         if (((Oper == Binary.Operator.Equality) && (left_is_null == right_is_null)) ||
2733                                             ((Oper == Binary.Operator.Inequality) && (left_is_null != right_is_null)))
2734                                                 ig.Emit (OpCodes.Br, true_label);
2735                                         else
2736                                                 ig.Emit (OpCodes.Br, false_label);
2737                                 } else {
2738                                         underlying.Emit (ec);
2739                                         ig.Emit (OpCodes.Br, end_label);
2740                                 }
2741
2742                                 ig.MarkLabel (false_label);
2743                                 ig.Emit (OpCodes.Ldc_I4_0);
2744                                 ig.Emit (OpCodes.Br, end_label);
2745
2746                                 ig.MarkLabel (true_label);
2747                                 ig.Emit (OpCodes.Ldc_I4_1);
2748
2749                                 ig.MarkLabel (end_label);
2750                         }
2751
2752                         void EmitComparision (EmitContext ec)
2753                         {
2754                                 ILGenerator ig = ec.ig;
2755
2756                                 Label is_null_label = ig.DefineLabel ();
2757                                 Label end_label = ig.DefineLabel ();
2758
2759                                 if (left_unwrap != null) {
2760                                         left_unwrap.EmitCheck (ec);
2761                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2762                                 }
2763
2764                                 if (right_unwrap != null) {
2765                                         right_unwrap.EmitCheck (ec);
2766                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2767                                 }
2768
2769                                 underlying.Emit (ec);
2770                                 ig.Emit (OpCodes.Br, end_label);
2771
2772                                 ig.MarkLabel (is_null_label);
2773                                 ig.Emit (OpCodes.Ldc_I4_0);
2774
2775                                 ig.MarkLabel (end_label);
2776                         }
2777
2778                         public override void Emit (EmitContext ec)
2779                         {
2780                                 if (is_boolean) {
2781                                         EmitBoolean (ec);
2782                                         return;
2783                                 } else if (is_equality) {
2784                                         EmitEquality (ec);
2785                                         return;
2786                                 } else if (is_comparision) {
2787                                         EmitComparision (ec);
2788                                         return;
2789                                 }
2790
2791                                 ILGenerator ig = ec.ig;
2792
2793                                 Label is_null_label = ig.DefineLabel ();
2794                                 Label end_label = ig.DefineLabel ();
2795
2796                                 if (left_unwrap != null) {
2797                                         left_unwrap.EmitCheck (ec);
2798                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2799                                 }
2800
2801                                 if (right_unwrap != null) {
2802                                         right_unwrap.EmitCheck (ec);
2803                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2804                                 }
2805
2806                                 underlying.Emit (ec);
2807                                 ig.Emit (OpCodes.Br, end_label);
2808
2809                                 ig.MarkLabel (is_null_label);
2810                                 null_value.Emit (ec);
2811
2812                                 ig.MarkLabel (end_label);
2813                         }
2814                 }
2815
2816                 public class OperatorTrueOrFalse : Expression
2817                 {
2818                         public readonly bool IsTrue;
2819
2820                         Expression expr;
2821                         Unwrap unwrap;
2822
2823                         public OperatorTrueOrFalse (Expression expr, bool is_true, Location loc)
2824                         {
2825                                 this.IsTrue = is_true;
2826                                 this.expr = expr;
2827                                 this.loc = loc;
2828                         }
2829
2830                         public override Expression DoResolve (EmitContext ec)
2831                         {
2832                                 unwrap = new Unwrap (expr, loc);
2833                                 expr = unwrap.Resolve (ec);
2834                                 if (expr == null)
2835                                         return null;
2836
2837                                 if (unwrap.Type != TypeManager.bool_type)
2838                                         return null;
2839
2840                                 type = TypeManager.bool_type;
2841                                 eclass = ExprClass.Value;
2842                                 return this;
2843                         }
2844
2845                         public override void Emit (EmitContext ec)
2846                         {
2847                                 ILGenerator ig = ec.ig;
2848
2849                                 Label is_null_label = ig.DefineLabel ();
2850                                 Label end_label = ig.DefineLabel ();
2851
2852                                 unwrap.EmitCheck (ec);
2853                                 ig.Emit (OpCodes.Brfalse, is_null_label);
2854
2855                                 unwrap.Emit (ec);
2856                                 if (!IsTrue) {
2857                                         ig.Emit (OpCodes.Ldc_I4_0);
2858                                         ig.Emit (OpCodes.Ceq);
2859                                 }
2860                                 ig.Emit (OpCodes.Br, end_label);
2861
2862                                 ig.MarkLabel (is_null_label);
2863                                 ig.Emit (OpCodes.Ldc_I4_0);
2864
2865                                 ig.MarkLabel (end_label);
2866                         }
2867                 }
2868
2869                 public class NullCoalescingOperator : Expression
2870                 {
2871                         Expression left, right;
2872                         Expression expr;
2873                         Unwrap unwrap;
2874
2875                         public NullCoalescingOperator (Expression left, Expression right, Location loc)
2876                         {
2877                                 this.left = left;
2878                                 this.right = right;
2879                                 this.loc = loc;
2880
2881                                 eclass = ExprClass.Value;
2882                         }
2883
2884                         public override Expression DoResolve (EmitContext ec)
2885                         {
2886                                 if (type != null)
2887                                         return this;
2888
2889                                 left = left.Resolve (ec);
2890                                 if (left == null)
2891                                         return null;
2892
2893                                 right = right.Resolve (ec);
2894                                 if (right == null)
2895                                         return null;
2896
2897                                 Type ltype = left.Type, rtype = right.Type;
2898
2899                                 if (!TypeManager.IsNullableType (ltype) && ltype.IsValueType) {
2900                                         Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2901                                         return null;
2902                                 }
2903
2904                                 if (TypeManager.IsNullableType (ltype)) {
2905                                         NullableInfo info = new NullableInfo (ltype);
2906
2907                                         unwrap = (Unwrap) new Unwrap (left, loc).Resolve (ec);
2908                                         if (unwrap == null)
2909                                                 return null;
2910
2911                                         expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
2912                                         if (expr != null) {
2913                                                 left = unwrap;
2914                                                 type = expr.Type;
2915                                                 return this;
2916                                         }
2917                                 }
2918
2919                                 expr = Convert.ImplicitConversion (ec, right, ltype, loc);
2920                                 if (expr != null) {
2921                                         type = expr.Type;
2922                                         return this;
2923                                 }
2924
2925                                 if (unwrap != null) {
2926                                         expr = Convert.ImplicitConversion (ec, unwrap, rtype, loc);
2927                                         if (expr != null) {
2928                                                 left = expr;
2929                                                 expr = right;
2930                                                 type = expr.Type;
2931                                                 return this;
2932                                         }
2933                                 }
2934
2935                                 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2936                                 return null;
2937                         }
2938
2939                         public override void Emit (EmitContext ec)
2940                         {
2941                                 ILGenerator ig = ec.ig;
2942
2943                                 Label is_null_label = ig.DefineLabel ();
2944                                 Label end_label = ig.DefineLabel ();
2945
2946                                 if (unwrap != null) {
2947                                         unwrap.EmitCheck (ec);
2948                                         ig.Emit (OpCodes.Brfalse, is_null_label);
2949                                 }
2950
2951                                 left.Emit (ec);
2952                                 ig.Emit (OpCodes.Br, end_label);
2953
2954                                 ig.MarkLabel (is_null_label);
2955                                 expr.Emit (ec);
2956
2957                                 ig.MarkLabel (end_label);
2958                         }
2959                 }
2960
2961                 public class LiftedUnaryMutator : ExpressionStatement
2962                 {
2963                         public readonly UnaryMutator.Mode Mode;
2964                         Expression expr, null_value;
2965                         UnaryMutator underlying;
2966                         Unwrap unwrap;
2967
2968                         public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
2969                         {
2970                                 this.expr = expr;
2971                                 this.Mode = mode;
2972                                 this.loc = loc;
2973
2974                                 eclass = ExprClass.Value;
2975                         }
2976
2977                         public override Expression DoResolve (EmitContext ec)
2978                         {
2979                                 expr = expr.Resolve (ec);
2980                                 if (expr == null)
2981                                         return null;
2982
2983                                 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2984                                 if (unwrap == null)
2985                                         return null;
2986
2987                                 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
2988                                 if (underlying == null)
2989                                         return null;
2990
2991                                 null_value = new NullableLiteral (expr.Type, loc).Resolve (ec);
2992                                 if (null_value == null)
2993                                         return null;
2994
2995                                 type = expr.Type;
2996                                 return this;
2997                         }
2998
2999                         void DoEmit (EmitContext ec, bool is_expr)
3000                         {
3001                                 ILGenerator ig = ec.ig;
3002                                 Label is_null_label = ig.DefineLabel ();
3003                                 Label end_label = ig.DefineLabel ();
3004
3005                                 unwrap.EmitCheck (ec);
3006                                 ig.Emit (OpCodes.Brfalse, is_null_label);
3007
3008                                 if (is_expr)
3009                                         underlying.Emit (ec);
3010                                 else
3011                                         underlying.EmitStatement (ec);
3012                                 ig.Emit (OpCodes.Br, end_label);
3013
3014                                 ig.MarkLabel (is_null_label);
3015                                 if (is_expr)
3016                                         null_value.Emit (ec);
3017
3018                                 ig.MarkLabel (end_label);
3019                         }
3020
3021                         public override void Emit (EmitContext ec)
3022                         {
3023                                 DoEmit (ec, true);
3024                         }
3025
3026                         public override void EmitStatement (EmitContext ec)
3027                         {
3028                                 DoEmit (ec, false);
3029                         }
3030                 }
3031         }
3032 }