}
}
- public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
if (Oper == Operator.LogicalNot)
- Expr.EmitBranchable (ec, target, !onTrue);
+ Expr.EmitBranchable (ec, target, !on_true);
else
- base.EmitBranchable (ec, target, onTrue);
+ base.EmitBranchable (ec, target, on_true);
}
public override string ToString ()
if (expr == null)
return null;
} else {
- if (expr.eclass == ExprClass.Value) {
- Error_ValueAssignment (loc);
- } else {
- expr.Error_UnexpectedKind (ec.DeclContainer, "variable, indexer or property access", loc);
- }
+ Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
return null;
}
if (expr == null)
return null;
- if (expr.Type.IsPointer) {
- Report.Error (244, loc, "\"is\" or \"as\" are not valid on pointer types");
+ if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
+ Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
+ OperatorName);
+ return null;
+ }
+
+ if (expr.Type == TypeManager.anonymous_method_type) {
+ Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
+ OperatorName);
return null;
}
+
return this;
}
+ protected abstract string OperatorName { get; }
+
protected override void CloneTo (CloneContext clonectx, Expression t)
{
Probe target = (Probe) t;
: base (expr, probe_type, l)
{
}
-
- enum Action {
- AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
- }
-
- Action action;
public override void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
expr.Emit (ec);
-
- switch (action){
- case Action.AlwaysFalse:
- ig.Emit (OpCodes.Pop);
- IntConstant.EmitInt (ig, 0);
- return;
- case Action.AlwaysTrue:
- ig.Emit (OpCodes.Pop);
- IntConstant.EmitInt (ig, 1);
- return;
- case Action.LeaveOnStack:
- // the `e != null' rule.
- ig.Emit (OpCodes.Ldnull);
- ig.Emit (OpCodes.Ceq);
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
- return;
- case Action.Probe:
- ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
- ig.Emit (OpCodes.Ldnull);
- ig.Emit (OpCodes.Cgt_Un);
- return;
- }
- throw new Exception ("never reached");
+ ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
+ ig.Emit (OpCodes.Ldnull);
+ ig.Emit (OpCodes.Cgt_Un);
}
- public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
ILGenerator ig = ec.ig;
- switch (action){
- case Action.AlwaysFalse:
- if (! onTrue)
- ig.Emit (OpCodes.Br, target);
-
- return;
- case Action.AlwaysTrue:
- if (onTrue)
- ig.Emit (OpCodes.Br, target);
-
- return;
- case Action.LeaveOnStack:
- // the `e != null' rule.
- expr.Emit (ec);
- ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
- return;
- case Action.Probe:
- expr.Emit (ec);
- ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
- ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
- return;
- }
- throw new Exception ("never reached");
+ expr.Emit (ec);
+ ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
+ ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
}
- public override Expression DoResolve (EmitContext ec)
+ Expression CreateConstantResult (bool result)
{
- Expression e = base.DoResolve (ec);
+ if (result)
+ Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
+ TypeManager.CSharpName (probe_type_expr.Type));
+ else
+ Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
+ TypeManager.CSharpName (probe_type_expr.Type));
- if ((e == null) || (expr == null))
+ return new BoolConstant (result, loc);
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ if (base.DoResolve (ec) == null)
return null;
- Type etype = expr.Type;
+ Type d = expr.Type;
+ bool d_is_nullable = false;
+
+ if (expr is Constant) {
+ //
+ // If E is a method group or the null literal, of if the type of E is a reference
+ // type or a nullable type and the value of E is null, the result is false
+ //
+ if (((Constant) expr).GetValue () == null)
+ return CreateConstantResult (false);
+ } else if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
+ d = TypeManager.GetTypeArguments (d) [0];
+ d_is_nullable = true;
+ }
+
type = TypeManager.bool_type;
eclass = ExprClass.Value;
+ Type t = probe_type_expr.Type;
+ bool t_is_nullable = false;
+ if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
+ t = TypeManager.GetTypeArguments (t) [0];
+ t_is_nullable = true;
+ }
- //
- // First case, if at compile time, there is an implicit conversion
- // then e != null (objects) or true (value types)
- //
- Type probe_type = probe_type_expr.Type;
- e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
- if (e != null){
- expr = e;
- if (etype.IsValueType)
- action = Action.AlwaysTrue;
- else
- action = Action.LeaveOnStack;
-
- Constant c = e as Constant;
- if (c != null && c.GetValue () == null) {
- action = Action.AlwaysFalse;
- Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
- TypeManager.CSharpName (probe_type));
- } else if (etype.IsValueType) {
- Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
- TypeManager.CSharpName (probe_type));
+ if (t.IsValueType) {
+ if (d == t) {
+ //
+ // D and T are the same value types but D can be null
+ //
+ if (d_is_nullable && !t_is_nullable)
+ return Nullable.HasValue.Create (expr, ec);
+
+ //
+ // The result is true if D and T are the same value types
+ //
+ return CreateConstantResult (true);
}
- return this;
- }
-
- if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
- if (TypeManager.IsGenericParameter (etype))
- expr = new BoxedCast (expr, etype);
+
+ if (TypeManager.IsGenericParameter (d))
+ return ResolveGenericParameter (t, d);
//
- // Second case: explicit reference convresion
+ // An unboxing conversion exists
//
- if (expr is NullLiteral)
- action = Action.AlwaysFalse;
- else
- action = Action.Probe;
- } else if (TypeManager.ContainsGenericParameters (etype) ||
- TypeManager.ContainsGenericParameters (probe_type)) {
- expr = new BoxedCast (expr, etype);
- action = Action.Probe;
+ if (Convert.ExplicitReferenceConversionExists (d, t))
+ return this;
} else {
- action = Action.AlwaysFalse;
- if (!(probe_type.IsInterface || expr.Type.IsInterface))
- Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type", TypeManager.CSharpName (probe_type));
+ if (TypeManager.IsGenericParameter (t))
+ return ResolveGenericParameter (d, t);
+
+ if (d.IsValueType) {
+ bool temp;
+ if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
+ return CreateConstantResult (true);
+ } else {
+ if (TypeManager.IsGenericParameter (d))
+ return ResolveGenericParameter (t, d);
+
+ if (TypeManager.ContainsGenericParameters (d))
+ return this;
+
+ if (Convert.ImplicitReferenceConversionExists (expr, t) ||
+ Convert.ExplicitReferenceConversionExists (d, t)) {
+ return this;
+ }
+ }
}
+ return CreateConstantResult (false);
+ }
+
+ Expression ResolveGenericParameter (Type d, Type t)
+ {
+#if GMCS_SOURCE
+ GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
+ if (constraints != null) {
+ if (constraints.IsReferenceType && d.IsValueType)
+ return CreateConstantResult (false);
+
+ if (constraints.IsValueType && !d.IsValueType)
+ return CreateConstantResult (false);
+ }
+
+ expr = new BoxedCast (expr, d);
return this;
+#else
+ return null;
+#endif
+ }
+
+ protected override string OperatorName {
+ get { return "is"; }
}
}
Error_CannotConvertType (etype, type, loc);
return null;
}
+
+ protected override string OperatorName {
+ get { return "as"; }
+ }
- public override bool GetAttributableValue (Type valueType, out object value)
+ public override bool GetAttributableValue (Type value_type, out object value)
{
- return expr.GetAttributableValue (valueType, out value);
+ return expr.GetAttributableValue (value_type, out value);
}
}
target.expr = expr.Clone (clonectx);
}
}
+
+ //
+ // C# 2.0 Default value expression
+ //
+ public class DefaultValueExpression : Expression
+ {
+ Expression expr;
+
+ public DefaultValueExpression (Expression expr, Location loc)
+ {
+ this.expr = expr;
+ this.loc = loc;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
+ if (texpr == null)
+ return null;
+
+ type = texpr.Type;
+
+ if (type == TypeManager.void_type) {
+ Error_VoidInvalidInTheContext (loc);
+ return null;
+ }
+
+ if (TypeManager.IsGenericParameter (type))
+ {
+ GenericConstraints constraints = TypeManager.GetTypeParameterConstraints(type);
+ if (constraints != null && constraints.IsReferenceType)
+ return new NullDefault (new NullLiteral (Location), type);
+ }
+ else
+ {
+ Constant c = New.Constantify(type);
+ if (c != null)
+ return new NullDefault (c, type);
+
+ if (!TypeManager.IsValueType (type))
+ return new NullDefault (new NullLiteral (Location), type);
+ }
+ eclass = ExprClass.Variable;
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ LocalTemporary temp_storage = new LocalTemporary(type);
+
+ temp_storage.AddressOf(ec, AddressOp.LoadStore);
+ ec.ig.Emit(OpCodes.Initobj, type);
+ temp_storage.Emit(ec);
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Expression t)
+ {
+ DefaultValueExpression target = (DefaultValueExpression) t;
+
+ target.expr = expr.Clone (clonectx);
+ }
+ }
/// <summary>
/// Binary operators
}
readonly Operator oper;
- Expression left, right;
+ protected Expression left, right;
+ readonly bool is_compound;
// This must be kept in sync with Operator!!!
public static readonly string [] oper_names;
oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
}
+ public Binary (Operator oper, Expression left, Expression right, bool isCompound)
+ : this (oper, left, right)
+ {
+ this.is_compound = isCompound;
+ }
+
public Binary (Operator oper, Expression left, Expression right)
{
this.oper = oper;
/// <summary>
/// Returns a stringified representation of the Operator
/// </summary>
- public static string OperName (Operator oper)
+ string OperName (Operator oper)
{
+ string s;
switch (oper){
case Operator.Multiply:
- return "*";
+ s = "*";
+ break;
case Operator.Division:
- return "/";
+ s = "/";
+ break;
case Operator.Modulus:
- return "%";
+ s = "%";
+ break;
case Operator.Addition:
- return "+";
+ s = "+";
+ break;
case Operator.Subtraction:
- return "-";
+ s = "-";
+ break;
case Operator.LeftShift:
- return "<<";
+ s = "<<";
+ break;
case Operator.RightShift:
- return ">>";
+ s = ">>";
+ break;
case Operator.LessThan:
- return "<";
+ s = "<";
+ break;
case Operator.GreaterThan:
- return ">";
+ s = ">";
+ break;
case Operator.LessThanOrEqual:
- return "<=";
+ s = "<=";
+ break;
case Operator.GreaterThanOrEqual:
- return ">=";
+ s = ">=";
+ break;
case Operator.Equality:
- return "==";
+ s = "==";
+ break;
case Operator.Inequality:
- return "!=";
+ s = "!=";
+ break;
case Operator.BitwiseAnd:
- return "&";
+ s = "&";
+ break;
case Operator.BitwiseOr:
- return "|";
+ s = "|";
+ break;
case Operator.ExclusiveOr:
- return "^";
+ s = "^";
+ break;
case Operator.LogicalOr:
- return "||";
+ s = "||";
+ break;
case Operator.LogicalAnd:
- return "&&";
+ s = "&&";
+ break;
+ default:
+ s = oper.ToString ();
+ break;
}
- return oper.ToString ();
+ if (is_compound)
+ return s + "=";
+
+ return s;
}
public override string ToString ()
return Convert.ImplicitConversion (ec, expr, target_type, loc);
}
- public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
+ void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
{
Report.Error (
34, loc, "Operator `" + OperName (oper)
{
if (!IsApplicable_String (ec, left, right, oper))
return false;
+
+ Type l = left.Type;
+ Type r = right.Type;
+ if (OverloadResolve_PredefinedIntegral (ec) ||
+ OverloadResolve_PredefinedFloating (ec)) {
+ Error_OperatorAmbiguous (loc, oper, l, r);
+ }
+
Type t = TypeManager.string_type;
if (Convert.ImplicitConversionExists (ec, left, t))
left = ForceConversion (ec, left, t);
name, left, right);
}
- void Error_OperatorCannotBeApplied ()
+ protected void Error_OperatorCannotBeApplied ()
{
Error_OperatorCannotBeApplied (Location, OperName (oper), TypeManager.CSharpName (left.Type),
TypeManager.CSharpName(right.Type));
}
- static bool is_unsigned (Type t)
+ static bool IsUnsigned (Type t)
{
if (t.IsPointer)
- return is_unsigned (t.GetElementType ());
+ return IsUnsigned (t.GetElementType ());
return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
t == TypeManager.short_type || t == TypeManager.byte_type);
}
//
- // Step 0: String concatenation (because overloading will get this wrong)
+ // String concatenation
+ //
+ // string operator + (string x, string y);
+ // string operator + (string x, object y);
+ // string operator + (object x, string y);
//
- if (oper == Operator.Addition){
- //
- // If any of the arguments is a string, cast to string
+ if (oper == Operator.Addition && !TypeManager.IsDelegateType (l)) {
+ //
+ // Either left or right expression is implicitly convertible to string
//
-
- // Simple constant folding
- if (left is StringConstant && right is StringConstant)
- return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value, left.Location);
-
- if (l == TypeManager.string_type || r == TypeManager.string_type) {
-
+ if (OverloadResolve_PredefinedString (ec, oper)) {
if (r == TypeManager.void_type || l == TypeManager.void_type) {
Error_OperatorCannotBeApplied ();
return null;
}
- // try to fold it in on the left
- if (left is StringConcat) {
+ //
+ // Constants folding for strings and nulls
+ //
+ if (left.Type == TypeManager.string_type && right.Type == TypeManager.string_type &&
+ left is Constant && right is Constant) {
+ string lvalue = (string)((Constant) left).GetValue ();
+ string rvalue = (string)((Constant) right).GetValue ();
+ return new StringConstant (lvalue + rvalue, left.Location);
+ }
- //
- // We have to test here for not-null, since we can be doubly-resolved
- // take care of not appending twice
- //
- if (type == null){
- type = TypeManager.string_type;
- ((StringConcat) left).Append (ec, right);
- return left.Resolve (ec);
- } else {
- return left;
- }
+ //
+ // Append to existing string concatenation
+ //
+ if (left is StringConcat) {
+ ((StringConcat) left).Append (ec, right);
+ return left;
}
- // Otherwise, start a new concat expression
+ //
+ // Otherwise, start a new concat expression using converted expression
+ //
return new StringConcat (ec, loc, left, right).Resolve (ec);
}
break;
return left;
}
- Error_OperatorCannotBeApplied (loc, Binary.OperName (oper), left.Type, right.Type);
+ Error_OperatorCannotBeApplied ();
return null;
}
/// The expression's code is generated, and we will generate a branch to `target'
/// if the resulting expression value is equal to isTrue
/// </remarks>
- public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
ILGenerator ig = ec.ig;
// if we are comparing against null
//
if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
- bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
+ bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
//
// put the constant on the rhs, for simplicity
} else if (oper == Operator.LogicalAnd) {
- if (onTrue) {
+ if (on_true) {
Label tests_end = ig.DefineLabel ();
left.EmitBranchable (ec, tests_end, false);
return;
} else if (oper == Operator.LogicalOr){
- if (onTrue) {
+ if (on_true) {
left.EmitBranchable (ec, target, true);
right.EmitBranchable (ec, target, true);
} else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
oper == Operator.Equality || oper == Operator.Inequality)) {
- base.EmitBranchable (ec, target, onTrue);
+ base.EmitBranchable (ec, target, on_true);
return;
}
right.Emit (ec);
Type t = left.Type;
- bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
+ bool is_unsigned = IsUnsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
switch (oper){
case Operator.Equality:
- if (onTrue)
+ if (on_true)
ig.Emit (OpCodes.Beq, target);
else
ig.Emit (OpCodes.Bne_Un, target);
break;
case Operator.Inequality:
- if (onTrue)
+ if (on_true)
ig.Emit (OpCodes.Bne_Un, target);
else
ig.Emit (OpCodes.Beq, target);
break;
case Operator.LessThan:
- if (onTrue)
- if (isUnsigned)
+ if (on_true)
+ if (is_unsigned)
ig.Emit (OpCodes.Blt_Un, target);
else
ig.Emit (OpCodes.Blt, target);
else
- if (isUnsigned)
+ if (is_unsigned)
ig.Emit (OpCodes.Bge_Un, target);
else
ig.Emit (OpCodes.Bge, target);
break;
case Operator.GreaterThan:
- if (onTrue)
- if (isUnsigned)
+ if (on_true)
+ if (is_unsigned)
ig.Emit (OpCodes.Bgt_Un, target);
else
ig.Emit (OpCodes.Bgt, target);
else
- if (isUnsigned)
+ if (is_unsigned)
ig.Emit (OpCodes.Ble_Un, target);
else
ig.Emit (OpCodes.Ble, target);
break;
case Operator.LessThanOrEqual:
- if (onTrue)
- if (isUnsigned)
+ if (on_true)
+ if (is_unsigned)
ig.Emit (OpCodes.Ble_Un, target);
else
ig.Emit (OpCodes.Ble, target);
else
- if (isUnsigned)
+ if (is_unsigned)
ig.Emit (OpCodes.Bgt_Un, target);
else
ig.Emit (OpCodes.Bgt, target);
case Operator.GreaterThanOrEqual:
- if (onTrue)
- if (isUnsigned)
+ if (on_true)
+ if (is_unsigned)
ig.Emit (OpCodes.Bge_Un, target);
else
ig.Emit (OpCodes.Bge, target);
else
- if (isUnsigned)
+ if (is_unsigned)
ig.Emit (OpCodes.Blt_Un, target);
else
ig.Emit (OpCodes.Blt, target);
left.Emit (ec);
right.Emit (ec);
- bool isUnsigned = is_unsigned (left.Type);
+ bool is_unsigned = IsUnsigned (left.Type);
switch (oper){
case Operator.Multiply:
if (ec.CheckState){
if (l == TypeManager.int32_type || l == TypeManager.int64_type)
opcode = OpCodes.Mul_Ovf;
- else if (isUnsigned)
+ else if (is_unsigned)
opcode = OpCodes.Mul_Ovf_Un;
else
opcode = OpCodes.Mul;
break;
case Operator.Division:
- if (isUnsigned)
+ if (is_unsigned)
opcode = OpCodes.Div_Un;
else
opcode = OpCodes.Div;
break;
case Operator.Modulus:
- if (isUnsigned)
+ if (is_unsigned)
opcode = OpCodes.Rem_Un;
else
opcode = OpCodes.Rem;
if (ec.CheckState){
if (l == TypeManager.int32_type || l == TypeManager.int64_type)
opcode = OpCodes.Add_Ovf;
- else if (isUnsigned)
+ else if (is_unsigned)
opcode = OpCodes.Add_Ovf_Un;
else
opcode = OpCodes.Add;
if (ec.CheckState){
if (l == TypeManager.int32_type || l == TypeManager.int64_type)
opcode = OpCodes.Sub_Ovf;
- else if (isUnsigned)
+ else if (is_unsigned)
opcode = OpCodes.Sub_Ovf_Un;
else
opcode = OpCodes.Sub;
break;
case Operator.RightShift:
- if (isUnsigned)
+ if (is_unsigned)
opcode = OpCodes.Shr_Un;
else
opcode = OpCodes.Shr;
break;
case Operator.LessThan:
- if (isUnsigned)
+ if (is_unsigned)
opcode = OpCodes.Clt_Un;
else
opcode = OpCodes.Clt;
break;
case Operator.GreaterThan:
- if (isUnsigned)
+ if (is_unsigned)
opcode = OpCodes.Cgt_Un;
else
opcode = OpCodes.Cgt;
case Operator.LessThanOrEqual:
Type lt = left.Type;
- if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
+ if (is_unsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
ig.Emit (OpCodes.Cgt_Un);
else
ig.Emit (OpCodes.Cgt);
case Operator.GreaterThanOrEqual:
Type le = left.Type;
- if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
+ if (is_unsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
ig.Emit (OpCodes.Clt_Un);
else
ig.Emit (OpCodes.Clt);
/// Implements the ternary conditional operator (?:)
/// </summary>
public class Conditional : Expression {
- Expression expr, trueExpr, falseExpr;
+ Expression expr, true_expr, false_expr;
- public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
+ public Conditional (Expression expr, Expression true_expr, Expression false_expr)
{
this.expr = expr;
- this.trueExpr = trueExpr;
- this.falseExpr = falseExpr;
+ this.true_expr = true_expr;
+ this.false_expr = false_expr;
this.loc = expr.Location;
}
public Expression TrueExpr {
get {
- return trueExpr;
+ return true_expr;
}
}
public Expression FalseExpr {
get {
- return falseExpr;
+ return false_expr;
}
}
#if GMCS_SOURCE
if (TypeManager.IsNullableValueType (expr.Type))
- return new Nullable.LiftedConditional (expr, trueExpr, falseExpr, loc).Resolve (ec);
+ return new Nullable.LiftedConditional (expr, true_expr, false_expr, loc).Resolve (ec);
#endif
if (expr.Type != TypeManager.bool_type){
Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
}
- trueExpr = trueExpr.Resolve (ec);
- falseExpr = falseExpr.Resolve (ec);
+ true_expr = true_expr.Resolve (ec);
+ false_expr = false_expr.Resolve (ec);
- if (trueExpr == null || falseExpr == null)
+ if (true_expr == null || false_expr == null)
return null;
eclass = ExprClass.Value;
- if (trueExpr.Type == falseExpr.Type) {
- type = trueExpr.Type;
+ if (true_expr.Type == false_expr.Type) {
+ type = true_expr.Type;
if (type == TypeManager.null_type) {
// TODO: probably will have to implement ConditionalConstant
// to call method without return constant as well
Report.Warning (-101, 1, loc, "Conditional expression will always return same value");
- return trueExpr;
+ return true_expr;
}
} else {
Expression conv;
- Type true_type = trueExpr.Type;
- Type false_type = falseExpr.Type;
+ Type true_type = true_expr.Type;
+ Type false_type = false_expr.Type;
//
- // First, if an implicit conversion exists from trueExpr
- // to falseExpr, then the result type is of type falseExpr.Type
+ // First, if an implicit conversion exists from true_expr
+ // to false_expr, then the result type is of type false_expr.Type
//
- conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
+ conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
if (conv != null){
//
// Check if both can convert implicitl to each other's type
//
- if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
+ if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null){
Error (172,
"Can not compute type of conditional expression " +
- "as `" + TypeManager.CSharpName (trueExpr.Type) +
- "' and `" + TypeManager.CSharpName (falseExpr.Type) +
+ "as `" + TypeManager.CSharpName (true_expr.Type) +
+ "' and `" + TypeManager.CSharpName (false_expr.Type) +
"' convert implicitly to each other");
return null;
}
type = false_type;
- trueExpr = conv;
- } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
+ true_expr = conv;
+ } else if ((conv = Convert.ImplicitConversion(ec, false_expr, true_type,loc))!= null){
type = true_type;
- falseExpr = conv;
+ false_expr = conv;
} else {
Report.Error (173, loc, "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
- trueExpr.GetSignatureForError (), falseExpr.GetSignatureForError ());
+ true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
return null;
}
}
if (expr is BoolConstant){
BoolConstant bc = (BoolConstant) expr;
- Report.Warning (429, 4, bc.Value ? falseExpr.Location : trueExpr.Location, "Unreachable expression code detected");
- return bc.Value ? trueExpr : falseExpr;
+ Report.Warning (429, 4, bc.Value ? false_expr.Location : true_expr.Location, "Unreachable expression code detected");
+ return bc.Value ? true_expr : false_expr;
}
return this;
Label end_target = ig.DefineLabel ();
expr.EmitBranchable (ec, false_target, false);
- trueExpr.Emit (ec);
+ true_expr.Emit (ec);
ig.Emit (OpCodes.Br, end_target);
ig.MarkLabel (false_target);
- falseExpr.Emit (ec);
+ false_expr.Emit (ec);
ig.MarkLabel (end_target);
}
Conditional target = (Conditional) t;
target.expr = expr.Clone (clonectx);
- target.trueExpr = trueExpr.Clone (clonectx);
- target.falseExpr = falseExpr.Clone (clonectx);
+ target.true_expr = true_expr.Clone (clonectx);
+ target.false_expr = false_expr.Clone (clonectx);
}
}
source.Emit (ec);
+ // HACK: variable is already emitted when source is an initializer
+ if (source is NewInitialize)
+ return;
+
if (leave_copy) {
ig.Emit (OpCodes.Dup);
if (IsRef || Variable.NeedsTemporary) {
public bool IsAssigned (EmitContext ec, Location loc)
{
+ // HACK: Variables are not captured in probing mode
+ if (ec.IsInProbingMode)
+ return true;
+
if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (VariableInfo))
return true;
return sb.ToString ();
}
- public static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
- ArrayList arguments, int arg_count,
- ref MethodBase candidate)
- {
- return IsParamsMethodApplicable (
- ec, me, arguments, arg_count, false, ref candidate) ||
- IsParamsMethodApplicable (
- ec, me, arguments, arg_count, true, ref candidate);
-
-
- }
-
- static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
- ArrayList arguments, int arg_count,
- bool do_varargs, ref MethodBase candidate)
- {
-#if GMCS_SOURCE
- if (!me.HasTypeArguments &&
- !TypeManager.InferParamsTypeArguments (ec, arguments, ref candidate))
- return false;
-
- if (TypeManager.IsGenericMethodDefinition (candidate))
- throw new InternalErrorException ("a generic method definition took part in overload resolution");
-#endif
-
- return IsParamsMethodApplicable (
- ec, arguments, arg_count, candidate, do_varargs);
- }
-
- /// <summary>
- /// Determines if the candidate method, if a params method, is applicable
- /// in its expanded form to the given set of arguments
- /// </summary>
- static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments,
- int arg_count, MethodBase candidate,
- bool do_varargs)
- {
- ParameterData pd = TypeManager.GetParameterData (candidate);
-
- int pd_count = pd.Count;
- if (pd_count == 0)
- return false;
-
- int count = pd_count - 1;
- if (do_varargs) {
- if (pd.ParameterModifier (count) != Parameter.Modifier.ARGLIST)
- return false;
- if (pd_count != arg_count)
- return false;
-
- if (!(((Argument) arguments [count]).Expr is Arglist))
- return false;
- --pd_count;
- } else {
- if (!pd.HasParams)
- return false;
- }
-
- if (count > arg_count)
- return false;
-
- if (pd_count == 1 && arg_count == 0)
- return true;
-
- //
- // If we have come this far, the case which
- // remains is when the number of parameters is
- // less than or equal to the argument count.
- //
- int argument_index = 0;
- Argument a;
- for (int i = 0; i < pd_count; ++i) {
-
- if ((pd.ParameterModifier (i) & Parameter.Modifier.PARAMS) != 0) {
- Type element_type = TypeManager.GetElementType (pd.ParameterType (i));
- int params_args_count = arg_count - pd_count;
- if (params_args_count < 0)
- continue;
-
- do {
- a = (Argument) arguments [argument_index++];
-
- if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
- return false;
- } while (params_args_count-- > 0);
- continue;
- }
-
- a = (Argument) arguments [argument_index++];
-
- Parameter.Modifier a_mod = a.Modifier &
- (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
- Parameter.Modifier p_mod = pd.ParameterModifier (i) &
- (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
-
- if (a_mod == p_mod) {
-
- if (a_mod == Parameter.Modifier.NONE)
- if (!Convert.ImplicitConversionExists (ec,
- a.Expr,
- pd.ParameterType (i)))
- return false;
-
- if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
- Type pt = pd.ParameterType (i);
-
- if (!pt.IsByRef)
- pt = TypeManager.GetReferenceType (pt);
-
- if (pt != a.Type)
- return false;
- }
- } else
- return false;
-
- }
-
- return true;
- }
-
- public static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
- ArrayList arguments, int arg_count,
- ref MethodBase method)
- {
- MethodBase candidate = method;
-
-#if GMCS_SOURCE
- if (!me.HasTypeArguments &&
- !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
- return false;
-
- if (TypeManager.IsGenericMethodDefinition (candidate))
- throw new InternalErrorException ("a generic method definition took part in overload resolution");
-#endif
-
- if (IsApplicable (ec, arguments, arg_count, candidate)) {
- method = candidate;
- return true;
- }
-
- return false;
- }
-
- /// <summary>
- /// Determines if the candidate method is applicable (section 14.4.2.1)
- /// to the given set of arguments
- /// </summary>
- public static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
- MethodBase candidate)
- {
- ParameterData pd = TypeManager.GetParameterData (candidate);
-
- if (arg_count != pd.Count)
- return false;
-
- for (int i = arg_count; i > 0; ) {
- i--;
-
- Argument a = (Argument) arguments [i];
-
- Parameter.Modifier a_mod = a.Modifier &
- ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
-
- Parameter.Modifier p_mod = pd.ParameterModifier (i) &
- ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK | Parameter.Modifier.PARAMS);
-
- if (a_mod != p_mod)
- return false;
-
- Type pt = pd.ParameterType (i);
-
- if (TypeManager.IsEqual (pt, a.Type))
- continue;
-
- if (a_mod != Parameter.Modifier.NONE)
- return false;
-
- // FIXME: Kill this abomination (EmitContext.TempEc)
- EmitContext prevec = EmitContext.TempEc;
- EmitContext.TempEc = ec;
- try {
- if (!Convert.ImplicitConversionExists (ec, a.Expr, pt))
- return false;
- } finally {
- EmitContext.TempEc = prevec;
- }
- }
-
- return true;
- }
-
- public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
- {
- Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
- name, arg_count.ToString ());
- }
-
- static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
- Type delegate_type, Argument a, ParameterData expected_par)
- {
- if (a is CollectionElementInitializer.ElementInitializerArgument) {
- Report.SymbolRelatedToPreviousError (method);
- if ((expected_par.ParameterModifier (idx) & Parameter.Modifier.ISBYREF) != 0) {
- Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have 'ref', or `out' modifier",
- TypeManager.CSharpSignature (method));
- return;
- }
- Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
- TypeManager.CSharpSignature (method));
- } else if (delegate_type == null) {
- Report.SymbolRelatedToPreviousError (method);
- Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
- TypeManager.CSharpSignature (method));
- } else
- Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
- TypeManager.CSharpName (delegate_type));
-
- Parameter.Modifier mod = expected_par.ParameterModifier (idx);
-
- string index = (idx + 1).ToString ();
- if ((a.Modifier & Parameter.Modifier.ISBYREF) != 0 && mod != a.Modifier) {
- if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
- Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
- index, Parameter.GetModifierSignature (a.Modifier));
- else
- Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
- index, Parameter.GetModifierSignature (mod));
- } else {
- string p1 = Argument.FullDesc (a);
- string p2 = TypeManager.CSharpName (expected_par.ParameterType (idx));
-
- if (p1 == p2) {
- Report.ExtraInformation (loc, "(equally named types possibly from different assemblies in previous ");
- Report.SymbolRelatedToPreviousError (a.Expr.Type);
- Report.SymbolRelatedToPreviousError (expected_par.ParameterType (idx));
- }
- Report.Error (1503, loc, "Argument {0}: Cannot convert type `{1}' to `{2}'", index, p1, p2);
- }
- }
-
- public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
- int arg_count, MethodBase method,
- bool chose_params_expanded,
- Type delegate_type, bool may_fail,
- Location loc)
- {
- ParameterData pd = TypeManager.GetParameterData (method);
- int j;
- int a_idx = 0;
- Argument a = null;
- for (j = 0; j < pd.Count; j++) {
- Type parameter_type = pd.ParameterType (j);
- Parameter.Modifier pm = pd.ParameterModifier (j);
-
- if (pm == Parameter.Modifier.ARGLIST) {
- a = (Argument) Arguments [a_idx];
- if (!(a.Expr is Arglist))
- break;
- ++a_idx;
- continue;
- }
-
- int params_arg_count = 1;
- if (pm == Parameter.Modifier.PARAMS) {
- pm = Parameter.Modifier.NONE;
- params_arg_count = arg_count - pd.Count + 1;
- if (chose_params_expanded)
- parameter_type = TypeManager.GetElementType (parameter_type);
- }
-
- while (params_arg_count > 0) {
- a = (Argument) Arguments [a_idx];
- if (pm != a.Modifier)
- break;
-
- if (!TypeManager.IsEqual (a.Type, parameter_type)) {
- if (pm == Parameter.Modifier.OUT || pm == Parameter.Modifier.REF)
- break;
-
- Expression conv = Convert.ImplicitConversion (ec, a.Expr, parameter_type, loc);
- if (conv == null)
- break;
-
- // Update the argument with the implicit conversion
- if (a.Expr != conv)
- a.Expr = conv;
- }
-
- --params_arg_count;
- ++a_idx;
- }
- if (params_arg_count > 0)
- break;
-
- if (parameter_type.IsPointer && !ec.InUnsafe) {
- if (!may_fail)
- UnsafeError (loc);
- return false;
- }
- }
-
- if (a_idx == arg_count)
- return true;
-
- if (!may_fail)
- Error_InvalidArguments (loc, a_idx, method, delegate_type, a, pd);
- return false;
- }
-
public override Expression DoResolve (EmitContext ec)
{
Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
return (new DelegateInvocation (
expr_resolved, Arguments, loc)).Resolve (ec);
}
- expr.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
+
+ expr_resolved.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
return null;
}
MethodInfo method = (MethodInfo)mg;
if (method != null) {
type = TypeManager.TypeToCoreType (method.ReturnType);
+
+ // TODO: this is a copy of mg.ResolveMemberAccess method
Expression iexpr = mg.InstanceExpression;
if (method.IsStatic) {
- if (iexpr == null ||
- iexpr is This || iexpr is EmptyExpression ||
- mg.IdenticalTypeName) {
+ if (iexpr == null ||
+ iexpr is This || iexpr is EmptyExpression ||
+ mg.IdenticalTypeName) {
mg.InstanceExpression = null;
} else {
- MemberExpr.error176 (loc, mg.GetSignatureForError ());
- return null;
- }
- } else {
- if (iexpr == null || iexpr is EmptyExpression) {
- SimpleName.Error_ObjectRefRequired (ec, loc, mg.GetSignatureForError ());
+ MemberExpr.error176 (loc, mg.GetSignatureForError ());
return null;
}
}
return null;
}
- if (Arguments == null && method.Name == "Finalize") {
+ if (Arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == "Finalize") {
if (mg.IsBase)
Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
else
/// <summary>
/// This checks the ConditionalAttribute on the method
/// </summary>
- static bool IsMethodExcluded (MethodBase method)
+ public static bool IsMethodExcluded (MethodBase method)
{
if (method.IsConstructor)
return false;
- IMethodData md = TypeManager.GetMethod (method);
- if (md != null)
- return md.IsExcluded ();
+ method = TypeManager.DropGenericMethodArguments (method);
+ if (method.DeclaringType.Module == CodeGen.Module.Builder) {
+ IMethodData md = TypeManager.GetMethod (method);
+ if (md != null)
+ return md.IsExcluded ();
- // For some methods (generated by delegate class) GetMethod returns null
- // because they are not included in builder_to_method table
- if (method.DeclaringType is TypeBuilder)
+ // For some methods (generated by delegate class) GetMethod returns null
+ // because they are not included in builder_to_method table
return false;
+ }
return AttributeTester.IsConditionalMethodExcluded (method);
}
// If set, the new expression is for a value_target, and
// we will not leave anything on the stack.
//
- Expression value_target;
- bool value_target_set = false;
+ protected Expression value_target;
+ protected bool value_target_set;
bool is_type_parameter = false;
public New (Expression requested_type, ArrayList arguments, Location l)
loc = l;
}
- public bool SetValueTypeVariable (Expression value)
+ public bool SetTargetVariable (Expression value)
{
value_target = value;
value_target_set = true;
}
+ public virtual bool HasInitializer {
+ get {
+ return false;
+ }
+ }
+
public void AddressOf (EmitContext ec, AddressOp Mode)
{
if (is_type_parameter) {
}
}
- public override bool GetAttributableValue (Type valueType, out object value)
+ public override bool GetAttributableValue (Type value_type, out object value)
{
if (arguments.Count != 1) {
// Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
{
ArrayCreation target = (ArrayCreation) t;
- target.requested_base_type = requested_base_type.Clone (clonectx);
+ if (requested_base_type != null)
+ target.requested_base_type = requested_base_type.Clone (clonectx);
if (arguments != null){
target.arguments = new ArrayList (arguments.Count);
public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
: base (null, rank, initializers, loc)
{
+ if (RootContext.Version <= LanguageVersion.ISO_2)
+ Report.FeatureIsNotAvailable (loc, "implicitly typed arrays");
+
if (rank.Length > 2) {
while (rank [++dimensions] == ',');
} else {
"Consider copying `this' to a local variable " +
"outside the anonymous method and using the " +
"local instead.");
- return false;
}
RootScopeInfo host = block.Toplevel.RootScope;
return null;
- if (ec.IsFieldInitializer) {
+ if (ec.IsInFieldInitializer) {
Error (27, "Keyword `this' is not available in the current context");
return null;
}
eclass = ExprClass.Variable;
type = TypeManager.runtime_argument_handle_type;
- if (ec.IsFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs)
+ if (ec.IsInFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs)
{
Error (190, "The __arglist construct is valid only within " +
"a variable argument method");
ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
}
- public override bool GetAttributableValue (Type valueType, out object value)
+ public override bool GetAttributableValue (Type value_type, out object value)
{
if (TypeManager.ContainsGenericParameters (typearg) &&
!TypeManager.IsGenericTypeDefinition (typearg)) {
return false;
}
- if (valueType == TypeManager.object_type) {
+ if (value_type == TypeManager.object_type) {
value = (object)typearg;
return true;
}
public QualifiedAliasMember (string alias, string identifier, Location l)
{
if (RootContext.Version == LanguageVersion.ISO_1)
- Report.FeatureIsNotISO1 (l, "namespace alias qualifier");
+ Report.FeatureIsNotAvailable (l, "namespace alias qualifier");
this.alias = alias;
this.identifier = identifier;
"System.NullReferenceException");
}
+ if (args != null)
+ args.Resolve (ec);
+
Expression member_lookup;
member_lookup = MemberLookup (
ec.ContainerType, expr_type, expr_type, Identifier, loc);
#endif
if (member_lookup == null) {
ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (expr_type, Identifier);
- if (ex_method_lookuConsole.WriteLine (loc.ToString ());p != null) {
+ if (ex_method_lookup != null) {
ex_method_lookup.ExtensionExpression = expr_resolved;
- if (args != null)
+ if (args != null) {
+ ex_method_lookup.SetTypeArguments (args);
return ex_method_lookup.ResolveGeneric (ec, args);
+ }
return ex_method_lookup.DoResolve (ec);
}
- if (!ec.IsInProbingMode)
- Error_MemberLookupFailed (
- ec.ContainerType, expr_type, expr_type, Identifier, null,
- AllMemberTypes, AllBindingFlags);
+ expr = expr_resolved;
+ Error_MemberLookupFailed (
+ ec.ContainerType, expr_type, expr_type, Identifier, null,
+ AllMemberTypes, AllBindingFlags);
return null;
}
}
MemberExpr me = (MemberExpr) member_lookup;
- member_lookup = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
- if (member_lookup == null)
- return me;
+ me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
+ if (me == null)
+ return null;
if (args != null) {
- MethodGroupExpr mg = member_lookup as MethodGroupExpr;
- if (mg == null)
- throw new InternalErrorException ();
-
- return mg.ResolveGeneric (ec, args);
+ me.SetTypeArguments (args);
+ return me.ResolveGeneric (ec, args);
}
if (original != null && !TypeManager.IsValueType (expr_type)) {
- me = member_lookup as MemberExpr;
- if (me != null && me.IsInstance) {
+ if (me.IsInstance) {
LocalVariableReference var = expr_resolved as LocalVariableReference;
if (var != null && !var.VerifyAssigned (ec))
return null;
// check.
if (right_side != null)
- return member_lookup.DoResolveLValue (ec, right_side);
+ return me.DoResolveLValue (ec, right_side);
else
- return member_lookup.DoResolve (ec);
+ return me.DoResolve (ec);
}
public override Expression DoResolve (EmitContext ec)
throw new Exception ("Should not happen");
}
+ protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
+ {
+ if (RootContext.Version > LanguageVersion.ISO_2 &&
+ ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
+ Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
+ "extension method `{1}' of type `{0}' could be found " +
+ "(are you missing a using directive or an assembly reference?)",
+ TypeManager.CSharpName (type), name);
+ return;
+ }
+
+ base.Error_TypeDoesNotContainDefinition (type, name);
+ }
+
public override string ToString ()
{
return expr + "." + MemberName.MakeName (Identifier, args);
Expr.Emit (ec);
}
- public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
- Expr.EmitBranchable (ec, target, onTrue);
+ Expr.EmitBranchable (ec, target, on_true);
}
protected override void CloneTo (CloneContext clonectx, Expression t)
Expr.Emit (ec);
}
- public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
- Expr.EmitBranchable (ec, target, onTrue);
+ Expr.EmitBranchable (ec, target, on_true);
}
protected override void CloneTo (CloneContext clonectx, Expression t)
if (!CommonResolve (ec))
return null;
- Type t = Expr.Type;
- if (t.IsArray)
+ type = Expr.Type;
+ if (type.IsArray)
return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
- if (t.IsPointer)
- return MakePointerAccess (ec, t);
+ if (type.IsPointer)
+ return MakePointerAccess (ec, type);
+
+ if (Expr.eclass != ExprClass.Variable && type.IsValueType)
+ Error_CannotModifyIntermediateExpressionValue (ec);
return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
}
throw new Exception ("Should never be reached");
}
+ public override string GetSignatureForError ()
+ {
+ return Expr.GetSignatureForError ();
+ }
+
protected override void CloneTo (CloneContext clonectx, Expression t)
{
ElementAccess target = (ElementAccess) t;
// initialized), then load the arguments the first time and store them
// in locals. otherwise load from local variables.
//
- // prepareForLoad is used in compound assignments to cache origal index
+ // prepare_for_load is used in compound assignments to cache original index
// values ( label[idx++] += s )
//
- LocalTemporary [] LoadArrayAndArguments (EmitContext ec, bool prepareForLoad)
+ LocalTemporary [] LoadArrayAndArguments (EmitContext ec, bool prepare_for_load)
{
ea.Expr.Emit (ec);
LocalTemporary[] indexes = null;
- if (prepareForLoad) {
+ if (prepare_for_load) {
ec.ig.Emit (OpCodes.Dup);
indexes = new LocalTemporary [ea.Arguments.Count];
}
for (int i = 0; i < ea.Arguments.Count; ++i) {
((Argument)ea.Arguments [i]).EmitArrayArgument (ec);
- if (!prepareForLoad)
+ if (!prepare_for_load)
continue;
// Keep original array index value on the stack
int rank = ea.Expr.Type.GetArrayRank ();
ILGenerator ig = ec.ig;
- if (prepared) {
+ if (prepared_value != null) {
prepared_value.Emit (ec);
+ } else if (prepared) {
+ LoadFromPtr (ig, this.type);
} else {
LoadArrayAndArguments (ec, false);
EmitLoadOpcode (ig, type, rank);
}
if (leave_copy) {
- ec.ig.Emit (OpCodes.Dup);
+ ig.Emit (OpCodes.Dup);
temp = new LocalTemporary (this.type);
temp.Store (ec);
}
int rank = ea.Expr.Type.GetArrayRank ();
ILGenerator ig = ec.ig;
Type t = source.Type;
- prepared = prepare_for_load;
-
- LocalTemporary[] original_indexes_values = LoadArrayAndArguments (ec, prepare_for_load);
+ prepared = prepare_for_load && !(source is StringConcat);
if (prepared) {
- // Store prepared value, it will be used later when index value is read
- prepared_value = new LocalTemporary (type);
- EmitLoadOpcode (ig, type, rank);
- prepared_value.Store (ec);
- foreach (LocalTemporary lt in original_indexes_values) {
- lt.Emit (ec);
- lt.Release (ec);
+ AddressOf (ec, AddressOp.LoadStore);
+ ec.ig.Emit (OpCodes.Dup);
+ } else {
+ LocalTemporary[] original_indexes_values = LoadArrayAndArguments (ec,
+ prepare_for_load && (source is StringConcat));
+
+ if (original_indexes_values != null) {
+ prepared_value = new LocalTemporary (type);
+ EmitLoadOpcode (ig, type, rank);
+ prepared_value.Store (ec);
+ foreach (LocalTemporary lt in original_indexes_values) {
+ lt.Emit (ec);
+ lt.Release (ec);
+ }
}
}
temp.Store (ec);
}
- if (is_stobj)
+ if (prepared)
+ StoreFromPtr (ig, t);
+ else if (is_stobj)
ig.Emit (OpCodes.Stobj, t);
else if (has_type_arg)
ig.Emit (op, t);
else
ig.Emit (op);
} else {
- ModuleBuilder mb = CodeGen.Module.Builder;
- int arg_count = ea.Arguments.Count;
- Type [] args = new Type [arg_count + 1];
- MethodInfo set;
-
source.Emit (ec);
if (leave_copy) {
ec.ig.Emit (OpCodes.Dup);
temp = new LocalTemporary (this.type);
temp.Store (ec);
}
-
- for (int i = 0; i < arg_count; i++){
- //args [i++] = a.Type;
- args [i] = TypeManager.int32_type;
- }
- args [arg_count] = type;
-
- set = mb.GetArrayMethod (
- ea.Expr.Type, "Set",
- CallingConventions.HasThis |
- CallingConventions.Standard,
- TypeManager.void_type, args);
-
- ig.Emit (OpCodes.Call, set);
+ if (prepared) {
+ StoreFromPtr (ig, t);
+ } else {
+ int arg_count = ea.Arguments.Count;
+ Type [] args = new Type [arg_count + 1];
+ for (int i = 0; i < arg_count; i++) {
+ //args [i++] = a.Type;
+ args [i] = TypeManager.int32_type;
+ }
+ args [arg_count] = type;
+
+ MethodInfo set = CodeGen.Module.Builder.GetArrayMethod (
+ ea.Expr.Type, "Set",
+ CallingConventions.HasThis |
+ CallingConventions.Standard,
+ TypeManager.void_type, args);
+
+ ig.Emit (OpCodes.Call, set);
+ }
}
if (temp != null) {
}
}
}
-
- class Indexers {
- // note that the ArrayList itself in mutable. We just can't assign to 'Properties' again.
- public readonly ArrayList Properties;
- static Indexers empty;
-
- public struct Indexer {
- public readonly PropertyInfo PropertyInfo;
- public readonly MethodInfo Getter, Setter;
- public Indexer (PropertyInfo property_info, MethodInfo get, MethodInfo set)
+ /// <summary>
+ /// Expressions that represent an indexer call.
+ /// </summary>
+ public class IndexerAccess : Expression, IAssignMethod
+ {
+ class IndexerMethodGroupExpr : MethodGroupExpr
+ {
+ public IndexerMethodGroupExpr (Indexers indexers, Location loc)
+ : base (null, loc)
{
- this.PropertyInfo = property_info;
- this.Getter = get;
- this.Setter = set;
+ Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
}
- }
-
- static Indexers ()
- {
- empty = new Indexers (null);
- }
- Indexers (ArrayList array)
- {
- Properties = array;
- }
+ public override string Name {
+ get {
+ return "this";
+ }
+ }
- static void Append (ref Indexers ix, Type caller_type, MemberInfo [] mi)
- {
- bool dummy;
- if (mi == null)
- return;
- foreach (PropertyInfo property in mi){
- MethodInfo get, set;
+ protected override int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
+ {
+ //
+ // Here is the trick, decrease number of arguments by 1 when only
+ // available property method is setter. This makes overload resolution
+ // work correctly for indexers.
+ //
- get = property.GetGetMethod (true);
- set = property.GetSetMethod (true);
- if (get != null && !Expression.IsAccessorAccessible (caller_type, get, out dummy))
- get = null;
- if (set != null && !Expression.IsAccessorAccessible (caller_type, set, out dummy))
- set = null;
- if (get != null || set != null) {
- if (ix == empty)
- ix = new Indexers (new ArrayList ());
- ix.Properties.Add (new Indexer (property, get, set));
- }
+ if (method.Name [0] == 'g')
+ return parameters.Count;
+
+ return parameters.Count - 1;
}
}
- static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
+ class Indexers
{
- string p_name = TypeManager.IndexerPropertyName (lookup_type);
+ // Contains either property getter or setter
+ public ArrayList Methods;
+ public ArrayList Properties;
- return TypeManager.MemberLookup (
- caller_type, caller_type, lookup_type, MemberTypes.Property,
- BindingFlags.Public | BindingFlags.Instance |
- BindingFlags.DeclaredOnly, p_name, null);
- }
-
- static public Indexers GetIndexersForType (Type caller_type, Type lookup_type)
- {
- Indexers ix = empty;
+ Indexers ()
+ {
+ }
-#if GMCS_SOURCE
- if (lookup_type.IsGenericParameter) {
- GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
- if (gc == null)
- return empty;
+ void Append (Type caller_type, MemberInfo [] mi)
+ {
+ if (mi == null)
+ return;
- if (gc.HasClassConstraint)
- Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
+ foreach (PropertyInfo property in mi) {
+ MethodInfo accessor = property.GetGetMethod (true);
+ if (accessor == null)
+ accessor = property.GetSetMethod (true);
- Type[] ifaces = gc.InterfaceConstraints;
- foreach (Type itype in ifaces)
- Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+ if (Methods == null) {
+ Methods = new ArrayList ();
+ Properties = new ArrayList ();
+ }
- return ix;
+ Methods.Add (accessor);
+ Properties.Add (property);
+ }
}
-#endif
- Type copy = lookup_type;
- while (copy != TypeManager.object_type && copy != null){
- Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
- copy = copy.BaseType;
+ static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
+ {
+ string p_name = TypeManager.IndexerPropertyName (lookup_type);
+
+ return TypeManager.MemberLookup (
+ caller_type, caller_type, lookup_type, MemberTypes.Property,
+ BindingFlags.Public | BindingFlags.Instance |
+ BindingFlags.DeclaredOnly, p_name, null);
}
+
+ public static Indexers GetIndexersForType (Type caller_type, Type lookup_type)
+ {
+ Indexers ix = new Indexers ();
- if (lookup_type.IsInterface) {
- Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
- if (ifaces != null) {
+ #if GMCS_SOURCE
+ if (lookup_type.IsGenericParameter) {
+ GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
+ if (gc == null)
+ return ix;
+
+ if (gc.HasClassConstraint)
+ ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
+
+ Type[] ifaces = gc.InterfaceConstraints;
foreach (Type itype in ifaces)
- Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+ ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+
+ return ix;
+ }
+ #endif
+
+ Type copy = lookup_type;
+ while (copy != TypeManager.object_type && copy != null){
+ ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
+ copy = copy.BaseType;
+ }
+
+ if (lookup_type.IsInterface) {
+ Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
+ if (ifaces != null) {
+ foreach (Type itype in ifaces)
+ ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+ }
}
+
+ return ix;
}
+ }
- return ix;
+ enum AccessorType
+ {
+ Get,
+ Set
}
- }
- /// <summary>
- /// Expressions that represent an indexer call.
- /// </summary>
- public class IndexerAccess : Expression, IAssignMethod {
//
// Points to our "data" repository
//
MethodInfo get, set;
- ArrayList set_arguments;
bool is_base_indexer;
bool prepared;
LocalTemporary temp;
LocalTemporary prepared_value;
+ Expression set_expr;
protected Type indexer_type;
protected Type current_type;
this.loc = loc;
}
+ static string GetAccessorName (AccessorType at)
+ {
+ if (at == AccessorType.Set)
+ return "set";
+
+ if (at == AccessorType.Get)
+ return "get";
+
+ throw new NotImplementedException (at.ToString ());
+ }
+
protected virtual bool CommonResolve (EmitContext ec)
{
indexer_type = instance_expr.Type;
public override Expression DoResolve (EmitContext ec)
{
- if (!CommonResolve (ec))
- return null;
-
- //
- // Step 1: Query for all `Item' *properties*. Notice
- // that the actual methods are pointed from here.
- //
- // This is a group of properties, piles of them.
-
- ArrayList AllGetters = null;
-
- Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
- if (ilist.Properties != null) {
- AllGetters = new ArrayList(ilist.Properties.Count);
- foreach (Indexers.Indexer ix in ilist.Properties) {
- if (ix.Getter != null)
- AllGetters.Add (ix.Getter);
- }
- }
-
- if (AllGetters == null) {
- Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
- TypeManager.CSharpName (indexer_type));
- return null;
- }
-
- if (AllGetters.Count == 0) {
- // FIXME: we cannot simply select first one as the error message is missleading when
- // multiple indexers exist
- Indexers.Indexer first_indexer = (Indexers.Indexer)ilist.Properties[ilist.Properties.Count - 1];
- Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
- TypeManager.GetFullNameSignature (first_indexer.PropertyInfo));
- return null;
- }
-
- get = (MethodInfo)new MethodGroupExpr (AllGetters, type, loc).OverloadResolve (ec,
- arguments, false, loc);
-
- if (get == null) {
- Invocation.Error_WrongNumArguments (loc, "this", arguments.Count);
- return null;
- }
-
- //
- // Only base will allow this invocation to happen.
- //
- if (get.IsAbstract && this is BaseIndexerAccess){
- Error_CannotCallAbstractBase (TypeManager.CSharpSignature (get));
- return null;
- }
-
- type = get.ReturnType;
- if (type.IsPointer && !ec.InUnsafe){
- UnsafeError (loc);
- return null;
- }
-
- instance_expr.CheckMarshalByRefAccess ();
-
- eclass = ExprClass.IndexerAccess;
- return this;
+ return ResolveAccessor (ec, AccessorType.Get);
}
public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
// if the indexer returns a value type, and we try to set a field in it
if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
- Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
- GetSignatureForError ());
- return null;
+ Error_CannotModifyIntermediateExpressionValue (ec);
}
- ArrayList AllSetters = new ArrayList();
- if (!CommonResolve (ec))
+ Expression e = ResolveAccessor (ec, AccessorType.Set);
+ if (e == null)
return null;
- bool found_any = false, found_any_setters = false;
+ set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
+ return e;
+ }
- Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
- if (ilist.Properties != null) {
- found_any = true;
- foreach (Indexers.Indexer ix in ilist.Properties) {
- if (ix.Setter != null)
- AllSetters.Add (ix.Setter);
- }
- }
- if (AllSetters.Count > 0) {
- found_any_setters = true;
- set_arguments = (ArrayList) arguments.Clone ();
- set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
- set = (MethodInfo)(new MethodGroupExpr (AllSetters, type, loc)).OverloadResolve (
- ec,
- set_arguments, false, loc);
- }
+ Expression ResolveAccessor (EmitContext ec, AccessorType accessorType)
+ {
+ if (!CommonResolve (ec))
+ return null;
- if (!found_any) {
+ Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
+ if (ilist.Methods == null) {
Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
- TypeManager.CSharpName (indexer_type));
+ TypeManager.CSharpName (indexer_type));
return null;
}
- if (!found_any_setters) {
- Error (154, "indexer can not be used in this context, because " +
- "it lacks a `set' accessor");
+ MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
+ mg = mg.OverloadResolve (ec, arguments, false, loc);
+ if (mg == null)
return null;
+
+ MethodInfo mi = (MethodInfo) mg;
+ PropertyInfo pi = null;
+ for (int i = 0; i < ilist.Methods.Count; ++i) {
+ if (ilist.Methods [i] == mi) {
+ pi = (PropertyInfo) ilist.Properties [i];
+ break;
+ }
+ }
+
+ type = pi.PropertyType;
+ if (type.IsPointer && !ec.InUnsafe)
+ UnsafeError (loc);
+
+ MethodInfo accessor;
+ if (accessorType == AccessorType.Get) {
+ accessor = get = pi.GetGetMethod (true);
+ } else {
+ accessor = set = pi.GetSetMethod (true);
+ if (accessor == null && pi.GetGetMethod (true) != null) {
+ Report.SymbolRelatedToPreviousError (pi);
+ Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
+ TypeManager.GetFullNameSignature (pi));
+ return null;
+ }
}
- if (set == null) {
- Invocation.Error_WrongNumArguments (loc, "this", arguments.Count);
+ if (accessor == null) {
+ Report.SymbolRelatedToPreviousError (pi);
+ Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
+ TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
return null;
}
//
// Only base will allow this invocation to happen.
//
- if (set.IsAbstract && this is BaseIndexerAccess){
- Error_CannotCallAbstractBase (TypeManager.CSharpSignature (set));
- return null;
+ if (accessor.IsAbstract && this is BaseIndexerAccess) {
+ Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
}
- //
- // Now look for the actual match in the list of indexers to set our "return" type
- //
- type = TypeManager.void_type; // default value
- foreach (Indexers.Indexer ix in ilist.Properties){
- if (ix.Setter == set){
- type = ix.PropertyInfo.PropertyType;
- break;
+ bool must_do_cs1540_check;
+ if (!IsAccessorAccessible (ec.ContainerType, accessor, out must_do_cs1540_check)) {
+ if (set == null)
+ set = pi.GetSetMethod (true);
+ else
+ get = pi.GetGetMethod (true);
+
+ if (set != null && get != null &&
+ (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
+ Report.SymbolRelatedToPreviousError (accessor);
+ Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
+ TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
+ } else {
+ Report.SymbolRelatedToPreviousError (pi);
+ ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
}
}
instance_expr.CheckMarshalByRefAccess ();
-
eclass = ExprClass.IndexerAccess;
return this;
}
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
{
prepared = prepare_for_load;
- Argument a = (Argument) set_arguments [set_arguments.Count - 1];
-
+ Expression value = set_expr;
+
if (prepared) {
Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
arguments, loc, true, false);
temp = new LocalTemporary (Type);
source.Emit (ec);
temp.Store (ec);
- a.Expr = temp;
+ value = temp;
}
- Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, set_arguments, loc, false, prepared);
+ arguments.Add (new Argument (value, Argument.AType.Expression));
+ Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
if (temp != null) {
temp.Emit (ec);
}
}
-
public override void Emit (EmitContext ec)
{
Emit (ec, false);
public override string GetSignatureForError ()
{
- // FIXME: print the argument list of the indexer
- return instance_expr.GetSignatureForError () + ".this[...]";
+ return TypeManager.CSharpSignature (get != null ? get : set, false);
}
protected override void CloneTo (CloneContext clonectx, Expression t)
return null;
}
- if (ec.IsFieldInitializer){
+ if (ec.IsInFieldInitializer){
Error (1512, "Keyword `base' is not available in the current context");
return null;
}
left = ec.GetThis (loc);
MemberExpr me = (MemberExpr) member_lookup;
-
- Expression e = me.ResolveMemberAccess (ec, left, loc, null);
-
- if (e is PropertyExpr) {
- PropertyExpr pe = (PropertyExpr) e;
- pe.IsBase = true;
- } else if (e is EventExpr) {
- EventExpr ee = (EventExpr) e;
- ee.IsBase = true;
- }
-
- MethodGroupExpr mg = e as MethodGroupExpr;
- if (mg != null)
- mg.IsBase = true;
+ me = me.ResolveMemberAccess (ec, left, loc, null);
+ if (me == null)
+ return null;
+ me.IsBase = true;
if (args != null) {
- if (mg != null)
- return mg.ResolveGeneric (ec, args);
-
- Report.Error (307, loc, "`{0}' cannot be used with type arguments",
- Identifier);
- return null;
+ args.Resolve (ec);
+ me.SetTypeArguments (args);
+ return me.ResolveGeneric (ec, args);
}
- return e;
+ return me;
}
public override void Emit (EmitContext ec)
{
BaseAccess target = (BaseAccess) t;
- target.args = args.Clone ();
+ if (args != null)
+ target.args = args.Clone ();
}
}
loc = l;
}
-#if GMCS_SOURCE
public Expression RemoveNullable ()
{
if (dim.EndsWith ("?")) {
dim = dim.Substring (0, dim.Length - 1);
- if (dim == "")
+ if (dim.Length == 0)
return left;
}
return this;
}
-#endif
protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
{
return initializer;
}
- return new Assign (element_member, initializer, loc).Resolve (ec);
+ Assign a = new Assign (element_member, initializer, loc);
+ if (a.Resolve (ec) == null)
+ return null;
+
+ //
+ // Ignore field initializers with default value
+ //
+ Constant c = a.Source as Constant;
+ if (c != null && c.IsDefaultInitializer (a.Type) && a.Target.eclass == ExprClass.Variable)
+ return EmptyExpressionStatement.Instance;
+
+ return a;
}
protected override Expression Error_MemberLookupFailed (MemberInfo[] members)
}
}
- initializers [i] = initializer.Resolve (ec);
+ Expression e = initializer.Resolve (ec);
+ if (e == EmptyExpressionStatement.Instance)
+ initializers.RemoveAt (i--);
+ else
+ initializers [i] = e;
}
type = typeof (CollectionOrObjectInitializers);
//
public class NewInitialize : New
{
+ //
+ // This class serves as a proxy for variable initializer target instances.
+ // A real variable is assigned later when we resolve left side of an
+ // assignment
+ //
+ sealed class InitializerTargetExpression : Expression, IMemoryLocation
+ {
+ NewInitialize new_instance;
+
+ public InitializerTargetExpression (NewInitialize newInstance)
+ {
+ this.type = newInstance.type;
+ this.loc = newInstance.loc;
+ this.eclass = newInstance.eclass;
+ this.new_instance = newInstance;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ return this;
+ }
+
+ public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ {
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ new_instance.value_target.Emit (ec);
+ }
+
+ #region IMemoryLocation Members
+
+ public void AddressOf (EmitContext ec, AddressOp mode)
+ {
+ ((IMemoryLocation)new_instance.value_target).AddressOf (ec, mode);
+ }
+
+ #endregion
+ }
+
CollectionOrObjectInitializers initializers;
- TemporaryVariable type_instance;
public NewInitialize (Expression requested_type, ArrayList arguments, CollectionOrObjectInitializers initializers, Location l)
: base (requested_type, arguments, l)
base.CloneTo (clonectx, t);
NewInitialize target = (NewInitialize) t;
- target.initializers = (CollectionOrObjectInitializers)initializers.Clone (clonectx);
+ target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
}
public override Expression DoResolve (EmitContext ec)
if (initializers.IsEmpty)
return e;
- type_instance = new TemporaryVariable (type, loc);
- type_instance = (TemporaryVariable)type_instance.Resolve (ec);
-
Expression previous = ec.CurrentInitializerVariable;
- ec.CurrentInitializerVariable = type_instance;
+ ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
initializers.Resolve (ec);
ec.CurrentInitializerVariable = previous;
return this;
{
base.Emit (ec);
- type_instance.EmitStore (ec);
+ //
+ // If target is a value, let's use it
+ //
+ VariableReference variable = value_target as VariableReference;
+ if (variable != null) {
+ if (variable.IsRef)
+ StoreFromPtr (ec.ig, type);
+ else
+ variable.Variable.EmitAssign (ec);
+ } else {
+ if (value_target == null || value_target_set)
+ value_target = new LocalTemporary (type);
+
+ ((LocalTemporary) value_target).Store (ec);
+ }
+
initializers.Emit (ec);
- type_instance.Emit (ec);
+
+ if (variable == null)
+ value_target.Emit (ec);
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ if (initializers.IsEmpty) {
+ base.EmitStatement (ec);
+ return;
+ }
+
+ base.Emit (ec);
+
+ if (value_target == null) {
+ LocalTemporary variable = new LocalTemporary (type);
+ variable.Store (ec);
+ value_target = variable;
+ }
+
+ initializers.EmitStatement (ec);
+ }
+
+ public override bool HasInitializer {
+ get {
+ return !initializers.IsEmpty;
+ }
}
}
type = e.Type;
if (type == TypeManager.void_type || type == TypeManager.null_type ||
type == TypeManager.anonymous_method_type || type.IsPointer) {
- Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
- Name, e.GetSignatureForError ());
+ Error_InvalidInitializer (e);
return null;
}
return e;
}
+ protected virtual void Error_InvalidInitializer (Expression initializer)
+ {
+ Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
+ Name, initializer.GetSignatureForError ());
+ }
+
public override void Emit (EmitContext ec)
{
throw new InternalErrorException ("Should not be reached");