In ilasm/tests:
[mcs.git] / bmcs / generic.cs
blob1f641368957eca9ca5486117e060adc9fa6dcfa2
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
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;
20 namespace Mono.CSharp {
22 public abstract class GenericConstraints {
23 public abstract GenericParameterAttributes Attributes {
24 get;
27 public bool HasConstructorConstraint {
28 get { return (Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
31 public bool HasReferenceTypeConstraint {
32 get { return (Attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
35 public bool HasValueTypeConstraint {
36 get { return (Attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
39 public virtual bool HasClassConstraint {
40 get { return ClassConstraint != null; }
43 public abstract Type ClassConstraint {
44 get;
47 public abstract Type[] InterfaceConstraints {
48 get;
51 public abstract Type EffectiveBaseClass {
52 get;
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;
65 if (ClassConstraint != null) {
66 if (ClassConstraint.IsValueType)
67 return false;
69 if (ClassConstraint != TypeManager.object_type)
70 return true;
73 foreach (Type t in InterfaceConstraints) {
74 if (!t.IsGenericParameter)
75 continue;
77 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
78 if ((gc != null) && gc.IsReferenceType)
79 return true;
82 return false;
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;
96 if (ClassConstraint != null) {
97 if (!ClassConstraint.IsValueType)
98 return false;
100 if (ClassConstraint != TypeManager.value_type)
101 return true;
104 foreach (Type t in InterfaceConstraints) {
105 if (!t.IsGenericParameter)
106 continue;
108 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
109 if ((gc != null) && gc.IsValueType)
110 return true;
113 return false;
118 public enum SpecialConstraint
120 Constructor,
121 ReferenceType,
122 ValueType
126 // Tracks the constraints for a type parameter
128 public class Constraints : GenericConstraints {
129 string name;
130 ArrayList constraints;
131 Location loc;
134 // name is the identifier, constraints is an arraylist of
135 // Expressions (with types) or `true' for the constructor constraint.
137 public Constraints (string name, ArrayList constraints,
138 Location loc)
140 this.name = name;
141 this.constraints = constraints;
142 this.loc = loc;
145 public string TypeParameter {
146 get {
147 return name;
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;
160 public bool Resolve (EmitContext ec)
162 iface_constraints = new ArrayList ();
163 type_param_constraints = new ArrayList ();
165 foreach (object obj in constraints) {
166 if (HasConstructorConstraint) {
167 Report.Error (401, loc,
168 "The new() constraint must be last.");
169 return false;
172 if (obj is SpecialConstraint) {
173 SpecialConstraint sc = (SpecialConstraint) obj;
175 if (sc == SpecialConstraint.Constructor) {
176 if (!HasValueTypeConstraint) {
177 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
178 continue;
181 Report.Error (
182 451, loc, "The new () constraint " +
183 "cannot be used with the `struct' " +
184 "constraint.");
185 return false;
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;
195 if (sc == SpecialConstraint.ReferenceType)
196 attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
197 else
198 attrs |= GenericParameterAttributes.NotNullableValueTypeConstraint;
199 continue;
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);
211 if (expr == null)
212 return false;
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;
233 num_constraints++;
236 return true;
239 bool CheckTypeParameterConstraints (TypeParameter tparam, Hashtable seen)
241 seen.Add (tparam, true);
243 Constraints constraints = tparam.Constraints;
244 if (constraints == null)
245 return true;
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;
255 if (constraints.type_param_constraints == null)
256 return true;
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;
266 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
267 return false;
270 return true;
273 public bool ResolveTypes (EmitContext ec)
275 if (effective_base_type != null)
276 return true;
278 foreach (object obj in constraints) {
279 ConstructedType cexpr = obj as ConstructedType;
280 if (cexpr == null)
281 continue;
283 if (!cexpr.CheckConstraints (ec))
284 return false;
287 foreach (TypeParameterExpr expr in type_param_constraints) {
288 Hashtable seen = new Hashtable ();
289 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
290 return false;
293 ArrayList list = new ArrayList ();
295 foreach (TypeExpr iface_constraint in iface_constraints) {
296 foreach (Type type in list) {
297 if (!type.Equals (iface_constraint.Type))
298 continue;
300 Report.Error (405, loc,
301 "Duplicate constraint `{0}' for type " +
302 "parameter `{1}'.", iface_constraint.Type,
303 name);
304 return false;
307 list.Add (iface_constraint.Type);
310 foreach (TypeParameterExpr expr in type_param_constraints) {
311 foreach (Type type in list) {
312 if (!type.Equals (expr.Type))
313 continue;
315 Report.Error (405, loc,
316 "Duplicate constraint `{0}' for type " +
317 "parameter `{1}'.", expr.Type, name);
318 return false;
321 list.Add (expr.Type);
324 iface_constraint_types = new Type [list.Count];
325 list.CopyTo (iface_constraint_types, 0);
327 if (class_constraint != null) {
328 class_constraint_type = class_constraint.Type;
329 if (class_constraint_type == null)
330 return false;
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;
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;
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;
359 return true;
362 public bool CheckDependencies (EmitContext ec)
364 foreach (TypeParameterExpr expr in type_param_constraints) {
365 if (!CheckDependencies (expr.TypeParameter, ec))
366 return false;
369 return true;
372 bool CheckDependencies (TypeParameter tparam, EmitContext ec)
374 Constraints constraints = tparam.Constraints;
375 if (constraints == null)
376 return true;
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;
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;
392 if (!Convert.WideningReferenceConversionExists (ec, e1, t2) &&
393 !Convert.WideningReferenceConversionExists (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;
402 if (constraints.type_param_constraints == null)
403 return true;
405 foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
406 if (!CheckDependencies (expr.TypeParameter, ec))
407 return false;
410 return true;
413 public void Define (GenericTypeParameterBuilder type)
415 type.SetGenericParameterAttributes (attrs);
418 public override GenericParameterAttributes Attributes {
419 get { return attrs; }
422 public override bool HasClassConstraint {
423 get { return class_constraint != null; }
426 public override Type ClassConstraint {
427 get { return class_constraint_type; }
430 public override Type[] InterfaceConstraints {
431 get { return iface_constraint_types; }
434 public override Type EffectiveBaseClass {
435 get { return effective_base_type; }
438 internal bool IsSubclassOf (Type t)
440 if ((class_constraint_type != null) &&
441 class_constraint_type.IsSubclassOf (t))
442 return true;
444 if (iface_constraint_types == null)
445 return false;
447 foreach (Type iface in iface_constraint_types) {
448 if (TypeManager.IsSubclassOf (iface, t))
449 return true;
452 return false;
455 public bool CheckInterfaceMethod (EmitContext ec, GenericConstraints gc)
457 if (gc.Attributes != attrs)
458 return false;
460 if (HasClassConstraint != gc.HasClassConstraint)
461 return false;
462 if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
463 return false;
465 int gc_icount = gc.InterfaceConstraints != null ?
466 gc.InterfaceConstraints.Length : 0;
467 int icount = InterfaceConstraints != null ?
468 InterfaceConstraints.Length : 0;
470 if (gc_icount != icount)
471 return false;
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;
482 if (!ok)
483 return false;
486 return true;
491 // This type represents a generic type parameter
493 public class TypeParameter : MemberCore, IMemberContainer {
494 string name;
495 GenericConstraints gc;
496 Constraints constraints;
497 Location loc;
498 GenericTypeParameterBuilder type;
500 public TypeParameter (TypeContainer parent, string name,
501 Constraints constraints, Location loc)
502 : base (parent, new MemberName (name), null, loc)
504 this.name = name;
505 this.constraints = constraints;
506 this.loc = loc;
509 public GenericConstraints GenericConstraints {
510 get {
511 return gc != null ? gc : constraints;
515 public Constraints Constraints {
516 get {
517 return constraints;
521 public bool HasConstructorConstraint {
522 get {
523 if (constraints != null)
524 return constraints.HasConstructorConstraint;
526 return false;
530 public Type Type {
531 get {
532 return type;
536 public bool Resolve (DeclSpace ds)
538 if (constraints != null) {
539 if (!constraints.Resolve (ds.EmitContext)) {
540 constraints = null;
541 return false;
545 return true;
548 public void Define (GenericTypeParameterBuilder type)
550 if (this.type != null)
551 throw new InvalidOperationException ();
553 this.type = type;
554 TypeManager.AddTypeParameter (type, this);
557 public void DefineConstraints ()
559 if (constraints != null)
560 constraints.Define (type);
563 public bool ResolveType (EmitContext ec)
565 if (constraints != null) {
566 if (!constraints.ResolveTypes (ec)) {
567 constraints = null;
568 return false;
572 return true;
575 public bool DefineType (EmitContext ec)
577 return DefineType (ec, null, null, false);
580 public bool DefineType (EmitContext ec, MethodBuilder builder,
581 MethodInfo implementing, bool is_override)
583 if (!ResolveType (ec))
584 return false;
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;
596 MethodBase mb = implementing;
597 if (mb.Mono_IsInflatedMethod)
598 mb = mb.GetGenericMethodDefinition ();
600 int pos = type.GenericParameterPosition;
601 ParameterData pd = Invocation.GetParameterData (mb);
602 GenericConstraints temp_gc = pd.GenericConstraints (pos);
603 Type mparam = mb.GetGenericArguments () [pos];
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);
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;
621 if (!ok) {
622 Report.SymbolRelatedToPreviousError (implementing);
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;
634 } else {
635 gc = (GenericConstraints) constraints;
638 if (gc == null)
639 return true;
641 if (gc.HasClassConstraint)
642 type.SetBaseTypeConstraint (gc.ClassConstraint);
644 type.SetInterfaceConstraints (gc.InterfaceConstraints);
645 TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
647 return true;
650 public bool CheckDependencies (EmitContext ec)
652 if (constraints != null)
653 return constraints.CheckDependencies (ec);
655 return true;
658 public bool UpdateConstraints (EmitContext ec, Constraints new_constraints, bool check)
661 // We're used in partial generic type definitions.
662 // If `check' is false, we just encountered the first ClassPart which has
663 // constraints - they become our "real" constraints.
664 // Otherwise we're called after the type parameters have already been defined
665 // and check whether the constraints are the same in all parts.
667 if (!check) {
668 if (type != null)
669 throw new InvalidOperationException ();
670 constraints = new_constraints;
671 return true;
674 if (type == null)
675 throw new InvalidOperationException ();
677 if (constraints == null)
678 return new_constraints == null;
679 else if (new_constraints == null)
680 return false;
682 if (!new_constraints.Resolve (ec))
683 return false;
684 if (!new_constraints.ResolveTypes (ec))
685 return false;
687 return constraints.CheckInterfaceMethod (ec, new_constraints);
690 public override string DocCommentHeader {
691 get {
692 throw new InvalidOperationException (
693 "Unexpected attempt to get doc comment from " + this.GetType () + ".");
698 // MemberContainer
701 public override bool Define ()
703 return true;
706 protected override void VerifyObsoleteAttribute ()
709 public override void ApplyAttributeBuilder (Attribute a,
710 CustomAttributeBuilder cb)
713 public override AttributeTargets AttributeTargets {
714 get {
715 return (AttributeTargets) 0;
719 public override string[] ValidAttributeTargets {
720 get {
721 return new string [0];
726 // IMemberContainer
729 string IMemberContainer.Name {
730 get { return Name; }
733 MemberCache IMemberContainer.BaseCache {
734 get { return null; }
737 bool IMemberContainer.IsInterface {
738 get { return true; }
741 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
743 return FindMembers (mt, bf, null, null);
746 MemberCache IMemberContainer.MemberCache {
747 get { return null; }
750 public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
751 MemberFilter filter, object criteria)
753 if (constraints == null)
754 return MemberList.Empty;
756 ArrayList members = new ArrayList ();
758 GenericConstraints gc = (GenericConstraints) constraints;
760 if (gc.HasClassConstraint) {
761 MemberList list = TypeManager.FindMembers (
762 gc.ClassConstraint, mt, bf, filter, criteria);
764 members.AddRange (list);
767 foreach (Type t in gc.InterfaceConstraints) {
768 MemberList list = TypeManager.FindMembers (
769 t, mt, bf, filter, criteria);
771 members.AddRange (list);
774 return new MemberList (members);
777 public bool IsSubclassOf (Type t)
779 if (type.Equals (t))
780 return true;
782 if (constraints != null)
783 return constraints.IsSubclassOf (t);
785 return false;
788 public override string ToString ()
790 return "TypeParameter[" + name + "]";
793 protected class InflatedConstraints : GenericConstraints
795 GenericConstraints gc;
796 Type base_type;
797 Type class_constraint;
798 Type[] iface_constraints;
799 Type[] dargs;
800 Type declaring;
802 public InflatedConstraints (GenericConstraints gc, Type declaring)
804 this.gc = gc;
805 this.declaring = declaring;
807 dargs = TypeManager.GetTypeArguments (declaring);
809 ArrayList list = new ArrayList ();
810 if (gc.HasClassConstraint)
811 list.Add (inflate (gc.ClassConstraint));
812 foreach (Type iface in gc.InterfaceConstraints)
813 list.Add (inflate (iface));
815 bool has_class_constr = false;
816 if (list.Count > 0) {
817 Type first = (Type) list [0];
818 has_class_constr = !first.IsInterface && !first.IsGenericParameter;
821 if ((list.Count > 0) && has_class_constr) {
822 class_constraint = (Type) list [0];
823 iface_constraints = new Type [list.Count - 1];
824 list.CopyTo (1, iface_constraints, 0, list.Count - 1);
825 } else {
826 iface_constraints = new Type [list.Count];
827 list.CopyTo (iface_constraints, 0);
830 if (HasValueTypeConstraint)
831 base_type = TypeManager.value_type;
832 else if (class_constraint != null)
833 base_type = class_constraint;
834 else
835 base_type = TypeManager.object_type;
838 Type inflate (Type t)
840 if (t == null)
841 return null;
842 if (t.IsGenericParameter)
843 return dargs [t.GenericParameterPosition];
844 if (t.IsGenericInstance) {
845 t = t.GetGenericTypeDefinition ();
846 t = t.MakeGenericType (dargs);
849 return t;
852 public override GenericParameterAttributes Attributes {
853 get { return gc.Attributes; }
856 public override Type ClassConstraint {
857 get { return class_constraint; }
860 public override Type EffectiveBaseClass {
861 get { return base_type; }
864 public override Type[] InterfaceConstraints {
865 get { return iface_constraints; }
871 // This type represents a generic type parameter reference.
873 // These expressions are born in a fully resolved state.
875 public class TypeParameterExpr : TypeExpr {
876 TypeParameter type_parameter;
878 public override string Name {
879 get {
880 return type_parameter.Name;
884 public override string FullName {
885 get {
886 return type_parameter.Name;
890 public TypeParameter TypeParameter {
891 get {
892 return type_parameter;
896 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
898 this.type_parameter = type_parameter;
899 this.loc = loc;
902 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
904 type = type_parameter.Type;
906 return this;
909 public override bool IsInterface {
910 get { return false; }
913 public override bool CheckAccessLevel (DeclSpace ds)
915 return true;
918 public void Error_CannotUseAsUnmanagedType (Location loc)
920 Report.Error (-203, loc, "Can not use type parameter as unamanged type");
924 public class TypeArguments {
925 public readonly Location Location;
926 ArrayList args;
927 Type[] atypes;
928 int dimension;
929 bool has_type_args;
930 bool created;
932 public TypeArguments (Location loc)
934 args = new ArrayList ();
935 this.Location = loc;
938 public TypeArguments (int dimension, Location loc)
940 this.dimension = dimension;
941 this.Location = loc;
944 public void Add (Expression type)
946 if (created)
947 throw new InvalidOperationException ();
949 args.Add (type);
952 public void Add (TypeArguments new_args)
954 if (created)
955 throw new InvalidOperationException ();
957 args.AddRange (new_args.args);
960 public string[] GetDeclarations ()
962 string[] ret = new string [args.Count];
963 for (int i = 0; i < args.Count; i++) {
964 SimpleName sn = args [i] as SimpleName;
965 if (sn != null) {
966 ret [i] = sn.Name;
967 continue;
970 Report.Error (81, Location, "Type parameter declaration " +
971 "must be an identifier not a type");
972 return null;
974 return ret;
977 public Type[] Arguments {
978 get {
979 return atypes;
983 public bool HasTypeArguments {
984 get {
985 return has_type_args;
989 public int Count {
990 get {
991 if (dimension > 0)
992 return dimension;
993 else
994 return args.Count;
998 public bool IsUnbound {
999 get {
1000 return dimension > 0;
1004 public override string ToString ()
1006 StringBuilder s = new StringBuilder ();
1008 int count = Count;
1009 for (int i = 0; i < count; i++){
1011 // FIXME: Use TypeManager.CSharpname once we have the type
1013 if (args != null)
1014 s.Append (args [i].ToString ());
1015 if (i+1 < count)
1016 s.Append (",");
1018 return s.ToString ();
1021 public bool Resolve (EmitContext ec)
1023 int count = args.Count;
1024 bool ok = true;
1026 atypes = new Type [count];
1028 for (int i = 0; i < count; i++){
1029 TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec);
1030 if (te == null) {
1031 ok = false;
1032 continue;
1034 if (te is TypeParameterExpr)
1035 has_type_args = true;
1037 if (te.Type.IsPointer) {
1038 Report.Error (306, Location, "The type `{0}' may not be used " +
1039 "as a type argument.", TypeManager.CSharpName (te.Type));
1040 return false;
1043 atypes [i] = te.Type;
1045 return ok;
1049 public class ConstructedType : TypeExpr {
1050 string name, full_name;
1051 TypeArguments args;
1052 Type[] gen_params, atypes;
1053 Type gt;
1055 public ConstructedType (string name, TypeArguments args, Location l)
1057 loc = l;
1058 this.name = MemberName.MakeName (name, args.Count);
1059 this.args = args;
1061 eclass = ExprClass.Type;
1062 full_name = name + "<" + args.ToString () + ">";
1065 public ConstructedType (string name, TypeParameter[] type_params, Location l)
1066 : this (type_params, l)
1068 loc = l;
1070 this.name = name;
1071 full_name = name + "<" + args.ToString () + ">";
1074 public ConstructedType (FullNamedExpression fname, TypeArguments args, Location l)
1076 loc = l;
1077 this.name = fname.FullName;
1078 this.args = args;
1080 eclass = ExprClass.Type;
1081 full_name = name + "<" + args.ToString () + ">";
1084 protected ConstructedType (TypeArguments args, Location l)
1086 loc = l;
1087 this.args = args;
1089 eclass = ExprClass.Type;
1092 protected ConstructedType (TypeParameter[] type_params, Location l)
1094 loc = l;
1096 args = new TypeArguments (l);
1097 foreach (TypeParameter type_param in type_params)
1098 args.Add (new TypeParameterExpr (type_param, l));
1100 eclass = ExprClass.Type;
1103 public ConstructedType (Type t, TypeParameter[] type_params, Location l)
1104 : this (type_params, l)
1106 gt = t.GetGenericTypeDefinition ();
1108 this.name = gt.FullName;
1109 full_name = gt.FullName + "<" + args.ToString () + ">";
1112 public ConstructedType (Type t, TypeArguments args, Location l)
1113 : this (args, l)
1115 gt = t.GetGenericTypeDefinition ();
1117 this.name = gt.FullName;
1118 full_name = gt.FullName + "<" + args.ToString () + ">";
1121 public TypeArguments TypeArguments {
1122 get { return args; }
1125 protected string DeclarationName {
1126 get {
1127 StringBuilder sb = new StringBuilder ();
1128 sb.Append (gt.FullName);
1129 sb.Append ("<");
1130 for (int i = 0; i < gen_params.Length; i++) {
1131 if (i > 0)
1132 sb.Append (",");
1133 sb.Append (gen_params [i]);
1135 sb.Append (">");
1136 return sb.ToString ();
1140 protected bool CheckConstraint (EmitContext ec, Type ptype, Expression expr,
1141 Type ctype)
1143 if (TypeManager.HasGenericArguments (ctype)) {
1144 Type[] types = TypeManager.GetTypeArguments (ctype);
1146 TypeArguments new_args = new TypeArguments (loc);
1148 for (int i = 0; i < types.Length; i++) {
1149 Type t = types [i];
1151 if (t.IsGenericParameter) {
1152 int pos = t.GenericParameterPosition;
1153 t = args.Arguments [pos];
1155 new_args.Add (new TypeExpression (t, loc));
1158 TypeExpr ct = new ConstructedType (ctype, new_args, loc);
1159 if (ct.ResolveAsTypeTerminal (ec) == null)
1160 return false;
1161 ctype = ct.Type;
1164 return Convert.WideningStandardConversionExists (ec, expr, ctype);
1167 protected bool CheckConstraints (EmitContext ec, int index)
1169 Type atype = atypes [index];
1170 Type ptype = gen_params [index];
1172 if (atype == ptype)
1173 return true;
1175 Expression aexpr = new EmptyExpression (atype);
1177 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1178 if (gc == null)
1179 return true;
1181 bool is_class, is_struct;
1182 if (atype.IsGenericParameter) {
1183 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1184 if (agc != null) {
1185 is_class = agc.HasReferenceTypeConstraint;
1186 is_struct = agc.HasValueTypeConstraint;
1187 } else {
1188 is_class = is_struct = false;
1190 } else {
1191 is_class = atype.IsClass;
1192 is_struct = atype.IsValueType;
1196 // First, check the `class' and `struct' constraints.
1198 if (gc.HasReferenceTypeConstraint && !is_class) {
1199 Report.Error (452, loc, "The type `{0}' must be " +
1200 "a reference type in order to use it " +
1201 "as type parameter `{1}' in the " +
1202 "generic type or method `{2}'.",
1203 atype, ptype, DeclarationName);
1204 return false;
1205 } else if (gc.HasValueTypeConstraint && !is_struct) {
1206 Report.Error (453, loc, "The type `{0}' must be " +
1207 "a value type in order to use it " +
1208 "as type parameter `{1}' in the " +
1209 "generic type or method `{2}'.",
1210 atype, ptype, DeclarationName);
1211 return false;
1215 // The class constraint comes next.
1217 if (gc.HasClassConstraint) {
1218 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint)) {
1219 Report.Error (309, loc, "The type `{0}' must be " +
1220 "convertible to `{1}' in order to " +
1221 "use it as parameter `{2}' in the " +
1222 "generic type or method `{3}'",
1223 atype, gc.ClassConstraint, ptype, DeclarationName);
1224 return false;
1229 // Now, check the interface constraints.
1231 foreach (Type it in gc.InterfaceConstraints) {
1232 Type itype;
1233 if (it.IsGenericParameter)
1234 itype = atypes [it.GenericParameterPosition];
1235 else
1236 itype = it;
1238 if (!CheckConstraint (ec, ptype, aexpr, itype)) {
1239 Report.Error (309, loc, "The type `{0}' must be " +
1240 "convertible to `{1}' in order to " +
1241 "use it as parameter `{2}' in the " +
1242 "generic type or method `{3}'",
1243 atype, itype, ptype, DeclarationName);
1244 return false;
1249 // Finally, check the constructor constraint.
1252 if (!gc.HasConstructorConstraint)
1253 return true;
1255 if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
1256 return true;
1258 MethodGroupExpr mg = Expression.MemberLookup (
1259 ec, atype, ".ctor", MemberTypes.Constructor,
1260 BindingFlags.Public | BindingFlags.Instance |
1261 BindingFlags.DeclaredOnly, loc)
1262 as MethodGroupExpr;
1264 if (atype.IsAbstract || (mg == null) || !mg.IsInstance) {
1265 Report.Error (310, loc, "The type `{0}' must have a public " +
1266 "parameterless constructor in order to use it " +
1267 "as parameter `{1}' in the generic type or " +
1268 "method `{2}'", atype, ptype, DeclarationName);
1269 return false;
1272 return true;
1275 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1277 if (!ResolveConstructedType (ec))
1278 return null;
1280 return this;
1283 public bool CheckConstraints (EmitContext ec)
1285 for (int i = 0; i < gen_params.Length; i++) {
1286 if (!CheckConstraints (ec, i))
1287 return false;
1290 return true;
1293 public override TypeExpr ResolveAsTypeTerminal (EmitContext ec)
1295 if (base.ResolveAsTypeTerminal (ec) == null)
1296 return null;
1298 if (!CheckConstraints (ec))
1299 return null;
1301 return this;
1304 public bool ResolveConstructedType (EmitContext ec)
1306 if (type != null)
1307 return true;
1308 if (gt != null)
1309 return DoResolveType (ec);
1312 // First, resolve the generic type.
1314 DeclSpace ds;
1315 Type nested = ec.DeclSpace.FindNestedType (loc, name, out ds);
1316 if (nested != null) {
1317 gt = nested.GetGenericTypeDefinition ();
1319 TypeArguments new_args = new TypeArguments (loc);
1320 if (ds.IsGeneric) {
1321 foreach (TypeParameter param in ds.TypeParameters)
1322 new_args.Add (new TypeParameterExpr (param, loc));
1324 new_args.Add (args);
1326 args = new_args;
1327 return DoResolveType (ec);
1330 Type t;
1331 int num_args;
1333 SimpleName sn = new SimpleName (name, loc);
1334 TypeExpr resolved = sn.ResolveAsTypeTerminal (ec);
1335 if (resolved == null)
1336 return false;
1338 t = resolved.Type;
1339 if (t == null) {
1340 Report.Error (246, loc, "Cannot find type `{0}'<...>",
1341 Basename);
1342 return false;
1345 num_args = TypeManager.GetNumberOfTypeArguments (t);
1346 if (num_args == 0) {
1347 Report.Error (308, loc,
1348 "The non-generic type `{0}' cannot " +
1349 "be used with type arguments.",
1350 TypeManager.CSharpName (t));
1351 return false;
1354 gt = t.GetGenericTypeDefinition ();
1355 return DoResolveType (ec);
1358 bool DoResolveType (EmitContext ec)
1361 // Resolve the arguments.
1363 if (args.Resolve (ec) == false)
1364 return false;
1366 gen_params = gt.GetGenericArguments ();
1367 atypes = args.Arguments;
1369 if (atypes.Length != gen_params.Length) {
1370 Report.Error (305, loc,
1371 "Using the generic type `{0}' " +
1372 "requires {1} type arguments",
1373 TypeManager.GetFullName (gt),
1374 gen_params.Length);
1375 return false;
1379 // Now bind the parameters.
1381 type = gt.MakeGenericType (atypes);
1382 return true;
1385 public Expression GetSimpleName (EmitContext ec)
1387 return new SimpleName (Basename, args, loc);
1390 public override bool CheckAccessLevel (DeclSpace ds)
1392 return ds.CheckAccessLevel (gt);
1395 public override bool AsAccessible (DeclSpace ds, int flags)
1397 return ds.AsAccessible (gt, flags);
1400 public override bool IsClass {
1401 get { return gt.IsClass; }
1404 public override bool IsValueType {
1405 get { return gt.IsValueType; }
1408 public override bool IsInterface {
1409 get { return gt.IsInterface; }
1412 public override bool IsSealed {
1413 get { return gt.IsSealed; }
1416 public override bool IsAttribute {
1417 get { return false; }
1420 public override bool Equals (object obj)
1422 ConstructedType cobj = obj as ConstructedType;
1423 if (cobj == null)
1424 return false;
1426 if ((type == null) || (cobj.type == null))
1427 return false;
1429 return type == cobj.type;
1432 public override int GetHashCode ()
1434 return base.GetHashCode ();
1437 public string Basename {
1438 get {
1439 int pos = name.LastIndexOf ('`');
1440 if (pos >= 0)
1441 return name.Substring (0, pos);
1442 else
1443 return name;
1447 public override string Name {
1448 get {
1449 return full_name;
1454 public override string FullName {
1455 get {
1456 return full_name;
1461 public class GenericMethod : DeclSpace
1463 public GenericMethod (NamespaceEntry ns, TypeContainer parent,
1464 MemberName name, Location l)
1465 : base (ns, parent, name, null, l)
1468 public override TypeBuilder DefineType ()
1470 throw new Exception ();
1473 public override bool Define ()
1475 for (int i = 0; i < TypeParameters.Length; i++)
1476 if (!TypeParameters [i].Resolve (Parent))
1477 return false;
1479 return true;
1482 public bool Define (MethodBuilder mb, Type return_type)
1484 if (!Define ())
1485 return false;
1487 GenericTypeParameterBuilder[] gen_params;
1488 string[] names = MemberName.TypeArguments.GetDeclarations ();
1489 gen_params = mb.DefineGenericParameters (names);
1490 for (int i = 0; i < TypeParameters.Length; i++)
1491 TypeParameters [i].Define (gen_params [i]);
1493 ec = new EmitContext (
1494 this, this, Location, null, return_type, ModFlags, false);
1496 for (int i = 0; i < TypeParameters.Length; i++) {
1497 if (!TypeParameters [i].ResolveType (ec))
1498 return false;
1501 return true;
1504 public bool DefineType (EmitContext ec, MethodBuilder mb,
1505 MethodInfo implementing, bool is_override)
1507 for (int i = 0; i < TypeParameters.Length; i++)
1508 if (!TypeParameters [i].DefineType (
1509 ec, mb, implementing, is_override))
1510 return false;
1512 return true;
1515 public override bool DefineMembers (TypeContainer parent)
1517 return true;
1520 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1521 MemberFilter filter, object criteria)
1523 throw new Exception ();
1526 public override MemberCache MemberCache {
1527 get {
1528 return null;
1532 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1534 // FIXME
1537 protected override void VerifyObsoleteAttribute()
1539 // FIXME
1542 public override AttributeTargets AttributeTargets {
1543 get {
1544 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1548 public override string DocCommentHeader {
1549 get { return "M:"; }
1553 public class DefaultValueExpression : Expression
1555 Expression expr;
1556 LocalTemporary temp_storage;
1558 public DefaultValueExpression (Expression expr, Location loc)
1560 this.expr = expr;
1561 this.loc = loc;
1564 public override Expression DoResolve (EmitContext ec)
1566 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec);
1567 if (texpr == null)
1568 return null;
1570 type = texpr.Type;
1571 if (type.IsGenericParameter || TypeManager.IsValueType (type))
1572 temp_storage = new LocalTemporary (ec, type);
1574 eclass = ExprClass.Variable;
1575 return this;
1578 public override void Emit (EmitContext ec)
1580 if (temp_storage != null) {
1581 temp_storage.AddressOf (ec, AddressOp.LoadStore);
1582 ec.ig.Emit (OpCodes.Initobj, type);
1583 temp_storage.Emit (ec);
1584 } else
1585 ec.ig.Emit (OpCodes.Ldnull);
1589 public class NullableType : TypeExpr
1591 Expression underlying;
1593 public NullableType (Expression underlying, Location l)
1595 this.underlying = underlying;
1596 loc = l;
1598 eclass = ExprClass.Type;
1601 public NullableType (Type type, Location loc)
1602 : this (new TypeExpression (type, loc), loc)
1605 public override string Name {
1606 get { return underlying.ToString () + "?"; }
1609 public override string FullName {
1610 get { return underlying.ToString () + "?"; }
1613 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1615 TypeArguments args = new TypeArguments (loc);
1616 args.Add (underlying);
1618 ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
1619 return ctype.ResolveAsTypeTerminal (ec);
1623 public partial class TypeManager
1626 // A list of core types that the compiler requires or uses
1628 static public Type new_constraint_attr_type;
1629 static public Type activator_type;
1630 static public Type generic_ienumerator_type;
1631 static public Type generic_ienumerable_type;
1632 static public Type generic_nullable_type;
1634 // <remarks>
1635 // Tracks the generic parameters.
1636 // </remarks>
1637 static PtrHashtable builder_to_type_param;
1640 // These methods are called by code generated by the compiler
1642 static public MethodInfo activator_create_instance;
1644 static void InitGenerics ()
1646 builder_to_type_param = new PtrHashtable ();
1649 static void CleanUpGenerics ()
1651 builder_to_type_param = null;
1654 static void InitGenericCoreTypes ()
1656 activator_type = CoreLookupType ("System.Activator");
1657 new_constraint_attr_type = CoreLookupType (
1658 "System.Runtime.CompilerServices.NewConstraintAttribute");
1660 generic_ienumerator_type = CoreLookupType ("System.Collections.Generic.IEnumerator", 1);
1661 generic_ienumerable_type = CoreLookupType ("System.Collections.Generic.IEnumerable", 1);
1662 generic_nullable_type = CoreLookupType ("System.Nullable", 1);
1665 static void InitGenericCodeHelpers ()
1667 // Activator
1668 Type [] type_arg = { type_type };
1669 activator_create_instance = GetMethod (
1670 activator_type, "CreateInstance", type_arg);
1673 static Type CoreLookupType (string name, int arity)
1675 return CoreLookupType (MemberName.MakeName (name, arity));
1678 public static void AddTypeParameter (Type t, TypeParameter tparam)
1680 if (!builder_to_type_param.Contains (t))
1681 builder_to_type_param.Add (t, tparam);
1684 public static TypeContainer LookupGenericTypeContainer (Type t)
1686 while (t.IsGenericInstance)
1687 t = t.GetGenericTypeDefinition ();
1689 return LookupTypeContainer (t);
1692 public static TypeParameter LookupTypeParameter (Type t)
1694 return (TypeParameter) builder_to_type_param [t];
1697 public static bool HasConstructorConstraint (Type t)
1699 GenericConstraints gc = GetTypeParameterConstraints (t);
1700 if (gc == null)
1701 return false;
1703 return (gc.Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
1706 public static GenericConstraints GetTypeParameterConstraints (Type t)
1708 if (!t.IsGenericParameter)
1709 throw new InvalidOperationException ();
1711 TypeParameter tparam = LookupTypeParameter (t);
1712 if (tparam != null)
1713 return tparam.GenericConstraints;
1715 return new ReflectionConstraints (t);
1718 public static bool IsGeneric (Type t)
1720 DeclSpace ds = (DeclSpace) builder_to_declspace [t];
1722 return ds.IsGeneric;
1725 public static bool HasGenericArguments (Type t)
1727 return GetNumberOfTypeArguments (t) > 0;
1730 public static int GetNumberOfTypeArguments (Type t)
1732 DeclSpace tc = LookupDeclSpace (t);
1733 if (tc != null)
1734 return tc.IsGeneric ? tc.CountTypeParameters : 0;
1735 else
1736 return t.IsGenericType ? t.GetGenericArguments ().Length : 0;
1739 public static Type[] GetTypeArguments (Type t)
1741 DeclSpace tc = LookupDeclSpace (t);
1742 if (tc != null) {
1743 if (!tc.IsGeneric)
1744 return Type.EmptyTypes;
1746 TypeParameter[] tparam = tc.TypeParameters;
1747 Type[] ret = new Type [tparam.Length];
1748 for (int i = 0; i < tparam.Length; i++) {
1749 ret [i] = tparam [i].Type;
1750 if (ret [i] == null)
1751 throw new InternalErrorException ();
1754 return ret;
1755 } else
1756 return t.GetGenericArguments ();
1760 // Whether `array' is an array of T and `enumerator' is `IEnumerable<T>'.
1761 // For instance "string[]" -> "IEnumerable<string>".
1763 public static bool IsIEnumerable (Type array, Type enumerator)
1765 if (!array.IsArray || !enumerator.IsGenericInstance)
1766 return false;
1768 if (enumerator.GetGenericTypeDefinition () != generic_ienumerable_type)
1769 return false;
1771 Type[] args = GetTypeArguments (enumerator);
1772 return args [0] == GetElementType (array);
1775 public static bool IsEqual (Type a, Type b)
1777 if (a.Equals (b))
1778 return true;
1780 if ((a is TypeBuilder) && a.IsGenericTypeDefinition && b.IsGenericInstance) {
1782 // `a' is a generic type definition's TypeBuilder and `b' is a
1783 // generic instance of the same type.
1785 // Example:
1787 // class Stack<T>
1788 // {
1789 // void Test (Stack<T> stack) { }
1790 // }
1792 // The first argument of `Test' will be the generic instance
1793 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1796 // We hit this via Closure.Filter() for gen-82.cs.
1798 if (a != b.GetGenericTypeDefinition ())
1799 return false;
1801 Type[] aparams = a.GetGenericArguments ();
1802 Type[] bparams = b.GetGenericArguments ();
1804 if (aparams.Length != bparams.Length)
1805 return false;
1807 for (int i = 0; i < aparams.Length; i++)
1808 if (!IsEqual (aparams [i], bparams [i]))
1809 return false;
1811 return true;
1814 if ((b is TypeBuilder) && b.IsGenericTypeDefinition && a.IsGenericInstance)
1815 return IsEqual (b, a);
1817 if (a.IsGenericParameter && b.IsGenericParameter) {
1818 if ((a.DeclaringMethod == null) || (b.DeclaringMethod == null))
1819 return false;
1820 return a.GenericParameterPosition == b.GenericParameterPosition;
1823 if (a.IsArray && b.IsArray) {
1824 if (a.GetArrayRank () != b.GetArrayRank ())
1825 return false;
1826 return IsEqual (a.GetElementType (), b.GetElementType ());
1829 if (a.IsGenericInstance && b.IsGenericInstance) {
1830 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1831 return false;
1833 Type[] aargs = a.GetGenericArguments ();
1834 Type[] bargs = b.GetGenericArguments ();
1836 if (aargs.Length != bargs.Length)
1837 return false;
1839 for (int i = 0; i < aargs.Length; i++) {
1840 if (!IsEqual (aargs [i], bargs [i]))
1841 return false;
1844 return true;
1847 return false;
1850 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_infered, Type[] method_infered)
1852 if (a.IsGenericParameter) {
1854 // If a is an array of a's type, they may never
1855 // become equal.
1857 while (b.IsArray) {
1858 b = b.GetElementType ();
1859 if (a.Equals (b))
1860 return false;
1864 // If b is a generic parameter or an actual type,
1865 // they may become equal:
1867 // class X<T,U> : I<T>, I<U>
1868 // class X<T> : I<T>, I<float>
1870 if (b.IsGenericParameter || !b.IsGenericInstance) {
1871 int pos = a.GenericParameterPosition;
1872 Type[] args = a.DeclaringMethod != null ? method_infered : class_infered;
1873 if (args [pos] == null) {
1874 args [pos] = b;
1875 return true;
1878 return args [pos] == a;
1882 // We're now comparing a type parameter with a
1883 // generic instance. They may become equal unless
1884 // the type parameter appears anywhere in the
1885 // generic instance:
1887 // class X<T,U> : I<T>, I<X<U>>
1888 // -> error because you could instanciate it as
1889 // X<X<int>,int>
1891 // class X<T> : I<T>, I<X<T>> -> ok
1894 Type[] bargs = GetTypeArguments (b);
1895 for (int i = 0; i < bargs.Length; i++) {
1896 if (a.Equals (bargs [i]))
1897 return false;
1900 return true;
1903 if (b.IsGenericParameter)
1904 return MayBecomeEqualGenericTypes (b, a, class_infered, method_infered);
1907 // At this point, neither a nor b are a type parameter.
1909 // If one of them is a generic instance, let
1910 // MayBecomeEqualGenericInstances() compare them (if the
1911 // other one is not a generic instance, they can never
1912 // become equal).
1915 if (a.IsGenericInstance || b.IsGenericInstance)
1916 return MayBecomeEqualGenericInstances (a, b, class_infered, method_infered);
1919 // If both of them are arrays.
1922 if (a.IsArray && b.IsArray) {
1923 if (a.GetArrayRank () != b.GetArrayRank ())
1924 return false;
1926 a = a.GetElementType ();
1927 b = b.GetElementType ();
1929 return MayBecomeEqualGenericTypes (a, b, class_infered, method_infered);
1933 // Ok, two ordinary types.
1936 return a.Equals (b);
1940 // Checks whether two generic instances may become equal for some
1941 // particular instantiation (26.3.1).
1943 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1944 Type[] class_infered, Type[] method_infered)
1946 if (!a.IsGenericInstance || !b.IsGenericInstance)
1947 return false;
1948 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1949 return false;
1951 return MayBecomeEqualGenericInstances (
1952 GetTypeArguments (a), GetTypeArguments (b), class_infered, method_infered);
1955 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1956 Type[] class_infered, Type[] method_infered)
1958 if (aargs.Length != bargs.Length)
1959 return false;
1961 for (int i = 0; i < aargs.Length; i++) {
1962 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_infered, method_infered))
1963 return false;
1966 return true;
1969 public static bool IsEqualGenericInstance (Type type, Type parent)
1971 int tcount = GetNumberOfTypeArguments (type);
1972 int pcount = GetNumberOfTypeArguments (parent);
1974 if (type.IsGenericInstance)
1975 type = type.GetGenericTypeDefinition ();
1976 if (parent.IsGenericInstance)
1977 parent = parent.GetGenericTypeDefinition ();
1979 if (tcount != pcount)
1980 return false;
1982 return type.Equals (parent);
1985 static public bool IsGenericMethod (MethodBase mb)
1987 if (mb.DeclaringType is TypeBuilder) {
1988 IMethodData method = (IMethodData) builder_to_method [mb];
1989 if (method == null)
1990 return false;
1992 return method.GenericMethod != null;
1995 return mb.IsGenericMethodDefinition;
1999 // Type inference.
2002 static bool InferType (Type pt, Type at, Type[] infered)
2004 if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
2005 int pos = pt.GenericParameterPosition;
2007 if (infered [pos] == null) {
2008 Type check = at;
2009 while (check.IsArray)
2010 check = check.GetElementType ();
2012 if (pt == check)
2013 return false;
2015 infered [pos] = at;
2016 return true;
2019 if (infered [pos] != at)
2020 return false;
2022 return true;
2025 if (!pt.ContainsGenericParameters) {
2026 if (at.ContainsGenericParameters)
2027 return InferType (at, pt, infered);
2028 else
2029 return true;
2032 if (at.IsArray) {
2033 if (!pt.IsArray ||
2034 (at.GetArrayRank () != pt.GetArrayRank ()))
2035 return false;
2037 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2040 if (pt.IsArray) {
2041 if (!at.IsArray ||
2042 (pt.GetArrayRank () != at.GetArrayRank ()))
2043 return false;
2045 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2048 if (pt.IsByRef && at.IsByRef)
2049 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2050 ArrayList list = new ArrayList ();
2051 if (at.IsGenericInstance)
2052 list.Add (at);
2053 else {
2054 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
2055 list.Add (bt);
2057 list.AddRange (TypeManager.GetInterfaces (at));
2060 bool found_one = false;
2062 foreach (Type type in list) {
2063 if (!type.IsGenericInstance)
2064 continue;
2066 Type[] infered_types = new Type [infered.Length];
2068 if (!InferGenericInstance (pt, type, infered_types))
2069 continue;
2071 for (int i = 0; i < infered_types.Length; i++) {
2072 if (infered [i] == null) {
2073 infered [i] = infered_types [i];
2074 continue;
2077 if (infered [i] != infered_types [i])
2078 return false;
2081 found_one = true;
2084 return found_one;
2087 static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
2089 Type[] at_args = at.GetGenericArguments ();
2090 Type[] pt_args = pt.GetGenericArguments ();
2092 if (at_args.Length != pt_args.Length)
2093 return false;
2095 for (int i = 0; i < at_args.Length; i++) {
2096 if (!InferType (pt_args [i], at_args [i], infered_types))
2097 return false;
2100 for (int i = 0; i < infered_types.Length; i++) {
2101 if (infered_types [i] == null)
2102 return false;
2105 return true;
2108 public static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
2109 ref MethodBase method)
2111 if ((arguments == null) || !TypeManager.IsGenericMethod (method))
2112 return true;
2114 int arg_count;
2116 if (arguments == null)
2117 arg_count = 0;
2118 else
2119 arg_count = arguments.Count;
2121 ParameterData pd = Invocation.GetParameterData (method);
2123 int pd_count = pd.Count;
2125 if (pd_count == 0)
2126 return false;
2128 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2129 return false;
2131 if (pd_count - 1 > arg_count)
2132 return false;
2134 if (pd_count == 1 && arg_count == 0)
2135 return true;
2137 Type[] method_args = method.GetGenericArguments ();
2138 Type[] infered_types = new Type [method_args.Length];
2141 // If we have come this far, the case which
2142 // remains is when the number of parameters is
2143 // less than or equal to the argument count.
2145 for (int i = 0; i < pd_count - 1; ++i) {
2146 Argument a = (Argument) arguments [i];
2148 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2149 continue;
2151 Type pt = pd.ParameterType (i);
2152 Type at = a.Type;
2154 if (!InferType (pt, at, infered_types))
2155 return false;
2158 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
2160 for (int i = pd_count - 1; i < arg_count; i++) {
2161 Argument a = (Argument) arguments [i];
2163 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2164 continue;
2166 if (!InferType (element_type, a.Type, infered_types))
2167 return false;
2170 for (int i = 0; i < infered_types.Length; i++)
2171 if (infered_types [i] == null)
2172 return false;
2174 method = ((MethodInfo)method).MakeGenericMethod (infered_types);
2175 return true;
2178 public static bool InferTypeArguments (Type[] param_types, Type[] arg_types, Type[] infered_types)
2180 if (infered_types == null)
2181 return false;
2183 for (int i = 0; i < arg_types.Length; i++) {
2184 if (arg_types [i] == null)
2185 continue;
2187 if (!InferType (param_types [i], arg_types [i], infered_types))
2188 return false;
2191 for (int i = 0; i < infered_types.Length; i++)
2192 if (infered_types [i] == null)
2193 return false;
2195 return true;
2198 public static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
2199 ref MethodBase method)
2201 if (!TypeManager.IsGenericMethod (method))
2202 return true;
2204 int arg_count;
2205 if (arguments != null)
2206 arg_count = arguments.Count;
2207 else
2208 arg_count = 0;
2210 ParameterData pd = Invocation.GetParameterData (method);
2211 if (arg_count != pd.Count)
2212 return false;
2214 Type[] method_args = method.GetGenericArguments ();
2216 bool is_open = false;
2217 for (int i = 0; i < method_args.Length; i++) {
2218 if (method_args [i].IsGenericParameter) {
2219 is_open = true;
2220 break;
2223 if (!is_open)
2224 return true;
2226 Type[] infered_types = new Type [method_args.Length];
2228 Type[] param_types = new Type [pd.Count];
2229 Type[] arg_types = new Type [pd.Count];
2231 for (int i = 0; i < arg_count; i++) {
2232 param_types [i] = pd.ParameterType (i);
2234 Argument a = (Argument) arguments [i];
2235 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2236 continue;
2238 arg_types [i] = a.Type;
2241 if (!InferTypeArguments (param_types, arg_types, infered_types))
2242 return false;
2244 method = ((MethodInfo)method).MakeGenericMethod (infered_types);
2245 return true;
2248 public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
2249 ref MethodBase method)
2251 if (!TypeManager.IsGenericMethod (method))
2252 return true;
2254 ParameterData pd = Invocation.GetParameterData (method);
2255 if (apd.Count != pd.Count)
2256 return false;
2258 Type[] method_args = method.GetGenericArguments ();
2259 Type[] infered_types = new Type [method_args.Length];
2261 Type[] param_types = new Type [pd.Count];
2262 Type[] arg_types = new Type [pd.Count];
2264 for (int i = 0; i < apd.Count; i++) {
2265 param_types [i] = pd.ParameterType (i);
2266 arg_types [i] = apd.ParameterType (i);
2269 if (!InferTypeArguments (param_types, arg_types, infered_types))
2270 return false;
2272 method = ((MethodInfo)method).MakeGenericMethod (infered_types);
2273 return true;
2276 public static bool IsNullableType (Type t)
2278 if (!t.IsGenericInstance)
2279 return false;
2281 Type gt = t.GetGenericTypeDefinition ();
2282 return gt == generic_nullable_type;
2286 public abstract class Nullable
2288 protected sealed class NullableInfo
2290 public readonly Type Type;
2291 public readonly Type UnderlyingType;
2292 public readonly MethodInfo HasValue;
2293 public readonly MethodInfo Value;
2294 public readonly ConstructorInfo Constructor;
2296 public NullableInfo (Type type)
2298 Type = type;
2299 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
2301 PropertyInfo has_value_pi = type.GetProperty ("HasValue");
2302 PropertyInfo value_pi = type.GetProperty ("Value");
2304 HasValue = has_value_pi.GetGetMethod (false);
2305 Value = value_pi.GetGetMethod (false);
2306 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
2310 protected class Unwrap : Expression, IMemoryLocation, IAssignMethod
2312 Expression expr;
2313 NullableInfo info;
2315 LocalTemporary temp;
2316 bool has_temp;
2318 public Unwrap (Expression expr, Location loc)
2320 this.expr = expr;
2321 this.loc = loc;
2324 public override Expression DoResolve (EmitContext ec)
2326 expr = expr.Resolve (ec);
2327 if (expr == null)
2328 return null;
2330 if (!(expr is IMemoryLocation))
2331 temp = new LocalTemporary (ec, expr.Type);
2333 info = new NullableInfo (expr.Type);
2334 type = info.UnderlyingType;
2335 eclass = expr.eclass;
2336 return this;
2339 public override void Emit (EmitContext ec)
2341 AddressOf (ec, AddressOp.LoadStore);
2342 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
2345 public void EmitCheck (EmitContext ec)
2347 AddressOf (ec, AddressOp.LoadStore);
2348 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
2351 void create_temp (EmitContext ec)
2353 if ((temp != null) && !has_temp) {
2354 expr.Emit (ec);
2355 temp.Store (ec);
2356 has_temp = true;
2360 public void AddressOf (EmitContext ec, AddressOp mode)
2362 create_temp (ec);
2363 if (temp != null)
2364 temp.AddressOf (ec, AddressOp.LoadStore);
2365 else
2366 ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
2369 public void Emit (EmitContext ec, bool leave_copy)
2371 create_temp (ec);
2372 if (leave_copy) {
2373 if (temp != null)
2374 temp.Emit (ec);
2375 else
2376 expr.Emit (ec);
2379 Emit (ec);
2382 public void EmitAssign (EmitContext ec, Expression source,
2383 bool leave_copy, bool prepare_for_load)
2385 source.Emit (ec);
2386 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2388 if (leave_copy)
2389 ec.ig.Emit (OpCodes.Dup);
2391 Expression empty = new EmptyExpression (expr.Type);
2392 ((IAssignMethod) expr).EmitAssign (ec, empty, false, prepare_for_load);
2396 protected class Wrap : Expression
2398 Expression expr;
2399 NullableInfo info;
2401 public Wrap (Expression expr, Location loc)
2403 this.expr = expr;
2404 this.loc = loc;
2407 public override Expression DoResolve (EmitContext ec)
2409 expr = expr.Resolve (ec);
2410 if (expr == null)
2411 return null;
2413 TypeExpr target_type = new NullableType (expr.Type, loc);
2414 target_type = target_type.ResolveAsTypeTerminal (ec);
2415 if (target_type == null)
2416 return null;
2418 type = target_type.Type;
2419 info = new NullableInfo (type);
2420 eclass = ExprClass.Value;
2421 return this;
2424 public override void Emit (EmitContext ec)
2426 expr.Emit (ec);
2427 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2431 public class NullableLiteral : Expression, IMemoryLocation {
2432 public NullableLiteral (Type target_type, Location loc)
2434 this.type = target_type;
2435 this.loc = loc;
2437 eclass = ExprClass.Value;
2440 public override Expression DoResolve (EmitContext ec)
2442 return this;
2445 public override void Emit (EmitContext ec)
2447 LocalTemporary value_target = new LocalTemporary (ec, type);
2449 value_target.AddressOf (ec, AddressOp.Store);
2450 ec.ig.Emit (OpCodes.Initobj, type);
2451 value_target.Emit (ec);
2454 public void AddressOf (EmitContext ec, AddressOp Mode)
2456 LocalTemporary value_target = new LocalTemporary (ec, type);
2458 value_target.AddressOf (ec, AddressOp.Store);
2459 ec.ig.Emit (OpCodes.Initobj, type);
2460 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
2464 public abstract class Lifted : Expression, IMemoryLocation
2466 Expression expr, underlying, wrap, null_value;
2467 Unwrap unwrap;
2469 protected Lifted (Expression expr, Location loc)
2471 this.expr = expr;
2472 this.loc = loc;
2475 public override Expression DoResolve (EmitContext ec)
2477 expr = expr.Resolve (ec);
2478 if (expr == null)
2479 return null;
2481 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2482 if (unwrap == null)
2483 return null;
2485 underlying = ResolveUnderlying (unwrap, ec);
2486 if (underlying == null)
2487 return null;
2489 wrap = new Wrap (underlying, loc).Resolve (ec);
2490 if (wrap == null)
2491 return null;
2493 null_value = new NullableLiteral (wrap.Type, loc).Resolve (ec);
2494 if (null_value == null)
2495 return null;
2497 type = wrap.Type;
2498 eclass = ExprClass.Value;
2499 return this;
2502 protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
2504 public override void Emit (EmitContext ec)
2506 ILGenerator ig = ec.ig;
2507 Label is_null_label = ig.DefineLabel ();
2508 Label end_label = ig.DefineLabel ();
2510 unwrap.EmitCheck (ec);
2511 ig.Emit (OpCodes.Brfalse, is_null_label);
2513 wrap.Emit (ec);
2514 ig.Emit (OpCodes.Br, end_label);
2516 ig.MarkLabel (is_null_label);
2517 null_value.Emit (ec);
2519 ig.MarkLabel (end_label);
2522 public void AddressOf (EmitContext ec, AddressOp mode)
2524 unwrap.AddressOf (ec, mode);
2528 public class LiftedConversion : Lifted
2530 public readonly bool IsUser;
2531 public readonly bool IsExplicit;
2532 public readonly Type TargetType;
2534 public LiftedConversion (Expression expr, Type target_type, bool is_user,
2535 bool is_explicit, Location loc)
2536 : base (expr, loc)
2538 this.IsUser = is_user;
2539 this.IsExplicit = is_explicit;
2540 this.TargetType = target_type;
2543 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2545 Type type = TypeManager.GetTypeArguments (TargetType) [0];
2547 if (IsUser) {
2548 return Convert.UserDefinedConversion (ec, unwrap, type, loc, IsExplicit);
2549 } else {
2550 if (IsExplicit)
2551 return Convert.WideningAndNarrowingConversion (ec, unwrap, type, loc);
2552 else
2553 return Convert.WideningConversion (ec, unwrap, type, loc);
2558 public class LiftedUnaryOperator : Lifted
2560 public readonly Unary.Operator Oper;
2562 public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
2563 : base (expr, loc)
2565 this.Oper = op;
2568 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2570 return new Unary (Oper, unwrap, loc);
2574 public class LiftedConditional : Lifted
2576 Expression expr, true_expr, false_expr;
2577 Unwrap unwrap;
2579 public LiftedConditional (Expression expr, Expression true_expr, Expression false_expr,
2580 Location loc)
2581 : base (expr, loc)
2583 this.true_expr = true_expr;
2584 this.false_expr = false_expr;
2587 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2589 return new Conditional (unwrap, true_expr, false_expr, loc);
2593 public class LiftedBinaryOperator : Expression
2595 public readonly Binary.Operator Oper;
2597 Expression left, right, underlying, null_value, bool_wrap;
2598 Unwrap left_unwrap, right_unwrap;
2599 bool is_equality, is_comparision, is_boolean;
2601 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
2602 Location loc)
2604 this.Oper = op;
2605 this.left = left;
2606 this.right = right;
2607 this.loc = loc;
2610 public override Expression DoResolve (EmitContext ec)
2612 if (TypeManager.IsNullableType (left.Type)) {
2613 left_unwrap = new Unwrap (left, loc);
2614 left = left_unwrap.Resolve (ec);
2615 if (left == null)
2616 return null;
2619 if (TypeManager.IsNullableType (right.Type)) {
2620 right_unwrap = new Unwrap (right, loc);
2621 right = right_unwrap.Resolve (ec);
2622 if (right == null)
2623 return null;
2626 if (((Oper == Binary.Operator.BitwiseAnd) || (Oper == Binary.Operator.BitwiseOr) ||
2627 (Oper == Binary.Operator.LogicalAndAlso) || (Oper == Binary.Operator.LogicalOrElse)) &&
2628 ((left.Type == TypeManager.bool_type) && (right.Type == TypeManager.bool_type))) {
2629 Expression empty = new EmptyExpression (TypeManager.bool_type);
2630 bool_wrap = new Wrap (empty, loc).Resolve (ec);
2631 null_value = new NullableLiteral (bool_wrap.Type, loc).Resolve (ec);
2633 type = bool_wrap.Type;
2634 is_boolean = true;
2635 } else if ((Oper == Binary.Operator.Equality) || (Oper == Binary.Operator.Inequality)) {
2636 if (!(left is NullLiteral) && !(right is NullLiteral)) {
2637 underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2638 if (underlying == null)
2639 return null;
2642 type = TypeManager.bool_type;
2643 is_equality = true;
2644 } else if ((Oper == Binary.Operator.LessThan) ||
2645 (Oper == Binary.Operator.GreaterThan) ||
2646 (Oper == Binary.Operator.LessThanOrEqual) ||
2647 (Oper == Binary.Operator.GreaterThanOrEqual)) {
2648 underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2649 if (underlying == null)
2650 return null;
2652 type = TypeManager.bool_type;
2653 is_comparision = true;
2654 } else {
2655 underlying = new Binary (Oper, left, right, loc).Resolve (ec);
2656 if (underlying == null)
2657 return null;
2659 underlying = new Wrap (underlying, loc).Resolve (ec);
2660 if (underlying == null)
2661 return null;
2663 type = underlying.Type;
2664 null_value = new NullableLiteral (type, loc).Resolve (ec);
2667 eclass = ExprClass.Value;
2668 return this;
2671 void EmitBoolean (EmitContext ec)
2673 ILGenerator ig = ec.ig;
2675 Label left_is_null_label = ig.DefineLabel ();
2676 Label right_is_null_label = ig.DefineLabel ();
2677 Label is_null_label = ig.DefineLabel ();
2678 Label wrap_label = ig.DefineLabel ();
2679 Label end_label = ig.DefineLabel ();
2681 if (left_unwrap != null) {
2682 left_unwrap.EmitCheck (ec);
2683 ig.Emit (OpCodes.Brfalse, left_is_null_label);
2686 left.Emit (ec);
2687 ig.Emit (OpCodes.Dup);
2688 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOrElse))
2689 ig.Emit (OpCodes.Brtrue, wrap_label);
2690 else
2691 ig.Emit (OpCodes.Brfalse, wrap_label);
2693 if (right_unwrap != null) {
2694 right_unwrap.EmitCheck (ec);
2695 ig.Emit (OpCodes.Brfalse, right_is_null_label);
2698 if ((Oper == Binary.Operator.LogicalAndAlso) || (Oper == Binary.Operator.LogicalOrElse))
2699 ig.Emit (OpCodes.Pop);
2701 right.Emit (ec);
2702 if (Oper == Binary.Operator.BitwiseOr)
2703 ig.Emit (OpCodes.Or);
2704 else if (Oper == Binary.Operator.BitwiseAnd)
2705 ig.Emit (OpCodes.And);
2706 ig.Emit (OpCodes.Br, wrap_label);
2708 ig.MarkLabel (left_is_null_label);
2709 if (right_unwrap != null) {
2710 right_unwrap.EmitCheck (ec);
2711 ig.Emit (OpCodes.Brfalse, is_null_label);
2714 right.Emit (ec);
2715 ig.Emit (OpCodes.Dup);
2716 if ((Oper == Binary.Operator.BitwiseOr) || (Oper == Binary.Operator.LogicalOrElse))
2717 ig.Emit (OpCodes.Brtrue, wrap_label);
2718 else
2719 ig.Emit (OpCodes.Brfalse, wrap_label);
2721 ig.MarkLabel (right_is_null_label);
2722 ig.Emit (OpCodes.Pop);
2723 ig.MarkLabel (is_null_label);
2724 null_value.Emit (ec);
2725 ig.Emit (OpCodes.Br, end_label);
2727 ig.MarkLabel (wrap_label);
2728 ig.Emit (OpCodes.Nop);
2729 bool_wrap.Emit (ec);
2730 ig.Emit (OpCodes.Nop);
2732 ig.MarkLabel (end_label);
2735 void EmitEquality (EmitContext ec)
2737 ILGenerator ig = ec.ig;
2739 Label left_not_null_label = ig.DefineLabel ();
2740 Label false_label = ig.DefineLabel ();
2741 Label true_label = ig.DefineLabel ();
2742 Label end_label = ig.DefineLabel ();
2744 if (left_unwrap != null) {
2745 left_unwrap.EmitCheck (ec);
2746 if (right is NullLiteral) {
2747 if (Oper == Binary.Operator.Equality)
2748 ig.Emit (OpCodes.Brfalse, true_label);
2749 else
2750 ig.Emit (OpCodes.Brfalse, false_label);
2751 } else if (right_unwrap != null) {
2752 ig.Emit (OpCodes.Dup);
2753 ig.Emit (OpCodes.Brtrue, left_not_null_label);
2754 right_unwrap.EmitCheck (ec);
2755 ig.Emit (OpCodes.Ceq);
2756 if (Oper == Binary.Operator.Inequality) {
2757 ig.Emit (OpCodes.Ldc_I4_0);
2758 ig.Emit (OpCodes.Ceq);
2760 ig.Emit (OpCodes.Br, end_label);
2762 ig.MarkLabel (left_not_null_label);
2763 ig.Emit (OpCodes.Pop);
2764 } else {
2765 if (Oper == Binary.Operator.Equality)
2766 ig.Emit (OpCodes.Brfalse, false_label);
2767 else
2768 ig.Emit (OpCodes.Brfalse, true_label);
2772 if (right_unwrap != null) {
2773 right_unwrap.EmitCheck (ec);
2774 if (left is NullLiteral) {
2775 if (Oper == Binary.Operator.Equality)
2776 ig.Emit (OpCodes.Brfalse, true_label);
2777 else
2778 ig.Emit (OpCodes.Brfalse, false_label);
2779 } else {
2780 if (Oper == Binary.Operator.Equality)
2781 ig.Emit (OpCodes.Brfalse, false_label);
2782 else
2783 ig.Emit (OpCodes.Brfalse, true_label);
2787 bool left_is_null = left is NullLiteral;
2788 bool right_is_null = right is NullLiteral;
2789 if (left_is_null || right_is_null) {
2790 if (((Oper == Binary.Operator.Equality) && (left_is_null == right_is_null)) ||
2791 ((Oper == Binary.Operator.Inequality) && (left_is_null != right_is_null)))
2792 ig.Emit (OpCodes.Br, true_label);
2793 else
2794 ig.Emit (OpCodes.Br, false_label);
2795 } else {
2796 underlying.Emit (ec);
2797 ig.Emit (OpCodes.Br, end_label);
2800 ig.MarkLabel (false_label);
2801 ig.Emit (OpCodes.Ldc_I4_0);
2802 ig.Emit (OpCodes.Br, end_label);
2804 ig.MarkLabel (true_label);
2805 ig.Emit (OpCodes.Ldc_I4_1);
2807 ig.MarkLabel (end_label);
2810 void EmitComparision (EmitContext ec)
2812 ILGenerator ig = ec.ig;
2814 Label is_null_label = ig.DefineLabel ();
2815 Label end_label = ig.DefineLabel ();
2817 if (left_unwrap != null) {
2818 left_unwrap.EmitCheck (ec);
2819 ig.Emit (OpCodes.Brfalse, is_null_label);
2822 if (right_unwrap != null) {
2823 right_unwrap.EmitCheck (ec);
2824 ig.Emit (OpCodes.Brfalse, is_null_label);
2827 underlying.Emit (ec);
2828 ig.Emit (OpCodes.Br, end_label);
2830 ig.MarkLabel (is_null_label);
2831 ig.Emit (OpCodes.Ldc_I4_0);
2833 ig.MarkLabel (end_label);
2836 public override void Emit (EmitContext ec)
2838 if (is_boolean) {
2839 EmitBoolean (ec);
2840 return;
2841 } else if (is_equality) {
2842 EmitEquality (ec);
2843 return;
2844 } else if (is_comparision) {
2845 EmitComparision (ec);
2846 return;
2849 ILGenerator ig = ec.ig;
2851 Label is_null_label = ig.DefineLabel ();
2852 Label end_label = ig.DefineLabel ();
2854 if (left_unwrap != null) {
2855 left_unwrap.EmitCheck (ec);
2856 ig.Emit (OpCodes.Brfalse, is_null_label);
2859 if (right_unwrap != null) {
2860 right_unwrap.EmitCheck (ec);
2861 ig.Emit (OpCodes.Brfalse, is_null_label);
2864 underlying.Emit (ec);
2865 ig.Emit (OpCodes.Br, end_label);
2867 ig.MarkLabel (is_null_label);
2868 null_value.Emit (ec);
2870 ig.MarkLabel (end_label);
2874 public class OperatorTrueOrFalse : Expression
2876 public readonly bool IsTrue;
2878 Expression expr;
2879 Unwrap unwrap;
2881 public OperatorTrueOrFalse (Expression expr, bool is_true, Location loc)
2883 this.IsTrue = is_true;
2884 this.expr = expr;
2885 this.loc = loc;
2888 public override Expression DoResolve (EmitContext ec)
2890 unwrap = new Unwrap (expr, loc);
2891 expr = unwrap.Resolve (ec);
2892 if (expr == null)
2893 return null;
2895 if (unwrap.Type != TypeManager.bool_type)
2896 return null;
2898 type = TypeManager.bool_type;
2899 eclass = ExprClass.Value;
2900 return this;
2903 public override void Emit (EmitContext ec)
2905 ILGenerator ig = ec.ig;
2907 Label is_null_label = ig.DefineLabel ();
2908 Label end_label = ig.DefineLabel ();
2910 unwrap.EmitCheck (ec);
2911 ig.Emit (OpCodes.Brfalse, is_null_label);
2913 unwrap.Emit (ec);
2914 if (!IsTrue) {
2915 ig.Emit (OpCodes.Ldc_I4_0);
2916 ig.Emit (OpCodes.Ceq);
2918 ig.Emit (OpCodes.Br, end_label);
2920 ig.MarkLabel (is_null_label);
2921 ig.Emit (OpCodes.Ldc_I4_0);
2923 ig.MarkLabel (end_label);
2927 public class NullCoalescingOperator : Expression
2929 Expression left, right;
2930 Expression expr;
2931 Unwrap unwrap;
2933 public NullCoalescingOperator (Expression left, Expression right, Location loc)
2935 this.left = left;
2936 this.right = right;
2937 this.loc = loc;
2939 eclass = ExprClass.Value;
2942 public override Expression DoResolve (EmitContext ec)
2944 if (type != null)
2945 return this;
2947 left = left.Resolve (ec);
2948 if (left == null)
2949 return null;
2951 right = right.Resolve (ec);
2952 if (right == null)
2953 return null;
2955 Type ltype = left.Type, rtype = right.Type;
2957 if (!TypeManager.IsNullableType (ltype) && ltype.IsValueType) {
2958 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2959 return null;
2962 if (TypeManager.IsNullableType (ltype)) {
2963 NullableInfo info = new NullableInfo (ltype);
2965 unwrap = (Unwrap) new Unwrap (left, loc).Resolve (ec);
2966 if (unwrap == null)
2967 return null;
2969 expr = Convert.WideningConversion (ec, right, info.UnderlyingType, loc);
2970 if (expr != null) {
2971 left = unwrap;
2972 type = expr.Type;
2973 return this;
2977 expr = Convert.WideningConversion (ec, right, ltype, loc);
2978 if (expr != null) {
2979 type = expr.Type;
2980 return this;
2983 if (unwrap != null) {
2984 expr = Convert.WideningConversion (ec, unwrap, rtype, loc);
2985 if (expr != null) {
2986 left = expr;
2987 expr = right;
2988 type = expr.Type;
2989 return this;
2993 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
2994 return null;
2997 public override void Emit (EmitContext ec)
2999 ILGenerator ig = ec.ig;
3001 Label is_null_label = ig.DefineLabel ();
3002 Label end_label = ig.DefineLabel ();
3004 if (unwrap != null) {
3005 unwrap.EmitCheck (ec);
3006 ig.Emit (OpCodes.Brfalse, is_null_label);
3009 left.Emit (ec);
3010 ig.Emit (OpCodes.Br, end_label);
3012 ig.MarkLabel (is_null_label);
3013 expr.Emit (ec);
3015 ig.MarkLabel (end_label);
3019 public class LiftedUnaryMutator : ExpressionStatement
3021 public readonly UnaryMutator.Mode Mode;
3022 Expression expr, null_value;
3023 UnaryMutator underlying;
3024 Unwrap unwrap;
3026 public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
3028 this.expr = expr;
3029 this.Mode = mode;
3030 this.loc = loc;
3032 eclass = ExprClass.Value;
3035 public override Expression DoResolve (EmitContext ec)
3037 expr = expr.Resolve (ec);
3038 if (expr == null)
3039 return null;
3041 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
3042 if (unwrap == null)
3043 return null;
3045 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
3046 if (underlying == null)
3047 return null;
3049 null_value = new NullableLiteral (expr.Type, loc).Resolve (ec);
3050 if (null_value == null)
3051 return null;
3053 type = expr.Type;
3054 return this;
3057 void DoEmit (EmitContext ec, bool is_expr)
3059 ILGenerator ig = ec.ig;
3060 Label is_null_label = ig.DefineLabel ();
3061 Label end_label = ig.DefineLabel ();
3063 unwrap.EmitCheck (ec);
3064 ig.Emit (OpCodes.Brfalse, is_null_label);
3066 if (is_expr)
3067 underlying.Emit (ec);
3068 else
3069 underlying.EmitStatement (ec);
3070 ig.Emit (OpCodes.Br, end_label);
3072 ig.MarkLabel (is_null_label);
3073 if (is_expr)
3074 null_value.Emit (ec);
3076 ig.MarkLabel (end_label);
3079 public override void Emit (EmitContext ec)
3081 DoEmit (ec, true);
3084 public override void EmitStatement (EmitContext ec)
3086 DoEmit (ec, false);