+ static Expression TypeParameter_to_Null (Expression expr, Type target_type,
+ Location loc)
+ {
+ if (!TypeParameter_to_Null (target_type)) {
+ Report.Error (403, loc, "Cannot convert null to the type " +
+ "parameter `{0}' becaues it could be a value " +
+ "type. Consider using `default ({0})' instead.",
+ target_type);
+ return null;
+ }
+
+ return new NullCast (expr, target_type);
+ }
+
+ static bool TypeParameter_to_Null (Type target_type)
+ {
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
+ if (gc == null)
+ return false;
+
+ if (gc.HasReferenceTypeConstraint)
+ return true;
+ if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
+ return true;
+
+ return false;
+ }
+
+ static Type TypeParam_EffectiveBaseType (EmitContext ec, Type t)
+ {
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
+ if (gc == null)
+ return TypeManager.object_type;
+
+ return TypeParam_EffectiveBaseType (ec, gc);
+ }
+
+ static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
+ {
+ ArrayList list = new ArrayList ();
+ list.Add (gc.EffectiveBaseClass);
+ foreach (Type t in gc.InterfaceConstraints) {
+ if (!t.IsGenericParameter)
+ continue;
+
+ GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
+ if (new_gc != null)
+ list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
+ }
+ return FindMostEncompassedType (ec, list);
+ }
+
+ static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
+ {
+ if (is_reference)
+ return new EmptyCast (expr, target_type);
+ else
+ return new BoxedCast (expr, target_type);
+ }
+
+ static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
+ Type target_type)
+ {
+ Type expr_type = expr.Type;
+
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
+
+ if (gc == null) {
+ if (target_type == TypeManager.object_type)
+ return new BoxedCast (expr);
+
+ return null;
+ }
+
+ // We're converting from a type parameter which is known to be a reference type.
+ bool is_reference = gc.IsReferenceType;
+ Type base_type = TypeParam_EffectiveBaseType (ec, gc);
+
+ if (TypeManager.IsSubclassOf (base_type, target_type))
+ return TypeParameterConversion (expr, is_reference, target_type);
+
+ if (target_type.IsInterface) {
+ if (TypeManager.ImplementsInterface (base_type, target_type))
+ return TypeParameterConversion (expr, is_reference, target_type);
+
+ foreach (Type t in gc.InterfaceConstraints) {
+ if (TypeManager.IsSubclassOf (t, target_type))
+ return TypeParameterConversion (expr, is_reference, target_type);
+ }
+ }
+
+ foreach (Type t in gc.InterfaceConstraints) {
+ if (!t.IsGenericParameter)
+ continue;
+ if (TypeManager.IsSubclassOf (t, target_type))
+ return TypeParameterConversion (expr, is_reference, target_type);
+ }
+
+ return null;
+ }
+