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 (!TypeManager.IsValueType (ClassConstraint))
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 class ReflectionConstraints : GenericConstraints
129 GenericParameterAttributes attrs;
131 Type class_constraint;
132 Type[] iface_constraints;
135 public static GenericConstraints GetConstraints (Type t)
137 Type[] constraints = t.GetGenericParameterConstraints ();
138 GenericParameterAttributes attrs = t.GenericParameterAttributes;
139 if (constraints.Length == 0 && attrs == GenericParameterAttributes.None)
141 return new ReflectionConstraints (t.Name, constraints, attrs);
144 private ReflectionConstraints (string name, Type[] constraints, GenericParameterAttributes attrs)
149 int interface_constraints_pos = 0;
150 if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) {
151 base_type = TypeManager.value_type;
152 interface_constraints_pos = 1;
153 } else if ((attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0) {
154 if (constraints.Length > 0 && constraints[0].IsClass) {
155 class_constraint = base_type = constraints[0];
156 interface_constraints_pos = 1;
158 base_type = TypeManager.object_type;
161 base_type = TypeManager.object_type;
164 if (constraints.Length > interface_constraints_pos) {
165 if (interface_constraints_pos == 0) {
166 iface_constraints = constraints;
168 iface_constraints = new Type[constraints.Length - interface_constraints_pos];
169 Array.Copy (constraints, interface_constraints_pos, iface_constraints, 0, iface_constraints.Length);
172 iface_constraints = Type.EmptyTypes;
176 public override string TypeParameter
181 public override GenericParameterAttributes Attributes
183 get { return attrs; }
186 public override Type ClassConstraint
188 get { return class_constraint; }
191 public override Type EffectiveBaseClass
193 get { return base_type; }
196 public override Type[] InterfaceConstraints
198 get { return iface_constraints; }
205 // Don't add or modify internal values, they are used as -/+ calculation signs
212 public enum SpecialConstraint
220 /// Tracks the constraints for a type parameter from a generic type definition.
222 public class Constraints : GenericConstraints {
224 ArrayList constraints;
228 // name is the identifier, constraints is an arraylist of
229 // Expressions (with types) or `true' for the constructor constraint.
231 public Constraints (string name, ArrayList constraints,
235 this.constraints = constraints;
239 public override string TypeParameter {
245 public Constraints Clone ()
247 return new Constraints (name, constraints, loc);
250 GenericParameterAttributes attrs;
251 TypeExpr class_constraint;
252 ArrayList iface_constraints;
253 ArrayList type_param_constraints;
255 Type class_constraint_type;
256 Type[] iface_constraint_types;
257 Type effective_base_type;
262 /// Resolve the constraints - but only resolve things into Expression's, not
263 /// into actual types.
265 public bool Resolve (MemberCore ec, TypeParameter tp, Report Report)
273 iface_constraints = new ArrayList (2); // TODO: Too expensive allocation
274 type_param_constraints = new ArrayList ();
276 foreach (object obj in constraints) {
277 if (HasConstructorConstraint) {
278 Report.Error (401, loc,
279 "The new() constraint must be the last constraint specified");
283 if (obj is SpecialConstraint) {
284 SpecialConstraint sc = (SpecialConstraint) obj;
286 if (sc == SpecialConstraint.Constructor) {
287 if (!HasValueTypeConstraint) {
288 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
292 Report.Error (451, loc, "The `new()' constraint " +
293 "cannot be used with the `struct' constraint");
297 if ((num_constraints > 0) || HasReferenceTypeConstraint || HasValueTypeConstraint) {
298 Report.Error (449, loc, "The `class' or `struct' " +
299 "constraint must be the first constraint specified");
303 if (sc == SpecialConstraint.ReferenceType)
304 attrs |= GenericParameterAttributes.ReferenceTypeConstraint;
306 attrs |= GenericParameterAttributes.NotNullableValueTypeConstraint;
310 int errors = Report.Errors;
311 FullNamedExpression fn = ((Expression) obj).ResolveAsTypeStep (ec, false);
314 if (errors != Report.Errors)
317 NamespaceEntry.Error_NamespaceNotFound (loc, ((Expression)obj).GetSignatureForError (), Report);
322 GenericTypeExpr cexpr = fn as GenericTypeExpr;
324 expr = cexpr.ResolveAsBaseTerminal (ec, false);
325 if (expr != null && cexpr.HasDynamicArguments ()) {
326 Report.Error (1968, cexpr.Location,
327 "A constraint cannot be the dynamic type `{0}'",
328 cexpr.GetSignatureForError ());
332 expr = ((Expression) obj).ResolveAsTypeTerminal (ec, false);
334 if ((expr == null) || (expr.Type == null))
337 if (TypeManager.IsGenericParameter (expr.Type))
338 type_param_constraints.Add (expr);
339 else if (expr.IsInterface)
340 iface_constraints.Add (expr);
341 else if (class_constraint != null || iface_constraints.Count != 0) {
342 Report.Error (406, loc,
343 "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
344 expr.GetSignatureForError ());
346 } else if (HasReferenceTypeConstraint || HasValueTypeConstraint) {
347 Report.Error (450, loc, "`{0}': cannot specify both " +
348 "a constraint class and the `class' " +
349 "or `struct' constraint", expr.GetSignatureForError ());
352 class_constraint = expr;
356 // Checks whether each generic method parameter constraint type
357 // is valid with respect to T
359 if (tp != null && tp.Type.DeclaringMethod != null) {
360 TypeManager.CheckTypeVariance (expr.Type, Variance.Contravariant, ec as MemberCore);
363 if (!ec.IsAccessibleAs (fn.Type)) {
364 Report.SymbolRelatedToPreviousError (fn.Type);
365 Report.Error (703, loc,
366 "Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
367 fn.GetSignatureForError (), ec.GetSignatureForError ());
373 ArrayList list = new ArrayList ();
374 foreach (TypeExpr iface_constraint in iface_constraints) {
375 foreach (Type type in list) {
376 if (!type.Equals (iface_constraint.Type))
379 Report.Error (405, loc,
380 "Duplicate constraint `{0}' for type " +
381 "parameter `{1}'.", iface_constraint.GetSignatureForError (),
386 list.Add (iface_constraint.Type);
389 foreach (TypeExpr expr in type_param_constraints) {
390 foreach (Type type in list) {
391 if (!type.Equals (expr.Type))
394 Report.Error (405, loc,
395 "Duplicate constraint `{0}' for type " +
396 "parameter `{1}'.", expr.GetSignatureForError (), name);
400 list.Add (expr.Type);
403 iface_constraint_types = new Type [list.Count];
404 list.CopyTo (iface_constraint_types, 0);
406 if (class_constraint != null) {
407 class_constraint_type = class_constraint.Type;
408 if (class_constraint_type == null)
411 if (class_constraint_type.IsSealed) {
412 if (class_constraint_type.IsAbstract)
414 Report.Error (717, loc, "`{0}' is not a valid constraint. Static classes cannot be used as constraints",
415 TypeManager.CSharpName (class_constraint_type));
419 Report.Error (701, loc, "`{0}' is not a valid constraint. A constraint must be an interface, " +
420 "a non-sealed class or a type parameter", TypeManager.CSharpName(class_constraint_type));
425 if ((class_constraint_type == TypeManager.array_type) ||
426 (class_constraint_type == TypeManager.delegate_type) ||
427 (class_constraint_type == TypeManager.enum_type) ||
428 (class_constraint_type == TypeManager.value_type) ||
429 (class_constraint_type == TypeManager.object_type) ||
430 class_constraint_type == TypeManager.multicast_delegate_type) {
431 Report.Error (702, loc,
432 "A constraint cannot be special class `{0}'",
433 TypeManager.CSharpName (class_constraint_type));
437 if (TypeManager.IsDynamicType (class_constraint_type)) {
438 Report.Error (1967, loc, "A constraint cannot be the dynamic type");
443 if (class_constraint_type != null)
444 effective_base_type = class_constraint_type;
445 else if (HasValueTypeConstraint)
446 effective_base_type = TypeManager.value_type;
448 effective_base_type = TypeManager.object_type;
450 if ((attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
451 attrs |= GenericParameterAttributes.DefaultConstructorConstraint;
457 bool CheckTypeParameterConstraints (Type tparam, ref TypeExpr prevConstraint, ArrayList seen, Report Report)
461 Constraints constraints = TypeManager.LookupTypeParameter (tparam).Constraints;
462 if (constraints == null)
465 if (constraints.HasValueTypeConstraint) {
466 Report.Error (456, loc,
467 "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
473 // Checks whether there are no conflicts between type parameter constraints
477 // where U : A, B // A and B are not convertible
479 if (constraints.HasClassConstraint) {
480 if (prevConstraint != null) {
481 Type t2 = constraints.ClassConstraint;
482 TypeExpr e2 = constraints.class_constraint;
484 if (!Convert.ImplicitReferenceConversionExists (prevConstraint, t2) &&
485 !Convert.ImplicitReferenceConversionExists (e2, prevConstraint.Type)) {
486 Report.Error (455, loc,
487 "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
488 name, TypeManager.CSharpName (prevConstraint.Type), TypeManager.CSharpName (t2));
493 prevConstraint = constraints.class_constraint;
496 if (constraints.type_param_constraints == null)
499 foreach (TypeExpr expr in constraints.type_param_constraints) {
500 if (seen.Contains (expr.Type)) {
501 Report.Error (454, loc, "Circular constraint " +
502 "dependency involving `{0}' and `{1}'",
503 tparam.Name, expr.GetSignatureForError ());
507 if (!CheckTypeParameterConstraints (expr.Type, ref prevConstraint, seen, Report))
515 /// Resolve the constraints into actual types.
517 public bool ResolveTypes (IMemberContext ec, Report r)
522 resolved_types = true;
524 foreach (object obj in constraints) {
525 GenericTypeExpr cexpr = obj as GenericTypeExpr;
529 if (!cexpr.CheckConstraints (ec))
533 if (type_param_constraints.Count != 0) {
534 ArrayList seen = new ArrayList ();
535 TypeExpr prev_constraint = class_constraint;
536 foreach (TypeExpr expr in type_param_constraints) {
537 if (!CheckTypeParameterConstraints (expr.Type, ref prev_constraint, seen, r))
543 for (int i = 0; i < iface_constraints.Count; ++i) {
544 TypeExpr iface_constraint = (TypeExpr) iface_constraints [i];
545 iface_constraint = iface_constraint.ResolveAsTypeTerminal (ec, false);
546 if (iface_constraint == null)
548 iface_constraints [i] = iface_constraint;
551 if (class_constraint != null) {
552 class_constraint = class_constraint.ResolveAsTypeTerminal (ec, false);
553 if (class_constraint == null)
560 public override GenericParameterAttributes Attributes {
561 get { return attrs; }
564 public override bool HasClassConstraint {
565 get { return class_constraint != null; }
568 public override Type ClassConstraint {
569 get { return class_constraint_type; }
572 public override Type[] InterfaceConstraints {
573 get { return iface_constraint_types; }
576 public override Type EffectiveBaseClass {
577 get { return effective_base_type; }
580 public bool IsSubclassOf (Type t)
582 if ((class_constraint_type != null) &&
583 class_constraint_type.IsSubclassOf (t))
586 if (iface_constraint_types == null)
589 foreach (Type iface in iface_constraint_types) {
590 if (TypeManager.IsSubclassOf (iface, t))
597 public Location Location {
604 /// This is used when we're implementing a generic interface method.
605 /// Each method type parameter in implementing method must have the same
606 /// constraints than the corresponding type parameter in the interface
607 /// method. To do that, we're called on each of the implementing method's
610 public bool AreEqual (GenericConstraints gc)
612 if (gc.Attributes != attrs)
615 if (HasClassConstraint != gc.HasClassConstraint)
617 if (HasClassConstraint && !TypeManager.IsEqual (gc.ClassConstraint, ClassConstraint))
620 int gc_icount = gc.InterfaceConstraints != null ?
621 gc.InterfaceConstraints.Length : 0;
622 int icount = InterfaceConstraints != null ?
623 InterfaceConstraints.Length : 0;
625 if (gc_icount != icount)
628 for (int i = 0; i < gc.InterfaceConstraints.Length; ++i) {
629 Type iface = gc.InterfaceConstraints [i];
630 if (iface.IsGenericType)
631 iface = iface.GetGenericTypeDefinition ();
634 for (int ii = 0; ii < InterfaceConstraints.Length; ii++) {
635 Type check = InterfaceConstraints [ii];
636 if (check.IsGenericType)
637 check = check.GetGenericTypeDefinition ();
639 if (TypeManager.IsEqual (iface, check)) {
652 public void VerifyClsCompliance (Report r)
654 if (class_constraint_type != null && !AttributeTester.IsClsCompliant (class_constraint_type))
655 Warning_ConstrainIsNotClsCompliant (class_constraint_type, class_constraint.Location, r);
657 if (iface_constraint_types != null) {
658 for (int i = 0; i < iface_constraint_types.Length; ++i) {
659 if (!AttributeTester.IsClsCompliant (iface_constraint_types [i]))
660 Warning_ConstrainIsNotClsCompliant (iface_constraint_types [i],
661 ((TypeExpr)iface_constraints [i]).Location, r);
666 void Warning_ConstrainIsNotClsCompliant (Type t, Location loc, Report Report)
668 Report.SymbolRelatedToPreviousError (t);
669 Report.Warning (3024, 1, loc, "Constraint type `{0}' is not CLS-compliant",
670 TypeManager.CSharpName (t));
675 /// A type parameter from a generic type definition.
677 public class TypeParameter : MemberCore, IMemberContainer
679 static readonly string[] attribute_target = new string [] { "type parameter" };
682 GenericConstraints gc;
683 Constraints constraints;
684 GenericTypeParameterBuilder type;
685 MemberCache member_cache;
688 public TypeParameter (DeclSpace parent, DeclSpace decl, string name,
689 Constraints constraints, Attributes attrs, Variance variance, Location loc)
690 : base (parent, new MemberName (name, loc), attrs)
693 this.constraints = constraints;
694 this.variance = variance;
697 public GenericConstraints GenericConstraints {
698 get { return gc != null ? gc : constraints; }
701 public Constraints Constraints {
702 get { return constraints; }
705 public DeclSpace DeclSpace {
709 public Variance Variance {
710 get { return variance; }
718 /// This is the first method which is called during the resolving
719 /// process; we're called immediately after creating the type parameters
720 /// with SRE (by calling `DefineGenericParameters()' on the TypeBuilder /
723 /// We're either called from TypeContainer.DefineType() or from
724 /// GenericMethod.Define() (called from Method.Define()).
726 public void Define (GenericTypeParameterBuilder type)
728 if (this.type != null)
729 throw new InvalidOperationException ();
732 TypeManager.AddTypeParameter (type, this);
735 public void ErrorInvalidVariance (IMemberContext mc, Variance expected)
737 // TODO: Report.SymbolRelatedToPreviousError (mc);
738 string input_variance = Variance == Variance.Contravariant ? "contravariant" : "covariant";
739 string gtype_variance;
741 case Variance.Contravariant: gtype_variance = "contravariantly"; break;
742 case Variance.Covariant: gtype_variance = "covariantly"; break;
743 default: gtype_variance = "invariantly"; break;
746 Delegate d = mc as Delegate;
747 string parameters = d != null ? d.Parameters.GetSignatureForError () : "";
749 Report.Error (1961, Location,
750 "The {2} type parameter `{0}' must be {3} valid on `{1}{4}'",
751 GetSignatureForError (), mc.GetSignatureForError (), input_variance, gtype_variance, parameters);
755 /// This is the second method which is called during the resolving
756 /// process - in case of class type parameters, we're called from
757 /// TypeContainer.ResolveType() - after it resolved the class'es
758 /// base class and interfaces. For method type parameters, we're
759 /// called immediately after Define().
761 /// We're just resolving the constraints into expressions here, we
762 /// don't resolve them into actual types.
764 /// Note that in the special case of partial generic classes, we may be
765 /// called _before_ Define() and we may also be called multiple types.
767 public bool Resolve (DeclSpace ds)
769 if (constraints != null) {
770 if (!constraints.Resolve (ds, this, Report)) {
780 /// This is the third method which is called during the resolving
781 /// process. We're called immediately after calling DefineConstraints()
782 /// on all of the current class'es type parameters.
784 /// Our job is to resolve the constraints to actual types.
786 /// Note that we may have circular dependencies on type parameters - this
787 /// is why Resolve() and ResolveType() are separate.
789 public bool ResolveType (IMemberContext ec)
791 if (constraints != null) {
792 if (!constraints.ResolveTypes (ec, Report)) {
802 /// This is the fourth and last method which is called during the resolving
803 /// process. We're called after everything is fully resolved and actually
804 /// register the constraints with SRE and the TypeManager.
806 public bool DefineType (IMemberContext ec)
808 return DefineType (ec, null, null, false);
812 /// This is the fith and last method which is called during the resolving
813 /// process. We're called after everything is fully resolved and actually
814 /// register the constraints with SRE and the TypeManager.
816 /// The `builder', `implementing' and `is_override' arguments are only
817 /// applicable to method type parameters.
819 public bool DefineType (IMemberContext ec, MethodBuilder builder,
820 MethodInfo implementing, bool is_override)
822 if (!ResolveType (ec))
825 if (implementing != null) {
826 if (is_override && (constraints != null)) {
827 Report.Error (460, Location,
828 "`{0}': Cannot specify constraints for overrides or explicit interface implementation methods",
829 TypeManager.CSharpSignature (builder));
833 MethodBase mb = TypeManager.DropGenericMethodArguments (implementing);
835 int pos = type.GenericParameterPosition;
836 Type mparam = mb.GetGenericArguments () [pos];
837 GenericConstraints temp_gc = ReflectionConstraints.GetConstraints (mparam);
840 gc = new InflatedConstraints (temp_gc, implementing.DeclaringType);
841 else if (constraints != null)
842 gc = new InflatedConstraints (constraints, implementing.DeclaringType);
845 if (constraints != null) {
848 else if (!constraints.AreEqual (gc))
851 if (!is_override && (temp_gc != null))
856 Report.SymbolRelatedToPreviousError (implementing);
859 425, Location, "The constraints for type " +
860 "parameter `{0}' of method `{1}' must match " +
861 "the constraints for type parameter `{2}' " +
862 "of interface method `{3}'. Consider using " +
863 "an explicit interface implementation instead",
864 Name, TypeManager.CSharpSignature (builder),
865 TypeManager.CSharpName (mparam), TypeManager.CSharpSignature (mb));
868 } else if (DeclSpace is CompilerGeneratedClass) {
869 TypeParameter[] tparams = DeclSpace.TypeParameters;
870 Type[] types = new Type [tparams.Length];
871 for (int i = 0; i < tparams.Length; i++)
872 types [i] = tparams [i].Type;
874 if (constraints != null)
875 gc = new InflatedConstraints (constraints, types);
877 gc = (GenericConstraints) constraints;
880 SetConstraints (type);
884 public static TypeParameter FindTypeParameter (TypeParameter[] tparams, string name)
886 foreach (var tp in tparams) {
894 public void SetConstraints (GenericTypeParameterBuilder type)
896 GenericParameterAttributes attr = GenericParameterAttributes.None;
897 if (variance == Variance.Contravariant)
898 attr |= GenericParameterAttributes.Contravariant;
899 else if (variance == Variance.Covariant)
900 attr |= GenericParameterAttributes.Covariant;
903 if (gc.HasClassConstraint || gc.HasValueTypeConstraint)
904 type.SetBaseTypeConstraint (gc.EffectiveBaseClass);
906 attr |= gc.Attributes;
907 type.SetInterfaceConstraints (gc.InterfaceConstraints);
908 TypeManager.RegisterBuilder (type, gc.InterfaceConstraints);
911 type.SetGenericParameterAttributes (attr);
915 /// This is called for each part of a partial generic type definition.
917 /// If `new_constraints' is not null and we don't already have constraints,
918 /// they become our constraints. If we already have constraints, we must
919 /// check that they're the same.
922 public bool UpdateConstraints (MemberCore ec, Constraints new_constraints)
925 throw new InvalidOperationException ();
927 if (new_constraints == null)
930 if (!new_constraints.Resolve (ec, this, Report))
932 if (!new_constraints.ResolveTypes (ec, Report))
935 if (constraints != null)
936 return constraints.AreEqual (new_constraints);
938 constraints = new_constraints;
942 public override void Emit ()
944 if (OptAttributes != null)
945 OptAttributes.Emit ();
950 public override string DocCommentHeader {
952 throw new InvalidOperationException (
953 "Unexpected attempt to get doc comment from " + this.GetType () + ".");
961 public override bool Define ()
966 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
968 type.SetCustomAttribute (cb);
971 public override AttributeTargets AttributeTargets {
973 return AttributeTargets.GenericParameter;
977 public override string[] ValidAttributeTargets {
979 return attribute_target;
987 string IMemberContainer.Name {
991 MemberCache IMemberContainer.BaseCache {
996 if (gc.EffectiveBaseClass.BaseType == null)
999 return TypeManager.LookupMemberCache (gc.EffectiveBaseClass.BaseType);
1003 bool IMemberContainer.IsInterface {
1004 get { return false; }
1007 MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf)
1009 throw new NotSupportedException ();
1012 public MemberCache MemberCache {
1014 if (member_cache != null)
1015 return member_cache;
1020 Type[] ifaces = TypeManager.ExpandInterfaces (gc.InterfaceConstraints);
1021 member_cache = new MemberCache (this, gc.EffectiveBaseClass, ifaces);
1023 return member_cache;
1027 public MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1028 MemberFilter filter, object criteria)
1031 return MemberList.Empty;
1033 ArrayList members = new ArrayList ();
1035 if (gc.HasClassConstraint) {
1036 MemberList list = TypeManager.FindMembers (
1037 gc.ClassConstraint, mt, bf, filter, criteria);
1039 members.AddRange (list);
1042 Type[] ifaces = TypeManager.ExpandInterfaces (gc.InterfaceConstraints);
1043 foreach (Type t in ifaces) {
1044 MemberList list = TypeManager.FindMembers (
1045 t, mt, bf, filter, criteria);
1047 members.AddRange (list);
1050 return new MemberList (members);
1053 public bool IsSubclassOf (Type t)
1055 if (type.Equals (t))
1058 if (constraints != null)
1059 return constraints.IsSubclassOf (t);
1064 public void InflateConstraints (Type declaring)
1066 if (constraints != null)
1067 gc = new InflatedConstraints (constraints, declaring);
1070 public override bool IsClsComplianceRequired ()
1075 protected class InflatedConstraints : GenericConstraints
1077 GenericConstraints gc;
1079 Type class_constraint;
1080 Type[] iface_constraints;
1083 public InflatedConstraints (GenericConstraints gc, Type declaring)
1084 : this (gc, TypeManager.GetTypeArguments (declaring))
1087 public InflatedConstraints (GenericConstraints gc, Type[] dargs)
1092 ArrayList list = new ArrayList ();
1093 if (gc.HasClassConstraint)
1094 list.Add (inflate (gc.ClassConstraint));
1095 foreach (Type iface in gc.InterfaceConstraints)
1096 list.Add (inflate (iface));
1098 bool has_class_constr = false;
1099 if (list.Count > 0) {
1100 Type first = (Type) list [0];
1101 has_class_constr = !first.IsGenericParameter && !first.IsInterface;
1104 if ((list.Count > 0) && has_class_constr) {
1105 class_constraint = (Type) list [0];
1106 iface_constraints = new Type [list.Count - 1];
1107 list.CopyTo (1, iface_constraints, 0, list.Count - 1);
1109 iface_constraints = new Type [list.Count];
1110 list.CopyTo (iface_constraints, 0);
1113 if (HasValueTypeConstraint)
1114 base_type = TypeManager.value_type;
1115 else if (class_constraint != null)
1116 base_type = class_constraint;
1118 base_type = TypeManager.object_type;
1121 Type inflate (Type t)
1125 if (t.IsGenericParameter)
1126 return t.GenericParameterPosition < dargs.Length ? dargs [t.GenericParameterPosition] : t;
1127 if (t.IsGenericType) {
1128 Type[] args = t.GetGenericArguments ();
1129 Type[] inflated = new Type [args.Length];
1131 for (int i = 0; i < args.Length; i++)
1132 inflated [i] = inflate (args [i]);
1134 t = t.GetGenericTypeDefinition ();
1135 t = t.MakeGenericType (inflated);
1141 public override string TypeParameter {
1142 get { return gc.TypeParameter; }
1145 public override GenericParameterAttributes Attributes {
1146 get { return gc.Attributes; }
1149 public override Type ClassConstraint {
1150 get { return class_constraint; }
1153 public override Type EffectiveBaseClass {
1154 get { return base_type; }
1157 public override Type[] InterfaceConstraints {
1158 get { return iface_constraints; }
1164 /// A TypeExpr which already resolved to a type parameter.
1166 public class TypeParameterExpr : TypeExpr {
1168 public TypeParameterExpr (TypeParameter type_parameter, Location loc)
1170 this.type = type_parameter.Type;
1171 this.eclass = ExprClass.TypeParameter;
1175 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
1177 throw new NotSupportedException ();
1180 public override FullNamedExpression ResolveAsTypeStep (IMemberContext ec, bool silent)
1185 public override bool IsInterface {
1186 get { return false; }
1189 public override bool CheckAccessLevel (IMemberContext ds)
1196 // Tracks the type arguments when instantiating a generic type. It's used
1197 // by both type arguments and type parameters
1199 public class TypeArguments {
1203 public TypeArguments ()
1205 args = new ArrayList ();
1208 public TypeArguments (params FullNamedExpression[] types)
1210 this.args = new ArrayList (types);
1213 public void Add (FullNamedExpression type)
1218 public void Add (TypeArguments new_args)
1220 args.AddRange (new_args.args);
1223 // TODO: Should be deleted
1224 public TypeParameterName[] GetDeclarations ()
1226 return (TypeParameterName[]) args.ToArray (typeof (TypeParameterName));
1230 /// We may only be used after Resolve() is called and return the fully
1233 public Type[] Arguments {
1245 public string GetSignatureForError()
1247 StringBuilder sb = new StringBuilder();
1248 for (int i = 0; i < Count; ++i)
1250 Expression expr = (Expression)args [i];
1251 sb.Append(expr.GetSignatureForError());
1255 return sb.ToString();
1259 /// Resolve the type arguments.
1261 public bool Resolve (IMemberContext ec)
1264 return atypes.Length != 0;
1266 int count = args.Count;
1269 atypes = new Type [count];
1271 for (int i = 0; i < count; i++){
1272 TypeExpr te = ((FullNamedExpression) args[i]).ResolveAsTypeTerminal (ec, false);
1278 atypes[i] = te.Type;
1280 if (te.Type.IsSealed && te.Type.IsAbstract) {
1281 ec.Compiler.Report.Error (718, te.Location, "`{0}': static classes cannot be used as generic arguments",
1282 te.GetSignatureForError ());
1286 if (te.Type.IsPointer || TypeManager.IsSpecialType (te.Type)) {
1287 ec.Compiler.Report.Error (306, te.Location,
1288 "The type `{0}' may not be used as a type argument",
1289 te.GetSignatureForError ());
1295 atypes = Type.EmptyTypes;
1300 public TypeArguments Clone ()
1302 TypeArguments copy = new TypeArguments ();
1303 foreach (Expression ta in args)
1310 public class TypeParameterName : SimpleName
1312 Attributes attributes;
1315 public TypeParameterName (string name, Attributes attrs, Location loc)
1316 : this (name, attrs, Variance.None, loc)
1320 public TypeParameterName (string name, Attributes attrs, Variance variance, Location loc)
1324 this.variance = variance;
1327 public Attributes OptAttributes {
1333 public Variance Variance {
1341 /// A reference expression to generic type
1343 class GenericTypeExpr : TypeExpr
1346 Type[] gen_params; // TODO: Waiting for constrains check cleanup
1350 // Should be carefully used only with defined generic containers. Type parameters
1351 // can be used as type arguments in this case.
1353 // TODO: This could be GenericTypeExpr specialization
1355 public GenericTypeExpr (DeclSpace gType, Location l)
1357 open_type = gType.TypeBuilder.GetGenericTypeDefinition ();
1359 args = new TypeArguments ();
1360 foreach (TypeParameter type_param in gType.TypeParameters)
1361 args.Add (new TypeParameterExpr (type_param, l));
1367 /// Instantiate the generic type `t' with the type arguments `args'.
1368 /// Use this constructor if you already know the fully resolved
1371 public GenericTypeExpr (Type t, TypeArguments args, Location l)
1373 open_type = t.GetGenericTypeDefinition ();
1379 public TypeArguments TypeArguments {
1380 get { return args; }
1383 public override string GetSignatureForError ()
1385 return TypeManager.CSharpName (type);
1388 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
1390 if (eclass != ExprClass.Invalid)
1393 eclass = ExprClass.Type;
1395 if (!args.Resolve (ec))
1398 gen_params = open_type.GetGenericArguments ();
1399 Type[] atypes = args.Arguments;
1401 if (atypes.Length != gen_params.Length) {
1402 Namespace.Error_InvalidNumberOfTypeArguments (open_type, loc);
1407 // Now bind the parameters
1409 type = open_type.MakeGenericType (atypes);
1414 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1415 /// after fully resolving the constructed type.
1417 public bool CheckConstraints (IMemberContext ec)
1419 return ConstraintChecker.CheckConstraints (ec, open_type, gen_params, args.Arguments, loc);
1422 public override bool CheckAccessLevel (IMemberContext mc)
1424 return mc.CurrentTypeDefinition.CheckAccessLevel (open_type);
1427 public bool HasDynamicArguments ()
1429 return HasDynamicArguments (args.Arguments);
1432 static bool HasDynamicArguments (Type[] args)
1434 foreach (var item in args)
1436 if (TypeManager.IsGenericType (item))
1437 return HasDynamicArguments (TypeManager.GetTypeArguments (item));
1439 if (TypeManager.IsDynamicType (item))
1446 public override bool IsClass {
1447 get { return open_type.IsClass; }
1450 public override bool IsValueType {
1451 get { return TypeManager.IsStruct (open_type); }
1454 public override bool IsInterface {
1455 get { return open_type.IsInterface; }
1458 public override bool IsSealed {
1459 get { return open_type.IsSealed; }
1462 public override bool Equals (object obj)
1464 GenericTypeExpr cobj = obj as GenericTypeExpr;
1468 if ((type == null) || (cobj.type == null))
1471 return type == cobj.type;
1474 public override int GetHashCode ()
1476 return base.GetHashCode ();
1480 public abstract class ConstraintChecker
1482 protected readonly Type[] gen_params;
1483 protected readonly Type[] atypes;
1484 protected readonly Location loc;
1485 protected Report Report;
1487 protected ConstraintChecker (Type[] gen_params, Type[] atypes, Location loc, Report r)
1489 this.gen_params = gen_params;
1490 this.atypes = atypes;
1496 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1497 /// after fully resolving the constructed type.
1499 public bool CheckConstraints (IMemberContext ec)
1501 for (int i = 0; i < gen_params.Length; i++) {
1502 if (!CheckConstraints (ec, i))
1509 protected bool CheckConstraints (IMemberContext ec, int index)
1511 Type atype = TypeManager.TypeToCoreType (atypes [index]);
1512 Type ptype = gen_params [index];
1517 Expression aexpr = new EmptyExpression (atype);
1519 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (ptype);
1523 bool is_class, is_struct;
1524 if (atype.IsGenericParameter) {
1525 GenericConstraints agc = TypeManager.GetTypeParameterConstraints (atype);
1527 if (agc is Constraints) {
1528 // FIXME: No constraints can be resolved here, we are in
1529 // completely wrong/different context. This path is hit
1530 // when resolving base type of unresolved generic type
1531 // with constraints. We are waiting with CheckConsttraints
1532 // after type-definition but not in this case
1533 if (!((Constraints) agc).Resolve (null, null, Report))
1536 is_class = agc.IsReferenceType;
1537 is_struct = agc.IsValueType;
1539 is_class = is_struct = false;
1542 is_class = TypeManager.IsReferenceType (atype);
1543 is_struct = TypeManager.IsValueType (atype) && !TypeManager.IsNullableType (atype);
1547 // First, check the `class' and `struct' constraints.
1549 if (gc.HasReferenceTypeConstraint && !is_class) {
1550 Report.Error (452, loc, "The type `{0}' must be " +
1551 "a reference type in order to use it " +
1552 "as type parameter `{1}' in the " +
1553 "generic type or method `{2}'.",
1554 TypeManager.CSharpName (atype),
1555 TypeManager.CSharpName (ptype),
1556 GetSignatureForError ());
1558 } else if (gc.HasValueTypeConstraint && !is_struct) {
1559 Report.Error (453, loc, "The type `{0}' must be a " +
1560 "non-nullable value type in order to use it " +
1561 "as type parameter `{1}' in the " +
1562 "generic type or method `{2}'.",
1563 TypeManager.CSharpName (atype),
1564 TypeManager.CSharpName (ptype),
1565 GetSignatureForError ());
1570 // The class constraint comes next.
1572 if (gc.HasClassConstraint) {
1573 if (!CheckConstraint (ec, ptype, aexpr, gc.ClassConstraint))
1578 // Now, check the interface constraints.
1580 if (gc.InterfaceConstraints != null) {
1581 foreach (Type it in gc.InterfaceConstraints) {
1582 if (!CheckConstraint (ec, ptype, aexpr, it))
1588 // Finally, check the constructor constraint.
1591 if (!gc.HasConstructorConstraint)
1594 if (TypeManager.IsValueType (atype))
1597 if (HasDefaultConstructor (atype))
1600 Report_SymbolRelatedToPreviousError ();
1601 Report.SymbolRelatedToPreviousError (atype);
1602 Report.Error (310, loc, "The type `{0}' must have a public " +
1603 "parameterless constructor in order to use it " +
1604 "as parameter `{1}' in the generic type or " +
1606 TypeManager.CSharpName (atype),
1607 TypeManager.CSharpName (ptype),
1608 GetSignatureForError ());
1612 Type InflateType(IMemberContext ec, Type ctype)
1614 Type[] types = TypeManager.GetTypeArguments (ctype);
1616 TypeArguments new_args = new TypeArguments ();
1618 for (int i = 0; i < types.Length; i++) {
1619 Type t = TypeManager.TypeToCoreType (types [i]);
1621 if (t.IsGenericParameter) {
1622 int pos = t.GenericParameterPosition;
1623 if (t.DeclaringMethod == null && this is MethodConstraintChecker) {
1624 Type parent = ((MethodConstraintChecker) this).declaring_type;
1625 t = parent.GetGenericArguments ()[pos];
1629 } else if(TypeManager.HasGenericArguments(t)) {
1630 t = InflateType (ec, t);
1635 new_args.Add (new TypeExpression (t, loc));
1638 TypeExpr ct = new GenericTypeExpr (ctype, new_args, loc);
1639 if (ct.ResolveAsTypeStep (ec, false) == null)
1645 protected bool CheckConstraint (IMemberContext ec, Type ptype, Expression expr,
1649 // All this is needed because we don't have
1650 // real inflated type hierarchy
1652 if (TypeManager.HasGenericArguments (ctype)) {
1653 ctype = InflateType (ec, ctype);
1657 } else if (ctype.IsGenericParameter) {
1658 int pos = ctype.GenericParameterPosition;
1659 if (ctype.DeclaringMethod == null) {
1663 ctype = atypes [pos];
1667 if (Convert.ImplicitStandardConversionExists (expr, ctype))
1670 Report_SymbolRelatedToPreviousError ();
1671 Report.SymbolRelatedToPreviousError (expr.Type);
1673 if (TypeManager.IsNullableType (expr.Type) && ctype.IsInterface) {
1674 Report.Error (313, loc,
1675 "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
1676 "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
1677 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ptype),
1678 GetSignatureForError (), TypeManager.CSharpName (ctype));
1680 Report.Error (309, loc,
1681 "The type `{0}' must be convertible to `{1}' in order to " +
1682 "use it as parameter `{2}' in the generic type or method `{3}'",
1683 TypeManager.CSharpName (expr.Type), TypeManager.CSharpName (ctype),
1684 TypeManager.CSharpName (ptype), GetSignatureForError ());
1689 static bool HasDefaultConstructor (Type atype)
1691 TypeParameter tparam = TypeManager.LookupTypeParameter (atype);
1692 if (tparam != null) {
1693 if (tparam.GenericConstraints == null)
1696 return tparam.GenericConstraints.HasConstructorConstraint ||
1697 tparam.GenericConstraints.HasValueTypeConstraint;
1700 if (atype.IsAbstract)
1704 atype = TypeManager.DropGenericTypeArguments (atype);
1705 if (atype is TypeBuilder) {
1706 TypeContainer tc = TypeManager.LookupTypeContainer (atype);
1707 if (tc.InstanceConstructors == null) {
1708 atype = atype.BaseType;
1712 foreach (Constructor c in tc.InstanceConstructors) {
1713 if ((c.ModFlags & Modifiers.PUBLIC) == 0)
1715 if ((c.Parameters.FixedParameters != null) &&
1716 (c.Parameters.FixedParameters.Length != 0))
1718 if (c.Parameters.HasArglist || c.Parameters.HasParams)
1725 MemberInfo [] list = TypeManager.MemberLookup (null, null, atype, MemberTypes.Constructor,
1726 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
1727 ConstructorInfo.ConstructorName, null);
1732 foreach (MethodBase mb in list) {
1733 AParametersCollection pd = TypeManager.GetParameterData (mb);
1741 protected abstract string GetSignatureForError ();
1742 protected abstract void Report_SymbolRelatedToPreviousError ();
1744 public static bool CheckConstraints (IMemberContext ec, MethodBase definition,
1745 MethodBase instantiated, Location loc)
1747 MethodConstraintChecker checker = new MethodConstraintChecker (
1748 definition, instantiated.DeclaringType, definition.GetGenericArguments (),
1749 instantiated.GetGenericArguments (), loc, ec.Compiler.Report);
1751 return checker.CheckConstraints (ec);
1754 public static bool CheckConstraints (IMemberContext ec, Type gt, Type[] gen_params,
1755 Type[] atypes, Location loc)
1757 TypeConstraintChecker checker = new TypeConstraintChecker (
1758 gt, gen_params, atypes, loc, ec.Compiler.Report);
1760 return checker.CheckConstraints (ec);
1763 protected class MethodConstraintChecker : ConstraintChecker
1765 MethodBase definition;
1766 public Type declaring_type;
1768 public MethodConstraintChecker (MethodBase definition, Type declaringType, Type[] gen_params,
1769 Type[] atypes, Location loc, Report r)
1770 : base (gen_params, atypes, loc, r)
1772 this.declaring_type = declaringType;
1773 this.definition = definition;
1776 protected override string GetSignatureForError ()
1778 return TypeManager.CSharpSignature (definition);
1781 protected override void Report_SymbolRelatedToPreviousError ()
1783 Report.SymbolRelatedToPreviousError (definition);
1787 protected class TypeConstraintChecker : ConstraintChecker
1791 public TypeConstraintChecker (Type gt, Type[] gen_params, Type[] atypes,
1792 Location loc, Report r)
1793 : base (gen_params, atypes, loc, r)
1798 protected override string GetSignatureForError ()
1800 return TypeManager.CSharpName (gt);
1803 protected override void Report_SymbolRelatedToPreviousError ()
1805 Report.SymbolRelatedToPreviousError (gt);
1811 /// A generic method definition.
1813 public class GenericMethod : DeclSpace
1815 FullNamedExpression return_type;
1816 ParametersCompiled parameters;
1818 public GenericMethod (NamespaceEntry ns, DeclSpace parent, MemberName name,
1819 FullNamedExpression return_type, ParametersCompiled parameters)
1820 : base (ns, parent, name, null)
1822 this.return_type = return_type;
1823 this.parameters = parameters;
1826 public override TypeContainer CurrentTypeDefinition {
1828 return Parent.CurrentTypeDefinition;
1832 public override TypeParameter[] CurrentTypeParameters {
1834 return base.type_params;
1838 public override TypeBuilder DefineType ()
1840 throw new Exception ();
1843 public override bool Define ()
1845 for (int i = 0; i < TypeParameters.Length; i++)
1846 if (!TypeParameters [i].Resolve (this))
1853 /// Define and resolve the type parameters.
1854 /// We're called from Method.Define().
1856 public bool Define (MethodOrOperator m)
1858 TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
1859 string[] snames = new string [names.Length];
1860 for (int i = 0; i < names.Length; i++) {
1861 string type_argument_name = names[i].Name;
1862 int idx = parameters.GetParameterIndexByName (type_argument_name);
1866 b = new Block (null);
1868 b.Error_AlreadyDeclaredTypeParameter (Report, parameters [i].Location,
1869 type_argument_name, "method parameter");
1872 snames[i] = type_argument_name;
1875 GenericTypeParameterBuilder[] gen_params = m.MethodBuilder.DefineGenericParameters (snames);
1876 for (int i = 0; i < TypeParameters.Length; i++)
1877 TypeParameters [i].Define (gen_params [i]);
1882 for (int i = 0; i < TypeParameters.Length; i++) {
1883 if (!TypeParameters [i].ResolveType (this))
1891 /// We're called from MethodData.Define() after creating the MethodBuilder.
1893 public bool DefineType (IMemberContext ec, MethodBuilder mb,
1894 MethodInfo implementing, bool is_override)
1896 for (int i = 0; i < TypeParameters.Length; i++)
1897 if (!TypeParameters [i].DefineType (
1898 ec, mb, implementing, is_override))
1901 bool ok = parameters.Resolve (ec);
1903 if ((return_type != null) && (return_type.ResolveAsTypeTerminal (ec, false) == null))
1909 public void EmitAttributes ()
1911 for (int i = 0; i < TypeParameters.Length; i++)
1912 TypeParameters [i].Emit ();
1914 if (OptAttributes != null)
1915 OptAttributes.Emit ();
1918 public override MemberList FindMembers (MemberTypes mt, BindingFlags bf,
1919 MemberFilter filter, object criteria)
1921 throw new Exception ();
1924 public override string GetSignatureForError ()
1926 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1929 public override MemberCache MemberCache {
1935 public override AttributeTargets AttributeTargets {
1937 return AttributeTargets.Method | AttributeTargets.ReturnValue;
1941 public override string DocCommentHeader {
1942 get { return "M:"; }
1945 public new void VerifyClsCompliance ()
1947 foreach (TypeParameter tp in TypeParameters) {
1948 if (tp.Constraints == null)
1951 tp.Constraints.VerifyClsCompliance (Report);
1956 public partial class TypeManager
1958 static public Type activator_type;
1960 public static TypeContainer LookupGenericTypeContainer (Type t)
1962 t = DropGenericTypeArguments (t);
1963 return LookupTypeContainer (t);
1966 public static Variance GetTypeParameterVariance (Type type)
1968 TypeParameter tparam = LookupTypeParameter (type);
1970 return tparam.Variance;
1972 switch (type.GenericParameterAttributes & GenericParameterAttributes.VarianceMask) {
1973 case GenericParameterAttributes.Covariant:
1974 return Variance.Covariant;
1975 case GenericParameterAttributes.Contravariant:
1976 return Variance.Contravariant;
1978 return Variance.None;
1982 public static Variance CheckTypeVariance (Type t, Variance expected, IMemberContext member)
1984 TypeParameter tp = LookupTypeParameter (t);
1986 Variance v = tp.Variance;
1987 if (expected == Variance.None && v != expected ||
1988 expected == Variance.Covariant && v == Variance.Contravariant ||
1989 expected == Variance.Contravariant && v == Variance.Covariant)
1990 tp.ErrorInvalidVariance (member, expected);
1995 if (t.IsGenericType) {
1996 Type[] targs_definition = GetTypeArguments (DropGenericTypeArguments (t));
1997 Type[] targs = GetTypeArguments (t);
1998 for (int i = 0; i < targs_definition.Length; ++i) {
1999 Variance v = GetTypeParameterVariance (targs_definition[i]);
2000 CheckTypeVariance (targs[i], (Variance) ((int)v * (int)expected), member);
2007 return CheckTypeVariance (GetElementType (t), expected, member);
2009 return Variance.None;
2012 public static bool IsVariantOf (Type type1, Type type2)
2014 if (!type1.IsGenericType || !type2.IsGenericType)
2017 Type generic_target_type = DropGenericTypeArguments (type2);
2018 if (DropGenericTypeArguments (type1) != generic_target_type)
2021 Type[] t1 = GetTypeArguments (type1);
2022 Type[] t2 = GetTypeArguments (type2);
2023 Type[] targs_definition = GetTypeArguments (generic_target_type);
2024 for (int i = 0; i < targs_definition.Length; ++i) {
2025 Variance v = GetTypeParameterVariance (targs_definition [i]);
2026 if (v == Variance.None) {
2032 if (v == Variance.Covariant) {
2033 if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t1 [i]), t2 [i]))
2035 } else if (!Convert.ImplicitReferenceConversionExists (new EmptyExpression (t2[i]), t1[i])) {
2044 /// Check whether `a' and `b' may become equal generic types.
2045 /// The algorithm to do that is a little bit complicated.
2047 public static bool MayBecomeEqualGenericTypes (Type a, Type b, Type[] class_inferred,
2048 Type[] method_inferred)
2050 if (a.IsGenericParameter) {
2052 // If a is an array of a's type, they may never
2056 b = GetElementType (b);
2062 // If b is a generic parameter or an actual type,
2063 // they may become equal:
2065 // class X<T,U> : I<T>, I<U>
2066 // class X<T> : I<T>, I<float>
2068 if (b.IsGenericParameter || !b.IsGenericType) {
2069 int pos = a.GenericParameterPosition;
2070 Type[] args = a.DeclaringMethod != null ? method_inferred : class_inferred;
2071 if (args [pos] == null) {
2076 return args [pos] == a;
2080 // We're now comparing a type parameter with a
2081 // generic instance. They may become equal unless
2082 // the type parameter appears anywhere in the
2083 // generic instance:
2085 // class X<T,U> : I<T>, I<X<U>>
2086 // -> error because you could instanciate it as
2089 // class X<T> : I<T>, I<X<T>> -> ok
2092 Type[] bargs = GetTypeArguments (b);
2093 for (int i = 0; i < bargs.Length; i++) {
2094 if (a.Equals (bargs [i]))
2101 if (b.IsGenericParameter)
2102 return MayBecomeEqualGenericTypes (b, a, class_inferred, method_inferred);
2105 // At this point, neither a nor b are a type parameter.
2107 // If one of them is a generic instance, let
2108 // MayBecomeEqualGenericInstances() compare them (if the
2109 // other one is not a generic instance, they can never
2113 if (a.IsGenericType || b.IsGenericType)
2114 return MayBecomeEqualGenericInstances (a, b, class_inferred, method_inferred);
2117 // If both of them are arrays.
2120 if (a.IsArray && b.IsArray) {
2121 if (a.GetArrayRank () != b.GetArrayRank ())
2124 a = GetElementType (a);
2125 b = GetElementType (b);
2127 return MayBecomeEqualGenericTypes (a, b, class_inferred, method_inferred);
2131 // Ok, two ordinary types.
2134 return a.Equals (b);
2138 // Checks whether two generic instances may become equal for some
2139 // particular instantiation (26.3.1).
2141 public static bool MayBecomeEqualGenericInstances (Type a, Type b,
2142 Type[] class_inferred,
2143 Type[] method_inferred)
2145 if (!a.IsGenericType || !b.IsGenericType)
2147 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
2150 return MayBecomeEqualGenericInstances (
2151 GetTypeArguments (a), GetTypeArguments (b), class_inferred, method_inferred);
2154 public static bool MayBecomeEqualGenericInstances (Type[] aargs, Type[] bargs,
2155 Type[] class_inferred,
2156 Type[] method_inferred)
2158 if (aargs.Length != bargs.Length)
2161 for (int i = 0; i < aargs.Length; i++) {
2162 if (!MayBecomeEqualGenericTypes (aargs [i], bargs [i], class_inferred, method_inferred))
2170 /// Type inference. Try to infer the type arguments from `method',
2171 /// which is invoked with the arguments `arguments'. This is used
2172 /// when resolving an Invocation or a DelegateInvocation and the user
2173 /// did not explicitly specify type arguments.
2175 public static int InferTypeArguments (ResolveContext ec, Arguments arguments, ref MethodBase method)
2177 ATypeInference ti = ATypeInference.CreateInstance (arguments);
2178 Type[] i_args = ti.InferMethodArguments (ec, method);
2180 return ti.InferenceScore;
2182 if (i_args.Length == 0)
2185 method = ((MethodInfo) method).MakeGenericMethod (i_args);
2190 public static bool InferTypeArguments (ResolveContext ec, AParametersCollection param, ref MethodBase method)
2192 if (!TypeManager.IsGenericMethod (method))
2195 ATypeInference ti = ATypeInference.CreateInstance (DelegateCreation.CreateDelegateMethodArguments (param, Location.Null));
2196 Type[] i_args = ti.InferDelegateArguments (ec, method);
2200 method = ((MethodInfo) method).MakeGenericMethod (i_args);
2206 abstract class ATypeInference
2208 protected readonly Arguments arguments;
2209 protected readonly int arg_count;
2211 protected ATypeInference (Arguments arguments)
2213 this.arguments = arguments;
2214 if (arguments != null)
2215 arg_count = arguments.Count;
2218 public static ATypeInference CreateInstance (Arguments arguments)
2220 return new TypeInference (arguments);
2223 public virtual int InferenceScore {
2225 return int.MaxValue;
2229 public abstract Type[] InferMethodArguments (ResolveContext ec, MethodBase method);
2230 // public abstract Type[] InferDelegateArguments (ResolveContext ec, MethodBase method);
2234 // Implements C# type inference
2236 class TypeInference : ATypeInference
2239 // Tracks successful rate of type inference
2241 int score = int.MaxValue;
2243 public TypeInference (Arguments arguments)
2248 public override int InferenceScore {
2255 public override Type[] InferDelegateArguments (ResolveContext ec, MethodBase method)
2257 AParametersCollection pd = TypeManager.GetParameterData (method);
2258 if (arg_count != pd.Count)
2261 Type[] d_gargs = method.GetGenericArguments ();
2262 TypeInferenceContext context = new TypeInferenceContext (d_gargs);
2264 // A lower-bound inference is made from each argument type Uj of D
2265 // to the corresponding parameter type Tj of M
2266 for (int i = 0; i < arg_count; ++i) {
2267 Type t = pd.Types [i];
2268 if (!t.IsGenericParameter)
2271 context.LowerBoundInference (arguments [i].Expr.Type, t);
2274 if (!context.FixAllTypes (ec))
2277 return context.InferredTypeArguments;
2280 public override Type[] InferMethodArguments (ResolveContext ec, MethodBase method)
2282 Type[] method_generic_args = method.GetGenericArguments ();
2283 TypeInferenceContext context = new TypeInferenceContext (method_generic_args);
2284 if (!context.UnfixedVariableExists)
2285 return Type.EmptyTypes;
2287 AParametersCollection pd = TypeManager.GetParameterData (method);
2288 if (!InferInPhases (ec, context, pd))
2291 return context.InferredTypeArguments;
2295 // Implements method type arguments inference
2297 bool InferInPhases (ResolveContext ec, TypeInferenceContext tic, AParametersCollection methodParameters)
2299 int params_arguments_start;
2300 if (methodParameters.HasParams) {
2301 params_arguments_start = methodParameters.Count - 1;
2303 params_arguments_start = arg_count;
2306 Type [] ptypes = methodParameters.Types;
2309 // The first inference phase
2311 Type method_parameter = null;
2312 for (int i = 0; i < arg_count; i++) {
2313 Argument a = arguments [i];
2317 if (i < params_arguments_start) {
2318 method_parameter = methodParameters.Types [i];
2319 } else if (i == params_arguments_start) {
2320 if (arg_count == params_arguments_start + 1 && TypeManager.HasElementType (a.Type))
2321 method_parameter = methodParameters.Types [params_arguments_start];
2323 method_parameter = TypeManager.GetElementType (methodParameters.Types [params_arguments_start]);
2325 ptypes = (Type[]) ptypes.Clone ();
2326 ptypes [i] = method_parameter;
2330 // When a lambda expression, an anonymous method
2331 // is used an explicit argument type inference takes a place
2333 AnonymousMethodExpression am = a.Expr as AnonymousMethodExpression;
2335 if (am.ExplicitTypeInference (ec, tic, method_parameter))
2341 score -= tic.ExactInference (a.Type, method_parameter);
2345 if (a.Expr.Type == TypeManager.null_type)
2348 if (TypeManager.IsValueType (method_parameter)) {
2349 score -= tic.LowerBoundInference (a.Type, method_parameter);
2354 // Otherwise an output type inference is made
2356 score -= tic.OutputTypeInference (ec, a.Expr, method_parameter);
2360 // Part of the second phase but because it happens only once
2361 // we don't need to call it in cycle
2363 bool fixed_any = false;
2364 if (!tic.FixIndependentTypeArguments (ec, ptypes, ref fixed_any))
2367 return DoSecondPhase (ec, tic, ptypes, !fixed_any);
2370 bool DoSecondPhase (ResolveContext ec, TypeInferenceContext tic, Type[] methodParameters, bool fixDependent)
2372 bool fixed_any = false;
2373 if (fixDependent && !tic.FixDependentTypes (ec, ref fixed_any))
2376 // If no further unfixed type variables exist, type inference succeeds
2377 if (!tic.UnfixedVariableExists)
2380 if (!fixed_any && fixDependent)
2383 // For all arguments where the corresponding argument output types
2384 // contain unfixed type variables but the input types do not,
2385 // an output type inference is made
2386 for (int i = 0; i < arg_count; i++) {
2388 // Align params arguments
2389 Type t_i = methodParameters [i >= methodParameters.Length ? methodParameters.Length - 1: i];
2391 if (!TypeManager.IsDelegateType (t_i)) {
2392 if (TypeManager.DropGenericTypeArguments (t_i) != TypeManager.expression_type)
2395 t_i = t_i.GetGenericArguments () [0];
2398 MethodInfo mi = Delegate.GetInvokeMethod (ec.Compiler, t_i, t_i);
2399 Type rtype = mi.ReturnType;
2402 // Blablabla, because reflection does not work with dynamic types
2403 Type[] g_args = t_i.GetGenericArguments ();
2404 rtype = g_args[rtype.GenericParameterPosition];
2407 if (tic.IsReturnTypeNonDependent (ec, mi, rtype))
2408 score -= tic.OutputTypeInference (ec, arguments [i].Expr, t_i);
2412 return DoSecondPhase (ec, tic, methodParameters, true);
2416 public class TypeInferenceContext
2427 public readonly Type Type;
2428 public readonly BoundKind Kind;
2430 public BoundInfo (Type type, BoundKind kind)
2436 public override int GetHashCode ()
2438 return Type.GetHashCode ();
2441 public override bool Equals (object obj)
2443 BoundInfo a = (BoundInfo) obj;
2444 return Type == a.Type && Kind == a.Kind;
2448 readonly Type[] unfixed_types;
2449 readonly Type[] fixed_types;
2450 readonly ArrayList[] bounds;
2453 public TypeInferenceContext (Type[] typeArguments)
2455 if (typeArguments.Length == 0)
2456 throw new ArgumentException ("Empty generic arguments");
2458 fixed_types = new Type [typeArguments.Length];
2459 for (int i = 0; i < typeArguments.Length; ++i) {
2460 if (typeArguments [i].IsGenericParameter) {
2461 if (bounds == null) {
2462 bounds = new ArrayList [typeArguments.Length];
2463 unfixed_types = new Type [typeArguments.Length];
2465 unfixed_types [i] = typeArguments [i];
2467 fixed_types [i] = typeArguments [i];
2473 // Used together with AddCommonTypeBound fo implement
2474 // 7.4.2.13 Finding the best common type of a set of expressions
2476 public TypeInferenceContext ()
2478 fixed_types = new Type [1];
2479 unfixed_types = new Type [1];
2480 unfixed_types[0] = InternalType.Arglist; // it can be any internal type
2481 bounds = new ArrayList [1];
2484 public Type[] InferredTypeArguments {
2490 public void AddCommonTypeBound (Type type)
2492 AddToBounds (new BoundInfo (type, BoundKind.Lower), 0);
2495 void AddToBounds (BoundInfo bound, int index)
2498 // Some types cannot be used as type arguments
2500 if (bound.Type == TypeManager.void_type || bound.Type.IsPointer)
2503 ArrayList a = bounds [index];
2505 a = new ArrayList ();
2508 if (a.Contains (bound))
2513 // SPEC: does not cover type inference using constraints
2515 //if (TypeManager.IsGenericParameter (t)) {
2516 // GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
2517 // if (constraints != null) {
2518 // //if (constraints.EffectiveBaseClass != null)
2519 // // t = constraints.EffectiveBaseClass;
2525 bool AllTypesAreFixed (Type[] types)
2527 foreach (Type t in types) {
2528 if (t.IsGenericParameter) {
2534 if (t.IsGenericType)
2535 return AllTypesAreFixed (t.GetGenericArguments ());
2542 // 26.3.3.8 Exact Inference
2544 public int ExactInference (Type u, Type v)
2546 // If V is an array type
2551 if (u.GetArrayRank () != v.GetArrayRank ())
2554 return ExactInference (TypeManager.GetElementType (u), TypeManager.GetElementType (v));
2557 // If V is constructed type and U is constructed type
2558 if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2559 if (!u.IsGenericType)
2562 Type [] ga_u = u.GetGenericArguments ();
2563 Type [] ga_v = v.GetGenericArguments ();
2564 if (ga_u.Length != ga_v.Length)
2568 for (int i = 0; i < ga_u.Length; ++i)
2569 score += ExactInference (ga_u [i], ga_v [i]);
2571 return score > 0 ? 1 : 0;
2574 // If V is one of the unfixed type arguments
2575 int pos = IsUnfixed (v);
2579 AddToBounds (new BoundInfo (u, BoundKind.Exact), pos);
2583 public bool FixAllTypes (ResolveContext ec)
2585 for (int i = 0; i < unfixed_types.Length; ++i) {
2586 if (!FixType (ec, i))
2593 // All unfixed type variables Xi are fixed for which all of the following hold:
2594 // a, There is at least one type variable Xj that depends on Xi
2595 // b, Xi has a non-empty set of bounds
2597 public bool FixDependentTypes (ResolveContext ec, ref bool fixed_any)
2599 for (int i = 0; i < unfixed_types.Length; ++i) {
2600 if (unfixed_types[i] == null)
2603 if (bounds[i] == null)
2606 if (!FixType (ec, i))
2616 // All unfixed type variables Xi which depend on no Xj are fixed
2618 public bool FixIndependentTypeArguments (ResolveContext ec, Type[] methodParameters, ref bool fixed_any)
2620 ArrayList types_to_fix = new ArrayList (unfixed_types);
2621 for (int i = 0; i < methodParameters.Length; ++i) {
2622 Type t = methodParameters[i];
2624 if (!TypeManager.IsDelegateType (t)) {
2625 if (TypeManager.DropGenericTypeArguments (t) != TypeManager.expression_type)
2628 t = t.GetGenericArguments () [0];
2631 if (t.IsGenericParameter)
2634 MethodInfo invoke = Delegate.GetInvokeMethod (ec.Compiler, t, t);
2635 Type rtype = invoke.ReturnType;
2636 if (!rtype.IsGenericParameter && !rtype.IsGenericType)
2640 // Blablabla, because reflection does not work with dynamic types
2641 if (rtype.IsGenericParameter) {
2642 Type [] g_args = t.GetGenericArguments ();
2643 rtype = g_args [rtype.GenericParameterPosition];
2646 // Remove dependent types, they cannot be fixed yet
2647 RemoveDependentTypes (types_to_fix, rtype);
2650 foreach (Type t in types_to_fix) {
2654 int idx = IsUnfixed (t);
2655 if (idx >= 0 && !FixType (ec, idx)) {
2660 fixed_any = types_to_fix.Count > 0;
2667 public bool FixType (ResolveContext ec, int i)
2669 // It's already fixed
2670 if (unfixed_types[i] == null)
2671 throw new InternalErrorException ("Type argument has been already fixed");
2676 ArrayList candidates = (ArrayList)bounds [i];
2677 if (candidates == null)
2680 if (candidates.Count == 1) {
2681 unfixed_types[i] = null;
2682 Type t = ((BoundInfo) candidates[0]).Type;
2683 if (t == TypeManager.null_type)
2686 fixed_types [i] = t;
2691 // Determines a unique type from which there is
2692 // a standard implicit conversion to all the other
2695 Type best_candidate = null;
2697 int candidates_count = candidates.Count;
2698 for (int ci = 0; ci < candidates_count; ++ci) {
2699 BoundInfo bound = (BoundInfo)candidates [ci];
2700 for (cii = 0; cii < candidates_count; ++cii) {
2704 BoundInfo cbound = (BoundInfo) candidates[cii];
2706 // Same type parameters with different bounds
2707 if (cbound.Type == bound.Type) {
2708 if (bound.Kind != BoundKind.Exact)
2714 if (bound.Kind == BoundKind.Exact || cbound.Kind == BoundKind.Exact) {
2715 if (cbound.Kind != BoundKind.Exact) {
2716 if (!Convert.ImplicitConversionExists (ec, new TypeExpression (cbound.Type, Location.Null), bound.Type)) {
2723 if (bound.Kind != BoundKind.Exact) {
2724 if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
2735 if (bound.Kind == BoundKind.Lower) {
2736 if (!Convert.ImplicitConversionExists (ec, new TypeExpression (cbound.Type, Location.Null), bound.Type)) {
2740 if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
2746 if (cii != candidates_count)
2749 if (best_candidate != null && best_candidate != bound.Type)
2752 best_candidate = bound.Type;
2755 if (best_candidate == null)
2758 unfixed_types[i] = null;
2759 fixed_types[i] = best_candidate;
2764 // Uses inferred types to inflate delegate type argument
2766 public Type InflateGenericArgument (Type parameter)
2768 if (parameter.IsGenericParameter) {
2770 // Inflate method generic argument (MVAR) only
2772 if (parameter.DeclaringMethod == null)
2775 return fixed_types [parameter.GenericParameterPosition];
2778 if (parameter.IsGenericType) {
2779 Type [] parameter_targs = parameter.GetGenericArguments ();
2780 for (int ii = 0; ii < parameter_targs.Length; ++ii) {
2781 parameter_targs [ii] = InflateGenericArgument (parameter_targs [ii]);
2783 return parameter.GetGenericTypeDefinition ().MakeGenericType (parameter_targs);
2790 // Tests whether all delegate input arguments are fixed and generic output type
2791 // requires output type inference
2793 public bool IsReturnTypeNonDependent (ResolveContext ec, MethodInfo invoke, Type returnType)
2795 if (returnType.IsGenericParameter) {
2796 if (IsFixed (returnType))
2798 } else if (returnType.IsGenericType) {
2799 if (TypeManager.IsDelegateType (returnType)) {
2800 invoke = Delegate.GetInvokeMethod (ec.Compiler, returnType, returnType);
2801 return IsReturnTypeNonDependent (ec, invoke, invoke.ReturnType);
2804 Type[] g_args = returnType.GetGenericArguments ();
2806 // At least one unfixed return type has to exist
2807 if (AllTypesAreFixed (g_args))
2813 // All generic input arguments have to be fixed
2814 AParametersCollection d_parameters = TypeManager.GetParameterData (invoke);
2815 return AllTypesAreFixed (d_parameters.Types);
2818 bool IsFixed (Type type)
2820 return IsUnfixed (type) == -1;
2823 int IsUnfixed (Type type)
2825 if (!type.IsGenericParameter)
2828 //return unfixed_types[type.GenericParameterPosition] != null;
2829 for (int i = 0; i < unfixed_types.Length; ++i) {
2830 if (unfixed_types [i] == type)
2838 // 26.3.3.9 Lower-bound Inference
2840 public int LowerBoundInference (Type u, Type v)
2842 return LowerBoundInference (u, v, false);
2846 // Lower-bound (false) or Upper-bound (true) inference based on inversed argument
2848 int LowerBoundInference (Type u, Type v, bool inversed)
2850 // If V is one of the unfixed type arguments
2851 int pos = IsUnfixed (v);
2853 AddToBounds (new BoundInfo (u, inversed ? BoundKind.Upper : BoundKind.Lower), pos);
2857 // If U is an array type
2859 int u_dim = u.GetArrayRank ();
2861 Type u_i = TypeManager.GetElementType (u);
2864 if (u_dim != v.GetArrayRank ())
2867 v_i = TypeManager.GetElementType (v);
2869 if (TypeManager.IsValueType (u_i))
2870 return ExactInference (u_i, v_i);
2872 return LowerBoundInference (u_i, v_i, inversed);
2878 if (v.IsGenericType) {
2879 Type g_v = v.GetGenericTypeDefinition ();
2880 if ((g_v != TypeManager.generic_ilist_type) && (g_v != TypeManager.generic_icollection_type) &&
2881 (g_v != TypeManager.generic_ienumerable_type))
2884 v_i = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (v) [0]);
2885 if (TypeManager.IsValueType (u_i))
2886 return ExactInference (u_i, v_i);
2888 return LowerBoundInference (u_i, v_i);
2890 } else if (v.IsGenericType && !v.IsGenericTypeDefinition) {
2892 // if V is a constructed type C<V1..Vk> and there is a unique type C<U1..Uk>
2893 // such that U is identical to, inherits from (directly or indirectly),
2894 // or implements (directly or indirectly) C<U1..Uk>
2896 ArrayList u_candidates = new ArrayList ();
2897 if (u.IsGenericType)
2898 u_candidates.Add (u);
2900 for (Type t = u.BaseType; t != null; t = t.BaseType) {
2901 if (t.IsGenericType && !t.IsGenericTypeDefinition)
2902 u_candidates.Add (t);
2905 // TODO: Implement GetGenericInterfaces only and remove
2906 // the if from foreach
2907 u_candidates.AddRange (TypeManager.GetInterfaces (u));
2909 Type open_v = v.GetGenericTypeDefinition ();
2910 Type [] unique_candidate_targs = null;
2911 Type [] ga_v = v.GetGenericArguments ();
2912 foreach (Type u_candidate in u_candidates) {
2913 if (!u_candidate.IsGenericType || u_candidate.IsGenericTypeDefinition)
2916 if (TypeManager.DropGenericTypeArguments (u_candidate) != open_v)
2920 // The unique set of types U1..Uk means that if we have an interface I<T>,
2921 // class U : I<int>, I<long> then no type inference is made when inferring
2922 // type I<T> by applying type U because T could be int or long
2924 if (unique_candidate_targs != null) {
2925 Type[] second_unique_candidate_targs = u_candidate.GetGenericArguments ();
2926 if (TypeManager.IsEqual (unique_candidate_targs, second_unique_candidate_targs)) {
2927 unique_candidate_targs = second_unique_candidate_targs;
2932 // This should always cause type inference failure
2938 unique_candidate_targs = u_candidate.GetGenericArguments ();
2941 if (unique_candidate_targs != null) {
2942 Type[] ga_open_v = open_v.GetGenericArguments ();
2944 for (int i = 0; i < unique_candidate_targs.Length; ++i) {
2945 Variance variance = TypeManager.GetTypeParameterVariance (ga_open_v [i]);
2947 Type u_i = unique_candidate_targs [i];
2948 if (variance == Variance.None || TypeManager.IsValueType (u_i)) {
2949 if (ExactInference (u_i, ga_v [i]) == 0)
2952 bool upper_bound = (variance == Variance.Contravariant && !inversed) ||
2953 (variance == Variance.Covariant && inversed);
2955 if (LowerBoundInference (u_i, ga_v [i], upper_bound) == 0)
2967 // 26.3.3.6 Output Type Inference
2969 public int OutputTypeInference (ResolveContext ec, Expression e, Type t)
2971 // If e is a lambda or anonymous method with inferred return type
2972 AnonymousMethodExpression ame = e as AnonymousMethodExpression;
2974 Type rt = ame.InferReturnType (ec, this, t);
2975 MethodInfo invoke = Delegate.GetInvokeMethod (ec.Compiler, t, t);
2978 AParametersCollection pd = TypeManager.GetParameterData (invoke);
2979 return ame.Parameters.Count == pd.Count ? 1 : 0;
2982 Type rtype = invoke.ReturnType;
2984 // Blablabla, because reflection does not work with dynamic types
2985 Type [] g_args = t.GetGenericArguments ();
2986 rtype = g_args [rtype.GenericParameterPosition];
2988 return LowerBoundInference (rt, rtype) + 1;
2992 // if E is a method group and T is a delegate type or expression tree type
2993 // return type Tb with parameter types T1..Tk and return type Tb, and overload
2994 // resolution of E with the types T1..Tk yields a single method with return type U,
2995 // then a lower-bound inference is made from U for Tb.
2997 if (e is MethodGroupExpr) {
2998 // TODO: Or expression tree
2999 if (!TypeManager.IsDelegateType (t))
3002 MethodInfo invoke = Delegate.GetInvokeMethod (ec.Compiler, t, t);
3003 Type rtype = invoke.ReturnType;
3005 // Blablabla, because reflection does not work with dynamic types
3006 Type [] g_args = t.GetGenericArguments ();
3007 rtype = g_args [rtype.GenericParameterPosition];
3010 if (!TypeManager.IsGenericType (rtype))
3013 MethodGroupExpr mg = (MethodGroupExpr) e;
3014 Arguments args = DelegateCreation.CreateDelegateMethodArguments (TypeManager.GetParameterData (invoke), e.Location);
3015 mg = mg.OverloadResolve (ec, ref args, true, e.Location);
3019 // TODO: What should happen when return type is of generic type ?
3020 throw new NotImplementedException ();
3021 // return LowerBoundInference (null, rtype) + 1;
3025 // if e is an expression with type U, then
3026 // a lower-bound inference is made from U for T
3028 return LowerBoundInference (e.Type, t) * 2;
3031 void RemoveDependentTypes (ArrayList types, Type returnType)
3033 int idx = IsUnfixed (returnType);
3039 if (returnType.IsGenericType) {
3040 foreach (Type t in returnType.GetGenericArguments ()) {
3041 RemoveDependentTypes (types, t);
3046 public bool UnfixedVariableExists {
3048 if (unfixed_types == null)
3051 foreach (Type ut in unfixed_types)