Nothing,
}
+ //
+ // This is just as a hint to AddressOf of what will be done with the
+ // address.
+ [Flags]
+ public enum AddressOp {
+ Store = 1,
+ Load = 2,
+ LoadStore = 3
+ };
+
/// <summary>
/// This interface is implemented by variables
/// </summary>
public interface IMemoryLocation {
/// <summary>
/// The AddressOf method should generate code that loads
- /// the address of the object and leaves it on the stack
+ /// the address of the object and leaves it on the stack.
+ ///
+ /// The `mode' argument is used to notify the expression
+ /// of whether this will be used to read from the address or
+ /// write to the address.
+ ///
+ /// This is just a hint that can be used to provide good error
+ /// reporting, and should have no other side effects.
/// </summary>
- void AddressOf (EmitContext ec);
+ void AddressOf (EmitContext ec, AddressOp mode);
}
/// <remarks>
/// Base class for expressions
/// </remarks>
public abstract class Expression {
- protected ExprClass eclass;
+ public ExprClass eclass;
protected Type type;
public Type Type {
}
}
- public ExprClass ExprClass {
- get {
- return eclass;
- }
-
- set {
- eclass = value;
- }
- }
-
/// <summary>
/// Utility wrapper routine for Error, just to beautify the code
/// </summary>
Report.Warning (warning, s);
}
- static public void error30 (Location loc, Type source, Type target)
+ static public void Error_CannotConvertType (Location loc, Type source, Type target)
{
Report.Error (30, loc, "Cannot convert type '" +
TypeManager.CSharpName (source) + "' to '" +
Expression e = DoResolve (ec);
if (e != null){
+
if (e is SimpleName){
SimpleName s = (SimpleName) e;
-
+
Report.Error (
- 103, s.Location,
- "The name `" + s.Name + "' could not be found in `" +
- ec.TypeContainer.Name + "'");
+ 103, s.Location,
+ "The name `" + s.Name + "' could not be found in `" +
+ ec.DeclSpace.Name + "'");
return null;
}
- if (e.ExprClass == ExprClass.Invalid)
- throw new Exception ("Expression " + e +
+ if (e.eclass == ExprClass.Invalid)
+ throw new Exception ("Expression " + e.GetType () +
" ExprClass is Invalid after resolve");
- if (e.ExprClass != ExprClass.MethodGroup)
+ if (e.eclass != ExprClass.MethodGroup)
if (e.type == null)
- throw new Exception ("Expression " + e +
- " did not set its type after Resolve");
+ throw new Exception (
+ "Expression " + e.GetType () +
+ " did not set its type after Resolve\n" +
+ "called from: " + this.GetType ());
}
return e;
/// </remarks>
public Expression ResolveWithSimpleName (EmitContext ec)
{
- Expression e = DoResolve (ec);
+ Expression e;
+
+ if (this is SimpleName)
+ e = ((SimpleName) this).DoResolveAllowStatic (ec);
+ else
+ e = DoResolve (ec);
if (e != null){
if (e is SimpleName)
return e;
- if (e.ExprClass == ExprClass.Invalid)
+ if (e.eclass == ExprClass.Invalid)
throw new Exception ("Expression " + e +
" ExprClass is Invalid after resolve");
- if (e.ExprClass != ExprClass.MethodGroup)
+ if (e.eclass != ExprClass.MethodGroup)
if (e.type == null)
throw new Exception ("Expression " + e +
" did not set its type after Resolve");
if (e != null){
if (e is SimpleName){
SimpleName s = (SimpleName) e;
-
+
Report.Error (
103, s.Location,
"The name `" + s.Name + "' could not be found in `" +
- ec.TypeContainer.Name + "'");
+ ec.DeclSpace.Name + "'");
return null;
}
- if (e.ExprClass == ExprClass.Invalid)
+ if (e.eclass == ExprClass.Invalid)
throw new Exception ("Expression " + e +
" ExprClass is Invalid after resolve");
- if (e.ExprClass != ExprClass.MethodGroup)
+ if (e.eclass != ExprClass.MethodGroup)
if (e.type == null)
throw new Exception ("Expression " + e +
" did not set its type after Resolve");
/// </remarks>
public abstract void Emit (EmitContext ec);
- /// <summary>
- /// This method should perform a reduction of the expression. This should
- /// never return null.
- /// </summary>
- public virtual Expression Reduce (EmitContext ec)
- {
- return this;
- }
-
/// <summary>
/// Protected constructor. Only derivate types should
/// be able to be created
///
/// <remarks>
/// The possible return values are:
- /// IntLiteral, UIntLiteral
- /// LongLiteral, ULongLiteral
- /// FloatLiteral, DoubleLiteral
- /// StringLiteral
+ /// IntConstant, UIntConstant
+ /// LongLiteral, ULongConstant
+ /// FloatConstant, DoubleConstant
+ /// StringConstant
+ ///
+ /// The value returned is already resolved.
/// </remarks>
- public static Expression Literalize (object v, Type t)
+ public static Constant Constantify (object v, Type t)
{
if (t == TypeManager.int32_type)
- return new IntLiteral ((int) v);
+ return new IntConstant ((int) v);
else if (t == TypeManager.uint32_type)
- return new UIntLiteral ((uint) v);
+ return new UIntConstant ((uint) v);
else if (t == TypeManager.int64_type)
- return new LongLiteral ((long) v);
+ return new LongConstant ((long) v);
else if (t == TypeManager.uint64_type)
- return new ULongLiteral ((ulong) v);
+ return new ULongConstant ((ulong) v);
else if (t == TypeManager.float_type)
- return new FloatLiteral ((float) v);
+ return new FloatConstant ((float) v);
else if (t == TypeManager.double_type)
- return new DoubleLiteral ((double) v);
+ return new DoubleConstant ((double) v);
else if (t == TypeManager.string_type)
- return new StringLiteral ((string) v);
+ return new StringConstant ((string) v);
else if (t == TypeManager.short_type)
- return new IntLiteral ((int) ((short)v));
+ return new ShortConstant ((short)v);
else if (t == TypeManager.ushort_type)
- return new IntLiteral ((int) ((ushort)v));
+ return new UShortConstant ((ushort)v);
else if (t == TypeManager.sbyte_type)
- return new IntLiteral ((int) ((sbyte)v));
+ return new SByteConstant (((sbyte)v));
else if (t == TypeManager.byte_type)
- return new IntLiteral ((int) ((byte)v));
+ return new ByteConstant ((byte)v);
else if (t == TypeManager.char_type)
- return new IntLiteral ((int) ((char)v));
- else
- throw new Exception ("Unknown type for literal (" + t +
+ return new CharConstant ((char)v);
+ else if (t == TypeManager.bool_type)
+ return new BoolConstant ((bool) v);
+ else if (TypeManager.IsEnumType (t)){
+ Constant e = Constantify (v, v.GetType ());
+
+ return new EnumConstant (e, t);
+ } else
+ throw new Exception ("Unknown type for constant (" + t +
"), details: " + v);
}
/// <summary>
/// Returns a fully formed expression after a MemberLookup
/// </summary>
- static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
+ public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
{
if (mi is EventInfo)
return new EventExpr ((EventInfo) mi, loc);
else if (mi is PropertyInfo)
return new PropertyExpr ((PropertyInfo) mi, loc);
else if (mi is Type){
- return new TypeExpr ((Type) mi);
+ return new TypeExpr ((System.Type) mi);
}
return null;
}
- //
- // We copy methods from `new_members' into `target_list' if the signature
- // for the method from in the new list does not exist in the target_list
- //
- // The name is assumed to be the same.
- //
- static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
- {
- if (target_list == null){
- target_list = new ArrayList ();
-
- target_list.AddRange (new_members);
- return target_list;
- }
-
- MemberInfo [] target_array = new MemberInfo [target_list.Count];
- target_list.CopyTo (target_array, 0);
-
- foreach (MemberInfo mi in new_members){
- MethodBase new_method = (MethodBase) mi;
- Type [] new_args = TypeManager.GetArgumentTypes (new_method);
-
- foreach (MethodBase method in target_array){
- Type [] old_args = TypeManager.GetArgumentTypes (method);
- int new_count = new_args.Length;
- int old_count = old_args.Length;
-
- if (new_count != old_count){
- target_list.Add (method);
- continue;
- }
-
- for (int i = 0; i < old_count; i++){
- if (old_args [i] == new_args [i])
- continue;
- target_list.Add (method);
- break;
- }
- }
- }
- return target_list;
- }
-
//
// FIXME: Probably implement a cache for (t,name,current_access_set)?
//
- // FIXME: We need to cope with access permissions here, or this wont
- // work!
- //
// This code could use some optimizations, but we need to do some
// measurements. For example, we could use a delegate to `flag' when
// something can not any longer be a method-group (because it is something
//
// FIXME: Potential optimization, have a static ArrayList
//
+
public static Expression MemberLookup (EmitContext ec, Type t, string name,
- bool same_type, MemberTypes mt,
- BindingFlags bf, Location loc)
+ MemberTypes mt, BindingFlags bf, Location loc)
{
- if (same_type)
- bf |= BindingFlags.NonPublic;
-
- //
- // Lookup for members starting in the type requested and going
- // up the hierarchy until a match is found.
- //
- // As soon as a non-method match is found, we return.
- //
- // If methods are found though, then the search proceeds scanning
- // for more public methods in the hierarchy with signatures that
- // do not match any of the signatures found so far.
- //
- ArrayList method_list = null;
- Type current_type = t;
- bool searching = true;
- do {
- MemberInfo [] mi;
-
- mi = RootContext.TypeManager.FindMembers (
- current_type, mt, bf | BindingFlags.DeclaredOnly,
- System.Type.FilterName, name);
-
- if (current_type == TypeManager.object_type)
- searching = false;
- else {
- current_type = current_type.BaseType;
-
- //
- // This happens with interfaces, they have a null
- // basetype
- //
- if (current_type == null)
- searching = false;
- }
+ MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
- if (mi == null)
- continue;
-
- int count = mi.Length;
+ if (mi == null)
+ return null;
- if (count == 0)
- continue;
-
- if (count == 1 && !(mi [0] is MethodBase))
- return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
+ int count = mi.Length;
- //
- // We found methods, turn the search into "method scan"
- // mode.
- //
- method_list = CopyNewMethods (method_list, mi);
- mt &= (MemberTypes.Method | MemberTypes.Constructor);
- } while (searching);
+ if (count > 1)
+ return new MethodGroupExpr (mi, loc);
- if (method_list != null && method_list.Count > 0)
- return new MethodGroupExpr (method_list);
+ if (mi [0] is MethodBase)
+ return new MethodGroupExpr (mi, loc);
- return null;
+ return ExprClassFromMemberInfo (ec, mi [0], loc);
}
public const MemberTypes AllMemberTypes =
MemberTypes.NestedType |
MemberTypes.Property;
- public const BindingFlags AllBindingsFlags =
+ public const BindingFlags AllBindingFlags =
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.Instance;
- public static Expression MemberLookup (EmitContext ec, Type t, string name,
- bool same_type, Location loc)
+ public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
+ {
+ return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+ }
+
+ public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
{
- return MemberLookup (ec, t, name, same_type, AllMemberTypes, AllBindingsFlags, loc);
+ return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
}
+ /// <summary>
+ /// This is a wrapper for MemberLookup that is not used to "probe", but
+ /// to find a final definition. If the final definition is not found, we
+ /// look for private members and display a useful debugging message if we
+ /// find it.
+ /// </summary>
+ public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
+ Location loc)
+ {
+ Expression e;
+
+ e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+
+ if (e != null)
+ return e;
+
+ e = MemberLookup (ec, t, name, AllMemberTypes,
+ AllBindingFlags | BindingFlags.NonPublic, loc);
+ if (e == null){
+ Report.Error (
+ 117, loc, "`" + t + "' does not contain a definition " +
+ "for `" + name + "'");
+ } else {
+ Report.Error (
+ 122, loc, "`" + t + "." + name +
+ "' is inaccessible due to its protection level");
+ }
+
+ return null;
+ }
+
+ static EmptyExpression MyEmptyExpr;
static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
{
Type expr_type = expr.Type;
+ if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
+ // if we are a method group, emit a warning
+
+ expr.Emit (null);
+ }
+
if (target_type == TypeManager.object_type) {
- if (expr_type.IsClass)
- return new EmptyCast (expr, target_type);
+ //
+ // A pointer type cannot be converted to object
+ //
+ if (expr_type.IsPointer)
+ return null;
+
if (expr_type.IsValueType)
return new BoxedCast (expr);
+ if (expr_type.IsClass || expr_type.IsInterface)
+ return new EmptyCast (expr, target_type);
} else if (expr_type.IsSubclassOf (target_type)) {
return new EmptyCast (expr, target_type);
} else {
+
+ // This code is kind of mirrored inside StandardConversionExists
+ // with the small distinction that we only probe there
+ //
+ // Always ensure that the code here and there is in sync
+
+ // from the null type to any reference-type.
+ if (expr is NullLiteral && !target_type.IsValueType)
+ return new EmptyCast (expr, target_type);
+
// from any class-type S to any interface-type T.
if (expr_type.IsClass && target_type.IsInterface) {
if (TypeManager.ImplementsInterface (expr_type, target_type))
if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
Type expr_element_type = expr_type.GetElementType ();
+
+ if (MyEmptyExpr == null)
+ MyEmptyExpr = new EmptyExpression ();
+
+ MyEmptyExpr.SetType (expr_element_type);
Type target_element_type = target_type.GetElementType ();
if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
- if (StandardConversionExists (expr_element_type,
+ if (StandardConversionExists (MyEmptyExpr,
target_element_type))
return new EmptyCast (expr, target_type);
}
if (target_type == TypeManager.icloneable_type)
return new EmptyCast (expr, target_type);
- // from the null type to any reference-type.
- if (expr is NullLiteral)
- return new EmptyCast (expr, target_type);
-
return null;
}
//
// Attempt to do the implicit constant expression conversions
- if (expr is IntLiteral){
+ if (expr is IntConstant){
Expression e;
- e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
+ e = TryImplicitIntConversion (target_type, (IntConstant) expr);
if (e != null)
return e;
- } else if (expr is LongLiteral && target_type == TypeManager.uint64_type){
+ } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
//
// Try the implicit constant expression conversion
// from long to ulong, instead of a nice routine,
// we just inline it
//
- if (((LongLiteral) expr).Value > 0)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+ long v = ((LongConstant) expr).Value;
+ if (v > 0)
+ return new ULongConstant ((ulong) v);
}
+
+ //
+ // If we have an enumeration, extract the underlying type,
+ // use this during the comparission, but wrap around the original
+ // target_type
+ //
+ Type real_target_type = target_type;
+
+ if (TypeManager.IsEnumType (real_target_type))
+ real_target_type = TypeManager.EnumToUnderlying (real_target_type);
+
+ if (expr_type == real_target_type)
+ return new EmptyCast (expr, target_type);
if (expr_type == TypeManager.sbyte_type){
//
// From sbyte to short, int, long, float, double.
//
- if (target_type == TypeManager.int32_type)
+ if (real_target_type == TypeManager.int32_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.short_type)
+ if (real_target_type == TypeManager.short_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.byte_type){
//
// From byte to short, ushort, int, uint, long, ulong, float, double
//
- if ((target_type == TypeManager.short_type) ||
- (target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type))
+ if ((real_target_type == TypeManager.short_type) ||
+ (real_target_type == TypeManager.ushort_type) ||
+ (real_target_type == TypeManager.int32_type) ||
+ (real_target_type == TypeManager.uint32_type))
return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.uint64_type)
+ if (real_target_type == TypeManager.uint64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.short_type){
//
// From short to int, long, float, double
//
- if (target_type == TypeManager.int32_type)
+ if (real_target_type == TypeManager.int32_type)
return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.ushort_type){
//
// From ushort to int, uint, long, ulong, float, double
//
- if (target_type == TypeManager.uint32_type)
+ if (real_target_type == TypeManager.uint32_type)
return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.uint64_type)
+ if (real_target_type == TypeManager.uint64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int32_type)
+ if (real_target_type == TypeManager.int32_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.int32_type){
//
// From int to long, float, double
//
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.uint32_type){
//
// From uint to long, ulong, float, double
//
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.uint64_type)
+ if (real_target_type == TypeManager.uint64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if ((expr_type == TypeManager.uint64_type) ||
(expr_type == TypeManager.int64_type)){
//
// From long/ulong to float, double
//
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.char_type){
//
// From char to ushort, int, uint, long, ulong, float, double
//
- if ((target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type))
+ if ((real_target_type == TypeManager.ushort_type) ||
+ (real_target_type == TypeManager.int32_type) ||
+ (real_target_type == TypeManager.uint32_type))
return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.uint64_type)
+ if (real_target_type == TypeManager.uint64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int64_type)
+ if (real_target_type == TypeManager.int64_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.float_type)
+ if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.float_type){
//
// float to double
//
- if (target_type == TypeManager.double_type)
+ if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
}
/// Determines if a standard implicit conversion exists from
/// expr_type to target_type
/// </summary>
- public static bool StandardConversionExists (Type expr_type, Type target_type)
+ public static bool StandardConversionExists (Expression expr, Type target_type)
{
+ Type expr_type = expr.Type;
+
if (expr_type == target_type)
return true;
// First numeric conversions
-
+
if (expr_type == TypeManager.sbyte_type){
//
// From sbyte to short, int, long, float, double.
if (target_type == TypeManager.object_type) {
if ((expr_type.IsClass) ||
- (expr_type.IsValueType))
+ (expr_type.IsValueType) ||
+ (expr_type.IsInterface))
return true;
} else if (expr_type.IsSubclassOf (target_type)) {
return true;
} else {
+ // Please remember that all code below actually comes
+ // from ImplicitReferenceConversion so make sure code remains in sync
+
// from any class-type S to any interface-type T.
- if (expr_type.IsClass && target_type.IsInterface)
- return true;
+ if (expr_type.IsClass && target_type.IsInterface) {
+ if (TypeManager.ImplementsInterface (expr_type, target_type))
+ return true;
+ }
// from any interface type S to interface-type T.
// FIXME : Is it right to use IsAssignableFrom ?
if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
Type expr_element_type = expr_type.GetElementType ();
+
+ if (MyEmptyExpr == null)
+ MyEmptyExpr = new EmptyExpression ();
+
+ MyEmptyExpr.SetType (expr_element_type);
Type target_element_type = target_type.GetElementType ();
if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
- if (StandardConversionExists (expr_element_type,
+ if (StandardConversionExists (MyEmptyExpr,
target_element_type))
return true;
}
return true;
// from the null type to any reference-type.
- // FIXME : How do we do this ?
+ if (expr is NullLiteral && !target_type.IsValueType)
+ return true;
+
+ }
+
+ if (expr is IntConstant){
+ int value = ((IntConstant) expr).Value;
+
+ if (target_type == TypeManager.sbyte_type){
+ if (value >= SByte.MinValue && value <= SByte.MaxValue)
+ return true;
+ } else if (target_type == TypeManager.byte_type){
+ if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
+ return true;
+ } else if (target_type == TypeManager.short_type){
+ if (value >= Int16.MinValue && value <= Int16.MaxValue)
+ return true;
+ } else if (target_type == TypeManager.ushort_type){
+ if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
+ return true;
+ } else if (target_type == TypeManager.uint32_type){
+ if (value >= 0)
+ return true;
+ } else if (target_type == TypeManager.uint64_type){
+ //
+ // we can optimize this case: a positive int32
+ // always fits on a uint64. But we need an opcode
+ // to do it.
+ //
+ if (value >= 0)
+ return true;
+ }
+
+ if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
+ return true;
+ }
+ if (expr is LongConstant && target_type == TypeManager.uint64_type){
+ //
+ // Try the implicit constant expression conversion
+ // from long to ulong, instead of a nice routine,
+ // we just inline it
+ //
+ long v = ((LongConstant) expr).Value;
+ if (v > 0)
+ return true;
}
+
+ if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
+ IntLiteral i = (IntLiteral) expr;
+ if (i.Value == 0)
+ return true;
+ }
return false;
}
- static EmptyExpression MyEmptyExpr;
- /// <summary>
- /// Tells whether an implicit conversion exists from expr_type to
- /// target_type
- /// </summary>
- public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
- Location l)
- {
- if (MyEmptyExpr == null)
- MyEmptyExpr = new EmptyExpression (expr_type);
- else
- MyEmptyExpr.SetType (expr_type);
-
- return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
- }
+ //
+ // Used internally by FindMostEncompassedType, this is used
+ // to avoid creating lots of objects in the tight loop inside
+ // FindMostEncompassedType
+ //
+ static EmptyExpression priv_fmet_param;
/// <summary>
/// Finds "most encompassed type" according to the spec (13.4.2)
- /// amongst the methods in the MethodGroupExpr which convert from a
- /// type encompassing source_type
+ /// amongst the methods in the MethodGroupExpr
/// </summary>
- static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
+ static Type FindMostEncompassedType (ArrayList types)
{
Type best = null;
-
- for (int i = me.Methods.Length; i > 0; ) {
- i--;
- MethodBase mb = me.Methods [i];
- ParameterData pd = Invocation.GetParameterData (mb);
- Type param_type = pd.ParameterType (0);
+ if (priv_fmet_param == null)
+ priv_fmet_param = new EmptyExpression ();
- if (StandardConversionExists (source_type, param_type)) {
- if (best == null)
- best = param_type;
-
- if (StandardConversionExists (param_type, best))
- best = param_type;
+ foreach (Type t in types){
+ priv_fmet_param.SetType (t);
+
+ if (best == null) {
+ best = t;
+ continue;
}
+
+ if (StandardConversionExists (priv_fmet_param, best))
+ best = t;
}
return best;
}
+
+ //
+ // Used internally by FindMostEncompassingType, this is used
+ // to avoid creating lots of objects in the tight loop inside
+ // FindMostEncompassingType
+ //
+ static EmptyExpression priv_fmee_ret;
/// <summary>
/// Finds "most encompassing type" according to the spec (13.4.2)
- /// amongst the methods in the MethodGroupExpr which convert to a
- /// type encompassed by target_type
+ /// amongst the types in the given set
/// </summary>
- static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
+ static Type FindMostEncompassingType (ArrayList types)
{
Type best = null;
-
- for (int i = me.Methods.Length; i > 0; ) {
- i--;
-
- MethodInfo mi = (MethodInfo) me.Methods [i];
- Type ret_type = mi.ReturnType;
-
- if (StandardConversionExists (ret_type, target)) {
- if (best == null)
- best = ret_type;
- if (!StandardConversionExists (ret_type, best))
- best = ret_type;
+ if (priv_fmee_ret == null)
+ priv_fmee_ret = new EmptyExpression ();
+
+ foreach (Type t in types){
+ priv_fmee_ret.SetType (best);
+
+ if (best == null) {
+ best = t;
+ continue;
}
-
+
+ if (StandardConversionExists (priv_fmee_ret, t))
+ best = t;
}
return best;
-
- }
-
-
- /// <summary>
- /// User-defined Implicit conversions
- /// </summary>
- static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
- Type target, Location loc)
- {
- return UserDefinedConversion (ec, source, target, loc, false);
}
- /// <summary>
- /// User-defined Explicit conversions
- /// </summary>
- static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
- Type target, Location loc)
- {
- return UserDefinedConversion (ec, source, target, loc, true);
- }
+ //
+ // Used to avoid creating too many objects
+ //
+ static EmptyExpression priv_fms_expr;
/// <summary>
- /// User-defined conversions
+ /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
+ /// by making use of FindMostEncomp* methods. Applies the correct rules separately
+ /// for explicit and implicit conversion operators.
/// </summary>
- static public Expression UserDefinedConversion (EmitContext ec, Expression source,
- Type target, Location loc,
- bool look_for_explicit)
+ static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
+ bool apply_explicit_conv_rules,
+ Location loc)
{
- Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
- Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
- Expression e;
- MethodBase method = null;
- Type source_type = source.Type;
-
- string op_name;
+ ArrayList src_types_set = new ArrayList ();
- // If we have a boolean type, we need to check for the True operator
-
- // FIXME : How does the False operator come into the picture ?
- // FIXME : This doesn't look complete and very correct !
- if (target == TypeManager.bool_type)
- op_name = "op_True";
- else
- op_name = "op_Implicit";
-
- mg1 = MemberLookup (ec, source_type, op_name, false, loc);
-
- if (source_type.BaseType != null)
- mg2 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
+ if (priv_fms_expr == null)
+ priv_fms_expr = new EmptyExpression ();
- mg3 = MemberLookup (ec, target, op_name, false, loc);
-
- if (target.BaseType != null)
- mg4 = MemberLookup (ec, target.BaseType, op_name, false, loc);
-
- MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
- MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
-
- MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
+ //
+ // If any operator converts from S then Sx = S
+ //
+ foreach (MethodBase mb in me.Methods){
+ ParameterData pd = Invocation.GetParameterData (mb);
+ Type param_type = pd.ParameterType (0);
- MethodGroupExpr union4 = null;
+ if (param_type == source_type)
+ return param_type;
- if (look_for_explicit) {
-
- op_name = "op_Explicit";
-
- mg5 = MemberLookup (ec, source_type, op_name, false, loc);
-
- if (source_type.BaseType != null)
- mg6 = MemberLookup (ec, source_type.BaseType, op_name, false, loc);
-
- mg7 = MemberLookup (ec, target, op_name, false, loc);
-
- if (target.BaseType != null)
- mg8 = MemberLookup (ec, target.BaseType, op_name, false, loc);
-
- MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
- MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
-
- union4 = Invocation.MakeUnionSet (union5, union6);
+ if (apply_explicit_conv_rules) {
+ //
+ // From the spec :
+ // Find the set of applicable user-defined conversion operators, U. This set
+ // consists of the
+ // user-defined implicit or explicit conversion operators declared by
+ // the classes or structs in D that convert from a type encompassing
+ // or encompassed by S to a type encompassing or encompassed by T
+ //
+ priv_fms_expr.SetType (param_type);
+ if (StandardConversionExists (priv_fms_expr, source_type))
+ src_types_set.Add (param_type);
+ else {
+ priv_fms_expr.SetType (source_type);
+ if (StandardConversionExists (priv_fms_expr, param_type))
+ src_types_set.Add (param_type);
+ }
+ } else {
+ //
+ // Only if S is encompassed by param_type
+ //
+ priv_fms_expr.SetType (source_type);
+ if (StandardConversionExists (priv_fms_expr, param_type))
+ src_types_set.Add (param_type);
+ }
}
- MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
+ //
+ // Explicit Conv rules
+ //
+ if (apply_explicit_conv_rules) {
+ ArrayList candidate_set = new ArrayList ();
- if (union != null) {
+ foreach (Type param_type in src_types_set){
+ priv_fms_expr.SetType (source_type);
+
+ if (StandardConversionExists (priv_fms_expr, param_type))
+ candidate_set.Add (param_type);
+ }
- Type most_specific_source, most_specific_target;
+ if (candidate_set.Count != 0)
+ return FindMostEncompassedType (candidate_set);
+ }
- most_specific_source = FindMostEncompassedType (union, source_type);
- if (most_specific_source == null)
- return null;
+ //
+ // Final case
+ //
+ if (apply_explicit_conv_rules)
+ return FindMostEncompassingType (src_types_set);
+ else
+ return FindMostEncompassedType (src_types_set);
+ }
- most_specific_target = FindMostEncompassingType (union, target);
- if (most_specific_target == null)
- return null;
-
- int count = 0;
-
- for (int i = union.Methods.Length; i > 0;) {
- i--;
+ //
+ // Useful in avoiding proliferation of objects
+ //
+ static EmptyExpression priv_fmt_expr;
+
+ /// <summary>
+ /// Finds the most specific target Tx according to section 13.4.4
+ /// </summary>
+ static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
+ bool apply_explicit_conv_rules,
+ Location loc)
+ {
+ ArrayList tgt_types_set = new ArrayList ();
+
+ if (priv_fmt_expr == null)
+ priv_fmt_expr = new EmptyExpression ();
+
+ //
+ // If any operator converts to T then Tx = T
+ //
+ foreach (MethodInfo mi in me.Methods){
+ Type ret_type = mi.ReturnType;
- MethodBase mb = union.Methods [i];
- ParameterData pd = Invocation.GetParameterData (mb);
- MethodInfo mi = (MethodInfo) union.Methods [i];
+ if (ret_type == target)
+ return ret_type;
- if (pd.ParameterType (0) == most_specific_source &&
- mi.ReturnType == most_specific_target) {
- method = mb;
- count++;
+ if (apply_explicit_conv_rules) {
+ //
+ // From the spec :
+ // Find the set of applicable user-defined conversion operators, U.
+ //
+ // This set consists of the
+ // user-defined implicit or explicit conversion operators declared by
+ // the classes or structs in D that convert from a type encompassing
+ // or encompassed by S to a type encompassing or encompassed by T
+ //
+ priv_fms_expr.SetType (ret_type);
+ if (StandardConversionExists (priv_fms_expr, target))
+ tgt_types_set.Add (ret_type);
+ else {
+ priv_fms_expr.SetType (target);
+ if (StandardConversionExists (priv_fms_expr, ret_type))
+ tgt_types_set.Add (ret_type);
}
+ } else {
+ //
+ // Only if T is encompassed by param_type
+ //
+ priv_fms_expr.SetType (ret_type);
+ if (StandardConversionExists (priv_fms_expr, target))
+ tgt_types_set.Add (ret_type);
}
+ }
- if (method == null || count > 1) {
- Report.Error (-11, loc, "Ambiguous user defined conversion");
- return null;
+ //
+ // Explicit conv rules
+ //
+ if (apply_explicit_conv_rules) {
+ ArrayList candidate_set = new ArrayList ();
+
+ foreach (Type ret_type in tgt_types_set){
+ priv_fmt_expr.SetType (ret_type);
+
+ if (StandardConversionExists (priv_fmt_expr, target))
+ candidate_set.Add (ret_type);
}
-
- //
- // 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 = ConvertExplicitStandard (ec, source, most_specific_source, loc);
+
+ if (candidate_set.Count != 0)
+ return FindMostEncompassingType (candidate_set);
+ }
+
+ //
+ // Okay, final case !
+ //
+ if (apply_explicit_conv_rules)
+ return FindMostEncompassedType (tgt_types_set);
+ else
+ return FindMostEncompassingType (tgt_types_set);
+ }
+
+ /// <summary>
+ /// User-defined Implicit conversions
+ /// </summary>
+ static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
+ Type target, Location loc)
+ {
+ return UserDefinedConversion (ec, source, target, loc, false);
+ }
+
+ /// <summary>
+ /// User-defined Explicit conversions
+ /// </summary>
+ static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
+ Type target, Location loc)
+ {
+ return UserDefinedConversion (ec, source, target, loc, true);
+ }
+
+ /// <summary>
+ /// Computes the MethodGroup for the user-defined conversion
+ /// operators from source_type to target_type. `look_for_explicit'
+ /// controls whether we should also include the list of explicit
+ /// operators
+ /// </summary>
+ static MethodGroupExpr GetConversionOperators (EmitContext ec,
+ Type source_type, Type target_type,
+ Location loc, bool look_for_explicit)
+ {
+ Expression mg1 = null, mg2 = null;
+ Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
+ string op_name;
+
+ //
+ // FIXME : How does the False operator come into the picture ?
+ // This doesn't look complete and very correct !
+ //
+ if (target_type == TypeManager.bool_type && !look_for_explicit)
+ op_name = "op_True";
+ else
+ op_name = "op_Implicit";
+
+ MethodGroupExpr union3;
+
+ mg1 = MethodLookup (ec, source_type, op_name, loc);
+ if (source_type.BaseType != null)
+ mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
+
+ if (mg1 == null)
+ union3 = (MethodGroupExpr) mg2;
+ else if (mg2 == null)
+ union3 = (MethodGroupExpr) mg1;
+ else
+ union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
+
+ mg1 = MethodLookup (ec, target_type, op_name, loc);
+ if (mg1 != null){
+ if (union3 != null)
+ union3 = Invocation.MakeUnionSet (union3, mg1, loc);
else
- source = ConvertImplicitStandard (ec, source,
- most_specific_source, loc);
+ union3 = (MethodGroupExpr) mg1;
+ }
- if (source == null)
- return null;
+ if (target_type.BaseType != null)
+ mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
+
+ if (mg1 != null){
+ if (union3 != null)
+ union3 = Invocation.MakeUnionSet (union3, mg1, loc);
+ else
+ union3 = (MethodGroupExpr) mg1;
+ }
+
+ MethodGroupExpr union4 = null;
- e = new UserCast ((MethodInfo) method, source);
+ if (look_for_explicit) {
+ op_name = "op_Explicit";
+
+ mg5 = MemberLookup (ec, source_type, op_name, loc);
+ if (source_type.BaseType != null)
+ mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
- if (e.Type != target){
- if (!look_for_explicit)
- e = ConvertImplicitStandard (ec, e, target, loc);
- else
- e = ConvertExplicitStandard (ec, e, target, loc);
+ mg7 = MemberLookup (ec, target_type, op_name, loc);
+ if (target_type.BaseType != null)
+ mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
+
+ MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
+ MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
- return e;
- } else
- return e;
+ union4 = Invocation.MakeUnionSet (union5, union6, loc);
}
- return null;
+ return Invocation.MakeUnionSet (union3, union4, loc);
+ }
+
+ /// <summary>
+ /// User-defined conversions
+ /// </summary>
+ static public Expression UserDefinedConversion (EmitContext ec, Expression source,
+ Type target, Location loc,
+ bool look_for_explicit)
+ {
+ MethodGroupExpr union;
+ Type source_type = source.Type;
+ MethodBase method = null;
+
+ union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
+ if (union == null)
+ return null;
+
+ Type most_specific_source, most_specific_target;
+
+#if BLAH
+ foreach (MethodBase m in union.Methods){
+ Console.WriteLine ("Name: " + m.Name);
+ Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
+ }
+#endif
+
+ most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
+ if (most_specific_source == null)
+ return null;
+
+ most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
+ if (most_specific_target == null)
+ return null;
+
+ Console.WriteLine ("S:T" + most_specific_source + ":" + most_specific_target);
+ int count = 0;
+
+ foreach (MethodBase mb in union.Methods){
+ ParameterData pd = Invocation.GetParameterData (mb);
+ MethodInfo mi = (MethodInfo) mb;
+
+ if (pd.ParameterType (0) == most_specific_source &&
+ mi.ReturnType == most_specific_target) {
+ method = mb;
+ count++;
+ }
+ }
+
+ if (method == null || count > 1) {
+ Report.Error (-11, loc, "Ambiguous user defined conversion");
+ return null;
+ }
+
+ //
+ // 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 = ConvertExplicitStandard (ec, source, most_specific_source, loc);
+ else
+ source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
+
+ if (source == null)
+ return null;
+
+ Expression e;
+ e = new UserCast ((MethodInfo) method, source);
+ if (e.Type != target){
+ if (!look_for_explicit)
+ e = ConvertImplicitStandard (ec, e, target, loc);
+ else
+ e = ConvertExplicitStandard (ec, e, target, loc);
+ }
+ return e;
}
/// <summary>
if (target_type == null)
throw new Exception ("Target type is null");
- e = ImplicitNumericConversion (ec, expr, target_type, loc);
- if (e != null)
- return e;
-
- e = ImplicitReferenceConversion (expr, target_type);
+ e = ConvertImplicitStandard (ec, expr, target_type, loc);
if (e != null)
return e;
if (e != null)
return e;
- if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
- IntLiteral i = (IntLiteral) expr;
-
- if (i.Value == 0)
- return new EmptyCast (expr, target_type);
- }
-
return null;
}
if (i.Value == 0)
return new EmptyCast (expr, target_type);
}
+
+ if (ec.InUnsafe) {
+ if (expr_type.IsPointer){
+ if (target_type == TypeManager.void_ptr_type)
+ return new EmptyCast (expr, target_type);
+
+ //
+ // yep, comparing pointer types cant be done with
+ // t1 == t2, we have to compare their element types.
+ //
+ if (target_type.IsPointer){
+ if (target_type.GetElementType()==expr_type.GetElementType())
+ return expr;
+ }
+ }
+
+ if (target_type.IsPointer){
+ if (expr is NullLiteral)
+ return new EmptyCast (expr, target_type);
+ }
+ }
+
return null;
}
/// <summary>
- /// Attemps to perform an implict constant conversion of the IntLiteral
+ /// Attemps to perform an implict constant conversion of the IntConstant
/// into a different data type using casts (See Implicit Constant
/// Expression Conversions)
/// </summary>
- static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
+ static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
{
- int value = il.Value;
-
+ int value = ic.Value;
+
+ //
+ // FIXME: This could return constants instead of EmptyCasts
+ //
if (target_type == TypeManager.sbyte_type){
if (value >= SByte.MinValue && value <= SByte.MaxValue)
- return new EmptyCast (il, target_type);
+ return new SByteConstant ((sbyte) value);
} else if (target_type == TypeManager.byte_type){
if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
- return new EmptyCast (il, target_type);
+ return new ByteConstant ((byte) value);
} else if (target_type == TypeManager.short_type){
if (value >= Int16.MinValue && value <= Int16.MaxValue)
- return new EmptyCast (il, target_type);
+ return new ShortConstant ((short) value);
} else if (target_type == TypeManager.ushort_type){
if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
- return new EmptyCast (il, target_type);
+ return new UShortConstant ((ushort) value);
} else if (target_type == TypeManager.uint32_type){
- //
- // we can optimize this case: a positive int32
- // always fits on a uint32
- //
if (value >= 0)
- return new EmptyCast (il, target_type);
+ return new UIntConstant ((uint) value);
} else if (target_type == TypeManager.uint64_type){
//
// we can optimize this case: a positive int32
// to do it.
//
if (value >= 0)
- return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
+ return new ULongConstant ((ulong) value);
}
+
+ if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
+ return new EnumConstant (ic, target_type);
return null;
}
+ static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
+ {
+ string msg = "Cannot convert implicitly from `"+
+ TypeManager.CSharpName (source) + "' to `" +
+ TypeManager.CSharpName (target) + "'";
+
+ Error (29, loc, msg);
+ }
+
/// <summary>
/// Attemptes to implicityly convert `target' into `type', using
/// ConvertImplicit. If there is no implicit conversion, then
"float type, use F suffix to create a float literal");
}
- string msg = "Cannot convert implicitly from `"+
- TypeManager.CSharpName (source.Type) + "' to `" +
- TypeManager.CSharpName (target_type) + "'";
-
- Error (29, loc, msg);
+ Error_CannotConvertImplicit (loc, source.Type, target_type);
return null;
}
Type target_type)
{
Type expr_type = expr.Type;
-
+
+ //
+ // If we have an enumeration, extract the underlying type,
+ // use this during the comparission, but wrap around the original
+ // target_type
+ //
+ Type real_target_type = target_type;
+
+ if (TypeManager.IsEnumType (real_target_type))
+ real_target_type = TypeManager.EnumToUnderlying (real_target_type);
+
if (expr_type == TypeManager.sbyte_type){
//
// From sbyte to byte, ushort, uint, ulong, char
//
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
} else if (expr_type == TypeManager.byte_type){
//
// From byte to sbyte and char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
} else if (expr_type == TypeManager.short_type){
//
// From short to sbyte, byte, ushort, uint, ulong, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
} else if (expr_type == TypeManager.ushort_type){
//
// From ushort to sbyte, byte, short, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
} else if (expr_type == TypeManager.int32_type){
//
// From int to sbyte, byte, short, ushort, uint, ulong, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
} else if (expr_type == TypeManager.uint32_type){
//
// From uint to sbyte, byte, short, ushort, int, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
- if (target_type == TypeManager.int32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
+ if (real_target_type == TypeManager.int32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
} else if (expr_type == TypeManager.int64_type){
//
// From long to sbyte, byte, short, ushort, int, uint, ulong, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
- if (target_type == TypeManager.int32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
+ if (real_target_type == TypeManager.int32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
} else if (expr_type == TypeManager.uint64_type){
//
// From ulong to sbyte, byte, short, ushort, int, uint, long, char
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
- if (target_type == TypeManager.int32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
- if (target_type == TypeManager.int64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
+ if (real_target_type == TypeManager.int32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
+ if (real_target_type == TypeManager.int64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
} else if (expr_type == TypeManager.char_type){
//
// From char to sbyte, byte, short
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
} else if (expr_type == TypeManager.float_type){
//
// From float to sbyte, byte, short,
// ushort, int, uint, long, ulong, char
// or decimal
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
- if (target_type == TypeManager.int32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
- if (target_type == TypeManager.int64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
+ if (real_target_type == TypeManager.int32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
+ if (real_target_type == TypeManager.int64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.double_type){
//
// ushort, int, uint, long, ulong,
// char, float or decimal
//
- if (target_type == TypeManager.sbyte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
- if (target_type == TypeManager.byte_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
- if (target_type == TypeManager.short_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
- if (target_type == TypeManager.ushort_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
- if (target_type == TypeManager.int32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
- if (target_type == TypeManager.uint32_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
- if (target_type == TypeManager.int64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
- if (target_type == TypeManager.uint64_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
- if (target_type == TypeManager.char_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
- if (target_type == TypeManager.float_type)
- return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
- if (target_type == TypeManager.decimal_type)
+ if (real_target_type == TypeManager.sbyte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
+ if (real_target_type == TypeManager.byte_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
+ if (real_target_type == TypeManager.short_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
+ if (real_target_type == TypeManager.ushort_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
+ if (real_target_type == TypeManager.int32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
+ if (real_target_type == TypeManager.uint32_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
+ if (real_target_type == TypeManager.int64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
+ if (real_target_type == TypeManager.uint64_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
+ if (real_target_type == TypeManager.char_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
+ if (real_target_type == TypeManager.float_type)
+ return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
+ if (real_target_type == TypeManager.decimal_type)
return InternalTypeConstructor (ec, expr, target_type);
}
}
//
- // From any class type S to any interface T, provides S is not sealed
+ // From any class type S to any interface T, provided S is not sealed
// and provided S does not implement T.
//
if (target_type.IsInterface && !source_type.IsSealed &&
- !target_type.IsAssignableFrom (source_type))
+ !TypeManager.ImplementsInterface (source_type, target_type))
return true;
//
// sealed, or provided T implements S.
//
if (source_type.IsInterface &&
- (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
+ (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
return true;
-
+
+
// From an array type S with an element type Se to an array type T with an
// element type Te provided all the following are true:
// * S and T differe only in element type, in other words, S and T
{
Type source_type = source.Type;
bool target_is_value_type = target_type.IsValueType;
-
+
//
// From object to any reference type
//
// From any interface type S to any interface T provided S is not derived from T
//
if (source_type.IsInterface && target_type.IsInterface){
-
- Type [] ifaces = source_type.GetInterfaces ();
-
if (TypeManager.ImplementsInterface (source_type, target_type))
return null;
else
// and provided S does not implement T.
//
if (target_type.IsInterface && !source_type.IsSealed) {
-
if (TypeManager.ImplementsInterface (source_type, target_type))
return null;
else
// sealed, or provided T implements S.
//
if (source_type.IsInterface) {
-
- if (target_type.IsSealed)
- return null;
-
- if (TypeManager.ImplementsInterface (target_type, source_type))
+ if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
return new ClassCast (source, target_type);
else
return null;
//
// Enum types
//
- if (expr is EnumLiteral) {
- Expression e = ((EnumLiteral) expr).Child;
+ if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
+ Expression e;
+
+ //
+ // FIXME: Is there any reason we should have EnumConstant
+ // dealt with here instead of just using always the
+ // UnderlyingSystemType to wrap the type?
+ //
+ if (expr is EnumConstant)
+ e = ((EnumConstant) expr).Child;
+ else {
+ e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
+ }
- return ConvertImplicit (ec, e, target_type, loc);
+ Expression t = ConvertImplicit (ec, e, target_type, loc);
+ if (t != null)
+ return t;
+
+ return ConvertNumericExplicit (ec, e, target_type);
}
ne = ConvertReferenceExplicit (expr, target_type);
if (ne != null)
return ne;
+ if (ec.InUnsafe){
+ if (target_type.IsPointer){
+ if (expr_type.IsPointer)
+ return new EmptyCast (expr, target_type);
+
+ if (expr_type == TypeManager.sbyte_type ||
+ expr_type == TypeManager.byte_type ||
+ expr_type == TypeManager.short_type ||
+ expr_type == TypeManager.ushort_type ||
+ expr_type == TypeManager.int32_type ||
+ expr_type == TypeManager.uint32_type ||
+ expr_type == TypeManager.uint64_type ||
+ expr_type == TypeManager.int64_type)
+ return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
+ }
+ if (expr_type.IsPointer){
+ if (target_type == TypeManager.sbyte_type ||
+ target_type == TypeManager.byte_type ||
+ target_type == TypeManager.short_type ||
+ target_type == TypeManager.ushort_type ||
+ target_type == TypeManager.int32_type ||
+ target_type == TypeManager.uint32_type ||
+ target_type == TypeManager.uint64_type ||
+ target_type == TypeManager.int64_type){
+ Expression e = new EmptyCast (expr, TypeManager.uint32_type);
+ Expression ci, ce;
+
+ ci = ConvertImplicitStandard (ec, e, target_type, loc);
+
+ if (ci != null)
+ return ci;
+
+ ce = ConvertNumericExplicit (ec, e, target_type);
+ if (ce != null)
+ return ce;
+ //
+ // We should always be able to go from an uint32
+ // implicitly or explicitly to the other integral
+ // types
+ //
+ throw new Exception ("Internal compiler error");
+ }
+ }
+ }
+
ne = ExplicitUserConversion (ec, expr, target_type, loc);
if (ne != null)
return ne;
- error30 (loc, expr_type, target_type);
+ Error_CannotConvertType (loc, expr_type, target_type);
return null;
}
/// <summary>
- /// Same as ConverExplicit, only it doesn't include user defined conversions
+ /// Same as ConvertExplicit, only it doesn't include user defined conversions
/// </summary>
static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
Type target_type, Location l)
if (ne != null)
return ne;
- error30 (l, expr.Type, target_type);
+ Error_CannotConvertType (l, expr.Type, target_type);
return null;
}
string kind = "Unknown";
if (expr != null)
- kind = ExprClassName (expr.ExprClass);
+ kind = ExprClassName (expr.eclass);
Error (118, loc, "Expression denotes a `" + kind +
"' where a `" + expected + "' was expected");
}
- /// <summary>
- /// This function tries to reduce the expression performing
- /// constant folding and common subexpression elimination
- /// </summary>
- static public Expression Reduce (EmitContext ec, Expression e)
+ static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
{
- //Console.WriteLine ("Calling reduce");
- return e.Reduce (ec);
+ Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
+ TypeManager.CSharpName (t));
}
- static void error31 (Location l, string val, Type t)
+ public static void UnsafeError (Location loc)
{
- Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
- TypeManager.CSharpName (t));
+ Report.Error (214, loc, "Pointers may only be used in an unsafe context");
}
/// <summary>
- /// Converts the IntLiteral, UIntLiteral, LongLiteral or
- /// ULongLiteral into the integral target_type.
+ /// Converts the IntConstant, UIntConstant, LongConstant or
+ /// ULongConstant into the integral target_type. Notice
+ /// that we do not return an `Expression' we do return
+ /// a boxed integral type.
+ ///
+ /// FIXME: Since I added the new constants, we need to
+ /// also support conversions from CharConstant, ByteConstant,
+ /// SByteConstant, UShortConstant, ShortConstant
///
/// This is used by the switch statement, so the domain
/// of work is restricted to the literals above, and the
/// targets are int32, uint32, char, byte, sbyte, ushort,
/// short, uint64 and int64
/// </summary>
- public static Literal ConvertIntLiteral (Literal l, Type target_type, Location loc)
+ public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
{
string s = "";
- if (l.Type == target_type)
- return l;
+ if (c.Type == target_type)
+ return ((Constant) c).GetValue ();
//
// Make into one of the literals we handle, we dont really care
// about this value as we will just return a few limited types
//
- if (l is EnumLiteral)
- l = ((EnumLiteral)l).WidenToCompilerLiteral ();
+ if (c is EnumConstant)
+ c = ((EnumConstant)c).WidenToCompilerConstant ();
- if (l is IntLiteral){
- int v = ((IntLiteral) l).Value;
+ if (c is IntConstant){
+ int v = ((IntConstant) c).Value;
if (target_type == TypeManager.uint32_type){
if (v >= 0)
- return new UIntLiteral ((uint) v);
+ return (uint) v;
} else if (target_type == TypeManager.char_type){
if (v >= Char.MinValue && v <= Char.MaxValue)
- return l;
+ return (char) v;
} else if (target_type == TypeManager.byte_type){
if (v >= Byte.MinValue && v <= Byte.MaxValue)
- return l;
+ return (byte) v;
} else if (target_type == TypeManager.sbyte_type){
if (v >= SByte.MinValue && v <= SByte.MaxValue)
- return l;
+ return (sbyte) v;
} else if (target_type == TypeManager.short_type){
if (v >= Int16.MinValue && v <= UInt16.MaxValue)
- return l;
+ return (short) v;
} else if (target_type == TypeManager.ushort_type){
if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
- return l;
+ return (ushort) v;
} else if (target_type == TypeManager.int64_type)
- return new LongLiteral (v);
+ return (long) v;
else if (target_type == TypeManager.uint64_type){
if (v > 0)
- return new ULongLiteral ((ulong) v);
+ return (ulong) v;
}
s = v.ToString ();
- } else if (l is UIntLiteral){
- uint v = ((UIntLiteral) l).Value;
+ } else if (c is UIntConstant){
+ uint v = ((UIntConstant) c).Value;
if (target_type == TypeManager.int32_type){
if (v <= Int32.MaxValue)
- return new IntLiteral ((int) v);
+ return (int) v;
} else if (target_type == TypeManager.char_type){
if (v >= Char.MinValue && v <= Char.MaxValue)
- return l;
+ return (char) v;
} else if (target_type == TypeManager.byte_type){
if (v <= Byte.MaxValue)
- return l;
+ return (byte) v;
} else if (target_type == TypeManager.sbyte_type){
if (v <= SByte.MaxValue)
- return l;
+ return (sbyte) v;
} else if (target_type == TypeManager.short_type){
if (v <= UInt16.MaxValue)
- return l;
+ return (short) v;
} else if (target_type == TypeManager.ushort_type){
if (v <= UInt16.MaxValue)
- return l;
+ return (ushort) v;
} else if (target_type == TypeManager.int64_type)
- return new LongLiteral (v);
+ return (long) v;
else if (target_type == TypeManager.uint64_type)
- return new ULongLiteral (v);
+ return (ulong) v;
s = v.ToString ();
- } else if (l is LongLiteral){
- long v = ((LongLiteral) l).Value;
+ } else if (c is LongConstant){
+ long v = ((LongConstant) c).Value;
if (target_type == TypeManager.int32_type){
if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
- return new IntLiteral ((int) v);
+ return (int) v;
} else if (target_type == TypeManager.uint32_type){
if (v >= 0 && v <= UInt32.MaxValue)
- return new UIntLiteral ((uint) v);
+ return (uint) v;
} else if (target_type == TypeManager.char_type){
if (v >= Char.MinValue && v <= Char.MaxValue)
- return new IntLiteral ((int) v);
+ return (char) v;
} else if (target_type == TypeManager.byte_type){
if (v >= Byte.MinValue && v <= Byte.MaxValue)
- return new IntLiteral ((int) v);
+ return (byte) v;
} else if (target_type == TypeManager.sbyte_type){
if (v >= SByte.MinValue && v <= SByte.MaxValue)
- return new IntLiteral ((int) v);
+ return (sbyte) v;
} else if (target_type == TypeManager.short_type){
if (v >= Int16.MinValue && v <= UInt16.MaxValue)
- return new IntLiteral ((int) v);
+ return (short) v;
} else if (target_type == TypeManager.ushort_type){
if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
- return new IntLiteral ((int) v);
+ return (ushort) v;
} else if (target_type == TypeManager.uint64_type){
if (v > 0)
- return new ULongLiteral ((ulong) v);
+ return (ulong) v;
}
s = v.ToString ();
- } else if (l is ULongLiteral){
- ulong v = ((ULongLiteral) l).Value;
+ } else if (c is ULongConstant){
+ ulong v = ((ULongConstant) c).Value;
if (target_type == TypeManager.int32_type){
if (v <= Int32.MaxValue)
- return new IntLiteral ((int) v);
+ return (int) v;
} else if (target_type == TypeManager.uint32_type){
if (v <= UInt32.MaxValue)
- return new UIntLiteral ((uint) v);
+ return (uint) v;
} else if (target_type == TypeManager.char_type){
if (v >= Char.MinValue && v <= Char.MaxValue)
- return new IntLiteral ((int) v);
+ return (char) v;
} else if (target_type == TypeManager.byte_type){
if (v >= Byte.MinValue && v <= Byte.MaxValue)
- return new IntLiteral ((int) v);
+ return (byte) v;
} else if (target_type == TypeManager.sbyte_type){
if (v <= (int) SByte.MaxValue)
- return new IntLiteral ((int) v);
+ return (sbyte) v;
} else if (target_type == TypeManager.short_type){
if (v <= UInt16.MaxValue)
- return new IntLiteral ((int) v);
+ return (short) v;
} else if (target_type == TypeManager.ushort_type){
if (v <= UInt16.MaxValue)
- return new IntLiteral ((int) v);
+ return (ushort) v;
} else if (target_type == TypeManager.int64_type){
if (v <= Int64.MaxValue)
- return new LongLiteral ((long) v);
+ return (long) v;
}
s = v.ToString ();
- }
-
- error31 (loc, s, target_type);
+ } else if (c is ByteConstant){
+ byte v = ((ByteConstant) c).Value;
+
+ if (target_type == TypeManager.int32_type)
+ return (int) v;
+ else if (target_type == TypeManager.uint32_type)
+ return (uint) v;
+ else if (target_type == TypeManager.char_type)
+ return (char) v;
+ else if (target_type == TypeManager.sbyte_type){
+ if (v <= SByte.MaxValue)
+ return (sbyte) v;
+ } else if (target_type == TypeManager.short_type)
+ return (short) v;
+ else if (target_type == TypeManager.ushort_type)
+ return (ushort) v;
+ else if (target_type == TypeManager.int64_type)
+ return (long) v;
+ else if (target_type == TypeManager.uint64_type)
+ return (ulong) v;
+ s = v.ToString ();
+ } else if (c is SByteConstant){
+ sbyte v = ((SByteConstant) c).Value;
+
+ if (target_type == TypeManager.int32_type)
+ return (int) v;
+ else if (target_type == TypeManager.uint32_type){
+ if (v >= 0)
+ return (uint) v;
+ } else if (target_type == TypeManager.char_type){
+ if (v >= 0)
+ return (char) v;
+ } else if (target_type == TypeManager.byte_type){
+ if (v >= 0)
+ return (byte) v;
+ } else if (target_type == TypeManager.short_type)
+ return (short) v;
+ else if (target_type == TypeManager.ushort_type){
+ if (v >= 0)
+ return (ushort) v;
+ } else if (target_type == TypeManager.int64_type)
+ return (long) v;
+ else if (target_type == TypeManager.uint64_type){
+ if (v >= 0)
+ return (ulong) v;
+ }
+ s = v.ToString ();
+ } else if (c is ShortConstant){
+ short v = ((ShortConstant) c).Value;
+
+ if (target_type == TypeManager.int32_type){
+ return (int) v;
+ } else if (target_type == TypeManager.uint32_type){
+ if (v >= 0)
+ return (uint) v;
+ } else if (target_type == TypeManager.char_type){
+ if (v >= 0)
+ return (char) v;
+ } else if (target_type == TypeManager.byte_type){
+ if (v >= Byte.MinValue && v <= Byte.MaxValue)
+ return (byte) v;
+ } else if (target_type == TypeManager.sbyte_type){
+ if (v >= SByte.MinValue && v <= SByte.MaxValue)
+ return (sbyte) v;
+ } else if (target_type == TypeManager.ushort_type){
+ if (v >= 0)
+ return (ushort) v;
+ } else if (target_type == TypeManager.int64_type)
+ return (long) v;
+ else if (target_type == TypeManager.uint64_type)
+ return (ulong) v;
+
+ s = v.ToString ();
+ } else if (c is UShortConstant){
+ ushort v = ((UShortConstant) c).Value;
+
+ if (target_type == TypeManager.int32_type)
+ return (int) v;
+ else if (target_type == TypeManager.uint32_type)
+ return (uint) v;
+ else if (target_type == TypeManager.char_type){
+ if (v >= Char.MinValue && v <= Char.MaxValue)
+ return (char) v;
+ } else if (target_type == TypeManager.byte_type){
+ if (v >= Byte.MinValue && v <= Byte.MaxValue)
+ return (byte) v;
+ } else if (target_type == TypeManager.sbyte_type){
+ if (v <= SByte.MaxValue)
+ return (byte) v;
+ } else if (target_type == TypeManager.short_type){
+ if (v <= Int16.MaxValue)
+ return (short) v;
+ } else if (target_type == TypeManager.int64_type)
+ return (long) v;
+ else if (target_type == TypeManager.uint64_type)
+ return (ulong) v;
+
+ s = v.ToString ();
+ } else if (c is CharConstant){
+ char v = ((CharConstant) c).Value;
+
+ if (target_type == TypeManager.int32_type)
+ return (int) v;
+ else if (target_type == TypeManager.uint32_type)
+ return (uint) v;
+ else if (target_type == TypeManager.byte_type){
+ if (v >= Byte.MinValue && v <= Byte.MaxValue)
+ return (byte) v;
+ } else if (target_type == TypeManager.sbyte_type){
+ if (v <= SByte.MaxValue)
+ return (sbyte) v;
+ } else if (target_type == TypeManager.short_type){
+ if (v <= Int16.MaxValue)
+ return (short) v;
+ } else if (target_type == TypeManager.ushort_type)
+ return (short) v;
+ else if (target_type == TypeManager.int64_type)
+ return (long) v;
+ else if (target_type == TypeManager.uint64_type)
+ return (ulong) v;
+
+ s = v.ToString ();
+ }
+ Error_ConstantValueCannotBeConverted (loc, s, target_type);
return null;
}
-
+
+ //
+ // Load the object from the pointer. The `IsReference' is used
+ // to control whether we should use Ldind_Ref or LdObj if the
+ // value is not a `core' type.
+ //
+ // Maybe we should try to extract this infromation form the type?
+ // TODO: Maybe this is a bug. The reason we have this flag is because
+ // I had almost identical code in ParameterReference (for handling
+ // references) and in UnboxCast.
+ //
+ public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
+ {
+ if (t == TypeManager.int32_type)
+ ig.Emit (OpCodes.Ldind_I4);
+ else if (t == TypeManager.uint32_type)
+ ig.Emit (OpCodes.Ldind_U4);
+ else if (t == TypeManager.short_type)
+ ig.Emit (OpCodes.Ldind_I2);
+ else if (t == TypeManager.ushort_type)
+ ig.Emit (OpCodes.Ldind_U2);
+ else if (t == TypeManager.char_type)
+ ig.Emit (OpCodes.Ldind_U2);
+ else if (t == TypeManager.byte_type)
+ ig.Emit (OpCodes.Ldind_U1);
+ else if (t == TypeManager.sbyte_type)
+ ig.Emit (OpCodes.Ldind_I1);
+ else if (t == TypeManager.uint64_type)
+ ig.Emit (OpCodes.Ldind_I8);
+ else if (t == TypeManager.int64_type)
+ ig.Emit (OpCodes.Ldind_I8);
+ else if (t == TypeManager.float_type)
+ ig.Emit (OpCodes.Ldind_R4);
+ else if (t == TypeManager.double_type)
+ ig.Emit (OpCodes.Ldind_R8);
+ else if (t == TypeManager.bool_type)
+ ig.Emit (OpCodes.Ldind_I1);
+ else if (t == TypeManager.intptr_type)
+ ig.Emit (OpCodes.Ldind_I);
+ else if (TypeManager.IsEnumType (t)){
+ LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
+ } else {
+ if (IsReference)
+ ig.Emit (OpCodes.Ldind_Ref);
+ else
+ ig.Emit (OpCodes.Ldobj, t);
+ }
+ }
+
+ //
+ // The stack contains the pointer and the value of type `type'
+ //
+ public static void StoreFromPtr (ILGenerator ig, Type type)
+ {
+ if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
+ ig.Emit (OpCodes.Stind_I4);
+ else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
+ ig.Emit (OpCodes.Stind_I8);
+ else if (type == TypeManager.char_type || type == TypeManager.short_type ||
+ type == TypeManager.ushort_type)
+ ig.Emit (OpCodes.Stind_I2);
+ else if (type == TypeManager.float_type)
+ ig.Emit (OpCodes.Stind_R4);
+ else if (type == TypeManager.double_type)
+ ig.Emit (OpCodes.Stind_R8);
+ else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
+ type == TypeManager.bool_type)
+ ig.Emit (OpCodes.Stind_I1);
+ else if (type == TypeManager.intptr_type)
+ ig.Emit (OpCodes.Stind_I);
+ else
+ ig.Emit (OpCodes.Stind_Ref);
+ }
+
+ //
+ // Returns the size of type `t' if known, otherwise, 0
+ //
+ public static int GetTypeSize (Type t)
+ {
+ if (t == TypeManager.int32_type ||
+ t == TypeManager.uint32_type ||
+ t == TypeManager.float_type)
+ return 4;
+ else if (t == TypeManager.int64_type ||
+ t == TypeManager.uint64_type ||
+ t == TypeManager.double_type)
+ return 8;
+ else if (t == TypeManager.byte_type ||
+ t == TypeManager.sbyte_type ||
+ t == TypeManager.bool_type)
+ return 1;
+ else if (t == TypeManager.short_type ||
+ t == TypeManager.char_type ||
+ t == TypeManager.ushort_type)
+ return 2;
+ else
+ return 0;
+ }
}
/// <summary>
public EmptyCast (Expression child, Type return_type)
{
- ExprClass = child.ExprClass;
+ eclass = child.eclass;
type = return_type;
this.child = child;
}
{
child.Emit (ec);
}
-
}
/// <summary>
/// This class is used to wrap literals which belong inside Enums
/// </summary>
- public class EnumLiteral : Literal {
- public Expression Child;
+ public class EnumConstant : Constant {
+ public Constant Child;
- public EnumLiteral (Expression child, Type enum_type)
+ public EnumConstant (Constant child, Type enum_type)
{
- ExprClass = child.ExprClass;
+ eclass = child.eclass;
this.Child = child;
type = enum_type;
}
public override object GetValue ()
{
- return ((Literal) Child).GetValue ();
+ return Child.GetValue ();
}
//
// (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
// one of the internal compiler literals: Int/UInt/Long/ULong Literals.
//
- public Literal WidenToCompilerLiteral ()
+ public Constant WidenToCompilerConstant ()
{
- Type t = Child.Type.UnderlyingSystemType;
- object v = ((Literal) Child).GetValue ();;
+ Type t = TypeManager.EnumToUnderlying (Child.Type);
+ object v = ((Constant) Child).GetValue ();;
if (t == TypeManager.int32_type)
- return new IntLiteral ((int) v);
+ return new IntConstant ((int) v);
if (t == TypeManager.uint32_type)
- return new UIntLiteral ((uint) v);
+ return new UIntConstant ((uint) v);
if (t == TypeManager.int64_type)
- return new LongLiteral ((long) v);
+ return new LongConstant ((long) v);
if (t == TypeManager.uint64_type)
- return new ULongLiteral ((ulong) v);
+ return new ULongConstant ((ulong) v);
if (t == TypeManager.short_type)
- return new IntLiteral ((short) v);
+ return new ShortConstant ((short) v);
if (t == TypeManager.ushort_type)
- return new UIntLiteral ((ushort) v);
+ return new UShortConstant ((ushort) v);
if (t == TypeManager.byte_type)
- return new UIntLiteral ((byte) v);
+ return new ByteConstant ((byte) v);
if (t == TypeManager.sbyte_type)
- return new IntLiteral ((sbyte) v);
+ return new SByteConstant ((sbyte) v);
throw new Exception ("Invalid enumeration underlying type: " + t);
}
+ //
+ // Extracts the value in the enumeration on its native representation
+ //
+ public object GetPlainValue ()
+ {
+ Type t = TypeManager.EnumToUnderlying (Child.Type);
+ object v = ((Constant) Child).GetValue ();;
+
+ if (t == TypeManager.int32_type)
+ return (int) v;
+ if (t == TypeManager.uint32_type)
+ return (uint) v;
+ if (t == TypeManager.int64_type)
+ return (long) v;
+ if (t == TypeManager.uint64_type)
+ return (ulong) v;
+ if (t == TypeManager.short_type)
+ return (short) v;
+ if (t == TypeManager.ushort_type)
+ return (ushort) v;
+ if (t == TypeManager.byte_type)
+ return (byte) v;
+ if (t == TypeManager.sbyte_type)
+ return (sbyte) v;
+
+ return null;
+ }
+
public override string AsString ()
{
- return ((Literal) Child).AsString ();
+ return Child.AsString ();
+ }
+
+ public override DoubleConstant ConvertToDouble ()
+ {
+ return Child.ConvertToDouble ();
+ }
+
+ public override FloatConstant ConvertToFloat ()
+ {
+ return Child.ConvertToFloat ();
+ }
+
+ public override ULongConstant ConvertToULong ()
+ {
+ return Child.ConvertToULong ();
+ }
+
+ public override LongConstant ConvertToLong ()
+ {
+ return Child.ConvertToLong ();
+ }
+
+ public override UIntConstant ConvertToUInt ()
+ {
+ return Child.ConvertToUInt ();
+ }
+
+ public override IntConstant ConvertToInt ()
+ {
+ return Child.ConvertToInt ();
}
}
public override void Emit (EmitContext ec)
{
base.Emit (ec);
+
ec.ig.Emit (OpCodes.Box, child.Type);
}
}
base.Emit (ec);
ig.Emit (OpCodes.Unbox, t);
- //
- // Load the object from the pointer
- //
- if (t == TypeManager.int32_type)
- ig.Emit (OpCodes.Ldind_I4);
- else if (t == TypeManager.uint32_type)
- ig.Emit (OpCodes.Ldind_U4);
- else if (t == TypeManager.short_type)
- ig.Emit (OpCodes.Ldind_I2);
- else if (t == TypeManager.ushort_type)
- ig.Emit (OpCodes.Ldind_U2);
- else if (t == TypeManager.char_type)
- ig.Emit (OpCodes.Ldind_U2);
- else if (t == TypeManager.byte_type)
- ig.Emit (OpCodes.Ldind_U1);
- else if (t == TypeManager.sbyte_type)
- ig.Emit (OpCodes.Ldind_I1);
- else if (t == TypeManager.uint64_type)
- ig.Emit (OpCodes.Ldind_I8);
- else if (t == TypeManager.int64_type)
- ig.Emit (OpCodes.Ldind_I8);
- else if (t == TypeManager.float_type)
- ig.Emit (OpCodes.Ldind_R4);
- else if (t == TypeManager.double_type)
- ig.Emit (OpCodes.Ldind_R8);
- else if (t == TypeManager.bool_type)
- ig.Emit (OpCodes.Ldind_I1);
- else if (t == TypeManager.intptr_type)
- ig.Emit (OpCodes.Ldind_I);
- else
- ig.Emit (OpCodes.Ldobj, t);
+ LoadFromPtr (ig, t, false);
}
}
}
Mode mode;
+ bool checked_state;
- public ConvCast (Expression child, Type return_type, Mode m)
+ public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
: base (child, return_type)
{
mode = m;
+ checked_state = ec.CheckState;
}
public override Expression DoResolve (EmitContext ec)
base.Emit (ec);
- if (ec.CheckState){
+ if (checked_state){
switch (mode){
case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
Error120 (Location, Name);
return null;
}
+ } else if (e is EventExpr) {
+ if (!((EventExpr) e).IsStatic) {
+ Error120 (Location, Name);
+ return null;
+ }
}
return e;
}
- // <remarks>
- // 7.5.2: Simple Names.
- //
- // Local Variables and Parameters are handled at
- // parse time, so they never occur as SimpleNames.
- // </remarks>
public override Expression DoResolve (EmitContext ec)
{
- Expression e;
+ return SimpleNameResolve (ec, null, false);
+ }
+
+ public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ {
+ return SimpleNameResolve (ec, right_side, false);
+ }
+
+
+ public Expression DoResolveAllowStatic (EmitContext ec)
+ {
+ return SimpleNameResolve (ec, null, true);
+ }
+
+ /// <remarks>
+ /// 7.5.2: Simple Names.
+ ///
+ /// Local Variables and Parameters are handled at
+ /// parse time, so they never occur as SimpleNames.
+ ///
+ /// The `allow_static' flag is used by MemberAccess only
+ /// and it is used to inform us that it is ok for us to
+ /// avoid the static check, because MemberAccess might end
+ /// up resolving the Name as a Type name and the access as
+ /// a static type access.
+ ///
+ /// ie: Type Type; .... { Type.GetType (""); }
+ ///
+ /// Type is both an instance variable and a Type; Type.GetType
+ /// is the static method not an instance method of type.
+ /// </remarks>
+ Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
+ {
+ Expression e = null;
//
// Stage 1: Performed by the parser (binding to locals or parameters).
//
+ if (!ec.OnlyLookupTypes){
+ Block current_block = ec.CurrentBlock;
+ if (current_block != null && current_block.IsVariableDefined (Name)){
+ LocalVariableReference var;
+
+ var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
- //
- // Stage 2: Lookup members
- //
- e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, true, Location);
- if (e == null) {
+ if (right_side != null)
+ return var.ResolveLValue (ec, right_side);
+ else
+ return var.Resolve (ec);
+ }
+
+ //
+ // Stage 2: Lookup members
+ //
+
+ //
+ // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
+ // Hence we have two different cases
+ //
+
+ DeclSpace lookup_ds = ec.DeclSpace;
+ do {
+ if (lookup_ds.TypeBuilder == null)
+ break;
+
+ e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
+ if (e != null)
+ break;
+
+ //
+ // Classes/structs keep looking, enums break
+ //
+ if (lookup_ds is TypeContainer)
+ lookup_ds = ((TypeContainer) lookup_ds).Parent;
+ else
+ break;
+ } while (lookup_ds != null);
+
+ if (e == null && ec.ContainerType != null)
+ e = MemberLookup (ec, ec.ContainerType, Name, Location);
+ }
+
+ // Continuation of stage 2
+ if (e == null){
//
// Stage 3: Lookup symbol in the various namespaces.
- //
- DeclSpace ds = ec.TypeContainer;
+ //
+ DeclSpace ds = ec.DeclSpace;
Type t;
string alias_value;
if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
return new TypeExpr (t);
-
+
//
- // Stage 3 part b: Lookup up if we are an alias to a type
+ // Stage 2 part b: Lookup up if we are an alias to a type
// or a namespace.
//
// Since we are cheating: we only do the Alias lookup for
// namespaces if the name does not include any dots in it
//
-
+
if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
- // System.Console.WriteLine (Name + " --> " + alias_value);
+ // System.Console.WriteLine (Name + " --> " + alias_value);
if ((t = RootContext.LookupType (ds, alias_value, true, Location))
!= null)
return new TypeExpr (t);
-
- // we have alias value, but it isn't Type, so try if it's namespace
+
+ // we have alias value, but it isn't Type, so try if it's namespace
return new SimpleName (alias_value, Location);
}
-
+
// No match, maybe our parent can compose us
// into something meaningful.
- //
return this;
}
-
- // Step 2, continues here.
+
+ //
+ // Stage 2 continues here.
+ //
if (e is TypeExpr)
return e;
+ if (ec.OnlyLookupTypes)
+ return null;
+
if (e is FieldExpr){
FieldExpr fe = (FieldExpr) e;
+ FieldInfo fi = fe.FieldInfo;
+
+ if (fi.FieldType.IsPointer && !ec.InUnsafe){
+ UnsafeError (Location);
+ }
- if (!fe.FieldInfo.IsStatic){
- This t = new This (Location.Null);
+ if (ec.IsStatic){
+ if (!allow_static && !fi.IsStatic){
+ Error120 (Location, Name);
+ return null;
+ }
+ } else {
+ // If we are not in static code and this
+ // field is not static, set the instance to `this'.
- fe.InstanceExpression = t.DoResolve (ec);
+ if (!fi.IsStatic)
+ fe.InstanceExpression = ec.This;
}
- FieldInfo fi = fe.FieldInfo;
if (fi is FieldBuilder) {
Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
if (c != null) {
object o = c.LookupConstantValue (ec);
- Expression l = Literalize (o, fi.FieldType);
- l = l.Resolve (ec);
- return ((Literal) l);
+ object real_value = ((Constant)c.Expr).GetValue ();
+ return Constantify (real_value, fi.FieldType);
+ }
+ }
+
+ if (fi.IsLiteral) {
+ Type t = fi.FieldType;
+ Type decl_type = fi.DeclaringType;
+ object o;
+
+ if (fi is FieldBuilder)
+ o = TypeManager.GetValue ((FieldBuilder) fi);
+ else
+ o = fi.GetValue (fi);
+
+ if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
+ Expression enum_member = MemberLookup (
+ ec, decl_type, "value__", MemberTypes.Field,
+ AllBindingFlags, Location);
+
+ Enum en = TypeManager.LookupEnum (decl_type);
+
+ Constant c;
+ if (en != null)
+ c = Constantify (o, en.UnderlyingType);
+ else
+ c = Constantify (o, enum_member.Type);
+
+ return new EnumConstant (c, decl_type);
}
+
+ Expression exp = Constantify (o, t);
}
+
+ return e;
}
- if (ec.IsStatic)
+ if (e is EventExpr) {
+ //
+ // If the event is local to this class, we transform ourselves into
+ // a FieldExpr
+ //
+ EventExpr ee = (EventExpr) e;
+
+ Expression ml = MemberLookup (
+ ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
+ MemberTypes.Event, AllBindingFlags, Location);
+
+ if (ml != null) {
+ MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
+
+ if (mi == null) {
+ //
+ // If this happens, then we have an event with its own
+ // accessors and private field etc so there's no need
+ // to transform ourselves : we should instead flag an error
+ //
+ Assign.error70 (ee.EventInfo, Location);
+ return null;
+ }
+
+ ml = ExprClassFromMemberInfo (ec, mi, Location);
+
+ if (ml == null) {
+ Report.Error (-200, Location, "Internal error!!");
+ return null;
+ }
+
+ Expression instance_expr;
+
+ FieldInfo fi = ((FieldExpr) ml).FieldInfo;
+
+ if (fi.IsStatic)
+ instance_expr = null;
+ else
+ instance_expr = ec.This;
+
+ instance_expr = instance_expr.Resolve (ec);
+
+ if (instance_expr != null)
+ instance_expr = instance_expr.Resolve (ec);
+
+ return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
+ }
+ }
+
+
+ if (ec.IsStatic){
+ if (allow_static)
+ return e;
+
return MemberStaticCheck (e);
- else
+ } else
return e;
}
Error (103, Location, "The name `" + Name +
"' does not exist in the class `" +
- ec.TypeContainer.Name + "'");
+ ec.DeclSpace.Name + "'");
}
}
/// </summary>
public class MethodGroupExpr : Expression {
public MethodBase [] Methods;
+ Location loc;
Expression instance_expression = null;
- public MethodGroupExpr (MemberInfo [] mi)
+ public MethodGroupExpr (MemberInfo [] mi, Location l)
{
Methods = new MethodBase [mi.Length];
mi.CopyTo (Methods, 0);
eclass = ExprClass.MethodGroup;
+ type = TypeManager.object_type;
+ loc = l;
}
- public MethodGroupExpr (ArrayList l)
+ public MethodGroupExpr (ArrayList list, Location l)
{
- Methods = new MethodBase [l.Count];
+ Methods = new MethodBase [list.Count];
- l.CopyTo (Methods, 0);
+ try {
+ list.CopyTo (Methods, 0);
+ } catch {
+ foreach (MemberInfo m in list){
+ if (!(m is MethodBase)){
+ Console.WriteLine ("Name " + m.Name);
+ Console.WriteLine ("Found a: " + m.GetType ().FullName);
+ }
+ }
+ throw;
+ }
+ loc = l;
eclass = ExprClass.MethodGroup;
+ type = TypeManager.object_type;
}
//
return this;
}
+ public void ReportUsageError ()
+ {
+ Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
+ Methods [0].Name + "()' is referenced without parentheses");
+ }
+
override public void Emit (EmitContext ec)
{
- throw new Exception ("This should never be reached");
+ ReportUsageError ();
}
bool RemoveMethods (bool keep_static)
{
ArrayList smethods = new ArrayList ();
- int top = Methods.Length;
- int i;
-
- for (i = 0; i < top; i++){
- MethodBase mb = Methods [i];
+ foreach (MethodBase mb in Methods){
if (mb.IsStatic == keep_static)
smethods.Add (mb);
}
InstanceExpression = InstanceExpression.Resolve (ec);
if (InstanceExpression == null)
return null;
-
}
return this;
}
+ void Report_AssignToReadonly (bool is_instance)
+ {
+ string msg;
+
+ if (is_instance)
+ msg = "Readonly field can not be assigned outside " +
+ "of constructor or variable initializer";
+ else
+ msg = "A static readonly field can only be assigned in " +
+ "a static constructor";
+
+ Report.Error (is_instance ? 191 : 198, loc, msg);
+ }
+
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
Expression e = DoResolve (ec);
if (ec.IsConstructor)
return this;
- Report.Error (191, loc,
- "Readonly field can not be assigned outside " +
- "of constructor or variable initializer");
+ Report_AssignToReadonly (true);
return null;
}
override public void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
+ bool is_volatile = false;
+
+ if (FieldInfo is FieldBuilder){
+ FieldBase f = TypeManager.GetField (FieldInfo);
- if (FieldInfo.IsStatic)
+ if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+ is_volatile = true;
+
+ f.status |= Field.Status.USED;
+ }
+
+ if (FieldInfo.IsStatic){
+ if (is_volatile)
+ ig.Emit (OpCodes.Volatile);
+
ig.Emit (OpCodes.Ldsfld, FieldInfo);
- else {
- InstanceExpression.Emit (ec);
+ } else {
+ if (InstanceExpression.Type.IsValueType){
+ IMemoryLocation ml;
+ LocalTemporary tempo = null;
+
+ if (!(InstanceExpression is IMemoryLocation)){
+ tempo = new LocalTemporary (
+ ec, InstanceExpression.Type);
+
+ InstanceExpression.Emit (ec);
+ tempo.Store (ec);
+ ml = tempo;
+ } else
+ ml = (IMemoryLocation) InstanceExpression;
+
+ ml.AddressOf (ec, AddressOp.Load);
+ } else
+ InstanceExpression.Emit (ec);
+
+ if (is_volatile)
+ ig.Emit (OpCodes.Volatile);
ig.Emit (OpCodes.Ldfld, FieldInfo);
}
public void EmitAssign (EmitContext ec, Expression source)
{
- bool is_static = FieldInfo.IsStatic;
+ FieldAttributes fa = FieldInfo.Attributes;
+ bool is_static = (fa & FieldAttributes.Static) != 0;
+ bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
+ ILGenerator ig = ec.ig;
+ if (is_readonly && !ec.IsConstructor){
+ Report_AssignToReadonly (!is_static);
+ return;
+ }
+
if (!is_static){
Expression instance = InstanceExpression;
if (instance is IMemoryLocation){
IMemoryLocation ml = (IMemoryLocation) instance;
- ml.AddressOf (ec);
+ ml.AddressOf (ec, AddressOp.Store);
} else
- throw new Exception ("The " + instance + " of type " + Type+
- "represents a ValueType and does not " +
- "implement IMemoryLocation");
+ throw new Exception ("The " + instance + " of type " +
+ instance.Type +
+ " represents a ValueType and does " +
+ "not implement IMemoryLocation");
} else
instance.Emit (ec);
}
source.Emit (ec);
+
+ if (FieldInfo is FieldBuilder){
+ FieldBase f = TypeManager.GetField (FieldInfo);
+
+ if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+ ig.Emit (OpCodes.Volatile);
+ }
if (is_static)
- ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
- else {
- ec.ig.Emit (OpCodes.Stfld, FieldInfo);
+ ig.Emit (OpCodes.Stsfld, FieldInfo);
+ else
+ ig.Emit (OpCodes.Stfld, FieldInfo);
+
+ if (FieldInfo is FieldBuilder){
+ FieldBase f = TypeManager.GetField (FieldInfo);
+
+ f.status |= Field.Status.ASSIGNED;
}
}
- public void AddressOf (EmitContext ec)
+ public void AddressOf (EmitContext ec, AddressOp mode)
{
+ ILGenerator ig = ec.ig;
+
+ if (FieldInfo is FieldBuilder){
+ FieldBase f = TypeManager.GetField (FieldInfo);
+ if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+ ig.Emit (OpCodes.Volatile);
+ }
+
+ if (FieldInfo is FieldBuilder){
+ FieldBase f = TypeManager.GetField (FieldInfo);
+
+ if ((mode & AddressOp.Store) != 0)
+ f.status |= Field.Status.ASSIGNED;
+ if ((mode & AddressOp.Load) != 0)
+ f.status |= Field.Status.USED;
+ }
+
+ //
+ // Handle initonly fields specially: make a copy and then
+ // get the address of the copy.
+ //
+ if (FieldInfo.IsInitOnly){
+ LocalBuilder local;
+
+ Emit (ec);
+ local = ig.DeclareLocal (type);
+ ig.Emit (OpCodes.Stloc, local);
+ ig.Emit (OpCodes.Ldloca, local);
+ return;
+ }
+
if (FieldInfo.IsStatic)
- ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
+ ig.Emit (OpCodes.Ldsflda, FieldInfo);
else {
InstanceExpression.Emit (ec);
- ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
+ ig.Emit (OpCodes.Ldflda, FieldInfo);
}
}
}
public class PropertyExpr : ExpressionStatement, IAssignMethod {
public readonly PropertyInfo PropertyInfo;
public readonly bool IsStatic;
+ public bool IsBase;
MethodInfo [] Accessors;
Location loc;
Accessors = TypeManager.GetAccessors (pi);
if (Accessors != null)
- for (int i = 0; i < Accessors.Length; i++){
- if (Accessors [i] != null)
- if (Accessors [i].IsStatic)
+ foreach (MethodInfo mi in Accessors){
+ if (mi != null)
+ if (mi.IsStatic)
IsStatic = true;
}
else
override public void Emit (EmitContext ec)
{
- Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [0], null);
+ MethodInfo method = Accessors [0];
+
+ //
+ // Special case: length of single dimension array is turned into ldlen
+ //
+ if (method == TypeManager.int_array_get_length){
+ Type iet = instance_expr.Type;
+
+ if (iet.GetArrayRank () == 1){
+ instance_expr.Emit (ec);
+ ec.ig.Emit (OpCodes.Ldlen);
+ return;
+ }
+ }
+
+ Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
}
ArrayList args = new ArrayList ();
args.Add (arg);
- Invocation.EmitCall (ec, IsStatic, instance_expr, Accessors [1], args);
+ Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
}
override public void EmitStatement (EmitContext ec)
}
/// <summary>
- /// Fully resolved expression that evaluates to a Expression
+ /// Fully resolved expression that evaluates to an Event
/// </summary>
public class EventExpr : Expression {
public readonly EventInfo EventInfo;
public Expression InstanceExpression;
public readonly bool IsStatic;
+
+ MethodInfo add_accessor, remove_accessor;
public EventExpr (EventInfo ei, Location loc)
{
this.loc = loc;
eclass = ExprClass.EventAccess;
- MethodInfo add_accessor = TypeManager.GetAddMethod (ei);
- MethodInfo remove_accessor = TypeManager.GetRemoveMethod (ei);
+ add_accessor = TypeManager.GetAddMethod (ei);
+ remove_accessor = TypeManager.GetRemoveMethod (ei);
- if (add_accessor != null)
- if (add_accessor.IsStatic)
+ if (add_accessor.IsStatic || remove_accessor.IsStatic)
IsStatic = true;
- if (remove_accessor != null)
- if (remove_accessor.IsStatic)
- IsStatic = true;
+ if (EventInfo is MyEventBuilder)
+ type = ((MyEventBuilder) EventInfo).EventType;
+ else
+ type = EventInfo.EventHandlerType;
}
override public Expression DoResolve (EmitContext ec)
{
- // We are born in resolved state.
-
- Console.WriteLine ("Came here");
- type = EventInfo.EventHandlerType;
+ // We are born fully resolved
return this;
}
override public void Emit (EmitContext ec)
{
- throw new Exception ("Implement me");
- // FIXME: Implement.
+ throw new Exception ("Should not happen I think");
+ }
+
+ public void EmitAddOrRemove (EmitContext ec, Expression source)
+ {
+ Expression handler = ((Binary) source).Right;
+
+ Argument arg = new Argument (handler, Argument.AType.Expression);
+ ArrayList args = new ArrayList ();
+
+ args.Add (arg);
+
+ if (((Binary) source).Oper == Binary.Operator.Addition)
+ Invocation.EmitCall (
+ ec, false, IsStatic, InstanceExpression, add_accessor, args);
+ else
+ Invocation.EmitCall (
+ ec, false, IsStatic, InstanceExpression, remove_accessor, args);
}
}
}