// (C) 2001 Ximian, Inc.
//
//
-// Ideas:
-// Maybe we should make Resolve be an instance method that just calls
-// the virtual DoResolve function and checks conditions like the eclass
-// and type being set if a non-null value is returned. For robustness
-// purposes.
-//
-
-namespace CIR {
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Text;
-
- // <remarks>
- // The ExprClass class contains the is used to pass the
- // classification of an expression (value, variable, namespace,
- // type, method group, property access, event access, indexer access,
- // nothing).
- // </remarks>
- public enum ExprClass {
- Invalid,
-
- Value,
- Variable, // Every Variable should implement LValue
- Namespace,
- Type,
- MethodGroup,
- PropertyAccess,
- EventAccess,
- IndexerAccess,
- Nothing,
- }
-
- // <remarks>
- // Base class for expressions
- // </remarks>
- public abstract class Expression {
- protected ExprClass eclass;
- protected Type type;
-
- public Type Type {
- get {
- return type;
- }
-
- set {
- type = value;
- }
- }
-
- public ExprClass ExprClass {
- get {
- return eclass;
- }
-
- set {
- eclass = value;
- }
- }
-
- // <summary>
- // Utility wrapper routine for Error, just to beautify the code
- // </summary>
- static protected void Error (TypeContainer tc, int error, string s)
- {
- Report.Error (error, s);
- }
-
- static protected void Error (TypeContainer tc, int error, Location l, string s)
- {
- Report.Error (error, l, s);
- }
-
- // <summary>
- // Utility wrapper routine for Warning, just to beautify the code
- // </summary>
- static protected void Warning (TypeContainer tc, int warning, string s)
- {
- Report.Warning (warning, s);
- }
-
- // <summary>
- // Performs semantic analysis on the Expression
- // </summary>
- //
- // <remarks>
- // The Resolve method is invoked to perform the semantic analysis
- // on the node.
- //
- // The return value is an expression (it can be the
- // same expression in some cases) or a new
- // expression that better represents this node.
- //
- // For example, optimizations of Unary (LiteralInt)
- // would return a new LiteralInt with a negated
- // value.
- //
- // If there is an error during semantic analysis,
- // then an error should
- // be reported (using TypeContainer.RootContext.Report) and a null
- // value should be returned.
- //
- // There are two side effects expected from calling
- // Resolve(): the the field variable "eclass" should
- // be set to any value of the enumeration
- // `ExprClass' and the type variable should be set
- // to a valid type (this is the type of the
- // expression).
- // </remarks>
-
- public abstract Expression DoResolve (TypeContainer tc);
-
-
- //
- // Currently Resolve wraps DoResolve to perform sanity
- // checking and assertion checking on what we expect from Resolve
- //
-
- public Expression Resolve (TypeContainer tc)
- {
- Expression e = DoResolve (tc);
-
- if (e != null){
- if (e.ExprClass == ExprClass.Invalid)
- throw new Exception ("Expression " + e +
- " ExprClass is Invalid after resolve");
-
- if (e.ExprClass != ExprClass.MethodGroup)
- if (e.type == null)
- throw new Exception ("Expression " + e +
- " did not set its type after Resolve");
- }
-
- return e;
- }
-
- // <summary>
- // Emits the code for the expression
- // </summary>
- //
- // <remarks>
- //
- // The Emit method is invoked to generate the code
- // for the expression.
- //
- // </remarks>
- public abstract void Emit (EmitContext ec);
-
- // <summary>
- // Protected constructor. Only derivate types should
- // be able to be created
- // </summary>
-
- protected Expression ()
- {
- eclass = ExprClass.Invalid;
- type = null;
- }
-
- // <summary>
- // Returns a literalized version of a literal FieldInfo
- // </summary>
- static Expression Literalize (FieldInfo fi)
- {
- Type t = fi.FieldType;
- object v = fi.GetValue (fi);
-
- if (t == TypeManager.int32_type)
- return new IntLiteral ((int) v);
- else if (t == TypeManager.uint32_type)
- return new UIntLiteral ((uint) v);
- else if (t == TypeManager.int64_type)
- return new LongLiteral ((long) v);
- else if (t == TypeManager.uint64_type)
- return new ULongLiteral ((ulong) v);
- else if (t == TypeManager.float_type)
- return new FloatLiteral ((float) v);
- else if (t == TypeManager.double_type)
- return new DoubleLiteral ((double) v);
- else if (t == TypeManager.string_type)
- return new StringLiteral ((string) v);
- else if (t == TypeManager.short_type)
- return new IntLiteral ((int) ((short)v));
- else if (t == TypeManager.ushort_type)
- return new IntLiteral ((int) ((ushort)v));
- else if (t == TypeManager.sbyte_type)
- return new IntLiteral ((int) ((sbyte)v));
- else if (t == TypeManager.byte_type)
- return new IntLiteral ((int) ((byte)v));
- else if (t == TypeManager.char_type)
- return new IntLiteral ((int) ((char)v));
- else
- throw new Exception ("Unknown type for literal (" + v.GetType () +
- "), details: " + fi);
- }
-
- //
- // Returns a fully formed expression after a MemberLookup
- //
- static Expression ExprClassFromMemberInfo (TypeContainer tc, MemberInfo mi)
- {
- if (mi is EventInfo){
- return new EventExpr ((EventInfo) mi);
- } else if (mi is FieldInfo){
- FieldInfo fi = (FieldInfo) mi;
-
- if (fi.IsLiteral){
- Expression e = Literalize (fi);
- e.Resolve (tc);
-
- return e;
- } else
- return new FieldExpr (fi);
- } else if (mi is PropertyInfo){
- return new PropertyExpr ((PropertyInfo) mi);
- } else if (mi is Type)
- return new TypeExpr ((Type) mi);
-
- return null;
- }
-
- //
- // FIXME: Probably implement a cache for (t,name,current_access_set)?
- //
- // FIXME: We need to cope with access permissions here, or this wont
- // work!
- //
- // This code could use some optimizations, but we need to do some
- // measurements. For example, we could use a delegate to `flag' when
- // something can not any longer be a method-group (because it is something
- // else).
- //
- // Return values:
- // If the return value is an Array, then it is an array of
- // MethodBases
- //
- // If the return value is an MemberInfo, it is anything, but a Method
- //
- // null on error.
- //
- // FIXME: When calling MemberLookup inside an `Invocation', we should pass
- // the arguments here and have MemberLookup return only the methods that
- // match the argument count/type, unlike we are doing now (we delay this
- // decision).
- //
- // This is so we can catch correctly attempts to invoke instance methods
- // from a static body (scan for error 120 in ResolveSimpleName).
- //
- public static Expression MemberLookup (TypeContainer tc, Type t, string name,
- bool same_type, MemberTypes mt, BindingFlags bf)
- {
- if (same_type)
- bf |= BindingFlags.NonPublic;
-
- MemberInfo [] mi = tc.RootContext.TypeManager.FindMembers (
- t, mt, bf, Type.FilterName, name);
-
- if (mi == null)
- return null;
-
- // FIXME : How does this wierd case arise ?
- if (mi.Length == 0)
- return null;
-
- if (mi.Length == 1 && !(mi [0] is MethodBase))
- return Expression.ExprClassFromMemberInfo (tc, mi [0]);
-
- for (int i = 0; i < mi.Length; i++)
- if (!(mi [i] is MethodBase)){
- Error (tc,
- -5, "Do not know how to reproduce this case: " +
- "Methods and non-Method with the same name, " +
- "report this please");
-
- for (i = 0; i < mi.Length; i++){
- Type tt = mi [i].GetType ();
-
- Console.WriteLine (i + ": " + mi [i]);
- while (tt != TypeManager.object_type){
- Console.WriteLine (tt);
- tt = tt.BaseType;
- }
- }
- }
-
- return new MethodGroupExpr (mi);
- }
-
- public const MemberTypes AllMemberTypes =
- MemberTypes.Constructor |
- MemberTypes.Event |
- MemberTypes.Field |
- MemberTypes.Method |
- MemberTypes.NestedType |
- MemberTypes.Property;
-
- public const BindingFlags AllBindingsFlags =
- BindingFlags.Public |
- BindingFlags.Static |
- BindingFlags.Instance;
-
- public static Expression MemberLookup (TypeContainer tc, Type t, string name,
- bool same_type)
- {
- return MemberLookup (tc, t, name, same_type, AllMemberTypes, AllBindingsFlags);
- }
-
- //
- // I am in general unhappy with this implementation.
- //
- // I need to revise this.
- //
- static public Expression ResolveMemberAccess (TypeContainer tc, string name)
- {
- Expression left_e = null;
- int dot_pos = name.LastIndexOf (".");
- string left = name.Substring (0, dot_pos);
- string right = name.Substring (dot_pos + 1);
- Type t;
-
- if ((t = tc.LookupType (left, false)) != null){
- Expression e;
-
- left_e = new TypeExpr (t);
- e = new MemberAccess (left_e, right);
- return e.Resolve (tc);
- } else {
- //
- // FIXME: IMplement:
-
- // Handle here:
- // T.P Static property access (P) on Type T.
- // e.P instance property access on instance e for P.
- // p
- //
- }
-
- if (left_e == null){
- Error (tc, 246, "Can not find type or namespace `"+left+"'");
- return null;
- }
-
- switch (left_e.ExprClass){
- case ExprClass.Type:
- return MemberLookup (tc,
- left_e.Type, right,
- left_e.Type == tc.TypeBuilder);
-
- case ExprClass.Namespace:
- case ExprClass.PropertyAccess:
- case ExprClass.IndexerAccess:
- case ExprClass.Variable:
- case ExprClass.Value:
- case ExprClass.Nothing:
- case ExprClass.EventAccess:
- case ExprClass.MethodGroup:
- case ExprClass.Invalid:
- throw new Exception ("Should have got the " + left_e.ExprClass +
- " handled before");
- }
-
- return null;
- }
-
- static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
- {
- Type expr_type = expr.Type;
-
- if (target_type == TypeManager.object_type) {
- if (expr_type.IsClass)
- return new EmptyCast (expr, target_type);
- if (expr_type.IsValueType)
- return new BoxedCast (expr);
- } else if (expr_type.IsSubclassOf (target_type)) {
- return new EmptyCast (expr, target_type);
- } else {
- // from any class-type S to any interface-type T.
- if (expr_type.IsClass && target_type.IsInterface) {
- Type [] interfaces = expr_type.FindInterfaces (Module.FilterTypeName,
- target_type.FullName);
- if (interfaces != null)
- return new EmptyCast (expr, target_type);
- }
-
- // from any interface type S to interface-type T.
- // FIXME : Is it right to use IsAssignableFrom ?
- if (expr_type.IsInterface && target_type.IsInterface)
- if (target_type.IsAssignableFrom (expr_type))
- return new EmptyCast (expr, target_type);
-
-
- // from an array-type S to an array-type of type T
- if (expr_type.IsArray && target_type.IsArray) {
-
- throw new Exception ("Implement array conversion");
-
- }
-
- // from an array-type to System.Array
- if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
- return new EmptyCast (expr, target_type);
-
- // from any delegate type to System.Delegate
- if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
- target_type == TypeManager.delegate_type)
- if (target_type.IsAssignableFrom (expr_type))
- return new EmptyCast (expr, target_type);
-
- // from any array-type or delegate type into System.ICloneable.
- if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
- if (target_type == TypeManager.cloneable_interface)
- throw new Exception ("Implement conversion to System.ICloneable");
-
- // from the null type to any reference-type.
- // FIXME : How do we do this ?
-
- return null;
-
- }
-
- return null;
- }
-
- // <summary>
- // Handles expressions like this: decimal d; d = 1;
- // and changes them into: decimal d; d = new System.Decimal (1);
- // </summary>
- static Expression InternalTypeConstructor (TypeContainer tc, Expression expr, Type target)
- {
- ArrayList args = new ArrayList ();
-
- args.Add (new Argument (expr, Argument.AType.Expression));
-
- Expression ne = new New (target.FullName, args,
- new Location (-1));
-
- return ne.Resolve (tc);
- }
-
- // <summary>
- // Implicit Numeric Conversions.
- //
- // expr is the expression to convert, returns a new expression of type
- // target_type or null if an implicit conversion is not possible.
- //
- // </summary>
- static public Expression ImplicitNumericConversion (TypeContainer tc, Expression expr,
- Type target_type, Location l)
- {
- Type expr_type = expr.Type;
-
- //
- // Attempt to do the implicit constant expression conversions
-
- if (expr is IntLiteral){
- Expression e;
-
- e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
- if (e != null)
- return e;
- } else if (expr is LongLiteral){
- //
- // Try the implicit constant expression conversion
- // from long to ulong, instead of a nice routine,
- // we just inline it
- //
- if (((LongLiteral) expr).Value > 0)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- }
-
- if (expr_type == TypeManager.sbyte_type){
- //
- // From sbyte to short, int, long, float, double.
- //
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.byte_type){
- //
- // From byte to short, ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.short_type) ||
- (target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type))
- return new EmptyCast (expr, target_type);
-
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
-
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.short_type){
- //
- // From short to int, long, float, double
- //
- if (target_type == TypeManager.int32_type)
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.ushort_type){
- //
- // From ushort to int, uint, long, ulong, float, double
- //
- if (target_type == TypeManager.uint32_type)
- return new EmptyCast (expr, target_type);
-
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.int32_type){
- //
- // From int to long, float, double
- //
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.uint32_type){
- //
- // From uint to long, ulong, float, double
- //
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
- OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
- OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if ((expr_type == TypeManager.uint64_type) ||
- (expr_type == TypeManager.int64_type)){
- //
- // From long/ulong to float, double
- //
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
- OpCodes.Conv_R8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
- OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.char_type){
- //
- // From char to ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type))
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.float_type){
- //
- // float to double
- //
- if (target_type == TypeManager.double_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
- }
-
- return null;
- }
-
- // <summary>
- // Determines if a standard implicit conversion exists from
- // expr_type to target_type
- // </summary>
- static bool StandardConversionExists (Type expr_type, Type target_type)
- {
- if (expr_type == target_type)
- return true;
-
- // First numeric conversions
-
- if (expr_type == TypeManager.sbyte_type){
- //
- // From sbyte to short, int, long, float, double.
- //
- if ((target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.short_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.byte_type){
- //
- // From byte to short, ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.short_type) ||
- (target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.short_type){
- //
- // From short to int, long, float, double
- //
- if ((target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.ushort_type){
- //
- // From ushort to int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.int32_type){
- //
- // From int to long, float, double
- //
- if ((target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.uint32_type){
- //
- // From uint to long, ulong, float, double
- //
- if ((target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if ((expr_type == TypeManager.uint64_type) ||
- (expr_type == TypeManager.int64_type)) {
- //
- // From long/ulong to float, double
- //
- if ((target_type == TypeManager.double_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.char_type){
- //
- // From char to ushort, int, uint, long, ulong, float, double
- //
- if ((target_type == TypeManager.ushort_type) ||
- (target_type == TypeManager.int32_type) ||
- (target_type == TypeManager.uint32_type) ||
- (target_type == TypeManager.uint64_type) ||
- (target_type == TypeManager.int64_type) ||
- (target_type == TypeManager.float_type) ||
- (target_type == TypeManager.double_type) ||
- (target_type == TypeManager.decimal_type))
- return true;
-
- } else if (expr_type == TypeManager.float_type){
- //
- // float to double
- //
- if (target_type == TypeManager.double_type)
- return true;
- }
-
- // Next reference conversions
-
- if (target_type == TypeManager.object_type) {
- if ((expr_type.IsClass) ||
- (expr_type.IsValueType))
- return true;
-
- } else if (expr_type.IsSubclassOf (target_type)) {
- return true;
-
- } else {
- // from any class-type S to any interface-type T.
- if (expr_type.IsClass && target_type.IsInterface)
- return true;
-
- // from any interface type S to interface-type T.
- // FIXME : Is it right to use IsAssignableFrom ?
- if (expr_type.IsInterface && target_type.IsInterface)
- if (target_type.IsAssignableFrom (expr_type))
- return true;
-
- // from an array-type S to an array-type of type T
- if (expr_type.IsArray && target_type.IsArray)
- return true;
-
- // from an array-type to System.Array
- if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
- return true;
-
- // from any delegate type to System.Delegate
- if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
- target_type == TypeManager.delegate_type)
- if (target_type.IsAssignableFrom (expr_type))
- return true;
-
- // from any array-type or delegate type into System.ICloneable.
- if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
- if (target_type == TypeManager.cloneable_interface)
- return true;
-
- // from the null type to any reference-type.
- // FIXME : How do we do this ?
-
- }
-
- return false;
- }
-
- // <summary>
- // Finds "most encompassed type" according to the spec (13.4.2)
- // amongst the methods in the MethodGroupExpr which convert from a
- // type encompassing source_type
- // </summary>
- static Type FindMostEncompassedType (TypeContainer tc, MethodGroupExpr me, Type source_type)
- {
- Type best = null;
-
- for (int i = me.Methods.Length; i > 0; ) {
- i--;
-
- MethodBase mb = me.Methods [i];
- ParameterData pd = Invocation.GetParameterData (mb);
- Type param_type = pd.ParameterType (0);
-
- if (StandardConversionExists (source_type, param_type)) {
- if (best == null)
- best = param_type;
-
- if (StandardConversionExists (param_type, best))
- best = param_type;
- }
- }
-
- return best;
- }
-
- // <summary>
- // Finds "most encompassing type" according to the spec (13.4.2)
- // amongst the methods in the MethodGroupExpr which convert to a
- // type encompassed by target_type
- // </summary>
- static Type FindMostEncompassingType (TypeContainer tc, MethodGroupExpr me, Type target)
- {
- Type best = null;
-
- for (int i = me.Methods.Length; i > 0; ) {
- i--;
-
- MethodInfo mi = (MethodInfo) me.Methods [i];
- Type ret_type = mi.ReturnType;
-
- if (StandardConversionExists (target, ret_type)) {
- if (best == null)
- best = ret_type;
-
- if (!StandardConversionExists (ret_type, best))
- best = ret_type;
- }
-
- }
-
- return best;
-
- }
-
-
- // <summary>
- // User-defined Implicit conversions
- // </summary>
- static public Expression ImplicitUserConversion (TypeContainer tc, Expression source, Type target,
- Location l)
- {
- return UserDefinedConversion (tc, source, target, l, false);
- }
-
- // <summary>
- // User-defined Explicit conversions
- // </summary>
- static public Expression ExplicitUserConversion (TypeContainer tc, Expression source, Type target,
- Location l)
- {
- return UserDefinedConversion (tc, source, target, l, true);
- }
-
- // <summary>
- // User-defined conversions
- // </summary>
- static public Expression UserDefinedConversion (TypeContainer tc, Expression source,
- Type target, Location l, bool look_for_explicit)
- {
- Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
- Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
- MethodBase method = null;
- Type source_type = source.Type;
-
- string op_name;
-
- // If we have a boolean type, we need to check for the True operator
-
- // FIXME : How does the False operator come into the picture ?
- // FIXME : This doesn't look complete and very correct !
- if (target == TypeManager.bool_type)
- op_name = "op_True";
- else
- op_name = "op_Implicit";
-
- mg1 = MemberLookup (tc, source_type, op_name, false);
-
- if (source_type.BaseType != null)
- mg2 = MemberLookup (tc, source_type.BaseType, op_name, false);
-
- mg3 = MemberLookup (tc, target, op_name, false);
-
- if (target.BaseType != null)
- mg4 = MemberLookup (tc, target.BaseType, op_name, false);
-
- MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
- MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
-
- MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
-
- MethodGroupExpr union4 = null;
-
- if (look_for_explicit) {
-
- op_name = "op_Explicit";
-
- mg5 = MemberLookup (tc, source_type, op_name, false);
-
- if (source_type.BaseType != null)
- mg6 = MemberLookup (tc, source_type.BaseType, op_name, false);
-
- mg7 = MemberLookup (tc, target, op_name, false);
-
- if (target.BaseType != null)
- mg8 = MemberLookup (tc, target.BaseType, op_name, false);
-
- MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
- MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
-
- union4 = Invocation.MakeUnionSet (union5, union6);
- }
-
- MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
-
- if (union != null) {
-
- Type most_specific_source, most_specific_target;
-
- most_specific_source = FindMostEncompassedType (tc, union, source_type);
-
- if (most_specific_source == null)
- return null;
-
-
- most_specific_target = FindMostEncompassingType (tc, union, target);
-
- if (most_specific_target == null)
- return null;
-
-
- int count = 0;
-
- for (int i = union.Methods.Length; i > 0;) {
- i--;
-
- MethodBase mb = union.Methods [i];
- ParameterData pd = Invocation.GetParameterData (mb);
- MethodInfo mi = (MethodInfo) union.Methods [i];
-
- if (pd.ParameterType (0) == most_specific_source &&
- mi.ReturnType == most_specific_target) {
- method = mb;
- count++;
- }
- }
-
- if (method == null || count > 1) {
- Report.Error (-11, l, "Ambiguous user defined conversion");
- return null;
- }
-
-
- return new UserCast ((MethodInfo) method, source, most_specific_source,
- most_specific_target, look_for_explicit);
-
- }
-
- return null;
- }
-
- // <summary>
- // Converts implicitly the resolved expression `expr' into the
- // `target_type'. It returns a new expression that can be used
- // in a context that expects a `target_type'.
- // </summary>
- static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
- Type target_type, Location l)
- {
- Type expr_type = expr.Type;
- Expression e;
-
- if (expr_type == target_type)
- return expr;
-
- e = ImplicitNumericConversion (tc, expr, target_type, l);
- if (e != null)
- return e;
-
- e = ImplicitReferenceConversion (expr, target_type);
- if (e != null)
- return e;
-
- e = ImplicitUserConversion (tc, expr, target_type, l);
- if (e != null)
- return e;
-
- if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
- IntLiteral i = (IntLiteral) expr;
-
- if (i.Value == 0)
- return new EmptyCast (expr, target_type);
- }
-
- return null;
- }
-
-
- // <summary>
- // Attempts to apply the `Standard Implicit
- // Conversion' rules to the expression `expr' into
- // the `target_type'. It returns a new expression
- // that can be used in a context that expects a
- // `target_type'.
- //
- // This is different from `ConvertImplicit' in that the
- // user defined implicit conversions are excluded.
- // </summary>
- static public Expression ConvertImplicitStandard (TypeContainer tc, Expression expr,
- Type target_type, Location l)
- {
- Type expr_type = expr.Type;
- Expression e;
-
- if (expr_type == target_type)
- return expr;
-
- e = ImplicitNumericConversion (tc, expr, target_type, l);
- if (e != null)
- return e;
-
- e = ImplicitReferenceConversion (expr, target_type);
- if (e != null)
- return e;
-
- if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
- IntLiteral i = (IntLiteral) expr;
-
- if (i.Value == 0)
- return new EmptyCast (expr, target_type);
- }
- return null;
- }
- // <summary>
- // Attemps to perform an implict constant conversion of the IntLiteral
- // into a different data type using casts (See Implicit Constant
- // Expression Conversions)
- // </summary>
- static protected Expression TryImplicitIntConversion (Type target_type, IntLiteral il)
- {
- int value = il.Value;
-
- if (target_type == TypeManager.sbyte_type){
- if (value >= SByte.MinValue && value <= SByte.MaxValue)
- return il;
- } else if (target_type == TypeManager.byte_type){
- if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
- return il;
- } else if (target_type == TypeManager.short_type){
- if (value >= Int16.MinValue && value <= Int16.MaxValue)
- return il;
- } else if (target_type == TypeManager.ushort_type){
- if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
- return il;
- } else if (target_type == TypeManager.uint32_type){
- //
- // we can optimize this case: a positive int32
- // always fits on a uint32
- //
- if (value >= 0)
- return il;
- } else if (target_type == TypeManager.uint64_type){
- //
- // we can optimize this case: a positive int32
- // always fits on a uint64. But we need an opcode
- // to do it.
- //
- if (value >= 0)
- return new OpcodeCast (il, target_type, OpCodes.Conv_I8);
- }
-
- return null;
- }
-
- // <summary>
- // Attemptes to implicityly convert `target' into `type', using
- // ConvertImplicit. If there is no implicit conversion, then
- // an error is signaled
- // </summary>
- static public Expression ConvertImplicitRequired (TypeContainer tc, Expression target,
- Type type, Location l)
- {
- Expression e;
-
- e = ConvertImplicit (tc, target, type, l);
- if (e != null)
- return e;
-
- string msg = "Can not convert implicitly from `"+
- TypeManager.CSharpName (target.Type) + "' to `" +
- TypeManager.CSharpName (type) + "'";
-
- Error (tc, 29, l, msg);
-
- return null;
- }
-
- // <summary>
- // Performs the explicit numeric conversions
- // </summary>
- static Expression ConvertNumericExplicit (TypeContainer tc, Expression expr,
- Type target_type)
- {
- Type expr_type = expr.Type;
-
- if (expr_type == TypeManager.sbyte_type){
- //
- // From sbyte to byte, ushort, uint, ulong, char
- //
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.byte_type){
- //
- // From byte to sbyte and char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.short_type){
- //
- // From short to sbyte, byte, ushort, uint, ulong, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.ushort_type){
- //
- // From ushort to sbyte, byte, short, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.int32_type){
- //
- // From int to sbyte, byte, short, ushort, uint, ulong, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.uint32_type)
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.uint32_type){
- //
- // From uint to sbyte, byte, short, ushort, int, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.int32_type)
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.int64_type){
- //
- // From long to sbyte, byte, short, ushort, int, uint, ulong, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.uint64_type)
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.uint64_type){
- //
- // From ulong to sbyte, byte, short, ushort, int, uint, long, char
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.int64_type)
- return new EmptyCast (expr, target_type);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- } else if (expr_type == TypeManager.char_type){
- //
- // From char to sbyte, byte, short
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- } else if (expr_type == TypeManager.float_type){
- //
- // From float to sbyte, byte, short,
- // ushort, int, uint, long, ulong, char
- // or decimal
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- } else if (expr_type == TypeManager.double_type){
- //
- // From double to byte, byte, short,
- // ushort, int, uint, long, ulong,
- // char, float or decimal
- //
- if (target_type == TypeManager.sbyte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
- if (target_type == TypeManager.byte_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
- if (target_type == TypeManager.short_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
- if (target_type == TypeManager.ushort_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.int32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
- if (target_type == TypeManager.uint32_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
- if (target_type == TypeManager.int64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
- if (target_type == TypeManager.uint64_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
- if (target_type == TypeManager.char_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
- if (target_type == TypeManager.float_type)
- return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
- if (target_type == TypeManager.decimal_type)
- return InternalTypeConstructor (tc, expr, target_type);
- }
-
- // decimal is taken care of by the op_Explicit methods.
-
- return null;
- }
-
- // <summary>
- // Implements Explicit Reference conversions
- // </summary>
- static Expression ConvertReferenceExplicit (TypeContainer tc, Expression expr,
- Type target_type)
- {
- Type expr_type = expr.Type;
- bool target_is_value_type = target_type.IsValueType;
-
- //
- // From object to any reference type
- //
- if (expr_type == TypeManager.object_type && !target_is_value_type)
- return new ClassCast (expr, expr_type);
-
- return null;
- }
-
- // <summary>
- // Performs an explicit conversion of the expression `expr' whose
- // type is expr.Type to `target_type'.
- // </summary>
- static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
- Type target_type, Location l)
- {
- Expression ne = ConvertImplicit (tc, expr, target_type, l);
-
- if (ne != null)
- return ne;
-
- ne = ConvertNumericExplicit (tc, expr, target_type);
- if (ne != null)
- return ne;
-
- ne = ConvertReferenceExplicit (tc, expr, target_type);
- if (ne != null)
- return ne;
-
- ne = ExplicitUserConversion (tc, expr, target_type, l);
- if (ne != null)
- return ne;
-
- Report.Error (30, l, "Cannot convert type '" + TypeManager.CSharpName (expr.Type) + "' to '"
- + TypeManager.CSharpName (target_type) + "'");
- return null;
- }
-
- static string ExprClassName (ExprClass c)
- {
- switch (c){
- case ExprClass.Invalid:
- return "Invalid";
- case ExprClass.Value:
- return "value";
- case ExprClass.Variable:
- return "variable";
- case ExprClass.Namespace:
- return "namespace";
- case ExprClass.Type:
- return "type";
- case ExprClass.MethodGroup:
- return "method group";
- case ExprClass.PropertyAccess:
- return "property access";
- case ExprClass.EventAccess:
- return "event access";
- case ExprClass.IndexerAccess:
- return "indexer access";
- case ExprClass.Nothing:
- return "null";
- }
- throw new Exception ("Should not happen");
- }
-
- // <summary>
- // Reports that we were expecting `expr' to be of class `expected'
- // </summary>
- protected void report118 (TypeContainer tc, Location l, Expression expr, string expected)
- {
- string kind = "Unknown";
-
- if (expr != null)
- kind = ExprClassName (expr.ExprClass);
-
- Error (tc, 118, l, "Expression denotes a '" + kind +
- "' where an " + expected + " was expected");
- }
- }
-
- // <summary>
- // This is just a base class for expressions that can
- // appear on statements (invocations, object creation,
- // assignments, post/pre increment and decrement). The idea
- // being that they would support an extra Emition interface that
- // does not leave a result on the stack.
- // </summary>
-
- public abstract class ExpressionStatement : Expression {
-
- // <summary>
- // Requests the expression to be emitted in a `statement'
- // context. This means that no new value is left on the
- // stack after invoking this method (constrasted with
- // Emit that will always leave a value on the stack).
- // </summary>
- public abstract void EmitStatement (EmitContext ec);
- }
-
- // <summary>
- // This kind of cast is used to encapsulate the child
- // whose type is child.Type into an expression that is
- // reported to return "return_type". This is used to encapsulate
- // expressions which have compatible types, but need to be dealt
- // at higher levels with.
- //
- // For example, a "byte" expression could be encapsulated in one
- // of these as an "unsigned int". The type for the expression
- // would be "unsigned int".
- //
- // </summary>
-
- public class EmptyCast : Expression {
- protected Expression child;
-
- public EmptyCast (Expression child, Type return_type)
- {
- ExprClass = child.ExprClass;
- type = return_type;
- this.child = child;
- }
-
- public override Expression DoResolve (TypeContainer tc)
- {
- // This should never be invoked, we are born in fully
- // initialized state.
-
- return this;
- }
- public override void Emit (EmitContext ec)
- {
- child.Emit (ec);
- }
- }
+namespace CIR {
+ using System;
+ using System.Collections;
+ using System.Diagnostics;
+ using System.Reflection;
+ using System.Reflection.Emit;
+ using System.Text;
// <summary>
- // This kind of cast is used to encapsulate Value Types in objects.
- //
- // The effect of it is to box the value type emitted by the previous
- // operation.
+ // 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 BoxedCast : EmptyCast {
+ public class StaticCallExpr : ExpressionStatement {
+ ArrayList args;
+ MethodInfo mi;
- public BoxedCast (Expression expr)
- : base (expr, TypeManager.object_type)
+ StaticCallExpr (MethodInfo m, ArrayList a)
{
+ mi = m;
+ args = a;
+
+ type = m.ReturnType;
+ eclass = ExprClass.Value;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- // This should never be invoked, we are born in fully
- // initialized state.
-
+ //
+ // We are born fully resolved
+ //
return this;
}
public override void Emit (EmitContext ec)
{
- base.Emit (ec);
- ec.ig.Emit (OpCodes.Box, child.Type);
- }
- }
+ if (args != null)
+ Invocation.EmitArguments (ec, mi, args);
- // <summary>
- // This kind of cast is used to encapsulate a child expression
- // that can be trivially converted to a target type using one or
- // two opcodes. The opcodes are passed as arguments.
- // </summary>
- public class OpcodeCast : EmptyCast {
- OpCode op, op2;
- bool second_valid;
+ ec.ig.Emit (OpCodes.Call, mi);
+ return;
+ }
- public OpcodeCast (Expression child, Type return_type, OpCode op)
- : base (child, return_type)
-
+ static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
+ Expression e, Location loc)
{
- this.op = op;
- second_valid = false;
- }
-
- public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
- : base (child, return_type)
+ ArrayList args;
+ MethodBase method;
- {
- this.op = op;
- this.op2 = op2;
- second_valid = true;
- }
-
- public override Expression DoResolve (TypeContainer tc)
- {
- // This should never be invoked, we are born in fully
- // initialized state.
+ args = new ArrayList (1);
+ args.Add (new Argument (e, Argument.AType.Expression));
+ method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
- return this;
- }
-
- public override void Emit (EmitContext ec)
- {
- base.Emit (ec);
- ec.ig.Emit (op);
-
- if (second_valid)
- ec.ig.Emit (op2);
- }
-
- }
+ if (method == null)
+ return null;
- // <summary>
- // This kind of cast is used to encapsulate a child and cast it
- // to the class requested
- // </summary>
- public class ClassCast : EmptyCast {
- public ClassCast (Expression child, Type return_type)
- : base (child, return_type)
-
- {
+ return new StaticCallExpr ((MethodInfo) method, args);
}
- public override Expression DoResolve (TypeContainer tc)
+ public override void EmitStatement (EmitContext ec)
{
- // This should never be invoked, we are born in fully
- // initialized state.
-
- return this;
+ Emit (ec);
+ if (type != TypeManager.void_type)
+ ec.ig.Emit (OpCodes.Pop);
}
-
- public override void Emit (EmitContext ec)
- {
- base.Emit (ec);
-
- ec.ig.Emit (OpCodes.Castclass, type);
- }
-
}
// <summary>
// ExpressionStatement becuase the pre/post increment/decrement
// operators can be used in a statement context.
// </remarks>
- public class Unary : ExpressionStatement {
- public enum Operator {
- Addition, Subtraction, Negate, BitComplement,
- Indirection, AddressOf, PreIncrement,
- PreDecrement, PostIncrement, PostDecrement
+ public class Unary : Expression {
+ public enum Operator : byte {
+ UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
+ Indirection, AddressOf,
}
Operator oper;
Expression expr;
- ArrayList Arguments;
- MethodBase method;
- Location location;
+ Location loc;
public Unary (Operator op, Expression expr, Location loc)
{
this.oper = op;
this.expr = expr;
- this.location = loc;
+ this.loc = loc;
}
public Expression Expr {
string OperName ()
{
switch (oper){
- case Operator.Addition:
+ case Operator.UnaryPlus:
return "+";
- case Operator.Subtraction:
+ case Operator.UnaryNegation:
return "-";
- case Operator.Negate:
+ case Operator.LogicalNot:
return "!";
- case Operator.BitComplement:
+ case Operator.OnesComplement:
return "~";
case Operator.AddressOf:
return "&";
case Operator.Indirection:
return "*";
- case Operator.PreIncrement : case Operator.PostIncrement :
- return "++";
- case Operator.PreDecrement : case Operator.PostDecrement :
- return "--";
}
return oper.ToString ();
}
- Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
+ void error23 (Type t)
{
- if (expr.Type == target_type)
- return expr;
-
- return ConvertImplicit (tc, expr, target_type, new Location (-1));
+ Report.Error (
+ 23, loc, "Operator " + OperName () +
+ " cannot be applied to operand of type `" +
+ TypeManager.CSharpName (t) + "'");
}
- void error23 (TypeContainer tc, Type t)
+ static Expression TryReduceNegative (Expression expr)
{
- Report.Error (23, location, "Operator " + OperName () +
- " cannot be applied to operand of type `" +
- TypeManager.CSharpName (t) + "'");
- }
+ Expression e = null;
+
+ if (expr is IntLiteral)
+ e = new IntLiteral (-((IntLiteral) expr).Value);
+ else if (expr is UIntLiteral)
+ e = new LongLiteral (-((UIntLiteral) expr).Value);
+ else if (expr is LongLiteral)
+ e = new LongLiteral (-((LongLiteral) expr).Value);
+ else if (expr is FloatLiteral)
+ e = new FloatLiteral (-((FloatLiteral) expr).Value);
+ else if (expr is DoubleLiteral)
+ e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
+ else if (expr is DecimalLiteral)
+ e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
- // <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);
+ return e;
}
-
- Expression ResolveOperator (TypeContainer tc)
+
+ Expression ResolveOperator (EmitContext ec)
{
Type expr_type = expr.Type;
//
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 (tc, expr_type, op_name, false);
+ mg = MemberLookup (ec, expr_type, op_name, false, loc);
if (mg == null && expr_type.BaseType != null)
- mg = MemberLookup (tc, expr_type.BaseType, op_name, false);
+ 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 (tc, (MethodGroupExpr) mg,
- Arguments, location);
- if (method != null) {
- MethodInfo mi = (MethodInfo) method;
- type = mi.ReturnType;
- return this;
- } else {
- error23 (tc, expr_type);
+ 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;
- if (oper == Operator.Negate){
+ if (oper == Operator.LogicalNot){
if (expr_type != TypeManager.bool_type) {
- error23 (tc, expr.Type);
+ error23 (expr.Type);
return null;
}
return this;
}
- if (oper == Operator.BitComplement) {
+ if (oper == Operator.OnesComplement) {
if (!((expr_type == TypeManager.int32_type) ||
(expr_type == TypeManager.uint32_type) ||
(expr_type == TypeManager.int64_type) ||
(expr_type == TypeManager.uint64_type) ||
(expr_type.IsSubclassOf (TypeManager.enum_type)))){
- error23 (tc, expr.Type);
+ error23 (expr.Type);
return null;
}
type = expr_type;
return this;
}
- if (oper == Operator.Addition) {
+ if (oper == Operator.UnaryPlus) {
//
// A plus in front of something is just a no-op, so return the child.
//
// double operator- (double d)
// decimal operator- (decimal d)
//
- if (oper == Operator.Subtraction){
+ if (oper == Operator.UnaryNegation){
//
// Fold a "- Constant" into a negative constant
//
//
// Is this a constant?
//
- if (expr is IntLiteral)
- e = new IntLiteral (-((IntLiteral) expr).Value);
- else if (expr is LongLiteral)
- e = new LongLiteral (-((LongLiteral) expr).Value);
- else if (expr is FloatLiteral)
- e = new FloatLiteral (-((FloatLiteral) expr).Value);
- else if (expr is DoubleLiteral)
- e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
- else if (expr is DecimalLiteral)
- e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
+ e = TryReduceNegative (expr);
if (e != null){
- e = e.Resolve (tc);
+ e = e.Resolve (ec);
return e;
}
// It is also not clear if we should convert to Float
// or Double initially.
//
- Location l = new Location (-1);
-
if (expr_type == TypeManager.uint32_type){
//
// FIXME: handle exception to this rule that
// bt written as a decimal interger literal
//
type = TypeManager.int64_type;
- expr = ConvertImplicit (tc, expr, type, l);
+ expr = ConvertImplicit (ec, expr, type, loc);
return this;
}
// -92233720368547758087 (-2^63) to be written as
// decimal integer literal.
//
- error23 (tc, expr_type);
+ error23 (expr_type);
return null;
}
- e = ConvertImplicit (tc, expr, TypeManager.int32_type, l);
+ e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
if (e != null){
expr = e;
type = e.Type;
return this;
}
- e = ConvertImplicit (tc, expr, TypeManager.int64_type, l);
- if (e != null){
- expr = e;
- type = e.Type;
- return this;
- }
+ e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
+ if (e != null){
+ expr = e;
+ type = e.Type;
+ return this;
+ }
+
+ e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
+ if (e != null){
+ expr = e;
+ type = e.Type;
+ return this;
+ }
+
+ error23 (expr_type);
+ return null;
+ }
+
+ if (oper == Operator.AddressOf){
+ if (expr.ExprClass != ExprClass.Variable){
+ Error (211, loc, "Cannot take the address of non-variables");
+ return null;
+ }
+ type = Type.GetType (expr.Type.ToString () + "*");
+
+ return this;
+ }
+
+ 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);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+ Type expr_type = expr.Type;
+
+ switch (oper) {
+ case Operator.UnaryPlus:
+ throw new Exception ("This should be caught by Resolve");
+
+ case Operator.UnaryNegation:
+ expr.Emit (ec);
+ ig.Emit (OpCodes.Neg);
+ break;
+
+ case Operator.LogicalNot:
+ expr.Emit (ec);
+ ig.Emit (OpCodes.Ldc_I4_0);
+ ig.Emit (OpCodes.Ceq);
+ break;
+
+ case Operator.OnesComplement:
+ expr.Emit (ec);
+ ig.Emit (OpCodes.Not);
+ break;
+
+ case Operator.AddressOf:
+ ((IMemoryLocation)expr).AddressOf (ec);
+ break;
+
+ case Operator.Indirection:
+ throw new Exception ("Not implemented yet");
+
+ default:
+ throw new Exception ("This should not happen: Operator = "
+ + oper.ToString ());
+ }
+ }
+
+ // <summary>
+ // This will emit the child expression for `ec' avoiding the logical
+ // not. The parent will take care of changing brfalse/brtrue
+ // </summary>
+ public void EmitLogicalNot (EmitContext ec)
+ {
+ if (oper != Operator.LogicalNot)
+ throw new Exception ("EmitLogicalNot can only be called with !expr");
+
+ expr.Emit (ec);
+ }
+
+ public override Expression Reduce (EmitContext ec)
+ {
+ Expression e;
+
+ //
+ // First, reduce our child. Note that although we handle
+ //
+ expr = expr.Reduce (ec);
+ if (!(expr is Literal))
+ return expr;
+
+ switch (oper){
+ case Operator.UnaryPlus:
+ return expr;
+
+ case Operator.UnaryNegation:
+ e = TryReduceNegative (expr);
+ if (e == null)
+ break;
+ return e;
+
+ case Operator.LogicalNot:
+ BoolLiteral b = (BoolLiteral) expr;
+
+ return new BoolLiteral (!(b.Value));
+
+ case Operator.OnesComplement:
+ Type et = expr.Type;
+
+ if (et == TypeManager.int32_type)
+ return new IntLiteral (~ ((IntLiteral) expr).Value);
+ if (et == TypeManager.uint32_type)
+ return new UIntLiteral (~ ((UIntLiteral) expr).Value);
+ if (et == TypeManager.int64_type)
+ return new LongLiteral (~ ((LongLiteral) expr).Value);
+ if (et == TypeManager.uint64_type)
+ return new ULongLiteral (~ ((ULongLiteral) expr).Value);
+ break;
+ }
+ 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);
- e = ConvertImplicit (tc, expr, TypeManager.double_type, l);
- if (e != null){
- expr = e;
- type = e.Type;
- return this;
- }
+ 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);
- error23 (tc, expr_type);
- return null;
+ type = method.Type;
+ return this;
}
//
// 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){
- //
- // FIXME: Verify that we have both get and set methods
- //
- throw new Exception ("Implement me");
- } else {
- report118 (tc, location, expr,
- "variable, indexer or property access");
+ type = expr_type;
+ if (expr.ExprClass == ExprClass.Variable){
+ if (IsIncrementableNumber (expr_type) ||
+ expr_type == TypeManager.decimal_type){
+ return this;
}
- }
-
- if (oper == Operator.AddressOf){
- if (expr.ExprClass != ExprClass.Variable){
- Error (tc, 211, "Cannot take the address of non-variables");
+ } 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;
- }
- type = Type.GetType (expr.Type.ToString () + "*");
+
+ 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 (tc, 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 (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- expr = expr.Resolve (tc);
-
+ expr = expr.Resolve (ec);
+
if (expr == null)
return null;
eclass = ExprClass.Value;
- return ResolveOperator (tc);
+ return ResolveOperator (ec);
}
+
- public override void Emit (EmitContext 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;
- Type expr_type = expr.Type;
-
- if (method != null) {
-
- // Note that operators are static anyway
-
- if (Arguments != null)
- Invocation.EmitArguments (ec, method, 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);
+ IAssignMethod ia = (IAssignMethod) expr;
- //
- // 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){
- ((LValue) expr).Store (ec);
- }
- return;
- }
+ if (temp_storage == null)
+ temp_storage = new LocalTemporary (ec, expr.Type);
- switch (oper) {
- case Operator.Addition:
- throw new Exception ("This should be caught by Resolve");
-
- case Operator.Subtraction:
- expr.Emit (ec);
- ig.Emit (OpCodes.Neg);
- break;
-
- case Operator.Negate:
- expr.Emit (ec);
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
- break;
-
- case Operator.BitComplement:
- expr.Emit (ec);
- ig.Emit (OpCodes.Not);
- break;
-
- case Operator.AddressOf:
- ((LValue)expr).AddressOf (ec);
- break;
-
- 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"
- //
+ switch (mode){
+ case Mode.PreIncrement:
+ case Mode.PreDecrement:
+ if (method == null){
expr.Emit (ec);
+
ig.Emit (OpCodes.Ldc_I4_1);
-
- if (oper == Operator.PreDecrement)
+
+ if (mode == Mode.PreDecrement)
ig.Emit (OpCodes.Sub);
else
ig.Emit (OpCodes.Add);
- ig.Emit (OpCodes.Dup);
- ((LValue) expr).Store (ec);
- } else {
- throw new Exception ("Handle Indexers and Properties here");
- }
+ } else
+ method.Emit (ec);
+
+ temp_storage.Store (ec);
+ ia.EmitAssign (ec, temp_storage);
+ if (is_expr)
+ temp_storage.Emit (ec);
break;
- case Operator.PostIncrement:
- case Operator.PostDecrement:
- if (expr.ExprClass == ExprClass.Variable){
- //
- // Resolve already verified that it is an "incrementable"
- //
+ case Mode.PostIncrement:
+ case Mode.PostDecrement:
+ if (is_expr)
expr.Emit (ec);
- ig.Emit (OpCodes.Dup);
+
+ if (method == null){
+ if (!is_expr)
+ expr.Emit (ec);
+ else
+ ig.Emit (OpCodes.Dup);
+
ig.Emit (OpCodes.Ldc_I4_1);
-
- if (oper == Operator.PostDecrement)
+
+ if (mode == Mode.PostDecrement)
ig.Emit (OpCodes.Sub);
else
ig.Emit (OpCodes.Add);
- ((LValue) expr).Store (ec);
} else {
- throw new Exception ("Handle Indexers and Properties here");
+ method.Emit (ec);
}
- break;
- default:
- throw new Exception ("This should not happen: Operator = "
- + oper.ToString ());
+ 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)
{
- //
- // 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);
+ EmitCode (ec, false);
}
+
}
public class Probe : Expression {
Expression expr;
Type probe_type;
- public enum Operator {
+ public enum Operator : byte {
Is, As
}
}
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- probe_type = tc.LookupType (ProbeType, false);
+ probe_type = ec.TypeContainer.LookupType (ProbeType, false);
if (probe_type == null)
return null;
- expr = expr.Resolve (tc);
+ expr = expr.Resolve (ec);
type = TypeManager.bool_type;
eclass = ExprClass.Value;
public class Cast : Expression {
string target_type;
Expression expr;
- Location location;
+ Location loc;
public Cast (string cast_type, Expression expr, Location loc)
{
this.target_type = cast_type;
this.expr = expr;
- this.location = loc;
+ this.loc = loc;
}
public string TargetType {
}
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- expr = expr.Resolve (tc);
+ expr = expr.Resolve (ec);
if (expr == null)
return null;
- type = tc.LookupType (target_type, false);
+ type = ec.TypeContainer.LookupType (target_type, false);
eclass = ExprClass.Value;
if (type == null)
return null;
- expr = ConvertExplicit (tc, expr, type, location);
+ expr = ConvertExplicit (ec, expr, type, loc);
return expr;
}
}
public class Binary : Expression {
- public enum Operator {
+ public enum Operator : byte {
Multiply, Division, Modulus,
Addition, Subtraction,
LeftShift, RightShift,
Expression left, right;
MethodBase method;
ArrayList Arguments;
- Location location;
+ Location loc;
public Binary (Operator oper, Expression left, Expression right, Location loc)
this.oper = oper;
this.left = left;
this.right = right;
- this.location = loc;
+ this.loc = loc;
}
public Operator Oper {
return oper.ToString ();
}
- Expression ForceConversion (TypeContainer tc, Expression expr, Type target_type)
+ Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
{
if (expr.Type == target_type)
return expr;
- return ConvertImplicit (tc, expr, target_type, new Location (-1));
+ return ConvertImplicit (ec, expr, target_type, new Location (-1));
}
//
// Note that handling the case l == Decimal || r == Decimal
// is taken care of by the Step 1 Operator Overload resolution.
//
- void DoNumericPromotions (TypeContainer tc, Type l, Type r)
+ bool DoNumericPromotions (EmitContext ec, Type l, Type r)
{
if (l == TypeManager.double_type || r == TypeManager.double_type){
//
// conveted to type double.
//
if (r != TypeManager.double_type)
- right = ConvertImplicit (tc, right, TypeManager.double_type, location);
+ right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
if (l != TypeManager.double_type)
- left = ConvertImplicit (tc, left, TypeManager.double_type, location);
+ left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
type = TypeManager.double_type;
} else if (l == TypeManager.float_type || r == TypeManager.float_type){
// converd to type float.
//
if (r != TypeManager.double_type)
- right = ConvertImplicit (tc, right, TypeManager.float_type, location);
+ right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
if (l != TypeManager.double_type)
- left = ConvertImplicit (tc, left, TypeManager.float_type, location);
+ left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
type = TypeManager.float_type;
} else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
Expression e;
(other == TypeManager.int64_type)){
string oper = OperName ();
- Error (tc, 34, location, "Operator `" + OperName ()
+ Error (34, loc, "Operator `" + OperName ()
+ "' is ambiguous on operands of type `"
+ TypeManager.CSharpName (l) + "' "
+ "and `" + TypeManager.CSharpName (r)
// to type long.
//
if (l != TypeManager.int64_type)
- left = ConvertImplicit (tc, left, TypeManager.int64_type, location);
+ left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
if (r != TypeManager.int64_type)
- right = ConvertImplicit (tc, right, TypeManager.int64_type, location);
+ right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
type = TypeManager.int64_type;
} else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
if ((other == TypeManager.sbyte_type) ||
(other == TypeManager.short_type) ||
(other == TypeManager.int32_type)){
- left = ForceConversion (tc, left, TypeManager.int64_type);
- right = ForceConversion (tc, right, TypeManager.int64_type);
+ left = ForceConversion (ec, left, TypeManager.int64_type);
+ right = ForceConversion (ec, right, TypeManager.int64_type);
type = TypeManager.int64_type;
} else {
//
// if either operand is of type uint, the other
// operand is converd to type uint
//
- left = ForceConversion (tc, left, TypeManager.uint32_type);
- right = ForceConversion (tc, right, TypeManager.uint32_type);
+ left = ForceConversion (ec, left, TypeManager.uint32_type);
+ right = ForceConversion (ec, right, TypeManager.uint32_type);
type = TypeManager.uint32_type;
}
} else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
if (l != TypeManager.decimal_type)
- left = ConvertImplicit (tc, left, TypeManager.decimal_type, location);
+ left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
if (r != TypeManager.decimal_type)
- right = ConvertImplicit (tc, right, TypeManager.decimal_type, location);
+ right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
type = TypeManager.decimal_type;
} else {
Expression l_tmp, r_tmp;
- l_tmp = ForceConversion (tc, left, TypeManager.int32_type);
- if (l_tmp == null) {
- error19 (tc);
- left = l_tmp;
- return;
- }
+ l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
+ if (l_tmp == null)
+ return false;
- r_tmp = ForceConversion (tc, right, TypeManager.int32_type);
- if (r_tmp == null) {
- error19 (tc);
- right = r_tmp;
- return;
- }
+ r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
+ if (r_tmp == null)
+ return false;
+
+ left = l_tmp;
+ right = r_tmp;
type = TypeManager.int32_type;
}
+
+ return true;
}
- void error19 (TypeContainer tc)
+ void error19 ()
{
- Error (tc, 19, location,
+ Error (19, loc,
"Operator " + OperName () + " cannot be applied to operands of type `" +
TypeManager.CSharpName (left.Type) + "' and `" +
TypeManager.CSharpName (right.Type) + "'");
}
- Expression CheckShiftArguments (TypeContainer tc)
+ Expression CheckShiftArguments (EmitContext ec)
{
Expression e;
Type l = left.Type;
Type r = right.Type;
- e = ForceConversion (tc, right, TypeManager.int32_type);
+ e = ForceConversion (ec, right, TypeManager.int32_type);
if (e == null){
- error19 (tc);
+ error19 ();
return null;
}
right = e;
- Location loc = location;
-
- if (((e = ConvertImplicit (tc, left, TypeManager.int32_type, loc)) != null) ||
- ((e = ConvertImplicit (tc, left, TypeManager.uint32_type, loc)) != null) ||
- ((e = ConvertImplicit (tc, left, TypeManager.int64_type, loc)) != null) ||
- ((e = ConvertImplicit (tc, left, TypeManager.uint64_type, loc)) != null)){
+ if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
+ ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
+ ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
+ ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
left = e;
type = e.Type;
return this;
}
- error19 (tc);
+ error19 ();
return null;
}
- Expression ResolveOperator (TypeContainer tc)
+ Expression ResolveOperator (EmitContext ec)
{
Type l = left.Type;
Type r = right.Type;
string op = "op_" + oper;
- left_expr = MemberLookup (tc, l, op, false);
+ left_expr = MemberLookup (ec, l, op, false, loc);
if (left_expr == null && l.BaseType != null)
- left_expr = MemberLookup (tc, l.BaseType, op, false);
+ left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
- right_expr = MemberLookup (tc, r, op, false);
+ right_expr = MemberLookup (ec, r, op, false, loc);
if (right_expr == null && r.BaseType != null)
- right_expr = MemberLookup (tc, r.BaseType, op, false);
+ right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
Arguments.Add (new Argument (left, Argument.AType.Expression));
Arguments.Add (new Argument (right, Argument.AType.Expression));
- method = Invocation.OverloadResolve (tc, union, Arguments, location);
+ method = Invocation.OverloadResolve (ec, union, Arguments, loc);
if (method != null) {
MethodInfo mi = (MethodInfo) method;
type = mi.ReturnType;
return this;
} else {
- error19 (tc);
+ error19 ();
return null;
}
}
//
if (l == TypeManager.string_type){
if (r == TypeManager.string_type){
+ if (left is Literal && right is Literal){
+ StringLiteral ls = (StringLiteral) left;
+ StringLiteral rs = (StringLiteral) right;
+
+ return new StringLiteral (ls.Value + rs.Value);
+ }
+
// string + string
method = TypeManager.string_concat_string_string;
} else {
// string + object
method = TypeManager.string_concat_object_object;
- right = ConvertImplicit (tc, right,
- TypeManager.object_type, location);
+ right = ConvertImplicit (ec, right,
+ TypeManager.object_type, loc);
}
type = TypeManager.string_type;
Arguments.Add (new Argument (left, Argument.AType.Expression));
Arguments.Add (new Argument (right, Argument.AType.Expression));
- left = ConvertImplicit (tc, left, TypeManager.object_type, location);
+ left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
type = TypeManager.string_type;
return this;
}
if (oper == Operator.LeftShift || oper == Operator.RightShift)
- return CheckShiftArguments (tc);
+ return CheckShiftArguments (ec);
if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
- if (l != TypeManager.bool_type || r != TypeManager.bool_type)
- error19 (tc);
+ if (l != TypeManager.bool_type || r != TypeManager.bool_type){
+ error19 ();
+ return null;
+ }
type = TypeManager.bool_type;
return this;
}
+ if (oper == Operator.Equality || oper == Operator.Inequality){
+ if (l == TypeManager.bool_type || r == TypeManager.bool_type){
+ if (r != TypeManager.bool_type || l != TypeManager.bool_type){
+ error19 ();
+ return null;
+ }
+
+ type = TypeManager.bool_type;
+ return this;
+ }
+
+ }
+
//
// We are dealing with numbers
//
- DoNumericPromotions (tc, 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;
(l == TypeManager.uint32_type) ||
(l == TypeManager.int64_type) ||
(l == TypeManager.uint64_type))){
- error19 (tc);
+ error19 ();
return null;
}
type = l;
return this;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- left = left.Resolve (tc);
- right = right.Resolve (tc);
+ left = left.Resolve (ec);
+ right = right.Resolve (ec);
if (left == null || right == null)
return null;
if (left.Type == null)
throw new Exception (
"Resolve returned non null, but did not set the type! (" +
- left + ")");
+ left + ") at Line: " + loc.Row);
if (right.Type == null)
throw new Exception (
"Resolve returned non null, but did not set the type! (" +
- right + ")");
+ right + ") at Line: "+ loc.Row);
eclass = ExprClass.Value;
- return ResolveOperator (tc);
+ return ResolveOperator (ec);
}
public bool IsBranchable ()
ig.Emit (opcode);
}
+
+ // <summary>
+ // Constant expression reducer for binary operations
+ // </summary>
+ public override Expression Reduce (EmitContext ec)
+ {
+
+ left = left.Reduce (ec);
+ right = right.Reduce (ec);
+
+ if (!(left is Literal && right is Literal))
+ return this;
+
+ if (method == TypeManager.string_concat_string_string){
+ StringLiteral ls = (StringLiteral) left;
+ StringLiteral rs = (StringLiteral) right;
+
+ return new StringLiteral (ls.Value + rs.Value);
+ }
+
+ // FINISH ME.
+
+ return this;
+ }
}
public class Conditional : Expression {
Expression expr, trueExpr, falseExpr;
- Location l;
+ Location loc;
public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
{
this.expr = expr;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
- this.l = l;
+ this.loc = l;
}
public Expression Expr {
}
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- expr = expr.Resolve (tc);
+ expr = expr.Resolve (ec);
if (expr.Type != TypeManager.bool_type)
expr = Expression.ConvertImplicitRequired (
- tc, expr, TypeManager.bool_type, l);
+ ec, expr, TypeManager.bool_type, loc);
- trueExpr = trueExpr.Resolve (tc);
- falseExpr = falseExpr.Resolve (tc);
+ trueExpr = trueExpr.Resolve (ec);
+ falseExpr = falseExpr.Resolve (ec);
if (expr == null || trueExpr == null || falseExpr == null)
return null;
-
+
if (trueExpr.Type == falseExpr.Type)
type = trueExpr.Type;
else {
// First, if an implicit conversion exists from trueExpr
// to falseExpr, then the result type is of type falseExpr.Type
//
- conv = ConvertImplicit (tc, trueExpr, falseExpr.Type, l);
+ conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
if (conv != null){
type = falseExpr.Type;
trueExpr = conv;
- } else if ((conv = ConvertImplicit (tc,falseExpr,trueExpr.Type,l)) != null){
+ } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
type = trueExpr.Type;
falseExpr = conv;
} else {
- Error (tc, 173, l, "The type of the conditional expression can " +
+ Error (173, loc, "The type of the conditional expression can " +
"not be computed because there is no implicit conversion" +
" from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
" and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
}
}
+ if (expr is BoolLiteral){
+ BoolLiteral bl = (BoolLiteral) expr;
+
+ if (bl.Value)
+ return trueExpr;
+ else
+ return falseExpr;
+ }
+
eclass = ExprClass.Value;
return this;
}
falseExpr.Emit (ec);
ig.MarkLabel (end_target);
}
- }
-
- public class SimpleName : Expression {
- public readonly string Name;
- public readonly Location Location;
-
- public SimpleName (string name, Location l)
- {
- Name = name;
- Location = l;
- }
-
- //
- // Checks whether we are trying to access an instance
- // property, method or field from a static body.
- //
- Expression MemberStaticCheck (Expression e)
- {
- if (e is FieldExpr){
- FieldInfo fi = ((FieldExpr) e).FieldInfo;
-
- if (!fi.IsStatic){
- Report.Error (
- 120,
- "An object reference is required " +
- "for the non-static field `"+Name+"'");
- return null;
- }
- } else if (e is MethodGroupExpr){
- // FIXME: Pending reorganization of MemberLookup
- // Basically at this point we should have the
- // best match already selected for us, and
- // we should only have to check a *single*
- // Method for its static on/off bit.
- return e;
- } else if (e is PropertyExpr){
- if (!((PropertyExpr) e).IsStatic){
- Report.Error (120,
- "An object reference is required " +
- "for the non-static property access `"+
- Name+"'");
- return null;
- }
- }
- return e;
- }
-
- //
- // 7.5.2: Simple Names.
- //
- // Local Variables and Parameters are handled at
- // parse time, so they never occur as SimpleNames.
- //
- Expression ResolveSimpleName (TypeContainer tc)
+ public override Expression Reduce (EmitContext ec)
{
- Expression e;
-
- e = MemberLookup (tc, tc.TypeBuilder, Name, true);
- if (e != null){
- if (e is TypeExpr)
- return e;
- else if (e is FieldExpr){
- FieldExpr fe = (FieldExpr) e;
-
- if (!fe.FieldInfo.IsStatic)
- fe.Instance = new This ();
- }
-
- if ((tc.ModFlags & Modifiers.STATIC) != 0)
- return MemberStaticCheck (e);
- else
- return e;
- }
+ expr = expr.Reduce (ec);
+ trueExpr = trueExpr.Reduce (ec);
+ falseExpr = falseExpr.Reduce (ec);
- //
- // Do step 3 of the Simple Name resolution.
- //
- // FIXME: implement me.
+ if (!(expr is Literal && trueExpr is Literal && falseExpr is Literal))
+ return this;
- Error (tc, 103, Location, "The name `" + Name + "' does not exist in the class `" +
- tc.Name + "'");
+ BoolLiteral bl = (BoolLiteral) expr;
- return null;
- }
-
- //
- // SimpleName needs to handle a multitude of cases:
- //
- // simple_names and qualified_identifiers are placed on
- // the tree equally.
- //
- public override Expression DoResolve (TypeContainer tc)
- {
- if (Name.IndexOf (".") != -1)
- return ResolveMemberAccess (tc, Name);
+ if (bl.Value)
+ return trueExpr;
else
- return ResolveSimpleName (tc);
- }
-
- public override void Emit (EmitContext ec)
- {
- throw new Exception ("SimpleNames should be gone from the tree");
+ return falseExpr;
}
}
- // <summary>
- // A simple interface that should be implemeneted by LValues
- // </summary>
- public interface LValue {
-
- // <summary>
- // The Store method should store the contents of the top
- // of the stack into the storage that is implemented by
- // the particular implementation of LValue
- // </summary>
- void Store (EmitContext ec);
-
- // <summary>
- // The AddressOf method should generate code that loads
- // the address of the LValue and leaves it on the stack
- // </summary>
- void AddressOf (EmitContext ec);
- }
-
- public class LocalVariableReference : Expression, LValue {
+ public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
public readonly string Name;
public readonly Block Block;
+
+ VariableInfo variable_info;
public LocalVariableReference (Block block, string name)
{
public VariableInfo VariableInfo {
get {
- return Block.GetVariableInfo (Name);
+ if (variable_info == null)
+ variable_info = Block.GetVariableInfo (Name);
+ return variable_info;
}
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- VariableInfo vi = Block.GetVariableInfo (Name);
+ VariableInfo vi = VariableInfo;
type = vi.VariableType;
return this;
int idx = vi.Idx;
vi.Used = true;
-
+
switch (idx){
case 0:
ig.Emit (OpCodes.Ldloc_0);
case 1:
ig.Emit (OpCodes.Ldloc_1);
break;
-
+
case 2:
ig.Emit (OpCodes.Ldloc_2);
break;
-
+
case 3:
ig.Emit (OpCodes.Ldloc_3);
break;
-
+
default:
if (idx <= 255)
ig.Emit (OpCodes.Ldloc_S, (byte) idx);
break;
}
}
-
- public void Store (EmitContext ec)
+
+ public static void Store (ILGenerator ig, int idx)
{
- ILGenerator ig = ec.ig;
- VariableInfo vi = VariableInfo;
- int idx = vi.Idx;
-
- vi.Assigned = true;
switch (idx){
case 0:
ig.Emit (OpCodes.Stloc_0);
}
}
+ public void EmitAssign (EmitContext ec, Expression source)
+ {
+ ILGenerator ig = ec.ig;
+ VariableInfo vi = VariableInfo;
+
+ vi.Assigned = true;
+
+ 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, LValue {
+ public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
public readonly Parameters Pars;
public readonly String Name;
public readonly int Idx;
+ int arg_idx;
public ParameterReference (Parameters pars, int idx, string name)
{
eclass = ExprClass.Variable;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- Type [] types = Pars.GetParameterInfo (tc);
+ Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
type = types [Idx];
+ arg_idx = Idx;
+ if (!ec.IsStatic)
+ arg_idx++;
+
return this;
}
public override void Emit (EmitContext ec)
{
- if (Idx <= 255)
- ec.ig.Emit (OpCodes.Ldarg_S, (byte) Idx);
+ if (arg_idx <= 255)
+ ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
else
- ec.ig.Emit (OpCodes.Ldarg, Idx);
+ ec.ig.Emit (OpCodes.Ldarg, arg_idx);
}
- public void Store (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
- if (Idx <= 255)
- ec.ig.Emit (OpCodes.Starg_S, (byte) Idx);
+ source.Emit (ec);
+
+ if (arg_idx <= 255)
+ ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
else
- ec.ig.Emit (OpCodes.Starg, Idx);
+ ec.ig.Emit (OpCodes.Starg, arg_idx);
}
public void AddressOf (EmitContext ec)
{
- if (Idx <= 255)
- ec.ig.Emit (OpCodes.Ldarga_S, (byte) Idx);
+ if (arg_idx <= 255)
+ ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
else
- ec.ig.Emit (OpCodes.Ldarga, Idx);
+ ec.ig.Emit (OpCodes.Ldarga, arg_idx);
}
}
// Used for arguments to New(), Invocation()
// </summary>
public class Argument {
- public enum AType {
+ public enum AType : byte {
Expression,
Ref,
Out
};
- public readonly AType Type;
- Expression expr;
-
+ public readonly AType ArgType;
+ public Expression expr;
+
public Argument (Expression expr, AType type)
{
this.expr = expr;
- this.Type = type;
+ this.ArgType = type;
}
public Expression Expr {
}
}
- public bool Resolve (TypeContainer tc)
+ public Type Type {
+ get {
+ return expr.Type;
+ }
+ }
+
+ public Parameter.Modifier GetParameterModifier ()
+ {
+ if (ArgType == AType.Ref)
+ return Parameter.Modifier.REF;
+
+ if (ArgType == AType.Out)
+ return Parameter.Modifier.OUT;
+
+ return Parameter.Modifier.NONE;
+ }
+
+ public static string FullDesc (Argument a)
{
- expr = expr.Resolve (tc);
+ return (a.ArgType == AType.Ref ? "ref " :
+ (a.ArgType == AType.Out ? "out " : "")) +
+ TypeManager.CSharpName (a.Expr.Type);
+ }
+
+ 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 {
public static ParameterData GetParameterData (MethodBase mb)
{
object pd = method_parameter_cache [mb];
-
+ object ip;
+
if (pd != null)
return (ParameterData) pd;
- if (mb is MethodBuilder || mb is ConstructorBuilder){
- MethodCore mc = TypeContainer.LookupMethodByBuilder (mb);
-
- InternalParameters ip = mc.ParameterInfo;
+
+ ip = TypeContainer.LookupParametersByBuilder (mb);
+ if (ip != null){
method_parameter_cache [mb] = ip;
return (ParameterData) ip;
} else {
+ Console.WriteLine ("Getting parameters for: " + mb);
ParameterInfo [] pi = mb.GetParameters ();
ReflectionParameters rp = new ReflectionParameters (pi);
method_parameter_cache [mb] = rp;
//
// FIXME: we could implement a cache here.
// </summary>
- static bool ConversionExists (TypeContainer tc, Type from, Type to)
+ static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
{
// Locate user-defined implicit operators
Expression mg;
- mg = MemberLookup (tc, to, "op_Implicit", false);
+ mg = MemberLookup (ec, to, "op_Implicit", false, loc);
if (mg != null) {
MethodGroupExpr me = (MethodGroupExpr) mg;
}
}
- mg = MemberLookup (tc, from, "op_Implicit", false);
+ mg = MemberLookup (ec, from, "op_Implicit", false, loc);
if (mg != null) {
MethodGroupExpr me = (MethodGroupExpr) mg;
// Returns : 1 if a->p is better
// 0 if a->q or neither is better
// </summary>
- static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q, bool use_standard)
+ static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
+ Location loc)
{
-
- Type argument_type = a.Expr.Type;
+ Type argument_type = a.Type;
Expression argument_expr = a.Expr;
if (argument_type == null)
Expression tmp;
if (use_standard)
- tmp = ConvertImplicitStandard (tc, argument_expr, p, Location.Null);
+ tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
else
- tmp = ConvertImplicit (tc, argument_expr, p, Location.Null);
+ tmp = ConvertImplicit (ec, argument_expr, p, loc);
if (tmp != null)
return 1;
}
- if (ConversionExists (tc, p, q) == true &&
- ConversionExists (tc, q, p) == false)
+ if (ConversionExists (ec, p, q, loc) == true &&
+ ConversionExists (ec, q, p, loc) == false)
return 1;
if (p == TypeManager.sbyte_type)
// 0 if candidate ain't better
// 1 if candidate is better than the current best match
// </summary>
- static int BetterFunction (TypeContainer tc, ArrayList args,
+ static int BetterFunction (EmitContext ec, ArrayList args,
MethodBase candidate, MethodBase best,
- bool use_standard)
+ bool use_standard, Location loc)
{
ParameterData candidate_pd = GetParameterData (candidate);
ParameterData best_pd;
Argument a = (Argument) args [j];
x = BetterConversion (
- tc, a, candidate_pd.ParameterType (j), null,
- use_standard);
+ ec, a, candidate_pd.ParameterType (j), null,
+ use_standard, loc);
if (x <= 0)
break;
Argument a = (Argument) args [j];
- x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
- best_pd.ParameterType (j), use_standard);
- y = BetterConversion (tc, a, best_pd.ParameterType (j),
- candidate_pd.ParameterType (j), use_standard);
+ x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
+ best_pd.ParameterType (j), use_standard, loc);
+ y = BetterConversion (ec, a, best_pd.ParameterType (j),
+ candidate_pd.ParameterType (j), use_standard,
+ loc);
rating1 += x;
rating2 += y;
{
StringBuilder sb = new StringBuilder (mb.Name);
ParameterData pd = GetParameterData (mb);
-
+
+ int count = pd.Count;
sb.Append (" (");
- for (int i = pd.Count; i > 0;) {
+
+ for (int i = count; i > 0; ) {
i--;
- sb.Append (TypeManager.CSharpName (pd.ParameterType (i)));
+
+ sb.Append (pd.ParameterDesc (count - i - 1));
if (i != 0)
sb.Append (", ");
}
return null;
}
+ // <summary>
+ // Determines is the candidate method, if a params method, is applicable
+ // in its expanded form to the given set of arguments
+ // </summary>
+ static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
+ {
+ int arg_count;
+
+ if (arguments == null)
+ arg_count = 0;
+ else
+ arg_count = arguments.Count;
+
+ ParameterData pd = GetParameterData (candidate);
+
+ int pd_count = pd.Count;
+
+ if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
+ return false;
+
+ if (pd_count - 1 > arg_count)
+ return false;
+
+ // 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. So, we now check if the element type
+ // of the params array is compatible with each argument type
+ //
+
+ Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
+
+ for (int i = pd_count - 1; i < arg_count - 1; i++) {
+ Argument a = (Argument) arguments [i];
+ if (!StandardConversionExists (a.Type, element_type))
+ return false;
+ }
+
+ return true;
+ }
+
+ // <summary>
+ // Determines if the candidate method is applicable (section 14.4.2.1)
+ // to the given set of arguments
+ // </summary>
+ static bool IsApplicable (ArrayList arguments, MethodBase candidate)
+ {
+ int arg_count;
+
+ if (arguments == null)
+ arg_count = 0;
+ else
+ arg_count = arguments.Count;
+
+ ParameterData pd = GetParameterData (candidate);
+
+ int pd_count = pd.Count;
+
+ 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.GetParameterModifier ();
+ Parameter.Modifier p_mod = pd.ParameterModifier (i);
+
+ if (a_mod == p_mod) {
+
+ if (a_mod == Parameter.Modifier.NONE)
+ if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
+ return false;
+
+ if (a_mod == Parameter.Modifier.REF ||
+ a_mod == Parameter.Modifier.OUT)
+ if (pd.ParameterType (i) != a.Type)
+ return false;
+ } else
+ return false;
+ }
+
+ return true;
+ }
+
+
+
// <summary>
// Find the Applicable Function Members (7.4.2.1)
//
// loc: The location if we want an error to be reported, or a Null
// location for "probing" purposes.
//
- // inside_user_defined: controls whether OverloadResolve should use the
+ // use_standard: controls whether OverloadResolve should use the
// ConvertImplicit or ConvertImplicitStandard during overload resolution.
//
// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
// that is the best match of me on Arguments.
//
// </summary>
- public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
+ public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
ArrayList Arguments, Location loc,
bool use_standard)
{
MethodBase candidate = me.Methods [i];
int x;
- x = BetterFunction (tc, Arguments, candidate, method, use_standard);
+ // Check if candidate is applicable (section 14.4.2.1)
+ if (!IsApplicable (Arguments, candidate))
+ continue;
+
+ x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
if (x == 0)
continue;
argument_count = 0;
else
argument_count = Arguments.Count;
-
+
+ //
+ // Now we see if we can find params functions, applicable in their expanded form
+ // since if they were applicable in their normal form, they would have been selected
+ // above anyways
+ //
+ if (best_match_idx == -1) {
+
+ for (int i = me.Methods.Length; i > 0; ) {
+ i--;
+ MethodBase candidate = me.Methods [i];
+
+ if (IsParamsMethodApplicable (Arguments, candidate)) {
+ best_match_idx = i;
+ method = me.Methods [best_match_idx];
+ break;
+ }
+ }
+ }
+
+ //
+ // Now we see if we can at least find a method with the same number of arguments
+ //
ParameterData pd;
- // Now we see if we can at least find a method with the same number of arguments
- // and then try doing implicit conversion on the arguments
if (best_match_idx == -1) {
for (int i = me.Methods.Length; i > 0;) {
} else
continue;
}
-
}
if (method == null)
return null;
-
+
// And now convert implicitly, each argument to the required type
pd = GetParameterData (method);
+ int pd_count = pd.Count;
+
+ for (int j = 0; j < argument_count; j++) {
- for (int j = argument_count; j > 0;) {
- j--;
Argument a = (Argument) Arguments [j];
Expression a_expr = a.Expr;
Type parameter_type = pd.ParameterType (j);
-
- if (a_expr.Type != parameter_type){
- Expression conv;
+ //
+ // Note that we need to compare against the element type
+ // when we have a params method
+ //
+ if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
+ if (j >= pd_count - 1)
+ parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
+ }
+
+ if (a.Type != parameter_type){
+ Expression conv;
+
if (use_standard)
- conv = ConvertImplicitStandard (tc, a_expr, parameter_type,
+ conv = ConvertImplicitStandard (ec, a_expr, parameter_type,
Location.Null);
else
- conv = ConvertImplicit (tc, a_expr, parameter_type,
+ conv = ConvertImplicit (ec, a_expr, parameter_type,
Location.Null);
- if (conv == null){
+ if (conv == null) {
if (!Location.IsNull (loc)) {
- Error (tc, 1502, loc,
- "The best overloaded match for method '" + FullMethodDesc (method) +
+ Error (1502, loc,
+ "The best overloaded match for method '" + FullMethodDesc (method)+
"' has some invalid arguments");
- Error (tc, 1503, loc,
- "Argument " + (j+1) +
- ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
- + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
+ Error (1503, loc,
+ "Argument " + (j+1) +
+ ": Cannot convert from '" + Argument.FullDesc (a)
+ + "' to '" + pd.ParameterDesc (j) + "'");
}
return null;
}
+
+
+
//
// Update the argument with the implicit conversion
//
if (a_expr != conv)
a.Expr = conv;
+
+ // FIXME : For the case of params methods, we need to actually instantiate
+ // an array and initialize it with the argument values etc etc.
+
+ }
+
+ if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
+ pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
+ if (!Location.IsNull (loc)) {
+ Error (1502, loc,
+ "The best overloaded match for method '" + FullMethodDesc (method)+
+ "' has some invalid arguments");
+ Error (1503, loc,
+ "Argument " + (j+1) +
+ ": Cannot convert from '" + Argument.FullDesc (a)
+ + "' to '" + pd.ParameterDesc (j) + "'");
+ }
+ return null;
}
+
+
}
return method;
}
-
- public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
+
+ public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
ArrayList Arguments, Location loc)
{
- return OverloadResolve (tc, me, Arguments, loc, false);
+ return OverloadResolve (ec, me, Arguments, loc, false);
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
//
// First, resolve the expression that is used to
// trigger the invocation
//
- this.expr = expr.Resolve (tc);
- if (this.expr == null)
+ expr = expr.Resolve (ec);
+ if (expr == null)
return null;
- if (!(this.expr is MethodGroupExpr)){
- report118 (tc, Location, this.expr, "method group");
+ if (!(expr is MethodGroupExpr)) {
+ Type expr_type = expr.Type;
+
+ if (expr_type != null){
+ bool IsDelegate = TypeManager.IsDelegateType (expr_type);
+ if (IsDelegate)
+ return (new DelegateInvocation (
+ this.expr, Arguments, loc)).Resolve (ec);
+ }
+ }
+
+ if (!(expr is MethodGroupExpr)){
+ report118 (loc, this.expr, "method group");
return null;
}
--i;
Argument a = (Argument) Arguments [i];
- if (!a.Resolve (tc))
+ if (!a.Resolve (ec, loc))
return null;
}
}
- method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments,
- Location);
+ method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
+
+ if (method == null){
+ Error (-6, loc,
+ "Could not find any applicable function for this argument list");
+ return null;
+ }
+
+ if (method is MethodInfo)
+ type = ((MethodInfo)method).ReturnType;
+
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ // <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;
+ else
+ top = 0;
+
+ if (mb != null)
+ pd = GetParameterData (mb);
+
+ for (int i = 0; i < top; i++){
+ Argument a = (Argument) arguments [i];
+
+ if (pd != null){
+ if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
+ EmitParams (ec, i, arguments);
+ return;
+ }
+ }
+
+ a.Emit (ec);
+ }
+ }
+
+ public static void EmitCall (EmitContext ec,
+ bool is_static, Expression instance_expr,
+ MethodBase method, ArrayList Arguments)
+ {
+ ILGenerator ig = ec.ig;
+ bool struct_call = false;
+
+ if (!is_static){
+ //
+ // If this is ourselves, push "this"
+ //
+ if (instance_expr == null){
+ ig.Emit (OpCodes.Ldarg_0);
+ } else {
+ //
+ // Push the instance expression
+ //
+ if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
+
+ struct_call = true;
+
+ //
+ // If the expression implements IMemoryLocation, then
+ // we can optimize and use AddressOf on the
+ // return.
+ //
+ // If not we have to use some temporary storage for
+ // it.
+ if (instance_expr is IMemoryLocation)
+ ((IMemoryLocation) instance_expr).AddressOf (ec);
+ else {
+ Type t = instance_expr.Type;
+
+ instance_expr.Emit (ec);
+ LocalBuilder temp = ec.GetTemporaryStorage (t);
+ ig.Emit (OpCodes.Stloc, temp);
+ ig.Emit (OpCodes.Ldloca, temp);
+ }
+ } else
+ instance_expr.Emit (ec);
+ }
+ }
+
+ if (Arguments != null)
+ EmitArguments (ec, method, Arguments);
+
+ if (is_static || struct_call){
+ if (method is MethodInfo)
+ ig.Emit (OpCodes.Call, (MethodInfo) method);
+ else
+ ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+ } else {
+ if (method is MethodInfo)
+ ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
+ else
+ ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
+ }
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ MethodGroupExpr mg = (MethodGroupExpr) this.expr;
+ EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
+ }
+
+ public override void EmitStatement (EmitContext ec)
+ {
+ Emit (ec);
+
+ //
+ // Pop the return value if there is one
+ //
+ if (method is MethodInfo){
+ if (((MethodInfo)method).ReturnType != TypeManager.void_type)
+ ec.ig.Emit (OpCodes.Pop);
+ }
+ }
+ }
+
+ public class New : ExpressionStatement {
+ public readonly ArrayList Arguments;
+ public readonly string RequestedType;
+
+ Location loc;
+ MethodBase method = null;
+
+ //
+ // If set, the new expression is for a value_target, and
+ // we will not leave anything on the stack.
+ //
+ Expression value_target;
+
+ public New (string requested_type, ArrayList arguments, Location l)
+ {
+ RequestedType = requested_type;
+ Arguments = arguments;
+ loc = l;
+ }
+
+ public Expression ValueTypeVariable {
+ get {
+ return value_target;
+ }
+
+ set {
+ value_target = value;
+ }
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ type = ec.TypeContainer.LookupType (RequestedType, false);
+
+ if (type == null)
+ return null;
+
+ bool IsDelegate = TypeManager.IsDelegateType (type);
+
+ if (IsDelegate)
+ return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
+
+ Expression ml;
+
+ ml = MemberLookup (ec, type, ".ctor", false,
+ MemberTypes.Constructor, AllBindingsFlags, loc);
+
+ bool is_struct = false;
+ is_struct = type.IsSubclassOf (TypeManager.value_type);
+
+ if (! (ml is MethodGroupExpr)){
+ if (!is_struct){
+ report118 (loc, ml, "method group");
+ return null;
+ }
+ }
+
+ if (ml != null) {
+ if (Arguments != null){
+ for (int i = Arguments.Count; i > 0;){
+ --i;
+ Argument a = (Argument) Arguments [i];
+
+ if (!a.Resolve (ec, loc))
+ return null;
+ }
+ }
- if (method == null){
- Error (tc, -6, Location,
- "Could not find any applicable function for this argument list");
+ method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
+ Arguments, loc);
+ }
+
+ if (method == null && !is_struct) {
+ Error (-6, loc,
+ "New invocation: Can not find a constructor for " +
+ "this argument list");
return null;
}
-
- if (method is MethodInfo)
- type = ((MethodInfo)method).ReturnType;
-
+
eclass = ExprClass.Value;
return this;
}
- public static void EmitArguments (EmitContext ec, MethodBase method, ArrayList Arguments)
+ //
+ // This DoEmit can be invoked in two contexts:
+ // * As a mechanism that will leave a value on the stack (new object)
+ // * As one that wont (init struct)
+ //
+ // You can control whether a value is required on the stack by passing
+ // need_value_on_stack. The code *might* leave a value on the stack
+ // so it must be popped manually
+ //
+ // Returns whether a value is left on the stack
+ //
+ bool DoEmit (EmitContext ec, bool need_value_on_stack)
{
- int top;
-
- if (Arguments != null)
- top = Arguments.Count;
- else
- top = 0;
-
- for (int i = 0; i < top; i++){
- Argument a = (Argument) Arguments [i];
+ if (method == null){
+ IMemoryLocation ml = (IMemoryLocation) value_target;
- a.Emit (ec);
+ ml.AddressOf (ec);
+ } else {
+ Invocation.EmitArguments (ec, method, Arguments);
+ ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
+ return true;
}
- }
-
- public override void Emit (EmitContext ec)
- {
- bool is_static = method.IsStatic;
- if (!is_static){
- MethodGroupExpr mg = (MethodGroupExpr) this.expr;
+ //
+ // It must be a value type, sanity check
+ //
+ if (value_target != null){
+ ec.ig.Emit (OpCodes.Initobj, type);
- //
- // If this is ourselves, push "this"
- //
- if (mg.InstanceExpression == null){
- ec.ig.Emit (OpCodes.Ldarg_0);
- } else {
- //
- // Push the instance expression
- //
- mg.InstanceExpression.Emit (ec);
+ if (need_value_on_stack){
+ value_target.Emit (ec);
+ return true;
}
+ return false;
}
- if (Arguments != null)
- EmitArguments (ec, method, Arguments);
-
- if (is_static){
- if (method is MethodInfo)
- ec.ig.Emit (OpCodes.Call, (MethodInfo) method);
- else
- ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
- } else {
- if (method is MethodInfo)
- ec.ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
- else
- ec.ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
- }
+ throw new Exception ("No method and no value type");
}
+ public override void Emit (EmitContext ec)
+ {
+ DoEmit (ec, true);
+ }
+
public override void EmitStatement (EmitContext ec)
{
- Emit (ec);
-
- //
- // Pop the return value if there is one
- //
- if (method is MethodInfo){
- if (((MethodInfo)method).ReturnType != TypeManager.void_type)
- ec.ig.Emit (OpCodes.Pop);
- }
+ if (DoEmit (ec, false))
+ ec.ig.Emit (OpCodes.Pop);
}
}
- public class New : ExpressionStatement {
-
- public enum NType {
- Object,
- Array
- };
+ // <summary>
+ // Represents an array creation expression.
+ // </summary>
+ //
+ // <remarks>
+ // There are two possible scenarios here: one is an array creation
+ // expression that specifies the dimensions and optionally the
+ // initialization data
+ // </remarks>
+ public class ArrayCreation : ExpressionStatement {
- public readonly NType NewType;
- public readonly ArrayList Arguments;
- public readonly string RequestedType;
- // These are for the case when we have an array
- public readonly string Rank;
- public readonly ArrayList Indices;
- public readonly ArrayList Initializers;
+ string RequestedType;
+ string Rank;
+ ArrayList Initializers;
+ Location loc;
+ ArrayList Arguments;
- Location Location;
MethodBase method = null;
+ Type array_element_type;
+ bool IsOneDimensional = false;
+
+ bool IsBuiltinType = false;
- public New (string requested_type, ArrayList arguments, Location loc)
+ public ArrayCreation (string requested_type, ArrayList exprs,
+ string rank, ArrayList initializers, Location l)
{
RequestedType = requested_type;
- Arguments = arguments;
- NewType = NType.Object;
- Location = loc;
+ Rank = rank;
+ Initializers = initializers;
+ loc = l;
+
+ Arguments = new ArrayList ();
+
+ foreach (Expression e in exprs)
+ Arguments.Add (new Argument (e, Argument.AType.Expression));
+
}
- public New (string requested_type, ArrayList exprs, string rank, ArrayList initializers, Location loc)
+ public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
{
RequestedType = requested_type;
- Indices = exprs;
- Rank = rank;
- Initializers = initializers;
- NewType = NType.Array;
- Location = loc;
+ Rank = rank;
+ Initializers = initializers;
+ loc = l;
}
-
- public override Expression DoResolve (TypeContainer tc)
+
+ public static string FormArrayType (string base_type, int idx_count, string rank)
{
- type = tc.LookupType (RequestedType, false);
+ StringBuilder sb = new StringBuilder (base_type);
- if (type == null)
- return null;
+ sb.Append (rank);
+
+ sb.Append ("[");
+ for (int i = 1; i < idx_count; i++)
+ sb.Append (",");
+ sb.Append ("]");
+
+ return sb.ToString ();
+ }
- Expression ml;
+ public static string FormElementType (string base_type, int idx_count, string rank)
+ {
+ StringBuilder sb = new StringBuilder (base_type);
+
+ sb.Append ("[");
+ for (int i = 1; i < idx_count; i++)
+ sb.Append (",");
+ sb.Append ("]");
- ml = MemberLookup (tc, type, ".ctor", false,
- MemberTypes.Constructor, AllBindingsFlags);
+ sb.Append (rank);
- if (! (ml is MethodGroupExpr)){
- //
- // FIXME: Find proper error
- //
- report118 (tc, Location, ml, "method group");
+ string val = sb.ToString ();
+
+ return val.Substring (0, val.LastIndexOf ("["));
+ }
+
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ int arg_count;
+
+ if (Arguments == null)
+ arg_count = 0;
+ else
+ arg_count = Arguments.Count;
+
+ string array_type = FormArrayType (RequestedType, arg_count, Rank);
+
+ string element_type = FormElementType (RequestedType, arg_count, Rank);
+
+ type = ec.TypeContainer.LookupType (array_type, false);
+
+ array_element_type = ec.TypeContainer.LookupType (element_type, false);
+
+ if (type == null)
return null;
- }
- if (Arguments != null){
- for (int i = Arguments.Count; i > 0;){
- --i;
- Argument a = (Argument) Arguments [i];
+ if (arg_count == 1) {
+ IsOneDimensional = true;
+ eclass = ExprClass.Value;
+ return this;
+ }
- if (!a.Resolve (tc))
- return null;
+ IsBuiltinType = TypeManager.IsBuiltinType (type);
+
+ if (IsBuiltinType) {
+
+ Expression ml;
+
+ ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
+ AllBindingsFlags, loc);
+
+ if (!(ml is MethodGroupExpr)){
+ report118 (loc, ml, "method group");
+ return null;
}
- }
+
+ if (ml == null) {
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
+ "this argument list");
+ return null;
+ }
+
+ if (Arguments != null) {
+ for (int i = arg_count; i > 0;){
+ --i;
+ Argument a = (Argument) Arguments [i];
+
+ if (!a.Resolve (ec, loc))
+ return null;
+ }
+ }
+
+ method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
+
+ if (method == null) {
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
+ "this argument list");
+ return null;
+ }
+
+ eclass = ExprClass.Value;
+ return this;
+
+ } else {
- method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments,
- Location);
+ ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
- if (method == null) {
- Error (tc, -6, Location,
- "New invocation: Can not find a constructor for this argument list");
- return null;
+ ArrayList args = new ArrayList ();
+ if (Arguments != null){
+ for (int i = arg_count; i > 0;){
+ --i;
+ Argument a = (Argument) Arguments [i];
+
+ if (!a.Resolve (ec, loc))
+ return null;
+
+ args.Add (a.Type);
+ }
+ }
+
+ Type [] arg_types = null;
+
+ if (args.Count > 0)
+ arg_types = new Type [args.Count];
+
+ args.CopyTo (arg_types, 0);
+
+ method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
+ arg_types);
+
+ if (method == null) {
+ Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
+ "this argument list");
+ return null;
+ }
+
+ eclass = ExprClass.Value;
+ return this;
+
}
-
- eclass = ExprClass.Value;
- return this;
}
public override void Emit (EmitContext ec)
{
- Invocation.EmitArguments (ec, method, Arguments);
- ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
- }
+ ILGenerator ig = ec.ig;
+
+ if (IsOneDimensional) {
+ Invocation.EmitArguments (ec, null, Arguments);
+ ig.Emit (OpCodes.Newarr, array_element_type);
+
+ } else {
+ Invocation.EmitArguments (ec, method, Arguments);
+
+ if (IsBuiltinType)
+ ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
+ else
+ ig.Emit (OpCodes.Newobj, (MethodInfo) method);
+ }
+ if (Initializers != null){
+ FieldBuilder fb;
+
+ // FIXME: This is just sample data, need to fill with
+ // real values.
+ byte [] a = new byte [4] { 1, 2, 3, 4 };
+
+ fb = ec.TypeContainer.RootContext.MakeStaticData (a);
+
+ ig.Emit (OpCodes.Dup);
+ ig.Emit (OpCodes.Ldtoken, fb);
+ ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
+ }
+ }
+
public override void EmitStatement (EmitContext ec)
{
Emit (ec);
ec.ig.Emit (OpCodes.Pop);
}
+
}
-
+
//
// Represents the `this' construct
//
- public class This : Expression, LValue {
- public override Expression DoResolve (TypeContainer tc)
+ public class This : Expression, IAssignMethod, IMemoryLocation {
+ Location loc;
+
+ public This (Location loc)
+ {
+ this.loc = loc;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
{
eclass = ExprClass.Variable;
- type = tc.TypeBuilder;
+ type = ec.TypeContainer.TypeBuilder;
+
+ if (ec.IsStatic){
+ Report.Error (26, loc,
+ "Keyword this not valid in static code");
+ return null;
+ }
+
+ return this;
+ }
+
+ public Expression DoResolveLValue (EmitContext ec)
+ {
+ DoResolve (ec);
+
+ if (ec.TypeContainer is Class){
+ Report.Error (1604, loc, "Cannot assign to `this'");
+ return null;
+ }
- //
- // FIXME: Verify that this is only used in instance contexts.
- //
return this;
}
ec.ig.Emit (OpCodes.Ldarg_0);
}
- public void Store (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
- //
- // Assignment to the "this" variable.
- //
- // FIXME: Apparently this is a bug that we
- // must catch as `this' seems to be readonly ;-)
- //
+ source.Emit (ec);
ec.ig.Emit (OpCodes.Starg, 0);
}
QueriedType = queried_type;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- typearg = tc.LookupType (QueriedType, false);
+ typearg = ec.TypeContainer.LookupType (QueriedType, false);
if (typearg == null)
return null;
this.QueriedType = queried_type;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
// FIXME: Implement;
throw new Exception ("Unimplemented");
public readonly string Identifier;
Expression expr;
Expression member_lookup;
+ Location loc;
- public MemberAccess (Expression expr, string id)
+ public MemberAccess (Expression expr, string id, Location l)
{
this.expr = expr;
Identifier = id;
+ loc = l;
}
public Expression Expr {
return expr;
}
}
+
+ void error176 (Location loc, string name)
+ {
+ Report.Error (176, loc, "Static member `" +
+ name + "' cannot be accessed " +
+ "with an instance reference, qualify with a " +
+ "type name instead");
+ }
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- Expression new_expression = expr.Resolve (tc);
+ //
+ // We are the sole users of ResolveWithSimpleName (ie, the only
+ // ones that can cope with it
+ //
+ expr = expr.ResolveWithSimpleName (ec);
- if (new_expression == null)
+ if (expr == null)
return null;
- member_lookup = MemberLookup (tc, expr.Type, Identifier, false);
+ if (expr is SimpleName){
+ SimpleName child_expr = (SimpleName) expr;
+
+ expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
+ return expr.Resolve (ec);
+ }
+
+ member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
+
+ if (member_lookup == null)
+ return null;
+
+ //
+ // Method Groups
+ //
if (member_lookup is MethodGroupExpr){
MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
-
- //
- // Bind the instance expression to it
+
//
- // FIXME: This is a horrible way of detecting if it is
- // an instance expression. Figure out how to fix this.
+ // Type.MethodGroup
//
+ if (expr is TypeExpr){
+ if (!mg.RemoveInstanceMethods ()){
+ SimpleName.Error120 (loc, mg.Methods [0].Name);
+ return null;
+ }
- if (expr is LocalVariableReference ||
- expr is ParameterReference ||
- expr is FieldExpr)
- mg.InstanceExpression = expr;
+ return member_lookup;
+ }
+
+ //
+ // Instance.MethodGroup
+ //
+ if (!mg.RemoveStaticMethods ()){
+ error176 (loc, mg.Methods [0].Name);
+ return null;
+ }
+
+ mg.InstanceExpression = expr;
return member_lookup;
- } else if (member_lookup is FieldExpr){
+ }
+
+ if (member_lookup is FieldExpr){
FieldExpr fe = (FieldExpr) member_lookup;
+ FieldInfo fi = fe.FieldInfo;
- fe.Instance = expr;
+ if (fi.IsLiteral) {
+ Type t = fi.FieldType;
+ object o;
- return member_lookup;
- } else
- //
- // FIXME: This should generate the proper node
- // ie, for a Property Access, it should like call it
- // and stuff.
+ 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);
+ }
- return member_lookup;
+ Expression exp = Literalize (o, t);
+ exp.Resolve (ec);
+
+ return exp;
+ }
+
+ if (expr is TypeExpr){
+ if (!fe.FieldInfo.IsStatic){
+ error176 (loc, fe.FieldInfo.Name);
+ return null;
+ }
+ return member_lookup;
+ } else {
+ if (fe.FieldInfo.IsStatic){
+ error176 (loc, fe.FieldInfo.Name);
+ return null;
+ }
+ fe.InstanceExpression = expr;
+
+ return fe;
+ }
+ }
+
+ if (member_lookup is PropertyExpr){
+ PropertyExpr pe = (PropertyExpr) member_lookup;
+
+ if (expr is TypeExpr){
+ if (!pe.IsStatic){
+ SimpleName.Error120 (loc, pe.PropertyInfo.Name);
+ return null;
+ }
+ return pe;
+ } else {
+ if (pe.IsStatic){
+ error176 (loc, pe.PropertyInfo.Name);
+ return null;
+ }
+ pe.InstanceExpression = expr;
+
+ return pe;
+ }
+ }
+
+ Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
+ Environment.Exit (0);
+ return null;
}
public override void Emit (EmitContext ec)
}
- // <summary>
- // Nodes of type Namespace are created during the semantic
- // analysis to resolve member_access/qualified_identifier/simple_name
- // accesses.
- //
- // They are born `resolved'.
- // </summary>
- public class NamespaceExpr : Expression {
- public readonly string Name;
-
- public NamespaceExpr (string name)
+ public class CheckedExpr : Expression {
+
+ public Expression Expr;
+
+ public CheckedExpr (Expression e)
{
- Name = name;
- eclass = ExprClass.Namespace;
+ Expr = e;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
+ Expr = Expr.Resolve (ec);
+
+ if (Expr == null)
+ return null;
+
+ eclass = Expr.ExprClass;
+ type = Expr.Type;
return this;
}
public override void Emit (EmitContext ec)
{
- throw new Exception ("Namespace expressions should never be emitted");
+ bool last_check = ec.CheckState;
+
+ ec.CheckState = true;
+ Expr.Emit (ec);
+ ec.CheckState = last_check;
}
+
}
- // <summary>
- // Fully resolved expression that evaluates to a type
- // </summary>
- public class TypeExpr : Expression {
- public TypeExpr (Type t)
+ public class UnCheckedExpr : Expression {
+
+ public Expression Expr;
+
+ public UnCheckedExpr (Expression e)
{
- Type = t;
- eclass = ExprClass.Type;
+ Expr = e;
}
- override public Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
+ Expr = Expr.Resolve (ec);
+
+ if (Expr == null)
+ return null;
+
+ eclass = Expr.ExprClass;
+ type = Expr.Type;
return this;
}
- override public void Emit (EmitContext ec)
+ public override void Emit (EmitContext ec)
{
- throw new Exception ("Implement me");
+ bool last_check = ec.CheckState;
+
+ ec.CheckState = false;
+ Expr.Emit (ec);
+ ec.CheckState = last_check;
}
+
}
- // <summary>
- // MethodGroup Expression.
- //
- // This is a fully resolved expression that evaluates to a type
- // </summary>
- public class MethodGroupExpr : Expression {
- public readonly MethodBase [] Methods;
- Expression instance_expression = null;
+ public class ElementAccess : Expression {
+ public ArrayList Arguments;
+ public Expression Expr;
+ public Location loc;
- public MethodGroupExpr (MemberInfo [] mi)
+ public ElementAccess (Expression e, ArrayList e_list, Location l)
{
- Methods = new MethodBase [mi.Length];
- mi.CopyTo (Methods, 0);
- eclass = ExprClass.MethodGroup;
+ Expr = e;
+
+ Arguments = new ArrayList ();
+ foreach (Expression tmp in e_list)
+ Arguments.Add (new Argument (tmp, Argument.AType.Expression));
+
+ loc = l;
}
- //
- // `A method group may have associated an instance expression'
- //
- public Expression InstanceExpression {
- get {
- return instance_expression;
- }
+ bool CommonResolve (EmitContext ec)
+ {
+ Expr = Expr.Resolve (ec);
- set {
- instance_expression = value;
+ if (Expr == null)
+ return false;
+
+ if (Arguments == null)
+ return false;
+
+ for (int i = Arguments.Count; i > 0;){
+ --i;
+ Argument a = (Argument) Arguments [i];
+
+ if (!a.Resolve (ec, loc))
+ return false;
}
+
+ return true;
}
-
- override public Expression DoResolve (TypeContainer tc)
+
+ public override Expression DoResolve (EmitContext ec)
{
- return this;
+ if (!CommonResolve (ec))
+ return null;
+
+ //
+ // We perform some simple tests, and then to "split" the emit and store
+ // code we create an instance of a different class, and return that.
+ //
+ // I am experimenting with this pattern.
+ //
+ if (Expr.Type.IsSubclassOf (TypeManager.array_type))
+ return (new ArrayAccess (this)).Resolve (ec);
+ else
+ return (new IndexerAccess (this)).Resolve (ec);
}
- override public void Emit (EmitContext ec)
+ public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ {
+ if (!CommonResolve (ec))
+ return null;
+
+ 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 override void Emit (EmitContext ec)
{
- throw new Exception ("This should never be reached");
+ throw new Exception ("Should never be reached");
}
}
-
- // Fully resolved expression that evaluates to a Field
- // </summary>
- public class FieldExpr : Expression, LValue {
- public readonly FieldInfo FieldInfo;
- public Expression Instance;
-
- public FieldExpr (FieldInfo fi)
+
+ //
+ // Implements array access
+ //
+ public class ArrayAccess : Expression, IAssignMethod {
+ //
+ // Points to our "data" repository
+ //
+ ElementAccess ea;
+
+ public ArrayAccess (ElementAccess ea_data)
{
- FieldInfo = fi;
+ ea = ea_data;
eclass = ExprClass.Variable;
- type = fi.FieldType;
}
- override public Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
- if (!FieldInfo.IsStatic){
- if (Instance == null){
- throw new Exception ("non-static FieldExpr without instance var\n" +
- "You have to assign the Instance variable\n" +
- "Of the FieldExpr to set this\n");
- }
+ if (ea.Expr.ExprClass != ExprClass.Variable) {
+ report118 (ea.loc, ea.Expr, "variable");
+ return null;
+ }
- Instance = Instance.Resolve (tc);
- if (Instance == null)
- return null;
-
+ 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;
}
- override public void Emit (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)
{
+ int rank = ea.Expr.Type.GetArrayRank ();
ILGenerator ig = ec.ig;
- if (FieldInfo.IsStatic)
- ig.Emit (OpCodes.Ldsfld, FieldInfo);
+ ea.Expr.Emit (ec);
+
+ foreach (Argument a in ea.Arguments)
+ a.Expr.Emit (ec);
+
+ if (rank == 1)
+ EmitLoadOpcode (ig, type);
else {
- Instance.Emit (ec);
+ ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
+ Type [] args = new Type [ea.Arguments.Count];
+ MethodInfo get;
+
+ int i = 0;
- ig.Emit (OpCodes.Ldfld, FieldInfo);
+ 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 void Store (EmitContext ec)
+ public void EmitAssign (EmitContext ec, Expression source)
{
- if (FieldInfo.IsStatic)
- ec.ig.Emit (OpCodes.Stsfld, FieldInfo);
- else
- ec.ig.Emit (OpCodes.Stfld, FieldInfo);
- }
+ int rank = ea.Expr.Type.GetArrayRank ();
+ ILGenerator ig = ec.ig;
- public void AddressOf (EmitContext ec)
- {
- if (FieldInfo.IsStatic)
- ec.ig.Emit (OpCodes.Ldsflda, FieldInfo);
- else {
- Instance.Emit (ec);
- ec.ig.Emit (OpCodes.Ldflda, FieldInfo);
+ 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);
}
}
}
-
- // <summary>
- // Fully resolved expression that evaluates to a Property
- // </summary>
- public class PropertyExpr : Expression {
- public readonly PropertyInfo PropertyInfo;
- public readonly bool IsStatic;
-
- public PropertyExpr (PropertyInfo pi)
- {
- PropertyInfo = pi;
- eclass = ExprClass.PropertyAccess;
- IsStatic = false;
-
- MethodBase [] acc = pi.GetAccessors ();
-
- for (int i = 0; i < acc.Length; i++)
- if (acc [i].IsStatic)
- IsStatic = true;
+ class Indexers {
+ public ArrayList getters, setters;
+ static Hashtable map;
- type = pi.PropertyType;
+ static Indexers ()
+ {
+ map = new Hashtable ();
}
- override public Expression DoResolve (TypeContainer tc)
+ Indexers (MemberInfo [] mi)
{
- // We are born in resolved state.
- return this;
- }
+ foreach (PropertyInfo property in mi){
+ MethodInfo get, set;
+
+ get = property.GetGetMethod (true);
+ if (get != null){
+ if (getters == null)
+ getters = new ArrayList ();
- override public void Emit (EmitContext ec)
+ getters.Add (get);
+ }
+
+ set = property.GetSetMethod (true);
+ if (set != null){
+ if (setters == null)
+ setters = new ArrayList ();
+ setters.Add (set);
+ }
+ }
+ }
+
+ static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc)
{
- // FIXME: Implement;
- throw new Exception ("Unimplemented");
+ Indexers ix = (Indexers) map [t];
+ string p_name = TypeManager.IndexerPropertyName (t);
+
+ if (ix != null)
+ return ix;
+
+ MemberInfo [] mi = tm.FindMembers (
+ t, MemberTypes.Property,
+ BindingFlags.Public | BindingFlags.Instance,
+ Type.FilterName, p_name);
+
+ if (mi == null || mi.Length == 0){
+ Report.Error (21, loc,
+ "Type `" + TypeManager.CSharpName (t) + "' does not have " +
+ "any indexers defined");
+ return null;
+ }
+
+ ix = new Indexers (mi);
+ map [t] = ix;
+
+ return ix;
}
}
// <summary>
- // Fully resolved expression that evaluates to a Expression
+ // Expressions that represent an indexer call.
// </summary>
- public class EventExpr : Expression {
- public readonly EventInfo EventInfo;
+ public class IndexerAccess : Expression, IAssignMethod {
+ //
+ // Points to our "data" repository
+ //
+ ElementAccess ea;
+ MethodInfo get, set;
+ Indexers ilist;
+ ArrayList set_arguments;
- public EventExpr (EventInfo ei)
- {
- EventInfo = ei;
- eclass = ExprClass.EventAccess;
- }
-
- override public Expression DoResolve (TypeContainer tc)
+ public IndexerAccess (ElementAccess ea_data)
{
- // We are born in resolved state.
- return this;
+ ea = ea_data;
+ eclass = ExprClass.Value;
}
- override public void Emit (EmitContext ec)
+ public override Expression DoResolve (EmitContext ec)
{
- throw new Exception ("Implement me");
- // FIXME: Implement.
- }
- }
-
- public class CheckedExpr : Expression {
-
- public Expression Expr;
+ Type indexer_type = ea.Expr.Type;
+
+ //
+ // 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.
- public CheckedExpr (Expression e)
- {
- Expr = e;
- }
+ if (ilist == null)
+ ilist = Indexers.GetIndexersForType (
+ indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
- public override Expression DoResolve (TypeContainer tc)
- {
- Expr = Expr.Resolve (tc);
- if (Expr == null)
+ //
+ // 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);
+
+ if (get == null){
+ Report.Error (154, ea.loc,
+ "indexer can not be used in this context, because " +
+ "it lacks a `get' accessor");
return null;
+ }
- eclass = Expr.ExprClass;
- type = Expr.Type;
+ type = get.ReturnType;
+ eclass = ExprClass.IndexerAccess;
return this;
}
- public override void Emit (EmitContext ec)
+ public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
- bool last_check = ec.CheckState;
-
- ec.CheckState = true;
- Expr.Emit (ec);
- ec.CheckState = last_check;
- }
-
- }
-
- public class UnCheckedExpr : Expression {
+ Type indexer_type = ea.Expr.Type;
+ Type right_type = right_side.Type;
- public Expression Expr;
-
- public UnCheckedExpr (Expression e)
- {
- Expr = e;
- }
+ if (ilist == null)
+ ilist = Indexers.GetIndexersForType (
+ indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
- public override Expression DoResolve (TypeContainer tc)
- {
- Expr = Expr.Resolve (tc);
+ if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
+ set_arguments = (ArrayList) ea.Arguments.Clone ();
+ set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
- if (Expr == null)
- return null;
+ set = (MethodInfo) Invocation.OverloadResolve (
+ ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
+ }
+
+ if (set == null){
+ Report.Error (200, ea.loc,
+ "indexer X.this [" + TypeManager.CSharpName (right_type) +
+ "] lacks a `set' accessor");
+ return null;
+ }
- eclass = Expr.ExprClass;
- type = Expr.Type;
+ type = TypeManager.void_type;
+ eclass = ExprClass.IndexerAccess;
return this;
}
-
- public override void Emit (EmitContext ec)
- {
- bool last_check = ec.CheckState;
-
- ec.CheckState = false;
- Expr.Emit (ec);
- ec.CheckState = last_check;
- }
-
- }
-
- public class ElementAccess : Expression {
- public readonly ArrayList Arguments;
- public readonly Expression Expr;
-
- public ElementAccess (Expression e, ArrayList e_list)
+ public override void Emit (EmitContext ec)
{
- Expr = e;
- Arguments = e_list;
+ Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
}
- public override Expression DoResolve (TypeContainer tc)
- {
- // FIXME: Implement;
- throw new Exception ("Unimplemented");
- // return this;
- }
-
- public override void Emit (EmitContext ec)
+ //
+ // source is ignored, because we already have a copy of it from the
+ // LValue resolution and we have already constructed a pre-cached
+ // version of the arguments (ea.set_arguments);
+ //
+ public void EmitAssign (EmitContext ec, Expression source)
{
- // FIXME : Implement !
- throw new Exception ("Unimplemented");
+ Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
}
-
}
public class BaseAccess : Expression {
- public enum BaseAccessType {
+ public enum BaseAccessType : byte {
Member,
Indexer
};
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
// FIXME: Implement;
throw new Exception ("Unimplemented");
}
}
+ // <summary>
+ // This class exists solely to pass the Type around and to be a dummy
+ // that can be passed to the conversion functions (this is used by
+ // foreach implementation to typecast the object return value from
+ // get_Current into the proper type. All code has been generated and
+ // we only care about the side effect conversions to be performed
+ // </summary>
+
+ public class EmptyExpression : Expression {
+ public EmptyExpression ()
+ {
+ type = TypeManager.object_type;
+ eclass = ExprClass.Value;
+ }
+
+ public EmptyExpression (Type t)
+ {
+ type = t;
+ eclass = ExprClass.Value;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ // nothing, as we only exist to not do anything.
+ }
+ }
+
public class UserCast : Expression {
MethodBase method;
Expression source;
- Type most_specific_source;
- Type most_specific_target;
- bool is_explicit;
- public UserCast (MethodInfo method, Expression source, Type most_specific_source,
- Type most_specific_target, bool is_explicit)
+ public UserCast (MethodInfo method, Expression source)
{
this.method = method;
this.source = source;
- this.most_specific_source = most_specific_source;
- this.most_specific_target = most_specific_target;
- this.is_explicit = is_explicit;
type = method.ReturnType;
eclass = ExprClass.Value;
}
- public override Expression DoResolve (TypeContainer tc)
+ public override Expression DoResolve (EmitContext ec)
{
//
- // We are born in a fully resolved state
+ // We are born fully resolved
//
return this;
}
public override void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
- Location tmp = new Location (-1);
- // Note that operators are static anyway
-
- Expression e;
-
- if (!is_explicit)
- e = ConvertImplicitStandard (ec.parent, source, most_specific_source, tmp);
- else
- e = ConvertExplicit (ec.parent, source, most_specific_source, tmp);
-
- e.Emit (ec);
+ source.Emit (ec);
if (method is MethodInfo)
ig.Emit (OpCodes.Call, (MethodInfo) method);
else
ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+
}
}