2 // generic.cs: Generics support
4 // Authors: Martin Baulig (martin@ximian.com)
5 // Miguel de Icaza (miguel@ximian.com)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
10 // (C) 2004 Novell, Inc
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Globalization;
16 using System.Collections;
18 using System.Text.RegularExpressions;
20 namespace Mono.CSharp {
22 public abstract class GenericConstraints {
23 public abstract GenericParameterAttributes Attributes {
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.ValueTypeConstraint) != 0; }
39 public virtual bool HasClassConstraint {
40 get { return ClassConstraint != null; }
43 public abstract Type ClassConstraint {
47 public abstract Type[] InterfaceConstraints {
51 public abstract Type EffectiveBaseClass {
56 // Returns whether the type parameter is "known to be a reference type".
58 public virtual bool IsReferenceType {
60 if (HasReferenceTypeConstraint)
62 if (HasValueTypeConstraint)
65 if (ClassConstraint != null) {
66 if (ClassConstraint.IsValueType)
69 if (ClassConstraint != TypeManager.object_type)
73 foreach (Type t in InterfaceConstraints) {
74 if (!t.IsGenericParameter)
77 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
78 if ((gc != null) && gc.IsReferenceType)
87 // Returns whether the type parameter is "known to be a value type".
89 public virtual bool IsValueType {
91 if (HasValueTypeConstraint)
93 if (HasReferenceTypeConstraint)
96 if (ClassConstraint != null) {
97 if (!ClassConstraint.IsValueType)
100 if (ClassConstraint != TypeManager.value_type)
104 foreach (Type t in InterfaceConstraints) {
105 if (!t.IsGenericParameter)
108 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
109 if ((gc != null) && gc.IsValueType)
118 public enum SpecialConstraint
126 // Tracks the constraints for a type parameter
128 public class Constraints : GenericConstraints {
130 ArrayList constraints;
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,
141 this.constraints = constraints;
145 public string TypeParameter {
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.");
172 if (obj is SpecialConstraint) {
173 SpecialConstraint sc = (SpecialConstraint) obj;
175 if (sc == SpecialConstraint.Constructor) {
176 if (!HasValueTypeConstraint) {
177 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
182 451, loc, "The new () constraint " +
183 "cannot be used with the `struct' " +
188 if ((num_constraints > 0) || HasReferenceTypeConstraint || HasValueTypeConstraint) {
189 Report.Error (449, loc,
190 "The `class' or `struct' " +
191 "constraint must be first");
195 if (sc == SpecialConstraint.ReferenceType)
196 attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
198 attrs |= GenericParameterAttributes.ValueTypeConstraint;
203 if (obj is ConstructedType) {
204 ConstructedType cexpr = (ConstructedType) obj;
205 if (!cexpr.ResolveConstructedType (ec))
209 expr = ((Expression) obj).ResolveAsTypeTerminal (ec);
214 TypeParameterExpr texpr = expr as TypeParameterExpr;
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.",
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);
231 class_constraint = expr;
239 bool CheckTypeParameterConstraints (TypeParameter tparam, Hashtable seen)
241 seen.Add (tparam, true);
243 Constraints constraints = tparam.Constraints;
244 if (constraints == null)
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}'",
255 if (constraints.type_param_constraints == null)
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);
266 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
273 public bool ResolveTypes (EmitContext ec)
275 if (effective_base_type != null)
278 foreach (object obj in constraints) {
279 ConstructedType cexpr = obj as ConstructedType;
283 if (!cexpr.CheckConstraints (ec))
287 foreach (TypeParameterExpr expr in type_param_constraints) {
288 Hashtable seen = new Hashtable ();
289 if (!CheckTypeParameterConstraints (expr.TypeParameter, seen))
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))
300 Report.Error (405, loc,
301 "Duplicate constraint `{0}' for type " +
302 "parameter `{1}'.", iface_constraint.Type,
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))
315 Report.Error (405, loc,
316 "Duplicate constraint `{0}' for type " +
317 "parameter `{1}'.", expr.Type, name);
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)
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);
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);
352 if (class_constraint_type != null)
353 effective_base_type = class_constraint_type;
354 else if (HasValueTypeConstraint)
355 effective_base_type = TypeManager.value_type;
357 effective_base_type = TypeManager.object_type;
362 public bool CheckDependencies (EmitContext ec)
364 foreach (TypeParameterExpr expr in type_param_constraints) {
365 if (!CheckDependencies (expr.TypeParameter, ec))
372 bool CheckDependencies (TypeParameter tparam, EmitContext ec)
374 Constraints constraints = tparam.Constraints;
375 if (constraints == null)
378 if (HasValueTypeConstraint && constraints.HasClassConstraint) {
379 Report.Error (455, loc, "Type parameter `{0}' inherits " +
380 "conflicting constraints `{1}' and `{2}'",
381 name, constraints.ClassConstraint,
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.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}'",
402 if (constraints.type_param_constraints == null)
405 foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
406 if (!CheckDependencies (expr.TypeParameter, ec))
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))
444 if (iface_constraint_types == null)
447 foreach (Type iface in iface_constraint_types) {
448 if (TypeManager.IsSubclassOf (iface, t))
455 public bool CheckInterfaceMethod (EmitContext ec, GenericConstraints gc)
457 if (gc.Attributes != attrs)
460 if (HasClassConstraint != gc.HasClassConstraint)
462 if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
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)
473 foreach (Type iface in gc.InterfaceConstraints) {
475 foreach (Type check in InterfaceConstraints) {
476 if (TypeManager.IsEqual (iface, check)) {
491 // This type represents a generic type parameter
493 public class TypeParameter : MemberCore, IMemberContainer {
495 GenericConstraints gc;
496 Constraints constraints;
498 GenericTypeParameterBuilder type;
500 public TypeParameter (TypeContainer parent, string name,
501 Constraints constraints, Location loc)
502 : base (parent, new MemberName (name), null, loc)
505 this.constraints = constraints;
509 public GenericConstraints GenericConstraints {
511 return gc != null ? gc : constraints;
515 public Constraints Constraints {
521 public bool HasConstructorConstraint {
523 if (constraints != null)
524 return constraints.HasConstructorConstraint;
536 public bool Resolve (DeclSpace ds)
538 if (constraints != null) {
539 if (!constraints.Resolve (ds.EmitContext)) {
548 public void Define (GenericTypeParameterBuilder type)
550 if (this.type != null)
551 throw new InvalidOperationException ();
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)) {
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))
586 if (implementing != null) {
587 if (is_override && (constraints != null)) {
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");
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];
606 gc = new InflatedConstraints (temp_gc, implementing.DeclaringType);
607 else if (constraints != null)
608 gc = new InflatedConstraints (constraints, implementing.DeclaringType);
611 if (constraints != null) {
614 else if (!constraints.CheckInterfaceMethod (ec, gc))
617 if (!is_override && (temp_gc != null))
622 Report.SymbolRelatedToPreviousError (implementing);
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));
635 gc = (GenericConstraints) constraints;
641 if (gc.HasClassConstraint)
642 type.SetBaseTypeConstraint (gc.ClassConstraint);
644 type.SetInterfaceConstraints (gc.InterfaceConstraints);
645 TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
650 public bool CheckDependencies (EmitContext ec)
652 if (constraints != null)
653 return constraints.CheckDependencies (ec);
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.
669 throw new InvalidOperationException ();
670 constraints = new_constraints;
675 throw new InvalidOperationException ();
677 if (constraints == null)
678 return new_constraints == null;
679 else if (new_constraints == null)
682 if (!new_constraints.Resolve (ec))
684 if (!new_constraints.ResolveTypes (ec))
687 return constraints.CheckInterfaceMethod (ec, new_constraints);
690 public override string DocCommentHeader {
692 throw new InvalidOperationException (
693 "Unexpected attempt to get doc comment from " + this.GetType () + ".");
701 public override bool Define ()
706 protected override void VerifyObsoleteAttribute ()
709 public override void ApplyAttributeBuilder (Attribute a,
710 CustomAttributeBuilder cb)
713 public override AttributeTargets AttributeTargets {
715 return (AttributeTargets) 0;
719 public override string[] ValidAttributeTargets {
721 return new string [0];
729 string IMemberContainer.Name {
733 MemberCache IMemberContainer.BaseCache {
737 bool IMemberContainer.IsInterface {
741 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
743 return FindMembers (mt, bf, null, null);
746 MemberCache IMemberContainer.MemberCache {
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)
782 if (constraints != null)
783 return constraints.IsSubclassOf (t);
788 public override string ToString ()
790 return "TypeParameter[" + name + "]";
793 protected class InflatedConstraints : GenericConstraints
795 GenericConstraints gc;
797 Type class_constraint;
798 Type[] iface_constraints;
802 public InflatedConstraints (GenericConstraints gc, Type declaring)
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);
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;
835 base_type = TypeManager.object_type;
838 Type inflate (Type t)
842 if (t.IsGenericParameter)
843 return dargs [t.GenericParameterPosition];
844 if (t.IsGenericInstance) {
845 t = t.GetGenericTypeDefinition ();
846 t = t.BindGenericParameters (dargs);
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 {
880 return type_parameter.Name;
884 public override string FullName {
886 return type_parameter.Name;
890 public TypeParameter TypeParameter {
892 return type_parameter;
896 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
898 this.type_parameter = type_parameter;
902 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
904 type = type_parameter.Type;
909 public override bool IsInterface {
910 get { return false; }
913 public override bool CheckAccessLevel (DeclSpace ds)
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;
932 public TypeArguments (Location loc)
934 args = new ArrayList ();
938 public TypeArguments (int dimension, Location loc)
940 this.dimension = dimension;
944 public void Add (Expression type)
947 throw new InvalidOperationException ();
952 public void Add (TypeArguments new_args)
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;
970 Report.Error (81, Location, "Type parameter declaration " +
971 "must be an identifier not a type");
977 public Type[] Arguments {
983 public bool HasTypeArguments {
985 return has_type_args;
998 public bool IsUnbound {
1000 return dimension > 0;
1004 public override string ToString ()
1006 StringBuilder s = new StringBuilder ();
1009 for (int i = 0; i < count; i++){
1011 // FIXME: Use TypeManager.CSharpname once we have the type
1014 s.Append (args [i].ToString ());
1018 return s.ToString ();
1021 public bool Resolve (EmitContext ec)
1023 int count = args.Count;
1026 atypes = new Type [count];
1028 for (int i = 0; i < count; i++){
1029 TypeExpr te = ((Expression) args [i]).ResolveAsTypeTerminal (ec);
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));
1043 atypes [i] = te.Type;
1049 public class ConstructedType : TypeExpr {
1050 string name, full_name;
1052 Type[] gen_params, atypes;
1055 public ConstructedType (string name, TypeArguments args, Location l)
1058 this.name = MemberName.MakeName (name, args.Count);
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)
1071 full_name = name + "<" + args.ToString () + ">";
1074 public ConstructedType (FullNamedExpression fname, TypeArguments args, Location l)
1077 this.name = fname.FullName;
1080 eclass = ExprClass.Type;
1081 full_name = name + "<" + args.ToString () + ">";
1084 protected ConstructedType (TypeArguments args, Location l)
1089 eclass = ExprClass.Type;
1092 protected ConstructedType (TypeParameter[] type_params, Location 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)
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 {
1127 StringBuilder sb = new StringBuilder ();
1128 sb.Append (gt.FullName);
1130 for (int i = 0; i < gen_params.Length; i++) {
1133 sb.Append (gen_params [i]);
1136 return sb.ToString ();
1140 protected bool CheckConstraint (EmitContext ec, Type ptype, Expression expr,
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++) {
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)
1164 return Convert.ImplicitStandardConversionExists (ec, expr, ctype);
1167 protected bool CheckConstraints (EmitContext ec, int index)
1169 Type atype = atypes [index];
1170 Type ptype = gen_params [index];
1175 Expression aexpr = new EmptyExpression (atype);
1177 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1181 bool is_class, is_struct;
1182 if (atype.IsGenericParameter) {
1183 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1185 is_class = agc.HasReferenceTypeConstraint;
1186 is_struct = agc.HasValueTypeConstraint;
1188 is_class = is_struct = false;
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);
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);
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);
1229 // Now, check the interface constraints.
1231 foreach (Type it in gc.InterfaceConstraints) {
1233 if (it.IsGenericParameter)
1234 itype = atypes [it.GenericParameterPosition];
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);
1249 // Finally, check the constructor constraint.
1252 if (!gc.HasConstructorConstraint)
1255 if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
1258 MethodGroupExpr mg = Expression.MemberLookup (
1259 ec, atype, ".ctor", MemberTypes.Constructor,
1260 BindingFlags.Public | BindingFlags.Instance |
1261 BindingFlags.DeclaredOnly, loc)
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);
1275 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
1277 if (!ResolveConstructedType (ec))
1283 public bool CheckConstraints (EmitContext ec)
1285 for (int i = 0; i < gen_params.Length; i++) {
1286 if (!CheckConstraints (ec, i))
1293 public override TypeExpr ResolveAsTypeTerminal (EmitContext ec)
1295 if (base.ResolveAsTypeTerminal (ec) == null)
1298 if (!CheckConstraints (ec))
1304 public bool ResolveConstructedType (EmitContext ec)
1309 return DoResolveType (ec);
1312 // First, resolve the generic type.
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);
1321 foreach (TypeParameter param in ds.TypeParameters)
1322 new_args.Add (new TypeParameterExpr (param, loc));
1324 new_args.Add (args);
1327 return DoResolveType (ec);
1333 SimpleName sn = new SimpleName (name, loc);
1334 TypeExpr resolved = sn.ResolveAsTypeTerminal (ec);
1335 if (resolved == null)
1340 Report.Error (246, loc, "Cannot find type `{0}'<...>",
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));
1354 gt = t.GetGenericTypeDefinition ();
1355 return DoResolveType (ec);
1358 bool DoResolveType (EmitContext ec)
1361 // Resolve the arguments.
1363 if (args.Resolve (ec) == 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),
1379 // Now bind the parameters.
1381 type = gt.BindGenericParameters (atypes);
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;
1426 if ((type == null) || (cobj.type == null))
1429 return type == cobj.type;
1432 public override int GetHashCode ()
1434 return base.GetHashCode ();
1437 public string Basename {
1439 int pos = name.LastIndexOf ('`');
1441 return name.Substring (0, pos);
1447 public override string Name {
1454 public override string FullName {
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))
1482 public bool Define (MethodBuilder mb, Type return_type)
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))
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))
1515 public override bool DefineMembers (TypeContainer parent)
1520 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1521 MemberFilter filter, object criteria)
1523 throw new Exception ();
1526 public override MemberCache MemberCache {
1532 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
1537 protected override void VerifyObsoleteAttribute()
1542 public override AttributeTargets AttributeTargets {
1544 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1548 public override string DocCommentHeader {
1549 get { return "M:"; }
1553 public class DefaultValueExpression : Expression
1556 LocalTemporary temp_storage;
1558 public DefaultValueExpression (Expression expr, Location loc)
1564 public override Expression DoResolve (EmitContext ec)
1566 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec);
1571 if (type.IsGenericParameter || TypeManager.IsValueType (type))
1572 temp_storage = new LocalTemporary (ec, type);
1574 eclass = ExprClass.Variable;
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);
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;
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;
1635 // Tracks the generic parameters.
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 ()
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);
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);
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);
1734 return tc.IsGeneric ? tc.CountTypeParameters : 0;
1736 return t.HasGenericArguments ? t.GetGenericArguments ().Length : 0;
1739 public static Type[] GetTypeArguments (Type t)
1741 DeclSpace tc = LookupDeclSpace (t);
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 ();
1756 return t.GetGenericArguments ();
1759 public static bool IsEqual (Type a, Type b)
1764 if ((a is TypeBuilder) && a.IsGenericTypeDefinition && b.IsGenericInstance) {
1766 // `a' is a generic type definition's TypeBuilder and `b' is a
1767 // generic instance of the same type.
1773 // void Test (Stack<T> stack) { }
1776 // The first argument of `Test' will be the generic instance
1777 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1780 // We hit this via Closure.Filter() for gen-82.cs.
1782 if (a != b.GetGenericTypeDefinition ())
1785 Type[] aparams = a.GetGenericArguments ();
1786 Type[] bparams = b.GetGenericArguments ();
1788 if (aparams.Length != bparams.Length)
1791 for (int i = 0; i < aparams.Length; i++)
1792 if (!IsEqual (aparams [i], bparams [i]))
1798 if ((b is TypeBuilder) && b.IsGenericTypeDefinition && a.IsGenericInstance)
1799 return IsEqual (b, a);
1801 if (a.IsGenericParameter && b.IsGenericParameter) {
1802 if ((a.DeclaringMethod == null) || (b.DeclaringMethod == null))
1804 return a.GenericParameterPosition == b.GenericParameterPosition;
1807 if (a.IsArray && b.IsArray) {
1808 if (a.GetArrayRank () != b.GetArrayRank ())
1810 return IsEqual (a.GetElementType (), b.GetElementType ());
1813 if (a.IsGenericInstance && b.IsGenericInstance) {
1814 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1817 Type[] aargs = a.GetGenericArguments ();
1818 Type[] bargs = b.GetGenericArguments ();
1820 if (aargs.Length != bargs.Length)
1823 for (int i = 0; i < aargs.Length; i++) {
1824 if (!IsEqual (aargs [i], bargs [i]))
1834 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_infered, Type[] method_infered)
1836 if (a.IsGenericParameter) {
1838 // If a is an array of a's type, they may never
1842 b = b.GetElementType ();
1848 // If b is a generic parameter or an actual type,
1849 // they may become equal:
1851 // class X<T,U> : I<T>, I<U>
1852 // class X<T> : I<T>, I<float>
1854 if (b.IsGenericParameter || !b.IsGenericInstance) {
1855 int pos = a.GenericParameterPosition;
1856 Type[] args = a.DeclaringMethod != null ? method_infered : class_infered;
1857 if (args [pos] == null) {
1862 return args [pos] == a;
1866 // We're now comparing a type parameter with a
1867 // generic instance. They may become equal unless
1868 // the type parameter appears anywhere in the
1869 // generic instance:
1871 // class X<T,U> : I<T>, I<X<U>>
1872 // -> error because you could instanciate it as
1875 // class X<T> : I<T>, I<X<T>> -> ok
1878 Type[] bargs = GetTypeArguments (b);
1879 for (int i = 0; i < bargs.Length; i++) {
1880 if (a.Equals (bargs [i]))
1887 if (b.IsGenericParameter)
1888 return MayBecomeEqualGenericTypes (b, a, class_infered, method_infered);
1891 // At this point, neither a nor b are a type parameter.
1893 // If one of them is a generic instance, let
1894 // MayBecomeEqualGenericInstances() compare them (if the
1895 // other one is not a generic instance, they can never
1899 if (a.IsGenericInstance || b.IsGenericInstance)
1900 return MayBecomeEqualGenericInstances (a, b, class_infered, method_infered);
1903 // If both of them are arrays.
1906 if (a.IsArray && b.IsArray) {
1907 if (a.GetArrayRank () != b.GetArrayRank ())
1910 a = a.GetElementType ();
1911 b = b.GetElementType ();
1913 return MayBecomeEqualGenericTypes (a, b, class_infered, method_infered);
1917 // Ok, two ordinary types.
1920 return a.Equals (b);
1924 // Checks whether two generic instances may become equal for some
1925 // particular instantiation (26.3.1).
1927 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1928 Type[] class_infered, Type[] method_infered)
1930 if (!a.IsGenericInstance || !b.IsGenericInstance)
1932 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1935 return MayBecomeEqualGenericInstances (
1936 GetTypeArguments (a), GetTypeArguments (b), class_infered, method_infered);
1939 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1940 Type[] class_infered, Type[] method_infered)
1942 if (aargs.Length != bargs.Length)
1945 for (int i = 0; i < aargs.Length; i++) {
1946 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_infered, method_infered))
1953 public static bool IsEqualGenericInstance (Type type, Type parent)
1955 int tcount = GetNumberOfTypeArguments (type);
1956 int pcount = GetNumberOfTypeArguments (parent);
1958 if (type.IsGenericInstance)
1959 type = type.GetGenericTypeDefinition ();
1960 if (parent.IsGenericInstance)
1961 parent = parent.GetGenericTypeDefinition ();
1963 if (tcount != pcount)
1966 return type.Equals (parent);
1969 static public bool IsGenericMethod (MethodBase mb)
1971 if (mb.DeclaringType is TypeBuilder) {
1972 IMethodData method = (IMethodData) builder_to_method [mb];
1976 return method.GenericMethod != null;
1979 return mb.IsGenericMethodDefinition;
1986 static bool InferType (Type pt, Type at, Type[] infered)
1988 if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
1989 int pos = pt.GenericParameterPosition;
1991 if (infered [pos] == null) {
1993 while (check.IsArray)
1994 check = check.GetElementType ();
2003 if (infered [pos] != at)
2009 if (!pt.ContainsGenericParameters) {
2010 if (at.ContainsGenericParameters)
2011 return InferType (at, pt, infered);
2018 (at.GetArrayRank () != pt.GetArrayRank ()))
2021 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2026 (pt.GetArrayRank () != at.GetArrayRank ()))
2029 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2032 if (pt.IsByRef && at.IsByRef)
2033 return InferType (pt.GetElementType (), at.GetElementType (), infered);
2034 ArrayList list = new ArrayList ();
2035 if (at.IsGenericInstance)
2038 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
2041 list.AddRange (TypeManager.GetInterfaces (at));
2044 bool found_one = false;
2046 foreach (Type type in list) {
2047 if (!type.IsGenericInstance)
2050 Type[] infered_types = new Type [infered.Length];
2052 if (!InferGenericInstance (pt, type, infered_types))
2055 for (int i = 0; i < infered_types.Length; i++) {
2056 if (infered [i] == null) {
2057 infered [i] = infered_types [i];
2061 if (infered [i] != infered_types [i])
2071 static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
2073 Type[] at_args = at.GetGenericArguments ();
2074 Type[] pt_args = pt.GetGenericArguments ();
2076 if (at_args.Length != pt_args.Length)
2079 for (int i = 0; i < at_args.Length; i++) {
2080 if (!InferType (pt_args [i], at_args [i], infered_types))
2084 for (int i = 0; i < infered_types.Length; i++) {
2085 if (infered_types [i] == null)
2092 public static bool InferParamsTypeArguments (EmitContext ec, ArrayList arguments,
2093 ref MethodBase method)
2095 if ((arguments == null) || !TypeManager.IsGenericMethod (method))
2100 if (arguments == null)
2103 arg_count = arguments.Count;
2105 ParameterData pd = Invocation.GetParameterData (method);
2107 int pd_count = pd.Count;
2112 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2115 if (pd_count - 1 > arg_count)
2118 if (pd_count == 1 && arg_count == 0)
2121 Type[] method_args = method.GetGenericArguments ();
2122 Type[] infered_types = new Type [method_args.Length];
2125 // If we have come this far, the case which
2126 // remains is when the number of parameters is
2127 // less than or equal to the argument count.
2129 for (int i = 0; i < pd_count - 1; ++i) {
2130 Argument a = (Argument) arguments [i];
2132 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2135 Type pt = pd.ParameterType (i);
2138 if (!InferType (pt, at, infered_types))
2142 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
2144 for (int i = pd_count - 1; i < arg_count; i++) {
2145 Argument a = (Argument) arguments [i];
2147 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2150 if (!InferType (element_type, a.Type, infered_types))
2154 for (int i = 0; i < infered_types.Length; i++)
2155 if (infered_types [i] == null)
2158 method = method.BindGenericParameters (infered_types);
2162 public static bool InferTypeArguments (Type[] param_types, Type[] arg_types, Type[] infered_types)
2164 if (infered_types == null)
2167 for (int i = 0; i < arg_types.Length; i++) {
2168 if (arg_types [i] == null)
2171 if (!InferType (param_types [i], arg_types [i], infered_types))
2175 for (int i = 0; i < infered_types.Length; i++)
2176 if (infered_types [i] == null)
2182 public static bool InferTypeArguments (EmitContext ec, ArrayList arguments,
2183 ref MethodBase method)
2185 if (!TypeManager.IsGenericMethod (method))
2189 if (arguments != null)
2190 arg_count = arguments.Count;
2194 ParameterData pd = Invocation.GetParameterData (method);
2195 if (arg_count != pd.Count)
2198 Type[] method_args = method.GetGenericArguments ();
2200 bool is_open = false;
2201 for (int i = 0; i < method_args.Length; i++) {
2202 if (method_args [i].IsGenericParameter) {
2210 Type[] infered_types = new Type [method_args.Length];
2212 Type[] param_types = new Type [pd.Count];
2213 Type[] arg_types = new Type [pd.Count];
2215 for (int i = 0; i < arg_count; i++) {
2216 param_types [i] = pd.ParameterType (i);
2218 Argument a = (Argument) arguments [i];
2219 if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
2222 arg_types [i] = a.Type;
2225 if (!InferTypeArguments (param_types, arg_types, infered_types))
2228 method = method.BindGenericParameters (infered_types);
2232 public static bool InferTypeArguments (EmitContext ec, ParameterData apd,
2233 ref MethodBase method)
2235 if (!TypeManager.IsGenericMethod (method))
2238 ParameterData pd = Invocation.GetParameterData (method);
2239 if (apd.Count != pd.Count)
2242 Type[] method_args = method.GetGenericArguments ();
2243 Type[] infered_types = new Type [method_args.Length];
2245 Type[] param_types = new Type [pd.Count];
2246 Type[] arg_types = new Type [pd.Count];
2248 for (int i = 0; i < apd.Count; i++) {
2249 param_types [i] = pd.ParameterType (i);
2250 arg_types [i] = apd.ParameterType (i);
2253 if (!InferTypeArguments (param_types, arg_types, infered_types))
2256 method = method.BindGenericParameters (infered_types);
2260 public static bool IsNullableType (Type t)
2262 if (!t.IsGenericInstance)
2265 Type gt = t.GetGenericTypeDefinition ();
2266 return gt == generic_nullable_type;
2270 public class NullCoalescingOperator : Expression
2275 public NullCoalescingOperator (Expression left, Expression right, Location loc)
2282 public override Expression DoResolve (EmitContext ec)
2284 Error (-1, "The ?? operator is not yet implemented.");
2288 public override void Emit (EmitContext ec)
2293 public abstract class Nullable
2295 protected sealed class NullableInfo
2297 public readonly Type Type;
2298 public readonly Type UnderlyingType;
2299 public readonly MethodInfo HasValue;
2300 public readonly MethodInfo Value;
2301 public readonly ConstructorInfo Constructor;
2303 public NullableInfo (Type type)
2306 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
2308 PropertyInfo has_value_pi = type.GetProperty ("HasValue");
2309 PropertyInfo value_pi = type.GetProperty ("Value");
2311 HasValue = has_value_pi.GetGetMethod (false);
2312 Value = value_pi.GetGetMethod (false);
2313 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
2317 protected class Unwrap : Expression, IMemoryLocation
2322 LocalTemporary temp;
2325 public Unwrap (Expression expr, Location loc)
2331 public override Expression DoResolve (EmitContext ec)
2333 expr = expr.Resolve (ec);
2337 if (!(expr is IMemoryLocation))
2338 temp = new LocalTemporary (ec, expr.Type);
2340 info = new NullableInfo (expr.Type);
2341 type = info.UnderlyingType;
2342 eclass = expr.eclass;
2346 public override void Emit (EmitContext ec)
2348 AddressOf (ec, AddressOp.LoadStore);
2349 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
2352 public void EmitCheck (EmitContext ec, Label label)
2354 AddressOf (ec, AddressOp.LoadStore);
2355 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
2356 ec.ig.Emit (OpCodes.Brfalse, label);
2359 public void AddressOf (EmitContext ec, AddressOp mode)
2366 temp.AddressOf (ec, AddressOp.LoadStore);
2368 ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
2372 protected class Wrap : Expression
2377 public Wrap (Expression expr, Location loc)
2383 public override Expression DoResolve (EmitContext ec)
2385 expr = expr.Resolve (ec);
2389 TypeExpr target_type = new NullableType (expr.Type, loc);
2390 target_type = target_type.ResolveAsTypeTerminal (ec);
2391 if (target_type == null)
2394 type = target_type.Type;
2395 info = new NullableInfo (type);
2396 eclass = ExprClass.Value;
2400 public override void Emit (EmitContext ec)
2403 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
2407 public class NullLiteral : Expression, IMemoryLocation {
2408 public NullLiteral (Type target_type, Location loc)
2410 this.type = target_type;
2413 eclass = ExprClass.Value;
2416 public override Expression DoResolve (EmitContext ec)
2421 public override void Emit (EmitContext ec)
2423 LocalTemporary value_target = new LocalTemporary (ec, type);
2425 value_target.AddressOf (ec, AddressOp.Store);
2426 ec.ig.Emit (OpCodes.Initobj, type);
2427 value_target.Emit (ec);
2430 public void AddressOf (EmitContext ec, AddressOp Mode)
2432 LocalTemporary value_target = new LocalTemporary (ec, type);
2434 value_target.AddressOf (ec, AddressOp.Store);
2435 ec.ig.Emit (OpCodes.Initobj, type);
2436 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
2440 public abstract class Lifted : Expression, IMemoryLocation
2442 Expression expr, underlying, wrap, null_value;
2445 protected Lifted (Expression expr, Location loc)
2451 public override Expression DoResolve (EmitContext ec)
2453 expr = expr.Resolve (ec);
2457 unwrap = (Unwrap) new Unwrap (expr, loc).Resolve (ec);
2461 underlying = ResolveUnderlying (unwrap, ec);
2462 if (underlying == null)
2465 wrap = new Wrap (underlying, loc).Resolve (ec);
2469 null_value = new NullLiteral (wrap.Type, loc).Resolve (ec);
2470 if (null_value == null)
2474 eclass = ExprClass.Value;
2478 protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
2480 public override void Emit (EmitContext ec)
2482 ILGenerator ig = ec.ig;
2483 Label is_null_label = ig.DefineLabel ();
2484 Label end_label = ig.DefineLabel ();
2486 unwrap.EmitCheck (ec, is_null_label);
2489 ig.Emit (OpCodes.Br, end_label);
2491 ig.MarkLabel (is_null_label);
2492 null_value.Emit (ec);
2494 ig.MarkLabel (end_label);
2497 public void AddressOf (EmitContext ec, AddressOp mode)
2499 unwrap.AddressOf (ec, mode);
2503 public class LiftedConversion : Lifted
2505 public readonly bool IsUser;
2506 public readonly bool IsExplicit;
2507 public readonly Type TargetType;
2509 public LiftedConversion (Expression expr, Type target_type, bool is_user,
2510 bool is_explicit, Location loc)
2513 this.IsUser = is_user;
2514 this.IsExplicit = is_explicit;
2515 this.TargetType = target_type;
2518 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2520 Type type = TypeManager.GetTypeArguments (TargetType) [0];
2523 return Convert.UserDefinedConversion (ec, unwrap, type, loc, IsExplicit);
2526 return Convert.ExplicitConversion (ec, unwrap, type, loc);
2528 return Convert.ImplicitConversion (ec, unwrap, type, loc);
2533 public class LiftedUnaryOperator : Lifted
2535 public readonly Unary.Operator Oper;
2537 public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
2543 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2545 return new Unary (Oper, unwrap, loc);
2549 public class LiftedConditional : Lifted
2551 Expression expr, true_expr, false_expr;
2554 public LiftedConditional (Expression expr, Expression true_expr, Expression false_expr,
2558 this.true_expr = true_expr;
2559 this.false_expr = false_expr;
2562 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
2564 return new Conditional (unwrap, true_expr, false_expr, loc);
2568 public class LiftedBinaryOperator : Expression
2570 public readonly Binary.Operator Oper;
2572 Expression left, right, underlying, null_value;
2573 Unwrap left_unwrap, right_unwrap;
2575 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
2584 public override Expression DoResolve (EmitContext ec)
2586 left = left.Resolve (ec);
2590 right = right.Resolve (ec);
2594 if (TypeManager.IsNullableType (left.Type)) {
2595 left_unwrap = new Unwrap (left, loc);
2596 left = left_unwrap.Resolve (ec);
2601 if (TypeManager.IsNullableType (right.Type)) {
2602 right_unwrap = new Unwrap (right, loc);
2603 right = right_unwrap.Resolve (ec);
2608 underlying = new Wrap (new Binary (Oper, left, right, loc), loc);
2609 underlying = underlying.Resolve (ec);
2610 if (underlying == null)
2613 null_value = new NullLiteral (underlying.Type, loc).Resolve (ec);
2614 if (null_value == null)
2617 type = underlying.Type;
2618 eclass = ExprClass.Value;
2622 public override void Emit (EmitContext ec)
2624 ILGenerator ig = ec.ig;
2626 Label is_null_label = ig.DefineLabel ();
2627 Label end_label = ig.DefineLabel ();
2629 if (left_unwrap != null)
2630 left_unwrap.EmitCheck (ec, is_null_label);
2632 if (right_unwrap != null)
2633 right_unwrap.EmitCheck (ec, is_null_label);
2635 underlying.Emit (ec);
2636 ig.Emit (OpCodes.Br, end_label);
2638 ig.MarkLabel (is_null_label);
2639 null_value.Emit (ec);
2641 ig.MarkLabel (end_label);