Example #1
0
        Conversion ImplicitEnumerationConversion(ResolveResult rr, IType toType)
        {
            // C# 4.0 spec: §6.1.3
            Debug.Assert(rr.IsCompileTimeConstant);
            TypeCode constantType = ReflectionHelper.GetTypeCode(rr.Type);

            if (constantType >= TypeCode.SByte && constantType <= TypeCode.Decimal && Convert.ToDouble(rr.ConstantValue) == 0)
            {
                if (NullableType.GetUnderlyingType(toType).Kind == TypeKind.Enum)
                {
                    return(Conversion.EnumerationConversion(true, NullableType.IsNullable(toType)));
                }
            }
            return(Conversion.None);
        }
Example #2
0
 Conversion ExplicitNullableConversion(IType fromType, IType toType)
 {
     // C# 4.0 spec: §6.1.4
     if (NullableType.IsNullable(toType) || NullableType.IsNullable(fromType))
     {
         IType t = NullableType.GetUnderlyingType(toType);
         IType s = NullableType.GetUnderlyingType(fromType);
         if (IdentityConversion(s, t))
         {
             return(Conversion.ExplicitNullableConversion);
         }
         if (AnyNumericConversion(s, t))
         {
             return(Conversion.ExplicitLiftedNumericConversion);
         }
         if (ExplicitEnumerationConversion(s, t))
         {
             return(Conversion.EnumerationConversion(false, true));
         }
     }
     return(Conversion.None);
 }
Example #3
0
        Conversion ExplicitConversionImpl(IType fromType, IType toType)
        {
            // This method is called after we already checked for implicit conversions,
            // so any remaining conversions must be explicit.
            if (AnyNumericConversion(fromType, toType))
            {
                return(Conversion.ExplicitNumericConversion);
            }
            if (ExplicitEnumerationConversion(fromType, toType))
            {
                return(Conversion.EnumerationConversion(false, false));
            }
            Conversion c = ExplicitNullableConversion(fromType, toType);

            if (c.IsValid)
            {
                return(c);
            }
            if (ExplicitReferenceConversion(fromType, toType))
            {
                return(Conversion.ExplicitReferenceConversion);
            }
            if (UnboxingConversion(fromType, toType))
            {
                return(Conversion.UnboxingConversion);
            }
            if (ExplicitTypeParameterConversion(fromType, toType))
            {
                // Explicit type parameter conversions that aren't also
                // reference conversions are considered to be unboxing conversions
                return(Conversion.UnboxingConversion);
            }
            if (ExplicitPointerConversion(fromType, toType))
            {
                return(Conversion.ExplicitPointerConversion);
            }
            return(UserDefinedExplicitConversion(fromType, toType));
        }