using System.Reflection.Emit;
using System.Text;
+ // <summary>
+ // This is just a helper class, it is generated by Unary, UnaryMutator
+ // when an overloaded method has been found. It just emits the code for a
+ // static call.
+ // </summary>
+ public class StaticCallExpr : ExpressionStatement {
+ ArrayList args;
+ MethodInfo mi;
+
+ StaticCallExpr (MethodInfo m, ArrayList a)
+ {
+ mi = m;
+ args = a;
+
+ type = m.ReturnType;
+ eclass = ExprClass.Value;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ //
+ // We are born fully resolved
+ //
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ if (args != null)
+ Invocation.EmitArguments (ec, mi, args);
+
+ ec.ig.Emit (OpCodes.Call, mi);
+ return;
+ }
+
+ static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
+ Expression e, Location loc)
+ {
+ ArrayList args;
+ MethodBase method;
+
+ args = new ArrayList (1);
+ args.Add (new Argument (e, Argument.AType.Expression));
+ method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
+
+ if (method == null)
+ return null;
+
+ return new StaticCallExpr ((MethodInfo) method, args);
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ Emit (ec);
+ if (type != TypeManager.void_type)
+ ec.ig.Emit (OpCodes.Pop);
+ }
+ }
+
// <summary>
// Unary expressions.
// </summary>
// ExpressionStatement becuase the pre/post increment/decrement
// operators can be used in a statement context.
// </remarks>
- public class Unary : ExpressionStatement {
- public enum Operator {
+ public class Unary : Expression {
+ public enum Operator : byte {
UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
- Indirection, AddressOf, PreIncrement,
- PreDecrement, PostIncrement, PostDecrement
+ Indirection, AddressOf,
}
Operator oper;
Expression expr;
- ArrayList Arguments;
- MethodBase method;
Location loc;
public Unary (Operator op, Expression expr, Location loc)
return "&";
case Operator.Indirection:
return "*";
- case Operator.PreIncrement : case Operator.PostIncrement :
- return "++";
- case Operator.PreDecrement : case Operator.PostDecrement :
- return "--";
}
return oper.ToString ();
}
- Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
- {
- if (expr.Type == target_type)
- return expr;
-
- return ConvertImplicit (ec, expr, target_type, new Location (-1));
- }
-
void error23 (Type t)
{
Report.Error (
TypeManager.CSharpName (t) + "'");
}
- // <summary>
- // Returns whether an object of type `t' can be incremented
- // or decremented with add/sub (ie, basically whether we can
- // use pre-post incr-decr operations on it, but it is not a
- // System.Decimal, which we test elsewhere)
- // </summary>
- static bool IsIncrementableNumber (Type t)
- {
- return (t == TypeManager.sbyte_type) ||
- (t == TypeManager.byte_type) ||
- (t == TypeManager.short_type) ||
- (t == TypeManager.ushort_type) ||
- (t == TypeManager.int32_type) ||
- (t == TypeManager.uint32_type) ||
- (t == TypeManager.int64_type) ||
- (t == TypeManager.uint64_type) ||
- (t == TypeManager.char_type) ||
- (t.IsSubclassOf (TypeManager.enum_type)) ||
- (t == TypeManager.float_type) ||
- (t == TypeManager.double_type);
- }
-
static Expression TryReduceNegative (Expression expr)
{
Expression e = null;
Expression mg;
string op_name;
- if (oper == Operator.PostIncrement || oper == Operator.PreIncrement)
- op_name = "op_Increment";
- else if (oper == Operator.PostDecrement || oper == Operator.PreDecrement)
- op_name = "op_Decrement";
- else
- op_name = "op_" + oper;
+ op_name = "op_" + oper;
mg = MemberLookup (ec, expr_type, op_name, false, loc);
mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
if (mg != null) {
- Arguments = new ArrayList ();
- Arguments.Add (new Argument (expr, Argument.AType.Expression));
-
- method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg,
- Arguments, loc);
- if (method != null) {
- MethodInfo mi = (MethodInfo) method;
- type = mi.ReturnType;
- return this;
- } else {
+ Expression e = StaticCallExpr.MakeSimpleCall (
+ ec, (MethodGroupExpr) mg, expr, loc);
+
+ if (e == null){
error23 (expr_type);
return null;
}
-
+
+ return e;
}
//
//
// Only perform numeric promotions on:
- // +, -, ++, --
+ // +, -
if (expr_type == null)
return null;
return null;
}
- //
- // The operand of the prefix/postfix increment decrement operators
- // should be an expression that is classified as a variable,
- // a property access or an indexer access
- //
- if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
- oper == Operator.PostDecrement || oper == Operator.PostIncrement){
- if (expr.ExprClass == ExprClass.Variable){
- if (IsIncrementableNumber (expr_type) ||
- expr_type == TypeManager.decimal_type){
- type = expr_type;
- return this;
- }
- } else if (expr.ExprClass == ExprClass.IndexerAccess){
- //
- // FIXME: Verify that we have both get and set methods
- //
- throw new Exception ("Implement me");
- } else if (expr.ExprClass == ExprClass.PropertyAccess){
- PropertyExpr pe = (PropertyExpr) expr;
-
- if (pe.VerifyAssignable ())
- return this;
- return null;
- } else {
- report118 (loc, expr, "variable, indexer or property access");
- }
- }
-
if (oper == Operator.AddressOf){
if (expr.ExprClass != ExprClass.Variable){
- Error (211, "Cannot take the address of non-variables");
+ Error (211, loc, "Cannot take the address of non-variables");
return null;
}
type = Type.GetType (expr.Type.ToString () + "*");
+
+ return this;
}
- Error (187, "No such operator '" + OperName () + "' defined for type '" +
+ Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
TypeManager.CSharpName (expr_type) + "'");
return null;
-
}
public override Expression DoResolve (EmitContext ec)
{
ILGenerator ig = ec.ig;
Type expr_type = expr.Type;
- ExprClass eclass;
-
- if (method != null) {
-
- // Note that operators are static anyway
-
- if (Arguments != null)
- Invocation.EmitArguments (ec, Arguments);
-
- //
- // Post increment/decrement operations need a copy at this
- // point.
- //
- if (oper == Operator.PostDecrement || oper == Operator.PostIncrement)
- ig.Emit (OpCodes.Dup);
-
-
- ig.Emit (OpCodes.Call, (MethodInfo) method);
-
- //
- // Pre Increment and Decrement operators
- //
- if (oper == Operator.PreIncrement || oper == Operator.PreDecrement){
- ig.Emit (OpCodes.Dup);
- }
-
- //
- // Increment and Decrement should store the result
- //
- if (oper == Operator.PreDecrement || oper == Operator.PreIncrement ||
- oper == Operator.PostDecrement || oper == Operator.PostIncrement){
- ((IStackStore) expr).Store (ec);
- }
- return;
- }
switch (oper) {
case Operator.UnaryPlus:
case Operator.Indirection:
throw new Exception ("Not implemented yet");
- case Operator.PreIncrement:
- case Operator.PreDecrement:
- if (expr.ExprClass == ExprClass.Variable){
- //
- // Resolve already verified that it is an "incrementable"
- //
- expr.Emit (ec);
- ig.Emit (OpCodes.Ldc_I4_1);
-
- if (oper == Operator.PreDecrement)
- ig.Emit (OpCodes.Sub);
- else
- ig.Emit (OpCodes.Add);
- ig.Emit (OpCodes.Dup);
- ((IStackStore) expr).Store (ec);
- } else {
- throw new Exception ("Handle Indexers and Properties here");
- }
- break;
-
- case Operator.PostIncrement:
- case Operator.PostDecrement:
- eclass = expr.ExprClass;
- if (eclass == ExprClass.Variable){
- //
- // Resolve already verified that it is an "incrementable"
- //
- expr.Emit (ec);
- ig.Emit (OpCodes.Dup);
- ig.Emit (OpCodes.Ldc_I4_1);
-
- if (oper == Operator.PostDecrement)
- ig.Emit (OpCodes.Sub);
- else
- ig.Emit (OpCodes.Add);
- ((IStackStore) expr).Store (ec);
- } else if (eclass == ExprClass.PropertyAccess){
- throw new Exception ("Handle Properties here");
- } else if (eclass == ExprClass.IndexerAccess) {
- throw new Exception ("Handle Indexers here");
- } else {
- Console.WriteLine ("Unknown exprclass: " + eclass);
- }
- break;
-
default:
throw new Exception ("This should not happen: Operator = "
+ oper.ToString ());
expr.Emit (ec);
}
- public override void EmitStatement (EmitContext ec)
- {
- //
- // FIXME: we should rewrite this code to generate
- // better code for ++ and -- as we know we wont need
- // the values on the stack
- //
- Emit (ec);
- ec.ig.Emit (OpCodes.Pop);
- }
-
public override Expression Reduce (EmitContext ec)
{
Expression e;
- //
- // We can not reduce expressions that invoke operator overloaded functions.
- //
- if (method != null)
- return this;
-
//
// First, reduce our child. Note that although we handle
//
return this;
}
}
+
+ // <summary>
+ // Unary Mutator expressions (pre and post ++ and --)
+ // </summary>
+ //
+ // <remarks>
+ // UnaryMutator implements ++ and -- expressions. It derives from
+ // ExpressionStatement becuase the pre/post increment/decrement
+ // operators can be used in a statement context.
+ // </remarks>
+ //
+ // FIXME: Idea, we could split this up in two classes, one simpler
+ // for the common case, and one with the extra fields for more complex
+ // classes (indexers require temporary access; overloaded require method)
+ //
+ // Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
+ // PostDecrement, that way we could save the `Mode' byte as well.
+ //
+ public class UnaryMutator : ExpressionStatement {
+ public enum Mode : byte {
+ PreIncrement, PreDecrement, PostIncrement, PostDecrement
+ }
+
+ Mode mode;
+ Location loc;
+ Expression expr;
+ LocalTemporary temp_storage;
+
+ //
+ // This is expensive for the simplest case.
+ //
+ Expression method;
+
+ public UnaryMutator (Mode m, Expression e, Location l)
+ {
+ mode = m;
+ loc = l;
+ expr = e;
+ }
+
+ string OperName ()
+ {
+ return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
+ "++" : "--";
+ }
+
+ void error23 (Type t)
+ {
+ Report.Error (
+ 23, loc, "Operator " + OperName () +
+ " cannot be applied to operand of type `" +
+ TypeManager.CSharpName (t) + "'");
+ }
+
+ // <summary>
+ // Returns whether an object of type `t' can be incremented
+ // or decremented with add/sub (ie, basically whether we can
+ // use pre-post incr-decr operations on it, but it is not a
+ // System.Decimal, which we require operator overloading to catch)
+ // </summary>
+ static bool IsIncrementableNumber (Type t)
+ {
+ return (t == TypeManager.sbyte_type) ||
+ (t == TypeManager.byte_type) ||
+ (t == TypeManager.short_type) ||
+ (t == TypeManager.ushort_type) ||
+ (t == TypeManager.int32_type) ||
+ (t == TypeManager.uint32_type) ||
+ (t == TypeManager.int64_type) ||
+ (t == TypeManager.uint64_type) ||
+ (t == TypeManager.char_type) ||
+ (t.IsSubclassOf (TypeManager.enum_type)) ||
+ (t == TypeManager.float_type) ||
+ (t == TypeManager.double_type);
+ }
+
+ Expression ResolveOperator (EmitContext ec)
+ {
+ Type expr_type = expr.Type;
+
+ //
+ // Step 1: Perform Operator Overload location
+ //
+ Expression mg;
+ string op_name;
+
+ if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
+ op_name = "op_Increment";
+ else
+ op_name = "op_Decrement";
+
+ mg = MemberLookup (ec, expr_type, op_name, false, loc);
+
+ if (mg == null && expr_type.BaseType != null)
+ mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
+
+ if (mg != null) {
+ method = StaticCallExpr.MakeSimpleCall (
+ ec, (MethodGroupExpr) mg, expr, loc);
+
+ type = method.Type;
+ return this;
+ }
+
+ //
+ // The operand of the prefix/postfix increment decrement operators
+ // should be an expression that is classified as a variable,
+ // a property access or an indexer access
+ //
+ type = expr_type;
+ if (expr.ExprClass == ExprClass.Variable){
+ if (IsIncrementableNumber (expr_type) ||
+ expr_type == TypeManager.decimal_type){
+ return this;
+ }
+ } else if (expr.ExprClass == ExprClass.IndexerAccess){
+ IndexerAccess ia = (IndexerAccess) expr;
+
+ temp_storage = new LocalTemporary (ec, expr.Type);
+
+ expr = ia.ResolveLValue (ec, temp_storage);
+ if (expr == null)
+ return null;
+
+ return this;
+ } else if (expr.ExprClass == ExprClass.PropertyAccess){
+ PropertyExpr pe = (PropertyExpr) expr;
+
+ if (pe.VerifyAssignable ())
+ return this;
+
+ return null;
+ } else {
+ report118 (loc, expr, "variable, indexer or property access");
+ return null;
+ }
+
+ Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
+ TypeManager.CSharpName (expr_type) + "'");
+ return null;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ expr = expr.Resolve (ec);
+
+ if (expr == null)
+ return null;
+
+ eclass = ExprClass.Value;
+ return ResolveOperator (ec);
+ }
+
+
+ //
+ // FIXME: We need some way of avoiding the use of temp_storage
+ // for some types of storage (parameters, local variables,
+ // static fields) and single-dimension array access.
+ //
+ void EmitCode (EmitContext ec, bool is_expr)
+ {
+ ILGenerator ig = ec.ig;
+ IAssignMethod ia = (IAssignMethod) expr;
+
+ if (temp_storage == null)
+ temp_storage = new LocalTemporary (ec, expr.Type);
+
+ switch (mode){
+ case Mode.PreIncrement:
+ case Mode.PreDecrement:
+ if (method == null){
+ expr.Emit (ec);
+
+ ig.Emit (OpCodes.Ldc_I4_1);
+
+ if (mode == Mode.PreDecrement)
+ ig.Emit (OpCodes.Sub);
+ else
+ ig.Emit (OpCodes.Add);
+ } else
+ method.Emit (ec);
+
+ temp_storage.Store (ec);
+ ia.EmitAssign (ec, temp_storage);
+ if (is_expr)
+ temp_storage.Emit (ec);
+ break;
+
+ case Mode.PostIncrement:
+ case Mode.PostDecrement:
+ if (is_expr)
+ expr.Emit (ec);
+
+ if (method == null){
+ if (!is_expr)
+ expr.Emit (ec);
+ else
+ ig.Emit (OpCodes.Dup);
+
+ ig.Emit (OpCodes.Ldc_I4_1);
+
+ if (mode == Mode.PostDecrement)
+ ig.Emit (OpCodes.Sub);
+ else
+ ig.Emit (OpCodes.Add);
+ } else {
+ method.Emit (ec);
+ }
+
+ temp_storage.Store (ec);
+ ia.EmitAssign (ec, temp_storage);
+ break;
+ }
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ EmitCode (ec, true);
+
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ EmitCode (ec, false);
+ }
+
+ }
public class Probe : Expression {
public readonly string ProbeType;
Expression expr;
Type probe_type;
- public enum Operator {
+ public enum Operator : byte {
Is, As
}
}
public class Binary : Expression {
- public enum Operator {
+ public enum Operator : byte {
Multiply, Division, Modulus,
Addition, Subtraction,
LeftShift, RightShift,
// Note that handling the case l == Decimal || r == Decimal
// is taken care of by the Step 1 Operator Overload resolution.
//
- void DoNumericPromotions (EmitContext ec, Type l, Type r)
+ bool DoNumericPromotions (EmitContext ec, Type l, Type r)
{
if (l == TypeManager.double_type || r == TypeManager.double_type){
//
Expression l_tmp, r_tmp;
l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
- if (l_tmp == null) {
- error19 ();
- left = l_tmp;
- return;
- }
+ if (l_tmp == null)
+ return false;
r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
- if (r_tmp == null) {
- error19 ();
- right = r_tmp;
- return;
- }
+ if (r_tmp == null)
+ return false;
+
+ left = l_tmp;
+ right = r_tmp;
type = TypeManager.int32_type;
}
+
+ return true;
}
void error19 ()
type = TypeManager.bool_type;
return this;
}
- //
- // fall here.
- //
+
}
//
// We are dealing with numbers
//
- DoNumericPromotions (ec, l, r);
+ if (!DoNumericPromotions (ec, l, r)){
+ // Attempt:
+ //
+ // operator != (object a, object b)
+ // operator == (object a, object b)
+ //
+
+ if (oper == Operator.Equality || oper == Operator.Inequality){
+ Expression li, ri;
+ li = ConvertImplicit (ec, left, TypeManager.object_type, loc);
+ if (li != null){
+ ri = ConvertImplicit (ec, right, TypeManager.object_type,
+ loc);
+ if (ri != null){
+ left = li;
+ right = ri;
+
+ type = TypeManager.bool_type;
+ return this;
+ }
+ }
+ }
+
+ error19 ();
+ return null;
+ }
if (left == null || right == null)
return null;
// Note that operators are static anyway
if (Arguments != null)
- Invocation.EmitArguments (ec, Arguments);
+ Invocation.EmitArguments (ec, method, Arguments);
if (method is MethodInfo)
ig.Emit (OpCodes.Call, (MethodInfo) method);
// </summary>
public override Expression Reduce (EmitContext ec)
{
- Console.WriteLine ("Reduce called");
left = left.Reduce (ec);
right = right.Reduce (ec);
}
}
- public class LocalVariableReference : Expression, IStackStore, IMemoryLocation {
+ public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
public readonly string Name;
public readonly Block Block;
break;
}
}
-
- public void Store (EmitContext ec)
+
+ public void EmitAssign (EmitContext ec, Expression source)
{
ILGenerator ig = ec.ig;
VariableInfo vi = VariableInfo;
vi.Assigned = true;
- // Funny seems the above generates optimal code for us, but
+ source.Emit (ec);
+
+ // Funny seems the code below generates optimal code for us, but
// seems to take too long to generate what we need.
// ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
Store (ig, vi.Idx);
}
-
+
public void AddressOf (EmitContext ec)
{
VariableInfo vi = VariableInfo;
}
}
- public class ParameterReference : Expression, IStackStore, IMemoryLocation {
+ public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
public readonly Parameters Pars;
public readonly String Name;
public readonly int Idx;
ec.ig.Emit (OpCodes.Ldarg, arg_idx);
}
- public void Store (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
+ source.Emit (ec);
+
if (arg_idx <= 255)
ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
else
// Used for arguments to New(), Invocation()
// </summary>
public class Argument {
- public enum AType {
+ public enum AType : byte {
Expression,
Ref,
Out
public readonly AType ArgType;
public Expression expr;
-
+
public Argument (Expression expr, AType type)
{
this.expr = expr;
public static string FullDesc (Argument a)
{
- StringBuilder sb = new StringBuilder ();
-
- if (a.ArgType == AType.Ref)
- sb.Append ("ref ");
-
- if (a.ArgType == AType.Out)
- sb.Append ("out ");
-
- sb.Append (TypeManager.CSharpName (a.Expr.Type));
-
- return sb.ToString ();
+ return (a.ArgType == AType.Ref ? "ref " :
+ (a.ArgType == AType.Out ? "out " : "")) +
+ TypeManager.CSharpName (a.Expr.Type);
}
- public bool Resolve (EmitContext ec)
+ public bool Resolve (EmitContext ec, Location loc)
{
expr = expr.Resolve (ec);
+ if (ArgType == AType.Expression)
+ return expr != null;
+
+ if (expr.ExprClass != ExprClass.Variable){
+ Report.Error (206, loc,
+ "A property or indexer can not be passed as an out or ref " +
+ "parameter");
+ return false;
+ }
+
return expr != null;
}
public void Emit (EmitContext ec)
{
- expr.Emit (ec);
+ if (ArgType == AType.Ref || ArgType == AType.Out)
+ ((IMemoryLocation)expr).AddressOf (ec);
+ else
+ expr.Emit (ec);
}
}
// </summary>
public class Invocation : ExpressionStatement {
public readonly ArrayList Arguments;
- public readonly Location Location;
+ Location loc;
Expression expr;
MethodBase method = null;
{
this.expr = expr;
Arguments = arguments;
- Location = l;
+ loc = l;
}
public Expression Expr {
return (ParameterData) ip;
} else {
+ Console.WriteLine ("Getting parameters for: " + mb);
ParameterInfo [] pi = mb.GetParameters ();
ReflectionParameters rp = new ReflectionParameters (pi);
method_parameter_cache [mb] = rp;
bool IsDelegate = TypeManager.IsDelegateType (expr_type);
if (IsDelegate)
return (new DelegateInvocation (
- this.expr, Arguments, Location)).Resolve (ec);
+ this.expr, Arguments, loc)).Resolve (ec);
}
}
if (!(expr is MethodGroupExpr)){
- report118 (Location, this.expr, "method group");
+ report118 (loc, this.expr, "method group");
return null;
}
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (ec))
+ if (!a.Resolve (ec, loc))
return null;
}
}
- method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments,
- Location);
+ method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
if (method == null){
- Error (-6, Location,
+ Error (-6, loc,
"Could not find any applicable function for this argument list");
return null;
}
return this;
}
- public static void EmitArguments (EmitContext ec, ArrayList Arguments)
+ // <summary>
+ // Emits the list of arguments as an array
+ // </summary>
+ static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
+ {
+ ILGenerator ig = ec.ig;
+ int count = arguments.Count - idx;
+ Argument a = (Argument) arguments [idx];
+ Type t = a.expr.Type;
+ string array_type = t.FullName + "[]";
+ LocalBuilder array;
+
+ array = ec.GetTemporaryStorage (Type.GetType (array_type));
+ IntLiteral.EmitInt (ig, count);
+ ig.Emit (OpCodes.Newarr, t);
+ ig.Emit (OpCodes.Stloc, array);
+
+ int top = arguments.Count;
+ for (int j = idx; j < top; j++){
+ ig.Emit (OpCodes.Ldloc, array);
+ IntLiteral.EmitInt (ig, j - idx);
+ a.Emit (ec);
+ ig.Emit (OpCodes.Stelem_Ref);
+ }
+ ig.Emit (OpCodes.Ldloc, array);
+ }
+
+ // <summary>
+ // Emits a list of resolved Arguments that are in the arguments
+ // ArrayList.
+ //
+ // The MethodBase argument might be null if the
+ // emission of the arguments is known not to contain
+ // a `params' field (for example in constructors or other routines
+ // that keep their arguments in this structure
+ // </summary>
+
+ public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
{
+ ParameterData pd = null;
int top;
- if (Arguments != null)
- top = Arguments.Count;
+ if (arguments != null)
+ top = arguments.Count;
else
top = 0;
+ if (mb != null)
+ pd = GetParameterData (mb);
+
for (int i = 0; i < top; i++){
- Argument a = (Argument) Arguments [i];
+ Argument a = (Argument) arguments [i];
+ if (pd != null){
+ if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
+ EmitParams (ec, i, arguments);
+ return;
+ }
+ }
+
a.Emit (ec);
}
}
}
if (Arguments != null)
- EmitArguments (ec, Arguments);
+ EmitArguments (ec, method, Arguments);
if (is_static || struct_call){
if (method is MethodInfo)
public readonly ArrayList Arguments;
public readonly string RequestedType;
- Location Location;
+ Location loc;
MethodBase method = null;
//
//
Expression value_target;
- public New (string requested_type, ArrayList arguments, Location loc)
+ public New (string requested_type, ArrayList arguments, Location l)
{
RequestedType = requested_type;
Arguments = arguments;
- Location = loc;
+ loc = l;
}
public Expression ValueTypeVariable {
bool IsDelegate = TypeManager.IsDelegateType (type);
if (IsDelegate)
- return (new NewDelegate (type, Arguments, Location)).Resolve (ec);
+ return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
Expression ml;
ml = MemberLookup (ec, type, ".ctor", false,
- MemberTypes.Constructor, AllBindingsFlags, Location);
+ MemberTypes.Constructor, AllBindingsFlags, loc);
bool is_struct = false;
is_struct = type.IsSubclassOf (TypeManager.value_type);
if (! (ml is MethodGroupExpr)){
if (!is_struct){
- report118 (Location, ml, "method group");
+ report118 (loc, ml, "method group");
return null;
}
}
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (ec))
+ if (!a.Resolve (ec, loc))
return null;
}
}
method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
- Arguments, Location);
+ Arguments, loc);
}
if (method == null && !is_struct) {
- Error (-6, Location,
+ Error (-6, loc,
"New invocation: Can not find a constructor for " +
"this argument list");
return null;
ml.AddressOf (ec);
} else {
- Invocation.EmitArguments (ec, Arguments);
+ Invocation.EmitArguments (ec, method, Arguments);
ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
return true;
}
string RequestedType;
string Rank;
ArrayList Initializers;
- Location Location;
+ Location loc;
ArrayList Arguments;
MethodBase method = null;
RequestedType = requested_type;
Rank = rank;
Initializers = initializers;
- Location = l;
+ loc = l;
Arguments = new ArrayList ();
RequestedType = requested_type;
Rank = rank;
Initializers = initializers;
- Location = l;
+ loc = l;
}
public static string FormArrayType (string base_type, int idx_count, string rank)
Expression ml;
ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
- AllBindingsFlags, Location);
+ AllBindingsFlags, loc);
if (!(ml is MethodGroupExpr)){
- report118 (Location, ml, "method group");
+ report118 (loc, ml, "method group");
return null;
}
if (ml == null) {
- Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
"this argument list");
return null;
}
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (ec))
+ if (!a.Resolve (ec, loc))
return null;
}
}
- method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, Location);
+ method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
if (method == null) {
- Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
"this argument list");
return null;
}
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (ec))
+ if (!a.Resolve (ec, loc))
return null;
args.Add (a.Type);
arg_types);
if (method == null) {
- Report.Error (-6, Location, "New invocation: Can not find a constructor for " +
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
"this argument list");
return null;
}
ILGenerator ig = ec.ig;
if (IsOneDimensional) {
- Invocation.EmitArguments (ec, Arguments);
+ Invocation.EmitArguments (ec, null, Arguments);
ig.Emit (OpCodes.Newarr, array_element_type);
} else {
- Invocation.EmitArguments (ec, Arguments);
+ Invocation.EmitArguments (ec, method, Arguments);
if (IsBuiltinType)
ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
//
// Represents the `this' construct
//
- public class This : Expression, IStackStore, IMemoryLocation {
+ public class This : Expression, IAssignMethod, IMemoryLocation {
Location loc;
public This (Location loc)
ec.ig.Emit (OpCodes.Ldarg_0);
}
- public void Store (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
+ source.Emit (ec);
ec.ig.Emit (OpCodes.Starg, 0);
}
if (expr is SimpleName){
SimpleName child_expr = (SimpleName) expr;
-
+
expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
return expr.Resolve (ec);
if (member_lookup is FieldExpr){
FieldExpr fe = (FieldExpr) member_lookup;
+ FieldInfo fi = fe.FieldInfo;
+ if (fi.IsLiteral) {
+ Type t = fi.FieldType;
+ object o;
+
+ if (fi is FieldBuilder)
+ o = TypeManager.GetValue ((FieldBuilder) fi);
+ else
+ o = fi.GetValue (fi);
+
+ if (t.IsSubclassOf (TypeManager.enum_type)) {
+ Expression enum_member = MemberLookup (ec, t, "value__", false, loc);
+ Type underlying_type = enum_member.Type;
+
+ Expression e = Literalize (o, underlying_type);
+ e.Resolve (ec);
+
+ return new EnumLiteral (e, t);
+ }
+
+ Expression exp = Literalize (o, t);
+ exp.Resolve (ec);
+
+ return exp;
+ }
+
if (expr is TypeExpr){
if (!fe.FieldInfo.IsStatic){
error176 (loc, fe.FieldInfo.Name);
}
public class ElementAccess : Expression {
-
public ArrayList Arguments;
public Expression Expr;
public Location loc;
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (ec))
+ if (!a.Resolve (ec, loc))
return false;
}
//
// I am experimenting with this pattern.
//
- if (Expr.Type == TypeManager.array_type)
+ if (Expr.Type.IsSubclassOf (TypeManager.array_type))
return (new ArrayAccess (this)).Resolve (ec);
else
return (new IndexerAccess (this)).Resolve (ec);
if (!CommonResolve (ec))
return null;
- if (Expr.Type == TypeManager.array_type)
+ if (Expr.Type.IsSubclassOf (TypeManager.array_type))
return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
else
return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
}
}
- public class ArrayAccess : Expression, IStackStore {
+ //
+ // Implements array access
+ //
+ public class ArrayAccess : Expression, IAssignMethod {
//
// Points to our "data" repository
//
{
ea = ea_data;
eclass = ExprClass.Variable;
-
- //
- // FIXME: Figure out the type here
- //
}
- Expression CommonResolve (EmitContext ec)
- {
- return this;
- }
-
public override Expression DoResolve (EmitContext ec)
{
if (ea.Expr.ExprClass != ExprClass.Variable) {
report118 (ea.loc, ea.Expr, "variable");
return null;
}
-
- throw new Exception ("Implement me");
+
+ Type t = ea.Expr.Type;
+
+ if (t.GetArrayRank () != ea.Arguments.Count){
+ Report.Error (22, ea.loc,
+ "Incorrect number of indexes for array " +
+ " expected: " + t.GetArrayRank () + " got: " +
+ ea.Arguments.Count);
+ return null;
+ }
+ type = t.GetElementType ();
+ eclass = ExprClass.Variable;
+
+ return this;
}
- public void Store (EmitContext ec)
+ // <summary>
+ // Emits the right opcode to load an object of Type `t'
+ // from an array of T
+ // </summary>
+ static public void EmitLoadOpcode (ILGenerator ig, Type type)
+ {
+ if (type == TypeManager.byte_type)
+ ig.Emit (OpCodes.Ldelem_I1);
+ else if (type == TypeManager.sbyte_type)
+ ig.Emit (OpCodes.Ldelem_U1);
+ else if (type == TypeManager.short_type)
+ ig.Emit (OpCodes.Ldelem_I2);
+ else if (type == TypeManager.ushort_type)
+ ig.Emit (OpCodes.Ldelem_U2);
+ else if (type == TypeManager.int32_type)
+ ig.Emit (OpCodes.Ldelem_I4);
+ else if (type == TypeManager.uint32_type)
+ ig.Emit (OpCodes.Ldelem_U4);
+ else if (type == TypeManager.uint64_type)
+ ig.Emit (OpCodes.Ldelem_I8);
+ else if (type == TypeManager.int64_type)
+ ig.Emit (OpCodes.Ldelem_I8);
+ else if (type == TypeManager.float_type)
+ ig.Emit (OpCodes.Ldelem_R4);
+ else if (type == TypeManager.double_type)
+ ig.Emit (OpCodes.Ldelem_R8);
+ else if (type == TypeManager.intptr_type)
+ ig.Emit (OpCodes.Ldelem_I);
+ else
+ ig.Emit (OpCodes.Ldelem_Ref);
+ }
+
+ public override void Emit (EmitContext ec)
{
- throw new Exception ("Implement me !");
+ int rank = ea.Expr.Type.GetArrayRank ();
+ ILGenerator ig = ec.ig;
+
+ ea.Expr.Emit (ec);
+
+ foreach (Argument a in ea.Arguments)
+ a.Expr.Emit (ec);
+
+ if (rank == 1)
+ EmitLoadOpcode (ig, type);
+ else {
+ ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
+ Type [] args = new Type [ea.Arguments.Count];
+ MethodInfo get;
+
+ int i = 0;
+
+ foreach (Argument a in ea.Arguments)
+ args [i++] = a.Type;
+
+ get = mb.GetArrayMethod (
+ ea.Expr.Type, "Get",
+ CallingConventions.HasThis |
+ CallingConventions.Standard,
+ type, args);
+
+ ig.Emit (OpCodes.Call, get);
+ }
}
- public override void Emit (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
- throw new Exception ("Implement me !");
+ int rank = ea.Expr.Type.GetArrayRank ();
+ ILGenerator ig = ec.ig;
+
+ ea.Expr.Emit (ec);
+
+ foreach (Argument a in ea.Arguments)
+ a.Expr.Emit (ec);
+
+ source.Emit (ec);
+
+ Type t = source.Type;
+ if (rank == 1){
+ if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
+ ig.Emit (OpCodes.Stelem_I1);
+ else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
+ ig.Emit (OpCodes.Stelem_I2);
+ else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
+ ig.Emit (OpCodes.Stelem_I4);
+ else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
+ ig.Emit (OpCodes.Stelem_I8);
+ else if (t == TypeManager.float_type)
+ ig.Emit (OpCodes.Stelem_R4);
+ else if (t == TypeManager.double_type)
+ ig.Emit (OpCodes.Stelem_R8);
+ else if (t == TypeManager.intptr_type)
+ ig.Emit (OpCodes.Stelem_I);
+ else
+ ig.Emit (OpCodes.Stelem_Ref);
+ } else {
+ ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
+ Type [] args = new Type [ea.Arguments.Count + 1];
+ MethodInfo set;
+
+ int i = 0;
+
+ foreach (Argument a in ea.Arguments)
+ args [i++] = a.Type;
+
+ args [i] = type;
+
+ set = mb.GetArrayMethod (
+ ea.Expr.Type, "Set",
+ CallingConventions.HasThis |
+ CallingConventions.Standard,
+ TypeManager.void_type, args);
+
+ ig.Emit (OpCodes.Call, set);
+ }
}
}
-
class Indexers {
public ArrayList getters, setters;
static Hashtable map;
return ix;
}
}
-
+
+ // <summary>
+ // Expressions that represent an indexer call.
+ // </summary>
public class IndexerAccess : Expression, IAssignMethod {
//
// Points to our "data" repository
eclass = ExprClass.Value;
}
- public bool VerifyAssignable (Expression source)
- {
- throw new Exception ("Implement me!");
- }
-
public override Expression DoResolve (EmitContext ec)
{
Type indexer_type = ea.Expr.Type;
if (ilist == null)
ilist = Indexers.GetIndexersForType (
indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
-
+
+
+ //
+ // Step 2: find the proper match
+ //
if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
get = (MethodInfo) Invocation.OverloadResolve (
ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
Report.Error (154, ea.loc,
"indexer can not be used in this context, because " +
"it lacks a `get' accessor");
- return null;
+ return null;
}
-
+
type = get.ReturnType;
- eclass = ExprClass.Value;
+ eclass = ExprClass.IndexerAccess;
return this;
}
public class BaseAccess : Expression {
- public enum BaseAccessType {
+ public enum BaseAccessType : byte {
Member,
Indexer
};
eclass = ExprClass.Value;
}
+ public EmptyExpression (Type t)
+ {
+ type = t;
+ eclass = ExprClass.Value;
+ }
+
public override Expression DoResolve (EmitContext ec)
{
return this;