// Copyright 2003-2008 Novell, Inc.
//
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Reflection;
+using System.Reflection.Emit;
+
namespace Mono.CSharp {
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.Reflection;
- using System.Reflection.Emit;
//
// A container class for all the conversion operations
implicit_conv = new DoubleHash (100);
}
-#if GMCS_SOURCE
static Type TypeParam_EffectiveBaseType (GenericConstraints gc)
{
- ArrayList list = new ArrayList ();
+ var list = new List<Type> ();
list.Add (gc.EffectiveBaseClass);
foreach (Type t in gc.InterfaceConstraints) {
if (!TypeManager.IsGenericParameter (t))
}
return FindMostEncompassedType (list);
}
-#endif
//
// From a one-dimensional array-type S[] to System.Collections.IList<T> and base
//
static bool Array_To_IList (Type array, Type list, bool isExplicit)
{
-#if GMCS_SOURCE
- if ((array.GetArrayRank () != 1) || !list.IsGenericType)
+ if ((array.GetArrayRank () != 1) || !TypeManager.IsGenericType (list))
return false;
- Type gt = list.GetGenericTypeDefinition ();
+ Type gt = TypeManager.DropGenericTypeArguments (list);
if ((gt != TypeManager.generic_ilist_type) &&
(gt != TypeManager.generic_icollection_type) &&
(gt != TypeManager.generic_ienumerable_type))
return false;
Type element_type = TypeManager.GetElementType (array);
- Type arg_type = TypeManager.GetTypeArguments (list) [0];
+ Type arg_type = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (list) [0]);
if (element_type == arg_type)
return true;
MyEmptyExpr.SetType (t);
return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
-#else
- return false;
-#endif
}
static bool IList_To_Array(Type list, Type array)
{
-# if GMCS_SOURCE
- if (!list.IsGenericType || !array.IsArray || array.GetArrayRank() != 1)
+ if (!TypeManager.IsGenericType (list) || !array.IsArray || array.GetArrayRank() != 1)
return false;
- Type gt = list.GetGenericTypeDefinition();
+ Type gt = TypeManager.DropGenericTypeArguments (list);
if (gt != TypeManager.generic_ilist_type &&
gt != TypeManager.generic_icollection_type &&
gt != TypeManager.generic_ienumerable_type)
return false;
- Type arg_type = TypeManager.GetTypeArguments(list)[0];
+ Type arg_type = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments(list)[0]);
Type element_type = TypeManager.GetElementType(array);
if (element_type == arg_type)
MyEmptyExpr.SetType(element_type);
return ImplicitReferenceConversionExists(MyEmptyExpr, arg_type) || ExplicitReferenceConversionExists(element_type, arg_type);
-#else
- return false;
-#endif
}
static Expression ImplicitTypeParameterConversion (Expression expr,
Type target_type)
{
-#if GMCS_SOURCE
Type expr_type = expr.Type;
GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
if (TypeManager.ImplementsInterface (t, target_type))
return new ClassCast (expr, target_type);
}
-#endif
+
return null;
}
static bool ImplicitTypeParameterBoxingConversion (Type expr_type, Type target_type,
out bool use_class_cast)
{
-#if GMCS_SOURCE
GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
if (gc == null) {
if (TypeManager.ImplementsInterface (t, target_type))
return true;
}
-#endif
use_class_cast = false;
return false;
static Expression ExplicitTypeParameterConversion (Expression source, Type source_type, Type target_type)
{
-#if GMCS_SOURCE
if (TypeManager.IsGenericParameter (target_type)) {
GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
if (gc == null)
if (target_type.IsInterface)
return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
-#endif
+
return null;
}
//
NullLiteral nl = expr as NullLiteral;
if (nl != null) {
- return nl.ConvertImplicitly(target_type);
+ return nl.ConvertImplicitly (null, target_type);
}
if (ImplicitReferenceConversionExists (expr, target_type)) {
// from the null type to any reference-type.
if (expr_type == TypeManager.null_type)
- return target_type != TypeManager.anonymous_method_type;
+ return target_type != InternalType.AnonymousMethod;
if (TypeManager.IsGenericParameter (expr_type))
return ImplicitTypeParameterConversion (expr, target_type) != null;
// notice that it is possible to write "ValueType v = 1", the ValueType here
// is an abstract class, and not really a value type, so we apply the same rules.
//
- if (target_type == TypeManager.object_type) {
+ if (target_type == TypeManager.object_type || TypeManager.IsDynamicType (target_type)) {
//
// A pointer type cannot be converted to object
//
if (TypeManager.IsValueType (expr_type))
return false;
+
if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
- return expr_type != TypeManager.anonymous_method_type;
+ // No mcs internal types are convertible
+ return expr_type.Module != typeof (Convert).Module;
}
return false;
return false;
}
+ if (TypeManager.IsVariantOf (expr_type, target_type))
+ return true;
+
// from any interface type S to interface-type T.
if (expr_type.IsInterface && target_type.IsInterface) {
return TypeManager.ImplementsInterface (expr_type, target_type);
if (TypeManager.IsEqual (expr_type, target_type))
return true;
- if (TypeManager.IsVariantOf (expr_type, target_type))
- return true;
-
return false;
}
//
// From any value-type to the type object.
//
- if (target_type == TypeManager.object_type) {
+ if (target_type == TypeManager.object_type || TypeManager.IsDynamicType (target_type)) {
//
// A pointer type cannot be converted to object
//
return false;
}
- static Expression ImplicitNulableConversion (EmitContext ec, Expression expr, Type target_type)
+ public static Expression ImplicitNulableConversion (ResolveContext ec, Expression expr, Type target_type)
{
Type expr_type = expr.Type;
if (expr_type == TypeManager.null_type)
return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
- Type target = TypeManager.GetTypeArguments (target_type)[0];
- Expression e;
+ // S -> T?
+ Type t_el = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (target_type)[0]);
// S? -> T?
- if (TypeManager.IsNullableType (expr_type)) {
- Type etype = TypeManager.GetTypeArguments (expr_type)[0];
+ if (TypeManager.IsNullableType (expr_type))
+ expr_type = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (expr_type)[0]);
- if (ec == null)
- return ImplicitConversionExists (ec, new EmptyExpression (etype), target) ? EmptyExpression.Null : null;
+ //
+ // Predefined implicit identity or implicit numeric conversion
+ // has to exist between underlying type S and underlying type T
+ //
- Expression unwrap = Nullable.Unwrap.Create (expr);
- e = ImplicitConversion (ec, unwrap, target, expr.Location);
- if (e == null)
- return null;
+ // Handles probing
+ if (ec == null) {
+ if (expr_type == t_el)
+ return EmptyExpression.Null;
- return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
+ return ImplicitNumericConversion (null, expr_type, t_el);
}
- // S -> T?
- if (ec == null)
- return ImplicitConversionExists (ec, expr, target) ? EmptyExpression.Null : null;
+ Expression unwrap;
+ if (expr_type != expr.Type)
+ unwrap = Nullable.Unwrap.Create (expr);
+ else
+ unwrap = expr;
- e = ImplicitConversion (ec, expr, target, expr.Location);
- if (e != null)
- return Nullable.Wrap.Create (e, target_type);
+ Expression conv = expr_type == t_el ? unwrap : ImplicitNumericConversion (unwrap, expr_type, t_el);
+ if (conv == null)
+ return null;
- return null;
+ if (expr_type != expr.Type)
+ return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
+
+ // Do constant optimization for S -> T?
+ if (unwrap is Constant)
+ conv = ((Constant) unwrap).ConvertImplicitly (ec, t_el);
+
+ return Nullable.Wrap.Create (conv, target_type);
}
/// <summary>
/// Same as ImplicitStandardConversionExists except that it also looks at
/// implicit user defined conversions - needed for overload resolution
/// </summary>
- public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
+ public static bool ImplicitConversionExists (ResolveContext ec, Expression expr, Type target_type)
{
if (ImplicitStandardConversionExists (expr, target_type))
return true;
- if (expr.Type == TypeManager.anonymous_method_type) {
+ if (expr.Type == InternalType.AnonymousMethod) {
if (!TypeManager.IsDelegateType (target_type) &&
TypeManager.DropGenericTypeArguments (target_type) != TypeManager.expression_type)
return false;
AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
return ame.ImplicitStandardConversionExists (ec, target_type);
}
+
+ if (expr.eclass == ExprClass.MethodGroup) {
+ if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1) {
+ MethodGroupExpr mg = expr as MethodGroupExpr;
+ if (mg != null)
+ return DelegateCreation.ImplicitStandardConversionExists (ec, mg, target_type);
+ }
+
+ return false;
+ }
return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
}
- public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
- {
- return ImplicitUserConversion (ec, new EmptyExpression (source), target, Location.Null) != null;
- }
-
/// <summary>
/// Determines if a standard implicit conversion exists from
/// expr_type to target_type
if (expr_type == TypeManager.null_type) {
NullLiteral nl = expr as NullLiteral;
if (nl != null)
- return nl.ConvertImplicitly (target_type) != null;
+ return nl.ConvertImplicitly (null, target_type) != null;
}
if (expr_type == TypeManager.void_type)
return false;
- if (expr.eclass == ExprClass.MethodGroup) {
- if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1) {
- MethodGroupExpr mg = expr as MethodGroupExpr;
- if (mg != null) {
- return DelegateCreation.ImplicitStandardConversionExists (mg, target_type) != null;
- }
- }
-
- return false;
- }
-
- //Console.WriteLine ("Expr is {0}", expr);
- //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
if (TypeManager.IsEqual (expr_type, target_type))
return true;
- if (TypeManager.IsNullableType (target_type))
+ if (TypeManager.IsNullableType (target_type)) {
return ImplicitNulableConversion (null, expr, target_type) != null;
+ }
// First numeric conversions
if (ImplicitNumericConversion (null, expr_type, target_type) != null)
return true;
}
- if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
+ if (value == 0 && TypeManager.IsEnumType (target_type))
return true;
}
if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
return true;
+ // Conversion from __arglist to System.ArgIterator
+ if (expr_type == InternalType.Arglist)
+ return target_type == TypeManager.arg_iterator_type;
+
return false;
}
/// Finds "most encompassed type" according to the spec (13.4.2)
/// amongst the methods in the MethodGroupExpr
/// </summary>
- static Type FindMostEncompassedType (ArrayList types)
+ static Type FindMostEncompassedType (IList<Type> types)
{
Type best = null;
/// Finds "most encompassing type" according to the spec (13.4.2)
/// amongst the types in the given set
/// </summary>
- static Type FindMostEncompassingType (ArrayList types)
+ static Type FindMostEncompassingType (IList<Type> types)
{
Type best = null;
return null;
if (types.Count == 1)
- return (Type) types [0];
+ return types [0];
EmptyExpression expr = EmptyExpression.Grab ();
/// by making use of FindMostEncomp* methods. Applies the correct rules separately
/// for explicit and implicit conversion operators.
/// </summary>
- static public Type FindMostSpecificSource (IList list,
+ static public Type FindMostSpecificSource (IList<MethodInfo> list,
Expression source, bool apply_explicit_conv_rules)
{
- ArrayList src_types_set = new ArrayList ();
+ var src_types_set = new List<Type> ();
//
// If any operator converts from S then Sx = S
// Explicit Conv rules
//
if (apply_explicit_conv_rules) {
- ArrayList candidate_set = new ArrayList ();
+ var candidate_set = new List<Type> ();
foreach (Type param_type in src_types_set){
if (ImplicitStandardConversionExists (source, param_type))
/// <summary>
/// Finds the most specific target Tx according to section 13.4.4
/// </summary>
- static public Type FindMostSpecificTarget (IList list,
+ static public Type FindMostSpecificTarget (IList<MethodInfo> list,
Type target, bool apply_explicit_conv_rules)
{
- ArrayList tgt_types_set = new ArrayList ();
+ var tgt_types_set = new List<Type> ();
//
// If any operator converts to T then Tx = T
// Explicit conv rules
//
if (apply_explicit_conv_rules) {
- ArrayList candidate_set = new ArrayList ();
+ var candidate_set = new List<Type> ();
EmptyExpression expr = EmptyExpression.Grab ();
/// <summary>
/// User-defined Implicit conversions
/// </summary>
- static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
+ static public Expression ImplicitUserConversion (ResolveContext ec, Expression source,
Type target, Location loc)
{
- Expression expr = UserDefinedConversion (ec, source, target, loc, false);
- if (expr != null && !TypeManager.IsEqual (expr.Type, target))
- expr = ImplicitConversionStandard (ec, expr, target, loc);
-
- return expr;
+ return UserDefinedConversion (ec, source, target, loc, false, true);
}
/// <summary>
/// User-defined Explicit conversions
/// </summary>
- static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
+ static Expression ExplicitUserConversion (ResolveContext ec, Expression source,
Type target, Location loc)
{
- Expression expr = UserDefinedConversion (ec, source, target, loc, true);
- if (expr != null && !TypeManager.IsEqual (expr.Type, target))
- expr = ExplicitConversionStandard (ec, expr, target, loc);
-
- return expr;
+ return UserDefinedConversion (ec, source, target, loc, true, true);
}
- static void AddConversionOperators (ArrayList list,
+ static void AddConversionOperators (List<MethodInfo> list,
Expression source, Type target_type,
bool look_for_explicit,
MethodGroupExpr mg)
/// Compute the user-defined conversion operator from source_type to target_type.
/// `look_for_explicit' controls whether we should also include the list of explicit operators
/// </summary>
- static MethodInfo GetConversionOperator (Type container_type, Expression source, Type target_type, bool look_for_explicit)
+ static MethodInfo GetConversionOperator (CompilerContext ctx, Type container_type, Expression source, Type target_type, bool look_for_explicit)
{
- ArrayList ops = new ArrayList (4);
+ var ops = new List<MethodInfo> (4);
Type source_type = source.Type;
if (source_type != TypeManager.decimal_type) {
AddConversionOperators (ops, source, target_type, look_for_explicit,
- Expression.MethodLookup (container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
+ Expression.MethodLookup (ctx, container_type, source_type, "op_Implicit", Location.Null) as MethodGroupExpr);
if (look_for_explicit) {
AddConversionOperators (ops, source, target_type, look_for_explicit,
- Expression.MethodLookup (
+ Expression.MethodLookup (ctx,
container_type, source_type, "op_Explicit", Location.Null) as MethodGroupExpr);
}
}
if (target_type != TypeManager.decimal_type) {
AddConversionOperators (ops, source, target_type, look_for_explicit,
- Expression.MethodLookup (container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
+ Expression.MethodLookup (ctx, container_type, target_type, "op_Implicit", Location.Null) as MethodGroupExpr);
if (look_for_explicit) {
AddConversionOperators (ops, source, target_type, look_for_explicit,
- Expression.MethodLookup (
+ Expression.MethodLookup (ctx,
container_type, target_type, "op_Explicit", Location.Null) as MethodGroupExpr);
}
}
/// <summary>
/// User-defined conversions
/// </summary>
- static public Expression UserDefinedConversion (EmitContext ec, Expression source,
+ public static Expression UserDefinedConversion (ResolveContext ec, Expression source,
Type target, Location loc,
- bool look_for_explicit)
+ bool look_for_explicit, bool return_convert)
{
Type source_type = source.Type;
MethodInfo method = null;
+ Expression expr = null;
object o;
DoubleHash hash;
if (!(source is Constant) && hash.Lookup (source_type, target, out o)) {
method = (MethodInfo) o;
} else {
- method = GetConversionOperator (null, source, target, look_for_explicit);
- if (!(source is Constant))
- hash.Insert (source_type, target, method);
+ if (TypeManager.IsDynamicType (source_type))
+ return null;
+
+ method = GetConversionOperator (ec.Compiler, null, source, target, look_for_explicit);
}
- if (method == null)
- return null;
+ if (method != null) {
+ Type most_specific_source = TypeManager.GetParameterData (method).Types[0];
- Type most_specific_source = TypeManager.GetParameterData (method).Types [0];
+ //
+ // This will do the conversion to the best match that we
+ // found. Now we need to perform an implict standard conversion
+ // if the best match was not the type that we were requested
+ // by target.
+ //
+ if (look_for_explicit) {
+ ReportPrinter temp = new SessionReportPrinter ();
+ ReportPrinter prev = ec.Report.SetPrinter (temp);
- //
- // This will do the conversion to the best match that we
- // found. Now we need to perform an implict standard conversion
- // if the best match was not the type that we were requested
- // by target.
- //
- if (look_for_explicit)
- source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
- else
- source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
+ expr = ExplicitConversionStandard (ec, source, most_specific_source, loc);
- if (source == null)
- return null;
+ ec.Report.SetPrinter (prev);
+ if (temp.ErrorsCount != 0)
+ expr = null;
+ } else {
+ if (ImplicitStandardConversionExists (source, most_specific_source))
+ expr = ImplicitConversionStandard (ec, source, most_specific_source, loc);
+ else
+ expr = null;
+ }
+ }
+
+ if (expr == null) {
+ bool nullable = false;
+
+ if (TypeManager.IsNullableType (source_type)) {
+ source = Nullable.Unwrap.Create (source);
+ nullable = true;
+ }
+
+ Type target_underlying;
+ if (TypeManager.IsNullableType (target)) {
+ target_underlying = TypeManager.GetTypeArguments (target)[0];
+ nullable = true;
+ } else {
+ // No implicit conversion S? -> T for non-reference type T
+ if (!look_for_explicit && !TypeManager.IsReferenceType (target))
+ nullable = false;
- return new UserCast (method, source, loc).DoResolve (ec);
+ target_underlying = target;
+ }
+
+ if (nullable) {
+ expr = UserDefinedConversion (ec, source, target_underlying, loc, look_for_explicit, return_convert);
+
+ // Do result expression lifting only when it's needed
+ if (expr != null && (!look_for_explicit || TypeManager.IsReferenceType (target)))
+ expr = new Nullable.Lifted (expr, source, target).Resolve (ec);
+
+ return expr;
+ }
+ } else {
+ expr = new UserCast (method, expr, loc).Resolve (ec);
+
+ if (return_convert && !TypeManager.IsEqual (expr.Type, target)) {
+ if (look_for_explicit) {
+ expr = ExplicitConversionStandard (ec, expr, target, loc);
+ } else {
+ expr = ImplicitConversionStandard (ec, expr, target, loc);
+ }
+ }
+ }
+
+ if (!(source is Constant))
+ hash.Insert (source_type, target, method);
+
+ return expr;
}
/// <summary>
/// `target_type'. It returns a new expression that can be used
/// in a context that expects a `target_type'.
/// </summary>
- static public Expression ImplicitConversion (EmitContext ec, Expression expr,
+ static public Expression ImplicitConversion (ResolveContext ec, Expression expr,
Type target_type, Location loc)
{
Expression e;
/// This is different from `ImplicitConversion' in that the
/// user defined implicit conversions are excluded.
/// </summary>
- static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
+ static public Expression ImplicitConversionStandard (ResolveContext ec, Expression expr,
Type target_type, Location loc)
{
return ImplicitConversionStandard (ec, expr, target_type, loc, false);
}
- static Expression ImplicitConversionStandard (EmitContext ec, Expression expr, Type target_type, Location loc, bool explicit_cast)
+ static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr, Type target_type, Location loc, bool explicit_cast)
{
if (expr.eclass == ExprClass.MethodGroup){
if (!TypeManager.IsDelegateType (target_type)){
Expression e;
if (expr_type.Equals (target_type)) {
- if (expr_type != TypeManager.null_type && expr_type != TypeManager.anonymous_method_type)
+ if (expr_type != TypeManager.null_type && expr_type != InternalType.AnonymousMethod)
return expr;
return null;
}
Constant c = expr as Constant;
if (c != null) {
try {
- c = c.ConvertImplicitly (target_type);
+ c = c.ConvertImplicitly (ec, target_type);
} catch {
Console.WriteLine ("Conversion error happened in line {0}", loc);
throw;
// type is an enum-type
//
if (i.IsDefaultValue)
- return new EnumConstant (i, target_type);
+ return new EnumConstant (i, target_type).Resolve (ec);
}
- if (ec.InUnsafe) {
+ if (ec.IsUnsafe) {
if (expr_type.IsPointer){
if (target_type == TypeManager.void_ptr_type)
return EmptyCast.Create (expr, target_type);
return EmptyCast.Create (new NullPointer (loc), target_type);
}
- if (expr_type == TypeManager.anonymous_method_type){
+ if (expr_type == InternalType.AnonymousMethod){
AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
Expression am = ame.Compatible (ec, target_type);
if (am != null)
- return am.DoResolve (ec);
+ return am.Resolve (ec);
}
+ if (expr_type == InternalType.Arglist && target_type == TypeManager.arg_iterator_type)
+ return expr;
+
return null;
}
/// ImplicitConversion. If there is no implicit conversion, then
/// an error is signaled
/// </summary>
- static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
+ static public Expression ImplicitConversionRequired (ResolveContext ec, Expression source,
Type target_type, Location loc)
{
Expression e = ImplicitConversion (ec, source, target_type, loc);
if (e != null)
return e;
+ if (TypeManager.IsDynamicType (source.Type)) {
+ Arguments args = new Arguments (1);
+ args.Add (new Argument (source));
+ return new DynamicConversion (target_type, 0, args, loc).Resolve (ec);
+ }
+
source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
return null;
}
/// Performs an explicit conversion of the expression `expr' whose
/// type is expr.Type to `target_type'.
/// </summary>
- static public Expression ExplicitConversionCore (EmitContext ec, Expression expr,
+ static public Expression ExplicitConversionCore (ResolveContext ec, Expression expr,
Type target_type, Location loc)
{
Type expr_type = expr.Type;
return ne;
}
- if (ec.InUnsafe){
+ if (ec.IsUnsafe){
ne = ExplicitUnsafe (expr, target_type);
if (ne != null)
return ne;
if (expr_type == TypeManager.ushort_type ||
expr_type == TypeManager.uint32_type ||
- expr_type == TypeManager.uint64_type || expr_type == TypeManager.int64_type ||
expr_type == TypeManager.byte_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
+
+ if (expr_type == TypeManager.int64_type)
+ return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
+
+ if (expr_type == TypeManager.uint64_type)
+ return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
}
if (expr_type.IsPointer){
if (target_type == TypeManager.sbyte_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- else if (target_type == TypeManager.byte_type)
+ if (target_type == TypeManager.byte_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- else if (target_type == TypeManager.short_type)
+ if (target_type == TypeManager.short_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- else if (target_type == TypeManager.ushort_type)
+ if (target_type == TypeManager.ushort_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- else if (target_type == TypeManager.int32_type)
+ if (target_type == TypeManager.int32_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- else if (target_type == TypeManager.uint32_type)
+ if (target_type == TypeManager.uint32_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- else if (target_type == TypeManager.uint64_type || target_type == TypeManager.int64_type)
+ if (target_type == TypeManager.int64_type)
+ return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
+ if (target_type == TypeManager.uint64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
}
return null;
/// <summary>
/// Same as ExplicitConversion, only it doesn't include user defined conversions
/// </summary>
- static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
+ static public Expression ExplicitConversionStandard (ResolveContext ec, Expression expr,
Type target_type, Location l)
{
- int errors = Report.Errors;
+ int errors = ec.Report.Errors;
Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
- if (Report.Errors > errors)
+ if (ec.Report.Errors > errors)
return null;
if (ne != null)
if (ne != null)
return ne;
- if (ec.InUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
+ if (ec.IsUnsafe && expr.Type == TypeManager.void_ptr_type && target_type.IsPointer)
return EmptyCast.Create (expr, target_type);
expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
/// Performs an explicit conversion of the expression `expr' whose
/// type is expr.Type to `target_type'.
/// </summary>
- static public Expression ExplicitConversion (EmitContext ec, Expression expr,
+ static public Expression ExplicitConversion (ResolveContext ec, Expression expr,
Type target_type, Location loc)
{
Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
- if (e != null)
+ if (e != null) {
+ //
+ // Don't eliminate explicit precission casts
+ //
+ if (e == expr) {
+ if (target_type == TypeManager.float_type)
+ return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
+
+ if (target_type == TypeManager.double_type)
+ return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
+ }
+
return e;
+ }
-#if GMCS_SOURCE
Type expr_type = expr.Type;
if (TypeManager.IsNullableType (target_type)) {
if (TypeManager.IsNullableType (expr_type)) {
- Type target = TypeManager.GetTypeArguments (target_type)[0];
+ Type target = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (target_type)[0]);
Expression unwrap = Nullable.Unwrap.Create (expr);
e = ExplicitConversion (ec, unwrap, target, expr.Location);
if (e == null)
} else if (expr_type == TypeManager.object_type) {
return new UnboxCast (expr, target_type);
} else {
- Type target = TypeManager.GetTypeArguments (target_type) [0];
+ Type target = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (target_type) [0]);
e = ExplicitConversionCore (ec, expr, target, loc);
if (e != null)
if (ImplicitBoxingConversionExists (e, target_type, out use_class_cast))
return new BoxedCast (expr, target_type);
- e = ExplicitConversion (ec, e, target_type, loc);
+ e = ExplicitConversionCore (ec, e, target_type, loc);
if (e != null)
- e = EmptyCast.Create (e, target_type);
- return e;
+ return EmptyCast.Create (e, target_type);
}
-#endif
e = ExplicitUserConversion (ec, expr, target_type, loc);
if (e != null)