2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 // (C) 2003, 2004 Novell, Inc.
12 namespace Mono.CSharp {
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// This is just a helper class, it is generated by Unary, UnaryMutator
21 /// when an overloaded method has been found. It just emits the code for a
24 public class StaticCallExpr : ExpressionStatement {
28 public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
34 eclass = ExprClass.Value;
38 public override Expression DoResolve (EmitContext ec)
41 // We are born fully resolved
46 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, mi, args, false, null);
51 ec.ig.Emit (OpCodes.Call, mi);
55 static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
56 Expression e, Location loc)
61 args = new ArrayList (1);
62 Argument a = new Argument (e, Argument.AType.Expression);
64 // We need to resolve the arguments before sending them in !
65 if (!a.Resolve (ec, loc))
69 method = Invocation.OverloadResolve (
70 ec, (MethodGroupExpr) mg, args, false, loc);
75 return new StaticCallExpr ((MethodInfo) method, args, loc);
78 public override void EmitStatement (EmitContext ec)
81 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
82 ec.ig.Emit (OpCodes.Pop);
85 public MethodInfo Method {
90 public class ParenthesizedExpression : Expression
92 public Expression Expr;
94 public ParenthesizedExpression (Expression expr, Location loc)
100 public override Expression DoResolve (EmitContext ec)
102 Expr = Expr.Resolve (ec);
106 public override void Emit (EmitContext ec)
108 throw new Exception ("Should not happen");
113 /// Unary expressions.
117 /// Unary implements unary expressions. It derives from
118 /// ExpressionStatement becuase the pre/post increment/decrement
119 /// operators can be used in a statement context.
121 public class Unary : Expression {
122 public enum Operator : byte {
123 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
124 Indirection, AddressOf, TOP
127 public Operator Oper;
128 public Expression Expr;
130 public Unary (Operator op, Expression expr, Location loc)
138 /// Returns a stringified representation of the Operator
140 static public string OperName (Operator oper)
143 case Operator.UnaryPlus:
145 case Operator.UnaryNegation:
147 case Operator.LogicalNot:
149 case Operator.OnesComplement:
151 case Operator.AddressOf:
153 case Operator.Indirection:
157 return oper.ToString ();
160 public static readonly string [] oper_names;
164 oper_names = new string [(int)Operator.TOP];
166 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
167 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
168 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
169 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
170 oper_names [(int) Operator.Indirection] = "op_Indirection";
171 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
174 void Error23 (Type t)
177 23, "Operator " + OperName (Oper) +
178 " cannot be applied to operand of type `" +
179 TypeManager.CSharpName (t) + "'");
183 /// The result has been already resolved:
185 /// FIXME: a minus constant -128 sbyte cant be turned into a
188 static Expression TryReduceNegative (Constant expr)
192 if (expr is IntConstant)
193 e = new IntConstant (-((IntConstant) expr).Value);
194 else if (expr is UIntConstant){
195 uint value = ((UIntConstant) expr).Value;
197 if (value < 2147483649)
198 return new IntConstant (-(int)value);
200 e = new LongConstant (-value);
202 else if (expr is LongConstant)
203 e = new LongConstant (-((LongConstant) expr).Value);
204 else if (expr is ULongConstant){
205 ulong value = ((ULongConstant) expr).Value;
207 if (value < 9223372036854775809)
208 return new LongConstant(-(long)value);
210 else if (expr is FloatConstant)
211 e = new FloatConstant (-((FloatConstant) expr).Value);
212 else if (expr is DoubleConstant)
213 e = new DoubleConstant (-((DoubleConstant) expr).Value);
214 else if (expr is DecimalConstant)
215 e = new DecimalConstant (-((DecimalConstant) expr).Value);
216 else if (expr is ShortConstant)
217 e = new IntConstant (-((ShortConstant) expr).Value);
218 else if (expr is UShortConstant)
219 e = new IntConstant (-((UShortConstant) expr).Value);
220 else if (expr is SByteConstant)
221 e = new IntConstant (-((SByteConstant) expr).Value);
222 else if (expr is ByteConstant)
223 e = new IntConstant (-((ByteConstant) expr).Value);
228 // This routine will attempt to simplify the unary expression when the
229 // argument is a constant. The result is returned in `result' and the
230 // function returns true or false depending on whether a reduction
231 // was performed or not
233 bool Reduce (EmitContext ec, Constant e, out Expression result)
235 Type expr_type = e.Type;
238 case Operator.UnaryPlus:
242 case Operator.UnaryNegation:
243 result = TryReduceNegative (e);
244 return result != null;
246 case Operator.LogicalNot:
247 if (expr_type != TypeManager.bool_type) {
253 BoolConstant b = (BoolConstant) e;
254 result = new BoolConstant (!(b.Value));
257 case Operator.OnesComplement:
258 if (!((expr_type == TypeManager.int32_type) ||
259 (expr_type == TypeManager.uint32_type) ||
260 (expr_type == TypeManager.int64_type) ||
261 (expr_type == TypeManager.uint64_type) ||
262 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
265 if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
266 result = new Cast (new TypeExpression (TypeManager.int32_type, loc), e, loc);
267 result = result.Resolve (ec);
268 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
269 result = new Cast (new TypeExpression (TypeManager.uint32_type, loc), e, loc);
270 result = result.Resolve (ec);
271 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
272 result = new Cast (new TypeExpression (TypeManager.int64_type, loc), e, loc);
273 result = result.Resolve (ec);
274 } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
275 result = new Cast (new TypeExpression (TypeManager.uint64_type, loc), e, loc);
276 result = result.Resolve (ec);
279 if (result == null || !(result is Constant)){
285 expr_type = result.Type;
286 e = (Constant) result;
289 if (e is EnumConstant){
290 EnumConstant enum_constant = (EnumConstant) e;
293 if (Reduce (ec, enum_constant.Child, out reduced)){
294 result = new EnumConstant ((Constant) reduced, enum_constant.Type);
302 if (expr_type == TypeManager.int32_type){
303 result = new IntConstant (~ ((IntConstant) e).Value);
304 } else if (expr_type == TypeManager.uint32_type){
305 result = new UIntConstant (~ ((UIntConstant) e).Value);
306 } else if (expr_type == TypeManager.int64_type){
307 result = new LongConstant (~ ((LongConstant) e).Value);
308 } else if (expr_type == TypeManager.uint64_type){
309 result = new ULongConstant (~ ((ULongConstant) e).Value);
317 case Operator.AddressOf:
321 case Operator.Indirection:
325 throw new Exception ("Can not constant fold: " + Oper.ToString());
328 Expression ResolveOperator (EmitContext ec)
331 // Step 1: Default operations on CLI native types.
334 // Attempt to use a constant folding operation.
335 if (Expr is Constant){
338 if (Reduce (ec, (Constant) Expr, out result))
343 // Step 2: Perform Operator Overload location
345 Type expr_type = Expr.Type;
349 op_name = oper_names [(int) Oper];
351 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
354 Expression e = StaticCallExpr.MakeSimpleCall (
355 ec, (MethodGroupExpr) mg, Expr, loc);
365 // Only perform numeric promotions on:
368 if (expr_type == null)
372 case Operator.LogicalNot:
373 if (expr_type != TypeManager.bool_type) {
374 Expr = ResolveBoolean (ec, Expr, loc);
381 type = TypeManager.bool_type;
384 case Operator.OnesComplement:
385 if (!((expr_type == TypeManager.int32_type) ||
386 (expr_type == TypeManager.uint32_type) ||
387 (expr_type == TypeManager.int64_type) ||
388 (expr_type == TypeManager.uint64_type) ||
389 (expr_type.IsSubclassOf (TypeManager.enum_type)))){
392 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
394 type = TypeManager.int32_type;
397 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
399 type = TypeManager.uint32_type;
402 e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
404 type = TypeManager.int64_type;
407 e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
409 type = TypeManager.uint64_type;
418 case Operator.AddressOf:
419 if (Expr.eclass != ExprClass.Variable){
420 Error (211, "Cannot take the address of non-variables");
429 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
433 IVariable variable = Expr as IVariable;
434 bool is_fixed = variable != null && variable.VerifyFixed (false);
436 if (!ec.InFixedInitializer && !is_fixed) {
437 Error (212, "You can only take the address of an unfixed expression inside " +
438 "of a fixed statement initializer");
442 if (ec.InFixedInitializer && is_fixed) {
443 Error (213, "You can not fix an already fixed expression");
447 LocalVariableReference lr = Expr as LocalVariableReference;
449 if (lr.local_info.IsCaptured){
450 AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
453 lr.local_info.AddressTaken = true;
454 lr.local_info.Used = true;
457 // According to the specs, a variable is considered definitely assigned if you take
459 if ((variable != null) && (variable.VariableInfo != null)){
460 variable.VariableInfo.SetAssigned (ec);
463 type = TypeManager.GetPointerType (Expr.Type);
466 case Operator.Indirection:
472 if (!expr_type.IsPointer){
473 Error (193, "The * or -> operator can only be applied to pointers");
478 // We create an Indirection expression, because
479 // it can implement the IMemoryLocation.
481 return new Indirection (Expr, loc);
483 case Operator.UnaryPlus:
485 // A plus in front of something is just a no-op, so return the child.
489 case Operator.UnaryNegation:
491 // Deals with -literals
492 // int operator- (int x)
493 // long operator- (long x)
494 // float operator- (float f)
495 // double operator- (double d)
496 // decimal operator- (decimal d)
498 Expression expr = null;
501 // transform - - expr into expr
504 Unary unary = (Unary) Expr;
506 if (unary.Oper == Operator.UnaryNegation)
511 // perform numeric promotions to int,
515 // The following is inneficient, because we call
516 // ImplicitConversion too many times.
518 // It is also not clear if we should convert to Float
519 // or Double initially.
521 if (expr_type == TypeManager.uint32_type){
523 // FIXME: handle exception to this rule that
524 // permits the int value -2147483648 (-2^31) to
525 // bt wrote as a decimal interger literal
527 type = TypeManager.int64_type;
528 Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
532 if (expr_type == TypeManager.uint64_type){
534 // FIXME: Handle exception of `long value'
535 // -92233720368547758087 (-2^63) to be wrote as
536 // decimal integer literal.
542 if (expr_type == TypeManager.float_type){
547 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
554 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
561 expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
572 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
573 TypeManager.CSharpName (expr_type) + "'");
577 public override Expression DoResolve (EmitContext ec)
579 if (Oper == Operator.AddressOf)
580 Expr = Expr.ResolveLValue (ec, new EmptyExpression ());
582 Expr = Expr.Resolve (ec);
587 eclass = ExprClass.Value;
588 return ResolveOperator (ec);
591 public override Expression DoResolveLValue (EmitContext ec, Expression right)
593 if (Oper == Operator.Indirection)
594 return base.DoResolveLValue (ec, right);
596 Error (131, "The left-hand side of an assignment must be a " +
597 "variable, property or indexer");
601 public override void Emit (EmitContext ec)
603 ILGenerator ig = ec.ig;
606 case Operator.UnaryPlus:
607 throw new Exception ("This should be caught by Resolve");
609 case Operator.UnaryNegation:
611 ig.Emit (OpCodes.Ldc_I4_0);
612 if (type == TypeManager.int64_type)
613 ig.Emit (OpCodes.Conv_U8);
615 ig.Emit (OpCodes.Sub_Ovf);
618 ig.Emit (OpCodes.Neg);
623 case Operator.LogicalNot:
625 ig.Emit (OpCodes.Ldc_I4_0);
626 ig.Emit (OpCodes.Ceq);
629 case Operator.OnesComplement:
631 ig.Emit (OpCodes.Not);
634 case Operator.AddressOf:
635 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
639 throw new Exception ("This should not happen: Operator = "
644 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
646 if (Oper == Operator.LogicalNot)
647 Expr.EmitBranchable (ec, target, !onTrue);
649 base.EmitBranchable (ec, target, onTrue);
652 public override string ToString ()
654 return "Unary (" + Oper + ", " + Expr + ")";
660 // Unary operators are turned into Indirection expressions
661 // after semantic analysis (this is so we can take the address
662 // of an indirection).
664 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IVariable {
666 LocalTemporary temporary;
669 public Indirection (Expression expr, Location l)
672 type = TypeManager.HasElementType (expr.Type) ? TypeManager.GetElementType (expr.Type) : expr.Type;
673 eclass = ExprClass.Variable;
677 void LoadExprValue (EmitContext ec)
681 public override void Emit (EmitContext ec)
686 LoadFromPtr (ec.ig, Type);
689 public void Emit (EmitContext ec, bool leave_copy)
693 ec.ig.Emit (OpCodes.Dup);
694 temporary = new LocalTemporary (ec, expr.Type);
695 temporary.Store (ec);
699 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
701 prepared = prepare_for_load;
705 if (prepare_for_load)
706 ec.ig.Emit (OpCodes.Dup);
710 ec.ig.Emit (OpCodes.Dup);
711 temporary = new LocalTemporary (ec, expr.Type);
712 temporary.Store (ec);
715 StoreFromPtr (ec.ig, type);
717 if (temporary != null)
721 public void AddressOf (EmitContext ec, AddressOp Mode)
726 public override Expression DoResolve (EmitContext ec)
729 // Born fully resolved
734 public override string ToString ()
736 return "*(" + expr + ")";
739 #region IVariable Members
741 public VariableInfo VariableInfo {
747 public bool VerifyFixed (bool is_expression)
756 /// Unary Mutator expressions (pre and post ++ and --)
760 /// UnaryMutator implements ++ and -- expressions. It derives from
761 /// ExpressionStatement becuase the pre/post increment/decrement
762 /// operators can be used in a statement context.
764 /// FIXME: Idea, we could split this up in two classes, one simpler
765 /// for the common case, and one with the extra fields for more complex
766 /// classes (indexers require temporary access; overloaded require method)
769 public class UnaryMutator : ExpressionStatement {
771 public enum Mode : byte {
778 PreDecrement = IsDecrement,
779 PostIncrement = IsPost,
780 PostDecrement = IsPost | IsDecrement
784 bool is_expr = false;
785 bool recurse = false;
790 // This is expensive for the simplest case.
792 StaticCallExpr method;
794 public UnaryMutator (Mode m, Expression e, Location l)
801 static string OperName (Mode mode)
803 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
807 void Error23 (Type t)
810 23, "Operator " + OperName (mode) +
811 " cannot be applied to operand of type `" +
812 TypeManager.CSharpName (t) + "'");
816 /// Returns whether an object of type `t' can be incremented
817 /// or decremented with add/sub (ie, basically whether we can
818 /// use pre-post incr-decr operations on it, but it is not a
819 /// System.Decimal, which we require operator overloading to catch)
821 static bool IsIncrementableNumber (Type t)
823 return (t == TypeManager.sbyte_type) ||
824 (t == TypeManager.byte_type) ||
825 (t == TypeManager.short_type) ||
826 (t == TypeManager.ushort_type) ||
827 (t == TypeManager.int32_type) ||
828 (t == TypeManager.uint32_type) ||
829 (t == TypeManager.int64_type) ||
830 (t == TypeManager.uint64_type) ||
831 (t == TypeManager.char_type) ||
832 (t.IsSubclassOf (TypeManager.enum_type)) ||
833 (t == TypeManager.float_type) ||
834 (t == TypeManager.double_type) ||
835 (t.IsPointer && t != TypeManager.void_ptr_type);
838 Expression ResolveOperator (EmitContext ec)
840 Type expr_type = expr.Type;
843 // Step 1: Perform Operator Overload location
848 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
849 op_name = "op_Increment";
851 op_name = "op_Decrement";
853 mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
855 if (mg == null && expr_type.BaseType != null)
856 mg = MemberLookup (ec, expr_type.BaseType, op_name,
857 MemberTypes.Method, AllBindingFlags, loc);
860 method = StaticCallExpr.MakeSimpleCall (
861 ec, (MethodGroupExpr) mg, expr, loc);
868 // The operand of the prefix/postfix increment decrement operators
869 // should be an expression that is classified as a variable,
870 // a property access or an indexer access
873 if (expr.eclass == ExprClass.Variable){
874 LocalVariableReference var = expr as LocalVariableReference;
875 if ((var != null) && var.IsReadOnly)
876 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
877 if (IsIncrementableNumber (expr_type) ||
878 expr_type == TypeManager.decimal_type){
881 } else if (expr.eclass == ExprClass.IndexerAccess){
882 IndexerAccess ia = (IndexerAccess) expr;
884 expr = ia.ResolveLValue (ec, this);
889 } else if (expr.eclass == ExprClass.PropertyAccess){
890 PropertyExpr pe = (PropertyExpr) expr;
892 if (pe.VerifyAssignable ())
897 expr.Error_UnexpectedKind ("variable, indexer or property access", loc);
901 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
902 TypeManager.CSharpName (expr_type) + "'");
906 public override Expression DoResolve (EmitContext ec)
908 expr = expr.Resolve (ec);
913 eclass = ExprClass.Value;
914 return ResolveOperator (ec);
917 static int PtrTypeSize (Type t)
919 return GetTypeSize (TypeManager.GetElementType (t));
923 // Loads the proper "1" into the stack based on the type, then it emits the
924 // opcode for the operation requested
926 void LoadOneAndEmitOp (EmitContext ec, Type t)
929 // Measure if getting the typecode and using that is more/less efficient
930 // that comparing types. t.GetTypeCode() is an internal call.
932 ILGenerator ig = ec.ig;
934 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
935 LongConstant.EmitLong (ig, 1);
936 else if (t == TypeManager.double_type)
937 ig.Emit (OpCodes.Ldc_R8, 1.0);
938 else if (t == TypeManager.float_type)
939 ig.Emit (OpCodes.Ldc_R4, 1.0F);
940 else if (t.IsPointer){
941 int n = PtrTypeSize (t);
944 ig.Emit (OpCodes.Sizeof, t);
946 IntConstant.EmitInt (ig, n);
948 ig.Emit (OpCodes.Ldc_I4_1);
951 // Now emit the operation
954 if (t == TypeManager.int32_type ||
955 t == TypeManager.int64_type){
956 if ((mode & Mode.IsDecrement) != 0)
957 ig.Emit (OpCodes.Sub_Ovf);
959 ig.Emit (OpCodes.Add_Ovf);
960 } else if (t == TypeManager.uint32_type ||
961 t == TypeManager.uint64_type){
962 if ((mode & Mode.IsDecrement) != 0)
963 ig.Emit (OpCodes.Sub_Ovf_Un);
965 ig.Emit (OpCodes.Add_Ovf_Un);
967 if ((mode & Mode.IsDecrement) != 0)
968 ig.Emit (OpCodes.Sub_Ovf);
970 ig.Emit (OpCodes.Add_Ovf);
973 if ((mode & Mode.IsDecrement) != 0)
974 ig.Emit (OpCodes.Sub);
976 ig.Emit (OpCodes.Add);
979 if (t == TypeManager.sbyte_type){
981 ig.Emit (OpCodes.Conv_Ovf_I1);
983 ig.Emit (OpCodes.Conv_I1);
984 } else if (t == TypeManager.byte_type){
986 ig.Emit (OpCodes.Conv_Ovf_U1);
988 ig.Emit (OpCodes.Conv_U1);
989 } else if (t == TypeManager.short_type){
991 ig.Emit (OpCodes.Conv_Ovf_I2);
993 ig.Emit (OpCodes.Conv_I2);
994 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
996 ig.Emit (OpCodes.Conv_Ovf_U2);
998 ig.Emit (OpCodes.Conv_U2);
1003 void EmitCode (EmitContext ec, bool is_expr)
1006 this.is_expr = is_expr;
1007 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1011 public override void Emit (EmitContext ec)
1014 // We use recurse to allow ourselfs to be the source
1015 // of an assignment. This little hack prevents us from
1016 // having to allocate another expression
1019 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1021 LoadOneAndEmitOp (ec, expr.Type);
1023 ec.ig.Emit (OpCodes.Call, method.Method);
1028 EmitCode (ec, true);
1031 public override void EmitStatement (EmitContext ec)
1033 EmitCode (ec, false);
1038 /// Base class for the `Is' and `As' classes.
1042 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1045 public abstract class Probe : Expression {
1046 public Expression ProbeType;
1047 protected Expression expr;
1048 protected Type probe_type;
1050 public Probe (Expression expr, Expression probe_type, Location l)
1052 ProbeType = probe_type;
1057 public Expression Expr {
1063 public override Expression DoResolve (EmitContext ec)
1065 TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec, false);
1068 probe_type = texpr.ResolveType (ec);
1070 CheckObsoleteAttribute (probe_type);
1072 expr = expr.Resolve (ec);
1076 if (expr.Type.IsPointer) {
1077 Report.Error (244, loc, "\"is\" or \"as\" are not valid on pointer types");
1085 /// Implementation of the `is' operator.
1087 public class Is : Probe {
1088 public Is (Expression expr, Expression probe_type, Location l)
1089 : base (expr, probe_type, l)
1094 AlwaysTrue, AlwaysNull, AlwaysFalse, LeaveOnStack, Probe
1099 public override void Emit (EmitContext ec)
1101 ILGenerator ig = ec.ig;
1106 case Action.AlwaysFalse:
1107 ig.Emit (OpCodes.Pop);
1108 IntConstant.EmitInt (ig, 0);
1110 case Action.AlwaysTrue:
1111 ig.Emit (OpCodes.Pop);
1112 IntConstant.EmitInt (ig, 1);
1114 case Action.LeaveOnStack:
1115 // the `e != null' rule.
1116 ig.Emit (OpCodes.Ldnull);
1117 ig.Emit (OpCodes.Ceq);
1118 ig.Emit (OpCodes.Ldc_I4_0);
1119 ig.Emit (OpCodes.Ceq);
1122 ig.Emit (OpCodes.Isinst, probe_type);
1123 ig.Emit (OpCodes.Ldnull);
1124 ig.Emit (OpCodes.Cgt_Un);
1127 throw new Exception ("never reached");
1130 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
1132 ILGenerator ig = ec.ig;
1135 case Action.AlwaysFalse:
1137 ig.Emit (OpCodes.Br, target);
1140 case Action.AlwaysTrue:
1142 ig.Emit (OpCodes.Br, target);
1145 case Action.LeaveOnStack:
1146 // the `e != null' rule.
1148 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1152 ig.Emit (OpCodes.Isinst, probe_type);
1153 ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1156 throw new Exception ("never reached");
1159 public override Expression DoResolve (EmitContext ec)
1161 Expression e = base.DoResolve (ec);
1163 if ((e == null) || (expr == null))
1166 Type etype = expr.Type;
1167 bool warning_always_matches = false;
1168 bool warning_never_matches = false;
1170 type = TypeManager.bool_type;
1171 eclass = ExprClass.Value;
1174 // First case, if at compile time, there is an implicit conversion
1175 // then e != null (objects) or true (value types)
1177 e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
1180 if (etype.IsValueType)
1181 action = Action.AlwaysTrue;
1183 action = Action.LeaveOnStack;
1185 warning_always_matches = true;
1186 } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1188 // Second case: explicit reference convresion
1190 if (expr is NullLiteral)
1191 action = Action.AlwaysFalse;
1193 action = Action.Probe;
1195 action = Action.AlwaysFalse;
1196 warning_never_matches = true;
1199 if (warning_always_matches)
1200 Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
1201 else if (warning_never_matches){
1202 if (!(probe_type.IsInterface || expr.Type.IsInterface))
1203 Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
1211 /// Implementation of the `as' operator.
1213 public class As : Probe {
1214 public As (Expression expr, Expression probe_type, Location l)
1215 : base (expr, probe_type, l)
1219 bool do_isinst = false;
1221 public override void Emit (EmitContext ec)
1223 ILGenerator ig = ec.ig;
1228 ig.Emit (OpCodes.Isinst, probe_type);
1231 static void Error_CannotConvertType (Type source, Type target, Location loc)
1234 39, loc, "as operator can not convert from `" +
1235 TypeManager.CSharpName (source) + "' to `" +
1236 TypeManager.CSharpName (target) + "'");
1239 public override Expression DoResolve (EmitContext ec)
1241 Expression e = base.DoResolve (ec);
1247 eclass = ExprClass.Value;
1248 Type etype = expr.Type;
1250 if (TypeManager.IsValueType (probe_type)){
1251 Report.Error (77, loc, "The as operator should be used with a reference type only (" +
1252 TypeManager.CSharpName (probe_type) + " is a value type)");
1257 e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
1264 if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
1269 Error_CannotConvertType (etype, probe_type, loc);
1275 /// This represents a typecast in the source language.
1277 /// FIXME: Cast expressions have an unusual set of parsing
1278 /// rules, we need to figure those out.
1280 public class Cast : Expression {
1281 Expression target_type;
1284 public Cast (Expression cast_type, Expression expr, Location loc)
1286 this.target_type = cast_type;
1291 public Expression TargetType {
1297 public Expression Expr {
1306 bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
1308 if (!ec.ConstantCheckState)
1311 if ((value < min) || (value > max)) {
1312 Error (221, "Constant value `" + value + "' cannot be converted " +
1313 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1314 "syntax to override)");
1321 bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
1323 if (!ec.ConstantCheckState)
1327 Error (221, "Constant value `" + value + "' cannot be converted " +
1328 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1329 "syntax to override)");
1336 bool CheckUnsigned (EmitContext ec, long value, Type type)
1338 if (!ec.ConstantCheckState)
1342 Error (221, "Constant value `" + value + "' cannot be converted " +
1343 "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
1344 "syntax to override)");
1352 /// Attempts to do a compile-time folding of a constant cast.
1354 Expression TryReduce (EmitContext ec, Type target_type)
1356 Expression real_expr = expr;
1357 if (real_expr is EnumConstant)
1358 real_expr = ((EnumConstant) real_expr).Child;
1360 if (real_expr is ByteConstant){
1361 byte v = ((ByteConstant) real_expr).Value;
1363 if (target_type == TypeManager.sbyte_type) {
1364 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1366 return new SByteConstant ((sbyte) v);
1368 if (target_type == TypeManager.short_type)
1369 return new ShortConstant ((short) v);
1370 if (target_type == TypeManager.ushort_type)
1371 return new UShortConstant ((ushort) v);
1372 if (target_type == TypeManager.int32_type)
1373 return new IntConstant ((int) v);
1374 if (target_type == TypeManager.uint32_type)
1375 return new UIntConstant ((uint) v);
1376 if (target_type == TypeManager.int64_type)
1377 return new LongConstant ((long) v);
1378 if (target_type == TypeManager.uint64_type)
1379 return new ULongConstant ((ulong) v);
1380 if (target_type == TypeManager.float_type)
1381 return new FloatConstant ((float) v);
1382 if (target_type == TypeManager.double_type)
1383 return new DoubleConstant ((double) v);
1384 if (target_type == TypeManager.char_type)
1385 return new CharConstant ((char) v);
1386 if (target_type == TypeManager.decimal_type)
1387 return new DecimalConstant ((decimal) v);
1389 if (real_expr is SByteConstant){
1390 sbyte v = ((SByteConstant) real_expr).Value;
1392 if (target_type == TypeManager.byte_type) {
1393 if (!CheckUnsigned (ec, v, target_type))
1395 return new ByteConstant ((byte) v);
1397 if (target_type == TypeManager.short_type)
1398 return new ShortConstant ((short) v);
1399 if (target_type == TypeManager.ushort_type) {
1400 if (!CheckUnsigned (ec, v, target_type))
1402 return new UShortConstant ((ushort) v);
1403 } if (target_type == TypeManager.int32_type)
1404 return new IntConstant ((int) v);
1405 if (target_type == TypeManager.uint32_type) {
1406 if (!CheckUnsigned (ec, v, target_type))
1408 return new UIntConstant ((uint) v);
1409 } if (target_type == TypeManager.int64_type)
1410 return new LongConstant ((long) v);
1411 if (target_type == TypeManager.uint64_type) {
1412 if (!CheckUnsigned (ec, v, target_type))
1414 return new ULongConstant ((ulong) v);
1416 if (target_type == TypeManager.float_type)
1417 return new FloatConstant ((float) v);
1418 if (target_type == TypeManager.double_type)
1419 return new DoubleConstant ((double) v);
1420 if (target_type == TypeManager.char_type) {
1421 if (!CheckUnsigned (ec, v, target_type))
1423 return new CharConstant ((char) v);
1425 if (target_type == TypeManager.decimal_type)
1426 return new DecimalConstant ((decimal) v);
1428 if (real_expr is ShortConstant){
1429 short v = ((ShortConstant) real_expr).Value;
1431 if (target_type == TypeManager.byte_type) {
1432 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1434 return new ByteConstant ((byte) v);
1436 if (target_type == TypeManager.sbyte_type) {
1437 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1439 return new SByteConstant ((sbyte) v);
1441 if (target_type == TypeManager.ushort_type) {
1442 if (!CheckUnsigned (ec, v, target_type))
1444 return new UShortConstant ((ushort) v);
1446 if (target_type == TypeManager.int32_type)
1447 return new IntConstant ((int) v);
1448 if (target_type == TypeManager.uint32_type) {
1449 if (!CheckUnsigned (ec, v, target_type))
1451 return new UIntConstant ((uint) v);
1453 if (target_type == TypeManager.int64_type)
1454 return new LongConstant ((long) v);
1455 if (target_type == TypeManager.uint64_type) {
1456 if (!CheckUnsigned (ec, v, target_type))
1458 return new ULongConstant ((ulong) v);
1460 if (target_type == TypeManager.float_type)
1461 return new FloatConstant ((float) v);
1462 if (target_type == TypeManager.double_type)
1463 return new DoubleConstant ((double) v);
1464 if (target_type == TypeManager.char_type) {
1465 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1467 return new CharConstant ((char) v);
1469 if (target_type == TypeManager.decimal_type)
1470 return new DecimalConstant ((decimal) v);
1472 if (real_expr is UShortConstant){
1473 ushort v = ((UShortConstant) real_expr).Value;
1475 if (target_type == TypeManager.byte_type) {
1476 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1478 return new ByteConstant ((byte) v);
1480 if (target_type == TypeManager.sbyte_type) {
1481 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1483 return new SByteConstant ((sbyte) v);
1485 if (target_type == TypeManager.short_type) {
1486 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1488 return new ShortConstant ((short) v);
1490 if (target_type == TypeManager.int32_type)
1491 return new IntConstant ((int) v);
1492 if (target_type == TypeManager.uint32_type)
1493 return new UIntConstant ((uint) v);
1494 if (target_type == TypeManager.int64_type)
1495 return new LongConstant ((long) v);
1496 if (target_type == TypeManager.uint64_type)
1497 return new ULongConstant ((ulong) v);
1498 if (target_type == TypeManager.float_type)
1499 return new FloatConstant ((float) v);
1500 if (target_type == TypeManager.double_type)
1501 return new DoubleConstant ((double) v);
1502 if (target_type == TypeManager.char_type) {
1503 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1505 return new CharConstant ((char) v);
1507 if (target_type == TypeManager.decimal_type)
1508 return new DecimalConstant ((decimal) v);
1510 if (real_expr is IntConstant){
1511 int v = ((IntConstant) real_expr).Value;
1513 if (target_type == TypeManager.byte_type) {
1514 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1516 return new ByteConstant ((byte) v);
1518 if (target_type == TypeManager.sbyte_type) {
1519 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1521 return new SByteConstant ((sbyte) v);
1523 if (target_type == TypeManager.short_type) {
1524 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1526 return new ShortConstant ((short) v);
1528 if (target_type == TypeManager.ushort_type) {
1529 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1531 return new UShortConstant ((ushort) v);
1533 if (target_type == TypeManager.uint32_type) {
1534 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1536 return new UIntConstant ((uint) v);
1538 if (target_type == TypeManager.int64_type)
1539 return new LongConstant ((long) v);
1540 if (target_type == TypeManager.uint64_type) {
1541 if (!CheckUnsigned (ec, v, target_type))
1543 return new ULongConstant ((ulong) v);
1545 if (target_type == TypeManager.float_type)
1546 return new FloatConstant ((float) v);
1547 if (target_type == TypeManager.double_type)
1548 return new DoubleConstant ((double) v);
1549 if (target_type == TypeManager.char_type) {
1550 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1552 return new CharConstant ((char) v);
1554 if (target_type == TypeManager.decimal_type)
1555 return new DecimalConstant ((decimal) v);
1557 if (real_expr is UIntConstant){
1558 uint v = ((UIntConstant) real_expr).Value;
1560 if (target_type == TypeManager.byte_type) {
1561 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1563 return new ByteConstant ((byte) v);
1565 if (target_type == TypeManager.sbyte_type) {
1566 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1568 return new SByteConstant ((sbyte) v);
1570 if (target_type == TypeManager.short_type) {
1571 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1573 return new ShortConstant ((short) v);
1575 if (target_type == TypeManager.ushort_type) {
1576 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1578 return new UShortConstant ((ushort) v);
1580 if (target_type == TypeManager.int32_type) {
1581 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1583 return new IntConstant ((int) v);
1585 if (target_type == TypeManager.int64_type)
1586 return new LongConstant ((long) v);
1587 if (target_type == TypeManager.uint64_type)
1588 return new ULongConstant ((ulong) v);
1589 if (target_type == TypeManager.float_type)
1590 return new FloatConstant ((float) v);
1591 if (target_type == TypeManager.double_type)
1592 return new DoubleConstant ((double) v);
1593 if (target_type == TypeManager.char_type) {
1594 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1596 return new CharConstant ((char) v);
1598 if (target_type == TypeManager.decimal_type)
1599 return new DecimalConstant ((decimal) v);
1601 if (real_expr is LongConstant){
1602 long v = ((LongConstant) real_expr).Value;
1604 if (target_type == TypeManager.byte_type) {
1605 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1607 return new ByteConstant ((byte) v);
1609 if (target_type == TypeManager.sbyte_type) {
1610 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1612 return new SByteConstant ((sbyte) v);
1614 if (target_type == TypeManager.short_type) {
1615 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1617 return new ShortConstant ((short) v);
1619 if (target_type == TypeManager.ushort_type) {
1620 if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
1622 return new UShortConstant ((ushort) v);
1624 if (target_type == TypeManager.int32_type) {
1625 if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
1627 return new IntConstant ((int) v);
1629 if (target_type == TypeManager.uint32_type) {
1630 if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
1632 return new UIntConstant ((uint) v);
1634 if (target_type == TypeManager.uint64_type) {
1635 if (!CheckUnsigned (ec, v, target_type))
1637 return new ULongConstant ((ulong) v);
1639 if (target_type == TypeManager.float_type)
1640 return new FloatConstant ((float) v);
1641 if (target_type == TypeManager.double_type)
1642 return new DoubleConstant ((double) v);
1643 if (target_type == TypeManager.char_type) {
1644 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1646 return new CharConstant ((char) v);
1648 if (target_type == TypeManager.decimal_type)
1649 return new DecimalConstant ((decimal) v);
1651 if (real_expr is ULongConstant){
1652 ulong v = ((ULongConstant) real_expr).Value;
1654 if (target_type == TypeManager.byte_type) {
1655 if (!CheckRange (ec, v, target_type, Byte.MaxValue))
1657 return new ByteConstant ((byte) v);
1659 if (target_type == TypeManager.sbyte_type) {
1660 if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
1662 return new SByteConstant ((sbyte) v);
1664 if (target_type == TypeManager.short_type) {
1665 if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
1667 return new ShortConstant ((short) v);
1669 if (target_type == TypeManager.ushort_type) {
1670 if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
1672 return new UShortConstant ((ushort) v);
1674 if (target_type == TypeManager.int32_type) {
1675 if (!CheckRange (ec, v, target_type, Int32.MaxValue))
1677 return new IntConstant ((int) v);
1679 if (target_type == TypeManager.uint32_type) {
1680 if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
1682 return new UIntConstant ((uint) v);
1684 if (target_type == TypeManager.int64_type) {
1685 if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
1687 return new LongConstant ((long) v);
1689 if (target_type == TypeManager.float_type)
1690 return new FloatConstant ((float) v);
1691 if (target_type == TypeManager.double_type)
1692 return new DoubleConstant ((double) v);
1693 if (target_type == TypeManager.char_type) {
1694 if (!CheckRange (ec, v, target_type, Char.MaxValue))
1696 return new CharConstant ((char) v);
1698 if (target_type == TypeManager.decimal_type)
1699 return new DecimalConstant ((decimal) v);
1701 if (real_expr is FloatConstant){
1702 float v = ((FloatConstant) real_expr).Value;
1704 if (target_type == TypeManager.byte_type)
1705 return new ByteConstant ((byte) v);
1706 if (target_type == TypeManager.sbyte_type)
1707 return new SByteConstant ((sbyte) v);
1708 if (target_type == TypeManager.short_type)
1709 return new ShortConstant ((short) v);
1710 if (target_type == TypeManager.ushort_type)
1711 return new UShortConstant ((ushort) v);
1712 if (target_type == TypeManager.int32_type)
1713 return new IntConstant ((int) v);
1714 if (target_type == TypeManager.uint32_type)
1715 return new UIntConstant ((uint) v);
1716 if (target_type == TypeManager.int64_type)
1717 return new LongConstant ((long) v);
1718 if (target_type == TypeManager.uint64_type)
1719 return new ULongConstant ((ulong) v);
1720 if (target_type == TypeManager.double_type)
1721 return new DoubleConstant ((double) v);
1722 if (target_type == TypeManager.char_type)
1723 return new CharConstant ((char) v);
1724 if (target_type == TypeManager.decimal_type)
1725 return new DecimalConstant ((decimal) v);
1727 if (real_expr is DoubleConstant){
1728 double v = ((DoubleConstant) real_expr).Value;
1730 if (target_type == TypeManager.byte_type){
1731 return new ByteConstant ((byte) v);
1732 } if (target_type == TypeManager.sbyte_type)
1733 return new SByteConstant ((sbyte) v);
1734 if (target_type == TypeManager.short_type)
1735 return new ShortConstant ((short) v);
1736 if (target_type == TypeManager.ushort_type)
1737 return new UShortConstant ((ushort) v);
1738 if (target_type == TypeManager.int32_type)
1739 return new IntConstant ((int) v);
1740 if (target_type == TypeManager.uint32_type)
1741 return new UIntConstant ((uint) v);
1742 if (target_type == TypeManager.int64_type)
1743 return new LongConstant ((long) v);
1744 if (target_type == TypeManager.uint64_type)
1745 return new ULongConstant ((ulong) v);
1746 if (target_type == TypeManager.float_type)
1747 return new FloatConstant ((float) v);
1748 if (target_type == TypeManager.char_type)
1749 return new CharConstant ((char) v);
1750 if (target_type == TypeManager.decimal_type)
1751 return new DecimalConstant ((decimal) v);
1754 if (real_expr is CharConstant){
1755 char v = ((CharConstant) real_expr).Value;
1757 if (target_type == TypeManager.byte_type) {
1758 if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
1760 return new ByteConstant ((byte) v);
1762 if (target_type == TypeManager.sbyte_type) {
1763 if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
1765 return new SByteConstant ((sbyte) v);
1767 if (target_type == TypeManager.short_type) {
1768 if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
1770 return new ShortConstant ((short) v);
1772 if (target_type == TypeManager.int32_type)
1773 return new IntConstant ((int) v);
1774 if (target_type == TypeManager.uint32_type)
1775 return new UIntConstant ((uint) v);
1776 if (target_type == TypeManager.int64_type)
1777 return new LongConstant ((long) v);
1778 if (target_type == TypeManager.uint64_type)
1779 return new ULongConstant ((ulong) v);
1780 if (target_type == TypeManager.float_type)
1781 return new FloatConstant ((float) v);
1782 if (target_type == TypeManager.double_type)
1783 return new DoubleConstant ((double) v);
1784 if (target_type == TypeManager.char_type) {
1785 if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
1787 return new CharConstant ((char) v);
1789 if (target_type == TypeManager.decimal_type)
1790 return new DecimalConstant ((decimal) v);
1796 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1798 expr = expr.DoResolveLValue (ec, right_side);
1802 return ResolveRest (ec);
1805 public override Expression DoResolve (EmitContext ec)
1807 expr = expr.Resolve (ec);
1811 return ResolveRest (ec);
1814 Expression ResolveRest (EmitContext ec)
1816 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1820 type = target.ResolveType (ec);
1822 CheckObsoleteAttribute (type);
1824 if (type.IsAbstract && type.IsSealed) {
1825 Report.Error (716, loc, "Cannot convert to static type '{0}'", TypeManager.CSharpName (type));
1829 eclass = ExprClass.Value;
1831 if (expr is Constant){
1832 Expression e = TryReduce (ec, type);
1838 if (type.IsPointer && !ec.InUnsafe) {
1842 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1846 public override void Emit (EmitContext ec)
1849 // This one will never happen
1851 throw new Exception ("Should not happen");
1856 /// Binary operators
1858 public class Binary : Expression {
1859 public enum Operator : byte {
1860 Multiply, Division, Modulus,
1861 Addition, Subtraction,
1862 LeftShift, RightShift,
1863 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1864 Equality, Inequality,
1874 Expression left, right;
1876 // This must be kept in sync with Operator!!!
1877 public static readonly string [] oper_names;
1881 oper_names = new string [(int) Operator.TOP];
1883 oper_names [(int) Operator.Multiply] = "op_Multiply";
1884 oper_names [(int) Operator.Division] = "op_Division";
1885 oper_names [(int) Operator.Modulus] = "op_Modulus";
1886 oper_names [(int) Operator.Addition] = "op_Addition";
1887 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1888 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1889 oper_names [(int) Operator.RightShift] = "op_RightShift";
1890 oper_names [(int) Operator.LessThan] = "op_LessThan";
1891 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1892 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1893 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1894 oper_names [(int) Operator.Equality] = "op_Equality";
1895 oper_names [(int) Operator.Inequality] = "op_Inequality";
1896 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1897 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1898 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1899 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1900 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1903 public Binary (Operator oper, Expression left, Expression right, Location loc)
1911 public Operator Oper {
1920 public Expression Left {
1929 public Expression Right {
1940 /// Returns a stringified representation of the Operator
1942 static string OperName (Operator oper)
1945 case Operator.Multiply:
1947 case Operator.Division:
1949 case Operator.Modulus:
1951 case Operator.Addition:
1953 case Operator.Subtraction:
1955 case Operator.LeftShift:
1957 case Operator.RightShift:
1959 case Operator.LessThan:
1961 case Operator.GreaterThan:
1963 case Operator.LessThanOrEqual:
1965 case Operator.GreaterThanOrEqual:
1967 case Operator.Equality:
1969 case Operator.Inequality:
1971 case Operator.BitwiseAnd:
1973 case Operator.BitwiseOr:
1975 case Operator.ExclusiveOr:
1977 case Operator.LogicalOr:
1979 case Operator.LogicalAnd:
1983 return oper.ToString ();
1986 public override string ToString ()
1988 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1989 right.ToString () + ")";
1992 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1994 if (expr.Type == target_type)
1997 return Convert.ImplicitConversion (ec, expr, target_type, loc);
2000 public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
2003 34, loc, "Operator `" + OperName (oper)
2004 + "' is ambiguous on operands of type `"
2005 + TypeManager.CSharpName (l) + "' "
2006 + "and `" + TypeManager.CSharpName (r)
2010 bool IsOfType (EmitContext ec, Type l, Type r, Type t, bool check_user_conversions)
2012 if ((l == t) || (r == t))
2015 if (!check_user_conversions)
2018 if (Convert.ImplicitUserConversionExists (ec, l, t))
2020 else if (Convert.ImplicitUserConversionExists (ec, r, t))
2027 // Note that handling the case l == Decimal || r == Decimal
2028 // is taken care of by the Step 1 Operator Overload resolution.
2030 // If `check_user_conv' is true, we also check whether a user-defined conversion
2031 // exists. Note that we only need to do this if both arguments are of a user-defined
2032 // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
2033 // so we don't explicitly check for performance reasons.
2035 bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
2037 if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
2039 // If either operand is of type double, the other operand is
2040 // conveted to type double.
2042 if (r != TypeManager.double_type)
2043 right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
2044 if (l != TypeManager.double_type)
2045 left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
2047 type = TypeManager.double_type;
2048 } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
2050 // if either operand is of type float, the other operand is
2051 // converted to type float.
2053 if (r != TypeManager.double_type)
2054 right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
2055 if (l != TypeManager.double_type)
2056 left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
2057 type = TypeManager.float_type;
2058 } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
2062 // If either operand is of type ulong, the other operand is
2063 // converted to type ulong. or an error ocurrs if the other
2064 // operand is of type sbyte, short, int or long
2066 if (l == TypeManager.uint64_type){
2067 if (r != TypeManager.uint64_type){
2068 if (right is IntConstant){
2069 IntConstant ic = (IntConstant) right;
2071 e = Convert.TryImplicitIntConversion (l, ic);
2074 } else if (right is LongConstant){
2075 long ll = ((LongConstant) right).Value;
2078 right = new ULongConstant ((ulong) ll);
2080 e = Convert.ImplicitNumericConversion (ec, right, l, loc);
2087 if (left is IntConstant){
2088 e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
2091 } else if (left is LongConstant){
2092 long ll = ((LongConstant) left).Value;
2095 left = new ULongConstant ((ulong) ll);
2097 e = Convert.ImplicitNumericConversion (ec, left, r, loc);
2104 if ((other == TypeManager.sbyte_type) ||
2105 (other == TypeManager.short_type) ||
2106 (other == TypeManager.int32_type) ||
2107 (other == TypeManager.int64_type))
2108 Error_OperatorAmbiguous (loc, oper, l, r);
2110 left = ForceConversion (ec, left, TypeManager.uint64_type);
2111 right = ForceConversion (ec, right, TypeManager.uint64_type);
2113 type = TypeManager.uint64_type;
2114 } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
2116 // If either operand is of type long, the other operand is converted
2119 if (l != TypeManager.int64_type)
2120 left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
2121 if (r != TypeManager.int64_type)
2122 right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
2124 type = TypeManager.int64_type;
2125 } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
2127 // If either operand is of type uint, and the other
2128 // operand is of type sbyte, short or int, othe operands are
2129 // converted to type long (unless we have an int constant).
2133 if (l == TypeManager.uint32_type){
2134 if (right is IntConstant){
2135 IntConstant ic = (IntConstant) right;
2139 right = new UIntConstant ((uint) val);
2146 } else if (r == TypeManager.uint32_type){
2147 if (left is IntConstant){
2148 IntConstant ic = (IntConstant) left;
2152 left = new UIntConstant ((uint) val);
2161 if ((other == TypeManager.sbyte_type) ||
2162 (other == TypeManager.short_type) ||
2163 (other == TypeManager.int32_type)){
2164 left = ForceConversion (ec, left, TypeManager.int64_type);
2165 right = ForceConversion (ec, right, TypeManager.int64_type);
2166 type = TypeManager.int64_type;
2169 // if either operand is of type uint, the other
2170 // operand is converd to type uint
2172 left = ForceConversion (ec, left, TypeManager.uint32_type);
2173 right = ForceConversion (ec, right, TypeManager.uint32_type);
2174 type = TypeManager.uint32_type;
2176 } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
2177 if (l != TypeManager.decimal_type)
2178 left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
2180 if (r != TypeManager.decimal_type)
2181 right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
2182 type = TypeManager.decimal_type;
2184 left = ForceConversion (ec, left, TypeManager.int32_type);
2185 right = ForceConversion (ec, right, TypeManager.int32_type);
2187 type = TypeManager.int32_type;
2190 return (left != null) && (right != null);
2193 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2195 Report.Error (19, loc,
2196 "Operator " + name + " cannot be applied to operands of type `" +
2197 TypeManager.CSharpName (l) + "' and `" +
2198 TypeManager.CSharpName (r) + "'");
2201 void Error_OperatorCannotBeApplied ()
2203 Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
2206 static bool is_unsigned (Type t)
2208 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2209 t == TypeManager.short_type || t == TypeManager.byte_type);
2212 static bool is_user_defined (Type t)
2214 if (t.IsSubclassOf (TypeManager.value_type) &&
2215 (!TypeManager.IsBuiltinType (t) || t == TypeManager.decimal_type))
2221 Expression Make32or64 (EmitContext ec, Expression e)
2225 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
2226 t == TypeManager.int64_type || t == TypeManager.uint64_type)
2228 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
2231 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
2234 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
2237 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
2243 Expression CheckShiftArguments (EmitContext ec)
2247 e = ForceConversion (ec, right, TypeManager.int32_type);
2249 Error_OperatorCannotBeApplied ();
2254 if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
2255 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
2256 ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
2257 ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
2261 if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
2262 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
2263 right = right.DoResolve (ec);
2265 right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
2266 right = right.DoResolve (ec);
2271 Error_OperatorCannotBeApplied ();
2275 Expression ResolveOperator (EmitContext ec)
2278 Type r = right.Type;
2281 // Special cases: string comapred to null
2283 if (oper == Operator.Equality || oper == Operator.Inequality){
2284 if ((!TypeManager.IsValueType (l) && r == TypeManager.null_type) ||
2285 (!TypeManager.IsValueType (r) && l == TypeManager.null_type)) {
2286 Type = TypeManager.bool_type;
2292 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
2293 Type = TypeManager.bool_type;
2300 // Do not perform operator overload resolution when both sides are
2303 if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){
2305 // Step 1: Perform Operator Overload location
2307 Expression left_expr, right_expr;
2309 string op = oper_names [(int) oper];
2311 MethodGroupExpr union;
2312 left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
2314 right_expr = MemberLookup (
2315 ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
2316 union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
2318 union = (MethodGroupExpr) left_expr;
2320 if (union != null) {
2321 ArrayList args = new ArrayList (2);
2322 args.Add (new Argument (left, Argument.AType.Expression));
2323 args.Add (new Argument (right, Argument.AType.Expression));
2325 MethodBase method = Invocation.OverloadResolve (
2326 ec, union, args, true, Location.Null);
2328 if (method != null) {
2329 MethodInfo mi = (MethodInfo) method;
2331 return new BinaryMethod (mi.ReturnType, method, args);
2337 // Step 0: String concatenation (because overloading will get this wrong)
2339 if (oper == Operator.Addition){
2341 // If any of the arguments is a string, cast to string
2344 // Simple constant folding
2345 if (left is StringConstant && right is StringConstant)
2346 return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
2348 if (l == TypeManager.string_type || r == TypeManager.string_type) {
2350 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2351 Error_OperatorCannotBeApplied ();
2355 // try to fold it in on the left
2356 if (left is StringConcat) {
2359 // We have to test here for not-null, since we can be doubly-resolved
2360 // take care of not appending twice
2363 type = TypeManager.string_type;
2364 ((StringConcat) left).Append (ec, right);
2365 return left.Resolve (ec);
2371 // Otherwise, start a new concat expression
2372 return new StringConcat (ec, loc, left, right).Resolve (ec);
2376 // Transform a + ( - b) into a - b
2378 if (right is Unary){
2379 Unary right_unary = (Unary) right;
2381 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2382 oper = Operator.Subtraction;
2383 right = right_unary.Expr;
2389 if (oper == Operator.Equality || oper == Operator.Inequality){
2390 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2391 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2392 Error_OperatorCannotBeApplied ();
2396 type = TypeManager.bool_type;
2401 // operator != (object a, object b)
2402 // operator == (object a, object b)
2404 // For this to be used, both arguments have to be reference-types.
2405 // Read the rationale on the spec (14.9.6)
2407 // Also, if at compile time we know that the classes do not inherit
2408 // one from the other, then we catch the error there.
2410 if (!(l.IsValueType || r.IsValueType)){
2411 type = TypeManager.bool_type;
2416 if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
2420 // Also, a standard conversion must exist from either one
2422 if (!(Convert.ImplicitStandardConversionExists (ec, left, r) ||
2423 Convert.ImplicitStandardConversionExists (ec, right, l))){
2424 Error_OperatorCannotBeApplied ();
2428 // We are going to have to convert to an object to compare
2430 if (l != TypeManager.object_type)
2431 left = new EmptyCast (left, TypeManager.object_type);
2432 if (r != TypeManager.object_type)
2433 right = new EmptyCast (right, TypeManager.object_type);
2436 // FIXME: CSC here catches errors cs254 and cs252
2442 // One of them is a valuetype, but the other one is not.
2444 if (!l.IsValueType || !r.IsValueType) {
2445 Error_OperatorCannotBeApplied ();
2450 // Only perform numeric promotions on:
2451 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2453 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2454 if (l.IsSubclassOf (TypeManager.delegate_type)){
2455 if (((right.eclass == ExprClass.MethodGroup) ||
2456 (r == TypeManager.anonymous_method_type))){
2457 if ((RootContext.Version != LanguageVersion.ISO_1)){
2458 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2466 if (r.IsSubclassOf (TypeManager.delegate_type)){
2468 ArrayList args = new ArrayList (2);
2470 args = new ArrayList (2);
2471 args.Add (new Argument (left, Argument.AType.Expression));
2472 args.Add (new Argument (right, Argument.AType.Expression));
2474 if (oper == Operator.Addition)
2475 method = TypeManager.delegate_combine_delegate_delegate;
2477 method = TypeManager.delegate_remove_delegate_delegate;
2480 Error_OperatorCannotBeApplied ();
2484 return new BinaryDelegate (l, method, args);
2489 // Pointer arithmetic:
2491 // T* operator + (T* x, int y);
2492 // T* operator + (T* x, uint y);
2493 // T* operator + (T* x, long y);
2494 // T* operator + (T* x, ulong y);
2496 // T* operator + (int y, T* x);
2497 // T* operator + (uint y, T *x);
2498 // T* operator + (long y, T *x);
2499 // T* operator + (ulong y, T *x);
2501 // T* operator - (T* x, int y);
2502 // T* operator - (T* x, uint y);
2503 // T* operator - (T* x, long y);
2504 // T* operator - (T* x, ulong y);
2506 // long operator - (T* x, T *y)
2509 if (r.IsPointer && oper == Operator.Subtraction){
2511 return new PointerArithmetic (
2512 false, left, right, TypeManager.int64_type,
2515 Expression t = Make32or64 (ec, right);
2517 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
2519 } else if (r.IsPointer && oper == Operator.Addition){
2520 Expression t = Make32or64 (ec, left);
2522 return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
2527 // Enumeration operators
2529 bool lie = TypeManager.IsEnumType (l);
2530 bool rie = TypeManager.IsEnumType (r);
2534 // U operator - (E e, E f)
2536 if (oper == Operator.Subtraction){
2538 type = TypeManager.EnumToUnderlying (l);
2541 Error_OperatorCannotBeApplied ();
2547 // operator + (E e, U x)
2548 // operator - (E e, U x)
2550 if (oper == Operator.Addition || oper == Operator.Subtraction){
2551 Type enum_type = lie ? l : r;
2552 Type other_type = lie ? r : l;
2553 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2555 if (underlying_type != other_type){
2556 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2566 Error_OperatorCannotBeApplied ();
2575 temp = Convert.ImplicitConversion (ec, right, l, loc);
2579 Error_OperatorCannotBeApplied ();
2583 temp = Convert.ImplicitConversion (ec, left, r, loc);
2588 Error_OperatorCannotBeApplied ();
2593 if (oper == Operator.Equality || oper == Operator.Inequality ||
2594 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2595 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2596 if (left.Type != right.Type){
2597 Error_OperatorCannotBeApplied ();
2600 type = TypeManager.bool_type;
2604 if (oper == Operator.BitwiseAnd ||
2605 oper == Operator.BitwiseOr ||
2606 oper == Operator.ExclusiveOr){
2610 Error_OperatorCannotBeApplied ();
2614 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2615 return CheckShiftArguments (ec);
2617 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2618 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2619 type = TypeManager.bool_type;
2624 Error_OperatorCannotBeApplied ();
2628 Expression e = new ConditionalLogicalOperator (
2629 oper == Operator.LogicalAnd, left, right, l, loc);
2630 return e.Resolve (ec);
2634 // operator & (bool x, bool y)
2635 // operator | (bool x, bool y)
2636 // operator ^ (bool x, bool y)
2638 if (l == TypeManager.bool_type && r == TypeManager.bool_type){
2639 if (oper == Operator.BitwiseAnd ||
2640 oper == Operator.BitwiseOr ||
2641 oper == Operator.ExclusiveOr){
2648 // Pointer comparison
2650 if (l.IsPointer && r.IsPointer){
2651 if (oper == Operator.Equality || oper == Operator.Inequality ||
2652 oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2653 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2654 type = TypeManager.bool_type;
2660 // This will leave left or right set to null if there is an error
2662 bool check_user_conv = is_user_defined (l) && is_user_defined (r);
2663 DoNumericPromotions (ec, l, r, check_user_conv);
2664 if (left == null || right == null){
2665 Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
2670 // reload our cached types if required
2675 if (oper == Operator.BitwiseAnd ||
2676 oper == Operator.BitwiseOr ||
2677 oper == Operator.ExclusiveOr){
2679 if (((l == TypeManager.int32_type) ||
2680 (l == TypeManager.uint32_type) ||
2681 (l == TypeManager.short_type) ||
2682 (l == TypeManager.ushort_type) ||
2683 (l == TypeManager.int64_type) ||
2684 (l == TypeManager.uint64_type))){
2687 Error_OperatorCannotBeApplied ();
2691 Error_OperatorCannotBeApplied ();
2696 if (oper == Operator.Equality ||
2697 oper == Operator.Inequality ||
2698 oper == Operator.LessThanOrEqual ||
2699 oper == Operator.LessThan ||
2700 oper == Operator.GreaterThanOrEqual ||
2701 oper == Operator.GreaterThan){
2702 type = TypeManager.bool_type;
2708 public override Expression DoResolve (EmitContext ec)
2710 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2711 left = ((ParenthesizedExpression) left).Expr;
2712 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2716 if (left.eclass == ExprClass.Type) {
2717 Error (75, "Casting a negative value needs to have the value in parentheses.");
2721 left = left.Resolve (ec);
2726 Constant lc = left as Constant;
2727 if (lc != null && lc.Type == TypeManager.bool_type &&
2728 ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
2729 (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
2731 // TODO: make a sense to resolve unreachable expression as we do for statement
2732 Report.Warning (429, 4, loc, "Unreachable expression code detected");
2736 right = right.Resolve (ec);
2740 eclass = ExprClass.Value;
2742 Constant rc = right as Constant;
2743 if (rc != null & lc != null){
2744 Expression e = ConstantFold.BinaryFold (
2745 ec, oper, lc, rc, loc);
2750 return ResolveOperator (ec);
2754 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2755 /// context of a conditional bool expression. This function will return
2756 /// false if it is was possible to use EmitBranchable, or true if it was.
2758 /// The expression's code is generated, and we will generate a branch to `target'
2759 /// if the resulting expression value is equal to isTrue
2761 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
2763 ILGenerator ig = ec.ig;
2766 // This is more complicated than it looks, but its just to avoid
2767 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2768 // but on top of that we want for == and != to use a special path
2769 // if we are comparing against null
2771 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2772 bool my_on_true = oper == Operator.Inequality ? onTrue : !onTrue;
2775 // put the constant on the rhs, for simplicity
2777 if (left is Constant) {
2778 Expression swap = right;
2783 if (((Constant) right).IsZeroInteger) {
2786 ig.Emit (OpCodes.Brtrue, target);
2788 ig.Emit (OpCodes.Brfalse, target);
2791 } else if (right is BoolConstant) {
2793 if (my_on_true != ((BoolConstant) right).Value)
2794 ig.Emit (OpCodes.Brtrue, target);
2796 ig.Emit (OpCodes.Brfalse, target);
2801 } else if (oper == Operator.LogicalAnd) {
2804 Label tests_end = ig.DefineLabel ();
2806 left.EmitBranchable (ec, tests_end, false);
2807 right.EmitBranchable (ec, target, true);
2808 ig.MarkLabel (tests_end);
2810 left.EmitBranchable (ec, target, false);
2811 right.EmitBranchable (ec, target, false);
2816 } else if (oper == Operator.LogicalOr){
2818 left.EmitBranchable (ec, target, true);
2819 right.EmitBranchable (ec, target, true);
2822 Label tests_end = ig.DefineLabel ();
2823 left.EmitBranchable (ec, tests_end, true);
2824 right.EmitBranchable (ec, target, false);
2825 ig.MarkLabel (tests_end);
2830 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2831 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2832 oper == Operator.Equality || oper == Operator.Inequality)) {
2833 base.EmitBranchable (ec, target, onTrue);
2841 bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
2844 case Operator.Equality:
2846 ig.Emit (OpCodes.Beq, target);
2848 ig.Emit (OpCodes.Bne_Un, target);
2851 case Operator.Inequality:
2853 ig.Emit (OpCodes.Bne_Un, target);
2855 ig.Emit (OpCodes.Beq, target);
2858 case Operator.LessThan:
2861 ig.Emit (OpCodes.Blt_Un, target);
2863 ig.Emit (OpCodes.Blt, target);
2866 ig.Emit (OpCodes.Bge_Un, target);
2868 ig.Emit (OpCodes.Bge, target);
2871 case Operator.GreaterThan:
2874 ig.Emit (OpCodes.Bgt_Un, target);
2876 ig.Emit (OpCodes.Bgt, target);
2879 ig.Emit (OpCodes.Ble_Un, target);
2881 ig.Emit (OpCodes.Ble, target);
2884 case Operator.LessThanOrEqual:
2887 ig.Emit (OpCodes.Ble_Un, target);
2889 ig.Emit (OpCodes.Ble, target);
2892 ig.Emit (OpCodes.Bgt_Un, target);
2894 ig.Emit (OpCodes.Bgt, target);
2898 case Operator.GreaterThanOrEqual:
2901 ig.Emit (OpCodes.Bge_Un, target);
2903 ig.Emit (OpCodes.Bge, target);
2906 ig.Emit (OpCodes.Blt_Un, target);
2908 ig.Emit (OpCodes.Blt, target);
2911 Console.WriteLine (oper);
2912 throw new Exception ("what is THAT");
2916 public override void Emit (EmitContext ec)
2918 ILGenerator ig = ec.ig;
2923 // Handle short-circuit operators differently
2926 if (oper == Operator.LogicalAnd) {
2927 Label load_zero = ig.DefineLabel ();
2928 Label end = ig.DefineLabel ();
2930 left.EmitBranchable (ec, load_zero, false);
2932 ig.Emit (OpCodes.Br, end);
2934 ig.MarkLabel (load_zero);
2935 ig.Emit (OpCodes.Ldc_I4_0);
2938 } else if (oper == Operator.LogicalOr) {
2939 Label load_one = ig.DefineLabel ();
2940 Label end = ig.DefineLabel ();
2942 left.EmitBranchable (ec, load_one, true);
2944 ig.Emit (OpCodes.Br, end);
2946 ig.MarkLabel (load_one);
2947 ig.Emit (OpCodes.Ldc_I4_1);
2955 bool isUnsigned = is_unsigned (left.Type);
2958 case Operator.Multiply:
2960 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2961 opcode = OpCodes.Mul_Ovf;
2962 else if (isUnsigned)
2963 opcode = OpCodes.Mul_Ovf_Un;
2965 opcode = OpCodes.Mul;
2967 opcode = OpCodes.Mul;
2971 case Operator.Division:
2973 opcode = OpCodes.Div_Un;
2975 opcode = OpCodes.Div;
2978 case Operator.Modulus:
2980 opcode = OpCodes.Rem_Un;
2982 opcode = OpCodes.Rem;
2985 case Operator.Addition:
2987 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2988 opcode = OpCodes.Add_Ovf;
2989 else if (isUnsigned)
2990 opcode = OpCodes.Add_Ovf_Un;
2992 opcode = OpCodes.Add;
2994 opcode = OpCodes.Add;
2997 case Operator.Subtraction:
2999 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3000 opcode = OpCodes.Sub_Ovf;
3001 else if (isUnsigned)
3002 opcode = OpCodes.Sub_Ovf_Un;
3004 opcode = OpCodes.Sub;
3006 opcode = OpCodes.Sub;
3009 case Operator.RightShift:
3011 opcode = OpCodes.Shr_Un;
3013 opcode = OpCodes.Shr;
3016 case Operator.LeftShift:
3017 opcode = OpCodes.Shl;
3020 case Operator.Equality:
3021 opcode = OpCodes.Ceq;
3024 case Operator.Inequality:
3025 ig.Emit (OpCodes.Ceq);
3026 ig.Emit (OpCodes.Ldc_I4_0);
3028 opcode = OpCodes.Ceq;
3031 case Operator.LessThan:
3033 opcode = OpCodes.Clt_Un;
3035 opcode = OpCodes.Clt;
3038 case Operator.GreaterThan:
3040 opcode = OpCodes.Cgt_Un;
3042 opcode = OpCodes.Cgt;
3045 case Operator.LessThanOrEqual:
3046 Type lt = left.Type;
3048 if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
3049 ig.Emit (OpCodes.Cgt_Un);
3051 ig.Emit (OpCodes.Cgt);
3052 ig.Emit (OpCodes.Ldc_I4_0);
3054 opcode = OpCodes.Ceq;
3057 case Operator.GreaterThanOrEqual:
3058 Type le = left.Type;
3060 if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
3061 ig.Emit (OpCodes.Clt_Un);
3063 ig.Emit (OpCodes.Clt);
3065 ig.Emit (OpCodes.Ldc_I4_0);
3067 opcode = OpCodes.Ceq;
3070 case Operator.BitwiseOr:
3071 opcode = OpCodes.Or;
3074 case Operator.BitwiseAnd:
3075 opcode = OpCodes.And;
3078 case Operator.ExclusiveOr:
3079 opcode = OpCodes.Xor;
3083 throw new Exception ("This should not happen: Operator = "
3084 + oper.ToString ());
3092 // Object created by Binary when the binary operator uses an method instead of being
3093 // a binary operation that maps to a CIL binary operation.
3095 public class BinaryMethod : Expression {
3096 public MethodBase method;
3097 public ArrayList Arguments;
3099 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3104 eclass = ExprClass.Value;
3107 public override Expression DoResolve (EmitContext ec)
3112 public override void Emit (EmitContext ec)
3114 ILGenerator ig = ec.ig;
3116 if (Arguments != null)
3117 Invocation.EmitArguments (ec, method, Arguments, false, null);
3119 if (method is MethodInfo)
3120 ig.Emit (OpCodes.Call, (MethodInfo) method);
3122 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3127 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3128 // b, c, d... may be strings or objects.
3130 public class StringConcat : Expression {
3132 bool invalid = false;
3133 bool emit_conv_done = false;
3135 // Are we also concating objects?
3137 bool is_strings_only = true;
3139 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3142 type = TypeManager.string_type;
3143 eclass = ExprClass.Value;
3145 operands = new ArrayList (2);
3150 public override Expression DoResolve (EmitContext ec)
3158 public void Append (EmitContext ec, Expression operand)
3163 if (operand is StringConstant && operands.Count != 0) {
3164 StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
3165 if (last_operand != null) {
3166 operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
3172 // Conversion to object
3174 if (operand.Type != TypeManager.string_type) {
3175 Expression no = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3178 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3184 operands.Add (operand);
3187 public override void Emit (EmitContext ec)
3189 MethodInfo concat_method = null;
3192 // Do conversion to arguments; check for strings only
3195 // This can get called multiple times, so we have to deal with that.
3196 if (!emit_conv_done) {
3197 emit_conv_done = true;
3198 for (int i = 0; i < operands.Count; i ++) {
3199 Expression e = (Expression) operands [i];
3200 is_strings_only &= e.Type == TypeManager.string_type;
3203 for (int i = 0; i < operands.Count; i ++) {
3204 Expression e = (Expression) operands [i];
3206 if (! is_strings_only && e.Type == TypeManager.string_type) {
3207 // need to make sure this is an object, because the EmitParams
3208 // method might look at the type of this expression, see it is a
3209 // string and emit a string [] when we want an object [];
3211 e = new EmptyCast (e, TypeManager.object_type);
3213 operands [i] = new Argument (e, Argument.AType.Expression);
3218 // Find the right method
3220 switch (operands.Count) {
3223 // This should not be possible, because simple constant folding
3224 // is taken care of in the Binary code.
3226 throw new Exception ("how did you get here?");
3229 concat_method = is_strings_only ?
3230 TypeManager.string_concat_string_string :
3231 TypeManager.string_concat_object_object ;
3234 concat_method = is_strings_only ?
3235 TypeManager.string_concat_string_string_string :
3236 TypeManager.string_concat_object_object_object ;
3240 // There is not a 4 param overlaod for object (the one that there is
3241 // is actually a varargs methods, and is only in corlib because it was
3242 // introduced there before.).
3244 if (!is_strings_only)
3247 concat_method = TypeManager.string_concat_string_string_string_string;
3250 concat_method = is_strings_only ?
3251 TypeManager.string_concat_string_dot_dot_dot :
3252 TypeManager.string_concat_object_dot_dot_dot ;
3256 Invocation.EmitArguments (ec, concat_method, operands, false, null);
3257 ec.ig.Emit (OpCodes.Call, concat_method);
3262 // Object created with +/= on delegates
3264 public class BinaryDelegate : Expression {
3268 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3273 eclass = ExprClass.Value;
3276 public override Expression DoResolve (EmitContext ec)
3281 public override void Emit (EmitContext ec)
3283 ILGenerator ig = ec.ig;
3285 Invocation.EmitArguments (ec, method, args, false, null);
3287 ig.Emit (OpCodes.Call, (MethodInfo) method);
3288 ig.Emit (OpCodes.Castclass, type);
3291 public Expression Right {
3293 Argument arg = (Argument) args [1];
3298 public bool IsAddition {
3300 return method == TypeManager.delegate_combine_delegate_delegate;
3306 // User-defined conditional logical operator
3307 public class ConditionalLogicalOperator : Expression {
3308 Expression left, right;
3311 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3314 eclass = ExprClass.Value;
3318 this.is_and = is_and;
3321 protected void Error19 ()
3323 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
3326 protected void Error218 ()
3328 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3329 "declarations of operator true and operator false");
3332 Expression op_true, op_false, op;
3333 LocalTemporary left_temp;
3335 public override Expression DoResolve (EmitContext ec)
3338 Expression operator_group;
3340 operator_group = MethodLookup (ec, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc);
3341 if (operator_group == null) {
3346 left_temp = new LocalTemporary (ec, type);
3348 ArrayList arguments = new ArrayList ();
3349 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3350 arguments.Add (new Argument (right, Argument.AType.Expression));
3351 method = Invocation.OverloadResolve (
3352 ec, (MethodGroupExpr) operator_group, arguments, false, loc)
3354 if ((method == null) || (method.ReturnType != type)) {
3359 op = new StaticCallExpr (method, arguments, loc);
3361 op_true = GetOperatorTrue (ec, left_temp, loc);
3362 op_false = GetOperatorFalse (ec, left_temp, loc);
3363 if ((op_true == null) || (op_false == null)) {
3371 public override void Emit (EmitContext ec)
3373 ILGenerator ig = ec.ig;
3374 Label false_target = ig.DefineLabel ();
3375 Label end_target = ig.DefineLabel ();
3378 left_temp.Store (ec);
3380 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3381 left_temp.Emit (ec);
3382 ig.Emit (OpCodes.Br, end_target);
3383 ig.MarkLabel (false_target);
3385 ig.MarkLabel (end_target);
3389 public class PointerArithmetic : Expression {
3390 Expression left, right;
3394 // We assume that `l' is always a pointer
3396 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3402 is_add = is_addition;
3405 public override Expression DoResolve (EmitContext ec)
3407 eclass = ExprClass.Variable;
3409 if (left.Type == TypeManager.void_ptr_type) {
3410 Error (242, "The operation in question is undefined on void pointers");
3417 public override void Emit (EmitContext ec)
3419 Type op_type = left.Type;
3420 ILGenerator ig = ec.ig;
3422 // It must be either array or fixed buffer
3423 Type element = TypeManager.HasElementType (op_type) ?
3424 element = TypeManager.GetElementType (op_type) :
3425 element = AttributeTester.GetFixedBuffer (((FieldExpr)left).FieldInfo).ElementType;
3427 int size = GetTypeSize (element);
3428 Type rtype = right.Type;
3430 if (rtype.IsPointer){
3432 // handle (pointer - pointer)
3436 ig.Emit (OpCodes.Sub);
3440 ig.Emit (OpCodes.Sizeof, element);
3442 IntLiteral.EmitInt (ig, size);
3443 ig.Emit (OpCodes.Div);
3445 ig.Emit (OpCodes.Conv_I8);
3448 // handle + and - on (pointer op int)
3451 ig.Emit (OpCodes.Conv_I);
3453 Constant right_const = right as Constant;
3454 if (right_const != null && size != 0) {
3455 Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size), right_const, loc);
3463 ig.Emit (OpCodes.Sizeof, element);
3465 IntLiteral.EmitInt (ig, size);
3466 if (rtype == TypeManager.int64_type)
3467 ig.Emit (OpCodes.Conv_I8);
3468 else if (rtype == TypeManager.uint64_type)
3469 ig.Emit (OpCodes.Conv_U8);
3470 ig.Emit (OpCodes.Mul);
3474 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
3475 ig.Emit (OpCodes.Conv_I);
3478 ig.Emit (OpCodes.Add);
3480 ig.Emit (OpCodes.Sub);
3486 /// Implements the ternary conditional operator (?:)
3488 public class Conditional : Expression {
3489 Expression expr, trueExpr, falseExpr;
3491 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
3494 this.trueExpr = trueExpr;
3495 this.falseExpr = falseExpr;
3499 public Expression Expr {
3505 public Expression TrueExpr {
3511 public Expression FalseExpr {
3517 public override Expression DoResolve (EmitContext ec)
3519 expr = expr.Resolve (ec);
3524 if (expr.Type != TypeManager.bool_type){
3525 expr = Expression.ResolveBoolean (
3532 trueExpr = trueExpr.Resolve (ec);
3533 falseExpr = falseExpr.Resolve (ec);
3535 if (trueExpr == null || falseExpr == null)
3538 eclass = ExprClass.Value;
3539 if (trueExpr.Type == falseExpr.Type)
3540 type = trueExpr.Type;
3543 Type true_type = trueExpr.Type;
3544 Type false_type = falseExpr.Type;
3547 // First, if an implicit conversion exists from trueExpr
3548 // to falseExpr, then the result type is of type falseExpr.Type
3550 conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
3553 // Check if both can convert implicitl to each other's type
3555 if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
3557 "Can not compute type of conditional expression " +
3558 "as `" + TypeManager.CSharpName (trueExpr.Type) +
3559 "' and `" + TypeManager.CSharpName (falseExpr.Type) +
3560 "' convert implicitly to each other");
3565 } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
3569 Error (173, "The type of the conditional expression can " +
3570 "not be computed because there is no implicit conversion" +
3571 " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
3572 " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
3577 // Dead code optimalization
3578 if (expr is BoolConstant){
3579 BoolConstant bc = (BoolConstant) expr;
3581 Report.Warning (429, 4, bc.Value ? falseExpr.Location : trueExpr.Location, "Unreachable expression code detected");
3582 return bc.Value ? trueExpr : falseExpr;
3588 public override void Emit (EmitContext ec)
3590 ILGenerator ig = ec.ig;
3591 Label false_target = ig.DefineLabel ();
3592 Label end_target = ig.DefineLabel ();
3594 expr.EmitBranchable (ec, false_target, false);
3596 ig.Emit (OpCodes.Br, end_target);
3597 ig.MarkLabel (false_target);
3598 falseExpr.Emit (ec);
3599 ig.MarkLabel (end_target);
3607 public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3608 public readonly string Name;
3609 public readonly Block Block;
3610 public LocalInfo local_info;
3613 LocalTemporary temp;
3615 public LocalVariableReference (Block block, string name, Location l)
3620 eclass = ExprClass.Variable;
3624 // Setting `is_readonly' to false will allow you to create a writable
3625 // reference to a read-only variable. This is used by foreach and using.
3627 public LocalVariableReference (Block block, string name, Location l,
3628 LocalInfo local_info, bool is_readonly)
3629 : this (block, name, l)
3631 this.local_info = local_info;
3632 this.is_readonly = is_readonly;
3635 public VariableInfo VariableInfo {
3637 return local_info.VariableInfo;
3641 public bool IsReadOnly {
3647 protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
3649 if (local_info == null) {
3650 local_info = Block.GetLocalInfo (Name);
3653 if (lvalue_right_side == EmptyExpression.Null)
3654 local_info.Used = true;
3656 is_readonly = local_info.ReadOnly;
3659 type = local_info.VariableType;
3661 VariableInfo variable_info = local_info.VariableInfo;
3662 if (lvalue_right_side != null){
3664 Error (1604, "cannot assign to `" + Name + "' because it is readonly");
3668 if (variable_info != null)
3669 variable_info.SetAssigned (ec);
3672 Expression e = Block.GetConstantExpression (Name);
3674 local_info.Used = true;
3675 eclass = ExprClass.Value;
3676 return e.Resolve (ec);
3679 if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
3682 if (lvalue_right_side == null)
3683 local_info.Used = true;
3685 if (ec.CurrentAnonymousMethod != null){
3687 // If we are referencing a variable from the external block
3688 // flag it for capturing
3690 if (local_info.Block.Toplevel != ec.CurrentBlock.Toplevel){
3691 if (local_info.AddressTaken){
3692 AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
3695 ec.CaptureVariable (local_info);
3702 public override Expression DoResolve (EmitContext ec)
3704 return DoResolveBase (ec, null);
3707 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3709 Expression ret = DoResolveBase (ec, right_side);
3711 CheckObsoleteAttribute (ret.Type);
3716 public bool VerifyFixed (bool is_expression)
3718 return !is_expression || local_info.IsFixed;
3721 public override void Emit (EmitContext ec)
3723 ILGenerator ig = ec.ig;
3725 if (local_info.FieldBuilder == null){
3727 // A local variable on the local CLR stack
3729 ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
3732 // A local variable captured by anonymous methods.
3735 ec.EmitCapturedVariableInstance (local_info);
3737 ig.Emit (OpCodes.Ldfld, local_info.FieldBuilder);
3741 public void Emit (EmitContext ec, bool leave_copy)
3745 ec.ig.Emit (OpCodes.Dup);
3746 if (local_info.FieldBuilder != null){
3747 temp = new LocalTemporary (ec, Type);
3753 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3755 ILGenerator ig = ec.ig;
3756 prepared = prepare_for_load;
3758 if (local_info.FieldBuilder == null){
3760 // A local variable on the local CLR stack
3762 if (local_info.LocalBuilder == null)
3763 throw new Exception ("This should not happen: both Field and Local are null");
3767 ec.ig.Emit (OpCodes.Dup);
3768 ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
3771 // A local variable captured by anonymous methods or itereators.
3773 ec.EmitCapturedVariableInstance (local_info);
3775 if (prepare_for_load)
3776 ig.Emit (OpCodes.Dup);
3779 ig.Emit (OpCodes.Dup);
3780 temp = new LocalTemporary (ec, Type);
3783 ig.Emit (OpCodes.Stfld, local_info.FieldBuilder);
3789 public void AddressOf (EmitContext ec, AddressOp mode)
3791 ILGenerator ig = ec.ig;
3793 if (local_info.FieldBuilder == null){
3795 // A local variable on the local CLR stack
3797 ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
3800 // A local variable captured by anonymous methods or iterators
3802 ec.EmitCapturedVariableInstance (local_info);
3803 ig.Emit (OpCodes.Ldflda, local_info.FieldBuilder);
3807 public override string ToString ()
3809 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3814 /// This represents a reference to a parameter in the intermediate
3817 public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
3823 public Parameter.Modifier mod;
3824 public bool is_ref, is_out, prepared;
3838 LocalTemporary temp;
3840 public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
3847 eclass = ExprClass.Variable;
3850 public VariableInfo VariableInfo {
3854 public bool VerifyFixed (bool is_expression)
3856 return !is_expression || TypeManager.IsValueType (type);
3859 public bool IsAssigned (EmitContext ec, Location loc)
3861 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (vi))
3864 Report.Error (165, loc,
3865 "Use of unassigned parameter `" + name + "'");
3869 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3871 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsFieldAssigned (vi, field_name))
3874 Report.Error (170, loc,
3875 "Use of possibly unassigned field `" + field_name + "'");
3879 public void SetAssigned (EmitContext ec)
3881 if (is_out && ec.DoFlowAnalysis)
3882 ec.CurrentBranching.SetAssigned (vi);
3885 public void SetFieldAssigned (EmitContext ec, string field_name)
3887 if (is_out && ec.DoFlowAnalysis)
3888 ec.CurrentBranching.SetFieldAssigned (vi, field_name);
3891 protected void DoResolveBase (EmitContext ec)
3893 type = pars.GetParameterInfo (ec, idx, out mod);
3894 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3895 is_out = (mod & Parameter.Modifier.OUT) != 0;
3896 eclass = ExprClass.Variable;
3899 vi = block.ParameterMap [idx];
3901 if (ec.CurrentAnonymousMethod != null){
3903 Report.Error (1628, Location,
3904 "Can not reference a ref or out parameter in an anonymous method");
3909 // If we are referencing the parameter from the external block
3910 // flag it for capturing
3912 //Console.WriteLine ("Is parameter `{0}' local? {1}", name, block.IsLocalParameter (name));
3913 if (!block.IsLocalParameter (name)){
3914 ec.CaptureParameter (name, type, idx);
3920 // Notice that for ref/out parameters, the type exposed is not the
3921 // same type exposed externally.
3924 // externally we expose "int&"
3925 // here we expose "int".
3927 // We record this in "is_ref". This means that the type system can treat
3928 // the type as it is expected, but when we generate the code, we generate
3929 // the alternate kind of code.
3931 public override Expression DoResolve (EmitContext ec)
3935 if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
3938 if (ec.RemapToProxy)
3939 return ec.RemapParameter (idx);
3944 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3950 if (ec.RemapToProxy)
3951 return ec.RemapParameterLValue (idx, right_side);
3956 static public void EmitLdArg (ILGenerator ig, int x)
3960 case 0: ig.Emit (OpCodes.Ldarg_0); break;
3961 case 1: ig.Emit (OpCodes.Ldarg_1); break;
3962 case 2: ig.Emit (OpCodes.Ldarg_2); break;
3963 case 3: ig.Emit (OpCodes.Ldarg_3); break;
3964 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
3967 ig.Emit (OpCodes.Ldarg, x);
3971 // This method is used by parameters that are references, that are
3972 // being passed as references: we only want to pass the pointer (that
3973 // is already stored in the parameter, not the address of the pointer,
3974 // and not the value of the variable).
3976 public void EmitLoad (EmitContext ec)
3978 ILGenerator ig = ec.ig;
3981 if (!ec.MethodIsStatic)
3985 EmitLdArg (ig, arg_idx);
3988 // FIXME: Review for anonymous methods
3992 public override void Emit (EmitContext ec)
3994 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
3995 ec.EmitParameter (name);
4002 public void Emit (EmitContext ec, bool leave_copy)
4004 ILGenerator ig = ec.ig;
4007 if (!ec.MethodIsStatic)
4010 EmitLdArg (ig, arg_idx);
4014 ec.ig.Emit (OpCodes.Dup);
4017 // If we are a reference, we loaded on the stack a pointer
4018 // Now lets load the real value
4020 LoadFromPtr (ig, type);
4024 ec.ig.Emit (OpCodes.Dup);
4027 temp = new LocalTemporary (ec, type);
4033 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
4035 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
4036 ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load);
4040 ILGenerator ig = ec.ig;
4043 prepared = prepare_for_load;
4045 if (!ec.MethodIsStatic)
4048 if (is_ref && !prepared)
4049 EmitLdArg (ig, arg_idx);
4054 ec.ig.Emit (OpCodes.Dup);
4058 temp = new LocalTemporary (ec, type);
4062 StoreFromPtr (ig, type);
4068 ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
4070 ig.Emit (OpCodes.Starg, arg_idx);
4074 public void AddressOf (EmitContext ec, AddressOp mode)
4076 if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
4077 ec.EmitAddressOfParameter (name);
4083 if (!ec.MethodIsStatic)
4088 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
4090 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
4093 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
4095 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
4102 /// Used for arguments to New(), Invocation()
4104 public class Argument {
4105 public enum AType : byte {
4112 public readonly AType ArgType;
4113 public Expression Expr;
4115 public Argument (Expression expr, AType type)
4118 this.ArgType = type;
4121 public Argument (Expression expr)
4124 this.ArgType = AType.Expression;
4129 if (ArgType == AType.Ref || ArgType == AType.Out)
4130 return TypeManager.GetReferenceType (Expr.Type);
4136 public Parameter.Modifier GetParameterModifier ()
4140 return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
4143 return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
4146 return Parameter.Modifier.NONE;
4150 public static string FullDesc (Argument a)
4152 if (a.ArgType == AType.ArgList)
4155 return (a.ArgType == AType.Ref ? "ref " :
4156 (a.ArgType == AType.Out ? "out " : "")) +
4157 TypeManager.CSharpName (a.Expr.Type);
4160 public bool ResolveMethodGroup (EmitContext ec, Location loc)
4162 // FIXME: csc doesn't report any error if you try to use `ref' or
4163 // `out' in a delegate creation expression.
4164 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4171 public bool Resolve (EmitContext ec, Location loc)
4173 if (ArgType == AType.Ref) {
4174 Expr = Expr.Resolve (ec);
4178 if (!ec.IsConstructor) {
4179 FieldExpr fe = Expr as FieldExpr;
4180 if (fe != null && fe.FieldInfo.IsInitOnly) {
4181 if (fe.FieldInfo.IsStatic)
4182 Report.Error (199, loc, "A static readonly field cannot be passed ref or out (except in a static constructor)");
4184 Report.Error (192, loc, "A readonly field cannot be passed ref or out (except in a constructor)");
4188 Expr = Expr.ResolveLValue (ec, Expr);
4189 } else if (ArgType == AType.Out)
4190 Expr = Expr.ResolveLValue (ec, EmptyExpression.Null);
4192 Expr = Expr.Resolve (ec);
4197 if (ArgType == AType.Expression)
4201 // Catch errors where fields of a MarshalByRefObject are passed as ref or out
4202 // This is only allowed for `this'
4204 FieldExpr fe = Expr as FieldExpr;
4205 if (fe != null && !fe.IsStatic){
4206 Expression instance = fe.InstanceExpression;
4208 if (instance.GetType () != typeof (This)){
4209 if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
4210 Report.SymbolRelatedToPreviousError (fe.InstanceExpression.Type);
4211 Report.Error (197, loc, "Cannot pass '{0}' as ref or out or take its address because it is a member of a marshal-by-reference class",
4219 if (Expr.eclass != ExprClass.Variable){
4221 // We just probe to match the CSC output
4223 if (Expr.eclass == ExprClass.PropertyAccess ||
4224 Expr.eclass == ExprClass.IndexerAccess){
4227 "A property or indexer can not be passed as an out or ref " +
4232 "An lvalue is required as an argument to out or ref");
4240 public void Emit (EmitContext ec)
4243 // Ref and Out parameters need to have their addresses taken.
4245 // ParameterReferences might already be references, so we want
4246 // to pass just the value
4248 if (ArgType == AType.Ref || ArgType == AType.Out){
4249 AddressOp mode = AddressOp.Store;
4251 if (ArgType == AType.Ref)
4252 mode |= AddressOp.Load;
4254 if (Expr is ParameterReference){
4255 ParameterReference pr = (ParameterReference) Expr;
4261 pr.AddressOf (ec, mode);
4264 if (Expr is IMemoryLocation)
4265 ((IMemoryLocation) Expr).AddressOf (ec, mode);
4268 1510, Expr.Location,
4269 "An lvalue is required as an argument to out or ref");
4279 /// Invocation of methods or delegates.
4281 public class Invocation : ExpressionStatement {
4282 public readonly ArrayList Arguments;
4285 MethodBase method = null;
4288 // arguments is an ArrayList, but we do not want to typecast,
4289 // as it might be null.
4291 // FIXME: only allow expr to be a method invocation or a
4292 // delegate invocation (7.5.5)
4294 public Invocation (Expression expr, ArrayList arguments, Location l)
4297 Arguments = arguments;
4301 public Expression Expr {
4308 /// Determines "better conversion" as specified in 7.4.2.3
4310 /// Returns : p if a->p is better,
4311 /// q if a->q is better,
4312 /// null if neither is better
4314 static Type BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
4316 Type argument_type = a.Type;
4317 Expression argument_expr = a.Expr;
4319 if (argument_type == null)
4320 throw new Exception ("Expression of type " + a.Expr +
4321 " does not resolve its type");
4323 if (p == null || q == null)
4324 throw new InternalErrorException ("BetterConversion Got a null conversion");
4329 if (argument_expr is NullLiteral) {
4331 // If the argument is null and one of the types to compare is 'object' and
4332 // the other is a reference type, we prefer the other.
4334 // This follows from the usual rules:
4335 // * There is an implicit conversion from 'null' to type 'object'
4336 // * There is an implicit conversion from 'null' to any reference type
4337 // * There is an implicit conversion from any reference type to type 'object'
4338 // * There is no implicit conversion from type 'object' to other reference types
4339 // => Conversion of 'null' to a reference type is better than conversion to 'object'
4341 // FIXME: This probably isn't necessary, since the type of a NullLiteral is the
4342 // null type. I think it used to be 'object' and thus needed a special
4343 // case to avoid the immediately following two checks.
4345 if (!p.IsValueType && q == TypeManager.object_type)
4347 if (!q.IsValueType && p == TypeManager.object_type)
4351 if (argument_type == p)
4354 if (argument_type == q)
4357 Expression p_tmp = new EmptyExpression (p);
4358 Expression q_tmp = new EmptyExpression (q);
4360 bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
4361 bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
4363 if (p_to_q && !q_to_p)
4366 if (q_to_p && !p_to_q)
4369 if (p == TypeManager.sbyte_type)
4370 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
4371 q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4373 if (q == TypeManager.sbyte_type)
4374 if (p == TypeManager.byte_type || p == TypeManager.ushort_type ||
4375 p == TypeManager.uint32_type || p == TypeManager.uint64_type)
4378 if (p == TypeManager.short_type)
4379 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
4380 q == TypeManager.uint64_type)
4382 if (q == TypeManager.short_type)
4383 if (p == TypeManager.ushort_type || p == TypeManager.uint32_type ||
4384 p == TypeManager.uint64_type)
4387 if (p == TypeManager.int32_type)
4388 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
4390 if (q == TypeManager.int32_type)
4391 if (p == TypeManager.uint32_type || p == TypeManager.uint64_type)
4394 if (p == TypeManager.int64_type)
4395 if (q == TypeManager.uint64_type)
4397 if (q == TypeManager.int64_type)
4398 if (p == TypeManager.uint64_type)
4405 /// Determines "Better function" between candidate
4406 /// and the current best match
4409 /// Returns an integer indicating :
4410 /// false if candidate ain't better
4411 /// true if candidate is better than the current best match
4413 static bool BetterFunction (EmitContext ec, ArrayList args, int argument_count,
4414 MethodBase candidate, bool candidate_params,
4415 MethodBase best, bool best_params, Location loc)
4417 ParameterData candidate_pd = TypeManager.GetParameterData (candidate);
4418 ParameterData best_pd = TypeManager.GetParameterData (best);
4420 bool better_at_least_one = false;
4422 for (int j = 0; j < argument_count; ++j) {
4423 Argument a = (Argument) args [j];
4425 Type ct = TypeManager.TypeToCoreType (candidate_pd.ParameterType (j));
4426 Type bt = TypeManager.TypeToCoreType (best_pd.ParameterType (j));
4428 if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4429 if (candidate_params)
4430 ct = TypeManager.GetElementType (ct);
4432 if (best_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
4434 bt = TypeManager.GetElementType (bt);
4440 Type better = BetterConversion (ec, a, ct, bt, loc);
4442 // for each argument, the conversion to 'ct' should be no worse than
4443 // the conversion to 'bt'.
4447 // for at least one argument, the conversion to 'ct' should be better than
4448 // the conversion to 'bt'.
4450 better_at_least_one = true;
4453 if (better_at_least_one)
4457 // Note that this is not just an optimization. This handles the case
4459 // Add (float f1, float f2, float f3);
4460 // Add (params decimal [] foo);
4462 // The call Add (3, 4, 5) should be ambiguous. Without this check, the
4463 // first candidate would've chosen as better.
4468 if (candidate_params == best_params) {
4470 // We need to handle the case of a virtual function and its override.
4471 // The override is ignored during 'applicable_type' calculation. However,
4472 // it should be chosen over the base virtual function, especially when handling
4475 return IsAncestralType (best.DeclaringType, candidate.DeclaringType);
4479 // This handles the following cases:
4481 // Trim () is better than Trim (params char[] chars)
4482 // Concat (string s1, string s2, string s3) is better than
4483 // Concat (string s1, params string [] srest)
4485 return !candidate_params && best_params;
4488 public static string FullMethodDesc (MethodBase mb)
4490 string ret_type = "";
4495 if (mb is MethodInfo)
4496 ret_type = TypeManager.CSharpName (((MethodInfo) mb).ReturnType);
4498 StringBuilder sb = new StringBuilder (ret_type);
4500 sb.Append (mb.ReflectedType.ToString ());
4502 sb.Append (mb.Name);
4504 ParameterData pd = TypeManager.GetParameterData (mb);
4506 int count = pd.Count;
4509 for (int i = count; i > 0; ) {
4512 sb.Append (pd.ParameterDesc (count - i - 1));
4518 return sb.ToString ();
4521 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2, Location loc)
4523 MemberInfo [] miset;
4524 MethodGroupExpr union;
4529 return (MethodGroupExpr) mg2;
4532 return (MethodGroupExpr) mg1;
4535 MethodGroupExpr left_set = null, right_set = null;
4536 int length1 = 0, length2 = 0;
4538 left_set = (MethodGroupExpr) mg1;
4539 length1 = left_set.Methods.Length;
4541 right_set = (MethodGroupExpr) mg2;
4542 length2 = right_set.Methods.Length;
4544 ArrayList common = new ArrayList ();
4546 foreach (MethodBase r in right_set.Methods){
4547 if (TypeManager.ArrayContainsMethod (left_set.Methods, r))
4551 miset = new MemberInfo [length1 + length2 - common.Count];
4552 left_set.Methods.CopyTo (miset, 0);
4556 foreach (MethodBase r in right_set.Methods) {
4557 if (!common.Contains (r))
4561 union = new MethodGroupExpr (miset, loc);
4566 static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
4567 ArrayList arguments, int arg_count,
4568 ref MethodBase candidate)
4570 return IsParamsMethodApplicable (
4571 ec, me, arguments, arg_count, false, ref candidate) ||
4572 IsParamsMethodApplicable (
4573 ec, me, arguments, arg_count, true, ref candidate);
4578 static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
4579 ArrayList arguments, int arg_count,
4580 bool do_varargs, ref MethodBase candidate)
4582 return IsParamsMethodApplicable (
4583 ec, arguments, arg_count, candidate, do_varargs);
4587 /// Determines if the candidate method, if a params method, is applicable
4588 /// in its expanded form to the given set of arguments
4590 static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments,
4591 int arg_count, MethodBase candidate,
4594 ParameterData pd = TypeManager.GetParameterData (candidate);
4596 int pd_count = pd.Count;
4600 int count = pd_count - 1;
4602 if (pd.ParameterModifier (count) != Parameter.Modifier.ARGLIST)
4604 if (pd_count != arg_count)
4607 if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
4611 if (count > arg_count)
4614 if (pd_count == 1 && arg_count == 0)
4618 // If we have come this far, the case which
4619 // remains is when the number of parameters is
4620 // less than or equal to the argument count.
4622 for (int i = 0; i < count; ++i) {
4624 Argument a = (Argument) arguments [i];
4626 Parameter.Modifier a_mod = a.GetParameterModifier () &
4627 (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
4628 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4629 (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
4631 if (a_mod == p_mod) {
4633 if (a_mod == Parameter.Modifier.NONE)
4634 if (!Convert.ImplicitConversionExists (ec,
4636 pd.ParameterType (i)))
4639 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4640 Type pt = pd.ParameterType (i);
4643 pt = TypeManager.GetReferenceType (pt);
4654 Argument a = (Argument) arguments [count];
4655 if (!(a.Expr is Arglist))
4661 Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
4663 for (int i = pd_count - 1; i < arg_count; i++) {
4664 Argument a = (Argument) arguments [i];
4666 if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
4673 static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
4674 ArrayList arguments, int arg_count,
4675 ref MethodBase candidate)
4677 return IsApplicable (ec, arguments, arg_count, candidate);
4681 /// Determines if the candidate method is applicable (section 14.4.2.1)
4682 /// to the given set of arguments
4684 static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
4685 MethodBase candidate)
4687 ParameterData pd = TypeManager.GetParameterData (candidate);
4689 if (arg_count != pd.Count)
4692 for (int i = arg_count; i > 0; ) {
4695 Argument a = (Argument) arguments [i];
4697 Parameter.Modifier a_mod = a.GetParameterModifier () &
4698 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
4699 Parameter.Modifier p_mod = pd.ParameterModifier (i) &
4700 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
4703 if (a_mod == p_mod ||
4704 (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
4705 if (a_mod == Parameter.Modifier.NONE) {
4706 if (!Convert.ImplicitConversionExists (ec,
4708 pd.ParameterType (i)))
4712 if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
4713 Type pt = pd.ParameterType (i);
4716 pt = TypeManager.GetReferenceType (pt);
4728 static private bool IsAncestralType (Type first_type, Type second_type)
4730 return first_type != second_type &&
4731 (second_type.IsSubclassOf (first_type) ||
4732 TypeManager.ImplementsInterface (second_type, first_type));
4736 /// Find the Applicable Function Members (7.4.2.1)
4738 /// me: Method Group expression with the members to select.
4739 /// it might contain constructors or methods (or anything
4740 /// that maps to a method).
4742 /// Arguments: ArrayList containing resolved Argument objects.
4744 /// loc: The location if we want an error to be reported, or a Null
4745 /// location for "probing" purposes.
4747 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4748 /// that is the best match of me on Arguments.
4751 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
4752 ArrayList Arguments, bool may_fail,
4755 MethodBase method = null;
4756 bool method_params = false;
4757 Type applicable_type = null;
4759 ArrayList candidates = new ArrayList ();
4762 // Used to keep a map between the candidate
4763 // and whether it is being considered in its
4764 // normal or expanded form
4766 // false is normal form, true is expanded form
4768 Hashtable candidate_to_form = null;
4770 if (Arguments != null)
4771 arg_count = Arguments.Count;
4773 if ((me.Name == "Invoke") &&
4774 TypeManager.IsDelegateType (me.DeclaringType)) {
4775 Error_InvokeOnDelegate (loc);
4779 MethodBase[] methods = me.Methods;
4782 // First we construct the set of applicable methods
4784 bool is_sorted = true;
4785 for (int i = 0; i < methods.Length; i++){
4786 Type decl_type = methods [i].DeclaringType;
4789 // If we have already found an applicable method
4790 // we eliminate all base types (Section 14.5.5.1)
4792 if ((applicable_type != null) &&
4793 IsAncestralType (decl_type, applicable_type))
4797 // Check if candidate is applicable (section 14.4.2.1)
4798 // Is candidate applicable in normal form?
4800 bool is_applicable = IsApplicable (
4801 ec, me, Arguments, arg_count, ref methods [i]);
4803 if (!is_applicable &&
4804 (IsParamsMethodApplicable (
4805 ec, me, Arguments, arg_count, ref methods [i]))) {
4806 MethodBase candidate = methods [i];
4807 if (candidate_to_form == null)
4808 candidate_to_form = new PtrHashtable ();
4809 candidate_to_form [candidate] = candidate;
4810 // Candidate is applicable in expanded form
4811 is_applicable = true;
4817 candidates.Add (methods [i]);
4820 // Methods marked 'override' don't take part in 'applicable_type'
4824 methods [i].IsVirtual &&
4825 (methods [i].Attributes & MethodAttributes.NewSlot) == 0)
4828 if (applicable_type == null)
4829 applicable_type = decl_type;
4830 else if (applicable_type != decl_type) {
4832 if (IsAncestralType (applicable_type, decl_type))
4833 applicable_type = decl_type;
4837 int candidate_top = candidates.Count;
4839 if (applicable_type == null) {
4841 // Okay so we have failed to find anything so we
4842 // return by providing info about the closest match
4844 for (int i = 0; i < methods.Length; ++i) {
4845 MethodBase c = (MethodBase) methods [i];
4846 ParameterData pd = TypeManager.GetParameterData (c);
4848 if (pd.Count != arg_count)
4851 VerifyArgumentsCompat (ec, Arguments, arg_count,
4852 c, false, null, may_fail, loc);
4857 string report_name = me.Name;
4858 if (report_name == ".ctor")
4859 report_name = me.DeclaringType.ToString ();
4861 Error_WrongNumArguments (
4862 loc, report_name, arg_count);
4871 // At this point, applicable_type is _one_ of the most derived types
4872 // in the set of types containing the methods in this MethodGroup.
4873 // Filter the candidates so that they only contain methods from the
4874 // most derived types.
4877 int finalized = 0; // Number of finalized candidates
4880 // Invariant: applicable_type is a most derived type
4882 // We'll try to complete Section 14.5.5.1 for 'applicable_type' by
4883 // eliminating all it's base types. At the same time, we'll also move
4884 // every unrelated type to the end of the array, and pick the next
4885 // 'applicable_type'.
4887 Type next_applicable_type = null;
4888 int j = finalized; // where to put the next finalized candidate
4889 int k = finalized; // where to put the next undiscarded candidate
4890 for (int i = finalized; i < candidate_top; ++i) {
4891 MethodBase candidate = (MethodBase) candidates [i];
4892 Type decl_type = candidate.DeclaringType;
4894 if (decl_type == applicable_type) {
4895 candidates [k++] = candidates [j];
4896 candidates [j++] = candidates [i];
4900 if (IsAncestralType (decl_type, applicable_type))
4903 if (next_applicable_type != null &&
4904 IsAncestralType (decl_type, next_applicable_type))
4907 candidates [k++] = candidates [i];
4911 // Methods marked 'override' don't take part in 'applicable_type'
4915 candidate.IsVirtual &&
4916 (candidate.Attributes & MethodAttributes.NewSlot) == 0)
4920 if (next_applicable_type == null ||
4921 IsAncestralType (next_applicable_type, decl_type))
4922 next_applicable_type = decl_type;
4925 applicable_type = next_applicable_type;
4928 } while (applicable_type != null);
4932 // Now we actually find the best method
4935 method = (MethodBase) candidates [0];
4936 method_params = candidate_to_form != null && candidate_to_form.Contains (method);
4937 for (int ix = 1; ix < candidate_top; ix++){
4938 MethodBase candidate = (MethodBase) candidates [ix];
4940 if (candidate == method)
4943 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4945 if (BetterFunction (ec, Arguments, arg_count,
4946 candidate, cand_params,
4947 method, method_params, loc)) {
4949 method_params = cand_params;
4954 // Now check that there are no ambiguities i.e the selected method
4955 // should be better than all the others
4957 bool ambiguous = false;
4958 for (int ix = 0; ix < candidate_top; ix++){
4959 MethodBase candidate = (MethodBase) candidates [ix];
4961 if (candidate == method)
4964 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
4965 if (!BetterFunction (ec, Arguments, arg_count,
4966 method, method_params,
4967 candidate, cand_params,
4969 Report.SymbolRelatedToPreviousError (candidate);
4975 Report.SymbolRelatedToPreviousError (method);
4976 Report.Error (121, loc, "Ambiguous call when selecting function due to implicit casts");
4982 // And now check if the arguments are all
4983 // compatible, perform conversions if
4984 // necessary etc. and return if everything is
4987 if (!VerifyArgumentsCompat (ec, Arguments, arg_count, method,
4988 method_params, null, may_fail, loc))
4994 static void Error_WrongNumArguments (Location loc, String name, int arg_count)
4996 Report.Error (1501, loc,
4997 "No overload for method `" + name + "' takes `" +
4998 arg_count + "' arguments");
5001 static void Error_InvokeOnDelegate (Location loc)
5003 Report.Error (1533, loc,
5004 "Invoke cannot be called directly on a delegate");
5007 static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
5008 Type delegate_type, string arg_sig, string par_desc)
5010 if (delegate_type == null)
5011 Report.Error (1502, loc,
5012 "The best overloaded match for method '" +
5013 FullMethodDesc (method) +
5014 "' has some invalid arguments");
5016 Report.Error (1594, loc,
5017 "Delegate '" + delegate_type.ToString () +
5018 "' has some invalid arguments.");
5019 Report.Error (1503, loc,
5020 String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'",
5021 idx, arg_sig, par_desc));
5024 public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
5025 int arg_count, MethodBase method,
5026 bool chose_params_expanded,
5027 Type delegate_type, bool may_fail,
5030 ParameterData pd = TypeManager.GetParameterData (method);
5031 int pd_count = pd.Count;
5033 for (int j = 0; j < arg_count; j++) {
5034 Argument a = (Argument) Arguments [j];
5035 Expression a_expr = a.Expr;
5036 Type parameter_type = pd.ParameterType (j);
5037 Parameter.Modifier pm = pd.ParameterModifier (j);
5039 if (pm == Parameter.Modifier.PARAMS){
5040 if ((pm & ~Parameter.Modifier.PARAMS) != a.GetParameterModifier ()) {
5042 Error_InvalidArguments (
5043 loc, j, method, delegate_type,
5044 Argument.FullDesc (a), pd.ParameterDesc (j));
5048 if (chose_params_expanded)
5049 parameter_type = TypeManager.GetElementType (parameter_type);
5050 } else if (pm == Parameter.Modifier.ARGLIST){
5056 if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
5058 Error_InvalidArguments (
5059 loc, j, method, delegate_type,
5060 Argument.FullDesc (a), pd.ParameterDesc (j));
5068 if (!a.Type.Equals (parameter_type)){
5071 conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
5075 Error_InvalidArguments (
5076 loc, j, method, delegate_type,
5077 Argument.FullDesc (a), pd.ParameterDesc (j));
5082 // Update the argument with the implicit conversion
5088 if (parameter_type.IsPointer){
5095 Parameter.Modifier a_mod = a.GetParameterModifier () &
5096 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
5097 Parameter.Modifier p_mod = pd.ParameterModifier (j) &
5098 unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
5100 if (a_mod != p_mod &&
5101 pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
5103 Report.Error (1502, loc,
5104 "The best overloaded match for method '" + FullMethodDesc (method)+
5105 "' has some invalid arguments");
5106 Report.Error (1503, loc,
5107 "Argument " + (j+1) +
5108 ": Cannot convert from '" + Argument.FullDesc (a)
5109 + "' to '" + pd.ParameterDesc (j) + "'");
5119 public override Expression DoResolve (EmitContext ec)
5122 // First, resolve the expression that is used to
5123 // trigger the invocation
5125 expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
5129 if (!(expr is MethodGroupExpr)) {
5130 Type expr_type = expr.Type;
5132 if (expr_type != null){
5133 bool IsDelegate = TypeManager.IsDelegateType (expr_type);
5135 return (new DelegateInvocation (
5136 this.expr, Arguments, loc)).Resolve (ec);
5140 if (!(expr is MethodGroupExpr)){
5141 expr.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
5146 // Next, evaluate all the expressions in the argument list
5148 if (Arguments != null){
5149 foreach (Argument a in Arguments){
5150 if (!a.Resolve (ec, loc))
5155 MethodGroupExpr mg = (MethodGroupExpr) expr;
5156 method = OverloadResolve (ec, mg, Arguments, false, loc);
5161 MethodInfo mi = method as MethodInfo;
5163 type = TypeManager.TypeToCoreType (mi.ReturnType);
5164 if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null)) {
5165 SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
5169 Expression iexpr = mg.InstanceExpression;
5170 if (mi.IsStatic && (iexpr != null) && !(iexpr is This)) {
5171 if (mg.IdenticalTypeName)
5172 mg.InstanceExpression = null;
5174 MemberAccess.error176 (loc, mi.Name);
5180 if (type.IsPointer){
5188 // Only base will allow this invocation to happen.
5190 if (mg.IsBase && method.IsAbstract){
5191 Report.Error (205, loc, "Cannot call an abstract base member: " +
5192 FullMethodDesc (method));
5196 if (method.Name == "Finalize" && Arguments == null) {
5198 Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
5200 Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
5204 if ((method.Attributes & MethodAttributes.SpecialName) != 0) {
5205 if (TypeManager.LookupDeclSpace (method.DeclaringType) != null || TypeManager.IsSpecialMethod (method)) {
5206 Report.Error (571, loc, TypeManager.CSharpSignature (method) + ": can not call operator or accessor");
5211 if (mg.InstanceExpression != null)
5212 mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
5214 eclass = ExprClass.Value;
5219 // Emits the list of arguments as an array
5221 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
5223 ILGenerator ig = ec.ig;
5224 int count = arguments.Count - idx;
5225 Argument a = (Argument) arguments [idx];
5226 Type t = a.Expr.Type;
5228 IntConstant.EmitInt (ig, count);
5229 ig.Emit (OpCodes.Newarr, TypeManager.TypeToCoreType (t));
5231 int top = arguments.Count;
5232 for (int j = idx; j < top; j++){
5233 a = (Argument) arguments [j];
5235 ig.Emit (OpCodes.Dup);
5236 IntConstant.EmitInt (ig, j - idx);
5239 OpCode op = ArrayAccess.GetStoreOpcode (t, out is_stobj);
5241 ig.Emit (OpCodes.Ldelema, t);
5246 ig.Emit (OpCodes.Stobj, t);
5253 /// Emits a list of resolved Arguments that are in the arguments
5256 /// The MethodBase argument might be null if the
5257 /// emission of the arguments is known not to contain
5258 /// a `params' field (for example in constructors or other routines
5259 /// that keep their arguments in this structure)
5261 /// if `dup_args' is true, a copy of the arguments will be left
5262 /// on the stack. If `dup_args' is true, you can specify `this_arg'
5263 /// which will be duplicated before any other args. Only EmitCall
5264 /// should be using this interface.
5266 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
5270 pd = TypeManager.GetParameterData (mb);
5274 LocalTemporary [] temps = null;
5277 temps = new LocalTemporary [arguments.Count];
5280 // If we are calling a params method with no arguments, special case it
5282 if (arguments == null){
5283 if (pd != null && pd.Count > 0 &&
5284 pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
5285 ILGenerator ig = ec.ig;
5287 IntConstant.EmitInt (ig, 0);
5288 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
5294 int top = arguments.Count;
5296 for (int i = 0; i < top; i++){
5297 Argument a = (Argument) arguments [i];
5300 if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
5302 // Special case if we are passing the same data as the
5303 // params argument, do not put it in an array.
5305 if (pd.ParameterType (i) == a.Type)
5308 EmitParams (ec, i, arguments);
5315 ec.ig.Emit (OpCodes.Dup);
5316 (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
5321 if (this_arg != null)
5324 for (int i = 0; i < top; i ++)
5325 temps [i].Emit (ec);
5328 if (pd != null && pd.Count > top &&
5329 pd.ParameterModifier (top) == Parameter.Modifier.PARAMS){
5330 ILGenerator ig = ec.ig;
5332 IntConstant.EmitInt (ig, 0);
5333 ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (top)));
5337 static Type[] GetVarargsTypes (EmitContext ec, MethodBase mb,
5338 ArrayList arguments)
5340 ParameterData pd = TypeManager.GetParameterData (mb);
5342 if (arguments == null)
5343 return new Type [0];
5345 Argument a = (Argument) arguments [pd.Count - 1];
5346 Arglist list = (Arglist) a.Expr;
5348 return list.ArgumentTypes;
5352 /// This checks the ConditionalAttribute on the method
5354 static bool IsMethodExcluded (MethodBase method, EmitContext ec)
5356 if (method.IsConstructor)
5359 IMethodData md = TypeManager.GetMethod (method);
5361 return md.IsExcluded (ec);
5363 // For some methods (generated by delegate class) GetMethod returns null
5364 // because they are not included in builder_to_method table
5365 if (method.DeclaringType is TypeBuilder)
5368 return AttributeTester.IsConditionalMethodExcluded (method);
5372 /// is_base tells whether we want to force the use of the `call'
5373 /// opcode instead of using callvirt. Call is required to call
5374 /// a specific method, while callvirt will always use the most
5375 /// recent method in the vtable.
5377 /// is_static tells whether this is an invocation on a static method
5379 /// instance_expr is an expression that represents the instance
5380 /// it must be non-null if is_static is false.
5382 /// method is the method to invoke.
5384 /// Arguments is the list of arguments to pass to the method or constructor.
5386 public static void EmitCall (EmitContext ec, bool is_base,
5387 bool is_static, Expression instance_expr,
5388 MethodBase method, ArrayList Arguments, Location loc)
5390 EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, loc, false, false);
5393 // `dup_args' leaves an extra copy of the arguments on the stack
5394 // `omit_args' does not leave any arguments at all.
5395 // So, basically, you could make one call with `dup_args' set to true,
5396 // and then another with `omit_args' set to true, and the two calls
5397 // would have the same set of arguments. However, each argument would
5398 // only have been evaluated once.
5399 public static void EmitCall (EmitContext ec, bool is_base,
5400 bool is_static, Expression instance_expr,
5401 MethodBase method, ArrayList Arguments, Location loc,
5402 bool dup_args, bool omit_args)
5404 ILGenerator ig = ec.ig;
5405 bool struct_call = false;
5406 bool this_call = false;
5407 LocalTemporary this_arg = null;
5409 Type decl_type = method.DeclaringType;
5411 if (!RootContext.StdLib) {
5412 // Replace any calls to the system's System.Array type with calls to
5413 // the newly created one.
5414 if (method == TypeManager.system_int_array_get_length)
5415 method = TypeManager.int_array_get_length;
5416 else if (method == TypeManager.system_int_array_get_rank)
5417 method = TypeManager.int_array_get_rank;
5418 else if (method == TypeManager.system_object_array_clone)
5419 method = TypeManager.object_array_clone;
5420 else if (method == TypeManager.system_int_array_get_length_int)
5421 method = TypeManager.int_array_get_length_int;
5422 else if (method == TypeManager.system_int_array_get_lower_bound_int)
5423 method = TypeManager.int_array_get_lower_bound_int;
5424 else if (method == TypeManager.system_int_array_get_upper_bound_int)
5425 method = TypeManager.int_array_get_upper_bound_int;
5426 else if (method == TypeManager.system_void_array_copyto_array_int)
5427 method = TypeManager.void_array_copyto_array_int;
5430 if (ec.TestObsoleteMethodUsage) {
5432 // This checks ObsoleteAttribute on the method and on the declaring type
5434 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
5436 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
5439 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
5441 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
5445 if (IsMethodExcluded (method, ec))
5449 this_call = instance_expr == null;
5450 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
5454 // If this is ourselves, push "this"
5459 ig.Emit (OpCodes.Ldarg_0);
5463 // Push the instance expression
5465 if (instance_expr.Type.IsValueType) {
5467 // Special case: calls to a function declared in a
5468 // reference-type with a value-type argument need
5469 // to have their value boxed.
5470 if (decl_type.IsValueType) {
5472 // If the expression implements IMemoryLocation, then
5473 // we can optimize and use AddressOf on the
5476 // If not we have to use some temporary storage for
5478 if (instance_expr is IMemoryLocation) {
5479 ((IMemoryLocation)instance_expr).
5480 AddressOf (ec, AddressOp.LoadStore);
5482 LocalTemporary temp = new LocalTemporary (ec, instance_expr.Type);
5483 instance_expr.Emit (ec);
5485 temp.AddressOf (ec, AddressOp.Load);
5488 // avoid the overhead of doing this all the time.
5490 t = TypeManager.GetReferenceType (instance_expr.Type);
5492 instance_expr.Emit (ec);
5493 ig.Emit (OpCodes.Box, instance_expr.Type);
5494 t = TypeManager.object_type;
5497 instance_expr.Emit (ec);
5498 t = instance_expr.Type;
5503 this_arg = new LocalTemporary (ec, t);
5504 ig.Emit (OpCodes.Dup);
5505 this_arg.Store (ec);
5511 EmitArguments (ec, method, Arguments, dup_args, this_arg);
5514 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
5515 call_op = OpCodes.Call;
5517 call_op = OpCodes.Callvirt;
5519 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
5520 Type[] varargs_types = GetVarargsTypes (ec, method, Arguments);
5521 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
5528 // and DoFoo is not virtual, you can omit the callvirt,
5529 // because you don't need the null checking behavior.
5531 if (method is MethodInfo)
5532 ig.Emit (call_op, (MethodInfo) method);
5534 ig.Emit (call_op, (ConstructorInfo) method);
5537 public override void Emit (EmitContext ec)
5539 MethodGroupExpr mg = (MethodGroupExpr) this.expr;
5541 EmitCall (ec, mg.IsBase, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
5544 public override void EmitStatement (EmitContext ec)
5549 // Pop the return value if there is one
5551 if (method is MethodInfo){
5552 Type ret = ((MethodInfo)method).ReturnType;
5553 if (TypeManager.TypeToCoreType (ret) != TypeManager.void_type)
5554 ec.ig.Emit (OpCodes.Pop);
5559 public class InvocationOrCast : ExpressionStatement
5562 Expression argument;
5564 public InvocationOrCast (Expression expr, Expression argument, Location loc)
5567 this.argument = argument;
5571 public override Expression DoResolve (EmitContext ec)
5574 // First try to resolve it as a cast.
5576 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
5578 Cast cast = new Cast (te, argument, loc);
5579 return cast.Resolve (ec);
5583 // This can either be a type or a delegate invocation.
5584 // Let's just resolve it and see what we'll get.
5586 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5591 // Ok, so it's a Cast.
5593 if (expr.eclass == ExprClass.Type) {
5594 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5595 return cast.Resolve (ec);
5599 // It's a delegate invocation.
5601 if (!TypeManager.IsDelegateType (expr.Type)) {
5602 Error (149, "Method name expected");
5606 ArrayList args = new ArrayList ();
5607 args.Add (new Argument (argument, Argument.AType.Expression));
5608 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5609 return invocation.Resolve (ec);
5614 Error (201, "Only assignment, call, increment, decrement and new object " +
5615 "expressions can be used as a statement");
5618 public override ExpressionStatement ResolveStatement (EmitContext ec)
5621 // First try to resolve it as a cast.
5623 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
5630 // This can either be a type or a delegate invocation.
5631 // Let's just resolve it and see what we'll get.
5633 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5634 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
5640 // It's a delegate invocation.
5642 if (!TypeManager.IsDelegateType (expr.Type)) {
5643 Error (149, "Method name expected");
5647 ArrayList args = new ArrayList ();
5648 args.Add (new Argument (argument, Argument.AType.Expression));
5649 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5650 return invocation.ResolveStatement (ec);
5653 public override void Emit (EmitContext ec)
5655 throw new Exception ("Cannot happen");
5658 public override void EmitStatement (EmitContext ec)
5660 throw new Exception ("Cannot happen");
5665 // This class is used to "disable" the code generation for the
5666 // temporary variable when initializing value types.
5668 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
5669 public void AddressOf (EmitContext ec, AddressOp Mode)
5676 /// Implements the new expression
5678 public class New : ExpressionStatement, IMemoryLocation {
5679 public readonly ArrayList Arguments;
5682 // During bootstrap, it contains the RequestedType,
5683 // but if `type' is not null, it *might* contain a NewDelegate
5684 // (because of field multi-initialization)
5686 public Expression RequestedType;
5688 MethodBase method = null;
5691 // If set, the new expression is for a value_target, and
5692 // we will not leave anything on the stack.
5694 Expression value_target;
5695 bool value_target_set = false;
5697 public New (Expression requested_type, ArrayList arguments, Location l)
5699 RequestedType = requested_type;
5700 Arguments = arguments;
5704 public bool SetValueTypeVariable (Expression value)
5706 value_target = value;
5707 value_target_set = true;
5708 if (!(value_target is IMemoryLocation)){
5709 Error_UnexpectedKind ("variable", loc);
5716 // This function is used to disable the following code sequence for
5717 // value type initialization:
5719 // AddressOf (temporary)
5723 // Instead the provide will have provided us with the address on the
5724 // stack to store the results.
5726 static Expression MyEmptyExpression;
5728 public void DisableTemporaryValueType ()
5730 if (MyEmptyExpression == null)
5731 MyEmptyExpression = new EmptyAddressOf ();
5734 // To enable this, look into:
5735 // test-34 and test-89 and self bootstrapping.
5737 // For instance, we can avoid a copy by using `newobj'
5738 // instead of Call + Push-temp on value types.
5739 // value_target = MyEmptyExpression;
5742 public override Expression DoResolve (EmitContext ec)
5745 // The New DoResolve might be called twice when initializing field
5746 // expressions (see EmitFieldInitializers, the call to
5747 // GetInitializerExpression will perform a resolve on the expression,
5748 // and later the assign will trigger another resolution
5750 // This leads to bugs (#37014)
5753 if (RequestedType is NewDelegate)
5754 return RequestedType;
5758 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
5762 type = texpr.ResolveType (ec);
5764 CheckObsoleteAttribute (type);
5766 bool IsDelegate = TypeManager.IsDelegateType (type);
5769 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5770 if (RequestedType != null)
5771 if (!(RequestedType is DelegateCreation))
5772 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
5773 return RequestedType;
5776 if (type.IsAbstract && type.IsSealed) {
5777 Report.Error (712, loc, "Cannot create an instance of the static class '{0}'", TypeManager.CSharpName (type));
5781 if (type.IsInterface || type.IsAbstract){
5782 Error (144, "It is not possible to create instances of interfaces or abstract classes");
5786 bool is_struct = type.IsValueType;
5787 eclass = ExprClass.Value;
5790 // SRE returns a match for .ctor () on structs (the object constructor),
5791 // so we have to manually ignore it.
5793 if (is_struct && Arguments == null)
5797 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5798 ml = MemberLookupFinal (ec, type, type, ".ctor",
5799 MemberTypes.Constructor,
5800 AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5805 if (! (ml is MethodGroupExpr)){
5807 ml.Error_UnexpectedKind ("method group", loc);
5813 if (Arguments != null){
5814 foreach (Argument a in Arguments){
5815 if (!a.Resolve (ec, loc))
5820 method = Invocation.OverloadResolve (
5821 ec, (MethodGroupExpr) ml, Arguments, true, loc);
5825 if (method == null) {
5826 if (almostMatchedMembers.Count != 0) {
5827 MemberLookupFailed (ec, type, type, ".ctor", null, loc);
5831 if (!is_struct || Arguments.Count > 0) {
5832 Error (1501, String.Format (
5833 "New invocation: Can not find a constructor in `{0}' for this argument list",
5834 TypeManager.CSharpName (type)));
5843 // This DoEmit can be invoked in two contexts:
5844 // * As a mechanism that will leave a value on the stack (new object)
5845 // * As one that wont (init struct)
5847 // You can control whether a value is required on the stack by passing
5848 // need_value_on_stack. The code *might* leave a value on the stack
5849 // so it must be popped manually
5851 // If we are dealing with a ValueType, we have a few
5852 // situations to deal with:
5854 // * The target is a ValueType, and we have been provided
5855 // the instance (this is easy, we are being assigned).
5857 // * The target of New is being passed as an argument,
5858 // to a boxing operation or a function that takes a
5861 // In this case, we need to create a temporary variable
5862 // that is the argument of New.
5864 // Returns whether a value is left on the stack
5866 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5868 bool is_value_type = type.IsValueType;
5869 ILGenerator ig = ec.ig;
5874 // Allow DoEmit() to be called multiple times.
5875 // We need to create a new LocalTemporary each time since
5876 // you can't share LocalBuilders among ILGeneators.
5877 if (!value_target_set)
5878 value_target = new LocalTemporary (ec, type);
5880 ml = (IMemoryLocation) value_target;
5881 ml.AddressOf (ec, AddressOp.Store);
5885 Invocation.EmitArguments (ec, method, Arguments, false, null);
5889 ig.Emit (OpCodes.Initobj, type);
5891 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5892 if (need_value_on_stack){
5893 value_target.Emit (ec);
5898 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5903 public override void Emit (EmitContext ec)
5908 public override void EmitStatement (EmitContext ec)
5910 if (DoEmit (ec, false))
5911 ec.ig.Emit (OpCodes.Pop);
5914 public void AddressOf (EmitContext ec, AddressOp Mode)
5916 if (!type.IsValueType){
5918 // We throw an exception. So far, I believe we only need to support
5920 // foreach (int j in new StructType ())
5923 throw new Exception ("AddressOf should not be used for classes");
5926 if (!value_target_set)
5927 value_target = new LocalTemporary (ec, type);
5929 IMemoryLocation ml = (IMemoryLocation) value_target;
5930 ml.AddressOf (ec, AddressOp.Store);
5932 Invocation.EmitArguments (ec, method, Arguments, false, null);
5935 ec.ig.Emit (OpCodes.Initobj, type);
5937 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5939 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5944 /// 14.5.10.2: Represents an array creation expression.
5948 /// There are two possible scenarios here: one is an array creation
5949 /// expression that specifies the dimensions and optionally the
5950 /// initialization data and the other which does not need dimensions
5951 /// specified but where initialization data is mandatory.
5953 public class ArrayCreation : Expression {
5954 Expression requested_base_type;
5955 ArrayList initializers;
5958 // The list of Argument types.
5959 // This is used to construct the `newarray' or constructor signature
5961 ArrayList arguments;
5964 // Method used to create the array object.
5966 MethodBase new_method = null;
5968 Type array_element_type;
5969 Type underlying_type;
5970 bool is_one_dimensional = false;
5971 bool is_builtin_type = false;
5972 bool expect_initializers = false;
5973 int num_arguments = 0;
5977 ArrayList array_data;
5982 // The number of array initializers that we can handle
5983 // via the InitializeArray method - through EmitStaticInitializers
5985 int num_automatic_initializers;
5987 const int max_automatic_initializers = 6;
5989 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5991 this.requested_base_type = requested_base_type;
5992 this.initializers = initializers;
5996 arguments = new ArrayList ();
5998 foreach (Expression e in exprs) {
5999 arguments.Add (new Argument (e, Argument.AType.Expression));
6004 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
6006 this.requested_base_type = requested_base_type;
6007 this.initializers = initializers;
6011 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
6013 //string tmp = rank.Substring (rank.LastIndexOf ('['));
6015 //dimensions = tmp.Length - 1;
6016 expect_initializers = true;
6019 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
6021 StringBuilder sb = new StringBuilder (rank);
6024 for (int i = 1; i < idx_count; i++)
6029 return new ComposedCast (base_type, sb.ToString (), loc);
6032 void Error_IncorrectArrayInitializer ()
6034 Error (178, "Incorrectly structured array initializer");
6037 public bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
6039 if (specified_dims) {
6040 Argument a = (Argument) arguments [idx];
6042 if (!a.Resolve (ec, loc))
6045 if (!(a.Expr is Constant)) {
6046 Error (150, "A constant value is expected");
6050 int value = (int) ((Constant) a.Expr).GetValue ();
6052 if (value != probe.Count) {
6053 Error_IncorrectArrayInitializer ();
6057 bounds [idx] = value;
6060 int child_bounds = -1;
6061 foreach (object o in probe) {
6062 if (o is ArrayList) {
6063 int current_bounds = ((ArrayList) o).Count;
6065 if (child_bounds == -1)
6066 child_bounds = current_bounds;
6068 else if (child_bounds != current_bounds){
6069 Error_IncorrectArrayInitializer ();
6072 if (specified_dims && (idx + 1 >= arguments.Count)){
6073 Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression");
6077 bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
6081 if (child_bounds != -1){
6082 Error_IncorrectArrayInitializer ();
6086 Expression tmp = (Expression) o;
6087 tmp = tmp.Resolve (ec);
6091 // Console.WriteLine ("I got: " + tmp);
6092 // Handle initialization from vars, fields etc.
6094 Expression conv = Convert.ImplicitConversionRequired (
6095 ec, tmp, underlying_type, loc);
6100 if (conv is StringConstant || conv is DecimalConstant || conv is NullCast) {
6101 // These are subclasses of Constant that can appear as elements of an
6102 // array that cannot be statically initialized (with num_automatic_initializers
6103 // > max_automatic_initializers), so num_automatic_initializers should be left as zero.
6104 array_data.Add (conv);
6105 } else if (conv is Constant) {
6106 // These are the types of Constant that can appear in arrays that can be
6107 // statically allocated.
6108 array_data.Add (conv);
6109 num_automatic_initializers++;
6111 array_data.Add (conv);
6118 public void UpdateIndices (EmitContext ec)
6121 for (ArrayList probe = initializers; probe != null;) {
6122 if (probe.Count > 0 && probe [0] is ArrayList) {
6123 Expression e = new IntConstant (probe.Count);
6124 arguments.Add (new Argument (e, Argument.AType.Expression));
6126 bounds [i++] = probe.Count;
6128 probe = (ArrayList) probe [0];
6131 Expression e = new IntConstant (probe.Count);
6132 arguments.Add (new Argument (e, Argument.AType.Expression));
6134 bounds [i++] = probe.Count;
6141 public bool ValidateInitializers (EmitContext ec, Type array_type)
6143 if (initializers == null) {
6144 if (expect_initializers)
6150 if (underlying_type == null)
6154 // We use this to store all the date values in the order in which we
6155 // will need to store them in the byte blob later
6157 array_data = new ArrayList ();
6158 bounds = new Hashtable ();
6162 if (arguments != null) {
6163 ret = CheckIndices (ec, initializers, 0, true);
6166 arguments = new ArrayList ();
6168 ret = CheckIndices (ec, initializers, 0, false);
6175 if (arguments.Count != dimensions) {
6176 Error_IncorrectArrayInitializer ();
6185 // Converts `source' to an int, uint, long or ulong.
6187 Expression ExpressionToArrayArgument (EmitContext ec, Expression source)
6191 bool old_checked = ec.CheckState;
6192 ec.CheckState = true;
6194 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
6195 if (target == null){
6196 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
6197 if (target == null){
6198 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
6199 if (target == null){
6200 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
6202 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
6206 ec.CheckState = old_checked;
6209 // Only positive constants are allowed at compile time
6211 if (target is Constant){
6212 if (target is IntConstant){
6213 if (((IntConstant) target).Value < 0){
6214 Expression.Error_NegativeArrayIndex (loc);
6219 if (target is LongConstant){
6220 if (((LongConstant) target).Value < 0){
6221 Expression.Error_NegativeArrayIndex (loc);
6232 // Creates the type of the array
6234 bool LookupType (EmitContext ec)
6236 StringBuilder array_qualifier = new StringBuilder (rank);
6239 // `In the first form allocates an array instace of the type that results
6240 // from deleting each of the individual expression from the expression list'
6242 if (num_arguments > 0) {
6243 array_qualifier.Append ("[");
6244 for (int i = num_arguments-1; i > 0; i--)
6245 array_qualifier.Append (",");
6246 array_qualifier.Append ("]");
6252 TypeExpr array_type_expr;
6253 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
6254 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
6255 if (array_type_expr == null)
6258 type = array_type_expr.ResolveType (ec);
6260 if (!type.IsArray) {
6261 Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
6264 underlying_type = TypeManager.GetElementType (type);
6265 dimensions = type.GetArrayRank ();
6270 public override Expression DoResolve (EmitContext ec)
6274 if (!LookupType (ec))
6278 // First step is to validate the initializers and fill
6279 // in any missing bits
6281 if (!ValidateInitializers (ec, type))
6284 if (arguments == null)
6287 arg_count = arguments.Count;
6288 foreach (Argument a in arguments){
6289 if (!a.Resolve (ec, loc))
6292 Expression real_arg = ExpressionToArrayArgument (ec, a.Expr, loc);
6293 if (real_arg == null)
6300 array_element_type = TypeManager.GetElementType (type);
6302 if (array_element_type.IsAbstract && array_element_type.IsSealed) {
6303 Report.Error (719, loc, "'{0}': array elements cannot be of static type", TypeManager.CSharpName (array_element_type));
6307 if (arg_count == 1) {
6308 is_one_dimensional = true;
6309 eclass = ExprClass.Value;
6313 is_builtin_type = TypeManager.IsBuiltinType (type);
6315 if (is_builtin_type) {
6318 ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
6319 AllBindingFlags, loc);
6321 if (!(ml is MethodGroupExpr)) {
6322 ml.Error_UnexpectedKind ("method group", loc);
6327 Error (-6, "New invocation: Can not find a constructor for " +
6328 "this argument list");
6332 new_method = Invocation.OverloadResolve (
6333 ec, (MethodGroupExpr) ml, arguments, false, loc);
6335 if (new_method == null) {
6336 Error (-6, "New invocation: Can not find a constructor for " +
6337 "this argument list");
6341 eclass = ExprClass.Value;
6344 ModuleBuilder mb = CodeGen.Module.Builder;
6345 ArrayList args = new ArrayList ();
6347 if (arguments != null) {
6348 for (int i = 0; i < arg_count; i++)
6349 args.Add (TypeManager.int32_type);
6352 Type [] arg_types = null;
6355 arg_types = new Type [args.Count];
6357 args.CopyTo (arg_types, 0);
6359 new_method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
6362 if (new_method == null) {
6363 Error (-6, "New invocation: Can not find a constructor for " +
6364 "this argument list");
6368 eclass = ExprClass.Value;
6373 public static byte [] MakeByteBlob (ArrayList array_data, Type underlying_type, Location loc)
6378 int count = array_data.Count;
6380 if (underlying_type.IsEnum)
6381 underlying_type = TypeManager.EnumToUnderlying (underlying_type);
6383 factor = GetTypeSize (underlying_type);
6385 throw new Exception ("unrecognized type in MakeByteBlob: " + underlying_type);
6387 data = new byte [(count * factor + 4) & ~3];
6390 for (int i = 0; i < count; ++i) {
6391 object v = array_data [i];
6393 if (v is EnumConstant)
6394 v = ((EnumConstant) v).Child;
6396 if (v is Constant && !(v is StringConstant))
6397 v = ((Constant) v).GetValue ();
6403 if (underlying_type == TypeManager.int64_type){
6404 if (!(v is Expression)){
6405 long val = (long) v;
6407 for (int j = 0; j < factor; ++j) {
6408 data [idx + j] = (byte) (val & 0xFF);
6412 } else if (underlying_type == TypeManager.uint64_type){
6413 if (!(v is Expression)){
6414 ulong val = (ulong) v;
6416 for (int j = 0; j < factor; ++j) {
6417 data [idx + j] = (byte) (val & 0xFF);
6421 } else if (underlying_type == TypeManager.float_type) {
6422 if (!(v is Expression)){
6423 element = BitConverter.GetBytes ((float) v);
6425 for (int j = 0; j < factor; ++j)
6426 data [idx + j] = element [j];
6428 } else if (underlying_type == TypeManager.double_type) {
6429 if (!(v is Expression)){
6430 element = BitConverter.GetBytes ((double) v);
6432 for (int j = 0; j < factor; ++j)
6433 data [idx + j] = element [j];
6435 } else if (underlying_type == TypeManager.char_type){
6436 if (!(v is Expression)){
6437 int val = (int) ((char) v);
6439 data [idx] = (byte) (val & 0xff);
6440 data [idx+1] = (byte) (val >> 8);
6442 } else if (underlying_type == TypeManager.short_type){
6443 if (!(v is Expression)){
6444 int val = (int) ((short) v);
6446 data [idx] = (byte) (val & 0xff);
6447 data [idx+1] = (byte) (val >> 8);
6449 } else if (underlying_type == TypeManager.ushort_type){
6450 if (!(v is Expression)){
6451 int val = (int) ((ushort) v);
6453 data [idx] = (byte) (val & 0xff);
6454 data [idx+1] = (byte) (val >> 8);
6456 } else if (underlying_type == TypeManager.int32_type) {
6457 if (!(v is Expression)){
6460 data [idx] = (byte) (val & 0xff);
6461 data [idx+1] = (byte) ((val >> 8) & 0xff);
6462 data [idx+2] = (byte) ((val >> 16) & 0xff);
6463 data [idx+3] = (byte) (val >> 24);
6465 } else if (underlying_type == TypeManager.uint32_type) {
6466 if (!(v is Expression)){
6467 uint val = (uint) v;
6469 data [idx] = (byte) (val & 0xff);
6470 data [idx+1] = (byte) ((val >> 8) & 0xff);
6471 data [idx+2] = (byte) ((val >> 16) & 0xff);
6472 data [idx+3] = (byte) (val >> 24);
6474 } else if (underlying_type == TypeManager.sbyte_type) {
6475 if (!(v is Expression)){
6476 sbyte val = (sbyte) v;
6477 data [idx] = (byte) val;
6479 } else if (underlying_type == TypeManager.byte_type) {
6480 if (!(v is Expression)){
6481 byte val = (byte) v;
6482 data [idx] = (byte) val;
6484 } else if (underlying_type == TypeManager.bool_type) {
6485 if (!(v is Expression)){
6486 bool val = (bool) v;
6487 data [idx] = (byte) (val ? 1 : 0);
6489 } else if (underlying_type == TypeManager.decimal_type){
6490 if (!(v is Expression)){
6491 int [] bits = Decimal.GetBits ((decimal) v);
6494 // FIXME: For some reason, this doesn't work on the MS runtime.
6495 int [] nbits = new int [4];
6496 nbits [0] = bits [3];
6497 nbits [1] = bits [2];
6498 nbits [2] = bits [0];
6499 nbits [3] = bits [1];
6501 for (int j = 0; j < 4; j++){
6502 data [p++] = (byte) (nbits [j] & 0xff);
6503 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6504 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6505 data [p++] = (byte) (nbits [j] >> 24);
6509 throw new Exception ("Unrecognized type in MakeByteBlob: " + underlying_type);
6518 // Emits the initializers for the array
6520 void EmitStaticInitializers (EmitContext ec)
6523 // First, the static data
6526 ILGenerator ig = ec.ig;
6528 byte [] data = MakeByteBlob (array_data, underlying_type, loc);
6530 fb = RootContext.MakeStaticData (data);
6532 ig.Emit (OpCodes.Dup);
6533 ig.Emit (OpCodes.Ldtoken, fb);
6534 ig.Emit (OpCodes.Call,
6535 TypeManager.void_initializearray_array_fieldhandle);
6539 // Emits pieces of the array that can not be computed at compile
6540 // time (variables and string locations).
6542 // This always expect the top value on the stack to be the array
6544 void EmitDynamicInitializers (EmitContext ec)
6546 ILGenerator ig = ec.ig;
6547 int dims = bounds.Count;
6548 int [] current_pos = new int [dims];
6549 int top = array_data.Count;
6551 MethodInfo set = null;
6555 ModuleBuilder mb = null;
6556 mb = CodeGen.Module.Builder;
6557 args = new Type [dims + 1];
6560 for (j = 0; j < dims; j++)
6561 args [j] = TypeManager.int32_type;
6563 args [j] = array_element_type;
6565 set = mb.GetArrayMethod (
6567 CallingConventions.HasThis | CallingConventions.Standard,
6568 TypeManager.void_type, args);
6571 for (int i = 0; i < top; i++){
6573 Expression e = null;
6575 if (array_data [i] is Expression)
6576 e = (Expression) array_data [i];
6580 // Basically we do this for string literals and
6581 // other non-literal expressions
6583 if (e is EnumConstant){
6584 e = ((EnumConstant) e).Child;
6587 if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
6588 num_automatic_initializers <= max_automatic_initializers) {
6589 Type etype = e.Type;
6591 ig.Emit (OpCodes.Dup);
6593 for (int idx = 0; idx < dims; idx++)
6594 IntConstant.EmitInt (ig, current_pos [idx]);
6597 // If we are dealing with a struct, get the
6598 // address of it, so we can store it.
6601 etype.IsSubclassOf (TypeManager.value_type) &&
6602 (!TypeManager.IsBuiltinOrEnum (etype) ||
6603 etype == TypeManager.decimal_type)) {
6608 // Let new know that we are providing
6609 // the address where to store the results
6611 n.DisableTemporaryValueType ();
6614 ig.Emit (OpCodes.Ldelema, etype);
6621 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
6623 ig.Emit (OpCodes.Stobj, etype);
6627 ig.Emit (OpCodes.Call, set);
6635 for (int j = dims - 1; j >= 0; j--){
6637 if (current_pos [j] < (int) bounds [j])
6639 current_pos [j] = 0;
6644 void EmitArrayArguments (EmitContext ec)
6646 ILGenerator ig = ec.ig;
6648 foreach (Argument a in arguments) {
6649 Type atype = a.Type;
6652 if (atype == TypeManager.uint64_type)
6653 ig.Emit (OpCodes.Conv_Ovf_U4);
6654 else if (atype == TypeManager.int64_type)
6655 ig.Emit (OpCodes.Conv_Ovf_I4);
6659 public override void Emit (EmitContext ec)
6661 ILGenerator ig = ec.ig;
6663 EmitArrayArguments (ec);
6664 if (is_one_dimensional)
6665 ig.Emit (OpCodes.Newarr, array_element_type);
6667 if (is_builtin_type)
6668 ig.Emit (OpCodes.Newobj, (ConstructorInfo) new_method);
6670 ig.Emit (OpCodes.Newobj, (MethodInfo) new_method);
6673 if (initializers != null){
6675 // FIXME: Set this variable correctly.
6677 bool dynamic_initializers = true;
6679 // This will never be true for array types that cannot be statically
6680 // initialized. num_automatic_initializers will always be zero. See
6682 if (num_automatic_initializers > max_automatic_initializers)
6683 EmitStaticInitializers (ec);
6685 if (dynamic_initializers)
6686 EmitDynamicInitializers (ec);
6690 public object EncodeAsAttribute ()
6692 if (!is_one_dimensional){
6693 Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6697 if (array_data == null){
6698 Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6702 object [] ret = new object [array_data.Count];
6704 foreach (Expression e in array_data){
6707 if (e is NullLiteral)
6710 if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
6720 /// Represents the `this' construct
6722 public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
6725 VariableInfo variable_info;
6727 public This (Block block, Location loc)
6733 public This (Location loc)
6738 public VariableInfo VariableInfo {
6739 get { return variable_info; }
6742 public bool VerifyFixed (bool is_expression)
6744 if ((variable_info == null) || (variable_info.LocalInfo == null))
6747 return variable_info.LocalInfo.IsFixed;
6750 public bool ResolveBase (EmitContext ec)
6752 eclass = ExprClass.Variable;
6753 type = ec.ContainerType;
6756 Error (26, "Keyword this not valid in static code");
6760 if ((block != null) && (block.ThisVariable != null))
6761 variable_info = block.ThisVariable.VariableInfo;
6763 if (ec.CurrentAnonymousMethod != null)
6769 public override Expression DoResolve (EmitContext ec)
6771 if (!ResolveBase (ec))
6774 if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
6775 Error (188, "The this object cannot be used before all " +
6776 "of its fields are assigned to");
6777 variable_info.SetAssigned (ec);
6781 if (ec.IsFieldInitializer) {
6782 Error (27, "Keyword `this' can't be used outside a constructor, " +
6783 "a method or a property.");
6790 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6792 if (!ResolveBase (ec))
6795 if (variable_info != null)
6796 variable_info.SetAssigned (ec);
6798 if (ec.TypeContainer is Class){
6799 Error (1604, "Cannot assign to `this'");
6806 public void Emit (EmitContext ec, bool leave_copy)
6810 ec.ig.Emit (OpCodes.Dup);
6813 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
6815 ILGenerator ig = ec.ig;
6817 if (ec.TypeContainer is Struct){
6821 ec.ig.Emit (OpCodes.Dup);
6822 ig.Emit (OpCodes.Stobj, type);
6824 throw new Exception ("how did you get here");
6828 public override void Emit (EmitContext ec)
6830 ILGenerator ig = ec.ig;
6833 if (ec.TypeContainer is Struct)
6834 ig.Emit (OpCodes.Ldobj, type);
6837 public void AddressOf (EmitContext ec, AddressOp mode)
6842 // FIGURE OUT WHY LDARG_S does not work
6844 // consider: struct X { int val; int P { set { val = value; }}}
6846 // Yes, this looks very bad. Look at `NOTAS' for
6848 // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
6853 /// Represents the `__arglist' construct
6855 public class ArglistAccess : Expression
6857 public ArglistAccess (Location loc)
6862 public bool ResolveBase (EmitContext ec)
6864 eclass = ExprClass.Variable;
6865 type = TypeManager.runtime_argument_handle_type;
6869 public override Expression DoResolve (EmitContext ec)
6871 if (!ResolveBase (ec))
6874 if (ec.IsFieldInitializer || !ec.CurrentBlock.HasVarargs) {
6875 Error (190, "The __arglist construct is valid only within " +
6876 "a variable argument method.");
6883 public override void Emit (EmitContext ec)
6885 ec.ig.Emit (OpCodes.Arglist);
6890 /// Represents the `__arglist (....)' construct
6892 public class Arglist : Expression
6894 public readonly Argument[] Arguments;
6896 public Arglist (Argument[] args, Location l)
6902 public Type[] ArgumentTypes {
6904 Type[] retval = new Type [Arguments.Length];
6905 for (int i = 0; i < Arguments.Length; i++)
6906 retval [i] = Arguments [i].Type;
6911 public override Expression DoResolve (EmitContext ec)
6913 eclass = ExprClass.Variable;
6914 type = TypeManager.runtime_argument_handle_type;
6916 foreach (Argument arg in Arguments) {
6917 if (!arg.Resolve (ec, loc))
6924 public override void Emit (EmitContext ec)
6926 foreach (Argument arg in Arguments)
6932 // This produces the value that renders an instance, used by the iterators code
6934 public class ProxyInstance : Expression, IMemoryLocation {
6935 public override Expression DoResolve (EmitContext ec)
6937 eclass = ExprClass.Variable;
6938 type = ec.ContainerType;
6942 public override void Emit (EmitContext ec)
6944 ec.ig.Emit (OpCodes.Ldarg_0);
6948 public void AddressOf (EmitContext ec, AddressOp mode)
6950 ec.ig.Emit (OpCodes.Ldarg_0);
6955 /// Implements the typeof operator
6957 public class TypeOf : Expression {
6958 public Expression QueriedType;
6959 protected Type typearg;
6961 public TypeOf (Expression queried_type, Location l)
6963 QueriedType = queried_type;
6967 public override Expression DoResolve (EmitContext ec)
6969 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6973 typearg = texpr.ResolveType (ec);
6975 if (typearg == TypeManager.void_type) {
6976 Error (673, "System.Void cannot be used from C# - " +
6977 "use typeof (void) to get the void type object");
6981 if (typearg.IsPointer && !ec.InUnsafe){
6985 CheckObsoleteAttribute (typearg);
6987 type = TypeManager.type_type;
6988 eclass = ExprClass.Type;
6992 public override void Emit (EmitContext ec)
6994 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6995 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6998 public Type TypeArg {
6999 get { return typearg; }
7004 /// Implements the `typeof (void)' operator
7006 public class TypeOfVoid : TypeOf {
7007 public TypeOfVoid (Location l) : base (null, l)
7012 public override Expression DoResolve (EmitContext ec)
7014 type = TypeManager.type_type;
7015 typearg = TypeManager.void_type;
7016 eclass = ExprClass.Type;
7022 /// Implements the sizeof expression
7024 public class SizeOf : Expression {
7025 public Expression QueriedType;
7028 public SizeOf (Expression queried_type, Location l)
7030 this.QueriedType = queried_type;
7034 public override Expression DoResolve (EmitContext ec)
7038 233, loc, "Sizeof may only be used in an unsafe context " +
7039 "(consider using System.Runtime.InteropServices.Marshal.SizeOf");
7043 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
7047 type_queried = texpr.ResolveType (ec);
7049 CheckObsoleteAttribute (type_queried);
7051 if (!TypeManager.IsUnmanagedType (type_queried)){
7052 Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
7056 type = TypeManager.int32_type;
7057 eclass = ExprClass.Value;
7061 public override void Emit (EmitContext ec)
7063 int size = GetTypeSize (type_queried);
7066 ec.ig.Emit (OpCodes.Sizeof, type_queried);
7068 IntConstant.EmitInt (ec.ig, size);
7073 /// Implements the member access expression
7075 public class MemberAccess : Expression {
7076 public readonly string Identifier;
7079 public MemberAccess (Expression expr, string id, Location l)
7086 public Expression Expr {
7092 public static void error176 (Location loc, string name)
7094 Report.Error (176, loc, "Static member `" +
7095 name + "' cannot be accessed " +
7096 "with an instance reference, qualify with a " +
7097 "type name instead");
7100 public static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Expression left, Location loc)
7102 SimpleName sn = left_original as SimpleName;
7103 if (sn == null || left == null || left.Type.Name != sn.Name)
7106 return ec.DeclSpace.LookupType (sn.Name, true, loc) != null;
7109 // TODO: possible optimalization
7110 // Cache resolved constant result in FieldBuilder <-> expresion map
7111 public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
7112 Expression left, Location loc,
7113 Expression left_original)
7115 bool left_is_type, left_is_explicit;
7117 // If `left' is null, then we're called from SimpleNameResolve and this is
7118 // a member in the currently defining class.
7120 left_is_type = ec.IsStatic || ec.IsFieldInitializer;
7121 left_is_explicit = false;
7123 // Implicitly default to `this' unless we're static.
7124 if (!ec.IsStatic && !ec.IsFieldInitializer && !ec.InEnumContext)
7125 left = ec.GetThis (loc);
7127 left_is_type = left is TypeExpr;
7128 left_is_explicit = true;
7131 if (member_lookup is FieldExpr){
7132 FieldExpr fe = (FieldExpr) member_lookup;
7133 FieldInfo fi = fe.FieldInfo;
7134 Type decl_type = fi.DeclaringType;
7136 bool is_emitted = fi is FieldBuilder;
7137 Type t = fi.FieldType;
7140 Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
7144 if (!c.LookupConstantValue (out o))
7147 object real_value = ((Constant) c.Expr).GetValue ();
7149 Expression exp = Constantify (real_value, t);
7151 if (left_is_explicit && !left_is_type && !IdenticalNameAndTypeName (ec, left_original, left, loc)) {
7152 Report.SymbolRelatedToPreviousError (c);
7153 error176 (loc, c.GetSignatureForError ());
7161 // IsInitOnly is because of MS compatibility, I don't know why but they emit decimal constant as InitOnly
7162 if (fi.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
7163 object[] attrs = fi.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
7164 if (attrs.Length == 1)
7165 return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
7172 o = TypeManager.GetValue ((FieldBuilder) fi);
7174 o = fi.GetValue (fi);
7176 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
7177 if (left_is_explicit && !left_is_type &&
7178 !IdenticalNameAndTypeName (ec, left_original, member_lookup, loc)) {
7179 error176 (loc, fe.FieldInfo.Name);
7183 Expression enum_member = MemberLookup (
7184 ec, decl_type, "value__", MemberTypes.Field,
7185 AllBindingFlags, loc);
7187 Enum en = TypeManager.LookupEnum (decl_type);
7191 c = Constantify (o, en.UnderlyingType);
7193 c = Constantify (o, enum_member.Type);
7195 return new EnumConstant (c, decl_type);
7198 Expression exp = Constantify (o, t);
7200 if (left_is_explicit && !left_is_type) {
7201 error176 (loc, fe.FieldInfo.Name);
7208 if (t.IsPointer && !ec.InUnsafe){
7214 if (member_lookup is EventExpr) {
7215 EventExpr ee = (EventExpr) member_lookup;
7218 // If the event is local to this class, we transform ourselves into
7222 if (ee.EventInfo.DeclaringType == ec.ContainerType ||
7223 TypeManager.IsNestedChildOf(ec.ContainerType, ee.EventInfo.DeclaringType)) {
7224 MemberInfo mi = GetFieldFromEvent (ee);
7228 // If this happens, then we have an event with its own
7229 // accessors and private field etc so there's no need
7230 // to transform ourselves.
7232 ee.InstanceExpression = left;
7236 Expression ml = ExprClassFromMemberInfo (ec, mi, loc);
7239 Report.Error (-200, loc, "Internal error!!");
7243 if (!left_is_explicit)
7246 ee.InstanceExpression = left;
7248 return ResolveMemberAccess (ec, ml, left, loc, left_original);
7252 if (member_lookup is IMemberExpr) {
7253 IMemberExpr me = (IMemberExpr) member_lookup;
7254 MethodGroupExpr mg = me as MethodGroupExpr;
7257 if ((mg != null) && left_is_explicit && left.Type.IsInterface)
7258 mg.IsExplicitImpl = left_is_explicit;
7261 if ((ec.IsFieldInitializer || ec.IsStatic) &&
7262 IdenticalNameAndTypeName (ec, left_original, member_lookup, loc))
7263 return member_lookup;
7265 SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
7270 if (!me.IsInstance) {
7271 if (IdenticalNameAndTypeName (ec, left_original, left, loc))
7272 return member_lookup;
7274 if (left_is_explicit) {
7275 error176 (loc, me.Name);
7281 // Since we can not check for instance objects in SimpleName,
7282 // becaue of the rule that allows types and variables to share
7283 // the name (as long as they can be de-ambiguated later, see
7284 // IdenticalNameAndTypeName), we have to check whether left
7285 // is an instance variable in a static context
7287 // However, if the left-hand value is explicitly given, then
7288 // it is already our instance expression, so we aren't in
7292 if (ec.IsStatic && !left_is_explicit && left is IMemberExpr){
7293 IMemberExpr mexp = (IMemberExpr) left;
7295 if (!mexp.IsStatic){
7296 SimpleName.Error_ObjectRefRequired (ec, loc, mexp.Name);
7301 if ((mg != null) && IdenticalNameAndTypeName (ec, left_original, left, loc))
7302 mg.IdenticalTypeName = true;
7304 me.InstanceExpression = left;
7307 return member_lookup;
7310 Console.WriteLine ("Left is: " + left);
7311 Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
7312 Environment.Exit (1);
7316 public Expression DoResolve (EmitContext ec, Expression right_side, ResolveFlags flags)
7319 throw new Exception ();
7322 // Resolve the expression with flow analysis turned off, we'll do the definite
7323 // assignment checks later. This is because we don't know yet what the expression
7324 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7325 // definite assignment check on the actual field and not on the whole struct.
7328 Expression original = expr;
7329 expr = expr.Resolve (ec, flags | ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
7333 if (expr is Namespace) {
7334 Namespace ns = (Namespace) expr;
7335 FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
7337 Report.Error (234, loc, "The type or namespace name `{0}' could not be found in namespace `{1}'", Identifier, ns.FullName);
7342 // TODO: I mailed Ravi about this, and apparently we can get rid
7343 // of this and put it in the right place.
7345 // Handle enums here when they are in transit.
7346 // Note that we cannot afford to hit MemberLookup in this case because
7347 // it will fail to find any members at all
7350 Type expr_type = expr.Type;
7351 if (expr is TypeExpr){
7352 if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
7353 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", expr_type);
7357 if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
7358 Enum en = TypeManager.LookupEnum (expr_type);
7361 object value = en.LookupEnumValue (ec, Identifier, loc);
7364 MemberCore mc = en.GetDefinition (Identifier);
7365 ObsoleteAttribute oa = mc.GetObsoleteAttribute (en);
7367 AttributeTester.Report_ObsoleteMessage (oa, mc.GetSignatureForError (), Location);
7369 oa = en.GetObsoleteAttribute (en);
7371 AttributeTester.Report_ObsoleteMessage (oa, en.GetSignatureForError (), Location);
7374 Constant c = Constantify (value, en.UnderlyingType);
7375 return new EnumConstant (c, expr_type);
7378 CheckObsoleteAttribute (expr_type);
7380 FieldInfo fi = expr_type.GetField (Identifier);
7382 ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
7384 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), Location);
7390 if (expr_type.IsPointer){
7391 Error (23, "The `.' operator can not be applied to pointer operands (" +
7392 TypeManager.CSharpName (expr_type) + ")");
7396 Expression member_lookup;
7397 member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
7398 if (member_lookup == null)
7401 if (member_lookup is TypeExpr) {
7402 if (!(expr is TypeExpr) &&
7403 !IdenticalNameAndTypeName (ec, original, expr, loc)) {
7404 Error (572, "Can't reference type `" + Identifier + "' through an expression; try `" +
7405 member_lookup.Type + "' instead");
7409 return member_lookup;
7412 member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
7413 if (member_lookup == null)
7416 // The following DoResolve/DoResolveLValue will do the definite assignment
7419 if (right_side != null)
7420 member_lookup = member_lookup.DoResolveLValue (ec, right_side);
7422 member_lookup = member_lookup.DoResolve (ec);
7424 return member_lookup;
7427 public override Expression DoResolve (EmitContext ec)
7429 return DoResolve (ec, null, ResolveFlags.VariableOrValue | ResolveFlags.Type);
7432 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7434 return DoResolve (ec, right_side, ResolveFlags.VariableOrValue | ResolveFlags.Type);
7437 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
7439 return ResolveNamespaceOrType (ec, false);
7442 public FullNamedExpression ResolveNamespaceOrType (EmitContext ec, bool silent)
7444 FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec);
7446 if (new_expr == null)
7449 if (new_expr is Namespace) {
7450 Namespace ns = (Namespace) new_expr;
7451 FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
7452 if (!silent && retval == null)
7453 Report.Error (234, loc, "The type or namespace name `{0}' could not be found in namespace `{1}'", Identifier, ns.FullName);
7457 Type expr_type = new_expr.Type;
7459 if (expr_type.IsPointer){
7460 Error (23, "The `.' operator can not be applied to pointer operands (" +
7461 TypeManager.CSharpName (expr_type) + ")");
7465 Expression member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
7466 if (!silent && member_lookup == null) {
7467 Report.Error (234, loc, "The type name `{0}' could not be found in type `{1}'",
7468 Identifier, new_expr.FullName);
7472 if (!(member_lookup is TypeExpr)) {
7473 Report.Error (118, loc, "'{0}.{1}' denotes a '{2}', where a type was expected",
7474 new_expr.FullName, Identifier, member_lookup.ExprClassName ());
7478 member_lookup = member_lookup.Resolve (ec, ResolveFlags.Type);
7479 return (member_lookup as TypeExpr);
7482 public override void Emit (EmitContext ec)
7484 throw new Exception ("Should not happen");
7487 public override string ToString ()
7489 return expr + "." + Identifier;
7494 /// Implements checked expressions
7496 public class CheckedExpr : Expression {
7498 public Expression Expr;
7500 public CheckedExpr (Expression e, Location l)
7506 public override Expression DoResolve (EmitContext ec)
7508 bool last_check = ec.CheckState;
7509 bool last_const_check = ec.ConstantCheckState;
7511 ec.CheckState = true;
7512 ec.ConstantCheckState = true;
7513 Expr = Expr.Resolve (ec);
7514 ec.CheckState = last_check;
7515 ec.ConstantCheckState = last_const_check;
7520 if (Expr is Constant)
7523 eclass = Expr.eclass;
7528 public override void Emit (EmitContext ec)
7530 bool last_check = ec.CheckState;
7531 bool last_const_check = ec.ConstantCheckState;
7533 ec.CheckState = true;
7534 ec.ConstantCheckState = true;
7536 ec.CheckState = last_check;
7537 ec.ConstantCheckState = last_const_check;
7543 /// Implements the unchecked expression
7545 public class UnCheckedExpr : Expression {
7547 public Expression Expr;
7549 public UnCheckedExpr (Expression e, Location l)
7555 public override Expression DoResolve (EmitContext ec)
7557 bool last_check = ec.CheckState;
7558 bool last_const_check = ec.ConstantCheckState;
7560 ec.CheckState = false;
7561 ec.ConstantCheckState = false;
7562 Expr = Expr.Resolve (ec);
7563 ec.CheckState = last_check;
7564 ec.ConstantCheckState = last_const_check;
7569 if (Expr is Constant)
7572 eclass = Expr.eclass;
7577 public override void Emit (EmitContext ec)
7579 bool last_check = ec.CheckState;
7580 bool last_const_check = ec.ConstantCheckState;
7582 ec.CheckState = false;
7583 ec.ConstantCheckState = false;
7585 ec.CheckState = last_check;
7586 ec.ConstantCheckState = last_const_check;
7592 /// An Element Access expression.
7594 /// During semantic analysis these are transformed into
7595 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7597 public class ElementAccess : Expression {
7598 public ArrayList Arguments;
7599 public Expression Expr;
7601 public ElementAccess (Expression e, ArrayList e_list, Location l)
7610 Arguments = new ArrayList ();
7611 foreach (Expression tmp in e_list)
7612 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7616 bool CommonResolve (EmitContext ec)
7618 Expr = Expr.Resolve (ec);
7623 if (Arguments == null)
7626 foreach (Argument a in Arguments){
7627 if (!a.Resolve (ec, loc))
7634 Expression MakePointerAccess (EmitContext ec, Type t)
7636 if (t == TypeManager.void_ptr_type){
7637 Error (242, "The array index operation is not valid for void pointers");
7640 if (Arguments.Count != 1){
7641 Error (196, "A pointer must be indexed by a single value");
7646 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
7649 return new Indirection (p, loc).Resolve (ec);
7652 public override Expression DoResolve (EmitContext ec)
7654 if (!CommonResolve (ec))
7658 // We perform some simple tests, and then to "split" the emit and store
7659 // code we create an instance of a different class, and return that.
7661 // I am experimenting with this pattern.
7665 if (t == TypeManager.array_type){
7666 Report.Error (21, loc, "Cannot use indexer on System.Array");
7671 return (new ArrayAccess (this, loc)).Resolve (ec);
7673 return MakePointerAccess (ec, Expr.Type);
7675 FieldExpr fe = Expr as FieldExpr;
7677 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7679 return MakePointerAccess (ec, ff.ElementType);
7682 return (new IndexerAccess (this, loc)).Resolve (ec);
7685 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7687 if (!CommonResolve (ec))
7692 return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
7695 return MakePointerAccess (ec, Expr.Type);
7697 FieldExpr fe = Expr as FieldExpr;
7699 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7701 // TODO: not sure whether it is correct
7702 // if (!ec.InFixedInitializer) {
7703 // if (!ec.InFixedInitializer) {
7704 // Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement.");
7707 return MakePointerAccess (ec, ff.ElementType);
7710 return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
7713 public override void Emit (EmitContext ec)
7715 throw new Exception ("Should never be reached");
7720 /// Implements array access
7722 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7724 // Points to our "data" repository
7728 LocalTemporary temp;
7731 public ArrayAccess (ElementAccess ea_data, Location l)
7734 eclass = ExprClass.Variable;
7738 public override Expression DoResolve (EmitContext ec)
7741 ExprClass eclass = ea.Expr.eclass;
7743 // As long as the type is valid
7744 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7745 eclass == ExprClass.Value)) {
7746 ea.Expr.Error_UnexpectedKind ("variable or value");
7751 Type t = ea.Expr.Type;
7752 if (t.GetArrayRank () != ea.Arguments.Count){
7754 "Incorrect number of indexes for array " +
7755 " expected: " + t.GetArrayRank () + " got: " +
7756 ea.Arguments.Count);
7760 type = TypeManager.GetElementType (t);
7761 if (type.IsPointer && !ec.InUnsafe){
7762 UnsafeError (ea.Location);
7766 foreach (Argument a in ea.Arguments){
7767 Type argtype = a.Type;
7769 if (argtype == TypeManager.int32_type ||
7770 argtype == TypeManager.uint32_type ||
7771 argtype == TypeManager.int64_type ||
7772 argtype == TypeManager.uint64_type) {
7773 Constant c = a.Expr as Constant;
7774 if (c != null && c.IsNegative) {
7775 Report.Warning (251, 2, a.Expr.Location, "Indexing an array with a negative index (array indices always start at zero)");
7781 // Mhm. This is strage, because the Argument.Type is not the same as
7782 // Argument.Expr.Type: the value changes depending on the ref/out setting.
7784 // Wonder if I will run into trouble for this.
7786 a.Expr = ExpressionToArrayArgument (ec, a.Expr, ea.Location);
7791 eclass = ExprClass.Variable;
7797 /// Emits the right opcode to load an object of Type `t'
7798 /// from an array of T
7800 static public void EmitLoadOpcode (ILGenerator ig, Type type)
7802 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7803 ig.Emit (OpCodes.Ldelem_U1);
7804 else if (type == TypeManager.sbyte_type)
7805 ig.Emit (OpCodes.Ldelem_I1);
7806 else if (type == TypeManager.short_type)
7807 ig.Emit (OpCodes.Ldelem_I2);
7808 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7809 ig.Emit (OpCodes.Ldelem_U2);
7810 else if (type == TypeManager.int32_type)
7811 ig.Emit (OpCodes.Ldelem_I4);
7812 else if (type == TypeManager.uint32_type)
7813 ig.Emit (OpCodes.Ldelem_U4);
7814 else if (type == TypeManager.uint64_type)
7815 ig.Emit (OpCodes.Ldelem_I8);
7816 else if (type == TypeManager.int64_type)
7817 ig.Emit (OpCodes.Ldelem_I8);
7818 else if (type == TypeManager.float_type)
7819 ig.Emit (OpCodes.Ldelem_R4);
7820 else if (type == TypeManager.double_type)
7821 ig.Emit (OpCodes.Ldelem_R8);
7822 else if (type == TypeManager.intptr_type)
7823 ig.Emit (OpCodes.Ldelem_I);
7824 else if (TypeManager.IsEnumType (type)){
7825 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type));
7826 } else if (type.IsValueType){
7827 ig.Emit (OpCodes.Ldelema, type);
7828 ig.Emit (OpCodes.Ldobj, type);
7830 ig.Emit (OpCodes.Ldelem_Ref);
7834 /// Returns the right opcode to store an object of Type `t'
7835 /// from an array of T.
7837 static public OpCode GetStoreOpcode (Type t, out bool is_stobj)
7839 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7841 t = TypeManager.TypeToCoreType (t);
7842 if (TypeManager.IsEnumType (t))
7843 t = TypeManager.EnumToUnderlying (t);
7844 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7845 t == TypeManager.bool_type)
7846 return OpCodes.Stelem_I1;
7847 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7848 t == TypeManager.char_type)
7849 return OpCodes.Stelem_I2;
7850 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7851 return OpCodes.Stelem_I4;
7852 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7853 return OpCodes.Stelem_I8;
7854 else if (t == TypeManager.float_type)
7855 return OpCodes.Stelem_R4;
7856 else if (t == TypeManager.double_type)
7857 return OpCodes.Stelem_R8;
7858 else if (t == TypeManager.intptr_type) {
7860 return OpCodes.Stobj;
7861 } else if (t.IsValueType) {
7863 return OpCodes.Stobj;
7865 return OpCodes.Stelem_Ref;
7868 MethodInfo FetchGetMethod ()
7870 ModuleBuilder mb = CodeGen.Module.Builder;
7871 int arg_count = ea.Arguments.Count;
7872 Type [] args = new Type [arg_count];
7875 for (int i = 0; i < arg_count; i++){
7876 //args [i++] = a.Type;
7877 args [i] = TypeManager.int32_type;
7880 get = mb.GetArrayMethod (
7881 ea.Expr.Type, "Get",
7882 CallingConventions.HasThis |
7883 CallingConventions.Standard,
7889 MethodInfo FetchAddressMethod ()
7891 ModuleBuilder mb = CodeGen.Module.Builder;
7892 int arg_count = ea.Arguments.Count;
7893 Type [] args = new Type [arg_count];
7897 ret_type = TypeManager.GetReferenceType (type);
7899 for (int i = 0; i < arg_count; i++){
7900 //args [i++] = a.Type;
7901 args [i] = TypeManager.int32_type;
7904 address = mb.GetArrayMethod (
7905 ea.Expr.Type, "Address",
7906 CallingConventions.HasThis |
7907 CallingConventions.Standard,
7914 // Load the array arguments into the stack.
7916 // If we have been requested to cache the values (cached_locations array
7917 // initialized), then load the arguments the first time and store them
7918 // in locals. otherwise load from local variables.
7920 void LoadArrayAndArguments (EmitContext ec)
7922 ILGenerator ig = ec.ig;
7925 foreach (Argument a in ea.Arguments){
7926 Type argtype = a.Expr.Type;
7930 if (argtype == TypeManager.int64_type)
7931 ig.Emit (OpCodes.Conv_Ovf_I);
7932 else if (argtype == TypeManager.uint64_type)
7933 ig.Emit (OpCodes.Conv_Ovf_I_Un);
7937 public void Emit (EmitContext ec, bool leave_copy)
7939 int rank = ea.Expr.Type.GetArrayRank ();
7940 ILGenerator ig = ec.ig;
7943 LoadArrayAndArguments (ec);
7946 EmitLoadOpcode (ig, type);
7950 method = FetchGetMethod ();
7951 ig.Emit (OpCodes.Call, method);
7954 LoadFromPtr (ec.ig, this.type);
7957 ec.ig.Emit (OpCodes.Dup);
7958 temp = new LocalTemporary (ec, this.type);
7963 public override void Emit (EmitContext ec)
7968 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7970 int rank = ea.Expr.Type.GetArrayRank ();
7971 ILGenerator ig = ec.ig;
7972 Type t = source.Type;
7973 prepared = prepare_for_load;
7975 if (prepare_for_load) {
7976 AddressOf (ec, AddressOp.LoadStore);
7977 ec.ig.Emit (OpCodes.Dup);
7980 ec.ig.Emit (OpCodes.Dup);
7981 temp = new LocalTemporary (ec, this.type);
7984 StoreFromPtr (ec.ig, t);
7992 LoadArrayAndArguments (ec);
7996 OpCode op = GetStoreOpcode (t, out is_stobj);
7998 // The stobj opcode used by value types will need
7999 // an address on the stack, not really an array/array
8003 ig.Emit (OpCodes.Ldelema, t);
8007 ec.ig.Emit (OpCodes.Dup);
8008 temp = new LocalTemporary (ec, this.type);
8013 ig.Emit (OpCodes.Stobj, t);
8017 ModuleBuilder mb = CodeGen.Module.Builder;
8018 int arg_count = ea.Arguments.Count;
8019 Type [] args = new Type [arg_count + 1];
8024 ec.ig.Emit (OpCodes.Dup);
8025 temp = new LocalTemporary (ec, this.type);
8029 for (int i = 0; i < arg_count; i++){
8030 //args [i++] = a.Type;
8031 args [i] = TypeManager.int32_type;
8034 args [arg_count] = type;
8036 set = mb.GetArrayMethod (
8037 ea.Expr.Type, "Set",
8038 CallingConventions.HasThis |
8039 CallingConventions.Standard,
8040 TypeManager.void_type, args);
8042 ig.Emit (OpCodes.Call, set);
8049 public void AddressOf (EmitContext ec, AddressOp mode)
8051 int rank = ea.Expr.Type.GetArrayRank ();
8052 ILGenerator ig = ec.ig;
8054 LoadArrayAndArguments (ec);
8057 ig.Emit (OpCodes.Ldelema, type);
8059 MethodInfo address = FetchAddressMethod ();
8060 ig.Emit (OpCodes.Call, address);
8067 public ArrayList Properties;
8068 static Hashtable map;
8070 public struct Indexer {
8071 public readonly Type Type;
8072 public readonly MethodInfo Getter, Setter;
8074 public Indexer (Type type, MethodInfo get, MethodInfo set)
8084 map = new Hashtable ();
8089 Properties = new ArrayList ();
8092 void Append (MemberInfo [] mi)
8094 foreach (PropertyInfo property in mi){
8095 MethodInfo get, set;
8097 get = property.GetGetMethod (true);
8098 set = property.GetSetMethod (true);
8099 Properties.Add (new Indexer (property.PropertyType, get, set));
8103 static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
8105 string p_name = TypeManager.IndexerPropertyName (lookup_type);
8107 MemberInfo [] mi = TypeManager.MemberLookup (
8108 caller_type, caller_type, lookup_type, MemberTypes.Property,
8109 BindingFlags.Public | BindingFlags.Instance |
8110 BindingFlags.DeclaredOnly, p_name, null);
8112 if (mi == null || mi.Length == 0)
8118 static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc)
8120 Indexers ix = (Indexers) map [lookup_type];
8125 Type copy = lookup_type;
8126 while (copy != TypeManager.object_type && copy != null){
8127 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy);
8131 ix = new Indexers ();
8136 copy = copy.BaseType;
8139 if (!lookup_type.IsInterface)
8142 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
8143 if (ifaces != null) {
8144 foreach (Type itype in ifaces) {
8145 MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype);
8148 ix = new Indexers ();
8160 /// Expressions that represent an indexer call.
8162 public class IndexerAccess : Expression, IAssignMethod {
8164 // Points to our "data" repository
8166 MethodInfo get, set;
8167 ArrayList set_arguments;
8168 bool is_base_indexer;
8170 protected Type indexer_type;
8171 protected Type current_type;
8172 protected Expression instance_expr;
8173 protected ArrayList arguments;
8175 public IndexerAccess (ElementAccess ea, Location loc)
8176 : this (ea.Expr, false, loc)
8178 this.arguments = ea.Arguments;
8181 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8184 this.instance_expr = instance_expr;
8185 this.is_base_indexer = is_base_indexer;
8186 this.eclass = ExprClass.Value;
8190 protected virtual bool CommonResolve (EmitContext ec)
8192 indexer_type = instance_expr.Type;
8193 current_type = ec.ContainerType;
8198 public override Expression DoResolve (EmitContext ec)
8200 ArrayList AllGetters = new ArrayList();
8201 if (!CommonResolve (ec))
8205 // Step 1: Query for all `Item' *properties*. Notice
8206 // that the actual methods are pointed from here.
8208 // This is a group of properties, piles of them.
8210 bool found_any = false, found_any_getters = false;
8211 Type lookup_type = indexer_type;
8214 ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
8215 if (ilist != null) {
8217 if (ilist.Properties != null) {
8218 foreach (Indexers.Indexer ix in ilist.Properties) {
8219 if (ix.Getter != null)
8220 AllGetters.Add(ix.Getter);
8225 if (AllGetters.Count > 0) {
8226 found_any_getters = true;
8227 get = (MethodInfo) Invocation.OverloadResolve (
8228 ec, new MethodGroupExpr (AllGetters, loc),
8229 arguments, false, loc);
8233 Report.Error (21, loc,
8234 "Type `" + TypeManager.CSharpName (indexer_type) +
8235 "' does not have any indexers defined");
8239 if (!found_any_getters) {
8240 Error (154, "indexer can not be used in this context, because " +
8241 "it lacks a `get' accessor");
8246 Error (1501, "No Overload for method `this' takes `" +
8247 arguments.Count + "' arguments");
8252 // Only base will allow this invocation to happen.
8254 if (get.IsAbstract && this is BaseIndexerAccess){
8255 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (get));
8259 type = get.ReturnType;
8260 if (type.IsPointer && !ec.InUnsafe){
8265 instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
8267 eclass = ExprClass.IndexerAccess;
8271 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8273 ArrayList AllSetters = new ArrayList();
8274 if (!CommonResolve (ec))
8277 bool found_any = false, found_any_setters = false;
8279 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
8280 if (ilist != null) {
8282 if (ilist.Properties != null) {
8283 foreach (Indexers.Indexer ix in ilist.Properties) {
8284 if (ix.Setter != null)
8285 AllSetters.Add(ix.Setter);
8289 if (AllSetters.Count > 0) {
8290 found_any_setters = true;
8291 set_arguments = (ArrayList) arguments.Clone ();
8292 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
8293 set = (MethodInfo) Invocation.OverloadResolve (
8294 ec, new MethodGroupExpr (AllSetters, loc),
8295 set_arguments, false, loc);
8299 Report.Error (21, loc,
8300 "Type `" + TypeManager.CSharpName (indexer_type) +
8301 "' does not have any indexers defined");
8305 if (!found_any_setters) {
8306 Error (154, "indexer can not be used in this context, because " +
8307 "it lacks a `set' accessor");
8312 Error (1501, "No Overload for method `this' takes `" +
8313 arguments.Count + "' arguments");
8318 // Only base will allow this invocation to happen.
8320 if (set.IsAbstract && this is BaseIndexerAccess){
8321 Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set));
8326 // Now look for the actual match in the list of indexers to set our "return" type
8328 type = TypeManager.void_type; // default value
8329 foreach (Indexers.Indexer ix in ilist.Properties){
8330 if (ix.Setter == set){
8336 instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
8338 eclass = ExprClass.IndexerAccess;
8342 bool prepared = false;
8343 LocalTemporary temp;
8345 public void Emit (EmitContext ec, bool leave_copy)
8347 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
8349 ec.ig.Emit (OpCodes.Dup);
8350 temp = new LocalTemporary (ec, Type);
8356 // source is ignored, because we already have a copy of it from the
8357 // LValue resolution and we have already constructed a pre-cached
8358 // version of the arguments (ea.set_arguments);
8360 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8362 prepared = prepare_for_load;
8363 Argument a = (Argument) set_arguments [set_arguments.Count - 1];
8368 ec.ig.Emit (OpCodes.Dup);
8369 temp = new LocalTemporary (ec, Type);
8372 } else if (leave_copy) {
8373 temp = new LocalTemporary (ec, Type);
8379 Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
8386 public override void Emit (EmitContext ec)
8393 /// The base operator for method names
8395 public class BaseAccess : Expression {
8398 public BaseAccess (string member, Location l)
8400 this.member = member;
8404 public override Expression DoResolve (EmitContext ec)
8406 Expression c = CommonResolve (ec);
8412 // MethodGroups use this opportunity to flag an error on lacking ()
8414 if (!(c is MethodGroupExpr))
8415 return c.Resolve (ec);
8419 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8421 Expression c = CommonResolve (ec);
8427 // MethodGroups use this opportunity to flag an error on lacking ()
8429 if (! (c is MethodGroupExpr))
8430 return c.DoResolveLValue (ec, right_side);
8435 Expression CommonResolve (EmitContext ec)
8437 Expression member_lookup;
8438 Type current_type = ec.ContainerType;
8439 Type base_type = current_type.BaseType;
8443 Error (1511, "Keyword base is not allowed in static method");
8447 if (ec.IsFieldInitializer){
8448 Error (1512, "Keyword base is not available in the current context");
8452 member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member,
8453 AllMemberTypes, AllBindingFlags, loc);
8454 if (member_lookup == null) {
8455 MemberLookupFailed (ec, base_type, base_type, member, null, loc);
8462 left = new TypeExpression (base_type, loc);
8464 left = ec.GetThis (loc);
8466 e = MemberAccess.ResolveMemberAccess (ec, member_lookup, left, loc, null);
8468 if (e is PropertyExpr){
8469 PropertyExpr pe = (PropertyExpr) e;
8474 if (e is MethodGroupExpr)
8475 ((MethodGroupExpr) e).IsBase = true;
8480 public override void Emit (EmitContext ec)
8482 throw new Exception ("Should never be called");
8487 /// The base indexer operator
8489 public class BaseIndexerAccess : IndexerAccess {
8490 public BaseIndexerAccess (ArrayList args, Location loc)
8491 : base (null, true, loc)
8493 arguments = new ArrayList ();
8494 foreach (Expression tmp in args)
8495 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8498 protected override bool CommonResolve (EmitContext ec)
8500 instance_expr = ec.GetThis (loc);
8502 current_type = ec.ContainerType.BaseType;
8503 indexer_type = current_type;
8505 foreach (Argument a in arguments){
8506 if (!a.Resolve (ec, loc))
8515 /// This class exists solely to pass the Type around and to be a dummy
8516 /// that can be passed to the conversion functions (this is used by
8517 /// foreach implementation to typecast the object return value from
8518 /// get_Current into the proper type. All code has been generated and
8519 /// we only care about the side effect conversions to be performed
8521 /// This is also now used as a placeholder where a no-action expression
8522 /// is needed (the `New' class).
8524 public class EmptyExpression : Expression {
8525 public static readonly EmptyExpression Null = new EmptyExpression ();
8527 // TODO: should be protected
8528 public EmptyExpression ()
8530 type = TypeManager.object_type;
8531 eclass = ExprClass.Value;
8532 loc = Location.Null;
8535 public EmptyExpression (Type t)
8538 eclass = ExprClass.Value;
8539 loc = Location.Null;
8542 public override Expression DoResolve (EmitContext ec)
8547 public override void Emit (EmitContext ec)
8549 // nothing, as we only exist to not do anything.
8553 // This is just because we might want to reuse this bad boy
8554 // instead of creating gazillions of EmptyExpressions.
8555 // (CanImplicitConversion uses it)
8557 public void SetType (Type t)
8563 public class UserCast : Expression {
8567 public UserCast (MethodInfo method, Expression source, Location l)
8569 this.method = method;
8570 this.source = source;
8571 type = method.ReturnType;
8572 eclass = ExprClass.Value;
8576 public Expression Source {
8582 public override Expression DoResolve (EmitContext ec)
8585 // We are born fully resolved
8590 public override void Emit (EmitContext ec)
8592 ILGenerator ig = ec.ig;
8596 if (method is MethodInfo)
8597 ig.Emit (OpCodes.Call, (MethodInfo) method);
8599 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8605 // This class is used to "construct" the type during a typecast
8606 // operation. Since the Type.GetType class in .NET can parse
8607 // the type specification, we just use this to construct the type
8608 // one bit at a time.
8610 public class ComposedCast : TypeExpr {
8614 public ComposedCast (Expression left, string dim, Location l)
8621 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
8623 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
8627 Type ltype = lexpr.ResolveType (ec);
8629 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8630 Report.Error (1547, Location,
8631 "Keyword 'void' cannot be used in this context");
8636 // ltype.Fullname is already fully qualified, so we can skip
8637 // a lot of probes, and go directly to TypeManager.LookupType
8639 string cname = ltype.FullName + dim;
8640 type = TypeManager.LookupTypeDirect (cname);
8643 // For arrays of enumerations we are having a problem
8644 // with the direct lookup. Need to investigate.
8646 // For now, fall back to the full lookup in that case.
8648 FullNamedExpression e = ec.DeclSpace.LookupType (cname, false, loc);
8650 type = ((TypeExpr) e).ResolveType (ec);
8655 if (!ec.InUnsafe && type.IsPointer){
8660 if (type.IsArray && (type.GetElementType () == TypeManager.arg_iterator_type ||
8661 type.GetElementType () == TypeManager.typed_reference_type)) {
8662 Report.Error (611, loc, "Array elements cannot be of type '{0}'", TypeManager.CSharpName (type.GetElementType ()));
8666 eclass = ExprClass.Type;
8670 public override string Name {
8676 public override string FullName {
8678 return type.FullName;
8683 public class FixedBufferPtr: Expression {
8686 public FixedBufferPtr (Expression array, Type array_type, Location l)
8691 type = TypeManager.GetPointerType (array_type);
8692 eclass = ExprClass.Value;
8695 public override void Emit(EmitContext ec)
8700 public override Expression DoResolve (EmitContext ec)
8703 // We are born fully resolved
8711 // This class is used to represent the address of an array, used
8712 // only by the Fixed statement, this generates "&a [0]" construct
8713 // for fixed (char *pa = a)
8715 public class ArrayPtr : FixedBufferPtr {
8718 public ArrayPtr (Expression array, Type array_type, Location l):
8719 base (array, array_type, l)
8721 this.array_type = array_type;
8724 public override void Emit (EmitContext ec)
8728 ILGenerator ig = ec.ig;
8729 IntLiteral.EmitInt (ig, 0);
8730 ig.Emit (OpCodes.Ldelema, array_type);
8735 // Used by the fixed statement
8737 public class StringPtr : Expression {
8740 public StringPtr (LocalBuilder b, Location l)
8743 eclass = ExprClass.Value;
8744 type = TypeManager.char_ptr_type;
8748 public override Expression DoResolve (EmitContext ec)
8750 // This should never be invoked, we are born in fully
8751 // initialized state.
8756 public override void Emit (EmitContext ec)
8758 ILGenerator ig = ec.ig;
8760 ig.Emit (OpCodes.Ldloc, b);
8761 ig.Emit (OpCodes.Conv_I);
8762 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8763 ig.Emit (OpCodes.Add);
8768 // Implements the `stackalloc' keyword
8770 public class StackAlloc : Expression {
8775 public StackAlloc (Expression type, Expression count, Location l)
8782 public override Expression DoResolve (EmitContext ec)
8784 count = count.Resolve (ec);
8788 if (count.Type != TypeManager.int32_type){
8789 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8794 Constant c = count as Constant;
8795 if (c != null && c.IsNegative) {
8796 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8800 if (ec.CurrentBranching.InCatch () ||
8801 ec.CurrentBranching.InFinally (true)) {
8803 "stackalloc can not be used in a catch or finally block");
8807 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
8811 otype = texpr.ResolveType (ec);
8813 if (!TypeManager.VerifyUnManaged (otype, loc))
8816 type = TypeManager.GetPointerType (otype);
8817 eclass = ExprClass.Value;
8822 public override void Emit (EmitContext ec)
8824 int size = GetTypeSize (otype);
8825 ILGenerator ig = ec.ig;
8828 ig.Emit (OpCodes.Sizeof, otype);
8830 IntConstant.EmitInt (ig, size);
8832 ig.Emit (OpCodes.Mul);
8833 ig.Emit (OpCodes.Localloc);