2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, 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);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public Expression 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);
85 public class ParenthesizedExpression : Expression
87 public Expression Expr;
89 public ParenthesizedExpression (Expression expr, Location loc)
95 public override Expression DoResolve (EmitContext ec)
97 Expr = Expr.Resolve (ec);
101 public override void Emit (EmitContext ec)
103 throw new Exception ("Should not happen");
108 /// Unary expressions.
112 /// Unary implements unary expressions. It derives from
113 /// ExpressionStatement becuase the pre/post increment/decrement
114 /// operators can be used in a statement context.
116 public class Unary : Expression {
117 public enum Operator : byte {
118 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
119 Indirection, AddressOf, TOP
122 public Operator Oper;
123 public Expression Expr;
125 public Unary (Operator op, Expression expr, Location loc)
133 /// Returns a stringified representation of the Operator
135 static public string OperName (Operator oper)
138 case Operator.UnaryPlus:
140 case Operator.UnaryNegation:
142 case Operator.LogicalNot:
144 case Operator.OnesComplement:
146 case Operator.AddressOf:
148 case Operator.Indirection:
152 return oper.ToString ();
155 public static readonly string [] oper_names;
159 oper_names = new string [(int)Operator.TOP];
161 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
162 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
163 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
164 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
165 oper_names [(int) Operator.Indirection] = "op_Indirection";
166 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
169 void Error23 (Type t)
172 23, "Operator " + OperName (Oper) +
173 " cannot be applied to operand of type `" +
174 TypeManager.CSharpName (t) + "'");
178 /// The result has been already resolved:
180 /// FIXME: a minus constant -128 sbyte cant be turned into a
183 static Expression TryReduceNegative (Constant expr)
187 if (expr is IntConstant)
188 e = new IntConstant (-((IntConstant) expr).Value);
189 else if (expr is UIntConstant){
190 uint value = ((UIntConstant) expr).Value;
192 if (value < 2147483649)
193 return new IntConstant (-(int)value);
195 e = new LongConstant (-value);
197 else if (expr is LongConstant)
198 e = new LongConstant (-((LongConstant) expr).Value);
199 else if (expr is ULongConstant){
200 ulong value = ((ULongConstant) expr).Value;
202 if (value < 9223372036854775809)
203 return new LongConstant(-(long)value);
205 else if (expr is FloatConstant)
206 e = new FloatConstant (-((FloatConstant) expr).Value);
207 else if (expr is DoubleConstant)
208 e = new DoubleConstant (-((DoubleConstant) expr).Value);
209 else if (expr is DecimalConstant)
210 e = new DecimalConstant (-((DecimalConstant) expr).Value);
211 else if (expr is ShortConstant)
212 e = new IntConstant (-((ShortConstant) expr).Value);
213 else if (expr is UShortConstant)
214 e = new IntConstant (-((UShortConstant) expr).Value);
219 // This routine will attempt to simplify the unary expression when the
220 // argument is a constant. The result is returned in `result' and the
221 // function returns true or false depending on whether a reduction
222 // was performed or not
224 bool Reduce (EmitContext ec, Constant e, out Expression result)
226 Type expr_type = e.Type;
229 case Operator.UnaryPlus:
233 case Operator.UnaryNegation:
234 result = TryReduceNegative (e);
237 case Operator.LogicalNot:
238 if (expr_type != TypeManager.bool_type) {
244 BoolConstant b = (BoolConstant) e;
245 result = new BoolConstant (!(b.Value));
248 case Operator.OnesComplement:
249 if (!((expr_type == TypeManager.int32_type) ||
250 (expr_type == TypeManager.uint32_type) ||
251 (expr_type == TypeManager.int64_type) ||
252 (expr_type == TypeManager.uint64_type) ||
253 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
256 if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
257 result = new Cast (new TypeExpression (TypeManager.int32_type, loc), e, loc);
258 result = result.Resolve (ec);
259 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
260 result = new Cast (new TypeExpression (TypeManager.uint32_type, loc), e, loc);
261 result = result.Resolve (ec);
262 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
263 result = new Cast (new TypeExpression (TypeManager.int64_type, loc), e, loc);
264 result = result.Resolve (ec);
265 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
266 result = new Cast (new TypeExpression (TypeManager.uint64_type, loc), e, loc);
267 result = result.Resolve (ec);
270 if (result == null || !(result is Constant)){
276 expr_type = result.Type;
277 e = (Constant) result;
280 if (e is EnumConstant){
281 EnumConstant enum_constant = (EnumConstant) e;
284 if (Reduce (ec, enum_constant.Child, out reduced)){
285 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
293 if (expr_type == TypeManager.int32_type){
294 result = new IntConstant (~ ((IntConstant) e).Value);
295 } else if (expr_type == TypeManager.uint32_type){
296 result = new UIntConstant (~ ((UIntConstant) e).Value);
297 } else if (expr_type == TypeManager.int64_type){
298 result = new LongConstant (~ ((LongConstant) e).Value);
299 } else if (expr_type == TypeManager.uint64_type){
300 result = new ULongConstant (~ ((ULongConstant) e).Value);
308 case Operator.AddressOf:
312 case Operator.Indirection:
316 throw new Exception ("Can not constant fold: " + Oper.ToString());
319 Expression ResolveOperator (EmitContext ec)
321 Type expr_type = Expr.Type;
324 // Step 1: Perform Operator Overload location
329 op_name = oper_names [(int) Oper];
331 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
334 Expression e = StaticCallExpr.MakeSimpleCall (
335 ec, (MethodGroupExpr) mg, Expr, loc);
345 // Only perform numeric promotions on:
348 if (expr_type == null)
352 // Step 2: Default operations on CLI native types.
355 // Attempt to use a constant folding operation.
356 if (Expr is Constant){
359 if (Reduce (ec, (Constant) Expr, out result))
364 case Operator.LogicalNot:
365 if (expr_type != TypeManager.bool_type) {
366 Expr = ResolveBoolean (ec, Expr, loc);
373 type = TypeManager.bool_type;
376 case Operator.OnesComplement:
377 if (!((expr_type == TypeManager.int32_type) ||
378 (expr_type == TypeManager.uint32_type) ||
379 (expr_type == TypeManager.int64_type) ||
380 (expr_type == TypeManager.uint64_type) ||
381 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
384 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
386 type = TypeManager.int32_type;
389 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
391 type = TypeManager.uint32_type;
394 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
396 type = TypeManager.int64_type;
399 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
401 type = TypeManager.uint64_type;
410 case Operator.AddressOf:
411 if (Expr.eclass != ExprClass.Variable){
412 Error (211, "Cannot take the address of non-variables");
421 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
425 IVariable variable = Expr as IVariable;
426 if (!ec.InFixedInitializer && ((variable == null) || !variable.VerifyFixed (false))) {
427 Error (212, "You can only take the address of an unfixed expression inside " +
428 "of a fixed statement initializer");
432 // According to the specs, a variable is considered definitely assigned if you take
434 if ((variable != null) && (variable.VariableInfo != null))
435 variable.VariableInfo.SetAssigned (ec);
437 type = TypeManager.GetPointerType (Expr.Type);
440 case Operator.Indirection:
446 if (!expr_type.IsPointer){
447 Error (193, "The * or -> operator can only be applied to pointers");
452 // We create an Indirection expression, because
453 // it can implement the IMemoryLocation.
455 return new Indirection (Expr, loc);
457 case Operator.UnaryPlus:
459 // A plus in front of something is just a no-op, so return the child.
463 case Operator.UnaryNegation:
465 // Deals with -literals
466 // int operator- (int x)
467 // long operator- (long x)
468 // float operator- (float f)
469 // double operator- (double d)
470 // decimal operator- (decimal d)
472 Expression expr = null;
475 // transform - - expr into expr
478 Unary unary = (Unary) Expr;
480 if (unary.Oper == Operator.UnaryNegation)
485 // perform numeric promotions to int,
489 // The following is inneficient, because we call
490 // ImplicitConversion too many times.
492 // It is also not clear if we should convert to Float
493 // or Double initially.
495 if (expr_type == TypeManager.uint32_type){
497 // FIXME: handle exception to this rule that
498 // permits the int value -2147483648 (-2^31) to
499 // bt wrote as a decimal interger literal
501 type = TypeManager.int64_type;
502 Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
506 if (expr_type == TypeManager.uint64_type){
508 // FIXME: Handle exception of `long value'
509 // -92233720368547758087 (-2^63) to be wrote as
510 // decimal integer literal.
516 if (expr_type == TypeManager.float_type){
521 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
528 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
535 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
546 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
547 TypeManager.CSharpName (expr_type) + "'");
551 public override Expression DoResolve (EmitContext ec)
553 if (Oper == Operator.AddressOf)
554 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
556 Expr = Expr.Resolve (ec);
561 eclass = ExprClass.Value;
562 return ResolveOperator (ec);
565 public override void Emit (EmitContext ec)
567 ILGenerator ig = ec.ig;
570 case Operator.UnaryPlus:
571 throw new Exception ("This should be caught by Resolve");
573 case Operator.UnaryNegation:
575 ig.Emit (OpCodes.Ldc_I4_0);
576 if (type == TypeManager.int64_type)
577 ig.Emit (OpCodes.Conv_U8);
579 ig.Emit (OpCodes.Sub_Ovf);
582 ig.Emit (OpCodes.Neg);
587 case Operator.LogicalNot:
589 ig.Emit (OpCodes.Ldc_I4_0);
590 ig.Emit (OpCodes.Ceq);
593 case Operator.OnesComplement:
595 ig.Emit (OpCodes.Not);
598 case Operator.AddressOf:
599 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
603 throw new Exception ("This should not happen: Operator = "
608 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
610 if (Oper == Operator.LogicalNot)
611 Expr.EmitBranchable (ec, target, !onTrue);
613 base.EmitBranchable (ec, target, onTrue);
616 public override string ToString ()
618 return "Unary (" + Oper + ", " + Expr + ")";
624 // Unary operators are turned into Indirection expressions
625 // after semantic analysis (this is so we can take the address
626 // of an indirection).
628 public class Indirection : Expression, IMemoryLocation, IAssignMethod {
630 LocalTemporary temporary;
633 public Indirection (Expression expr, Location l)
636 this.type = TypeManager.GetElementType (expr.Type);
637 eclass = ExprClass.Variable;
641 void LoadExprValue (EmitContext ec)
645 public override void Emit (EmitContext ec)
647 ILGenerator ig = ec.ig;
649 if (temporary != null){
650 if (have_temporary) {
654 ec.ig.Emit (OpCodes.Dup);
655 temporary.Store (ec);
656 have_temporary = true;
661 LoadFromPtr (ig, Type);
664 public void EmitAssign (EmitContext ec, Expression source)
666 if (temporary != null){
671 ec.ig.Emit (OpCodes.Dup);
672 temporary.Store (ec);
673 have_temporary = true;
679 StoreFromPtr (ec.ig, type);
682 public void AddressOf (EmitContext ec, AddressOp Mode)
684 if (temporary != null){
690 ec.ig.Emit (OpCodes.Dup);
691 temporary.Store (ec);
692 have_temporary = true;
697 public override Expression DoResolve (EmitContext ec)
700 // Born fully resolved
705 public new void CacheTemporaries (EmitContext ec)
707 temporary = new LocalTemporary (ec, expr.Type);
710 public override string ToString ()
712 return "*(" + expr + ")";
717 /// Unary Mutator expressions (pre and post ++ and --)
721 /// UnaryMutator implements ++ and -- expressions. It derives from
722 /// ExpressionStatement becuase the pre/post increment/decrement
723 /// operators can be used in a statement context.
725 /// FIXME: Idea, we could split this up in two classes, one simpler
726 /// for the common case, and one with the extra fields for more complex
727 /// classes (indexers require temporary access; overloaded require method)
730 public class UnaryMutator : ExpressionStatement {
732 public enum Mode : byte {
739 PreDecrement = IsDecrement,
740 PostIncrement = IsPost,
741 PostDecrement = IsPost | IsDecrement
746 LocalTemporary temp_storage;
749 // This is expensive for the simplest case.
753 public UnaryMutator (Mode m, Expression e, Location l)
760 static string OperName (Mode mode)
762 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
766 void Error23 (Type t)
769 23, "Operator " + OperName (mode) +
770 " cannot be applied to operand of type `" +
771 TypeManager.CSharpName (t) + "'");
775 /// Returns whether an object of type `t' can be incremented
776 /// or decremented with add/sub (ie, basically whether we can
777 /// use pre-post incr-decr operations on it, but it is not a
778 /// System.Decimal, which we require operator overloading to catch)
780 static bool IsIncrementableNumber (Type t)
782 return (t == TypeManager.sbyte_type) ||
783 (t == TypeManager.byte_type) ||
784 (t == TypeManager.short_type) ||
785 (t == TypeManager.ushort_type) ||
786 (t == TypeManager.int32_type) ||
787 (t == TypeManager.uint32_type) ||
788 (t == TypeManager.int64_type) ||
789 (t == TypeManager.uint64_type) ||
790 (t == TypeManager.char_type) ||
791 (t.IsSubclassOf (TypeManager.enum_type)) ||
792 (t == TypeManager.float_type) ||
793 (t == TypeManager.double_type) ||
794 (t.IsPointer && t != TypeManager.void_ptr_type);
797 Expression ResolveOperator (EmitContext ec)
799 Type expr_type = expr.Type;
802 // Step 1: Perform Operator Overload location
807 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
808 op_name = "op_Increment";
810 op_name = "op_Decrement";
812 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
814 if (mg == null && expr_type.BaseType != null)
815 mg = MemberLookup (ec, expr_type.BaseType, op_name,
816 MemberTypes.Method, AllBindingFlags, loc);
819 method = StaticCallExpr.MakeSimpleCall (
820 ec, (MethodGroupExpr) mg, expr, loc);
827 // The operand of the prefix/postfix increment decrement operators
828 // should be an expression that is classified as a variable,
829 // a property access or an indexer access
832 if (expr.eclass == ExprClass.Variable){
833 LocalVariableReference var = expr as LocalVariableReference;
834 if ((var != null) && var.IsReadOnly)
835 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
836 if (IsIncrementableNumber (expr_type) ||
837 expr_type == TypeManager.decimal_type){
840 } else if (expr.eclass == ExprClass.IndexerAccess){
841 IndexerAccess ia = (IndexerAccess) expr;
843 temp_storage = new LocalTemporary (ec, expr.Type);
845 expr = ia.ResolveLValue (ec, temp_storage);
850 } else if (expr.eclass == ExprClass.PropertyAccess){
851 PropertyExpr pe = (PropertyExpr) expr;
853 if (pe.VerifyAssignable ())
858 expr.Error_UnexpectedKind ("variable, indexer or property access");
862 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
863 TypeManager.CSharpName (expr_type) + "'");
867 public override Expression DoResolve (EmitContext ec)
869 expr = expr.Resolve (ec);
874 eclass = ExprClass.Value;
875 return ResolveOperator (ec);
878 static int PtrTypeSize (Type t)
880 return GetTypeSize (TypeManager.GetElementType (t));
884 // Loads the proper "1" into the stack based on the type, then it emits the
885 // opcode for the operation requested
887 void LoadOneAndEmitOp (EmitContext ec, Type t)
890 // Measure if getting the typecode and using that is more/less efficient
891 // that comparing types. t.GetTypeCode() is an internal call.
893 ILGenerator ig = ec.ig;
895 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
896 LongConstant.EmitLong (ig, 1);
897 else if (t == TypeManager.double_type)
898 ig.Emit (OpCodes.Ldc_R8, 1.0);
899 else if (t == TypeManager.float_type)
900 ig.Emit (OpCodes.Ldc_R4, 1.0F);
901 else if (t.IsPointer){
902 int n = PtrTypeSize (t);
905 ig.Emit (OpCodes.Sizeof, t);
907 IntConstant.EmitInt (ig, n);
909 ig.Emit (OpCodes.Ldc_I4_1);
912 // Now emit the operation
915 if (t == TypeManager.int32_type ||
916 t == TypeManager.int64_type){
917 if ((mode & Mode.IsDecrement) != 0)
918 ig.Emit (OpCodes.Sub_Ovf);
920 ig.Emit (OpCodes.Add_Ovf);
921 } else if (t == TypeManager.uint32_type ||
922 t == TypeManager.uint64_type){
923 if ((mode & Mode.IsDecrement) != 0)
924 ig.Emit (OpCodes.Sub_Ovf_Un);
926 ig.Emit (OpCodes.Add_Ovf_Un);
928 if ((mode & Mode.IsDecrement) != 0)
929 ig.Emit (OpCodes.Sub_Ovf);
931 ig.Emit (OpCodes.Add_Ovf);
934 if ((mode & Mode.IsDecrement) != 0)
935 ig.Emit (OpCodes.Sub);
937 ig.Emit (OpCodes.Add);
940 if (t == TypeManager.sbyte_type){
942 ig.Emit (OpCodes.Conv_Ovf_I1);
944 ig.Emit (OpCodes.Conv_I1);
945 } else if (t == TypeManager.byte_type){
947 ig.Emit (OpCodes.Conv_Ovf_U1);
949 ig.Emit (OpCodes.Conv_U1);
950 } else if (t == TypeManager.short_type){
952 ig.Emit (OpCodes.Conv_Ovf_I2);
954 ig.Emit (OpCodes.Conv_I2);
955 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
957 ig.Emit (OpCodes.Conv_Ovf_U2);
959 ig.Emit (OpCodes.Conv_U2);
964 static EmptyExpression empty_expr;
966 void EmitCode (EmitContext ec, bool is_expr)
968 ILGenerator ig = ec.ig;
969 IAssignMethod ia = (IAssignMethod) expr;
970 Type expr_type = expr.Type;
972 ia.CacheTemporaries (ec);
974 if (temp_storage == null){
976 // Temporary improvement: if we are dealing with something that does
977 // not require complicated instance setup, avoid using a temporary
979 // For now: only localvariables when not remapped
982 if (method == null &&
983 ((expr is LocalVariableReference) ||(expr is FieldExpr && ((FieldExpr) expr).FieldInfo.IsStatic))){
984 if (empty_expr == null)
985 empty_expr = new EmptyExpression ();
988 case Mode.PreIncrement:
989 case Mode.PreDecrement:
992 LoadOneAndEmitOp (ec, expr_type);
994 ig.Emit (OpCodes.Dup);
995 ia.EmitAssign (ec, empty_expr);
998 case Mode.PostIncrement:
999 case Mode.PostDecrement:
1002 ig.Emit (OpCodes.Dup);
1004 LoadOneAndEmitOp (ec, expr_type);
1005 ia.EmitAssign (ec, empty_expr);
1010 temp_storage = new LocalTemporary (ec, expr_type);
1014 case Mode.PreIncrement:
1015 case Mode.PreDecrement:
1016 if (method == null){
1019 LoadOneAndEmitOp (ec, expr_type);
1023 temp_storage.Store (ec);
1024 ia.EmitAssign (ec, temp_storage);
1026 temp_storage.Emit (ec);
1029 case Mode.PostIncrement:
1030 case Mode.PostDecrement:
1034 if (method == null){
1038 ig.Emit (OpCodes.Dup);
1040 LoadOneAndEmitOp (ec, expr_type);
1045 temp_storage.Store (ec);
1046 ia.EmitAssign (ec, temp_storage);
1050 temp_storage.Release (ec);
1053 public override void Emit (EmitContext ec)
1055 EmitCode (ec, true);
1059 public override void EmitStatement (EmitContext ec)
1061 EmitCode (ec, false);
1067 /// Base class for the `Is' and `As' classes.
1071 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1074 public abstract class Probe : Expression {
1075 public readonly Expression ProbeType;
1076 protected Expression expr;
1077 protected Type probe_type;
1079 public Probe (Expression expr, Expression probe_type, Location l)
1081 ProbeType = probe_type;
1086 public Expression Expr {
1092 public override Expression DoResolve (EmitContext ec)
1094 probe_type = ec.DeclSpace.ResolveType (ProbeType, false, loc);
1096 if (probe_type == null)
1099 expr = expr.Resolve (ec);
1108 /// Implementation of the `is' operator.
1110 public class Is : Probe {
1111 public Is (Expression expr, Expression probe_type, Location l)
1112 : base (expr, probe_type, l)
1117 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1122 public override void Emit (EmitContext ec)
1124 ILGenerator ig = ec.ig;
1129 case Action.AlwaysFalse:
1130 ig.Emit (OpCodes.Pop);
1131 IntConstant.EmitInt (ig, 0);
1133 case Action.AlwaysTrue:
1134 ig.Emit (OpCodes.Pop);
1135 IntConstant.EmitInt (ig, 1);
1137 case Action.LeaveOnStack:
1138 // the `e != null' rule.
1139 ig.Emit (OpCodes.Ldnull);
1140 ig.Emit (OpCodes.Ceq);
1141 ig.Emit (OpCodes.Ldc_I4_0);
1142 ig.Emit (OpCodes.Ceq);
1145 ig.Emit (OpCodes.Isinst, probe_type);
1146 ig.Emit (OpCodes.Ldnull);
1147 ig.Emit (OpCodes.Cgt_Un);
1150 throw new Exception ("never reached");
1153 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
1155 ILGenerator ig = ec.ig;
1158 case Action.AlwaysFalse:
1160 ig.Emit (OpCodes.Br, target);
1163 case Action.AlwaysTrue:
1165 ig.Emit (OpCodes.Br, target);
1168 case Action.LeaveOnStack:
1169 // the `e != null' rule.
1171 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1175 ig.Emit (OpCodes.Isinst, probe_type);
1176 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1179 throw new Exception ("never reached");
1182 public override Expression DoResolve (EmitContext ec)
1184 Expression e = base.DoResolve (ec);
1186 if ((e == null) || (expr == null))
1189 Type etype = expr.Type;
1190 bool warning_always_matches = false;
1191 bool warning_never_matches = false;
1193 type = TypeManager.bool_type;
1194 eclass = ExprClass.Value;
1197 // First case, if at compile time, there is an implicit conversion
1198 // then e != null (objects) or true (value types)
1200 e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
1203 if (etype.IsValueType)
1204 action = Action.AlwaysTrue;
1206 action = Action.LeaveOnStack;
1208 warning_always_matches = true;
1209 } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1211 // Second case: explicit reference convresion
1213 if (expr is NullLiteral)
1214 action = Action.AlwaysFalse;
1216 action = Action.Probe;
1218 action = Action.AlwaysFalse;
1219 warning_never_matches = true;
1222 if (RootContext.WarningLevel >= 1){
1223 if (warning_always_matches)
1224 Warning (183, "The expression is always of type `" +
1225 TypeManager.CSharpName (probe_type) + "'");
1226 else if (warning_never_matches){
1227 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1229 "The expression is never of type `" +
1230 TypeManager.CSharpName (probe_type) + "'");
1239 /// Implementation of the `as' operator.
1241 public class As : Probe {
1242 public As (Expression expr, Expression probe_type, Location l)
1243 : base (expr, probe_type, l)
1247 bool do_isinst = false;
1249 public override void Emit (EmitContext ec)
1251 ILGenerator ig = ec.ig;
1256 ig.Emit (OpCodes.Isinst, probe_type);
1259 static void Error_CannotConvertType (Type source, Type target, Location loc)
1262 39, loc, "as operator can not convert from `" +
1263 TypeManager.CSharpName (source) + "' to `" +
1264 TypeManager.CSharpName (target) + "'");
1267 public override Expression DoResolve (EmitContext ec)
1269 Expression e = base.DoResolve (ec);
1275 eclass = ExprClass.Value;
1276 Type etype = expr.Type;
1278 if (TypeManager.IsValueType (probe_type)){
1279 Report.Error (77, loc, "The as operator should be used with a reference type only (" +
1280 TypeManager.CSharpName (probe_type) + " is a value type)");
1285 e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
1292 if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1297 Error_CannotConvertType (etype, probe_type, loc);
1303 /// This represents a typecast in the source language.
1305 /// FIXME: Cast expressions have an unusual set of parsing
1306 /// rules, we need to figure those out.
1308 public class Cast : Expression {
1309 Expression target_type;
1312 public Cast (Expression cast_type, Expression expr, Location loc)
1314 this.target_type = cast_type;
1319 public Expression TargetType {
1325 public Expression Expr {
1334 bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
1336 if (!ec.ConstantCheckState)
1339 if ((value < min) || (value > max)) {
1340 Error (221, "Constant value `" + value + "' cannot be converted " +
1341 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1342 "syntax to override)");
1349 bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
1351 if (!ec.ConstantCheckState)
1355 Error (221, "Constant value `" + value + "' cannot be converted " +
1356 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1357 "syntax to override)");
1364 bool CheckUnsigned (EmitContext ec, long value, Type type)
1366 if (!ec.ConstantCheckState)
1370 Error (221, "Constant value `" + value + "' cannot be converted " +
1371 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1372 "syntax to override)");
1380 /// Attempts to do a compile-time folding of a constant cast.
1382 Expression TryReduce (EmitContext ec, Type target_type)
1384 Expression real_expr = expr;
1385 if (real_expr is EnumConstant)
1386 real_expr = ((EnumConstant) real_expr).Child;
1388 if (real_expr is ByteConstant){
1389 byte v = ((ByteConstant) real_expr).Value;
1391 if (target_type == TypeManager.sbyte_type) {
1392 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1394 return new SByteConstant ((sbyte) v);
1396 if (target_type == TypeManager.short_type)
1397 return new ShortConstant ((short) v);
1398 if (target_type == TypeManager.ushort_type)
1399 return new UShortConstant ((ushort) v);
1400 if (target_type == TypeManager.int32_type)
1401 return new IntConstant ((int) v);
1402 if (target_type == TypeManager.uint32_type)
1403 return new UIntConstant ((uint) v);
1404 if (target_type == TypeManager.int64_type)
1405 return new LongConstant ((long) v);
1406 if (target_type == TypeManager.uint64_type)
1407 return new ULongConstant ((ulong) v);
1408 if (target_type == TypeManager.float_type)
1409 return new FloatConstant ((float) v);
1410 if (target_type == TypeManager.double_type)
1411 return new DoubleConstant ((double) v);
1412 if (target_type == TypeManager.char_type)
1413 return new CharConstant ((char) v);
1414 if (target_type == TypeManager.decimal_type)
1415 return new DecimalConstant ((decimal) v);
1417 if (real_expr is SByteConstant){
1418 sbyte v = ((SByteConstant) real_expr).Value;
1420 if (target_type == TypeManager.byte_type) {
1421 if (!CheckUnsigned (ec, v, target_type))
1423 return new ByteConstant ((byte) v);
1425 if (target_type == TypeManager.short_type)
1426 return new ShortConstant ((short) v);
1427 if (target_type == TypeManager.ushort_type) {
1428 if (!CheckUnsigned (ec, v, target_type))
1430 return new UShortConstant ((ushort) v);
1431 } if (target_type == TypeManager.int32_type)
1432 return new IntConstant ((int) v);
1433 if (target_type == TypeManager.uint32_type) {
1434 if (!CheckUnsigned (ec, v, target_type))
1436 return new UIntConstant ((uint) v);
1437 } if (target_type == TypeManager.int64_type)
1438 return new LongConstant ((long) v);
1439 if (target_type == TypeManager.uint64_type) {
1440 if (!CheckUnsigned (ec, v, target_type))
1442 return new ULongConstant ((ulong) v);
1444 if (target_type == TypeManager.float_type)
1445 return new FloatConstant ((float) v);
1446 if (target_type == TypeManager.double_type)
1447 return new DoubleConstant ((double) v);
1448 if (target_type == TypeManager.char_type) {
1449 if (!CheckUnsigned (ec, v, target_type))
1451 return new CharConstant ((char) v);
1453 if (target_type == TypeManager.decimal_type)
1454 return new DecimalConstant ((decimal) v);
1456 if (real_expr is ShortConstant){
1457 short v = ((ShortConstant) real_expr).Value;
1459 if (target_type == TypeManager.byte_type) {
1460 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1462 return new ByteConstant ((byte) v);
1464 if (target_type == TypeManager.sbyte_type) {
1465 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1467 return new SByteConstant ((sbyte) v);
1469 if (target_type == TypeManager.ushort_type) {
1470 if (!CheckUnsigned (ec, v, target_type))
1472 return new UShortConstant ((ushort) v);
1474 if (target_type == TypeManager.int32_type)
1475 return new IntConstant ((int) v);
1476 if (target_type == TypeManager.uint32_type) {
1477 if (!CheckUnsigned (ec, v, target_type))
1479 return new UIntConstant ((uint) v);
1481 if (target_type == TypeManager.int64_type)
1482 return new LongConstant ((long) v);
1483 if (target_type == TypeManager.uint64_type) {
1484 if (!CheckUnsigned (ec, v, target_type))
1486 return new ULongConstant ((ulong) v);
1488 if (target_type == TypeManager.float_type)
1489 return new FloatConstant ((float) v);
1490 if (target_type == TypeManager.double_type)
1491 return new DoubleConstant ((double) v);
1492 if (target_type == TypeManager.char_type) {
1493 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1495 return new CharConstant ((char) v);
1497 if (target_type == TypeManager.decimal_type)
1498 return new DecimalConstant ((decimal) v);
1500 if (real_expr is UShortConstant){
1501 ushort v = ((UShortConstant) real_expr).Value;
1503 if (target_type == TypeManager.byte_type) {
1504 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1506 return new ByteConstant ((byte) v);
1508 if (target_type == TypeManager.sbyte_type) {
1509 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1511 return new SByteConstant ((sbyte) v);
1513 if (target_type == TypeManager.short_type) {
1514 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1516 return new ShortConstant ((short) v);
1518 if (target_type == TypeManager.int32_type)
1519 return new IntConstant ((int) v);
1520 if (target_type == TypeManager.uint32_type)
1521 return new UIntConstant ((uint) v);
1522 if (target_type == TypeManager.int64_type)
1523 return new LongConstant ((long) v);
1524 if (target_type == TypeManager.uint64_type)
1525 return new ULongConstant ((ulong) v);
1526 if (target_type == TypeManager.float_type)
1527 return new FloatConstant ((float) v);
1528 if (target_type == TypeManager.double_type)
1529 return new DoubleConstant ((double) v);
1530 if (target_type == TypeManager.char_type) {
1531 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1533 return new CharConstant ((char) v);
1535 if (target_type == TypeManager.decimal_type)
1536 return new DecimalConstant ((decimal) v);
1538 if (real_expr is IntConstant){
1539 int v = ((IntConstant) real_expr).Value;
1541 if (target_type == TypeManager.byte_type) {
1542 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1544 return new ByteConstant ((byte) v);
1546 if (target_type == TypeManager.sbyte_type) {
1547 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1549 return new SByteConstant ((sbyte) v);
1551 if (target_type == TypeManager.short_type) {
1552 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1554 return new ShortConstant ((short) v);
1556 if (target_type == TypeManager.ushort_type) {
1557 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1559 return new UShortConstant ((ushort) v);
1561 if (target_type == TypeManager.uint32_type) {
1562 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1564 return new UIntConstant ((uint) v);
1566 if (target_type == TypeManager.int64_type)
1567 return new LongConstant ((long) v);
1568 if (target_type == TypeManager.uint64_type) {
1569 if (!CheckUnsigned (ec, v, target_type))
1571 return new ULongConstant ((ulong) v);
1573 if (target_type == TypeManager.float_type)
1574 return new FloatConstant ((float) v);
1575 if (target_type == TypeManager.double_type)
1576 return new DoubleConstant ((double) v);
1577 if (target_type == TypeManager.char_type) {
1578 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1580 return new CharConstant ((char) v);
1582 if (target_type == TypeManager.decimal_type)
1583 return new DecimalConstant ((decimal) v);
1585 if (real_expr is UIntConstant){
1586 uint v = ((UIntConstant) real_expr).Value;
1588 if (target_type == TypeManager.byte_type) {
1589 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1591 return new ByteConstant ((byte) v);
1593 if (target_type == TypeManager.sbyte_type) {
1594 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1596 return new SByteConstant ((sbyte) v);
1598 if (target_type == TypeManager.short_type) {
1599 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1601 return new ShortConstant ((short) v);
1603 if (target_type == TypeManager.ushort_type) {
1604 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1606 return new UShortConstant ((ushort) v);
1608 if (target_type == TypeManager.int32_type) {
1609 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1611 return new IntConstant ((int) v);
1613 if (target_type == TypeManager.int64_type)
1614 return new LongConstant ((long) v);
1615 if (target_type == TypeManager.uint64_type)
1616 return new ULongConstant ((ulong) v);
1617 if (target_type == TypeManager.float_type)
1618 return new FloatConstant ((float) v);
1619 if (target_type == TypeManager.double_type)
1620 return new DoubleConstant ((double) v);
1621 if (target_type == TypeManager.char_type) {
1622 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1624 return new CharConstant ((char) v);
1626 if (target_type == TypeManager.decimal_type)
1627 return new DecimalConstant ((decimal) v);
1629 if (real_expr is LongConstant){
1630 long v = ((LongConstant) real_expr).Value;
1632 if (target_type == TypeManager.byte_type) {
1633 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1635 return new ByteConstant ((byte) v);
1637 if (target_type == TypeManager.sbyte_type) {
1638 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1640 return new SByteConstant ((sbyte) v);
1642 if (target_type == TypeManager.short_type) {
1643 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1645 return new ShortConstant ((short) v);
1647 if (target_type == TypeManager.ushort_type) {
1648 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1650 return new UShortConstant ((ushort) v);
1652 if (target_type == TypeManager.int32_type) {
1653 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1655 return new IntConstant ((int) v);
1657 if (target_type == TypeManager.uint32_type) {
1658 if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
1660 return new UIntConstant ((uint) v);
1662 if (target_type == TypeManager.uint64_type) {
1663 if (!CheckUnsigned (ec, v, target_type))
1665 return new ULongConstant ((ulong) v);
1667 if (target_type == TypeManager.float_type)
1668 return new FloatConstant ((float) v);
1669 if (target_type == TypeManager.double_type)
1670 return new DoubleConstant ((double) v);
1671 if (target_type == TypeManager.char_type) {
1672 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1674 return new CharConstant ((char) v);
1676 if (target_type == TypeManager.decimal_type)
1677 return new DecimalConstant ((decimal) v);
1679 if (real_expr is ULongConstant){
1680 ulong v = ((ULongConstant) real_expr).Value;
1682 if (target_type == TypeManager.byte_type) {
1683 if (!CheckRange (ec, v, target_type, Byte.MaxValue))
1685 return new ByteConstant ((byte) v);
1687 if (target_type == TypeManager.sbyte_type) {
1688 if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
1690 return new SByteConstant ((sbyte) v);
1692 if (target_type == TypeManager.short_type) {
1693 if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
1695 return new ShortConstant ((short) v);
1697 if (target_type == TypeManager.ushort_type) {
1698 if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
1700 return new UShortConstant ((ushort) v);
1702 if (target_type == TypeManager.int32_type) {
1703 if (!CheckRange (ec, v, target_type, Int32.MaxValue))
1705 return new IntConstant ((int) v);
1707 if (target_type == TypeManager.uint32_type) {
1708 if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
1710 return new UIntConstant ((uint) v);
1712 if (target_type == TypeManager.int64_type) {
1713 if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
1715 return new LongConstant ((long) v);
1717 if (target_type == TypeManager.float_type)
1718 return new FloatConstant ((float) v);
1719 if (target_type == TypeManager.double_type)
1720 return new DoubleConstant ((double) v);
1721 if (target_type == TypeManager.char_type) {
1722 if (!CheckRange (ec, v, target_type, Char.MaxValue))
1724 return new CharConstant ((char) v);
1726 if (target_type == TypeManager.decimal_type)
1727 return new DecimalConstant ((decimal) v);
1729 if (real_expr is FloatConstant){
1730 float v = ((FloatConstant) real_expr).Value;
1732 if (target_type == TypeManager.byte_type)
1733 return new ByteConstant ((byte) v);
1734 if (target_type == TypeManager.sbyte_type)
1735 return new SByteConstant ((sbyte) v);
1736 if (target_type == TypeManager.short_type)
1737 return new ShortConstant ((short) v);
1738 if (target_type == TypeManager.ushort_type)
1739 return new UShortConstant ((ushort) v);
1740 if (target_type == TypeManager.int32_type)
1741 return new IntConstant ((int) v);
1742 if (target_type == TypeManager.uint32_type)
1743 return new UIntConstant ((uint) v);
1744 if (target_type == TypeManager.int64_type)
1745 return new LongConstant ((long) v);
1746 if (target_type == TypeManager.uint64_type)
1747 return new ULongConstant ((ulong) v);
1748 if (target_type == TypeManager.double_type)
1749 return new DoubleConstant ((double) v);
1750 if (target_type == TypeManager.char_type)
1751 return new CharConstant ((char) v);
1752 if (target_type == TypeManager.decimal_type)
1753 return new DecimalConstant ((decimal) v);
1755 if (real_expr is DoubleConstant){
1756 double v = ((DoubleConstant) real_expr).Value;
1758 if (target_type == TypeManager.byte_type){
1759 return new ByteConstant ((byte) v);
1760 } if (target_type == TypeManager.sbyte_type)
1761 return new SByteConstant ((sbyte) v);
1762 if (target_type == TypeManager.short_type)
1763 return new ShortConstant ((short) v);
1764 if (target_type == TypeManager.ushort_type)
1765 return new UShortConstant ((ushort) v);
1766 if (target_type == TypeManager.int32_type)
1767 return new IntConstant ((int) v);
1768 if (target_type == TypeManager.uint32_type)
1769 return new UIntConstant ((uint) v);
1770 if (target_type == TypeManager.int64_type)
1771 return new LongConstant ((long) v);
1772 if (target_type == TypeManager.uint64_type)
1773 return new ULongConstant ((ulong) v);
1774 if (target_type == TypeManager.float_type)
1775 return new FloatConstant ((float) v);
1776 if (target_type == TypeManager.char_type)
1777 return new CharConstant ((char) v);
1778 if (target_type == TypeManager.decimal_type)
1779 return new DecimalConstant ((decimal) v);
1782 if (real_expr is CharConstant){
1783 char v = ((CharConstant) real_expr).Value;
1785 if (target_type == TypeManager.byte_type) {
1786 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1788 return new ByteConstant ((byte) v);
1790 if (target_type == TypeManager.sbyte_type) {
1791 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1793 return new SByteConstant ((sbyte) v);
1795 if (target_type == TypeManager.short_type) {
1796 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1798 return new ShortConstant ((short) v);
1800 if (target_type == TypeManager.int32_type)
1801 return new IntConstant ((int) v);
1802 if (target_type == TypeManager.uint32_type)
1803 return new UIntConstant ((uint) v);
1804 if (target_type == TypeManager.int64_type)
1805 return new LongConstant ((long) v);
1806 if (target_type == TypeManager.uint64_type)
1807 return new ULongConstant ((ulong) v);
1808 if (target_type == TypeManager.float_type)
1809 return new FloatConstant ((float) v);
1810 if (target_type == TypeManager.double_type)
1811 return new DoubleConstant ((double) v);
1812 if (target_type == TypeManager.char_type) {
1813 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1815 return new CharConstant ((char) v);
1817 if (target_type == TypeManager.decimal_type)
1818 return new DecimalConstant ((decimal) v);
1824 public override Expression DoResolve (EmitContext ec)
1826 expr = expr.Resolve (ec);
1830 type = ec.DeclSpace.ResolveType (target_type, false, Location);
1835 eclass = ExprClass.Value;
1837 if (expr is Constant){
1838 Expression e = TryReduce (ec, type);
1844 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1848 public override void Emit (EmitContext ec)
1851 // This one will never happen
1853 throw new Exception ("Should not happen");
1858 /// Binary operators
1860 public class Binary : Expression {
1861 public enum Operator : byte {
1862 Multiply, Division, Modulus,
1863 Addition, Subtraction,
1864 LeftShift, RightShift,
1865 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1866 Equality, Inequality,
1876 Expression left, right;
1878 // This must be kept in sync with Operator!!!
1879 public static readonly string [] oper_names;
1883 oper_names = new string [(int) Operator.TOP];
1885 oper_names [(int) Operator.Multiply] = "op_Multiply";
1886 oper_names [(int) Operator.Division] = "op_Division";
1887 oper_names [(int) Operator.Modulus] = "op_Modulus";
1888 oper_names [(int) Operator.Addition] = "op_Addition";
1889 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1890 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1891 oper_names [(int) Operator.RightShift] = "op_RightShift";
1892 oper_names [(int) Operator.LessThan] = "op_LessThan";
1893 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1894 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1895 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1896 oper_names [(int) Operator.Equality] = "op_Equality";
1897 oper_names [(int) Operator.Inequality] = "op_Inequality";
1898 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1899 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1900 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1901 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1902 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1905 public Binary (Operator oper, Expression left, Expression right, Location loc)
1913 public Operator Oper {
1922 public Expression Left {
1931 public Expression Right {
1942 /// Returns a stringified representation of the Operator
1944 static string OperName (Operator oper)
1947 case Operator.Multiply:
1949 case Operator.Division:
1951 case Operator.Modulus:
1953 case Operator.Addition:
1955 case Operator.Subtraction:
1957 case Operator.LeftShift:
1959 case Operator.RightShift:
1961 case Operator.LessThan:
1963 case Operator.GreaterThan:
1965 case Operator.LessThanOrEqual:
1967 case Operator.GreaterThanOrEqual:
1969 case Operator.Equality:
1971 case Operator.Inequality:
1973 case Operator.BitwiseAnd:
1975 case Operator.BitwiseOr:
1977 case Operator.ExclusiveOr:
1979 case Operator.LogicalOr:
1981 case Operator.LogicalAnd:
1985 return oper.ToString ();
1988 public override string ToString ()
1990 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1991 right.ToString () + ")";
1994 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1996 if (expr.Type == target_type)
1999 return Convert.ImplicitConversion (ec, expr, target_type, loc);
2002 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
2005 34, loc, "Operator `" + OperName (oper)
2006 + "' is ambiguous on operands of type `"
2007 + TypeManager.CSharpName (l) + "' "
2008 + "and `" + TypeManager.CSharpName (r)
2012 bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions)
2014 if ((l == t) || (r == t))
2017 if (!check_user_conversions)
2020 if (Convert.ImplicitUserConversionExists (ec, l, t))
2022 else if (Convert.ImplicitUserConversionExists (ec, r, t))
2029 // Note that handling the case l == Decimal || r == Decimal
2030 // is taken care of by the Step 1 Operator Overload resolution.
2032 // If `check_user_conv' is true, we also check whether a user-defined conversion
2033 // exists. Note that we only need to do this if both arguments are of a user-defined
2034 // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
2035 // so we don't explicitly check for performance reasons.
2037 bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
2039 if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
2041 // If either operand is of type double, the other operand is
2042 // conveted to type double.
2044 if (r != TypeManager.double_type)
2045 right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
2046 if (l != TypeManager.double_type)
2047 left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
2049 type = TypeManager.double_type;
2050 } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
2052 // if either operand is of type float, the other operand is
2053 // converted to type float.
2055 if (r != TypeManager.double_type)
2056 right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
2057 if (l != TypeManager.double_type)
2058 left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
2059 type = TypeManager.float_type;
2060 } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
2064 // If either operand is of type ulong, the other operand is
2065 // converted to type ulong. or an error ocurrs if the other
2066 // operand is of type sbyte, short, int or long
2068 if (l == TypeManager.uint64_type){
2069 if (r != TypeManager.uint64_type){
2070 if (right is IntConstant){
2071 IntConstant ic = (IntConstant) right;
2073 e = Convert.TryImplicitIntConversion (l, ic);
2076 } else if (right is LongConstant){
2077 long ll = ((LongConstant) right).Value;
2080 right = new ULongConstant ((ulong) ll);
2082 e = Convert.ImplicitNumericConversion (ec, right, l, loc);
2089 if (left is IntConstant){
2090 e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
2093 } else if (left is LongConstant){
2094 long ll = ((LongConstant) left).Value;
2097 left = new ULongConstant ((ulong) ll);
2099 e = Convert.ImplicitNumericConversion (ec, left, r, loc);
2106 if ((other == TypeManager.sbyte_type) ||
2107 (other == TypeManager.short_type) ||
2108 (other == TypeManager.int32_type) ||
2109 (other == TypeManager.int64_type))
2110 Error_OperatorAmbiguous (loc, oper, l, r);
2111 type = TypeManager.uint64_type;
2112 } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
2114 // If either operand is of type long, the other operand is converted
2117 if (l != TypeManager.int64_type)
2118 left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
2119 if (r != TypeManager.int64_type)
2120 right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
2122 type = TypeManager.int64_type;
2123 } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
2125 // If either operand is of type uint, and the other
2126 // operand is of type sbyte, short or int, othe operands are
2127 // converted to type long (unless we have an int constant).
2131 if (l == TypeManager.uint32_type){
2132 if (right is IntConstant){
2133 IntConstant ic = (IntConstant) right;
2137 right = new UIntConstant ((uint) val);
2144 } else if (r == TypeManager.uint32_type){
2145 if (left is IntConstant){
2146 IntConstant ic = (IntConstant) left;
2150 left = new UIntConstant ((uint) val);
2159 if ((other == TypeManager.sbyte_type) ||
2160 (other == TypeManager.short_type) ||
2161 (other == TypeManager.int32_type)){
2162 left = ForceConversion (ec, left, TypeManager.int64_type);
2163 right = ForceConversion (ec, right, TypeManager.int64_type);
2164 type = TypeManager.int64_type;
2167 // if either operand is of type uint, the other
2168 // operand is converd to type uint
2170 left = ForceConversion (ec, left, TypeManager.uint32_type);
2171 right = ForceConversion (ec, right, TypeManager.uint32_type);
2172 type = TypeManager.uint32_type;
2174 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2175 if (l != TypeManager.decimal_type)
2176 left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
2178 if (r != TypeManager.decimal_type)
2179 right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
2180 type = TypeManager.decimal_type;
2182 left = ForceConversion (ec, left, TypeManager.int32_type);
2183 right = ForceConversion (ec, right, TypeManager.int32_type);
2185 type = TypeManager.int32_type;
2188 return (left != null) && (right != null);
2191 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2193 Report.Error (19, loc,
2194 "Operator " + name + " cannot be applied to operands of type `" +
2195 TypeManager.CSharpName (l) + "' and `" +
2196 TypeManager.CSharpName (r) + "'");
2199 void Error_OperatorCannotBeApplied ()
2201 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
2204 static bool is_32_or_64 (Type t)
2206 return (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
2207 t == TypeManager.int64_type || t == TypeManager.uint64_type);
2210 static bool is_unsigned (Type t)
2212 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2213 t == TypeManager.short_type || t == TypeManager.byte_type);
2216 static bool is_user_defined (Type t)
2218 if (t.IsSubclassOf (TypeManager.value_type) &&
2219 (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type))
2225 Expression CheckShiftArguments (EmitContext ec)
2229 e = ForceConversion (ec, right, TypeManager.int32_type);
2231 Error_OperatorCannotBeApplied ();
2236 if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
2237 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
2238 ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
2239 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
2243 if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
2244 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
2245 right = right.DoResolve (ec);
2247 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
2248 right = right.DoResolve (ec);
2253 Error_OperatorCannotBeApplied ();
2257 Expression ResolveOperator (EmitContext ec)
2260 Type r = right.Type;
2262 bool overload_failed = false;
2265 // Special cases: string comapred to null
2267 if (oper == Operator.Equality || oper == Operator.Inequality){
2268 if ((l == TypeManager.string_type && (right is NullLiteral)) ||
2269 (r == TypeManager.string_type && (left is NullLiteral))){
2270 Type = TypeManager.bool_type;
2277 // Do not perform operator overload resolution when both sides are
2280 if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){
2282 // Step 1: Perform Operator Overload location
2284 Expression left_expr, right_expr;
2286 string op = oper_names [(int) oper];
2288 MethodGroupExpr union;
2289 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
2291 right_expr = MemberLookup (
2292 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
2293 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
2295 union = (MethodGroupExpr) left_expr;
2297 if (union != null) {
2298 ArrayList args = new ArrayList (2);
2299 args.Add (new Argument (left, Argument.AType.Expression));
2300 args.Add (new Argument (right, Argument.AType.Expression));
2302 MethodBase method = Invocation.OverloadResolve (ec, union, args, Location.Null);
2303 if (method != null) {
2304 MethodInfo mi = (MethodInfo) method;
2306 return new BinaryMethod (mi.ReturnType, method, args);
2308 overload_failed = true;
2314 // Step 0: String concatenation (because overloading will get this wrong)
2316 if (oper == Operator.Addition){
2318 // If any of the arguments is a string, cast to string
2321 if (l == TypeManager.string_type){
2324 if (r == TypeManager.void_type) {
2325 Error_OperatorCannotBeApplied ();
2329 if (r == TypeManager.string_type){
2330 if (left is Constant && right is Constant){
2331 StringConstant ls = (StringConstant) left;
2332 StringConstant rs = (StringConstant) right;
2334 return new StringConstant (
2335 ls.Value + rs.Value);
2338 if (left is BinaryMethod){
2339 BinaryMethod b = (BinaryMethod) left;
2342 // Call String.Concat (string, string, string) or
2343 // String.Concat (string, string, string, string)
2346 if (b.method == TypeManager.string_concat_string_string ||
2347 b.method == TypeManager.string_concat_string_string_string){
2348 int count = b.Arguments.Count;
2351 ArrayList bargs = new ArrayList (3);
2352 bargs.AddRange (b.Arguments);
2353 bargs.Add (new Argument (right, Argument.AType.Expression));
2354 return new BinaryMethod (
2355 TypeManager.string_type,
2356 TypeManager.string_concat_string_string_string, bargs);
2357 } else if (count == 3){
2358 ArrayList bargs = new ArrayList (4);
2359 bargs.AddRange (b.Arguments);
2360 bargs.Add (new Argument (right, Argument.AType.Expression));
2361 return new BinaryMethod (
2362 TypeManager.string_type,
2363 TypeManager.string_concat_string_string_string_string, bargs);
2369 method = TypeManager.string_concat_string_string;
2372 method = TypeManager.string_concat_object_object;
2373 right = Convert.ImplicitConversion (
2374 ec, right, TypeManager.object_type, loc);
2376 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2382 // Cascading concats will hold up to 2 arguments, any extras will be
2383 // reallocated above.
2385 ArrayList args = new ArrayList (2);
2386 args.Add (new Argument (left, Argument.AType.Expression));
2387 args.Add (new Argument (right, Argument.AType.Expression));
2389 return new BinaryMethod (TypeManager.string_type, method, args);
2390 } else if (r == TypeManager.string_type){
2393 if (l == TypeManager.void_type) {
2394 Error_OperatorCannotBeApplied ();
2398 left = Convert.ImplicitConversion (ec, left, TypeManager.object_type, loc);
2400 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2403 ArrayList args = new ArrayList (2);
2404 args.Add (new Argument (left, Argument.AType.Expression));
2405 args.Add (new Argument (right, Argument.AType.Expression));
2407 return new BinaryMethod (TypeManager.string_type, TypeManager.string_concat_object_object, args);
2411 // Transform a + ( - b) into a - b
2413 if (right is Unary){
2414 Unary right_unary = (Unary) right;
2416 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2417 oper = Operator.Subtraction;
2418 right = right_unary.Expr;
2424 if (oper == Operator.Equality || oper == Operator.Inequality){
2425 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2426 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2427 Error_OperatorCannotBeApplied ();
2431 type = TypeManager.bool_type;
2436 // operator != (object a, object b)
2437 // operator == (object a, object b)
2439 // For this to be used, both arguments have to be reference-types.
2440 // Read the rationale on the spec (14.9.6)
2442 // Also, if at compile time we know that the classes do not inherit
2443 // one from the other, then we catch the error there.
2445 if (!(l.IsValueType || r.IsValueType)){
2446 type = TypeManager.bool_type;
2451 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
2455 // Also, a standard conversion must exist from either one
2457 if (!(Convert.ImplicitStandardConversionExists (left, r) ||
2458 Convert.ImplicitStandardConversionExists (right, l))){
2459 Error_OperatorCannotBeApplied ();
2463 // We are going to have to convert to an object to compare
2465 if (l != TypeManager.object_type)
2466 left = new EmptyCast (left, TypeManager.object_type);
2467 if (r != TypeManager.object_type)
2468 right = new EmptyCast (right, TypeManager.object_type);
2471 // FIXME: CSC here catches errors cs254 and cs252
2477 // One of them is a valuetype, but the other one is not.
2479 if (!l.IsValueType || !r.IsValueType) {
2480 Error_OperatorCannotBeApplied ();
2485 // Only perform numeric promotions on:
2486 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2488 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2489 if (l.IsSubclassOf (TypeManager.delegate_type)){
2490 if (right.eclass == ExprClass.MethodGroup && RootContext.V2){
2491 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2498 if (r.IsSubclassOf (TypeManager.delegate_type)){
2500 ArrayList args = new ArrayList (2);
2502 args = new ArrayList (2);
2503 args.Add (new Argument (left, Argument.AType.Expression));
2504 args.Add (new Argument (right, Argument.AType.Expression));
2506 if (oper == Operator.Addition)
2507 method = TypeManager.delegate_combine_delegate_delegate;
2509 method = TypeManager.delegate_remove_delegate_delegate;
2512 Error_OperatorCannotBeApplied ();
2516 return new BinaryDelegate (l, method, args);
2521 // Pointer arithmetic:
2523 // T* operator + (T* x, int y);
2524 // T* operator + (T* x, uint y);
2525 // T* operator + (T* x, long y);
2526 // T* operator + (T* x, ulong y);
2528 // T* operator + (int y, T* x);
2529 // T* operator + (uint y, T *x);
2530 // T* operator + (long y, T *x);
2531 // T* operator + (ulong y, T *x);
2533 // T* operator - (T* x, int y);
2534 // T* operator - (T* x, uint y);
2535 // T* operator - (T* x, long y);
2536 // T* operator - (T* x, ulong y);
2538 // long operator - (T* x, T *y)
2541 if (r.IsPointer && oper == Operator.Subtraction){
2543 return new PointerArithmetic (
2544 false, left, right, TypeManager.int64_type,
2546 } else if (is_32_or_64 (r))
2547 return new PointerArithmetic (
2548 oper == Operator.Addition, left, right, l, loc);
2549 } else if (r.IsPointer && is_32_or_64 (l) && oper == Operator.Addition)
2550 return new PointerArithmetic (
2551 true, right, left, r, loc);
2555 // Enumeration operators
2557 bool lie = TypeManager.IsEnumType (l);
2558 bool rie = TypeManager.IsEnumType (r);
2562 // U operator - (E e, E f)
2564 if (oper == Operator.Subtraction){
2566 type = TypeManager.EnumToUnderlying (l);
2569 Error_OperatorCannotBeApplied ();
2575 // operator + (E e, U x)
2576 // operator - (E e, U x)
2578 if (oper == Operator.Addition || oper == Operator.Subtraction){
2579 Type enum_type = lie ? l : r;
2580 Type other_type = lie ? r : l;
2581 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2583 if (underlying_type != other_type){
2584 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2594 Error_OperatorCannotBeApplied ();
2603 temp = Convert.ImplicitConversion (ec, right, l, loc);
2607 Error_OperatorCannotBeApplied ();
2611 temp = Convert.ImplicitConversion (ec, left, r, loc);
2616 Error_OperatorCannotBeApplied ();
2621 if (oper == Operator.Equality || oper == Operator.Inequality ||
2622 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2623 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2624 if (left.Type != right.Type){
2625 Error_OperatorCannotBeApplied ();
2628 type = TypeManager.bool_type;
2632 if (oper == Operator.BitwiseAnd ||
2633 oper == Operator.BitwiseOr ||
2634 oper == Operator.ExclusiveOr){
2638 Error_OperatorCannotBeApplied ();
2642 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2643 return CheckShiftArguments (ec);
2645 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2646 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2647 type = TypeManager.bool_type;
2652 Error_OperatorCannotBeApplied ();
2656 Expression e = new ConditionalLogicalOperator (
2657 oper == Operator.LogicalAnd, left, right, l, loc);
2658 return e.Resolve (ec);
2662 // operator & (bool x, bool y)
2663 // operator | (bool x, bool y)
2664 // operator ^ (bool x, bool y)
2666 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2667 if (oper == Operator.BitwiseAnd ||
2668 oper == Operator.BitwiseOr ||
2669 oper == Operator.ExclusiveOr){
2676 // Pointer comparison
2678 if (l.IsPointer && r.IsPointer){
2679 if (oper == Operator.Equality || oper == Operator.Inequality ||
2680 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2681 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2682 type = TypeManager.bool_type;
2688 // We are dealing with numbers
2690 if (overload_failed){
2691 Error_OperatorCannotBeApplied ();
2696 // This will leave left or right set to null if there is an error
2698 bool check_user_conv = is_user_defined (l) && is_user_defined (r);
2699 DoNumericPromotions (ec, l, r, check_user_conv);
2700 if (left == null || right == null){
2701 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2706 // reload our cached types if required
2711 if (oper == Operator.BitwiseAnd ||
2712 oper == Operator.BitwiseOr ||
2713 oper == Operator.ExclusiveOr){
2715 if (!((l == TypeManager.int32_type) ||
2716 (l == TypeManager.uint32_type) ||
2717 (l == TypeManager.short_type) ||
2718 (l == TypeManager.ushort_type) ||
2719 (l == TypeManager.int64_type) ||
2720 (l == TypeManager.uint64_type))){
2724 Error_OperatorCannotBeApplied ();
2729 if (oper == Operator.Equality ||
2730 oper == Operator.Inequality ||
2731 oper == Operator.LessThanOrEqual ||
2732 oper == Operator.LessThan ||
2733 oper == Operator.GreaterThanOrEqual ||
2734 oper == Operator.GreaterThan){
2735 type = TypeManager.bool_type;
2741 public override Expression DoResolve (EmitContext ec)
2743 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2744 left = ((ParenthesizedExpression) left).Expr;
2745 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2749 if (left.eclass == ExprClass.Type) {
2750 Error (75, "Casting a negative value needs to have the value in parentheses.");
2754 left = left.Resolve (ec);
2755 right = right.Resolve (ec);
2757 if (left == null || right == null)
2760 eclass = ExprClass.Value;
2762 Constant rc = right as Constant;
2763 Constant lc = left as Constant;
2765 if (rc != null & lc != null){
2766 Expression e = ConstantFold.BinaryFold (
2767 ec, oper, lc, rc, loc);
2772 return ResolveOperator (ec);
2776 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2777 /// context of a conditional bool expression. This function will return
2778 /// false if it is was possible to use EmitBranchable, or true if it was.
2780 /// The expression's code is generated, and we will generate a branch to `target'
2781 /// if the resulting expression value is equal to isTrue
2783 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
2785 ILGenerator ig = ec.ig;
2788 // This is more complicated than it looks, but its just to avoid
2789 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2790 // but on top of that we want for == and != to use a special path
2791 // if we are comparing against null
2793 if (oper == Operator.Equality || oper == Operator.Inequality) {
2794 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2796 if (left is NullLiteral || left is IntConstant && ((IntConstant) left).Value == 0) {
2799 ig.Emit (OpCodes.Brtrue, target);
2801 ig.Emit (OpCodes.Brfalse, target);
2804 } else if (right is NullLiteral || right is IntConstant && ((IntConstant) right).Value == 0){
2807 ig.Emit (OpCodes.Brtrue, target);
2809 ig.Emit (OpCodes.Brfalse, target);
2812 } else if (left is BoolConstant){
2814 if (my_on_true != ((BoolConstant) left).Value)
2815 ig.Emit (OpCodes.Brtrue, target);
2817 ig.Emit (OpCodes.Brfalse, target);
2820 } else if (right is BoolConstant){
2822 if (my_on_true != ((BoolConstant) right).Value)
2823 ig.Emit (OpCodes.Brtrue, target);
2825 ig.Emit (OpCodes.Brfalse, target);
2830 } else if (oper == Operator.LogicalAnd) {
2833 Label tests_end = ig.DefineLabel ();
2835 left.EmitBranchable (ec, tests_end, false);
2836 right.EmitBranchable (ec, target, true);
2837 ig.MarkLabel (tests_end);
2839 left.EmitBranchable (ec, target, false);
2840 right.EmitBranchable (ec, target, false);
2845 } else if (oper == Operator.LogicalOr){
2847 left.EmitBranchable (ec, target, true);
2848 right.EmitBranchable (ec, target, true);
2851 Label tests_end = ig.DefineLabel ();
2852 left.EmitBranchable (ec, tests_end, true);
2853 right.EmitBranchable (ec, target, false);
2854 ig.MarkLabel (tests_end);
2859 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2860 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2861 oper == Operator.Equality || oper == Operator.Inequality)) {
2862 base.EmitBranchable (ec, target, onTrue);
2870 bool isUnsigned = is_unsigned (t);
2873 case Operator.Equality:
2875 ig.Emit (OpCodes.Beq, target);
2877 ig.Emit (OpCodes.Bne_Un, target);
2880 case Operator.Inequality:
2882 ig.Emit (OpCodes.Bne_Un, target);
2884 ig.Emit (OpCodes.Beq, target);
2887 case Operator.LessThan:
2890 ig.Emit (OpCodes.Blt_Un, target);
2892 ig.Emit (OpCodes.Blt, target);
2895 ig.Emit (OpCodes.Bge_Un, target);
2897 ig.Emit (OpCodes.Bge, target);
2900 case Operator.GreaterThan:
2903 ig.Emit (OpCodes.Bgt_Un, target);
2905 ig.Emit (OpCodes.Bgt, target);
2908 ig.Emit (OpCodes.Ble_Un, target);
2910 ig.Emit (OpCodes.Ble, target);
2913 case Operator.LessThanOrEqual:
2914 if (t == TypeManager.double_type || t == TypeManager.float_type)
2919 ig.Emit (OpCodes.Ble_Un, target);
2921 ig.Emit (OpCodes.Ble, target);
2924 ig.Emit (OpCodes.Bgt_Un, target);
2926 ig.Emit (OpCodes.Bgt, target);
2930 case Operator.GreaterThanOrEqual:
2931 if (t == TypeManager.double_type || t == TypeManager.float_type)
2935 ig.Emit (OpCodes.Bge_Un, target);
2937 ig.Emit (OpCodes.Bge, target);
2940 ig.Emit (OpCodes.Blt_Un, target);
2942 ig.Emit (OpCodes.Blt, target);
2945 Console.WriteLine (oper);
2946 throw new Exception ("what is THAT");
2950 public override void Emit (EmitContext ec)
2952 ILGenerator ig = ec.ig;
2957 // Handle short-circuit operators differently
2960 if (oper == Operator.LogicalAnd) {
2961 Label load_zero = ig.DefineLabel ();
2962 Label end = ig.DefineLabel ();
2964 left.EmitBranchable (ec, load_zero, false);
2966 ig.Emit (OpCodes.Br, end);
2968 ig.MarkLabel (load_zero);
2969 ig.Emit (OpCodes.Ldc_I4_0);
2972 } else if (oper == Operator.LogicalOr) {
2973 Label load_one = ig.DefineLabel ();
2974 Label end = ig.DefineLabel ();
2976 left.EmitBranchable (ec, load_one, true);
2978 ig.Emit (OpCodes.Br, end);
2980 ig.MarkLabel (load_one);
2981 ig.Emit (OpCodes.Ldc_I4_1);
2989 bool isUnsigned = is_unsigned (left.Type);
2992 case Operator.Multiply:
2994 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2995 opcode = OpCodes.Mul_Ovf;
2996 else if (isUnsigned)
2997 opcode = OpCodes.Mul_Ovf_Un;
2999 opcode = OpCodes.Mul;
3001 opcode = OpCodes.Mul;
3005 case Operator.Division:
3007 opcode = OpCodes.Div_Un;
3009 opcode = OpCodes.Div;
3012 case Operator.Modulus:
3014 opcode = OpCodes.Rem_Un;
3016 opcode = OpCodes.Rem;
3019 case Operator.Addition:
3021 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3022 opcode = OpCodes.Add_Ovf;
3023 else if (isUnsigned)
3024 opcode = OpCodes.Add_Ovf_Un;
3026 opcode = OpCodes.Add;
3028 opcode = OpCodes.Add;
3031 case Operator.Subtraction:
3033 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3034 opcode = OpCodes.Sub_Ovf;
3035 else if (isUnsigned)
3036 opcode = OpCodes.Sub_Ovf_Un;
3038 opcode = OpCodes.Sub;
3040 opcode = OpCodes.Sub;
3043 case Operator.RightShift:
3045 opcode = OpCodes.Shr_Un;
3047 opcode = OpCodes.Shr;
3050 case Operator.LeftShift:
3051 opcode = OpCodes.Shl;
3054 case Operator.Equality:
3055 opcode = OpCodes.Ceq;
3058 case Operator.Inequality:
3059 ig.Emit (OpCodes.Ceq);
3060 ig.Emit (OpCodes.Ldc_I4_0);
3062 opcode = OpCodes.Ceq;
3065 case Operator.LessThan:
3067 opcode = OpCodes.Clt_Un;
3069 opcode = OpCodes.Clt;
3072 case Operator.GreaterThan:
3074 opcode = OpCodes.Cgt_Un;
3076 opcode = OpCodes.Cgt;
3079 case Operator.LessThanOrEqual:
3080 Type lt = left.Type;
3082 if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
3083 ig.Emit (OpCodes.Cgt_Un);
3085 ig.Emit (OpCodes.Cgt);
3086 ig.Emit (OpCodes.Ldc_I4_0);
3088 opcode = OpCodes.Ceq;
3091 case Operator.GreaterThanOrEqual:
3092 Type le = left.Type;
3094 if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
3095 ig.Emit (OpCodes.Clt_Un);
3097 ig.Emit (OpCodes.Clt);
3099 ig.Emit (OpCodes.Ldc_I4_0);
3101 opcode = OpCodes.Ceq;
3104 case Operator.BitwiseOr:
3105 opcode = OpCodes.Or;
3108 case Operator.BitwiseAnd:
3109 opcode = OpCodes.And;
3112 case Operator.ExclusiveOr:
3113 opcode = OpCodes.Xor;
3117 throw new Exception ("This should not happen: Operator = "
3118 + oper.ToString ());
3126 // Object created by Binary when the binary operator uses an method instead of being
3127 // a binary operation that maps to a CIL binary operation.
3129 public class BinaryMethod : Expression {
3130 public MethodBase method;
3131 public ArrayList Arguments;
3133 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3138 eclass = ExprClass.Value;
3141 public override Expression DoResolve (EmitContext ec)
3146 public override void Emit (EmitContext ec)
3148 ILGenerator ig = ec.ig;
3150 if (Arguments != null)
3151 Invocation.EmitArguments (ec, method, Arguments);
3153 if (method is MethodInfo)
3154 ig.Emit (OpCodes.Call, (MethodInfo) method);
3156 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3161 // Object created with +/= on delegates
3163 public class BinaryDelegate : Expression {
3167 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3172 eclass = ExprClass.Value;
3175 public override Expression DoResolve (EmitContext ec)
3180 public override void Emit (EmitContext ec)
3182 ILGenerator ig = ec.ig;
3184 Invocation.EmitArguments (ec, method, args);
3186 ig.Emit (OpCodes.Call, (MethodInfo) method);
3187 ig.Emit (OpCodes.Castclass, type);
3190 public Expression Right {
3192 Argument arg = (Argument) args [1];
3197 public bool IsAddition {
3199 return method == TypeManager.delegate_combine_delegate_delegate;
3205 // User-defined conditional logical operator
3206 public class ConditionalLogicalOperator : Expression {
3207 Expression left, right;
3210 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3213 eclass = ExprClass.Value;
3217 this.is_and = is_and;
3220 protected void Error19 ()
3222 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
3225 protected void Error218 ()
3227 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3228 "declarations of operator true and operator false");
3231 Expression op_true, op_false, op;
3233 public override Expression DoResolve (EmitContext ec)
3236 Expression operator_group;
3238 operator_group = MethodLookup (ec, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc);
3239 if (operator_group == null) {
3244 ArrayList arguments = new ArrayList ();
3245 arguments.Add (new Argument (left, Argument.AType.Expression));
3246 arguments.Add (new Argument (right, Argument.AType.Expression));
3247 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc) as MethodInfo;
3248 if ((method == null) || (method.ReturnType != type)) {
3253 op = new StaticCallExpr (method, arguments, loc);
3255 op_true = GetOperatorTrue (ec, left, loc);
3256 op_false = GetOperatorFalse (ec, left, loc);
3257 if ((op_true == null) || (op_false == null)) {
3265 public override void Emit (EmitContext ec)
3267 ILGenerator ig = ec.ig;
3268 Label false_target = ig.DefineLabel ();
3269 Label end_target = ig.DefineLabel ();
3271 ig.Emit (OpCodes.Nop);
3273 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3275 ig.Emit (OpCodes.Br, end_target);
3276 ig.MarkLabel (false_target);
3278 ig.MarkLabel (end_target);
3280 ig.Emit (OpCodes.Nop);
3284 public class PointerArithmetic : Expression {
3285 Expression left, right;
3289 // We assume that `l' is always a pointer
3291 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3294 eclass = ExprClass.Variable;
3298 is_add = is_addition;
3301 public override Expression DoResolve (EmitContext ec)
3304 // We are born fully resolved
3309 public override void Emit (EmitContext ec)
3311 Type op_type = left.Type;
3312 ILGenerator ig = ec.ig;
3313 int size = GetTypeSize (TypeManager.GetElementType (op_type));
3314 Type rtype = right.Type;
3316 if (rtype.IsPointer){
3318 // handle (pointer - pointer)
3322 ig.Emit (OpCodes.Sub);
3326 ig.Emit (OpCodes.Sizeof, op_type);
3328 IntLiteral.EmitInt (ig, size);
3329 ig.Emit (OpCodes.Div);
3331 ig.Emit (OpCodes.Conv_I8);
3334 // handle + and - on (pointer op int)
3337 ig.Emit (OpCodes.Conv_I);
3341 ig.Emit (OpCodes.Sizeof, op_type);
3343 IntLiteral.EmitInt (ig, size);
3344 if (rtype == TypeManager.int64_type)
3345 ig.Emit (OpCodes.Conv_I8);
3346 else if (rtype == TypeManager.uint64_type)
3347 ig.Emit (OpCodes.Conv_U8);
3348 ig.Emit (OpCodes.Mul);
3349 ig.Emit (OpCodes.Conv_I);
3352 ig.Emit (OpCodes.Add);
3354 ig.Emit (OpCodes.Sub);
3360 /// Implements the ternary conditional operator (?:)
3362 public class Conditional : Expression {
3363 Expression expr, trueExpr, falseExpr;
3365 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3368 this.trueExpr = trueExpr;
3369 this.falseExpr = falseExpr;
3373 public Expression Expr {
3379 public Expression TrueExpr {
3385 public Expression FalseExpr {
3391 public override Expression DoResolve (EmitContext ec)
3393 expr = expr.Resolve (ec);
3398 if (expr.Type != TypeManager.bool_type){
3399 expr = Expression.ResolveBoolean (
3406 trueExpr = trueExpr.Resolve (ec);
3407 falseExpr = falseExpr.Resolve (ec);
3409 if (trueExpr == null || falseExpr == null)
3412 eclass = ExprClass.Value;
3413 if (trueExpr.Type == falseExpr.Type)
3414 type = trueExpr.Type;
3417 Type true_type = trueExpr.Type;
3418 Type false_type = falseExpr.Type;
3420 if (trueExpr is NullLiteral){
3423 } else if (falseExpr is NullLiteral){
3429 // First, if an implicit conversion exists from trueExpr
3430 // to falseExpr, then the result type is of type falseExpr.Type
3432 conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
3435 // Check if both can convert implicitl to each other's type
3437 if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
3439 "Can not compute type of conditional expression " +
3440 "as `" + TypeManager.CSharpName (trueExpr.Type) +
3441 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
3442 "' convert implicitly to each other");
3447 } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
3451 Error (173, "The type of the conditional expression can " +
3452 "not be computed because there is no implicit conversion" +
3453 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3454 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3459 if (expr is BoolConstant){
3460 BoolConstant bc = (BoolConstant) expr;
3471 public override void Emit (EmitContext ec)
3473 ILGenerator ig = ec.ig;
3474 Label false_target = ig.DefineLabel ();
3475 Label end_target = ig.DefineLabel ();
3477 expr.EmitBranchable (ec, false_target, false);
3479 ig.Emit (OpCodes.Br, end_target);
3480 ig.MarkLabel (false_target);
3481 falseExpr.Emit (ec);
3482 ig.MarkLabel (end_target);
3490 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3491 public readonly string Name;
3492 public readonly Block Block;
3493 LocalInfo local_info;
3496 public LocalVariableReference (Block block, string name, Location l)
3501 eclass = ExprClass.Variable;
3504 // Setting `is_readonly' to false will allow you to create a writable
3505 // reference to a read-only variable. This is used by foreach and using.
3506 public LocalVariableReference (Block block, string name, Location l,
3507 LocalInfo local_info, bool is_readonly)
3508 : this (block, name, l)
3510 this.local_info = local_info;
3511 this.is_readonly = is_readonly;
3514 public VariableInfo VariableInfo {
3515 get { return local_info.VariableInfo; }
3518 public bool IsReadOnly {
3524 protected void DoResolveBase (EmitContext ec)
3526 if (local_info == null) {
3527 local_info = Block.GetLocalInfo (Name);
3528 is_readonly = local_info.ReadOnly;
3531 type = local_info.VariableType;
3533 if (ec.InAnonymousMethod)
3534 Block.LiftVariable (local_info);
3538 protected Expression DoResolve (EmitContext ec, bool is_lvalue)
3540 Expression e = Block.GetConstantExpression (Name);
3542 local_info.Used = true;
3543 eclass = ExprClass.Value;
3547 VariableInfo variable_info = local_info.VariableInfo;
3548 if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
3552 local_info.Used = true;
3554 if (local_info.LocalBuilder == null)
3555 return ec.RemapLocal (local_info);
3560 public override Expression DoResolve (EmitContext ec)
3564 return DoResolve (ec, false);
3567 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3571 VariableInfo variable_info = local_info.VariableInfo;
3572 if (variable_info != null)
3573 variable_info.SetAssigned (ec);
3575 Expression e = DoResolve (ec, true);
3581 Error (1604, "cannot assign to `" + Name + "' because it is readonly");
3585 if (local_info.LocalBuilder == null)
3586 return ec.RemapLocalLValue (local_info, right_side);
3591 public bool VerifyFixed (bool is_expression)
3593 return !is_expression || local_info.IsFixed;
3596 public override void Emit (EmitContext ec)
3598 ILGenerator ig = ec.ig;
3600 ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
3603 public void EmitAssign (EmitContext ec, Expression source)
3605 ILGenerator ig = ec.ig;
3608 ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
3611 public void AddressOf (EmitContext ec, AddressOp mode)
3613 ILGenerator ig = ec.ig;
3615 ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
3618 public override string ToString ()
3620 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3625 /// This represents a reference to a parameter in the intermediate
3628 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3634 public Parameter.Modifier mod;
3635 public bool is_ref, is_out;
3637 public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
3644 eclass = ExprClass.Variable;
3647 public VariableInfo VariableInfo {
3651 public bool VerifyFixed (bool is_expression)
3653 return !is_expression || TypeManager.IsValueType (type);
3656 public bool IsAssigned (EmitContext ec, Location loc)
3658 if (!ec.DoFlowAnalysis || !is_out ||
3659 ec.CurrentBranching.IsAssigned (vi))
3662 Report.Error (165, loc,
3663 "Use of unassigned parameter `" + name + "'");
3667 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3669 if (!ec.DoFlowAnalysis || !is_out ||
3670 ec.CurrentBranching.IsFieldAssigned (vi, field_name))
3673 Report.Error (170, loc,
3674 "Use of possibly unassigned field `" + field_name + "'");
3678 public void SetAssigned (EmitContext ec)
3680 if (is_out && ec.DoFlowAnalysis)
3681 ec.CurrentBranching.SetAssigned (vi);
3684 public void SetFieldAssigned (EmitContext ec, string field_name)
3686 if (is_out && ec.DoFlowAnalysis)
3687 ec.CurrentBranching.SetFieldAssigned (vi, field_name);
3690 protected void DoResolveBase (EmitContext ec)
3692 type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
3693 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3694 is_out = (mod & Parameter.Modifier.OUT) != 0;
3695 eclass = ExprClass.Variable;
3698 vi = block.ParameterMap [idx];
3702 // Notice that for ref/out parameters, the type exposed is not the
3703 // same type exposed externally.
3706 // externally we expose "int&"
3707 // here we expose "int".
3709 // We record this in "is_ref". This means that the type system can treat
3710 // the type as it is expected, but when we generate the code, we generate
3711 // the alternate kind of code.
3713 public override Expression DoResolve (EmitContext ec)
3717 if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
3720 if (ec.RemapToProxy)
3721 return ec.RemapParameter (idx);
3726 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3732 if (ec.RemapToProxy)
3733 return ec.RemapParameterLValue (idx, right_side);
3738 static public void EmitLdArg (ILGenerator ig, int x)
3742 case 0: ig.Emit (OpCodes.Ldarg_0); break;
3743 case 1: ig.Emit (OpCodes.Ldarg_1); break;
3744 case 2: ig.Emit (OpCodes.Ldarg_2); break;
3745 case 3: ig.Emit (OpCodes.Ldarg_3); break;
3746 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
3749 ig.Emit (OpCodes.Ldarg, x);
3753 // This method is used by parameters that are references, that are
3754 // being passed as references: we only want to pass the pointer (that
3755 // is already stored in the parameter, not the address of the pointer,
3756 // and not the value of the variable).
3758 public void EmitLoad (EmitContext ec)
3760 ILGenerator ig = ec.ig;
3766 EmitLdArg (ig, arg_idx);
3769 public override void Emit (EmitContext ec)
3771 ILGenerator ig = ec.ig;
3778 EmitLdArg (ig, arg_idx);
3784 // If we are a reference, we loaded on the stack a pointer
3785 // Now lets load the real value
3787 LoadFromPtr (ig, type);
3790 public void EmitAssign (EmitContext ec, Expression source)
3792 ILGenerator ig = ec.ig;
3800 EmitLdArg (ig, arg_idx);
3805 StoreFromPtr (ig, type);
3808 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
3810 ig.Emit (OpCodes.Starg, arg_idx);
3814 public void AddressOf (EmitContext ec, AddressOp mode)
3823 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
3825 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
3828 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
3830 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
3837 /// Used for arguments to New(), Invocation()
3839 public class Argument {
3840 public enum AType : byte {
3846 public readonly AType ArgType;
3847 public Expression Expr;
3849 public Argument (Expression expr, AType type)
3852 this.ArgType = type;
3857 if (ArgType == AType.Ref || ArgType == AType.Out)
3858 return TypeManager.GetReferenceType (Expr.Type);
3864 public Parameter.Modifier GetParameterModifier ()
3868 return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
3871 return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
3874 return Parameter.Modifier.NONE;
3878 public static string FullDesc (Argument a)
3880 return (a.ArgType == AType.Ref ? "ref " :
3881 (a.ArgType == AType.Out ? "out " : "")) +
3882 TypeManager.CSharpName (a.Expr.Type);
3885 public bool ResolveMethodGroup (EmitContext ec, Location loc)
3887 // FIXME: csc doesn't report any error if you try to use `ref' or
3888 // `out' in a delegate creation expression.
3889 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
3896 public bool Resolve (EmitContext ec, Location loc)
3898 if (ArgType == AType.Ref) {
3899 Expr = Expr.Resolve (ec);
3903 Expr = Expr.ResolveLValue (ec, Expr);
3904 } else if (ArgType == AType.Out)
3905 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
3907 Expr = Expr.Resolve (ec);
3912 if (ArgType == AType.Expression)
3915 if (Expr.eclass != ExprClass.Variable){
3917 // We just probe to match the CSC output
3919 if (Expr.eclass == ExprClass.PropertyAccess ||
3920 Expr.eclass == ExprClass.IndexerAccess){
3923 "A property or indexer can not be passed as an out or ref " +
3928 "An lvalue is required as an argument to out or ref");
3936 public void Emit (EmitContext ec)
3939 // Ref and Out parameters need to have their addresses taken.
3941 // ParameterReferences might already be references, so we want
3942 // to pass just the value
3944 if (ArgType == AType.Ref || ArgType == AType.Out){
3945 AddressOp mode = AddressOp.Store;
3947 if (ArgType == AType.Ref)
3948 mode |= AddressOp.Load;
3950 if (Expr is ParameterReference){
3951 ParameterReference pr = (ParameterReference) Expr;
3957 pr.AddressOf (ec, mode);
3960 ((IMemoryLocation)Expr).AddressOf (ec, mode);
3967 /// Invocation of methods or delegates.
3969 public class Invocation : ExpressionStatement {
3970 public readonly ArrayList Arguments;
3973 MethodBase method = null;
3976 static Hashtable method_parameter_cache;
3978 static Invocation ()
3980 method_parameter_cache = new PtrHashtable ();
3984 // arguments is an ArrayList, but we do not want to typecast,
3985 // as it might be null.
3987 // FIXME: only allow expr to be a method invocation or a
3988 // delegate invocation (7.5.5)
3990 public Invocation (Expression expr, ArrayList arguments, Location l)
3993 Arguments = arguments;
3997 public Expression Expr {
4004 /// Returns the Parameters (a ParameterData interface) for the
4007 public static ParameterData GetParameterData (MethodBase mb)
4009 object pd = method_parameter_cache [mb];
4013 return (ParameterData) pd;
4016 ip = TypeManager.LookupParametersByBuilder (mb);
4018 method_parameter_cache [mb] = ip;
4020 return (ParameterData) ip;
4022 ParameterInfo [] pi = mb.GetParameters ();
4023 ReflectionParameters rp = new ReflectionParameters (pi);
4024 method_parameter_cache [mb] = rp;
4026 return (ParameterData) rp;
4031 /// Determines "better conversion" as specified in 7.4.2.3
4033 /// Returns : 1 if a->p is better
4034 /// 0 if a->q or neither is better
4036 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
4038 Type argument_type = a.Type;
4039 Expression argument_expr = a.Expr;
4041 if (argument_type == null)
4042 throw new Exception ("Expression of type " + a.Expr +
4043 " does not resolve its type");
4046 // This is a special case since csc behaves this way. I can't find
4047 // it anywhere in the spec but oh well ...
4049 if (argument_expr is NullLiteral &&
4050 p == TypeManager.string_type &&
4051 q == TypeManager.object_type)
4053 else if (argument_expr is NullLiteral &&
4054 p == TypeManager.object_type &&
4055 q == TypeManager.string_type)
4061 if (argument_type == p)
4064 if (argument_type == q)
4068 // Now probe whether an implicit constant expression conversion
4071 // An implicit constant expression conversion permits the following
4074 // * A constant-expression of type `int' can be converted to type
4075 // sbyte, byute, short, ushort, uint, ulong provided the value of
4076 // of the expression is withing the range of the destination type.
4078 // * A constant-expression of type long can be converted to type
4079 // ulong, provided the value of the constant expression is not negative
4081 // FIXME: Note that this assumes that constant folding has
4082 // taken place. We dont do constant folding yet.
4085 if (argument_expr is IntConstant){
4086 IntConstant ei = (IntConstant) argument_expr;
4087 int value = ei.Value;
4089 if (p == TypeManager.sbyte_type){
4090 if (value >= SByte.MinValue && value <= SByte.MaxValue)
4092 } else if (p == TypeManager.byte_type){
4093 if (q == TypeManager.sbyte_type &&
4094 value >= SByte.MinValue && value <= SByte.MaxValue)
4096 else if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
4098 } else if (p == TypeManager.short_type){
4099 if (value >= Int16.MinValue && value <= Int16.MaxValue)
4101 } else if (p == TypeManager.ushort_type){
4102 if (q == TypeManager.short_type &&
4103 value >= Int16.MinValue && value <= Int16.MaxValue)
4105 else if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
4107 } else if (p == TypeManager.int32_type){
4108 if (value >= Int32.MinValue && value <= Int32.MaxValue)
4110 } else if (p == TypeManager.uint32_type){
4112 // we can optimize this case: a positive int32
4113 // always fits on a uint32
4117 } else if (p == TypeManager.uint64_type){
4119 // we can optimize this case: a positive int32
4120 // always fits on a uint64
4124 // This special case is needed because csc behaves like this.
4125 // int -> uint is better than int -> ulong!
4127 if (q == TypeManager.uint32_type)
4130 if (q == TypeManager.int64_type)
4132 else if (value >= 0)
4134 } else if (p == TypeManager.int64_type){
4137 } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
4138 LongConstant lc = (LongConstant) argument_expr;
4140 if (p == TypeManager.uint64_type){
4147 Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
4155 Expression p_tmp = new EmptyExpression (p);
4156 Expression q_tmp = new EmptyExpression (q);
4158 if (Convert.ImplicitConversionExists (ec, p_tmp, q) == true &&
4159 Convert.ImplicitConversionExists (ec, q_tmp, p) == false)
4162 if (p == TypeManager.sbyte_type)
4163 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
4164 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4167 if (p == TypeManager.short_type)
4168 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
4169 q == TypeManager.uint64_type)
4172 if (p == TypeManager.int32_type)
4173 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4176 if (p == TypeManager.int64_type)
4177 if (q == TypeManager.uint64_type)
4184 /// Determines "Better function" between candidate
4185 /// and the current best match
4188 /// Returns an integer indicating :
4189 /// 0 if candidate ain't better
4190 /// 1 if candidate is better than the current best match
4192 static int BetterFunction (EmitContext ec, ArrayList args,
4193 MethodBase candidate, bool candidate_params,
4194 MethodBase best, bool best_params,
4197 ParameterData candidate_pd = GetParameterData (candidate);
4198 ParameterData best_pd;
4204 argument_count = args.Count;
4206 int cand_count = candidate_pd.Count;
4209 // If there is no best method, than this one
4210 // is better, however, if we already found a
4211 // best method, we cant tell. This happens
4223 // interface IFooBar : IFoo, IBar {}
4225 // We cant tell if IFoo.DoIt is better than IBar.DoIt
4227 // However, we have to consider that
4228 // Trim (); is better than Trim (params char[] chars);
4229 if (cand_count == 0 && argument_count == 0)
4230 return best == null || best_params ? 1 : 0;
4232 if (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS)
4233 if (cand_count != argument_count)
4239 if (argument_count == 0 && cand_count == 1 &&
4240 candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
4243 for (int j = 0; j < argument_count; ++j) {
4245 Argument a = (Argument) args [j];
4246 Type t = candidate_pd.ParameterType (j);
4248 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4249 if (candidate_params)
4250 t = TypeManager.GetElementType (t);
4252 x = BetterConversion (ec, a, t, null, loc);
4264 best_pd = GetParameterData (best);
4266 int rating1 = 0, rating2 = 0;
4268 for (int j = 0; j < argument_count; ++j) {
4271 Argument a = (Argument) args [j];
4273 Type ct = candidate_pd.ParameterType (j);
4274 Type bt = best_pd.ParameterType (j);
4276 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4277 if (candidate_params)
4278 ct = TypeManager.GetElementType (ct);
4280 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4282 bt = TypeManager.GetElementType (bt);
4284 x = BetterConversion (ec, a, ct, bt, loc);
4285 y = BetterConversion (ec, a, bt, ct, loc);
4295 // If a method (in the normal form) with the
4296 // same signature as the expanded form of the
4297 // current best params method already exists,
4298 // the expanded form is not applicable so we
4299 // force it to select the candidate
4301 if (!candidate_params && best_params && cand_count == argument_count)
4304 if (rating1 > rating2)
4310 public static string FullMethodDesc (MethodBase mb)
4312 string ret_type = "";
4314 if (mb is MethodInfo)
4315 ret_type = TypeManager.CSharpName (((MethodInfo) mb).ReturnType);
4317 StringBuilder sb = new StringBuilder (ret_type);
4319 sb.Append (mb.ReflectedType.ToString ());
4321 sb.Append (mb.Name);
4323 ParameterData pd = GetParameterData (mb);
4325 int count = pd.Count;
4328 for (int i = count; i > 0; ) {
4331 sb.Append (pd.ParameterDesc (count - i - 1));
4337 return sb.ToString ();
4340 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
4342 MemberInfo [] miset;
4343 MethodGroupExpr union;
4348 return (MethodGroupExpr) mg2;
4351 return (MethodGroupExpr) mg1;
4354 MethodGroupExpr left_set = null, right_set = null;
4355 int length1 = 0, length2 = 0;
4357 left_set = (MethodGroupExpr) mg1;
4358 length1 = left_set.Methods.Length;
4360 right_set = (MethodGroupExpr) mg2;
4361 length2 = right_set.Methods.Length;
4363 ArrayList common = new ArrayList ();
4365 foreach (MethodBase r in right_set.Methods){
4366 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
4370 miset = new MemberInfo [length1 + length2 - common.Count];
4371 left_set.Methods.CopyTo (miset, 0);
4375 foreach (MethodBase r in right_set.Methods) {
4376 if (!common.Contains (r))
4380 union = new MethodGroupExpr (miset, loc);
4386 /// Determines if the candidate method, if a params method, is applicable
4387 /// in its expanded form to the given set of arguments
4389 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
4393 if (arguments == null)
4396 arg_count = arguments.Count;
4398 ParameterData pd = GetParameterData (candidate);
4400 int pd_count = pd.Count;
4405 if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
4408 if (pd_count - 1 > arg_count)
4411 if (pd_count == 1 && arg_count == 0)
4415 // If we have come this far, the case which
4416 // remains is when the number of parameters is
4417 // less than or equal to the argument count.
4419 for (int i = 0; i < pd_count - 1; ++i) {
4421 Argument a = (Argument) arguments [i];
4423 Parameter.Modifier a_mod = a.GetParameterModifier () &
4424 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4425 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4426 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4428 if (a_mod == p_mod) {
4430 if (a_mod == Parameter.Modifier.NONE)
4431 if (!Convert.ImplicitConversionExists (ec,
4433 pd.ParameterType (i)))
4436 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4437 Type pt = pd.ParameterType (i);
4440 pt = TypeManager.GetReferenceType (pt);
4450 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
4452 for (int i = pd_count - 1; i < arg_count; i++) {
4453 Argument a = (Argument) arguments [i];
4455 if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
4463 /// Determines if the candidate method is applicable (section 14.4.2.1)
4464 /// to the given set of arguments
4466 static bool IsApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
4470 if (arguments == null)
4473 arg_count = arguments.Count;
4476 ParameterData pd = GetParameterData (candidate);
4478 if (arg_count != pd.Count)
4481 for (int i = arg_count; i > 0; ) {
4484 Argument a = (Argument) arguments [i];
4486 Parameter.Modifier a_mod = a.GetParameterModifier () &
4487 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4488 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4489 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4492 if (a_mod == p_mod ||
4493 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
4494 if (a_mod == Parameter.Modifier.NONE) {
4495 if (!Convert.ImplicitConversionExists (ec,
4497 pd.ParameterType (i)))
4501 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4502 Type pt = pd.ParameterType (i);
4505 pt = TypeManager.GetReferenceType (pt);
4520 /// Find the Applicable Function Members (7.4.2.1)
4522 /// me: Method Group expression with the members to select.
4523 /// it might contain constructors or methods (or anything
4524 /// that maps to a method).
4526 /// Arguments: ArrayList containing resolved Argument objects.
4528 /// loc: The location if we want an error to be reported, or a Null
4529 /// location for "probing" purposes.
4531 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4532 /// that is the best match of me on Arguments.
4535 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4536 ArrayList Arguments, Location loc)
4538 MethodBase method = null;
4539 Type applicable_type = null;
4541 ArrayList candidates = new ArrayList ();
4544 // Used to keep a map between the candidate
4545 // and whether it is being considered in its
4546 // normal or expanded form
4548 Hashtable candidate_to_form = new PtrHashtable ();
4552 // First we construct the set of applicable methods
4554 // We start at the top of the type hierarchy and
4555 // go down to find applicable methods
4557 applicable_type = me.DeclaringType;
4559 if (me.Name == "Invoke" && TypeManager.IsDelegateType (applicable_type)) {
4560 Error_InvokeOnDelegate (loc);
4564 bool found_applicable = false;
4566 foreach (MethodBase candidate in me.Methods){
4567 Type decl_type = candidate.DeclaringType;
4570 // If we have already found an applicable method
4571 // we eliminate all base types (Section 14.5.5.1)
4573 if (decl_type != applicable_type &&
4574 (applicable_type.IsSubclassOf (decl_type) ||
4575 TypeManager.ImplementsInterface (applicable_type, decl_type)) &&
4580 // Check if candidate is applicable (section 14.4.2.1)
4581 if (IsApplicable (ec, Arguments, candidate)) {
4582 // Candidate is applicable in normal form
4583 candidates.Add (candidate);
4584 applicable_type = candidate.DeclaringType;
4585 found_applicable = true;
4586 candidate_to_form [candidate] = false;
4588 if (IsParamsMethodApplicable (ec, Arguments, candidate)) {
4589 // Candidate is applicable in expanded form
4590 candidates.Add (candidate);
4591 applicable_type = candidate.DeclaringType;
4592 found_applicable = true;
4593 candidate_to_form [candidate] = true;
4600 // Now we actually find the best method
4602 int candidate_top = candidates.Count;
4603 for (int ix = 0; ix < candidate_top; ix++){
4604 MethodBase candidate = (MethodBase) candidates [ix];
4606 bool cand_params = (bool) candidate_to_form [candidate];
4607 bool method_params = false;
4610 method_params = (bool) candidate_to_form [method];
4612 int x = BetterFunction (ec, Arguments,
4613 candidate, cand_params,
4614 method, method_params,
4623 if (Arguments == null)
4626 argument_count = Arguments.Count;
4629 if (method == null) {
4631 // Okay so we have failed to find anything so we
4632 // return by providing info about the closest match
4634 for (int i = 0; i < me.Methods.Length; ++i) {
4636 MethodBase c = (MethodBase) me.Methods [i];
4637 ParameterData pd = GetParameterData (c);
4639 if (pd.Count != argument_count)
4642 VerifyArgumentsCompat (ec, Arguments, argument_count, c, false,
4647 if (!Location.IsNull (loc)) {
4648 string report_name = me.Name;
4649 if (report_name == ".ctor")
4650 report_name = me.DeclaringType.ToString ();
4652 Error_WrongNumArguments (loc, report_name, argument_count);
4659 // Now check that there are no ambiguities i.e the selected method
4660 // should be better than all the others
4662 bool best_params = (bool) candidate_to_form [method];
4664 for (int ix = 0; ix < candidate_top; ix++){
4665 MethodBase candidate = (MethodBase) candidates [ix];
4667 if (candidate == method)
4671 // If a normal method is applicable in
4672 // the sense that it has the same
4673 // number of arguments, then the
4674 // expanded params method is never
4675 // applicable so we debar the params
4678 if ((IsParamsMethodApplicable (ec, Arguments, candidate) &&
4679 IsApplicable (ec, Arguments, method)))
4682 bool cand_params = (bool) candidate_to_form [candidate];
4683 int x = BetterFunction (ec, Arguments,
4684 method, best_params,
4685 candidate, cand_params,
4691 "Ambiguous call when selecting function due to implicit casts");
4697 // And now check if the arguments are all
4698 // compatible, perform conversions if
4699 // necessary etc. and return if everything is
4702 if (!VerifyArgumentsCompat (ec, Arguments, argument_count, method,
4703 best_params, null, loc))
4709 static void Error_WrongNumArguments (Location loc, String name, int arg_count)
4711 Report.Error (1501, loc,
4712 "No overload for method `" + name + "' takes `" +
4713 arg_count + "' arguments");
4716 static void Error_InvokeOnDelegate (Location loc)
4718 Report.Error (1533, loc,
4719 "Invoke cannot be called directly on a delegate");
4722 static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
4723 Type delegate_type, string arg_sig, string par_desc)
4725 if (delegate_type == null)
4726 Report.Error (1502, loc,
4727 "The best overloaded match for method '" +
4728 FullMethodDesc (method) +
4729 "' has some invalid arguments");
4731 Report.Error (1594, loc,
4732 "Delegate '" + delegate_type.ToString () +
4733 "' has some invalid arguments.");
4734 Report.Error (1503, loc,
4735 String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'",
4736 idx, arg_sig, par_desc));
4739 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
4742 bool chose_params_expanded,
4746 ParameterData pd = GetParameterData (method);
4747 int pd_count = pd.Count;
4749 for (int j = 0; j < argument_count; j++) {
4750 Argument a = (Argument) Arguments [j];
4751 Expression a_expr = a.Expr;
4752 Type parameter_type = pd.ParameterType (j);
4753 Parameter.Modifier pm = pd.ParameterModifier (j);
4755 if (pm == Parameter.Modifier.PARAMS){
4756 if ((pm & ~Parameter.Modifier.PARAMS) != a.GetParameterModifier ()) {
4757 if (!Location.IsNull (loc))
4758 Error_InvalidArguments (
4759 loc, j, method, delegate_type,
4760 Argument.FullDesc (a), pd.ParameterDesc (j));
4764 if (chose_params_expanded)
4765 parameter_type = TypeManager.GetElementType (parameter_type);
4770 if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
4771 if (!Location.IsNull (loc))
4772 Error_InvalidArguments (
4773 loc, j, method, delegate_type,
4774 Argument.FullDesc (a), pd.ParameterDesc (j));
4782 if (a.Type != parameter_type){
4785 conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
4788 if (!Location.IsNull (loc))
4789 Error_InvalidArguments (
4790 loc, j, method, delegate_type,
4791 Argument.FullDesc (a), pd.ParameterDesc (j));
4796 // Update the argument with the implicit conversion
4802 Parameter.Modifier a_mod = a.GetParameterModifier () &
4803 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4804 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
4805 ~(Parameter.Modifier.OUT | Parameter.Modifier.REF);
4807 if (a_mod != p_mod &&
4808 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
4809 if (!Location.IsNull (loc)) {
4810 Report.Error (1502, loc,
4811 "The best overloaded match for method '" + FullMethodDesc (method)+
4812 "' has some invalid arguments");
4813 Report.Error (1503, loc,
4814 "Argument " + (j+1) +
4815 ": Cannot convert from '" + Argument.FullDesc (a)
4816 + "' to '" + pd.ParameterDesc (j) + "'");
4826 public override Expression DoResolve (EmitContext ec)
4829 // First, resolve the expression that is used to
4830 // trigger the invocation
4832 if (expr is BaseAccess)
4835 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4839 if (!(expr is MethodGroupExpr)) {
4840 Type expr_type = expr.Type;
4842 if (expr_type != null){
4843 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
4845 return (new DelegateInvocation (
4846 this.expr, Arguments, loc)).Resolve (ec);
4850 if (!(expr is MethodGroupExpr)){
4851 expr.Error_UnexpectedKind (ResolveFlags.MethodGroup);
4856 // Next, evaluate all the expressions in the argument list
4858 if (Arguments != null){
4859 foreach (Argument a in Arguments){
4860 if (!a.Resolve (ec, loc))
4865 MethodGroupExpr mg = (MethodGroupExpr) expr;
4866 method = OverloadResolve (ec, mg, Arguments, loc);
4868 if (method == null){
4870 "Could not find any applicable function for this argument list");
4874 MethodInfo mi = method as MethodInfo;
4876 type = TypeManager.TypeToCoreType (mi.ReturnType);
4877 if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null))
4878 SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
4881 if (type.IsPointer){
4889 // Only base will allow this invocation to happen.
4891 if (is_base && method.IsAbstract){
4892 Report.Error (205, loc, "Cannot call an abstract base member: " +
4893 FullMethodDesc (method));
4897 if ((method.Attributes & MethodAttributes.SpecialName) != 0){
4898 if (TypeManager.IsSpecialMethod (method))
4899 Report.Error (571, loc, method.Name + ": can not call operator or accessor");
4902 eclass = ExprClass.Value;
4907 // Emits the list of arguments as an array
4909 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
4911 ILGenerator ig = ec.ig;
4912 int count = arguments.Count - idx;
4913 Argument a = (Argument) arguments [idx];
4914 Type t = a.Expr.Type;
4915 string array_type = t.FullName + "[]";
4918 array = ig.DeclareLocal (TypeManager.LookupType (array_type));
4919 IntConstant.EmitInt (ig, count);
4920 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
4921 ig.Emit (OpCodes.Stloc, array);
4923 int top = arguments.Count;
4924 for (int j = idx; j < top; j++){
4925 a = (Argument) arguments [j];
4927 ig.Emit (OpCodes.Ldloc, array);
4928 IntConstant.EmitInt (ig, j - idx);
4931 OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj);
4933 ig.Emit (OpCodes.Ldelema, t);
4938 ig.Emit (OpCodes.Stobj, t);
4942 ig.Emit (OpCodes.Ldloc, array);
4946 /// Emits a list of resolved Arguments that are in the arguments
4949 /// The MethodBase argument might be null if the
4950 /// emission of the arguments is known not to contain
4951 /// a `params' field (for example in constructors or other routines
4952 /// that keep their arguments in this structure)
4954 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
4958 pd = GetParameterData (mb);
4963 // If we are calling a params method with no arguments, special case it
4965 if (arguments == null){
4966 if (pd != null && pd.Count > 0 &&
4967 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
4968 ILGenerator ig = ec.ig;
4970 IntConstant.EmitInt (ig, 0);
4971 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
4977 int top = arguments.Count;
4979 for (int i = 0; i < top; i++){
4980 Argument a = (Argument) arguments [i];
4983 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
4985 // Special case if we are passing the same data as the
4986 // params argument, do not put it in an array.
4988 if (pd.ParameterType (i) == a.Type)
4991 EmitParams (ec, i, arguments);
4999 if (pd != null && pd.Count > top &&
5000 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
5001 ILGenerator ig = ec.ig;
5003 IntConstant.EmitInt (ig, 0);
5004 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (top)));
5009 /// is_base tells whether we want to force the use of the `call'
5010 /// opcode instead of using callvirt. Call is required to call
5011 /// a specific method, while callvirt will always use the most
5012 /// recent method in the vtable.
5014 /// is_static tells whether this is an invocation on a static method
5016 /// instance_expr is an expression that represents the instance
5017 /// it must be non-null if is_static is false.
5019 /// method is the method to invoke.
5021 /// Arguments is the list of arguments to pass to the method or constructor.
5023 public static void EmitCall (EmitContext ec, bool is_base,
5024 bool is_static, Expression instance_expr,
5025 MethodBase method, ArrayList Arguments, Location loc)
5027 ILGenerator ig = ec.ig;
5028 bool struct_call = false;
5030 Type decl_type = method.DeclaringType;
5032 if (!RootContext.StdLib) {
5033 // Replace any calls to the system's System.Array type with calls to
5034 // the newly created one.
5035 if (method == TypeManager.system_int_array_get_length)
5036 method = TypeManager.int_array_get_length;
5037 else if (method == TypeManager.system_int_array_get_rank)
5038 method = TypeManager.int_array_get_rank;
5039 else if (method == TypeManager.system_object_array_clone)
5040 method = TypeManager.object_array_clone;
5041 else if (method == TypeManager.system_int_array_get_length_int)
5042 method = TypeManager.int_array_get_length_int;
5043 else if (method == TypeManager.system_int_array_get_lower_bound_int)
5044 method = TypeManager.int_array_get_lower_bound_int;
5045 else if (method == TypeManager.system_int_array_get_upper_bound_int)
5046 method = TypeManager.int_array_get_upper_bound_int;
5047 else if (method == TypeManager.system_void_array_copyto_array_int)
5048 method = TypeManager.void_array_copyto_array_int;
5052 // This checks the `ConditionalAttribute' on the method, and the
5053 // ObsoleteAttribute
5055 TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (method, loc);
5056 if ((flags & TypeManager.MethodFlags.IsObsoleteError) != 0)
5058 if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
5062 if (decl_type.IsValueType)
5065 // If this is ourselves, push "this"
5067 if (instance_expr == null){
5068 ig.Emit (OpCodes.Ldarg_0);
5071 // Push the instance expression
5073 if (instance_expr.Type.IsValueType){
5075 // Special case: calls to a function declared in a
5076 // reference-type with a value-type argument need
5077 // to have their value boxed.
5080 if (decl_type.IsValueType){
5082 // If the expression implements IMemoryLocation, then
5083 // we can optimize and use AddressOf on the
5086 // If not we have to use some temporary storage for
5088 if (instance_expr is IMemoryLocation){
5089 ((IMemoryLocation)instance_expr).
5090 AddressOf (ec, AddressOp.LoadStore);
5093 Type t = instance_expr.Type;
5095 instance_expr.Emit (ec);
5096 LocalBuilder temp = ig.DeclareLocal (t);
5097 ig.Emit (OpCodes.Stloc, temp);
5098 ig.Emit (OpCodes.Ldloca, temp);
5101 instance_expr.Emit (ec);
5102 ig.Emit (OpCodes.Box, instance_expr.Type);
5105 instance_expr.Emit (ec);
5109 EmitArguments (ec, method, Arguments);
5111 if (is_static || struct_call || is_base){
5112 if (method is MethodInfo) {
5113 ig.Emit (OpCodes.Call, (MethodInfo) method);
5115 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5117 if (method is MethodInfo)
5118 ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
5120 ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
5124 public override void Emit (EmitContext ec)
5126 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
5128 EmitCall (ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
5131 public override void EmitStatement (EmitContext ec)
5136 // Pop the return value if there is one
5138 if (method is MethodInfo){
5139 Type ret = ((MethodInfo)method).ReturnType;
5140 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
5141 ec.ig.Emit (OpCodes.Pop);
5146 public class InvocationOrCast : ExpressionStatement
5149 Expression argument;
5151 public InvocationOrCast (Expression expr, Expression argument, Location loc)
5154 this.argument = argument;
5158 public override Expression DoResolve (EmitContext ec)
5161 // First try to resolve it as a cast.
5163 type = ec.DeclSpace.ResolveType (expr, true, loc);
5165 Cast cast = new Cast (new TypeExpression (type, loc), argument, loc);
5166 return cast.Resolve (ec);
5170 // This can either be a type or a delegate invocation.
5171 // Let's just resolve it and see what we'll get.
5173 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5178 // Ok, so it's a Cast.
5180 if (expr.eclass == ExprClass.Type) {
5181 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5182 return cast.Resolve (ec);
5186 // It's a delegate invocation.
5188 if (!TypeManager.IsDelegateType (expr.Type)) {
5189 Error (149, "Method name expected");
5193 ArrayList args = new ArrayList ();
5194 args.Add (new Argument (argument, Argument.AType.Expression));
5195 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5196 return invocation.Resolve (ec);
5201 Error (201, "Only assignment, call, increment, decrement and new object " +
5202 "expressions can be used as a statement");
5205 public override ExpressionStatement ResolveStatement (EmitContext ec)
5208 // First try to resolve it as a cast.
5210 type = ec.DeclSpace.ResolveType (expr, true, loc);
5217 // This can either be a type or a delegate invocation.
5218 // Let's just resolve it and see what we'll get.
5220 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5221 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
5227 // It's a delegate invocation.
5229 if (!TypeManager.IsDelegateType (expr.Type)) {
5230 Error (149, "Method name expected");
5234 ArrayList args = new ArrayList ();
5235 args.Add (new Argument (argument, Argument.AType.Expression));
5236 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5237 return invocation.ResolveStatement (ec);
5240 public override void Emit (EmitContext ec)
5242 throw new Exception ("Cannot happen");
5245 public override void EmitStatement (EmitContext ec)
5247 throw new Exception ("Cannot happen");
5252 // This class is used to "disable" the code generation for the
5253 // temporary variable when initializing value types.
5255 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
5256 public void AddressOf (EmitContext ec, AddressOp Mode)
5263 /// Implements the new expression
5265 public class New : ExpressionStatement, IMemoryLocation {
5266 public readonly ArrayList Arguments;
5269 // During bootstrap, it contains the RequestedType,
5270 // but if `type' is not null, it *might* contain a NewDelegate
5271 // (because of field multi-initialization)
5273 public Expression RequestedType;
5275 MethodBase method = null;
5278 // If set, the new expression is for a value_target, and
5279 // we will not leave anything on the stack.
5281 Expression value_target;
5282 bool value_target_set = false;
5284 public New (Expression requested_type, ArrayList arguments, Location l)
5286 RequestedType = requested_type;
5287 Arguments = arguments;
5291 public bool SetValueTypeVariable (Expression value)
5293 value_target = value;
5294 value_target_set = true;
5295 if (!(value_target is IMemoryLocation)){
5296 Error_UnexpectedKind ("variable");
5303 // This function is used to disable the following code sequence for
5304 // value type initialization:
5306 // AddressOf (temporary)
5310 // Instead the provide will have provided us with the address on the
5311 // stack to store the results.
5313 static Expression MyEmptyExpression;
5315 public void DisableTemporaryValueType ()
5317 if (MyEmptyExpression == null)
5318 MyEmptyExpression = new EmptyAddressOf ();
5321 // To enable this, look into:
5322 // test-34 and test-89 and self bootstrapping.
5324 // For instance, we can avoid a copy by using `newobj'
5325 // instead of Call + Push-temp on value types.
5326 // value_target = MyEmptyExpression;
5329 public override Expression DoResolve (EmitContext ec)
5332 // The New DoResolve might be called twice when initializing field
5333 // expressions (see EmitFieldInitializers, the call to
5334 // GetInitializerExpression will perform a resolve on the expression,
5335 // and later the assign will trigger another resolution
5337 // This leads to bugs (#37014)
5340 if (RequestedType is NewDelegate)
5341 return RequestedType;
5345 type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
5350 bool IsDelegate = TypeManager.IsDelegateType (type);
5353 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5354 if (RequestedType != null)
5355 if (!(RequestedType is NewDelegate))
5356 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
5357 return RequestedType;
5360 if (type.IsInterface || type.IsAbstract){
5361 Error (144, "It is not possible to create instances of interfaces or abstract classes");
5365 bool is_struct = type.IsValueType;
5366 eclass = ExprClass.Value;
5369 // SRE returns a match for .ctor () on structs (the object constructor),
5370 // so we have to manually ignore it.
5372 if (is_struct && Arguments == null)
5376 ml = MemberLookupFinal (ec, null, type, ".ctor",
5377 MemberTypes.Constructor,
5378 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5383 if (! (ml is MethodGroupExpr)){
5385 ml.Error_UnexpectedKind ("method group");
5391 if (Arguments != null){
5392 foreach (Argument a in Arguments){
5393 if (!a.Resolve (ec, loc))
5398 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
5402 if (method == null) {
5403 if (!is_struct || Arguments.Count > 0) {
5404 Error (1501, String.Format (
5405 "New invocation: Can not find a constructor in `{0}' for this argument list",
5406 TypeManager.CSharpName (type)));
5415 // This DoEmit can be invoked in two contexts:
5416 // * As a mechanism that will leave a value on the stack (new object)
5417 // * As one that wont (init struct)
5419 // You can control whether a value is required on the stack by passing
5420 // need_value_on_stack. The code *might* leave a value on the stack
5421 // so it must be popped manually
5423 // If we are dealing with a ValueType, we have a few
5424 // situations to deal with:
5426 // * The target is a ValueType, and we have been provided
5427 // the instance (this is easy, we are being assigned).
5429 // * The target of New is being passed as an argument,
5430 // to a boxing operation or a function that takes a
5433 // In this case, we need to create a temporary variable
5434 // that is the argument of New.
5436 // Returns whether a value is left on the stack
5438 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5440 bool is_value_type = type.IsValueType;
5441 ILGenerator ig = ec.ig;
5446 // Allow DoEmit() to be called multiple times.
5447 // We need to create a new LocalTemporary each time since
5448 // you can't share LocalBuilders among ILGeneators.
5449 if (!value_target_set)
5450 value_target = new LocalTemporary (ec, type);
5452 ml = (IMemoryLocation) value_target;
5453 ml.AddressOf (ec, AddressOp.Store);
5457 Invocation.EmitArguments (ec, method, Arguments);
5461 ig.Emit (OpCodes.Initobj, type);
5463 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5464 if (need_value_on_stack){
5465 value_target.Emit (ec);
5470 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5475 public override void Emit (EmitContext ec)
5480 public override void EmitStatement (EmitContext ec)
5482 if (DoEmit (ec, false))
5483 ec.ig.Emit (OpCodes.Pop);
5486 public void AddressOf (EmitContext ec, AddressOp Mode)
5488 if (!type.IsValueType){
5490 // We throw an exception. So far, I believe we only need to support
5492 // foreach (int j in new StructType ())
5495 throw new Exception ("AddressOf should not be used for classes");
5498 if (!value_target_set)
5499 value_target = new LocalTemporary (ec, type);
5501 IMemoryLocation ml = (IMemoryLocation) value_target;
5502 ml.AddressOf (ec, AddressOp.Store);
5504 Invocation.EmitArguments (ec, method, Arguments);
5507 ec.ig.Emit (OpCodes.Initobj, type);
5509 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5511 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5516 /// 14.5.10.2: Represents an array creation expression.
5520 /// There are two possible scenarios here: one is an array creation
5521 /// expression that specifies the dimensions and optionally the
5522 /// initialization data and the other which does not need dimensions
5523 /// specified but where initialization data is mandatory.
5525 public class ArrayCreation : ExpressionStatement {
5526 Expression requested_base_type;
5527 ArrayList initializers;
5530 // The list of Argument types.
5531 // This is used to construct the `newarray' or constructor signature
5533 ArrayList arguments;
5536 // Method used to create the array object.
5538 MethodBase new_method = null;
5540 Type array_element_type;
5541 Type underlying_type;
5542 bool is_one_dimensional = false;
5543 bool is_builtin_type = false;
5544 bool expect_initializers = false;
5545 int num_arguments = 0;
5549 ArrayList array_data;
5554 // The number of array initializers that we can handle
5555 // via the InitializeArray method - through EmitStaticInitializers
5557 int num_automatic_initializers;
5559 const int max_automatic_initializers = 6;
5561 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5563 this.requested_base_type = requested_base_type;
5564 this.initializers = initializers;
5568 arguments = new ArrayList ();
5570 foreach (Expression e in exprs) {
5571 arguments.Add (new Argument (e, Argument.AType.Expression));
5576 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
5578 this.requested_base_type = requested_base_type;
5579 this.initializers = initializers;
5583 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5585 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5587 //dimensions = tmp.Length - 1;
5588 expect_initializers = true;
5591 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
5593 StringBuilder sb = new StringBuilder (rank);
5596 for (int i = 1; i < idx_count; i++)
5601 return new ComposedCast (base_type, sb.ToString (), loc);
5604 void Error_IncorrectArrayInitializer ()
5606 Error (178, "Incorrectly structured array initializer");
5609 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
5611 if (specified_dims) {
5612 Argument a = (Argument) arguments [idx];
5614 if (!a.Resolve (ec, loc))
5617 if (!(a.Expr is Constant)) {
5618 Error (150, "A constant value is expected");
5622 int value = (int) ((Constant) a.Expr).GetValue ();
5624 if (value != probe.Count) {
5625 Error_IncorrectArrayInitializer ();
5629 bounds [idx] = value;
5632 int child_bounds = -1;
5633 foreach (object o in probe) {
5634 if (o is ArrayList) {
5635 int current_bounds = ((ArrayList) o).Count;
5637 if (child_bounds == -1)
5638 child_bounds = current_bounds;
5640 else if (child_bounds != current_bounds){
5641 Error_IncorrectArrayInitializer ();
5644 if (specified_dims && (idx + 1 >= arguments.Count)){
5645 Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression");
5649 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
5653 if (child_bounds != -1){
5654 Error_IncorrectArrayInitializer ();
5658 Expression tmp = (Expression) o;
5659 tmp = tmp.Resolve (ec);
5663 // Console.WriteLine ("I got: " + tmp);
5664 // Handle initialization from vars, fields etc.
5666 Expression conv = Convert.ImplicitConversionRequired (
5667 ec, tmp, underlying_type, loc);
5672 if (conv is StringConstant)
5673 array_data.Add (conv);
5674 else if (conv is Constant) {
5675 array_data.Add (conv);
5676 num_automatic_initializers++;
5678 array_data.Add (conv);
5685 public void UpdateIndices (EmitContext ec)
5688 for (ArrayList probe = initializers; probe != null;) {
5689 if (probe.Count > 0 && probe [0] is ArrayList) {
5690 Expression e = new IntConstant (probe.Count);
5691 arguments.Add (new Argument (e, Argument.AType.Expression));
5693 bounds [i++] = probe.Count;
5695 probe = (ArrayList) probe [0];
5698 Expression e = new IntConstant (probe.Count);
5699 arguments.Add (new Argument (e, Argument.AType.Expression));
5701 bounds [i++] = probe.Count;
5708 public bool ValidateInitializers (EmitContext ec, Type array_type)
5710 if (initializers == null) {
5711 if (expect_initializers)
5717 if (underlying_type == null)
5721 // We use this to store all the date values in the order in which we
5722 // will need to store them in the byte blob later
5724 array_data = new ArrayList ();
5725 bounds = new Hashtable ();
5729 if (arguments != null) {
5730 ret = CheckIndices (ec, initializers, 0, true);
5733 arguments = new ArrayList ();
5735 ret = CheckIndices (ec, initializers, 0, false);
5742 if (arguments.Count != dimensions) {
5743 Error_IncorrectArrayInitializer ();
5751 void Error_NegativeArrayIndex ()
5753 Error (284, "Can not create array with a negative size");
5757 // Converts `source' to an int, uint, long or ulong.
5759 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
5763 bool old_checked = ec.CheckState;
5764 ec.CheckState = true;
5766 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
5767 if (target == null){
5768 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
5769 if (target == null){
5770 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
5771 if (target == null){
5772 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
5774 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
5778 ec.CheckState = old_checked;
5781 // Only positive constants are allowed at compile time
5783 if (target is Constant){
5784 if (target is IntConstant){
5785 if (((IntConstant) target).Value < 0){
5786 Error_NegativeArrayIndex ();
5791 if (target is LongConstant){
5792 if (((LongConstant) target).Value < 0){
5793 Error_NegativeArrayIndex ();
5804 // Creates the type of the array
5806 bool LookupType (EmitContext ec)
5808 StringBuilder array_qualifier = new StringBuilder (rank);
5811 // `In the first form allocates an array instace of the type that results
5812 // from deleting each of the individual expression from the expression list'
5814 if (num_arguments > 0) {
5815 array_qualifier.Append ("[");
5816 for (int i = num_arguments-1; i > 0; i--)
5817 array_qualifier.Append (",");
5818 array_qualifier.Append ("]");
5824 Expression array_type_expr;
5825 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
5826 type = ec.DeclSpace.ResolveType (array_type_expr, false, loc);
5831 underlying_type = type;
5832 if (underlying_type.IsArray)
5833 underlying_type = TypeManager.GetElementType (underlying_type);
5834 dimensions = type.GetArrayRank ();
5839 public override Expression DoResolve (EmitContext ec)
5843 if (!LookupType (ec))
5847 // First step is to validate the initializers and fill
5848 // in any missing bits
5850 if (!ValidateInitializers (ec, type))
5853 if (arguments == null)
5856 arg_count = arguments.Count;
5857 foreach (Argument a in arguments){
5858 if (!a.Resolve (ec, loc))
5861 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
5862 if (real_arg == null)
5869 array_element_type = TypeManager.GetElementType (type);
5871 if (arg_count == 1) {
5872 is_one_dimensional = true;
5873 eclass = ExprClass.Value;
5877 is_builtin_type = TypeManager.IsBuiltinType (type);
5879 if (is_builtin_type) {
5882 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
5883 AllBindingFlags, loc);
5885 if (!(ml is MethodGroupExpr)) {
5886 ml.Error_UnexpectedKind ("method group");
5891 Error (-6, "New invocation: Can not find a constructor for " +
5892 "this argument list");
5896 new_method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, arguments, loc);
5898 if (new_method == null) {
5899 Error (-6, "New invocation: Can not find a constructor for " +
5900 "this argument list");
5904 eclass = ExprClass.Value;
5907 ModuleBuilder mb = CodeGen.ModuleBuilder;
5908 ArrayList args = new ArrayList ();
5910 if (arguments != null) {
5911 for (int i = 0; i < arg_count; i++)
5912 args.Add (TypeManager.int32_type);
5915 Type [] arg_types = null;
5918 arg_types = new Type [args.Count];
5920 args.CopyTo (arg_types, 0);
5922 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
5925 if (new_method == null) {
5926 Error (-6, "New invocation: Can not find a constructor for " +
5927 "this argument list");
5931 eclass = ExprClass.Value;
5936 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
5941 int count = array_data.Count;
5943 if (underlying_type.IsEnum)
5944 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
5946 factor = GetTypeSize (underlying_type);
5948 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
5950 data = new byte [(count * factor + 4) & ~3];
5953 for (int i = 0; i < count; ++i) {
5954 object v = array_data [i];
5956 if (v is EnumConstant)
5957 v = ((EnumConstant) v).Child;
5959 if (v is Constant && !(v is StringConstant))
5960 v = ((Constant) v).GetValue ();
5966 if (underlying_type == TypeManager.int64_type){
5967 if (!(v is Expression)){
5968 long val = (long) v;
5970 for (int j = 0; j < factor; ++j) {
5971 data [idx + j] = (byte) (val & 0xFF);
5975 } else if (underlying_type == TypeManager.uint64_type){
5976 if (!(v is Expression)){
5977 ulong val = (ulong) v;
5979 for (int j = 0; j < factor; ++j) {
5980 data [idx + j] = (byte) (val & 0xFF);
5984 } else if (underlying_type == TypeManager.float_type) {
5985 if (!(v is Expression)){
5986 element = BitConverter.GetBytes ((float) v);
5988 for (int j = 0; j < factor; ++j)
5989 data [idx + j] = element [j];
5991 } else if (underlying_type == TypeManager.double_type) {
5992 if (!(v is Expression)){
5993 element = BitConverter.GetBytes ((double) v);
5995 for (int j = 0; j < factor; ++j)
5996 data [idx + j] = element [j];
5998 } else if (underlying_type == TypeManager.char_type){
5999 if (!(v is Expression)){
6000 int val = (int) ((char) v);
6002 data [idx] = (byte) (val & 0xff);
6003 data [idx+1] = (byte) (val >> 8);
6005 } else if (underlying_type == TypeManager.short_type){
6006 if (!(v is Expression)){
6007 int val = (int) ((short) v);
6009 data [idx] = (byte) (val & 0xff);
6010 data [idx+1] = (byte) (val >> 8);
6012 } else if (underlying_type == TypeManager.ushort_type){
6013 if (!(v is Expression)){
6014 int val = (int) ((ushort) v);
6016 data [idx] = (byte) (val & 0xff);
6017 data [idx+1] = (byte) (val >> 8);
6019 } else if (underlying_type == TypeManager.int32_type) {
6020 if (!(v is Expression)){
6023 data [idx] = (byte) (val & 0xff);
6024 data [idx+1] = (byte) ((val >> 8) & 0xff);
6025 data [idx+2] = (byte) ((val >> 16) & 0xff);
6026 data [idx+3] = (byte) (val >> 24);
6028 } else if (underlying_type == TypeManager.uint32_type) {
6029 if (!(v is Expression)){
6030 uint val = (uint) v;
6032 data [idx] = (byte) (val & 0xff);
6033 data [idx+1] = (byte) ((val >> 8) & 0xff);
6034 data [idx+2] = (byte) ((val >> 16) & 0xff);
6035 data [idx+3] = (byte) (val >> 24);
6037 } else if (underlying_type == TypeManager.sbyte_type) {
6038 if (!(v is Expression)){
6039 sbyte val = (sbyte) v;
6040 data [idx] = (byte) val;
6042 } else if (underlying_type == TypeManager.byte_type) {
6043 if (!(v is Expression)){
6044 byte val = (byte) v;
6045 data [idx] = (byte) val;
6047 } else if (underlying_type == TypeManager.bool_type) {
6048 if (!(v is Expression)){
6049 bool val = (bool) v;
6050 data [idx] = (byte) (val ? 1 : 0);
6052 } else if (underlying_type == TypeManager.decimal_type){
6053 if (!(v is Expression)){
6054 int [] bits = Decimal.GetBits ((decimal) v);
6057 // FIXME: For some reason, this doesn't work on the MS runtime.
6058 int [] nbits = new int [4];
6059 nbits [0] = bits [3];
6060 nbits [1] = bits [2];
6061 nbits [2] = bits [0];
6062 nbits [3] = bits [1];
6064 for (int j = 0; j < 4; j++){
6065 data [p++] = (byte) (nbits [j] & 0xff);
6066 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6067 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6068 data [p++] = (byte) (nbits [j] >> 24);
6072 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
6081 // Emits the initializers for the array
6083 void EmitStaticInitializers (EmitContext ec, bool is_expression)
6086 // First, the static data
6089 ILGenerator ig = ec.ig;
6091 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
6093 fb = RootContext.MakeStaticData (data);
6096 ig.Emit (OpCodes.Dup);
6097 ig.Emit (OpCodes.Ldtoken, fb);
6098 ig.Emit (OpCodes.Call,
6099 TypeManager.void_initializearray_array_fieldhandle);
6103 // Emits pieces of the array that can not be computed at compile
6104 // time (variables and string locations).
6106 // This always expect the top value on the stack to be the array
6108 void EmitDynamicInitializers (EmitContext ec, bool is_expression)
6110 ILGenerator ig = ec.ig;
6111 int dims = bounds.Count;
6112 int [] current_pos = new int [dims];
6113 int top = array_data.Count;
6114 LocalBuilder temp = ig.DeclareLocal (type);
6116 ig.Emit (OpCodes.Stloc, temp);
6118 MethodInfo set = null;
6122 ModuleBuilder mb = null;
6123 mb = CodeGen.ModuleBuilder;
6124 args = new Type [dims + 1];
6127 for (j = 0; j < dims; j++)
6128 args [j] = TypeManager.int32_type;
6130 args [j] = array_element_type;
6132 set = mb.GetArrayMethod (
6134 CallingConventions.HasThis | CallingConventions.Standard,
6135 TypeManager.void_type, args);
6138 for (int i = 0; i < top; i++){
6140 Expression e = null;
6142 if (array_data [i] is Expression)
6143 e = (Expression) array_data [i];
6147 // Basically we do this for string literals and
6148 // other non-literal expressions
6150 if (e is EnumConstant){
6151 e = ((EnumConstant) e).Child;
6154 if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
6155 num_automatic_initializers <= max_automatic_initializers) {
6156 Type etype = e.Type;
6158 ig.Emit (OpCodes.Ldloc, temp);
6160 for (int idx = 0; idx < dims; idx++)
6161 IntConstant.EmitInt (ig, current_pos [idx]);
6164 // If we are dealing with a struct, get the
6165 // address of it, so we can store it.
6168 etype.IsSubclassOf (TypeManager.value_type) &&
6169 (!TypeManager.IsBuiltinOrEnum (etype) ||
6170 etype == TypeManager.decimal_type)) {
6175 // Let new know that we are providing
6176 // the address where to store the results
6178 n.DisableTemporaryValueType ();
6181 ig.Emit (OpCodes.Ldelema, etype);
6187 ArrayAccess.EmitStoreOpcode (ig, array_element_type);
6189 ig.Emit (OpCodes.Call, set);
6197 for (int j = dims - 1; j >= 0; j--){
6199 if (current_pos [j] < (int) bounds [j])
6201 current_pos [j] = 0;
6206 ig.Emit (OpCodes.Ldloc, temp);
6209 void EmitArrayArguments (EmitContext ec)
6211 ILGenerator ig = ec.ig;
6213 foreach (Argument a in arguments) {
6214 Type atype = a.Type;
6217 if (atype == TypeManager.uint64_type)
6218 ig.Emit (OpCodes.Conv_Ovf_U4);
6219 else if (atype == TypeManager.int64_type)
6220 ig.Emit (OpCodes.Conv_Ovf_I4);
6224 void DoEmit (EmitContext ec, bool is_statement)
6226 ILGenerator ig = ec.ig;
6228 EmitArrayArguments (ec);
6229 if (is_one_dimensional)
6230 ig.Emit (OpCodes.Newarr, array_element_type);
6232 if (is_builtin_type)
6233 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
6235 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
6238 if (initializers != null){
6240 // FIXME: Set this variable correctly.
6242 bool dynamic_initializers = true;
6244 if (underlying_type != TypeManager.string_type &&
6245 underlying_type != TypeManager.decimal_type &&
6246 underlying_type != TypeManager.object_type) {
6247 if (num_automatic_initializers > max_automatic_initializers)
6248 EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
6251 if (dynamic_initializers)
6252 EmitDynamicInitializers (ec, !is_statement);
6256 public override void Emit (EmitContext ec)
6261 public override void EmitStatement (EmitContext ec)
6266 public object EncodeAsAttribute ()
6268 if (!is_one_dimensional){
6269 Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6273 if (array_data == null){
6274 Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6278 object [] ret = new object [array_data.Count];
6280 foreach (Expression e in array_data){
6283 if (e is NullLiteral)
6286 if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
6296 /// Represents the `this' construct
6298 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
6301 VariableInfo variable_info;
6303 public This (Block block, Location loc)
6309 public This (Location loc)
6314 public VariableInfo VariableInfo {
6315 get { return variable_info; }
6318 public bool VerifyFixed (bool is_expression)
6320 if ((variable_info == null) || (variable_info.LocalInfo == null))
6323 return variable_info.LocalInfo.IsFixed;
6326 public bool ResolveBase (EmitContext ec)
6328 eclass = ExprClass.Variable;
6329 type = ec.ContainerType;
6332 Error (26, "Keyword this not valid in static code");
6336 if ((block != null) && (block.ThisVariable != null))
6337 variable_info = block.ThisVariable.VariableInfo;
6342 public override Expression DoResolve (EmitContext ec)
6344 if (!ResolveBase (ec))
6347 if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
6348 Error (188, "The this object cannot be used before all " +
6349 "of its fields are assigned to");
6350 variable_info.SetAssigned (ec);
6354 if (ec.IsFieldInitializer) {
6355 Error (27, "Keyword `this' can't be used outside a constructor, " +
6356 "a method or a property.");
6363 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6365 if (!ResolveBase (ec))
6368 if (variable_info != null)
6369 variable_info.SetAssigned (ec);
6371 if (ec.TypeContainer is Class){
6372 Error (1604, "Cannot assign to `this'");
6379 public override void Emit (EmitContext ec)
6381 ILGenerator ig = ec.ig;
6383 ig.Emit (OpCodes.Ldarg_0);
6384 if (ec.TypeContainer is Struct)
6385 ig.Emit (OpCodes.Ldobj, type);
6388 public void EmitAssign (EmitContext ec, Expression source)
6390 ILGenerator ig = ec.ig;
6392 if (ec.TypeContainer is Struct){
6393 ig.Emit (OpCodes.Ldarg_0);
6395 ig.Emit (OpCodes.Stobj, type);
6398 ig.Emit (OpCodes.Starg, 0);
6402 public void AddressOf (EmitContext ec, AddressOp mode)
6404 ec.ig.Emit (OpCodes.Ldarg_0);
6407 // FIGURE OUT WHY LDARG_S does not work
6409 // consider: struct X { int val; int P { set { val = value; }}}
6411 // Yes, this looks very bad. Look at `NOTAS' for
6413 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
6418 // This produces the value that renders an instance, used by the iterators code
6420 public class ProxyInstance : Expression, IMemoryLocation {
6421 public override Expression DoResolve (EmitContext ec)
6423 eclass = ExprClass.Variable;
6424 type = ec.ContainerType;
6428 public override void Emit (EmitContext ec)
6430 ec.ig.Emit (OpCodes.Ldarg_0);
6434 public void AddressOf (EmitContext ec, AddressOp mode)
6436 ec.ig.Emit (OpCodes.Ldarg_0);
6441 /// Implements the typeof operator
6443 public class TypeOf : Expression {
6444 public readonly Expression QueriedType;
6445 protected Type typearg;
6447 public TypeOf (Expression queried_type, Location l)
6449 QueriedType = queried_type;
6453 public override Expression DoResolve (EmitContext ec)
6455 typearg = ec.DeclSpace.ResolveType (QueriedType, false, loc);
6457 if (typearg == null)
6460 if (typearg == TypeManager.void_type) {
6461 Error (673, "System.Void cannot be used from C# - " +
6462 "use typeof (void) to get the void type object");
6466 type = TypeManager.type_type;
6467 eclass = ExprClass.Type;
6471 public override void Emit (EmitContext ec)
6473 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6474 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6477 public Type TypeArg {
6478 get { return typearg; }
6483 /// Implements the `typeof (void)' operator
6485 public class TypeOfVoid : TypeOf {
6486 public TypeOfVoid (Location l) : base (null, l)
6491 public override Expression DoResolve (EmitContext ec)
6493 type = TypeManager.type_type;
6494 typearg = TypeManager.void_type;
6495 eclass = ExprClass.Type;
6501 /// Implements the sizeof expression
6503 public class SizeOf : Expression {
6504 public readonly Expression QueriedType;
6507 public SizeOf (Expression queried_type, Location l)
6509 this.QueriedType = queried_type;
6513 public override Expression DoResolve (EmitContext ec)
6517 233, loc, "Sizeof may only be used in an unsafe context " +
6518 "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
6522 type_queried = ec.DeclSpace.ResolveType (QueriedType, false, loc);
6523 if (type_queried == null)
6526 if (!TypeManager.IsUnmanagedType (type_queried)){
6527 Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
6531 type = TypeManager.int32_type;
6532 eclass = ExprClass.Value;
6536 public override void Emit (EmitContext ec)
6538 int size = GetTypeSize (type_queried);
6541 ec.ig.Emit (OpCodes.Sizeof, type_queried);
6543 IntConstant.EmitInt (ec.ig, size);
6548 /// Implements the member access expression
6550 public class MemberAccess : Expression {
6551 public readonly string Identifier;
6554 public MemberAccess (Expression expr, string id, Location l)
6561 public Expression Expr {
6567 static void error176 (Location loc, string name)
6569 Report.Error (176, loc, "Static member `" +
6570 name + "' cannot be accessed " +
6571 "with an instance reference, qualify with a " +
6572 "type name instead");
6575 static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
6577 if (left_original == null)
6580 if (!(left_original is SimpleName))
6583 SimpleName sn = (SimpleName) left_original;
6585 Type t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
6592 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
6593 Expression left, Location loc,
6594 Expression left_original)
6596 bool left_is_type, left_is_explicit;
6598 // If `left' is null, then we're called from SimpleNameResolve and this is
6599 // a member in the currently defining class.
6601 left_is_type = ec.IsStatic || ec.IsFieldInitializer;
6602 left_is_explicit = false;
6604 // Implicitly default to `this' unless we're static.
6605 if (!ec.IsStatic && !ec.IsFieldInitializer && !ec.InEnumContext)
6606 left = ec.GetThis (loc);
6608 left_is_type = left is TypeExpr;
6609 left_is_explicit = true;
6612 if (member_lookup is FieldExpr){
6613 FieldExpr fe = (FieldExpr) member_lookup;
6614 FieldInfo fi = fe.FieldInfo;
6615 Type decl_type = fi.DeclaringType;
6617 if (fi is FieldBuilder) {
6618 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
6621 object o = c.LookupConstantValue ();
6625 object real_value = ((Constant) c.Expr).GetValue ();
6627 return Constantify (real_value, fi.FieldType);
6632 Type t = fi.FieldType;
6636 if (fi is FieldBuilder)
6637 o = TypeManager.GetValue ((FieldBuilder) fi);
6639 o = fi.GetValue (fi);
6641 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
6642 if (left_is_explicit && !left_is_type &&
6643 !IdenticalNameAndTypeName (ec, left_original, loc)) {
6644 error176 (loc, fe.FieldInfo.Name);
6648 Expression enum_member = MemberLookup (
6649 ec, decl_type, "value__", MemberTypes.Field,
6650 AllBindingFlags, loc);
6652 Enum en = TypeManager.LookupEnum (decl_type);
6656 c = Constantify (o, en.UnderlyingType);
6658 c = Constantify (o, enum_member.Type);
6660 return new EnumConstant (c, decl_type);
6663 Expression exp = Constantify (o, t);
6665 if (left_is_explicit && !left_is_type) {
6666 error176 (loc, fe.FieldInfo.Name);
6673 if (fi.FieldType.IsPointer && !ec.InUnsafe){
6679 if (member_lookup is EventExpr) {
6680 EventExpr ee = (EventExpr) member_lookup;
6683 // If the event is local to this class, we transform ourselves into
6687 if (ee.EventInfo.DeclaringType == ec.ContainerType ||
6688 TypeManager.IsNestedChildOf(ec.ContainerType, ee.EventInfo.DeclaringType)) {
6689 MemberInfo mi = GetFieldFromEvent (ee);
6693 // If this happens, then we have an event with its own
6694 // accessors and private field etc so there's no need
6695 // to transform ourselves.
6700 Expression ml = ExprClassFromMemberInfo (ec, mi, loc);
6703 Report.Error (-200, loc, "Internal error!!");
6707 if (!left_is_explicit)
6710 return ResolveMemberAccess (ec, ml, left, loc, left_original);
6714 if (member_lookup is IMemberExpr) {
6715 IMemberExpr me = (IMemberExpr) member_lookup;
6718 MethodGroupExpr mg = me as MethodGroupExpr;
6719 if ((mg != null) && left_is_explicit && left.Type.IsInterface)
6720 mg.IsExplicitImpl = left_is_explicit;
6723 if ((ec.IsFieldInitializer || ec.IsStatic) &&
6724 IdenticalNameAndTypeName (ec, left_original, loc))
6725 return member_lookup;
6727 SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
6732 if (!me.IsInstance){
6733 if (IdenticalNameAndTypeName (ec, left_original, loc))
6734 return member_lookup;
6736 if (left_is_explicit) {
6737 error176 (loc, me.Name);
6743 // Since we can not check for instance objects in SimpleName,
6744 // becaue of the rule that allows types and variables to share
6745 // the name (as long as they can be de-ambiguated later, see
6746 // IdenticalNameAndTypeName), we have to check whether left
6747 // is an instance variable in a static context
6749 // However, if the left-hand value is explicitly given, then
6750 // it is already our instance expression, so we aren't in
6754 if (ec.IsStatic && !left_is_explicit && left is IMemberExpr){
6755 IMemberExpr mexp = (IMemberExpr) left;
6757 if (!mexp.IsStatic){
6758 SimpleName.Error_ObjectRefRequired (ec, loc, mexp.Name);
6763 me.InstanceExpression = left;
6766 return member_lookup;
6769 Console.WriteLine ("Left is: " + left);
6770 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
6771 Environment.Exit (0);
6775 public Expression DoResolve (EmitContext ec, Expression right_side, ResolveFlags flags)
6778 throw new Exception ();
6781 // Resolve the expression with flow analysis turned off, we'll do the definite
6782 // assignment checks later. This is because we don't know yet what the expression
6783 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
6784 // definite assignment check on the actual field and not on the whole struct.
6787 Expression original = expr;
6788 expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis);
6792 if (expr is SimpleName){
6793 SimpleName child_expr = (SimpleName) expr;
6795 Expression new_expr = new SimpleName (child_expr.Name, Identifier, loc);
6797 return new_expr.Resolve (ec, flags);
6801 // TODO: I mailed Ravi about this, and apparently we can get rid
6802 // of this and put it in the right place.
6804 // Handle enums here when they are in transit.
6805 // Note that we cannot afford to hit MemberLookup in this case because
6806 // it will fail to find any members at all
6809 Type expr_type = expr.Type;
6810 if (expr is TypeExpr){
6811 if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
6812 Error (122, "`" + expr_type + "' " +
6813 "is inaccessible because of its protection level");
6817 if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
6818 Enum en = TypeManager.LookupEnum (expr_type);
6821 object value = en.LookupEnumValue (ec, Identifier, loc);
6824 Constant c = Constantify (value, en.UnderlyingType);
6825 return new EnumConstant (c, expr_type);
6831 if (expr_type.IsPointer){
6832 Error (23, "The `.' operator can not be applied to pointer operands (" +
6833 TypeManager.CSharpName (expr_type) + ")");
6837 Expression member_lookup;
6838 member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
6839 if (member_lookup == null)
6842 if (member_lookup is TypeExpr) {
6843 if (!(expr is TypeExpr) && !(expr is SimpleName)) {
6844 Error (572, "Can't reference type `" + Identifier + "' through an expression; try `" +
6845 member_lookup.Type + "' instead");
6849 return member_lookup;
6852 member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
6853 if (member_lookup == null)
6856 // The following DoResolve/DoResolveLValue will do the definite assignment
6859 if (right_side != null)
6860 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
6862 member_lookup = member_lookup.DoResolve (ec);
6864 return member_lookup;
6867 public override Expression DoResolve (EmitContext ec)
6869 return DoResolve (ec, null, ResolveFlags.VariableOrValue |
6870 ResolveFlags.SimpleName | ResolveFlags.Type);
6873 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6875 return DoResolve (ec, right_side, ResolveFlags.VariableOrValue |
6876 ResolveFlags.SimpleName | ResolveFlags.Type);
6879 public override Expression ResolveAsTypeStep (EmitContext ec)
6881 string fname = null;
6882 MemberAccess full_expr = this;
6883 while (full_expr != null) {
6885 fname = String.Concat (full_expr.Identifier, ".", fname);
6887 fname = full_expr.Identifier;
6889 if (full_expr.Expr is SimpleName) {
6890 string full_name = String.Concat (((SimpleName) full_expr.Expr).Name, ".", fname);
6891 Type fully_qualified = ec.DeclSpace.FindType (loc, full_name);
6892 if (fully_qualified != null)
6893 return new TypeExpression (fully_qualified, loc);
6896 full_expr = full_expr.Expr as MemberAccess;
6899 Expression new_expr = expr.ResolveAsTypeStep (ec);
6901 if (new_expr == null)
6904 if (new_expr is SimpleName){
6905 SimpleName child_expr = (SimpleName) new_expr;
6907 new_expr = new SimpleName (child_expr.Name, Identifier, loc);
6909 return new_expr.ResolveAsTypeStep (ec);
6912 Type expr_type = new_expr.Type;
6914 if (expr_type.IsPointer){
6915 Error (23, "The `.' operator can not be applied to pointer operands (" +
6916 TypeManager.CSharpName (expr_type) + ")");
6920 Expression member_lookup;
6921 member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
6922 if (member_lookup == null)
6925 if (member_lookup is TypeExpr){
6926 member_lookup.Resolve (ec, ResolveFlags.Type);
6927 return member_lookup;
6933 public override void Emit (EmitContext ec)
6935 throw new Exception ("Should not happen");
6938 public override string ToString ()
6940 return expr + "." + Identifier;
6945 /// Implements checked expressions
6947 public class CheckedExpr : Expression {
6949 public Expression Expr;
6951 public CheckedExpr (Expression e, Location l)
6957 public override Expression DoResolve (EmitContext ec)
6959 bool last_check = ec.CheckState;
6960 bool last_const_check = ec.ConstantCheckState;
6962 ec.CheckState = true;
6963 ec.ConstantCheckState = true;
6964 Expr = Expr.Resolve (ec);
6965 ec.CheckState = last_check;
6966 ec.ConstantCheckState = last_const_check;
6971 if (Expr is Constant)
6974 eclass = Expr.eclass;
6979 public override void Emit (EmitContext ec)
6981 bool last_check = ec.CheckState;
6982 bool last_const_check = ec.ConstantCheckState;
6984 ec.CheckState = true;
6985 ec.ConstantCheckState = true;
6987 ec.CheckState = last_check;
6988 ec.ConstantCheckState = last_const_check;
6994 /// Implements the unchecked expression
6996 public class UnCheckedExpr : Expression {
6998 public Expression Expr;
7000 public UnCheckedExpr (Expression e, Location l)
7006 public override Expression DoResolve (EmitContext ec)
7008 bool last_check = ec.CheckState;
7009 bool last_const_check = ec.ConstantCheckState;
7011 ec.CheckState = false;
7012 ec.ConstantCheckState = false;
7013 Expr = Expr.Resolve (ec);
7014 ec.CheckState = last_check;
7015 ec.ConstantCheckState = last_const_check;
7020 if (Expr is Constant)
7023 eclass = Expr.eclass;
7028 public override void Emit (EmitContext ec)
7030 bool last_check = ec.CheckState;
7031 bool last_const_check = ec.ConstantCheckState;
7033 ec.CheckState = false;
7034 ec.ConstantCheckState = false;
7036 ec.CheckState = last_check;
7037 ec.ConstantCheckState = last_const_check;
7043 /// An Element Access expression.
7045 /// During semantic analysis these are transformed into
7046 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7048 public class ElementAccess : Expression {
7049 public ArrayList Arguments;
7050 public Expression Expr;
7052 public ElementAccess (Expression e, ArrayList e_list, Location l)
7061 Arguments = new ArrayList ();
7062 foreach (Expression tmp in e_list)
7063 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7067 bool CommonResolve (EmitContext ec)
7069 Expr = Expr.Resolve (ec);
7074 if (Arguments == null)
7077 foreach (Argument a in Arguments){
7078 if (!a.Resolve (ec, loc))
7085 Expression MakePointerAccess ()
7089 if (t == TypeManager.void_ptr_type){
7090 Error (242, "The array index operation is not valid for void pointers");
7093 if (Arguments.Count != 1){
7094 Error (196, "A pointer must be indexed by a single value");
7099 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
7100 return new Indirection (p, loc);
7103 public override Expression DoResolve (EmitContext ec)
7105 if (!CommonResolve (ec))
7109 // We perform some simple tests, and then to "split" the emit and store
7110 // code we create an instance of a different class, and return that.
7112 // I am experimenting with this pattern.
7116 if (t == TypeManager.array_type){
7117 Report.Error (21, loc, "Cannot use indexer on System.Array");
7122 return (new ArrayAccess (this, loc)).Resolve (ec);
7123 else if (t.IsPointer)
7124 return MakePointerAccess ();
7126 return (new IndexerAccess (this, loc)).Resolve (ec);
7129 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7131 if (!CommonResolve (ec))
7136 return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
7137 else if (t.IsPointer)
7138 return MakePointerAccess ();
7140 return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
7143 public override void Emit (EmitContext ec)
7145 throw new Exception ("Should never be reached");
7150 /// Implements array access
7152 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7154 // Points to our "data" repository
7158 LocalTemporary [] cached_locations;
7160 public ArrayAccess (ElementAccess ea_data, Location l)
7163 eclass = ExprClass.Variable;
7167 public override Expression DoResolve (EmitContext ec)
7170 ExprClass eclass = ea.Expr.eclass;
7172 // As long as the type is valid
7173 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7174 eclass == ExprClass.Value)) {
7175 ea.Expr.Error_UnexpectedKind ("variable or value");
7180 Type t = ea.Expr.Type;
7181 if (t.GetArrayRank () != ea.Arguments.Count){
7183 "Incorrect number of indexes for array " +
7184 " expected: " + t.GetArrayRank () + " got: " +
7185 ea.Arguments.Count);
7189 type = TypeManager.GetElementType (t);
7190 if (type.IsPointer && !ec.InUnsafe){
7191 UnsafeError (ea.Location);
7195 foreach (Argument a in ea.Arguments){
7196 Type argtype = a.Type;
7198 if (argtype == TypeManager.int32_type ||
7199 argtype == TypeManager.uint32_type ||
7200 argtype == TypeManager.int64_type ||
7201 argtype == TypeManager.uint64_type)
7205 // Mhm. This is strage, because the Argument.Type is not the same as
7206 // Argument.Expr.Type: the value changes depending on the ref/out setting.
7208 // Wonder if I will run into trouble for this.
7210 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
7215 eclass = ExprClass.Variable;
7221 /// Emits the right opcode to load an object of Type `t'
7222 /// from an array of T
7224 static public void EmitLoadOpcode (ILGenerator ig, Type type)
7226 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7227 ig.Emit (OpCodes.Ldelem_U1);
7228 else if (type == TypeManager.sbyte_type)
7229 ig.Emit (OpCodes.Ldelem_I1);
7230 else if (type == TypeManager.short_type)
7231 ig.Emit (OpCodes.Ldelem_I2);
7232 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7233 ig.Emit (OpCodes.Ldelem_U2);
7234 else if (type == TypeManager.int32_type)
7235 ig.Emit (OpCodes.Ldelem_I4);
7236 else if (type == TypeManager.uint32_type)
7237 ig.Emit (OpCodes.Ldelem_U4);
7238 else if (type == TypeManager.uint64_type)
7239 ig.Emit (OpCodes.Ldelem_I8);
7240 else if (type == TypeManager.int64_type)
7241 ig.Emit (OpCodes.Ldelem_I8);
7242 else if (type == TypeManager.float_type)
7243 ig.Emit (OpCodes.Ldelem_R4);
7244 else if (type == TypeManager.double_type)
7245 ig.Emit (OpCodes.Ldelem_R8);
7246 else if (type == TypeManager.intptr_type)
7247 ig.Emit (OpCodes.Ldelem_I);
7248 else if (type.IsValueType){
7249 ig.Emit (OpCodes.Ldelema, type);
7250 ig.Emit (OpCodes.Ldobj, type);
7252 ig.Emit (OpCodes.Ldelem_Ref);
7256 /// Emits the right opcode to store an object of Type `t'
7257 /// from an array of T.
7259 static public void EmitStoreOpcode (ILGenerator ig, Type t)
7262 OpCode op = GetStoreOpcode (t, out is_stobj);
7264 ig.Emit (OpCodes.Stobj, t);
7270 /// Returns the right opcode to store an object of Type `t'
7271 /// from an array of T.
7273 static public OpCode GetStoreOpcode (Type t, out bool is_stobj)
7275 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7277 t = TypeManager.TypeToCoreType (t);
7278 if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type)
7279 t = TypeManager.EnumToUnderlying (t);
7280 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7281 t == TypeManager.bool_type)
7282 return OpCodes.Stelem_I1;
7283 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7284 t == TypeManager.char_type)
7285 return OpCodes.Stelem_I2;
7286 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7287 return OpCodes.Stelem_I4;
7288 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7289 return OpCodes.Stelem_I8;
7290 else if (t == TypeManager.float_type)
7291 return OpCodes.Stelem_R4;
7292 else if (t == TypeManager.double_type)
7293 return OpCodes.Stelem_R8;
7294 else if (t == TypeManager.intptr_type) {
7296 return OpCodes.Stobj;
7297 } else if (t.IsValueType) {
7299 return OpCodes.Stobj;
7301 return OpCodes.Stelem_Ref;
7304 MethodInfo FetchGetMethod ()
7306 ModuleBuilder mb = CodeGen.ModuleBuilder;
7307 int arg_count = ea.Arguments.Count;
7308 Type [] args = new Type [arg_count];
7311 for (int i = 0; i < arg_count; i++){
7312 //args [i++] = a.Type;
7313 args [i] = TypeManager.int32_type;
7316 get = mb.GetArrayMethod (
7317 ea.Expr.Type, "Get",
7318 CallingConventions.HasThis |
7319 CallingConventions.Standard,
7325 MethodInfo FetchAddressMethod ()
7327 ModuleBuilder mb = CodeGen.ModuleBuilder;
7328 int arg_count = ea.Arguments.Count;
7329 Type [] args = new Type [arg_count];
7333 ret_type = TypeManager.GetReferenceType (type);
7335 for (int i = 0; i < arg_count; i++){
7336 //args [i++] = a.Type;
7337 args [i] = TypeManager.int32_type;
7340 address = mb.GetArrayMethod (
7341 ea.Expr.Type, "Address",
7342 CallingConventions.HasThis |
7343 CallingConventions.Standard,
7350 // Load the array arguments into the stack.
7352 // If we have been requested to cache the values (cached_locations array
7353 // initialized), then load the arguments the first time and store them
7354 // in locals. otherwise load from local variables.
7356 void LoadArrayAndArguments (EmitContext ec)
7358 ILGenerator ig = ec.ig;
7360 if (cached_locations == null){
7362 foreach (Argument a in ea.Arguments){
7363 Type argtype = a.Expr.Type;
7367 if (argtype == TypeManager.int64_type)
7368 ig.Emit (OpCodes.Conv_Ovf_I);
7369 else if (argtype == TypeManager.uint64_type)
7370 ig.Emit (OpCodes.Conv_Ovf_I_Un);
7375 if (cached_locations [0] == null){
7376 cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
7378 ig.Emit (OpCodes.Dup);
7379 cached_locations [0].Store (ec);
7383 foreach (Argument a in ea.Arguments){
7384 Type argtype = a.Expr.Type;
7386 cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
7388 if (argtype == TypeManager.int64_type)
7389 ig.Emit (OpCodes.Conv_Ovf_I);
7390 else if (argtype == TypeManager.uint64_type)
7391 ig.Emit (OpCodes.Conv_Ovf_I_Un);
7393 ig.Emit (OpCodes.Dup);
7394 cached_locations [j].Store (ec);
7400 foreach (LocalTemporary lt in cached_locations)
7404 public new void CacheTemporaries (EmitContext ec)
7406 cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
7409 public override void Emit (EmitContext ec)
7411 int rank = ea.Expr.Type.GetArrayRank ();
7412 ILGenerator ig = ec.ig;
7414 LoadArrayAndArguments (ec);
7417 EmitLoadOpcode (ig, type);
7421 method = FetchGetMethod ();
7422 ig.Emit (OpCodes.Call, method);
7426 public void EmitAssign (EmitContext ec, Expression source)
7428 int rank = ea.Expr.Type.GetArrayRank ();
7429 ILGenerator ig = ec.ig;
7430 Type t = source.Type;
7432 LoadArrayAndArguments (ec);
7435 // The stobj opcode used by value types will need
7436 // an address on the stack, not really an array/array
7440 if (t == TypeManager.enum_type || t == TypeManager.decimal_type ||
7441 (t.IsSubclassOf (TypeManager.value_type) && !TypeManager.IsEnumType (t) && !TypeManager.IsBuiltinType (t)))
7442 ig.Emit (OpCodes.Ldelema, t);
7448 EmitStoreOpcode (ig, t);
7450 ModuleBuilder mb = CodeGen.ModuleBuilder;
7451 int arg_count = ea.Arguments.Count;
7452 Type [] args = new Type [arg_count + 1];
7455 for (int i = 0; i < arg_count; i++){
7456 //args [i++] = a.Type;
7457 args [i] = TypeManager.int32_type;
7460 args [arg_count] = type;
7462 set = mb.GetArrayMethod (
7463 ea.Expr.Type, "Set",
7464 CallingConventions.HasThis |
7465 CallingConventions.Standard,
7466 TypeManager.void_type, args);
7468 ig.Emit (OpCodes.Call, set);
7472 public void AddressOf (EmitContext ec, AddressOp mode)
7474 int rank = ea.Expr.Type.GetArrayRank ();
7475 ILGenerator ig = ec.ig;
7477 LoadArrayAndArguments (ec);
7480 ig.Emit (OpCodes.Ldelema, type);
7482 MethodInfo address = FetchAddressMethod ();
7483 ig.Emit (OpCodes.Call, address);
7490 public ArrayList Properties;
7491 static Hashtable map;
7493 public struct Indexer {
7494 public readonly Type Type;
7495 public readonly MethodInfo Getter, Setter;
7497 public Indexer (Type type, MethodInfo get, MethodInfo set)
7507 map = new Hashtable ();
7512 Properties = new ArrayList ();
7515 void Append (MemberInfo [] mi)
7517 foreach (PropertyInfo property in mi){
7518 MethodInfo get, set;
7520 get = property.GetGetMethod (true);
7521 set = property.GetSetMethod (true);
7522 Properties.Add (new Indexer (property.PropertyType, get, set));
7526 static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
7528 string p_name = TypeManager.IndexerPropertyName (lookup_type);
7530 MemberInfo [] mi = TypeManager.MemberLookup (
7531 caller_type, caller_type, lookup_type, MemberTypes.Property,
7532 BindingFlags.Public | BindingFlags.Instance |
7533 BindingFlags.DeclaredOnly, p_name);
7535 if (mi == null || mi.Length == 0)
7541 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
7543 Indexers ix = (Indexers) map [lookup_type];
7548 Type copy = lookup_type;
7549 while (copy != TypeManager.object_type && copy != null){
7550 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy);
7554 ix = new Indexers ();
7559 copy = copy.BaseType;
7562 if (!lookup_type.IsInterface)
7565 TypeExpr [] ifaces = TypeManager.GetInterfaces (lookup_type);
7566 if (ifaces != null) {
7567 foreach (TypeExpr iface in ifaces) {
7568 Type itype = iface.Type;
7569 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype);
7572 ix = new Indexers ();
7584 /// Expressions that represent an indexer call.
7586 public class IndexerAccess : Expression, IAssignMethod {
7588 // Points to our "data" repository
7590 MethodInfo get, set;
7591 ArrayList set_arguments;
7592 bool is_base_indexer;
7594 protected Type indexer_type;
7595 protected Type current_type;
7596 protected Expression instance_expr;
7597 protected ArrayList arguments;
7599 public IndexerAccess (ElementAccess ea, Location loc)
7600 : this (ea.Expr, false, loc)
7602 this.arguments = ea.Arguments;
7605 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
7608 this.instance_expr = instance_expr;
7609 this.is_base_indexer = is_base_indexer;
7610 this.eclass = ExprClass.Value;
7614 protected virtual bool CommonResolve (EmitContext ec)
7616 indexer_type = instance_expr.Type;
7617 current_type = ec.ContainerType;
7622 public override Expression DoResolve (EmitContext ec)
7624 ArrayList AllGetters = new ArrayList();
7625 if (!CommonResolve (ec))
7629 // Step 1: Query for all `Item' *properties*. Notice
7630 // that the actual methods are pointed from here.
7632 // This is a group of properties, piles of them.
7634 bool found_any = false, found_any_getters = false;
7635 Type lookup_type = indexer_type;
7638 ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
7639 if (ilist != null) {
7641 if (ilist.Properties != null) {
7642 foreach (Indexers.Indexer ix in ilist.Properties) {
7643 if (ix.Getter != null)
7644 AllGetters.Add(ix.Getter);
7649 if (AllGetters.Count > 0) {
7650 found_any_getters = true;
7651 get = (MethodInfo) Invocation.OverloadResolve (
7652 ec, new MethodGroupExpr (AllGetters, loc), arguments, loc);
7656 Report.Error (21, loc,
7657 "Type `" + TypeManager.CSharpName (indexer_type) +
7658 "' does not have any indexers defined");
7662 if (!found_any_getters) {
7663 Error (154, "indexer can not be used in this context, because " +
7664 "it lacks a `get' accessor");
7669 Error (1501, "No Overload for method `this' takes `" +
7670 arguments.Count + "' arguments");
7675 // Only base will allow this invocation to happen.
7677 if (get.IsAbstract && this is BaseIndexerAccess){
7678 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (get));
7682 type = get.ReturnType;
7683 if (type.IsPointer && !ec.InUnsafe){
7688 eclass = ExprClass.IndexerAccess;
7692 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7694 ArrayList AllSetters = new ArrayList();
7695 if (!CommonResolve (ec))
7698 bool found_any = false, found_any_setters = false;
7700 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
7701 if (ilist != null) {
7703 if (ilist.Properties != null) {
7704 foreach (Indexers.Indexer ix in ilist.Properties) {
7705 if (ix.Setter != null)
7706 AllSetters.Add(ix.Setter);
7710 if (AllSetters.Count > 0) {
7711 found_any_setters = true;
7712 set_arguments = (ArrayList) arguments.Clone ();
7713 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
7714 set = (MethodInfo) Invocation.OverloadResolve (
7715 ec, new MethodGroupExpr (AllSetters, loc),
7716 set_arguments, loc);
7720 Report.Error (21, loc,
7721 "Type `" + TypeManager.CSharpName (indexer_type) +
7722 "' does not have any indexers defined");
7726 if (!found_any_setters) {
7727 Error (154, "indexer can not be used in this context, because " +
7728 "it lacks a `set' accessor");
7733 Error (1501, "No Overload for method `this' takes `" +
7734 arguments.Count + "' arguments");
7739 // Only base will allow this invocation to happen.
7741 if (set.IsAbstract && this is BaseIndexerAccess){
7742 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set));
7747 // Now look for the actual match in the list of indexers to set our "return" type
7749 type = TypeManager.void_type; // default value
7750 foreach (Indexers.Indexer ix in ilist.Properties){
7751 if (ix.Setter == set){
7757 eclass = ExprClass.IndexerAccess;
7761 public override void Emit (EmitContext ec)
7763 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc);
7767 // source is ignored, because we already have a copy of it from the
7768 // LValue resolution and we have already constructed a pre-cached
7769 // version of the arguments (ea.set_arguments);
7771 public void EmitAssign (EmitContext ec, Expression source)
7773 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc);
7778 /// The base operator for method names
7780 public class BaseAccess : Expression {
7783 public BaseAccess (string member, Location l)
7785 this.member = member;
7789 public override Expression DoResolve (EmitContext ec)
7791 Expression c = CommonResolve (ec);
7797 // MethodGroups use this opportunity to flag an error on lacking ()
7799 if (!(c is MethodGroupExpr))
7800 return c.Resolve (ec);
7804 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7806 Expression c = CommonResolve (ec);
7812 // MethodGroups use this opportunity to flag an error on lacking ()
7814 if (! (c is MethodGroupExpr))
7815 return c.DoResolveLValue (ec, right_side);
7820 Expression CommonResolve (EmitContext ec)
7822 Expression member_lookup;
7823 Type current_type = ec.ContainerType;
7824 Type base_type = current_type.BaseType;
7828 Error (1511, "Keyword base is not allowed in static method");
7832 member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member,
7833 AllMemberTypes, AllBindingFlags, loc);
7834 if (member_lookup == null) {
7835 MemberLookupFailed (ec, base_type, base_type, member, null, loc);
7842 left = new TypeExpression (base_type, loc);
7844 left = ec.GetThis (loc);
7846 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
7848 if (e is PropertyExpr){
7849 PropertyExpr pe = (PropertyExpr) e;
7857 public override void Emit (EmitContext ec)
7859 throw new Exception ("Should never be called");
7864 /// The base indexer operator
7866 public class BaseIndexerAccess : IndexerAccess {
7867 public BaseIndexerAccess (ArrayList args, Location loc)
7868 : base (null, true, loc)
7870 arguments = new ArrayList ();
7871 foreach (Expression tmp in args)
7872 arguments.Add (new Argument (tmp, Argument.AType.Expression));
7875 protected override bool CommonResolve (EmitContext ec)
7877 instance_expr = ec.GetThis (loc);
7879 current_type = ec.ContainerType.BaseType;
7880 indexer_type = current_type;
7882 foreach (Argument a in arguments){
7883 if (!a.Resolve (ec, loc))
7892 /// This class exists solely to pass the Type around and to be a dummy
7893 /// that can be passed to the conversion functions (this is used by
7894 /// foreach implementation to typecast the object return value from
7895 /// get_Current into the proper type. All code has been generated and
7896 /// we only care about the side effect conversions to be performed
7898 /// This is also now used as a placeholder where a no-action expression
7899 /// is needed (the `New' class).
7901 public class EmptyExpression : Expression {
7902 public EmptyExpression ()
7904 type = TypeManager.object_type;
7905 eclass = ExprClass.Value;
7906 loc = Location.Null;
7909 public EmptyExpression (Type t)
7912 eclass = ExprClass.Value;
7913 loc = Location.Null;
7916 public override Expression DoResolve (EmitContext ec)
7921 public override void Emit (EmitContext ec)
7923 // nothing, as we only exist to not do anything.
7927 // This is just because we might want to reuse this bad boy
7928 // instead of creating gazillions of EmptyExpressions.
7929 // (CanImplicitConversion uses it)
7931 public void SetType (Type t)
7937 public class UserCast : Expression {
7941 public UserCast (MethodInfo method, Expression source, Location l)
7943 this.method = method;
7944 this.source = source;
7945 type = method.ReturnType;
7946 eclass = ExprClass.Value;
7950 public override Expression DoResolve (EmitContext ec)
7953 // We are born fully resolved
7958 public override void Emit (EmitContext ec)
7960 ILGenerator ig = ec.ig;
7964 if (method is MethodInfo)
7965 ig.Emit (OpCodes.Call, (MethodInfo) method);
7967 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
7973 // This class is used to "construct" the type during a typecast
7974 // operation. Since the Type.GetType class in .NET can parse
7975 // the type specification, we just use this to construct the type
7976 // one bit at a time.
7978 public class ComposedCast : TypeExpr {
7982 public ComposedCast (Expression left, string dim, Location l)
7989 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
7991 Type ltype = ec.DeclSpace.ResolveType (left, false, loc);
7996 // ltype.Fullname is already fully qualified, so we can skip
7997 // a lot of probes, and go directly to TypeManager.LookupType
7999 string cname = ltype.FullName + dim;
8000 type = TypeManager.LookupTypeDirect (cname);
8003 // For arrays of enumerations we are having a problem
8004 // with the direct lookup. Need to investigate.
8006 // For now, fall back to the full lookup in that case.
8008 type = RootContext.LookupType (
8009 ec.DeclSpace, cname, false, loc);
8015 if (!ec.ResolvingTypeTree){
8017 // If the above flag is set, this is being invoked from the ResolveType function.
8018 // Upper layers take care of the type validity in this context.
8020 if (!ec.InUnsafe && type.IsPointer){
8026 eclass = ExprClass.Type;
8030 public override string Name {
8038 // This class is used to represent the address of an array, used
8039 // only by the Fixed statement, this is like the C "&a [0]" construct.
8041 public class ArrayPtr : Expression {
8044 public ArrayPtr (Expression array, Location l)
8046 Type array_type = TypeManager.GetElementType (array.Type);
8050 type = TypeManager.GetPointerType (array_type);
8051 eclass = ExprClass.Value;
8055 public override void Emit (EmitContext ec)
8057 ILGenerator ig = ec.ig;
8060 IntLiteral.EmitInt (ig, 0);
8061 ig.Emit (OpCodes.Ldelema, TypeManager.GetElementType (array.Type));
8064 public override Expression DoResolve (EmitContext ec)
8067 // We are born fully resolved
8074 // Used by the fixed statement
8076 public class StringPtr : Expression {
8079 public StringPtr (LocalBuilder b, Location l)
8082 eclass = ExprClass.Value;
8083 type = TypeManager.char_ptr_type;
8087 public override Expression DoResolve (EmitContext ec)
8089 // This should never be invoked, we are born in fully
8090 // initialized state.
8095 public override void Emit (EmitContext ec)
8097 ILGenerator ig = ec.ig;
8099 ig.Emit (OpCodes.Ldloc, b);
8100 ig.Emit (OpCodes.Conv_I);
8101 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8102 ig.Emit (OpCodes.Add);
8107 // Implements the `stackalloc' keyword
8109 public class StackAlloc : Expression {
8114 public StackAlloc (Expression type, Expression count, Location l)
8121 public override Expression DoResolve (EmitContext ec)
8123 count = count.Resolve (ec);
8127 if (count.Type != TypeManager.int32_type){
8128 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8133 if (ec.InCatch || ec.InFinally){
8135 "stackalloc can not be used in a catch or finally block");
8139 otype = ec.DeclSpace.ResolveType (t, false, loc);
8144 if (!TypeManager.VerifyUnManaged (otype, loc))
8147 type = TypeManager.GetPointerType (otype);
8148 eclass = ExprClass.Value;
8153 public override void Emit (EmitContext ec)
8155 int size = GetTypeSize (otype);
8156 ILGenerator ig = ec.ig;
8159 ig.Emit (OpCodes.Sizeof, otype);
8161 IntConstant.EmitInt (ig, size);
8163 ig.Emit (OpCodes.Mul);
8164 ig.Emit (OpCodes.Localloc);