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 if ((constraints
.Length
> 0) && !constraints
[0].IsInterface
) {
150 class_constraint
= constraints
[0];
151 iface_constraints
= new Type
[constraints
.Length
- 1];
152 Array
.Copy (constraints
, 1, iface_constraints
, 0, constraints
.Length
- 1);
154 iface_constraints
= constraints
;
156 if (HasValueTypeConstraint
)
157 base_type
= TypeManager
.value_type
;
158 else if (class_constraint
!= null)
159 base_type
= class_constraint
;
161 base_type
= TypeManager
.object_type
;
164 public override string TypeParameter
169 public override GenericParameterAttributes Attributes
171 get { return attrs; }
174 public override Type ClassConstraint
176 get { return class_constraint; }
179 public override Type EffectiveBaseClass
181 get { return base_type; }
184 public override Type
[] InterfaceConstraints
186 get { return iface_constraints; }
193 // Don't add or modify internal values, they are used as -/+ calculation signs
200 public enum SpecialConstraint
208 /// Tracks the constraints for a type parameter from a generic type definition.
210 public class Constraints
: GenericConstraints
{
212 ArrayList constraints
;
216 // name is the identifier, constraints is an arraylist of
217 // Expressions (with types) or `true' for the constructor constraint.
219 public Constraints (string name
, ArrayList constraints
,
223 this.constraints
= constraints
;
227 public override string TypeParameter
{
233 public Constraints
Clone ()
235 return new Constraints (name
, constraints
, loc
);
238 GenericParameterAttributes attrs
;
239 TypeExpr class_constraint
;
240 ArrayList iface_constraints
;
241 ArrayList type_param_constraints
;
243 Type class_constraint_type
;
244 Type
[] iface_constraint_types
;
245 Type effective_base_type
;
250 /// Resolve the constraints - but only resolve things into Expression's, not
251 /// into actual types.
253 public bool Resolve (IResolveContext ec
, TypeParameter tp
)
258 iface_constraints
= new ArrayList (2); // TODO: Too expensive allocation
259 type_param_constraints
= new ArrayList ();
261 foreach (object obj
in constraints
) {
262 if (HasConstructorConstraint
) {
263 Report
.Error (401, loc
,
264 "The new() constraint must be the last constraint specified");
268 if (obj
is SpecialConstraint
) {
269 SpecialConstraint sc
= (SpecialConstraint
) obj
;
271 if (sc
== SpecialConstraint
.Constructor
) {
272 if (!HasValueTypeConstraint
) {
273 attrs
|= GenericParameterAttributes
.DefaultConstructorConstraint
;
277 Report
.Error (451, loc
, "The `new()' constraint " +
278 "cannot be used with the `struct' constraint");
282 if ((num_constraints
> 0) || HasReferenceTypeConstraint
|| HasValueTypeConstraint
) {
283 Report
.Error (449, loc
, "The `class' or `struct' " +
284 "constraint must be the first constraint specified");
288 if (sc
== SpecialConstraint
.ReferenceType
)
289 attrs
|= GenericParameterAttributes
.ReferenceTypeConstraint
;
291 attrs
|= GenericParameterAttributes
.NotNullableValueTypeConstraint
;
295 int errors
= Report
.Errors
;
296 FullNamedExpression fn
= ((Expression
) obj
).ResolveAsTypeStep (ec
, false);
299 if (errors
!= Report
.Errors
)
302 NamespaceEntry
.Error_NamespaceNotFound (loc
, ((Expression
)obj
).GetSignatureForError ());
307 GenericTypeExpr cexpr
= fn
as GenericTypeExpr
;
309 expr
= cexpr
.ResolveAsBaseTerminal (ec
, false);
311 expr
= ((Expression
) obj
).ResolveAsTypeTerminal (ec
, false);
313 if ((expr
== null) || (expr
.Type
== null))
316 if (!ec
.GenericDeclContainer
.IsAccessibleAs (fn
.Type
)) {
317 Report
.SymbolRelatedToPreviousError (fn
.Type
);
318 Report
.Error (703, loc
,
319 "Inconsistent accessibility: constraint type `{0}' is less accessible than `{1}'",
320 fn
.GetSignatureForError (), ec
.GenericDeclContainer
.GetSignatureForError ());
324 TypeParameterExpr texpr
= expr
as TypeParameterExpr
;
326 type_param_constraints
.Add (expr
);
327 else if (expr
.IsInterface
)
328 iface_constraints
.Add (expr
);
329 else if (class_constraint
!= null || iface_constraints
.Count
!= 0) {
330 Report
.Error (406, loc
,
331 "The class type constraint `{0}' must be listed before any other constraints. Consider moving type constraint to the beginning of the constraint list",
332 expr
.GetSignatureForError ());
334 } else if (HasReferenceTypeConstraint
|| HasValueTypeConstraint
) {
335 Report
.Error (450, loc
, "`{0}': cannot specify both " +
336 "a constraint class and the `class' " +
337 "or `struct' constraint", expr
.GetSignatureForError ());
340 class_constraint
= expr
;
344 // Checks whether each generic method parameter constraint type
345 // is valid with respect to T
347 if (tp
!= null && tp
.Type
.DeclaringMethod
!= null) {
348 TypeManager
.CheckTypeVariance (expr
.Type
, Variance
.Contravariant
, ec
as MemberCore
);
354 ArrayList list
= new ArrayList ();
355 foreach (TypeExpr iface_constraint
in iface_constraints
) {
356 foreach (Type type
in list
) {
357 if (!type
.Equals (iface_constraint
.Type
))
360 Report
.Error (405, loc
,
361 "Duplicate constraint `{0}' for type " +
362 "parameter `{1}'.", iface_constraint
.GetSignatureForError (),
367 list
.Add (iface_constraint
.Type
);
370 foreach (TypeParameterExpr expr
in type_param_constraints
) {
371 foreach (Type type
in list
) {
372 if (!type
.Equals (expr
.Type
))
375 Report
.Error (405, loc
,
376 "Duplicate constraint `{0}' for type " +
377 "parameter `{1}'.", expr
.GetSignatureForError (), name
);
381 list
.Add (expr
.Type
);
384 iface_constraint_types
= new Type
[list
.Count
];
385 list
.CopyTo (iface_constraint_types
, 0);
387 if (class_constraint
!= null) {
388 class_constraint_type
= class_constraint
.Type
;
389 if (class_constraint_type
== null)
392 if (class_constraint_type
.IsSealed
) {
393 if (class_constraint_type
.IsAbstract
)
395 Report
.Error (717, loc
, "`{0}' is not a valid constraint. Static classes cannot be used as constraints",
396 TypeManager
.CSharpName (class_constraint_type
));
400 Report
.Error (701, loc
, "`{0}' is not a valid constraint. A constraint must be an interface, " +
401 "a non-sealed class or a type parameter", TypeManager
.CSharpName(class_constraint_type
));
406 if ((class_constraint_type
== TypeManager
.array_type
) ||
407 (class_constraint_type
== TypeManager
.delegate_type
) ||
408 (class_constraint_type
== TypeManager
.enum_type
) ||
409 (class_constraint_type
== TypeManager
.value_type
) ||
410 (class_constraint_type
== TypeManager
.object_type
) ||
411 class_constraint_type
== TypeManager
.multicast_delegate_type
) {
412 Report
.Error (702, loc
,
413 "A constraint cannot be special class `{0}'",
414 TypeManager
.CSharpName (class_constraint_type
));
419 if (class_constraint_type
!= null)
420 effective_base_type
= class_constraint_type
;
421 else if (HasValueTypeConstraint
)
422 effective_base_type
= TypeManager
.value_type
;
424 effective_base_type
= TypeManager
.object_type
;
426 if ((attrs
& GenericParameterAttributes
.NotNullableValueTypeConstraint
) != 0)
427 attrs
|= GenericParameterAttributes
.DefaultConstructorConstraint
;
433 bool CheckTypeParameterConstraints (TypeParameter tparam
, ref TypeExpr prevConstraint
, ArrayList seen
)
437 Constraints constraints
= tparam
.Constraints
;
438 if (constraints
== null)
441 if (constraints
.HasValueTypeConstraint
) {
442 Report
.Error (456, loc
,
443 "Type parameter `{0}' has the `struct' constraint, so it cannot be used as a constraint for `{1}'",
449 // Checks whether there are no conflicts between type parameter constraints
453 // where U : A, B // A and B are not convertible
455 if (constraints
.HasClassConstraint
) {
456 if (prevConstraint
!= null) {
457 Type t2
= constraints
.ClassConstraint
;
458 TypeExpr e2
= constraints
.class_constraint
;
460 if (!Convert
.ImplicitReferenceConversionExists (prevConstraint
, t2
) &&
461 !Convert
.ImplicitReferenceConversionExists (e2
, prevConstraint
.Type
)) {
462 Report
.Error (455, loc
,
463 "Type parameter `{0}' inherits conflicting constraints `{1}' and `{2}'",
464 name
, TypeManager
.CSharpName (prevConstraint
.Type
), TypeManager
.CSharpName (t2
));
469 prevConstraint
= constraints
.class_constraint
;
472 if (constraints
.type_param_constraints
== null)
475 foreach (TypeParameterExpr expr
in constraints
.type_param_constraints
) {
476 if (seen
.Contains (expr
.TypeParameter
)) {
477 Report
.Error (454, loc
, "Circular constraint " +
478 "dependency involving `{0}' and `{1}'",
479 tparam
.Name
, expr
.GetSignatureForError ());
483 if (!CheckTypeParameterConstraints (expr
.TypeParameter
, ref prevConstraint
, seen
))
491 /// Resolve the constraints into actual types.
493 public bool ResolveTypes (IResolveContext ec
)
498 resolved_types
= true;
500 foreach (object obj
in constraints
) {
501 GenericTypeExpr cexpr
= obj
as GenericTypeExpr
;
505 if (!cexpr
.CheckConstraints (ec
))
509 if (type_param_constraints
.Count
!= 0) {
510 ArrayList seen
= new ArrayList ();
511 TypeExpr prev_constraint
= class_constraint
;
512 foreach (TypeParameterExpr expr
in type_param_constraints
) {
513 if (!CheckTypeParameterConstraints (expr
.TypeParameter
, ref prev_constraint
, seen
))
519 for (int i
= 0; i
< iface_constraints
.Count
; ++i
) {
520 TypeExpr iface_constraint
= (TypeExpr
) iface_constraints
[i
];
521 iface_constraint
= iface_constraint
.ResolveAsTypeTerminal (ec
, false);
522 if (iface_constraint
== null)
524 iface_constraints
[i
] = iface_constraint
;
527 if (class_constraint
!= null) {
528 class_constraint
= class_constraint
.ResolveAsTypeTerminal (ec
, false);
529 if (class_constraint
== null)
536 public override GenericParameterAttributes Attributes
{
537 get { return attrs; }
540 public override bool HasClassConstraint
{
541 get { return class_constraint != null; }
544 public override Type ClassConstraint
{
545 get { return class_constraint_type; }
548 public override Type
[] InterfaceConstraints
{
549 get { return iface_constraint_types; }
552 public override Type EffectiveBaseClass
{
553 get { return effective_base_type; }
556 public bool IsSubclassOf (Type t
)
558 if ((class_constraint_type
!= null) &&
559 class_constraint_type
.IsSubclassOf (t
))
562 if (iface_constraint_types
== null)
565 foreach (Type iface
in iface_constraint_types
) {
566 if (TypeManager
.IsSubclassOf (iface
, t
))
573 public Location Location
{
580 /// This is used when we're implementing a generic interface method.
581 /// Each method type parameter in implementing method must have the same
582 /// constraints than the corresponding type parameter in the interface
583 /// method. To do that, we're called on each of the implementing method's
586 public bool AreEqual (GenericConstraints gc
)
588 if (gc
.Attributes
!= attrs
)
591 if (HasClassConstraint
!= gc
.HasClassConstraint
)
593 if (HasClassConstraint
&& !TypeManager
.IsEqual (gc
.ClassConstraint
, ClassConstraint
))
596 int gc_icount
= gc
.InterfaceConstraints
!= null ?
597 gc
.InterfaceConstraints
.Length
: 0;
598 int icount
= InterfaceConstraints
!= null ?
599 InterfaceConstraints
.Length
: 0;
601 if (gc_icount
!= icount
)
604 for (int i
= 0; i
< gc
.InterfaceConstraints
.Length
; ++i
) {
605 Type iface
= gc
.InterfaceConstraints
[i
];
606 if (iface
.IsGenericType
)
607 iface
= iface
.GetGenericTypeDefinition ();
610 for (int ii
= 0; i
< InterfaceConstraints
.Length
; ++ii
) {
611 Type check
= InterfaceConstraints
[ii
];
612 if (check
.IsGenericType
)
613 check
= check
.GetGenericTypeDefinition ();
615 if (TypeManager
.IsEqual (iface
, check
)) {
628 public void VerifyClsCompliance ()
630 if (class_constraint_type
!= null && !AttributeTester
.IsClsCompliant (class_constraint_type
))
631 Warning_ConstrainIsNotClsCompliant (class_constraint_type
, class_constraint
.Location
);
633 if (iface_constraint_types
!= null) {
634 for (int i
= 0; i
< iface_constraint_types
.Length
; ++i
) {
635 if (!AttributeTester
.IsClsCompliant (iface_constraint_types
[i
]))
636 Warning_ConstrainIsNotClsCompliant (iface_constraint_types
[i
],
637 ((TypeExpr
)iface_constraints
[i
]).Location
);
642 void Warning_ConstrainIsNotClsCompliant (Type t
, Location loc
)
644 Report
.SymbolRelatedToPreviousError (t
);
645 Report
.Warning (3024, 1, loc
, "Constraint type `{0}' is not CLS-compliant",
646 TypeManager
.CSharpName (t
));
651 /// A type parameter from a generic type definition.
653 public class TypeParameter
: MemberCore
, IMemberContainer
655 static readonly string[] attribute_target
= new string [] { "type parameter" }
;
658 GenericConstraints gc
;
659 Constraints constraints
;
660 GenericTypeParameterBuilder type
;
661 MemberCache member_cache
;
664 public TypeParameter (DeclSpace parent
, DeclSpace decl
, string name
,
665 Constraints constraints
, Attributes attrs
, Variance variance
, Location loc
)
666 : base (parent
, new MemberName (name
, loc
), attrs
)
669 this.constraints
= constraints
;
670 this.variance
= variance
;
673 public GenericConstraints GenericConstraints
{
674 get { return gc != null ? gc : constraints; }
677 public Constraints Constraints
{
678 get { return constraints; }
681 public DeclSpace DeclSpace
{
685 public Variance Variance
{
686 get { return variance; }
694 /// This is the first method which is called during the resolving
695 /// process; we're called immediately after creating the type parameters
696 /// with SRE (by calling `DefineGenericParameters()' on the TypeBuilder /
699 /// We're either called from TypeContainer.DefineType() or from
700 /// GenericMethod.Define() (called from Method.Define()).
702 public void Define (GenericTypeParameterBuilder type
)
704 if (this.type
!= null)
705 throw new InvalidOperationException ();
708 TypeManager
.AddTypeParameter (type
, this);
711 public void ErrorInvalidVariance (MemberCore mc
, Variance expected
)
713 Report
.SymbolRelatedToPreviousError (mc
);
714 string input_variance
= Variance
== Variance
.Contravariant
? "contravariant" : "covariant";
715 string gtype_variance
;
717 case Variance
.Contravariant
: gtype_variance
= "contravariantly"; break;
718 case Variance
.Covariant
: gtype_variance
= "covariantly"; break;
719 default: gtype_variance
= "invariantly"; break;
722 Delegate d
= mc
as Delegate
;
723 string parameters
= d
!= null ? d
.Parameters
.GetSignatureForError () : "";
725 Report
.Error (1961, Location
,
726 "The {2} type parameter `{0}' must be {3} valid on `{1}{4}'",
727 GetSignatureForError (), mc
.GetSignatureForError (), input_variance
, gtype_variance
, parameters
);
731 /// This is the second method which is called during the resolving
732 /// process - in case of class type parameters, we're called from
733 /// TypeContainer.ResolveType() - after it resolved the class'es
734 /// base class and interfaces. For method type parameters, we're
735 /// called immediately after Define().
737 /// We're just resolving the constraints into expressions here, we
738 /// don't resolve them into actual types.
740 /// Note that in the special case of partial generic classes, we may be
741 /// called _before_ Define() and we may also be called multiple types.
743 public bool Resolve (DeclSpace ds
)
745 if (constraints
!= null) {
746 if (!constraints
.Resolve (ds
, this)) {
756 /// This is the third method which is called during the resolving
757 /// process. We're called immediately after calling DefineConstraints()
758 /// on all of the current class'es type parameters.
760 /// Our job is to resolve the constraints to actual types.
762 /// Note that we may have circular dependencies on type parameters - this
763 /// is why Resolve() and ResolveType() are separate.
765 public bool ResolveType (IResolveContext ec
)
767 if (constraints
!= null) {
768 if (!constraints
.ResolveTypes (ec
)) {
778 /// This is the fourth and last method which is called during the resolving
779 /// process. We're called after everything is fully resolved and actually
780 /// register the constraints with SRE and the TypeManager.
782 public bool DefineType (IResolveContext ec
)
784 return DefineType (ec
, null, null, false);
788 /// This is the fith and last method which is called during the resolving
789 /// process. We're called after everything is fully resolved and actually
790 /// register the constraints with SRE and the TypeManager.
792 /// The `builder', `implementing' and `is_override' arguments are only
793 /// applicable to method type parameters.
795 public bool DefineType (IResolveContext ec
, MethodBuilder builder
,
796 MethodInfo implementing
, bool is_override
)
798 if (!ResolveType (ec
))
801 if (implementing
!= null) {
802 if (is_override
&& (constraints
!= null)) {
803 Report
.Error (460, Location
,
804 "`{0}': Cannot specify constraints for overrides or explicit interface implementation methods",
805 TypeManager
.CSharpSignature (builder
));
809 MethodBase mb
= TypeManager
.DropGenericMethodArguments (implementing
);
811 int pos
= type
.GenericParameterPosition
;
812 Type mparam
= mb
.GetGenericArguments () [pos
];
813 GenericConstraints temp_gc
= ReflectionConstraints
.GetConstraints (mparam
);
816 gc
= new InflatedConstraints (temp_gc
, implementing
.DeclaringType
);
817 else if (constraints
!= null)
818 gc
= new InflatedConstraints (constraints
, implementing
.DeclaringType
);
821 if (constraints
!= null) {
824 else if (!constraints
.AreEqual (gc
))
827 if (!is_override
&& (temp_gc
!= null))
832 Report
.SymbolRelatedToPreviousError (implementing
);
835 425, Location
, "The constraints for type " +
836 "parameter `{0}' of method `{1}' must match " +
837 "the constraints for type parameter `{2}' " +
838 "of interface method `{3}'. Consider using " +
839 "an explicit interface implementation instead",
840 Name
, TypeManager
.CSharpSignature (builder
),
841 TypeManager
.CSharpName (mparam
), TypeManager
.CSharpSignature (mb
));
844 } else if (DeclSpace
is CompilerGeneratedClass
) {
845 TypeParameter
[] tparams
= DeclSpace
.TypeParameters
;
846 Type
[] types
= new Type
[tparams
.Length
];
847 for (int i
= 0; i
< tparams
.Length
; i
++)
848 types
[i
] = tparams
[i
].Type
;
850 if (constraints
!= null)
851 gc
= new InflatedConstraints (constraints
, types
);
853 gc
= (GenericConstraints
) constraints
;
856 SetConstraints (type
);
860 public void SetConstraints (GenericTypeParameterBuilder type
)
862 GenericParameterAttributes attr
= GenericParameterAttributes
.None
;
863 if (variance
== Variance
.Contravariant
)
864 attr
|= GenericParameterAttributes
.Contravariant
;
865 else if (variance
== Variance
.Covariant
)
866 attr
|= GenericParameterAttributes
.Covariant
;
869 if (gc
.HasClassConstraint
|| gc
.HasValueTypeConstraint
)
870 type
.SetBaseTypeConstraint (gc
.EffectiveBaseClass
);
872 attr
|= gc
.Attributes
;
873 type
.SetInterfaceConstraints (gc
.InterfaceConstraints
);
874 TypeManager
.RegisterBuilder (type
, gc
.InterfaceConstraints
);
877 type
.SetGenericParameterAttributes (attr
);
881 /// This is called for each part of a partial generic type definition.
883 /// If `new_constraints' is not null and we don't already have constraints,
884 /// they become our constraints. If we already have constraints, we must
885 /// check that they're the same.
888 public bool UpdateConstraints (IResolveContext ec
, Constraints new_constraints
)
891 throw new InvalidOperationException ();
893 if (new_constraints
== null)
896 if (!new_constraints
.Resolve (ec
, this))
898 if (!new_constraints
.ResolveTypes (ec
))
901 if (constraints
!= null)
902 return constraints
.AreEqual (new_constraints
);
904 constraints
= new_constraints
;
908 public override void Emit ()
910 if (OptAttributes
!= null)
911 OptAttributes
.Emit ();
916 public override string DocCommentHeader
{
918 throw new InvalidOperationException (
919 "Unexpected attempt to get doc comment from " + this.GetType () + ".");
927 public override bool Define ()
932 public override void ApplyAttributeBuilder (Attribute a
, CustomAttributeBuilder cb
, PredefinedAttributes pa
)
934 type
.SetCustomAttribute (cb
);
937 public override AttributeTargets AttributeTargets
{
939 return AttributeTargets
.GenericParameter
;
943 public override string[] ValidAttributeTargets
{
945 return attribute_target
;
953 string IMemberContainer
.Name
{
957 MemberCache IMemberContainer
.BaseCache
{
962 if (gc
.EffectiveBaseClass
.BaseType
== null)
965 return TypeManager
.LookupMemberCache (gc
.EffectiveBaseClass
.BaseType
);
969 bool IMemberContainer
.IsInterface
{
970 get { return false; }
973 MemberList IMemberContainer
.GetMembers (MemberTypes mt
, BindingFlags bf
)
975 throw new NotSupportedException ();
978 public MemberCache MemberCache
{
980 if (member_cache
!= null)
986 Type
[] ifaces
= TypeManager
.ExpandInterfaces (gc
.InterfaceConstraints
);
987 member_cache
= new MemberCache (this, gc
.EffectiveBaseClass
, ifaces
);
993 public MemberList
FindMembers (MemberTypes mt
, BindingFlags bf
,
994 MemberFilter filter
, object criteria
)
997 return MemberList
.Empty
;
999 ArrayList members
= new ArrayList ();
1001 if (gc
.HasClassConstraint
) {
1002 MemberList list
= TypeManager
.FindMembers (
1003 gc
.ClassConstraint
, mt
, bf
, filter
, criteria
);
1005 members
.AddRange (list
);
1008 Type
[] ifaces
= TypeManager
.ExpandInterfaces (gc
.InterfaceConstraints
);
1009 foreach (Type t
in ifaces
) {
1010 MemberList list
= TypeManager
.FindMembers (
1011 t
, mt
, bf
, filter
, criteria
);
1013 members
.AddRange (list
);
1016 return new MemberList (members
);
1019 public bool IsSubclassOf (Type t
)
1021 if (type
.Equals (t
))
1024 if (constraints
!= null)
1025 return constraints
.IsSubclassOf (t
);
1030 public void InflateConstraints (Type declaring
)
1032 if (constraints
!= null)
1033 gc
= new InflatedConstraints (constraints
, declaring
);
1036 public override bool IsClsComplianceRequired ()
1041 protected class InflatedConstraints
: GenericConstraints
1043 GenericConstraints gc
;
1045 Type class_constraint
;
1046 Type
[] iface_constraints
;
1049 public InflatedConstraints (GenericConstraints gc
, Type declaring
)
1050 : this (gc
, TypeManager
.GetTypeArguments (declaring
))
1053 public InflatedConstraints (GenericConstraints gc
, Type
[] dargs
)
1058 ArrayList list
= new ArrayList ();
1059 if (gc
.HasClassConstraint
)
1060 list
.Add (inflate (gc
.ClassConstraint
));
1061 foreach (Type iface
in gc
.InterfaceConstraints
)
1062 list
.Add (inflate (iface
));
1064 bool has_class_constr
= false;
1065 if (list
.Count
> 0) {
1066 Type first
= (Type
) list
[0];
1067 has_class_constr
= !first
.IsGenericParameter
&& !first
.IsInterface
;
1070 if ((list
.Count
> 0) && has_class_constr
) {
1071 class_constraint
= (Type
) list
[0];
1072 iface_constraints
= new Type
[list
.Count
- 1];
1073 list
.CopyTo (1, iface_constraints
, 0, list
.Count
- 1);
1075 iface_constraints
= new Type
[list
.Count
];
1076 list
.CopyTo (iface_constraints
, 0);
1079 if (HasValueTypeConstraint
)
1080 base_type
= TypeManager
.value_type
;
1081 else if (class_constraint
!= null)
1082 base_type
= class_constraint
;
1084 base_type
= TypeManager
.object_type
;
1087 Type
inflate (Type t
)
1091 if (t
.IsGenericParameter
)
1092 return t
.GenericParameterPosition
< dargs
.Length
? dargs
[t
.GenericParameterPosition
] : t
;
1093 if (t
.IsGenericType
) {
1094 Type
[] args
= t
.GetGenericArguments ();
1095 Type
[] inflated
= new Type
[args
.Length
];
1097 for (int i
= 0; i
< args
.Length
; i
++)
1098 inflated
[i
] = inflate (args
[i
]);
1100 t
= t
.GetGenericTypeDefinition ();
1101 t
= t
.MakeGenericType (inflated
);
1107 public override string TypeParameter
{
1108 get { return gc.TypeParameter; }
1111 public override GenericParameterAttributes Attributes
{
1112 get { return gc.Attributes; }
1115 public override Type ClassConstraint
{
1116 get { return class_constraint; }
1119 public override Type EffectiveBaseClass
{
1120 get { return base_type; }
1123 public override Type
[] InterfaceConstraints
{
1124 get { return iface_constraints; }
1130 /// A TypeExpr which already resolved to a type parameter.
1132 public class TypeParameterExpr
: TypeExpr
{
1133 TypeParameter type_parameter
;
1135 public TypeParameter TypeParameter
{
1137 return type_parameter
;
1141 public TypeParameterExpr (TypeParameter type_parameter
, Location loc
)
1143 this.type_parameter
= type_parameter
;
1147 protected override TypeExpr
DoResolveAsTypeStep (IResolveContext ec
)
1149 throw new NotSupportedException ();
1152 public override FullNamedExpression
ResolveAsTypeStep (IResolveContext ec
, bool silent
)
1154 type
= type_parameter
.Type
;
1155 eclass
= ExprClass
.TypeParameter
;
1159 public override bool IsInterface
{
1160 get { return false; }
1163 public override bool CheckAccessLevel (DeclSpace ds
)
1170 // Tracks the type arguments when instantiating a generic type. It's used
1171 // by both type arguments and type parameters
1173 public class TypeArguments
{
1177 public TypeArguments ()
1179 args
= new ArrayList ();
1182 public TypeArguments (params FullNamedExpression
[] types
)
1184 this.args
= new ArrayList (types
);
1187 public void Add (FullNamedExpression type
)
1192 public void Add (TypeArguments new_args
)
1194 args
.AddRange (new_args
.args
);
1197 // TODO: Should be deleted
1198 public TypeParameterName
[] GetDeclarations ()
1200 return (TypeParameterName
[]) args
.ToArray (typeof (TypeParameterName
));
1204 /// We may only be used after Resolve() is called and return the fully
1207 public Type
[] Arguments
{
1219 public string GetSignatureForError()
1221 StringBuilder sb
= new StringBuilder();
1222 for (int i
= 0; i
< Count
; ++i
)
1224 Expression expr
= (Expression
)args
[i
];
1225 sb
.Append(expr
.GetSignatureForError());
1229 return sb
.ToString();
1233 /// Resolve the type arguments.
1235 public bool Resolve (IResolveContext ec
)
1238 return atypes
.Length
!= 0;
1240 int count
= args
.Count
;
1243 atypes
= new Type
[count
];
1245 for (int i
= 0; i
< count
; i
++){
1246 TypeExpr te
= ((FullNamedExpression
) args
[i
]).ResolveAsTypeTerminal (ec
, false);
1252 atypes
[i
] = te
.Type
;
1254 if (te
.Type
.IsSealed
&& te
.Type
.IsAbstract
) {
1255 Report
.Error (718, te
.Location
, "`{0}': static classes cannot be used as generic arguments",
1256 te
.GetSignatureForError ());
1260 if (te
.Type
.IsPointer
|| TypeManager
.IsSpecialType (te
.Type
)) {
1261 Report
.Error (306, te
.Location
,
1262 "The type `{0}' may not be used as a type argument",
1263 te
.GetSignatureForError ());
1269 atypes
= Type
.EmptyTypes
;
1274 public TypeArguments
Clone ()
1276 TypeArguments copy
= new TypeArguments ();
1277 foreach (Expression ta
in args
)
1284 public class TypeParameterName
: SimpleName
1286 Attributes attributes
;
1289 public TypeParameterName (string name
, Attributes attrs
, Location loc
)
1290 : this (name
, attrs
, Variance
.None
, loc
)
1294 public TypeParameterName (string name
, Attributes attrs
, Variance variance
, Location loc
)
1298 this.variance
= variance
;
1301 public Attributes OptAttributes
{
1307 public Variance Variance
{
1315 /// A reference expression to generic type
1317 class GenericTypeExpr
: TypeExpr
1320 Type
[] gen_params
; // TODO: Waiting for constrains check cleanup
1324 // Should be carefully used only with defined generic containers. Type parameters
1325 // can be used as type arguments in this case.
1327 // TODO: This could be GenericTypeExpr specialization
1329 public GenericTypeExpr (DeclSpace gType
, Location l
)
1331 open_type
= gType
.TypeBuilder
.GetGenericTypeDefinition ();
1333 args
= new TypeArguments ();
1334 foreach (TypeParameter type_param
in gType
.TypeParameters
)
1335 args
.Add (new TypeParameterExpr (type_param
, l
));
1341 /// Instantiate the generic type `t' with the type arguments `args'.
1342 /// Use this constructor if you already know the fully resolved
1345 public GenericTypeExpr (Type t
, TypeArguments args
, Location l
)
1347 open_type
= t
.GetGenericTypeDefinition ();
1353 public TypeArguments TypeArguments
{
1354 get { return args; }
1357 public override string GetSignatureForError ()
1359 return TypeManager
.CSharpName (type
);
1362 protected override TypeExpr
DoResolveAsTypeStep (IResolveContext ec
)
1364 if (eclass
!= ExprClass
.Invalid
)
1367 eclass
= ExprClass
.Type
;
1369 if (!args
.Resolve (ec
))
1372 gen_params
= open_type
.GetGenericArguments ();
1373 Type
[] atypes
= args
.Arguments
;
1375 if (atypes
.Length
!= gen_params
.Length
) {
1376 Namespace
.Error_InvalidNumberOfTypeArguments (open_type
, loc
);
1381 // Now bind the parameters
1383 type
= open_type
.MakeGenericType (atypes
);
1388 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1389 /// after fully resolving the constructed type.
1391 public bool CheckConstraints (IResolveContext ec
)
1393 return ConstraintChecker
.CheckConstraints (ec
, open_type
, gen_params
, args
.Arguments
, loc
);
1396 public override bool CheckAccessLevel (DeclSpace ds
)
1398 return ds
.CheckAccessLevel (open_type
);
1401 public override bool IsClass
{
1402 get { return open_type.IsClass; }
1405 public override bool IsValueType
{
1406 get { return TypeManager.IsStruct (open_type); }
1409 public override bool IsInterface
{
1410 get { return open_type.IsInterface; }
1413 public override bool IsSealed
{
1414 get { return open_type.IsSealed; }
1417 public override bool Equals (object obj
)
1419 GenericTypeExpr cobj
= obj
as GenericTypeExpr
;
1423 if ((type
== null) || (cobj
.type
== null))
1426 return type
== cobj
.type
;
1429 public override int GetHashCode ()
1431 return base.GetHashCode ();
1435 public abstract class ConstraintChecker
1437 protected readonly Type
[] gen_params
;
1438 protected readonly Type
[] atypes
;
1439 protected readonly Location loc
;
1441 protected ConstraintChecker (Type
[] gen_params
, Type
[] atypes
, Location loc
)
1443 this.gen_params
= gen_params
;
1444 this.atypes
= atypes
;
1449 /// Check the constraints; we're called from ResolveAsTypeTerminal()
1450 /// after fully resolving the constructed type.
1452 public bool CheckConstraints (IResolveContext ec
)
1454 for (int i
= 0; i
< gen_params
.Length
; i
++) {
1455 if (!CheckConstraints (ec
, i
))
1462 protected bool CheckConstraints (IResolveContext ec
, int index
)
1464 Type atype
= atypes
[index
];
1465 Type ptype
= gen_params
[index
];
1470 Expression aexpr
= new EmptyExpression (atype
);
1472 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (ptype
);
1476 bool is_class
, is_struct
;
1477 if (atype
.IsGenericParameter
) {
1478 GenericConstraints agc
= TypeManager
.GetTypeParameterConstraints (atype
);
1480 if (agc
is Constraints
)
1481 ((Constraints
) agc
).Resolve (ec
, null);
1482 is_class
= agc
.IsReferenceType
;
1483 is_struct
= agc
.IsValueType
;
1485 is_class
= is_struct
= false;
1490 if (!atype
.IsGenericType
)
1492 is_class
= atype
.IsClass
|| atype
.IsInterface
;
1493 is_struct
= TypeManager
.IsValueType (atype
) && !TypeManager
.IsNullableType (atype
);
1497 // First, check the `class' and `struct' constraints.
1499 if (gc
.HasReferenceTypeConstraint
&& !is_class
) {
1500 Report
.Error (452, loc
, "The type `{0}' must be " +
1501 "a reference type in order to use it " +
1502 "as type parameter `{1}' in the " +
1503 "generic type or method `{2}'.",
1504 TypeManager
.CSharpName (atype
),
1505 TypeManager
.CSharpName (ptype
),
1506 GetSignatureForError ());
1508 } else if (gc
.HasValueTypeConstraint
&& !is_struct
) {
1509 Report
.Error (453, loc
, "The type `{0}' must be a " +
1510 "non-nullable value type in order to use it " +
1511 "as type parameter `{1}' in the " +
1512 "generic type or method `{2}'.",
1513 TypeManager
.CSharpName (atype
),
1514 TypeManager
.CSharpName (ptype
),
1515 GetSignatureForError ());
1520 // The class constraint comes next.
1522 if (gc
.HasClassConstraint
) {
1523 if (!CheckConstraint (ec
, ptype
, aexpr
, gc
.ClassConstraint
))
1528 // Now, check the interface constraints.
1530 if (gc
.InterfaceConstraints
!= null) {
1531 foreach (Type it
in gc
.InterfaceConstraints
) {
1532 if (!CheckConstraint (ec
, ptype
, aexpr
, it
))
1538 // Finally, check the constructor constraint.
1541 if (!gc
.HasConstructorConstraint
)
1544 if (TypeManager
.IsBuiltinType (atype
) || TypeManager
.IsValueType (atype
))
1547 if (HasDefaultConstructor (atype
))
1550 Report_SymbolRelatedToPreviousError ();
1551 Report
.SymbolRelatedToPreviousError (atype
);
1552 Report
.Error (310, loc
, "The type `{0}' must have a public " +
1553 "parameterless constructor in order to use it " +
1554 "as parameter `{1}' in the generic type or " +
1556 TypeManager
.CSharpName (atype
),
1557 TypeManager
.CSharpName (ptype
),
1558 GetSignatureForError ());
1562 protected bool CheckConstraint (IResolveContext ec
, Type ptype
, Expression expr
,
1565 if (TypeManager
.HasGenericArguments (ctype
)) {
1566 Type
[] types
= TypeManager
.GetTypeArguments (ctype
);
1568 TypeArguments new_args
= new TypeArguments ();
1570 for (int i
= 0; i
< types
.Length
; i
++) {
1573 if (t
.IsGenericParameter
) {
1574 int pos
= t
.GenericParameterPosition
;
1577 new_args
.Add (new TypeExpression (t
, loc
));
1580 TypeExpr ct
= new GenericTypeExpr (ctype
, new_args
, loc
);
1581 if (ct
.ResolveAsTypeStep (ec
, false) == null)
1584 } else if (ctype
.IsGenericParameter
) {
1585 int pos
= ctype
.GenericParameterPosition
;
1586 if (ctype
.DeclaringMethod
== null) {
1590 ctype
= atypes
[pos
];
1594 if (Convert
.ImplicitStandardConversionExists (expr
, ctype
))
1597 Report_SymbolRelatedToPreviousError ();
1598 Report
.SymbolRelatedToPreviousError (expr
.Type
);
1600 if (TypeManager
.IsNullableType (expr
.Type
) && ctype
.IsInterface
) {
1601 Report
.Error (313, loc
,
1602 "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. " +
1603 "The nullable type `{0}' never satisfies interface constraint of type `{3}'",
1604 TypeManager
.CSharpName (expr
.Type
), TypeManager
.CSharpName (ptype
),
1605 GetSignatureForError (), TypeManager
.CSharpName (ctype
));
1607 Report
.Error (309, loc
,
1608 "The type `{0}' must be convertible to `{1}' in order to " +
1609 "use it as parameter `{2}' in the generic type or method `{3}'",
1610 TypeManager
.CSharpName (expr
.Type
), TypeManager
.CSharpName (ctype
),
1611 TypeManager
.CSharpName (ptype
), GetSignatureForError ());
1616 static bool HasDefaultConstructor (Type atype
)
1618 TypeParameter tparam
= TypeManager
.LookupTypeParameter (atype
);
1619 if (tparam
!= null) {
1620 if (tparam
.GenericConstraints
== null)
1623 return tparam
.GenericConstraints
.HasConstructorConstraint
||
1624 tparam
.GenericConstraints
.HasValueTypeConstraint
;
1627 if (atype
.IsAbstract
)
1631 atype
= TypeManager
.DropGenericTypeArguments (atype
);
1632 if (atype
is TypeBuilder
) {
1633 TypeContainer tc
= TypeManager
.LookupTypeContainer (atype
);
1634 if (tc
.InstanceConstructors
== null) {
1635 atype
= atype
.BaseType
;
1639 foreach (Constructor c
in tc
.InstanceConstructors
) {
1640 if ((c
.ModFlags
& Modifiers
.PUBLIC
) == 0)
1642 if ((c
.Parameters
.FixedParameters
!= null) &&
1643 (c
.Parameters
.FixedParameters
.Length
!= 0))
1645 if (c
.Parameters
.HasArglist
|| c
.Parameters
.HasParams
)
1652 MemberInfo
[] list
= TypeManager
.MemberLookup (null, null, atype
, MemberTypes
.Constructor
,
1653 BindingFlags
.Public
| BindingFlags
.Instance
| BindingFlags
.DeclaredOnly
,
1654 ConstructorInfo
.ConstructorName
, null);
1659 foreach (MethodBase mb
in list
) {
1660 AParametersCollection pd
= TypeManager
.GetParameterData (mb
);
1668 protected abstract string GetSignatureForError ();
1669 protected abstract void Report_SymbolRelatedToPreviousError ();
1671 public static bool CheckConstraints (EmitContext ec
, MethodBase definition
,
1672 MethodBase instantiated
, Location loc
)
1674 MethodConstraintChecker checker
= new MethodConstraintChecker (
1675 definition
, definition
.GetGenericArguments (),
1676 instantiated
.GetGenericArguments (), loc
);
1678 return checker
.CheckConstraints (ec
);
1681 public static bool CheckConstraints (IResolveContext ec
, Type gt
, Type
[] gen_params
,
1682 Type
[] atypes
, Location loc
)
1684 TypeConstraintChecker checker
= new TypeConstraintChecker (
1685 gt
, gen_params
, atypes
, loc
);
1687 return checker
.CheckConstraints (ec
);
1690 protected class MethodConstraintChecker
: ConstraintChecker
1692 MethodBase definition
;
1694 public MethodConstraintChecker (MethodBase definition
, Type
[] gen_params
,
1695 Type
[] atypes
, Location loc
)
1696 : base (gen_params
, atypes
, loc
)
1698 this.definition
= definition
;
1701 protected override string GetSignatureForError ()
1703 return TypeManager
.CSharpSignature (definition
);
1706 protected override void Report_SymbolRelatedToPreviousError ()
1708 Report
.SymbolRelatedToPreviousError (definition
);
1712 protected class TypeConstraintChecker
: ConstraintChecker
1716 public TypeConstraintChecker (Type gt
, Type
[] gen_params
, Type
[] atypes
,
1718 : base (gen_params
, atypes
, loc
)
1723 protected override string GetSignatureForError ()
1725 return TypeManager
.CSharpName (gt
);
1728 protected override void Report_SymbolRelatedToPreviousError ()
1730 Report
.SymbolRelatedToPreviousError (gt
);
1736 /// A generic method definition.
1738 public class GenericMethod
: DeclSpace
1740 FullNamedExpression return_type
;
1741 ParametersCompiled parameters
;
1743 public GenericMethod (NamespaceEntry ns
, DeclSpace parent
, MemberName name
,
1744 FullNamedExpression return_type
, ParametersCompiled parameters
)
1745 : base (ns
, parent
, name
, null)
1747 this.return_type
= return_type
;
1748 this.parameters
= parameters
;
1751 public override TypeBuilder
DefineType ()
1753 throw new Exception ();
1756 public override bool Define ()
1758 for (int i
= 0; i
< TypeParameters
.Length
; i
++)
1759 if (!TypeParameters
[i
].Resolve (this))
1766 /// Define and resolve the type parameters.
1767 /// We're called from Method.Define().
1769 public bool Define (MethodOrOperator m
)
1771 TypeParameterName
[] names
= MemberName
.TypeArguments
.GetDeclarations ();
1772 string[] snames
= new string [names
.Length
];
1773 for (int i
= 0; i
< names
.Length
; i
++) {
1774 string type_argument_name
= names
[i
].Name
;
1775 int idx
= parameters
.GetParameterIndexByName (type_argument_name
);
1779 b
= new Block (null);
1781 b
.Error_AlreadyDeclaredTypeParameter (parameters
[i
].Location
,
1782 type_argument_name
, "method parameter");
1785 snames
[i
] = type_argument_name
;
1788 GenericTypeParameterBuilder
[] gen_params
= m
.MethodBuilder
.DefineGenericParameters (snames
);
1789 for (int i
= 0; i
< TypeParameters
.Length
; i
++)
1790 TypeParameters
[i
].Define (gen_params
[i
]);
1795 for (int i
= 0; i
< TypeParameters
.Length
; i
++) {
1796 if (!TypeParameters
[i
].ResolveType (this))
1804 /// We're called from MethodData.Define() after creating the MethodBuilder.
1806 public bool DefineType (EmitContext ec
, MethodBuilder mb
,
1807 MethodInfo implementing
, bool is_override
)
1809 for (int i
= 0; i
< TypeParameters
.Length
; i
++)
1810 if (!TypeParameters
[i
].DefineType (
1811 ec
, mb
, implementing
, is_override
))
1814 bool ok
= parameters
.Resolve (ec
);
1816 if ((return_type
!= null) && (return_type
.ResolveAsTypeTerminal (ec
, false) == null))
1822 public void EmitAttributes ()
1824 for (int i
= 0; i
< TypeParameters
.Length
; i
++)
1825 TypeParameters
[i
].Emit ();
1827 if (OptAttributes
!= null)
1828 OptAttributes
.Emit ();
1831 public override MemberList
FindMembers (MemberTypes mt
, BindingFlags bf
,
1832 MemberFilter filter
, object criteria
)
1834 throw new Exception ();
1837 public override MemberCache MemberCache
{
1843 public override AttributeTargets AttributeTargets
{
1845 return AttributeTargets
.Method
| AttributeTargets
.ReturnValue
;
1849 public override string DocCommentHeader
{
1850 get { return "M:"; }
1853 public new void VerifyClsCompliance ()
1855 foreach (TypeParameter tp
in TypeParameters
) {
1856 if (tp
.Constraints
== null)
1859 tp
.Constraints
.VerifyClsCompliance ();
1864 public partial class TypeManager
1866 static public Type activator_type
;
1868 public static TypeContainer
LookupGenericTypeContainer (Type t
)
1870 t
= DropGenericTypeArguments (t
);
1871 return LookupTypeContainer (t
);
1874 static Variance
GetTypeParameterVariance (Type type
)
1876 TypeParameter tparam
= LookupTypeParameter (type
);
1878 return tparam
.Variance
;
1880 switch (type
.GenericParameterAttributes
& GenericParameterAttributes
.VarianceMask
) {
1881 case GenericParameterAttributes
.Covariant
:
1882 return Variance
.Covariant
;
1883 case GenericParameterAttributes
.Contravariant
:
1884 return Variance
.Contravariant
;
1886 return Variance
.None
;
1890 public static Variance
CheckTypeVariance (Type t
, Variance expected
, MemberCore member
)
1892 TypeParameter tp
= LookupTypeParameter (t
);
1894 Variance v
= tp
.Variance
;
1895 if (expected
== Variance
.None
&& v
!= expected
||
1896 expected
== Variance
.Covariant
&& v
== Variance
.Contravariant
||
1897 expected
== Variance
.Contravariant
&& v
== Variance
.Covariant
)
1898 tp
.ErrorInvalidVariance (member
, expected
);
1903 if (t
.IsGenericType
) {
1904 Type
[] targs_definition
= GetTypeArguments (DropGenericTypeArguments (t
));
1905 Type
[] targs
= GetTypeArguments (t
);
1906 for (int i
= 0; i
< targs_definition
.Length
; ++i
) {
1907 Variance v
= GetTypeParameterVariance (targs_definition
[i
]);
1908 CheckTypeVariance (targs
[i
], (Variance
) ((int)v
* (int)expected
), member
);
1915 return CheckTypeVariance (GetElementType (t
), expected
, member
);
1917 return Variance
.None
;
1920 public static bool IsVariantOf (Type type1
, Type type2
)
1922 if (!type1
.IsGenericType
|| !type2
.IsGenericType
)
1925 Type generic_target_type
= DropGenericTypeArguments (type2
);
1926 if (DropGenericTypeArguments (type1
) != generic_target_type
)
1929 Type
[] t1
= GetTypeArguments (type1
);
1930 Type
[] t2
= GetTypeArguments (type2
);
1931 Type
[] targs_definition
= GetTypeArguments (generic_target_type
);
1932 for (int i
= 0; i
< targs_definition
.Length
; ++i
) {
1933 Variance v
= GetTypeParameterVariance (targs_definition
[i
]);
1934 if (v
== Variance
.None
) {
1940 if (v
== Variance
.Covariant
) {
1941 if (!Convert
.ImplicitReferenceConversionExists (new EmptyExpression (t1
[i
]), t2
[i
]))
1943 } else if (!Convert
.ImplicitReferenceConversionExists (new EmptyExpression (t2
[i
]), t1
[i
])) {
1952 /// Check whether `a' and `b' may become equal generic types.
1953 /// The algorithm to do that is a little bit complicated.
1955 public static bool MayBecomeEqualGenericTypes (Type a
, Type b
, Type
[] class_inferred
,
1956 Type
[] method_inferred
)
1958 if (a
.IsGenericParameter
) {
1960 // If a is an array of a's type, they may never
1964 b
= GetElementType (b
);
1970 // If b is a generic parameter or an actual type,
1971 // they may become equal:
1973 // class X<T,U> : I<T>, I<U>
1974 // class X<T> : I<T>, I<float>
1976 if (b
.IsGenericParameter
|| !b
.IsGenericType
) {
1977 int pos
= a
.GenericParameterPosition
;
1978 Type
[] args
= a
.DeclaringMethod
!= null ? method_inferred
: class_inferred
;
1979 if (args
[pos
] == null) {
1984 return args
[pos
] == a
;
1988 // We're now comparing a type parameter with a
1989 // generic instance. They may become equal unless
1990 // the type parameter appears anywhere in the
1991 // generic instance:
1993 // class X<T,U> : I<T>, I<X<U>>
1994 // -> error because you could instanciate it as
1997 // class X<T> : I<T>, I<X<T>> -> ok
2000 Type
[] bargs
= GetTypeArguments (b
);
2001 for (int i
= 0; i
< bargs
.Length
; i
++) {
2002 if (a
.Equals (bargs
[i
]))
2009 if (b
.IsGenericParameter
)
2010 return MayBecomeEqualGenericTypes (b
, a
, class_inferred
, method_inferred
);
2013 // At this point, neither a nor b are a type parameter.
2015 // If one of them is a generic instance, let
2016 // MayBecomeEqualGenericInstances() compare them (if the
2017 // other one is not a generic instance, they can never
2021 if (a
.IsGenericType
|| b
.IsGenericType
)
2022 return MayBecomeEqualGenericInstances (a
, b
, class_inferred
, method_inferred
);
2025 // If both of them are arrays.
2028 if (a
.IsArray
&& b
.IsArray
) {
2029 if (a
.GetArrayRank () != b
.GetArrayRank ())
2032 a
= GetElementType (a
);
2033 b
= GetElementType (b
);
2035 return MayBecomeEqualGenericTypes (a
, b
, class_inferred
, method_inferred
);
2039 // Ok, two ordinary types.
2042 return a
.Equals (b
);
2046 // Checks whether two generic instances may become equal for some
2047 // particular instantiation (26.3.1).
2049 public static bool MayBecomeEqualGenericInstances (Type a
, Type b
,
2050 Type
[] class_inferred
,
2051 Type
[] method_inferred
)
2053 if (!a
.IsGenericType
|| !b
.IsGenericType
)
2055 if (a
.GetGenericTypeDefinition () != b
.GetGenericTypeDefinition ())
2058 return MayBecomeEqualGenericInstances (
2059 GetTypeArguments (a
), GetTypeArguments (b
), class_inferred
, method_inferred
);
2062 public static bool MayBecomeEqualGenericInstances (Type
[] aargs
, Type
[] bargs
,
2063 Type
[] class_inferred
,
2064 Type
[] method_inferred
)
2066 if (aargs
.Length
!= bargs
.Length
)
2069 for (int i
= 0; i
< aargs
.Length
; i
++) {
2070 if (!MayBecomeEqualGenericTypes (aargs
[i
], bargs
[i
], class_inferred
, method_inferred
))
2078 /// Type inference. Try to infer the type arguments from `method',
2079 /// which is invoked with the arguments `arguments'. This is used
2080 /// when resolving an Invocation or a DelegateInvocation and the user
2081 /// did not explicitly specify type arguments.
2083 public static int InferTypeArguments (EmitContext ec
,
2084 ArrayList arguments
,
2085 ref MethodBase method
)
2087 ATypeInference ti
= ATypeInference
.CreateInstance (arguments
);
2088 Type
[] i_args
= ti
.InferMethodArguments (ec
, method
);
2090 return ti
.InferenceScore
;
2092 if (i_args
.Length
== 0)
2095 method
= ((MethodInfo
) method
).MakeGenericMethod (i_args
);
2102 public static bool InferTypeArguments (AParametersCollection apd
,
2103 ref MethodBase method
)
2105 if (!TypeManager
.IsGenericMethod (method
))
2108 ATypeInference ti
= ATypeInference
.CreateInstance (ArrayList
.Adapter (apd
.Types
));
2109 Type
[] i_args
= ti
.InferDelegateArguments (method
);
2113 method
= ((MethodInfo
) method
).MakeGenericMethod (i_args
);
2118 abstract class ATypeInference
2120 protected readonly ArrayList arguments
;
2121 protected readonly int arg_count
;
2123 protected ATypeInference (ArrayList arguments
)
2125 this.arguments
= arguments
;
2126 if (arguments
!= null)
2127 arg_count
= arguments
.Count
;
2130 public static ATypeInference
CreateInstance (ArrayList arguments
)
2132 return new TypeInferenceV3 (arguments
);
2135 public virtual int InferenceScore
{
2137 return int.MaxValue
;
2141 public abstract Type
[] InferMethodArguments (EmitContext ec
, MethodBase method
);
2142 public abstract Type
[] InferDelegateArguments (MethodBase method
);
2146 // Implements C# 3.0 type inference
2148 class TypeInferenceV3
: ATypeInference
2151 // Tracks successful rate of type inference
2153 int score
= int.MaxValue
;
2155 public TypeInferenceV3 (ArrayList arguments
)
2160 public override int InferenceScore
{
2166 public override Type
[] InferDelegateArguments (MethodBase method
)
2168 AParametersCollection pd
= TypeManager
.GetParameterData (method
);
2169 if (arg_count
!= pd
.Count
)
2172 Type
[] d_gargs
= method
.GetGenericArguments ();
2173 TypeInferenceContext context
= new TypeInferenceContext (d_gargs
);
2175 // A lower-bound inference is made from each argument type Uj of D
2176 // to the corresponding parameter type Tj of M
2177 for (int i
= 0; i
< arg_count
; ++i
) {
2178 Type t
= pd
.Types
[i
];
2179 if (!t
.IsGenericParameter
)
2182 context
.LowerBoundInference ((Type
)arguments
[i
], t
);
2185 if (!context
.FixAllTypes ())
2188 return context
.InferredTypeArguments
;
2191 public override Type
[] InferMethodArguments (EmitContext ec
, MethodBase method
)
2193 Type
[] method_generic_args
= method
.GetGenericArguments ();
2194 TypeInferenceContext context
= new TypeInferenceContext (method_generic_args
);
2195 if (!context
.UnfixedVariableExists
)
2196 return Type
.EmptyTypes
;
2198 AParametersCollection pd
= TypeManager
.GetParameterData (method
);
2199 if (!InferInPhases (ec
, context
, pd
))
2202 return context
.InferredTypeArguments
;
2206 // Implements method type arguments inference
2208 bool InferInPhases (EmitContext ec
, TypeInferenceContext tic
, AParametersCollection methodParameters
)
2210 int params_arguments_start
;
2211 if (methodParameters
.HasParams
) {
2212 params_arguments_start
= methodParameters
.Count
- 1;
2214 params_arguments_start
= arg_count
;
2217 Type
[] ptypes
= methodParameters
.Types
;
2220 // The first inference phase
2222 Type method_parameter
= null;
2223 for (int i
= 0; i
< arg_count
; i
++) {
2224 Argument a
= (Argument
) arguments
[i
];
2226 if (i
< params_arguments_start
) {
2227 method_parameter
= methodParameters
.Types
[i
];
2228 } else if (i
== params_arguments_start
) {
2229 if (arg_count
== params_arguments_start
+ 1 && TypeManager
.HasElementType (a
.Type
))
2230 method_parameter
= methodParameters
.Types
[params_arguments_start
];
2232 method_parameter
= TypeManager
.GetElementType (methodParameters
.Types
[params_arguments_start
]);
2234 ptypes
= (Type
[]) ptypes
.Clone ();
2235 ptypes
[i
] = method_parameter
;
2239 // When a lambda expression, an anonymous method
2240 // is used an explicit argument type inference takes a place
2242 AnonymousMethodExpression am
= a
.Expr
as AnonymousMethodExpression
;
2244 if (am
.ExplicitTypeInference (tic
, method_parameter
))
2249 if (a
.Expr
.Type
== TypeManager
.null_type
)
2253 // Otherwise an output type inference is made
2255 score
-= tic
.OutputTypeInference (ec
, a
.Expr
, method_parameter
);
2259 // Part of the second phase but because it happens only once
2260 // we don't need to call it in cycle
2262 bool fixed_any
= false;
2263 if (!tic
.FixIndependentTypeArguments (ptypes
, ref fixed_any
))
2266 return DoSecondPhase (ec
, tic
, ptypes
, !fixed_any
);
2269 bool DoSecondPhase (EmitContext ec
, TypeInferenceContext tic
, Type
[] methodParameters
, bool fixDependent
)
2271 bool fixed_any
= false;
2272 if (fixDependent
&& !tic
.FixDependentTypes (ref fixed_any
))
2275 // If no further unfixed type variables exist, type inference succeeds
2276 if (!tic
.UnfixedVariableExists
)
2279 if (!fixed_any
&& fixDependent
)
2282 // For all arguments where the corresponding argument output types
2283 // contain unfixed type variables but the input types do not,
2284 // an output type inference is made
2285 for (int i
= 0; i
< arg_count
; i
++) {
2287 // Align params arguments
2288 Type t_i
= methodParameters
[i
>= methodParameters
.Length
? methodParameters
.Length
- 1: i
];
2290 if (!TypeManager
.IsDelegateType (t_i
)) {
2291 if (TypeManager
.DropGenericTypeArguments (t_i
) != TypeManager
.expression_type
)
2294 t_i
= t_i
.GetGenericArguments () [0];
2297 MethodInfo mi
= Delegate
.GetInvokeMethod (t_i
, t_i
);
2298 Type rtype
= mi
.ReturnType
;
2301 // Blablabla, because reflection does not work with dynamic types
2302 Type
[] g_args
= t_i
.GetGenericArguments ();
2303 rtype
= g_args
[rtype
.GenericParameterPosition
];
2306 if (tic
.IsReturnTypeNonDependent (mi
, rtype
))
2307 score
-= tic
.OutputTypeInference (ec
, ((Argument
) arguments
[i
]).Expr
, t_i
);
2311 return DoSecondPhase (ec
, tic
, methodParameters
, true);
2315 public class TypeInferenceContext
2317 readonly Type
[] unfixed_types
;
2318 readonly Type
[] fixed_types
;
2319 readonly ArrayList
[] bounds
;
2322 public TypeInferenceContext (Type
[] typeArguments
)
2324 if (typeArguments
.Length
== 0)
2325 throw new ArgumentException ("Empty generic arguments");
2327 fixed_types
= new Type
[typeArguments
.Length
];
2328 for (int i
= 0; i
< typeArguments
.Length
; ++i
) {
2329 if (typeArguments
[i
].IsGenericParameter
) {
2330 if (bounds
== null) {
2331 bounds
= new ArrayList
[typeArguments
.Length
];
2332 unfixed_types
= new Type
[typeArguments
.Length
];
2334 unfixed_types
[i
] = typeArguments
[i
];
2336 fixed_types
[i
] = typeArguments
[i
];
2341 public Type
[] InferredTypeArguments
{
2347 void AddToBounds (Type t
, int index
)
2350 // Some types cannot be used as type arguments
2352 if (t
== TypeManager
.void_type
|| t
.IsPointer
)
2355 ArrayList a
= bounds
[index
];
2357 a
= new ArrayList ();
2365 // SPEC: does not cover type inference using constraints
2367 //if (TypeManager.IsGenericParameter (t)) {
2368 // GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
2369 // if (constraints != null) {
2370 // //if (constraints.EffectiveBaseClass != null)
2371 // // t = constraints.EffectiveBaseClass;
2377 bool AllTypesAreFixed (Type
[] types
)
2379 foreach (Type t
in types
) {
2380 if (t
.IsGenericParameter
) {
2386 if (t
.IsGenericType
)
2387 return AllTypesAreFixed (t
.GetGenericArguments ());
2394 // 26.3.3.8 Exact Inference
2396 public int ExactInference (Type u
, Type v
)
2398 // If V is an array type
2403 if (u
.GetArrayRank () != v
.GetArrayRank ())
2406 return ExactInference (TypeManager
.GetElementType (u
), TypeManager
.GetElementType (v
));
2409 // If V is constructed type and U is constructed type
2410 if (v
.IsGenericType
&& !v
.IsGenericTypeDefinition
) {
2411 if (!u
.IsGenericType
)
2414 Type
[] ga_u
= u
.GetGenericArguments ();
2415 Type
[] ga_v
= v
.GetGenericArguments ();
2416 if (ga_u
.Length
!= ga_v
.Length
)
2420 for (int i
= 0; i
< ga_u
.Length
; ++i
)
2421 score
+= ExactInference (ga_u
[i
], ga_v
[i
]);
2423 return score
> 0 ? 1 : 0;
2426 // If V is one of the unfixed type arguments
2427 int pos
= IsUnfixed (v
);
2431 AddToBounds (u
, pos
);
2435 public bool FixAllTypes ()
2437 for (int i
= 0; i
< unfixed_types
.Length
; ++i
) {
2445 // All unfixed type variables Xi are fixed for which all of the following hold:
2446 // a, There is at least one type variable Xj that depends on Xi
2447 // b, Xi has a non-empty set of bounds
2449 public bool FixDependentTypes (ref bool fixed_any
)
2451 for (int i
= 0; i
< unfixed_types
.Length
; ++i
) {
2452 if (unfixed_types
[i
] == null)
2455 if (bounds
[i
] == null)
2468 // All unfixed type variables Xi which depend on no Xj are fixed
2470 public bool FixIndependentTypeArguments (Type
[] methodParameters
, ref bool fixed_any
)
2472 ArrayList types_to_fix
= new ArrayList (unfixed_types
);
2473 for (int i
= 0; i
< methodParameters
.Length
; ++i
) {
2474 Type t
= methodParameters
[i
];
2476 if (!TypeManager
.IsDelegateType (t
)) {
2477 if (TypeManager
.DropGenericTypeArguments (t
) != TypeManager
.expression_type
)
2480 t
= t
.GetGenericArguments () [0];
2483 if (t
.IsGenericParameter
)
2486 MethodInfo invoke
= Delegate
.GetInvokeMethod (t
, t
);
2487 Type rtype
= invoke
.ReturnType
;
2488 if (!rtype
.IsGenericParameter
&& !rtype
.IsGenericType
)
2492 // Blablabla, because reflection does not work with dynamic types
2493 if (rtype
.IsGenericParameter
) {
2494 Type
[] g_args
= t
.GetGenericArguments ();
2495 rtype
= g_args
[rtype
.GenericParameterPosition
];
2498 // Remove dependent types, they cannot be fixed yet
2499 RemoveDependentTypes (types_to_fix
, rtype
);
2502 foreach (Type t
in types_to_fix
) {
2506 int idx
= IsUnfixed (t
);
2507 if (idx
>= 0 && !FixType (idx
)) {
2512 fixed_any
= types_to_fix
.Count
> 0;
2519 public bool FixType (int i
)
2521 // It's already fixed
2522 if (unfixed_types
[i
] == null)
2523 throw new InternalErrorException ("Type argument has been already fixed");
2528 ArrayList candidates
= (ArrayList
)bounds
[i
];
2529 if (candidates
== null)
2532 if (candidates
.Count
== 1) {
2533 unfixed_types
[i
] = null;
2534 fixed_types
[i
] = (Type
)candidates
[0];
2539 // Determines a unique type from which there is
2540 // a standard implicit conversion to all the other
2543 Type best_candidate
= null;
2545 int candidates_count
= candidates
.Count
;
2546 for (int ci
= 0; ci
< candidates_count
; ++ci
) {
2547 Type candidate
= (Type
)candidates
[ci
];
2548 for (cii
= 0; cii
< candidates_count
; ++cii
) {
2552 if (!Convert
.ImplicitConversionExists (null,
2553 new TypeExpression ((Type
)candidates
[cii
], Location
.Null
), candidate
)) {
2558 if (cii
!= candidates_count
)
2561 if (best_candidate
!= null)
2564 best_candidate
= candidate
;
2567 if (best_candidate
== null)
2570 unfixed_types
[i
] = null;
2571 fixed_types
[i
] = best_candidate
;
2576 // Uses inferred types to inflate delegate type argument
2578 public Type
InflateGenericArgument (Type parameter
)
2580 if (parameter
.IsGenericParameter
) {
2582 // Inflate method generic argument (MVAR) only
2584 if (parameter
.DeclaringMethod
== null)
2587 return fixed_types
[parameter
.GenericParameterPosition
];
2590 if (parameter
.IsGenericType
) {
2591 Type
[] parameter_targs
= parameter
.GetGenericArguments ();
2592 for (int ii
= 0; ii
< parameter_targs
.Length
; ++ii
) {
2593 parameter_targs
[ii
] = InflateGenericArgument (parameter_targs
[ii
]);
2595 return parameter
.GetGenericTypeDefinition ().MakeGenericType (parameter_targs
);
2602 // Tests whether all delegate input arguments are fixed and generic output type
2603 // requires output type inference
2605 public bool IsReturnTypeNonDependent (MethodInfo invoke
, Type returnType
)
2607 if (returnType
.IsGenericParameter
) {
2608 if (IsFixed (returnType
))
2610 } else if (returnType
.IsGenericType
) {
2611 if (TypeManager
.IsDelegateType (returnType
)) {
2612 invoke
= Delegate
.GetInvokeMethod (returnType
, returnType
);
2613 return IsReturnTypeNonDependent (invoke
, invoke
.ReturnType
);
2616 Type
[] g_args
= returnType
.GetGenericArguments ();
2618 // At least one unfixed return type has to exist
2619 if (AllTypesAreFixed (g_args
))
2625 // All generic input arguments have to be fixed
2626 AParametersCollection d_parameters
= TypeManager
.GetParameterData (invoke
);
2627 return AllTypesAreFixed (d_parameters
.Types
);
2630 bool IsFixed (Type type
)
2632 return IsUnfixed (type
) == -1;
2635 int IsUnfixed (Type type
)
2637 if (!type
.IsGenericParameter
)
2640 //return unfixed_types[type.GenericParameterPosition] != null;
2641 for (int i
= 0; i
< unfixed_types
.Length
; ++i
) {
2642 if (unfixed_types
[i
] == type
)
2650 // 26.3.3.9 Lower-bound Inference
2652 public int LowerBoundInference (Type u
, Type v
)
2654 // If V is one of the unfixed type arguments
2655 int pos
= IsUnfixed (v
);
2657 AddToBounds (u
, pos
);
2661 // If U is an array type
2663 int u_dim
= u
.GetArrayRank ();
2665 Type u_e
= TypeManager
.GetElementType (u
);
2668 if (u_dim
!= v
.GetArrayRank ())
2671 v_e
= TypeManager
.GetElementType (v
);
2674 return LowerBoundInference (u_e
, v_e
);
2677 return ExactInference (u_e
, v_e
);
2683 if (v
.IsGenericType
) {
2684 Type g_v
= v
.GetGenericTypeDefinition ();
2685 if ((g_v
!= TypeManager
.generic_ilist_type
) && (g_v
!= TypeManager
.generic_icollection_type
) &&
2686 (g_v
!= TypeManager
.generic_ienumerable_type
))
2689 v_e
= TypeManager
.GetTypeArguments (v
)[0];
2692 return LowerBoundInference (u_e
, v_e
);
2695 return ExactInference (u_e
, v_e
);
2697 } else if (v
.IsGenericType
&& !v
.IsGenericTypeDefinition
) {
2699 // if V is a constructed type C<V1..Vk> and there is a unique set of types U1..Uk
2700 // such that a standard implicit conversion exists from U to C<U1..Uk> then an exact
2701 // inference is made from each Ui for the corresponding Vi
2703 ArrayList u_candidates
= new ArrayList ();
2704 if (u
.IsGenericType
)
2705 u_candidates
.Add (u
);
2707 for (Type t
= u
.BaseType
; t
!= null; t
= t
.BaseType
) {
2708 if (t
.IsGenericType
&& !t
.IsGenericTypeDefinition
)
2709 u_candidates
.Add (t
);
2712 // TODO: Implement GetGenericInterfaces only and remove
2713 // the if from foreach
2714 u_candidates
.AddRange (TypeManager
.GetInterfaces (u
));
2716 Type open_v
= v
.GetGenericTypeDefinition ();
2717 Type
[] unique_candidate_targs
= null;
2718 Type
[] ga_v
= v
.GetGenericArguments ();
2719 foreach (Type u_candidate
in u_candidates
) {
2720 if (!u_candidate
.IsGenericType
|| u_candidate
.IsGenericTypeDefinition
)
2723 if (TypeManager
.DropGenericTypeArguments (u_candidate
) != open_v
)
2727 // The unique set of types U1..Uk means that if we have an interface C<T>,
2728 // class U: C<int>, C<long> then no type inference is made when inferring
2729 // from U to C<T> because T could be int or long
2731 if (unique_candidate_targs
!= null) {
2732 Type
[] second_unique_candidate_targs
= u_candidate
.GetGenericArguments ();
2733 if (TypeManager
.IsEqual (unique_candidate_targs
, second_unique_candidate_targs
)) {
2734 unique_candidate_targs
= second_unique_candidate_targs
;
2739 // This should always cause type inference failure
2745 unique_candidate_targs
= u_candidate
.GetGenericArguments ();
2748 if (unique_candidate_targs
!= null) {
2750 for (int i
= 0; i
< unique_candidate_targs
.Length
; ++i
)
2751 if (ExactInference (unique_candidate_targs
[i
], ga_v
[i
]) == 0)
2761 // 26.3.3.6 Output Type Inference
2763 public int OutputTypeInference (EmitContext ec
, Expression e
, Type t
)
2765 // If e is a lambda or anonymous method with inferred return type
2766 AnonymousMethodExpression ame
= e
as AnonymousMethodExpression
;
2768 Type rt
= ame
.InferReturnType (ec
, this, t
);
2769 MethodInfo invoke
= Delegate
.GetInvokeMethod (t
, t
);
2772 AParametersCollection pd
= TypeManager
.GetParameterData (invoke
);
2773 return ame
.Parameters
.Count
== pd
.Count
? 1 : 0;
2776 Type rtype
= invoke
.ReturnType
;
2778 // Blablabla, because reflection does not work with dynamic types
2779 Type
[] g_args
= t
.GetGenericArguments ();
2780 rtype
= g_args
[rtype
.GenericParameterPosition
];
2782 return LowerBoundInference (rt
, rtype
) + 1;
2786 // if E is a method group and T is a delegate type or expression tree type
2787 // return type Tb with parameter types T1..Tk and return type Tb, and overload
2788 // resolution of E with the types T1..Tk yields a single method with return type U,
2789 // then a lower-bound inference is made from U for Tb.
2791 if (e
is MethodGroupExpr
) {
2792 // TODO: Or expression tree
2793 if (!TypeManager
.IsDelegateType (t
))
2796 MethodInfo invoke
= Delegate
.GetInvokeMethod (t
, t
);
2797 Type rtype
= invoke
.ReturnType
;
2799 // Blablabla, because reflection does not work with dynamic types
2800 Type
[] g_args
= t
.GetGenericArguments ();
2801 rtype
= g_args
[rtype
.GenericParameterPosition
];
2804 if (!TypeManager
.IsGenericType (rtype
))
2807 MethodGroupExpr mg
= (MethodGroupExpr
) e
;
2808 ArrayList args
= DelegateCreation
.CreateDelegateMethodArguments (invoke
, e
.Location
);
2809 mg
= mg
.OverloadResolve (ec
, ref args
, true, e
.Location
);
2813 // TODO: What should happen when return type is of generic type ?
2814 throw new NotImplementedException ();
2815 // return LowerBoundInference (null, rtype) + 1;
2819 // if e is an expression with type U, then
2820 // a lower-bound inference is made from U for T
2822 return LowerBoundInference (e
.Type
, t
) * 2;
2825 void RemoveDependentTypes (ArrayList types
, Type returnType
)
2827 int idx
= IsUnfixed (returnType
);
2833 if (returnType
.IsGenericType
) {
2834 foreach (Type t
in returnType
.GetGenericArguments ()) {
2835 RemoveDependentTypes (types
, t
);
2840 public bool UnfixedVariableExists
{
2842 if (unfixed_types
== null)
2845 foreach (Type ut
in unfixed_types
)