2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 // (C) 2003, 2004 Novell, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// This is just a helper class, it is generated by Unary, UnaryMutator
21 /// when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
34 eclass = ExprClass.Value;
38 public override Expression DoResolve (EmitContext ec)
41 // We are born fully resolved
46 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, mi, args, false, null);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
56 Expression e, Location loc)
61 args = new ArrayList (1);
62 Argument a = new Argument (e, Argument.AType.Expression);
64 // We need to resolve the arguments before sending them in !
65 if (!a.Resolve (ec, loc))
69 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
74 return new StaticCallExpr ((MethodInfo) method, args, loc);
77 public override void EmitStatement (EmitContext ec)
80 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
81 ec.ig.Emit (OpCodes.Pop);
84 public MethodInfo Method {
89 public class ParenthesizedExpression : Expression
91 public Expression Expr;
93 public ParenthesizedExpression (Expression expr, Location loc)
99 public override Expression DoResolve (EmitContext ec)
101 Expr = Expr.Resolve (ec);
105 public override void Emit (EmitContext ec)
107 throw new Exception ("Should not happen");
112 /// Unary expressions.
116 /// Unary implements unary expressions. It derives from
117 /// ExpressionStatement becuase the pre/post increment/decrement
118 /// operators can be used in a statement context.
120 public class Unary : Expression {
121 public enum Operator : byte {
122 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
123 Indirection, AddressOf, TOP
126 public Operator Oper;
127 public Expression Expr;
129 public Unary (Operator op, Expression expr, Location loc)
137 /// Returns a stringified representation of the Operator
139 static public string OperName (Operator oper)
142 case Operator.UnaryPlus:
144 case Operator.UnaryNegation:
146 case Operator.LogicalNot:
148 case Operator.OnesComplement:
150 case Operator.AddressOf:
152 case Operator.Indirection:
156 return oper.ToString ();
159 public static readonly string [] oper_names;
163 oper_names = new string [(int)Operator.TOP];
165 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
166 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
167 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
168 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
169 oper_names [(int) Operator.Indirection] = "op_Indirection";
170 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
173 void Error23 (Type t)
176 23, "Operator " + OperName (Oper) +
177 " cannot be applied to operand of type `" +
178 TypeManager.CSharpName (t) + "'");
182 /// The result has been already resolved:
184 /// FIXME: a minus constant -128 sbyte cant be turned into a
187 static Expression TryReduceNegative (Constant expr)
191 if (expr is IntConstant)
192 e = new IntConstant (-((IntConstant) expr).Value);
193 else if (expr is UIntConstant){
194 uint value = ((UIntConstant) expr).Value;
196 if (value < 2147483649)
197 return new IntConstant (-(int)value);
199 e = new LongConstant (-value);
201 else if (expr is LongConstant)
202 e = new LongConstant (-((LongConstant) expr).Value);
203 else if (expr is ULongConstant){
204 ulong value = ((ULongConstant) expr).Value;
206 if (value < 9223372036854775809)
207 return new LongConstant(-(long)value);
209 else if (expr is FloatConstant)
210 e = new FloatConstant (-((FloatConstant) expr).Value);
211 else if (expr is DoubleConstant)
212 e = new DoubleConstant (-((DoubleConstant) expr).Value);
213 else if (expr is DecimalConstant)
214 e = new DecimalConstant (-((DecimalConstant) expr).Value);
215 else if (expr is ShortConstant)
216 e = new IntConstant (-((ShortConstant) expr).Value);
217 else if (expr is UShortConstant)
218 e = new IntConstant (-((UShortConstant) expr).Value);
223 // This routine will attempt to simplify the unary expression when the
224 // argument is a constant. The result is returned in `result' and the
225 // function returns true or false depending on whether a reduction
226 // was performed or not
228 bool Reduce (EmitContext ec, Constant e, out Expression result)
230 Type expr_type = e.Type;
233 case Operator.UnaryPlus:
237 case Operator.UnaryNegation:
238 result = TryReduceNegative (e);
241 case Operator.LogicalNot:
242 if (expr_type != TypeManager.bool_type) {
248 BoolConstant b = (BoolConstant) e;
249 result = new BoolConstant (!(b.Value));
252 case Operator.OnesComplement:
253 if (!((expr_type == TypeManager.int32_type) ||
254 (expr_type == TypeManager.uint32_type) ||
255 (expr_type == TypeManager.int64_type) ||
256 (expr_type == TypeManager.uint64_type) ||
257 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
260 if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
261 result = new Cast (new TypeExpression (TypeManager.int32_type, loc), e, loc);
262 result = result.Resolve (ec);
263 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
264 result = new Cast (new TypeExpression (TypeManager.uint32_type, loc), e, loc);
265 result = result.Resolve (ec);
266 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
267 result = new Cast (new TypeExpression (TypeManager.int64_type, loc), e, loc);
268 result = result.Resolve (ec);
269 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
270 result = new Cast (new TypeExpression (TypeManager.uint64_type, loc), e, loc);
271 result = result.Resolve (ec);
274 if (result == null || !(result is Constant)){
280 expr_type = result.Type;
281 e = (Constant) result;
284 if (e is EnumConstant){
285 EnumConstant enum_constant = (EnumConstant) e;
288 if (Reduce (ec, enum_constant.Child, out reduced)){
289 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
297 if (expr_type == TypeManager.int32_type){
298 result = new IntConstant (~ ((IntConstant) e).Value);
299 } else if (expr_type == TypeManager.uint32_type){
300 result = new UIntConstant (~ ((UIntConstant) e).Value);
301 } else if (expr_type == TypeManager.int64_type){
302 result = new LongConstant (~ ((LongConstant) e).Value);
303 } else if (expr_type == TypeManager.uint64_type){
304 result = new ULongConstant (~ ((ULongConstant) e).Value);
312 case Operator.AddressOf:
316 case Operator.Indirection:
320 throw new Exception ("Can not constant fold: " + Oper.ToString());
323 Expression ResolveOperator (EmitContext ec)
325 Type expr_type = Expr.Type;
328 // Step 1: Perform Operator Overload location
333 op_name = oper_names [(int) Oper];
335 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
338 Expression e = StaticCallExpr.MakeSimpleCall (
339 ec, (MethodGroupExpr) mg, Expr, loc);
349 // Only perform numeric promotions on:
352 if (expr_type == null)
356 // Step 2: Default operations on CLI native types.
359 // Attempt to use a constant folding operation.
360 if (Expr is Constant){
363 if (Reduce (ec, (Constant) Expr, out result))
368 case Operator.LogicalNot:
369 if (expr_type != TypeManager.bool_type) {
370 Expr = ResolveBoolean (ec, Expr, loc);
377 type = TypeManager.bool_type;
380 case Operator.OnesComplement:
381 if (!((expr_type == TypeManager.int32_type) ||
382 (expr_type == TypeManager.uint32_type) ||
383 (expr_type == TypeManager.int64_type) ||
384 (expr_type == TypeManager.uint64_type) ||
385 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
388 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
390 type = TypeManager.int32_type;
393 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
395 type = TypeManager.uint32_type;
398 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
400 type = TypeManager.int64_type;
403 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
405 type = TypeManager.uint64_type;
414 case Operator.AddressOf:
415 if (Expr.eclass != ExprClass.Variable){
416 Error (211, "Cannot take the address of non-variables");
425 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
429 IVariable variable = Expr as IVariable;
430 if (!ec.InFixedInitializer && ((variable == null) || !variable.VerifyFixed (false))) {
431 Error (212, "You can only take the address of an unfixed expression inside " +
432 "of a fixed statement initializer");
436 if (ec.InFixedInitializer && ((variable != null) && variable.VerifyFixed (false))) {
437 Error (213, "You can not fix an already fixed expression");
441 // According to the specs, a variable is considered definitely assigned if you take
443 if ((variable != null) && (variable.VariableInfo != null))
444 variable.VariableInfo.SetAssigned (ec);
446 type = TypeManager.GetPointerType (Expr.Type);
449 case Operator.Indirection:
455 if (!expr_type.IsPointer){
456 Error (193, "The * or -> operator can only be applied to pointers");
461 // We create an Indirection expression, because
462 // it can implement the IMemoryLocation.
464 return new Indirection (Expr, loc);
466 case Operator.UnaryPlus:
468 // A plus in front of something is just a no-op, so return the child.
472 case Operator.UnaryNegation:
474 // Deals with -literals
475 // int operator- (int x)
476 // long operator- (long x)
477 // float operator- (float f)
478 // double operator- (double d)
479 // decimal operator- (decimal d)
481 Expression expr = null;
484 // transform - - expr into expr
487 Unary unary = (Unary) Expr;
489 if (unary.Oper == Operator.UnaryNegation)
494 // perform numeric promotions to int,
498 // The following is inneficient, because we call
499 // ImplicitConversion too many times.
501 // It is also not clear if we should convert to Float
502 // or Double initially.
504 if (expr_type == TypeManager.uint32_type){
506 // FIXME: handle exception to this rule that
507 // permits the int value -2147483648 (-2^31) to
508 // bt wrote as a decimal interger literal
510 type = TypeManager.int64_type;
511 Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
515 if (expr_type == TypeManager.uint64_type){
517 // FIXME: Handle exception of `long value'
518 // -92233720368547758087 (-2^63) to be wrote as
519 // decimal integer literal.
525 if (expr_type == TypeManager.float_type){
530 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
537 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
544 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
555 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
556 TypeManager.CSharpName (expr_type) + "'");
560 public override Expression DoResolve (EmitContext ec)
562 if (Oper == Operator.AddressOf)
563 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
565 Expr = Expr.Resolve (ec);
570 eclass = ExprClass.Value;
571 return ResolveOperator (ec);
574 public override Expression DoResolveLValue (EmitContext ec, Expression right)
576 if (Oper == Operator.Indirection)
577 return base.DoResolveLValue (ec, right);
579 Error (131, "The left-hand side of an assignment must be a " +
580 "variable, property or indexer");
584 public override void Emit (EmitContext ec)
586 ILGenerator ig = ec.ig;
589 case Operator.UnaryPlus:
590 throw new Exception ("This should be caught by Resolve");
592 case Operator.UnaryNegation:
594 ig.Emit (OpCodes.Ldc_I4_0);
595 if (type == TypeManager.int64_type)
596 ig.Emit (OpCodes.Conv_U8);
598 ig.Emit (OpCodes.Sub_Ovf);
601 ig.Emit (OpCodes.Neg);
606 case Operator.LogicalNot:
608 ig.Emit (OpCodes.Ldc_I4_0);
609 ig.Emit (OpCodes.Ceq);
612 case Operator.OnesComplement:
614 ig.Emit (OpCodes.Not);
617 case Operator.AddressOf:
618 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
622 throw new Exception ("This should not happen: Operator = "
627 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
629 if (Oper == Operator.LogicalNot)
630 Expr.EmitBranchable (ec, target, !onTrue);
632 base.EmitBranchable (ec, target, onTrue);
635 public override string ToString ()
637 return "Unary (" + Oper + ", " + Expr + ")";
643 // Unary operators are turned into Indirection expressions
644 // after semantic analysis (this is so we can take the address
645 // of an indirection).
647 public class Indirection : Expression, IMemoryLocation, IAssignMethod {
649 LocalTemporary temporary;
652 public Indirection (Expression expr, Location l)
655 this.type = TypeManager.GetElementType (expr.Type);
656 eclass = ExprClass.Variable;
660 void LoadExprValue (EmitContext ec)
664 public override void Emit (EmitContext ec)
669 LoadFromPtr (ec.ig, Type);
672 public void Emit (EmitContext ec, bool leave_copy)
676 ec.ig.Emit (OpCodes.Dup);
677 temporary = new LocalTemporary (ec, expr.Type);
678 temporary.Store (ec);
682 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
684 prepared = prepare_for_load;
688 if (prepare_for_load)
689 ec.ig.Emit (OpCodes.Dup);
693 ec.ig.Emit (OpCodes.Dup);
694 temporary = new LocalTemporary (ec, expr.Type);
695 temporary.Store (ec);
698 StoreFromPtr (ec.ig, type);
700 if (temporary != null)
704 public void AddressOf (EmitContext ec, AddressOp Mode)
709 public override Expression DoResolve (EmitContext ec)
712 // Born fully resolved
717 public override string ToString ()
719 return "*(" + expr + ")";
724 /// Unary Mutator expressions (pre and post ++ and --)
728 /// UnaryMutator implements ++ and -- expressions. It derives from
729 /// ExpressionStatement becuase the pre/post increment/decrement
730 /// operators can be used in a statement context.
732 /// FIXME: Idea, we could split this up in two classes, one simpler
733 /// for the common case, and one with the extra fields for more complex
734 /// classes (indexers require temporary access; overloaded require method)
737 public class UnaryMutator : ExpressionStatement {
739 public enum Mode : byte {
746 PreDecrement = IsDecrement,
747 PostIncrement = IsPost,
748 PostDecrement = IsPost | IsDecrement
752 bool is_expr = false;
753 bool recurse = false;
758 // This is expensive for the simplest case.
760 StaticCallExpr method;
762 public UnaryMutator (Mode m, Expression e, Location l)
769 static string OperName (Mode mode)
771 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
775 void Error23 (Type t)
778 23, "Operator " + OperName (mode) +
779 " cannot be applied to operand of type `" +
780 TypeManager.CSharpName (t) + "'");
784 /// Returns whether an object of type `t' can be incremented
785 /// or decremented with add/sub (ie, basically whether we can
786 /// use pre-post incr-decr operations on it, but it is not a
787 /// System.Decimal, which we require operator overloading to catch)
789 static bool IsIncrementableNumber (Type t)
791 return (t == TypeManager.sbyte_type) ||
792 (t == TypeManager.byte_type) ||
793 (t == TypeManager.short_type) ||
794 (t == TypeManager.ushort_type) ||
795 (t == TypeManager.int32_type) ||
796 (t == TypeManager.uint32_type) ||
797 (t == TypeManager.int64_type) ||
798 (t == TypeManager.uint64_type) ||
799 (t == TypeManager.char_type) ||
800 (t.IsSubclassOf (TypeManager.enum_type)) ||
801 (t == TypeManager.float_type) ||
802 (t == TypeManager.double_type) ||
803 (t.IsPointer && t != TypeManager.void_ptr_type);
806 Expression ResolveOperator (EmitContext ec)
808 Type expr_type = expr.Type;
811 // Step 1: Perform Operator Overload location
816 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
817 op_name = "op_Increment";
819 op_name = "op_Decrement";
821 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
823 if (mg == null && expr_type.BaseType != null)
824 mg = MemberLookup (ec, expr_type.BaseType, op_name,
825 MemberTypes.Method, AllBindingFlags, loc);
828 method = StaticCallExpr.MakeSimpleCall (
829 ec, (MethodGroupExpr) mg, expr, loc);
836 // The operand of the prefix/postfix increment decrement operators
837 // should be an expression that is classified as a variable,
838 // a property access or an indexer access
841 if (expr.eclass == ExprClass.Variable){
842 LocalVariableReference var = expr as LocalVariableReference;
843 if ((var != null) && var.IsReadOnly)
844 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
845 if (IsIncrementableNumber (expr_type) ||
846 expr_type == TypeManager.decimal_type){
849 } else if (expr.eclass == ExprClass.IndexerAccess){
850 IndexerAccess ia = (IndexerAccess) expr;
852 expr = ia.ResolveLValue (ec, this);
857 } else if (expr.eclass == ExprClass.PropertyAccess){
858 PropertyExpr pe = (PropertyExpr) expr;
860 if (pe.VerifyAssignable ())
865 expr.Error_UnexpectedKind ("variable, indexer or property access");
869 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
870 TypeManager.CSharpName (expr_type) + "'");
874 public override Expression DoResolve (EmitContext ec)
876 expr = expr.Resolve (ec);
881 eclass = ExprClass.Value;
882 return ResolveOperator (ec);
885 static int PtrTypeSize (Type t)
887 return GetTypeSize (TypeManager.GetElementType (t));
891 // Loads the proper "1" into the stack based on the type, then it emits the
892 // opcode for the operation requested
894 void LoadOneAndEmitOp (EmitContext ec, Type t)
897 // Measure if getting the typecode and using that is more/less efficient
898 // that comparing types. t.GetTypeCode() is an internal call.
900 ILGenerator ig = ec.ig;
902 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
903 LongConstant.EmitLong (ig, 1);
904 else if (t == TypeManager.double_type)
905 ig.Emit (OpCodes.Ldc_R8, 1.0);
906 else if (t == TypeManager.float_type)
907 ig.Emit (OpCodes.Ldc_R4, 1.0F);
908 else if (t.IsPointer){
909 int n = PtrTypeSize (t);
912 ig.Emit (OpCodes.Sizeof, t);
914 IntConstant.EmitInt (ig, n);
916 ig.Emit (OpCodes.Ldc_I4_1);
919 // Now emit the operation
922 if (t == TypeManager.int32_type ||
923 t == TypeManager.int64_type){
924 if ((mode & Mode.IsDecrement) != 0)
925 ig.Emit (OpCodes.Sub_Ovf);
927 ig.Emit (OpCodes.Add_Ovf);
928 } else if (t == TypeManager.uint32_type ||
929 t == TypeManager.uint64_type){
930 if ((mode & Mode.IsDecrement) != 0)
931 ig.Emit (OpCodes.Sub_Ovf_Un);
933 ig.Emit (OpCodes.Add_Ovf_Un);
935 if ((mode & Mode.IsDecrement) != 0)
936 ig.Emit (OpCodes.Sub_Ovf);
938 ig.Emit (OpCodes.Add_Ovf);
941 if ((mode & Mode.IsDecrement) != 0)
942 ig.Emit (OpCodes.Sub);
944 ig.Emit (OpCodes.Add);
947 if (t == TypeManager.sbyte_type){
949 ig.Emit (OpCodes.Conv_Ovf_I1);
951 ig.Emit (OpCodes.Conv_I1);
952 } else if (t == TypeManager.byte_type){
954 ig.Emit (OpCodes.Conv_Ovf_U1);
956 ig.Emit (OpCodes.Conv_U1);
957 } else if (t == TypeManager.short_type){
959 ig.Emit (OpCodes.Conv_Ovf_I2);
961 ig.Emit (OpCodes.Conv_I2);
962 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
964 ig.Emit (OpCodes.Conv_Ovf_U2);
966 ig.Emit (OpCodes.Conv_U2);
971 void EmitCode (EmitContext ec, bool is_expr)
974 this.is_expr = is_expr;
975 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
979 public override void Emit (EmitContext ec)
982 // We use recurse to allow ourselfs to be the source
983 // of an assignment. This little hack prevents us from
984 // having to allocate another expression
987 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
989 LoadOneAndEmitOp (ec, expr.Type);
991 ec.ig.Emit (OpCodes.Call, method.Method);
999 public override void EmitStatement (EmitContext ec)
1001 EmitCode (ec, false);
1006 /// Base class for the `Is' and `As' classes.
1010 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1013 public abstract class Probe : Expression {
1014 public readonly Expression ProbeType;
1015 protected Expression expr;
1016 protected Type probe_type;
1018 public Probe (Expression expr, Expression probe_type, Location l)
1020 ProbeType = probe_type;
1025 public Expression Expr {
1031 public override Expression DoResolve (EmitContext ec)
1033 probe_type = ec.DeclSpace.ResolveType (ProbeType, false, loc);
1035 if (probe_type == null)
1038 CheckObsoleteAttribute (probe_type);
1040 expr = expr.Resolve (ec);
1049 /// Implementation of the `is' operator.
1051 public class Is : Probe {
1052 public Is (Expression expr, Expression probe_type, Location l)
1053 : base (expr, probe_type, l)
1058 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1063 public override void Emit (EmitContext ec)
1065 ILGenerator ig = ec.ig;
1070 case Action.AlwaysFalse:
1071 ig.Emit (OpCodes.Pop);
1072 IntConstant.EmitInt (ig, 0);
1074 case Action.AlwaysTrue:
1075 ig.Emit (OpCodes.Pop);
1076 IntConstant.EmitInt (ig, 1);
1078 case Action.LeaveOnStack:
1079 // the `e != null' rule.
1080 ig.Emit (OpCodes.Ldnull);
1081 ig.Emit (OpCodes.Ceq);
1082 ig.Emit (OpCodes.Ldc_I4_0);
1083 ig.Emit (OpCodes.Ceq);
1086 ig.Emit (OpCodes.Isinst, probe_type);
1087 ig.Emit (OpCodes.Ldnull);
1088 ig.Emit (OpCodes.Cgt_Un);
1091 throw new Exception ("never reached");
1094 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
1096 ILGenerator ig = ec.ig;
1099 case Action.AlwaysFalse:
1101 ig.Emit (OpCodes.Br, target);
1104 case Action.AlwaysTrue:
1106 ig.Emit (OpCodes.Br, target);
1109 case Action.LeaveOnStack:
1110 // the `e != null' rule.
1112 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1116 ig.Emit (OpCodes.Isinst, probe_type);
1117 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1120 throw new Exception ("never reached");
1123 public override Expression DoResolve (EmitContext ec)
1125 Expression e = base.DoResolve (ec);
1127 if ((e == null) || (expr == null))
1130 Type etype = expr.Type;
1131 bool warning_always_matches = false;
1132 bool warning_never_matches = false;
1134 type = TypeManager.bool_type;
1135 eclass = ExprClass.Value;
1138 // First case, if at compile time, there is an implicit conversion
1139 // then e != null (objects) or true (value types)
1141 e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
1144 if (etype.IsValueType)
1145 action = Action.AlwaysTrue;
1147 action = Action.LeaveOnStack;
1149 warning_always_matches = true;
1150 } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1152 // Second case: explicit reference convresion
1154 if (expr is NullLiteral)
1155 action = Action.AlwaysFalse;
1157 action = Action.Probe;
1159 action = Action.AlwaysFalse;
1160 warning_never_matches = true;
1163 if (warning_always_matches)
1164 Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
1165 else if (warning_never_matches){
1166 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1167 Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
1175 /// Implementation of the `as' operator.
1177 public class As : Probe {
1178 public As (Expression expr, Expression probe_type, Location l)
1179 : base (expr, probe_type, l)
1183 bool do_isinst = false;
1185 public override void Emit (EmitContext ec)
1187 ILGenerator ig = ec.ig;
1192 ig.Emit (OpCodes.Isinst, probe_type);
1195 static void Error_CannotConvertType (Type source, Type target, Location loc)
1198 39, loc, "as operator can not convert from `" +
1199 TypeManager.CSharpName (source) + "' to `" +
1200 TypeManager.CSharpName (target) + "'");
1203 public override Expression DoResolve (EmitContext ec)
1205 Expression e = base.DoResolve (ec);
1211 eclass = ExprClass.Value;
1212 Type etype = expr.Type;
1214 if (TypeManager.IsValueType (probe_type)){
1215 Report.Error (77, loc, "The as operator should be used with a reference type only (" +
1216 TypeManager.CSharpName (probe_type) + " is a value type)");
1221 e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
1228 if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1233 Error_CannotConvertType (etype, probe_type, loc);
1239 /// This represents a typecast in the source language.
1241 /// FIXME: Cast expressions have an unusual set of parsing
1242 /// rules, we need to figure those out.
1244 public class Cast : Expression {
1245 Expression target_type;
1248 public Cast (Expression cast_type, Expression expr, Location loc)
1250 this.target_type = cast_type;
1255 public Expression TargetType {
1261 public Expression Expr {
1270 bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
1272 if (!ec.ConstantCheckState)
1275 if ((value < min) || (value > max)) {
1276 Error (221, "Constant value `" + value + "' cannot be converted " +
1277 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1278 "syntax to override)");
1285 bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
1287 if (!ec.ConstantCheckState)
1291 Error (221, "Constant value `" + value + "' cannot be converted " +
1292 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1293 "syntax to override)");
1300 bool CheckUnsigned (EmitContext ec, long value, Type type)
1302 if (!ec.ConstantCheckState)
1306 Error (221, "Constant value `" + value + "' cannot be converted " +
1307 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1308 "syntax to override)");
1316 /// Attempts to do a compile-time folding of a constant cast.
1318 Expression TryReduce (EmitContext ec, Type target_type)
1320 Expression real_expr = expr;
1321 if (real_expr is EnumConstant)
1322 real_expr = ((EnumConstant) real_expr).Child;
1324 if (real_expr is ByteConstant){
1325 byte v = ((ByteConstant) real_expr).Value;
1327 if (target_type == TypeManager.sbyte_type) {
1328 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1330 return new SByteConstant ((sbyte) v);
1332 if (target_type == TypeManager.short_type)
1333 return new ShortConstant ((short) v);
1334 if (target_type == TypeManager.ushort_type)
1335 return new UShortConstant ((ushort) v);
1336 if (target_type == TypeManager.int32_type)
1337 return new IntConstant ((int) v);
1338 if (target_type == TypeManager.uint32_type)
1339 return new UIntConstant ((uint) v);
1340 if (target_type == TypeManager.int64_type)
1341 return new LongConstant ((long) v);
1342 if (target_type == TypeManager.uint64_type)
1343 return new ULongConstant ((ulong) v);
1344 if (target_type == TypeManager.float_type)
1345 return new FloatConstant ((float) v);
1346 if (target_type == TypeManager.double_type)
1347 return new DoubleConstant ((double) v);
1348 if (target_type == TypeManager.char_type)
1349 return new CharConstant ((char) v);
1350 if (target_type == TypeManager.decimal_type)
1351 return new DecimalConstant ((decimal) v);
1353 if (real_expr is SByteConstant){
1354 sbyte v = ((SByteConstant) real_expr).Value;
1356 if (target_type == TypeManager.byte_type) {
1357 if (!CheckUnsigned (ec, v, target_type))
1359 return new ByteConstant ((byte) v);
1361 if (target_type == TypeManager.short_type)
1362 return new ShortConstant ((short) v);
1363 if (target_type == TypeManager.ushort_type) {
1364 if (!CheckUnsigned (ec, v, target_type))
1366 return new UShortConstant ((ushort) v);
1367 } if (target_type == TypeManager.int32_type)
1368 return new IntConstant ((int) v);
1369 if (target_type == TypeManager.uint32_type) {
1370 if (!CheckUnsigned (ec, v, target_type))
1372 return new UIntConstant ((uint) v);
1373 } if (target_type == TypeManager.int64_type)
1374 return new LongConstant ((long) v);
1375 if (target_type == TypeManager.uint64_type) {
1376 if (!CheckUnsigned (ec, v, target_type))
1378 return new ULongConstant ((ulong) v);
1380 if (target_type == TypeManager.float_type)
1381 return new FloatConstant ((float) v);
1382 if (target_type == TypeManager.double_type)
1383 return new DoubleConstant ((double) v);
1384 if (target_type == TypeManager.char_type) {
1385 if (!CheckUnsigned (ec, v, target_type))
1387 return new CharConstant ((char) v);
1389 if (target_type == TypeManager.decimal_type)
1390 return new DecimalConstant ((decimal) v);
1392 if (real_expr is ShortConstant){
1393 short v = ((ShortConstant) real_expr).Value;
1395 if (target_type == TypeManager.byte_type) {
1396 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1398 return new ByteConstant ((byte) v);
1400 if (target_type == TypeManager.sbyte_type) {
1401 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1403 return new SByteConstant ((sbyte) v);
1405 if (target_type == TypeManager.ushort_type) {
1406 if (!CheckUnsigned (ec, v, target_type))
1408 return new UShortConstant ((ushort) v);
1410 if (target_type == TypeManager.int32_type)
1411 return new IntConstant ((int) v);
1412 if (target_type == TypeManager.uint32_type) {
1413 if (!CheckUnsigned (ec, v, target_type))
1415 return new UIntConstant ((uint) v);
1417 if (target_type == TypeManager.int64_type)
1418 return new LongConstant ((long) v);
1419 if (target_type == TypeManager.uint64_type) {
1420 if (!CheckUnsigned (ec, v, target_type))
1422 return new ULongConstant ((ulong) v);
1424 if (target_type == TypeManager.float_type)
1425 return new FloatConstant ((float) v);
1426 if (target_type == TypeManager.double_type)
1427 return new DoubleConstant ((double) v);
1428 if (target_type == TypeManager.char_type) {
1429 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1431 return new CharConstant ((char) v);
1433 if (target_type == TypeManager.decimal_type)
1434 return new DecimalConstant ((decimal) v);
1436 if (real_expr is UShortConstant){
1437 ushort v = ((UShortConstant) real_expr).Value;
1439 if (target_type == TypeManager.byte_type) {
1440 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1442 return new ByteConstant ((byte) v);
1444 if (target_type == TypeManager.sbyte_type) {
1445 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1447 return new SByteConstant ((sbyte) v);
1449 if (target_type == TypeManager.short_type) {
1450 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1452 return new ShortConstant ((short) v);
1454 if (target_type == TypeManager.int32_type)
1455 return new IntConstant ((int) v);
1456 if (target_type == TypeManager.uint32_type)
1457 return new UIntConstant ((uint) v);
1458 if (target_type == TypeManager.int64_type)
1459 return new LongConstant ((long) v);
1460 if (target_type == TypeManager.uint64_type)
1461 return new ULongConstant ((ulong) v);
1462 if (target_type == TypeManager.float_type)
1463 return new FloatConstant ((float) v);
1464 if (target_type == TypeManager.double_type)
1465 return new DoubleConstant ((double) v);
1466 if (target_type == TypeManager.char_type) {
1467 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1469 return new CharConstant ((char) v);
1471 if (target_type == TypeManager.decimal_type)
1472 return new DecimalConstant ((decimal) v);
1474 if (real_expr is IntConstant){
1475 int v = ((IntConstant) real_expr).Value;
1477 if (target_type == TypeManager.byte_type) {
1478 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1480 return new ByteConstant ((byte) v);
1482 if (target_type == TypeManager.sbyte_type) {
1483 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1485 return new SByteConstant ((sbyte) v);
1487 if (target_type == TypeManager.short_type) {
1488 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1490 return new ShortConstant ((short) v);
1492 if (target_type == TypeManager.ushort_type) {
1493 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1495 return new UShortConstant ((ushort) v);
1497 if (target_type == TypeManager.uint32_type) {
1498 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1500 return new UIntConstant ((uint) v);
1502 if (target_type == TypeManager.int64_type)
1503 return new LongConstant ((long) v);
1504 if (target_type == TypeManager.uint64_type) {
1505 if (!CheckUnsigned (ec, v, target_type))
1507 return new ULongConstant ((ulong) v);
1509 if (target_type == TypeManager.float_type)
1510 return new FloatConstant ((float) v);
1511 if (target_type == TypeManager.double_type)
1512 return new DoubleConstant ((double) v);
1513 if (target_type == TypeManager.char_type) {
1514 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1516 return new CharConstant ((char) v);
1518 if (target_type == TypeManager.decimal_type)
1519 return new DecimalConstant ((decimal) v);
1521 if (real_expr is UIntConstant){
1522 uint v = ((UIntConstant) real_expr).Value;
1524 if (target_type == TypeManager.byte_type) {
1525 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1527 return new ByteConstant ((byte) v);
1529 if (target_type == TypeManager.sbyte_type) {
1530 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1532 return new SByteConstant ((sbyte) v);
1534 if (target_type == TypeManager.short_type) {
1535 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1537 return new ShortConstant ((short) v);
1539 if (target_type == TypeManager.ushort_type) {
1540 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1542 return new UShortConstant ((ushort) v);
1544 if (target_type == TypeManager.int32_type) {
1545 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1547 return new IntConstant ((int) v);
1549 if (target_type == TypeManager.int64_type)
1550 return new LongConstant ((long) v);
1551 if (target_type == TypeManager.uint64_type)
1552 return new ULongConstant ((ulong) v);
1553 if (target_type == TypeManager.float_type)
1554 return new FloatConstant ((float) v);
1555 if (target_type == TypeManager.double_type)
1556 return new DoubleConstant ((double) v);
1557 if (target_type == TypeManager.char_type) {
1558 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1560 return new CharConstant ((char) v);
1562 if (target_type == TypeManager.decimal_type)
1563 return new DecimalConstant ((decimal) v);
1565 if (real_expr is LongConstant){
1566 long v = ((LongConstant) real_expr).Value;
1568 if (target_type == TypeManager.byte_type) {
1569 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1571 return new ByteConstant ((byte) v);
1573 if (target_type == TypeManager.sbyte_type) {
1574 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1576 return new SByteConstant ((sbyte) v);
1578 if (target_type == TypeManager.short_type) {
1579 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1581 return new ShortConstant ((short) v);
1583 if (target_type == TypeManager.ushort_type) {
1584 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1586 return new UShortConstant ((ushort) v);
1588 if (target_type == TypeManager.int32_type) {
1589 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1591 return new IntConstant ((int) v);
1593 if (target_type == TypeManager.uint32_type) {
1594 if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
1596 return new UIntConstant ((uint) v);
1598 if (target_type == TypeManager.uint64_type) {
1599 if (!CheckUnsigned (ec, v, target_type))
1601 return new ULongConstant ((ulong) v);
1603 if (target_type == TypeManager.float_type)
1604 return new FloatConstant ((float) v);
1605 if (target_type == TypeManager.double_type)
1606 return new DoubleConstant ((double) v);
1607 if (target_type == TypeManager.char_type) {
1608 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1610 return new CharConstant ((char) v);
1612 if (target_type == TypeManager.decimal_type)
1613 return new DecimalConstant ((decimal) v);
1615 if (real_expr is ULongConstant){
1616 ulong v = ((ULongConstant) real_expr).Value;
1618 if (target_type == TypeManager.byte_type) {
1619 if (!CheckRange (ec, v, target_type, Byte.MaxValue))
1621 return new ByteConstant ((byte) v);
1623 if (target_type == TypeManager.sbyte_type) {
1624 if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
1626 return new SByteConstant ((sbyte) v);
1628 if (target_type == TypeManager.short_type) {
1629 if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
1631 return new ShortConstant ((short) v);
1633 if (target_type == TypeManager.ushort_type) {
1634 if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
1636 return new UShortConstant ((ushort) v);
1638 if (target_type == TypeManager.int32_type) {
1639 if (!CheckRange (ec, v, target_type, Int32.MaxValue))
1641 return new IntConstant ((int) v);
1643 if (target_type == TypeManager.uint32_type) {
1644 if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
1646 return new UIntConstant ((uint) v);
1648 if (target_type == TypeManager.int64_type) {
1649 if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
1651 return new LongConstant ((long) v);
1653 if (target_type == TypeManager.float_type)
1654 return new FloatConstant ((float) v);
1655 if (target_type == TypeManager.double_type)
1656 return new DoubleConstant ((double) v);
1657 if (target_type == TypeManager.char_type) {
1658 if (!CheckRange (ec, v, target_type, Char.MaxValue))
1660 return new CharConstant ((char) v);
1662 if (target_type == TypeManager.decimal_type)
1663 return new DecimalConstant ((decimal) v);
1665 if (real_expr is FloatConstant){
1666 float v = ((FloatConstant) real_expr).Value;
1668 if (target_type == TypeManager.byte_type)
1669 return new ByteConstant ((byte) v);
1670 if (target_type == TypeManager.sbyte_type)
1671 return new SByteConstant ((sbyte) v);
1672 if (target_type == TypeManager.short_type)
1673 return new ShortConstant ((short) v);
1674 if (target_type == TypeManager.ushort_type)
1675 return new UShortConstant ((ushort) v);
1676 if (target_type == TypeManager.int32_type)
1677 return new IntConstant ((int) v);
1678 if (target_type == TypeManager.uint32_type)
1679 return new UIntConstant ((uint) v);
1680 if (target_type == TypeManager.int64_type)
1681 return new LongConstant ((long) v);
1682 if (target_type == TypeManager.uint64_type)
1683 return new ULongConstant ((ulong) v);
1684 if (target_type == TypeManager.double_type)
1685 return new DoubleConstant ((double) v);
1686 if (target_type == TypeManager.char_type)
1687 return new CharConstant ((char) v);
1688 if (target_type == TypeManager.decimal_type)
1689 return new DecimalConstant ((decimal) v);
1691 if (real_expr is DoubleConstant){
1692 double v = ((DoubleConstant) real_expr).Value;
1694 if (target_type == TypeManager.byte_type){
1695 return new ByteConstant ((byte) v);
1696 } if (target_type == TypeManager.sbyte_type)
1697 return new SByteConstant ((sbyte) v);
1698 if (target_type == TypeManager.short_type)
1699 return new ShortConstant ((short) v);
1700 if (target_type == TypeManager.ushort_type)
1701 return new UShortConstant ((ushort) v);
1702 if (target_type == TypeManager.int32_type)
1703 return new IntConstant ((int) v);
1704 if (target_type == TypeManager.uint32_type)
1705 return new UIntConstant ((uint) v);
1706 if (target_type == TypeManager.int64_type)
1707 return new LongConstant ((long) v);
1708 if (target_type == TypeManager.uint64_type)
1709 return new ULongConstant ((ulong) v);
1710 if (target_type == TypeManager.float_type)
1711 return new FloatConstant ((float) v);
1712 if (target_type == TypeManager.char_type)
1713 return new CharConstant ((char) v);
1714 if (target_type == TypeManager.decimal_type)
1715 return new DecimalConstant ((decimal) v);
1718 if (real_expr is CharConstant){
1719 char v = ((CharConstant) real_expr).Value;
1721 if (target_type == TypeManager.byte_type) {
1722 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1724 return new ByteConstant ((byte) v);
1726 if (target_type == TypeManager.sbyte_type) {
1727 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1729 return new SByteConstant ((sbyte) v);
1731 if (target_type == TypeManager.short_type) {
1732 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1734 return new ShortConstant ((short) v);
1736 if (target_type == TypeManager.int32_type)
1737 return new IntConstant ((int) v);
1738 if (target_type == TypeManager.uint32_type)
1739 return new UIntConstant ((uint) v);
1740 if (target_type == TypeManager.int64_type)
1741 return new LongConstant ((long) v);
1742 if (target_type == TypeManager.uint64_type)
1743 return new ULongConstant ((ulong) v);
1744 if (target_type == TypeManager.float_type)
1745 return new FloatConstant ((float) v);
1746 if (target_type == TypeManager.double_type)
1747 return new DoubleConstant ((double) v);
1748 if (target_type == TypeManager.char_type) {
1749 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1751 return new CharConstant ((char) v);
1753 if (target_type == TypeManager.decimal_type)
1754 return new DecimalConstant ((decimal) v);
1760 public override Expression DoResolve (EmitContext ec)
1762 expr = expr.Resolve (ec);
1766 type = ec.DeclSpace.ResolveType (target_type, false, Location);
1771 CheckObsoleteAttribute (type);
1773 eclass = ExprClass.Value;
1775 if (expr is Constant){
1776 Expression e = TryReduce (ec, type);
1782 if (type.IsPointer && !ec.InUnsafe) {
1786 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1790 public override void Emit (EmitContext ec)
1793 // This one will never happen
1795 throw new Exception ("Should not happen");
1800 /// Binary operators
1802 public class Binary : Expression {
1803 public enum Operator : byte {
1804 Multiply, Division, Modulus,
1805 Addition, Subtraction,
1806 LeftShift, RightShift,
1807 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1808 Equality, Inequality,
1818 Expression left, right;
1820 // This must be kept in sync with Operator!!!
1821 public static readonly string [] oper_names;
1825 oper_names = new string [(int) Operator.TOP];
1827 oper_names [(int) Operator.Multiply] = "op_Multiply";
1828 oper_names [(int) Operator.Division] = "op_Division";
1829 oper_names [(int) Operator.Modulus] = "op_Modulus";
1830 oper_names [(int) Operator.Addition] = "op_Addition";
1831 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1832 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1833 oper_names [(int) Operator.RightShift] = "op_RightShift";
1834 oper_names [(int) Operator.LessThan] = "op_LessThan";
1835 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1836 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1837 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1838 oper_names [(int) Operator.Equality] = "op_Equality";
1839 oper_names [(int) Operator.Inequality] = "op_Inequality";
1840 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1841 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1842 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1843 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1844 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1847 public Binary (Operator oper, Expression left, Expression right, Location loc)
1855 public Operator Oper {
1864 public Expression Left {
1873 public Expression Right {
1884 /// Returns a stringified representation of the Operator
1886 static string OperName (Operator oper)
1889 case Operator.Multiply:
1891 case Operator.Division:
1893 case Operator.Modulus:
1895 case Operator.Addition:
1897 case Operator.Subtraction:
1899 case Operator.LeftShift:
1901 case Operator.RightShift:
1903 case Operator.LessThan:
1905 case Operator.GreaterThan:
1907 case Operator.LessThanOrEqual:
1909 case Operator.GreaterThanOrEqual:
1911 case Operator.Equality:
1913 case Operator.Inequality:
1915 case Operator.BitwiseAnd:
1917 case Operator.BitwiseOr:
1919 case Operator.ExclusiveOr:
1921 case Operator.LogicalOr:
1923 case Operator.LogicalAnd:
1927 return oper.ToString ();
1930 public override string ToString ()
1932 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1933 right.ToString () + ")";
1936 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1938 if (expr.Type == target_type)
1941 return Convert.ImplicitConversion (ec, expr, target_type, loc);
1944 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
1947 34, loc, "Operator `" + OperName (oper)
1948 + "' is ambiguous on operands of type `"
1949 + TypeManager.CSharpName (l) + "' "
1950 + "and `" + TypeManager.CSharpName (r)
1954 bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions)
1956 if ((l == t) || (r == t))
1959 if (!check_user_conversions)
1962 if (Convert.ImplicitUserConversionExists (ec, l, t))
1964 else if (Convert.ImplicitUserConversionExists (ec, r, t))
1971 // Note that handling the case l == Decimal || r == Decimal
1972 // is taken care of by the Step 1 Operator Overload resolution.
1974 // If `check_user_conv' is true, we also check whether a user-defined conversion
1975 // exists. Note that we only need to do this if both arguments are of a user-defined
1976 // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
1977 // so we don't explicitly check for performance reasons.
1979 bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
1981 if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
1983 // If either operand is of type double, the other operand is
1984 // conveted to type double.
1986 if (r != TypeManager.double_type)
1987 right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
1988 if (l != TypeManager.double_type)
1989 left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
1991 type = TypeManager.double_type;
1992 } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
1994 // if either operand is of type float, the other operand is
1995 // converted to type float.
1997 if (r != TypeManager.double_type)
1998 right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
1999 if (l != TypeManager.double_type)
2000 left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
2001 type = TypeManager.float_type;
2002 } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
2006 // If either operand is of type ulong, the other operand is
2007 // converted to type ulong. or an error ocurrs if the other
2008 // operand is of type sbyte, short, int or long
2010 if (l == TypeManager.uint64_type){
2011 if (r != TypeManager.uint64_type){
2012 if (right is IntConstant){
2013 IntConstant ic = (IntConstant) right;
2015 e = Convert.TryImplicitIntConversion (l, ic);
2018 } else if (right is LongConstant){
2019 long ll = ((LongConstant) right).Value;
2022 right = new ULongConstant ((ulong) ll);
2024 e = Convert.ImplicitNumericConversion (ec, right, l, loc);
2031 if (left is IntConstant){
2032 e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
2035 } else if (left is LongConstant){
2036 long ll = ((LongConstant) left).Value;
2039 left = new ULongConstant ((ulong) ll);
2041 e = Convert.ImplicitNumericConversion (ec, left, r, loc);
2048 if ((other == TypeManager.sbyte_type) ||
2049 (other == TypeManager.short_type) ||
2050 (other == TypeManager.int32_type) ||
2051 (other == TypeManager.int64_type))
2052 Error_OperatorAmbiguous (loc, oper, l, r);
2054 left = ForceConversion (ec, left, TypeManager.uint64_type);
2055 right = ForceConversion (ec, right, TypeManager.uint64_type);
2057 type = TypeManager.uint64_type;
2058 } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
2060 // If either operand is of type long, the other operand is converted
2063 if (l != TypeManager.int64_type)
2064 left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
2065 if (r != TypeManager.int64_type)
2066 right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
2068 type = TypeManager.int64_type;
2069 } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
2071 // If either operand is of type uint, and the other
2072 // operand is of type sbyte, short or int, othe operands are
2073 // converted to type long (unless we have an int constant).
2077 if (l == TypeManager.uint32_type){
2078 if (right is IntConstant){
2079 IntConstant ic = (IntConstant) right;
2083 right = new UIntConstant ((uint) val);
2090 } else if (r == TypeManager.uint32_type){
2091 if (left is IntConstant){
2092 IntConstant ic = (IntConstant) left;
2096 left = new UIntConstant ((uint) val);
2105 if ((other == TypeManager.sbyte_type) ||
2106 (other == TypeManager.short_type) ||
2107 (other == TypeManager.int32_type)){
2108 left = ForceConversion (ec, left, TypeManager.int64_type);
2109 right = ForceConversion (ec, right, TypeManager.int64_type);
2110 type = TypeManager.int64_type;
2113 // if either operand is of type uint, the other
2114 // operand is converd to type uint
2116 left = ForceConversion (ec, left, TypeManager.uint32_type);
2117 right = ForceConversion (ec, right, TypeManager.uint32_type);
2118 type = TypeManager.uint32_type;
2120 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2121 if (l != TypeManager.decimal_type)
2122 left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
2124 if (r != TypeManager.decimal_type)
2125 right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
2126 type = TypeManager.decimal_type;
2128 left = ForceConversion (ec, left, TypeManager.int32_type);
2129 right = ForceConversion (ec, right, TypeManager.int32_type);
2131 type = TypeManager.int32_type;
2134 return (left != null) && (right != null);
2137 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2139 Report.Error (19, loc,
2140 "Operator " + name + " cannot be applied to operands of type `" +
2141 TypeManager.CSharpName (l) + "' and `" +
2142 TypeManager.CSharpName (r) + "'");
2145 void Error_OperatorCannotBeApplied ()
2147 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
2150 static bool is_unsigned (Type t)
2152 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2153 t == TypeManager.short_type || t == TypeManager.byte_type);
2156 static bool is_user_defined (Type t)
2158 if (t.IsSubclassOf (TypeManager.value_type) &&
2159 (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type))
2165 Expression Make32or64 (EmitContext ec, Expression e)
2169 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
2170 t == TypeManager.int64_type || t == TypeManager.uint64_type)
2172 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
2175 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
2178 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
2181 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
2187 Expression CheckShiftArguments (EmitContext ec)
2191 e = ForceConversion (ec, right, TypeManager.int32_type);
2193 Error_OperatorCannotBeApplied ();
2198 if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
2199 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
2200 ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
2201 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
2205 if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
2206 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
2207 right = right.DoResolve (ec);
2209 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
2210 right = right.DoResolve (ec);
2215 Error_OperatorCannotBeApplied ();
2219 Expression ResolveOperator (EmitContext ec)
2222 Type r = right.Type;
2225 // Special cases: string comapred to null
2227 if (oper == Operator.Equality || oper == Operator.Inequality){
2228 if ((l == TypeManager.string_type && (right is NullLiteral)) ||
2229 (r == TypeManager.string_type && (left is NullLiteral))){
2230 Type = TypeManager.bool_type;
2236 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
2237 Type = TypeManager.bool_type;
2244 // Do not perform operator overload resolution when both sides are
2247 if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){
2249 // Step 1: Perform Operator Overload location
2251 Expression left_expr, right_expr;
2253 string op = oper_names [(int) oper];
2255 MethodGroupExpr union;
2256 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
2258 right_expr = MemberLookup (
2259 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
2260 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
2262 union = (MethodGroupExpr) left_expr;
2264 if (union != null) {
2265 ArrayList args = new ArrayList (2);
2266 args.Add (new Argument (left, Argument.AType.Expression));
2267 args.Add (new Argument (right, Argument.AType.Expression));
2269 MethodBase method = Invocation.OverloadResolve (ec, union, args, Location.Null);
2270 if (method != null) {
2271 MethodInfo mi = (MethodInfo) method;
2273 return new BinaryMethod (mi.ReturnType, method, args);
2279 // Step 0: String concatenation (because overloading will get this wrong)
2281 if (oper == Operator.Addition){
2283 // If any of the arguments is a string, cast to string
2286 // Simple constant folding
2287 if (left is StringConstant && right is StringConstant)
2288 return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
2290 if (l == TypeManager.string_type || r == TypeManager.string_type) {
2292 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2293 Error_OperatorCannotBeApplied ();
2297 // try to fold it in on the left
2298 if (left is StringConcat) {
2301 // We have to test here for not-null, since we can be doubly-resolved
2302 // take care of not appending twice
2305 type = TypeManager.string_type;
2306 ((StringConcat) left).Append (ec, right);
2307 return left.Resolve (ec);
2313 // Otherwise, start a new concat expression
2314 return new StringConcat (ec, loc, left, right).Resolve (ec);
2318 // Transform a + ( - b) into a - b
2320 if (right is Unary){
2321 Unary right_unary = (Unary) right;
2323 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2324 oper = Operator.Subtraction;
2325 right = right_unary.Expr;
2331 if (oper == Operator.Equality || oper == Operator.Inequality){
2332 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2333 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2334 Error_OperatorCannotBeApplied ();
2338 type = TypeManager.bool_type;
2343 // operator != (object a, object b)
2344 // operator == (object a, object b)
2346 // For this to be used, both arguments have to be reference-types.
2347 // Read the rationale on the spec (14.9.6)
2349 // Also, if at compile time we know that the classes do not inherit
2350 // one from the other, then we catch the error there.
2352 if (!(l.IsValueType || r.IsValueType)){
2353 type = TypeManager.bool_type;
2358 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
2362 // Also, a standard conversion must exist from either one
2364 if (!(Convert.ImplicitStandardConversionExists (left, r) ||
2365 Convert.ImplicitStandardConversionExists (right, l))){
2366 Error_OperatorCannotBeApplied ();
2370 // We are going to have to convert to an object to compare
2372 if (l != TypeManager.object_type)
2373 left = new EmptyCast (left, TypeManager.object_type);
2374 if (r != TypeManager.object_type)
2375 right = new EmptyCast (right, TypeManager.object_type);
2378 // FIXME: CSC here catches errors cs254 and cs252
2384 // One of them is a valuetype, but the other one is not.
2386 if (!l.IsValueType || !r.IsValueType) {
2387 Error_OperatorCannotBeApplied ();
2392 // Only perform numeric promotions on:
2393 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2395 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2396 if (l.IsSubclassOf (TypeManager.delegate_type)){
2397 if (right.eclass == ExprClass.MethodGroup && RootContext.V2){
2398 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2405 if (r.IsSubclassOf (TypeManager.delegate_type)){
2407 ArrayList args = new ArrayList (2);
2409 args = new ArrayList (2);
2410 args.Add (new Argument (left, Argument.AType.Expression));
2411 args.Add (new Argument (right, Argument.AType.Expression));
2413 if (oper == Operator.Addition)
2414 method = TypeManager.delegate_combine_delegate_delegate;
2416 method = TypeManager.delegate_remove_delegate_delegate;
2419 Error_OperatorCannotBeApplied ();
2423 return new BinaryDelegate (l, method, args);
2428 // Pointer arithmetic:
2430 // T* operator + (T* x, int y);
2431 // T* operator + (T* x, uint y);
2432 // T* operator + (T* x, long y);
2433 // T* operator + (T* x, ulong y);
2435 // T* operator + (int y, T* x);
2436 // T* operator + (uint y, T *x);
2437 // T* operator + (long y, T *x);
2438 // T* operator + (ulong y, T *x);
2440 // T* operator - (T* x, int y);
2441 // T* operator - (T* x, uint y);
2442 // T* operator - (T* x, long y);
2443 // T* operator - (T* x, ulong y);
2445 // long operator - (T* x, T *y)
2448 if (r.IsPointer && oper == Operator.Subtraction){
2450 return new PointerArithmetic (
2451 false, left, right, TypeManager.int64_type,
2454 Expression t = Make32or64 (ec, right);
2456 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc);
2458 } else if (r.IsPointer && oper == Operator.Addition){
2459 Expression t = Make32or64 (ec, left);
2461 return new PointerArithmetic (true, right, t, r, loc);
2466 // Enumeration operators
2468 bool lie = TypeManager.IsEnumType (l);
2469 bool rie = TypeManager.IsEnumType (r);
2473 // U operator - (E e, E f)
2475 if (oper == Operator.Subtraction){
2477 type = TypeManager.EnumToUnderlying (l);
2480 Error_OperatorCannotBeApplied ();
2486 // operator + (E e, U x)
2487 // operator - (E e, U x)
2489 if (oper == Operator.Addition || oper == Operator.Subtraction){
2490 Type enum_type = lie ? l : r;
2491 Type other_type = lie ? r : l;
2492 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2494 if (underlying_type != other_type){
2495 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2505 Error_OperatorCannotBeApplied ();
2514 temp = Convert.ImplicitConversion (ec, right, l, loc);
2518 Error_OperatorCannotBeApplied ();
2522 temp = Convert.ImplicitConversion (ec, left, r, loc);
2527 Error_OperatorCannotBeApplied ();
2532 if (oper == Operator.Equality || oper == Operator.Inequality ||
2533 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2534 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2535 if (left.Type != right.Type){
2536 Error_OperatorCannotBeApplied ();
2539 type = TypeManager.bool_type;
2543 if (oper == Operator.BitwiseAnd ||
2544 oper == Operator.BitwiseOr ||
2545 oper == Operator.ExclusiveOr){
2549 Error_OperatorCannotBeApplied ();
2553 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2554 return CheckShiftArguments (ec);
2556 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2557 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2558 type = TypeManager.bool_type;
2563 Error_OperatorCannotBeApplied ();
2567 Expression e = new ConditionalLogicalOperator (
2568 oper == Operator.LogicalAnd, left, right, l, loc);
2569 return e.Resolve (ec);
2573 // operator & (bool x, bool y)
2574 // operator | (bool x, bool y)
2575 // operator ^ (bool x, bool y)
2577 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2578 if (oper == Operator.BitwiseAnd ||
2579 oper == Operator.BitwiseOr ||
2580 oper == Operator.ExclusiveOr){
2587 // Pointer comparison
2589 if (l.IsPointer && r.IsPointer){
2590 if (oper == Operator.Equality || oper == Operator.Inequality ||
2591 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2592 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2593 type = TypeManager.bool_type;
2599 // This will leave left or right set to null if there is an error
2601 bool check_user_conv = is_user_defined (l) && is_user_defined (r);
2602 DoNumericPromotions (ec, l, r, check_user_conv);
2603 if (left == null || right == null){
2604 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2609 // reload our cached types if required
2614 if (oper == Operator.BitwiseAnd ||
2615 oper == Operator.BitwiseOr ||
2616 oper == Operator.ExclusiveOr){
2618 if (((l == TypeManager.int32_type) ||
2619 (l == TypeManager.uint32_type) ||
2620 (l == TypeManager.short_type) ||
2621 (l == TypeManager.ushort_type) ||
2622 (l == TypeManager.int64_type) ||
2623 (l == TypeManager.uint64_type))){
2626 Error_OperatorCannotBeApplied ();
2630 Error_OperatorCannotBeApplied ();
2635 if (oper == Operator.Equality ||
2636 oper == Operator.Inequality ||
2637 oper == Operator.LessThanOrEqual ||
2638 oper == Operator.LessThan ||
2639 oper == Operator.GreaterThanOrEqual ||
2640 oper == Operator.GreaterThan){
2641 type = TypeManager.bool_type;
2647 public override Expression DoResolve (EmitContext ec)
2649 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2650 left = ((ParenthesizedExpression) left).Expr;
2651 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2655 if (left.eclass == ExprClass.Type) {
2656 Error (75, "Casting a negative value needs to have the value in parentheses.");
2660 left = left.Resolve (ec);
2661 right = right.Resolve (ec);
2663 if (left == null || right == null)
2666 eclass = ExprClass.Value;
2668 Constant rc = right as Constant;
2669 Constant lc = left as Constant;
2671 if (rc != null & lc != null){
2672 Expression e = ConstantFold.BinaryFold (
2673 ec, oper, lc, rc, loc);
2678 return ResolveOperator (ec);
2682 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2683 /// context of a conditional bool expression. This function will return
2684 /// false if it is was possible to use EmitBranchable, or true if it was.
2686 /// The expression's code is generated, and we will generate a branch to `target'
2687 /// if the resulting expression value is equal to isTrue
2689 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
2691 ILGenerator ig = ec.ig;
2694 // This is more complicated than it looks, but its just to avoid
2695 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2696 // but on top of that we want for == and != to use a special path
2697 // if we are comparing against null
2699 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2700 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2703 // put the constant on the rhs, for simplicity
2705 if (left is Constant) {
2706 Expression swap = right;
2711 if (((Constant) right).IsZeroInteger) {
2714 ig.Emit (OpCodes.Brtrue, target);
2716 ig.Emit (OpCodes.Brfalse, target);
2719 } else if (right is BoolConstant) {
2721 if (my_on_true != ((BoolConstant) right).Value)
2722 ig.Emit (OpCodes.Brtrue, target);
2724 ig.Emit (OpCodes.Brfalse, target);
2729 } else if (oper == Operator.LogicalAnd) {
2732 Label tests_end = ig.DefineLabel ();
2734 left.EmitBranchable (ec, tests_end, false);
2735 right.EmitBranchable (ec, target, true);
2736 ig.MarkLabel (tests_end);
2738 left.EmitBranchable (ec, target, false);
2739 right.EmitBranchable (ec, target, false);
2744 } else if (oper == Operator.LogicalOr){
2746 left.EmitBranchable (ec, target, true);
2747 right.EmitBranchable (ec, target, true);
2750 Label tests_end = ig.DefineLabel ();
2751 left.EmitBranchable (ec, tests_end, true);
2752 right.EmitBranchable (ec, target, false);
2753 ig.MarkLabel (tests_end);
2758 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2759 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2760 oper == Operator.Equality || oper == Operator.Inequality)) {
2761 base.EmitBranchable (ec, target, onTrue);
2769 bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
2772 case Operator.Equality:
2774 ig.Emit (OpCodes.Beq, target);
2776 ig.Emit (OpCodes.Bne_Un, target);
2779 case Operator.Inequality:
2781 ig.Emit (OpCodes.Bne_Un, target);
2783 ig.Emit (OpCodes.Beq, target);
2786 case Operator.LessThan:
2789 ig.Emit (OpCodes.Blt_Un, target);
2791 ig.Emit (OpCodes.Blt, target);
2794 ig.Emit (OpCodes.Bge_Un, target);
2796 ig.Emit (OpCodes.Bge, target);
2799 case Operator.GreaterThan:
2802 ig.Emit (OpCodes.Bgt_Un, target);
2804 ig.Emit (OpCodes.Bgt, target);
2807 ig.Emit (OpCodes.Ble_Un, target);
2809 ig.Emit (OpCodes.Ble, target);
2812 case Operator.LessThanOrEqual:
2815 ig.Emit (OpCodes.Ble_Un, target);
2817 ig.Emit (OpCodes.Ble, target);
2820 ig.Emit (OpCodes.Bgt_Un, target);
2822 ig.Emit (OpCodes.Bgt, target);
2826 case Operator.GreaterThanOrEqual:
2829 ig.Emit (OpCodes.Bge_Un, target);
2831 ig.Emit (OpCodes.Bge, target);
2834 ig.Emit (OpCodes.Blt_Un, target);
2836 ig.Emit (OpCodes.Blt, target);
2839 Console.WriteLine (oper);
2840 throw new Exception ("what is THAT");
2844 public override void Emit (EmitContext ec)
2846 ILGenerator ig = ec.ig;
2851 // Handle short-circuit operators differently
2854 if (oper == Operator.LogicalAnd) {
2855 Label load_zero = ig.DefineLabel ();
2856 Label end = ig.DefineLabel ();
2858 left.EmitBranchable (ec, load_zero, false);
2860 ig.Emit (OpCodes.Br, end);
2862 ig.MarkLabel (load_zero);
2863 ig.Emit (OpCodes.Ldc_I4_0);
2866 } else if (oper == Operator.LogicalOr) {
2867 Label load_one = ig.DefineLabel ();
2868 Label end = ig.DefineLabel ();
2870 left.EmitBranchable (ec, load_one, true);
2872 ig.Emit (OpCodes.Br, end);
2874 ig.MarkLabel (load_one);
2875 ig.Emit (OpCodes.Ldc_I4_1);
2883 bool isUnsigned = is_unsigned (left.Type);
2886 case Operator.Multiply:
2888 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2889 opcode = OpCodes.Mul_Ovf;
2890 else if (isUnsigned)
2891 opcode = OpCodes.Mul_Ovf_Un;
2893 opcode = OpCodes.Mul;
2895 opcode = OpCodes.Mul;
2899 case Operator.Division:
2901 opcode = OpCodes.Div_Un;
2903 opcode = OpCodes.Div;
2906 case Operator.Modulus:
2908 opcode = OpCodes.Rem_Un;
2910 opcode = OpCodes.Rem;
2913 case Operator.Addition:
2915 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2916 opcode = OpCodes.Add_Ovf;
2917 else if (isUnsigned)
2918 opcode = OpCodes.Add_Ovf_Un;
2920 opcode = OpCodes.Add;
2922 opcode = OpCodes.Add;
2925 case Operator.Subtraction:
2927 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2928 opcode = OpCodes.Sub_Ovf;
2929 else if (isUnsigned)
2930 opcode = OpCodes.Sub_Ovf_Un;
2932 opcode = OpCodes.Sub;
2934 opcode = OpCodes.Sub;
2937 case Operator.RightShift:
2939 opcode = OpCodes.Shr_Un;
2941 opcode = OpCodes.Shr;
2944 case Operator.LeftShift:
2945 opcode = OpCodes.Shl;
2948 case Operator.Equality:
2949 opcode = OpCodes.Ceq;
2952 case Operator.Inequality:
2953 ig.Emit (OpCodes.Ceq);
2954 ig.Emit (OpCodes.Ldc_I4_0);
2956 opcode = OpCodes.Ceq;
2959 case Operator.LessThan:
2961 opcode = OpCodes.Clt_Un;
2963 opcode = OpCodes.Clt;
2966 case Operator.GreaterThan:
2968 opcode = OpCodes.Cgt_Un;
2970 opcode = OpCodes.Cgt;
2973 case Operator.LessThanOrEqual:
2974 Type lt = left.Type;
2976 if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
2977 ig.Emit (OpCodes.Cgt_Un);
2979 ig.Emit (OpCodes.Cgt);
2980 ig.Emit (OpCodes.Ldc_I4_0);
2982 opcode = OpCodes.Ceq;
2985 case Operator.GreaterThanOrEqual:
2986 Type le = left.Type;
2988 if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
2989 ig.Emit (OpCodes.Clt_Un);
2991 ig.Emit (OpCodes.Clt);
2993 ig.Emit (OpCodes.Ldc_I4_0);
2995 opcode = OpCodes.Ceq;
2998 case Operator.BitwiseOr:
2999 opcode = OpCodes.Or;
3002 case Operator.BitwiseAnd:
3003 opcode = OpCodes.And;
3006 case Operator.ExclusiveOr:
3007 opcode = OpCodes.Xor;
3011 throw new Exception ("This should not happen: Operator = "
3012 + oper.ToString ());
3020 // Object created by Binary when the binary operator uses an method instead of being
3021 // a binary operation that maps to a CIL binary operation.
3023 public class BinaryMethod : Expression {
3024 public MethodBase method;
3025 public ArrayList Arguments;
3027 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3032 eclass = ExprClass.Value;
3035 public override Expression DoResolve (EmitContext ec)
3040 public override void Emit (EmitContext ec)
3042 ILGenerator ig = ec.ig;
3044 if (Arguments != null)
3045 Invocation.EmitArguments (ec, method, Arguments, false, null);
3047 if (method is MethodInfo)
3048 ig.Emit (OpCodes.Call, (MethodInfo) method);
3050 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3055 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3056 // b, c, d... may be strings or objects.
3058 public class StringConcat : Expression {
3060 bool invalid = false;
3063 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3066 type = TypeManager.string_type;
3067 eclass = ExprClass.Value;
3069 operands = new ArrayList (2);
3074 public override Expression DoResolve (EmitContext ec)
3082 public void Append (EmitContext ec, Expression operand)
3087 if (operand is StringConstant && operands.Count != 0) {
3088 StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
3089 if (last_operand != null) {
3090 operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
3096 // Conversion to object
3098 if (operand.Type != TypeManager.string_type) {
3099 Expression no = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3102 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3108 operands.Add (operand);
3111 public override void Emit (EmitContext ec)
3113 MethodInfo concat_method = null;
3116 // Are we also concating objects?
3118 bool is_strings_only = true;
3121 // Do conversion to arguments; check for strings only
3123 for (int i = 0; i < operands.Count; i ++) {
3124 Expression e = (Expression) operands [i];
3125 is_strings_only &= e.Type == TypeManager.string_type;
3128 for (int i = 0; i < operands.Count; i ++) {
3129 Expression e = (Expression) operands [i];
3131 if (! is_strings_only && e.Type == TypeManager.string_type) {
3132 // need to make sure this is an object, because the EmitParams
3133 // method might look at the type of this expression, see it is a
3134 // string and emit a string [] when we want an object [];
3136 e = Convert.ImplicitConversion (ec, e, TypeManager.object_type, loc);
3138 operands [i] = new Argument (e, Argument.AType.Expression);
3142 // Find the right method
3144 switch (operands.Count) {
3147 // This should not be possible, because simple constant folding
3148 // is taken care of in the Binary code.
3150 throw new Exception ("how did you get here?");
3153 concat_method = is_strings_only ?
3154 TypeManager.string_concat_string_string :
3155 TypeManager.string_concat_object_object ;
3158 concat_method = is_strings_only ?
3159 TypeManager.string_concat_string_string_string :
3160 TypeManager.string_concat_object_object_object ;
3164 // There is not a 4 param overlaod for object (the one that there is
3165 // is actually a varargs methods, and is only in corlib because it was
3166 // introduced there before.).
3168 if (!is_strings_only)
3171 concat_method = TypeManager.string_concat_string_string_string_string;
3174 concat_method = is_strings_only ?
3175 TypeManager.string_concat_string_dot_dot_dot :
3176 TypeManager.string_concat_object_dot_dot_dot ;
3180 Invocation.EmitArguments (ec, concat_method, operands, false, null);
3181 ec.ig.Emit (OpCodes.Call, concat_method);
3186 // Object created with +/= on delegates
3188 public class BinaryDelegate : Expression {
3192 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3197 eclass = ExprClass.Value;
3200 public override Expression DoResolve (EmitContext ec)
3205 public override void Emit (EmitContext ec)
3207 ILGenerator ig = ec.ig;
3209 Invocation.EmitArguments (ec, method, args, false, null);
3211 ig.Emit (OpCodes.Call, (MethodInfo) method);
3212 ig.Emit (OpCodes.Castclass, type);
3215 public Expression Right {
3217 Argument arg = (Argument) args [1];
3222 public bool IsAddition {
3224 return method == TypeManager.delegate_combine_delegate_delegate;
3230 // User-defined conditional logical operator
3231 public class ConditionalLogicalOperator : Expression {
3232 Expression left, right;
3235 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3238 eclass = ExprClass.Value;
3242 this.is_and = is_and;
3245 protected void Error19 ()
3247 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
3250 protected void Error218 ()
3252 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3253 "declarations of operator true and operator false");
3256 Expression op_true, op_false, op;
3257 LocalTemporary left_temp;
3259 public override Expression DoResolve (EmitContext ec)
3262 Expression operator_group;
3264 operator_group = MethodLookup (ec, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc);
3265 if (operator_group == null) {
3270 left_temp = new LocalTemporary (ec, type);
3272 ArrayList arguments = new ArrayList ();
3273 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3274 arguments.Add (new Argument (right, Argument.AType.Expression));
3275 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc) as MethodInfo;
3276 if ((method == null) || (method.ReturnType != type)) {
3281 op = new StaticCallExpr (method, arguments, loc);
3283 op_true = GetOperatorTrue (ec, left_temp, loc);
3284 op_false = GetOperatorFalse (ec, left_temp, loc);
3285 if ((op_true == null) || (op_false == null)) {
3293 public override void Emit (EmitContext ec)
3295 ILGenerator ig = ec.ig;
3296 Label false_target = ig.DefineLabel ();
3297 Label end_target = ig.DefineLabel ();
3299 ig.Emit (OpCodes.Nop);
3302 left_temp.Store (ec);
3304 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3305 left_temp.Emit (ec);
3306 ig.Emit (OpCodes.Br, end_target);
3307 ig.MarkLabel (false_target);
3309 ig.MarkLabel (end_target);
3311 ig.Emit (OpCodes.Nop);
3315 public class PointerArithmetic : Expression {
3316 Expression left, right;
3320 // We assume that `l' is always a pointer
3322 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3325 eclass = ExprClass.Variable;
3329 is_add = is_addition;
3332 public override Expression DoResolve (EmitContext ec)
3335 // We are born fully resolved
3340 public override void Emit (EmitContext ec)
3342 Type op_type = left.Type;
3343 ILGenerator ig = ec.ig;
3344 Type element = TypeManager.GetElementType (op_type);
3345 int size = GetTypeSize (element);
3346 Type rtype = right.Type;
3348 if (rtype.IsPointer){
3350 // handle (pointer - pointer)
3354 ig.Emit (OpCodes.Sub);
3358 ig.Emit (OpCodes.Sizeof, element);
3360 IntLiteral.EmitInt (ig, size);
3361 ig.Emit (OpCodes.Div);
3363 ig.Emit (OpCodes.Conv_I8);
3366 // handle + and - on (pointer op int)
3369 ig.Emit (OpCodes.Conv_I);
3373 ig.Emit (OpCodes.Sizeof, element);
3375 IntLiteral.EmitInt (ig, size);
3376 if (rtype == TypeManager.int64_type)
3377 ig.Emit (OpCodes.Conv_I8);
3378 else if (rtype == TypeManager.uint64_type)
3379 ig.Emit (OpCodes.Conv_U8);
3380 ig.Emit (OpCodes.Mul);
3381 ig.Emit (OpCodes.Conv_I);
3384 ig.Emit (OpCodes.Add);
3386 ig.Emit (OpCodes.Sub);
3392 /// Implements the ternary conditional operator (?:)
3394 public class Conditional : Expression {
3395 Expression expr, trueExpr, falseExpr;
3397 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3400 this.trueExpr = trueExpr;
3401 this.falseExpr = falseExpr;
3405 public Expression Expr {
3411 public Expression TrueExpr {
3417 public Expression FalseExpr {
3423 public override Expression DoResolve (EmitContext ec)
3425 expr = expr.Resolve (ec);
3430 if (expr.Type != TypeManager.bool_type){
3431 expr = Expression.ResolveBoolean (
3438 trueExpr = trueExpr.Resolve (ec);
3439 falseExpr = falseExpr.Resolve (ec);
3441 if (trueExpr == null || falseExpr == null)
3444 eclass = ExprClass.Value;
3445 if (trueExpr.Type == falseExpr.Type)
3446 type = trueExpr.Type;
3449 Type true_type = trueExpr.Type;
3450 Type false_type = falseExpr.Type;
3452 if (trueExpr is NullLiteral){
3455 } else if (falseExpr is NullLiteral){
3461 // First, if an implicit conversion exists from trueExpr
3462 // to falseExpr, then the result type is of type falseExpr.Type
3464 conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
3467 // Check if both can convert implicitl to each other's type
3469 if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
3471 "Can not compute type of conditional expression " +
3472 "as `" + TypeManager.CSharpName (trueExpr.Type) +
3473 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
3474 "' convert implicitly to each other");
3479 } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
3483 Error (173, "The type of the conditional expression can " +
3484 "not be computed because there is no implicit conversion" +
3485 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3486 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3491 if (expr is BoolConstant){
3492 BoolConstant bc = (BoolConstant) expr;
3503 public override void Emit (EmitContext ec)
3505 ILGenerator ig = ec.ig;
3506 Label false_target = ig.DefineLabel ();
3507 Label end_target = ig.DefineLabel ();
3509 expr.EmitBranchable (ec, false_target, false);
3511 ig.Emit (OpCodes.Br, end_target);
3512 ig.MarkLabel (false_target);
3513 falseExpr.Emit (ec);
3514 ig.MarkLabel (end_target);
3522 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3523 public readonly string Name;
3524 public readonly Block Block;
3525 LocalInfo local_info;
3528 public LocalVariableReference (Block block, string name, Location l)
3533 eclass = ExprClass.Variable;
3536 // Setting `is_readonly' to false will allow you to create a writable
3537 // reference to a read-only variable. This is used by foreach and using.
3538 public LocalVariableReference (Block block, string name, Location l,
3539 LocalInfo local_info, bool is_readonly)
3540 : this (block, name, l)
3542 this.local_info = local_info;
3543 this.is_readonly = is_readonly;
3546 public VariableInfo VariableInfo {
3547 get { return local_info.VariableInfo; }
3550 public bool IsReadOnly {
3556 protected void DoResolveBase (EmitContext ec)
3558 if (local_info == null) {
3559 local_info = Block.GetLocalInfo (Name);
3560 is_readonly = local_info.ReadOnly;
3563 type = local_info.VariableType;
3565 if (ec.InAnonymousMethod)
3566 Block.LiftVariable (local_info);
3570 protected Expression DoResolve (EmitContext ec, bool is_lvalue)
3572 Expression e = Block.GetConstantExpression (Name);
3574 local_info.Used = true;
3575 eclass = ExprClass.Value;
3576 return e.Resolve (ec);
3579 VariableInfo variable_info = local_info.VariableInfo;
3580 if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
3584 local_info.Used = true;
3586 if (local_info.LocalBuilder == null)
3587 return ec.RemapLocal (local_info);
3592 public override Expression DoResolve (EmitContext ec)
3596 return DoResolve (ec, false);
3599 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3603 VariableInfo variable_info = local_info.VariableInfo;
3604 if (variable_info != null)
3605 variable_info.SetAssigned (ec);
3607 Expression e = DoResolve (ec, true);
3613 Error (1604, "cannot assign to `" + Name + "' because it is readonly");
3617 CheckObsoleteAttribute (e.Type);
3619 if (local_info.LocalBuilder == null)
3620 return ec.RemapLocalLValue (local_info, right_side);
3625 public bool VerifyFixed (bool is_expression)
3627 return !is_expression || local_info.IsFixed;
3630 public override void Emit (EmitContext ec)
3632 ILGenerator ig = ec.ig;
3634 ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
3637 public void Emit (EmitContext ec, bool leave_copy)
3641 ec.ig.Emit (OpCodes.Dup);
3644 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3648 ec.ig.Emit (OpCodes.Dup);
3649 ec.ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
3652 public void AddressOf (EmitContext ec, AddressOp mode)
3654 ILGenerator ig = ec.ig;
3656 ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
3659 public override string ToString ()
3661 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3666 /// This represents a reference to a parameter in the intermediate
3669 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3675 public Parameter.Modifier mod;
3676 public bool is_ref, is_out, prepared;
3677 LocalTemporary temp;
3679 public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
3686 eclass = ExprClass.Variable;
3689 public VariableInfo VariableInfo {
3693 public bool VerifyFixed (bool is_expression)
3695 return !is_expression || TypeManager.IsValueType (type);
3698 public bool IsAssigned (EmitContext ec, Location loc)
3700 if (!ec.DoFlowAnalysis || !is_out ||
3701 ec.CurrentBranching.IsAssigned (vi))
3704 Report.Error (165, loc,
3705 "Use of unassigned parameter `" + name + "'");
3709 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3711 if (!ec.DoFlowAnalysis || !is_out ||
3712 ec.CurrentBranching.IsFieldAssigned (vi, field_name))
3715 Report.Error (170, loc,
3716 "Use of possibly unassigned field `" + field_name + "'");
3720 public void SetAssigned (EmitContext ec)
3722 if (is_out && ec.DoFlowAnalysis)
3723 ec.CurrentBranching.SetAssigned (vi);
3726 public void SetFieldAssigned (EmitContext ec, string field_name)
3728 if (is_out && ec.DoFlowAnalysis)
3729 ec.CurrentBranching.SetFieldAssigned (vi, field_name);
3732 protected void DoResolveBase (EmitContext ec)
3734 type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
3735 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3736 is_out = (mod & Parameter.Modifier.OUT) != 0;
3737 eclass = ExprClass.Variable;
3740 vi = block.ParameterMap [idx];
3744 // Notice that for ref/out parameters, the type exposed is not the
3745 // same type exposed externally.
3748 // externally we expose "int&"
3749 // here we expose "int".
3751 // We record this in "is_ref". This means that the type system can treat
3752 // the type as it is expected, but when we generate the code, we generate
3753 // the alternate kind of code.
3755 public override Expression DoResolve (EmitContext ec)
3759 if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
3762 if (ec.RemapToProxy)
3763 return ec.RemapParameter (idx);
3768 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3774 if (ec.RemapToProxy)
3775 return ec.RemapParameterLValue (idx, right_side);
3780 static public void EmitLdArg (ILGenerator ig, int x)
3784 case 0: ig.Emit (OpCodes.Ldarg_0); break;
3785 case 1: ig.Emit (OpCodes.Ldarg_1); break;
3786 case 2: ig.Emit (OpCodes.Ldarg_2); break;
3787 case 3: ig.Emit (OpCodes.Ldarg_3); break;
3788 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
3791 ig.Emit (OpCodes.Ldarg, x);
3795 // This method is used by parameters that are references, that are
3796 // being passed as references: we only want to pass the pointer (that
3797 // is already stored in the parameter, not the address of the pointer,
3798 // and not the value of the variable).
3800 public void EmitLoad (EmitContext ec)
3802 ILGenerator ig = ec.ig;
3808 EmitLdArg (ig, arg_idx);
3811 public override void Emit (EmitContext ec)
3816 public void Emit (EmitContext ec, bool leave_copy)
3818 ILGenerator ig = ec.ig;
3825 EmitLdArg (ig, arg_idx);
3829 ec.ig.Emit (OpCodes.Dup);
3832 // If we are a reference, we loaded on the stack a pointer
3833 // Now lets load the real value
3835 LoadFromPtr (ig, type);
3839 ec.ig.Emit (OpCodes.Dup);
3842 temp = new LocalTemporary (ec, type);
3848 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3850 ILGenerator ig = ec.ig;
3853 prepared = prepare_for_load;
3858 if (is_ref && !prepared)
3859 EmitLdArg (ig, arg_idx);
3864 ec.ig.Emit (OpCodes.Dup);
3868 temp = new LocalTemporary (ec, type);
3872 StoreFromPtr (ig, type);
3878 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3880 ig.Emit (OpCodes.Starg, arg_idx);
3884 public void AddressOf (EmitContext ec, AddressOp mode)
3893 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3895 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3898 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3900 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3907 /// Used for arguments to New(), Invocation()
3909 public class Argument {
3910 public enum AType : byte {
3917 public readonly AType ArgType;
3918 public Expression Expr;
3920 public Argument (Expression expr, AType type)
3923 this.ArgType = type;
3926 public Argument (Expression expr)
3929 this.ArgType = AType.Expression;
3934 if (ArgType == AType.Ref || ArgType == AType.Out)
3935 return TypeManager.GetReferenceType (Expr.Type);
3941 public Parameter.Modifier GetParameterModifier ()
3945 return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
3948 return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
3951 return Parameter.Modifier.NONE;
3955 public static string FullDesc (Argument a)
3957 if (a.ArgType == AType.ArgList)
3960 return (a.ArgType == AType.Ref ? "ref " :
3961 (a.ArgType == AType.Out ? "out " : "")) +
3962 TypeManager.CSharpName (a.Expr.Type);
3965 public bool ResolveMethodGroup (EmitContext ec, Location loc)
3967 // FIXME: csc doesn't report any error if you try to use `ref' or
3968 // `out' in a delegate creation expression.
3969 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
3976 public bool Resolve (EmitContext ec, Location loc)
3978 if (ArgType == AType.Ref) {
3979 Expr = Expr.Resolve (ec);
3983 Expr = Expr.ResolveLValue (ec, Expr);
3984 } else if (ArgType == AType.Out)
3985 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
3987 Expr = Expr.Resolve (ec);
3992 if (ArgType == AType.Expression)
3996 // Catch errors where fields of a MarshalByRefObject are passed as ref or out
3997 // This is only allowed for `this'
3999 FieldExpr fe = Expr as FieldExpr;
4000 if (fe != null && !fe.IsStatic){
4001 Expression instance = fe.InstanceExpression;
4003 if (instance.GetType () != typeof (This)){
4004 if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
4005 Report.Error (197, loc,
4006 "Can not pass a type that derives from MarshalByRefObject with out or ref");
4013 if (Expr.eclass != ExprClass.Variable){
4015 // We just probe to match the CSC output
4017 if (Expr.eclass == ExprClass.PropertyAccess ||
4018 Expr.eclass == ExprClass.IndexerAccess){
4021 "A property or indexer can not be passed as an out or ref " +
4026 "An lvalue is required as an argument to out or ref");
4034 public void Emit (EmitContext ec)
4037 // Ref and Out parameters need to have their addresses taken.
4039 // ParameterReferences might already be references, so we want
4040 // to pass just the value
4042 if (ArgType == AType.Ref || ArgType == AType.Out){
4043 AddressOp mode = AddressOp.Store;
4045 if (ArgType == AType.Ref)
4046 mode |= AddressOp.Load;
4048 if (Expr is ParameterReference){
4049 ParameterReference pr = (ParameterReference) Expr;
4055 pr.AddressOf (ec, mode);
4058 ((IMemoryLocation)Expr).AddressOf (ec, mode);
4066 /// Invocation of methods or delegates.
4068 public class Invocation : ExpressionStatement {
4069 public readonly ArrayList Arguments;
4072 MethodBase method = null;
4075 static Hashtable method_parameter_cache;
4077 static Invocation ()
4079 method_parameter_cache = new PtrHashtable ();
4083 // arguments is an ArrayList, but we do not want to typecast,
4084 // as it might be null.
4086 // FIXME: only allow expr to be a method invocation or a
4087 // delegate invocation (7.5.5)
4089 public Invocation (Expression expr, ArrayList arguments, Location l)
4092 Arguments = arguments;
4096 public Expression Expr {
4103 /// Returns the Parameters (a ParameterData interface) for the
4106 public static ParameterData GetParameterData (MethodBase mb)
4108 object pd = method_parameter_cache [mb];
4112 return (ParameterData) pd;
4115 ip = TypeManager.LookupParametersByBuilder (mb);
4117 method_parameter_cache [mb] = ip;
4119 return (ParameterData) ip;
4121 ReflectionParameters rp = new ReflectionParameters (mb);
4122 method_parameter_cache [mb] = rp;
4124 return (ParameterData) rp;
4129 /// Determines "better conversion" as specified in 7.4.2.3
4131 /// Returns : 1 if a->p is better
4132 /// 0 if a->q or neither is better
4134 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
4136 Type argument_type = a.Type;
4137 Expression argument_expr = a.Expr;
4139 if (argument_type == null)
4140 throw new Exception ("Expression of type " + a.Expr +
4141 " does not resolve its type");
4144 // This is a special case since csc behaves this way.
4146 if (argument_expr is NullLiteral &&
4147 p == TypeManager.string_type &&
4148 q == TypeManager.object_type)
4150 else if (argument_expr is NullLiteral &&
4151 p == TypeManager.object_type &&
4152 q == TypeManager.string_type)
4156 // csc behaves this way so we emulate it. Basically, if the argument
4157 // is null and one of the types to compare is 'object' and the other
4158 // is a reference type, we prefer the other.
4160 // I can't find this anywhere in the spec but we can interpret this
4161 // to mean that null can be of any type you wish in such a context
4163 if (p != null && q != null) {
4164 if (argument_expr is NullLiteral &&
4166 q == TypeManager.object_type)
4168 else if (argument_expr is NullLiteral &&
4170 p == TypeManager.object_type)
4177 if (argument_type == p)
4180 if (argument_type == q)
4184 Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
4192 Expression p_tmp = new EmptyExpression (p);
4193 Expression q_tmp = new EmptyExpression (q);
4195 if (Convert.ImplicitConversionExists (ec, p_tmp, q) == true &&
4196 Convert.ImplicitConversionExists (ec, q_tmp, p) == false)
4199 if (p == TypeManager.sbyte_type)
4200 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
4201 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4204 if (p == TypeManager.short_type)
4205 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
4206 q == TypeManager.uint64_type)
4209 if (p == TypeManager.int32_type)
4210 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4213 if (p == TypeManager.int64_type)
4214 if (q == TypeManager.uint64_type)
4221 /// Determines "Better function" between candidate
4222 /// and the current best match
4225 /// Returns an integer indicating :
4226 /// 0 if candidate ain't better
4227 /// 1 if candidate is better than the current best match
4229 static int BetterFunction (EmitContext ec, ArrayList args,
4230 MethodBase candidate, bool candidate_params,
4231 MethodBase best, bool best_params,
4234 ParameterData candidate_pd = GetParameterData (candidate);
4235 ParameterData best_pd;
4241 argument_count = args.Count;
4243 int cand_count = candidate_pd.Count;
4246 // If there is no best method, than this one
4247 // is better, however, if we already found a
4248 // best method, we cant tell. This happens
4259 // interface IFooBar : IFoo, IBar {}
4261 // We cant tell if IFoo.DoIt is better than IBar.DoIt
4263 // However, we have to consider that
4264 // Trim (); is better than Trim (params char[] chars);
4266 if (cand_count == 0 && argument_count == 0)
4267 return best == null || best_params ? 1 : 0;
4269 if ((candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS) &&
4270 (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.ARGLIST))
4271 if (cand_count != argument_count)
4277 if (argument_count == 0 && cand_count == 1 &&
4278 candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
4281 for (int j = 0; j < argument_count; ++j) {
4283 Argument a = (Argument) args [j];
4284 Type t = candidate_pd.ParameterType (j);
4286 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4287 if (candidate_params)
4288 t = TypeManager.GetElementType (t);
4290 x = BetterConversion (ec, a, t, null, loc);
4302 best_pd = GetParameterData (best);
4304 int rating1 = 0, rating2 = 0;
4306 for (int j = 0; j < argument_count; ++j) {
4309 Argument a = (Argument) args [j];
4311 Type ct = candidate_pd.ParameterType (j);
4312 Type bt = best_pd.ParameterType (j);
4314 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4315 if (candidate_params)
4316 ct = TypeManager.GetElementType (ct);
4318 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4320 bt = TypeManager.GetElementType (bt);
4322 x = BetterConversion (ec, a, ct, bt, loc);
4323 y = BetterConversion (ec, a, bt, ct, loc);
4333 // If a method (in the normal form) with the
4334 // same signature as the expanded form of the
4335 // current best params method already exists,
4336 // the expanded form is not applicable so we
4337 // force it to select the candidate
4339 if (!candidate_params && best_params && cand_count == argument_count)
4342 if (rating1 > rating2)
4348 public static string FullMethodDesc (MethodBase mb)
4350 string ret_type = "";
4355 if (mb is MethodInfo)
4356 ret_type = TypeManager.CSharpName (((MethodInfo) mb).ReturnType);
4358 StringBuilder sb = new StringBuilder (ret_type);
4360 sb.Append (mb.ReflectedType.ToString ());
4362 sb.Append (mb.Name);
4364 ParameterData pd = GetParameterData (mb);
4366 int count = pd.Count;
4369 for (int i = count; i > 0; ) {
4372 sb.Append (pd.ParameterDesc (count - i - 1));
4378 return sb.ToString ();
4381 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
4383 MemberInfo [] miset;
4384 MethodGroupExpr union;
4389 return (MethodGroupExpr) mg2;
4392 return (MethodGroupExpr) mg1;
4395 MethodGroupExpr left_set = null, right_set = null;
4396 int length1 = 0, length2 = 0;
4398 left_set = (MethodGroupExpr) mg1;
4399 length1 = left_set.Methods.Length;
4401 right_set = (MethodGroupExpr) mg2;
4402 length2 = right_set.Methods.Length;
4404 ArrayList common = new ArrayList ();
4406 foreach (MethodBase r in right_set.Methods){
4407 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
4411 miset = new MemberInfo [length1 + length2 - common.Count];
4412 left_set.Methods.CopyTo (miset, 0);
4416 foreach (MethodBase r in right_set.Methods) {
4417 if (!common.Contains (r))
4421 union = new MethodGroupExpr (miset, loc);
4427 /// Determines if the candidate method, if a params method, is applicable
4428 /// in its expanded form to the given set of arguments
4430 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments,
4431 MethodBase candidate, bool do_varargs)
4435 if (arguments == null)
4438 arg_count = arguments.Count;
4440 ParameterData pd = GetParameterData (candidate);
4442 int pd_count = pd.Count;
4446 int count = pd_count - 1;
4448 if (pd.ParameterModifier (count) != Parameter.Modifier.ARGLIST)
4450 if (pd_count != arg_count)
4453 if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
4457 if (count > arg_count)
4460 if (pd_count == 1 && arg_count == 0)
4464 // If we have come this far, the case which
4465 // remains is when the number of parameters is
4466 // less than or equal to the argument count.
4468 for (int i = 0; i < count; ++i) {
4470 Argument a = (Argument) arguments [i];
4472 Parameter.Modifier a_mod = a.GetParameterModifier () &
4473 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4474 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4475 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4477 if (a_mod == p_mod) {
4479 if (a_mod == Parameter.Modifier.NONE)
4480 if (!Convert.ImplicitConversionExists (ec,
4482 pd.ParameterType (i)))
4485 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4486 Type pt = pd.ParameterType (i);
4489 pt = TypeManager.GetReferenceType (pt);
4500 Argument a = (Argument) arguments [count];
4501 if (!(a.Expr is Arglist))
4507 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
4509 for (int i = pd_count - 1; i < arg_count; i++) {
4510 Argument a = (Argument) arguments [i];
4512 if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
4520 /// Determines if the candidate method is applicable (section 14.4.2.1)
4521 /// to the given set of arguments
4523 static bool IsApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
4527 if (arguments == null)
4530 arg_count = arguments.Count;
4533 ParameterData pd = GetParameterData (candidate);
4535 if (arg_count != pd.Count)
4538 for (int i = arg_count; i > 0; ) {
4541 Argument a = (Argument) arguments [i];
4543 Parameter.Modifier a_mod = a.GetParameterModifier () &
4544 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4545 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4546 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4549 if (a_mod == p_mod ||
4550 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
4551 if (a_mod == Parameter.Modifier.NONE) {
4552 if (!Convert.ImplicitConversionExists (ec,
4554 pd.ParameterType (i)))
4558 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4559 Type pt = pd.ParameterType (i);
4562 pt = TypeManager.GetReferenceType (pt);
4575 /// Find the Applicable Function Members (7.4.2.1)
4577 /// me: Method Group expression with the members to select.
4578 /// it might contain constructors or methods (or anything
4579 /// that maps to a method).
4581 /// Arguments: ArrayList containing resolved Argument objects.
4583 /// loc: The location if we want an error to be reported, or a Null
4584 /// location for "probing" purposes.
4586 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4587 /// that is the best match of me on Arguments.
4590 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4591 ArrayList Arguments, Location loc)
4593 MethodBase method = null;
4594 Type applicable_type = null;
4596 ArrayList candidates = new ArrayList ();
4599 // Used to keep a map between the candidate
4600 // and whether it is being considered in its
4601 // normal or expanded form
4603 // false is normal form, true is expanded form
4605 Hashtable candidate_to_form = null;
4609 // First we construct the set of applicable methods
4611 // We start at the top of the type hierarchy and
4612 // go down to find applicable methods
4614 applicable_type = me.DeclaringType;
4616 if (me.Name == "Invoke" && TypeManager.IsDelegateType (applicable_type)) {
4617 Error_InvokeOnDelegate (loc);
4621 bool found_applicable = false;
4623 foreach (MethodBase candidate in me.Methods){
4624 Type decl_type = candidate.DeclaringType;
4627 // If we have already found an applicable method
4628 // we eliminate all base types (Section 14.5.5.1)
4630 if (decl_type != applicable_type &&
4631 (applicable_type.IsSubclassOf (decl_type) ||
4632 TypeManager.ImplementsInterface (applicable_type, decl_type)) &&
4637 // Check if candidate is applicable (section 14.4.2.1)
4638 if (IsApplicable (ec, Arguments, candidate)) {
4639 // Candidate is applicable in normal form
4640 candidates.Add (candidate);
4641 applicable_type = candidate.DeclaringType;
4642 found_applicable = true;
4643 } else if (IsParamsMethodApplicable (ec, Arguments, candidate, false)) {
4644 if (candidate_to_form == null)
4645 candidate_to_form = new PtrHashtable ();
4647 // Candidate is applicable in expanded form
4648 candidates.Add (candidate);
4649 applicable_type = candidate.DeclaringType;
4650 found_applicable = true;
4651 candidate_to_form [candidate] = candidate;
4652 } else if (IsParamsMethodApplicable (ec, Arguments, candidate, true)) {
4653 if (candidate_to_form == null)
4654 candidate_to_form = new PtrHashtable ();
4656 // Candidate is applicable in expanded form
4657 candidates.Add (candidate);
4658 applicable_type = candidate.DeclaringType;
4659 found_applicable = true;
4660 candidate_to_form [candidate] = candidate;
4666 // Now we actually find the best method
4668 int candidate_top = candidates.Count;
4669 for (int ix = 0; ix < candidate_top; ix++){
4670 MethodBase candidate = (MethodBase) candidates [ix];
4672 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4673 bool method_params = false;
4676 method_params = candidate_to_form != null && candidate_to_form.Contains (method);
4678 int x = BetterFunction (ec, Arguments,
4679 candidate, cand_params,
4680 method, method_params,
4689 if (Arguments == null)
4692 argument_count = Arguments.Count;
4695 if (method == null) {
4697 // Okay so we have failed to find anything so we
4698 // return by providing info about the closest match
4700 for (int i = 0; i < me.Methods.Length; ++i) {
4702 MethodBase c = (MethodBase) me.Methods [i];
4703 ParameterData pd = GetParameterData (c);
4705 if (pd.Count != argument_count)
4708 VerifyArgumentsCompat (ec, Arguments, argument_count, c, false,
4713 if (!Location.IsNull (loc)) {
4714 string report_name = me.Name;
4715 if (report_name == ".ctor")
4716 report_name = me.DeclaringType.ToString ();
4718 Error_WrongNumArguments (loc, report_name, argument_count);
4725 // Now check that there are no ambiguities i.e the selected method
4726 // should be better than all the others
4728 bool best_params = candidate_to_form != null && candidate_to_form.Contains (method);
4730 for (int ix = 0; ix < candidate_top; ix++){
4731 MethodBase candidate = (MethodBase) candidates [ix];
4733 if (candidate == method)
4737 // If a normal method is applicable in
4738 // the sense that it has the same
4739 // number of arguments, then the
4740 // expanded params method is never
4741 // applicable so we debar the params
4744 // if ((IsParamsMethodApplicable (ec, Arguments, candidate) &&
4745 // IsApplicable (ec, Arguments, method)))
4748 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4749 int x = BetterFunction (ec, Arguments,
4750 method, best_params,
4751 candidate, cand_params,
4757 "Ambiguous call when selecting function due to implicit casts");
4763 // And now check if the arguments are all
4764 // compatible, perform conversions if
4765 // necessary etc. and return if everything is
4768 if (!VerifyArgumentsCompat (ec, Arguments, argument_count, method,
4769 best_params, null, loc))
4775 static void Error_WrongNumArguments (Location loc, String name, int arg_count)
4777 Report.Error (1501, loc,
4778 "No overload for method `" + name + "' takes `" +
4779 arg_count + "' arguments");
4782 static void Error_InvokeOnDelegate (Location loc)
4784 Report.Error (1533, loc,
4785 "Invoke cannot be called directly on a delegate");
4788 static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
4789 Type delegate_type, string arg_sig, string par_desc)
4791 if (delegate_type == null)
4792 Report.Error (1502, loc,
4793 "The best overloaded match for method '" +
4794 FullMethodDesc (method) +
4795 "' has some invalid arguments");
4797 Report.Error (1594, loc,
4798 "Delegate '" + delegate_type.ToString () +
4799 "' has some invalid arguments.");
4800 Report.Error (1503, loc,
4801 String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'",
4802 idx, arg_sig, par_desc));
4805 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
4808 bool chose_params_expanded,
4812 ParameterData pd = GetParameterData (method);
4813 int pd_count = pd.Count;
4815 for (int j = 0; j < argument_count; j++) {
4816 Argument a = (Argument) Arguments [j];
4817 Expression a_expr = a.Expr;
4818 Type parameter_type = pd.ParameterType (j);
4819 Parameter.Modifier pm = pd.ParameterModifier (j);
4821 if (pm == Parameter.Modifier.PARAMS){
4822 if ((pm & ~Parameter.Modifier.PARAMS) != a.GetParameterModifier ()) {
4823 if (!Location.IsNull (loc))
4824 Error_InvalidArguments (
4825 loc, j, method, delegate_type,
4826 Argument.FullDesc (a), pd.ParameterDesc (j));
4830 if (chose_params_expanded)
4831 parameter_type = TypeManager.GetElementType (parameter_type);
4832 } else if (pm == Parameter.Modifier.ARGLIST){
4838 if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
4839 if (!Location.IsNull (loc))
4840 Error_InvalidArguments (
4841 loc, j, method, delegate_type,
4842 Argument.FullDesc (a), pd.ParameterDesc (j));
4850 if (a.Type != parameter_type){
4853 conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
4856 if (!Location.IsNull (loc))
4857 Error_InvalidArguments (
4858 loc, j, method, delegate_type,
4859 Argument.FullDesc (a), pd.ParameterDesc (j));
4864 // Update the argument with the implicit conversion
4870 Parameter.Modifier a_mod = a.GetParameterModifier () &
4871 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4872 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
4873 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4875 if (a_mod != p_mod &&
4876 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
4877 if (!Location.IsNull (loc)) {
4878 Report.Error (1502, loc,
4879 "The best overloaded match for method '" + FullMethodDesc (method)+
4880 "' has some invalid arguments");
4881 Report.Error (1503, loc,
4882 "Argument " + (j+1) +
4883 ": Cannot convert from '" + Argument.FullDesc (a)
4884 + "' to '" + pd.ParameterDesc (j) + "'");
4894 public override Expression DoResolve (EmitContext ec)
4897 // First, resolve the expression that is used to
4898 // trigger the invocation
4900 if (expr is BaseAccess)
4903 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4907 if (!(expr is MethodGroupExpr)) {
4908 Type expr_type = expr.Type;
4910 if (expr_type != null){
4911 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
4913 return (new DelegateInvocation (
4914 this.expr, Arguments, loc)).Resolve (ec);
4918 if (!(expr is MethodGroupExpr)){
4919 expr.Error_UnexpectedKind (ResolveFlags.MethodGroup);
4924 // Next, evaluate all the expressions in the argument list
4926 if (Arguments != null){
4927 foreach (Argument a in Arguments){
4928 if (!a.Resolve (ec, loc))
4933 MethodGroupExpr mg = (MethodGroupExpr) expr;
4934 method = OverloadResolve (ec, mg, Arguments, loc);
4936 if (method == null){
4938 "Could not find any applicable function for this argument list");
4942 MethodInfo mi = method as MethodInfo;
4944 type = TypeManager.TypeToCoreType (mi.ReturnType);
4945 if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null)) {
4946 SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
4950 Expression iexpr = mg.InstanceExpression;
4951 if (mi.IsStatic && (iexpr != null) && !(iexpr is This)) {
4952 if (mg.IdenticalTypeName)
4953 mg.InstanceExpression = null;
4955 MemberAccess.error176 (loc, mi.Name);
4961 if (type.IsPointer){
4969 // Only base will allow this invocation to happen.
4971 if (is_base && method.IsAbstract){
4972 Report.Error (205, loc, "Cannot call an abstract base member: " +
4973 FullMethodDesc (method));
4977 if ((method.Attributes & MethodAttributes.SpecialName) != 0){
4978 if (TypeManager.IsSpecialMethod (method))
4979 Report.Error (571, loc, method.Name + ": can not call operator or accessor");
4982 eclass = ExprClass.Value;
4987 // Emits the list of arguments as an array
4989 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
4991 ILGenerator ig = ec.ig;
4992 int count = arguments.Count - idx;
4993 Argument a = (Argument) arguments [idx];
4994 Type t = a.Expr.Type;
4996 IntConstant.EmitInt (ig, count);
4997 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
4999 int top = arguments.Count;
5000 for (int j = idx; j < top; j++){
5001 a = (Argument) arguments [j];
5003 ig.Emit (OpCodes.Dup);
5004 IntConstant.EmitInt (ig, j - idx);
5007 OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj);
5009 ig.Emit (OpCodes.Ldelema, t);
5014 ig.Emit (OpCodes.Stobj, t);
5021 /// Emits a list of resolved Arguments that are in the arguments
5024 /// The MethodBase argument might be null if the
5025 /// emission of the arguments is known not to contain
5026 /// a `params' field (for example in constructors or other routines
5027 /// that keep their arguments in this structure)
5029 /// if `dup_args' is true, a copy of the arguments will be left
5030 /// on the stack. If `dup_args' is true, you can specify `this_arg'
5031 /// which will be duplicated before any other args. Only EmitCall
5032 /// should be using this interface.
5034 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
5038 pd = GetParameterData (mb);
5042 LocalTemporary [] temps = null;
5045 temps = new LocalTemporary [arguments.Count];
5048 // If we are calling a params method with no arguments, special case it
5050 if (arguments == null){
5051 if (pd != null && pd.Count > 0 &&
5052 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
5053 ILGenerator ig = ec.ig;
5055 IntConstant.EmitInt (ig, 0);
5056 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
5062 int top = arguments.Count;
5064 for (int i = 0; i < top; i++){
5065 Argument a = (Argument) arguments [i];
5068 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
5070 // Special case if we are passing the same data as the
5071 // params argument, do not put it in an array.
5073 if (pd.ParameterType (i) == a.Type)
5076 EmitParams (ec, i, arguments);
5083 ec.ig.Emit (OpCodes.Dup);
5084 (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
5089 if (this_arg != null)
5092 for (int i = 0; i < top; i ++)
5093 temps [i].Emit (ec);
5096 if (pd != null && pd.Count > top &&
5097 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
5098 ILGenerator ig = ec.ig;
5100 IntConstant.EmitInt (ig, 0);
5101 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (top)));
5105 static Type[] GetVarargsTypes (EmitContext ec, MethodBase mb,
5106 ArrayList arguments)
5108 ParameterData pd = GetParameterData (mb);
5110 if (arguments == null)
5111 return new Type [0];
5113 Argument a = (Argument) arguments [pd.Count - 1];
5114 Arglist list = (Arglist) a.Expr;
5116 return list.ArgumentTypes;
5120 /// This checks the ConditionalAttribute on the method
5122 static bool IsMethodExcluded (MethodBase method, EmitContext ec)
5124 if (method.IsConstructor)
5127 IMethodData md = TypeManager.GetMethod (method);
5129 return md.IsExcluded (ec);
5131 // For some methods (generated by delegate class) GetMethod returns null
5132 // because they are not included in builder_to_method table
5133 if (method.DeclaringType is TypeBuilder)
5136 return AttributeTester.IsConditionalMethodExcluded (method);
5140 /// is_base tells whether we want to force the use of the `call'
5141 /// opcode instead of using callvirt. Call is required to call
5142 /// a specific method, while callvirt will always use the most
5143 /// recent method in the vtable.
5145 /// is_static tells whether this is an invocation on a static method
5147 /// instance_expr is an expression that represents the instance
5148 /// it must be non-null if is_static is false.
5150 /// method is the method to invoke.
5152 /// Arguments is the list of arguments to pass to the method or constructor.
5154 public static void EmitCall (EmitContext ec, bool is_base,
5155 bool is_static, Expression instance_expr,
5156 MethodBase method, ArrayList Arguments, Location loc)
5158 EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, loc, false, false);
5161 // `dup_args' leaves an extra copy of the arguments on the stack
5162 // `omit_args' does not leave any arguments at all.
5163 // So, basically, you could make one call with `dup_args' set to true,
5164 // and then another with `omit_args' set to true, and the two calls
5165 // would have the same set of arguments. However, each argument would
5166 // only have been evaluated once.
5167 public static void EmitCall (EmitContext ec, bool is_base,
5168 bool is_static, Expression instance_expr,
5169 MethodBase method, ArrayList Arguments, Location loc,
5170 bool dup_args, bool omit_args)
5172 ILGenerator ig = ec.ig;
5173 bool struct_call = false;
5174 bool this_call = false;
5175 LocalTemporary this_arg = null;
5177 Type decl_type = method.DeclaringType;
5179 if (!RootContext.StdLib) {
5180 // Replace any calls to the system's System.Array type with calls to
5181 // the newly created one.
5182 if (method == TypeManager.system_int_array_get_length)
5183 method = TypeManager.int_array_get_length;
5184 else if (method == TypeManager.system_int_array_get_rank)
5185 method = TypeManager.int_array_get_rank;
5186 else if (method == TypeManager.system_object_array_clone)
5187 method = TypeManager.object_array_clone;
5188 else if (method == TypeManager.system_int_array_get_length_int)
5189 method = TypeManager.int_array_get_length_int;
5190 else if (method == TypeManager.system_int_array_get_lower_bound_int)
5191 method = TypeManager.int_array_get_lower_bound_int;
5192 else if (method == TypeManager.system_int_array_get_upper_bound_int)
5193 method = TypeManager.int_array_get_upper_bound_int;
5194 else if (method == TypeManager.system_void_array_copyto_array_int)
5195 method = TypeManager.void_array_copyto_array_int;
5199 // This checks ObsoleteAttribute on the method and on the declaring type
5201 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
5203 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
5206 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
5208 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
5211 if (IsMethodExcluded (method, ec))
5215 this_call = instance_expr == null;
5216 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
5220 // If this is ourselves, push "this"
5225 ig.Emit (OpCodes.Ldarg_0);
5229 // Push the instance expression
5231 if (instance_expr.Type.IsValueType) {
5233 // Special case: calls to a function declared in a
5234 // reference-type with a value-type argument need
5235 // to have their value boxed.
5236 if (decl_type.IsValueType) {
5238 // If the expression implements IMemoryLocation, then
5239 // we can optimize and use AddressOf on the
5242 // If not we have to use some temporary storage for
5244 if (instance_expr is IMemoryLocation) {
5245 ((IMemoryLocation)instance_expr).
5246 AddressOf (ec, AddressOp.LoadStore);
5248 LocalTemporary temp = new LocalTemporary (ec, instance_expr.Type);
5249 instance_expr.Emit (ec);
5251 temp.AddressOf (ec, AddressOp.Load);
5254 // avoid the overhead of doing this all the time.
5256 t = TypeManager.GetReferenceType (instance_expr.Type);
5258 instance_expr.Emit (ec);
5259 ig.Emit (OpCodes.Box, instance_expr.Type);
5260 t = TypeManager.object_type;
5263 instance_expr.Emit (ec);
5264 t = instance_expr.Type;
5269 this_arg = new LocalTemporary (ec, t);
5270 ig.Emit (OpCodes.Dup);
5271 this_arg.Store (ec);
5277 EmitArguments (ec, method, Arguments, dup_args, this_arg);
5280 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
5281 call_op = OpCodes.Call;
5283 call_op = OpCodes.Callvirt;
5285 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
5286 Type[] varargs_types = GetVarargsTypes (ec, method, Arguments);
5287 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
5294 // and DoFoo is not virtual, you can omit the callvirt,
5295 // because you don't need the null checking behavior.
5297 if (method is MethodInfo)
5298 ig.Emit (call_op, (MethodInfo) method);
5300 ig.Emit (call_op, (ConstructorInfo) method);
5303 public override void Emit (EmitContext ec)
5305 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
5307 EmitCall (ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
5310 public override void EmitStatement (EmitContext ec)
5315 // Pop the return value if there is one
5317 if (method is MethodInfo){
5318 Type ret = ((MethodInfo)method).ReturnType;
5319 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
5320 ec.ig.Emit (OpCodes.Pop);
5325 public class InvocationOrCast : ExpressionStatement
5328 Expression argument;
5330 public InvocationOrCast (Expression expr, Expression argument, Location loc)
5333 this.argument = argument;
5337 public override Expression DoResolve (EmitContext ec)
5340 // First try to resolve it as a cast.
5342 type = ec.DeclSpace.ResolveType (expr, true, loc);
5344 Cast cast = new Cast (new TypeExpression (type, loc), argument, loc);
5345 return cast.Resolve (ec);
5349 // This can either be a type or a delegate invocation.
5350 // Let's just resolve it and see what we'll get.
5352 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5357 // Ok, so it's a Cast.
5359 if (expr.eclass == ExprClass.Type) {
5360 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5361 return cast.Resolve (ec);
5365 // It's a delegate invocation.
5367 if (!TypeManager.IsDelegateType (expr.Type)) {
5368 Error (149, "Method name expected");
5372 ArrayList args = new ArrayList ();
5373 args.Add (new Argument (argument, Argument.AType.Expression));
5374 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5375 return invocation.Resolve (ec);
5380 Error (201, "Only assignment, call, increment, decrement and new object " +
5381 "expressions can be used as a statement");
5384 public override ExpressionStatement ResolveStatement (EmitContext ec)
5387 // First try to resolve it as a cast.
5389 type = ec.DeclSpace.ResolveType (expr, true, loc);
5396 // This can either be a type or a delegate invocation.
5397 // Let's just resolve it and see what we'll get.
5399 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5400 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
5406 // It's a delegate invocation.
5408 if (!TypeManager.IsDelegateType (expr.Type)) {
5409 Error (149, "Method name expected");
5413 ArrayList args = new ArrayList ();
5414 args.Add (new Argument (argument, Argument.AType.Expression));
5415 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5416 return invocation.ResolveStatement (ec);
5419 public override void Emit (EmitContext ec)
5421 throw new Exception ("Cannot happen");
5424 public override void EmitStatement (EmitContext ec)
5426 throw new Exception ("Cannot happen");
5431 // This class is used to "disable" the code generation for the
5432 // temporary variable when initializing value types.
5434 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
5435 public void AddressOf (EmitContext ec, AddressOp Mode)
5442 /// Implements the new expression
5444 public class New : ExpressionStatement, IMemoryLocation {
5445 public readonly ArrayList Arguments;
5448 // During bootstrap, it contains the RequestedType,
5449 // but if `type' is not null, it *might* contain a NewDelegate
5450 // (because of field multi-initialization)
5452 public Expression RequestedType;
5454 MethodBase method = null;
5457 // If set, the new expression is for a value_target, and
5458 // we will not leave anything on the stack.
5460 Expression value_target;
5461 bool value_target_set = false;
5463 public New (Expression requested_type, ArrayList arguments, Location l)
5465 RequestedType = requested_type;
5466 Arguments = arguments;
5470 public bool SetValueTypeVariable (Expression value)
5472 value_target = value;
5473 value_target_set = true;
5474 if (!(value_target is IMemoryLocation)){
5475 Error_UnexpectedKind ("variable");
5482 // This function is used to disable the following code sequence for
5483 // value type initialization:
5485 // AddressOf (temporary)
5489 // Instead the provide will have provided us with the address on the
5490 // stack to store the results.
5492 static Expression MyEmptyExpression;
5494 public void DisableTemporaryValueType ()
5496 if (MyEmptyExpression == null)
5497 MyEmptyExpression = new EmptyAddressOf ();
5500 // To enable this, look into:
5501 // test-34 and test-89 and self bootstrapping.
5503 // For instance, we can avoid a copy by using `newobj'
5504 // instead of Call + Push-temp on value types.
5505 // value_target = MyEmptyExpression;
5508 public override Expression DoResolve (EmitContext ec)
5511 // The New DoResolve might be called twice when initializing field
5512 // expressions (see EmitFieldInitializers, the call to
5513 // GetInitializerExpression will perform a resolve on the expression,
5514 // and later the assign will trigger another resolution
5516 // This leads to bugs (#37014)
5519 if (RequestedType is NewDelegate)
5520 return RequestedType;
5524 type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
5529 CheckObsoleteAttribute (type);
5531 bool IsDelegate = TypeManager.IsDelegateType (type);
5534 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5535 if (RequestedType != null)
5536 if (!(RequestedType is NewDelegate))
5537 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
5538 return RequestedType;
5541 if (type.IsInterface || type.IsAbstract){
5542 Error (144, "It is not possible to create instances of interfaces or abstract classes");
5546 bool is_struct = type.IsValueType;
5547 eclass = ExprClass.Value;
5550 // SRE returns a match for .ctor () on structs (the object constructor),
5551 // so we have to manually ignore it.
5553 if (is_struct && Arguments == null)
5557 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5558 ml = MemberLookupFinal (ec, type, type, ".ctor",
5559 MemberTypes.Constructor,
5560 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5565 if (! (ml is MethodGroupExpr)){
5567 ml.Error_UnexpectedKind ("method group");
5573 if (Arguments != null){
5574 foreach (Argument a in Arguments){
5575 if (!a.Resolve (ec, loc))
5580 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
5584 if (method == null) {
5585 if (!is_struct || Arguments.Count > 0) {
5586 Error (1501, String.Format (
5587 "New invocation: Can not find a constructor in `{0}' for this argument list",
5588 TypeManager.CSharpName (type)));
5597 // This DoEmit can be invoked in two contexts:
5598 // * As a mechanism that will leave a value on the stack (new object)
5599 // * As one that wont (init struct)
5601 // You can control whether a value is required on the stack by passing
5602 // need_value_on_stack. The code *might* leave a value on the stack
5603 // so it must be popped manually
5605 // If we are dealing with a ValueType, we have a few
5606 // situations to deal with:
5608 // * The target is a ValueType, and we have been provided
5609 // the instance (this is easy, we are being assigned).
5611 // * The target of New is being passed as an argument,
5612 // to a boxing operation or a function that takes a
5615 // In this case, we need to create a temporary variable
5616 // that is the argument of New.
5618 // Returns whether a value is left on the stack
5620 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5622 bool is_value_type = type.IsValueType;
5623 ILGenerator ig = ec.ig;
5628 // Allow DoEmit() to be called multiple times.
5629 // We need to create a new LocalTemporary each time since
5630 // you can't share LocalBuilders among ILGeneators.
5631 if (!value_target_set)
5632 value_target = new LocalTemporary (ec, type);
5634 ml = (IMemoryLocation) value_target;
5635 ml.AddressOf (ec, AddressOp.Store);
5639 Invocation.EmitArguments (ec, method, Arguments, false, null);
5643 ig.Emit (OpCodes.Initobj, type);
5645 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5646 if (need_value_on_stack){
5647 value_target.Emit (ec);
5652 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5657 public override void Emit (EmitContext ec)
5662 public override void EmitStatement (EmitContext ec)
5664 if (DoEmit (ec, false))
5665 ec.ig.Emit (OpCodes.Pop);
5668 public void AddressOf (EmitContext ec, AddressOp Mode)
5670 if (!type.IsValueType){
5672 // We throw an exception. So far, I believe we only need to support
5674 // foreach (int j in new StructType ())
5677 throw new Exception ("AddressOf should not be used for classes");
5680 if (!value_target_set)
5681 value_target = new LocalTemporary (ec, type);
5683 IMemoryLocation ml = (IMemoryLocation) value_target;
5684 ml.AddressOf (ec, AddressOp.Store);
5686 Invocation.EmitArguments (ec, method, Arguments, false, null);
5689 ec.ig.Emit (OpCodes.Initobj, type);
5691 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5693 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5698 /// 14.5.10.2: Represents an array creation expression.
5702 /// There are two possible scenarios here: one is an array creation
5703 /// expression that specifies the dimensions and optionally the
5704 /// initialization data and the other which does not need dimensions
5705 /// specified but where initialization data is mandatory.
5707 public class ArrayCreation : Expression {
5708 Expression requested_base_type;
5709 ArrayList initializers;
5712 // The list of Argument types.
5713 // This is used to construct the `newarray' or constructor signature
5715 ArrayList arguments;
5718 // Method used to create the array object.
5720 MethodBase new_method = null;
5722 Type array_element_type;
5723 Type underlying_type;
5724 bool is_one_dimensional = false;
5725 bool is_builtin_type = false;
5726 bool expect_initializers = false;
5727 int num_arguments = 0;
5731 ArrayList array_data;
5736 // The number of array initializers that we can handle
5737 // via the InitializeArray method - through EmitStaticInitializers
5739 int num_automatic_initializers;
5741 const int max_automatic_initializers = 6;
5743 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5745 this.requested_base_type = requested_base_type;
5746 this.initializers = initializers;
5750 arguments = new ArrayList ();
5752 foreach (Expression e in exprs) {
5753 arguments.Add (new Argument (e, Argument.AType.Expression));
5758 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
5760 this.requested_base_type = requested_base_type;
5761 this.initializers = initializers;
5765 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5767 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5769 //dimensions = tmp.Length - 1;
5770 expect_initializers = true;
5773 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
5775 StringBuilder sb = new StringBuilder (rank);
5778 for (int i = 1; i < idx_count; i++)
5783 return new ComposedCast (base_type, sb.ToString (), loc);
5786 void Error_IncorrectArrayInitializer ()
5788 Error (178, "Incorrectly structured array initializer");
5791 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
5793 if (specified_dims) {
5794 Argument a = (Argument) arguments [idx];
5796 if (!a.Resolve (ec, loc))
5799 if (!(a.Expr is Constant)) {
5800 Error (150, "A constant value is expected");
5804 int value = (int) ((Constant) a.Expr).GetValue ();
5806 if (value != probe.Count) {
5807 Error_IncorrectArrayInitializer ();
5811 bounds [idx] = value;
5814 int child_bounds = -1;
5815 foreach (object o in probe) {
5816 if (o is ArrayList) {
5817 int current_bounds = ((ArrayList) o).Count;
5819 if (child_bounds == -1)
5820 child_bounds = current_bounds;
5822 else if (child_bounds != current_bounds){
5823 Error_IncorrectArrayInitializer ();
5826 if (specified_dims && (idx + 1 >= arguments.Count)){
5827 Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression");
5831 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
5835 if (child_bounds != -1){
5836 Error_IncorrectArrayInitializer ();
5840 Expression tmp = (Expression) o;
5841 tmp = tmp.Resolve (ec);
5845 // Console.WriteLine ("I got: " + tmp);
5846 // Handle initialization from vars, fields etc.
5848 Expression conv = Convert.ImplicitConversionRequired (
5849 ec, tmp, underlying_type, loc);
5854 if (conv is StringConstant || conv is DecimalConstant || conv is NullCast) {
5855 // These are subclasses of Constant that can appear as elements of an
5856 // array that cannot be statically initialized (with num_automatic_initializers
5857 // > max_automatic_initializers), so num_automatic_initializers should be left as zero.
5858 array_data.Add (conv);
5859 } else if (conv is Constant) {
5860 // These are the types of Constant that can appear in arrays that can be
5861 // statically allocated.
5862 array_data.Add (conv);
5863 num_automatic_initializers++;
5865 array_data.Add (conv);
5872 public void UpdateIndices (EmitContext ec)
5875 for (ArrayList probe = initializers; probe != null;) {
5876 if (probe.Count > 0 && probe [0] is ArrayList) {
5877 Expression e = new IntConstant (probe.Count);
5878 arguments.Add (new Argument (e, Argument.AType.Expression));
5880 bounds [i++] = probe.Count;
5882 probe = (ArrayList) probe [0];
5885 Expression e = new IntConstant (probe.Count);
5886 arguments.Add (new Argument (e, Argument.AType.Expression));
5888 bounds [i++] = probe.Count;
5895 public bool ValidateInitializers (EmitContext ec, Type array_type)
5897 if (initializers == null) {
5898 if (expect_initializers)
5904 if (underlying_type == null)
5908 // We use this to store all the date values in the order in which we
5909 // will need to store them in the byte blob later
5911 array_data = new ArrayList ();
5912 bounds = new Hashtable ();
5916 if (arguments != null) {
5917 ret = CheckIndices (ec, initializers, 0, true);
5920 arguments = new ArrayList ();
5922 ret = CheckIndices (ec, initializers, 0, false);
5929 if (arguments.Count != dimensions) {
5930 Error_IncorrectArrayInitializer ();
5938 void Error_NegativeArrayIndex ()
5940 Error (284, "Can not create array with a negative size");
5944 // Converts `source' to an int, uint, long or ulong.
5946 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
5950 bool old_checked = ec.CheckState;
5951 ec.CheckState = true;
5953 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
5954 if (target == null){
5955 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
5956 if (target == null){
5957 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
5958 if (target == null){
5959 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
5961 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
5965 ec.CheckState = old_checked;
5968 // Only positive constants are allowed at compile time
5970 if (target is Constant){
5971 if (target is IntConstant){
5972 if (((IntConstant) target).Value < 0){
5973 Error_NegativeArrayIndex ();
5978 if (target is LongConstant){
5979 if (((LongConstant) target).Value < 0){
5980 Error_NegativeArrayIndex ();
5991 // Creates the type of the array
5993 bool LookupType (EmitContext ec)
5995 StringBuilder array_qualifier = new StringBuilder (rank);
5998 // `In the first form allocates an array instace of the type that results
5999 // from deleting each of the individual expression from the expression list'
6001 if (num_arguments > 0) {
6002 array_qualifier.Append ("[");
6003 for (int i = num_arguments-1; i > 0; i--)
6004 array_qualifier.Append (",");
6005 array_qualifier.Append ("]");
6011 Expression array_type_expr;
6012 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
6013 type = ec.DeclSpace.ResolveType (array_type_expr, false, loc);
6018 underlying_type = type;
6019 if (underlying_type.IsArray)
6020 underlying_type = TypeManager.GetElementType (underlying_type);
6021 dimensions = type.GetArrayRank ();
6026 public override Expression DoResolve (EmitContext ec)
6030 if (!LookupType (ec))
6034 // First step is to validate the initializers and fill
6035 // in any missing bits
6037 if (!ValidateInitializers (ec, type))
6040 if (arguments == null)
6043 arg_count = arguments.Count;
6044 foreach (Argument a in arguments){
6045 if (!a.Resolve (ec, loc))
6048 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
6049 if (real_arg == null)
6056 array_element_type = TypeManager.GetElementType (type);
6058 if (arg_count == 1) {
6059 is_one_dimensional = true;
6060 eclass = ExprClass.Value;
6064 is_builtin_type = TypeManager.IsBuiltinType (type);
6066 if (is_builtin_type) {
6069 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
6070 AllBindingFlags, loc);
6072 if (!(ml is MethodGroupExpr)) {
6073 ml.Error_UnexpectedKind ("method group");
6078 Error (-6, "New invocation: Can not find a constructor for " +
6079 "this argument list");
6083 new_method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, arguments, loc);
6085 if (new_method == null) {
6086 Error (-6, "New invocation: Can not find a constructor for " +
6087 "this argument list");
6091 eclass = ExprClass.Value;
6094 ModuleBuilder mb = CodeGen.Module.Builder;
6095 ArrayList args = new ArrayList ();
6097 if (arguments != null) {
6098 for (int i = 0; i < arg_count; i++)
6099 args.Add (TypeManager.int32_type);
6102 Type [] arg_types = null;
6105 arg_types = new Type [args.Count];
6107 args.CopyTo (arg_types, 0);
6109 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
6112 if (new_method == null) {
6113 Error (-6, "New invocation: Can not find a constructor for " +
6114 "this argument list");
6118 eclass = ExprClass.Value;
6123 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
6128 int count = array_data.Count;
6130 if (underlying_type.IsEnum)
6131 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
6133 factor = GetTypeSize (underlying_type);
6135 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
6137 data = new byte [(count * factor + 4) & ~3];
6140 for (int i = 0; i < count; ++i) {
6141 object v = array_data [i];
6143 if (v is EnumConstant)
6144 v = ((EnumConstant) v).Child;
6146 if (v is Constant && !(v is StringConstant))
6147 v = ((Constant) v).GetValue ();
6153 if (underlying_type == TypeManager.int64_type){
6154 if (!(v is Expression)){
6155 long val = (long) v;
6157 for (int j = 0; j < factor; ++j) {
6158 data [idx + j] = (byte) (val & 0xFF);
6162 } else if (underlying_type == TypeManager.uint64_type){
6163 if (!(v is Expression)){
6164 ulong val = (ulong) v;
6166 for (int j = 0; j < factor; ++j) {
6167 data [idx + j] = (byte) (val & 0xFF);
6171 } else if (underlying_type == TypeManager.float_type) {
6172 if (!(v is Expression)){
6173 element = BitConverter.GetBytes ((float) v);
6175 for (int j = 0; j < factor; ++j)
6176 data [idx + j] = element [j];
6178 } else if (underlying_type == TypeManager.double_type) {
6179 if (!(v is Expression)){
6180 element = BitConverter.GetBytes ((double) v);
6182 for (int j = 0; j < factor; ++j)
6183 data [idx + j] = element [j];
6185 } else if (underlying_type == TypeManager.char_type){
6186 if (!(v is Expression)){
6187 int val = (int) ((char) v);
6189 data [idx] = (byte) (val & 0xff);
6190 data [idx+1] = (byte) (val >> 8);
6192 } else if (underlying_type == TypeManager.short_type){
6193 if (!(v is Expression)){
6194 int val = (int) ((short) v);
6196 data [idx] = (byte) (val & 0xff);
6197 data [idx+1] = (byte) (val >> 8);
6199 } else if (underlying_type == TypeManager.ushort_type){
6200 if (!(v is Expression)){
6201 int val = (int) ((ushort) v);
6203 data [idx] = (byte) (val & 0xff);
6204 data [idx+1] = (byte) (val >> 8);
6206 } else if (underlying_type == TypeManager.int32_type) {
6207 if (!(v is Expression)){
6210 data [idx] = (byte) (val & 0xff);
6211 data [idx+1] = (byte) ((val >> 8) & 0xff);
6212 data [idx+2] = (byte) ((val >> 16) & 0xff);
6213 data [idx+3] = (byte) (val >> 24);
6215 } else if (underlying_type == TypeManager.uint32_type) {
6216 if (!(v is Expression)){
6217 uint val = (uint) v;
6219 data [idx] = (byte) (val & 0xff);
6220 data [idx+1] = (byte) ((val >> 8) & 0xff);
6221 data [idx+2] = (byte) ((val >> 16) & 0xff);
6222 data [idx+3] = (byte) (val >> 24);
6224 } else if (underlying_type == TypeManager.sbyte_type) {
6225 if (!(v is Expression)){
6226 sbyte val = (sbyte) v;
6227 data [idx] = (byte) val;
6229 } else if (underlying_type == TypeManager.byte_type) {
6230 if (!(v is Expression)){
6231 byte val = (byte) v;
6232 data [idx] = (byte) val;
6234 } else if (underlying_type == TypeManager.bool_type) {
6235 if (!(v is Expression)){
6236 bool val = (bool) v;
6237 data [idx] = (byte) (val ? 1 : 0);
6239 } else if (underlying_type == TypeManager.decimal_type){
6240 if (!(v is Expression)){
6241 int [] bits = Decimal.GetBits ((decimal) v);
6244 // FIXME: For some reason, this doesn't work on the MS runtime.
6245 int [] nbits = new int [4];
6246 nbits [0] = bits [3];
6247 nbits [1] = bits [2];
6248 nbits [2] = bits [0];
6249 nbits [3] = bits [1];
6251 for (int j = 0; j < 4; j++){
6252 data [p++] = (byte) (nbits [j] & 0xff);
6253 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6254 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6255 data [p++] = (byte) (nbits [j] >> 24);
6259 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
6268 // Emits the initializers for the array
6270 void EmitStaticInitializers (EmitContext ec)
6273 // First, the static data
6276 ILGenerator ig = ec.ig;
6278 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
6280 fb = RootContext.MakeStaticData (data);
6282 ig.Emit (OpCodes.Dup);
6283 ig.Emit (OpCodes.Ldtoken, fb);
6284 ig.Emit (OpCodes.Call,
6285 TypeManager.void_initializearray_array_fieldhandle);
6289 // Emits pieces of the array that can not be computed at compile
6290 // time (variables and string locations).
6292 // This always expect the top value on the stack to be the array
6294 void EmitDynamicInitializers (EmitContext ec)
6296 ILGenerator ig = ec.ig;
6297 int dims = bounds.Count;
6298 int [] current_pos = new int [dims];
6299 int top = array_data.Count;
6301 MethodInfo set = null;
6305 ModuleBuilder mb = null;
6306 mb = CodeGen.Module.Builder;
6307 args = new Type [dims + 1];
6310 for (j = 0; j < dims; j++)
6311 args [j] = TypeManager.int32_type;
6313 args [j] = array_element_type;
6315 set = mb.GetArrayMethod (
6317 CallingConventions.HasThis | CallingConventions.Standard,
6318 TypeManager.void_type, args);
6321 for (int i = 0; i < top; i++){
6323 Expression e = null;
6325 if (array_data [i] is Expression)
6326 e = (Expression) array_data [i];
6330 // Basically we do this for string literals and
6331 // other non-literal expressions
6333 if (e is EnumConstant){
6334 e = ((EnumConstant) e).Child;
6337 if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
6338 num_automatic_initializers <= max_automatic_initializers) {
6339 Type etype = e.Type;
6341 ig.Emit (OpCodes.Dup);
6343 for (int idx = 0; idx < dims; idx++)
6344 IntConstant.EmitInt (ig, current_pos [idx]);
6347 // If we are dealing with a struct, get the
6348 // address of it, so we can store it.
6351 etype.IsSubclassOf (TypeManager.value_type) &&
6352 (!TypeManager.IsBuiltinOrEnum (etype) ||
6353 etype == TypeManager.decimal_type)) {
6358 // Let new know that we are providing
6359 // the address where to store the results
6361 n.DisableTemporaryValueType ();
6364 ig.Emit (OpCodes.Ldelema, etype);
6371 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
6373 ig.Emit (OpCodes.Stobj, etype);
6377 ig.Emit (OpCodes.Call, set);
6385 for (int j = dims - 1; j >= 0; j--){
6387 if (current_pos [j] < (int) bounds [j])
6389 current_pos [j] = 0;
6394 void EmitArrayArguments (EmitContext ec)
6396 ILGenerator ig = ec.ig;
6398 foreach (Argument a in arguments) {
6399 Type atype = a.Type;
6402 if (atype == TypeManager.uint64_type)
6403 ig.Emit (OpCodes.Conv_Ovf_U4);
6404 else if (atype == TypeManager.int64_type)
6405 ig.Emit (OpCodes.Conv_Ovf_I4);
6409 public override void Emit (EmitContext ec)
6411 ILGenerator ig = ec.ig;
6413 EmitArrayArguments (ec);
6414 if (is_one_dimensional)
6415 ig.Emit (OpCodes.Newarr, array_element_type);
6417 if (is_builtin_type)
6418 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
6420 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
6423 if (initializers != null){
6425 // FIXME: Set this variable correctly.
6427 bool dynamic_initializers = true;
6429 // This will never be true for array types that cannot be statically
6430 // initialized. num_automatic_initializers will always be zero. See
6432 if (num_automatic_initializers > max_automatic_initializers)
6433 EmitStaticInitializers (ec);
6435 if (dynamic_initializers)
6436 EmitDynamicInitializers (ec);
6440 public object EncodeAsAttribute ()
6442 if (!is_one_dimensional){
6443 Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6447 if (array_data == null){
6448 Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6452 object [] ret = new object [array_data.Count];
6454 foreach (Expression e in array_data){
6457 if (e is NullLiteral)
6460 if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
6468 public Expression TurnIntoConstant ()
6471 // Should use something like the above attribute thing.
6472 // It should return a subclass of Constant that just returns
6473 // the computed value of the array
6475 throw new Exception ("Does not support yet Turning array into a Constant");
6480 /// Represents the `this' construct
6482 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
6485 VariableInfo variable_info;
6487 public This (Block block, Location loc)
6493 public This (Location loc)
6498 public VariableInfo VariableInfo {
6499 get { return variable_info; }
6502 public bool VerifyFixed (bool is_expression)
6504 if ((variable_info == null) || (variable_info.LocalInfo == null))
6507 return variable_info.LocalInfo.IsFixed;
6510 public bool ResolveBase (EmitContext ec)
6512 eclass = ExprClass.Variable;
6513 type = ec.ContainerType;
6516 Error (26, "Keyword this not valid in static code");
6520 if ((block != null) && (block.ThisVariable != null))
6521 variable_info = block.ThisVariable.VariableInfo;
6526 public override Expression DoResolve (EmitContext ec)
6528 if (!ResolveBase (ec))
6531 if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
6532 Error (188, "The this object cannot be used before all " +
6533 "of its fields are assigned to");
6534 variable_info.SetAssigned (ec);
6538 if (ec.IsFieldInitializer) {
6539 Error (27, "Keyword `this' can't be used outside a constructor, " +
6540 "a method or a property.");
6547 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6549 if (!ResolveBase (ec))
6552 if (variable_info != null)
6553 variable_info.SetAssigned (ec);
6555 if (ec.TypeContainer is Class){
6556 Error (1604, "Cannot assign to `this'");
6563 public void Emit (EmitContext ec, bool leave_copy)
6567 ec.ig.Emit (OpCodes.Dup);
6570 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
6572 ILGenerator ig = ec.ig;
6574 if (ec.TypeContainer is Struct){
6578 ec.ig.Emit (OpCodes.Dup);
6579 ig.Emit (OpCodes.Stobj, type);
6581 throw new Exception ("how did you get here");
6585 public override void Emit (EmitContext ec)
6587 ILGenerator ig = ec.ig;
6590 if (ec.TypeContainer is Struct)
6591 ig.Emit (OpCodes.Ldobj, type);
6594 public void AddressOf (EmitContext ec, AddressOp mode)
6599 // FIGURE OUT WHY LDARG_S does not work
6601 // consider: struct X { int val; int P { set { val = value; }}}
6603 // Yes, this looks very bad. Look at `NOTAS' for
6605 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
6610 /// Represents the `__arglist' construct
6612 public class ArglistAccess : Expression
6614 public ArglistAccess (Location loc)
6619 public bool ResolveBase (EmitContext ec)
6621 eclass = ExprClass.Variable;
6622 type = TypeManager.runtime_argument_handle_type;
6626 public override Expression DoResolve (EmitContext ec)
6628 if (!ResolveBase (ec))
6631 if (ec.IsFieldInitializer || !ec.CurrentBlock.HasVarargs) {
6632 Error (190, "The __arglist construct is valid only within " +
6633 "a variable argument method.");
6640 public override void Emit (EmitContext ec)
6642 ec.ig.Emit (OpCodes.Arglist);
6647 /// Represents the `__arglist (....)' construct
6649 public class Arglist : Expression
6651 public readonly Argument[] Arguments;
6653 public Arglist (Argument[] args, Location l)
6659 public Type[] ArgumentTypes {
6661 Type[] retval = new Type [Arguments.Length];
6662 for (int i = 0; i < Arguments.Length; i++)
6663 retval [i] = Arguments [i].Type;
6668 public override Expression DoResolve (EmitContext ec)
6670 eclass = ExprClass.Variable;
6671 type = TypeManager.runtime_argument_handle_type;
6673 foreach (Argument arg in Arguments) {
6674 if (!arg.Resolve (ec, loc))
6681 public override void Emit (EmitContext ec)
6683 foreach (Argument arg in Arguments)
6689 // This produces the value that renders an instance, used by the iterators code
6691 public class ProxyInstance : Expression, IMemoryLocation {
6692 public override Expression DoResolve (EmitContext ec)
6694 eclass = ExprClass.Variable;
6695 type = ec.ContainerType;
6699 public override void Emit (EmitContext ec)
6701 ec.ig.Emit (OpCodes.Ldarg_0);
6705 public void AddressOf (EmitContext ec, AddressOp mode)
6707 ec.ig.Emit (OpCodes.Ldarg_0);
6712 /// Implements the typeof operator
6714 public class TypeOf : Expression {
6715 public readonly Expression QueriedType;
6716 protected Type typearg;
6718 public TypeOf (Expression queried_type, Location l)
6720 QueriedType = queried_type;
6724 public override Expression DoResolve (EmitContext ec)
6726 typearg = ec.DeclSpace.ResolveType (QueriedType, false, loc);
6728 if (typearg == null)
6731 if (typearg == TypeManager.void_type) {
6732 Error (673, "System.Void cannot be used from C# - " +
6733 "use typeof (void) to get the void type object");
6737 CheckObsoleteAttribute (typearg);
6739 type = TypeManager.type_type;
6740 eclass = ExprClass.Type;
6744 public override void Emit (EmitContext ec)
6746 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6747 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6750 public Type TypeArg {
6751 get { return typearg; }
6756 /// Implements the `typeof (void)' operator
6758 public class TypeOfVoid : TypeOf {
6759 public TypeOfVoid (Location l) : base (null, l)
6764 public override Expression DoResolve (EmitContext ec)
6766 type = TypeManager.type_type;
6767 typearg = TypeManager.void_type;
6768 eclass = ExprClass.Type;
6774 /// Implements the sizeof expression
6776 public class SizeOf : Expression {
6777 public readonly Expression QueriedType;
6780 public SizeOf (Expression queried_type, Location l)
6782 this.QueriedType = queried_type;
6786 public override Expression DoResolve (EmitContext ec)
6790 233, loc, "Sizeof may only be used in an unsafe context " +
6791 "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
6795 type_queried = ec.DeclSpace.ResolveType (QueriedType, false, loc);
6796 if (type_queried == null)
6799 CheckObsoleteAttribute (type_queried);
6801 if (!TypeManager.IsUnmanagedType (type_queried)){
6802 Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
6806 type = TypeManager.int32_type;
6807 eclass = ExprClass.Value;
6811 public override void Emit (EmitContext ec)
6813 int size = GetTypeSize (type_queried);
6816 ec.ig.Emit (OpCodes.Sizeof, type_queried);
6818 IntConstant.EmitInt (ec.ig, size);
6823 /// Implements the member access expression
6825 public class MemberAccess : Expression {
6826 public readonly string Identifier;
6829 public MemberAccess (Expression expr, string id, Location l)
6836 public Expression Expr {
6842 public static void error176 (Location loc, string name)
6844 Report.Error (176, loc, "Static member `" +
6845 name + "' cannot be accessed " +
6846 "with an instance reference, qualify with a " +
6847 "type name instead");
6850 public static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Expression left, Location loc)
6852 SimpleName sn = left_original as SimpleName;
6853 if (sn == null || left == null || left.Type.Name != sn.Name)
6856 return RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc) != null;
6859 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
6860 Expression left, Location loc,
6861 Expression left_original)
6863 bool left_is_type, left_is_explicit;
6865 // If `left' is null, then we're called from SimpleNameResolve and this is
6866 // a member in the currently defining class.
6868 left_is_type = ec.IsStatic || ec.IsFieldInitializer;
6869 left_is_explicit = false;
6871 // Implicitly default to `this' unless we're static.
6872 if (!ec.IsStatic && !ec.IsFieldInitializer && !ec.InEnumContext)
6873 left = ec.GetThis (loc);
6875 left_is_type = left is TypeExpr;
6876 left_is_explicit = true;
6879 if (member_lookup is FieldExpr){
6880 FieldExpr fe = (FieldExpr) member_lookup;
6881 FieldInfo fi = fe.FieldInfo;
6882 Type decl_type = fi.DeclaringType;
6884 if (fi is FieldBuilder) {
6885 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
6889 if (!c.LookupConstantValue (out o))
6892 object real_value = ((Constant) c.Expr).GetValue ();
6894 return Constantify (real_value, fi.FieldType);
6899 Type t = fi.FieldType;
6903 if (fi is FieldBuilder)
6904 o = TypeManager.GetValue ((FieldBuilder) fi);
6906 o = fi.GetValue (fi);
6908 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
6909 if (left_is_explicit && !left_is_type &&
6910 !IdenticalNameAndTypeName (ec, left_original, member_lookup, loc)) {
6911 error176 (loc, fe.FieldInfo.Name);
6915 Expression enum_member = MemberLookup (
6916 ec, decl_type, "value__", MemberTypes.Field,
6917 AllBindingFlags, loc);
6919 Enum en = TypeManager.LookupEnum (decl_type);
6923 c = Constantify (o, en.UnderlyingType);
6925 c = Constantify (o, enum_member.Type);
6927 return new EnumConstant (c, decl_type);
6930 Expression exp = Constantify (o, t);
6932 if (left_is_explicit && !left_is_type) {
6933 error176 (loc, fe.FieldInfo.Name);
6940 if (fi.FieldType.IsPointer && !ec.InUnsafe){
6946 if (member_lookup is EventExpr) {
6947 EventExpr ee = (EventExpr) member_lookup;
6950 // If the event is local to this class, we transform ourselves into
6954 if (ee.EventInfo.DeclaringType == ec.ContainerType ||
6955 TypeManager.IsNestedChildOf(ec.ContainerType, ee.EventInfo.DeclaringType)) {
6956 MemberInfo mi = GetFieldFromEvent (ee);
6960 // If this happens, then we have an event with its own
6961 // accessors and private field etc so there's no need
6962 // to transform ourselves.
6964 ee.InstanceExpression = left;
6968 Expression ml = ExprClassFromMemberInfo (ec, mi, loc);
6971 Report.Error (-200, loc, "Internal error!!");
6975 if (!left_is_explicit)
6978 ee.InstanceExpression = left;
6980 return ResolveMemberAccess (ec, ml, left, loc, left_original);
6984 if (member_lookup is IMemberExpr) {
6985 IMemberExpr me = (IMemberExpr) member_lookup;
6986 MethodGroupExpr mg = me as MethodGroupExpr;
6989 if ((mg != null) && left_is_explicit && left.Type.IsInterface)
6990 mg.IsExplicitImpl = left_is_explicit;
6993 if ((ec.IsFieldInitializer || ec.IsStatic) &&
6994 IdenticalNameAndTypeName (ec, left_original, member_lookup, loc))
6995 return member_lookup;
6997 SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
7002 if (!me.IsInstance) {
7003 if (IdenticalNameAndTypeName (ec, left_original, left, loc))
7004 return member_lookup;
7006 if (left_is_explicit) {
7007 error176 (loc, me.Name);
7013 // Since we can not check for instance objects in SimpleName,
7014 // becaue of the rule that allows types and variables to share
7015 // the name (as long as they can be de-ambiguated later, see
7016 // IdenticalNameAndTypeName), we have to check whether left
7017 // is an instance variable in a static context
7019 // However, if the left-hand value is explicitly given, then
7020 // it is already our instance expression, so we aren't in
7024 if (ec.IsStatic && !left_is_explicit && left is IMemberExpr){
7025 IMemberExpr mexp = (IMemberExpr) left;
7027 if (!mexp.IsStatic){
7028 SimpleName.Error_ObjectRefRequired (ec, loc, mexp.Name);
7033 if ((mg != null) && IdenticalNameAndTypeName (ec, left_original, left, loc))
7034 mg.IdenticalTypeName = true;
7036 me.InstanceExpression = left;
7039 return member_lookup;
7042 Console.WriteLine ("Left is: " + left);
7043 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
7044 Environment.Exit (1);
7048 public Expression DoResolve (EmitContext ec, Expression right_side, ResolveFlags flags)
7051 throw new Exception ();
7054 // Resolve the expression with flow analysis turned off, we'll do the definite
7055 // assignment checks later. This is because we don't know yet what the expression
7056 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7057 // definite assignment check on the actual field and not on the whole struct.
7060 Expression original = expr;
7061 expr = expr.Resolve (ec, flags | ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
7065 if (expr is SimpleName){
7066 SimpleName child_expr = (SimpleName) expr;
7068 Expression new_expr = new SimpleName (child_expr.Name, Identifier, loc);
7070 return new_expr.Resolve (ec, flags);
7074 // TODO: I mailed Ravi about this, and apparently we can get rid
7075 // of this and put it in the right place.
7077 // Handle enums here when they are in transit.
7078 // Note that we cannot afford to hit MemberLookup in this case because
7079 // it will fail to find any members at all
7082 Type expr_type = expr.Type;
7083 if (expr is TypeExpr){
7084 if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
7085 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", expr_type);
7089 if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
7090 Enum en = TypeManager.LookupEnum (expr_type);
7093 object value = en.LookupEnumValue (ec, Identifier, loc);
7096 ObsoleteAttribute oa = en.GetObsoleteAttribute (ec, Identifier);
7098 AttributeTester.Report_ObsoleteMessage (oa, en.GetSignatureForError (), Location);
7101 Constant c = Constantify (value, en.UnderlyingType);
7102 return new EnumConstant (c, expr_type);
7105 CheckObsoleteAttribute (expr_type);
7107 FieldInfo fi = expr_type.GetField (Identifier);
7109 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
7111 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), Location);
7117 if (expr_type.IsPointer){
7118 Error (23, "The `.' operator can not be applied to pointer operands (" +
7119 TypeManager.CSharpName (expr_type) + ")");
7123 Expression member_lookup;
7124 member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
7125 if (member_lookup == null)
7128 if (member_lookup is TypeExpr) {
7129 if (!(expr is TypeExpr) && !(expr is SimpleName)) {
7130 Error (572, "Can't reference type `" + Identifier + "' through an expression; try `" +
7131 member_lookup.Type + "' instead");
7135 return member_lookup;
7138 member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
7139 if (member_lookup == null)
7142 // The following DoResolve/DoResolveLValue will do the definite assignment
7145 if (right_side != null)
7146 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
7148 member_lookup = member_lookup.DoResolve (ec);
7150 return member_lookup;
7153 public override Expression DoResolve (EmitContext ec)
7155 return DoResolve (ec, null, ResolveFlags.VariableOrValue |
7156 ResolveFlags.SimpleName | ResolveFlags.Type);
7159 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7161 return DoResolve (ec, right_side, ResolveFlags.VariableOrValue |
7162 ResolveFlags.SimpleName | ResolveFlags.Type);
7165 public override Expression ResolveAsTypeStep (EmitContext ec)
7167 string fname = null;
7168 MemberAccess full_expr = this;
7169 while (full_expr != null) {
7171 fname = String.Concat (full_expr.Identifier, ".", fname);
7173 fname = full_expr.Identifier;
7175 if (full_expr.Expr is SimpleName) {
7176 string full_name = String.Concat (((SimpleName) full_expr.Expr).Name, ".", fname);
7177 Type fully_qualified = ec.DeclSpace.FindType (loc, full_name);
7178 if (fully_qualified != null)
7179 return new TypeExpression (fully_qualified, loc);
7182 full_expr = full_expr.Expr as MemberAccess;
7185 Expression new_expr = expr.ResolveAsTypeStep (ec);
7187 if (new_expr == null)
7190 if (new_expr is SimpleName){
7191 SimpleName child_expr = (SimpleName) new_expr;
7193 new_expr = new SimpleName (child_expr.Name, Identifier, loc);
7195 return new_expr.ResolveAsTypeStep (ec);
7198 Type expr_type = new_expr.Type;
7200 if (expr_type.IsPointer){
7201 Error (23, "The `.' operator can not be applied to pointer operands (" +
7202 TypeManager.CSharpName (expr_type) + ")");
7206 Expression member_lookup;
7207 member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
7208 if (member_lookup == null)
7211 if (member_lookup is TypeExpr){
7212 member_lookup.Resolve (ec, ResolveFlags.Type);
7213 return member_lookup;
7219 public override void Emit (EmitContext ec)
7221 throw new Exception ("Should not happen");
7224 public override string ToString ()
7226 return expr + "." + Identifier;
7231 /// Implements checked expressions
7233 public class CheckedExpr : Expression {
7235 public Expression Expr;
7237 public CheckedExpr (Expression e, Location l)
7243 public override Expression DoResolve (EmitContext ec)
7245 bool last_check = ec.CheckState;
7246 bool last_const_check = ec.ConstantCheckState;
7248 ec.CheckState = true;
7249 ec.ConstantCheckState = true;
7250 Expr = Expr.Resolve (ec);
7251 ec.CheckState = last_check;
7252 ec.ConstantCheckState = last_const_check;
7257 if (Expr is Constant)
7260 eclass = Expr.eclass;
7265 public override void Emit (EmitContext ec)
7267 bool last_check = ec.CheckState;
7268 bool last_const_check = ec.ConstantCheckState;
7270 ec.CheckState = true;
7271 ec.ConstantCheckState = true;
7273 ec.CheckState = last_check;
7274 ec.ConstantCheckState = last_const_check;
7280 /// Implements the unchecked expression
7282 public class UnCheckedExpr : Expression {
7284 public Expression Expr;
7286 public UnCheckedExpr (Expression e, Location l)
7292 public override Expression DoResolve (EmitContext ec)
7294 bool last_check = ec.CheckState;
7295 bool last_const_check = ec.ConstantCheckState;
7297 ec.CheckState = false;
7298 ec.ConstantCheckState = false;
7299 Expr = Expr.Resolve (ec);
7300 ec.CheckState = last_check;
7301 ec.ConstantCheckState = last_const_check;
7306 if (Expr is Constant)
7309 eclass = Expr.eclass;
7314 public override void Emit (EmitContext ec)
7316 bool last_check = ec.CheckState;
7317 bool last_const_check = ec.ConstantCheckState;
7319 ec.CheckState = false;
7320 ec.ConstantCheckState = false;
7322 ec.CheckState = last_check;
7323 ec.ConstantCheckState = last_const_check;
7329 /// An Element Access expression.
7331 /// During semantic analysis these are transformed into
7332 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7334 public class ElementAccess : Expression {
7335 public ArrayList Arguments;
7336 public Expression Expr;
7338 public ElementAccess (Expression e, ArrayList e_list, Location l)
7347 Arguments = new ArrayList ();
7348 foreach (Expression tmp in e_list)
7349 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7353 bool CommonResolve (EmitContext ec)
7355 Expr = Expr.Resolve (ec);
7360 if (Arguments == null)
7363 foreach (Argument a in Arguments){
7364 if (!a.Resolve (ec, loc))
7371 Expression MakePointerAccess ()
7375 if (t == TypeManager.void_ptr_type){
7376 Error (242, "The array index operation is not valid for void pointers");
7379 if (Arguments.Count != 1){
7380 Error (196, "A pointer must be indexed by a single value");
7385 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
7386 return new Indirection (p, loc);
7389 public override Expression DoResolve (EmitContext ec)
7391 if (!CommonResolve (ec))
7395 // We perform some simple tests, and then to "split" the emit and store
7396 // code we create an instance of a different class, and return that.
7398 // I am experimenting with this pattern.
7402 if (t == TypeManager.array_type){
7403 Report.Error (21, loc, "Cannot use indexer on System.Array");
7408 return (new ArrayAccess (this, loc)).Resolve (ec);
7409 else if (t.IsPointer)
7410 return MakePointerAccess ();
7412 return (new IndexerAccess (this, loc)).Resolve (ec);
7415 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7417 if (!CommonResolve (ec))
7422 return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
7423 else if (t.IsPointer)
7424 return MakePointerAccess ();
7426 return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
7429 public override void Emit (EmitContext ec)
7431 throw new Exception ("Should never be reached");
7436 /// Implements array access
7438 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7440 // Points to our "data" repository
7444 LocalTemporary temp;
7447 public ArrayAccess (ElementAccess ea_data, Location l)
7450 eclass = ExprClass.Variable;
7454 public override Expression DoResolve (EmitContext ec)
7457 ExprClass eclass = ea.Expr.eclass;
7459 // As long as the type is valid
7460 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7461 eclass == ExprClass.Value)) {
7462 ea.Expr.Error_UnexpectedKind ("variable or value");
7467 Type t = ea.Expr.Type;
7468 if (t.GetArrayRank () != ea.Arguments.Count){
7470 "Incorrect number of indexes for array " +
7471 " expected: " + t.GetArrayRank () + " got: " +
7472 ea.Arguments.Count);
7476 type = TypeManager.GetElementType (t);
7477 if (type.IsPointer && !ec.InUnsafe){
7478 UnsafeError (ea.Location);
7482 foreach (Argument a in ea.Arguments){
7483 Type argtype = a.Type;
7485 if (argtype == TypeManager.int32_type ||
7486 argtype == TypeManager.uint32_type ||
7487 argtype == TypeManager.int64_type ||
7488 argtype == TypeManager.uint64_type)
7492 // Mhm. This is strage, because the Argument.Type is not the same as
7493 // Argument.Expr.Type: the value changes depending on the ref/out setting.
7495 // Wonder if I will run into trouble for this.
7497 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
7502 eclass = ExprClass.Variable;
7508 /// Emits the right opcode to load an object of Type `t'
7509 /// from an array of T
7511 static public void EmitLoadOpcode (ILGenerator ig, Type type)
7513 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7514 ig.Emit (OpCodes.Ldelem_U1);
7515 else if (type == TypeManager.sbyte_type)
7516 ig.Emit (OpCodes.Ldelem_I1);
7517 else if (type == TypeManager.short_type)
7518 ig.Emit (OpCodes.Ldelem_I2);
7519 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7520 ig.Emit (OpCodes.Ldelem_U2);
7521 else if (type == TypeManager.int32_type)
7522 ig.Emit (OpCodes.Ldelem_I4);
7523 else if (type == TypeManager.uint32_type)
7524 ig.Emit (OpCodes.Ldelem_U4);
7525 else if (type == TypeManager.uint64_type)
7526 ig.Emit (OpCodes.Ldelem_I8);
7527 else if (type == TypeManager.int64_type)
7528 ig.Emit (OpCodes.Ldelem_I8);
7529 else if (type == TypeManager.float_type)
7530 ig.Emit (OpCodes.Ldelem_R4);
7531 else if (type == TypeManager.double_type)
7532 ig.Emit (OpCodes.Ldelem_R8);
7533 else if (type == TypeManager.intptr_type)
7534 ig.Emit (OpCodes.Ldelem_I);
7535 else if (TypeManager.IsEnumType (type)){
7536 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type));
7537 } else if (type.IsValueType){
7538 ig.Emit (OpCodes.Ldelema, type);
7539 ig.Emit (OpCodes.Ldobj, type);
7541 ig.Emit (OpCodes.Ldelem_Ref);
7545 /// Returns the right opcode to store an object of Type `t'
7546 /// from an array of T.
7548 static public OpCode GetStoreOpcode (Type t, out bool is_stobj)
7550 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7552 t = TypeManager.TypeToCoreType (t);
7553 if (TypeManager.IsEnumType (t))
7554 t = TypeManager.EnumToUnderlying (t);
7555 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7556 t == TypeManager.bool_type)
7557 return OpCodes.Stelem_I1;
7558 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7559 t == TypeManager.char_type)
7560 return OpCodes.Stelem_I2;
7561 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7562 return OpCodes.Stelem_I4;
7563 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7564 return OpCodes.Stelem_I8;
7565 else if (t == TypeManager.float_type)
7566 return OpCodes.Stelem_R4;
7567 else if (t == TypeManager.double_type)
7568 return OpCodes.Stelem_R8;
7569 else if (t == TypeManager.intptr_type) {
7571 return OpCodes.Stobj;
7572 } else if (t.IsValueType) {
7574 return OpCodes.Stobj;
7576 return OpCodes.Stelem_Ref;
7579 MethodInfo FetchGetMethod ()
7581 ModuleBuilder mb = CodeGen.Module.Builder;
7582 int arg_count = ea.Arguments.Count;
7583 Type [] args = new Type [arg_count];
7586 for (int i = 0; i < arg_count; i++){
7587 //args [i++] = a.Type;
7588 args [i] = TypeManager.int32_type;
7591 get = mb.GetArrayMethod (
7592 ea.Expr.Type, "Get",
7593 CallingConventions.HasThis |
7594 CallingConventions.Standard,
7600 MethodInfo FetchAddressMethod ()
7602 ModuleBuilder mb = CodeGen.Module.Builder;
7603 int arg_count = ea.Arguments.Count;
7604 Type [] args = new Type [arg_count];
7608 ret_type = TypeManager.GetReferenceType (type);
7610 for (int i = 0; i < arg_count; i++){
7611 //args [i++] = a.Type;
7612 args [i] = TypeManager.int32_type;
7615 address = mb.GetArrayMethod (
7616 ea.Expr.Type, "Address",
7617 CallingConventions.HasThis |
7618 CallingConventions.Standard,
7625 // Load the array arguments into the stack.
7627 // If we have been requested to cache the values (cached_locations array
7628 // initialized), then load the arguments the first time and store them
7629 // in locals. otherwise load from local variables.
7631 void LoadArrayAndArguments (EmitContext ec)
7633 ILGenerator ig = ec.ig;
7636 foreach (Argument a in ea.Arguments){
7637 Type argtype = a.Expr.Type;
7641 if (argtype == TypeManager.int64_type)
7642 ig.Emit (OpCodes.Conv_Ovf_I);
7643 else if (argtype == TypeManager.uint64_type)
7644 ig.Emit (OpCodes.Conv_Ovf_I_Un);
7648 public void Emit (EmitContext ec, bool leave_copy)
7650 int rank = ea.Expr.Type.GetArrayRank ();
7651 ILGenerator ig = ec.ig;
7654 LoadArrayAndArguments (ec);
7657 EmitLoadOpcode (ig, type);
7661 method = FetchGetMethod ();
7662 ig.Emit (OpCodes.Call, method);
7665 LoadFromPtr (ec.ig, this.type);
7668 ec.ig.Emit (OpCodes.Dup);
7669 temp = new LocalTemporary (ec, this.type);
7674 public override void Emit (EmitContext ec)
7679 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7681 int rank = ea.Expr.Type.GetArrayRank ();
7682 ILGenerator ig = ec.ig;
7683 Type t = source.Type;
7684 prepared = prepare_for_load;
7686 if (prepare_for_load) {
7687 AddressOf (ec, AddressOp.LoadStore);
7688 ec.ig.Emit (OpCodes.Dup);
7691 ec.ig.Emit (OpCodes.Dup);
7692 temp = new LocalTemporary (ec, this.type);
7695 StoreFromPtr (ec.ig, t);
7703 LoadArrayAndArguments (ec);
7707 OpCode op = GetStoreOpcode (t, out is_stobj);
7709 // The stobj opcode used by value types will need
7710 // an address on the stack, not really an array/array
7714 ig.Emit (OpCodes.Ldelema, t);
7718 ec.ig.Emit (OpCodes.Dup);
7719 temp = new LocalTemporary (ec, this.type);
7724 ig.Emit (OpCodes.Stobj, t);
7728 ModuleBuilder mb = CodeGen.Module.Builder;
7729 int arg_count = ea.Arguments.Count;
7730 Type [] args = new Type [arg_count + 1];
7735 ec.ig.Emit (OpCodes.Dup);
7736 temp = new LocalTemporary (ec, this.type);
7740 for (int i = 0; i < arg_count; i++){
7741 //args [i++] = a.Type;
7742 args [i] = TypeManager.int32_type;
7745 args [arg_count] = type;
7747 set = mb.GetArrayMethod (
7748 ea.Expr.Type, "Set",
7749 CallingConventions.HasThis |
7750 CallingConventions.Standard,
7751 TypeManager.void_type, args);
7753 ig.Emit (OpCodes.Call, set);
7760 public void AddressOf (EmitContext ec, AddressOp mode)
7762 int rank = ea.Expr.Type.GetArrayRank ();
7763 ILGenerator ig = ec.ig;
7765 LoadArrayAndArguments (ec);
7768 ig.Emit (OpCodes.Ldelema, type);
7770 MethodInfo address = FetchAddressMethod ();
7771 ig.Emit (OpCodes.Call, address);
7778 public ArrayList Properties;
7779 static Hashtable map;
7781 public struct Indexer {
7782 public readonly Type Type;
7783 public readonly MethodInfo Getter, Setter;
7785 public Indexer (Type type, MethodInfo get, MethodInfo set)
7795 map = new Hashtable ();
7800 Properties = new ArrayList ();
7803 void Append (MemberInfo [] mi)
7805 foreach (PropertyInfo property in mi){
7806 MethodInfo get, set;
7808 get = property.GetGetMethod (true);
7809 set = property.GetSetMethod (true);
7810 Properties.Add (new Indexer (property.PropertyType, get, set));
7814 static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
7816 string p_name = TypeManager.IndexerPropertyName (lookup_type);
7818 MemberInfo [] mi = TypeManager.MemberLookup (
7819 caller_type, caller_type, lookup_type, MemberTypes.Property,
7820 BindingFlags.Public | BindingFlags.Instance |
7821 BindingFlags.DeclaredOnly, p_name, null);
7823 if (mi == null || mi.Length == 0)
7829 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
7831 Indexers ix = (Indexers) map [lookup_type];
7836 Type copy = lookup_type;
7837 while (copy != TypeManager.object_type && copy != null){
7838 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy);
7842 ix = new Indexers ();
7847 copy = copy.BaseType;
7850 if (!lookup_type.IsInterface)
7853 TypeExpr [] ifaces = TypeManager.GetInterfaces (lookup_type);
7854 if (ifaces != null) {
7855 foreach (TypeExpr iface in ifaces) {
7856 Type itype = iface.Type;
7857 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype);
7860 ix = new Indexers ();
7872 /// Expressions that represent an indexer call.
7874 public class IndexerAccess : Expression, IAssignMethod {
7876 // Points to our "data" repository
7878 MethodInfo get, set;
7879 ArrayList set_arguments;
7880 bool is_base_indexer;
7882 protected Type indexer_type;
7883 protected Type current_type;
7884 protected Expression instance_expr;
7885 protected ArrayList arguments;
7887 public IndexerAccess (ElementAccess ea, Location loc)
7888 : this (ea.Expr, false, loc)
7890 this.arguments = ea.Arguments;
7893 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
7896 this.instance_expr = instance_expr;
7897 this.is_base_indexer = is_base_indexer;
7898 this.eclass = ExprClass.Value;
7902 protected virtual bool CommonResolve (EmitContext ec)
7904 indexer_type = instance_expr.Type;
7905 current_type = ec.ContainerType;
7910 public override Expression DoResolve (EmitContext ec)
7912 ArrayList AllGetters = new ArrayList();
7913 if (!CommonResolve (ec))
7917 // Step 1: Query for all `Item' *properties*. Notice
7918 // that the actual methods are pointed from here.
7920 // This is a group of properties, piles of them.
7922 bool found_any = false, found_any_getters = false;
7923 Type lookup_type = indexer_type;
7926 ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
7927 if (ilist != null) {
7929 if (ilist.Properties != null) {
7930 foreach (Indexers.Indexer ix in ilist.Properties) {
7931 if (ix.Getter != null)
7932 AllGetters.Add(ix.Getter);
7937 if (AllGetters.Count > 0) {
7938 found_any_getters = true;
7939 get = (MethodInfo) Invocation.OverloadResolve (
7940 ec, new MethodGroupExpr (AllGetters, loc), arguments, loc);
7944 Report.Error (21, loc,
7945 "Type `" + TypeManager.CSharpName (indexer_type) +
7946 "' does not have any indexers defined");
7950 if (!found_any_getters) {
7951 Error (154, "indexer can not be used in this context, because " +
7952 "it lacks a `get' accessor");
7957 Error (1501, "No Overload for method `this' takes `" +
7958 arguments.Count + "' arguments");
7963 // Only base will allow this invocation to happen.
7965 if (get.IsAbstract && this is BaseIndexerAccess){
7966 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (get));
7970 type = get.ReturnType;
7971 if (type.IsPointer && !ec.InUnsafe){
7976 eclass = ExprClass.IndexerAccess;
7980 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7982 ArrayList AllSetters = new ArrayList();
7983 if (!CommonResolve (ec))
7986 bool found_any = false, found_any_setters = false;
7988 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
7989 if (ilist != null) {
7991 if (ilist.Properties != null) {
7992 foreach (Indexers.Indexer ix in ilist.Properties) {
7993 if (ix.Setter != null)
7994 AllSetters.Add(ix.Setter);
7998 if (AllSetters.Count > 0) {
7999 found_any_setters = true;
8000 set_arguments = (ArrayList) arguments.Clone ();
8001 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
8002 set = (MethodInfo) Invocation.OverloadResolve (
8003 ec, new MethodGroupExpr (AllSetters, loc),
8004 set_arguments, loc);
8008 Report.Error (21, loc,
8009 "Type `" + TypeManager.CSharpName (indexer_type) +
8010 "' does not have any indexers defined");
8014 if (!found_any_setters) {
8015 Error (154, "indexer can not be used in this context, because " +
8016 "it lacks a `set' accessor");
8021 Error (1501, "No Overload for method `this' takes `" +
8022 arguments.Count + "' arguments");
8027 // Only base will allow this invocation to happen.
8029 if (set.IsAbstract && this is BaseIndexerAccess){
8030 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set));
8035 // Now look for the actual match in the list of indexers to set our "return" type
8037 type = TypeManager.void_type; // default value
8038 foreach (Indexers.Indexer ix in ilist.Properties){
8039 if (ix.Setter == set){
8045 eclass = ExprClass.IndexerAccess;
8049 bool prepared = false;
8050 LocalTemporary temp;
8052 public void Emit (EmitContext ec, bool leave_copy)
8054 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
8056 ec.ig.Emit (OpCodes.Dup);
8057 temp = new LocalTemporary (ec, Type);
8063 // source is ignored, because we already have a copy of it from the
8064 // LValue resolution and we have already constructed a pre-cached
8065 // version of the arguments (ea.set_arguments);
8067 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8069 prepared = prepare_for_load;
8070 Argument a = (Argument) set_arguments [set_arguments.Count - 1];
8075 ec.ig.Emit (OpCodes.Dup);
8076 temp = new LocalTemporary (ec, Type);
8079 } else if (leave_copy) {
8080 temp = new LocalTemporary (ec, Type);
8086 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
8093 public override void Emit (EmitContext ec)
8100 /// The base operator for method names
8102 public class BaseAccess : Expression {
8105 public BaseAccess (string member, Location l)
8107 this.member = member;
8111 public override Expression DoResolve (EmitContext ec)
8113 Expression c = CommonResolve (ec);
8119 // MethodGroups use this opportunity to flag an error on lacking ()
8121 if (!(c is MethodGroupExpr))
8122 return c.Resolve (ec);
8126 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8128 Expression c = CommonResolve (ec);
8134 // MethodGroups use this opportunity to flag an error on lacking ()
8136 if (! (c is MethodGroupExpr))
8137 return c.DoResolveLValue (ec, right_side);
8142 Expression CommonResolve (EmitContext ec)
8144 Expression member_lookup;
8145 Type current_type = ec.ContainerType;
8146 Type base_type = current_type.BaseType;
8150 Error (1511, "Keyword base is not allowed in static method");
8154 if (ec.IsFieldInitializer){
8155 Error (1512, "Keyword base is not available in the current context");
8159 member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member,
8160 AllMemberTypes, AllBindingFlags, loc);
8161 if (member_lookup == null) {
8162 MemberLookupFailed (ec, base_type, base_type, member, null, loc);
8169 left = new TypeExpression (base_type, loc);
8171 left = ec.GetThis (loc);
8173 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
8175 if (e is PropertyExpr){
8176 PropertyExpr pe = (PropertyExpr) e;
8181 if (e is MethodGroupExpr)
8182 ((MethodGroupExpr) e).IsBase = true;
8187 public override void Emit (EmitContext ec)
8189 throw new Exception ("Should never be called");
8194 /// The base indexer operator
8196 public class BaseIndexerAccess : IndexerAccess {
8197 public BaseIndexerAccess (ArrayList args, Location loc)
8198 : base (null, true, loc)
8200 arguments = new ArrayList ();
8201 foreach (Expression tmp in args)
8202 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8205 protected override bool CommonResolve (EmitContext ec)
8207 instance_expr = ec.GetThis (loc);
8209 current_type = ec.ContainerType.BaseType;
8210 indexer_type = current_type;
8212 foreach (Argument a in arguments){
8213 if (!a.Resolve (ec, loc))
8222 /// This class exists solely to pass the Type around and to be a dummy
8223 /// that can be passed to the conversion functions (this is used by
8224 /// foreach implementation to typecast the object return value from
8225 /// get_Current into the proper type. All code has been generated and
8226 /// we only care about the side effect conversions to be performed
8228 /// This is also now used as a placeholder where a no-action expression
8229 /// is needed (the `New' class).
8231 public class EmptyExpression : Expression {
8232 public EmptyExpression ()
8234 type = TypeManager.object_type;
8235 eclass = ExprClass.Value;
8236 loc = Location.Null;
8239 public EmptyExpression (Type t)
8242 eclass = ExprClass.Value;
8243 loc = Location.Null;
8246 public override Expression DoResolve (EmitContext ec)
8251 public override void Emit (EmitContext ec)
8253 // nothing, as we only exist to not do anything.
8257 // This is just because we might want to reuse this bad boy
8258 // instead of creating gazillions of EmptyExpressions.
8259 // (CanImplicitConversion uses it)
8261 public void SetType (Type t)
8267 public class UserCast : Expression {
8271 public UserCast (MethodInfo method, Expression source, Location l)
8273 this.method = method;
8274 this.source = source;
8275 type = method.ReturnType;
8276 eclass = ExprClass.Value;
8280 public override Expression DoResolve (EmitContext ec)
8283 // We are born fully resolved
8288 public override void Emit (EmitContext ec)
8290 ILGenerator ig = ec.ig;
8294 if (method is MethodInfo)
8295 ig.Emit (OpCodes.Call, (MethodInfo) method);
8297 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8303 // This class is used to "construct" the type during a typecast
8304 // operation. Since the Type.GetType class in .NET can parse
8305 // the type specification, we just use this to construct the type
8306 // one bit at a time.
8308 public class ComposedCast : TypeExpr {
8312 public ComposedCast (Expression left, string dim, Location l)
8319 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
8321 Type ltype = ec.DeclSpace.ResolveType (left, false, loc);
8325 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8326 Report.Error (1547, Location,
8327 "Keyword 'void' cannot be used in this context");
8332 // ltype.Fullname is already fully qualified, so we can skip
8333 // a lot of probes, and go directly to TypeManager.LookupType
8335 string cname = ltype.FullName + dim;
8336 type = TypeManager.LookupTypeDirect (cname);
8339 // For arrays of enumerations we are having a problem
8340 // with the direct lookup. Need to investigate.
8342 // For now, fall back to the full lookup in that case.
8344 type = RootContext.LookupType (
8345 ec.DeclSpace, cname, false, loc);
8351 if (!ec.ResolvingTypeTree){
8353 // If the above flag is set, this is being invoked from the ResolveType function.
8354 // Upper layers take care of the type validity in this context.
8356 if (!ec.InUnsafe && type.IsPointer){
8362 eclass = ExprClass.Type;
8366 public override string Name {
8374 // This class is used to represent the address of an array, used
8375 // only by the Fixed statement, this is like the C "&a [0]" construct.
8377 public class ArrayPtr : Expression {
8380 public ArrayPtr (Expression array, Location l)
8382 Type array_type = TypeManager.GetElementType (array.Type);
8386 type = TypeManager.GetPointerType (array_type);
8387 eclass = ExprClass.Value;
8391 public override void Emit (EmitContext ec)
8393 ILGenerator ig = ec.ig;
8396 IntLiteral.EmitInt (ig, 0);
8397 ig.Emit (OpCodes.Ldelema, TypeManager.GetElementType (array.Type));
8400 public override Expression DoResolve (EmitContext ec)
8403 // We are born fully resolved
8410 // Used by the fixed statement
8412 public class StringPtr : Expression {
8415 public StringPtr (LocalBuilder b, Location l)
8418 eclass = ExprClass.Value;
8419 type = TypeManager.char_ptr_type;
8423 public override Expression DoResolve (EmitContext ec)
8425 // This should never be invoked, we are born in fully
8426 // initialized state.
8431 public override void Emit (EmitContext ec)
8433 ILGenerator ig = ec.ig;
8435 ig.Emit (OpCodes.Ldloc, b);
8436 ig.Emit (OpCodes.Conv_I);
8437 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8438 ig.Emit (OpCodes.Add);
8443 // Implements the `stackalloc' keyword
8445 public class StackAlloc : Expression {
8450 public StackAlloc (Expression type, Expression count, Location l)
8457 public override Expression DoResolve (EmitContext ec)
8459 count = count.Resolve (ec);
8463 if (count.Type != TypeManager.int32_type){
8464 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8469 Constant c = count as Constant;
8470 // TODO: because we don't have property IsNegative
8471 if (c != null && c.ConvertToUInt () == null) {
8472 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8476 if (ec.CurrentBranching.InCatch () ||
8477 ec.CurrentBranching.InFinally (true)) {
8479 "stackalloc can not be used in a catch or finally block");
8483 otype = ec.DeclSpace.ResolveType (t, false, loc);
8488 if (!TypeManager.VerifyUnManaged (otype, loc))
8491 type = TypeManager.GetPointerType (otype);
8492 eclass = ExprClass.Value;
8497 public override void Emit (EmitContext ec)
8499 int size = GetTypeSize (otype);
8500 ILGenerator ig = ec.ig;
8503 ig.Emit (OpCodes.Sizeof, otype);
8505 IntConstant.EmitInt (ig, size);
8507 ig.Emit (OpCodes.Mul);
8508 ig.Emit (OpCodes.Localloc);