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 void InflateConstraints (Type declaring)
915 if (constraints != null)
916 gc = new InflatedConstraints (constraints, declaring);
919 public override bool IsClsComplianceRequired ()
924 protected class InflatedConstraints : GenericConstraints
926 GenericConstraints gc;
928 Type class_constraint;
929 Type[] iface_constraints;
932 public InflatedConstraints (GenericConstraints gc, Type declaring)
933 : this (gc, TypeManager.GetTypeArguments (declaring))
936 public InflatedConstraints (GenericConstraints gc, Type[] dargs)
941 ArrayList list = new ArrayList ();
942 if (gc.HasClassConstraint)
943 list.Add (inflate (gc.ClassConstraint));
944 foreach (Type iface in gc.InterfaceConstraints)
945 list.Add (inflate (iface));
947 bool has_class_constr = false;
948 if (list.Count > 0) {
949 Type first = (Type) list [0];
950 has_class_constr = !first.IsGenericParameter && !first.IsInterface;
953 if ((list.Count > 0) && has_class_constr) {
954 class_constraint = (Type) list [0];
955 iface_constraints = new Type [list.Count - 1];
956 list.CopyTo (1, iface_constraints, 0, list.Count - 1);
958 iface_constraints = new Type [list.Count];
959 list.CopyTo (iface_constraints, 0);
962 if (HasValueTypeConstraint)
963 base_type = TypeManager.value_type;
964 else if (class_constraint != null)
965 base_type = class_constraint;
967 base_type = TypeManager.object_type;
970 Type inflate (Type t)
974 if (t.IsGenericParameter)
975 return t.GenericParameterPosition < dargs.Length ? dargs [t.GenericParameterPosition] : t;
976 if (t.IsGenericType) {
977 Type[] args = t.GetGenericArguments ();
978 Type[] inflated = new Type [args.Length];
980 for (int i = 0; i < args.Length; i++)
981 inflated [i] = inflate (args [i]);
983 t = t.GetGenericTypeDefinition ();
984 t = t.MakeGenericType (inflated);
990 public override string TypeParameter {
991 get { return gc.TypeParameter; }
994 public override GenericParameterAttributes Attributes {
995 get { return gc.Attributes; }
998 public override Type ClassConstraint {
999 get { return class_constraint; }
1002 public override Type EffectiveBaseClass {
1003 get { return base_type; }
1006 public override Type[] InterfaceConstraints {
1007 get { return iface_constraints; }
1013 /// A TypeExpr which already resolved to a type parameter.
1015 public class TypeParameterExpr : TypeExpr {
1016 TypeParameter type_parameter;
1018 public TypeParameter TypeParameter {
1020 return type_parameter;
1024 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
1026 this.type_parameter = type_parameter;
1030 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
1032 throw new NotSupportedException ();
1035 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
1037 type = type_parameter.Type;
1038 eclass = ExprClass.TypeParameter;
1042 public override bool IsInterface {
1043 get { return false; }
1046 public override bool CheckAccessLevel (DeclSpace ds)
1053 // Tracks the type arguments when instantiating a generic type. It's used
1054 // by both type arguments and type parameters
1056 public class TypeArguments {
1060 public TypeArguments ()
1062 args = new ArrayList ();
1065 public TypeArguments (params FullNamedExpression[] types)
1067 this.args = new ArrayList (types);
1070 public void Add (FullNamedExpression type)
1075 public void Add (TypeArguments new_args)
1077 args.AddRange (new_args.args);
1080 // TODO: Should be deleted
1081 public TypeParameterName[] GetDeclarations ()
1083 return (TypeParameterName[]) args.ToArray (typeof (TypeParameterName));
1087 /// We may only be used after Resolve() is called and return the fully
1090 public Type[] Arguments {
1102 public string GetSignatureForError()
1104 StringBuilder sb = new StringBuilder();
1105 for (int i = 0; i < Count; ++i)
1107 Expression expr = (Expression)args [i];
1108 sb.Append(expr.GetSignatureForError());
1112 return sb.ToString();
1116 /// Resolve the type arguments.
1118 public bool Resolve (IResolveContext ec)
1121 return atypes.Length != 0;
1123 int count = args.Count;
1126 atypes = new Type [count];
1128 for (int i = 0; i < count; i++){
1129 TypeExpr te = ((FullNamedExpression) args[i]).ResolveAsTypeTerminal (ec, false);
1135 atypes[i] = te.Type;
1137 if (te.Type.IsSealed && te.Type.IsAbstract) {
1138 Report.Error (718, te.Location, "`{0}': static classes cannot be used as generic arguments",
1139 te.GetSignatureForError ());
1143 if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) {
1144 Report.Error (306, te.Location,
1145 "The type `{0}' may not be used as a type argument",
1146 te.GetSignatureForError ());
1152 atypes = Type.EmptyTypes;
1157 public TypeArguments Clone ()
1159 TypeArguments copy = new TypeArguments ();
1160 foreach (Expression ta in args)
1167 public class TypeParameterName : SimpleName
1169 Attributes attributes;
1171 public TypeParameterName (string name, Attributes attrs, Location loc)
1177 public Attributes OptAttributes {
1185 /// A reference expression to generic type
1187 class GenericTypeExpr : TypeExpr
1190 Type[] gen_params; // TODO: Waiting for constrains check cleanup
1194 // Should be carefully used only with defined generic containers. Type parameters
1195 // can be used as type arguments in this case.
1197 // TODO: This could be GenericTypeExpr specialization
1199 public GenericTypeExpr (DeclSpace gType, Location l)
1201 open_type = gType.TypeBuilder.GetGenericTypeDefinition ();
1203 args = new TypeArguments ();
1204 foreach (TypeParameter type_param in gType.TypeParameters)
1205 args.Add (new TypeParameterExpr (type_param, l));
1211 /// Instantiate the generic type `t' with the type arguments `args'.
1212 /// Use this constructor if you already know the fully resolved
1215 public GenericTypeExpr (Type t, TypeArguments args, Location l)
1217 open_type = t.GetGenericTypeDefinition ();
1223 public TypeArguments TypeArguments {
1224 get { return args; }
1227 public override string GetSignatureForError ()
1229 return TypeManager.CSharpName (type);
1232 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
1234 if (eclass != ExprClass.Invalid)
1237 eclass = ExprClass.Type;
1239 if (!args.Resolve (ec))
1242 gen_params = open_type.GetGenericArguments ();
1243 Type[] atypes = args.Arguments;
1245 if (atypes.Length != gen_params.Length) {
1246 Namespace.Error_InvalidNumberOfTypeArguments (open_type, loc);
1251 // Now bind the parameters
1253 type = open_type.MakeGenericType (atypes);
1258 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1259 /// after fully resolving the constructed type.
1261 public bool CheckConstraints (IResolveContext ec)
1263 return ConstraintChecker.CheckConstraints (ec, open_type, gen_params, args.Arguments, loc);
1266 public override bool CheckAccessLevel (DeclSpace ds)
1268 return ds.CheckAccessLevel (open_type);
1271 public override bool AsAccessible (DeclSpace ds)
1273 foreach (Type t in args.Arguments) {
1274 if (!ds.IsAccessibleAs (t))
1278 return ds.IsAccessibleAs (open_type);
1281 public override bool IsClass {
1282 get { return open_type.IsClass; }
1285 public override bool IsValueType {
1286 get { return TypeManager.IsStruct (open_type); }
1289 public override bool IsInterface {
1290 get { return open_type.IsInterface; }
1293 public override bool IsSealed {
1294 get { return open_type.IsSealed; }
1297 public override bool Equals (object obj)
1299 GenericTypeExpr cobj = obj as GenericTypeExpr;
1303 if ((type == null) || (cobj.type == null))
1306 return type == cobj.type;
1309 public override int GetHashCode ()
1311 return base.GetHashCode ();
1315 public abstract class ConstraintChecker
1317 protected readonly Type[] gen_params;
1318 protected readonly Type[] atypes;
1319 protected readonly Location loc;
1321 protected ConstraintChecker (Type[] gen_params, Type[] atypes, Location loc)
1323 this.gen_params = gen_params;
1324 this.atypes = atypes;
1329 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1330 /// after fully resolving the constructed type.
1332 public bool CheckConstraints (IResolveContext ec)
1334 for (int i = 0; i < gen_params.Length; i++) {
1335 if (!CheckConstraints (ec, i))
1342 protected bool CheckConstraints (IResolveContext ec, int index)
1344 Type atype = atypes [index];
1345 Type ptype = gen_params [index];
1350 Expression aexpr = new EmptyExpression (atype);
1352 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1356 bool is_class, is_struct;
1357 if (atype.IsGenericParameter) {
1358 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1360 if (agc is Constraints)
1361 ((Constraints) agc).Resolve (ec);
1362 is_class = agc.IsReferenceType;
1363 is_struct = agc.IsValueType;
1365 is_class = is_struct = false;
1370 if (!atype.IsGenericType)
1372 is_class = atype.IsClass || atype.IsInterface;
1373 is_struct = TypeManager.IsValueType (atype) && !TypeManager.IsNullableType (atype);
1377 // First, check the `class' and `struct' constraints.
1379 if (gc.HasReferenceTypeConstraint && !is_class) {
1380 Report.Error (452, loc, "The type `{0}' must be " +
1381 "a reference type in order to use it " +
1382 "as type parameter `{1}' in the " +
1383 "generic type or method `{2}'.",
1384 TypeManager.CSharpName (atype),
1385 TypeManager.CSharpName (ptype),
1386 GetSignatureForError ());
1388 } else if (gc.HasValueTypeConstraint && !is_struct) {
1389 Report.Error (453, loc, "The type `{0}' must be a " +
1390 "non-nullable value type in order to use it " +
1391 "as type parameter `{1}' in the " +
1392 "generic type or method `{2}'.",
1393 TypeManager.CSharpName (atype),
1394 TypeManager.CSharpName (ptype),
1395 GetSignatureForError ());
1400 // The class constraint comes next.
1402 if (gc.HasClassConstraint) {
1403 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint))
1408 // Now, check the interface constraints.
1410 if (gc.InterfaceConstraints != null) {
1411 foreach (Type it in gc.InterfaceConstraints) {
1412 if (!CheckConstraint (ec, ptype, aexpr, it))
1418 // Finally, check the constructor constraint.
1421 if (!gc.HasConstructorConstraint)
1424 if (TypeManager.IsBuiltinType (atype) || TypeManager.IsValueType (atype))
1427 if (HasDefaultConstructor (atype))
1430 Report_SymbolRelatedToPreviousError ();
1431 Report.SymbolRelatedToPreviousError (atype);
1432 Report.Error (310, loc, "The type `{0}' must have a public " +
1433 "parameterless constructor in order to use it " +
1434 "as parameter `{1}' in the generic type or " +
1436 TypeManager.CSharpName (atype),
1437 TypeManager.CSharpName (ptype),
1438 GetSignatureForError ());
1442 protected bool CheckConstraint (IResolveContext ec, Type ptype, Expression expr,
1445 if (TypeManager.HasGenericArguments (ctype)) {
1446 Type[] types = TypeManager.GetTypeArguments (ctype);
1448 TypeArguments new_args = new TypeArguments ();
1450 for (int i = 0; i < types.Length; i++) {
1453 if (t.IsGenericParameter) {
1454 int pos = t.GenericParameterPosition;
1457 new_args.Add (new TypeExpression (t, loc));
1460 TypeExpr ct = new GenericTypeExpr (ctype, new_args, loc);
1461 if (ct.ResolveAsTypeStep (ec, false) == null)
1464 } else if (ctype.IsGenericParameter) {
1465 int pos = ctype.GenericParameterPosition;
1466 if (ctype.DeclaringMethod == null) {
1470 ctype = atypes [pos];
1474 if (Convert.ImplicitStandardConversionExists (expr, ctype))
1477 Report_SymbolRelatedToPreviousError ();
1478 Report.SymbolRelatedToPreviousError (expr.Type);
1480 if (TypeManager.IsNullableType (expr.Type) && ctype.IsInterface) {
1481 Report.Error (313, loc,
1482 "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
1483 "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
1484 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ptype),
1485 GetSignatureForError (), TypeManager.CSharpName (ctype));
1487 Report.Error (309, loc,
1488 "The type `{0}' must be convertible to `{1}' in order to " +
1489 "use it as parameter `{2}' in the generic type or method `{3}'",
1490 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ctype),
1491 TypeManager.CSharpName (ptype), GetSignatureForError ());
1496 static bool HasDefaultConstructor (Type atype)
1498 TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
1499 if (tparam != null) {
1500 if (tparam.GenericConstraints == null)
1503 return tparam.GenericConstraints.HasConstructorConstraint ||
1504 tparam.GenericConstraints.HasValueTypeConstraint;
1507 if (atype.IsAbstract)
1511 atype = TypeManager.DropGenericTypeArguments (atype);
1512 if (atype is TypeBuilder) {
1513 TypeContainer tc = TypeManager.LookupTypeContainer (atype);
1514 if (tc.InstanceConstructors == null) {
1515 atype = atype.BaseType;
1519 foreach (Constructor c in tc.InstanceConstructors) {
1520 if ((c.ModFlags & Modifiers.PUBLIC) == 0)
1522 if ((c.Parameters.FixedParameters != null) &&
1523 (c.Parameters.FixedParameters.Length != 0))
1525 if (c.Parameters.HasArglist || c.Parameters.HasParams)
1532 MemberInfo [] list = TypeManager.MemberLookup (null, null, atype, MemberTypes.Constructor,
1533 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
1534 ConstructorInfo.ConstructorName, null);
1539 foreach (MethodBase mb in list) {
1540 AParametersCollection pd = TypeManager.GetParameterData (mb);
1548 protected abstract string GetSignatureForError ();
1549 protected abstract void Report_SymbolRelatedToPreviousError ();
1551 public static bool CheckConstraints (EmitContext ec, MethodBase definition,
1552 MethodBase instantiated, Location loc)
1554 MethodConstraintChecker checker = new MethodConstraintChecker (
1555 definition, definition.GetGenericArguments (),
1556 instantiated.GetGenericArguments (), loc);
1558 return checker.CheckConstraints (ec);
1561 public static bool CheckConstraints (IResolveContext ec, Type gt, Type[] gen_params,
1562 Type[] atypes, Location loc)
1564 TypeConstraintChecker checker = new TypeConstraintChecker (
1565 gt, gen_params, atypes, loc);
1567 return checker.CheckConstraints (ec);
1570 protected class MethodConstraintChecker : ConstraintChecker
1572 MethodBase definition;
1574 public MethodConstraintChecker (MethodBase definition, Type[] gen_params,
1575 Type[] atypes, Location loc)
1576 : base (gen_params, atypes, loc)
1578 this.definition = definition;
1581 protected override string GetSignatureForError ()
1583 return TypeManager.CSharpSignature (definition);
1586 protected override void Report_SymbolRelatedToPreviousError ()
1588 Report.SymbolRelatedToPreviousError (definition);
1592 protected class TypeConstraintChecker : ConstraintChecker
1596 public TypeConstraintChecker (Type gt, Type[] gen_params, Type[] atypes,
1598 : base (gen_params, atypes, loc)
1603 protected override string GetSignatureForError ()
1605 return TypeManager.CSharpName (gt);
1608 protected override void Report_SymbolRelatedToPreviousError ()
1610 Report.SymbolRelatedToPreviousError (gt);
1616 /// A generic method definition.
1618 public class GenericMethod : DeclSpace
1620 FullNamedExpression return_type;
1621 Parameters parameters;
1623 public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
1624 FullNamedExpression return_type, Parameters parameters)
1625 : base (ns, parent, name, null)
1627 this.return_type = return_type;
1628 this.parameters = parameters;
1631 public override TypeBuilder DefineType ()
1633 throw new Exception ();
1636 public override bool Define ()
1638 for (int i = 0; i < TypeParameters.Length; i++)
1639 if (!TypeParameters [i].Resolve (this))
1646 /// Define and resolve the type parameters.
1647 /// We're called from Method.Define().
1649 public bool Define (MethodOrOperator m)
1651 TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1652 string[] snames = new string [names.Length];
1653 for (int i = 0; i < names.Length; i++) {
1654 string type_argument_name = names[i].Name;
1655 int idx = parameters.GetParameterIndexByName (type_argument_name);
1659 b = new Block (null);
1661 b.Error_AlreadyDeclaredTypeParameter (parameters [i].Location,
1662 type_argument_name, "method parameter");
1665 snames[i] = type_argument_name;
1668 GenericTypeParameterBuilder[] gen_params = m.MethodBuilder.DefineGenericParameters (snames);
1669 for (int i = 0; i < TypeParameters.Length; i++)
1670 TypeParameters [i].Define (gen_params [i]);
1675 for (int i = 0; i < TypeParameters.Length; i++) {
1676 if (!TypeParameters [i].ResolveType (this))
1684 /// We're called from MethodData.Define() after creating the MethodBuilder.
1686 public bool DefineType (EmitContext ec, MethodBuilder mb,
1687 MethodInfo implementing, bool is_override)
1689 for (int i = 0; i < TypeParameters.Length; i++)
1690 if (!TypeParameters [i].DefineType (
1691 ec, mb, implementing, is_override))
1694 bool ok = parameters.Resolve (ec);
1696 if ((return_type != null) && (return_type.ResolveAsTypeTerminal (ec, false) == null))
1702 public void EmitAttributes ()
1704 for (int i = 0; i < TypeParameters.Length; i++)
1705 TypeParameters [i].Emit ();
1707 if (OptAttributes != null)
1708 OptAttributes.Emit ();
1711 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1712 MemberFilter filter, object criteria)
1714 throw new Exception ();
1717 public override MemberCache MemberCache {
1723 public override AttributeTargets AttributeTargets {
1725 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1729 public override string DocCommentHeader {
1730 get { return "M:"; }
1733 public new void VerifyClsCompliance ()
1735 foreach (TypeParameter tp in TypeParameters) {
1736 if (tp.Constraints == null)
1739 tp.Constraints.VerifyClsCompliance ();
1744 public partial class TypeManager
1746 static public Type activator_type;
1748 public static TypeContainer LookupGenericTypeContainer (Type t)
1750 t = DropGenericTypeArguments (t);
1751 return LookupTypeContainer (t);
1755 /// Check whether `a' and `b' may become equal generic types.
1756 /// The algorithm to do that is a little bit complicated.
1758 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_inferred,
1759 Type[] method_inferred)
1761 if (a.IsGenericParameter) {
1763 // If a is an array of a's type, they may never
1767 b = GetElementType (b);
1773 // If b is a generic parameter or an actual type,
1774 // they may become equal:
1776 // class X<T,U> : I<T>, I<U>
1777 // class X<T> : I<T>, I<float>
1779 if (b.IsGenericParameter || !b.IsGenericType) {
1780 int pos = a.GenericParameterPosition;
1781 Type[] args = a.DeclaringMethod != null ? method_inferred : class_inferred;
1782 if (args [pos] == null) {
1787 return args [pos] == a;
1791 // We're now comparing a type parameter with a
1792 // generic instance. They may become equal unless
1793 // the type parameter appears anywhere in the
1794 // generic instance:
1796 // class X<T,U> : I<T>, I<X<U>>
1797 // -> error because you could instanciate it as
1800 // class X<T> : I<T>, I<X<T>> -> ok
1803 Type[] bargs = GetTypeArguments (b);
1804 for (int i = 0; i < bargs.Length; i++) {
1805 if (a.Equals (bargs [i]))
1812 if (b.IsGenericParameter)
1813 return MayBecomeEqualGenericTypes (b, a, class_inferred, method_inferred);
1816 // At this point, neither a nor b are a type parameter.
1818 // If one of them is a generic instance, let
1819 // MayBecomeEqualGenericInstances() compare them (if the
1820 // other one is not a generic instance, they can never
1824 if (a.IsGenericType || b.IsGenericType)
1825 return MayBecomeEqualGenericInstances (a, b, class_inferred, method_inferred);
1828 // If both of them are arrays.
1831 if (a.IsArray && b.IsArray) {
1832 if (a.GetArrayRank () != b.GetArrayRank ())
1835 a = GetElementType (a);
1836 b = GetElementType (b);
1838 return MayBecomeEqualGenericTypes (a, b, class_inferred, method_inferred);
1842 // Ok, two ordinary types.
1845 return a.Equals (b);
1849 // Checks whether two generic instances may become equal for some
1850 // particular instantiation (26.3.1).
1852 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
1853 Type[] class_inferred,
1854 Type[] method_inferred)
1856 if (!a.IsGenericType || !b.IsGenericType)
1858 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1861 return MayBecomeEqualGenericInstances (
1862 GetTypeArguments (a), GetTypeArguments (b), class_inferred, method_inferred);
1865 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
1866 Type[] class_inferred,
1867 Type[] method_inferred)
1869 if (aargs.Length != bargs.Length)
1872 for (int i = 0; i < aargs.Length; i++) {
1873 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_inferred, method_inferred))
1881 /// Type inference. Try to infer the type arguments from `method',
1882 /// which is invoked with the arguments `arguments'. This is used
1883 /// when resolving an Invocation or a DelegateInvocation and the user
1884 /// did not explicitly specify type arguments.
1886 public static int InferTypeArguments (EmitContext ec,
1887 ArrayList arguments,
1888 ref MethodBase method)
1890 ATypeInference ti = ATypeInference.CreateInstance (arguments);
1891 Type[] i_args = ti.InferMethodArguments (ec, method);
1893 return ti.InferenceScore;
1895 if (i_args.Length == 0)
1898 method = ((MethodInfo) method).MakeGenericMethod (i_args);
1905 public static bool InferTypeArguments (AParametersCollection apd,
1906 ref MethodBase method)
1908 if (!TypeManager.IsGenericMethod (method))
1911 ATypeInference ti = ATypeInference.CreateInstance (ArrayList.Adapter (apd.Types));
1912 Type[] i_args = ti.InferDelegateArguments (method);
1916 method = ((MethodInfo) method).MakeGenericMethod (i_args);
1921 abstract class ATypeInference
1923 protected readonly ArrayList arguments;
1924 protected readonly int arg_count;
1926 protected ATypeInference (ArrayList arguments)
1928 this.arguments = arguments;
1929 if (arguments != null)
1930 arg_count = arguments.Count;
1933 public static ATypeInference CreateInstance (ArrayList arguments)
1935 if (RootContext.Version == LanguageVersion.ISO_2)
1936 return new TypeInferenceV2 (arguments);
1938 return new TypeInferenceV3 (arguments);
1941 public virtual int InferenceScore {
1943 return int.MaxValue;
1947 public abstract Type[] InferMethodArguments (EmitContext ec, MethodBase method);
1948 public abstract Type[] InferDelegateArguments (MethodBase method);
1952 // Implements C# 2.0 type inference
1954 class TypeInferenceV2 : ATypeInference
1956 public TypeInferenceV2 (ArrayList arguments)
1961 public override Type[] InferDelegateArguments (MethodBase method)
1963 AParametersCollection pd = TypeManager.GetParameterData (method);
1964 if (arg_count != pd.Count)
1967 Type[] method_args = method.GetGenericArguments ();
1968 Type[] inferred_types = new Type[method_args.Length];
1970 Type[] param_types = new Type[pd.Count];
1971 Type[] arg_types = (Type[])arguments.ToArray (typeof (Type));
1973 for (int i = 0; i < arg_count; i++) {
1974 param_types[i] = pd.Types [i];
1977 if (!InferTypeArguments (param_types, arg_types, inferred_types))
1980 return inferred_types;
1983 public override Type[] InferMethodArguments (EmitContext ec, MethodBase method)
1985 AParametersCollection pd = TypeManager.GetParameterData (method);
1986 Type[] method_generic_args = method.GetGenericArguments ();
1987 Type [] inferred_types = new Type [method_generic_args.Length];
1988 Type[] arg_types = new Type [pd.Count];
1990 int a_count = arg_types.Length;
1994 for (int i = 0; i < a_count; i++) {
1995 Argument a = (Argument) arguments[i];
1996 if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
1999 if (!TypeInferenceV2.UnifyType (pd.Types [i], a.Type, inferred_types))
2004 Type element_type = TypeManager.GetElementType (pd.Types [a_count]);
2005 for (int i = a_count; i < arg_count; i++) {
2006 Argument a = (Argument) arguments [i];
2007 if (a.Expr is NullLiteral || a.Expr is MethodGroupExpr || a.Expr is AnonymousMethodExpression)
2010 if (!TypeInferenceV2.UnifyType (element_type, a.Type, inferred_types))
2015 for (int i = 0; i < inferred_types.Length; i++)
2016 if (inferred_types [i] == null)
2019 return inferred_types;
2022 static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
2023 Type[] inferred_types)
2025 for (int i = 0; i < arg_types.Length; i++) {
2026 if (arg_types[i] == null)
2029 if (!UnifyType (param_types[i], arg_types[i], inferred_types))
2033 for (int i = 0; i < inferred_types.Length; ++i)
2034 if (inferred_types[i] == null)
2040 public static bool UnifyType (Type pt, Type at, Type[] inferred)
2042 if (pt.IsGenericParameter) {
2043 if (pt.DeclaringMethod == null)
2046 int pos = pt.GenericParameterPosition;
2048 if (inferred [pos] == null)
2049 inferred [pos] = at;
2051 return inferred [pos] == at;
2054 if (!pt.ContainsGenericParameters) {
2055 if (at.ContainsGenericParameters)
2056 return UnifyType (at, pt, inferred);
2063 if (at.GetArrayRank () != pt.GetArrayRank ())
2066 return UnifyType (TypeManager.GetElementType (pt), TypeManager.GetElementType (at), inferred);
2069 if (!pt.IsGenericType)
2072 Type gt = pt.GetGenericTypeDefinition ();
2073 if ((gt != TypeManager.generic_ilist_type) && (gt != TypeManager.generic_icollection_type) &&
2074 (gt != TypeManager.generic_ienumerable_type))
2077 Type[] args = TypeManager.GetTypeArguments (pt);
2078 return UnifyType (args[0], at.GetElementType (), inferred);
2083 (pt.GetArrayRank () != at.GetArrayRank ()))
2086 return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
2089 if (pt.IsByRef && at.IsByRef)
2090 return UnifyType (pt.GetElementType (), at.GetElementType (), inferred);
2091 ArrayList list = new ArrayList ();
2092 if (at.IsGenericType)
2094 for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
2097 list.AddRange (TypeManager.GetInterfaces (at));
2099 foreach (Type type in list) {
2100 if (!type.IsGenericType)
2103 if (TypeManager.DropGenericTypeArguments (pt) != TypeManager.DropGenericTypeArguments (type))
2106 if (!UnifyTypes (pt.GetGenericArguments (), type.GetGenericArguments (), inferred))
2113 static bool UnifyTypes (Type[] pts, Type[] ats, Type[] inferred)
2115 for (int i = 0; i < ats.Length; i++) {
2116 if (!UnifyType (pts [i], ats [i], inferred))
2124 // Implements C# 3.0 type inference
2126 class TypeInferenceV3 : ATypeInference
2129 // Tracks successful rate of type inference
2131 int score = int.MaxValue;
2133 public TypeInferenceV3 (ArrayList arguments)
2138 public override int InferenceScore {
2144 public override Type[] InferDelegateArguments (MethodBase method)
2146 AParametersCollection pd = TypeManager.GetParameterData (method);
2147 if (arg_count != pd.Count)
2150 Type[] d_gargs = method.GetGenericArguments ();
2151 TypeInferenceContext context = new TypeInferenceContext (d_gargs);
2153 // A lower-bound inference is made from each argument type Uj of D
2154 // to the corresponding parameter type Tj of M
2155 for (int i = 0; i < arg_count; ++i) {
2156 Type t = pd.Types [i];
2157 if (!t.IsGenericParameter)
2160 context.LowerBoundInference ((Type)arguments[i], t);
2163 if (!context.FixAllTypes ())
2166 return context.InferredTypeArguments;
2169 public override Type[] InferMethodArguments (EmitContext ec, MethodBase method)
2171 Type[] method_generic_args = method.GetGenericArguments ();
2172 TypeInferenceContext context = new TypeInferenceContext (method_generic_args);
2173 if (!context.UnfixedVariableExists)
2174 return Type.EmptyTypes;
2176 AParametersCollection pd = TypeManager.GetParameterData (method);
2177 if (!InferInPhases (ec, context, pd))
2180 return context.InferredTypeArguments;
2184 // Implements method type arguments inference
2186 bool InferInPhases (EmitContext ec, TypeInferenceContext tic, AParametersCollection methodParameters)
2188 int params_arguments_start;
2189 if (methodParameters.HasParams) {
2190 params_arguments_start = methodParameters.Count - 1;
2192 params_arguments_start = arg_count;
2195 Type [] ptypes = methodParameters.Types;
2198 // The first inference phase
2200 Type method_parameter = null;
2201 for (int i = 0; i < arg_count; i++) {
2202 Argument a = (Argument) arguments [i];
2204 if (i < params_arguments_start) {
2205 method_parameter = methodParameters.Types [i];
2206 } else if (i == params_arguments_start) {
2207 if (arg_count == params_arguments_start + 1 && TypeManager.HasElementType (a.Type))
2208 method_parameter = methodParameters.Types [params_arguments_start];
2210 method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
2212 ptypes = (Type[]) ptypes.Clone ();
2213 ptypes [i] = method_parameter;
2217 // When a lambda expression, an anonymous method
2218 // is used an explicit argument type inference takes a place
2220 AnonymousMethodExpression am = a.Expr as AnonymousMethodExpression;
2222 if (am.ExplicitTypeInference (tic, method_parameter))
2227 if (a.Expr is NullLiteral)
2231 // Otherwise an output type inference is made
2233 score -= tic.OutputTypeInference (ec, a.Expr, method_parameter);
2237 // Part of the second phase but because it happens only once
2238 // we don't need to call it in cycle
2240 bool fixed_any = false;
2241 if (!tic.FixIndependentTypeArguments (ptypes, ref fixed_any))
2244 return DoSecondPhase (ec, tic, ptypes, !fixed_any);
2247 bool DoSecondPhase (EmitContext ec, TypeInferenceContext tic, Type[] methodParameters, bool fixDependent)
2249 bool fixed_any = false;
2250 if (fixDependent && !tic.FixDependentTypes (ref fixed_any))
2253 // If no further unfixed type variables exist, type inference succeeds
2254 if (!tic.UnfixedVariableExists)
2257 if (!fixed_any && fixDependent)
2260 // For all arguments where the corresponding argument output types
2261 // contain unfixed type variables but the input types do not,
2262 // an output type inference is made
2263 for (int i = 0; i < arg_count; i++) {
2265 // Align params arguments
2266 Type t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
2268 if (!TypeManager.IsDelegateType (t_i)) {
2269 if (TypeManager.DropGenericTypeArguments (t_i) != TypeManager.expression_type)
2272 t_i = t_i.GetGenericArguments () [0];
2275 MethodInfo mi = Delegate.GetInvokeMethod (t_i, t_i);
2276 Type rtype = mi.ReturnType;
2279 // Blablabla, because reflection does not work with dynamic types
2280 Type[] g_args = t_i.GetGenericArguments ();
2281 rtype = g_args[rtype.GenericParameterPosition];
2284 if (tic.IsReturnTypeNonDependent (mi, rtype))
2285 score -= tic.OutputTypeInference (ec, ((Argument) arguments [i]).Expr, t_i);
2289 return DoSecondPhase (ec, tic, methodParameters, true);
2293 public class TypeInferenceContext
2295 readonly Type[] unfixed_types;
2296 readonly Type[] fixed_types;
2297 readonly ArrayList[] bounds;
2300 public TypeInferenceContext (Type[] typeArguments)
2302 if (typeArguments.Length == 0)
2303 throw new ArgumentException ("Empty generic arguments");
2305 fixed_types = new Type [typeArguments.Length];
2306 for (int i = 0; i < typeArguments.Length; ++i) {
2307 if (typeArguments [i].IsGenericParameter) {
2308 if (bounds == null) {
2309 bounds = new ArrayList [typeArguments.Length];
2310 unfixed_types = new Type [typeArguments.Length];
2312 unfixed_types [i] = typeArguments [i];
2314 fixed_types [i] = typeArguments [i];
2319 public Type[] InferredTypeArguments {
2325 void AddToBounds (Type t, int index)
2328 // Some types cannot be used as type arguments
2330 if (t == TypeManager.void_type || t.IsPointer)
2333 ArrayList a = bounds [index];
2335 a = new ArrayList ();
2343 // SPEC: does not cover type inference using constraints
2345 //if (TypeManager.IsGenericParameter (t)) {
2346 // GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
2347 // if (constraints != null) {
2348 // //if (constraints.EffectiveBaseClass != null)
2349 // // t = constraints.EffectiveBaseClass;
2355 bool AllTypesAreFixed (Type[] types)
2357 foreach (Type t in types) {
2358 if (t.IsGenericParameter) {
2364 if (t.IsGenericType)
2365 return AllTypesAreFixed (t.GetGenericArguments ());
2372 // 26.3.3.8 Exact Inference
2374 public int ExactInference (Type u, Type v)
2376 // If V is an array type
2381 if (u.GetArrayRank () != v.GetArrayRank ())
2384 return ExactInference (TypeManager.GetElementType (u), TypeManager.GetElementType (v));
2387 // If V is constructed type and U is constructed type
2388 if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2389 if (!u.IsGenericType)
2392 Type [] ga_u = u.GetGenericArguments ();
2393 Type [] ga_v = v.GetGenericArguments ();
2394 if (ga_u.Length != ga_v.Length)
2398 for (int i = 0; i < ga_u.Length; ++i)
2399 score += ExactInference (ga_u [i], ga_v [i]);
2401 return score > 0 ? 1 : 0;
2404 // If V is one of the unfixed type arguments
2405 int pos = IsUnfixed (v);
2409 AddToBounds (u, pos);
2413 public bool FixAllTypes ()
2415 for (int i = 0; i < unfixed_types.Length; ++i) {
2423 // All unfixed type variables Xi are fixed for which all of the following hold:
2424 // a, There is at least one type variable Xj that depends on Xi
2425 // b, Xi has a non-empty set of bounds
2427 public bool FixDependentTypes (ref bool fixed_any)
2429 for (int i = 0; i < unfixed_types.Length; ++i) {
2430 if (unfixed_types[i] == null)
2433 if (bounds[i] == null)
2446 // All unfixed type variables Xi which depend on no Xj are fixed
2448 public bool FixIndependentTypeArguments (Type[] methodParameters, ref bool fixed_any)
2450 ArrayList types_to_fix = new ArrayList (unfixed_types);
2451 for (int i = 0; i < methodParameters.Length; ++i) {
2452 Type t = methodParameters[i];
2453 if (t.IsGenericParameter)
2456 if (!TypeManager.IsDelegateType (t)) {
2457 if (TypeManager.DropGenericTypeArguments (t) != TypeManager.expression_type)
2460 t = t.GetGenericArguments () [0];
2463 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2464 Type rtype = invoke.ReturnType;
2465 if (!rtype.IsGenericParameter && !rtype.IsGenericType)
2469 // Blablabla, because reflection does not work with dynamic types
2470 if (rtype.IsGenericParameter) {
2471 Type [] g_args = t.GetGenericArguments ();
2472 rtype = g_args [rtype.GenericParameterPosition];
2475 // Remove dependent types, they cannot be fixed yet
2476 RemoveDependentTypes (types_to_fix, rtype);
2479 foreach (Type t in types_to_fix) {
2483 int idx = IsUnfixed (t);
2484 if (idx >= 0 && !FixType (idx)) {
2489 fixed_any = types_to_fix.Count > 0;
2496 public bool FixType (int i)
2498 // It's already fixed
2499 if (unfixed_types[i] == null)
2500 throw new InternalErrorException ("Type argument has been already fixed");
2505 ArrayList candidates = (ArrayList)bounds [i];
2506 if (candidates == null)
2509 if (candidates.Count == 1) {
2510 unfixed_types[i] = null;
2511 fixed_types[i] = (Type)candidates[0];
2516 // Determines a unique type from which there is
2517 // a standard implicit conversion to all the other
2520 Type best_candidate = null;
2522 int candidates_count = candidates.Count;
2523 for (int ci = 0; ci < candidates_count; ++ci) {
2524 Type candidate = (Type)candidates [ci];
2525 for (cii = 0; cii < candidates_count; ++cii) {
2529 if (!Convert.ImplicitConversionExists (null,
2530 new TypeExpression ((Type)candidates [cii], Location.Null), candidate)) {
2535 if (cii != candidates_count)
2538 if (best_candidate != null)
2541 best_candidate = candidate;
2544 if (best_candidate == null)
2547 unfixed_types[i] = null;
2548 fixed_types[i] = best_candidate;
2553 // Uses inferred types to inflate delegate type argument
2555 public Type InflateGenericArgument (Type parameter)
2557 if (parameter.IsGenericParameter) {
2559 // Inflate method generic argument (MVAR) only
2561 if (parameter.DeclaringMethod == null)
2564 return fixed_types [parameter.GenericParameterPosition];
2567 if (parameter.IsGenericType) {
2568 Type [] parameter_targs = parameter.GetGenericArguments ();
2569 for (int ii = 0; ii < parameter_targs.Length; ++ii) {
2570 parameter_targs [ii] = InflateGenericArgument (parameter_targs [ii]);
2572 return parameter.GetGenericTypeDefinition ().MakeGenericType (parameter_targs);
2579 // Tests whether all delegate input arguments are fixed and generic output type
2580 // requires output type inference
2582 public bool IsReturnTypeNonDependent (MethodInfo invoke, Type returnType)
2584 if (returnType.IsGenericParameter) {
2585 if (IsFixed (returnType))
2587 } else if (returnType.IsGenericType) {
2588 if (TypeManager.IsDelegateType (returnType)) {
2589 invoke = Delegate.GetInvokeMethod (returnType, returnType);
2590 return IsReturnTypeNonDependent (invoke, invoke.ReturnType);
2593 Type[] g_args = returnType.GetGenericArguments ();
2595 // At least one unfixed return type has to exist
2596 if (AllTypesAreFixed (g_args))
2602 // All generic input arguments have to be fixed
2603 AParametersCollection d_parameters = TypeManager.GetParameterData (invoke);
2604 return AllTypesAreFixed (d_parameters.Types);
2607 bool IsFixed (Type type)
2609 return IsUnfixed (type) == -1;
2612 int IsUnfixed (Type type)
2614 if (!type.IsGenericParameter)
2617 //return unfixed_types[type.GenericParameterPosition] != null;
2618 for (int i = 0; i < unfixed_types.Length; ++i) {
2619 if (unfixed_types [i] == type)
2627 // 26.3.3.9 Lower-bound Inference
2629 public int LowerBoundInference (Type u, Type v)
2631 // If V is one of the unfixed type arguments
2632 int pos = IsUnfixed (v);
2634 AddToBounds (u, pos);
2638 // If U is an array type
2640 int u_dim = u.GetArrayRank ();
2642 Type u_e = TypeManager.GetElementType (u);
2645 if (u_dim != v.GetArrayRank ())
2648 v_e = TypeManager.GetElementType (v);
2651 return LowerBoundInference (u_e, v_e);
2654 return ExactInference (u_e, v_e);
2660 if (v.IsGenericType) {
2661 Type g_v = v.GetGenericTypeDefinition ();
2662 if ((g_v != TypeManager.generic_ilist_type) && (g_v != TypeManager.generic_icollection_type) &&
2663 (g_v != TypeManager.generic_ienumerable_type))
2666 v_e = TypeManager.GetTypeArguments (v)[0];
2669 return LowerBoundInference (u_e, v_e);
2672 return ExactInference (u_e, v_e);
2674 } else if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2676 // if V is a constructed type C<V1..Vk> and there is a unique set of types U1..Uk
2677 // such that a standard implicit conversion exists from U to C<U1..Uk> then an exact
2678 // inference is made from each Ui for the corresponding Vi
2680 ArrayList u_candidates = new ArrayList ();
2681 if (u.IsGenericType)
2682 u_candidates.Add (u);
2684 for (Type t = u.BaseType; t != null; t = t.BaseType) {
2685 if (t.IsGenericType && !t.IsGenericTypeDefinition)
2686 u_candidates.Add (t);
2689 // TODO: Implement GetGenericInterfaces only and remove
2690 // the if from foreach
2691 u_candidates.AddRange (TypeManager.GetInterfaces (u));
2693 Type open_v = v.GetGenericTypeDefinition ();
2694 Type [] unique_candidate_targs = null;
2695 Type [] ga_v = v.GetGenericArguments ();
2696 foreach (Type u_candidate in u_candidates) {
2697 if (!u_candidate.IsGenericType || u_candidate.IsGenericTypeDefinition)
2700 if (TypeManager.DropGenericTypeArguments (u_candidate) != open_v)
2704 // The unique set of types U1..Uk means that if we have an interface C<T>,
2705 // class U: C<int>, C<long> then no type inference is made when inferring
2706 // from U to C<T> because T could be int or long
2708 if (unique_candidate_targs != null) {
2709 Type[] second_unique_candidate_targs = u_candidate.GetGenericArguments ();
2710 if (TypeManager.IsEqual (unique_candidate_targs, second_unique_candidate_targs)) {
2711 unique_candidate_targs = second_unique_candidate_targs;
2716 // This should always cause type inference failure
2722 unique_candidate_targs = u_candidate.GetGenericArguments ();
2725 if (unique_candidate_targs != null) {
2727 for (int i = 0; i < unique_candidate_targs.Length; ++i)
2728 if (ExactInference (unique_candidate_targs [i], ga_v [i]) == 0)
2738 // 26.3.3.6 Output Type Inference
2740 public int OutputTypeInference (EmitContext ec, Expression e, Type t)
2742 // If e is a lambda or anonymous method with inferred return type
2743 AnonymousMethodExpression ame = e as AnonymousMethodExpression;
2745 Type rt = ame.InferReturnType (ec, this, t);
2746 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2749 AParametersCollection pd = TypeManager.GetParameterData (invoke);
2750 return ame.Parameters.Count == pd.Count ? 1 : 0;
2753 Type rtype = invoke.ReturnType;
2755 // Blablabla, because reflection does not work with dynamic types
2756 Type [] g_args = t.GetGenericArguments ();
2757 rtype = g_args [rtype.GenericParameterPosition];
2759 return LowerBoundInference (rt, rtype) + 1;
2763 // if E is a method group and T is a delegate type or expression tree type
2764 // return type Tb with parameter types T1..Tk and return type Tb, and overload
2765 // resolution of E with the types T1..Tk yields a single method with return type U,
2766 // then a lower-bound inference is made from U for Tb.
2768 if (e is MethodGroupExpr) {
2769 // TODO: Or expression tree
2770 if (!TypeManager.IsDelegateType (t))
2773 MethodInfo invoke = Delegate.GetInvokeMethod (t, t);
2774 Type rtype = invoke.ReturnType;
2776 // Blablabla, because reflection does not work with dynamic types
2777 Type [] g_args = t.GetGenericArguments ();
2778 rtype = g_args [rtype.GenericParameterPosition];
2781 if (!TypeManager.IsGenericType (rtype))
2784 MethodGroupExpr mg = (MethodGroupExpr) e;
2785 ArrayList args = DelegateCreation.CreateDelegateMethodArguments (invoke, e.Location);
2786 mg = mg.OverloadResolve (ec, ref args, true, e.Location);
2790 // TODO: What should happen when return type is of generic type ?
2791 throw new NotImplementedException ();
2792 // return LowerBoundInference (null, rtype) + 1;
2796 // if e is an expression with type U, then
2797 // a lower-bound inference is made from U for T
2799 return LowerBoundInference (e.Type, t) * 2;
2802 void RemoveDependentTypes (ArrayList types, Type returnType)
2804 int idx = IsUnfixed (returnType);
2810 if (returnType.IsGenericType) {
2811 foreach (Type t in returnType.GetGenericArguments ()) {
2812 RemoveDependentTypes (types, t);
2817 public bool UnfixedVariableExists {
2819 if (unfixed_types == null)
2822 foreach (Type ut in unfixed_types)