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