else if (mi is FieldInfo)
return new FieldExpr ((FieldInfo) mi, loc);
else if (mi is PropertyInfo)
- return new PropertyExpr ((PropertyInfo) mi, loc);
+ return new PropertyExpr (ec, (PropertyInfo) mi, loc);
else if (mi is Type){
return new TypeExpr ((System.Type) mi, loc);
}
expr.Emit (null);
}
-
- if (target_type == TypeManager.object_type) {
+
+ //
+ // notice that it is possible to write "ValueType v = 1", the ValueType here
+ // is an abstract class, and not really a value type, so we apply the same rules.
+ //
+ if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
//
// A pointer type cannot be converted to object
//
if (expr_type.IsClass || expr_type.IsInterface)
return new EmptyCast (expr, target_type);
} else if (expr_type.IsSubclassOf (target_type)) {
+ //
+ // Special case: enumeration to System.Enum.
+ // System.Enum is not a value type, it is a class, so we need
+ // a boxing conversion
+ //
+ if (expr_type.IsEnum)
+ return new BoxedCast (expr);
+
return new EmptyCast (expr, target_type);
} else {
return null;
}
- /// <summary>
- /// Handles expressions like this: decimal d; d = 1;
- /// and changes them into: decimal d; d = new System.Decimal (1);
- /// </summary>
- static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
- {
- ArrayList args = new ArrayList ();
-
- args.Add (new Argument (expr, Argument.AType.Expression));
-
- Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
-
- return ne.Resolve (ec);
- }
-
/// <summary>
/// Implicit Numeric Conversions.
///
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
if (real_target_type == TypeManager.short_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- 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
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- 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
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- 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
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- 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
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- 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 (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R4);
- if (real_target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.int64_type){
//
// From long/ulong to float, double
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (real_target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.uint64_type){
//
// From ulong to float, double
if (real_target_type == TypeManager.float_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
OpCodes.Conv_R4);
- 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
return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
if (real_target_type == TypeManager.double_type)
return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (real_target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (ec, expr, target_type);
} else if (expr_type == TypeManager.float_type){
//
// float to double
} 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
}
}
- if (method == null || count > 1) {
- Report.Error (-11, loc, "Ambiguous user defined conversion");
+ if (method == null || count > 1)
return null;
- }
+
//
// This will do the conversion to the best match that we
//
if (value >= 0)
return new ULongConstant ((ulong) value);
- }
+ } else if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) value);
+ else if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) value);
if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
Type underlying = TypeManager.EnumToUnderlying (target_type);
"Double literal cannot be implicitly converted to " +
"float type, use F suffix to create a float literal");
}
-
+
Error_CannotConvertImplicit (loc, source.Type, target_type);
return null;
/// <summary>
/// Performs the explicit numeric conversions
/// </summary>
- static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
+ static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
{
Type expr_type = expr.Type;
if (TypeManager.IsEnumType (real_target_type))
real_target_type = TypeManager.EnumToUnderlying (real_target_type);
- if (StandardConversionExists (expr, real_target_type))
- return new EmptyCast (expr, target_type);
+ if (StandardConversionExists (expr, real_target_type)){
+ Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
+
+ if (real_target_type != target_type)
+ return new EmptyCast (ce, target_type);
+ return ce;
+ }
if (expr_type == TypeManager.sbyte_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){
//
// From double to byte, byte, short,
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);
}
// decimal is taken care of by the op_Explicit methods.
if (ne != null)
return ne;
- ne = ConvertNumericExplicit (ec, expr, target_type);
+ ne = ConvertNumericExplicit (ec, expr, target_type, loc);
if (ne != null)
return ne;
//
// Unboxing conversion.
//
- if (expr_type == TypeManager.object_type && target_type.IsValueType)
+ if (expr_type == TypeManager.object_type && target_type.IsValueType){
+ if (expr is NullLiteral){
+ Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
+ return null;
+ }
return new UnboxCast (expr, target_type);
+ }
//
// Enum types
if (t != null)
return t;
- return ConvertNumericExplicit (ec, e, target_type);
+ t = ConvertNumericExplicit (ec, e, target_type, loc);
+ if (t != null)
+ return t;
+
+ Error_CannotConvertType (loc, expr_type, target_type);
+ return null;
}
ne = ConvertReferenceExplicit (expr, target_type);
if (ci != null)
return ci;
- ce = ConvertNumericExplicit (ec, e, target_type);
+ ce = ConvertNumericExplicit (ec, e, target_type, loc);
if (ce != null)
return ce;
//
if (ne != null)
return ne;
- ne = ConvertNumericExplicit (ec, expr, target_type);
+ ne = ConvertNumericExplicit (ec, expr, target_type, l);
if (ne != null)
return ne;
// Stage 1: Performed by the parser (binding to locals or parameters).
//
Block current_block = ec.CurrentBlock;
- if (current_block != null && current_block.IsVariableDefined (Name)){
+ if (current_block != null && current_block.GetVariableInfo (Name) != null){
LocalVariableReference var;
var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
// Handle initonly fields specially: make a copy and then
// get the address of the copy.
//
- if (FieldInfo.IsInitOnly){
- if (ec.IsConstructor) {
- ig.Emit (OpCodes.Ldsflda, FieldInfo);
- } else {
- LocalBuilder local;
+ if (FieldInfo.IsInitOnly && !ec.IsConstructor){
+ LocalBuilder local;
- Emit (ec);
- local = ig.DeclareLocal (type);
- ig.Emit (OpCodes.Stloc, local);
- ig.Emit (OpCodes.Ldloca, local);
- }
+ Emit (ec);
+ local = ig.DeclareLocal (type);
+ ig.Emit (OpCodes.Stloc, local);
+ ig.Emit (OpCodes.Ldloca, local);
return;
}
/// </summary>
public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
public readonly PropertyInfo PropertyInfo;
+
+ //
+ // This is set externally by the `BaseAccess' class
+ //
public bool IsBase;
- MethodInfo [] Accessors;
+ MethodInfo getter, setter;
bool is_static;
Expression instance_expr;
- public PropertyExpr (PropertyInfo pi, Location l)
+ public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
{
PropertyInfo = pi;
eclass = ExprClass.PropertyAccess;
is_static = false;
loc = l;
- Accessors = TypeManager.GetAccessors (pi);
- if (Accessors != null)
- foreach (MethodInfo mi in Accessors){
- if (mi != null)
- if (mi.IsStatic)
- is_static = true;
- }
- else
- Accessors = new MethodInfo [2];
-
type = TypeManager.TypeToCoreType (pi.PropertyType);
+
+ ResolveAccessors (ec);
}
public string Name {
public bool VerifyAssignable ()
{
- if (!PropertyInfo.CanWrite){
+ if (setter == null) {
Report.Error (200, loc,
"The property `" + PropertyInfo.Name +
"' can not be assigned to, as it has not set accessor");
return true;
}
- override public Expression DoResolve (EmitContext ec)
+ //
+ // We also perform the permission checking here, as the PropertyInfo does not
+ // hold the information for the accessibility of its setter/getter
+ //
+ void ResolveAccessors (EmitContext ec)
{
- if (!PropertyInfo.CanRead){
- Report.Error (154, loc,
- "The property `" + PropertyInfo.Name +
- "' can not be used in " +
- "this context because it lacks a get accessor");
- return null;
+ BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
+ MemberInfo [] group;
+
+ group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+ MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
+
+ //
+ // The first method is the closest to us
+ //
+ if (group != null && group.Length > 0){
+ getter = (MethodInfo) group [0];
+
+ if (getter.IsStatic)
+ is_static = true;
+ }
+
+ //
+ // The first method is the closest to us
+ //
+ group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+ MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
+ if (group != null && group.Length > 0){
+ setter = (MethodInfo) group [0];
+ if (setter.IsStatic)
+ is_static = true;
}
+ if (setter == null && getter == null){
+ Error (122, "`" + PropertyInfo.Name + "' " +
+ "is inaccessible because of its protection level");
+
+ }
+ }
+
+ bool InstanceResolve (EmitContext ec)
+ {
if ((instance_expr == null) && ec.IsStatic && !is_static) {
SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
- return null;
+ return false;
}
if (instance_expr != null) {
instance_expr = instance_expr.DoResolve (ec);
if (instance_expr == null)
+ return false;
+ }
+
+ return true;
+ }
+
+ override public Expression DoResolve (EmitContext ec)
+ {
+ if (getter == null){
+ //
+ // The following condition happens if the PropertyExpr was
+ // created, but is invalid (ie, the property is inaccessible),
+ // and we did not want to embed the knowledge about this in
+ // the caller routine. This only avoids double error reporting.
+ //
+ if (setter == null)
return null;
+
+ Report.Error (154, loc,
+ "The property `" + PropertyInfo.Name +
+ "' can not be used in " +
+ "this context because it lacks a get accessor");
+ return null;
+ }
+
+ if (!InstanceResolve (ec))
+ return null;
+
+ //
+ // Only base will allow this invocation to happen.
+ //
+ if (IsBase && getter.IsAbstract){
+ Report.Error (205, loc, "Cannot call an abstract base property: " +
+ PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+ return null;
}
return this;
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
- if (!PropertyInfo.CanWrite){
+ if (setter == null){
+ //
+ // The following condition happens if the PropertyExpr was
+ // created, but is invalid (ie, the property is inaccessible),
+ // and we did not want to embed the knowledge about this in
+ // the caller routine. This only avoids double error reporting.
+ //
+ if (getter == null)
+ return null;
+
Report.Error (154, loc,
"The property `" + PropertyInfo.Name +
"' can not be used in " +
return null;
}
- if (instance_expr != null) {
- instance_expr = instance_expr.DoResolve (ec);
- if (instance_expr == null)
- return null;
+ if (!InstanceResolve (ec))
+ return null;
+
+ //
+ // Only base will allow this invocation to happen.
+ //
+ if (IsBase && setter.IsAbstract){
+ Report.Error (205, loc, "Cannot call an abstract base property: " +
+ PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+ return null;
}
-
return this;
}
override public void Emit (EmitContext ec)
{
- MethodInfo method = Accessors [0];
-
//
- // Special case: length of single dimension array is turned into ldlen
+ // Special case: length of single dimension array property is turned into ldlen
//
- if ((method == TypeManager.system_int_array_get_length) ||
- (method == TypeManager.int_array_get_length)){
+ if ((getter == TypeManager.system_int_array_get_length) ||
+ (getter == TypeManager.int_array_get_length)){
Type iet = instance_expr.Type;
//
}
}
- Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
+ Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
}
ArrayList args = new ArrayList ();
args.Add (arg);
- Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
+ Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
}
override public void EmitStatement (EmitContext ec)
if (add_accessor.IsStatic || remove_accessor.IsStatic)
is_static = true;
- if (EventInfo is MyEventBuilder)
- type = ((MyEventBuilder) EventInfo).EventType;
- else
+ if (EventInfo is MyEventBuilder){
+ MyEventBuilder eb = (MyEventBuilder) EventInfo;
+ type = eb.EventType;
+ eb.SetUsed ();
+ } else
type = EventInfo.EventHandlerType;
}
return null;
}
+
return this;
}
public override void Emit (EmitContext ec)
{
- throw new Exception ("Should not happen I think");
+ Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
}
public void EmitAddOrRemove (EmitContext ec, Expression source)