2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // (C) 2001, 2002, 2003 Ximian, Inc.
9 // (C) 2003, 2004 Novell, Inc.
13 namespace Mono.CSharp {
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
21 /// This is just a helper class, it is generated by Unary, UnaryMutator
22 /// when an overloaded method has been found. It just emits the code for a
25 public class StaticCallExpr : ExpressionStatement {
29 public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
35 eclass = ExprClass.Value;
39 public override Expression DoResolve (EmitContext ec)
42 // We are born fully resolved
47 public override void Emit (EmitContext ec)
49 Invocation.EmitArguments (ec, args, false, null);
50 ec.ig.Emit (OpCodes.Call, mi);
54 static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
55 Expression e, Location loc)
59 args = new ArrayList (1);
60 Argument a = new Argument (e, Argument.AType.Expression);
62 // We need to resolve the arguments before sending them in !
63 if (!a.Resolve (ec, loc))
67 mg = mg.OverloadResolve (ec, ref args, false, loc);
72 return new StaticCallExpr ((MethodInfo) mg, args, loc);
75 public override void EmitStatement (EmitContext ec)
78 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
79 ec.ig.Emit (OpCodes.Pop);
82 public MethodInfo Method {
87 public class ParenthesizedExpression : Expression
89 public Expression Expr;
91 public ParenthesizedExpression (Expression expr)
96 public override Expression DoResolve (EmitContext ec)
98 Expr = Expr.Resolve (ec);
102 public override void Emit (EmitContext ec)
104 throw new Exception ("Should not happen");
107 public override Location Location
110 return Expr.Location;
114 protected override void CloneTo (CloneContext clonectx, Expression t)
116 ParenthesizedExpression target = (ParenthesizedExpression) t;
118 target.Expr = Expr.Clone (clonectx);
123 // Unary implements unary expressions.
125 public class Unary : Expression {
126 public enum Operator : byte {
127 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
128 Indirection, AddressOf, TOP
131 public readonly Operator Oper;
132 public Expression Expr;
134 public Unary (Operator op, Expression expr, Location loc)
142 /// Returns a stringified representation of the Operator
144 static public string OperName (Operator oper)
147 case Operator.UnaryPlus:
149 case Operator.UnaryNegation:
151 case Operator.LogicalNot:
153 case Operator.OnesComplement:
155 case Operator.AddressOf:
157 case Operator.Indirection:
161 return oper.ToString ();
164 public static readonly string [] oper_names;
168 oper_names = new string [(int)Operator.TOP];
170 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
171 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
172 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
173 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
174 oper_names [(int) Operator.Indirection] = "op_Indirection";
175 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
178 public static void Error_OperatorCannotBeApplied (Location loc, string oper, Type t)
180 Error_OperatorCannotBeApplied (loc, oper, TypeManager.CSharpName (t));
183 public static void Error_OperatorCannotBeApplied (Location loc, string oper, string type)
185 Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
189 void Error23 (Type t)
191 Error_OperatorCannotBeApplied (loc, OperName (Oper), t);
195 // This routine will attempt to simplify the unary expression when the
196 // argument is a constant.
198 Constant TryReduceConstant (EmitContext ec, Constant e)
200 Type expr_type = e.Type;
203 case Operator.UnaryPlus:
204 // Unary numeric promotions
205 if (expr_type == TypeManager.byte_type)
206 return new IntConstant (((ByteConstant)e).Value, e.Location);
207 if (expr_type == TypeManager.sbyte_type)
208 return new IntConstant (((SByteConstant)e).Value, e.Location);
209 if (expr_type == TypeManager.short_type)
210 return new IntConstant (((ShortConstant)e).Value, e.Location);
211 if (expr_type == TypeManager.ushort_type)
212 return new IntConstant (((UShortConstant)e).Value, e.Location);
213 if (expr_type == TypeManager.char_type)
214 return new IntConstant (((CharConstant)e).Value, e.Location);
216 // Predefined operators
217 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
218 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
219 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
220 expr_type == TypeManager.decimal_type)
227 case Operator.UnaryNegation:
228 // Unary numeric promotions
229 if (expr_type == TypeManager.byte_type)
230 return new IntConstant (-((ByteConstant)e).Value, e.Location);
231 if (expr_type == TypeManager.sbyte_type)
232 return new IntConstant (-((SByteConstant)e).Value, e.Location);
233 if (expr_type == TypeManager.short_type)
234 return new IntConstant (-((ShortConstant)e).Value, e.Location);
235 if (expr_type == TypeManager.ushort_type)
236 return new IntConstant (-((UShortConstant)e).Value, e.Location);
237 if (expr_type == TypeManager.char_type)
238 return new IntConstant (-((CharConstant)e).Value, e.Location);
240 // Predefined operators
241 if (expr_type == TypeManager.int32_type) {
242 int value = ((IntConstant)e).Value;
243 if (value == int.MinValue) {
244 if (ec.ConstantCheckState) {
245 ConstantFold.Error_CompileTimeOverflow (loc);
250 return new IntConstant (-value, e.Location);
252 if (expr_type == TypeManager.int64_type) {
253 long value = ((LongConstant)e).Value;
254 if (value == long.MinValue) {
255 if (ec.ConstantCheckState) {
256 ConstantFold.Error_CompileTimeOverflow (loc);
261 return new LongConstant (-value, e.Location);
264 if (expr_type == TypeManager.uint32_type) {
265 UIntLiteral uil = e as UIntLiteral;
267 if (uil.Value == 2147483648)
268 return new IntLiteral (int.MinValue, e.Location);
269 return new LongLiteral (-uil.Value, e.Location);
271 return new LongConstant (-((UIntConstant)e).Value, e.Location);
274 if (expr_type == TypeManager.uint64_type) {
275 ULongLiteral ull = e as ULongLiteral;
276 if (ull != null && ull.Value == 9223372036854775808)
277 return new LongLiteral (long.MinValue, e.Location);
281 if (expr_type == TypeManager.float_type) {
282 FloatLiteral fl = e as FloatLiteral;
283 // For better error reporting
285 fl.Value = -fl.Value;
288 return new FloatConstant (-((FloatConstant)e).Value, e.Location);
290 if (expr_type == TypeManager.double_type) {
291 DoubleLiteral dl = e as DoubleLiteral;
292 // For better error reporting
294 dl.Value = -dl.Value;
298 return new DoubleConstant (-((DoubleConstant)e).Value, e.Location);
300 if (expr_type == TypeManager.decimal_type)
301 return new DecimalConstant (-((DecimalConstant)e).Value, e.Location);
305 case Operator.LogicalNot:
306 if (expr_type != TypeManager.bool_type)
309 BoolConstant b = (BoolConstant) e;
310 return new BoolConstant (!(b.Value), b.Location);
312 case Operator.OnesComplement:
313 // Unary numeric promotions
314 if (expr_type == TypeManager.byte_type)
315 return new IntConstant (~((ByteConstant)e).Value, e.Location);
316 if (expr_type == TypeManager.sbyte_type)
317 return new IntConstant (~((SByteConstant)e).Value, e.Location);
318 if (expr_type == TypeManager.short_type)
319 return new IntConstant (~((ShortConstant)e).Value, e.Location);
320 if (expr_type == TypeManager.ushort_type)
321 return new IntConstant (~((UShortConstant)e).Value, e.Location);
322 if (expr_type == TypeManager.char_type)
323 return new IntConstant (~((CharConstant)e).Value, e.Location);
325 // Predefined operators
326 if (expr_type == TypeManager.int32_type)
327 return new IntConstant (~((IntConstant)e).Value, e.Location);
328 if (expr_type == TypeManager.uint32_type)
329 return new UIntConstant (~((UIntConstant)e).Value, e.Location);
330 if (expr_type == TypeManager.int64_type)
331 return new LongConstant (~((LongConstant)e).Value, e.Location);
332 if (expr_type == TypeManager.uint64_type){
333 return new ULongConstant (~((ULongConstant)e).Value, e.Location);
335 if (e is EnumConstant) {
336 e = TryReduceConstant (ec, ((EnumConstant)e).Child);
338 e = new EnumConstant (e, expr_type);
343 case Operator.AddressOf:
346 case Operator.Indirection:
349 throw new Exception ("Can not constant fold: " + Oper.ToString());
352 Expression ResolveOperator (EmitContext ec)
355 // Step 1: Default operations on CLI native types.
358 // Attempt to use a constant folding operation.
359 Constant cexpr = Expr as Constant;
361 cexpr = TryReduceConstant (ec, cexpr);
368 // Step 2: Perform Operator Overload location
370 Type expr_type = Expr.Type;
371 string op_name = oper_names [(int) Oper];
373 Expression mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
375 Expression e = StaticCallExpr.MakeSimpleCall (
376 ec, (MethodGroupExpr) mg, Expr, loc);
387 case Operator.LogicalNot:
388 if (expr_type != TypeManager.bool_type) {
389 Expr = ResolveBoolean (ec, Expr, loc);
396 type = TypeManager.bool_type;
399 case Operator.OnesComplement:
400 // Unary numeric promotions
401 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
402 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
403 expr_type == TypeManager.char_type)
405 type = TypeManager.int32_type;
406 return EmptyCast.Create (this, type);
409 // Predefined operators
410 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
411 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
412 TypeManager.IsEnumType (expr_type))
418 type = TypeManager.int32_type;
419 Expr = Convert.ImplicitUserConversion(ec, Expr, type, loc);
426 case Operator.AddressOf:
432 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
436 IVariable variable = Expr as IVariable;
437 bool is_fixed = variable != null && variable.VerifyFixed ();
439 if (!ec.InFixedInitializer && !is_fixed) {
440 Error (212, "You can only take the address of unfixed expression inside " +
441 "of a fixed statement initializer");
445 if (ec.InFixedInitializer && is_fixed) {
446 Error (213, "You cannot use the fixed statement to take the address of an already fixed expression");
450 LocalVariableReference lr = Expr as LocalVariableReference;
452 if (lr.local_info.IsCaptured){
453 AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
456 lr.local_info.AddressTaken = true;
457 lr.local_info.Used = true;
460 ParameterReference pr = Expr as ParameterReference;
461 if ((pr != null) && pr.Parameter.IsCaptured) {
462 AnonymousMethod.Error_AddressOfCapturedVar (pr.Name, loc);
466 // According to the specs, a variable is considered definitely assigned if you take
468 if ((variable != null) && (variable.VariableInfo != null)){
469 variable.VariableInfo.SetAssigned (ec);
472 type = TypeManager.GetPointerType (Expr.Type);
475 case Operator.Indirection:
481 if (!expr_type.IsPointer){
482 Error (193, "The * or -> operator must be applied to a pointer");
487 // We create an Indirection expression, because
488 // it can implement the IMemoryLocation.
490 return new Indirection (Expr, loc);
492 case Operator.UnaryPlus:
493 // Unary numeric promotions
494 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
495 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
496 expr_type == TypeManager.char_type)
498 return EmptyCast.Create (Expr, TypeManager.int32_type);
501 // Predefined operators
502 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
503 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
504 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
505 expr_type == TypeManager.decimal_type)
510 Expr = Convert.ImplicitUserConversion(ec, Expr, TypeManager.int32_type, loc);
512 // Because we can completely ignore unary +
519 case Operator.UnaryNegation:
521 // transform - - expr into expr
523 Unary u = Expr as Unary;
524 if (u != null && u.Oper == Operator.UnaryNegation) {
528 // Unary numeric promotions
529 if (expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
530 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
531 expr_type == TypeManager.char_type)
533 type = TypeManager.int32_type;
534 return EmptyCast.Create (this, type);
538 // Predefined operators
540 if (expr_type == TypeManager.uint32_type) {
541 type = TypeManager.int64_type;
542 Expr = Convert.ImplicitNumericConversion (Expr, type);
546 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.int64_type ||
547 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
548 expr_type == TypeManager.decimal_type)
557 type = TypeManager.int32_type;
558 Expr = Convert.ImplicitUserConversion(ec, Expr, type, loc);
566 Error (187, "No such operator '" + OperName (Oper) + "' defined for type '" +
567 TypeManager.CSharpName (expr_type) + "'");
571 public override Expression DoResolve (EmitContext ec)
573 if (Oper == Operator.AddressOf) {
574 Expr = Expr.DoResolveLValue (ec, new EmptyExpression ());
576 if (Expr == null || Expr.eclass != ExprClass.Variable){
577 Error (211, "Cannot take the address of the given expression");
582 Expr = Expr.Resolve (ec);
588 if (TypeManager.IsNullableValueType (Expr.Type))
589 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
592 eclass = ExprClass.Value;
593 return ResolveOperator (ec);
596 public override Expression DoResolveLValue (EmitContext ec, Expression right)
598 if (Oper == Operator.Indirection)
599 return DoResolve (ec);
604 public override void Emit (EmitContext ec)
606 ILGenerator ig = ec.ig;
609 case Operator.UnaryPlus:
610 throw new Exception ("This should be caught by Resolve");
612 case Operator.UnaryNegation:
613 if (ec.CheckState && type != TypeManager.float_type && type != TypeManager.double_type) {
614 ig.Emit (OpCodes.Ldc_I4_0);
615 if (type == TypeManager.int64_type)
616 ig.Emit (OpCodes.Conv_U8);
618 ig.Emit (OpCodes.Sub_Ovf);
621 ig.Emit (OpCodes.Neg);
626 case Operator.LogicalNot:
628 ig.Emit (OpCodes.Ldc_I4_0);
629 ig.Emit (OpCodes.Ceq);
632 case Operator.OnesComplement:
634 ig.Emit (OpCodes.Not);
637 case Operator.AddressOf:
638 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
642 throw new Exception ("This should not happen: Operator = "
647 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
649 if (Oper == Operator.LogicalNot)
650 Expr.EmitBranchable (ec, target, !on_true);
652 base.EmitBranchable (ec, target, on_true);
655 public override string ToString ()
657 return "Unary (" + Oper + ", " + Expr + ")";
660 protected override void CloneTo (CloneContext clonectx, Expression t)
662 Unary target = (Unary) t;
664 target.Expr = Expr.Clone (clonectx);
669 // Unary operators are turned into Indirection expressions
670 // after semantic analysis (this is so we can take the address
671 // of an indirection).
673 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IVariable {
675 LocalTemporary temporary;
678 public Indirection (Expression expr, Location l)
681 type = TypeManager.HasElementType (expr.Type) ? TypeManager.GetElementType (expr.Type) : expr.Type;
682 eclass = ExprClass.Variable;
686 public override void Emit (EmitContext ec)
691 LoadFromPtr (ec.ig, Type);
694 public void Emit (EmitContext ec, bool leave_copy)
698 ec.ig.Emit (OpCodes.Dup);
699 temporary = new LocalTemporary (expr.Type);
700 temporary.Store (ec);
704 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
706 prepared = prepare_for_load;
710 if (prepare_for_load)
711 ec.ig.Emit (OpCodes.Dup);
715 ec.ig.Emit (OpCodes.Dup);
716 temporary = new LocalTemporary (expr.Type);
717 temporary.Store (ec);
720 StoreFromPtr (ec.ig, type);
722 if (temporary != null) {
724 temporary.Release (ec);
728 public void AddressOf (EmitContext ec, AddressOp Mode)
733 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
735 return DoResolve (ec);
738 public override Expression DoResolve (EmitContext ec)
741 // Born fully resolved
746 public override string ToString ()
748 return "*(" + expr + ")";
751 #region IVariable Members
753 public VariableInfo VariableInfo {
757 public bool VerifyFixed ()
759 // A pointer-indirection is always fixed.
767 /// Unary Mutator expressions (pre and post ++ and --)
771 /// UnaryMutator implements ++ and -- expressions. It derives from
772 /// ExpressionStatement becuase the pre/post increment/decrement
773 /// operators can be used in a statement context.
775 /// FIXME: Idea, we could split this up in two classes, one simpler
776 /// for the common case, and one with the extra fields for more complex
777 /// classes (indexers require temporary access; overloaded require method)
780 public class UnaryMutator : ExpressionStatement {
782 public enum Mode : byte {
789 PreDecrement = IsDecrement,
790 PostIncrement = IsPost,
791 PostDecrement = IsPost | IsDecrement
795 bool is_expr = false;
796 bool recurse = false;
801 // This is expensive for the simplest case.
803 StaticCallExpr method;
805 public UnaryMutator (Mode m, Expression e, Location l)
812 static string OperName (Mode mode)
814 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
819 /// Returns whether an object of type `t' can be incremented
820 /// or decremented with add/sub (ie, basically whether we can
821 /// use pre-post incr-decr operations on it, but it is not a
822 /// System.Decimal, which we require operator overloading to catch)
824 static bool IsIncrementableNumber (Type t)
826 return (t == TypeManager.sbyte_type) ||
827 (t == TypeManager.byte_type) ||
828 (t == TypeManager.short_type) ||
829 (t == TypeManager.ushort_type) ||
830 (t == TypeManager.int32_type) ||
831 (t == TypeManager.uint32_type) ||
832 (t == TypeManager.int64_type) ||
833 (t == TypeManager.uint64_type) ||
834 (t == TypeManager.char_type) ||
835 (t.IsSubclassOf (TypeManager.enum_type)) ||
836 (t == TypeManager.float_type) ||
837 (t == TypeManager.double_type) ||
838 (t.IsPointer && t != TypeManager.void_ptr_type);
841 Expression ResolveOperator (EmitContext ec)
843 Type expr_type = expr.Type;
846 // Step 1: Perform Operator Overload location
851 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
852 op_name = "op_Increment";
854 op_name = "op_Decrement";
856 mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
859 method = StaticCallExpr.MakeSimpleCall (
860 ec, (MethodGroupExpr) mg, expr, loc);
863 } else if (!IsIncrementableNumber (expr_type)) {
864 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
865 TypeManager.CSharpName (expr_type) + "'");
870 // The operand of the prefix/postfix increment decrement operators
871 // should be an expression that is classified as a variable,
872 // a property access or an indexer access
875 if (expr.eclass == ExprClass.Variable){
876 LocalVariableReference var = expr as LocalVariableReference;
877 if ((var != null) && var.IsReadOnly) {
878 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
881 } else if (expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess){
882 expr = expr.ResolveLValue (ec, this, Location);
886 Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
893 public override Expression DoResolve (EmitContext ec)
895 expr = expr.Resolve (ec);
900 eclass = ExprClass.Value;
903 if (TypeManager.IsNullableValueType (expr.Type))
904 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
907 return ResolveOperator (ec);
911 // Loads the proper "1" into the stack based on the type, then it emits the
912 // opcode for the operation requested
914 void LoadOneAndEmitOp (EmitContext ec, Type t)
917 // Measure if getting the typecode and using that is more/less efficient
918 // that comparing types. t.GetTypeCode() is an internal call.
920 ILGenerator ig = ec.ig;
922 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
923 LongConstant.EmitLong (ig, 1);
924 else if (t == TypeManager.double_type)
925 ig.Emit (OpCodes.Ldc_R8, 1.0);
926 else if (t == TypeManager.float_type)
927 ig.Emit (OpCodes.Ldc_R4, 1.0F);
928 else if (t.IsPointer){
929 Type et = TypeManager.GetElementType (t);
930 int n = GetTypeSize (et);
933 ig.Emit (OpCodes.Sizeof, et);
935 IntConstant.EmitInt (ig, n);
937 ig.Emit (OpCodes.Ldc_I4_1);
940 // Now emit the operation
943 if (t == TypeManager.int32_type ||
944 t == TypeManager.int64_type){
945 if ((mode & Mode.IsDecrement) != 0)
946 ig.Emit (OpCodes.Sub_Ovf);
948 ig.Emit (OpCodes.Add_Ovf);
949 } else if (t == TypeManager.uint32_type ||
950 t == TypeManager.uint64_type){
951 if ((mode & Mode.IsDecrement) != 0)
952 ig.Emit (OpCodes.Sub_Ovf_Un);
954 ig.Emit (OpCodes.Add_Ovf_Un);
956 if ((mode & Mode.IsDecrement) != 0)
957 ig.Emit (OpCodes.Sub_Ovf);
959 ig.Emit (OpCodes.Add_Ovf);
962 if ((mode & Mode.IsDecrement) != 0)
963 ig.Emit (OpCodes.Sub);
965 ig.Emit (OpCodes.Add);
968 if (t == TypeManager.sbyte_type){
970 ig.Emit (OpCodes.Conv_Ovf_I1);
972 ig.Emit (OpCodes.Conv_I1);
973 } else if (t == TypeManager.byte_type){
975 ig.Emit (OpCodes.Conv_Ovf_U1);
977 ig.Emit (OpCodes.Conv_U1);
978 } else if (t == TypeManager.short_type){
980 ig.Emit (OpCodes.Conv_Ovf_I2);
982 ig.Emit (OpCodes.Conv_I2);
983 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
985 ig.Emit (OpCodes.Conv_Ovf_U2);
987 ig.Emit (OpCodes.Conv_U2);
992 void EmitCode (EmitContext ec, bool is_expr)
995 this.is_expr = is_expr;
996 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
999 public override void Emit (EmitContext ec)
1002 // We use recurse to allow ourselfs to be the source
1003 // of an assignment. This little hack prevents us from
1004 // having to allocate another expression
1007 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1009 LoadOneAndEmitOp (ec, expr.Type);
1011 ec.ig.Emit (OpCodes.Call, method.Method);
1016 EmitCode (ec, true);
1019 public override void EmitStatement (EmitContext ec)
1021 EmitCode (ec, false);
1024 protected override void CloneTo (CloneContext clonectx, Expression t)
1026 UnaryMutator target = (UnaryMutator) t;
1028 target.expr = expr.Clone (clonectx);
1033 /// Base class for the `Is' and `As' classes.
1037 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1040 public abstract class Probe : Expression {
1041 public Expression ProbeType;
1042 protected Expression expr;
1043 protected TypeExpr probe_type_expr;
1045 public Probe (Expression expr, Expression probe_type, Location l)
1047 ProbeType = probe_type;
1052 public Expression Expr {
1058 public override Expression DoResolve (EmitContext ec)
1060 probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
1061 if (probe_type_expr == null)
1064 expr = expr.Resolve (ec);
1068 if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
1069 Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1074 if (expr.Type == TypeManager.anonymous_method_type) {
1075 Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1083 protected abstract string OperatorName { get; }
1085 protected override void CloneTo (CloneContext clonectx, Expression t)
1087 Probe target = (Probe) t;
1089 target.expr = expr.Clone (clonectx);
1090 target.ProbeType = ProbeType.Clone (clonectx);
1096 /// Implementation of the `is' operator.
1098 public class Is : Probe {
1099 public Is (Expression expr, Expression probe_type, Location l)
1100 : base (expr, probe_type, l)
1104 public override void Emit (EmitContext ec)
1106 ILGenerator ig = ec.ig;
1109 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1110 ig.Emit (OpCodes.Ldnull);
1111 ig.Emit (OpCodes.Cgt_Un);
1114 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1116 ILGenerator ig = ec.ig;
1119 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1120 ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1123 Expression CreateConstantResult (bool result)
1126 Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1127 TypeManager.CSharpName (probe_type_expr.Type));
1129 Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1130 TypeManager.CSharpName (probe_type_expr.Type));
1132 return new BoolConstant (result, loc);
1135 public override Expression DoResolve (EmitContext ec)
1137 if (base.DoResolve (ec) == null)
1141 bool d_is_nullable = false;
1143 if (expr is Constant) {
1145 // If E is a method group or the null literal, of if the type of E is a reference
1146 // type or a nullable type and the value of E is null, the result is false
1149 return CreateConstantResult (false);
1150 } else if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
1151 d = TypeManager.GetTypeArguments (d) [0];
1152 d_is_nullable = true;
1155 type = TypeManager.bool_type;
1156 eclass = ExprClass.Value;
1157 Type t = probe_type_expr.Type;
1158 bool t_is_nullable = false;
1159 if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
1160 t = TypeManager.GetTypeArguments (t) [0];
1161 t_is_nullable = true;
1164 if (t.IsValueType) {
1167 // D and T are the same value types but D can be null
1169 if (d_is_nullable && !t_is_nullable)
1170 return Nullable.HasValue.Create (expr, ec);
1173 // The result is true if D and T are the same value types
1175 return CreateConstantResult (true);
1178 if (TypeManager.IsGenericParameter (d))
1179 return ResolveGenericParameter (t, d);
1182 // An unboxing conversion exists
1184 if (Convert.ExplicitReferenceConversionExists (d, t))
1187 if (TypeManager.IsGenericParameter (t))
1188 return ResolveGenericParameter (d, t);
1190 if (d.IsValueType) {
1192 if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
1193 return CreateConstantResult (true);
1195 if (TypeManager.IsGenericParameter (d))
1196 return ResolveGenericParameter (t, d);
1198 if (TypeManager.ContainsGenericParameters (d))
1201 if (Convert.ImplicitReferenceConversionExists (expr, t) ||
1202 Convert.ExplicitReferenceConversionExists (d, t)) {
1208 return CreateConstantResult (false);
1211 Expression ResolveGenericParameter (Type d, Type t)
1214 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1215 if (constraints != null) {
1216 if (constraints.IsReferenceType && d.IsValueType)
1217 return CreateConstantResult (false);
1219 if (constraints.IsValueType && !d.IsValueType)
1220 return CreateConstantResult (false);
1223 expr = new BoxedCast (expr, d);
1230 protected override string OperatorName {
1231 get { return "is"; }
1236 /// Implementation of the `as' operator.
1238 public class As : Probe {
1239 public As (Expression expr, Expression probe_type, Location l)
1240 : base (expr, probe_type, l)
1244 bool do_isinst = false;
1245 Expression resolved_type;
1247 public override void Emit (EmitContext ec)
1249 ILGenerator ig = ec.ig;
1254 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1257 if (TypeManager.IsNullableType (type))
1258 ig.Emit (OpCodes.Unbox_Any, type);
1262 static void Error_CannotConvertType (Type source, Type target, Location loc)
1264 Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1265 TypeManager.CSharpName (source),
1266 TypeManager.CSharpName (target));
1269 public override Expression DoResolve (EmitContext ec)
1271 if (resolved_type == null) {
1272 resolved_type = base.DoResolve (ec);
1274 if (resolved_type == null)
1278 type = probe_type_expr.Type;
1279 eclass = ExprClass.Value;
1280 Type etype = expr.Type;
1282 if (type.IsValueType && !TypeManager.IsNullableType (type)) {
1283 Report.Error (77, loc, "The as operator must be used with a reference type (`" +
1284 TypeManager.CSharpName (type) + "' is a value type)");
1291 // If the type is a type parameter, ensure
1292 // that it is constrained by a class
1294 TypeParameterExpr tpe = probe_type_expr as TypeParameterExpr;
1296 GenericConstraints constraints = tpe.TypeParameter.GenericConstraints;
1299 if (constraints == null)
1302 if (!constraints.HasClassConstraint)
1303 if ((constraints.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == 0)
1307 Report.Error (413, loc,
1308 "The as operator requires that the `{0}' type parameter be constrained by a class",
1309 probe_type_expr.GetSignatureForError ());
1314 if (expr.IsNull && TypeManager.IsNullableType (type)) {
1315 Report.Warning (458, 2, loc, "The result of the expression is always `null' of type `{0}'",
1316 TypeManager.CSharpName (type));
1319 Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
1326 if (Convert.ExplicitReferenceConversionExists (etype, type)){
1327 if (TypeManager.IsGenericParameter (etype))
1328 expr = new BoxedCast (expr, etype);
1334 if (TypeManager.ContainsGenericParameters (etype) ||
1335 TypeManager.ContainsGenericParameters (type)) {
1336 expr = new BoxedCast (expr, etype);
1341 Error_CannotConvertType (etype, type, loc);
1345 protected override string OperatorName {
1346 get { return "as"; }
1349 public override bool GetAttributableValue (Type value_type, out object value)
1351 return expr.GetAttributableValue (value_type, out value);
1356 /// This represents a typecast in the source language.
1358 /// FIXME: Cast expressions have an unusual set of parsing
1359 /// rules, we need to figure those out.
1361 public class Cast : Expression {
1362 Expression target_type;
1365 public Cast (Expression cast_type, Expression expr)
1366 : this (cast_type, expr, cast_type.Location)
1370 public Cast (Expression cast_type, Expression expr, Location loc)
1372 this.target_type = cast_type;
1376 if (target_type == TypeManager.system_void_expr)
1377 Error_VoidInvalidInTheContext (loc);
1380 public Expression TargetType {
1381 get { return target_type; }
1384 public Expression Expr {
1385 get { return expr; }
1388 public override Expression DoResolve (EmitContext ec)
1390 expr = expr.Resolve (ec);
1394 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1400 if (type.IsAbstract && type.IsSealed) {
1401 Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1405 eclass = ExprClass.Value;
1407 Constant c = expr as Constant;
1409 c = c.TryReduce (ec, type, loc);
1414 if (type.IsPointer && !ec.InUnsafe) {
1418 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1422 public override void Emit (EmitContext ec)
1424 throw new Exception ("Should not happen");
1427 protected override void CloneTo (CloneContext clonectx, Expression t)
1429 Cast target = (Cast) t;
1431 target.target_type = target_type.Clone (clonectx);
1432 target.expr = expr.Clone (clonectx);
1437 // C# 2.0 Default value expression
1439 public class DefaultValueExpression : Expression
1443 public DefaultValueExpression (Expression expr, Location loc)
1449 public override Expression DoResolve (EmitContext ec)
1451 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
1457 if (type == TypeManager.void_type) {
1458 Error_VoidInvalidInTheContext (loc);
1462 if (TypeManager.IsGenericParameter (type))
1464 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints(type);
1465 if (constraints != null && constraints.IsReferenceType)
1466 return new NullDefault (new NullLiteral (Location), type);
1470 Constant c = New.Constantify(type);
1472 return new NullDefault (c, type);
1474 if (!TypeManager.IsValueType (type))
1475 return new NullDefault (new NullLiteral (Location), type);
1477 eclass = ExprClass.Variable;
1481 public override void Emit (EmitContext ec)
1483 LocalTemporary temp_storage = new LocalTemporary(type);
1485 temp_storage.AddressOf(ec, AddressOp.LoadStore);
1486 ec.ig.Emit(OpCodes.Initobj, type);
1487 temp_storage.Emit(ec);
1490 protected override void CloneTo (CloneContext clonectx, Expression t)
1492 DefaultValueExpression target = (DefaultValueExpression) t;
1494 target.expr = expr.Clone (clonectx);
1499 /// Binary operators
1501 public class Binary : Expression {
1502 public enum Operator : byte {
1503 Multiply, Division, Modulus,
1504 Addition, Subtraction,
1505 LeftShift, RightShift,
1506 LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual,
1507 Equality, Inequality,
1516 readonly Operator oper;
1517 protected Expression left, right;
1518 readonly bool is_compound;
1520 // This must be kept in sync with Operator!!!
1521 public static readonly string [] oper_names;
1525 oper_names = new string [(int) Operator.TOP];
1527 oper_names [(int) Operator.Multiply] = "op_Multiply";
1528 oper_names [(int) Operator.Division] = "op_Division";
1529 oper_names [(int) Operator.Modulus] = "op_Modulus";
1530 oper_names [(int) Operator.Addition] = "op_Addition";
1531 oper_names [(int) Operator.Subtraction] = "op_Subtraction";
1532 oper_names [(int) Operator.LeftShift] = "op_LeftShift";
1533 oper_names [(int) Operator.RightShift] = "op_RightShift";
1534 oper_names [(int) Operator.LessThan] = "op_LessThan";
1535 oper_names [(int) Operator.GreaterThan] = "op_GreaterThan";
1536 oper_names [(int) Operator.LessThanOrEqual] = "op_LessThanOrEqual";
1537 oper_names [(int) Operator.GreaterThanOrEqual] = "op_GreaterThanOrEqual";
1538 oper_names [(int) Operator.Equality] = "op_Equality";
1539 oper_names [(int) Operator.Inequality] = "op_Inequality";
1540 oper_names [(int) Operator.BitwiseAnd] = "op_BitwiseAnd";
1541 oper_names [(int) Operator.BitwiseOr] = "op_BitwiseOr";
1542 oper_names [(int) Operator.ExclusiveOr] = "op_ExclusiveOr";
1543 oper_names [(int) Operator.LogicalOr] = "op_LogicalOr";
1544 oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
1547 public Binary (Operator oper, Expression left, Expression right, bool isCompound)
1548 : this (oper, left, right)
1550 this.is_compound = isCompound;
1553 public Binary (Operator oper, Expression left, Expression right)
1558 this.loc = left.Location;
1561 public Operator Oper {
1568 /// Returns a stringified representation of the Operator
1570 string OperName (Operator oper)
1574 case Operator.Multiply:
1577 case Operator.Division:
1580 case Operator.Modulus:
1583 case Operator.Addition:
1586 case Operator.Subtraction:
1589 case Operator.LeftShift:
1592 case Operator.RightShift:
1595 case Operator.LessThan:
1598 case Operator.GreaterThan:
1601 case Operator.LessThanOrEqual:
1604 case Operator.GreaterThanOrEqual:
1607 case Operator.Equality:
1610 case Operator.Inequality:
1613 case Operator.BitwiseAnd:
1616 case Operator.BitwiseOr:
1619 case Operator.ExclusiveOr:
1622 case Operator.LogicalOr:
1625 case Operator.LogicalAnd:
1629 s = oper.ToString ();
1639 public override string ToString ()
1641 return "operator " + OperName (oper) + "(" + left.ToString () + ", " +
1642 right.ToString () + ")";
1645 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
1647 if (expr.Type == target_type)
1650 return Convert.ImplicitConversion (ec, expr, target_type, loc);
1653 void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
1656 34, loc, "Operator `" + OperName (oper)
1657 + "' is ambiguous on operands of type `"
1658 + TypeManager.CSharpName (l) + "' "
1659 + "and `" + TypeManager.CSharpName (r)
1663 bool IsConvertible (EmitContext ec, Expression le, Expression re, Type t)
1665 return Convert.ImplicitConversionExists (ec, le, t) && Convert.ImplicitConversionExists (ec, re, t);
1668 bool VerifyApplicable_Predefined (EmitContext ec, Type t)
1670 if (!IsConvertible (ec, left, right, t))
1672 left = ForceConversion (ec, left, t);
1673 right = ForceConversion (ec, right, t);
1678 bool IsApplicable_String (EmitContext ec, Expression le, Expression re, Operator oper)
1680 bool l = Convert.ImplicitConversionExists (ec, le, TypeManager.string_type);
1681 bool r = Convert.ImplicitConversionExists (ec, re, TypeManager.string_type);
1683 if (oper == Operator.Equality || oper == Operator.Inequality)
1685 if (oper == Operator.Addition)
1690 bool OverloadResolve_PredefinedString (EmitContext ec, Operator oper)
1692 if (!IsApplicable_String (ec, left, right, oper))
1696 Type r = right.Type;
1697 if (OverloadResolve_PredefinedIntegral (ec) ||
1698 OverloadResolve_PredefinedFloating (ec)) {
1699 Error_OperatorAmbiguous (loc, oper, l, r);
1702 Type t = TypeManager.string_type;
1703 if (Convert.ImplicitConversionExists (ec, left, t))
1704 left = ForceConversion (ec, left, t);
1705 if (Convert.ImplicitConversionExists (ec, right, t))
1706 right = ForceConversion (ec, right, t);
1711 bool OverloadResolve_PredefinedIntegral (EmitContext ec)
1713 return VerifyApplicable_Predefined (ec, TypeManager.int32_type) ||
1714 VerifyApplicable_Predefined (ec, TypeManager.uint32_type) ||
1715 VerifyApplicable_Predefined (ec, TypeManager.int64_type) ||
1716 VerifyApplicable_Predefined (ec, TypeManager.uint64_type) ||
1720 bool OverloadResolve_PredefinedFloating (EmitContext ec)
1722 return VerifyApplicable_Predefined (ec, TypeManager.float_type) ||
1723 VerifyApplicable_Predefined (ec, TypeManager.double_type) ||
1727 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
1729 Error_OperatorCannotBeApplied (loc, name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
1732 public static void Error_OperatorCannotBeApplied (Location loc, string name, string left, string right)
1734 Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
1738 protected void Error_OperatorCannotBeApplied ()
1740 Error_OperatorCannotBeApplied (Location, OperName (oper), TypeManager.CSharpName (left.Type),
1741 TypeManager.CSharpName(right.Type));
1744 static bool IsUnsigned (Type t)
1747 t = t.GetElementType ();
1749 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
1750 t == TypeManager.ushort_type || t == TypeManager.byte_type);
1753 Expression Make32or64 (EmitContext ec, Expression e)
1757 if (t == TypeManager.int32_type || t == TypeManager.uint32_type ||
1758 t == TypeManager.int64_type || t == TypeManager.uint64_type)
1760 Expression ee = Convert.ImplicitConversion (ec, e, TypeManager.int32_type, loc);
1763 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint32_type, loc);
1766 ee = Convert.ImplicitConversion (ec, e, TypeManager.int64_type, loc);
1769 ee = Convert.ImplicitConversion (ec, e, TypeManager.uint64_type, loc);
1775 Expression CheckShiftArguments (EmitContext ec)
1777 Expression new_left = Make32or64 (ec, left);
1778 Expression new_right = ForceConversion (ec, right, TypeManager.int32_type);
1779 if (new_left == null || new_right == null) {
1780 Error_OperatorCannotBeApplied ();
1783 type = new_left.Type;
1784 int shiftmask = (type == TypeManager.int32_type || type == TypeManager.uint32_type) ? 31 : 63;
1786 right = new Binary (Binary.Operator.BitwiseAnd, new_right, new IntConstant (shiftmask, loc)).DoResolve (ec);
1791 // This is used to check if a test 'x == null' can be optimized to a reference equals,
1792 // i.e., not invoke op_Equality.
1794 static bool EqualsNullIsReferenceEquals (Type t)
1796 return t == TypeManager.object_type || t == TypeManager.string_type ||
1797 t == TypeManager.delegate_type || t.IsSubclassOf (TypeManager.delegate_type);
1800 static void Warning_UnintendedReferenceComparison (Location loc, string side, Type type)
1802 Report.Warning ((side == "left" ? 252 : 253), 2, loc,
1803 "Possible unintended reference comparison; to get a value comparison, " +
1804 "cast the {0} hand side to type `{1}'.", side, TypeManager.CSharpName (type));
1807 static void Warning_Constant_Result (Location loc, bool result, Type type)
1809 Report.Warning (472, 2, loc, "The result of comparing `{0}' against null is always `{1}'. " +
1810 "This operation is undocumented and it is temporary supported for compatibility reasons only",
1811 TypeManager.CSharpName (type), result ? "true" : "false");
1814 Expression ResolveOperator (EmitContext ec)
1817 Type r = right.Type;
1819 if (oper == Operator.Equality || oper == Operator.Inequality){
1820 if (right.Type == TypeManager.null_type){
1821 if (TypeManager.IsGenericParameter (l)){
1822 if (l.BaseType == TypeManager.value_type) {
1823 Error_OperatorCannotBeApplied ();
1827 left = new BoxedCast (left, TypeManager.object_type);
1828 Type = TypeManager.bool_type;
1833 // 7.9.9 Equality operators and null
1835 // CSC 2 has this behavior, it allows structs to be compared
1836 // with the null literal *outside* of a generics context and
1837 // inlines that as true or false.
1839 // This is, in my opinion, completely wrong.
1841 if (RootContext.Version != LanguageVersion.ISO_1 && l.IsValueType) {
1842 if (!TypeManager.IsPrimitiveType (l) && !TypeManager.IsEnumType (l)) {
1843 if (MemberLookup (ec.ContainerType, l, oper_names [(int)Operator.Equality], MemberTypes.Method, AllBindingFlags, loc) == null &&
1844 MemberLookup (ec.ContainerType, l, oper_names [(int)Operator.Inequality], MemberTypes.Method, AllBindingFlags, loc) == null) {
1845 Error_OperatorCannotBeApplied ();
1850 Warning_Constant_Result (loc, oper == Operator.Inequality, l);
1851 return new BoolConstant (oper == Operator.Inequality, loc);
1855 if (left is NullLiteral){
1856 if (TypeManager.IsGenericParameter (r)){
1857 if (r.BaseType == TypeManager.value_type) {
1858 Error_OperatorCannotBeApplied ();
1862 right = new BoxedCast (right, TypeManager.object_type);
1863 Type = TypeManager.bool_type;
1868 // 7.9.9 Equality operators and null
1870 // CSC 2 has this behavior, it allows structs to be compared
1871 // with the null literal *outside* of a generics context and
1872 // inlines that as true or false.
1874 // This is, in my opinion, completely wrong.
1876 if (RootContext.Version != LanguageVersion.ISO_1 && r.IsValueType){
1877 if (!TypeManager.IsPrimitiveType (r) && !TypeManager.IsEnumType (r)) {
1878 if (MemberLookup (ec.ContainerType, r, oper_names [(int) Operator.Equality], MemberTypes.Method, AllBindingFlags, loc) == null &&
1879 MemberLookup (ec.ContainerType, r, oper_names [(int) Operator.Inequality], MemberTypes.Method, AllBindingFlags, loc) == null) {
1880 Error_OperatorCannotBeApplied ();
1885 Warning_Constant_Result (loc, oper == Operator.Inequality, r);
1886 return new BoolConstant (oper == Operator.Inequality, loc);
1891 // Optimize out call to op_Equality in a few cases.
1893 if ((l == TypeManager.null_type && EqualsNullIsReferenceEquals (r)) ||
1894 (r == TypeManager.null_type && EqualsNullIsReferenceEquals (l))) {
1895 Type = TypeManager.bool_type;
1900 if (l == TypeManager.intptr_type && r == TypeManager.intptr_type) {
1901 Type = TypeManager.bool_type;
1907 // Delegate equality
1909 MethodGroupExpr mg = null;
1910 Type delegate_type = null;
1911 if (left.eclass == ExprClass.MethodGroup) {
1912 if (!TypeManager.IsDelegateType(r)) {
1913 Error_OperatorCannotBeApplied(Location, OperName(oper),
1914 left.ExprClassName, right.ExprClassName);
1917 mg = (MethodGroupExpr)left;
1919 } else if (right.eclass == ExprClass.MethodGroup) {
1920 if (!TypeManager.IsDelegateType(l)) {
1921 Error_OperatorCannotBeApplied(Location, OperName(oper),
1922 left.ExprClassName, right.ExprClassName);
1925 mg = (MethodGroupExpr)right;
1930 Expression e = ImplicitDelegateCreation.Create (ec, mg, delegate_type, loc);
1934 // Find operator method
1935 string op = oper_names[(int)oper];
1936 MemberInfo[] mi = TypeManager.MemberLookup(ec.ContainerType, null,
1937 TypeManager.delegate_type, MemberTypes.Method, AllBindingFlags, op, null);
1939 ArrayList args = new ArrayList(2);
1940 args.Add(new Argument(e, Argument.AType.Expression));
1941 if (delegate_type == l)
1942 args.Insert(0, new Argument(left, Argument.AType.Expression));
1944 args.Add(new Argument(right, Argument.AType.Expression));
1946 return new BinaryMethod (TypeManager.bool_type, (MethodInfo)mi [0], args);
1949 if (l == TypeManager.anonymous_method_type || r == TypeManager.anonymous_method_type) {
1950 Error_OperatorCannotBeApplied(Location, OperName(oper),
1951 left.ExprClassName, right.ExprClassName);
1958 // Do not perform operator overload resolution when both sides are
1961 MethodGroupExpr left_operators = null, right_operators = null;
1962 if (!(TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r))) {
1964 // Step 1: Perform Operator Overload location
1966 string op = oper_names [(int) oper];
1968 MethodGroupExpr union;
1969 left_operators = MemberLookup (ec.ContainerType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
1971 right_operators = MemberLookup (
1972 ec.ContainerType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
1973 union = MethodGroupExpr.MakeUnionSet (left_operators, right_operators, loc);
1975 union = left_operators;
1977 if (union != null) {
1978 ArrayList args = new ArrayList (2);
1979 args.Add (new Argument (left, Argument.AType.Expression));
1980 args.Add (new Argument (right, Argument.AType.Expression));
1982 union = union.OverloadResolve (ec, ref args, true, Location.Null);
1984 if (union != null) {
1985 MethodInfo mi = (MethodInfo) union;
1986 return new BinaryMethod (mi.ReturnType, mi, args);
1992 // String concatenation
1994 // string operator + (string x, string y);
1995 // string operator + (string x, object y);
1996 // string operator + (object x, string y);
1998 if (oper == Operator.Addition && !TypeManager.IsDelegateType (l)) {
2000 // Either left or right expression is implicitly convertible to string
2002 if (OverloadResolve_PredefinedString (ec, oper)) {
2003 if (r == TypeManager.void_type || l == TypeManager.void_type) {
2004 Error_OperatorCannotBeApplied ();
2009 // Constants folding for strings and nulls
2011 if (left.Type == TypeManager.string_type && right.Type == TypeManager.string_type &&
2012 left is Constant && right is Constant) {
2013 string lvalue = (string)((Constant) left).GetValue ();
2014 string rvalue = (string)((Constant) right).GetValue ();
2015 return new StringConstant (lvalue + rvalue, left.Location);
2019 // Append to existing string concatenation
2021 if (left is StringConcat) {
2022 ((StringConcat) left).Append (ec, right);
2027 // Otherwise, start a new concat expression using converted expression
2029 return new StringConcat (ec, loc, left, right).Resolve (ec);
2033 // Transform a + ( - b) into a - b
2035 if (right is Unary){
2036 Unary right_unary = (Unary) right;
2038 if (right_unary.Oper == Unary.Operator.UnaryNegation){
2039 return new Binary (Operator.Subtraction, left, right_unary.Expr).Resolve (ec);
2044 if (oper == Operator.Equality || oper == Operator.Inequality){
2045 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
2046 if (r != TypeManager.bool_type || l != TypeManager.bool_type){
2047 Error_OperatorCannotBeApplied ();
2051 type = TypeManager.bool_type;
2055 if (l.IsPointer || r.IsPointer) {
2056 if (l.IsPointer && r.IsPointer) {
2057 type = TypeManager.bool_type;
2061 if (l.IsPointer && r == TypeManager.null_type) {
2062 right = new EmptyConstantCast (NullPointer.Null, l);
2063 type = TypeManager.bool_type;
2067 if (r.IsPointer && l == TypeManager.null_type) {
2068 left = new EmptyConstantCast (NullPointer.Null, r);
2069 type = TypeManager.bool_type;
2075 if (l.IsGenericParameter && r.IsGenericParameter) {
2076 GenericConstraints l_gc, r_gc;
2078 l_gc = TypeManager.GetTypeParameterConstraints (l);
2079 r_gc = TypeManager.GetTypeParameterConstraints (r);
2081 if ((l_gc == null) || (r_gc == null) ||
2082 !(l_gc.HasReferenceTypeConstraint || l_gc.HasClassConstraint) ||
2083 !(r_gc.HasReferenceTypeConstraint || r_gc.HasClassConstraint)) {
2084 Error_OperatorCannotBeApplied ();
2092 // operator != (object a, object b)
2093 // operator == (object a, object b)
2095 // For this to be used, both arguments have to be reference-types.
2096 // Read the rationale on the spec (14.9.6)
2098 if (!(l.IsValueType || r.IsValueType)){
2099 type = TypeManager.bool_type;
2105 // Also, a standard conversion must exist from either one
2107 // NOTE: An interface is converted to the object before the
2108 // standard conversion is applied. It's not clear from the
2109 // standard but it looks like it works like that.
2112 l = TypeManager.object_type;
2114 r = TypeManager.object_type;
2116 bool left_to_right =
2117 Convert.ImplicitStandardConversionExists (left, r);
2118 bool right_to_left = !left_to_right &&
2119 Convert.ImplicitStandardConversionExists (right, l);
2121 if (!left_to_right && !right_to_left) {
2122 Error_OperatorCannotBeApplied ();
2126 if (left_to_right && left_operators != null &&
2127 Report.WarningLevel >= 2) {
2128 ArrayList args = new ArrayList (2);
2129 args.Add (new Argument (left, Argument.AType.Expression));
2130 args.Add (new Argument (left, Argument.AType.Expression));
2131 if (left_operators.OverloadResolve (ec, ref args, true, Location.Null) != null)
2132 Warning_UnintendedReferenceComparison (loc, "right", l);
2135 if (right_to_left && right_operators != null &&
2136 Report.WarningLevel >= 2) {
2137 ArrayList args = new ArrayList (2);
2138 args.Add (new Argument (right, Argument.AType.Expression));
2139 args.Add (new Argument (right, Argument.AType.Expression));
2140 if (right_operators.OverloadResolve (ec, ref args, true, Location.Null) != null)
2141 Warning_UnintendedReferenceComparison (loc, "left", r);
2145 // We are going to have to convert to an object to compare
2147 if (l != TypeManager.object_type)
2148 left = EmptyCast.Create (left, TypeManager.object_type);
2149 if (r != TypeManager.object_type)
2150 right = EmptyCast.Create (right, TypeManager.object_type);
2156 // Only perform numeric promotions on:
2157 // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
2159 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2160 if (TypeManager.IsDelegateType (l)){
2161 if (((right.eclass == ExprClass.MethodGroup) ||
2162 (r == TypeManager.anonymous_method_type))){
2163 if ((RootContext.Version != LanguageVersion.ISO_1)){
2164 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2172 if (TypeManager.IsDelegateType (r) || right is NullLiteral){
2174 ArrayList args = new ArrayList (2);
2176 args = new ArrayList (2);
2177 args.Add (new Argument (left, Argument.AType.Expression));
2178 args.Add (new Argument (right, Argument.AType.Expression));
2180 if (oper == Operator.Addition)
2181 method = TypeManager.delegate_combine_delegate_delegate;
2183 method = TypeManager.delegate_remove_delegate_delegate;
2185 if (!TypeManager.IsEqual (l, r) && !(right is NullLiteral)) {
2186 Error_OperatorCannotBeApplied ();
2190 return new BinaryDelegate (l, method, args);
2195 // Pointer arithmetic:
2197 // T* operator + (T* x, int y);
2198 // T* operator + (T* x, uint y);
2199 // T* operator + (T* x, long y);
2200 // T* operator + (T* x, ulong y);
2202 // T* operator + (int y, T* x);
2203 // T* operator + (uint y, T *x);
2204 // T* operator + (long y, T *x);
2205 // T* operator + (ulong y, T *x);
2207 // T* operator - (T* x, int y);
2208 // T* operator - (T* x, uint y);
2209 // T* operator - (T* x, long y);
2210 // T* operator - (T* x, ulong y);
2212 // long operator - (T* x, T *y)
2215 if (r.IsPointer && oper == Operator.Subtraction){
2217 return new PointerArithmetic (
2218 false, left, right, TypeManager.int64_type,
2221 Expression t = Make32or64 (ec, right);
2223 return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
2225 } else if (r.IsPointer && oper == Operator.Addition){
2226 Expression t = Make32or64 (ec, left);
2228 return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
2233 // Enumeration operators
2235 bool lie = TypeManager.IsEnumType (l);
2236 bool rie = TypeManager.IsEnumType (r);
2240 // U operator - (E e, E f)
2242 if (oper == Operator.Subtraction){
2244 type = TypeManager.EnumToUnderlying (l);
2247 Error_OperatorCannotBeApplied ();
2253 // operator + (E e, U x)
2254 // operator - (E e, U x)
2256 if (oper == Operator.Addition || oper == Operator.Subtraction){
2257 Type enum_type = lie ? l : r;
2258 Type other_type = lie ? r : l;
2259 Type underlying_type = TypeManager.EnumToUnderlying (enum_type);
2261 if (underlying_type != other_type){
2262 temp = Convert.ImplicitConversion (ec, lie ? right : left, underlying_type, loc);
2272 Error_OperatorCannotBeApplied ();
2281 temp = Convert.ImplicitConversion (ec, right, l, loc);
2285 Error_OperatorCannotBeApplied ();
2289 temp = Convert.ImplicitConversion (ec, left, r, loc);
2294 Error_OperatorCannotBeApplied ();
2299 if (oper == Operator.Equality || oper == Operator.Inequality ||
2300 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2301 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2302 if (left.Type != right.Type){
2303 Error_OperatorCannotBeApplied ();
2306 type = TypeManager.bool_type;
2310 if (oper == Operator.BitwiseAnd ||
2311 oper == Operator.BitwiseOr ||
2312 oper == Operator.ExclusiveOr){
2313 if (left.Type != right.Type){
2314 Error_OperatorCannotBeApplied ();
2320 Error_OperatorCannotBeApplied ();
2324 if (oper == Operator.LeftShift || oper == Operator.RightShift)
2325 return CheckShiftArguments (ec);
2327 if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
2328 if (l == TypeManager.bool_type && r == TypeManager.bool_type) {
2329 type = TypeManager.bool_type;
2333 Expression left_operators_e = l == TypeManager.bool_type ?
2334 left : Convert.ImplicitUserConversion (ec, left, TypeManager.bool_type, loc);
2335 Expression right_operators_e = r == TypeManager.bool_type ?
2336 right : Convert.ImplicitUserConversion (ec, right, TypeManager.bool_type, loc);
2338 if (left_operators_e != null && right_operators_e != null) {
2339 left = left_operators_e;
2340 right = right_operators_e;
2341 type = TypeManager.bool_type;
2345 Expression e = new ConditionalLogicalOperator (
2346 oper == Operator.LogicalAnd, left, right, l, loc);
2347 return e.Resolve (ec);
2350 Expression orig_left = left;
2351 Expression orig_right = right;
2354 // operator & (bool x, bool y)
2355 // operator | (bool x, bool y)
2356 // operator ^ (bool x, bool y)
2358 if (oper == Operator.BitwiseAnd ||
2359 oper == Operator.BitwiseOr ||
2360 oper == Operator.ExclusiveOr) {
2361 if (OverloadResolve_PredefinedIntegral (ec)) {
2362 if (IsConvertible (ec, orig_left, orig_right, TypeManager.bool_type)) {
2363 Error_OperatorAmbiguous (loc, oper, l, r);
2367 if (oper == Operator.BitwiseOr && l != r && !(orig_right is Constant) && right is OpcodeCast &&
2368 (r == TypeManager.sbyte_type || r == TypeManager.short_type ||
2369 r == TypeManager.int32_type || r == TypeManager.int64_type)) {
2370 Report.Warning (675, 3, loc, "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2371 TypeManager.CSharpName (r));
2374 } else if (!VerifyApplicable_Predefined (ec, TypeManager.bool_type)) {
2375 Error_OperatorCannotBeApplied ();
2382 // Pointer comparison
2384 if (l.IsPointer && r.IsPointer){
2385 if (oper == Operator.LessThan || oper == Operator.LessThanOrEqual ||
2386 oper == Operator.GreaterThan || oper == Operator.GreaterThanOrEqual){
2387 type = TypeManager.bool_type;
2392 if (OverloadResolve_PredefinedIntegral (ec)) {
2393 if (IsApplicable_String (ec, orig_left, orig_right, oper)) {
2394 Error_OperatorAmbiguous (loc, oper, l, r);
2397 } else if (OverloadResolve_PredefinedFloating (ec)) {
2398 if (IsConvertible (ec, orig_left, orig_right, TypeManager.decimal_type) ||
2399 IsApplicable_String (ec, orig_left, orig_right, oper)) {
2400 Error_OperatorAmbiguous (loc, oper, l, r);
2403 } else if (VerifyApplicable_Predefined (ec, TypeManager.decimal_type)) {
2404 if (IsApplicable_String (ec, orig_left, orig_right, oper)) {
2405 Error_OperatorAmbiguous (loc, oper, l, r);
2408 } else if (!OverloadResolve_PredefinedString (ec, oper)) {
2409 Error_OperatorCannotBeApplied ();
2413 if (oper == Operator.Equality ||
2414 oper == Operator.Inequality ||
2415 oper == Operator.LessThanOrEqual ||
2416 oper == Operator.LessThan ||
2417 oper == Operator.GreaterThanOrEqual ||
2418 oper == Operator.GreaterThan)
2419 type = TypeManager.bool_type;
2424 if (l == TypeManager.decimal_type || l == TypeManager.string_type || r == TypeManager.string_type) {
2426 if (r == TypeManager.string_type)
2428 MethodGroupExpr ops = (MethodGroupExpr) MemberLookup (
2429 ec.ContainerType, lookup, oper_names [(int) oper],
2430 MemberTypes.Method, AllBindingFlags, loc);
2431 ArrayList args = new ArrayList (2);
2432 args.Add (new Argument (left, Argument.AType.Expression));
2433 args.Add (new Argument (right, Argument.AType.Expression));
2434 ops = ops.OverloadResolve (ec, ref args, true, Location.Null);
2435 return new BinaryMethod (type, (MethodInfo)ops, args);
2441 Constant EnumLiftUp (Constant left, Constant right)
2444 case Operator.BitwiseOr:
2445 case Operator.BitwiseAnd:
2446 case Operator.ExclusiveOr:
2447 case Operator.Equality:
2448 case Operator.Inequality:
2449 case Operator.LessThan:
2450 case Operator.LessThanOrEqual:
2451 case Operator.GreaterThan:
2452 case Operator.GreaterThanOrEqual:
2453 if (left is EnumConstant)
2456 if (left.IsZeroInteger)
2457 return new EnumConstant (left, right.Type);
2461 case Operator.Addition:
2462 case Operator.Subtraction:
2465 case Operator.Multiply:
2466 case Operator.Division:
2467 case Operator.Modulus:
2468 case Operator.LeftShift:
2469 case Operator.RightShift:
2470 if (right is EnumConstant || left is EnumConstant)
2474 Error_OperatorCannotBeApplied ();
2478 public override Expression DoResolve (EmitContext ec)
2483 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2484 left = ((ParenthesizedExpression) left).Expr;
2485 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2489 if (left.eclass == ExprClass.Type) {
2490 Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
2494 left = left.Resolve (ec);
2499 Constant lc = left as Constant;
2500 if (lc != null && lc.Type == TypeManager.bool_type &&
2501 ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
2502 (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
2504 // TODO: make a sense to resolve unreachable expression as we do for statement
2505 Report.Warning (429, 4, loc, "Unreachable expression code detected");
2509 right = right.Resolve (ec);
2513 eclass = ExprClass.Value;
2514 Constant rc = right as Constant;
2516 // The conversion rules are ignored in enum context but why
2517 if (!ec.InEnumContext && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
2518 left = lc = EnumLiftUp (lc, rc);
2522 right = rc = EnumLiftUp (rc, lc);
2527 if (oper == Operator.BitwiseAnd) {
2528 if (rc != null && rc.IsZeroInteger) {
2529 return lc is EnumConstant ?
2530 new EnumConstant (rc, lc.Type):
2534 if (lc != null && lc.IsZeroInteger) {
2535 return rc is EnumConstant ?
2536 new EnumConstant (lc, rc.Type):
2540 else if (oper == Operator.BitwiseOr) {
2541 if (lc is EnumConstant &&
2542 rc != null && rc.IsZeroInteger)
2544 if (rc is EnumConstant &&
2545 lc != null && lc.IsZeroInteger)
2547 } else if (oper == Operator.LogicalAnd) {
2548 if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
2550 if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
2554 if (rc != null && lc != null){
2555 int prev_e = Report.Errors;
2556 Expression e = ConstantFold.BinaryFold (
2557 ec, oper, lc, rc, loc);
2558 if (e != null || Report.Errors != prev_e)
2563 if ((left is NullLiteral || left.Type.IsValueType) &&
2564 (right is NullLiteral || right.Type.IsValueType) &&
2565 !(left is NullLiteral && right is NullLiteral) &&
2566 (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type)))
2567 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2570 // Comparison warnings
2571 if (oper == Operator.Equality || oper == Operator.Inequality ||
2572 oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
2573 oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
2574 if (left.Equals (right)) {
2575 Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
2577 CheckUselessComparison (lc, right.Type);
2578 CheckUselessComparison (rc, left.Type);
2581 return ResolveOperator (ec);
2584 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2589 private void CheckUselessComparison (Constant c, Type type)
2591 if (c == null || !IsTypeIntegral (type)
2592 || c is StringConstant
2593 || c is BoolConstant
2594 || c is FloatConstant
2595 || c is DoubleConstant
2596 || c is DecimalConstant
2602 if (c is ULongConstant) {
2603 ulong uvalue = ((ULongConstant) c).Value;
2604 if (uvalue > long.MaxValue) {
2605 if (type == TypeManager.byte_type ||
2606 type == TypeManager.sbyte_type ||
2607 type == TypeManager.short_type ||
2608 type == TypeManager.ushort_type ||
2609 type == TypeManager.int32_type ||
2610 type == TypeManager.uint32_type ||
2611 type == TypeManager.int64_type ||
2612 type == TypeManager.char_type)
2613 WarnUselessComparison (type);
2616 value = (long) uvalue;
2618 else if (c is ByteConstant)
2619 value = ((ByteConstant) c).Value;
2620 else if (c is SByteConstant)
2621 value = ((SByteConstant) c).Value;
2622 else if (c is ShortConstant)
2623 value = ((ShortConstant) c).Value;
2624 else if (c is UShortConstant)
2625 value = ((UShortConstant) c).Value;
2626 else if (c is IntConstant)
2627 value = ((IntConstant) c).Value;
2628 else if (c is UIntConstant)
2629 value = ((UIntConstant) c).Value;
2630 else if (c is LongConstant)
2631 value = ((LongConstant) c).Value;
2632 else if (c is CharConstant)
2633 value = ((CharConstant)c).Value;
2638 if (IsValueOutOfRange (value, type))
2639 WarnUselessComparison (type);
2642 private bool IsValueOutOfRange (long value, Type type)
2644 if (IsTypeUnsigned (type) && value < 0)
2646 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
2647 type == TypeManager.byte_type && value >= 0x100 ||
2648 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
2649 type == TypeManager.ushort_type && value >= 0x10000 ||
2650 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
2651 type == TypeManager.uint32_type && value >= 0x100000000;
2654 private static bool IsTypeIntegral (Type type)
2656 return type == TypeManager.uint64_type ||
2657 type == TypeManager.int64_type ||
2658 type == TypeManager.uint32_type ||
2659 type == TypeManager.int32_type ||
2660 type == TypeManager.ushort_type ||
2661 type == TypeManager.short_type ||
2662 type == TypeManager.sbyte_type ||
2663 type == TypeManager.byte_type ||
2664 type == TypeManager.char_type;
2667 private static bool IsTypeUnsigned (Type type)
2669 return type == TypeManager.uint64_type ||
2670 type == TypeManager.uint32_type ||
2671 type == TypeManager.ushort_type ||
2672 type == TypeManager.byte_type ||
2673 type == TypeManager.char_type;
2676 private void WarnUselessComparison (Type type)
2678 Report.Warning (652, 2, loc, "A comparison between a constant and a variable is useless. The constant is out of the range of the variable type `{0}'",
2679 TypeManager.CSharpName (type));
2683 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2684 /// context of a conditional bool expression. This function will return
2685 /// false if it is was possible to use EmitBranchable, or true if it was.
2687 /// The expression's code is generated, and we will generate a branch to `target'
2688 /// if the resulting expression value is equal to isTrue
2690 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2692 ILGenerator ig = ec.ig;
2695 // This is more complicated than it looks, but its just to avoid
2696 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
2697 // but on top of that we want for == and != to use a special path
2698 // if we are comparing against null
2700 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
2701 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
2704 // put the constant on the rhs, for simplicity
2706 if (left is Constant) {
2707 Expression swap = right;
2712 if (((Constant) right).IsZeroInteger) {
2715 ig.Emit (OpCodes.Brtrue, target);
2717 ig.Emit (OpCodes.Brfalse, target);
2720 } else if (right is BoolConstant) {
2722 if (my_on_true != ((BoolConstant) right).Value)
2723 ig.Emit (OpCodes.Brtrue, target);
2725 ig.Emit (OpCodes.Brfalse, target);
2730 } else if (oper == Operator.LogicalAnd) {
2733 Label tests_end = ig.DefineLabel ();
2735 left.EmitBranchable (ec, tests_end, false);
2736 right.EmitBranchable (ec, target, true);
2737 ig.MarkLabel (tests_end);
2740 // This optimizes code like this
2741 // if (true && i > 4)
2743 if (!(left is Constant))
2744 left.EmitBranchable (ec, target, false);
2746 if (!(right is Constant))
2747 right.EmitBranchable (ec, target, false);
2752 } else if (oper == Operator.LogicalOr){
2754 left.EmitBranchable (ec, target, true);
2755 right.EmitBranchable (ec, target, true);
2758 Label tests_end = ig.DefineLabel ();
2759 left.EmitBranchable (ec, tests_end, true);
2760 right.EmitBranchable (ec, target, false);
2761 ig.MarkLabel (tests_end);
2766 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
2767 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
2768 oper == Operator.Equality || oper == Operator.Inequality)) {
2769 base.EmitBranchable (ec, target, on_true);
2777 bool is_unsigned = IsUnsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
2780 case Operator.Equality:
2782 ig.Emit (OpCodes.Beq, target);
2784 ig.Emit (OpCodes.Bne_Un, target);
2787 case Operator.Inequality:
2789 ig.Emit (OpCodes.Bne_Un, target);
2791 ig.Emit (OpCodes.Beq, target);
2794 case Operator.LessThan:
2797 ig.Emit (OpCodes.Blt_Un, target);
2799 ig.Emit (OpCodes.Blt, target);
2802 ig.Emit (OpCodes.Bge_Un, target);
2804 ig.Emit (OpCodes.Bge, target);
2807 case Operator.GreaterThan:
2810 ig.Emit (OpCodes.Bgt_Un, target);
2812 ig.Emit (OpCodes.Bgt, target);
2815 ig.Emit (OpCodes.Ble_Un, target);
2817 ig.Emit (OpCodes.Ble, target);
2820 case Operator.LessThanOrEqual:
2823 ig.Emit (OpCodes.Ble_Un, target);
2825 ig.Emit (OpCodes.Ble, target);
2828 ig.Emit (OpCodes.Bgt_Un, target);
2830 ig.Emit (OpCodes.Bgt, target);
2834 case Operator.GreaterThanOrEqual:
2837 ig.Emit (OpCodes.Bge_Un, target);
2839 ig.Emit (OpCodes.Bge, target);
2842 ig.Emit (OpCodes.Blt_Un, target);
2844 ig.Emit (OpCodes.Blt, target);
2847 Console.WriteLine (oper);
2848 throw new Exception ("what is THAT");
2852 public override void Emit (EmitContext ec)
2854 ILGenerator ig = ec.ig;
2859 // Handle short-circuit operators differently
2862 if (oper == Operator.LogicalAnd) {
2863 Label load_zero = ig.DefineLabel ();
2864 Label end = ig.DefineLabel ();
2866 left.EmitBranchable (ec, load_zero, false);
2868 ig.Emit (OpCodes.Br, end);
2870 ig.MarkLabel (load_zero);
2871 ig.Emit (OpCodes.Ldc_I4_0);
2874 } else if (oper == Operator.LogicalOr) {
2875 Label load_one = ig.DefineLabel ();
2876 Label end = ig.DefineLabel ();
2878 left.EmitBranchable (ec, load_one, true);
2880 ig.Emit (OpCodes.Br, end);
2882 ig.MarkLabel (load_one);
2883 ig.Emit (OpCodes.Ldc_I4_1);
2891 bool is_unsigned = IsUnsigned (left.Type);
2894 case Operator.Multiply:
2896 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2897 opcode = OpCodes.Mul_Ovf;
2898 else if (is_unsigned)
2899 opcode = OpCodes.Mul_Ovf_Un;
2901 opcode = OpCodes.Mul;
2903 opcode = OpCodes.Mul;
2907 case Operator.Division:
2909 opcode = OpCodes.Div_Un;
2911 opcode = OpCodes.Div;
2914 case Operator.Modulus:
2916 opcode = OpCodes.Rem_Un;
2918 opcode = OpCodes.Rem;
2921 case Operator.Addition:
2923 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2924 opcode = OpCodes.Add_Ovf;
2925 else if (is_unsigned)
2926 opcode = OpCodes.Add_Ovf_Un;
2928 opcode = OpCodes.Add;
2930 opcode = OpCodes.Add;
2933 case Operator.Subtraction:
2935 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
2936 opcode = OpCodes.Sub_Ovf;
2937 else if (is_unsigned)
2938 opcode = OpCodes.Sub_Ovf_Un;
2940 opcode = OpCodes.Sub;
2942 opcode = OpCodes.Sub;
2945 case Operator.RightShift:
2947 opcode = OpCodes.Shr_Un;
2949 opcode = OpCodes.Shr;
2952 case Operator.LeftShift:
2953 opcode = OpCodes.Shl;
2956 case Operator.Equality:
2957 opcode = OpCodes.Ceq;
2960 case Operator.Inequality:
2961 ig.Emit (OpCodes.Ceq);
2962 ig.Emit (OpCodes.Ldc_I4_0);
2964 opcode = OpCodes.Ceq;
2967 case Operator.LessThan:
2969 opcode = OpCodes.Clt_Un;
2971 opcode = OpCodes.Clt;
2974 case Operator.GreaterThan:
2976 opcode = OpCodes.Cgt_Un;
2978 opcode = OpCodes.Cgt;
2981 case Operator.LessThanOrEqual:
2982 Type lt = left.Type;
2984 if (is_unsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
2985 ig.Emit (OpCodes.Cgt_Un);
2987 ig.Emit (OpCodes.Cgt);
2988 ig.Emit (OpCodes.Ldc_I4_0);
2990 opcode = OpCodes.Ceq;
2993 case Operator.GreaterThanOrEqual:
2994 Type le = left.Type;
2996 if (is_unsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
2997 ig.Emit (OpCodes.Clt_Un);
2999 ig.Emit (OpCodes.Clt);
3001 ig.Emit (OpCodes.Ldc_I4_0);
3003 opcode = OpCodes.Ceq;
3006 case Operator.BitwiseOr:
3007 opcode = OpCodes.Or;
3010 case Operator.BitwiseAnd:
3011 opcode = OpCodes.And;
3014 case Operator.ExclusiveOr:
3015 opcode = OpCodes.Xor;
3019 throw new Exception ("This should not happen: Operator = "
3020 + oper.ToString ());
3026 protected override void CloneTo (CloneContext clonectx, Expression t)
3028 Binary target = (Binary) t;
3030 target.left = left.Clone (clonectx);
3031 target.right = right.Clone (clonectx);
3034 public override Expression CreateExpressionTree (EmitContext ec)
3038 case Operator.Addition:
3040 method_name = "AddChecked";
3042 method_name = "Add";
3044 case Operator.BitwiseAnd:
3045 method_name = "And";
3047 case Operator.LogicalAnd:
3048 method_name = "AndAlso";
3051 case Operator.BitwiseOr:
3055 case Operator.LogicalOr:
3056 method_name = "OrElse";
3059 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
3062 ArrayList args = new ArrayList (2);
3063 args.Add (new Argument (left.CreateExpressionTree (ec)));
3064 args.Add (new Argument (right.CreateExpressionTree (ec)));
3065 return CreateExpressionFactoryCall (method_name, args);
3070 // Object created by Binary when the binary operator uses an method instead of being
3071 // a binary operation that maps to a CIL binary operation.
3073 public class BinaryMethod : Expression {
3074 public MethodBase method;
3075 public ArrayList Arguments;
3077 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3082 eclass = ExprClass.Value;
3085 public override Expression DoResolve (EmitContext ec)
3090 public override void Emit (EmitContext ec)
3092 ILGenerator ig = ec.ig;
3094 Invocation.EmitArguments (ec, Arguments, false, null);
3096 if (method is MethodInfo)
3097 ig.Emit (OpCodes.Call, (MethodInfo) method);
3099 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3104 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3105 // b, c, d... may be strings or objects.
3107 public class StringConcat : Expression {
3108 ArrayList arguments;
3110 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3113 type = TypeManager.string_type;
3114 eclass = ExprClass.Value;
3116 arguments = new ArrayList (2);
3121 public override Expression DoResolve (EmitContext ec)
3126 public void Append (EmitContext ec, Expression operand)
3131 StringConstant sc = operand as StringConstant;
3133 if (arguments.Count != 0) {
3134 Argument last_argument = (Argument) arguments [arguments.Count - 1];
3135 StringConstant last_expr_constant = last_argument.Expr as StringConstant;
3136 if (last_expr_constant != null) {
3137 last_argument.Expr = new StringConstant (
3138 last_expr_constant.Value + sc.Value, sc.Location);
3144 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3146 StringConcat concat_oper = operand as StringConcat;
3147 if (concat_oper != null) {
3148 arguments.AddRange (concat_oper.arguments);
3154 // Conversion to object
3156 if (operand.Type != TypeManager.string_type) {
3157 Expression expr = Convert.ImplicitConversion (ec, operand, TypeManager.object_type, loc);
3159 Binary.Error_OperatorCannotBeApplied (loc, "+", TypeManager.string_type, operand.Type);
3165 arguments.Add (new Argument (operand));
3168 Expression CreateConcatInvocation ()
3170 return new Invocation (
3171 new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "String", loc), "Concat", loc),
3175 public override void Emit (EmitContext ec)
3177 Expression concat = CreateConcatInvocation ();
3178 concat = concat.Resolve (ec);
3185 // Object created with +/= on delegates
3187 public class BinaryDelegate : Expression {
3191 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3196 eclass = ExprClass.Value;
3199 public override Expression DoResolve (EmitContext ec)
3204 public override void Emit (EmitContext ec)
3206 ILGenerator ig = ec.ig;
3208 Invocation.EmitArguments (ec, args, false, null);
3210 ig.Emit (OpCodes.Call, (MethodInfo) method);
3211 ig.Emit (OpCodes.Castclass, type);
3214 public Expression Right {
3216 Argument arg = (Argument) args [1];
3221 public bool IsAddition {
3223 return method == TypeManager.delegate_combine_delegate_delegate;
3229 // User-defined conditional logical operator
3230 public class ConditionalLogicalOperator : Expression {
3231 Expression left, right;
3234 public ConditionalLogicalOperator (bool is_and, Expression left, Expression right, Type t, Location loc)
3237 eclass = ExprClass.Value;
3241 this.is_and = is_and;
3244 protected void Error19 ()
3246 Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", left.GetSignatureForError (), right.GetSignatureForError ());
3249 protected void Error218 ()
3251 Error (218, "The type ('" + TypeManager.CSharpName (type) + "') must contain " +
3252 "declarations of operator true and operator false");
3255 Expression op_true, op_false, op;
3256 LocalTemporary left_temp;
3258 public override Expression DoResolve (EmitContext ec)
3260 MethodGroupExpr operator_group;
3262 operator_group = MethodLookup (ec.ContainerType, type, is_and ? "op_BitwiseAnd" : "op_BitwiseOr", loc) as MethodGroupExpr;
3263 if (operator_group == null) {
3268 left_temp = new LocalTemporary (type);
3270 ArrayList arguments = new ArrayList (2);
3271 arguments.Add (new Argument (left_temp, Argument.AType.Expression));
3272 arguments.Add (new Argument (right, Argument.AType.Expression));
3273 operator_group = operator_group.OverloadResolve (ec, ref arguments, false, loc);
3274 if (operator_group == null) {
3279 MethodInfo method = (MethodInfo)operator_group;
3280 if (method.ReturnType != type) {
3281 Report.Error (217, loc, "In order to be applicable as a short circuit operator a user-defined logical operator `{0}' " +
3282 "must have the same return type as the type of its 2 parameters", TypeManager.CSharpSignature (method));
3286 op = new StaticCallExpr (method, arguments, loc);
3288 op_true = GetOperatorTrue (ec, left_temp, loc);
3289 op_false = GetOperatorFalse (ec, left_temp, loc);
3290 if ((op_true == null) || (op_false == null)) {
3298 public override void Emit (EmitContext ec)
3300 ILGenerator ig = ec.ig;
3301 Label false_target = ig.DefineLabel ();
3302 Label end_target = ig.DefineLabel ();
3305 left_temp.Store (ec);
3307 (is_and ? op_false : op_true).EmitBranchable (ec, false_target, false);
3308 left_temp.Emit (ec);
3309 ig.Emit (OpCodes.Br, end_target);
3310 ig.MarkLabel (false_target);
3312 ig.MarkLabel (end_target);
3314 // We release 'left_temp' here since 'op' may refer to it too
3315 left_temp.Release (ec);
3319 public class PointerArithmetic : Expression {
3320 Expression left, right;
3324 // We assume that `l' is always a pointer
3326 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3332 is_add = is_addition;
3335 public override Expression DoResolve (EmitContext ec)
3337 eclass = ExprClass.Variable;
3339 if (left.Type == TypeManager.void_ptr_type) {
3340 Error (242, "The operation in question is undefined on void pointers");
3347 public override void Emit (EmitContext ec)
3349 Type op_type = left.Type;
3350 ILGenerator ig = ec.ig;
3352 // It must be either array or fixed buffer
3353 Type element = TypeManager.HasElementType (op_type) ?
3354 element = TypeManager.GetElementType (op_type) :
3355 element = AttributeTester.GetFixedBuffer (((FieldExpr)left).FieldInfo).ElementType;
3357 int size = GetTypeSize (element);
3358 Type rtype = right.Type;
3360 if (rtype.IsPointer){
3362 // handle (pointer - pointer)
3366 ig.Emit (OpCodes.Sub);
3370 ig.Emit (OpCodes.Sizeof, element);
3372 IntLiteral.EmitInt (ig, size);
3373 ig.Emit (OpCodes.Div);
3375 ig.Emit (OpCodes.Conv_I8);
3378 // handle + and - on (pointer op int)
3381 ig.Emit (OpCodes.Conv_I);
3383 Constant right_const = right as Constant;
3384 if (right_const != null && size != 0) {
3385 Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
3393 ig.Emit (OpCodes.Sizeof, element);
3395 IntLiteral.EmitInt (ig, size);
3396 if (rtype == TypeManager.int64_type)
3397 ig.Emit (OpCodes.Conv_I8);
3398 else if (rtype == TypeManager.uint64_type)
3399 ig.Emit (OpCodes.Conv_U8);
3400 ig.Emit (OpCodes.Mul);
3404 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
3405 ig.Emit (OpCodes.Conv_I);
3408 ig.Emit (OpCodes.Add);
3410 ig.Emit (OpCodes.Sub);
3416 /// Implements the ternary conditional operator (?:)
3418 public class Conditional : Expression {
3419 Expression expr, true_expr, false_expr;
3421 public Conditional (Expression expr, Expression true_expr, Expression false_expr)
3424 this.true_expr = true_expr;
3425 this.false_expr = false_expr;
3426 this.loc = expr.Location;
3429 public Expression Expr {
3435 public Expression TrueExpr {
3441 public Expression FalseExpr {
3447 public override Expression DoResolve (EmitContext ec)
3449 expr = expr.Resolve (ec);
3455 if (TypeManager.IsNullableValueType (expr.Type))
3456 return new Nullable.LiftedConditional (expr, true_expr, false_expr, loc).Resolve (ec);
3459 if (expr.Type != TypeManager.bool_type){
3460 expr = Expression.ResolveBoolean (
3467 Assign ass = expr as Assign;
3468 if (ass != null && ass.Source is Constant) {
3469 Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
3472 true_expr = true_expr.Resolve (ec);
3473 false_expr = false_expr.Resolve (ec);
3475 if (true_expr == null || false_expr == null)
3478 eclass = ExprClass.Value;
3479 if (true_expr.Type == false_expr.Type) {
3480 type = true_expr.Type;
3481 if (type == TypeManager.null_type) {
3482 // TODO: probably will have to implement ConditionalConstant
3483 // to call method without return constant as well
3484 Report.Warning (-101, 1, loc, "Conditional expression will always return same value");
3489 Type true_type = true_expr.Type;
3490 Type false_type = false_expr.Type;
3493 // First, if an implicit conversion exists from true_expr
3494 // to false_expr, then the result type is of type false_expr.Type
3496 conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
3499 // Check if both can convert implicitl to each other's type
3501 if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null){
3503 "Can not compute type of conditional expression " +
3504 "as `" + TypeManager.CSharpName (true_expr.Type) +
3505 "' and `" + TypeManager.CSharpName (false_expr.Type) +
3506 "' convert implicitly to each other");
3511 } else if ((conv = Convert.ImplicitConversion(ec, false_expr, true_type,loc))!= null){
3515 Report.Error (173, loc, "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
3516 true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
3521 // Dead code optimalization
3522 if (expr is BoolConstant){
3523 BoolConstant bc = (BoolConstant) expr;
3525 Report.Warning (429, 4, bc.Value ? false_expr.Location : true_expr.Location, "Unreachable expression code detected");
3526 return bc.Value ? true_expr : false_expr;
3532 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
3537 public override void Emit (EmitContext ec)
3539 ILGenerator ig = ec.ig;
3540 Label false_target = ig.DefineLabel ();
3541 Label end_target = ig.DefineLabel ();
3543 expr.EmitBranchable (ec, false_target, false);
3544 true_expr.Emit (ec);
3545 ig.Emit (OpCodes.Br, end_target);
3546 ig.MarkLabel (false_target);
3547 false_expr.Emit (ec);
3548 ig.MarkLabel (end_target);
3551 protected override void CloneTo (CloneContext clonectx, Expression t)
3553 Conditional target = (Conditional) t;
3555 target.expr = expr.Clone (clonectx);
3556 target.true_expr = true_expr.Clone (clonectx);
3557 target.false_expr = false_expr.Clone (clonectx);
3561 public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation {
3563 LocalTemporary temp;
3565 public abstract Variable Variable {
3569 public abstract bool IsRef {
3573 public override void Emit (EmitContext ec)
3579 // This method is used by parameters that are references, that are
3580 // being passed as references: we only want to pass the pointer (that
3581 // is already stored in the parameter, not the address of the pointer,
3582 // and not the value of the variable).
3584 public void EmitLoad (EmitContext ec)
3586 Report.Debug (64, "VARIABLE EMIT LOAD", this, Variable, type, loc);
3588 Variable.EmitInstance (ec);
3592 public void Emit (EmitContext ec, bool leave_copy)
3594 Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
3600 // If we are a reference, we loaded on the stack a pointer
3601 // Now lets load the real value
3603 LoadFromPtr (ec.ig, type);
3607 ec.ig.Emit (OpCodes.Dup);
3609 if (IsRef || Variable.NeedsTemporary) {
3610 temp = new LocalTemporary (Type);
3616 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
3617 bool prepare_for_load)
3619 Report.Debug (64, "VARIABLE EMIT ASSIGN", this, Variable, type, IsRef,
3622 ILGenerator ig = ec.ig;
3623 prepared = prepare_for_load;
3625 Variable.EmitInstance (ec);
3626 if (prepare_for_load) {
3627 if (Variable.HasInstance)
3628 ig.Emit (OpCodes.Dup);
3636 // HACK: variable is already emitted when source is an initializer
3637 if (source is NewInitialize)
3641 ig.Emit (OpCodes.Dup);
3642 if (IsRef || Variable.NeedsTemporary) {
3643 temp = new LocalTemporary (Type);
3649 StoreFromPtr (ig, type);
3651 Variable.EmitAssign (ec);
3659 public void AddressOf (EmitContext ec, AddressOp mode)
3661 Variable.EmitInstance (ec);
3662 Variable.EmitAddressOf (ec);
3669 public class LocalVariableReference : VariableReference, IVariable {
3670 public readonly string Name;
3672 public LocalInfo local_info;
3676 public LocalVariableReference (Block block, string name, Location l)
3681 eclass = ExprClass.Variable;
3685 // Setting `is_readonly' to false will allow you to create a writable
3686 // reference to a read-only variable. This is used by foreach and using.
3688 public LocalVariableReference (Block block, string name, Location l,
3689 LocalInfo local_info, bool is_readonly)
3690 : this (block, name, l)
3692 this.local_info = local_info;
3693 this.is_readonly = is_readonly;
3696 public VariableInfo VariableInfo {
3697 get { return local_info.VariableInfo; }
3700 public override bool IsRef {
3701 get { return false; }
3704 public bool IsReadOnly {
3705 get { return is_readonly; }
3708 public bool VerifyAssigned (EmitContext ec)
3710 VariableInfo variable_info = local_info.VariableInfo;
3711 return variable_info == null || variable_info.IsAssigned (ec, loc);
3714 void ResolveLocalInfo ()
3716 if (local_info == null) {
3717 local_info = Block.GetLocalInfo (Name);
3718 type = local_info.VariableType;
3719 is_readonly = local_info.ReadOnly;
3723 protected Expression DoResolveBase (EmitContext ec)
3725 type = local_info.VariableType;
3727 Expression e = Block.GetConstantExpression (Name);
3729 return e.Resolve (ec);
3731 if (!VerifyAssigned (ec))
3735 // If we are referencing a variable from the external block
3736 // flag it for capturing
3738 if (ec.MustCaptureVariable (local_info)) {
3739 if (local_info.AddressTaken){
3740 AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
3744 if (!ec.IsInProbingMode)
3746 ScopeInfo scope = local_info.Block.CreateScopeInfo ();
3747 variable = scope.AddLocal (local_info);
3748 type = variable.Type;
3755 public override Expression DoResolve (EmitContext ec)
3757 ResolveLocalInfo ();
3758 local_info.Used = true;
3760 if (type == null && local_info.Type is VarExpr) {
3761 local_info.VariableType = TypeManager.object_type;
3762 Error_VariableIsUsedBeforeItIsDeclared (Name);
3766 return DoResolveBase (ec);
3769 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3771 ResolveLocalInfo ();
3774 if (right_side == EmptyExpression.OutAccess)
3775 local_info.Used = true;
3777 // Infer implicitly typed local variable
3779 VarExpr ve = local_info.Type as VarExpr;
3781 ve.DoResolveLValue (ec, right_side);
3782 type = local_info.VariableType = ve.Type;
3789 if (right_side == EmptyExpression.OutAccess) {
3790 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
3791 } else if (right_side == EmptyExpression.LValueMemberAccess) {
3792 code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
3793 } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
3794 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
3796 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
3798 Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
3802 if (VariableInfo != null)
3803 VariableInfo.SetAssigned (ec);
3805 return DoResolveBase (ec);
3808 public bool VerifyFixed ()
3810 // A local Variable is always fixed.
3814 public override int GetHashCode ()
3816 return Name.GetHashCode ();
3819 public override bool Equals (object obj)
3821 LocalVariableReference lvr = obj as LocalVariableReference;
3825 return Name == lvr.Name && Block == lvr.Block;
3828 public override Variable Variable {
3829 get { return variable != null ? variable : local_info.Variable; }
3832 public override string ToString ()
3834 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
3837 protected override void CloneTo (CloneContext clonectx, Expression t)
3839 LocalVariableReference target = (LocalVariableReference) t;
3841 target.Block = clonectx.LookupBlock (Block);
3842 if (local_info != null)
3843 target.local_info = clonectx.LookupVariable (local_info);
3848 /// This represents a reference to a parameter in the intermediate
3851 public class ParameterReference : VariableReference, IVariable {
3852 readonly ToplevelParameterInfo pi;
3853 readonly ToplevelBlock referenced;
3856 public bool is_ref, is_out;
3859 get { return is_out; }
3862 public override bool IsRef {
3863 get { return is_ref; }
3866 public string Name {
3867 get { return Parameter.Name; }
3870 public Parameter Parameter {
3871 get { return pi.Parameter; }
3874 public ParameterReference (ToplevelBlock referenced, ToplevelParameterInfo pi, Location loc)
3877 this.referenced = referenced;
3879 eclass = ExprClass.Variable;
3882 public VariableInfo VariableInfo {
3883 get { return pi.VariableInfo; }
3886 public override Variable Variable {
3887 get { return variable != null ? variable : Parameter.Variable; }
3890 public bool VerifyFixed ()
3892 // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
3893 return Parameter.ModFlags == Parameter.Modifier.NONE;
3896 public bool IsAssigned (EmitContext ec, Location loc)
3898 // HACK: Variables are not captured in probing mode
3899 if (ec.IsInProbingMode)
3902 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (VariableInfo))
3905 Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
3909 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
3911 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsFieldAssigned (VariableInfo, field_name))
3914 Report.Error (170, loc, "Use of possibly unassigned field `{0}'", field_name);
3918 public void SetAssigned (EmitContext ec)
3920 if (is_out && ec.DoFlowAnalysis)
3921 ec.CurrentBranching.SetAssigned (VariableInfo);
3924 public void SetFieldAssigned (EmitContext ec, string field_name)
3926 if (is_out && ec.DoFlowAnalysis)
3927 ec.CurrentBranching.SetFieldAssigned (VariableInfo, field_name);
3930 protected bool DoResolveBase (EmitContext ec)
3932 Parameter par = Parameter;
3933 if (!par.Resolve (ec)) {
3937 type = par.ParameterType;
3938 Parameter.Modifier mod = par.ModFlags;
3939 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
3940 is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
3941 eclass = ExprClass.Variable;
3943 AnonymousContainer am = ec.CurrentAnonymousMethod;
3947 ToplevelBlock declared = pi.Block;
3948 if (is_ref && declared != referenced) {
3949 Report.Error (1628, Location,
3950 "Cannot use ref or out parameter `{0}' inside an " +
3951 "anonymous method block", par.Name);
3955 if (!am.IsIterator && declared == referenced)
3958 // Don't capture aruments when the probing is on
3959 if (!ec.IsInProbingMode) {
3960 ScopeInfo scope = declared.CreateScopeInfo ();
3961 variable = scope.AddParameter (par, pi.Index);
3962 type = variable.Type;
3967 public override int GetHashCode ()
3969 return Name.GetHashCode ();
3972 public override bool Equals (object obj)
3974 ParameterReference pr = obj as ParameterReference;
3978 return Name == pr.Name && referenced == pr.referenced;
3981 public override Expression CreateExpressionTree (EmitContext ec)
3983 return Parameter.ExpressionTreeVariableReference ();
3987 // Notice that for ref/out parameters, the type exposed is not the
3988 // same type exposed externally.
3991 // externally we expose "int&"
3992 // here we expose "int".
3994 // We record this in "is_ref". This means that the type system can treat
3995 // the type as it is expected, but when we generate the code, we generate
3996 // the alternate kind of code.
3998 public override Expression DoResolve (EmitContext ec)
4000 if (!DoResolveBase (ec))
4003 if (is_out && ec.DoFlowAnalysis &&
4004 (!ec.OmitStructFlowAnalysis || !VariableInfo.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4010 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4012 if (!DoResolveBase (ec))
4015 // HACK: parameters are not captured when probing is on
4016 if (!ec.IsInProbingMode)
4022 static public void EmitLdArg (ILGenerator ig, int x)
4026 case 0: ig.Emit (OpCodes.Ldarg_0); break;
4027 case 1: ig.Emit (OpCodes.Ldarg_1); break;
4028 case 2: ig.Emit (OpCodes.Ldarg_2); break;
4029 case 3: ig.Emit (OpCodes.Ldarg_3); break;
4030 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
4033 ig.Emit (OpCodes.Ldarg, x);
4036 public override string ToString ()
4038 return "ParameterReference[" + Name + "]";
4043 /// Used for arguments to New(), Invocation()
4045 public class Argument {
4046 public enum AType : byte {
4053 public static readonly Argument[] Empty = new Argument [0];
4055 public readonly AType ArgType;
4056 public Expression Expr;
4058 public Argument (Expression expr, AType type)
4061 this.ArgType = type;
4064 public Argument (Expression expr)
4067 this.ArgType = AType.Expression;
4072 if (ArgType == AType.Ref || ArgType == AType.Out)
4073 return TypeManager.GetReferenceType (Expr.Type);
4079 public Parameter.Modifier Modifier
4084 return Parameter.Modifier.OUT;
4087 return Parameter.Modifier.REF;
4090 return Parameter.Modifier.NONE;
4095 public string GetSignatureForError ()
4097 if (Expr.eclass == ExprClass.MethodGroup)
4098 return Expr.ExprClassName;
4100 return Expr.GetSignatureForError ();
4103 public bool ResolveMethodGroup (EmitContext ec)
4105 SimpleName sn = Expr as SimpleName;
4107 Expr = sn.GetMethodGroup ();
4109 // FIXME: csc doesn't report any error if you try to use `ref' or
4110 // `out' in a delegate creation expression.
4111 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4118 public bool Resolve (EmitContext ec, Location loc)
4120 using (ec.With (EmitContext.Flags.DoFlowAnalysis, true)) {
4121 // Verify that the argument is readable
4122 if (ArgType != AType.Out)
4123 Expr = Expr.Resolve (ec);
4125 // Verify that the argument is writeable
4126 if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
4127 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
4129 return Expr != null;
4133 public void Emit (EmitContext ec)
4135 if (ArgType != AType.Ref && ArgType != AType.Out) {
4140 AddressOp mode = AddressOp.Store;
4141 if (ArgType == AType.Ref)
4142 mode |= AddressOp.Load;
4144 IMemoryLocation ml = (IMemoryLocation) Expr;
4145 ParameterReference pr = ml as ParameterReference;
4148 // ParameterReferences might already be references, so we want
4149 // to pass just the value
4151 if (pr != null && pr.IsRef)
4154 ml.AddressOf (ec, mode);
4157 public Argument Clone (CloneContext clonectx)
4159 return new Argument (Expr.Clone (clonectx), ArgType);
4164 /// Invocation of methods or delegates.
4166 public class Invocation : ExpressionStatement {
4167 protected ArrayList Arguments;
4169 protected MethodGroupExpr mg;
4170 bool arguments_resolved;
4173 // arguments is an ArrayList, but we do not want to typecast,
4174 // as it might be null.
4176 public Invocation (Expression expr, ArrayList arguments)
4178 SimpleName sn = expr as SimpleName;
4180 this.expr = sn.GetMethodGroup ();
4184 Arguments = arguments;
4185 loc = expr.Location;
4188 public Invocation (Expression expr, ArrayList arguments, bool arguments_resolved)
4189 : this (expr, arguments)
4191 this.arguments_resolved = arguments_resolved;
4194 public static string FullMethodDesc (MethodBase mb)
4200 if (mb is MethodInfo) {
4201 sb = new StringBuilder (TypeManager.CSharpName (((MethodInfo) mb).ReturnType));
4205 sb = new StringBuilder ();
4207 sb.Append (TypeManager.CSharpSignature (mb));
4208 return sb.ToString ();
4211 public override Expression CreateExpressionTree (EmitContext ec)
4213 ArrayList args = new ArrayList (Arguments.Count + 3);
4215 args.Add (new Argument (mg.InstanceExpression.CreateExpressionTree (ec)));
4217 args.Add (new Argument (new NullConstant (loc).CreateExpressionTree (ec)));
4219 args.Add (new Argument (mg.CreateExpressionTree (ec)));
4220 foreach (Argument a in Arguments) {
4221 Expression e = a.Expr.CreateExpressionTree (ec);
4223 args.Add (new Argument (e));
4226 return CreateExpressionFactoryCall ("Call", args);
4229 public override Expression DoResolve (EmitContext ec)
4231 // Don't resolve already resolved expression
4232 if (eclass != ExprClass.Invalid)
4235 Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4236 if (expr_resolved == null)
4239 mg = expr_resolved as MethodGroupExpr;
4241 Type expr_type = expr_resolved.Type;
4243 if (expr_type != null && TypeManager.IsDelegateType (expr_type)){
4244 return (new DelegateInvocation (
4245 expr_resolved, Arguments, loc)).Resolve (ec);
4248 expr_resolved.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
4253 // Next, evaluate all the expressions in the argument list
4255 if (Arguments != null && !arguments_resolved) {
4256 for (int i = 0; i < Arguments.Count; ++i)
4258 if (!((Argument)Arguments[i]).Resolve(ec, loc))
4263 mg = DoResolveOverload (ec);
4267 MethodInfo method = (MethodInfo)mg;
4268 if (method != null) {
4269 type = TypeManager.TypeToCoreType (method.ReturnType);
4271 // TODO: this is a copy of mg.ResolveMemberAccess method
4272 Expression iexpr = mg.InstanceExpression;
4273 if (method.IsStatic) {
4274 if (iexpr == null ||
4275 iexpr is This || iexpr is EmptyExpression ||
4276 mg.IdenticalTypeName) {
4277 mg.InstanceExpression = null;
4279 MemberExpr.error176 (loc, mg.GetSignatureForError ());
4285 if (type.IsPointer){
4293 // Only base will allow this invocation to happen.
4295 if (mg.IsBase && method.IsAbstract){
4296 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (method));
4300 if (Arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == "Finalize") {
4302 Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4304 Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4308 if (IsSpecialMethodInvocation (method)) {
4312 if (mg.InstanceExpression != null)
4313 mg.InstanceExpression.CheckMarshalByRefAccess (ec);
4315 eclass = ExprClass.Value;
4319 protected virtual MethodGroupExpr DoResolveOverload (EmitContext ec)
4321 return mg.OverloadResolve (ec, ref Arguments, false, loc);
4324 bool IsSpecialMethodInvocation (MethodBase method)
4326 if (!TypeManager.IsSpecialMethod (method))
4329 Report.SymbolRelatedToPreviousError (method);
4330 Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
4331 TypeManager.CSharpSignature (method, true));
4337 /// Emits a list of resolved Arguments that are in the arguments
4340 /// The MethodBase argument might be null if the
4341 /// emission of the arguments is known not to contain
4342 /// a `params' field (for example in constructors or other routines
4343 /// that keep their arguments in this structure)
4345 /// if `dup_args' is true, a copy of the arguments will be left
4346 /// on the stack. If `dup_args' is true, you can specify `this_arg'
4347 /// which will be duplicated before any other args. Only EmitCall
4348 /// should be using this interface.
4350 public static void EmitArguments (EmitContext ec, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
4352 if (arguments == null)
4355 int top = arguments.Count;
4356 LocalTemporary [] temps = null;
4358 if (dup_args && top != 0)
4359 temps = new LocalTemporary [top];
4361 int argument_index = 0;
4363 for (int i = 0; i < top; i++) {
4364 a = (Argument) arguments [argument_index++];
4367 ec.ig.Emit (OpCodes.Dup);
4368 (temps [i] = new LocalTemporary (a.Type)).Store (ec);
4373 if (this_arg != null)
4376 for (int i = 0; i < top; i ++) {
4377 temps [i].Emit (ec);
4378 temps [i].Release (ec);
4383 static Type[] GetVarargsTypes (MethodBase mb, ArrayList arguments)
4385 ParameterData pd = TypeManager.GetParameterData (mb);
4387 if (arguments == null)
4388 return new Type [0];
4390 Argument a = (Argument) arguments [pd.Count - 1];
4391 Arglist list = (Arglist) a.Expr;
4393 return list.ArgumentTypes;
4397 /// This checks the ConditionalAttribute on the method
4399 public static bool IsMethodExcluded (MethodBase method)
4401 if (method.IsConstructor)
4404 method = TypeManager.DropGenericMethodArguments (method);
4405 if (method.DeclaringType.Module == CodeGen.Module.Builder) {
4406 IMethodData md = TypeManager.GetMethod (method);
4408 return md.IsExcluded ();
4410 // For some methods (generated by delegate class) GetMethod returns null
4411 // because they are not included in builder_to_method table
4415 return AttributeTester.IsConditionalMethodExcluded (method);
4419 /// is_base tells whether we want to force the use of the `call'
4420 /// opcode instead of using callvirt. Call is required to call
4421 /// a specific method, while callvirt will always use the most
4422 /// recent method in the vtable.
4424 /// is_static tells whether this is an invocation on a static method
4426 /// instance_expr is an expression that represents the instance
4427 /// it must be non-null if is_static is false.
4429 /// method is the method to invoke.
4431 /// Arguments is the list of arguments to pass to the method or constructor.
4433 public static void EmitCall (EmitContext ec, bool is_base,
4434 Expression instance_expr,
4435 MethodBase method, ArrayList Arguments, Location loc)
4437 EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
4440 // `dup_args' leaves an extra copy of the arguments on the stack
4441 // `omit_args' does not leave any arguments at all.
4442 // So, basically, you could make one call with `dup_args' set to true,
4443 // and then another with `omit_args' set to true, and the two calls
4444 // would have the same set of arguments. However, each argument would
4445 // only have been evaluated once.
4446 public static void EmitCall (EmitContext ec, bool is_base,
4447 Expression instance_expr,
4448 MethodBase method, ArrayList Arguments, Location loc,
4449 bool dup_args, bool omit_args)
4451 ILGenerator ig = ec.ig;
4452 bool struct_call = false;
4453 bool this_call = false;
4454 LocalTemporary this_arg = null;
4456 Type decl_type = method.DeclaringType;
4458 if (!RootContext.StdLib) {
4459 // Replace any calls to the system's System.Array type with calls to
4460 // the newly created one.
4461 if (method == TypeManager.system_int_array_get_length)
4462 method = TypeManager.int_array_get_length;
4463 else if (method == TypeManager.system_int_array_get_rank)
4464 method = TypeManager.int_array_get_rank;
4465 else if (method == TypeManager.system_object_array_clone)
4466 method = TypeManager.object_array_clone;
4467 else if (method == TypeManager.system_int_array_get_length_int)
4468 method = TypeManager.int_array_get_length_int;
4469 else if (method == TypeManager.system_int_array_get_lower_bound_int)
4470 method = TypeManager.int_array_get_lower_bound_int;
4471 else if (method == TypeManager.system_int_array_get_upper_bound_int)
4472 method = TypeManager.int_array_get_upper_bound_int;
4473 else if (method == TypeManager.system_void_array_copyto_array_int)
4474 method = TypeManager.void_array_copyto_array_int;
4477 if (!ec.IsInObsoleteScope) {
4479 // This checks ObsoleteAttribute on the method and on the declaring type
4481 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
4483 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
4485 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
4487 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
4491 if (IsMethodExcluded (method))
4494 bool is_static = method.IsStatic;
4496 if (instance_expr == EmptyExpression.Null) {
4497 SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (method));
4501 this_call = instance_expr is This;
4502 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
4506 // If this is ourselves, push "this"
4510 Type iexpr_type = instance_expr.Type;
4513 // Push the instance expression
4515 if (TypeManager.IsValueType (iexpr_type)) {
4517 // Special case: calls to a function declared in a
4518 // reference-type with a value-type argument need
4519 // to have their value boxed.
4520 if (decl_type.IsValueType ||
4521 TypeManager.IsGenericParameter (iexpr_type)) {
4523 // If the expression implements IMemoryLocation, then
4524 // we can optimize and use AddressOf on the
4527 // If not we have to use some temporary storage for
4529 if (instance_expr is IMemoryLocation) {
4530 ((IMemoryLocation)instance_expr).
4531 AddressOf (ec, AddressOp.LoadStore);
4533 LocalTemporary temp = new LocalTemporary (iexpr_type);
4534 instance_expr.Emit (ec);
4536 temp.AddressOf (ec, AddressOp.Load);
4539 // avoid the overhead of doing this all the time.
4541 t = TypeManager.GetReferenceType (iexpr_type);
4543 instance_expr.Emit (ec);
4544 ig.Emit (OpCodes.Box, instance_expr.Type);
4545 t = TypeManager.object_type;
4548 instance_expr.Emit (ec);
4549 t = instance_expr.Type;
4553 ig.Emit (OpCodes.Dup);
4554 if (Arguments != null && Arguments.Count != 0) {
4555 this_arg = new LocalTemporary (t);
4556 this_arg.Store (ec);
4563 EmitArguments (ec, Arguments, dup_args, this_arg);
4566 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
4567 ig.Emit (OpCodes.Constrained, instance_expr.Type);
4571 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
4572 call_op = OpCodes.Call;
4574 call_op = OpCodes.Callvirt;
4576 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
4577 Type[] varargs_types = GetVarargsTypes (method, Arguments);
4578 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
4585 // and DoFoo is not virtual, you can omit the callvirt,
4586 // because you don't need the null checking behavior.
4588 if (method is MethodInfo)
4589 ig.Emit (call_op, (MethodInfo) method);
4591 ig.Emit (call_op, (ConstructorInfo) method);
4594 public override void Emit (EmitContext ec)
4596 mg.EmitCall (ec, Arguments);
4599 public override void EmitStatement (EmitContext ec)
4604 // Pop the return value if there is one
4606 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
4607 ec.ig.Emit (OpCodes.Pop);
4610 protected override void CloneTo (CloneContext clonectx, Expression t)
4612 Invocation target = (Invocation) t;
4614 if (Arguments != null) {
4615 target.Arguments = new ArrayList (Arguments.Count);
4616 foreach (Argument a in Arguments)
4617 target.Arguments.Add (a.Clone (clonectx));
4620 target.expr = expr.Clone (clonectx);
4624 public class InvocationOrCast : ExpressionStatement
4627 Expression argument;
4629 public InvocationOrCast (Expression expr, Expression argument)
4632 this.argument = argument;
4633 this.loc = expr.Location;
4636 public override Expression DoResolve (EmitContext ec)
4639 // First try to resolve it as a cast.
4641 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
4642 if ((te != null) && (te.eclass == ExprClass.Type)) {
4643 Cast cast = new Cast (te, argument, loc);
4644 return cast.Resolve (ec);
4648 // This can either be a type or a delegate invocation.
4649 // Let's just resolve it and see what we'll get.
4651 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
4656 // Ok, so it's a Cast.
4658 if (expr.eclass == ExprClass.Type) {
4659 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
4660 return cast.Resolve (ec);
4664 // It's a delegate invocation.
4666 if (!TypeManager.IsDelegateType (expr.Type)) {
4667 Error (149, "Method name expected");
4671 ArrayList args = new ArrayList ();
4672 args.Add (new Argument (argument, Argument.AType.Expression));
4673 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
4674 return invocation.Resolve (ec);
4677 public override ExpressionStatement ResolveStatement (EmitContext ec)
4680 // First try to resolve it as a cast.
4682 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
4683 if ((te != null) && (te.eclass == ExprClass.Type)) {
4684 Error_InvalidExpressionStatement ();
4689 // This can either be a type or a delegate invocation.
4690 // Let's just resolve it and see what we'll get.
4692 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
4693 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
4694 Error_InvalidExpressionStatement ();
4699 // It's a delegate invocation.
4701 if (!TypeManager.IsDelegateType (expr.Type)) {
4702 Error (149, "Method name expected");
4706 ArrayList args = new ArrayList ();
4707 args.Add (new Argument (argument, Argument.AType.Expression));
4708 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
4709 return invocation.ResolveStatement (ec);
4712 public override void Emit (EmitContext ec)
4714 throw new Exception ("Cannot happen");
4717 public override void EmitStatement (EmitContext ec)
4719 throw new Exception ("Cannot happen");
4722 protected override void CloneTo (CloneContext clonectx, Expression t)
4724 InvocationOrCast target = (InvocationOrCast) t;
4726 target.expr = expr.Clone (clonectx);
4727 target.argument = argument.Clone (clonectx);
4732 // This class is used to "disable" the code generation for the
4733 // temporary variable when initializing value types.
4735 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
4736 public void AddressOf (EmitContext ec, AddressOp Mode)
4743 /// Implements the new expression
4745 public class New : ExpressionStatement, IMemoryLocation {
4746 ArrayList Arguments;
4749 // During bootstrap, it contains the RequestedType,
4750 // but if `type' is not null, it *might* contain a NewDelegate
4751 // (because of field multi-initialization)
4753 public Expression RequestedType;
4755 MethodGroupExpr method;
4758 // If set, the new expression is for a value_target, and
4759 // we will not leave anything on the stack.
4761 protected Expression value_target;
4762 protected bool value_target_set;
4763 bool is_type_parameter = false;
4765 public New (Expression requested_type, ArrayList arguments, Location l)
4767 RequestedType = requested_type;
4768 Arguments = arguments;
4772 public bool SetTargetVariable (Expression value)
4774 value_target = value;
4775 value_target_set = true;
4776 if (!(value_target is IMemoryLocation)){
4777 Error_UnexpectedKind (null, "variable", loc);
4784 // This function is used to disable the following code sequence for
4785 // value type initialization:
4787 // AddressOf (temporary)
4791 // Instead the provide will have provided us with the address on the
4792 // stack to store the results.
4794 static Expression MyEmptyExpression;
4796 public void DisableTemporaryValueType ()
4798 if (MyEmptyExpression == null)
4799 MyEmptyExpression = new EmptyAddressOf ();
4802 // To enable this, look into:
4803 // test-34 and test-89 and self bootstrapping.
4805 // For instance, we can avoid a copy by using `newobj'
4806 // instead of Call + Push-temp on value types.
4807 // value_target = MyEmptyExpression;
4812 /// Converts complex core type syntax like 'new int ()' to simple constant
4814 public static Constant Constantify (Type t)
4816 if (t == TypeManager.int32_type)
4817 return new IntConstant (0, Location.Null);
4818 if (t == TypeManager.uint32_type)
4819 return new UIntConstant (0, Location.Null);
4820 if (t == TypeManager.int64_type)
4821 return new LongConstant (0, Location.Null);
4822 if (t == TypeManager.uint64_type)
4823 return new ULongConstant (0, Location.Null);
4824 if (t == TypeManager.float_type)
4825 return new FloatConstant (0, Location.Null);
4826 if (t == TypeManager.double_type)
4827 return new DoubleConstant (0, Location.Null);
4828 if (t == TypeManager.short_type)
4829 return new ShortConstant (0, Location.Null);
4830 if (t == TypeManager.ushort_type)
4831 return new UShortConstant (0, Location.Null);
4832 if (t == TypeManager.sbyte_type)
4833 return new SByteConstant (0, Location.Null);
4834 if (t == TypeManager.byte_type)
4835 return new ByteConstant (0, Location.Null);
4836 if (t == TypeManager.char_type)
4837 return new CharConstant ('\0', Location.Null);
4838 if (t == TypeManager.bool_type)
4839 return new BoolConstant (false, Location.Null);
4840 if (t == TypeManager.decimal_type)
4841 return new DecimalConstant (0, Location.Null);
4842 if (TypeManager.IsEnumType (t))
4843 return new EnumConstant (Constantify (TypeManager.EnumToUnderlying (t)), t);
4849 // Checks whether the type is an interface that has the
4850 // [ComImport, CoClass] attributes and must be treated
4853 public Expression CheckComImport (EmitContext ec)
4855 if (!type.IsInterface)
4859 // Turn the call into:
4860 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
4862 Type real_class = AttributeTester.GetCoClassAttribute (type);
4863 if (real_class == null)
4866 New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
4867 Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
4868 return cast.Resolve (ec);
4871 public override Expression DoResolve (EmitContext ec)
4874 // The New DoResolve might be called twice when initializing field
4875 // expressions (see EmitFieldInitializers, the call to
4876 // GetInitializerExpression will perform a resolve on the expression,
4877 // and later the assign will trigger another resolution
4879 // This leads to bugs (#37014)
4882 if (RequestedType is NewDelegate)
4883 return RequestedType;
4887 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
4893 if (type == TypeManager.void_type) {
4894 Error_VoidInvalidInTheContext (loc);
4898 if (type.IsPointer) {
4899 Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
4900 TypeManager.CSharpName (type));
4904 if (Arguments == null) {
4905 Expression c = Constantify (type);
4910 if (TypeManager.IsDelegateType (type)) {
4911 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
4912 if (RequestedType != null)
4913 if (!(RequestedType is DelegateCreation))
4914 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
4915 return RequestedType;
4919 if (type.IsGenericParameter) {
4920 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
4922 if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
4923 Error (304, String.Format (
4924 "Cannot create an instance of the " +
4925 "variable type '{0}' because it " +
4926 "doesn't have the new() constraint",
4931 if ((Arguments != null) && (Arguments.Count != 0)) {
4932 Error (417, String.Format (
4933 "`{0}': cannot provide arguments " +
4934 "when creating an instance of a " +
4935 "variable type.", type));
4939 is_type_parameter = true;
4940 eclass = ExprClass.Value;
4945 if (type.IsAbstract && type.IsSealed) {
4946 Report.SymbolRelatedToPreviousError (type);
4947 Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
4951 if (type.IsInterface || type.IsAbstract){
4952 if (!TypeManager.IsGenericType (type)) {
4953 RequestedType = CheckComImport (ec);
4954 if (RequestedType != null)
4955 return RequestedType;
4958 Report.SymbolRelatedToPreviousError (type);
4959 Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
4963 bool is_struct = type.IsValueType;
4964 eclass = ExprClass.Value;
4967 // SRE returns a match for .ctor () on structs (the object constructor),
4968 // so we have to manually ignore it.
4970 if (is_struct && Arguments == null)
4973 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
4974 Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
4975 MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
4977 if (Arguments != null){
4978 foreach (Argument a in Arguments){
4979 if (!a.Resolve (ec, loc))
4987 method = ml as MethodGroupExpr;
4988 if (method == null) {
4989 ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
4993 method = method.OverloadResolve (ec, ref Arguments, false, loc);
5000 bool DoEmitTypeParameter (EmitContext ec)
5003 ILGenerator ig = ec.ig;
5004 // IMemoryLocation ml;
5006 MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
5007 new Type [] { type });
5009 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5010 if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
5011 ig.Emit (OpCodes.Call, ci);
5015 // Allow DoEmit() to be called multiple times.
5016 // We need to create a new LocalTemporary each time since
5017 // you can't share LocalBuilders among ILGeneators.
5018 LocalTemporary temp = new LocalTemporary (type);
5020 Label label_activator = ig.DefineLabel ();
5021 Label label_end = ig.DefineLabel ();
5023 temp.AddressOf (ec, AddressOp.Store);
5024 ig.Emit (OpCodes.Initobj, type);
5027 ig.Emit (OpCodes.Box, type);
5028 ig.Emit (OpCodes.Brfalse, label_activator);
5030 temp.AddressOf (ec, AddressOp.Store);
5031 ig.Emit (OpCodes.Initobj, type);
5033 ig.Emit (OpCodes.Br, label_end);
5035 ig.MarkLabel (label_activator);
5037 ig.Emit (OpCodes.Call, ci);
5038 ig.MarkLabel (label_end);
5041 throw new InternalErrorException ();
5046 // This DoEmit can be invoked in two contexts:
5047 // * As a mechanism that will leave a value on the stack (new object)
5048 // * As one that wont (init struct)
5050 // You can control whether a value is required on the stack by passing
5051 // need_value_on_stack. The code *might* leave a value on the stack
5052 // so it must be popped manually
5054 // If we are dealing with a ValueType, we have a few
5055 // situations to deal with:
5057 // * The target is a ValueType, and we have been provided
5058 // the instance (this is easy, we are being assigned).
5060 // * The target of New is being passed as an argument,
5061 // to a boxing operation or a function that takes a
5064 // In this case, we need to create a temporary variable
5065 // that is the argument of New.
5067 // Returns whether a value is left on the stack
5069 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5071 bool is_value_type = TypeManager.IsValueType (type);
5072 ILGenerator ig = ec.ig;
5077 // Allow DoEmit() to be called multiple times.
5078 // We need to create a new LocalTemporary each time since
5079 // you can't share LocalBuilders among ILGeneators.
5080 if (!value_target_set)
5081 value_target = new LocalTemporary (type);
5083 ml = (IMemoryLocation) value_target;
5084 ml.AddressOf (ec, AddressOp.Store);
5088 method.EmitArguments (ec, Arguments);
5092 ig.Emit (OpCodes.Initobj, type);
5094 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5095 if (need_value_on_stack){
5096 value_target.Emit (ec);
5101 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5106 public override void Emit (EmitContext ec)
5108 if (is_type_parameter)
5109 DoEmitTypeParameter (ec);
5114 public override void EmitStatement (EmitContext ec)
5116 bool value_on_stack;
5118 if (is_type_parameter)
5119 value_on_stack = DoEmitTypeParameter (ec);
5121 value_on_stack = DoEmit (ec, false);
5124 ec.ig.Emit (OpCodes.Pop);
5128 public virtual bool HasInitializer {
5134 public void AddressOf (EmitContext ec, AddressOp Mode)
5136 if (is_type_parameter) {
5137 LocalTemporary temp = new LocalTemporary (type);
5138 DoEmitTypeParameter (ec);
5140 temp.AddressOf (ec, Mode);
5144 if (!type.IsValueType){
5146 // We throw an exception. So far, I believe we only need to support
5148 // foreach (int j in new StructType ())
5151 throw new Exception ("AddressOf should not be used for classes");
5154 if (!value_target_set)
5155 value_target = new LocalTemporary (type);
5156 IMemoryLocation ml = (IMemoryLocation) value_target;
5158 ml.AddressOf (ec, AddressOp.Store);
5159 if (method == null) {
5160 ec.ig.Emit (OpCodes.Initobj, type);
5162 method.EmitArguments (ec, Arguments);
5163 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5166 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5169 protected override void CloneTo (CloneContext clonectx, Expression t)
5171 New target = (New) t;
5173 target.RequestedType = RequestedType.Clone (clonectx);
5174 if (Arguments != null){
5175 target.Arguments = new ArrayList ();
5176 foreach (Argument a in Arguments){
5177 target.Arguments.Add (a.Clone (clonectx));
5184 /// 14.5.10.2: Represents an array creation expression.
5188 /// There are two possible scenarios here: one is an array creation
5189 /// expression that specifies the dimensions and optionally the
5190 /// initialization data and the other which does not need dimensions
5191 /// specified but where initialization data is mandatory.
5193 public class ArrayCreation : Expression {
5194 Expression requested_base_type;
5195 ArrayList initializers;
5198 // The list of Argument types.
5199 // This is used to construct the `newarray' or constructor signature
5201 protected ArrayList arguments;
5203 protected Type array_element_type;
5204 bool expect_initializers = false;
5205 int num_arguments = 0;
5206 protected int dimensions;
5207 protected readonly string rank;
5209 protected ArrayList array_data;
5213 // The number of constants in array initializers
5214 int const_initializers_count;
5215 bool only_constant_initializers;
5217 public ArrayCreation (Expression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5219 this.requested_base_type = requested_base_type;
5220 this.initializers = initializers;
5224 arguments = new ArrayList ();
5226 foreach (Expression e in exprs) {
5227 arguments.Add (new Argument (e, Argument.AType.Expression));
5232 public ArrayCreation (Expression requested_base_type, string rank, ArrayList initializers, Location l)
5234 this.requested_base_type = requested_base_type;
5235 this.initializers = initializers;
5239 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5241 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5243 //dimensions = tmp.Length - 1;
5244 expect_initializers = true;
5247 public Expression FormArrayType (Expression base_type, int idx_count, string rank)
5249 StringBuilder sb = new StringBuilder (rank);
5252 for (int i = 1; i < idx_count; i++)
5257 return new ComposedCast (base_type, sb.ToString (), loc);
5260 void Error_IncorrectArrayInitializer ()
5262 Error (178, "Invalid rank specifier: expected `,' or `]'");
5265 protected override void Error_NegativeArrayIndex (Location loc)
5267 Report.Error (248, loc, "Cannot create an array with a negative size");
5270 bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
5272 if (specified_dims) {
5273 Argument a = (Argument) arguments [idx];
5275 if (!a.Resolve (ec, loc))
5278 Constant c = a.Expr as Constant;
5280 c = c.ImplicitConversionRequired (TypeManager.int32_type, a.Expr.Location);
5284 Report.Error (150, a.Expr.Location, "A constant value is expected");
5288 int value = (int) c.GetValue ();
5290 if (value != probe.Count) {
5291 Error_IncorrectArrayInitializer ();
5295 bounds [idx] = value;
5298 int child_bounds = -1;
5299 only_constant_initializers = true;
5300 for (int i = 0; i < probe.Count; ++i) {
5301 object o = probe [i];
5302 if (o is ArrayList) {
5303 ArrayList sub_probe = o as ArrayList;
5304 int current_bounds = sub_probe.Count;
5306 if (child_bounds == -1)
5307 child_bounds = current_bounds;
5309 else if (child_bounds != current_bounds){
5310 Error_IncorrectArrayInitializer ();
5313 if (idx + 1 >= dimensions){
5314 Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5318 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims);
5322 if (child_bounds != -1){
5323 Error_IncorrectArrayInitializer ();
5327 Expression element = ResolveArrayElement (ec, (Expression) o);
5328 if (element == null)
5331 // Initializers with the default values can be ignored
5332 Constant c = element as Constant;
5334 if (c.IsDefaultInitializer (array_element_type)) {
5338 ++const_initializers_count;
5341 only_constant_initializers = false;
5344 array_data.Add (element);
5351 public override Expression CreateExpressionTree (EmitContext ec)
5353 if (dimensions != 1) {
5354 Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
5358 ArrayList args = new ArrayList (array_data == null ? 1 : array_data.Count + 1);
5359 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5360 if (array_data != null) {
5361 foreach (Expression e in array_data)
5362 args.Add (new Argument (e.CreateExpressionTree (ec)));
5365 return CreateExpressionFactoryCall ("NewArrayInit", args);
5368 public void UpdateIndices ()
5371 for (ArrayList probe = initializers; probe != null;) {
5372 if (probe.Count > 0 && probe [0] is ArrayList) {
5373 Expression e = new IntConstant (probe.Count, Location.Null);
5374 arguments.Add (new Argument (e, Argument.AType.Expression));
5376 bounds [i++] = probe.Count;
5378 probe = (ArrayList) probe [0];
5381 Expression e = new IntConstant (probe.Count, Location.Null);
5382 arguments.Add (new Argument (e, Argument.AType.Expression));
5384 bounds [i++] = probe.Count;
5391 protected virtual Expression ResolveArrayElement (EmitContext ec, Expression element)
5393 element = element.Resolve (ec);
5394 if (element == null)
5397 return Convert.ImplicitConversionRequired (
5398 ec, element, array_element_type, loc);
5401 protected bool ResolveInitializers (EmitContext ec)
5403 if (initializers == null) {
5404 return !expect_initializers;
5408 // We use this to store all the date values in the order in which we
5409 // will need to store them in the byte blob later
5411 array_data = new ArrayList ();
5412 bounds = new System.Collections.Specialized.HybridDictionary ();
5414 if (arguments != null)
5415 return CheckIndices (ec, initializers, 0, true);
5417 arguments = new ArrayList ();
5419 if (!CheckIndices (ec, initializers, 0, false))
5428 // Resolved the type of the array
5430 bool ResolveArrayType (EmitContext ec)
5432 if (requested_base_type == null) {
5433 Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
5437 StringBuilder array_qualifier = new StringBuilder (rank);
5440 // `In the first form allocates an array instace of the type that results
5441 // from deleting each of the individual expression from the expression list'
5443 if (num_arguments > 0) {
5444 array_qualifier.Append ("[");
5445 for (int i = num_arguments-1; i > 0; i--)
5446 array_qualifier.Append (",");
5447 array_qualifier.Append ("]");
5453 TypeExpr array_type_expr;
5454 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
5455 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
5456 if (array_type_expr == null)
5459 type = array_type_expr.Type;
5460 array_element_type = TypeManager.GetElementType (type);
5461 dimensions = type.GetArrayRank ();
5466 public override Expression DoResolve (EmitContext ec)
5471 if (!ResolveArrayType (ec))
5474 if ((array_element_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
5475 Report.Error (719, loc, "`{0}': array elements cannot be of static type",
5476 TypeManager.CSharpName (array_element_type));
5480 // First step is to validate the initializers and fill
5481 // in any missing bits
5483 if (!ResolveInitializers (ec))
5486 if (arguments.Count != dimensions) {
5487 Error_IncorrectArrayInitializer ();
5490 foreach (Argument a in arguments){
5491 if (!a.Resolve (ec, loc))
5494 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
5497 eclass = ExprClass.Value;
5501 MethodInfo GetArrayMethod (int arguments)
5503 ModuleBuilder mb = CodeGen.Module.Builder;
5505 Type[] arg_types = new Type[arguments];
5506 for (int i = 0; i < arguments; i++)
5507 arg_types[i] = TypeManager.int32_type;
5509 MethodInfo mi = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
5513 Report.Error (-6, "New invocation: Can not find a constructor for " +
5514 "this argument list");
5521 byte [] MakeByteBlob ()
5526 int count = array_data.Count;
5528 if (array_element_type.IsEnum)
5529 array_element_type = TypeManager.EnumToUnderlying (array_element_type);
5531 factor = GetTypeSize (array_element_type);
5533 throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type);
5535 data = new byte [(count * factor + 3) & ~3];
5538 for (int i = 0; i < count; ++i) {
5539 object v = array_data [i];
5541 if (v is EnumConstant)
5542 v = ((EnumConstant) v).Child;
5544 if (v is Constant && !(v is StringConstant))
5545 v = ((Constant) v).GetValue ();
5551 if (array_element_type == TypeManager.int64_type){
5552 if (!(v is Expression)){
5553 long val = (long) v;
5555 for (int j = 0; j < factor; ++j) {
5556 data [idx + j] = (byte) (val & 0xFF);
5560 } else if (array_element_type == TypeManager.uint64_type){
5561 if (!(v is Expression)){
5562 ulong val = (ulong) v;
5564 for (int j = 0; j < factor; ++j) {
5565 data [idx + j] = (byte) (val & 0xFF);
5569 } else if (array_element_type == TypeManager.float_type) {
5570 if (!(v is Expression)){
5571 element = BitConverter.GetBytes ((float) v);
5573 for (int j = 0; j < factor; ++j)
5574 data [idx + j] = element [j];
5575 if (!BitConverter.IsLittleEndian)
5576 System.Array.Reverse (data, idx, 4);
5578 } else if (array_element_type == TypeManager.double_type) {
5579 if (!(v is Expression)){
5580 element = BitConverter.GetBytes ((double) v);
5582 for (int j = 0; j < factor; ++j)
5583 data [idx + j] = element [j];
5585 // FIXME: Handle the ARM float format.
5586 if (!BitConverter.IsLittleEndian)
5587 System.Array.Reverse (data, idx, 8);
5589 } else if (array_element_type == TypeManager.char_type){
5590 if (!(v is Expression)){
5591 int val = (int) ((char) v);
5593 data [idx] = (byte) (val & 0xff);
5594 data [idx+1] = (byte) (val >> 8);
5596 } else if (array_element_type == TypeManager.short_type){
5597 if (!(v is Expression)){
5598 int val = (int) ((short) v);
5600 data [idx] = (byte) (val & 0xff);
5601 data [idx+1] = (byte) (val >> 8);
5603 } else if (array_element_type == TypeManager.ushort_type){
5604 if (!(v is Expression)){
5605 int val = (int) ((ushort) v);
5607 data [idx] = (byte) (val & 0xff);
5608 data [idx+1] = (byte) (val >> 8);
5610 } else if (array_element_type == TypeManager.int32_type) {
5611 if (!(v is Expression)){
5614 data [idx] = (byte) (val & 0xff);
5615 data [idx+1] = (byte) ((val >> 8) & 0xff);
5616 data [idx+2] = (byte) ((val >> 16) & 0xff);
5617 data [idx+3] = (byte) (val >> 24);
5619 } else if (array_element_type == TypeManager.uint32_type) {
5620 if (!(v is Expression)){
5621 uint val = (uint) v;
5623 data [idx] = (byte) (val & 0xff);
5624 data [idx+1] = (byte) ((val >> 8) & 0xff);
5625 data [idx+2] = (byte) ((val >> 16) & 0xff);
5626 data [idx+3] = (byte) (val >> 24);
5628 } else if (array_element_type == TypeManager.sbyte_type) {
5629 if (!(v is Expression)){
5630 sbyte val = (sbyte) v;
5631 data [idx] = (byte) val;
5633 } else if (array_element_type == TypeManager.byte_type) {
5634 if (!(v is Expression)){
5635 byte val = (byte) v;
5636 data [idx] = (byte) val;
5638 } else if (array_element_type == TypeManager.bool_type) {
5639 if (!(v is Expression)){
5640 bool val = (bool) v;
5641 data [idx] = (byte) (val ? 1 : 0);
5643 } else if (array_element_type == TypeManager.decimal_type){
5644 if (!(v is Expression)){
5645 int [] bits = Decimal.GetBits ((decimal) v);
5648 // FIXME: For some reason, this doesn't work on the MS runtime.
5649 int [] nbits = new int [4];
5650 nbits [0] = bits [3];
5651 nbits [1] = bits [2];
5652 nbits [2] = bits [0];
5653 nbits [3] = bits [1];
5655 for (int j = 0; j < 4; j++){
5656 data [p++] = (byte) (nbits [j] & 0xff);
5657 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
5658 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
5659 data [p++] = (byte) (nbits [j] >> 24);
5663 throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type);
5672 // Emits the initializers for the array
5674 void EmitStaticInitializers (EmitContext ec)
5677 // First, the static data
5680 ILGenerator ig = ec.ig;
5682 byte [] data = MakeByteBlob ();
5684 fb = RootContext.MakeStaticData (data);
5686 ig.Emit (OpCodes.Dup);
5687 ig.Emit (OpCodes.Ldtoken, fb);
5688 ig.Emit (OpCodes.Call,
5689 TypeManager.void_initializearray_array_fieldhandle);
5693 // Emits pieces of the array that can not be computed at compile
5694 // time (variables and string locations).
5696 // This always expect the top value on the stack to be the array
5698 void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
5700 ILGenerator ig = ec.ig;
5701 int dims = bounds.Count;
5702 int [] current_pos = new int [dims];
5704 MethodInfo set = null;
5707 Type [] args = new Type [dims + 1];
5709 for (int j = 0; j < dims; j++)
5710 args [j] = TypeManager.int32_type;
5711 args [dims] = array_element_type;
5713 set = CodeGen.Module.Builder.GetArrayMethod (
5715 CallingConventions.HasThis | CallingConventions.Standard,
5716 TypeManager.void_type, args);
5719 for (int i = 0; i < array_data.Count; i++){
5721 Expression e = (Expression)array_data [i];
5723 // Constant can be initialized via StaticInitializer
5724 if (e != null && !(!emitConstants && e is Constant)) {
5725 Type etype = e.Type;
5727 ig.Emit (OpCodes.Dup);
5729 for (int idx = 0; idx < dims; idx++)
5730 IntConstant.EmitInt (ig, current_pos [idx]);
5733 // If we are dealing with a struct, get the
5734 // address of it, so we can store it.
5736 if ((dims == 1) && etype.IsValueType &&
5737 (!TypeManager.IsBuiltinOrEnum (etype) ||
5738 etype == TypeManager.decimal_type)) {
5743 // Let new know that we are providing
5744 // the address where to store the results
5746 n.DisableTemporaryValueType ();
5749 ig.Emit (OpCodes.Ldelema, etype);
5755 bool is_stobj, has_type_arg;
5756 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj, out has_type_arg);
5758 ig.Emit (OpCodes.Stobj, etype);
5759 else if (has_type_arg)
5760 ig.Emit (op, etype);
5764 ig.Emit (OpCodes.Call, set);
5771 for (int j = dims - 1; j >= 0; j--){
5773 if (current_pos [j] < (int) bounds [j])
5775 current_pos [j] = 0;
5780 public override void Emit (EmitContext ec)
5782 ILGenerator ig = ec.ig;
5784 foreach (Argument a in arguments)
5787 if (arguments.Count == 1)
5788 ig.Emit (OpCodes.Newarr, array_element_type);
5790 ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
5793 if (initializers == null)
5796 // Emit static initializer for arrays which have contain more than 4 items and
5797 // the static initializer will initialize at least 25% of array values.
5798 // NOTE: const_initializers_count does not contain default constant values.
5799 if (const_initializers_count >= 4 && const_initializers_count * 4 > (array_data.Count) &&
5800 TypeManager.IsPrimitiveType (array_element_type)) {
5801 EmitStaticInitializers (ec);
5803 if (!only_constant_initializers)
5804 EmitDynamicInitializers (ec, false);
5806 EmitDynamicInitializers (ec, true);
5810 public override bool GetAttributableValue (Type value_type, out object value)
5812 if (arguments.Count != 1) {
5813 // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
5814 return base.GetAttributableValue (null, out value);
5817 if (array_data == null) {
5818 Constant c = (Constant)((Argument)arguments [0]).Expr;
5819 if (c.IsDefaultValue) {
5820 value = Array.CreateInstance (array_element_type, 0);
5823 // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
5824 return base.GetAttributableValue (null, out value);
5827 Array ret = Array.CreateInstance (array_element_type, array_data.Count);
5828 object element_value;
5829 for (int i = 0; i < ret.Length; ++i)
5831 Expression e = (Expression)array_data [i];
5833 // Is null when an initializer is optimized (value == predefined value)
5837 if (!e.GetAttributableValue (array_element_type, out element_value)) {
5841 ret.SetValue (element_value, i);
5847 protected override void CloneTo (CloneContext clonectx, Expression t)
5849 ArrayCreation target = (ArrayCreation) t;
5851 if (requested_base_type != null)
5852 target.requested_base_type = requested_base_type.Clone (clonectx);
5854 if (arguments != null){
5855 target.arguments = new ArrayList (arguments.Count);
5856 foreach (Argument a in arguments)
5857 target.arguments.Add (a.Clone (clonectx));
5860 if (initializers != null){
5861 target.initializers = new ArrayList (initializers.Count);
5862 foreach (Expression initializer in initializers)
5863 target.initializers.Add (initializer.Clone (clonectx));
5869 // Represents an implicitly typed array epxression
5871 public class ImplicitlyTypedArrayCreation : ArrayCreation
5873 public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
5874 : base (null, rank, initializers, loc)
5876 if (RootContext.Version <= LanguageVersion.ISO_2)
5877 Report.FeatureIsNotAvailable (loc, "implicitly typed arrays");
5879 if (rank.Length > 2) {
5880 while (rank [++dimensions] == ',');
5886 public override Expression DoResolve (EmitContext ec)
5891 if (!ResolveInitializers (ec))
5894 if (array_element_type == null || array_element_type == TypeManager.null_type ||
5895 array_element_type == TypeManager.void_type || array_element_type == TypeManager.anonymous_method_type ||
5896 arguments.Count != dimensions) {
5897 Report.Error (826, loc, "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
5902 // At this point we found common base type for all initializer elements
5903 // but we have to be sure that all static initializer elements are of
5906 UnifyInitializerElement (ec);
5908 type = TypeManager.GetConstructedType (array_element_type, rank);
5909 eclass = ExprClass.Value;
5914 // Converts static initializer only
5916 void UnifyInitializerElement (EmitContext ec)
5918 for (int i = 0; i < array_data.Count; ++i) {
5919 Expression e = (Expression)array_data[i];
5921 array_data [i] = Convert.ImplicitConversionStandard (ec, e, array_element_type, Location.Null);
5925 protected override Expression ResolveArrayElement (EmitContext ec, Expression element)
5927 element = element.Resolve (ec);
5928 if (element == null)
5931 if (array_element_type == null) {
5932 array_element_type = element.Type;
5936 if (Convert.ImplicitStandardConversionExists (element, array_element_type)) {
5940 if (Convert.ImplicitStandardConversionExists (new TypeExpression (array_element_type, loc), element.Type)) {
5941 array_element_type = element.Type;
5945 element.Error_ValueCannotBeConverted (ec, element.Location, array_element_type, false);
5950 public sealed class CompilerGeneratedThis : This
5952 public static This Instance = new CompilerGeneratedThis ();
5954 private CompilerGeneratedThis ()
5955 : base (Location.Null)
5959 public override Expression DoResolve (EmitContext ec)
5961 eclass = ExprClass.Variable;
5962 type = ec.ContainerType;
5963 variable = new SimpleThis (type);
5969 /// Represents the `this' construct
5972 public class This : VariableReference, IVariable
5975 VariableInfo variable_info;
5976 protected Variable variable;
5979 public This (Block block, Location loc)
5985 public This (Location loc)
5990 public VariableInfo VariableInfo {
5991 get { return variable_info; }
5994 public bool VerifyFixed ()
5996 return !TypeManager.IsValueType (Type);
5999 public override bool IsRef {
6000 get { return is_struct; }
6003 public override Variable Variable {
6004 get { return variable; }
6007 public bool ResolveBase (EmitContext ec)
6009 eclass = ExprClass.Variable;
6011 if (ec.TypeContainer.CurrentType != null)
6012 type = ec.TypeContainer.CurrentType;
6014 type = ec.ContainerType;
6016 is_struct = ec.TypeContainer is Struct;
6019 Error (26, "Keyword `this' is not valid in a static property, " +
6020 "static method, or static field initializer");
6024 if (block != null) {
6025 if (block.Toplevel.ThisVariable != null)
6026 variable_info = block.Toplevel.ThisVariable.VariableInfo;
6028 AnonymousContainer am = ec.CurrentAnonymousMethod;
6029 if (is_struct && (am != null) && !am.IsIterator) {
6030 Report.Error (1673, loc, "Anonymous methods inside structs " +
6031 "cannot access instance members of `this'. " +
6032 "Consider copying `this' to a local variable " +
6033 "outside the anonymous method and using the " +
6037 RootScopeInfo host = block.Toplevel.RootScope;
6038 if ((host != null) && !ec.IsConstructor &&
6039 (!is_struct || host.IsIterator)) {
6040 variable = host.CaptureThis ();
6041 type = variable.Type;
6046 if (variable == null)
6047 variable = new SimpleThis (type);
6053 // Called from Invocation to check if the invocation is correct
6055 public override void CheckMarshalByRefAccess (EmitContext ec)
6057 if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) &&
6058 !variable_info.IsAssigned (ec)) {
6059 Error (188, "The `this' object cannot be used before all of its " +
6060 "fields are assigned to");
6061 variable_info.SetAssigned (ec);
6065 public override Expression CreateExpressionTree (EmitContext ec)
6067 ArrayList args = new ArrayList (2);
6068 args.Add (new Argument (this));
6069 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6070 return CreateExpressionFactoryCall ("Constant", args);
6073 public override Expression DoResolve (EmitContext ec)
6075 if (!ResolveBase (ec))
6079 if (ec.IsInFieldInitializer) {
6080 Error (27, "Keyword `this' is not available in the current context");
6087 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6089 if (!ResolveBase (ec))
6092 if (variable_info != null)
6093 variable_info.SetAssigned (ec);
6095 if (ec.TypeContainer is Class){
6096 Error (1604, "Cannot assign to 'this' because it is read-only");
6102 public override int GetHashCode()
6104 return block.GetHashCode ();
6107 public override bool Equals (object obj)
6109 This t = obj as This;
6113 return block == t.block;
6116 protected class SimpleThis : Variable
6120 public SimpleThis (Type type)
6125 public override Type Type {
6126 get { return type; }
6129 public override bool HasInstance {
6130 get { return false; }
6133 public override bool NeedsTemporary {
6134 get { return false; }
6137 public override void EmitInstance (EmitContext ec)
6142 public override void Emit (EmitContext ec)
6144 ec.ig.Emit (OpCodes.Ldarg_0);
6147 public override void EmitAssign (EmitContext ec)
6149 throw new InvalidOperationException ();
6152 public override void EmitAddressOf (EmitContext ec)
6154 ec.ig.Emit (OpCodes.Ldarg_0);
6158 protected override void CloneTo (CloneContext clonectx, Expression t)
6160 This target = (This) t;
6162 target.block = clonectx.LookupBlock (block);
6167 /// Represents the `__arglist' construct
6169 public class ArglistAccess : Expression
6171 public ArglistAccess (Location loc)
6176 public override Expression DoResolve (EmitContext ec)
6178 eclass = ExprClass.Variable;
6179 type = TypeManager.runtime_argument_handle_type;
6181 if (ec.IsInFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs)
6183 Error (190, "The __arglist construct is valid only within " +
6184 "a variable argument method");
6191 public override void Emit (EmitContext ec)
6193 ec.ig.Emit (OpCodes.Arglist);
6196 protected override void CloneTo (CloneContext clonectx, Expression target)
6203 /// Represents the `__arglist (....)' construct
6205 public class Arglist : Expression
6207 Argument[] Arguments;
6209 public Arglist (Location loc)
6210 : this (Argument.Empty, loc)
6214 public Arglist (Argument[] args, Location l)
6220 public Type[] ArgumentTypes {
6222 Type[] retval = new Type [Arguments.Length];
6223 for (int i = 0; i < Arguments.Length; i++)
6224 retval [i] = Arguments [i].Type;
6229 public override Expression CreateExpressionTree (EmitContext ec)
6231 Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
6235 public override Expression DoResolve (EmitContext ec)
6237 eclass = ExprClass.Variable;
6238 type = TypeManager.runtime_argument_handle_type;
6240 foreach (Argument arg in Arguments) {
6241 if (!arg.Resolve (ec, loc))
6248 public override void Emit (EmitContext ec)
6250 foreach (Argument arg in Arguments)
6254 protected override void CloneTo (CloneContext clonectx, Expression t)
6256 Arglist target = (Arglist) t;
6258 target.Arguments = new Argument [Arguments.Length];
6259 for (int i = 0; i < Arguments.Length; i++)
6260 target.Arguments [i] = Arguments [i].Clone (clonectx);
6265 // This produces the value that renders an instance, used by the iterators code
6267 public class ProxyInstance : Expression, IMemoryLocation {
6268 public override Expression DoResolve (EmitContext ec)
6270 eclass = ExprClass.Variable;
6271 type = ec.ContainerType;
6275 public override void Emit (EmitContext ec)
6277 ec.ig.Emit (OpCodes.Ldarg_0);
6281 public void AddressOf (EmitContext ec, AddressOp mode)
6283 ec.ig.Emit (OpCodes.Ldarg_0);
6288 /// Implements the typeof operator
6290 public class TypeOf : Expression {
6291 Expression QueriedType;
6292 protected Type typearg;
6294 public TypeOf (Expression queried_type, Location l)
6296 QueriedType = queried_type;
6300 public override Expression DoResolve (EmitContext ec)
6302 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6306 typearg = texpr.Type;
6308 if (typearg == TypeManager.void_type) {
6309 Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6313 if (typearg.IsPointer && !ec.InUnsafe){
6318 type = TypeManager.type_type;
6319 // Even though what is returned is a type object, it's treated as a value by the compiler.
6320 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6321 eclass = ExprClass.Value;
6325 public override void Emit (EmitContext ec)
6327 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6328 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6331 public override bool GetAttributableValue (Type value_type, out object value)
6333 if (TypeManager.ContainsGenericParameters (typearg) &&
6334 !TypeManager.IsGenericTypeDefinition (typearg)) {
6335 Report.SymbolRelatedToPreviousError (typearg);
6336 Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6337 TypeManager.CSharpName (typearg));
6342 if (value_type == TypeManager.object_type) {
6343 value = (object)typearg;
6350 public Type TypeArgument
6358 protected override void CloneTo (CloneContext clonectx, Expression t)
6360 TypeOf target = (TypeOf) t;
6362 target.QueriedType = QueriedType.Clone (clonectx);
6367 /// Implements the `typeof (void)' operator
6369 public class TypeOfVoid : TypeOf {
6370 public TypeOfVoid (Location l) : base (null, l)
6375 public override Expression DoResolve (EmitContext ec)
6377 type = TypeManager.type_type;
6378 typearg = TypeManager.void_type;
6379 // See description in TypeOf.
6380 eclass = ExprClass.Value;
6385 internal class TypeOfMethod : Expression
6387 readonly MethodGroupExpr method;
6388 static MethodInfo get_type_from_handle;
6390 static TypeOfMethod ()
6392 get_type_from_handle = typeof (MethodBase).GetMethod ("GetMethodFromHandle",
6393 new Type [] { TypeManager.runtime_method_handle_type });
6396 public TypeOfMethod (MethodGroupExpr method)
6398 this.method = method;
6399 loc = method.Location;
6402 public override Expression DoResolve (EmitContext ec)
6404 type = typeof (MethodBase);
6405 eclass = ExprClass.Value;
6409 public override void Emit (EmitContext ec)
6411 ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo)method);
6412 ec.ig.Emit (OpCodes.Call, get_type_from_handle);
6417 /// Implements the sizeof expression
6419 public class SizeOf : Expression {
6420 readonly Expression QueriedType;
6423 public SizeOf (Expression queried_type, Location l)
6425 this.QueriedType = queried_type;
6429 public override Expression DoResolve (EmitContext ec)
6431 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6436 if (texpr is TypeParameterExpr){
6437 ((TypeParameterExpr)texpr).Error_CannotUseAsUnmanagedType (loc);
6442 type_queried = texpr.Type;
6443 if (type_queried.IsEnum)
6444 type_queried = TypeManager.EnumToUnderlying (type_queried);
6446 if (type_queried == TypeManager.void_type) {
6447 Expression.Error_VoidInvalidInTheContext (loc);
6451 int size_of = GetTypeSize (type_queried);
6453 return new IntConstant (size_of, loc);
6457 Report.Error (233, loc, "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
6458 TypeManager.CSharpName (type_queried));
6462 if (!TypeManager.VerifyUnManaged (type_queried, loc)){
6466 type = TypeManager.int32_type;
6467 eclass = ExprClass.Value;
6471 public override void Emit (EmitContext ec)
6473 int size = GetTypeSize (type_queried);
6476 ec.ig.Emit (OpCodes.Sizeof, type_queried);
6478 IntConstant.EmitInt (ec.ig, size);
6481 protected override void CloneTo (CloneContext clonectx, Expression t)
6487 /// Implements the qualified-alias-member (::) expression.
6489 public class QualifiedAliasMember : Expression
6491 string alias, identifier;
6493 public QualifiedAliasMember (string alias, string identifier, Location l)
6496 this.identifier = identifier;
6500 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
6502 if (alias == "global")
6503 return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
6505 int errors = Report.Errors;
6506 FullNamedExpression fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
6508 if (errors == Report.Errors)
6509 Report.Error (432, loc, "Alias `{0}' not found", alias);
6512 if (fne.eclass != ExprClass.Namespace) {
6514 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
6517 return new MemberAccess (fne, identifier).ResolveAsTypeStep (ec, silent);
6520 public override Expression DoResolve (EmitContext ec)
6522 FullNamedExpression fne;
6523 if (alias == "global") {
6524 fne = RootNamespace.Global;
6526 int errors = Report.Errors;
6527 fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
6529 if (errors == Report.Errors)
6530 Report.Error (432, loc, "Alias `{0}' not found", alias);
6535 Expression retval = new MemberAccess (fne, identifier).DoResolve (ec);
6539 if (!(retval is FullNamedExpression)) {
6540 Report.Error (687, loc, "The expression `{0}::{1}' did not resolve to a namespace or a type", alias, identifier);
6544 // We defer this check till the end to match the behaviour of CSC
6545 if (fne.eclass != ExprClass.Namespace) {
6546 Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
6552 public override void Emit (EmitContext ec)
6554 throw new InternalErrorException ("QualifiedAliasMember found in resolved tree");
6558 public override string ToString ()
6560 return alias + "::" + identifier;
6563 public override string GetSignatureForError ()
6568 protected override void CloneTo (CloneContext clonectx, Expression t)
6575 /// Implements the member access expression
6577 public class MemberAccess : Expression {
6578 public readonly string Identifier;
6580 readonly TypeArguments args;
6582 public MemberAccess (Expression expr, string id)
6583 : this (expr, id, expr.Location)
6587 public MemberAccess (Expression expr, string identifier, Location loc)
6590 Identifier = identifier;
6594 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
6595 : this (expr, identifier, loc)
6600 protected string LookupIdentifier {
6601 get { return MemberName.MakeName (Identifier, args); }
6604 // TODO: this method has very poor performace for Enum fields and
6605 // probably for other constants as well
6606 Expression DoResolve (EmitContext ec, Expression right_side)
6609 throw new Exception ();
6612 // Resolve the expression with flow analysis turned off, we'll do the definite
6613 // assignment checks later. This is because we don't know yet what the expression
6614 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
6615 // definite assignment check on the actual field and not on the whole struct.
6618 SimpleName original = expr as SimpleName;
6619 Expression expr_resolved = expr.Resolve (ec,
6620 ResolveFlags.VariableOrValue | ResolveFlags.Type |
6621 ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
6623 if (expr_resolved == null)
6626 if (expr_resolved is Namespace) {
6627 Namespace ns = (Namespace) expr_resolved;
6628 FullNamedExpression retval = ns.Lookup (ec.DeclContainer, LookupIdentifier, loc);
6630 if ((retval != null) && (args != null))
6631 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec, false);
6635 ns.Error_NamespaceDoesNotExist (ec.DeclContainer, loc, Identifier);
6639 Type expr_type = expr_resolved.Type;
6640 if (expr_type.IsPointer || expr_type == TypeManager.void_type || expr_resolved is NullLiteral){
6641 Unary.Error_OperatorCannotBeApplied (loc, ".", expr_type);
6644 if (expr_type == TypeManager.anonymous_method_type){
6645 Unary.Error_OperatorCannotBeApplied (loc, ".", "anonymous method");
6649 Constant c = expr_resolved as Constant;
6650 if (c != null && c.GetValue () == null) {
6651 Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
6652 "System.NullReferenceException");
6656 if (!args.Resolve (ec))
6660 Expression member_lookup;
6661 member_lookup = MemberLookup (
6662 ec.ContainerType, expr_type, expr_type, Identifier, loc);
6664 if ((member_lookup == null) && (args != null)) {
6665 member_lookup = MemberLookup (
6666 ec.ContainerType, expr_type, expr_type, LookupIdentifier, loc);
6669 if (member_lookup == null) {
6670 ExprClass expr_eclass = expr_resolved.eclass;
6673 // Extension methods are not allowed on all expression types
6675 if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
6676 expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
6677 expr_eclass == ExprClass.EventAccess) {
6678 ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (expr_type, Identifier);
6679 if (ex_method_lookup != null) {
6680 ex_method_lookup.ExtensionExpression = expr_resolved;
6683 ex_method_lookup.SetTypeArguments (args);
6686 return ex_method_lookup.DoResolve (ec);
6690 expr = expr_resolved;
6691 Error_MemberLookupFailed (
6692 ec.ContainerType, expr_type, expr_type, Identifier, null,
6693 AllMemberTypes, AllBindingFlags);
6697 TypeExpr texpr = member_lookup as TypeExpr;
6698 if (texpr != null) {
6699 if (!(expr_resolved is TypeExpr) &&
6700 (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
6701 Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
6702 Identifier, member_lookup.GetSignatureForError ());
6706 if (!texpr.CheckAccessLevel (ec.DeclContainer)) {
6707 Report.SymbolRelatedToPreviousError (member_lookup.Type);
6708 ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type));
6713 ConstructedType ct = expr_resolved as ConstructedType;
6716 // When looking up a nested type in a generic instance
6717 // via reflection, we always get a generic type definition
6718 // and not a generic instance - so we have to do this here.
6720 // See gtest-172-lib.cs and gtest-172.cs for an example.
6722 ct = new ConstructedType (
6723 member_lookup.Type, ct.TypeArguments, loc);
6725 return ct.ResolveAsTypeStep (ec, false);
6728 return member_lookup;
6731 MemberExpr me = (MemberExpr) member_lookup;
6732 me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
6737 me.SetTypeArguments (args);
6740 if (original != null && !TypeManager.IsValueType (expr_type)) {
6741 if (me.IsInstance) {
6742 LocalVariableReference var = expr_resolved as LocalVariableReference;
6743 if (var != null && !var.VerifyAssigned (ec))
6748 // The following DoResolve/DoResolveLValue will do the definite assignment
6751 if (right_side != null)
6752 return me.DoResolveLValue (ec, right_side);
6754 return me.DoResolve (ec);
6757 public override Expression DoResolve (EmitContext ec)
6759 return DoResolve (ec, null);
6762 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
6764 return DoResolve (ec, right_side);
6767 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
6769 return ResolveNamespaceOrType (ec, silent);
6772 public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
6774 FullNamedExpression new_expr = expr.ResolveAsTypeStep (rc, silent);
6776 if (new_expr == null)
6779 if (new_expr is Namespace) {
6780 Namespace ns = (Namespace) new_expr;
6781 FullNamedExpression retval = ns.Lookup (rc.DeclContainer, LookupIdentifier, loc);
6783 if ((retval != null) && (args != null))
6784 retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (rc, false);
6786 if (!silent && retval == null)
6787 ns.Error_NamespaceDoesNotExist (rc.DeclContainer, loc, LookupIdentifier);
6791 TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (rc, false);
6792 if (tnew_expr == null)
6795 Type expr_type = tnew_expr.Type;
6797 if (expr_type.IsPointer){
6798 Error (23, "The `.' operator can not be applied to pointer operands (" +
6799 TypeManager.CSharpName (expr_type) + ")");
6803 Expression member_lookup = MemberLookup (
6804 rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
6805 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
6806 if (member_lookup == null) {
6810 member_lookup = MemberLookup (
6811 rc.DeclContainer.TypeBuilder, expr_type, expr_type, SimpleName.RemoveGenericArity (LookupIdentifier),
6812 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
6814 if (member_lookup != null) {
6815 tnew_expr = member_lookup.ResolveAsTypeTerminal (rc, false);
6816 if (tnew_expr == null)
6819 Namespace.Error_TypeArgumentsCannotBeUsed (tnew_expr.Type, loc);
6823 member_lookup = MemberLookup (
6824 rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
6825 MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
6827 if (member_lookup == null) {
6828 Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
6829 Identifier, new_expr.GetSignatureForError ());
6831 // TODO: Report.SymbolRelatedToPreviousError
6832 member_lookup.Error_UnexpectedKind (null, "type", loc);
6837 TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
6842 TypeArguments the_args = args;
6843 Type declaring_type = texpr.Type.DeclaringType;
6844 if (TypeManager.HasGenericArguments (declaring_type)) {
6845 while (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (expr_type), declaring_type)) {
6846 expr_type = expr_type.BaseType;
6849 TypeArguments new_args = new TypeArguments (loc);
6850 foreach (Type decl in TypeManager.GetTypeArguments (expr_type))
6851 new_args.Add (new TypeExpression (decl, loc));
6854 new_args.Add (args);
6856 the_args = new_args;
6859 if (the_args != null) {
6860 ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
6861 return ctype.ResolveAsTypeStep (rc, false);
6868 public override void Emit (EmitContext ec)
6870 throw new Exception ("Should not happen");
6873 protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
6875 if (RootContext.Version > LanguageVersion.ISO_2 &&
6876 ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
6877 Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
6878 "extension method `{1}' of type `{0}' could be found " +
6879 "(are you missing a using directive or an assembly reference?)",
6880 TypeManager.CSharpName (type), name);
6884 base.Error_TypeDoesNotContainDefinition (type, name);
6887 public override string ToString ()
6889 return expr + "." + MemberName.MakeName (Identifier, args);
6892 public override string GetSignatureForError ()
6894 return expr.GetSignatureForError () + "." + Identifier;
6897 protected override void CloneTo (CloneContext clonectx, Expression t)
6899 MemberAccess target = (MemberAccess) t;
6901 target.expr = expr.Clone (clonectx);
6906 /// Implements checked expressions
6908 public class CheckedExpr : Expression {
6910 public Expression Expr;
6912 public CheckedExpr (Expression e, Location l)
6918 public override Expression DoResolve (EmitContext ec)
6920 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6921 Expr = Expr.Resolve (ec);
6926 if (Expr is Constant)
6929 eclass = Expr.eclass;
6934 public override void Emit (EmitContext ec)
6936 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6940 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
6942 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
6943 Expr.EmitBranchable (ec, target, on_true);
6946 protected override void CloneTo (CloneContext clonectx, Expression t)
6948 CheckedExpr target = (CheckedExpr) t;
6950 target.Expr = Expr.Clone (clonectx);
6955 /// Implements the unchecked expression
6957 public class UnCheckedExpr : Expression {
6959 public Expression Expr;
6961 public UnCheckedExpr (Expression e, Location l)
6967 public override Expression DoResolve (EmitContext ec)
6969 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
6970 Expr = Expr.Resolve (ec);
6975 if (Expr is Constant)
6978 eclass = Expr.eclass;
6983 public override void Emit (EmitContext ec)
6985 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
6989 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
6991 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
6992 Expr.EmitBranchable (ec, target, on_true);
6995 protected override void CloneTo (CloneContext clonectx, Expression t)
6997 UnCheckedExpr target = (UnCheckedExpr) t;
6999 target.Expr = Expr.Clone (clonectx);
7004 /// An Element Access expression.
7006 /// During semantic analysis these are transformed into
7007 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7009 public class ElementAccess : Expression {
7010 public ArrayList Arguments;
7011 public Expression Expr;
7013 public ElementAccess (Expression e, ArrayList e_list)
7022 Arguments = new ArrayList ();
7023 foreach (Expression tmp in e_list)
7024 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7028 bool CommonResolve (EmitContext ec)
7030 Expr = Expr.Resolve (ec);
7032 if (Arguments == null)
7035 foreach (Argument a in Arguments){
7036 if (!a.Resolve (ec, loc))
7040 return Expr != null;
7043 public override Expression CreateExpressionTree (EmitContext ec)
7045 ArrayList args = new ArrayList (Arguments.Count + 1);
7046 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
7047 foreach (Argument a in Arguments)
7048 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
7050 return CreateExpressionFactoryCall ("ArrayIndex", args);
7053 Expression MakePointerAccess (EmitContext ec, Type t)
7055 if (t == TypeManager.void_ptr_type){
7056 Error (242, "The array index operation is not valid on void pointers");
7059 if (Arguments.Count != 1){
7060 Error (196, "A pointer must be indexed by only one value");
7065 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
7068 return new Indirection (p, loc).Resolve (ec);
7071 public override Expression DoResolve (EmitContext ec)
7073 if (!CommonResolve (ec))
7077 // We perform some simple tests, and then to "split" the emit and store
7078 // code we create an instance of a different class, and return that.
7080 // I am experimenting with this pattern.
7084 if (t == TypeManager.array_type){
7085 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
7090 return (new ArrayAccess (this, loc)).Resolve (ec);
7092 return MakePointerAccess (ec, t);
7094 FieldExpr fe = Expr as FieldExpr;
7096 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7098 return MakePointerAccess (ec, ff.ElementType);
7101 return (new IndexerAccess (this, loc)).Resolve (ec);
7104 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7106 if (!CommonResolve (ec))
7111 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
7114 return MakePointerAccess (ec, type);
7116 if (Expr.eclass != ExprClass.Variable && type.IsValueType)
7117 Error_CannotModifyIntermediateExpressionValue (ec);
7119 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
7122 public override void Emit (EmitContext ec)
7124 throw new Exception ("Should never be reached");
7127 public override string GetSignatureForError ()
7129 return Expr.GetSignatureForError ();
7132 protected override void CloneTo (CloneContext clonectx, Expression t)
7134 ElementAccess target = (ElementAccess) t;
7136 target.Expr = Expr.Clone (clonectx);
7137 target.Arguments = new ArrayList (Arguments.Count);
7138 foreach (Argument a in Arguments)
7139 target.Arguments.Add (a.Clone (clonectx));
7144 /// Implements array access
7146 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7148 // Points to our "data" repository
7152 LocalTemporary temp;
7153 LocalTemporary prepared_value;
7157 public ArrayAccess (ElementAccess ea_data, Location l)
7160 eclass = ExprClass.Variable;
7164 public override Expression CreateExpressionTree (EmitContext ec)
7166 return ea.CreateExpressionTree (ec);
7169 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7171 return DoResolve (ec);
7174 public override Expression DoResolve (EmitContext ec)
7177 ExprClass eclass = ea.Expr.eclass;
7179 // As long as the type is valid
7180 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7181 eclass == ExprClass.Value)) {
7182 ea.Expr.Error_UnexpectedKind ("variable or value");
7187 Type t = ea.Expr.Type;
7188 if (t.GetArrayRank () != ea.Arguments.Count) {
7189 Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7190 ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
7194 type = TypeManager.GetElementType (t);
7195 if (type.IsPointer && !ec.InUnsafe) {
7196 UnsafeError (ea.Location);
7200 foreach (Argument a in ea.Arguments) {
7201 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
7204 eclass = ExprClass.Variable;
7210 /// Emits the right opcode to load an object of Type `t'
7211 /// from an array of T
7213 void EmitLoadOpcode (ILGenerator ig, Type type, int rank)
7216 MethodInfo get = FetchGetMethod ();
7217 ig.Emit (OpCodes.Call, get);
7221 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7222 ig.Emit (OpCodes.Ldelem_U1);
7223 else if (type == TypeManager.sbyte_type)
7224 ig.Emit (OpCodes.Ldelem_I1);
7225 else if (type == TypeManager.short_type)
7226 ig.Emit (OpCodes.Ldelem_I2);
7227 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7228 ig.Emit (OpCodes.Ldelem_U2);
7229 else if (type == TypeManager.int32_type)
7230 ig.Emit (OpCodes.Ldelem_I4);
7231 else if (type == TypeManager.uint32_type)
7232 ig.Emit (OpCodes.Ldelem_U4);
7233 else if (type == TypeManager.uint64_type)
7234 ig.Emit (OpCodes.Ldelem_I8);
7235 else if (type == TypeManager.int64_type)
7236 ig.Emit (OpCodes.Ldelem_I8);
7237 else if (type == TypeManager.float_type)
7238 ig.Emit (OpCodes.Ldelem_R4);
7239 else if (type == TypeManager.double_type)
7240 ig.Emit (OpCodes.Ldelem_R8);
7241 else if (type == TypeManager.intptr_type)
7242 ig.Emit (OpCodes.Ldelem_I);
7243 else if (TypeManager.IsEnumType (type)){
7244 EmitLoadOpcode (ig, TypeManager.EnumToUnderlying (type), rank);
7245 } else if (type.IsValueType){
7246 ig.Emit (OpCodes.Ldelema, type);
7247 ig.Emit (OpCodes.Ldobj, type);
7249 } else if (type.IsGenericParameter) {
7250 ig.Emit (OpCodes.Ldelem, type);
7252 } else if (type.IsPointer)
7253 ig.Emit (OpCodes.Ldelem_I);
7255 ig.Emit (OpCodes.Ldelem_Ref);
7258 protected override void Error_NegativeArrayIndex (Location loc)
7260 Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
7264 /// Returns the right opcode to store an object of Type `t'
7265 /// from an array of T.
7267 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
7269 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7270 has_type_arg = false; is_stobj = false;
7271 t = TypeManager.TypeToCoreType (t);
7272 if (TypeManager.IsEnumType (t))
7273 t = TypeManager.EnumToUnderlying (t);
7274 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7275 t == TypeManager.bool_type)
7276 return OpCodes.Stelem_I1;
7277 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7278 t == TypeManager.char_type)
7279 return OpCodes.Stelem_I2;
7280 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7281 return OpCodes.Stelem_I4;
7282 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7283 return OpCodes.Stelem_I8;
7284 else if (t == TypeManager.float_type)
7285 return OpCodes.Stelem_R4;
7286 else if (t == TypeManager.double_type)
7287 return OpCodes.Stelem_R8;
7288 else if (t == TypeManager.intptr_type) {
7289 has_type_arg = true;
7291 return OpCodes.Stobj;
7292 } else if (t.IsValueType) {
7293 has_type_arg = true;
7295 return OpCodes.Stobj;
7297 } else if (t.IsGenericParameter) {
7298 has_type_arg = true;
7299 return OpCodes.Stelem;
7302 } else if (t.IsPointer)
7303 return OpCodes.Stelem_I;
7305 return OpCodes.Stelem_Ref;
7308 MethodInfo FetchGetMethod ()
7310 ModuleBuilder mb = CodeGen.Module.Builder;
7311 int arg_count = ea.Arguments.Count;
7312 Type [] args = new Type [arg_count];
7315 for (int i = 0; i < arg_count; i++){
7316 //args [i++] = a.Type;
7317 args [i] = TypeManager.int32_type;
7320 get = mb.GetArrayMethod (
7321 ea.Expr.Type, "Get",
7322 CallingConventions.HasThis |
7323 CallingConventions.Standard,
7329 MethodInfo FetchAddressMethod ()
7331 ModuleBuilder mb = CodeGen.Module.Builder;
7332 int arg_count = ea.Arguments.Count;
7333 Type [] args = new Type [arg_count];
7337 ret_type = TypeManager.GetReferenceType (type);
7339 for (int i = 0; i < arg_count; i++){
7340 //args [i++] = a.Type;
7341 args [i] = TypeManager.int32_type;
7344 address = mb.GetArrayMethod (
7345 ea.Expr.Type, "Address",
7346 CallingConventions.HasThis |
7347 CallingConventions.Standard,
7354 // Load the array arguments into the stack.
7356 // If we have been requested to cache the values (cached_locations array
7357 // initialized), then load the arguments the first time and store them
7358 // in locals. otherwise load from local variables.
7360 // prepare_for_load is used in compound assignments to cache original index
7361 // values ( label[idx++] += s )
7363 LocalTemporary [] LoadArrayAndArguments (EmitContext ec, bool prepare_for_load)
7367 LocalTemporary[] indexes = null;
7368 if (prepare_for_load) {
7369 ec.ig.Emit (OpCodes.Dup);
7370 indexes = new LocalTemporary [ea.Arguments.Count];
7373 for (int i = 0; i < ea.Arguments.Count; ++i) {
7374 ((Argument)ea.Arguments [i]).Emit (ec);
7375 if (!prepare_for_load)
7378 // Keep original array index value on the stack
7379 ec.ig.Emit (OpCodes.Dup);
7381 indexes [i] = new LocalTemporary (TypeManager.intptr_type);
7382 indexes [i].Store (ec);
7388 public void Emit (EmitContext ec, bool leave_copy)
7390 int rank = ea.Expr.Type.GetArrayRank ();
7391 ILGenerator ig = ec.ig;
7393 if (prepared_value != null) {
7394 prepared_value.Emit (ec);
7395 } else if (prepared) {
7396 LoadFromPtr (ig, this.type);
7398 LoadArrayAndArguments (ec, false);
7399 EmitLoadOpcode (ig, type, rank);
7403 ig.Emit (OpCodes.Dup);
7404 temp = new LocalTemporary (this.type);
7409 public override void Emit (EmitContext ec)
7414 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7416 int rank = ea.Expr.Type.GetArrayRank ();
7417 ILGenerator ig = ec.ig;
7418 Type t = source.Type;
7419 prepared = prepare_for_load && !(source is StringConcat);
7422 AddressOf (ec, AddressOp.LoadStore);
7423 ec.ig.Emit (OpCodes.Dup);
7425 LocalTemporary[] original_indexes_values = LoadArrayAndArguments (ec,
7426 prepare_for_load && (source is StringConcat));
7428 if (original_indexes_values != null) {
7429 prepared_value = new LocalTemporary (type);
7430 EmitLoadOpcode (ig, type, rank);
7431 prepared_value.Store (ec);
7432 foreach (LocalTemporary lt in original_indexes_values) {
7440 bool is_stobj, has_type_arg;
7441 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
7445 // The stobj opcode used by value types will need
7446 // an address on the stack, not really an array/array
7450 ig.Emit (OpCodes.Ldelema, t);
7455 ec.ig.Emit (OpCodes.Dup);
7456 temp = new LocalTemporary (this.type);
7461 StoreFromPtr (ig, t);
7463 ig.Emit (OpCodes.Stobj, t);
7464 else if (has_type_arg)
7471 ec.ig.Emit (OpCodes.Dup);
7472 temp = new LocalTemporary (this.type);
7477 StoreFromPtr (ig, t);
7479 int arg_count = ea.Arguments.Count;
7480 Type [] args = new Type [arg_count + 1];
7481 for (int i = 0; i < arg_count; i++) {
7482 //args [i++] = a.Type;
7483 args [i] = TypeManager.int32_type;
7485 args [arg_count] = type;
7487 MethodInfo set = CodeGen.Module.Builder.GetArrayMethod (
7488 ea.Expr.Type, "Set",
7489 CallingConventions.HasThis |
7490 CallingConventions.Standard,
7491 TypeManager.void_type, args);
7493 ig.Emit (OpCodes.Call, set);
7503 public void AddressOf (EmitContext ec, AddressOp mode)
7505 int rank = ea.Expr.Type.GetArrayRank ();
7506 ILGenerator ig = ec.ig;
7508 LoadArrayAndArguments (ec, false);
7511 ig.Emit (OpCodes.Ldelema, type);
7513 MethodInfo address = FetchAddressMethod ();
7514 ig.Emit (OpCodes.Call, address);
7518 public void EmitGetLength (EmitContext ec, int dim)
7520 int rank = ea.Expr.Type.GetArrayRank ();
7521 ILGenerator ig = ec.ig;
7525 ig.Emit (OpCodes.Ldlen);
7526 ig.Emit (OpCodes.Conv_I4);
7528 IntLiteral.EmitInt (ig, dim);
7529 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
7535 /// Expressions that represent an indexer call.
7537 public class IndexerAccess : Expression, IAssignMethod
7539 class IndexerMethodGroupExpr : MethodGroupExpr
7541 public IndexerMethodGroupExpr (Indexers indexers, Location loc)
7544 Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
7547 public override string Name {
7553 protected override int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
7556 // Here is the trick, decrease number of arguments by 1 when only
7557 // available property method is setter. This makes overload resolution
7558 // work correctly for indexers.
7561 if (method.Name [0] == 'g')
7562 return parameters.Count;
7564 return parameters.Count - 1;
7570 // Contains either property getter or setter
7571 public ArrayList Methods;
7572 public ArrayList Properties;
7578 void Append (Type caller_type, MemberInfo [] mi)
7583 foreach (PropertyInfo property in mi) {
7584 MethodInfo accessor = property.GetGetMethod (true);
7585 if (accessor == null)
7586 accessor = property.GetSetMethod (true);
7588 if (Methods == null) {
7589 Methods = new ArrayList ();
7590 Properties = new ArrayList ();
7593 Methods.Add (accessor);
7594 Properties.Add (property);
7598 static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
7600 string p_name = TypeManager.IndexerPropertyName (lookup_type);
7602 return TypeManager.MemberLookup (
7603 caller_type, caller_type, lookup_type, MemberTypes.Property,
7604 BindingFlags.Public | BindingFlags.Instance |
7605 BindingFlags.DeclaredOnly, p_name, null);
7608 public static Indexers GetIndexersForType (Type caller_type, Type lookup_type)
7610 Indexers ix = new Indexers ();
7613 if (lookup_type.IsGenericParameter) {
7614 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
7618 if (gc.HasClassConstraint)
7619 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
7621 Type[] ifaces = gc.InterfaceConstraints;
7622 foreach (Type itype in ifaces)
7623 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
7629 Type copy = lookup_type;
7630 while (copy != TypeManager.object_type && copy != null){
7631 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
7632 copy = copy.BaseType;
7635 if (lookup_type.IsInterface) {
7636 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
7637 if (ifaces != null) {
7638 foreach (Type itype in ifaces)
7639 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
7654 // Points to our "data" repository
7656 MethodInfo get, set;
7657 bool is_base_indexer;
7659 LocalTemporary temp;
7660 LocalTemporary prepared_value;
7661 Expression set_expr;
7663 protected Type indexer_type;
7664 protected Type current_type;
7665 protected Expression instance_expr;
7666 protected ArrayList arguments;
7668 public IndexerAccess (ElementAccess ea, Location loc)
7669 : this (ea.Expr, false, loc)
7671 this.arguments = ea.Arguments;
7674 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
7677 this.instance_expr = instance_expr;
7678 this.is_base_indexer = is_base_indexer;
7679 this.eclass = ExprClass.Value;
7683 static string GetAccessorName (AccessorType at)
7685 if (at == AccessorType.Set)
7688 if (at == AccessorType.Get)
7691 throw new NotImplementedException (at.ToString ());
7694 protected virtual bool CommonResolve (EmitContext ec)
7696 indexer_type = instance_expr.Type;
7697 current_type = ec.ContainerType;
7702 public override Expression DoResolve (EmitContext ec)
7704 return ResolveAccessor (ec, AccessorType.Get);
7707 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7709 if (right_side == EmptyExpression.OutAccess) {
7710 Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
7711 GetSignatureForError ());
7715 // if the indexer returns a value type, and we try to set a field in it
7716 if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
7717 Error_CannotModifyIntermediateExpressionValue (ec);
7720 Expression e = ResolveAccessor (ec, AccessorType.Set);
7724 set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
7728 Expression ResolveAccessor (EmitContext ec, AccessorType accessorType)
7730 if (!CommonResolve (ec))
7733 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
7734 if (ilist.Methods == null) {
7735 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
7736 TypeManager.CSharpName (indexer_type));
7740 MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
7741 mg = mg.OverloadResolve (ec, ref arguments, false, loc);
7745 MethodInfo mi = (MethodInfo) mg;
7746 PropertyInfo pi = null;
7747 for (int i = 0; i < ilist.Methods.Count; ++i) {
7748 if (ilist.Methods [i] == mi) {
7749 pi = (PropertyInfo) ilist.Properties [i];
7754 type = pi.PropertyType;
7755 if (type.IsPointer && !ec.InUnsafe)
7758 MethodInfo accessor;
7759 if (accessorType == AccessorType.Get) {
7760 accessor = get = pi.GetGetMethod (true);
7762 accessor = set = pi.GetSetMethod (true);
7763 if (accessor == null && pi.GetGetMethod (true) != null) {
7764 Report.SymbolRelatedToPreviousError (pi);
7765 Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
7766 TypeManager.GetFullNameSignature (pi));
7771 if (accessor == null) {
7772 Report.SymbolRelatedToPreviousError (pi);
7773 Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
7774 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
7779 // Only base will allow this invocation to happen.
7781 if (accessor.IsAbstract && this is BaseIndexerAccess) {
7782 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
7785 bool must_do_cs1540_check;
7786 if (!IsAccessorAccessible (ec.ContainerType, accessor, out must_do_cs1540_check)) {
7788 set = pi.GetSetMethod (true);
7790 get = pi.GetGetMethod (true);
7792 if (set != null && get != null &&
7793 (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
7794 Report.SymbolRelatedToPreviousError (accessor);
7795 Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
7796 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
7798 Report.SymbolRelatedToPreviousError (pi);
7799 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
7803 instance_expr.CheckMarshalByRefAccess (ec);
7804 eclass = ExprClass.IndexerAccess;
7808 public void Emit (EmitContext ec, bool leave_copy)
7811 prepared_value.Emit (ec);
7813 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
7814 arguments, loc, false, false);
7818 ec.ig.Emit (OpCodes.Dup);
7819 temp = new LocalTemporary (Type);
7825 // source is ignored, because we already have a copy of it from the
7826 // LValue resolution and we have already constructed a pre-cached
7827 // version of the arguments (ea.set_arguments);
7829 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7831 prepared = prepare_for_load;
7832 Expression value = set_expr;
7835 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
7836 arguments, loc, true, false);
7838 prepared_value = new LocalTemporary (type);
7839 prepared_value.Store (ec);
7841 prepared_value.Release (ec);
7844 ec.ig.Emit (OpCodes.Dup);
7845 temp = new LocalTemporary (Type);
7848 } else if (leave_copy) {
7849 temp = new LocalTemporary (Type);
7855 arguments.Add (new Argument (value, Argument.AType.Expression));
7856 Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
7864 public override void Emit (EmitContext ec)
7869 public override string GetSignatureForError ()
7871 return TypeManager.CSharpSignature (get != null ? get : set, false);
7874 protected override void CloneTo (CloneContext clonectx, Expression t)
7876 IndexerAccess target = (IndexerAccess) t;
7878 if (arguments != null){
7879 target.arguments = new ArrayList ();
7880 foreach (Argument a in arguments)
7881 target.arguments.Add (a.Clone (clonectx));
7883 if (instance_expr != null)
7884 target.instance_expr = instance_expr.Clone (clonectx);
7889 /// The base operator for method names
7891 public class BaseAccess : Expression {
7892 public readonly string Identifier;
7895 public BaseAccess (string member, Location l)
7897 this.Identifier = member;
7901 public BaseAccess (string member, TypeArguments args, Location l)
7907 public override Expression DoResolve (EmitContext ec)
7909 Expression c = CommonResolve (ec);
7915 // MethodGroups use this opportunity to flag an error on lacking ()
7917 if (!(c is MethodGroupExpr))
7918 return c.Resolve (ec);
7922 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7924 Expression c = CommonResolve (ec);
7930 // MethodGroups use this opportunity to flag an error on lacking ()
7932 if (! (c is MethodGroupExpr))
7933 return c.DoResolveLValue (ec, right_side);
7938 Expression CommonResolve (EmitContext ec)
7940 Expression member_lookup;
7941 Type current_type = ec.ContainerType;
7942 Type base_type = current_type.BaseType;
7945 Error (1511, "Keyword `base' is not available in a static method");
7949 if (ec.IsInFieldInitializer){
7950 Error (1512, "Keyword `base' is not available in the current context");
7954 member_lookup = MemberLookup (ec.ContainerType, null, base_type, Identifier,
7955 AllMemberTypes, AllBindingFlags, loc);
7956 if (member_lookup == null) {
7957 Error_MemberLookupFailed (ec.ContainerType, base_type, base_type, Identifier,
7958 null, AllMemberTypes, AllBindingFlags);
7965 left = new TypeExpression (base_type, loc);
7967 left = ec.GetThis (loc);
7969 MemberExpr me = (MemberExpr) member_lookup;
7970 me = me.ResolveMemberAccess (ec, left, loc, null);
7977 me.SetTypeArguments (args);
7983 public override void Emit (EmitContext ec)
7985 throw new Exception ("Should never be called");
7988 protected override void CloneTo (CloneContext clonectx, Expression t)
7990 BaseAccess target = (BaseAccess) t;
7993 target.args = args.Clone ();
7998 /// The base indexer operator
8000 public class BaseIndexerAccess : IndexerAccess {
8001 public BaseIndexerAccess (ArrayList args, Location loc)
8002 : base (null, true, loc)
8004 arguments = new ArrayList ();
8005 foreach (Expression tmp in args)
8006 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8009 protected override bool CommonResolve (EmitContext ec)
8011 instance_expr = ec.GetThis (loc);
8013 current_type = ec.ContainerType.BaseType;
8014 indexer_type = current_type;
8016 foreach (Argument a in arguments){
8017 if (!a.Resolve (ec, loc))
8026 /// This class exists solely to pass the Type around and to be a dummy
8027 /// that can be passed to the conversion functions (this is used by
8028 /// foreach implementation to typecast the object return value from
8029 /// get_Current into the proper type. All code has been generated and
8030 /// we only care about the side effect conversions to be performed
8032 /// This is also now used as a placeholder where a no-action expression
8033 /// is needed (the `New' class).
8035 public class EmptyExpression : Expression {
8036 public static readonly EmptyExpression Null = new EmptyExpression ();
8038 public static readonly EmptyExpression OutAccess = new EmptyExpression ();
8039 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
8040 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
8042 static EmptyExpression temp = new EmptyExpression ();
8043 public static EmptyExpression Grab ()
8045 EmptyExpression retval = temp == null ? new EmptyExpression () : temp;
8050 public static void Release (EmptyExpression e)
8055 // TODO: should be protected
8056 public EmptyExpression ()
8058 type = TypeManager.object_type;
8059 eclass = ExprClass.Value;
8060 loc = Location.Null;
8063 public EmptyExpression (Type t)
8066 eclass = ExprClass.Value;
8067 loc = Location.Null;
8070 public override Expression DoResolve (EmitContext ec)
8075 public override void Emit (EmitContext ec)
8077 // nothing, as we only exist to not do anything.
8081 // This is just because we might want to reuse this bad boy
8082 // instead of creating gazillions of EmptyExpressions.
8083 // (CanImplicitConversion uses it)
8085 public void SetType (Type t)
8092 // Empty statement expression
8094 public sealed class EmptyExpressionStatement : ExpressionStatement
8096 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
8098 private EmptyExpressionStatement ()
8100 type = TypeManager.object_type;
8101 eclass = ExprClass.Value;
8102 loc = Location.Null;
8105 public override void EmitStatement (EmitContext ec)
8110 public override Expression DoResolve (EmitContext ec)
8115 public override void Emit (EmitContext ec)
8121 public class UserCast : Expression {
8125 public UserCast (MethodInfo method, Expression source, Location l)
8127 this.method = method;
8128 this.source = source;
8129 type = method.ReturnType;
8130 eclass = ExprClass.Value;
8134 public Expression Source {
8140 public override Expression DoResolve (EmitContext ec)
8143 // We are born fully resolved
8148 public override void Emit (EmitContext ec)
8150 ILGenerator ig = ec.ig;
8154 if (method is MethodInfo)
8155 ig.Emit (OpCodes.Call, (MethodInfo) method);
8157 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
8163 // This class is used to "construct" the type during a typecast
8164 // operation. Since the Type.GetType class in .NET can parse
8165 // the type specification, we just use this to construct the type
8166 // one bit at a time.
8168 public class ComposedCast : TypeExpr {
8172 public ComposedCast (Expression left, string dim)
8173 : this (left, dim, left.Location)
8177 public ComposedCast (Expression left, string dim, Location l)
8184 public Expression RemoveNullable ()
8186 if (dim.EndsWith ("?")) {
8187 dim = dim.Substring (0, dim.Length - 1);
8188 if (dim.Length == 0)
8195 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
8197 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
8201 Type ltype = lexpr.Type;
8202 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8203 Error_VoidInvalidInTheContext (loc);
8208 if ((dim.Length > 0) && (dim [0] == '?')) {
8209 TypeExpr nullable = new NullableType (left, loc);
8211 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
8212 return nullable.ResolveAsTypeTerminal (ec, false);
8216 if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc))
8219 if (dim != "" && dim [0] == '[' &&
8220 (ltype == TypeManager.arg_iterator_type || ltype == TypeManager.typed_reference_type)) {
8221 Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
8226 type = TypeManager.GetConstructedType (ltype, dim);
8231 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
8233 if (type.IsPointer && !ec.IsInUnsafeScope){
8238 eclass = ExprClass.Type;
8242 public override string Name {
8243 get { return left + dim; }
8246 public override string FullName {
8247 get { return type.FullName; }
8250 public override string GetSignatureForError ()
8252 return left.GetSignatureForError () + dim;
8255 protected override void CloneTo (CloneContext clonectx, Expression t)
8257 ComposedCast target = (ComposedCast) t;
8259 target.left = left.Clone (clonectx);
8263 public class FixedBufferPtr : Expression {
8266 public FixedBufferPtr (Expression array, Type array_type, Location l)
8271 type = TypeManager.GetPointerType (array_type);
8272 eclass = ExprClass.Value;
8275 public override void Emit(EmitContext ec)
8280 public override Expression DoResolve (EmitContext ec)
8283 // We are born fully resolved
8291 // This class is used to represent the address of an array, used
8292 // only by the Fixed statement, this generates "&a [0]" construct
8293 // for fixed (char *pa = a)
8295 public class ArrayPtr : FixedBufferPtr {
8298 public ArrayPtr (Expression array, Type array_type, Location l):
8299 base (array, array_type, l)
8301 this.array_type = array_type;
8304 public override void Emit (EmitContext ec)
8308 ILGenerator ig = ec.ig;
8309 IntLiteral.EmitInt (ig, 0);
8310 ig.Emit (OpCodes.Ldelema, array_type);
8315 // Encapsulates a conversion rules required for array indexes
8317 public class ArrayIndexCast : Expression
8321 public ArrayIndexCast (Expression expr)
8324 this.loc = expr.Location;
8327 public override Expression CreateExpressionTree (EmitContext ec)
8329 ArrayList args = new ArrayList (2);
8330 args.Add (new Argument (expr.CreateExpressionTree (ec)));
8331 args.Add (new Argument (new TypeOf (new TypeExpression (TypeManager.int32_type, loc), loc)));
8332 return CreateExpressionFactoryCall ("ConvertChecked", args);
8335 public override Expression DoResolve (EmitContext ec)
8338 eclass = expr.eclass;
8342 public override void Emit (EmitContext ec)
8346 if (type == TypeManager.int32_type)
8349 if (type == TypeManager.uint32_type)
8350 ec.ig.Emit (OpCodes.Conv_U);
8351 else if (type == TypeManager.int64_type)
8352 ec.ig.Emit (OpCodes.Conv_Ovf_I);
8353 else if (type == TypeManager.uint64_type)
8354 ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
8356 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
8361 // Used by the fixed statement
8363 public class StringPtr : Expression {
8366 public StringPtr (LocalBuilder b, Location l)
8369 eclass = ExprClass.Value;
8370 type = TypeManager.char_ptr_type;
8374 public override Expression DoResolve (EmitContext ec)
8376 // This should never be invoked, we are born in fully
8377 // initialized state.
8382 public override void Emit (EmitContext ec)
8384 ILGenerator ig = ec.ig;
8386 ig.Emit (OpCodes.Ldloc, b);
8387 ig.Emit (OpCodes.Conv_I);
8388 ig.Emit (OpCodes.Call, TypeManager.int_get_offset_to_string_data);
8389 ig.Emit (OpCodes.Add);
8394 // Implements the `stackalloc' keyword
8396 public class StackAlloc : Expression {
8401 public StackAlloc (Expression type, Expression count, Location l)
8408 public override Expression DoResolve (EmitContext ec)
8410 count = count.Resolve (ec);
8414 if (count.Type != TypeManager.int32_type){
8415 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8420 Constant c = count as Constant;
8421 if (c != null && c.IsNegative) {
8422 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8426 if (ec.InCatch || ec.InFinally) {
8427 Error (255, "Cannot use stackalloc in finally or catch");
8431 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
8437 if (!TypeManager.VerifyUnManaged (otype, loc))
8440 type = TypeManager.GetPointerType (otype);
8441 eclass = ExprClass.Value;
8446 public override void Emit (EmitContext ec)
8448 int size = GetTypeSize (otype);
8449 ILGenerator ig = ec.ig;
8452 ig.Emit (OpCodes.Sizeof, otype);
8454 IntConstant.EmitInt (ig, size);
8456 ig.Emit (OpCodes.Mul);
8457 ig.Emit (OpCodes.Localloc);
8460 protected override void CloneTo (CloneContext clonectx, Expression t)
8462 StackAlloc target = (StackAlloc) t;
8463 target.count = count.Clone (clonectx);
8464 target.t = t.Clone (clonectx);
8469 // An object initializer expression
8471 public class ElementInitializer : Expression
8473 Expression initializer;
8474 public readonly string Name;
8476 public ElementInitializer (string name, Expression initializer, Location loc)
8479 this.initializer = initializer;
8483 protected override void CloneTo (CloneContext clonectx, Expression t)
8485 if (initializer == null)
8488 ElementInitializer target = (ElementInitializer) t;
8489 target.initializer = initializer.Clone (clonectx);
8492 public override Expression DoResolve (EmitContext ec)
8494 if (initializer == null)
8495 return EmptyExpressionStatement.Instance;
8497 MemberExpr element_member = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
8498 Name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
8500 if (element_member == null)
8503 element_member.InstanceExpression = ec.CurrentInitializerVariable;
8505 if (initializer is CollectionOrObjectInitializers) {
8506 Expression previous = ec.CurrentInitializerVariable;
8507 ec.CurrentInitializerVariable = element_member;
8508 initializer = initializer.Resolve (ec);
8509 ec.CurrentInitializerVariable = previous;
8513 Assign a = new Assign (element_member, initializer, loc);
8514 if (a.Resolve (ec) == null)
8518 // Ignore field initializers with default value
8520 Constant c = a.Source as Constant;
8521 if (c != null && c.IsDefaultInitializer (a.Type) && a.Target.eclass == ExprClass.Variable)
8522 return EmptyExpressionStatement.Instance;
8527 protected override Expression Error_MemberLookupFailed (MemberInfo[] members)
8529 MemberInfo member = members [0];
8530 if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
8531 Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
8532 "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
8534 Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
8535 TypeManager.GetFullNameSignature (member));
8540 public override void Emit (EmitContext ec)
8542 throw new NotSupportedException ("Should not be reached");
8547 // A collection initializer expression
8549 public class CollectionElementInitializer : Expression
8551 public class ElementInitializerArgument : Argument
8553 public ElementInitializerArgument (Expression e)
8559 ArrayList arguments;
8561 public CollectionElementInitializer (Expression argument)
8563 arguments = new ArrayList (1);
8564 arguments.Add (argument);
8565 this.loc = argument.Location;
8568 public CollectionElementInitializer (ArrayList arguments, Location loc)
8570 this.arguments = arguments;
8574 protected override void CloneTo (CloneContext clonectx, Expression t)
8576 CollectionElementInitializer target = (CollectionElementInitializer) t;
8577 ArrayList t_arguments = target.arguments = new ArrayList (arguments.Count);
8578 foreach (Expression e in arguments)
8579 t_arguments.Add (e.Clone (clonectx));
8582 public override Expression DoResolve (EmitContext ec)
8584 // TODO: We should call a constructor which takes element counts argument,
8585 // for know types like List<T>, Dictionary<T, U>
8587 for (int i = 0; i < arguments.Count; ++i)
8588 arguments [i] = new ElementInitializerArgument ((Expression)arguments [i]);
8590 Expression add_method = new Invocation (
8591 new MemberAccess (ec.CurrentInitializerVariable, "Add", loc),
8594 add_method = add_method.Resolve (ec);
8599 public override void Emit (EmitContext ec)
8601 throw new NotSupportedException ("Should not be reached");
8606 // A block of object or collection initializers
8608 public class CollectionOrObjectInitializers : ExpressionStatement
8610 ArrayList initializers;
8612 public static readonly CollectionOrObjectInitializers Empty =
8613 new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
8615 public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
8617 this.initializers = initializers;
8621 public bool IsEmpty {
8623 return initializers.Count == 0;
8627 protected override void CloneTo (CloneContext clonectx, Expression target)
8629 CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
8631 t.initializers = new ArrayList (initializers.Count);
8632 foreach (Expression e in initializers)
8633 t.initializers.Add (e.Clone (clonectx));
8636 public override Expression DoResolve (EmitContext ec)
8638 bool is_elements_initialization = false;
8639 ArrayList element_names = null;
8640 for (int i = 0; i < initializers.Count; ++i) {
8641 Expression initializer = (Expression) initializers [i];
8642 ElementInitializer element_initializer = initializer as ElementInitializer;
8645 if (element_initializer != null) {
8646 is_elements_initialization = true;
8647 element_names = new ArrayList (initializers.Count);
8648 element_names.Add (element_initializer.Name);
8650 if (!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type,
8651 TypeManager.ienumerable_type)) {
8652 Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
8653 "object initializer because type `{1}' does not implement `{2}' interface",
8654 ec.CurrentInitializerVariable.GetSignatureForError (),
8655 TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
8656 TypeManager.CSharpName (TypeManager.ienumerable_type));
8661 if (is_elements_initialization == (element_initializer == null)) {
8662 Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
8663 is_elements_initialization ? "object initializer" : "collection initializer");
8667 if (is_elements_initialization) {
8668 if (element_names.Contains (element_initializer.Name)) {
8669 Report.Error (1912, element_initializer.Location,
8670 "An object initializer includes more than one member `{0}' initialization",
8671 element_initializer.Name);
8673 element_names.Add (element_initializer.Name);
8678 Expression e = initializer.Resolve (ec);
8679 if (e == EmptyExpressionStatement.Instance)
8680 initializers.RemoveAt (i--);
8682 initializers [i] = e;
8685 type = typeof (CollectionOrObjectInitializers);
8686 eclass = ExprClass.Variable;
8690 public override void Emit (EmitContext ec)
8695 public override void EmitStatement (EmitContext ec)
8697 foreach (ExpressionStatement e in initializers)
8698 e.EmitStatement (ec);
8703 // New expression with element/object initializers
8705 public class NewInitialize : New
8708 // This class serves as a proxy for variable initializer target instances.
8709 // A real variable is assigned later when we resolve left side of an
8712 sealed class InitializerTargetExpression : Expression, IMemoryLocation
8714 NewInitialize new_instance;
8716 public InitializerTargetExpression (NewInitialize newInstance)
8718 this.type = newInstance.type;
8719 this.loc = newInstance.loc;
8720 this.eclass = newInstance.eclass;
8721 this.new_instance = newInstance;
8724 public override Expression DoResolve (EmitContext ec)
8729 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8734 public override void Emit (EmitContext ec)
8736 new_instance.value_target.Emit (ec);
8739 #region IMemoryLocation Members
8741 public void AddressOf (EmitContext ec, AddressOp mode)
8743 ((IMemoryLocation)new_instance.value_target).AddressOf (ec, mode);
8749 CollectionOrObjectInitializers initializers;
8751 public NewInitialize (Expression requested_type, ArrayList arguments, CollectionOrObjectInitializers initializers, Location l)
8752 : base (requested_type, arguments, l)
8754 this.initializers = initializers;
8757 protected override void CloneTo (CloneContext clonectx, Expression t)
8759 base.CloneTo (clonectx, t);
8761 NewInitialize target = (NewInitialize) t;
8762 target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
8765 public override Expression DoResolve (EmitContext ec)
8767 Expression e = base.DoResolve (ec);
8771 // Empty initializer can be optimized to simple new
8772 if (initializers.IsEmpty)
8775 Expression previous = ec.CurrentInitializerVariable;
8776 ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
8777 initializers.Resolve (ec);
8778 ec.CurrentInitializerVariable = previous;
8782 public override void Emit (EmitContext ec)
8787 // If target is a value, let's use it
8789 VariableReference variable = value_target as VariableReference;
8790 if (variable != null) {
8792 StoreFromPtr (ec.ig, type);
8794 variable.Variable.EmitAssign (ec);
8796 if (value_target == null || value_target_set)
8797 value_target = new LocalTemporary (type);
8799 ((LocalTemporary) value_target).Store (ec);
8802 initializers.Emit (ec);
8804 if (variable == null)
8805 value_target.Emit (ec);
8808 public override void EmitStatement (EmitContext ec)
8810 if (initializers.IsEmpty) {
8811 base.EmitStatement (ec);
8817 if (value_target == null) {
8818 LocalTemporary variable = new LocalTemporary (type);
8819 variable.Store (ec);
8820 value_target = variable;
8823 initializers.EmitStatement (ec);
8826 public override bool HasInitializer {
8828 return !initializers.IsEmpty;
8833 public class AnonymousTypeDeclaration : Expression
8835 ArrayList parameters;
8836 readonly TypeContainer parent;
8837 static readonly ArrayList EmptyParameters = new ArrayList (0);
8839 public AnonymousTypeDeclaration (ArrayList parameters, TypeContainer parent, Location loc)
8841 this.parameters = parameters;
8842 this.parent = parent;
8846 protected override void CloneTo (CloneContext clonectx, Expression target)
8848 if (parameters == null)
8851 AnonymousTypeDeclaration t = (AnonymousTypeDeclaration) target;
8852 t.parameters = new ArrayList (parameters.Count);
8853 foreach (AnonymousTypeParameter atp in parameters)
8854 t.parameters.Add (atp.Clone (clonectx));
8857 AnonymousTypeClass CreateAnonymousType (ArrayList parameters)
8859 AnonymousTypeClass type = RootContext.ToplevelTypes.GetAnonymousType (parameters);
8863 type = AnonymousTypeClass.Create (parent, parameters, loc);
8868 type.DefineMembers ();
8872 RootContext.ToplevelTypes.AddAnonymousType (type);
8876 public override Expression DoResolve (EmitContext ec)
8878 AnonymousTypeClass anonymous_type;
8880 if (parameters == null) {
8881 anonymous_type = CreateAnonymousType (EmptyParameters);
8882 return new New (new TypeExpression (anonymous_type.TypeBuilder, loc),
8883 null, loc).Resolve (ec);
8887 ArrayList arguments = new ArrayList (parameters.Count);
8888 TypeExpression [] t_args = new TypeExpression [parameters.Count];
8889 for (int i = 0; i < parameters.Count; ++i) {
8890 Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
8896 arguments.Add (new Argument (e));
8897 t_args [i] = new TypeExpression (e.Type, e.Location);
8903 anonymous_type = CreateAnonymousType (parameters);
8904 if (anonymous_type == null)
8907 ConstructedType te = new ConstructedType (anonymous_type.TypeBuilder,
8908 new TypeArguments (loc, t_args), loc);
8910 return new New (te, arguments, loc).Resolve (ec);
8913 public override void Emit (EmitContext ec)
8915 throw new InternalErrorException ("Should not be reached");
8919 public class AnonymousTypeParameter : Expression
8921 public readonly string Name;
8922 Expression initializer;
8924 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
8928 this.initializer = initializer;
8931 public AnonymousTypeParameter (Parameter parameter)
8933 this.Name = parameter.Name;
8934 this.loc = parameter.Location;
8935 this.initializer = new SimpleName (Name, loc);
8938 protected override void CloneTo (CloneContext clonectx, Expression target)
8940 AnonymousTypeParameter t = (AnonymousTypeParameter) target;
8941 t.initializer = initializer.Clone (clonectx);
8944 public override bool Equals (object o)
8946 AnonymousTypeParameter other = o as AnonymousTypeParameter;
8947 return other != null && Name == other.Name;
8950 public override int GetHashCode ()
8952 return Name.GetHashCode ();
8955 public override Expression DoResolve (EmitContext ec)
8957 Expression e = initializer.Resolve (ec);
8962 if (type == TypeManager.void_type || type == TypeManager.null_type ||
8963 type == TypeManager.anonymous_method_type || type.IsPointer) {
8964 Error_InvalidInitializer (e);
8971 protected virtual void Error_InvalidInitializer (Expression initializer)
8973 Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
8974 Name, initializer.GetSignatureForError ());
8977 public override void Emit (EmitContext ec)
8979 throw new InternalErrorException ("Should not be reached");