// Miguel de Icaza (miguel@ximian.com)
// Marek Safar (marek.safar@seznam.cz)
//
-// (C) 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
//
//
void AddressOf (EmitContext ec, AddressOp mode);
}
- /// <summary>
- /// This interface is implemented by variables
- /// </summary>
+ // TODO: Rename to something meaningful, this is flow-analysis interface only
public interface IVariable {
- VariableInfo VariableInfo {
- get;
- }
-
- bool VerifyFixed ();
+ VariableInfo VariableInfo { get; }
+ bool IsFixed { get; }
}
/// <remarks>
// This is used if the expression should be resolved as a type or namespace name.
// the default implementation fails.
//
- public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
+ public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext rc, bool silent)
{
+ if (!silent) {
+ Expression e = this;
+ EmitContext ec = rc as EmitContext;
+ if (ec != null)
+ e = e.Resolve (ec);
+ if (e != null)
+ e.Error_UnexpectedKind (ResolveFlags.Type, loc);
+ }
return null;
}
if (b ||
Convert.ExplicitReferenceConversionExists (Type, target) ||
Convert.ExplicitUnsafe (e, target) != null ||
- (ec != null && Convert.UserDefinedConversion (ec, this, target, Location.Null, true) != null))
+ (ec != null && Convert.ExplicitUserConversion (ec, this, target, Location.Null) != null))
{
Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. " +
"An explicit conversion exists (are you missing a cast?)",
return;
}
- if (Type != TypeManager.string_type && this is Constant && !(this is EmptyConstantCast)) {
- Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
- ((Constant)(this)).GetValue ().ToString (), TypeManager.CSharpName (target));
- return;
- }
-
Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
TypeManager.CSharpName (type),
TypeManager.CSharpName (target));
/// </remarks>
public abstract void Emit (EmitContext ec);
+ // Emit code to branch to @target if this expression is equivalent to @on_true.
+ // The default implementation is to emit the value, and then emit a brtrue or brfalse.
+ // Subclasses can provide more efficient implementations, but those MUST be equivalent,
+ // including the use of conditional branches. Note also that a branch MUST be emitted
public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
{
Emit (ec);
ec.ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
}
+ // Emit this expression for its side effects, not for its value.
+ // The default implementation is to emit the value, and then throw it away.
+ // Subclasses can provide more efficient implementations, but those MUST be equivalent
+ public virtual void EmitSideEffect (EmitContext ec)
+ {
+ Emit (ec);
+ ec.ig.Emit (OpCodes.Pop);
+ }
+
/// <summary>
/// Protected constructor. Only derivate types should
/// be able to be created
throw new NotImplementedException ();
}
+ protected void Error_PointerInsideExpressionTree ()
+ {
+ Report.Error (1944, loc, "An expression tree cannot contain an unsafe pointer operation");
+ }
+
/// <summary>
/// Returns an expression that can be used to invoke operator true
/// on the expression if it exists.
static Expression GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
{
MethodGroupExpr operator_group;
- operator_group = MethodLookup (ec.ContainerType, e.Type, is_true ? "op_True" : "op_False", loc) as MethodGroupExpr;
+ string mname = Operator.GetMetadataName (is_true ? Operator.OpType.True : Operator.OpType.False);
+ operator_group = MethodLookup (ec.ContainerType, e.Type, mname, loc) as MethodGroupExpr;
if (operator_group == null)
return null;
return cloned;
}
- public virtual Expression CreateExpressionTree (EmitContext ec)
- {
- throw new NotImplementedException (
- "Expression tree conversion not implemented for " + GetType ());
- }
+ //
+ // Implementation of expression to expression tree conversion
+ //
+ public abstract Expression CreateExpressionTree (EmitContext ec);
protected Expression CreateExpressionFactoryCall (string name, ArrayList args)
{
}
public static Expression CreateExpressionFactoryCall (string name, TypeArguments typeArguments, ArrayList args, Location loc)
+ {
+ return new Invocation (new MemberAccess (CreateExpressionTypeExpression (loc), name, typeArguments, loc), args);
+ }
+
+ protected static TypeExpr CreateExpressionTypeExpression (Location loc)
{
TypeExpr texpr = TypeManager.expression_type_expr;
if (texpr == null) {
TypeManager.expression_type_expr = texpr = new TypeExpression (t, Location.Null);
}
- return new Invocation (new MemberAccess (texpr, name, typeArguments, loc), args);
+ return texpr;
}
}
/// Emit that will always leave a value on the stack).
/// </summary>
public abstract void EmitStatement (EmitContext ec);
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ EmitStatement (ec);
+ }
}
/// <summary>
/// would be "unsigned int".
///
/// </summary>
- public class EmptyCast : Expression
+ public abstract class TypeCast : Expression
{
protected Expression child;
- protected EmptyCast (Expression child, Type return_type)
+ protected TypeCast (Expression child, Type return_type)
{
eclass = child.eclass;
loc = child.Location;
type = return_type;
this.child = child;
}
-
- public static Expression Create (Expression child, Type type)
- {
- Constant c = child as Constant;
- if (c != null)
- return new EmptyConstantCast (c, type);
-
- return new EmptyCast (child, type);
- }
public override Expression CreateExpressionTree (EmitContext ec)
{
ArrayList args = new ArrayList (2);
args.Add (new Argument (child.CreateExpressionTree (ec)));
args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
+
+ if (type.IsPointer || child.Type.IsPointer)
+ Error_PointerInsideExpressionTree ();
+
return CreateExpressionFactoryCall (ec.CheckState ? "ConvertChecked" : "Convert", args);
}
protected override void CloneTo (CloneContext clonectx, Expression t)
{
- EmptyCast target = (EmptyCast) t;
+ TypeCast target = (TypeCast) t;
target.child = child.Clone (clonectx);
}
+
+ public override bool IsNull {
+ get { return child.IsNull; }
+ }
+ }
+
+ public class EmptyCast : TypeCast {
+ EmptyCast (Expression child, Type target_type)
+ : base (child, target_type)
+ {
+ }
+
+ public static Expression Create (Expression child, Type type)
+ {
+ Constant c = child as Constant;
+ if (c != null)
+ return new EmptyConstantCast (c, type);
+
+ EmptyCast e = child as EmptyCast;
+ if (e != null)
+ return new EmptyCast (e.child, type);
+
+ return new EmptyCast (child, type);
+ }
+
+ public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
+ {
+ child.EmitBranchable (ec, label, on_true);
+ }
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ child.EmitSideEffect (ec);
+ }
}
/// <summary>
/// Performs a cast using an operator (op_Explicit or op_Implicit)
/// </summary>
- public class OperatorCast : EmptyCast {
+ public class OperatorCast : TypeCast {
MethodInfo conversion_operator;
bool find_explicit;
/// <summary>
/// This is a numeric cast to a Decimal
/// </summary>
- public class CastToDecimal : EmptyCast {
+ public class CastToDecimal : TypeCast {
MethodInfo conversion_operator;
public CastToDecimal (Expression child)
/// <summary>
/// This is an explicit numeric cast from a Decimal
/// </summary>
- public class CastFromDecimal : EmptyCast
+ public class CastFromDecimal : TypeCast
{
static IDictionary operators;
return child.ConvertExplicitly (in_checked_context, target_type);
}
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ ArrayList args = new ArrayList (2);
+ args.Add (new Argument (child.CreateExpressionTree (ec)));
+ args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
+ if (type.IsPointer)
+ Error_PointerInsideExpressionTree ();
+
+ return CreateExpressionFactoryCall ("Convert", args);
+ }
+
public override Constant Increment ()
{
return child.Increment ();
child.Emit (ec);
}
+ public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
+ {
+ child.EmitBranchable (ec, label, on_true);
+ }
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ child.EmitSideEffect (ec);
+ }
+
public override Constant ConvertImplicitly (Type target_type)
{
// FIXME: Do we need to check user conversions?
Child.Emit (ec);
}
+ public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
+ {
+ Child.EmitBranchable (ec, label, on_true);
+ }
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ Child.EmitSideEffect (ec);
+ }
+
public override bool GetAttributableValue (Type value_type, out object value)
{
value = GetTypedValue ();
/// The effect of it is to box the value type emitted by the previous
/// operation.
/// </summary>
- public class BoxedCast : EmptyCast {
+ public class BoxedCast : TypeCast {
public BoxedCast (Expression expr, Type target_type)
: base (expr, target_type)
ec.ig.Emit (OpCodes.Box, child.Type);
}
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
+ // so, we need to emit the box+pop instructions in most cases
+ if (child.Type.IsValueType &&
+ (type == TypeManager.object_type || type == TypeManager.value_type))
+ child.EmitSideEffect (ec);
+ else
+ base.EmitSideEffect (ec);
+ }
}
- public class UnboxCast : EmptyCast {
+ public class UnboxCast : TypeCast {
public UnboxCast (Expression expr, Type return_type)
: base (expr, return_type)
{
/// context, so they should generate the conv.ovf opcodes instead of
/// conv opcodes.
/// </summary>
- public class ConvCast : EmptyCast {
+ public class ConvCast : TypeCast {
public enum Mode : byte {
I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
U1_I1, U1_CH,
}
}
- public class OpcodeCast : EmptyCast {
+ public class OpcodeCast : TypeCast {
OpCode op, op2;
bool second_valid;
/// This kind of cast is used to encapsulate a child and cast it
/// to the class requested
/// </summary>
- public class ClassCast : EmptyCast {
+ public class ClassCast : TypeCast {
public ClassCast (Expression child, Type return_type)
: base (child, return_type)
{
this.expr = expr;
this.orig_expr = orig_expr;
+ this.loc = orig_expr.Location;
}
public static Expression Create (Constant expr, Expression original_expr)
{
expr.Emit (ec);
}
+
+ public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
+ {
+ expr.EmitBranchable (ec, target, on_true);
+ }
+ }
+
+ //
+ // Unresolved type name expressions
+ //
+ public abstract class ATypeNameExpression : FullNamedExpression
+ {
+ public readonly string Name;
+ protected TypeArguments targs;
+
+ protected ATypeNameExpression (string name, Location l)
+ {
+ Name = name;
+ loc = l;
+ }
+
+ protected ATypeNameExpression (string name, TypeArguments targs, Location l)
+ {
+ Name = name;
+ this.targs = targs;
+ loc = l;
+ }
+
+ public bool HasTypeArguments {
+ get {
+ return targs != null;
+ }
+ }
+
+ public override string GetSignatureForError ()
+ {
+ if (targs != null) {
+ return TypeManager.RemoveGenericArity (Name) + "<" +
+ targs.GetSignatureForError () + ">";
+ }
+
+ return Name;
+ }
}
/// <summary>
/// SimpleName expressions are formed of a single word and only happen at the beginning
/// of a dotted-name.
/// </summary>
- public class SimpleName : Expression {
- public readonly string Name;
- public readonly TypeArguments Arguments;
+ public class SimpleName : ATypeNameExpression {
bool in_transit;
public SimpleName (string name, Location l)
+ : base (name, l)
{
- Name = name;
- loc = l;
}
public SimpleName (string name, TypeArguments args, Location l)
+ : base (name, args, l)
{
- Name = name;
- Arguments = args;
- loc = l;
}
public SimpleName (string name, TypeParameter[] type_params, Location l)
+ : base (name, l)
{
- Name = name;
- loc = l;
-
- Arguments = new TypeArguments (l);
+ targs = new TypeArguments (l);
foreach (TypeParameter type_param in type_params)
- Arguments.Add (new TypeParameterExpr (type_param, l));
+ targs.Add (new TypeParameterExpr (type_param, l));
}
public static string RemoveGenericArity (string name)
public SimpleName GetMethodGroup ()
{
- return new SimpleName (RemoveGenericArity (Name), Arguments, loc);
+ return new SimpleName (RemoveGenericArity (Name), targs, loc);
}
public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
Type[] gen_params = TypeManager.GetTypeArguments (t);
- int arg_count = Arguments != null ? Arguments.Count : 0;
+ int arg_count = targs != null ? targs.Count : 0;
for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) {
if (arg_count + ds.CountTypeParameters == gen_params.Length) {
foreach (TypeParameter param in ds.TypeParameters)
new_args.Add (new TypeParameterExpr (param, loc));
- if (Arguments != null)
- new_args.Add (Arguments);
+ if (targs != null)
+ new_args.Add (targs);
return new ConstructedType (t, new_args, loc);
}
if (nested != null)
return nested.ResolveAsTypeStep (ec, false);
- if (Arguments != null) {
- ConstructedType ct = new ConstructedType (fne, Arguments, loc);
+ if (targs != null) {
+ ConstructedType ct = new ConstructedType (fne, targs, loc);
return ct.ResolveAsTypeStep (ec, false);
}
return;
}
- if (Arguments != null) {
+ if (targs != null) {
FullNamedExpression retval = ec.DeclContainer.LookupNamespaceOrType (SimpleName.RemoveGenericArity (Name), loc, true);
if (retval != null) {
Namespace.Error_TypeArgumentsCannotBeUsed (retval.Type, loc);
if (current_block != null){
LocalInfo vi = current_block.GetLocalInfo (Name);
if (vi != null){
- if (Arguments != null) {
+ if (targs != null) {
Report.Error (307, loc,
"The variable `{0}' cannot be used with type arguments",
Name);
ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
if (pref != null) {
- if (Arguments != null) {
+ if (targs != null) {
Report.Error (307, loc,
"The variable `{0}' cannot be used with type arguments",
Name);
}
if (e is TypeExpr) {
- if (Arguments == null)
+ if (targs == null)
return e;
ConstructedType ct = new ConstructedType (
- (FullNamedExpression) e, Arguments, loc);
+ e.Type, targs, loc);
return ct.ResolveAsTypeStep (ec, false);
}
if (me == null)
return null;
- if (Arguments != null) {
- Arguments.Resolve (ec);
- me.SetTypeArguments (Arguments);
+ if (targs != null) {
+ targs.Resolve (ec);
+ me.SetTypeArguments (targs);
}
if (!me.IsStatic && (me.InstanceExpression != null) &&
return e;
}
- public override void Emit (EmitContext ec)
- {
- throw new InternalErrorException ("The resolve phase was not executed");
- }
-
- public override string ToString ()
- {
- return Name;
- }
-
- public override string GetSignatureForError ()
- {
- if (Arguments != null) {
- return TypeManager.RemoveGenericArity (Name) + "<" +
- Arguments.GetSignatureForError () + ">";
- }
-
- return Name;
- }
-
protected override void CloneTo (CloneContext clonectx, Expression target)
{
// CloneTo: Nothing, we do not keep any state on this expression
/// section 10.8.1 (Fully Qualified Names).
/// </summary>
public abstract class FullNamedExpression : Expression {
+
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ throw new NotSupportedException ("ET");
+ }
+
public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
return this;
}
- public abstract string FullName {
- get;
+ public override void Emit (EmitContext ec)
+ {
+ throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
+ GetSignatureForError ());
}
}
return ResolveAsTypeTerminal (ec, false);
}
- override public void Emit (EmitContext ec)
- {
- throw new Exception ("Should never be called");
- }
-
public virtual bool CheckAccessLevel (DeclSpace ds)
{
return ds.CheckAccessLevel (Type);
protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
- public abstract string Name {
- get;
- }
-
public override bool Equals (object obj)
{
TypeExpr tobj = obj as TypeExpr;
{
return Type.GetHashCode ();
}
-
- public override string ToString ()
- {
- return Name;
- }
}
/// <summary>
{
return this;
}
-
- public override string Name {
- get { return Type.ToString (); }
- }
-
- public override string FullName {
- get { return Type.FullName; }
- }
}
/// <summary>
return this;
}
- public override string Name {
- get { return name; }
- }
-
- public override string FullName {
- get { return name; }
- }
-
protected override void CloneTo (CloneContext clonectx, Expression target)
{
// CloneTo: Nothing, we do not keep any state on this expression
loc = l;
}
+ protected override void CloneTo (CloneContext clonectx, Expression target)
+ {
+ // Nothing to clone
+ }
+
protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
{
Expression expr;
type = fne.Type;
return new TypeExpression (type, loc);
}
-
- public override string Name {
- get { return name.FullName; }
- }
-
- public override string FullName {
- get { return name.FullName; }
- }
- }
-
- public class TypeAliasExpression : TypeExpr {
- FullNamedExpression alias;
- TypeExpr texpr;
- TypeArguments args;
- string name;
-
- public TypeAliasExpression (FullNamedExpression alias, TypeArguments args, Location l)
- {
- this.alias = alias;
- this.args = args;
- loc = l;
-
- eclass = ExprClass.Type;
- if (args != null)
- name = alias.FullName + "<" + args.ToString () + ">";
- else
- name = alias.FullName;
- }
-
- public override string Name {
- get { return alias.FullName; }
- }
-
- public override string FullName {
- get { return name; }
- }
-
- protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
- {
- texpr = alias.ResolveAsTypeTerminal (ec, false);
- if (texpr == null)
- return null;
-
- Type type = texpr.Type;
- int num_args = TypeManager.GetNumberOfTypeArguments (type);
-
- if (args != null) {
- if (num_args == 0) {
- Report.Error (308, loc,
- "The non-generic type `{0}' cannot " +
- "be used with type arguments.",
- TypeManager.CSharpName (type));
- return null;
- }
-
- ConstructedType ctype = new ConstructedType (type, args, loc);
- return ctype.ResolveAsTypeTerminal (ec, false);
- } else if (num_args > 0) {
- Report.Error (305, loc,
- "Using the generic type `{0}' " +
- "requires {1} type arguments",
- TypeManager.CSharpName (type), num_args.ToString ());
- return null;
- }
-
- return texpr;
- }
-
- public override bool CheckAccessLevel (DeclSpace ds)
- {
- return texpr.CheckAccessLevel (ds);
- }
-
- public override bool AsAccessible (DeclSpace ds)
- {
- return texpr.AsAccessible (ds);
- }
-
- public override bool IsClass {
- get { return texpr.IsClass; }
- }
-
- public override bool IsValueType {
- get { return texpr.IsValueType; }
- }
-
- public override bool IsInterface {
- get { return texpr.IsInterface; }
- }
-
- public override bool IsSealed {
- get { return texpr.IsSealed; }
- }
}
/// <summary>
"with an instance reference, qualify it with a type name instead", name);
}
+ public static void Error_BaseAccessInExpressionTree (Location loc)
+ {
+ Report.Error (831, loc, "An expression tree may not contain a base access");
+ }
+
// TODO: possible optimalization
// Cache resolved constant result in FieldBuilder <-> expression map
public virtual MemberExpr ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
return null;
// Search continues
- ExtensionMethodGroupExpr e = ns.LookupExtensionMethod (type, null, Name);
+ ExtensionMethodGroupExpr e = ns.LookupExtensionMethod (type, null, Name, loc);
if (e == null)
return base.OverloadResolve (ec, ref arguments, false, loc);
//
if (TypeManager.DropGenericTypeArguments (p) == TypeManager.expression_type) {
p = TypeManager.GetTypeArguments (p) [0];
+ }
+ if (TypeManager.DropGenericTypeArguments (q) == TypeManager.expression_type) {
q = TypeManager.GetTypeArguments (q) [0];
}
+
p = Delegate.GetInvokeMethod (null, p).ReturnType;
q = Delegate.GetInvokeMethod (null, q).ReturnType;
} else {
public override Expression CreateExpressionTree (EmitContext ec)
{
- return new Cast (new TypeExpression (typeof (MethodInfo), loc),
- new TypeOfMethod ((MethodInfo)best_candidate, loc));
+ if (best_candidate == null) {
+ Report.Error (1953, loc, "An expression tree cannot contain an expression with method group");
+ return null;
+ }
+
+ if (best_candidate.IsConstructor)
+ return new TypeOfConstructorInfo (best_candidate, loc);
+
+ IMethodData md = TypeManager.GetMethod (best_candidate);
+ if (md != null && md.IsExcluded ())
+ Report.Error (765, loc,
+ "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
+
+ return new TypeOfMethodInfo (best_candidate, loc);
}
override public Expression DoResolve (EmitContext ec)
Report.Error (1503, loc, "Argument {0}: Cannot convert type `{1}' to `{2}'", index, p1, p2);
}
}
+
+ public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
+ {
+ Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
+ Name, TypeManager.CSharpName (target));
+ }
protected virtual int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
{
return 0;
}
- // FIXME: Kill this abomination (EmitContext.TempEc)
- EmitContext prevec = EmitContext.TempEc;
- EmitContext.TempEc = ec;
- try {
- if (delegate_type != null ?
- !Delegate.IsTypeCovariant (argument.Expr, parameter) :
- !Convert.ImplicitConversionExists (ec, argument.Expr, parameter))
- return 2;
-
- if (arg_mod != param_mod)
- return 1;
+ if (delegate_type != null ?
+ !Delegate.IsTypeCovariant (argument.Expr, parameter) :
+ !Convert.ImplicitConversionExists (ec, argument.Expr, parameter))
+ return 2;
- } finally {
- EmitContext.TempEc = prevec;
- }
+ if (arg_mod != param_mod)
+ return 1;
return 0;
}
// not an extension method. We start extension methods lookup from here
//
if (InstanceExpression != null) {
- ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (type, Name);
+ ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (type, Name, loc);
if (ex_method_lookup != null) {
ex_method_lookup.ExtensionExpression = InstanceExpression;
ex_method_lookup.SetTypeArguments (type_arguments);
continue;
}
-
+
Expression conv;
if (TypeManager.IsEqual (a.Type, pt)) {
conv = a.Expr;
// Convert params arguments to an array initializer
//
if (params_initializers != null) {
- params_initializers.Add (conv);
+ // we choose to use 'a.Expr' rather than 'conv' so that
+ // we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
+ params_initializers.Add (a.Expr);
arguments.RemoveAt (a_idx--);
--arg_count;
continue;
}
-
+
// Update the argument with the implicit conversion
a.Expr = conv;
}
return base.ResolveMemberAccess (ec, left, loc, original);
}
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ throw new NotSupportedException ("ET");
+ }
+
public override Expression DoResolve (EmitContext ec)
{
IConstant ic = TypeManager.GetConstant (constant);
return base.ResolveMemberAccess (ec, left, loc, original);
}
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ Expression instance;
+ if (InstanceExpression == null) {
+ instance = new NullLiteral (loc);
+ } else {
+ instance = InstanceExpression.CreateExpressionTree (ec);
+ }
+
+ ArrayList args = new ArrayList (2);
+ args.Add (new Argument (instance));
+ args.Add (new Argument (CreateTypeOfExpression ()));
+ return CreateExpressionFactoryCall ("Field", args);
+ }
+
+ public Expression CreateTypeOfExpression ()
+ {
+ return new TypeOfField (FieldInfo, loc);
+ }
+
override public Expression DoResolve (EmitContext ec)
{
return DoResolve (ec, false, false);
// If the instance expression is a local variable or parameter.
IVariable var = InstanceExpression as IVariable;
- if ((var == null) || (var.VariableInfo == null))
+ if (var == null || var.VariableInfo == null)
return this;
VariableInfo vi = var.VariableInfo;
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
IVariable var = InstanceExpression as IVariable;
- if ((var != null) && (var.VariableInfo != null))
+ if (var != null && var.VariableInfo != null)
var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
return this;
}
+ bool is_marshal_by_ref ()
+ {
+ return !IsStatic && Type.IsValueType && TypeManager.mbr_type != null && TypeManager.IsSubclassOf (DeclaringType, TypeManager.mbr_type);
+ }
+
public override void CheckMarshalByRefAccess (EmitContext ec)
{
- if (!IsStatic && Type.IsValueType && !(InstanceExpression is This) && TypeManager.mbr_type != null && TypeManager.IsSubclassOf (DeclaringType, TypeManager.mbr_type)) {
+ if (is_marshal_by_ref () && !(InstanceExpression is This)) {
Report.SymbolRelatedToPreviousError (DeclaringType);
Report.Warning (1690, 1, loc, "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
GetSignatureForError ());
}
}
- public bool VerifyFixed ()
- {
- IVariable variable = InstanceExpression as IVariable;
- // A variable of the form V.I is fixed when V is a fixed variable of a struct type.
- // We defer the InstanceExpression check after the variable check to avoid a
- // separate null check on InstanceExpression.
- return variable != null && InstanceExpression.Type.IsValueType && variable.VerifyFixed ();
- }
-
public override int GetHashCode ()
{
return FieldInfo.GetHashCode ();
}
+
+ public bool IsFixed {
+ get {
+ IVariable variable = InstanceExpression as IVariable;
+ // A variable of the form V.I is fixed when V is a fixed variable of a struct type.
+ // We defer the InstanceExpression check after the variable check to avoid a
+ // separate null check on InstanceExpression.
+ return variable != null && InstanceExpression.Type.IsValueType && variable.IsFixed;
+ }
+ }
public override bool Equals (object obj)
{
return;
}
- //
- // String concatenation creates a new string instance
- //
- prepared = prepare_for_load && !(source is StringConcat);
+ prepared = prepare_for_load;
EmitInstance (ec, prepared);
source.Emit (ec);
Emit (ec, false);
}
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ FieldBase f = TypeManager.GetField (FieldInfo);
+ bool is_volatile = f != null && (f.ModFlags & Modifiers.VOLATILE) != 0;
+
+ if (is_volatile || is_marshal_by_ref ())
+ base.EmitSideEffect (ec);
+ }
+
public void AddressOf (EmitContext ec, AddressOp mode)
{
ILGenerator ig = ec.ig;
public override Expression CreateExpressionTree (EmitContext ec)
{
+ ArrayList args;
if (IsSingleDimensionalArrayLength ()) {
- ArrayList args = new ArrayList (1);
+ args = new ArrayList (1);
args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
return CreateExpressionFactoryCall ("ArrayLength", args);
}
- // TODO: it's waiting for PropertyExpr refactoring
- //ArrayList args = new ArrayList (2);
- //args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
- //args.Add (getter expression);
- //return CreateExpressionFactoryCall ("Property", args);
- return base.CreateExpressionTree (ec);
+ if (is_base) {
+ Error_BaseAccessInExpressionTree (loc);
+ return null;
+ }
+
+ args = new ArrayList (2);
+ if (InstanceExpression == null)
+ args.Add (new Argument (new NullLiteral (loc)));
+ else
+ args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
+ args.Add (new Argument (new TypeOfMethodInfo (getter, loc)));
+ return CreateExpressionFactoryCall ("Property", args);
+ }
+
+ public Expression CreateSetterTypeOfExpression ()
+ {
+ return new TypeOfMethodInfo (setter, loc);
}
public override Type DeclaringType {
string t_name = InstanceExpression.Type.Name;
int t_name_len = t_name.Length;
- return t_name_len > 2 && t_name [t_name_len - 2] == '[' && t_name [t_name_len - 3] != ']';
+ return t_name_len > 2 && t_name [t_name_len - 2] == '[';
}
override public Expression DoResolve (EmitContext ec)
Expression my_source = source;
if (prepare_for_load) {
- if (source is StringConcat)
- EmitInstance (ec, false);
- else
- prepared = true;
-
+ prepared = true;
source.Emit (ec);
- prepared = true;
if (leave_copy) {
ec.ig.Emit (OpCodes.Dup);
if (!is_static) {
return base.ResolveMemberAccess (ec, left, loc, original);
}
-
bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
{
if (is_static) {
IsAccessorAccessible (invocation_type, remove_accessor, out dummy);
}
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ throw new NotSupportedException ("ET");
+ }
+
public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
- return DoResolve (ec);
+ // contexts where an LValue is valid have already devolved to FieldExprs
+ Error_CannotAssign ();
+ return null;
}
public override Expression DoResolve (EmitContext ec)
if (!InstanceResolve (ec, must_do_cs1540_check))
return null;
+
+ if (!ec.IsInCompoundAssignment) {
+ Error_CannotAssign ();
+ return null;
+ }
return this;
}
public override void Emit (EmitContext ec)
{
- Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
- "(except on the defining type)", GetSignatureForError ());
+ Error_CannotAssign ();
+ }
+
+ public void Error_CannotAssign ()
+ {
+ Report.Error (70, loc,
+ "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
+ GetSignatureForError (), TypeManager.CSharpName (EventInfo.DeclaringType));
}
public override string GetSignatureForError ()
return TypeManager.CSharpSignature (EventInfo);
}
- public void EmitAddOrRemove (EmitContext ec, Expression source)
+ public void EmitAddOrRemove (EmitContext ec, bool is_add, Expression source)
{
- BinaryDelegate source_del = source as BinaryDelegate;
- if (source_del == null) {
- Emit (ec);
- return;
- }
- Expression handler = source_del.Right;
-
- Argument arg = new Argument (handler, Argument.AType.Expression);
- ArrayList args = new ArrayList ();
-
- args.Add (arg);
-
- if (source_del.IsAddition)
- Invocation.EmitCall (
- ec, IsBase, InstanceExpression, add_accessor, args, loc);
- else
- Invocation.EmitCall (
- ec, IsBase, InstanceExpression, remove_accessor, args, loc);
+ ArrayList args = new ArrayList (1);
+ args.Add (new Argument (source, Argument.AType.Expression));
+ Invocation.EmitCall (ec, IsBase, InstanceExpression, is_add ? add_accessor : remove_accessor, args, loc);
}
}
- public class TemporaryVariable : Expression, IMemoryLocation
+ public class TemporaryVariable : VariableReference
{
LocalInfo li;
Variable var;
-
+
public TemporaryVariable (Type type, Location loc)
{
this.type = type;
this.loc = loc;
- eclass = ExprClass.Value;
+ eclass = ExprClass.Variable;
}
-
+
+ public override Expression CreateExpressionTree (EmitContext ec)
+ {
+ throw new NotSupportedException ("ET");
+ }
+
public override Expression DoResolve (EmitContext ec)
{
if (li != null)
return this;
-
+
TypeExpr te = new TypeExpression (type, loc);
li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
if (!li.Resolve (ec))
var = scope.AddLocal (li);
type = var.Type;
}
-
+
return this;
}
- public Variable Variable {
- get { return var != null ? var : li.Variable; }
- }
-
public override void Emit (EmitContext ec)
{
- Variable.EmitInstance (ec);
- Variable.Emit (ec);
+ Emit (ec, false);
}
-
- public void EmitLoadAddress (EmitContext ec)
+
+ public void EmitAssign (EmitContext ec, Expression source)
{
- Variable.EmitInstance (ec);
- Variable.EmitAddressOf (ec);
+ EmitAssign (ec, source, false, false);
}
-
- public void Store (EmitContext ec, Expression right_side)
- {
- Variable.EmitInstance (ec);
- right_side.Emit (ec);
- Variable.EmitAssign (ec);
+
+ public override bool IsFixed {
+ get { return true; }
}
-
- public void EmitThis (EmitContext ec)
- {
- Variable.EmitInstance (ec);
+
+ public override bool IsRef {
+ get { return false; }
}
-
- public void EmitStore (EmitContext ec)
- {
- Variable.EmitAssign (ec);
+
+ public override Variable Variable {
+ get { return var != null ? var : li.Variable; }
}
-
- public void AddressOf (EmitContext ec, AddressOp mode)
- {
- EmitLoadAddress (ec);
+
+ public override VariableInfo VariableInfo {
+ get { throw new NotImplementedException (); }
}
}
}
}
- public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ public bool InferType (EmitContext ec, Expression right_side)
{
if (type != null)
throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
if (type == TypeManager.null_type || type == TypeManager.void_type || type == TypeManager.anonymous_method_type) {
Report.Error (815, loc, "An implicitly typed local variable declaration cannot be initialized with `{0}'",
right_side.GetSignatureForError ());
- return null;
+ return false;
}
eclass = ExprClass.Variable;
- return this;
+ return true;
}
protected override void Error_TypeOrNamespaceNotFound (IResolveContext ec)