2 // generic.cs: Generics support
4 // Authors: Martin Baulig (martin@ximian.com)
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Globalization;
17 using System.Collections;
19 using System.Text.RegularExpressions;
21 namespace Mono.CSharp {
24 /// Abstract base class for type parameter constraints.
25 /// The type parameter can come from a generic type definition or from reflection.
27 public abstract class GenericConstraints {
28 public abstract string TypeParameter {
32 public abstract GenericParameterAttributes Attributes {
36 public bool HasConstructorConstraint {
37 get { return (Attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
40 public bool HasReferenceTypeConstraint {
41 get { return (Attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
44 public bool HasValueTypeConstraint {
45 get { return (Attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
48 public virtual bool HasClassConstraint {
49 get { return ClassConstraint != null; }
52 public abstract Type ClassConstraint {
56 public abstract Type[] InterfaceConstraints {
60 public abstract Type EffectiveBaseClass {
65 // Returns whether the type parameter is "known to be a reference type".
67 public virtual bool IsReferenceType {
69 if (HasReferenceTypeConstraint)
71 if (HasValueTypeConstraint)
74 if (ClassConstraint != null) {
75 if (ClassConstraint.IsValueType)
78 if (ClassConstraint != TypeManager.object_type)
82 foreach (Type t in InterfaceConstraints) {
83 if (!t.IsGenericParameter)
86 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
87 if ((gc != null) && gc.IsReferenceType)
96 // Returns whether the type parameter is "known to be a value type".
98 public virtual bool IsValueType {
100 if (HasValueTypeConstraint)
102 if (HasReferenceTypeConstraint)
105 if (ClassConstraint != null) {
106 if (!ClassConstraint.IsValueType)
109 if (ClassConstraint != TypeManager.value_type)
113 foreach (Type t in InterfaceConstraints) {
114 if (!t.IsGenericParameter)
117 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
118 if ((gc != null) && gc.IsValueType)
127 public enum SpecialConstraint
135 /// Tracks the constraints for a type parameter from a generic type definition.
137 public class Constraints : GenericConstraints {
139 ArrayList constraints;
143 // name is the identifier, constraints is an arraylist of
144 // Expressions (with types) or `true' for the constructor constraint.
146 public Constraints (string name, ArrayList constraints,
150 this.constraints = constraints;
154 public override string TypeParameter {
160 public Constraints Clone ()
162 return new Constraints (name, constraints, loc);
165 GenericParameterAttributes attrs;
166 TypeExpr class_constraint;
167 ArrayList iface_constraints;
168 ArrayList type_param_constraints;
170 Type class_constraint_type;
171 Type[] iface_constraint_types;
172 Type effective_base_type;
177 /// Resolve the constraints - but only resolve things into Expression's, not
178 /// into actual types.
180 public bool Resolve (IResolveContext ec)
185 iface_constraints = new ArrayList (2); // TODO: Too expensive allocation
186 type_param_constraints = new ArrayList ();
188 foreach (object obj in constraints) {
189 if (HasConstructorConstraint) {
190 Report.Error (401, loc,
191 "The new() constraint must be the last constraint specified");
195 if (obj is SpecialConstraint) {
196 SpecialConstraint sc = (SpecialConstraint) obj;
198 if (sc == SpecialConstraint.Constructor) {
199 if (!HasValueTypeConstraint) {
200 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
204 Report.Error (451, loc, "The `new()' constraint " +
205 "cannot be used with the `struct' constraint");
209 if ((num_constraints > 0) || HasReferenceTypeConstraint || HasValueTypeConstraint) {
210 Report.Error (449, loc, "The `class' or `struct' " +
211 "constraint must be the first constraint specified");
215 if (sc == SpecialConstraint.ReferenceType)
216 attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
218 attrs |= GenericParameterAttributes.NotNullableValueTypeConstraint;
222 int errors = Report.Errors;
223 FullNamedExpression fn = ((Expression) obj).ResolveAsTypeStep (ec, false);
226 if (errors != Report.Errors)
229 NamespaceEntry.Error_NamespaceNotFound (loc, ((Expression)obj).GetSignatureForError ());
234 GenericTypeExpr cexpr = fn as GenericTypeExpr;
236 expr = cexpr.ResolveAsBaseTerminal (ec, false);
238 expr = ((Expression) obj).ResolveAsTypeTerminal (ec, false);
240 if ((expr == null) || (expr.Type == null))
243 if (!ec.GenericDeclContainer.IsAccessibleAs (fn.Type)) {
244 Report.SymbolRelatedToPreviousError (fn.Type);
245 Report.Error (703, loc,
246 "Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
247 fn.GetSignatureForError (), ec.GenericDeclContainer.GetSignatureForError ());
251 TypeParameterExpr texpr = expr as TypeParameterExpr;
253 type_param_constraints.Add (expr);
254 else if (expr.IsInterface)
255 iface_constraints.Add (expr);
256 else if (class_constraint != null || iface_constraints.Count != 0) {
257 Report.Error (406, loc,
258 "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
259 expr.GetSignatureForError ());
261 } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
262 Report.Error (450, loc, "`{0}': cannot specify both " +
263 "a constraint class and the `class' " +
264 "or `struct' constraint", expr.GetSignatureForError ());
267 class_constraint = expr;
272 ArrayList list = new ArrayList ();
273 foreach (TypeExpr iface_constraint in iface_constraints) {
274 foreach (Type type in list) {
275 if (!type.Equals (iface_constraint.Type))
278 Report.Error (405, loc,
279 "Duplicate constraint `{0}' for type " +
280 "parameter `{1}'.", iface_constraint.GetSignatureForError (),
285 list.Add (iface_constraint.Type);
288 foreach (TypeParameterExpr expr in type_param_constraints) {
289 foreach (Type type in list) {
290 if (!type.Equals (expr.Type))
293 Report.Error (405, loc,
294 "Duplicate constraint `{0}' for type " +
295 "parameter `{1}'.", expr.GetSignatureForError (), name);
299 list.Add (expr.Type);
302 iface_constraint_types = new Type [list.Count];
303 list.CopyTo (iface_constraint_types, 0);
305 if (class_constraint != null) {
306 class_constraint_type = class_constraint.Type;
307 if (class_constraint_type == null)
310 if (class_constraint_type.IsSealed) {
311 if (class_constraint_type.IsAbstract)
313 Report.Error (717, loc, "`{0}' is not a valid constraint. Static classes cannot be used as constraints",
314 TypeManager.CSharpName (class_constraint_type));
318 Report.Error (701, loc, "`{0}' is not a valid constraint. A constraint must be an interface, " +
319 "a non-sealed class or a type parameter", TypeManager.CSharpName(class_constraint_type));
324 if ((class_constraint_type == TypeManager.array_type) ||
325 (class_constraint_type == TypeManager.delegate_type) ||
326 (class_constraint_type == TypeManager.enum_type) ||
327 (class_constraint_type == TypeManager.value_type) ||
328 (class_constraint_type == TypeManager.object_type) ||
329 class_constraint_type == TypeManager.multicast_delegate_type) {
330 Report.Error (702, loc,
331 "A constraint cannot be special class `{0}'",
332 TypeManager.CSharpName (class_constraint_type));
337 if (class_constraint_type != null)
338 effective_base_type = class_constraint_type;
339 else if (HasValueTypeConstraint)
340 effective_base_type = TypeManager.value_type;
342 effective_base_type = TypeManager.object_type;
344 if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
345 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
351 bool CheckTypeParameterConstraints (TypeParameter tparam, ref TypeExpr prevConstraint, ArrayList seen)
355 Constraints constraints = tparam.Constraints;
356 if (constraints == null)
359 if (constraints.HasValueTypeConstraint) {
360 Report.Error (456, loc,
361 "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
367 // Checks whether there are no conflicts between type parameter constraints
371 // where U : A, B // A and B are not convertible
373 if (constraints.HasClassConstraint) {
374 if (prevConstraint != null) {
375 Type t2 = constraints.ClassConstraint;
376 TypeExpr e2 = constraints.class_constraint;
378 if (!Convert.ImplicitReferenceConversionExists (prevConstraint, t2) &&
379 !Convert.ImplicitReferenceConversionExists (e2, prevConstraint.Type)) {
380 Report.Error (455, loc,
381 "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
382 name, TypeManager.CSharpName (prevConstraint.Type), TypeManager.CSharpName (t2));
387 prevConstraint = constraints.class_constraint;
390 if (constraints.type_param_constraints == null)
393 foreach (TypeParameterExpr expr in constraints.type_param_constraints) {
394 if (seen.Contains (expr.TypeParameter)) {
395 Report.Error (454, loc, "Circular constraint " +
396 "dependency involving `{0}' and `{1}'",
397 tparam.Name, expr.GetSignatureForError ());
401 if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prevConstraint, seen))
409 /// Resolve the constraints into actual types.
411 public bool ResolveTypes (IResolveContext ec)
416 resolved_types = true;
418 foreach (object obj in constraints) {
419 GenericTypeExpr cexpr = obj as GenericTypeExpr;
423 if (!cexpr.CheckConstraints (ec))
427 if (type_param_constraints.Count != 0) {
428 ArrayList seen = new ArrayList ();
429 TypeExpr prev_constraint = class_constraint;
430 foreach (TypeParameterExpr expr in type_param_constraints) {
431 if (!CheckTypeParameterConstraints (expr.TypeParameter, ref prev_constraint, seen))
437 for (int i = 0; i < iface_constraints.Count; ++i) {
438 TypeExpr iface_constraint = (TypeExpr) iface_constraints [i];
439 iface_constraint = iface_constraint.ResolveAsTypeTerminal (ec, false);
440 if (iface_constraint == null)
442 iface_constraints [i] = iface_constraint;
445 if (class_constraint != null) {
446 class_constraint = class_constraint.ResolveAsTypeTerminal (ec, false);
447 if (class_constraint == null)
454 public override GenericParameterAttributes Attributes {
455 get { return attrs; }
458 public override bool HasClassConstraint {
459 get { return class_constraint != null; }
462 public override Type ClassConstraint {
463 get { return class_constraint_type; }
466 public override Type[] InterfaceConstraints {
467 get { return iface_constraint_types; }
470 public override Type EffectiveBaseClass {
471 get { return effective_base_type; }
474 public bool IsSubclassOf (Type t)
476 if ((class_constraint_type != null) &&
477 class_constraint_type.IsSubclassOf (t))
480 if (iface_constraint_types == null)
483 foreach (Type iface in iface_constraint_types) {
484 if (TypeManager.IsSubclassOf (iface, t))
491 public Location Location {
498 /// This is used when we're implementing a generic interface method.
499 /// Each method type parameter in implementing method must have the same
500 /// constraints than the corresponding type parameter in the interface
501 /// method. To do that, we're called on each of the implementing method's
504 public bool AreEqual (GenericConstraints gc)
506 if (gc.Attributes != attrs)
509 if (HasClassConstraint != gc.HasClassConstraint)
511 if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
514 int gc_icount = gc.InterfaceConstraints != null ?
515 gc.InterfaceConstraints.Length : 0;
516 int icount = InterfaceConstraints != null ?
517 InterfaceConstraints.Length : 0;
519 if (gc_icount != icount)
522 for (int i = 0; i < gc.InterfaceConstraints.Length; ++i) {
523 Type iface = gc.InterfaceConstraints [i];
524 if (iface.IsGenericType)
525 iface = iface.GetGenericTypeDefinition ();
528 for (int ii = 0; i < InterfaceConstraints.Length; ++ii) {
529 Type check = InterfaceConstraints [ii];
530 if (check.IsGenericType)
531 check = check.GetGenericTypeDefinition ();
533 if (TypeManager.IsEqual (iface, check)) {
546 public void VerifyClsCompliance ()
548 if (class_constraint_type != null && !AttributeTester.IsClsCompliant (class_constraint_type))
549 Warning_ConstrainIsNotClsCompliant (class_constraint_type, class_constraint.Location);
551 if (iface_constraint_types != null) {
552 for (int i = 0; i < iface_constraint_types.Length; ++i) {
553 if (!AttributeTester.IsClsCompliant (iface_constraint_types [i]))
554 Warning_ConstrainIsNotClsCompliant (iface_constraint_types [i],
555 ((TypeExpr)iface_constraints [i]).Location);
560 void Warning_ConstrainIsNotClsCompliant (Type t, Location loc)
562 Report.SymbolRelatedToPreviousError (t);
563 Report.Warning (3024, 1, loc, "Constraint type `{0}' is not CLS-compliant",
564 TypeManager.CSharpName (t));
569 /// A type parameter from a generic type definition.
571 public class TypeParameter : MemberCore, IMemberContainer
573 static readonly string[] attribute_target = new string [] { "type parameter" };
576 GenericConstraints gc;
577 Constraints constraints;
578 GenericTypeParameterBuilder type;
579 MemberCache member_cache;
581 public TypeParameter (DeclSpace parent, DeclSpace decl, string name,
582 Constraints constraints, Attributes attrs, Location loc)
583 : base (parent, new MemberName (name, loc), attrs)
586 this.constraints = constraints;
589 public GenericConstraints GenericConstraints {
590 get { return gc != null ? gc : constraints; }
593 public Constraints Constraints {
594 get { return constraints; }
597 public DeclSpace DeclSpace {
606 /// This is the first method which is called during the resolving
607 /// process; we're called immediately after creating the type parameters
608 /// with SRE (by calling `DefineGenericParameters()' on the TypeBuilder /
611 /// We're either called from TypeContainer.DefineType() or from
612 /// GenericMethod.Define() (called from Method.Define()).
614 public void Define (GenericTypeParameterBuilder type)
616 if (this.type != null)
617 throw new InvalidOperationException ();
620 TypeManager.AddTypeParameter (type, this);
624 /// This is the second method which is called during the resolving
625 /// process - in case of class type parameters, we're called from
626 /// TypeContainer.ResolveType() - after it resolved the class'es
627 /// base class and interfaces. For method type parameters, we're
628 /// called immediately after Define().
630 /// We're just resolving the constraints into expressions here, we
631 /// don't resolve them into actual types.
633 /// Note that in the special case of partial generic classes, we may be
634 /// called _before_ Define() and we may also be called multiple types.
636 public bool Resolve (DeclSpace ds)
638 if (constraints != null) {
639 if (!constraints.Resolve (ds)) {
649 /// This is the third method which is called during the resolving
650 /// process. We're called immediately after calling DefineConstraints()
651 /// on all of the current class'es type parameters.
653 /// Our job is to resolve the constraints to actual types.
655 /// Note that we may have circular dependencies on type parameters - this
656 /// is why Resolve() and ResolveType() are separate.
658 public bool ResolveType (IResolveContext ec)
660 if (constraints != null) {
661 if (!constraints.ResolveTypes (ec)) {
671 /// This is the fourth and last method which is called during the resolving
672 /// process. We're called after everything is fully resolved and actually
673 /// register the constraints with SRE and the TypeManager.
675 public bool DefineType (IResolveContext ec)
677 return DefineType (ec, null, null, false);
681 /// This is the fith and last method which is called during the resolving
682 /// process. We're called after everything is fully resolved and actually
683 /// register the constraints with SRE and the TypeManager.
685 /// The `builder', `implementing' and `is_override' arguments are only
686 /// applicable to method type parameters.
688 public bool DefineType (IResolveContext ec, MethodBuilder builder,
689 MethodInfo implementing, bool is_override)
691 if (!ResolveType (ec))
694 if (implementing != null) {
695 if (is_override && (constraints != null)) {
696 Report.Error (460, Location,
697 "`{0}': Cannot specify constraints for overrides or explicit interface implementation methods",
698 TypeManager.CSharpSignature (builder));
702 MethodBase mb = TypeManager.DropGenericMethodArguments (implementing);
704 int pos = type.GenericParameterPosition;
705 Type mparam = mb.GetGenericArguments () [pos];
706 GenericConstraints temp_gc = ReflectionConstraints.GetConstraints (mparam);
709 gc = new InflatedConstraints (temp_gc, implementing.DeclaringType);
710 else if (constraints != null)
711 gc = new InflatedConstraints (constraints, implementing.DeclaringType);
714 if (constraints != null) {
717 else if (!constraints.AreEqual (gc))
720 if (!is_override && (temp_gc != null))
725 Report.SymbolRelatedToPreviousError (implementing);
728 425, Location, "The constraints for type " +
729 "parameter `{0}' of method `{1}' must match " +
730 "the constraints for type parameter `{2}' " +
731 "of interface method `{3}'. Consider using " +
732 "an explicit interface implementation instead",
733 Name, TypeManager.CSharpSignature (builder),
734 TypeManager.CSharpName (mparam), TypeManager.CSharpSignature (mb));
737 } else if (DeclSpace is CompilerGeneratedClass) {
738 TypeParameter[] tparams = DeclSpace.TypeParameters;
739 Type[] types = new Type [tparams.Length];
740 for (int i = 0; i < tparams.Length; i++)
741 types [i] = tparams [i].Type;
743 if (constraints != null)
744 gc = new InflatedConstraints (constraints, types);
746 gc = (GenericConstraints) constraints;
752 if (gc.HasClassConstraint || gc.HasValueTypeConstraint)
753 type.SetBaseTypeConstraint (gc.EffectiveBaseClass);
755 type.SetInterfaceConstraints (gc.InterfaceConstraints);
756 type.SetGenericParameterAttributes (gc.Attributes);
757 TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
763 /// This is called for each part of a partial generic type definition.
765 /// If `new_constraints' is not null and we don't already have constraints,
766 /// they become our constraints. If we already have constraints, we must
767 /// check that they're the same.
770 public bool UpdateConstraints (IResolveContext ec, Constraints new_constraints)
773 throw new InvalidOperationException ();
775 if (new_constraints == null)
778 if (!new_constraints.Resolve (ec))
780 if (!new_constraints.ResolveTypes (ec))
783 if (constraints != null)
784 return constraints.AreEqual (new_constraints);
786 constraints = new_constraints;
790 public override void Emit ()
792 if (OptAttributes != null)
793 OptAttributes.Emit ();
798 public override string DocCommentHeader {
800 throw new InvalidOperationException (
801 "Unexpected attempt to get doc comment from " + this.GetType () + ".");
809 public override bool Define ()
814 public override void ApplyAttributeBuilder (Attribute a,
815 CustomAttributeBuilder cb)
817 type.SetCustomAttribute (cb);
820 public override AttributeTargets AttributeTargets {
822 return AttributeTargets.GenericParameter;
826 public override string[] ValidAttributeTargets {
828 return attribute_target;
836 string IMemberContainer.Name {
840 MemberCache IMemberContainer.BaseCache {
845 if (gc.EffectiveBaseClass.BaseType == null)
848 return TypeManager.LookupMemberCache (gc.EffectiveBaseClass.BaseType);
852 bool IMemberContainer.IsInterface {
853 get { return false; }
856 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
858 throw new NotSupportedException ();
861 public MemberCache MemberCache {
863 if (member_cache != null)
869 Type[] ifaces = TypeManager.ExpandInterfaces (gc.InterfaceConstraints);
870 member_cache = new MemberCache (this, gc.EffectiveBaseClass, ifaces);
876 public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
877 MemberFilter filter, object criteria)
880 return MemberList.Empty;
882 ArrayList members = new ArrayList ();
884 if (gc.HasClassConstraint) {
885 MemberList list = TypeManager.FindMembers (
886 gc.ClassConstraint, mt, bf, filter, criteria);
888 members.AddRange (list);
891 Type[] ifaces = TypeManager.ExpandInterfaces (gc.InterfaceConstraints);
892 foreach (Type t in ifaces) {
893 MemberList list = TypeManager.FindMembers (
894 t, mt, bf, filter, criteria);
896 members.AddRange (list);
899 return new MemberList (members);
902 public bool IsSubclassOf (Type t)
907 if (constraints != null)
908 return constraints.IsSubclassOf (t);
913 public static string GetSignatureForError (TypeParameter[] tp)
915 if (tp == null || tp.Length == 0)
918 StringBuilder sb = new StringBuilder ("<");
919 for (int i = 0; i < tp.Length; ++i) {
922 sb.Append (tp[i].GetSignatureForError ());
925 return sb.ToString ();
928 public void InflateConstraints (Type declaring)
930 if (constraints != null)
931 gc = new InflatedConstraints (constraints, declaring);
934 public override bool IsClsComplianceRequired ()
939 protected class InflatedConstraints : GenericConstraints
941 GenericConstraints gc;
943 Type class_constraint;
944 Type[] iface_constraints;
947 public InflatedConstraints (GenericConstraints gc, Type declaring)
948 : this (gc, TypeManager.GetTypeArguments (declaring))
951 public InflatedConstraints (GenericConstraints gc, Type[] dargs)
956 ArrayList list = new ArrayList ();
957 if (gc.HasClassConstraint)
958 list.Add (inflate (gc.ClassConstraint));
959 foreach (Type iface in gc.InterfaceConstraints)
960 list.Add (inflate (iface));
962 bool has_class_constr = false;
963 if (list.Count > 0) {
964 Type first = (Type) list [0];
965 has_class_constr = !first.IsGenericParameter && !first.IsInterface;
968 if ((list.Count > 0) && has_class_constr) {
969 class_constraint = (Type) list [0];
970 iface_constraints = new Type [list.Count - 1];
971 list.CopyTo (1, iface_constraints, 0, list.Count - 1);
973 iface_constraints = new Type [list.Count];
974 list.CopyTo (iface_constraints, 0);
977 if (HasValueTypeConstraint)
978 base_type = TypeManager.value_type;
979 else if (class_constraint != null)
980 base_type = class_constraint;
982 base_type = TypeManager.object_type;
985 Type inflate (Type t)
989 if (t.IsGenericParameter)
990 return t.GenericParameterPosition < dargs.Length ? dargs [t.GenericParameterPosition] : t;
991 if (t.IsGenericType) {
992 Type[] args = t.GetGenericArguments ();
993 Type[] inflated = new Type [args.Length];
995 for (int i = 0; i < args.Length; i++)
996 inflated [i] = inflate (args [i]);
998 t = t.GetGenericTypeDefinition ();
999 t = t.MakeGenericType (inflated);
1005 public override string TypeParameter {
1006 get { return gc.TypeParameter; }
1009 public override GenericParameterAttributes Attributes {
1010 get { return gc.Attributes; }
1013 public override Type ClassConstraint {
1014 get { return class_constraint; }
1017 public override Type EffectiveBaseClass {
1018 get { return base_type; }
1021 public override Type[] InterfaceConstraints {
1022 get { return iface_constraints; }
1028 /// A TypeExpr which already resolved to a type parameter.
1030 public class TypeParameterExpr : TypeExpr {
1031 TypeParameter type_parameter;
1033 public TypeParameter TypeParameter {
1035 return type_parameter;
1039 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
1041 this.type_parameter = type_parameter;
1045 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
1047 throw new NotSupportedException ();
1050 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
1052 type = type_parameter.Type;
1053 eclass = ExprClass.TypeParameter;
1057 public override bool IsInterface {
1058 get { return false; }
1061 public override bool CheckAccessLevel (DeclSpace ds)
1068 // Tracks the type arguments when instantiating a generic type. It's used
1069 // by both type arguments and type parameters
1071 public class TypeArguments {
1075 public TypeArguments ()
1077 args = new ArrayList ();
1080 public TypeArguments (params FullNamedExpression[] types)
1082 this.args = new ArrayList (types);
1085 public void Add (FullNamedExpression type)
1090 public void Add (TypeArguments new_args)
1092 args.AddRange (new_args.args);
1095 // TODO: Should be deleted
1096 public TypeParameterName[] GetDeclarations ()
1098 return (TypeParameterName[]) args.ToArray (typeof (TypeParameterName));
1102 /// We may only be used after Resolve() is called and return the fully
1105 public Type[] Arguments {
1117 public string GetSignatureForError()
1119 StringBuilder sb = new StringBuilder();
1120 for (int i = 0; i < Count; ++i)
1122 Expression expr = (Expression)args [i];
1123 sb.Append(expr.GetSignatureForError());
1127 return sb.ToString();
1131 /// Resolve the type arguments.
1133 public bool Resolve (IResolveContext ec)
1138 int count = args.Count;
1141 atypes = new Type [count];
1143 for (int i = 0; i < count; i++){
1144 TypeExpr te = ((FullNamedExpression) args[i]).ResolveAsTypeTerminal (ec, false);
1150 atypes[i] = te.Type;
1152 if (te.Type.IsSealed && te.Type.IsAbstract) {
1153 Report.Error (718, te.Location, "`{0}': static classes cannot be used as generic arguments",
1154 te.GetSignatureForError ());
1158 if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) {
1159 Report.Error (306, te.Location,
1160 "The type `{0}' may not be used as a type argument",
1161 te.GetSignatureForError ());
1168 public TypeArguments Clone ()
1170 TypeArguments copy = new TypeArguments ();
1171 foreach (Expression ta in args)
1178 public class TypeParameterName : SimpleName
1180 Attributes attributes;
1182 public TypeParameterName (string name, Attributes attrs, Location loc)
1188 public Attributes OptAttributes {
1196 /// A reference expression to generic type
1198 class GenericTypeExpr : TypeExpr
1201 Type[] gen_params; // TODO: Waiting for constrains check cleanup
1205 // Should be carefully used only with defined generic containers. Type parameters
1206 // can be used as type arguments in this case.
1208 // TODO: This could be GenericTypeExpr specialization
1210 public GenericTypeExpr (DeclSpace gType, Location l)
1212 open_type = gType.TypeBuilder.GetGenericTypeDefinition ();
1214 args = new TypeArguments ();
1215 foreach (TypeParameter type_param in gType.TypeParameters)
1216 args.Add (new TypeParameterExpr (type_param, l));
1222 /// Instantiate the generic type `t' with the type arguments `args'.
1223 /// Use this constructor if you already know the fully resolved
1226 public GenericTypeExpr (Type t, TypeArguments args, Location l)
1228 open_type = t.GetGenericTypeDefinition ();
1234 public TypeArguments TypeArguments {
1235 get { return args; }
1238 public override string GetSignatureForError ()
1240 return TypeManager.CSharpName (type);
1243 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
1245 if (eclass != ExprClass.Invalid)
1248 if (!args.Resolve (ec))
1251 gen_params = open_type.GetGenericArguments ();
1252 Type[] atypes = args.Arguments;
1254 if (atypes.Length != gen_params.Length) {
1255 Namespace.Error_InvalidNumberOfTypeArguments (open_type, loc);
1260 // Now bind the parameters
1262 type = open_type.MakeGenericType (atypes);
1263 eclass = ExprClass.Type;
1268 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1269 /// after fully resolving the constructed type.
1271 public bool CheckConstraints (IResolveContext ec)
1273 return ConstraintChecker.CheckConstraints (ec, open_type, gen_params, args.Arguments, loc);
1276 public override bool CheckAccessLevel (DeclSpace ds)
1278 return ds.CheckAccessLevel (open_type);
1281 public override bool AsAccessible (DeclSpace ds)
1283 foreach (Type t in args.Arguments) {
1284 if (!ds.IsAccessibleAs (t))
1288 return ds.IsAccessibleAs (open_type);
1291 public override bool IsClass {
1292 get { return open_type.IsClass; }
1295 public override bool IsValueType {
1296 get { return open_type.IsValueType; }
1299 public override bool IsInterface {
1300 get { return open_type.IsInterface; }
1303 public override bool IsSealed {
1304 get { return open_type.IsSealed; }
1307 public override bool Equals (object obj)
1309 GenericTypeExpr cobj = obj as GenericTypeExpr;
1313 if ((type == null) || (cobj.type == null))
1316 return type == cobj.type;
1319 public override int GetHashCode ()
1321 return base.GetHashCode ();
1325 public abstract class ConstraintChecker
1327 protected readonly Type[] gen_params;
1328 protected readonly Type[] atypes;
1329 protected readonly Location loc;
1331 protected ConstraintChecker (Type[] gen_params, Type[] atypes, Location loc)
1333 this.gen_params = gen_params;
1334 this.atypes = atypes;
1339 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1340 /// after fully resolving the constructed type.
1342 public bool CheckConstraints (IResolveContext ec)
1344 for (int i = 0; i < gen_params.Length; i++) {
1345 if (!CheckConstraints (ec, i))
1352 protected bool CheckConstraints (IResolveContext ec, int index)
1354 Type atype = atypes [index];
1355 Type ptype = gen_params [index];
1360 Expression aexpr = new EmptyExpression (atype);
1362 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1366 bool is_class, is_struct;
1367 if (atype.IsGenericParameter) {
1368 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1370 if (agc is Constraints)
1371 ((Constraints) agc).Resolve (ec);
1372 is_class = agc.IsReferenceType;
1373 is_struct = agc.IsValueType;
1375 is_class = is_struct = false;
1380 if (!atype.IsGenericType)
1382 is_class = atype.IsClass || atype.IsInterface;
1383 is_struct = atype.IsValueType && !TypeManager.IsNullableType (atype);
1387 // First, check the `class' and `struct' constraints.
1389 if (gc.HasReferenceTypeConstraint && !is_class) {
1390 Report.Error (452, loc, "The type `{0}' must be " +
1391 "a reference type in order to use it " +
1392 "as type parameter `{1}' in the " +
1393 "generic type or method `{2}'.",
1394 TypeManager.CSharpName (atype),
1395 TypeManager.CSharpName (ptype),
1396 GetSignatureForError ());
1398 } else if (gc.HasValueTypeConstraint && !is_struct) {
1399 Report.Error (453, loc, "The type `{0}' must be a " +
1400 "non-nullable value type in order to use it " +
1401 "as type parameter `{1}' in the " +
1402 "generic type or method `{2}'.",
1403 TypeManager.CSharpName (atype),
1404 TypeManager.CSharpName (ptype),
1405 GetSignatureForError ());
1410 // The class constraint comes next.
1412 if (gc.HasClassConstraint) {
1413 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint))
1418 // Now, check the interface constraints.
1420 if (gc.InterfaceConstraints != null) {
1421 foreach (Type it in gc.InterfaceConstraints) {
1422 if (!CheckConstraint (ec, ptype, aexpr, it))
1428 // Finally, check the constructor constraint.
1431 if (!gc.HasConstructorConstraint)
1434 if (TypeManager.IsBuiltinType (atype) || atype.IsValueType)
1437 if (HasDefaultConstructor (atype))
1440 Report_SymbolRelatedToPreviousError ();
1441 Report.SymbolRelatedToPreviousError (atype);
1442 Report.Error (310, loc, "The type `{0}' must have a public " +
1443 "parameterless constructor in order to use it " +
1444 "as parameter `{1}' in the generic type or " +
1446 TypeManager.CSharpName (atype),
1447 TypeManager.CSharpName (ptype),
1448 GetSignatureForError ());
1452 protected bool CheckConstraint (IResolveContext ec, Type ptype, Expression expr,
1455 if (TypeManager.HasGenericArguments (ctype)) {
1456 Type[] types = TypeManager.GetTypeArguments (ctype);
1458 TypeArguments new_args = new TypeArguments ();
1460 for (int i = 0; i < types.Length; i++) {
1463 if (t.IsGenericParameter) {
1464 int pos = t.GenericParameterPosition;
1467 new_args.Add (new TypeExpression (t, loc));
1470 TypeExpr ct = new GenericTypeExpr (ctype, new_args, loc);
1471 if (ct.ResolveAsTypeStep (ec, false) == null)
1474 } else if (ctype.IsGenericParameter) {
1475 int pos = ctype.GenericParameterPosition;
1476 ctype = atypes [pos];
1479 if (Convert.ImplicitStandardConversionExists (expr, ctype))
1482 Report_SymbolRelatedToPreviousError ();
1483 Report.SymbolRelatedToPreviousError (expr.Type);
1485 if (TypeManager.IsNullableType (expr.Type) && ctype.IsInterface) {
1486 Report.Error (313, loc,
1487 "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
1488 "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
1489 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ptype),
1490 GetSignatureForError (), TypeManager.CSharpName (ctype));
1492 Report.Error (309, loc,
1493 "The type `{0}' must be convertible to `{1}' in order to " +
1494 "use it as parameter `{2}' in the generic type or method `{3}'",
1495 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ctype),
1496 TypeManager.CSharpName (ptype), GetSignatureForError ());
1501 static bool HasDefaultConstructor (Type atype)
1503 TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
1504 if (tparam != null) {
1505 if (tparam.GenericConstraints == null)
1508 return tparam.GenericConstraints.HasConstructorConstraint ||
1509 tparam.GenericConstraints.HasValueTypeConstraint;
1512 if (atype.IsAbstract)
1516 atype = TypeManager.DropGenericTypeArguments (atype);
1517 if (atype is TypeBuilder) {
1518 TypeContainer tc = TypeManager.LookupTypeContainer (atype);
1519 if (tc.InstanceConstructors == null) {
1520 atype = atype.BaseType;
1524 foreach (Constructor c in tc.InstanceConstructors) {
1525 if ((c.ModFlags & Modifiers.PUBLIC) == 0)
1527 if ((c.Parameters.FixedParameters != null) &&
1528 (c.Parameters.FixedParameters.Length != 0))
1530 if (c.Parameters.HasArglist || c.Parameters.HasParams)
1537 MemberInfo [] list = TypeManager.MemberLookup (null, null, atype, MemberTypes.Constructor,
1538 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
1539 ConstructorInfo.ConstructorName, null);
1544 foreach (MethodBase mb in list) {
1545 AParametersCollection pd = TypeManager.GetParameterData (mb);
1553 protected abstract string GetSignatureForError ();
1554 protected abstract void Report_SymbolRelatedToPreviousError ();
1556 public static bool CheckConstraints (EmitContext ec, MethodBase definition,
1557 MethodBase instantiated, Location loc)
1559 MethodConstraintChecker checker = new MethodConstraintChecker (
1560 definition, definition.GetGenericArguments (),
1561 instantiated.GetGenericArguments (), loc);
1563 return checker.CheckConstraints (ec);
1566 public static bool CheckConstraints (IResolveContext ec, Type gt, Type[] gen_params,
1567 Type[] atypes, Location loc)
1569 TypeConstraintChecker checker = new TypeConstraintChecker (
1570 gt, gen_params, atypes, loc);
1572 return checker.CheckConstraints (ec);
1575 protected class MethodConstraintChecker : ConstraintChecker
1577 MethodBase definition;
1579 public MethodConstraintChecker (MethodBase definition, Type[] gen_params,
1580 Type[] atypes, Location loc)
1581 : base (gen_params, atypes, loc)
1583 this.definition = definition;
1586 protected override string GetSignatureForError ()
1588 return TypeManager.CSharpSignature (definition);
1591 protected override void Report_SymbolRelatedToPreviousError ()
1593 Report.SymbolRelatedToPreviousError (definition);
1597 protected class TypeConstraintChecker : ConstraintChecker
1601 public TypeConstraintChecker (Type gt, Type[] gen_params, Type[] atypes,
1603 : base (gen_params, atypes, loc)
1608 protected override string GetSignatureForError ()
1610 return TypeManager.CSharpName (gt);
1613 protected override void Report_SymbolRelatedToPreviousError ()
1615 Report.SymbolRelatedToPreviousError (gt);
1621 /// A generic method definition.
1623 public class GenericMethod : DeclSpace
1625 FullNamedExpression return_type;
1626 Parameters parameters;
1628 public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
1629 FullNamedExpression return_type, Parameters parameters)
1630 : base (ns, parent, name, null)
1632 this.return_type = return_type;
1633 this.parameters = parameters;
1636 public override TypeBuilder DefineType ()
1638 throw new Exception ();
1641 public override bool Define ()
1643 for (int i = 0; i < TypeParameters.Length; i++)
1644 if (!TypeParameters [i].Resolve (this))
1651 /// Define and resolve the type parameters.
1652 /// We're called from Method.Define().
1654 public bool Define (MethodBuilder mb)
1656 TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1657 string[] snames = new string [names.Length];
1658 for (int i = 0; i < names.Length; i++) {
1659 string type_argument_name = names[i].Name;
1660 Parameter p = parameters.GetParameterByName (type_argument_name);
1662 Error_ParameterNameCollision (p.Location, type_argument_name, "method parameter");
1666 snames[i] = type_argument_name;
1669 GenericTypeParameterBuilder[] gen_params = mb.DefineGenericParameters (snames);
1670 for (int i = 0; i < TypeParameters.Length; i++)
1671 TypeParameters [i].Define (gen_params [i]);
1676 for (int i = 0; i < TypeParameters.Length; i++) {
1677 if (!TypeParameters [i].ResolveType (this))
1684 internal static void Error_ParameterNameCollision (Location loc, string name, string collisionWith)
1686 Report.Error (412, loc, "The type parameter name `{0}' is the same as `{1}'",
1687 name, collisionWith);
1691 /// We're called from MethodData.Define() after creating the MethodBuilder.
1693 public bool DefineType (EmitContext ec, MethodBuilder mb,
1694 MethodInfo implementing, bool is_override)
1696 for (int i = 0; i < TypeParameters.Length; i++)
1697 if (!TypeParameters [i].DefineType (
1698 ec, mb, implementing, is_override))
1701 bool ok = parameters.Resolve (ec);
1703 if ((return_type != null) && (return_type.ResolveAsTypeTerminal (ec, false) == null))
1709 public void EmitAttributes ()
1711 for (int i = 0; i < TypeParameters.Length; i++)
1712 TypeParameters [i].Emit ();
1714 if (OptAttributes != null)
1715 OptAttributes.Emit ();
1718 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1719 MemberFilter filter, object criteria)
1721 throw new Exception ();
1724 public override MemberCache MemberCache {
1730 public override AttributeTargets AttributeTargets {
1732 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1736 public override string DocCommentHeader {
1737 get { return "M:"; }
1740 public new void VerifyClsCompliance ()
1742 foreach (TypeParameter tp in TypeParameters) {
1743 if (tp.Constraints == null)
1746 tp.Constraints.VerifyClsCompliance ();
1751 public partial class TypeManager
1753 static public Type activator_type;
1755 public static TypeContainer LookupGenericTypeContainer (Type t)
1757 t = DropGenericTypeArguments (t);
1758 return LookupTypeContainer (t);
1762 /// Check whether `a' and `b' may become equal generic types.
1763 /// The algorithm to do that is a little bit complicated.
1765 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_inferred,
1766 Type[] method_inferred)
1768 if (a.IsGenericParameter) {
1770 // If a is an array of a's type, they may never
1774 b = b.GetElementType ();
1780 // If b is a generic parameter or an actual type,
1781 // they may become equal:
1783 // class X<T,U> : I<T>, I<U>
1784 // class X<T> : I<T>, I<float>
1786 if (b.IsGenericParameter || !b.IsGenericType) {
1787 int pos = a.GenericParameterPosition;
1788 Type[] args = a.DeclaringMethod != null ? method_inferred : class_inferred;
1789 if (args [pos] == null) {
1794 return args [pos] == a;
1798 // We're now comparing a type parameter with a
1799 // generic instance. They may become equal unless
1800 // the type parameter appears anywhere in the
1801 // generic instance:
1803 // class X<T,U> : I<T>, I<X<U>>
1804 // -> error because you could instanciate it as
1807 // class X<T> : I<T>, I<X<T>> -> ok
1810 Type[] bargs = GetTypeArguments (b);
1811 for (int i = 0; i < bargs.Length; i++) {
1812 if (a.Equals (bargs [i]))
1819 if (b.IsGenericParameter)
1820 return MayBecomeEqualGenericTypes (b, a, class_inferred, method_inferred);
1823 // At this point, neither a nor b are a type parameter.
1825 // If one of them is a generic instance, let
1826 // MayBecomeEqualGenericInstances() compare them (if the
1827 // other one is not a generic instance, they can never
1831 if (a.IsGenericType || b.IsGenericType)
1832 return MayBecomeEqualGenericInstances (a, b, class_inferred, method_inferred);
1835 // If both of them are arrays.
1838 if (a.IsArray && b.IsArray) {
1839 if (a.GetArrayRank () != b.GetArrayRank ())
1842 a = a.GetElementType ();
1843 b = b.GetElementType ();
1845 return MayBecomeEqualGenericTypes (a, b, class_inferred, method_inferred);
1849 // Ok, two ordinary types.
1852 return a.Equals (b);
1856 // Checks whether two generic instances may become equal for some
1857 // particular instantiation (26.3.1).
1859 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1860 Type[] class_inferred,
1861 Type[] method_inferred)
1863 if (!a.IsGenericType || !b.IsGenericType)
1865 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1868 return MayBecomeEqualGenericInstances (
1869 GetTypeArguments (a), GetTypeArguments (b), class_inferred, method_inferred);
1872 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1873 Type[] class_inferred,
1874 Type[] method_inferred)
1876 if (aargs.Length != bargs.Length)
1879 for (int i = 0; i < aargs.Length; i++) {
1880 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_inferred, method_inferred))
1888 /// Type inference. Try to infer the type arguments from `method',
1889 /// which is invoked with the arguments `arguments'. This is used
1890 /// when resolving an Invocation or a DelegateInvocation and the user
1891 /// did not explicitly specify type arguments.
1893 public static int InferTypeArguments (EmitContext ec,
1894 ArrayList arguments,
1895 ref MethodBase method)
1897 ATypeInference ti = ATypeInference.CreateInstance (arguments);
1898 Type[] i_args = ti.InferMethodArguments (ec, method);
1900 return ti.InferenceScore;
1902 if (i_args.Length == 0)
1905 method = ((MethodInfo) method).MakeGenericMethod (i_args);
1912 public static bool InferTypeArguments (AParametersCollection apd,
1913 ref MethodBase method)
1915 if (!TypeManager.IsGenericMethod (method))
1918 ATypeInference ti = ATypeInference.CreateInstance (ArrayList.Adapter (apd.Types));
1919 Type[] i_args = ti.InferDelegateArguments (method);
1923 method = ((MethodInfo) method).MakeGenericMethod (i_args);
1928 abstract class ATypeInference
1930 protected readonly ArrayList arguments;
1931 protected readonly int arg_count;
1933 protected ATypeInference (ArrayList arguments)
1935 this.arguments = arguments;
1936 if (arguments != null)
1937 arg_count = arguments.Count;
1940 public static ATypeInference CreateInstance (ArrayList arguments)
1942 if (RootContext.Version == LanguageVersion.ISO_2)
1943 return new TypeInferenceV2 (arguments);
1945 return new TypeInferenceV3 (arguments);
1948 public virtual int InferenceScore {
1950 return int.MaxValue;
1954 public abstract Type[] InferMethodArguments (EmitContext ec, MethodBase method);
1955 public abstract Type[] InferDelegateArguments (MethodBase method);
1959 // Implements C# 2.0 type inference
1961 class TypeInferenceV2 : ATypeInference
1963 public TypeInferenceV2 (ArrayList arguments)
1968 public override Type[] InferDelegateArguments (MethodBase method)
1970 AParametersCollection pd = TypeManager.GetParameterData (method);
1971 if (arg_count != pd.Count)
1974 Type[] method_args = method.GetGenericArguments ();
1975 Type[] inferred_types = new Type[method_args.Length];
1977 Type[] param_types = new Type[pd.Count];
1978 Type[] arg_types = (Type[])arguments.ToArray (typeof (Type));
1980 for (int i = 0; i < arg_count; i++) {
1981 param_types[i] = pd.Types [i];
1984 if (!InferTypeArguments (param_types, arg_types, inferred_types))
1987 return inferred_types;
1990 public override Type[] InferMethodArguments (EmitContext ec, MethodBase method)
1992 AParametersCollection pd = TypeManager.GetParameterData (method);
1993 Type[] method_generic_args = method.GetGenericArguments ();
1994 Type [] inferred_types = new Type [method_generic_args.Length];
1995 Type[] arg_types = new Type [pd.Count];
1997 int a_count = arg_types.Length;
2001 for (int i = 0; i < a_count; i++) {
2002 Argument a = (Argument) arguments[i];
2003 if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
2006 if (!TypeInferenceV2.UnifyType (pd.Types [i], a.Type, inferred_types))
2011 Type element_type = TypeManager.GetElementType (pd.Types [a_count]);
2012 for (int i = a_count; i < arg_count; i++) {
2013 Argument a = (Argument) arguments [i];
2014 if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
2017 if (!TypeInferenceV2.UnifyType (element_type, a.Type, inferred_types))
2022 for (int i = 0; i < inferred_types.Length; i++)
2023 if (inferred_types [i] == null)
2026 return inferred_types;
2029 static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
2030 Type[] inferred_types)
2032 for (int i = 0; i < arg_types.Length; i++) {
2033 if (arg_types[i] == null)
2036 if (!UnifyType (param_types[i], arg_types[i], inferred_types))
2040 for (int i = 0; i < inferred_types.Length; ++i)
2041 if (inferred_types[i] == null)
2047 public static bool UnifyType (Type pt, Type at, Type[] inferred)
2049 if (pt.IsGenericParameter) {
2050 if (pt.DeclaringMethod == null)
2053 int pos = pt.GenericParameterPosition;
2055 if (inferred [pos] == null)
2056 inferred [pos] = at;
2058 return inferred [pos] == at;
2061 if (!pt.ContainsGenericParameters) {
2062 if (at.ContainsGenericParameters)
2063 return UnifyType (at, pt, inferred);
2070 if (at.GetArrayRank () != pt.GetArrayRank ())
2073 return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
2076 if (!pt.IsGenericType)
2079 Type gt = pt.GetGenericTypeDefinition ();
2080 if ((gt != TypeManager.generic_ilist_type) && (gt != TypeManager.generic_icollection_type) &&
2081 (gt != TypeManager.generic_ienumerable_type))
2084 Type[] args = TypeManager.GetTypeArguments (pt);
2085 return UnifyType (args[0], at.GetElementType (), inferred);
2090 (pt.GetArrayRank () != at.GetArrayRank ()))
2093 return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
2096 if (pt.IsByRef && at.IsByRef)
2097 return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
2098 ArrayList list = new ArrayList ();
2099 if (at.IsGenericType)
2101 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
2104 list.AddRange (TypeManager.GetInterfaces (at));
2106 foreach (Type type in list) {
2107 if (!type.IsGenericType)
2110 if (TypeManager.DropGenericTypeArguments (pt) != TypeManager.DropGenericTypeArguments (type))
2113 if (!UnifyTypes (pt.GetGenericArguments (), type.GetGenericArguments (), inferred))
2120 static bool UnifyTypes (Type[] pts, Type[] ats, Type[] inferred)
2122 for (int i = 0; i < ats.Length; i++) {
2123 if (!UnifyType (pts [i], ats [i], inferred))
2131 // Implements C# 3.0 type inference
2133 class TypeInferenceV3 : ATypeInference
2136 // Tracks successful rate of type inference
2138 int score = int.MaxValue;
2140 public TypeInferenceV3 (ArrayList arguments)
2145 public override int InferenceScore {
2151 public override Type[] InferDelegateArguments (MethodBase method)
2153 AParametersCollection pd = TypeManager.GetParameterData (method);
2154 if (arg_count != pd.Count)
2157 Type[] d_gargs = method.GetGenericArguments ();
2158 TypeInferenceContext context = new TypeInferenceContext (d_gargs);
2160 // A lower-bound inference is made from each argument type Uj of D
2161 // to the corresponding parameter type Tj of M
2162 for (int i = 0; i < arg_count; ++i) {
2163 Type t = pd.Types [i];
2164 if (!t.IsGenericParameter)
2167 context.LowerBoundInference ((Type)arguments[i], t);
2170 if (!context.FixAllTypes ())
2173 return context.InferredTypeArguments;
2176 public override Type[] InferMethodArguments (EmitContext ec, MethodBase method)
2178 Type[] method_generic_args = method.GetGenericArguments ();
2179 TypeInferenceContext context = new TypeInferenceContext (method_generic_args);
2180 if (!context.UnfixedVariableExists)
2181 return Type.EmptyTypes;
2183 AParametersCollection pd = TypeManager.GetParameterData (method);
2184 if (!InferInPhases (ec, context, pd))
2187 return context.InferredTypeArguments;
2191 // Implements method type arguments inference
2193 bool InferInPhases (EmitContext ec, TypeInferenceContext tic, AParametersCollection methodParameters)
2195 int params_arguments_start;
2196 if (methodParameters.HasParams) {
2197 params_arguments_start = methodParameters.Count - 1;
2199 params_arguments_start = arg_count;
2202 Type [] ptypes = methodParameters.Types;
2205 // The first inference phase
2207 Type method_parameter = null;
2208 for (int i = 0; i < arg_count; i++) {
2209 Argument a = (Argument) arguments [i];
2211 if (i < params_arguments_start) {
2212 method_parameter = methodParameters.Types [i];
2213 } else if (i == params_arguments_start) {
2214 if (arg_count == params_arguments_start + 1 && TypeManager.HasElementType (a.Type))
2215 method_parameter = methodParameters.Types [params_arguments_start];
2217 method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
2219 ptypes = (Type[]) ptypes.Clone ();
2220 ptypes [i] = method_parameter;
2224 // When a lambda expression, an anonymous method
2225 // is used an explicit argument type inference takes a place
2227 AnonymousMethodExpression am = a.Expr as AnonymousMethodExpression;
2229 if (am.ExplicitTypeInference (tic, method_parameter))
2234 if (a.Expr is NullLiteral)
2238 // Otherwise an output type inference is made
2240 score -= tic.OutputTypeInference (ec, a.Expr, method_parameter);
2244 // Part of the second phase but because it happens only once
2245 // we don't need to call it in cycle
2247 bool fixed_any = false;
2248 if (!tic.FixIndependentTypeArguments (ptypes, ref fixed_any))
2251 return DoSecondPhase (ec, tic, ptypes, !fixed_any);
2254 bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, Type[] methodParameters, bool fixDependent)
2256 bool fixed_any = false;
2257 if (fixDependent && !tic.FixDependentTypes (ref fixed_any))
2260 // If no further unfixed type variables exist, type inference succeeds
2261 if (!tic.UnfixedVariableExists)
2264 if (!fixed_any && fixDependent)
2267 // For all arguments where the corresponding argument output types
2268 // contain unfixed type variables but the input types do not,
2269 // an output type inference is made
2270 for (int i = 0; i < arg_count; i++) {
2272 // Align params arguments
2273 Type t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
2275 if (!TypeManager.IsDelegateType (t_i)) {
2276 if (TypeManager.DropGenericTypeArguments (t_i) != TypeManager.expression_type)
2279 t_i = t_i.GetGenericArguments () [0];
2282 MethodInfo mi = Delegate.GetInvokeMethod (t_i, t_i);
2283 Type rtype = mi.ReturnType;
2286 // Blablabla, because reflection does not work with dynamic types
2287 Type[] g_args = t_i.GetGenericArguments ();
2288 rtype = g_args[rtype.GenericParameterPosition];
2291 if (tic.IsReturnTypeNonDependent (mi, rtype))
2292 score -= tic.OutputTypeInference (ec, ((Argument) arguments [i]).Expr, t_i);
2296 return DoSecondPhase (ec, tic, methodParameters, true);
2300 public class TypeInferenceContext
2302 readonly Type[] unfixed_types;
2303 readonly Type[] fixed_types;
2304 readonly ArrayList[] bounds;
2307 public TypeInferenceContext (Type[] typeArguments)
2309 if (typeArguments.Length == 0)
2310 throw new ArgumentException ("Empty generic arguments");
2312 fixed_types = new Type [typeArguments.Length];
2313 for (int i = 0; i < typeArguments.Length; ++i) {
2314 if (typeArguments [i].IsGenericParameter) {
2315 if (bounds == null) {
2316 bounds = new ArrayList [typeArguments.Length];
2317 unfixed_types = new Type [typeArguments.Length];
2319 unfixed_types [i] = typeArguments [i];
2321 fixed_types [i] = typeArguments [i];
2326 public Type[] InferredTypeArguments {
2332 void AddToBounds (Type t, int index)
2335 // Some types cannot be used as type arguments
2337 if (t == TypeManager.void_type || t.IsPointer)
2340 ArrayList a = bounds [index];
2342 a = new ArrayList ();
2350 // SPEC: does not cover type inference using constraints
2352 //if (TypeManager.IsGenericParameter (t)) {
2353 // GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
2354 // if (constraints != null) {
2355 // //if (constraints.EffectiveBaseClass != null)
2356 // // t = constraints.EffectiveBaseClass;
2362 bool AllTypesAreFixed (Type[] types)
2364 foreach (Type t in types) {
2365 if (t.IsGenericParameter) {
2371 if (t.IsGenericType)
2372 return AllTypesAreFixed (t.GetGenericArguments ());
2379 // 26.3.3.8 Exact Inference
2381 public int ExactInference (Type u, Type v)
2383 // If V is an array type
2388 if (u.GetArrayRank () != v.GetArrayRank ())
2391 return ExactInference (TypeManager.GetElementType (u), TypeManager.GetElementType (v));
2394 // If V is constructed type and U is constructed type
2395 if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2396 if (!u.IsGenericType)
2399 Type [] ga_u = u.GetGenericArguments ();
2400 Type [] ga_v = v.GetGenericArguments ();
2401 if (ga_u.Length != ga_v.Length)
2405 for (int i = 0; i < ga_u.Length; ++i)
2406 score += ExactInference (ga_u [i], ga_v [i]);
2408 return score > 0 ? 1 : 0;
2411 // If V is one of the unfixed type arguments
2412 int pos = IsUnfixed (v);
2416 AddToBounds (u, pos);
2420 public bool FixAllTypes ()
2422 for (int i = 0; i < unfixed_types.Length; ++i) {
2430 // All unfixed type variables Xi are fixed for which all of the following hold:
2431 // a, There is at least one type variable Xj that depends on Xi
2432 // b, Xi has a non-empty set of bounds
2434 public bool FixDependentTypes (ref bool fixed_any)
2436 for (int i = 0; i < unfixed_types.Length; ++i) {
2437 if (unfixed_types[i] == null)
2440 if (bounds[i] == null)
2453 // All unfixed type variables Xi which depend on no Xj are fixed
2455 public bool FixIndependentTypeArguments (Type[] methodParameters, ref bool fixed_any)
2457 ArrayList types_to_fix = new ArrayList (unfixed_types);
2458 for (int i = 0; i < methodParameters.Length; ++i) {
2459 Type t = methodParameters[i];
2460 if (t.IsGenericParameter)
2463 if (!TypeManager.IsDelegateType (t)) {
2464 if (TypeManager.DropGenericTypeArguments (t) != TypeManager.expression_type)
2467 t = t.GetGenericArguments () [0];
2470 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2471 Type rtype = invoke.ReturnType;
2472 if (!rtype.IsGenericParameter && !rtype.IsGenericType)
2476 // Blablabla, because reflection does not work with dynamic types
2477 if (rtype.IsGenericParameter) {
2478 Type [] g_args = t.GetGenericArguments ();
2479 rtype = g_args [rtype.GenericParameterPosition];
2482 // Remove dependent types, they cannot be fixed yet
2483 RemoveDependentTypes (types_to_fix, rtype);
2486 foreach (Type t in types_to_fix) {
2490 int idx = IsUnfixed (t);
2491 if (idx >= 0 && !FixType (idx)) {
2496 fixed_any = types_to_fix.Count > 0;
2503 public bool FixType (int i)
2505 // It's already fixed
2506 if (unfixed_types[i] == null)
2507 throw new InternalErrorException ("Type argument has been already fixed");
2512 ArrayList candidates = (ArrayList)bounds [i];
2513 if (candidates == null)
2516 if (candidates.Count == 1) {
2517 unfixed_types[i] = null;
2518 fixed_types[i] = (Type)candidates[0];
2523 // Determines a unique type from which there is
2524 // a standard implicit conversion to all the other
2527 Type best_candidate = null;
2529 int candidates_count = candidates.Count;
2530 for (int ci = 0; ci < candidates_count; ++ci) {
2531 Type candidate = (Type)candidates [ci];
2532 for (cii = 0; cii < candidates_count; ++cii) {
2536 if (!Convert.ImplicitConversionExists (null,
2537 new TypeExpression ((Type)candidates [cii], Location.Null), candidate)) {
2542 if (cii != candidates_count)
2545 if (best_candidate != null)
2548 best_candidate = candidate;
2551 if (best_candidate == null)
2554 unfixed_types[i] = null;
2555 fixed_types[i] = best_candidate;
2560 // Uses inferred types to inflate delegate type argument
2562 public Type InflateGenericArgument (Type parameter)
2564 if (parameter.IsGenericParameter) {
2566 // Inflate method generic argument (MVAR) only
2568 if (parameter.DeclaringMethod == null)
2571 return fixed_types [parameter.GenericParameterPosition];
2574 if (parameter.IsGenericType) {
2575 Type [] parameter_targs = parameter.GetGenericArguments ();
2576 for (int ii = 0; ii < parameter_targs.Length; ++ii) {
2577 parameter_targs [ii] = InflateGenericArgument (parameter_targs [ii]);
2579 return parameter.GetGenericTypeDefinition ().MakeGenericType (parameter_targs);
2586 // Tests whether all delegate input arguments are fixed and generic output type
2587 // requires output type inference
2589 public bool IsReturnTypeNonDependent (MethodInfo invoke, Type returnType)
2591 if (returnType.IsGenericParameter) {
2592 if (IsFixed (returnType))
2594 } else if (returnType.IsGenericType) {
2595 if (TypeManager.IsDelegateType (returnType)) {
2596 invoke = Delegate.GetInvokeMethod (returnType, returnType);
2597 return IsReturnTypeNonDependent (invoke, invoke.ReturnType);
2600 Type[] g_args = returnType.GetGenericArguments ();
2602 // At least one unfixed return type has to exist
2603 if (AllTypesAreFixed (g_args))
2609 // All generic input arguments have to be fixed
2610 AParametersCollection d_parameters = TypeManager.GetParameterData (invoke);
2611 return AllTypesAreFixed (d_parameters.Types);
2614 bool IsFixed (Type type)
2616 return IsUnfixed (type) == -1;
2619 int IsUnfixed (Type type)
2621 if (!type.IsGenericParameter)
2624 //return unfixed_types[type.GenericParameterPosition] != null;
2625 for (int i = 0; i < unfixed_types.Length; ++i) {
2626 if (unfixed_types [i] == type)
2634 // 26.3.3.9 Lower-bound Inference
2636 public int LowerBoundInference (Type u, Type v)
2638 // If V is one of the unfixed type arguments
2639 int pos = IsUnfixed (v);
2641 AddToBounds (u, pos);
2645 // If U is an array type
2647 int u_dim = u.GetArrayRank ();
2649 Type u_e = TypeManager.GetElementType (u);
2652 if (u_dim != v.GetArrayRank ())
2655 v_e = TypeManager.GetElementType (v);
2658 return LowerBoundInference (u_e, v_e);
2661 return ExactInference (u_e, v_e);
2667 if (v.IsGenericType) {
2668 Type g_v = v.GetGenericTypeDefinition ();
2669 if ((g_v != TypeManager.generic_ilist_type) && (g_v != TypeManager.generic_icollection_type) &&
2670 (g_v != TypeManager.generic_ienumerable_type))
2673 v_e = TypeManager.GetTypeArguments (v)[0];
2676 return LowerBoundInference (u_e, v_e);
2679 return ExactInference (u_e, v_e);
2681 } else if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2683 // if V is a constructed type C<V1..Vk> and there is a unique set of types U1..Uk
2684 // such that a standard implicit conversion exists from U to C<U1..Uk> then an exact
2685 // inference is made from each Ui for the corresponding Vi
2687 ArrayList u_candidates = new ArrayList ();
2688 if (u.IsGenericType)
2689 u_candidates.Add (u);
2691 for (Type t = u.BaseType; t != null; t = t.BaseType) {
2692 if (t.IsGenericType && !t.IsGenericTypeDefinition)
2693 u_candidates.Add (t);
2696 // TODO: Implement GetGenericInterfaces only and remove
2697 // the if from foreach
2698 u_candidates.AddRange (TypeManager.GetInterfaces (u));
2700 Type open_v = v.GetGenericTypeDefinition ();
2701 Type [] unique_candidate_targs = null;
2702 Type [] ga_v = v.GetGenericArguments ();
2703 foreach (Type u_candidate in u_candidates) {
2704 if (!u_candidate.IsGenericType || u_candidate.IsGenericTypeDefinition)
2707 if (TypeManager.DropGenericTypeArguments (u_candidate) != open_v)
2711 // The unique set of types U1..Uk means that if we have an interface C<T>,
2712 // class U: C<int>, C<long> then no type inference is made when inferring
2713 // from U to C<T> because T could be int or long
2715 if (unique_candidate_targs != null) {
2716 Type[] second_unique_candidate_targs = u_candidate.GetGenericArguments ();
2717 if (TypeManager.IsEqual (unique_candidate_targs, second_unique_candidate_targs)) {
2718 unique_candidate_targs = second_unique_candidate_targs;
2723 // This should always cause type inference failure
2729 unique_candidate_targs = u_candidate.GetGenericArguments ();
2732 if (unique_candidate_targs != null) {
2734 for (int i = 0; i < unique_candidate_targs.Length; ++i)
2735 if (ExactInference (unique_candidate_targs [i], ga_v [i]) == 0)
2745 // 26.3.3.6 Output Type Inference
2747 public int OutputTypeInference (EmitContext ec, Expression e, Type t)
2749 // If e is a lambda or anonymous method with inferred return type
2750 AnonymousMethodExpression ame = e as AnonymousMethodExpression;
2752 Type rt = ame.InferReturnType (ec, this, t);
2753 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2756 AParametersCollection pd = TypeManager.GetParameterData (invoke);
2757 return ame.Parameters.Count == pd.Count ? 1 : 0;
2760 Type rtype = invoke.ReturnType;
2762 // Blablabla, because reflection does not work with dynamic types
2763 Type [] g_args = t.GetGenericArguments ();
2764 rtype = g_args [rtype.GenericParameterPosition];
2766 return LowerBoundInference (rt, rtype) + 1;
2770 // if E is a method group and T is a delegate type or expression tree type
2771 // return type Tb with parameter types T1..Tk and return type Tb, and overload
2772 // resolution of E with the types T1..Tk yields a single method with return type U,
2773 // then a lower-bound inference is made from U for Tb.
2775 if (e is MethodGroupExpr) {
2776 // TODO: Or expression tree
2777 if (!TypeManager.IsDelegateType (t))
2780 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2781 Type rtype = invoke.ReturnType;
2783 // Blablabla, because reflection does not work with dynamic types
2784 Type [] g_args = t.GetGenericArguments ();
2785 rtype = g_args [rtype.GenericParameterPosition];
2788 if (!TypeManager.IsGenericType (rtype))
2791 MethodGroupExpr mg = (MethodGroupExpr) e;
2792 ArrayList args = DelegateCreation.CreateDelegateMethodArguments (invoke, e.Location);
2793 mg = mg.OverloadResolve (ec, ref args, true, e.Location);
2797 // TODO: What should happen when return type is of generic type ?
2798 throw new NotImplementedException ();
2799 // return LowerBoundInference (null, rtype) + 1;
2803 // if e is an expression with type U, then
2804 // a lower-bound inference is made from U for T
2806 return LowerBoundInference (e.Type, t) * 2;
2809 static void RemoveDependentTypes (ArrayList types, Type returnType)
2811 if (returnType.IsGenericParameter) {
2812 types [returnType.GenericParameterPosition] = null;
2816 if (returnType.IsGenericType) {
2817 foreach (Type t in returnType.GetGenericArguments ()) {
2818 RemoveDependentTypes (types, t);
2823 public bool UnfixedVariableExists {
2825 if (unfixed_types == null)
2828 foreach (Type ut in unfixed_types)