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