2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
13 namespace Mono.CSharp {
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
21 // This is an user operator expression, automatically created during
24 public class UserOperatorCall : Expression {
25 public delegate Expression ExpressionTreeExpression (EmitContext ec, MethodGroupExpr mg);
27 protected readonly ArrayList arguments;
28 protected readonly MethodGroupExpr mg;
29 readonly ExpressionTreeExpression expr_tree;
31 public UserOperatorCall (MethodGroupExpr mg, ArrayList args, ExpressionTreeExpression expr_tree, Location loc)
34 this.arguments = args;
35 this.expr_tree = expr_tree;
37 type = TypeManager.TypeToCoreType (((MethodInfo) mg).ReturnType);
38 eclass = ExprClass.Value;
42 public override Expression CreateExpressionTree (EmitContext ec)
44 if (expr_tree != null)
45 return expr_tree (ec, mg);
47 ArrayList args = new ArrayList (arguments.Count + 1);
48 args.Add (new Argument (new NullLiteral (loc)));
49 args.Add (new Argument (mg.CreateExpressionTree (ec)));
50 foreach (Argument a in arguments) {
51 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
54 return CreateExpressionFactoryCall ("Call", args);
57 public override Expression DoResolve (EmitContext ec)
60 // We are born fully resolved
65 public override void Emit (EmitContext ec)
67 mg.EmitCall (ec, arguments);
70 [Obsolete ("It may not be compatible with expression trees")]
71 static public UserOperatorCall MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
72 Expression e, Location loc)
76 args = new ArrayList (1);
77 Argument a = new Argument (e, Argument.AType.Expression);
79 // We need to resolve the arguments before sending them in !
80 if (!a.Resolve (ec, loc))
84 mg = mg.OverloadResolve (ec, ref args, false, loc);
89 return new UserOperatorCall (mg, args, null, loc);
92 public MethodGroupExpr Method {
97 public class ParenthesizedExpression : Expression
99 public Expression Expr;
101 public ParenthesizedExpression (Expression expr)
106 public override Expression DoResolve (EmitContext ec)
108 Expr = Expr.Resolve (ec);
112 public override void Emit (EmitContext ec)
114 throw new Exception ("Should not happen");
117 public override Location Location
120 return Expr.Location;
124 protected override void CloneTo (CloneContext clonectx, Expression t)
126 ParenthesizedExpression target = (ParenthesizedExpression) t;
128 target.Expr = Expr.Clone (clonectx);
133 // Unary implements unary expressions.
135 public class Unary : Expression {
136 public enum Operator : byte {
137 UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
141 public static readonly string [] oper_names;
142 static Type [] [] predefined_operators;
144 public readonly Operator Oper;
145 public Expression Expr;
146 Expression enum_conversion;
148 public Unary (Operator op, Expression expr, Location loc)
157 oper_names = new string [(int)Operator.TOP];
159 oper_names [(int) Operator.UnaryPlus] = "op_UnaryPlus";
160 oper_names [(int) Operator.UnaryNegation] = "op_UnaryNegation";
161 oper_names [(int) Operator.LogicalNot] = "op_LogicalNot";
162 oper_names [(int) Operator.OnesComplement] = "op_OnesComplement";
163 oper_names [(int) Operator.AddressOf] = "op_AddressOf";
167 // This routine will attempt to simplify the unary expression when the
168 // argument is a constant.
170 Constant TryReduceConstant (EmitContext ec, Constant e)
172 if (e is SideEffectConstant) {
173 Constant r = TryReduceConstant (ec, ((SideEffectConstant) e).value);
174 return r == null ? null : new SideEffectConstant (r, e, r.Location);
177 Type expr_type = e.Type;
180 case Operator.UnaryPlus:
181 // Unary numeric promotions
182 if (expr_type == TypeManager.byte_type)
183 return new IntConstant (((ByteConstant)e).Value, e.Location);
184 if (expr_type == TypeManager.sbyte_type)
185 return new IntConstant (((SByteConstant)e).Value, e.Location);
186 if (expr_type == TypeManager.short_type)
187 return new IntConstant (((ShortConstant)e).Value, e.Location);
188 if (expr_type == TypeManager.ushort_type)
189 return new IntConstant (((UShortConstant)e).Value, e.Location);
190 if (expr_type == TypeManager.char_type)
191 return new IntConstant (((CharConstant)e).Value, e.Location);
193 // Predefined operators
194 if (expr_type == TypeManager.int32_type || expr_type == TypeManager.uint32_type ||
195 expr_type == TypeManager.int64_type || expr_type == TypeManager.uint64_type ||
196 expr_type == TypeManager.float_type || expr_type == TypeManager.double_type ||
197 expr_type == TypeManager.decimal_type)
204 case Operator.UnaryNegation:
205 // Unary numeric promotions
206 if (expr_type == TypeManager.byte_type)
207 return new IntConstant (-((ByteConstant)e).Value, e.Location);
208 if (expr_type == TypeManager.sbyte_type)
209 return new IntConstant (-((SByteConstant)e).Value, e.Location);
210 if (expr_type == TypeManager.short_type)
211 return new IntConstant (-((ShortConstant)e).Value, e.Location);
212 if (expr_type == TypeManager.ushort_type)
213 return new IntConstant (-((UShortConstant)e).Value, e.Location);
214 if (expr_type == TypeManager.char_type)
215 return new IntConstant (-((CharConstant)e).Value, e.Location);
217 // Predefined operators
218 if (expr_type == TypeManager.int32_type) {
219 int value = ((IntConstant)e).Value;
220 if (value == int.MinValue) {
221 if (ec.ConstantCheckState) {
222 ConstantFold.Error_CompileTimeOverflow (loc);
227 return new IntConstant (-value, e.Location);
229 if (expr_type == TypeManager.int64_type) {
230 long value = ((LongConstant)e).Value;
231 if (value == long.MinValue) {
232 if (ec.ConstantCheckState) {
233 ConstantFold.Error_CompileTimeOverflow (loc);
238 return new LongConstant (-value, e.Location);
241 if (expr_type == TypeManager.uint32_type) {
242 UIntLiteral uil = e as UIntLiteral;
244 if (uil.Value == 2147483648)
245 return new IntLiteral (int.MinValue, e.Location);
246 return new LongLiteral (-uil.Value, e.Location);
248 return new LongConstant (-((UIntConstant)e).Value, e.Location);
251 if (expr_type == TypeManager.uint64_type) {
252 ULongLiteral ull = e as ULongLiteral;
253 if (ull != null && ull.Value == 9223372036854775808)
254 return new LongLiteral (long.MinValue, e.Location);
258 if (expr_type == TypeManager.float_type) {
259 FloatLiteral fl = e as FloatLiteral;
260 // For better error reporting
262 fl.Value = -fl.Value;
265 return new FloatConstant (-((FloatConstant)e).Value, e.Location);
267 if (expr_type == TypeManager.double_type) {
268 DoubleLiteral dl = e as DoubleLiteral;
269 // For better error reporting
271 dl.Value = -dl.Value;
275 return new DoubleConstant (-((DoubleConstant)e).Value, e.Location);
277 if (expr_type == TypeManager.decimal_type)
278 return new DecimalConstant (-((DecimalConstant)e).Value, e.Location);
282 case Operator.LogicalNot:
283 if (expr_type != TypeManager.bool_type)
286 bool b = (bool)e.GetValue ();
287 return new BoolConstant (!b, e.Location);
289 case Operator.OnesComplement:
290 // Unary numeric promotions
291 if (expr_type == TypeManager.byte_type)
292 return new IntConstant (~((ByteConstant)e).Value, e.Location);
293 if (expr_type == TypeManager.sbyte_type)
294 return new IntConstant (~((SByteConstant)e).Value, e.Location);
295 if (expr_type == TypeManager.short_type)
296 return new IntConstant (~((ShortConstant)e).Value, e.Location);
297 if (expr_type == TypeManager.ushort_type)
298 return new IntConstant (~((UShortConstant)e).Value, e.Location);
299 if (expr_type == TypeManager.char_type)
300 return new IntConstant (~((CharConstant)e).Value, e.Location);
302 // Predefined operators
303 if (expr_type == TypeManager.int32_type)
304 return new IntConstant (~((IntConstant)e).Value, e.Location);
305 if (expr_type == TypeManager.uint32_type)
306 return new UIntConstant (~((UIntConstant)e).Value, e.Location);
307 if (expr_type == TypeManager.int64_type)
308 return new LongConstant (~((LongConstant)e).Value, e.Location);
309 if (expr_type == TypeManager.uint64_type){
310 return new ULongConstant (~((ULongConstant)e).Value, e.Location);
312 if (e is EnumConstant) {
313 e = TryReduceConstant (ec, ((EnumConstant)e).Child);
315 e = new EnumConstant (e, expr_type);
320 throw new Exception ("Can not constant fold: " + Oper.ToString());
323 protected Expression ResolveOperator (EmitContext ec, Expression expr)
325 eclass = ExprClass.Value;
327 if (predefined_operators == null)
328 CreatePredefinedOperatorsTable ();
330 Type expr_type = expr.Type;
331 Expression best_expr;
334 // Primitive types first
336 if (TypeManager.IsPrimitiveType (expr_type)) {
337 best_expr = ResolvePrimitivePredefinedType (expr);
338 if (best_expr == null)
341 type = best_expr.Type;
347 // E operator ~(E x);
349 if (Oper == Operator.OnesComplement && TypeManager.IsEnumType (expr_type))
350 return ResolveEnumOperator (ec, expr);
352 return ResolveUserType (ec, expr);
355 protected virtual Expression ResolveEnumOperator (EmitContext ec, Expression expr)
357 Type underlying_type = TypeManager.GetEnumUnderlyingType (expr.Type);
358 Expression best_expr = ResolvePrimitivePredefinedType (EmptyCast.Create (expr, underlying_type));
359 if (best_expr == null)
363 enum_conversion = Convert.ExplicitNumericConversion (new EmptyExpression (best_expr.Type), underlying_type);
365 return EmptyCast.Create (this, type);
368 public override Expression CreateExpressionTree (EmitContext ec)
370 return CreateExpressionTree (ec, null);
373 Expression CreateExpressionTree (EmitContext ec, MethodGroupExpr user_op)
377 case Operator.AddressOf:
378 Error_PointerInsideExpressionTree ();
380 case Operator.UnaryNegation:
381 if (ec.CheckState && user_op == null && !IsFloat (type))
382 method_name = "NegateChecked";
384 method_name = "Negate";
386 case Operator.OnesComplement:
387 case Operator.LogicalNot:
390 case Operator.UnaryPlus:
391 method_name = "UnaryPlus";
394 throw new InternalErrorException ("Unknown unary operator " + Oper.ToString ());
397 ArrayList args = new ArrayList (2);
398 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
400 args.Add (new Argument (user_op.CreateExpressionTree (ec)));
401 return CreateExpressionFactoryCall (method_name, args);
404 static void CreatePredefinedOperatorsTable ()
406 predefined_operators = new Type [(int) Operator.TOP] [];
409 // 7.6.1 Unary plus operator
411 predefined_operators [(int) Operator.UnaryPlus] = new Type [] {
412 TypeManager.int32_type, TypeManager.uint32_type,
413 TypeManager.int64_type, TypeManager.uint64_type,
414 TypeManager.float_type, TypeManager.double_type,
415 TypeManager.decimal_type
419 // 7.6.2 Unary minus operator
421 predefined_operators [(int) Operator.UnaryNegation] = new Type [] {
422 TypeManager.int32_type,
423 TypeManager.int64_type,
424 TypeManager.float_type, TypeManager.double_type,
425 TypeManager.decimal_type
429 // 7.6.3 Logical negation operator
431 predefined_operators [(int) Operator.LogicalNot] = new Type [] {
432 TypeManager.bool_type
436 // 7.6.4 Bitwise complement operator
438 predefined_operators [(int) Operator.OnesComplement] = new Type [] {
439 TypeManager.int32_type, TypeManager.uint32_type,
440 TypeManager.int64_type, TypeManager.uint64_type
445 // Unary numeric promotions
447 static Expression DoNumericPromotion (Operator op, Expression expr)
449 Type expr_type = expr.Type;
450 if ((op == Operator.UnaryPlus || op == Operator.UnaryNegation || op == Operator.OnesComplement) &&
451 expr_type == TypeManager.byte_type || expr_type == TypeManager.sbyte_type ||
452 expr_type == TypeManager.short_type || expr_type == TypeManager.ushort_type ||
453 expr_type == TypeManager.char_type)
454 return Convert.ImplicitNumericConversion (expr, TypeManager.int32_type);
456 if (op == Operator.UnaryNegation && expr_type == TypeManager.uint32_type)
457 return Convert.ImplicitNumericConversion (expr, TypeManager.int64_type);
462 public override Expression DoResolve (EmitContext ec)
464 if (Oper == Operator.AddressOf) {
465 Expr = Expr.DoResolveLValue (ec, new EmptyExpression ());
467 if (Expr == null || Expr.eclass != ExprClass.Variable){
468 Error (211, "Cannot take the address of the given expression");
472 return ResolveAddressOf (ec);
475 Expr = Expr.Resolve (ec);
479 if (TypeManager.IsNullableValueType (Expr.Type))
480 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
483 // Attempt to use a constant folding operation.
485 Constant cexpr = Expr as Constant;
487 cexpr = TryReduceConstant (ec, cexpr);
492 Expression expr = ResolveOperator (ec, Expr);
494 Error_OperatorCannotBeApplied (loc, OperName (Oper), Expr.Type);
497 // Reduce unary operator on predefined types
499 if (expr == this && Oper == Operator.UnaryPlus)
505 public override Expression DoResolveLValue (EmitContext ec, Expression right)
510 public override void Emit (EmitContext ec)
512 EmitOperator (ec, type);
515 protected void EmitOperator (EmitContext ec, Type type)
517 ILGenerator ig = ec.ig;
520 case Operator.UnaryPlus:
524 case Operator.UnaryNegation:
525 if (ec.CheckState && !IsFloat (type)) {
526 ig.Emit (OpCodes.Ldc_I4_0);
527 if (type == TypeManager.int64_type)
528 ig.Emit (OpCodes.Conv_U8);
530 ig.Emit (OpCodes.Sub_Ovf);
533 ig.Emit (OpCodes.Neg);
538 case Operator.LogicalNot:
540 ig.Emit (OpCodes.Ldc_I4_0);
541 ig.Emit (OpCodes.Ceq);
544 case Operator.OnesComplement:
546 ig.Emit (OpCodes.Not);
549 case Operator.AddressOf:
550 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
554 throw new Exception ("This should not happen: Operator = "
559 // Same trick as in Binary expression
561 if (enum_conversion != null)
562 enum_conversion.Emit (ec);
565 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
567 if (Oper == Operator.LogicalNot)
568 Expr.EmitBranchable (ec, target, !on_true);
570 base.EmitBranchable (ec, target, on_true);
573 public override void EmitSideEffect (EmitContext ec)
575 Expr.EmitSideEffect (ec);
578 public static void Error_OperatorCannotBeApplied (Location loc, string oper, Type t)
580 Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
581 oper, TypeManager.CSharpName (t));
584 static bool IsFloat (Type t)
586 return t == TypeManager.float_type || t == TypeManager.double_type;
590 // Returns a stringified representation of the Operator
592 public static string OperName (Operator oper)
595 case Operator.UnaryPlus:
597 case Operator.UnaryNegation:
599 case Operator.LogicalNot:
601 case Operator.OnesComplement:
603 case Operator.AddressOf:
607 throw new NotImplementedException (oper.ToString ());
610 Expression ResolveAddressOf (EmitContext ec)
617 if (!TypeManager.VerifyUnManaged (Expr.Type, loc)) {
621 IVariable variable = Expr as IVariable;
622 bool is_fixed = variable != null && variable.VerifyFixed ();
624 if (!ec.InFixedInitializer && !is_fixed) {
625 Error (212, "You can only take the address of unfixed expression inside " +
626 "of a fixed statement initializer");
630 if (ec.InFixedInitializer && is_fixed) {
631 Error (213, "You cannot use the fixed statement to take the address of an already fixed expression");
635 LocalVariableReference lr = Expr as LocalVariableReference;
637 if (lr.local_info.IsCaptured) {
638 AnonymousMethod.Error_AddressOfCapturedVar (lr.Name, loc);
641 lr.local_info.AddressTaken = true;
642 lr.local_info.Used = true;
645 ParameterReference pr = Expr as ParameterReference;
646 if ((pr != null) && pr.Parameter.IsCaptured) {
647 AnonymousMethod.Error_AddressOfCapturedVar (pr.Name, loc);
651 // According to the specs, a variable is considered definitely assigned if you take
653 if ((variable != null) && (variable.VariableInfo != null)) {
654 variable.VariableInfo.SetAssigned (ec);
657 type = TypeManager.GetPointerType (Expr.Type);
658 eclass = ExprClass.Value;
662 Expression ResolvePrimitivePredefinedType (Expression expr)
664 expr = DoNumericPromotion (Oper, expr);
665 Type expr_type = expr.Type;
666 Type[] predefined = predefined_operators [(int) Oper];
667 foreach (Type t in predefined) {
675 // Perform user-operator overload resolution
677 protected virtual Expression ResolveUserOperator (EmitContext ec, Expression expr)
679 string op_name = oper_names [(int) Oper];
680 MethodGroupExpr user_op = MemberLookup (ec.ContainerType, expr.Type, op_name, MemberTypes.Method, AllBindingFlags, expr.Location) as MethodGroupExpr;
684 ArrayList args = new ArrayList (1);
685 args.Add (new Argument (expr));
686 user_op = user_op.OverloadResolve (ec, ref args, false, expr.Location);
691 Expr = ((Argument) args [0]).Expr;
692 return new UserOperatorCall (user_op, args, CreateExpressionTree, expr.Location);
696 // Unary user type overload resolution
698 Expression ResolveUserType (EmitContext ec, Expression expr)
700 Expression best_expr = ResolveUserOperator (ec, expr);
701 if (best_expr != null)
704 Type[] predefined = predefined_operators [(int) Oper];
705 foreach (Type t in predefined) {
706 Expression oper_expr = Convert.UserDefinedConversion (ec, expr, t, expr.Location, false);
707 if (oper_expr == null)
711 // decimal type is predefined but has user-operators
713 if (oper_expr.Type == TypeManager.decimal_type)
714 oper_expr = ResolveUserType (ec, oper_expr);
716 oper_expr = ResolvePrimitivePredefinedType (oper_expr);
718 if (oper_expr == null)
721 if (best_expr == null) {
722 best_expr = oper_expr;
726 int result = MethodGroupExpr.BetterTypeConversion (ec, best_expr.Type, t);
728 Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
729 OperName (Oper), TypeManager.CSharpName (expr.Type));
734 best_expr = oper_expr;
737 if (best_expr == null)
741 // HACK: Decimal user-operator is included in standard operators
743 if (best_expr.Type == TypeManager.decimal_type)
747 type = best_expr.Type;
751 protected override void CloneTo (CloneContext clonectx, Expression t)
753 Unary target = (Unary) t;
755 target.Expr = Expr.Clone (clonectx);
760 // Unary operators are turned into Indirection expressions
761 // after semantic analysis (this is so we can take the address
762 // of an indirection).
764 public class Indirection : Expression, IMemoryLocation, IAssignMethod, IVariable {
766 LocalTemporary temporary;
769 public Indirection (Expression expr, Location l)
775 public override Expression CreateExpressionTree (EmitContext ec)
777 Error_PointerInsideExpressionTree ();
781 public override void Emit (EmitContext ec)
786 LoadFromPtr (ec.ig, Type);
789 public void Emit (EmitContext ec, bool leave_copy)
793 ec.ig.Emit (OpCodes.Dup);
794 temporary = new LocalTemporary (expr.Type);
795 temporary.Store (ec);
799 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
801 prepared = prepare_for_load;
805 if (prepare_for_load)
806 ec.ig.Emit (OpCodes.Dup);
810 ec.ig.Emit (OpCodes.Dup);
811 temporary = new LocalTemporary (expr.Type);
812 temporary.Store (ec);
815 StoreFromPtr (ec.ig, type);
817 if (temporary != null) {
819 temporary.Release (ec);
823 public void AddressOf (EmitContext ec, AddressOp Mode)
828 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
830 return DoResolve (ec);
833 public override Expression DoResolve (EmitContext ec)
835 expr = expr.Resolve (ec);
842 if (!expr.Type.IsPointer) {
843 Error (193, "The * or -> operator must be applied to a pointer");
847 type = TypeManager.GetElementType (expr.Type);
848 eclass = ExprClass.Variable;
852 public override string ToString ()
854 return "*(" + expr + ")";
857 #region IVariable Members
859 public VariableInfo VariableInfo {
863 public bool VerifyFixed ()
865 // A pointer-indirection is always fixed.
873 /// Unary Mutator expressions (pre and post ++ and --)
877 /// UnaryMutator implements ++ and -- expressions. It derives from
878 /// ExpressionStatement becuase the pre/post increment/decrement
879 /// operators can be used in a statement context.
881 /// FIXME: Idea, we could split this up in two classes, one simpler
882 /// for the common case, and one with the extra fields for more complex
883 /// classes (indexers require temporary access; overloaded require method)
886 public class UnaryMutator : ExpressionStatement {
888 public enum Mode : byte {
895 PreDecrement = IsDecrement,
896 PostIncrement = IsPost,
897 PostDecrement = IsPost | IsDecrement
901 bool is_expr = false;
902 bool recurse = false;
907 // This is expensive for the simplest case.
909 UserOperatorCall method;
911 public UnaryMutator (Mode m, Expression e, Location l)
918 static string OperName (Mode mode)
920 return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
925 /// Returns whether an object of type `t' can be incremented
926 /// or decremented with add/sub (ie, basically whether we can
927 /// use pre-post incr-decr operations on it, but it is not a
928 /// System.Decimal, which we require operator overloading to catch)
930 static bool IsIncrementableNumber (Type t)
932 return (t == TypeManager.sbyte_type) ||
933 (t == TypeManager.byte_type) ||
934 (t == TypeManager.short_type) ||
935 (t == TypeManager.ushort_type) ||
936 (t == TypeManager.int32_type) ||
937 (t == TypeManager.uint32_type) ||
938 (t == TypeManager.int64_type) ||
939 (t == TypeManager.uint64_type) ||
940 (t == TypeManager.char_type) ||
941 (TypeManager.IsSubclassOf (t, TypeManager.enum_type)) ||
942 (t == TypeManager.float_type) ||
943 (t == TypeManager.double_type) ||
944 (t.IsPointer && t != TypeManager.void_ptr_type);
947 Expression ResolveOperator (EmitContext ec)
949 Type expr_type = expr.Type;
952 // Step 1: Perform Operator Overload location
957 if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
958 op_name = "op_Increment";
960 op_name = "op_Decrement";
962 mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
965 method = UserOperatorCall.MakeSimpleCall (
966 ec, (MethodGroupExpr) mg, expr, loc);
969 } else if (!IsIncrementableNumber (expr_type)) {
970 Error (187, "No such operator '" + OperName (mode) + "' defined for type '" +
971 TypeManager.CSharpName (expr_type) + "'");
976 // The operand of the prefix/postfix increment decrement operators
977 // should be an expression that is classified as a variable,
978 // a property access or an indexer access
981 if (expr.eclass == ExprClass.Variable){
982 LocalVariableReference var = expr as LocalVariableReference;
983 if ((var != null) && var.IsReadOnly) {
984 Error (1604, "cannot assign to `" + var.Name + "' because it is readonly");
987 } else if (expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess){
988 expr = expr.ResolveLValue (ec, this, Location);
992 Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
999 public override Expression CreateExpressionTree (EmitContext ec)
1001 return new SimpleAssign (this, this).CreateExpressionTree (ec);
1004 public override Expression DoResolve (EmitContext ec)
1006 expr = expr.Resolve (ec);
1011 eclass = ExprClass.Value;
1014 if (TypeManager.IsNullableValueType (expr.Type))
1015 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
1018 return ResolveOperator (ec);
1022 // Loads the proper "1" into the stack based on the type, then it emits the
1023 // opcode for the operation requested
1025 void LoadOneAndEmitOp (EmitContext ec, Type t)
1028 // Measure if getting the typecode and using that is more/less efficient
1029 // that comparing types. t.GetTypeCode() is an internal call.
1031 ILGenerator ig = ec.ig;
1033 if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
1034 LongConstant.EmitLong (ig, 1);
1035 else if (t == TypeManager.double_type)
1036 ig.Emit (OpCodes.Ldc_R8, 1.0);
1037 else if (t == TypeManager.float_type)
1038 ig.Emit (OpCodes.Ldc_R4, 1.0F);
1039 else if (t.IsPointer){
1040 Type et = TypeManager.GetElementType (t);
1041 int n = GetTypeSize (et);
1044 ig.Emit (OpCodes.Sizeof, et);
1046 IntConstant.EmitInt (ig, n);
1048 ig.Emit (OpCodes.Ldc_I4_1);
1051 // Now emit the operation
1054 if (t == TypeManager.int32_type ||
1055 t == TypeManager.int64_type){
1056 if ((mode & Mode.IsDecrement) != 0)
1057 ig.Emit (OpCodes.Sub_Ovf);
1059 ig.Emit (OpCodes.Add_Ovf);
1060 } else if (t == TypeManager.uint32_type ||
1061 t == TypeManager.uint64_type){
1062 if ((mode & Mode.IsDecrement) != 0)
1063 ig.Emit (OpCodes.Sub_Ovf_Un);
1065 ig.Emit (OpCodes.Add_Ovf_Un);
1067 if ((mode & Mode.IsDecrement) != 0)
1068 ig.Emit (OpCodes.Sub_Ovf);
1070 ig.Emit (OpCodes.Add_Ovf);
1073 if ((mode & Mode.IsDecrement) != 0)
1074 ig.Emit (OpCodes.Sub);
1076 ig.Emit (OpCodes.Add);
1079 if (t == TypeManager.sbyte_type){
1081 ig.Emit (OpCodes.Conv_Ovf_I1);
1083 ig.Emit (OpCodes.Conv_I1);
1084 } else if (t == TypeManager.byte_type){
1086 ig.Emit (OpCodes.Conv_Ovf_U1);
1088 ig.Emit (OpCodes.Conv_U1);
1089 } else if (t == TypeManager.short_type){
1091 ig.Emit (OpCodes.Conv_Ovf_I2);
1093 ig.Emit (OpCodes.Conv_I2);
1094 } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
1096 ig.Emit (OpCodes.Conv_Ovf_U2);
1098 ig.Emit (OpCodes.Conv_U2);
1103 void EmitCode (EmitContext ec, bool is_expr)
1106 this.is_expr = is_expr;
1107 ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1110 public override void Emit (EmitContext ec)
1113 // We use recurse to allow ourselfs to be the source
1114 // of an assignment. This little hack prevents us from
1115 // having to allocate another expression
1118 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1120 LoadOneAndEmitOp (ec, expr.Type);
1122 ec.ig.Emit (OpCodes.Call, (MethodInfo)method.Method);
1127 EmitCode (ec, true);
1130 public override void EmitStatement (EmitContext ec)
1132 EmitCode (ec, false);
1135 protected override void CloneTo (CloneContext clonectx, Expression t)
1137 UnaryMutator target = (UnaryMutator) t;
1139 target.expr = expr.Clone (clonectx);
1144 /// Base class for the `Is' and `As' classes.
1148 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1151 public abstract class Probe : Expression {
1152 public Expression ProbeType;
1153 protected Expression expr;
1154 protected TypeExpr probe_type_expr;
1156 public Probe (Expression expr, Expression probe_type, Location l)
1158 ProbeType = probe_type;
1163 public Expression Expr {
1169 public override Expression DoResolve (EmitContext ec)
1171 probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
1172 if (probe_type_expr == null)
1175 expr = expr.Resolve (ec);
1179 if (expr.Type.IsPointer || probe_type_expr.Type.IsPointer) {
1180 Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1185 if (expr.Type == TypeManager.anonymous_method_type) {
1186 Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1194 protected abstract string OperatorName { get; }
1196 protected override void CloneTo (CloneContext clonectx, Expression t)
1198 Probe target = (Probe) t;
1200 target.expr = expr.Clone (clonectx);
1201 target.ProbeType = ProbeType.Clone (clonectx);
1207 /// Implementation of the `is' operator.
1209 public class Is : Probe {
1210 public Is (Expression expr, Expression probe_type, Location l)
1211 : base (expr, probe_type, l)
1215 public override Expression CreateExpressionTree (EmitContext ec)
1217 ArrayList args = new ArrayList (2);
1218 args.Add (new Argument (expr.CreateExpressionTree (ec)));
1219 args.Add (new Argument (new TypeOf (probe_type_expr, loc)));
1220 return CreateExpressionFactoryCall ("TypeIs", args);
1223 public override void Emit (EmitContext ec)
1225 ILGenerator ig = ec.ig;
1228 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1229 ig.Emit (OpCodes.Ldnull);
1230 ig.Emit (OpCodes.Cgt_Un);
1233 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1235 ILGenerator ig = ec.ig;
1238 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1239 ig.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1242 Expression CreateConstantResult (bool result)
1245 Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1246 TypeManager.CSharpName (probe_type_expr.Type));
1248 Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1249 TypeManager.CSharpName (probe_type_expr.Type));
1251 return ReducedExpression.Create (new BoolConstant (result, loc), this);
1254 public override Expression DoResolve (EmitContext ec)
1256 if (base.DoResolve (ec) == null)
1260 bool d_is_nullable = false;
1262 if (expr is Constant) {
1264 // If E is a method group or the null literal, of if the type of E is a reference
1265 // type or a nullable type and the value of E is null, the result is false
1268 return CreateConstantResult (false);
1269 } else if (TypeManager.IsNullableType (d) && !TypeManager.ContainsGenericParameters (d)) {
1270 d = TypeManager.GetTypeArguments (d) [0];
1271 d_is_nullable = true;
1274 type = TypeManager.bool_type;
1275 eclass = ExprClass.Value;
1276 Type t = probe_type_expr.Type;
1277 bool t_is_nullable = false;
1278 if (TypeManager.IsNullableType (t) && !TypeManager.ContainsGenericParameters (t)) {
1279 t = TypeManager.GetTypeArguments (t) [0];
1280 t_is_nullable = true;
1283 if (t.IsValueType) {
1286 // D and T are the same value types but D can be null
1288 if (d_is_nullable && !t_is_nullable)
1289 return Nullable.HasValue.Create (expr, ec);
1292 // The result is true if D and T are the same value types
1294 return CreateConstantResult (true);
1297 if (TypeManager.IsGenericParameter (d))
1298 return ResolveGenericParameter (t, d);
1301 // An unboxing conversion exists
1303 if (Convert.ExplicitReferenceConversionExists (d, t))
1306 if (TypeManager.IsGenericParameter (t))
1307 return ResolveGenericParameter (d, t);
1309 if (d.IsValueType) {
1311 if (Convert.ImplicitBoxingConversionExists (expr, t, out temp))
1312 return CreateConstantResult (true);
1314 if (TypeManager.IsGenericParameter (d))
1315 return ResolveGenericParameter (t, d);
1317 if (TypeManager.ContainsGenericParameters (d))
1320 if (Convert.ImplicitReferenceConversionExists (expr, t) ||
1321 Convert.ExplicitReferenceConversionExists (d, t)) {
1327 return CreateConstantResult (false);
1330 Expression ResolveGenericParameter (Type d, Type t)
1333 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1334 if (constraints != null) {
1335 if (constraints.IsReferenceType && d.IsValueType)
1336 return CreateConstantResult (false);
1338 if (constraints.IsValueType && !d.IsValueType)
1339 return CreateConstantResult (false);
1342 expr = new BoxedCast (expr, d);
1349 protected override string OperatorName {
1350 get { return "is"; }
1355 /// Implementation of the `as' operator.
1357 public class As : Probe {
1359 Expression resolved_type;
1361 public As (Expression expr, Expression probe_type, Location l)
1362 : base (expr, probe_type, l)
1366 public override Expression CreateExpressionTree (EmitContext ec)
1368 ArrayList args = new ArrayList (2);
1369 args.Add (new Argument (expr.CreateExpressionTree (ec)));
1370 args.Add (new Argument (new TypeOf (probe_type_expr, loc)));
1371 return CreateExpressionFactoryCall ("TypeAs", args);
1374 public override void Emit (EmitContext ec)
1376 ILGenerator ig = ec.ig;
1381 ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
1384 if (TypeManager.IsNullableType (type))
1385 ig.Emit (OpCodes.Unbox_Any, type);
1389 static void Error_CannotConvertType (Type source, Type target, Location loc)
1391 Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1392 TypeManager.CSharpName (source),
1393 TypeManager.CSharpName (target));
1396 public override Expression DoResolve (EmitContext ec)
1398 if (resolved_type == null) {
1399 resolved_type = base.DoResolve (ec);
1401 if (resolved_type == null)
1405 type = probe_type_expr.Type;
1406 eclass = ExprClass.Value;
1407 Type etype = expr.Type;
1409 if (type.IsValueType && !TypeManager.IsNullableType (type)) {
1410 Report.Error (77, loc, "The `as' operator cannot be used with a non-nullable value type `{0}'",
1411 TypeManager.CSharpName (type));
1418 // If the type is a type parameter, ensure
1419 // that it is constrained by a class
1421 TypeParameterExpr tpe = probe_type_expr as TypeParameterExpr;
1423 GenericConstraints constraints = tpe.TypeParameter.GenericConstraints;
1426 if (constraints == null)
1429 if (!constraints.HasClassConstraint)
1430 if ((constraints.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == 0)
1434 Report.Error (413, loc,
1435 "The as operator requires that the `{0}' type parameter be constrained by a class",
1436 probe_type_expr.GetSignatureForError ());
1441 if (expr.IsNull && TypeManager.IsNullableType (type)) {
1442 return Nullable.LiftedNull.CreateFromExpression (this);
1445 Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
1452 if (Convert.ExplicitReferenceConversionExists (etype, type)){
1453 if (TypeManager.IsGenericParameter (etype))
1454 expr = new BoxedCast (expr, etype);
1460 if (TypeManager.ContainsGenericParameters (etype) ||
1461 TypeManager.ContainsGenericParameters (type)) {
1462 expr = new BoxedCast (expr, etype);
1467 Error_CannotConvertType (etype, type, loc);
1471 protected override string OperatorName {
1472 get { return "as"; }
1475 public override bool GetAttributableValue (Type value_type, out object value)
1477 return expr.GetAttributableValue (value_type, out value);
1482 /// This represents a typecast in the source language.
1484 /// FIXME: Cast expressions have an unusual set of parsing
1485 /// rules, we need to figure those out.
1487 public class Cast : Expression {
1488 Expression target_type;
1491 public Cast (Expression cast_type, Expression expr)
1492 : this (cast_type, expr, cast_type.Location)
1496 public Cast (Expression cast_type, Expression expr, Location loc)
1498 this.target_type = cast_type;
1502 if (target_type == TypeManager.system_void_expr)
1503 Error_VoidInvalidInTheContext (loc);
1506 public Expression TargetType {
1507 get { return target_type; }
1510 public Expression Expr {
1511 get { return expr; }
1514 public override Expression DoResolve (EmitContext ec)
1516 expr = expr.Resolve (ec);
1520 TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
1526 if (type.IsAbstract && type.IsSealed) {
1527 Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
1531 eclass = ExprClass.Value;
1533 Constant c = expr as Constant;
1535 c = c.TryReduce (ec, type, loc);
1540 if (type.IsPointer && !ec.InUnsafe) {
1544 expr = Convert.ExplicitConversion (ec, expr, type, loc);
1548 public override void Emit (EmitContext ec)
1550 throw new Exception ("Should not happen");
1553 protected override void CloneTo (CloneContext clonectx, Expression t)
1555 Cast target = (Cast) t;
1557 target.target_type = target_type.Clone (clonectx);
1558 target.expr = expr.Clone (clonectx);
1563 // C# 2.0 Default value expression
1565 public class DefaultValueExpression : Expression
1569 public DefaultValueExpression (Expression expr, Location loc)
1575 public override Expression CreateExpressionTree (EmitContext ec)
1577 ArrayList args = new ArrayList (2);
1578 args.Add (new Argument (this));
1579 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
1580 return CreateExpressionFactoryCall ("Constant", args);
1583 public override Expression DoResolve (EmitContext ec)
1585 TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
1591 if (type == TypeManager.void_type) {
1592 Error_VoidInvalidInTheContext (loc);
1596 if (TypeManager.IsGenericParameter (type)) {
1597 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints(type);
1598 if (constraints != null && constraints.IsReferenceType)
1599 return new EmptyConstantCast (new NullLiteral (Location), type);
1601 Constant c = New.Constantify (type);
1603 return new EmptyConstantCast (c, type);
1605 if (!TypeManager.IsValueType (type))
1606 return new EmptyConstantCast (new NullLiteral (Location), type);
1608 eclass = ExprClass.Variable;
1612 public override void Emit (EmitContext ec)
1614 LocalTemporary temp_storage = new LocalTemporary(type);
1616 temp_storage.AddressOf(ec, AddressOp.LoadStore);
1617 ec.ig.Emit(OpCodes.Initobj, type);
1618 temp_storage.Emit(ec);
1621 protected override void CloneTo (CloneContext clonectx, Expression t)
1623 DefaultValueExpression target = (DefaultValueExpression) t;
1625 target.expr = expr.Clone (clonectx);
1630 /// Binary operators
1632 public class Binary : Expression {
1634 protected class PredefinedOperator {
1635 protected readonly Type left;
1636 protected readonly Type right;
1637 public readonly Operator OperatorsMask;
1638 public Type ReturnType;
1640 public PredefinedOperator (Type ltype, Type rtype, Operator op_mask)
1641 : this (ltype, rtype, op_mask, ltype)
1645 public PredefinedOperator (Type type, Operator op_mask, Type return_type)
1646 : this (type, type, op_mask, return_type)
1650 public PredefinedOperator (Type type, Operator op_mask)
1651 : this (type, type, op_mask, type)
1655 public PredefinedOperator (Type ltype, Type rtype, Operator op_mask, Type return_type)
1657 if ((op_mask & Operator.ValuesOnlyMask) != 0)
1658 throw new InternalErrorException ("Only masked values can be used");
1662 this.OperatorsMask = op_mask;
1663 this.ReturnType = return_type;
1666 public virtual Expression ConvertResult (EmitContext ec, Binary b)
1668 b.type = ReturnType;
1671 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1674 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1679 public bool IsPrimitiveApplicable (Type type)
1682 // We are dealing with primitive types only
1684 return left == type;
1687 public virtual bool IsApplicable (EmitContext ec, Expression lexpr, Expression rexpr)
1689 if (TypeManager.IsEqual (left, lexpr.Type) &&
1690 TypeManager.IsEqual (right, rexpr.Type))
1693 return Convert.ImplicitConversionExists (ec, lexpr, left) &&
1694 Convert.ImplicitConversionExists (ec, rexpr, right);
1697 public PredefinedOperator ResolveBetterOperator (EmitContext ec, Expression lexpr, Expression rexpr, PredefinedOperator best_operator)
1700 if (left != null && best_operator.left != null) {
1701 result = MethodGroupExpr.BetterTypeConversion (ec, best_operator.left, left);
1705 // When second arguments are same as the first one, the result is same
1707 if (left != right || best_operator.left != best_operator.right) {
1708 result |= MethodGroupExpr.BetterTypeConversion (ec, best_operator.right, right);
1711 if (result == 0 || result > 2)
1714 return result == 1 ? best_operator : this;
1718 class PredefinedStringOperator : PredefinedOperator {
1719 public PredefinedStringOperator (Type type, Operator op_mask)
1720 : base (type, op_mask, type)
1722 ReturnType = TypeManager.string_type;
1725 public PredefinedStringOperator (Type ltype, Type rtype, Operator op_mask)
1726 : base (ltype, rtype, op_mask)
1728 ReturnType = TypeManager.string_type;
1731 public override Expression ConvertResult (EmitContext ec, Binary b)
1734 // Use original expression for nullable arguments
1736 Nullable.Unwrap unwrap = b.left as Nullable.Unwrap;
1738 b.left = unwrap.Original;
1740 unwrap = b.right as Nullable.Unwrap;
1742 b.right = unwrap.Original;
1744 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1745 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
1748 // Start a new concat expression using converted expression
1750 return new StringConcat (ec, b.loc, b.left, b.right).Resolve (ec);
1754 class PredefinedShiftOperator : PredefinedOperator {
1755 public PredefinedShiftOperator (Type ltype, Operator op_mask) :
1756 base (ltype, TypeManager.int32_type, op_mask)
1760 public override Expression ConvertResult (EmitContext ec, Binary b)
1762 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
1764 Expression expr_tree_expr = EmptyCast.Create (b.right, TypeManager.int32_type);
1766 int right_mask = left == TypeManager.int32_type || left == TypeManager.uint32_type ? 0x1f : 0x3f;
1769 // b = b.left >> b.right & (0x1f|0x3f)
1771 b.right = new Binary (Operator.BitwiseAnd,
1772 b.right, new IntConstant (right_mask, b.right.Location)).Resolve (ec);
1775 // Expression tree representation does not use & mask
1777 b.right = ReducedExpression.Create (b.right, expr_tree_expr).Resolve (ec);
1778 b.type = ReturnType;
1783 class PredefinedPointerOperator : PredefinedOperator {
1784 public PredefinedPointerOperator (Type ltype, Type rtype, Operator op_mask)
1785 : base (ltype, rtype, op_mask)
1789 public PredefinedPointerOperator (Type type, Operator op_mask, Type return_type)
1790 : base (type, op_mask, return_type)
1794 public override bool IsApplicable (EmitContext ec, Expression lexpr, Expression rexpr)
1797 if (!lexpr.Type.IsPointer)
1800 if (!Convert.ImplicitConversionExists (ec, lexpr, left))
1804 if (right == null) {
1805 if (!rexpr.Type.IsPointer)
1808 if (!Convert.ImplicitConversionExists (ec, rexpr, right))
1815 public override Expression ConvertResult (EmitContext ec, Binary b)
1817 base.ConvertResult (ec, b);
1819 Type r_type = ReturnType;
1820 if (r_type == null) {
1821 r_type = b.left.Type;
1823 r_type = b.right.Type;
1826 return new PointerArithmetic (b.oper == Operator.Addition,
1827 b.left, b.right, r_type, b.loc).Resolve (ec);
1832 public enum Operator {
1833 Multiply = 0 | ArithmeticMask,
1834 Division = 1 | ArithmeticMask,
1835 Modulus = 2 | ArithmeticMask,
1836 Addition = 3 | ArithmeticMask | AdditionMask,
1837 Subtraction = 4 | ArithmeticMask | SubtractionMask,
1839 LeftShift = 5 | ShiftMask,
1840 RightShift = 6 | ShiftMask,
1842 LessThan = 7 | ComparisonMask | RelationalMask,
1843 GreaterThan = 8 | ComparisonMask | RelationalMask,
1844 LessThanOrEqual = 9 | ComparisonMask | RelationalMask,
1845 GreaterThanOrEqual = 10 | ComparisonMask | RelationalMask,
1846 Equality = 11 | ComparisonMask | EqualityMask,
1847 Inequality = 12 | ComparisonMask | EqualityMask,
1849 BitwiseAnd = 13 | BitwiseMask,
1850 ExclusiveOr = 14 | BitwiseMask,
1851 BitwiseOr = 15 | BitwiseMask,
1853 LogicalAnd = 16 | LogicalMask,
1854 LogicalOr = 17 | LogicalMask,
1859 ValuesOnlyMask = ArithmeticMask - 1,
1860 ArithmeticMask = 1 << 5,
1862 ComparisonMask = 1 << 7,
1863 EqualityMask = 1 << 8,
1864 BitwiseMask = 1 << 9,
1865 LogicalMask = 1 << 10,
1866 AdditionMask = 1 << 11,
1867 SubtractionMask = 1 << 12,
1868 RelationalMask = 1 << 13
1871 readonly Operator oper;
1872 protected Expression left, right;
1873 readonly bool is_compound;
1874 Expression enum_conversion;
1876 // This must be kept in sync with Operator!!!
1877 public static readonly string [] oper_names;
1879 static PredefinedOperator [] standard_operators;
1880 static PredefinedOperator [] pointer_operators;
1884 oper_names = new string [18];
1886 oper_names [(int) (Operator.Multiply & Operator.ValuesOnlyMask)] = "op_Multiply";
1887 oper_names [(int) (Operator.Division & Operator.ValuesOnlyMask)] = "op_Division";
1888 oper_names [(int) (Operator.Modulus & Operator.ValuesOnlyMask)] = "op_Modulus";
1889 oper_names [(int) (Operator.Addition & Operator.ValuesOnlyMask)] = "op_Addition";
1890 oper_names [(int) (Operator.Subtraction & Operator.ValuesOnlyMask)] = "op_Subtraction";
1891 oper_names [(int) (Operator.LeftShift & Operator.ValuesOnlyMask)] = "op_LeftShift";
1892 oper_names [(int) (Operator.RightShift & Operator.ValuesOnlyMask)] = "op_RightShift";
1893 oper_names [(int) (Operator.LessThan & Operator.ValuesOnlyMask)] = "op_LessThan";
1894 oper_names [(int) (Operator.GreaterThan & Operator.ValuesOnlyMask)] = "op_GreaterThan";
1895 oper_names [(int) (Operator.LessThanOrEqual & Operator.ValuesOnlyMask)] = "op_LessThanOrEqual";
1896 oper_names [(int) (Operator.GreaterThanOrEqual & Operator.ValuesOnlyMask)] = "op_GreaterThanOrEqual";
1897 oper_names [(int) (Operator.Equality & Operator.ValuesOnlyMask)] = "op_Equality";
1898 oper_names [(int) (Operator.Inequality & Operator.ValuesOnlyMask)] = "op_Inequality";
1899 oper_names [(int) (Operator.BitwiseAnd & Operator.ValuesOnlyMask)] = "op_BitwiseAnd";
1900 oper_names [(int) (Operator.BitwiseOr & Operator.ValuesOnlyMask)] = "op_BitwiseOr";
1901 oper_names [(int) (Operator.ExclusiveOr & Operator.ValuesOnlyMask)] = "op_ExclusiveOr";
1902 oper_names [(int) (Operator.LogicalOr & Operator.ValuesOnlyMask)] = "op_LogicalOr";
1903 oper_names [(int) (Operator.LogicalAnd & Operator.ValuesOnlyMask)] = "op_LogicalAnd";
1906 public Binary (Operator oper, Expression left, Expression right, bool isCompound)
1907 : this (oper, left, right)
1909 this.is_compound = isCompound;
1912 public Binary (Operator oper, Expression left, Expression right)
1917 this.loc = left.Location;
1920 public Operator Oper {
1927 /// Returns a stringified representation of the Operator
1929 string OperName (Operator oper)
1933 case Operator.Multiply:
1936 case Operator.Division:
1939 case Operator.Modulus:
1942 case Operator.Addition:
1945 case Operator.Subtraction:
1948 case Operator.LeftShift:
1951 case Operator.RightShift:
1954 case Operator.LessThan:
1957 case Operator.GreaterThan:
1960 case Operator.LessThanOrEqual:
1963 case Operator.GreaterThanOrEqual:
1966 case Operator.Equality:
1969 case Operator.Inequality:
1972 case Operator.BitwiseAnd:
1975 case Operator.BitwiseOr:
1978 case Operator.ExclusiveOr:
1981 case Operator.LogicalOr:
1984 case Operator.LogicalAnd:
1988 s = oper.ToString ();
1998 static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
2000 Error_OperatorCannotBeApplied (loc, name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
2003 public static void Error_OperatorCannotBeApplied (Location loc, string name, string left, string right)
2005 Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2009 protected void Error_OperatorCannotBeApplied (Expression left, Expression right)
2012 // TODO: This should be handled as Type of method group in CSharpName
2013 if (left.eclass == ExprClass.MethodGroup)
2014 l = left.ExprClassName;
2016 l = TypeManager.CSharpName (left.Type);
2018 if (right.eclass == ExprClass.MethodGroup)
2019 r = right.ExprClassName;
2021 r = TypeManager.CSharpName (right.Type);
2023 Error_OperatorCannotBeApplied (Location, OperName (oper), l, r);
2026 public static string GetOperatorMetadataName (Operator op)
2028 return oper_names [(int)(op & Operator.ValuesOnlyMask)];
2031 static bool IsUnsigned (Type t)
2034 t = TypeManager.GetElementType (t);
2036 return (t == TypeManager.uint32_type || t == TypeManager.uint64_type ||
2037 t == TypeManager.ushort_type || t == TypeManager.byte_type);
2040 static bool IsFloat (Type t)
2042 return t == TypeManager.float_type || t == TypeManager.double_type;
2045 Expression ResolveOperator (EmitContext ec)
2048 Type r = right.Type;
2050 bool primitives_only = false;
2052 if (standard_operators == null)
2053 CreateStandardOperatorsTable ();
2056 // Handles predefined primitive types
2058 if (TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r)) {
2059 if ((oper & Operator.ShiftMask) == 0) {
2060 if (l != TypeManager.bool_type && !DoBinaryOperatorPromotion (ec))
2063 primitives_only = true;
2067 if (l.IsPointer || r.IsPointer)
2068 return ResolveOperatorPointer (ec, l, r);
2071 bool lenum = TypeManager.IsEnumType (l);
2072 bool renum = TypeManager.IsEnumType (r);
2073 if (lenum || renum) {
2074 expr = ResolveOperatorEnum (ec, lenum, renum, l, r);
2076 // TODO: Can this be ambiguous
2082 if (oper == Operator.Addition || oper == Operator.Subtraction) {
2083 if (TypeManager.IsDelegateType (l))
2084 return ResolveOperatorDelegateBinary (ec, l, r);
2088 expr = ResolveUserOperator (ec, l, r);
2092 // Predefined reference types equality
2093 if ((oper & Operator.EqualityMask) != 0) {
2094 expr = ResolveOperatorEqualityRerefence (ec, l, r);
2100 return ResolveOperatorPredefined (ec, standard_operators, primitives_only, null);
2103 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
2104 // if 'left' is not an enumeration constant, create one from the type of 'right'
2105 Constant EnumLiftUp (EmitContext ec, Constant left, Constant right, Location loc)
2108 case Operator.BitwiseOr:
2109 case Operator.BitwiseAnd:
2110 case Operator.ExclusiveOr:
2111 case Operator.Equality:
2112 case Operator.Inequality:
2113 case Operator.LessThan:
2114 case Operator.LessThanOrEqual:
2115 case Operator.GreaterThan:
2116 case Operator.GreaterThanOrEqual:
2117 if (TypeManager.IsEnumType (left.Type))
2120 if (left.IsZeroInteger)
2121 return left.TryReduce (ec, right.Type, loc);
2125 case Operator.Addition:
2126 case Operator.Subtraction:
2129 case Operator.Multiply:
2130 case Operator.Division:
2131 case Operator.Modulus:
2132 case Operator.LeftShift:
2133 case Operator.RightShift:
2134 if (TypeManager.IsEnumType (right.Type) || TypeManager.IsEnumType (left.Type))
2138 Error_OperatorCannotBeApplied (this.left, this.right);
2143 // The `|' operator used on types which were extended is dangerous
2145 void CheckBitwiseOrOnSignExtended ()
2147 OpcodeCast lcast = left as OpcodeCast;
2148 if (lcast != null) {
2149 if (IsUnsigned (lcast.UnderlyingType))
2153 OpcodeCast rcast = right as OpcodeCast;
2154 if (rcast != null) {
2155 if (IsUnsigned (rcast.UnderlyingType))
2159 if (lcast == null && rcast == null)
2162 // FIXME: consider constants
2164 Report.Warning (675, 3, loc,
2165 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2166 TypeManager.CSharpName (lcast != null ? lcast.UnderlyingType : rcast.UnderlyingType));
2169 static void CreatePointerOperatorsTable ()
2171 ArrayList temp = new ArrayList ();
2174 // Pointer arithmetic:
2176 // T* operator + (T* x, int y); T* operator - (T* x, int y);
2177 // T* operator + (T* x, uint y); T* operator - (T* x, uint y);
2178 // T* operator + (T* x, long y); T* operator - (T* x, long y);
2179 // T* operator + (T* x, ulong y); T* operator - (T* x, ulong y);
2181 temp.Add (new PredefinedPointerOperator (null, TypeManager.int32_type, Operator.AdditionMask | Operator.SubtractionMask));
2182 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint32_type, Operator.AdditionMask | Operator.SubtractionMask));
2183 temp.Add (new PredefinedPointerOperator (null, TypeManager.int64_type, Operator.AdditionMask | Operator.SubtractionMask));
2184 temp.Add (new PredefinedPointerOperator (null, TypeManager.uint64_type, Operator.AdditionMask | Operator.SubtractionMask));
2187 // T* operator + (int y, T* x);
2188 // T* operator + (uint y, T *x);
2189 // T* operator + (long y, T *x);
2190 // T* operator + (ulong y, T *x);
2192 temp.Add (new PredefinedPointerOperator (TypeManager.int32_type, null, Operator.AdditionMask));
2193 temp.Add (new PredefinedPointerOperator (TypeManager.uint32_type, null, Operator.AdditionMask));
2194 temp.Add (new PredefinedPointerOperator (TypeManager.int64_type, null, Operator.AdditionMask));
2195 temp.Add (new PredefinedPointerOperator (TypeManager.uint64_type, null, Operator.AdditionMask));
2198 // long operator - (T* x, T *y)
2200 temp.Add (new PredefinedPointerOperator (null, Operator.SubtractionMask, TypeManager.int64_type));
2202 pointer_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
2205 static void CreateStandardOperatorsTable ()
2207 ArrayList temp = new ArrayList ();
2208 Type bool_type = TypeManager.bool_type;
2210 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2211 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2212 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2213 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ArithmeticMask | Operator.BitwiseMask));
2214 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ArithmeticMask));
2215 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ArithmeticMask));
2217 temp.Add (new PredefinedOperator (TypeManager.int32_type, Operator.ComparisonMask, bool_type));
2218 temp.Add (new PredefinedOperator (TypeManager.uint32_type, Operator.ComparisonMask, bool_type));
2219 temp.Add (new PredefinedOperator (TypeManager.int64_type, Operator.ComparisonMask, bool_type));
2220 temp.Add (new PredefinedOperator (TypeManager.uint64_type, Operator.ComparisonMask, bool_type));
2221 temp.Add (new PredefinedOperator (TypeManager.float_type, Operator.ComparisonMask, bool_type));
2222 temp.Add (new PredefinedOperator (TypeManager.double_type, Operator.ComparisonMask, bool_type));
2224 temp.Add (new PredefinedOperator (TypeManager.string_type, Operator.EqualityMask, bool_type));
2226 temp.Add (new PredefinedStringOperator (TypeManager.string_type, Operator.AdditionMask));
2227 temp.Add (new PredefinedStringOperator (TypeManager.string_type, TypeManager.object_type, Operator.AdditionMask));
2228 temp.Add (new PredefinedStringOperator (TypeManager.object_type, TypeManager.string_type, Operator.AdditionMask));
2230 temp.Add (new PredefinedOperator (bool_type,
2231 Operator.BitwiseMask | Operator.LogicalMask | Operator.EqualityMask, bool_type));
2233 temp.Add (new PredefinedShiftOperator (TypeManager.int32_type, Operator.ShiftMask));
2234 temp.Add (new PredefinedShiftOperator (TypeManager.uint32_type, Operator.ShiftMask));
2235 temp.Add (new PredefinedShiftOperator (TypeManager.int64_type, Operator.ShiftMask));
2236 temp.Add (new PredefinedShiftOperator (TypeManager.uint64_type, Operator.ShiftMask));
2238 standard_operators = (PredefinedOperator []) temp.ToArray (typeof (PredefinedOperator));
2242 // Rules used during binary numeric promotion
2244 static bool DoNumericPromotion (ref Expression prim_expr, ref Expression second_expr, Type type)
2249 Constant c = prim_expr as Constant;
2251 temp = c.ConvertImplicitly (type);
2258 if (type == TypeManager.uint32_type) {
2259 etype = prim_expr.Type;
2260 if (etype == TypeManager.int32_type || etype == TypeManager.short_type || etype == TypeManager.sbyte_type) {
2261 type = TypeManager.int64_type;
2263 if (type != second_expr.Type) {
2264 c = second_expr as Constant;
2266 temp = c.ConvertImplicitly (type);
2268 temp = Convert.ImplicitNumericConversion (second_expr, type);
2274 } else if (type == TypeManager.uint64_type) {
2276 // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
2278 if (type == TypeManager.int32_type || type == TypeManager.int64_type ||
2279 type == TypeManager.sbyte_type || type == TypeManager.sbyte_type)
2283 temp = Convert.ImplicitNumericConversion (prim_expr, type);
2292 // 7.2.6.2 Binary numeric promotions
2294 public bool DoBinaryOperatorPromotion (EmitContext ec)
2296 Type ltype = left.Type;
2297 Type rtype = right.Type;
2300 foreach (Type t in ConstantFold.binary_promotions) {
2302 return t == rtype || DoNumericPromotion (ref right, ref left, t);
2305 return t == ltype || DoNumericPromotion (ref left, ref right, t);
2308 Type int32 = TypeManager.int32_type;
2309 if (ltype != int32) {
2310 Constant c = left as Constant;
2312 temp = c.ConvertImplicitly (int32);
2314 temp = Convert.ImplicitNumericConversion (left, int32);
2321 if (rtype != int32) {
2322 Constant c = right as Constant;
2324 temp = c.ConvertImplicitly (int32);
2326 temp = Convert.ImplicitNumericConversion (right, int32);
2336 public override Expression DoResolve (EmitContext ec)
2341 if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
2342 left = ((ParenthesizedExpression) left).Expr;
2343 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
2347 if (left.eclass == ExprClass.Type) {
2348 Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
2352 left = left.Resolve (ec);
2357 Constant lc = left as Constant;
2359 if (lc != null && lc.Type == TypeManager.bool_type &&
2360 ((oper == Operator.LogicalAnd && lc.IsDefaultValue) ||
2361 (oper == Operator.LogicalOr && !lc.IsDefaultValue))) {
2363 // FIXME: resolve right expression as unreachable
2364 // right.Resolve (ec);
2366 Report.Warning (429, 4, loc, "Unreachable expression code detected");
2370 right = right.Resolve (ec);
2374 eclass = ExprClass.Value;
2375 Constant rc = right as Constant;
2377 // The conversion rules are ignored in enum context but why
2378 if (!ec.InEnumContext && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
2379 left = lc = EnumLiftUp (ec, lc, rc, loc);
2383 right = rc = EnumLiftUp (ec, rc, lc, loc);
2388 if (rc != null && lc != null) {
2389 int prev_e = Report.Errors;
2390 Expression e = ConstantFold.BinaryFold (
2391 ec, oper, lc, rc, loc);
2392 if (e != null || Report.Errors != prev_e)
2395 if ((oper == Operator.BitwiseAnd || oper == Operator.LogicalAnd) &&
2396 ((lc != null && lc.IsDefaultValue) || (rc != null && rc.IsDefaultValue))) {
2398 if ((ResolveOperator (ec)) == null) {
2399 Error_OperatorCannotBeApplied (left, right);
2408 // The result is a constant with side-effect
2409 return new SideEffectConstant (lc, right, loc);
2413 // Comparison warnings
2414 if ((oper & Operator.ComparisonMask) != 0) {
2415 if (left.Equals (right)) {
2416 Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
2418 CheckUselessComparison (lc, right.Type);
2419 CheckUselessComparison (rc, left.Type);
2422 if (RootContext.Version >= LanguageVersion.ISO_2 &&
2423 (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type) ||
2424 (left is NullLiteral && right.Type.IsValueType) || (right is NullLiteral && left.Type.IsValueType)))
2425 return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
2427 return DoResolveCore (ec, left, right);
2430 protected Expression DoResolveCore (EmitContext ec, Expression left_orig, Expression right_orig)
2432 Expression expr = ResolveOperator (ec);
2434 Error_OperatorCannotBeApplied (left_orig, right_orig);
2436 if (left == null || right == null)
2437 throw new InternalErrorException ("Invalid conversion");
2439 if (oper == Operator.BitwiseOr)
2440 CheckBitwiseOrOnSignExtended ();
2446 // D operator + (D x, D y)
2447 // D operator - (D x, D y)
2449 Expression ResolveOperatorDelegateBinary (EmitContext ec, Type l, Type r)
2451 if (((right.eclass == ExprClass.MethodGroup) || (r == TypeManager.anonymous_method_type))) {
2452 if ((RootContext.Version != LanguageVersion.ISO_1)) {
2453 Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
2460 if (!TypeManager.IsEqual (l, r) && !(right is NullLiteral))
2465 ArrayList args = new ArrayList (2);
2467 args = new ArrayList (2);
2468 args.Add (new Argument (left, Argument.AType.Expression));
2469 args.Add (new Argument (right, Argument.AType.Expression));
2471 if (oper == Operator.Addition) {
2472 if (TypeManager.delegate_combine_delegate_delegate == null) {
2473 TypeManager.delegate_combine_delegate_delegate = TypeManager.GetPredefinedMethod (
2474 TypeManager.delegate_type, "Combine", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2477 method = TypeManager.delegate_combine_delegate_delegate;
2479 if (TypeManager.delegate_remove_delegate_delegate == null) {
2480 TypeManager.delegate_remove_delegate_delegate = TypeManager.GetPredefinedMethod (
2481 TypeManager.delegate_type, "Remove", loc, TypeManager.delegate_type, TypeManager.delegate_type);
2484 method = TypeManager.delegate_remove_delegate_delegate;
2487 return new BinaryDelegate (l, method, args);
2491 // Enumeration operators
2493 Expression ResolveOperatorEnum (EmitContext ec, bool lenum, bool renum, Type ltype, Type rtype)
2496 // bool operator == (E x, E y);
2497 // bool operator != (E x, E y);
2498 // bool operator < (E x, E y);
2499 // bool operator > (E x, E y);
2500 // bool operator <= (E x, E y);
2501 // bool operator >= (E x, E y);
2503 // E operator & (E x, E y);
2504 // E operator | (E x, E y);
2505 // E operator ^ (E x, E y);
2507 // U operator - (E e, E f)
2508 // E operator - (E e, U x)
2510 // E operator + (U x, E e)
2511 // E operator + (E e, U x)
2513 if (!((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0 ||
2514 (oper == Operator.Subtraction && lenum) || (oper == Operator.Addition && lenum != renum)))
2517 Expression ltemp = left;
2518 Expression rtemp = right;
2519 Type underlying_type;
2521 if (TypeManager.IsEqual (ltype, rtype)) {
2522 underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
2524 if (left is Constant)
2525 left = ((Constant) left).ConvertExplicitly (false, underlying_type);
2527 left = EmptyCast.Create (left, underlying_type);
2529 if (right is Constant)
2530 right = ((Constant) right).ConvertExplicitly (false, underlying_type);
2532 right = EmptyCast.Create (right, underlying_type);
2534 if (oper != Operator.Subtraction && oper != Operator.Addition) {
2535 Constant c = right as Constant;
2536 if (c == null || !c.IsDefaultValue)
2540 underlying_type = TypeManager.GetEnumUnderlyingType (ltype);
2541 if (left is Constant)
2542 left = ((Constant) left).ConvertExplicitly (false, underlying_type);
2544 left = EmptyCast.Create (left, underlying_type);
2546 if (oper != Operator.Addition) {
2547 Constant c = left as Constant;
2548 if (c == null || !c.IsDefaultValue)
2552 underlying_type = TypeManager.GetEnumUnderlyingType (rtype);
2553 if (right is Constant)
2554 right = ((Constant) right).ConvertExplicitly (false, underlying_type);
2556 right = EmptyCast.Create (right, underlying_type);
2562 // C# specification uses explicit cast syntax which means binary promotion
2563 // should happen, however it seems that csc does not do that
2565 if (!DoBinaryOperatorPromotion (ec)) {
2571 Type res_type = null;
2572 if ((oper & Operator.BitwiseMask) != 0 || oper == Operator.Subtraction || oper == Operator.Addition) {
2573 Type promoted_type = lenum ? left.Type : right.Type;
2574 enum_conversion = Convert.ExplicitNumericConversion (
2575 new EmptyExpression (promoted_type), underlying_type);
2577 if (oper == Operator.Subtraction && renum && lenum)
2578 res_type = underlying_type;
2579 else if (oper == Operator.Addition && renum)
2585 Expression expr = ResolveOperatorPredefined (ec, standard_operators, true, res_type);
2586 if (!is_compound || expr == null)
2590 // TODO: Need to corectly implemented Coumpound Assigment for all operators
2593 if (Convert.ImplicitConversionExists (ec, left, rtype))
2596 if (!Convert.ImplicitConversionExists (ec, ltemp, rtype))
2599 expr = Convert.ExplicitConversion (ec, expr, rtype, loc);
2604 // 7.9.6 Reference type equality operators
2606 Binary ResolveOperatorEqualityRerefence (EmitContext ec, Type l, Type r)
2609 // operator != (object a, object b)
2610 // operator == (object a, object b)
2613 // TODO: this method is almost equivalent to Convert.ImplicitReferenceConversion
2615 if (left.eclass == ExprClass.MethodGroup || right.eclass == ExprClass.MethodGroup)
2618 type = TypeManager.bool_type;
2619 GenericConstraints constraints;
2621 bool lgen = TypeManager.IsGenericParameter (l);
2623 if (TypeManager.IsEqual (l, r)) {
2626 // Only allow to compare same reference type parameter
2628 constraints = TypeManager.GetTypeParameterConstraints (l);
2629 if (constraints != null && constraints.IsReferenceType)
2635 if (l == TypeManager.anonymous_method_type)
2638 if (TypeManager.IsValueType (l))
2644 bool rgen = TypeManager.IsGenericParameter (r);
2647 // a, Both operands are reference-type values or the value null
2648 // b, One operand is a value of type T where T is a type-parameter and
2649 // the other operand is the value null. Furthermore T does not have the
2650 // value type constrain
2652 if (left is NullLiteral || right is NullLiteral) {
2654 constraints = TypeManager.GetTypeParameterConstraints (l);
2655 if (constraints != null && constraints.HasValueTypeConstraint)
2658 left = new BoxedCast (left, TypeManager.object_type);
2663 constraints = TypeManager.GetTypeParameterConstraints (r);
2664 if (constraints != null && constraints.HasValueTypeConstraint)
2667 right = new BoxedCast (right, TypeManager.object_type);
2673 // An interface is converted to the object before the
2674 // standard conversion is applied. It's not clear from the
2675 // standard but it looks like it works like that.
2678 constraints = TypeManager.GetTypeParameterConstraints (l);
2679 if (constraints == null || constraints.IsReferenceType)
2681 } else if (l.IsInterface) {
2682 l = TypeManager.object_type;
2683 } else if (l.IsValueType) {
2688 constraints = TypeManager.GetTypeParameterConstraints (r);
2689 if (constraints == null || constraints.IsReferenceType)
2691 } else if (r.IsInterface) {
2692 r = TypeManager.object_type;
2693 } else if (r.IsValueType) {
2698 const string ref_comparison = "Possible unintended reference comparison. " +
2699 "Consider casting the {0} side of the expression to `string' to compare the values";
2702 // A standard implicit conversion exists from the type of either
2703 // operand to the type of the other operand
2705 if (Convert.ImplicitReferenceConversionExists (left, r)) {
2706 if (l == TypeManager.string_type)
2707 Report.Warning (253, 2, loc, ref_comparison, "right");
2712 if (Convert.ImplicitReferenceConversionExists (right, l)) {
2713 if (r == TypeManager.string_type)
2714 Report.Warning (252, 2, loc, ref_comparison, "left");
2723 Expression ResolveOperatorPointer (EmitContext ec, Type l, Type r)
2726 // bool operator == (void* x, void* y);
2727 // bool operator != (void* x, void* y);
2728 // bool operator < (void* x, void* y);
2729 // bool operator > (void* x, void* y);
2730 // bool operator <= (void* x, void* y);
2731 // bool operator >= (void* x, void* y);
2733 if ((oper & Operator.ComparisonMask) != 0) {
2736 temp = Convert.ImplicitConversion (ec, left, r, left.Location);
2743 temp = Convert.ImplicitConversion (ec, right, l, right.Location);
2749 type = TypeManager.bool_type;
2753 if (pointer_operators == null)
2754 CreatePointerOperatorsTable ();
2756 return ResolveOperatorPredefined (ec, pointer_operators, false, null);
2760 // Build-in operators method overloading
2762 protected virtual Expression ResolveOperatorPredefined (EmitContext ec, PredefinedOperator [] operators, bool primitives_only, Type enum_type)
2764 PredefinedOperator best_operator = null;
2766 Operator oper_mask = oper & ~Operator.ValuesOnlyMask;
2768 foreach (PredefinedOperator po in operators) {
2769 if ((po.OperatorsMask & oper_mask) == 0)
2772 if (primitives_only) {
2773 if (!po.IsPrimitiveApplicable (l))
2776 if (!po.IsApplicable (ec, left, right))
2780 if (best_operator == null) {
2782 if (primitives_only)
2788 best_operator = po.ResolveBetterOperator (ec, left, right, best_operator);
2790 if (best_operator == null) {
2791 Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
2792 OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
2799 if (best_operator == null)
2802 Expression expr = best_operator.ConvertResult (ec, this);
2803 if (enum_type == null)
2807 // HACK: required by enum_conversion
2809 expr.Type = enum_type;
2810 return EmptyCast.Create (expr, enum_type);
2814 // Performs user-operator overloading
2816 protected virtual Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
2819 if (oper == Operator.LogicalAnd)
2820 user_oper = Operator.BitwiseAnd;
2821 else if (oper == Operator.LogicalOr)
2822 user_oper = Operator.BitwiseOr;
2826 string op = GetOperatorMetadataName (user_oper);
2828 MethodGroupExpr union;
2829 MethodGroupExpr left_operators = MemberLookup (ec.ContainerType, l, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
2830 if (!TypeManager.IsEqual (r, l)) {
2831 MethodGroupExpr right_operators = MemberLookup (
2832 ec.ContainerType, r, op, MemberTypes.Method, AllBindingFlags, loc) as MethodGroupExpr;
2833 union = MethodGroupExpr.MakeUnionSet (left_operators, right_operators, loc);
2835 union = left_operators;
2840 ArrayList args = new ArrayList (2);
2841 Argument larg = new Argument (left);
2843 Argument rarg = new Argument (right);
2846 union = union.OverloadResolve (ec, ref args, true, loc);
2850 Expression oper_expr;
2852 // TODO: CreateExpressionTree is allocated every time
2853 if (user_oper != oper) {
2854 oper_expr = new ConditionalLogicalOperator (union, args, CreateExpressionTree,
2855 oper == Operator.LogicalAnd, loc).Resolve (ec);
2857 oper_expr = new UserOperatorCall (union, args, CreateExpressionTree, loc);
2860 // This is used to check if a test 'x == null' can be optimized to a reference equals,
2861 // and not invoke user operator
2863 if ((oper & Operator.EqualityMask) != 0) {
2864 if ((left is NullLiteral && IsBuildInEqualityOperator (r)) ||
2865 (right is NullLiteral && IsBuildInEqualityOperator (l))) {
2866 type = TypeManager.bool_type;
2867 if (left is NullLiteral || right is NullLiteral)
2868 oper_expr = ReducedExpression.Create (this, oper_expr).Resolve (ec);
2869 } else if (union.DeclaringType == TypeManager.delegate_type && l != r) {
2871 // Two System.Delegate(s) are never equal
2883 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
2888 private void CheckUselessComparison (Constant c, Type type)
2890 if (c == null || !IsTypeIntegral (type)
2891 || c is StringConstant
2892 || c is BoolConstant
2893 || c is FloatConstant
2894 || c is DoubleConstant
2895 || c is DecimalConstant
2901 if (c is ULongConstant) {
2902 ulong uvalue = ((ULongConstant) c).Value;
2903 if (uvalue > long.MaxValue) {
2904 if (type == TypeManager.byte_type ||
2905 type == TypeManager.sbyte_type ||
2906 type == TypeManager.short_type ||
2907 type == TypeManager.ushort_type ||
2908 type == TypeManager.int32_type ||
2909 type == TypeManager.uint32_type ||
2910 type == TypeManager.int64_type ||
2911 type == TypeManager.char_type)
2912 WarnUselessComparison (type);
2915 value = (long) uvalue;
2917 else if (c is ByteConstant)
2918 value = ((ByteConstant) c).Value;
2919 else if (c is SByteConstant)
2920 value = ((SByteConstant) c).Value;
2921 else if (c is ShortConstant)
2922 value = ((ShortConstant) c).Value;
2923 else if (c is UShortConstant)
2924 value = ((UShortConstant) c).Value;
2925 else if (c is IntConstant)
2926 value = ((IntConstant) c).Value;
2927 else if (c is UIntConstant)
2928 value = ((UIntConstant) c).Value;
2929 else if (c is LongConstant)
2930 value = ((LongConstant) c).Value;
2931 else if (c is CharConstant)
2932 value = ((CharConstant)c).Value;
2937 if (IsValueOutOfRange (value, type))
2938 WarnUselessComparison (type);
2941 private bool IsValueOutOfRange (long value, Type type)
2943 if (IsTypeUnsigned (type) && value < 0)
2945 return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
2946 type == TypeManager.byte_type && value >= 0x100 ||
2947 type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
2948 type == TypeManager.ushort_type && value >= 0x10000 ||
2949 type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
2950 type == TypeManager.uint32_type && value >= 0x100000000;
2953 static bool IsBuildInEqualityOperator (Type t)
2955 return t == TypeManager.object_type || t == TypeManager.string_type ||
2956 t == TypeManager.delegate_type || TypeManager.IsDelegateType (t);
2959 private static bool IsTypeIntegral (Type type)
2961 return type == TypeManager.uint64_type ||
2962 type == TypeManager.int64_type ||
2963 type == TypeManager.uint32_type ||
2964 type == TypeManager.int32_type ||
2965 type == TypeManager.ushort_type ||
2966 type == TypeManager.short_type ||
2967 type == TypeManager.sbyte_type ||
2968 type == TypeManager.byte_type ||
2969 type == TypeManager.char_type;
2972 private static bool IsTypeUnsigned (Type type)
2974 return type == TypeManager.uint64_type ||
2975 type == TypeManager.uint32_type ||
2976 type == TypeManager.ushort_type ||
2977 type == TypeManager.byte_type ||
2978 type == TypeManager.char_type;
2981 private void WarnUselessComparison (Type type)
2983 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}'",
2984 TypeManager.CSharpName (type));
2988 /// EmitBranchable is called from Statement.EmitBoolExpression in the
2989 /// context of a conditional bool expression. This function will return
2990 /// false if it is was possible to use EmitBranchable, or true if it was.
2992 /// The expression's code is generated, and we will generate a branch to `target'
2993 /// if the resulting expression value is equal to isTrue
2995 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2997 ILGenerator ig = ec.ig;
3000 // This is more complicated than it looks, but its just to avoid
3001 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
3002 // but on top of that we want for == and != to use a special path
3003 // if we are comparing against null
3005 if ((oper == Operator.Equality || oper == Operator.Inequality) && (left is Constant || right is Constant)) {
3006 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
3009 // put the constant on the rhs, for simplicity
3011 if (left is Constant) {
3012 Expression swap = right;
3017 if (((Constant) right).IsZeroInteger) {
3018 left.EmitBranchable (ec, target, my_on_true);
3021 if (right.Type == TypeManager.bool_type) {
3022 // right is a boolean, and it's not 'false' => it is 'true'
3023 left.EmitBranchable (ec, target, !my_on_true);
3027 } else if (oper == Operator.LogicalAnd) {
3030 Label tests_end = ig.DefineLabel ();
3032 left.EmitBranchable (ec, tests_end, false);
3033 right.EmitBranchable (ec, target, true);
3034 ig.MarkLabel (tests_end);
3037 // This optimizes code like this
3038 // if (true && i > 4)
3040 if (!(left is Constant))
3041 left.EmitBranchable (ec, target, false);
3043 if (!(right is Constant))
3044 right.EmitBranchable (ec, target, false);
3049 } else if (oper == Operator.LogicalOr){
3051 left.EmitBranchable (ec, target, true);
3052 right.EmitBranchable (ec, target, true);
3055 Label tests_end = ig.DefineLabel ();
3056 left.EmitBranchable (ec, tests_end, true);
3057 right.EmitBranchable (ec, target, false);
3058 ig.MarkLabel (tests_end);
3063 } else if (!(oper == Operator.LessThan || oper == Operator.GreaterThan ||
3064 oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
3065 oper == Operator.Equality || oper == Operator.Inequality)) {
3066 base.EmitBranchable (ec, target, on_true);
3074 bool is_unsigned = IsUnsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
3077 case Operator.Equality:
3079 ig.Emit (OpCodes.Beq, target);
3081 ig.Emit (OpCodes.Bne_Un, target);
3084 case Operator.Inequality:
3086 ig.Emit (OpCodes.Bne_Un, target);
3088 ig.Emit (OpCodes.Beq, target);
3091 case Operator.LessThan:
3094 ig.Emit (OpCodes.Blt_Un, target);
3096 ig.Emit (OpCodes.Blt, target);
3099 ig.Emit (OpCodes.Bge_Un, target);
3101 ig.Emit (OpCodes.Bge, target);
3104 case Operator.GreaterThan:
3107 ig.Emit (OpCodes.Bgt_Un, target);
3109 ig.Emit (OpCodes.Bgt, target);
3112 ig.Emit (OpCodes.Ble_Un, target);
3114 ig.Emit (OpCodes.Ble, target);
3117 case Operator.LessThanOrEqual:
3120 ig.Emit (OpCodes.Ble_Un, target);
3122 ig.Emit (OpCodes.Ble, target);
3125 ig.Emit (OpCodes.Bgt_Un, target);
3127 ig.Emit (OpCodes.Bgt, target);
3131 case Operator.GreaterThanOrEqual:
3134 ig.Emit (OpCodes.Bge_Un, target);
3136 ig.Emit (OpCodes.Bge, target);
3139 ig.Emit (OpCodes.Blt_Un, target);
3141 ig.Emit (OpCodes.Blt, target);
3144 throw new InternalErrorException (oper.ToString ());
3148 public override void Emit (EmitContext ec)
3150 EmitOperator (ec, left.Type);
3153 protected virtual void EmitOperator (EmitContext ec, Type l)
3155 ILGenerator ig = ec.ig;
3158 // Handle short-circuit operators differently
3161 if ((oper & Operator.LogicalMask) != 0) {
3162 Label load_result = ig.DefineLabel ();
3163 Label end = ig.DefineLabel ();
3165 bool is_or = oper == Operator.LogicalOr;
3166 left.EmitBranchable (ec, load_result, is_or);
3168 ig.Emit (OpCodes.Br_S, end);
3170 ig.MarkLabel (load_result);
3171 ig.Emit (is_or ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
3179 // Optimize zero-based operations
3181 // TODO: Implement more optimizations, but it should probably go to PredefinedOperators
3183 if ((oper & Operator.ShiftMask) != 0 || oper == Operator.Addition || oper == Operator.Subtraction) {
3184 Constant rc = right as Constant;
3185 if (rc != null && rc.IsDefaultValue) {
3195 case Operator.Multiply:
3197 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3198 opcode = OpCodes.Mul_Ovf;
3199 else if (!IsFloat (l))
3200 opcode = OpCodes.Mul_Ovf_Un;
3202 opcode = OpCodes.Mul;
3204 opcode = OpCodes.Mul;
3208 case Operator.Division:
3210 opcode = OpCodes.Div_Un;
3212 opcode = OpCodes.Div;
3215 case Operator.Modulus:
3217 opcode = OpCodes.Rem_Un;
3219 opcode = OpCodes.Rem;
3222 case Operator.Addition:
3224 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3225 opcode = OpCodes.Add_Ovf;
3226 else if (!IsFloat (l))
3227 opcode = OpCodes.Add_Ovf_Un;
3229 opcode = OpCodes.Add;
3231 opcode = OpCodes.Add;
3234 case Operator.Subtraction:
3236 if (l == TypeManager.int32_type || l == TypeManager.int64_type)
3237 opcode = OpCodes.Sub_Ovf;
3238 else if (!IsFloat (l))
3239 opcode = OpCodes.Sub_Ovf_Un;
3241 opcode = OpCodes.Sub;
3243 opcode = OpCodes.Sub;
3246 case Operator.RightShift:
3248 opcode = OpCodes.Shr_Un;
3250 opcode = OpCodes.Shr;
3253 case Operator.LeftShift:
3254 opcode = OpCodes.Shl;
3257 case Operator.Equality:
3258 opcode = OpCodes.Ceq;
3261 case Operator.Inequality:
3262 ig.Emit (OpCodes.Ceq);
3263 ig.Emit (OpCodes.Ldc_I4_0);
3265 opcode = OpCodes.Ceq;
3268 case Operator.LessThan:
3270 opcode = OpCodes.Clt_Un;
3272 opcode = OpCodes.Clt;
3275 case Operator.GreaterThan:
3277 opcode = OpCodes.Cgt_Un;
3279 opcode = OpCodes.Cgt;
3282 case Operator.LessThanOrEqual:
3283 if (IsUnsigned (l) || IsFloat (l))
3284 ig.Emit (OpCodes.Cgt_Un);
3286 ig.Emit (OpCodes.Cgt);
3287 ig.Emit (OpCodes.Ldc_I4_0);
3289 opcode = OpCodes.Ceq;
3292 case Operator.GreaterThanOrEqual:
3293 if (IsUnsigned (l) || IsFloat (l))
3294 ig.Emit (OpCodes.Clt_Un);
3296 ig.Emit (OpCodes.Clt);
3298 ig.Emit (OpCodes.Ldc_I4_0);
3300 opcode = OpCodes.Ceq;
3303 case Operator.BitwiseOr:
3304 opcode = OpCodes.Or;
3307 case Operator.BitwiseAnd:
3308 opcode = OpCodes.And;
3311 case Operator.ExclusiveOr:
3312 opcode = OpCodes.Xor;
3316 throw new InternalErrorException (oper.ToString ());
3322 // Nullable enum could require underlying type cast and we cannot simply wrap binary
3323 // expression because that would wrap lifted binary operation
3325 if (enum_conversion != null)
3326 enum_conversion.Emit (ec);
3329 public override void EmitSideEffect (EmitContext ec)
3331 if ((oper & Operator.LogicalMask) != 0 ||
3332 (ec.CheckState && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
3333 base.EmitSideEffect (ec);
3335 left.EmitSideEffect (ec);
3336 right.EmitSideEffect (ec);
3340 protected override void CloneTo (CloneContext clonectx, Expression t)
3342 Binary target = (Binary) t;
3344 target.left = left.Clone (clonectx);
3345 target.right = right.Clone (clonectx);
3348 public override Expression CreateExpressionTree (EmitContext ec)
3350 return CreateExpressionTree (ec, null);
3353 Expression CreateExpressionTree (EmitContext ec, MethodGroupExpr method)
3356 bool lift_arg = false;
3359 case Operator.Addition:
3360 if (method == null && ec.CheckState && !IsFloat (type))
3361 method_name = "AddChecked";
3363 method_name = "Add";
3365 case Operator.BitwiseAnd:
3366 method_name = "And";
3368 case Operator.BitwiseOr:
3371 case Operator.Division:
3372 method_name = "Divide";
3374 case Operator.Equality:
3375 method_name = "Equal";
3378 case Operator.ExclusiveOr:
3379 method_name = "ExclusiveOr";
3381 case Operator.GreaterThan:
3382 method_name = "GreaterThan";
3385 case Operator.GreaterThanOrEqual:
3386 method_name = "GreaterThanOrEqual";
3389 case Operator.Inequality:
3390 method_name = "NotEqual";
3393 case Operator.LeftShift:
3394 method_name = "LeftShift";
3396 case Operator.LessThan:
3397 method_name = "LessThan";
3400 case Operator.LessThanOrEqual:
3401 method_name = "LessThanOrEqual";
3404 case Operator.LogicalAnd:
3405 method_name = "AndAlso";
3407 case Operator.LogicalOr:
3408 method_name = "OrElse";
3410 case Operator.Modulus:
3411 method_name = "Modulo";
3413 case Operator.Multiply:
3414 if (method == null && ec.CheckState && !IsFloat (type))
3415 method_name = "MultiplyChecked";
3417 method_name = "Multiply";
3419 case Operator.RightShift:
3420 method_name = "RightShift";
3422 case Operator.Subtraction:
3423 if (method == null && ec.CheckState && !IsFloat (type))
3424 method_name = "SubtractChecked";
3426 method_name = "Subtract";
3430 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
3433 ArrayList args = new ArrayList (2);
3434 args.Add (new Argument (left.CreateExpressionTree (ec)));
3435 args.Add (new Argument (right.CreateExpressionTree (ec)));
3436 if (method != null) {
3438 args.Add (new Argument (new BoolConstant (false, loc)));
3440 args.Add (new Argument (method.CreateExpressionTree (ec)));
3443 return CreateExpressionFactoryCall (method_name, args);
3448 // Object created by Binary when the binary operator uses an method instead of being
3449 // a binary operation that maps to a CIL binary operation.
3451 public class BinaryMethod : Expression {
3452 public MethodBase method;
3453 public ArrayList Arguments;
3455 public BinaryMethod (Type t, MethodBase m, ArrayList args)
3460 eclass = ExprClass.Value;
3463 public override Expression DoResolve (EmitContext ec)
3468 public override void Emit (EmitContext ec)
3470 ILGenerator ig = ec.ig;
3472 Invocation.EmitArguments (ec, Arguments, false, null);
3474 if (method is MethodInfo)
3475 ig.Emit (OpCodes.Call, (MethodInfo) method);
3477 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3482 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3483 // b, c, d... may be strings or objects.
3485 public class StringConcat : Expression {
3486 ArrayList arguments;
3488 public StringConcat (EmitContext ec, Location loc, Expression left, Expression right)
3491 type = TypeManager.string_type;
3492 eclass = ExprClass.Value;
3494 arguments = new ArrayList (2);
3499 public override Expression CreateExpressionTree (EmitContext ec)
3501 Argument arg = (Argument) arguments [0];
3502 return CreateExpressionAddCall (ec, arg, arg.Expr.CreateExpressionTree (ec), 1);
3506 // Creates nested calls tree from an array of arguments used for IL emit
3508 Expression CreateExpressionAddCall (EmitContext ec, Argument left, Expression left_etree, int pos)
3510 ArrayList concat_args = new ArrayList (2);
3511 ArrayList add_args = new ArrayList (3);
3513 concat_args.Add (left);
3514 add_args.Add (new Argument (left_etree));
3516 concat_args.Add (arguments [pos]);
3517 add_args.Add (new Argument (((Argument) arguments [pos]).Expr.CreateExpressionTree (ec)));
3519 MethodGroupExpr method = CreateConcatMemberExpression ().Resolve (ec) as MethodGroupExpr;
3523 method = method.OverloadResolve (ec, ref concat_args, false, loc);
3527 add_args.Add (new Argument (method.CreateExpressionTree (ec)));
3529 Expression expr = CreateExpressionFactoryCall ("Add", add_args);
3530 if (++pos == arguments.Count)
3533 left = new Argument (new EmptyExpression (method.Type));
3534 return CreateExpressionAddCall (ec, left, expr, pos);
3537 public override Expression DoResolve (EmitContext ec)
3542 public void Append (EmitContext ec, Expression operand)
3547 StringConstant sc = operand as StringConstant;
3549 if (arguments.Count != 0) {
3550 Argument last_argument = (Argument) arguments [arguments.Count - 1];
3551 StringConstant last_expr_constant = last_argument.Expr as StringConstant;
3552 if (last_expr_constant != null) {
3553 last_argument.Expr = new StringConstant (
3554 last_expr_constant.Value + sc.Value, sc.Location);
3560 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3562 StringConcat concat_oper = operand as StringConcat;
3563 if (concat_oper != null) {
3564 arguments.AddRange (concat_oper.arguments);
3569 arguments.Add (new Argument (operand));
3572 Expression CreateConcatMemberExpression ()
3574 return new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "String", loc), "Concat", loc);
3577 public override void Emit (EmitContext ec)
3579 Expression concat = new Invocation (CreateConcatMemberExpression (), arguments, true);
3580 concat = concat.Resolve (ec);
3587 // Object created with +/= on delegates
3589 public class BinaryDelegate : Expression {
3593 public BinaryDelegate (Type t, MethodInfo mi, ArrayList args)
3598 eclass = ExprClass.Value;
3601 public override Expression DoResolve (EmitContext ec)
3606 public override void Emit (EmitContext ec)
3608 ILGenerator ig = ec.ig;
3610 Invocation.EmitArguments (ec, args, false, null);
3612 ig.Emit (OpCodes.Call, (MethodInfo) method);
3613 ig.Emit (OpCodes.Castclass, type);
3616 public Expression Right {
3618 Argument arg = (Argument) args [1];
3623 public bool IsAddition {
3625 return method == TypeManager.delegate_combine_delegate_delegate;
3631 // User-defined conditional logical operator
3633 public class ConditionalLogicalOperator : UserOperatorCall {
3634 readonly bool is_and;
3637 public ConditionalLogicalOperator (MethodGroupExpr oper_method, ArrayList arguments,
3638 ExpressionTreeExpression expr_tree, bool is_and, Location loc)
3639 : base (oper_method, arguments, expr_tree, loc)
3641 this.is_and = is_and;
3644 public override Expression DoResolve (EmitContext ec)
3646 MethodInfo method = (MethodInfo)mg;
3647 type = TypeManager.TypeToCoreType (method.ReturnType);
3648 ParameterData pd = TypeManager.GetParameterData (method);
3649 if (!TypeManager.IsEqual (type, type) || !TypeManager.IsEqual (type, pd.Types [0]) || !TypeManager.IsEqual (type, pd.Types [1])) {
3650 Report.Error (217, loc,
3651 "A user-defined operator `{0}' must have parameters and return values of the same type in order to be applicable as a short circuit operator",
3652 TypeManager.CSharpSignature (method));
3656 Expression left_dup = new EmptyExpression (type);
3657 Expression op_true = GetOperatorTrue (ec, left_dup, loc);
3658 Expression op_false = GetOperatorFalse (ec, left_dup, loc);
3659 if (op_true == null || op_false == null) {
3660 Report.Error (218, loc,
3661 "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
3662 TypeManager.CSharpName (type), TypeManager.CSharpSignature (method));
3666 oper = is_and ? op_false : op_true;
3667 eclass = ExprClass.Value;
3671 public override void Emit (EmitContext ec)
3673 ILGenerator ig = ec.ig;
3674 Label end_target = ig.DefineLabel ();
3677 // Emit and duplicate left argument
3679 ((Argument)arguments [0]).Expr.Emit (ec);
3680 ig.Emit (OpCodes.Dup);
3681 arguments.RemoveAt (0);
3683 oper.EmitBranchable (ec, end_target, true);
3685 ig.MarkLabel (end_target);
3689 public class PointerArithmetic : Expression {
3690 Expression left, right;
3694 // We assume that `l' is always a pointer
3696 public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
3702 is_add = is_addition;
3705 public override Expression CreateExpressionTree (EmitContext ec)
3707 Error_PointerInsideExpressionTree ();
3711 public override Expression DoResolve (EmitContext ec)
3713 eclass = ExprClass.Variable;
3715 if (left.Type == TypeManager.void_ptr_type) {
3716 Error (242, "The operation in question is undefined on void pointers");
3723 public override void Emit (EmitContext ec)
3725 Type op_type = left.Type;
3726 ILGenerator ig = ec.ig;
3728 // It must be either array or fixed buffer
3729 Type element = TypeManager.HasElementType (op_type) ?
3730 element = TypeManager.GetElementType (op_type) :
3731 element = AttributeTester.GetFixedBuffer (((FieldExpr)left).FieldInfo).ElementType;
3733 int size = GetTypeSize (element);
3734 Type rtype = right.Type;
3736 if (rtype.IsPointer){
3738 // handle (pointer - pointer)
3742 ig.Emit (OpCodes.Sub);
3746 ig.Emit (OpCodes.Sizeof, element);
3748 IntLiteral.EmitInt (ig, size);
3749 ig.Emit (OpCodes.Div);
3751 ig.Emit (OpCodes.Conv_I8);
3754 // handle + and - on (pointer op int)
3757 ig.Emit (OpCodes.Conv_I);
3759 Constant right_const = right as Constant;
3760 if (right_const != null && size != 0) {
3761 Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
3769 ig.Emit (OpCodes.Sizeof, element);
3771 IntLiteral.EmitInt (ig, size);
3772 if (rtype == TypeManager.int64_type)
3773 ig.Emit (OpCodes.Conv_I8);
3774 else if (rtype == TypeManager.uint64_type)
3775 ig.Emit (OpCodes.Conv_U8);
3776 ig.Emit (OpCodes.Mul);
3780 if (rtype == TypeManager.int64_type || rtype == TypeManager.uint64_type)
3781 ig.Emit (OpCodes.Conv_I);
3784 ig.Emit (OpCodes.Add);
3786 ig.Emit (OpCodes.Sub);
3792 /// Implements the ternary conditional operator (?:)
3794 public class Conditional : Expression {
3795 Expression expr, true_expr, false_expr;
3797 public Conditional (Expression expr, Expression true_expr, Expression false_expr)
3800 this.true_expr = true_expr;
3801 this.false_expr = false_expr;
3802 this.loc = expr.Location;
3805 public Expression Expr {
3811 public Expression TrueExpr {
3817 public Expression FalseExpr {
3823 public override Expression CreateExpressionTree (EmitContext ec)
3825 ArrayList args = new ArrayList (3);
3826 args.Add (new Argument (expr.CreateExpressionTree (ec)));
3827 args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
3828 args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
3829 return CreateExpressionFactoryCall ("Condition", args);
3832 public override Expression DoResolve (EmitContext ec)
3834 expr = expr.Resolve (ec);
3839 if (expr.Type != TypeManager.bool_type){
3840 expr = Expression.ResolveBoolean (
3847 Assign ass = expr as Assign;
3848 if (ass != null && ass.Source is Constant) {
3849 Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
3852 true_expr = true_expr.Resolve (ec);
3853 false_expr = false_expr.Resolve (ec);
3855 if (true_expr == null || false_expr == null)
3858 eclass = ExprClass.Value;
3859 if (true_expr.Type == false_expr.Type) {
3860 type = true_expr.Type;
3861 if (type == TypeManager.null_type) {
3862 // TODO: probably will have to implement ConditionalConstant
3863 // to call method without return constant as well
3864 Report.Warning (-101, 1, loc, "Conditional expression will always return same value");
3869 Type true_type = true_expr.Type;
3870 Type false_type = false_expr.Type;
3873 // First, if an implicit conversion exists from true_expr
3874 // to false_expr, then the result type is of type false_expr.Type
3876 conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
3879 // Check if both can convert implicitl to each other's type
3881 if (Convert.ImplicitConversion (ec, false_expr, true_type, loc) != null){
3883 "Can not compute type of conditional expression " +
3884 "as `" + TypeManager.CSharpName (true_expr.Type) +
3885 "' and `" + TypeManager.CSharpName (false_expr.Type) +
3886 "' convert implicitly to each other");
3891 } else if ((conv = Convert.ImplicitConversion(ec, false_expr, true_type,loc))!= null){
3895 Report.Error (173, loc, "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
3896 true_expr.GetSignatureForError (), false_expr.GetSignatureForError ());
3901 // Dead code optimalization
3902 if (expr is BoolConstant){
3903 BoolConstant bc = (BoolConstant) expr;
3905 Report.Warning (429, 4, bc.Value ? false_expr.Location : true_expr.Location, "Unreachable expression code detected");
3906 return bc.Value ? true_expr : false_expr;
3912 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
3917 public override void Emit (EmitContext ec)
3919 ILGenerator ig = ec.ig;
3920 Label false_target = ig.DefineLabel ();
3921 Label end_target = ig.DefineLabel ();
3923 expr.EmitBranchable (ec, false_target, false);
3924 true_expr.Emit (ec);
3926 if (type.IsInterface) {
3927 LocalBuilder temp = ec.GetTemporaryLocal (type);
3928 ig.Emit (OpCodes.Stloc, temp);
3929 ig.Emit (OpCodes.Ldloc, temp);
3930 ec.FreeTemporaryLocal (temp, type);
3933 ig.Emit (OpCodes.Br, end_target);
3934 ig.MarkLabel (false_target);
3935 false_expr.Emit (ec);
3936 ig.MarkLabel (end_target);
3939 protected override void CloneTo (CloneContext clonectx, Expression t)
3941 Conditional target = (Conditional) t;
3943 target.expr = expr.Clone (clonectx);
3944 target.true_expr = true_expr.Clone (clonectx);
3945 target.false_expr = false_expr.Clone (clonectx);
3949 public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation {
3951 LocalTemporary temp;
3953 public abstract Variable Variable {
3957 public abstract bool IsRef {
3961 public override void Emit (EmitContext ec)
3966 public override void EmitSideEffect (EmitContext ec)
3972 // This method is used by parameters that are references, that are
3973 // being passed as references: we only want to pass the pointer (that
3974 // is already stored in the parameter, not the address of the pointer,
3975 // and not the value of the variable).
3977 public void EmitLoad (EmitContext ec)
3979 Report.Debug (64, "VARIABLE EMIT LOAD", this, Variable, type, loc);
3981 Variable.EmitInstance (ec);
3985 public void Emit (EmitContext ec, bool leave_copy)
3987 Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
3993 // If we are a reference, we loaded on the stack a pointer
3994 // Now lets load the real value
3996 LoadFromPtr (ec.ig, type);
4000 ec.ig.Emit (OpCodes.Dup);
4002 if (IsRef || Variable.NeedsTemporary) {
4003 temp = new LocalTemporary (Type);
4009 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
4010 bool prepare_for_load)
4012 Report.Debug (64, "VARIABLE EMIT ASSIGN", this, Variable, type, IsRef,
4015 ILGenerator ig = ec.ig;
4016 prepared = prepare_for_load;
4018 Variable.EmitInstance (ec);
4019 if (prepare_for_load) {
4020 if (Variable.HasInstance)
4021 ig.Emit (OpCodes.Dup);
4029 // HACK: variable is already emitted when source is an initializer
4030 if (source is NewInitialize) {
4032 Variable.EmitInstance (ec);
4039 ig.Emit (OpCodes.Dup);
4040 if (IsRef || Variable.NeedsTemporary) {
4041 temp = new LocalTemporary (Type);
4047 StoreFromPtr (ig, type);
4049 Variable.EmitAssign (ec);
4057 public void AddressOf (EmitContext ec, AddressOp mode)
4059 Variable.EmitInstance (ec);
4060 Variable.EmitAddressOf (ec);
4067 public class LocalVariableReference : VariableReference, IVariable {
4068 public readonly string Name;
4070 public LocalInfo local_info;
4074 public LocalVariableReference (Block block, string name, Location l)
4079 eclass = ExprClass.Variable;
4083 // Setting `is_readonly' to false will allow you to create a writable
4084 // reference to a read-only variable. This is used by foreach and using.
4086 public LocalVariableReference (Block block, string name, Location l,
4087 LocalInfo local_info, bool is_readonly)
4088 : this (block, name, l)
4090 this.local_info = local_info;
4091 this.is_readonly = is_readonly;
4094 public VariableInfo VariableInfo {
4095 get { return local_info.VariableInfo; }
4098 public override bool IsRef {
4099 get { return false; }
4102 public bool IsReadOnly {
4103 get { return is_readonly; }
4106 public bool VerifyAssigned (EmitContext ec)
4108 VariableInfo variable_info = local_info.VariableInfo;
4109 return variable_info == null || variable_info.IsAssigned (ec, loc);
4112 void ResolveLocalInfo ()
4114 if (local_info == null) {
4115 local_info = Block.GetLocalInfo (Name);
4116 type = local_info.VariableType;
4117 is_readonly = local_info.ReadOnly;
4121 public override Expression CreateExpressionTree (EmitContext ec)
4123 ArrayList arg = new ArrayList (1);
4124 arg.Add (new Argument (this));
4125 return CreateExpressionFactoryCall ("Constant", arg);
4128 protected Expression DoResolveBase (EmitContext ec)
4130 type = local_info.VariableType;
4132 Expression e = Block.GetConstantExpression (Name);
4134 return e.Resolve (ec);
4136 if (!VerifyAssigned (ec))
4140 // If we are referencing a variable from the external block
4141 // flag it for capturing
4143 if (ec.MustCaptureVariable (local_info)) {
4144 if (local_info.AddressTaken){
4145 AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
4149 if (!ec.IsInProbingMode)
4151 ScopeInfo scope = local_info.Block.CreateScopeInfo ();
4152 variable = scope.AddLocal (local_info);
4153 type = variable.Type;
4160 public override Expression DoResolve (EmitContext ec)
4162 ResolveLocalInfo ();
4163 local_info.Used = true;
4165 if (type == null && local_info.Type is VarExpr) {
4166 local_info.VariableType = TypeManager.object_type;
4167 Error_VariableIsUsedBeforeItIsDeclared (Name);
4171 return DoResolveBase (ec);
4174 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4176 ResolveLocalInfo ();
4179 if (right_side == EmptyExpression.OutAccess)
4180 local_info.Used = true;
4182 // Infer implicitly typed local variable
4184 VarExpr ve = local_info.Type as VarExpr;
4186 if (!ve.InferType (ec, right_side))
4188 type = local_info.VariableType = ve.Type;
4195 if (right_side == EmptyExpression.OutAccess) {
4196 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
4197 } else if (right_side == EmptyExpression.LValueMemberAccess) {
4198 code = 1654; msg = "Cannot assign to members of `{0}' because it is a `{1}'";
4199 } else if (right_side == EmptyExpression.LValueMemberOutAccess) {
4200 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
4202 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
4204 Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
4208 if (VariableInfo != null)
4209 VariableInfo.SetAssigned (ec);
4211 return DoResolveBase (ec);
4214 public bool VerifyFixed ()
4216 // A local Variable is always fixed.
4220 public override int GetHashCode ()
4222 return Name.GetHashCode ();
4225 public override bool Equals (object obj)
4227 LocalVariableReference lvr = obj as LocalVariableReference;
4231 return Name == lvr.Name && Block == lvr.Block;
4234 public override Variable Variable {
4235 get { return variable != null ? variable : local_info.Variable; }
4238 public override string ToString ()
4240 return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
4243 protected override void CloneTo (CloneContext clonectx, Expression t)
4245 LocalVariableReference target = (LocalVariableReference) t;
4247 target.Block = clonectx.LookupBlock (Block);
4248 if (local_info != null)
4249 target.local_info = clonectx.LookupVariable (local_info);
4254 /// This represents a reference to a parameter in the intermediate
4257 public class ParameterReference : VariableReference, IVariable {
4258 readonly ToplevelParameterInfo pi;
4259 readonly ToplevelBlock referenced;
4262 public bool is_ref, is_out;
4265 get { return is_out; }
4268 public override bool IsRef {
4269 get { return is_ref; }
4272 public string Name {
4273 get { return Parameter.Name; }
4276 public Parameter Parameter {
4277 get { return pi.Parameter; }
4280 public ParameterReference (ToplevelBlock referenced, ToplevelParameterInfo pi, Location loc)
4283 this.referenced = referenced;
4285 eclass = ExprClass.Variable;
4288 public VariableInfo VariableInfo {
4289 get { return pi.VariableInfo; }
4292 public override Variable Variable {
4293 get { return variable != null ? variable : Parameter.Variable; }
4296 public bool VerifyFixed ()
4298 // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
4299 return Parameter.ModFlags == Parameter.Modifier.NONE;
4302 public bool IsAssigned (EmitContext ec, Location loc)
4304 // HACK: Variables are not captured in probing mode
4305 if (ec.IsInProbingMode)
4308 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsAssigned (VariableInfo))
4311 Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
4315 public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
4317 if (!ec.DoFlowAnalysis || !is_out || ec.CurrentBranching.IsFieldAssigned (VariableInfo, field_name))
4320 Report.Error (170, loc, "Use of possibly unassigned field `{0}'", field_name);
4324 public void SetAssigned (EmitContext ec)
4326 if (is_out && ec.DoFlowAnalysis)
4327 ec.CurrentBranching.SetAssigned (VariableInfo);
4330 public void SetFieldAssigned (EmitContext ec, string field_name)
4332 if (is_out && ec.DoFlowAnalysis)
4333 ec.CurrentBranching.SetFieldAssigned (VariableInfo, field_name);
4336 protected bool DoResolveBase (EmitContext ec)
4338 Parameter par = Parameter;
4339 if (!par.Resolve (ec)) {
4343 type = par.ParameterType;
4344 Parameter.Modifier mod = par.ModFlags;
4345 is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
4346 is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
4347 eclass = ExprClass.Variable;
4349 AnonymousContainer am = ec.CurrentAnonymousMethod;
4353 ToplevelBlock declared = pi.Block;
4354 if (is_ref && declared != referenced) {
4355 Report.Error (1628, Location,
4356 "Cannot use ref or out parameter `{0}' inside an " +
4357 "anonymous method block", par.Name);
4361 if (!am.IsIterator && declared == referenced)
4364 // Don't capture aruments when the probing is on
4365 if (!ec.IsInProbingMode) {
4366 ScopeInfo scope = declared.CreateScopeInfo ();
4367 variable = scope.AddParameter (par, pi.Index);
4368 type = variable.Type;
4373 public override int GetHashCode ()
4375 return Name.GetHashCode ();
4378 public override bool Equals (object obj)
4380 ParameterReference pr = obj as ParameterReference;
4384 return Name == pr.Name && referenced == pr.referenced;
4387 public override Expression CreateExpressionTree (EmitContext ec)
4389 return Parameter.ExpressionTreeVariableReference ();
4393 // Notice that for ref/out parameters, the type exposed is not the
4394 // same type exposed externally.
4397 // externally we expose "int&"
4398 // here we expose "int".
4400 // We record this in "is_ref". This means that the type system can treat
4401 // the type as it is expected, but when we generate the code, we generate
4402 // the alternate kind of code.
4404 public override Expression DoResolve (EmitContext ec)
4406 if (!DoResolveBase (ec))
4409 if (is_out && ec.DoFlowAnalysis &&
4410 (!ec.OmitStructFlowAnalysis || !VariableInfo.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
4416 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4418 if (!DoResolveBase (ec))
4421 // HACK: parameters are not captured when probing is on
4422 if (!ec.IsInProbingMode)
4428 static public void EmitLdArg (ILGenerator ig, int x)
4432 case 0: ig.Emit (OpCodes.Ldarg_0); break;
4433 case 1: ig.Emit (OpCodes.Ldarg_1); break;
4434 case 2: ig.Emit (OpCodes.Ldarg_2); break;
4435 case 3: ig.Emit (OpCodes.Ldarg_3); break;
4436 default: ig.Emit (OpCodes.Ldarg_S, (byte) x); break;
4439 ig.Emit (OpCodes.Ldarg, x);
4442 public override string ToString ()
4444 return "ParameterReference[" + Name + "]";
4449 /// Used for arguments to New(), Invocation()
4451 public class Argument {
4452 public enum AType : byte {
4459 public static readonly Argument[] Empty = new Argument [0];
4461 public readonly AType ArgType;
4462 public Expression Expr;
4464 public Argument (Expression expr, AType type)
4467 this.ArgType = type;
4470 public Argument (Expression expr)
4473 this.ArgType = AType.Expression;
4478 if (ArgType == AType.Ref || ArgType == AType.Out)
4479 return TypeManager.GetReferenceType (Expr.Type);
4485 public Parameter.Modifier Modifier
4490 return Parameter.Modifier.OUT;
4493 return Parameter.Modifier.REF;
4496 return Parameter.Modifier.NONE;
4501 public string GetSignatureForError ()
4503 if (Expr.eclass == ExprClass.MethodGroup)
4504 return Expr.ExprClassName;
4506 return Expr.GetSignatureForError ();
4509 public bool ResolveMethodGroup (EmitContext ec)
4511 SimpleName sn = Expr as SimpleName;
4513 Expr = sn.GetMethodGroup ();
4515 // FIXME: csc doesn't report any error if you try to use `ref' or
4516 // `out' in a delegate creation expression.
4517 Expr = Expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4524 public bool Resolve (EmitContext ec, Location loc)
4529 using (ec.With (EmitContext.Flags.DoFlowAnalysis, true)) {
4530 // Verify that the argument is readable
4531 if (ArgType != AType.Out)
4532 Expr = Expr.Resolve (ec);
4534 // Verify that the argument is writeable
4535 if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
4536 Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
4538 return Expr != null;
4542 public void Emit (EmitContext ec)
4544 if (ArgType != AType.Ref && ArgType != AType.Out) {
4549 AddressOp mode = AddressOp.Store;
4550 if (ArgType == AType.Ref)
4551 mode |= AddressOp.Load;
4553 IMemoryLocation ml = (IMemoryLocation) Expr;
4554 ParameterReference pr = ml as ParameterReference;
4557 // ParameterReferences might already be references, so we want
4558 // to pass just the value
4560 if (pr != null && pr.IsRef)
4563 ml.AddressOf (ec, mode);
4566 public Argument Clone (CloneContext clonectx)
4568 return new Argument (Expr.Clone (clonectx), ArgType);
4573 /// Invocation of methods or delegates.
4575 public class Invocation : ExpressionStatement {
4576 protected ArrayList Arguments;
4577 protected Expression expr;
4578 protected MethodGroupExpr mg;
4579 bool arguments_resolved;
4582 // arguments is an ArrayList, but we do not want to typecast,
4583 // as it might be null.
4585 public Invocation (Expression expr, ArrayList arguments)
4587 SimpleName sn = expr as SimpleName;
4589 this.expr = sn.GetMethodGroup ();
4593 Arguments = arguments;
4595 loc = expr.Location;
4598 public Invocation (Expression expr, ArrayList arguments, bool arguments_resolved)
4599 : this (expr, arguments)
4601 this.arguments_resolved = arguments_resolved;
4604 public override Expression CreateExpressionTree (EmitContext ec)
4609 // Special conversion for nested expression trees
4611 if (TypeManager.DropGenericTypeArguments (type) == TypeManager.expression_type) {
4612 args = new ArrayList (1);
4613 args.Add (new Argument (this));
4614 return CreateExpressionFactoryCall ("Quote", args);
4617 args = new ArrayList (Arguments == null ? 2 : Arguments.Count + 2);
4619 args.Add (new Argument (mg.InstanceExpression.CreateExpressionTree (ec)));
4621 args.Add (new Argument (new NullLiteral (loc)));
4623 args.Add (new Argument (mg.CreateExpressionTree (ec)));
4624 if (Arguments != null) {
4625 foreach (Argument a in Arguments) {
4626 Expression e = a.Expr.CreateExpressionTree (ec);
4628 args.Add (new Argument (e));
4632 return CreateExpressionFactoryCall ("Call", args);
4635 public override Expression DoResolve (EmitContext ec)
4637 // Don't resolve already resolved expression
4638 if (eclass != ExprClass.Invalid)
4641 Expression expr_resolved = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
4642 if (expr_resolved == null)
4645 mg = expr_resolved as MethodGroupExpr;
4647 Type expr_type = expr_resolved.Type;
4649 if (expr_type != null && TypeManager.IsDelegateType (expr_type)){
4650 return (new DelegateInvocation (
4651 expr_resolved, Arguments, loc)).Resolve (ec);
4654 MemberExpr me = expr_resolved as MemberExpr;
4656 expr_resolved.Error_UnexpectedKind (ResolveFlags.MethodGroup, loc);
4660 mg = ec.TypeContainer.LookupExtensionMethod (me.Type, me.Name, loc);
4662 Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
4663 expr_resolved.GetSignatureForError ());
4667 ((ExtensionMethodGroupExpr)mg).ExtensionExpression = me.InstanceExpression;
4671 // Next, evaluate all the expressions in the argument list
4673 if (Arguments != null && !arguments_resolved) {
4674 for (int i = 0; i < Arguments.Count; ++i)
4676 if (!((Argument)Arguments[i]).Resolve(ec, loc))
4681 mg = DoResolveOverload (ec);
4685 MethodInfo method = (MethodInfo)mg;
4686 if (method != null) {
4687 type = TypeManager.TypeToCoreType (method.ReturnType);
4689 // TODO: this is a copy of mg.ResolveMemberAccess method
4690 Expression iexpr = mg.InstanceExpression;
4691 if (method.IsStatic) {
4692 if (iexpr == null ||
4693 iexpr is This || iexpr is EmptyExpression ||
4694 mg.IdenticalTypeName) {
4695 mg.InstanceExpression = null;
4697 MemberExpr.error176 (loc, mg.GetSignatureForError ());
4703 if (type.IsPointer){
4711 // Only base will allow this invocation to happen.
4713 if (mg.IsBase && method.IsAbstract){
4714 Error_CannotCallAbstractBase (TypeManager.CSharpSignature (method));
4718 if (Arguments == null && method.DeclaringType == TypeManager.object_type && method.Name == "Finalize") {
4720 Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4722 Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4726 if (IsSpecialMethodInvocation (method)) {
4730 if (mg.InstanceExpression != null)
4731 mg.InstanceExpression.CheckMarshalByRefAccess (ec);
4733 eclass = ExprClass.Value;
4737 protected virtual MethodGroupExpr DoResolveOverload (EmitContext ec)
4739 return mg.OverloadResolve (ec, ref Arguments, false, loc);
4742 bool IsSpecialMethodInvocation (MethodBase method)
4744 if (!TypeManager.IsSpecialMethod (method))
4747 Report.SymbolRelatedToPreviousError (method);
4748 Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
4749 TypeManager.CSharpSignature (method, true));
4755 /// Emits a list of resolved Arguments that are in the arguments
4758 /// The MethodBase argument might be null if the
4759 /// emission of the arguments is known not to contain
4760 /// a `params' field (for example in constructors or other routines
4761 /// that keep their arguments in this structure)
4763 /// if `dup_args' is true, a copy of the arguments will be left
4764 /// on the stack. If `dup_args' is true, you can specify `this_arg'
4765 /// which will be duplicated before any other args. Only EmitCall
4766 /// should be using this interface.
4768 public static void EmitArguments (EmitContext ec, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
4770 if (arguments == null)
4773 int top = arguments.Count;
4774 LocalTemporary [] temps = null;
4776 if (dup_args && top != 0)
4777 temps = new LocalTemporary [top];
4779 int argument_index = 0;
4781 for (int i = 0; i < top; i++) {
4782 a = (Argument) arguments [argument_index++];
4785 ec.ig.Emit (OpCodes.Dup);
4786 (temps [i] = new LocalTemporary (a.Type)).Store (ec);
4791 if (this_arg != null)
4794 for (int i = 0; i < top; i ++) {
4795 temps [i].Emit (ec);
4796 temps [i].Release (ec);
4801 static Type[] GetVarargsTypes (MethodBase mb, ArrayList arguments)
4803 ParameterData pd = TypeManager.GetParameterData (mb);
4805 if (arguments == null)
4806 return new Type [0];
4808 Argument a = (Argument) arguments [pd.Count - 1];
4809 Arglist list = (Arglist) a.Expr;
4811 return list.ArgumentTypes;
4815 /// This checks the ConditionalAttribute on the method
4817 public static bool IsMethodExcluded (MethodBase method)
4819 if (method.IsConstructor)
4822 method = TypeManager.DropGenericMethodArguments (method);
4823 if (method.DeclaringType.Module == CodeGen.Module.Builder) {
4824 IMethodData md = TypeManager.GetMethod (method);
4826 return md.IsExcluded ();
4828 // For some methods (generated by delegate class) GetMethod returns null
4829 // because they are not included in builder_to_method table
4833 return AttributeTester.IsConditionalMethodExcluded (method);
4837 /// is_base tells whether we want to force the use of the `call'
4838 /// opcode instead of using callvirt. Call is required to call
4839 /// a specific method, while callvirt will always use the most
4840 /// recent method in the vtable.
4842 /// is_static tells whether this is an invocation on a static method
4844 /// instance_expr is an expression that represents the instance
4845 /// it must be non-null if is_static is false.
4847 /// method is the method to invoke.
4849 /// Arguments is the list of arguments to pass to the method or constructor.
4851 public static void EmitCall (EmitContext ec, bool is_base,
4852 Expression instance_expr,
4853 MethodBase method, ArrayList Arguments, Location loc)
4855 EmitCall (ec, is_base, instance_expr, method, Arguments, loc, false, false);
4858 // `dup_args' leaves an extra copy of the arguments on the stack
4859 // `omit_args' does not leave any arguments at all.
4860 // So, basically, you could make one call with `dup_args' set to true,
4861 // and then another with `omit_args' set to true, and the two calls
4862 // would have the same set of arguments. However, each argument would
4863 // only have been evaluated once.
4864 public static void EmitCall (EmitContext ec, bool is_base,
4865 Expression instance_expr,
4866 MethodBase method, ArrayList Arguments, Location loc,
4867 bool dup_args, bool omit_args)
4869 ILGenerator ig = ec.ig;
4870 bool struct_call = false;
4871 bool this_call = false;
4872 LocalTemporary this_arg = null;
4874 Type decl_type = method.DeclaringType;
4876 if (!ec.IsInObsoleteScope) {
4878 // This checks ObsoleteAttribute on the method and on the declaring type
4880 ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
4882 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
4884 oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
4886 AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
4890 if (IsMethodExcluded (method))
4893 bool is_static = method.IsStatic;
4895 if (instance_expr == EmptyExpression.Null) {
4896 SimpleName.Error_ObjectRefRequired (ec, loc, TypeManager.CSharpSignature (method));
4900 this_call = instance_expr is This;
4901 if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
4905 // If this is ourselves, push "this"
4909 Type iexpr_type = instance_expr.Type;
4912 // Push the instance expression
4914 if (TypeManager.IsValueType (iexpr_type)) {
4916 // Special case: calls to a function declared in a
4917 // reference-type with a value-type argument need
4918 // to have their value boxed.
4919 if (decl_type.IsValueType ||
4920 TypeManager.IsGenericParameter (iexpr_type)) {
4922 // If the expression implements IMemoryLocation, then
4923 // we can optimize and use AddressOf on the
4926 // If not we have to use some temporary storage for
4928 if (instance_expr is IMemoryLocation) {
4929 ((IMemoryLocation)instance_expr).
4930 AddressOf (ec, AddressOp.LoadStore);
4932 LocalTemporary temp = new LocalTemporary (iexpr_type);
4933 instance_expr.Emit (ec);
4935 temp.AddressOf (ec, AddressOp.Load);
4938 // avoid the overhead of doing this all the time.
4940 t = TypeManager.GetReferenceType (iexpr_type);
4942 instance_expr.Emit (ec);
4943 ig.Emit (OpCodes.Box, instance_expr.Type);
4944 t = TypeManager.object_type;
4947 instance_expr.Emit (ec);
4948 t = instance_expr.Type;
4952 ig.Emit (OpCodes.Dup);
4953 if (Arguments != null && Arguments.Count != 0) {
4954 this_arg = new LocalTemporary (t);
4955 this_arg.Store (ec);
4962 EmitArguments (ec, Arguments, dup_args, this_arg);
4965 if ((instance_expr != null) && (instance_expr.Type.IsGenericParameter))
4966 ig.Emit (OpCodes.Constrained, instance_expr.Type);
4970 if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
4971 call_op = OpCodes.Call;
4973 call_op = OpCodes.Callvirt;
4975 if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
4976 Type[] varargs_types = GetVarargsTypes (method, Arguments);
4977 ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
4984 // and DoFoo is not virtual, you can omit the callvirt,
4985 // because you don't need the null checking behavior.
4987 if (method is MethodInfo)
4988 ig.Emit (call_op, (MethodInfo) method);
4990 ig.Emit (call_op, (ConstructorInfo) method);
4993 public override void Emit (EmitContext ec)
4995 mg.EmitCall (ec, Arguments);
4998 public override void EmitStatement (EmitContext ec)
5003 // Pop the return value if there is one
5005 if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
5006 ec.ig.Emit (OpCodes.Pop);
5009 protected override void CloneTo (CloneContext clonectx, Expression t)
5011 Invocation target = (Invocation) t;
5013 if (Arguments != null) {
5014 target.Arguments = new ArrayList (Arguments.Count);
5015 foreach (Argument a in Arguments)
5016 target.Arguments.Add (a.Clone (clonectx));
5019 target.expr = expr.Clone (clonectx);
5023 public class InvocationOrCast : ExpressionStatement
5026 Expression argument;
5028 public InvocationOrCast (Expression expr, Expression argument)
5031 this.argument = argument;
5032 this.loc = expr.Location;
5035 public override Expression DoResolve (EmitContext ec)
5038 // First try to resolve it as a cast.
5040 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
5041 if ((te != null) && (te.eclass == ExprClass.Type)) {
5042 Cast cast = new Cast (te, argument, loc);
5043 return cast.Resolve (ec);
5047 // This can either be a type or a delegate invocation.
5048 // Let's just resolve it and see what we'll get.
5050 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5055 // Ok, so it's a Cast.
5057 if (expr.eclass == ExprClass.Type) {
5058 Cast cast = new Cast (new TypeExpression (expr.Type, loc), argument, loc);
5059 return cast.Resolve (ec);
5063 // It's a delegate invocation.
5065 if (!TypeManager.IsDelegateType (expr.Type)) {
5066 Error (149, "Method name expected");
5070 ArrayList args = new ArrayList ();
5071 args.Add (new Argument (argument, Argument.AType.Expression));
5072 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5073 return invocation.Resolve (ec);
5076 public override ExpressionStatement ResolveStatement (EmitContext ec)
5079 // First try to resolve it as a cast.
5081 TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
5082 if ((te != null) && (te.eclass == ExprClass.Type)) {
5083 Error_InvalidExpressionStatement ();
5088 // This can either be a type or a delegate invocation.
5089 // Let's just resolve it and see what we'll get.
5091 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5092 if ((expr == null) || (expr.eclass == ExprClass.Type)) {
5093 Error_InvalidExpressionStatement ();
5098 // It's a delegate invocation.
5100 if (!TypeManager.IsDelegateType (expr.Type)) {
5101 Error (149, "Method name expected");
5105 ArrayList args = new ArrayList ();
5106 args.Add (new Argument (argument, Argument.AType.Expression));
5107 DelegateInvocation invocation = new DelegateInvocation (expr, args, loc);
5108 return invocation.ResolveStatement (ec);
5111 public override void Emit (EmitContext ec)
5113 throw new Exception ("Cannot happen");
5116 public override void EmitStatement (EmitContext ec)
5118 throw new Exception ("Cannot happen");
5121 protected override void CloneTo (CloneContext clonectx, Expression t)
5123 InvocationOrCast target = (InvocationOrCast) t;
5125 target.expr = expr.Clone (clonectx);
5126 target.argument = argument.Clone (clonectx);
5131 // This class is used to "disable" the code generation for the
5132 // temporary variable when initializing value types.
5134 class EmptyAddressOf : EmptyExpression, IMemoryLocation {
5135 public void AddressOf (EmitContext ec, AddressOp Mode)
5142 /// Implements the new expression
5144 public class New : ExpressionStatement, IMemoryLocation {
5145 ArrayList Arguments;
5148 // During bootstrap, it contains the RequestedType,
5149 // but if `type' is not null, it *might* contain a NewDelegate
5150 // (because of field multi-initialization)
5152 public Expression RequestedType;
5154 MethodGroupExpr method;
5157 // If set, the new expression is for a value_target, and
5158 // we will not leave anything on the stack.
5160 protected Expression value_target;
5161 protected bool value_target_set;
5162 bool is_type_parameter = false;
5164 public New (Expression requested_type, ArrayList arguments, Location l)
5166 RequestedType = requested_type;
5167 Arguments = arguments;
5171 public bool SetTargetVariable (Expression value)
5173 value_target = value;
5174 value_target_set = true;
5175 if (!(value_target is IMemoryLocation)){
5176 Error_UnexpectedKind (null, "variable", loc);
5183 // This function is used to disable the following code sequence for
5184 // value type initialization:
5186 // AddressOf (temporary)
5190 // Instead the provide will have provided us with the address on the
5191 // stack to store the results.
5193 static Expression MyEmptyExpression;
5195 public void DisableTemporaryValueType ()
5197 if (MyEmptyExpression == null)
5198 MyEmptyExpression = new EmptyAddressOf ();
5201 // To enable this, look into:
5202 // test-34 and test-89 and self bootstrapping.
5204 // For instance, we can avoid a copy by using `newobj'
5205 // instead of Call + Push-temp on value types.
5206 // value_target = MyEmptyExpression;
5211 /// Converts complex core type syntax like 'new int ()' to simple constant
5213 public static Constant Constantify (Type t)
5215 if (t == TypeManager.int32_type)
5216 return new IntConstant (0, Location.Null);
5217 if (t == TypeManager.uint32_type)
5218 return new UIntConstant (0, Location.Null);
5219 if (t == TypeManager.int64_type)
5220 return new LongConstant (0, Location.Null);
5221 if (t == TypeManager.uint64_type)
5222 return new ULongConstant (0, Location.Null);
5223 if (t == TypeManager.float_type)
5224 return new FloatConstant (0, Location.Null);
5225 if (t == TypeManager.double_type)
5226 return new DoubleConstant (0, Location.Null);
5227 if (t == TypeManager.short_type)
5228 return new ShortConstant (0, Location.Null);
5229 if (t == TypeManager.ushort_type)
5230 return new UShortConstant (0, Location.Null);
5231 if (t == TypeManager.sbyte_type)
5232 return new SByteConstant (0, Location.Null);
5233 if (t == TypeManager.byte_type)
5234 return new ByteConstant (0, Location.Null);
5235 if (t == TypeManager.char_type)
5236 return new CharConstant ('\0', Location.Null);
5237 if (t == TypeManager.bool_type)
5238 return new BoolConstant (false, Location.Null);
5239 if (t == TypeManager.decimal_type)
5240 return new DecimalConstant (0, Location.Null);
5241 if (TypeManager.IsEnumType (t))
5242 return new EnumConstant (Constantify (TypeManager.GetEnumUnderlyingType (t)), t);
5248 // Checks whether the type is an interface that has the
5249 // [ComImport, CoClass] attributes and must be treated
5252 public Expression CheckComImport (EmitContext ec)
5254 if (!type.IsInterface)
5258 // Turn the call into:
5259 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
5261 Type real_class = AttributeTester.GetCoClassAttribute (type);
5262 if (real_class == null)
5265 New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
5266 Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
5267 return cast.Resolve (ec);
5270 public override Expression CreateExpressionTree (EmitContext ec)
5272 ArrayList args = Arguments == null ?
5273 new ArrayList (1) : new ArrayList (Arguments.Count + 1);
5275 if (method == null) {
5276 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
5278 args.Add (new Argument (method.CreateExpressionTree (ec)));
5279 if (Arguments != null) {
5281 foreach (Argument a in Arguments) {
5282 expr = a.Expr.CreateExpressionTree (ec);
5284 args.Add (new Argument (expr));
5289 return CreateExpressionFactoryCall ("New", args);
5292 public override Expression DoResolve (EmitContext ec)
5295 // The New DoResolve might be called twice when initializing field
5296 // expressions (see EmitFieldInitializers, the call to
5297 // GetInitializerExpression will perform a resolve on the expression,
5298 // and later the assign will trigger another resolution
5300 // This leads to bugs (#37014)
5303 if (RequestedType is NewDelegate)
5304 return RequestedType;
5308 TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
5314 if (type == TypeManager.void_type) {
5315 Error_VoidInvalidInTheContext (loc);
5319 if (type.IsPointer) {
5320 Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
5321 TypeManager.CSharpName (type));
5325 if (Arguments == null) {
5326 Expression c = Constantify (type);
5331 if (TypeManager.IsDelegateType (type)) {
5332 RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
5333 if (RequestedType != null)
5334 if (!(RequestedType is DelegateCreation))
5335 throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
5336 return RequestedType;
5340 if (type.IsGenericParameter) {
5341 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5343 if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType)) {
5344 Error (304, String.Format (
5345 "Cannot create an instance of the " +
5346 "variable type '{0}' because it " +
5347 "doesn't have the new() constraint",
5352 if ((Arguments != null) && (Arguments.Count != 0)) {
5353 Error (417, String.Format (
5354 "`{0}': cannot provide arguments " +
5355 "when creating an instance of a " +
5356 "variable type.", type));
5360 if (TypeManager.activator_create_instance == null) {
5361 Type activator_type = TypeManager.CoreLookupType ("System", "Activator", Kind.Class, true);
5362 if (activator_type != null) {
5363 TypeManager.activator_create_instance = TypeManager.GetPredefinedMethod (
5364 activator_type, "CreateInstance", loc, Type.EmptyTypes);
5368 is_type_parameter = true;
5369 eclass = ExprClass.Value;
5374 if (type.IsAbstract && type.IsSealed) {
5375 Report.SymbolRelatedToPreviousError (type);
5376 Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
5380 if (type.IsInterface || type.IsAbstract){
5381 if (!TypeManager.IsGenericType (type)) {
5382 RequestedType = CheckComImport (ec);
5383 if (RequestedType != null)
5384 return RequestedType;
5387 Report.SymbolRelatedToPreviousError (type);
5388 Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
5392 bool is_struct = type.IsValueType;
5393 eclass = ExprClass.Value;
5396 // SRE returns a match for .ctor () on structs (the object constructor),
5397 // so we have to manually ignore it.
5399 if (is_struct && Arguments == null)
5402 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5403 Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
5404 MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
5406 if (Arguments != null){
5407 foreach (Argument a in Arguments){
5408 if (!a.Resolve (ec, loc))
5416 method = ml as MethodGroupExpr;
5417 if (method == null) {
5418 ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
5422 method = method.OverloadResolve (ec, ref Arguments, false, loc);
5429 bool DoEmitTypeParameter (EmitContext ec)
5432 ILGenerator ig = ec.ig;
5433 // IMemoryLocation ml;
5435 MethodInfo ci = TypeManager.activator_create_instance.MakeGenericMethod (
5436 new Type [] { type });
5438 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (type);
5439 if (gc.HasReferenceTypeConstraint || gc.HasClassConstraint) {
5440 ig.Emit (OpCodes.Call, ci);
5444 // Allow DoEmit() to be called multiple times.
5445 // We need to create a new LocalTemporary each time since
5446 // you can't share LocalBuilders among ILGeneators.
5447 LocalTemporary temp = new LocalTemporary (type);
5449 Label label_activator = ig.DefineLabel ();
5450 Label label_end = ig.DefineLabel ();
5452 temp.AddressOf (ec, AddressOp.Store);
5453 ig.Emit (OpCodes.Initobj, type);
5456 ig.Emit (OpCodes.Box, type);
5457 ig.Emit (OpCodes.Brfalse, label_activator);
5459 temp.AddressOf (ec, AddressOp.Store);
5460 ig.Emit (OpCodes.Initobj, type);
5462 ig.Emit (OpCodes.Br, label_end);
5464 ig.MarkLabel (label_activator);
5466 ig.Emit (OpCodes.Call, ci);
5467 ig.MarkLabel (label_end);
5470 throw new InternalErrorException ();
5475 // This DoEmit can be invoked in two contexts:
5476 // * As a mechanism that will leave a value on the stack (new object)
5477 // * As one that wont (init struct)
5479 // You can control whether a value is required on the stack by passing
5480 // need_value_on_stack. The code *might* leave a value on the stack
5481 // so it must be popped manually
5483 // If we are dealing with a ValueType, we have a few
5484 // situations to deal with:
5486 // * The target is a ValueType, and we have been provided
5487 // the instance (this is easy, we are being assigned).
5489 // * The target of New is being passed as an argument,
5490 // to a boxing operation or a function that takes a
5493 // In this case, we need to create a temporary variable
5494 // that is the argument of New.
5496 // Returns whether a value is left on the stack
5498 bool DoEmit (EmitContext ec, bool need_value_on_stack)
5500 bool is_value_type = TypeManager.IsValueType (type);
5501 ILGenerator ig = ec.ig;
5506 // Allow DoEmit() to be called multiple times.
5507 // We need to create a new LocalTemporary each time since
5508 // you can't share LocalBuilders among ILGeneators.
5509 if (!value_target_set)
5510 value_target = new LocalTemporary (type);
5512 ml = (IMemoryLocation) value_target;
5513 ml.AddressOf (ec, AddressOp.Store);
5517 method.EmitArguments (ec, Arguments);
5521 ig.Emit (OpCodes.Initobj, type);
5523 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5524 if (need_value_on_stack){
5525 value_target.Emit (ec);
5530 ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
5535 public override void Emit (EmitContext ec)
5537 if (is_type_parameter)
5538 DoEmitTypeParameter (ec);
5543 public override void EmitStatement (EmitContext ec)
5545 bool value_on_stack;
5547 if (is_type_parameter)
5548 value_on_stack = DoEmitTypeParameter (ec);
5550 value_on_stack = DoEmit (ec, false);
5553 ec.ig.Emit (OpCodes.Pop);
5557 public virtual bool HasInitializer {
5563 public void AddressOf (EmitContext ec, AddressOp Mode)
5565 if (is_type_parameter) {
5566 LocalTemporary temp = new LocalTemporary (type);
5567 DoEmitTypeParameter (ec);
5569 temp.AddressOf (ec, Mode);
5573 if (!type.IsValueType){
5575 // We throw an exception. So far, I believe we only need to support
5577 // foreach (int j in new StructType ())
5580 throw new Exception ("AddressOf should not be used for classes");
5583 if (!value_target_set)
5584 value_target = new LocalTemporary (type);
5585 IMemoryLocation ml = (IMemoryLocation) value_target;
5587 ml.AddressOf (ec, AddressOp.Store);
5588 if (method == null) {
5589 ec.ig.Emit (OpCodes.Initobj, type);
5591 method.EmitArguments (ec, Arguments);
5592 ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
5595 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
5598 protected override void CloneTo (CloneContext clonectx, Expression t)
5600 New target = (New) t;
5602 target.RequestedType = RequestedType.Clone (clonectx);
5603 if (Arguments != null){
5604 target.Arguments = new ArrayList ();
5605 foreach (Argument a in Arguments){
5606 target.Arguments.Add (a.Clone (clonectx));
5613 /// 14.5.10.2: Represents an array creation expression.
5617 /// There are two possible scenarios here: one is an array creation
5618 /// expression that specifies the dimensions and optionally the
5619 /// initialization data and the other which does not need dimensions
5620 /// specified but where initialization data is mandatory.
5622 public class ArrayCreation : Expression {
5623 FullNamedExpression requested_base_type;
5624 ArrayList initializers;
5627 // The list of Argument types.
5628 // This is used to construct the `newarray' or constructor signature
5630 protected ArrayList arguments;
5632 protected Type array_element_type;
5633 bool expect_initializers = false;
5634 int num_arguments = 0;
5635 protected int dimensions;
5636 protected readonly string rank;
5638 protected ArrayList array_data;
5642 // The number of constants in array initializers
5643 int const_initializers_count;
5644 bool only_constant_initializers;
5646 public ArrayCreation (FullNamedExpression requested_base_type, ArrayList exprs, string rank, ArrayList initializers, Location l)
5648 this.requested_base_type = requested_base_type;
5649 this.initializers = initializers;
5653 arguments = new ArrayList ();
5655 foreach (Expression e in exprs) {
5656 arguments.Add (new Argument (e, Argument.AType.Expression));
5661 public ArrayCreation (FullNamedExpression requested_base_type, string rank, ArrayList initializers, Location l)
5663 this.requested_base_type = requested_base_type;
5664 this.initializers = initializers;
5668 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5670 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5672 //dimensions = tmp.Length - 1;
5673 expect_initializers = true;
5676 void Error_IncorrectArrayInitializer ()
5678 Error (178, "Invalid rank specifier: expected `,' or `]'");
5681 protected override void Error_NegativeArrayIndex (Location loc)
5683 Report.Error (248, loc, "Cannot create an array with a negative size");
5686 bool CheckIndices (EmitContext ec, ArrayList probe, int idx, bool specified_dims)
5688 if (specified_dims) {
5689 Argument a = (Argument) arguments [idx];
5691 if (!a.Resolve (ec, loc))
5694 Constant c = a.Expr as Constant;
5696 c = c.ImplicitConversionRequired (TypeManager.int32_type, a.Expr.Location);
5700 Report.Error (150, a.Expr.Location, "A constant value is expected");
5704 int value = (int) c.GetValue ();
5706 if (value != probe.Count) {
5707 Error_IncorrectArrayInitializer ();
5711 bounds [idx] = value;
5714 int child_bounds = -1;
5715 only_constant_initializers = true;
5716 for (int i = 0; i < probe.Count; ++i) {
5717 object o = probe [i];
5718 if (o is ArrayList) {
5719 ArrayList sub_probe = o as ArrayList;
5720 int current_bounds = sub_probe.Count;
5722 if (child_bounds == -1)
5723 child_bounds = current_bounds;
5725 else if (child_bounds != current_bounds){
5726 Error_IncorrectArrayInitializer ();
5729 if (idx + 1 >= dimensions){
5730 Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5734 bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims);
5738 if (child_bounds != -1){
5739 Error_IncorrectArrayInitializer ();
5743 Expression element = ResolveArrayElement (ec, (Expression) o);
5744 if (element == null)
5747 // Initializers with the default values can be ignored
5748 Constant c = element as Constant;
5750 if (c.IsDefaultInitializer (array_element_type)) {
5754 ++const_initializers_count;
5757 only_constant_initializers = false;
5760 array_data.Add (element);
5767 public override Expression CreateExpressionTree (EmitContext ec)
5771 if (dimensions != 1) {
5772 if (initializers != null) {
5773 Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
5777 args = new ArrayList (arguments.Count + 1);
5778 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5779 foreach (Argument a in arguments)
5780 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
5782 return CreateExpressionFactoryCall ("NewArrayBounds", args);
5785 args = new ArrayList (array_data == null ? 1 : array_data.Count + 1);
5786 args.Add (new Argument (new TypeOf (new TypeExpression (array_element_type, loc), loc)));
5787 if (array_data != null) {
5788 foreach (Expression e in array_data)
5789 args.Add (new Argument (e.CreateExpressionTree (ec)));
5792 return CreateExpressionFactoryCall ("NewArrayInit", args);
5795 public void UpdateIndices ()
5798 for (ArrayList probe = initializers; probe != null;) {
5799 if (probe.Count > 0 && probe [0] is ArrayList) {
5800 Expression e = new IntConstant (probe.Count, Location.Null);
5801 arguments.Add (new Argument (e, Argument.AType.Expression));
5803 bounds [i++] = probe.Count;
5805 probe = (ArrayList) probe [0];
5808 Expression e = new IntConstant (probe.Count, Location.Null);
5809 arguments.Add (new Argument (e, Argument.AType.Expression));
5811 bounds [i++] = probe.Count;
5818 Expression first_emit;
5819 LocalTemporary first_emit_temp;
5821 protected virtual Expression ResolveArrayElement (EmitContext ec, Expression element)
5823 element = element.Resolve (ec);
5824 if (element == null)
5827 if (element is CompoundAssign.Helper) {
5828 if (first_emit != null)
5829 throw new InternalErrorException ("Can only handle one mutator at a time");
5830 first_emit = element;
5831 element = first_emit_temp = new LocalTemporary (element.Type);
5834 return Convert.ImplicitConversionRequired (
5835 ec, element, array_element_type, loc);
5838 protected bool ResolveInitializers (EmitContext ec)
5840 if (initializers == null) {
5841 return !expect_initializers;
5845 // We use this to store all the date values in the order in which we
5846 // will need to store them in the byte blob later
5848 array_data = new ArrayList ();
5849 bounds = new System.Collections.Specialized.HybridDictionary ();
5851 if (arguments != null)
5852 return CheckIndices (ec, initializers, 0, true);
5854 arguments = new ArrayList ();
5856 if (!CheckIndices (ec, initializers, 0, false))
5865 // Resolved the type of the array
5867 bool ResolveArrayType (EmitContext ec)
5869 if (requested_base_type == null) {
5870 Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
5874 StringBuilder array_qualifier = new StringBuilder (rank);
5877 // `In the first form allocates an array instace of the type that results
5878 // from deleting each of the individual expression from the expression list'
5880 if (num_arguments > 0) {
5881 array_qualifier.Append ("[");
5882 for (int i = num_arguments-1; i > 0; i--)
5883 array_qualifier.Append (",");
5884 array_qualifier.Append ("]");
5890 TypeExpr array_type_expr;
5891 array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
5892 array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
5893 if (array_type_expr == null)
5896 type = array_type_expr.Type;
5897 array_element_type = TypeManager.GetElementType (type);
5898 dimensions = type.GetArrayRank ();
5903 public override Expression DoResolve (EmitContext ec)
5908 if (!ResolveArrayType (ec))
5911 if ((array_element_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
5912 Report.Error (719, loc, "`{0}': array elements cannot be of static type",
5913 TypeManager.CSharpName (array_element_type));
5917 // First step is to validate the initializers and fill
5918 // in any missing bits
5920 if (!ResolveInitializers (ec))
5923 if (arguments.Count != dimensions) {
5924 Error_IncorrectArrayInitializer ();
5927 foreach (Argument a in arguments){
5928 if (!a.Resolve (ec, loc))
5931 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
5934 eclass = ExprClass.Value;
5938 MethodInfo GetArrayMethod (int arguments)
5940 ModuleBuilder mb = CodeGen.Module.Builder;
5942 Type[] arg_types = new Type[arguments];
5943 for (int i = 0; i < arguments; i++)
5944 arg_types[i] = TypeManager.int32_type;
5946 MethodInfo mi = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
5950 Report.Error (-6, "New invocation: Can not find a constructor for " +
5951 "this argument list");
5958 byte [] MakeByteBlob ()
5963 int count = array_data.Count;
5965 if (TypeManager.IsEnumType (array_element_type))
5966 array_element_type = TypeManager.GetEnumUnderlyingType (array_element_type);
5968 factor = GetTypeSize (array_element_type);
5970 throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type);
5972 data = new byte [(count * factor + 3) & ~3];
5975 for (int i = 0; i < count; ++i) {
5976 object v = array_data [i];
5978 if (v is EnumConstant)
5979 v = ((EnumConstant) v).Child;
5981 if (v is Constant && !(v is StringConstant))
5982 v = ((Constant) v).GetValue ();
5988 if (array_element_type == TypeManager.int64_type){
5989 if (!(v is Expression)){
5990 long val = (long) v;
5992 for (int j = 0; j < factor; ++j) {
5993 data [idx + j] = (byte) (val & 0xFF);
5997 } else if (array_element_type == TypeManager.uint64_type){
5998 if (!(v is Expression)){
5999 ulong val = (ulong) v;
6001 for (int j = 0; j < factor; ++j) {
6002 data [idx + j] = (byte) (val & 0xFF);
6006 } else if (array_element_type == TypeManager.float_type) {
6007 if (!(v is Expression)){
6008 element = BitConverter.GetBytes ((float) v);
6010 for (int j = 0; j < factor; ++j)
6011 data [idx + j] = element [j];
6012 if (!BitConverter.IsLittleEndian)
6013 System.Array.Reverse (data, idx, 4);
6015 } else if (array_element_type == TypeManager.double_type) {
6016 if (!(v is Expression)){
6017 element = BitConverter.GetBytes ((double) v);
6019 for (int j = 0; j < factor; ++j)
6020 data [idx + j] = element [j];
6022 // FIXME: Handle the ARM float format.
6023 if (!BitConverter.IsLittleEndian)
6024 System.Array.Reverse (data, idx, 8);
6026 } else if (array_element_type == TypeManager.char_type){
6027 if (!(v is Expression)){
6028 int val = (int) ((char) v);
6030 data [idx] = (byte) (val & 0xff);
6031 data [idx+1] = (byte) (val >> 8);
6033 } else if (array_element_type == TypeManager.short_type){
6034 if (!(v is Expression)){
6035 int val = (int) ((short) v);
6037 data [idx] = (byte) (val & 0xff);
6038 data [idx+1] = (byte) (val >> 8);
6040 } else if (array_element_type == TypeManager.ushort_type){
6041 if (!(v is Expression)){
6042 int val = (int) ((ushort) v);
6044 data [idx] = (byte) (val & 0xff);
6045 data [idx+1] = (byte) (val >> 8);
6047 } else if (array_element_type == TypeManager.int32_type) {
6048 if (!(v is Expression)){
6051 data [idx] = (byte) (val & 0xff);
6052 data [idx+1] = (byte) ((val >> 8) & 0xff);
6053 data [idx+2] = (byte) ((val >> 16) & 0xff);
6054 data [idx+3] = (byte) (val >> 24);
6056 } else if (array_element_type == TypeManager.uint32_type) {
6057 if (!(v is Expression)){
6058 uint val = (uint) v;
6060 data [idx] = (byte) (val & 0xff);
6061 data [idx+1] = (byte) ((val >> 8) & 0xff);
6062 data [idx+2] = (byte) ((val >> 16) & 0xff);
6063 data [idx+3] = (byte) (val >> 24);
6065 } else if (array_element_type == TypeManager.sbyte_type) {
6066 if (!(v is Expression)){
6067 sbyte val = (sbyte) v;
6068 data [idx] = (byte) val;
6070 } else if (array_element_type == TypeManager.byte_type) {
6071 if (!(v is Expression)){
6072 byte val = (byte) v;
6073 data [idx] = (byte) val;
6075 } else if (array_element_type == TypeManager.bool_type) {
6076 if (!(v is Expression)){
6077 bool val = (bool) v;
6078 data [idx] = (byte) (val ? 1 : 0);
6080 } else if (array_element_type == TypeManager.decimal_type){
6081 if (!(v is Expression)){
6082 int [] bits = Decimal.GetBits ((decimal) v);
6085 // FIXME: For some reason, this doesn't work on the MS runtime.
6086 int [] nbits = new int [4];
6087 nbits [0] = bits [3];
6088 nbits [1] = bits [2];
6089 nbits [2] = bits [0];
6090 nbits [3] = bits [1];
6092 for (int j = 0; j < 4; j++){
6093 data [p++] = (byte) (nbits [j] & 0xff);
6094 data [p++] = (byte) ((nbits [j] >> 8) & 0xff);
6095 data [p++] = (byte) ((nbits [j] >> 16) & 0xff);
6096 data [p++] = (byte) (nbits [j] >> 24);
6100 throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type);
6109 // Emits the initializers for the array
6111 void EmitStaticInitializers (EmitContext ec)
6113 // FIXME: This should go to Resolve !
6114 if (TypeManager.void_initializearray_array_fieldhandle == null) {
6115 TypeManager.void_initializearray_array_fieldhandle = TypeManager.GetPredefinedMethod (
6116 TypeManager.runtime_helpers_type, "InitializeArray", loc,
6117 TypeManager.array_type, TypeManager.runtime_field_handle_type);
6118 if (TypeManager.void_initializearray_array_fieldhandle == null)
6123 // First, the static data
6126 ILGenerator ig = ec.ig;
6128 byte [] data = MakeByteBlob ();
6130 fb = RootContext.MakeStaticData (data);
6132 ig.Emit (OpCodes.Dup);
6133 ig.Emit (OpCodes.Ldtoken, fb);
6134 ig.Emit (OpCodes.Call,
6135 TypeManager.void_initializearray_array_fieldhandle);
6139 // Emits pieces of the array that can not be computed at compile
6140 // time (variables and string locations).
6142 // This always expect the top value on the stack to be the array
6144 void EmitDynamicInitializers (EmitContext ec, bool emitConstants)
6146 ILGenerator ig = ec.ig;
6147 int dims = bounds.Count;
6148 int [] current_pos = new int [dims];
6150 MethodInfo set = null;
6153 Type [] args = new Type [dims + 1];
6155 for (int j = 0; j < dims; j++)
6156 args [j] = TypeManager.int32_type;
6157 args [dims] = array_element_type;
6159 set = CodeGen.Module.Builder.GetArrayMethod (
6161 CallingConventions.HasThis | CallingConventions.Standard,
6162 TypeManager.void_type, args);
6165 for (int i = 0; i < array_data.Count; i++){
6167 Expression e = (Expression)array_data [i];
6169 // Constant can be initialized via StaticInitializer
6170 if (e != null && !(!emitConstants && e is Constant)) {
6171 Type etype = e.Type;
6173 ig.Emit (OpCodes.Dup);
6175 for (int idx = 0; idx < dims; idx++)
6176 IntConstant.EmitInt (ig, current_pos [idx]);
6179 // If we are dealing with a struct, get the
6180 // address of it, so we can store it.
6182 if ((dims == 1) && etype.IsValueType &&
6183 (!TypeManager.IsBuiltinOrEnum (etype) ||
6184 etype == TypeManager.decimal_type)) {
6189 // Let new know that we are providing
6190 // the address where to store the results
6192 n.DisableTemporaryValueType ();
6195 ig.Emit (OpCodes.Ldelema, etype);
6201 bool is_stobj, has_type_arg;
6202 OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj, out has_type_arg);
6204 ig.Emit (OpCodes.Stobj, etype);
6205 else if (has_type_arg)
6206 ig.Emit (op, etype);
6210 ig.Emit (OpCodes.Call, set);
6217 for (int j = dims - 1; j >= 0; j--){
6219 if (current_pos [j] < (int) bounds [j])
6221 current_pos [j] = 0;
6226 public override void Emit (EmitContext ec)
6228 ILGenerator ig = ec.ig;
6230 if (first_emit != null) {
6231 first_emit.Emit (ec);
6232 first_emit_temp.Store (ec);
6235 foreach (Argument a in arguments)
6238 if (arguments.Count == 1)
6239 ig.Emit (OpCodes.Newarr, array_element_type);
6241 ig.Emit (OpCodes.Newobj, GetArrayMethod (arguments.Count));
6244 if (initializers == null)
6247 // Emit static initializer for arrays which have contain more than 4 items and
6248 // the static initializer will initialize at least 25% of array values.
6249 // NOTE: const_initializers_count does not contain default constant values.
6250 if (const_initializers_count >= 4 && const_initializers_count * 4 > (array_data.Count) &&
6251 TypeManager.IsPrimitiveType (array_element_type)) {
6252 EmitStaticInitializers (ec);
6254 if (!only_constant_initializers)
6255 EmitDynamicInitializers (ec, false);
6257 EmitDynamicInitializers (ec, true);
6260 if (first_emit_temp != null)
6261 first_emit_temp.Release (ec);
6264 public override bool GetAttributableValue (Type value_type, out object value)
6266 if (arguments.Count != 1) {
6267 // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6268 return base.GetAttributableValue (null, out value);
6271 if (array_data == null) {
6272 Constant c = (Constant)((Argument)arguments [0]).Expr;
6273 if (c.IsDefaultValue) {
6274 value = Array.CreateInstance (array_element_type, 0);
6277 // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6278 return base.GetAttributableValue (null, out value);
6281 Array ret = Array.CreateInstance (array_element_type, array_data.Count);
6282 object element_value;
6283 for (int i = 0; i < ret.Length; ++i)
6285 Expression e = (Expression)array_data [i];
6287 // Is null when an initializer is optimized (value == predefined value)
6291 if (!e.GetAttributableValue (array_element_type, out element_value)) {
6295 ret.SetValue (element_value, i);
6301 protected override void CloneTo (CloneContext clonectx, Expression t)
6303 ArrayCreation target = (ArrayCreation) t;
6305 if (requested_base_type != null)
6306 target.requested_base_type = (FullNamedExpression)requested_base_type.Clone (clonectx);
6308 if (arguments != null){
6309 target.arguments = new ArrayList (arguments.Count);
6310 foreach (Argument a in arguments)
6311 target.arguments.Add (a.Clone (clonectx));
6314 if (initializers != null){
6315 target.initializers = new ArrayList (initializers.Count);
6316 foreach (object initializer in initializers)
6317 if (initializer is ArrayList) {
6318 ArrayList this_al = (ArrayList)initializer;
6319 ArrayList al = new ArrayList (this_al.Count);
6320 target.initializers.Add (al);
6321 foreach (Expression e in this_al)
6322 al.Add (e.Clone (clonectx));
6324 target.initializers.Add (((Expression)initializer).Clone (clonectx));
6331 // Represents an implicitly typed array epxression
6333 public class ImplicitlyTypedArrayCreation : ArrayCreation
6335 public ImplicitlyTypedArrayCreation (string rank, ArrayList initializers, Location loc)
6336 : base (null, rank, initializers, loc)
6338 if (RootContext.Version <= LanguageVersion.ISO_2)
6339 Report.FeatureIsNotAvailable (loc, "implicitly typed arrays");
6341 if (rank.Length > 2) {
6342 while (rank [++dimensions] == ',');
6348 public override Expression DoResolve (EmitContext ec)
6353 if (!ResolveInitializers (ec))
6356 if (array_element_type == null || array_element_type == TypeManager.null_type ||
6357 array_element_type == TypeManager.void_type || array_element_type == TypeManager.anonymous_method_type ||
6358 arguments.Count != dimensions) {
6359 Report.Error (826, loc, "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
6364 // At this point we found common base type for all initializer elements
6365 // but we have to be sure that all static initializer elements are of
6368 UnifyInitializerElement (ec);
6370 type = TypeManager.GetConstructedType (array_element_type, rank);
6371 eclass = ExprClass.Value;
6376 // Converts static initializer only
6378 void UnifyInitializerElement (EmitContext ec)
6380 for (int i = 0; i < array_data.Count; ++i) {
6381 Expression e = (Expression)array_data[i];
6383 array_data [i] = Convert.ImplicitConversionStandard (ec, e, array_element_type, Location.Null);
6387 protected override Expression ResolveArrayElement (EmitContext ec, Expression element)
6389 element = element.Resolve (ec);
6390 if (element == null)
6393 if (array_element_type == null) {
6394 array_element_type = element.Type;
6398 if (Convert.ImplicitStandardConversionExists (element, array_element_type)) {
6402 if (Convert.ImplicitStandardConversionExists (new TypeExpression (array_element_type, loc), element.Type)) {
6403 array_element_type = element.Type;
6407 element.Error_ValueCannotBeConverted (ec, element.Location, array_element_type, false);
6412 public sealed class CompilerGeneratedThis : This
6414 public static This Instance = new CompilerGeneratedThis ();
6416 private CompilerGeneratedThis ()
6417 : base (Location.Null)
6421 public override Expression DoResolve (EmitContext ec)
6423 eclass = ExprClass.Variable;
6424 type = ec.ContainerType;
6425 variable = new SimpleThis (type);
6431 /// Represents the `this' construct
6434 public class This : VariableReference, IVariable
6437 VariableInfo variable_info;
6438 protected Variable variable;
6441 public This (Block block, Location loc)
6447 public This (Location loc)
6452 public VariableInfo VariableInfo {
6453 get { return variable_info; }
6456 public bool VerifyFixed ()
6458 return !TypeManager.IsValueType (Type);
6461 public override bool IsRef {
6462 get { return is_struct; }
6465 public override Variable Variable {
6466 get { return variable; }
6469 public bool ResolveBase (EmitContext ec)
6471 eclass = ExprClass.Variable;
6473 if (ec.TypeContainer.CurrentType != null)
6474 type = ec.TypeContainer.CurrentType;
6476 type = ec.ContainerType;
6478 is_struct = ec.TypeContainer is Struct;
6481 Error (26, "Keyword `this' is not valid in a static property, " +
6482 "static method, or static field initializer");
6486 if (block != null) {
6487 if (block.Toplevel.ThisVariable != null)
6488 variable_info = block.Toplevel.ThisVariable.VariableInfo;
6490 AnonymousContainer am = ec.CurrentAnonymousMethod;
6491 if (is_struct && (am != null) && !am.IsIterator) {
6492 Report.Error (1673, loc, "Anonymous methods inside structs " +
6493 "cannot access instance members of `this'. " +
6494 "Consider copying `this' to a local variable " +
6495 "outside the anonymous method and using the " +
6499 RootScopeInfo host = block.Toplevel.RootScope;
6500 if ((host != null) && !ec.IsConstructor &&
6501 (!is_struct || host.IsIterator)) {
6502 variable = host.CaptureThis ();
6503 type = variable.Type;
6508 if (variable == null)
6509 variable = new SimpleThis (type);
6515 // Called from Invocation to check if the invocation is correct
6517 public override void CheckMarshalByRefAccess (EmitContext ec)
6519 if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) &&
6520 !variable_info.IsAssigned (ec)) {
6521 Error (188, "The `this' object cannot be used before all of its " +
6522 "fields are assigned to");
6523 variable_info.SetAssigned (ec);
6527 public override Expression CreateExpressionTree (EmitContext ec)
6529 ArrayList args = new ArrayList (2);
6530 args.Add (new Argument (this));
6531 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6532 return CreateExpressionFactoryCall ("Constant", args);
6535 public override Expression DoResolve (EmitContext ec)
6537 if (!ResolveBase (ec))
6541 if (ec.IsInFieldInitializer) {
6542 Error (27, "Keyword `this' is not available in the current context");
6549 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
6551 if (!ResolveBase (ec))
6554 if (variable_info != null)
6555 variable_info.SetAssigned (ec);
6557 if (ec.TypeContainer is Class){
6558 Error (1604, "Cannot assign to 'this' because it is read-only");
6564 public override int GetHashCode()
6566 return block.GetHashCode ();
6569 public override bool Equals (object obj)
6571 This t = obj as This;
6575 return block == t.block;
6578 protected class SimpleThis : Variable
6582 public SimpleThis (Type type)
6587 public override Type Type {
6588 get { return type; }
6591 public override bool HasInstance {
6592 get { return false; }
6595 public override bool NeedsTemporary {
6596 get { return false; }
6599 public override void EmitInstance (EmitContext ec)
6604 public override void Emit (EmitContext ec)
6606 ec.ig.Emit (OpCodes.Ldarg_0);
6609 public override void EmitAssign (EmitContext ec)
6611 throw new InvalidOperationException ();
6614 public override void EmitAddressOf (EmitContext ec)
6616 ec.ig.Emit (OpCodes.Ldarg_0);
6620 protected override void CloneTo (CloneContext clonectx, Expression t)
6622 This target = (This) t;
6624 target.block = clonectx.LookupBlock (block);
6629 /// Represents the `__arglist' construct
6631 public class ArglistAccess : Expression
6633 public ArglistAccess (Location loc)
6638 public override Expression CreateExpressionTree (EmitContext ec)
6640 throw new NotSupportedException ("ET");
6643 public override Expression DoResolve (EmitContext ec)
6645 eclass = ExprClass.Variable;
6646 type = TypeManager.runtime_argument_handle_type;
6648 if (ec.IsInFieldInitializer || !ec.CurrentBlock.Toplevel.HasVarargs)
6650 Error (190, "The __arglist construct is valid only within " +
6651 "a variable argument method");
6658 public override void Emit (EmitContext ec)
6660 ec.ig.Emit (OpCodes.Arglist);
6663 protected override void CloneTo (CloneContext clonectx, Expression target)
6670 /// Represents the `__arglist (....)' construct
6672 public class Arglist : Expression
6674 Argument[] Arguments;
6676 public Arglist (Location loc)
6677 : this (Argument.Empty, loc)
6681 public Arglist (Argument[] args, Location l)
6687 public Type[] ArgumentTypes {
6689 Type[] retval = new Type [Arguments.Length];
6690 for (int i = 0; i < Arguments.Length; i++)
6691 retval [i] = Arguments [i].Type;
6696 public override Expression CreateExpressionTree (EmitContext ec)
6698 Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
6702 public override Expression DoResolve (EmitContext ec)
6704 eclass = ExprClass.Variable;
6705 type = TypeManager.runtime_argument_handle_type;
6707 foreach (Argument arg in Arguments) {
6708 if (!arg.Resolve (ec, loc))
6715 public override void Emit (EmitContext ec)
6717 foreach (Argument arg in Arguments)
6721 protected override void CloneTo (CloneContext clonectx, Expression t)
6723 Arglist target = (Arglist) t;
6725 target.Arguments = new Argument [Arguments.Length];
6726 for (int i = 0; i < Arguments.Length; i++)
6727 target.Arguments [i] = Arguments [i].Clone (clonectx);
6732 /// Implements the typeof operator
6734 public class TypeOf : Expression {
6735 Expression QueriedType;
6736 protected Type typearg;
6738 public TypeOf (Expression queried_type, Location l)
6740 QueriedType = queried_type;
6744 public override Expression CreateExpressionTree (EmitContext ec)
6746 ArrayList args = new ArrayList (2);
6747 args.Add (new Argument (this));
6748 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6749 return CreateExpressionFactoryCall ("Constant", args);
6752 public override Expression DoResolve (EmitContext ec)
6754 if (eclass != ExprClass.Invalid)
6757 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
6761 typearg = texpr.Type;
6763 if (typearg == TypeManager.void_type) {
6764 Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6768 if (typearg.IsPointer && !ec.InUnsafe){
6773 type = TypeManager.type_type;
6775 return DoResolveBase ();
6778 protected Expression DoResolveBase ()
6780 if (TypeManager.system_type_get_type_from_handle == null) {
6781 TypeManager.system_type_get_type_from_handle = TypeManager.GetPredefinedMethod (
6782 TypeManager.type_type, "GetTypeFromHandle", loc, TypeManager.runtime_handle_type);
6785 // Even though what is returned is a type object, it's treated as a value by the compiler.
6786 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6787 eclass = ExprClass.Value;
6791 public override void Emit (EmitContext ec)
6793 ec.ig.Emit (OpCodes.Ldtoken, typearg);
6794 ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
6797 public override bool GetAttributableValue (Type value_type, out object value)
6799 if (TypeManager.ContainsGenericParameters (typearg) &&
6800 !TypeManager.IsGenericTypeDefinition (typearg)) {
6801 Report.SymbolRelatedToPreviousError (typearg);
6802 Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
6803 TypeManager.CSharpName (typearg));
6808 if (value_type == TypeManager.object_type) {
6809 value = (object)typearg;
6816 public Type TypeArgument
6824 protected override void CloneTo (CloneContext clonectx, Expression t)
6826 TypeOf target = (TypeOf) t;
6827 if (QueriedType != null)
6828 target.QueriedType = QueriedType.Clone (clonectx);
6833 /// Implements the `typeof (void)' operator
6835 public class TypeOfVoid : TypeOf {
6836 public TypeOfVoid (Location l) : base (null, l)
6841 public override Expression DoResolve (EmitContext ec)
6843 type = TypeManager.type_type;
6844 typearg = TypeManager.void_type;
6846 return DoResolveBase ();
6850 class TypeOfMethodInfo : TypeOfMethod
6852 public TypeOfMethodInfo (MethodBase method, Location loc)
6853 : base (method, loc)
6857 public override Expression DoResolve (EmitContext ec)
6859 type = typeof (MethodInfo);
6860 return base.DoResolve (ec);
6863 public override void Emit (EmitContext ec)
6865 ec.ig.Emit (OpCodes.Ldtoken, (MethodInfo) method);
6867 ec.ig.Emit (OpCodes.Castclass, type);
6871 class TypeOfConstructorInfo : TypeOfMethod
6873 public TypeOfConstructorInfo (MethodBase method, Location loc)
6874 : base (method, loc)
6878 public override Expression DoResolve (EmitContext ec)
6880 type = typeof (ConstructorInfo);
6881 return base.DoResolve (ec);
6884 public override void Emit (EmitContext ec)
6886 ec.ig.Emit (OpCodes.Ldtoken, (ConstructorInfo) method);
6888 ec.ig.Emit (OpCodes.Castclass, type);
6892 abstract class TypeOfMethod : Expression
6894 protected readonly MethodBase method;
6896 protected TypeOfMethod (MethodBase method, Location loc)
6898 this.method = method;
6902 public override Expression CreateExpressionTree (EmitContext ec)
6904 ArrayList args = new ArrayList (2);
6905 args.Add (new Argument (this));
6906 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
6907 return CreateExpressionFactoryCall ("Constant", args);
6910 public override Expression DoResolve (EmitContext ec)
6912 bool is_generic = TypeManager.IsGenericType (method.DeclaringType);
6913 MethodInfo mi = is_generic ?
6914 TypeManager.methodbase_get_type_from_handle_generic :
6915 TypeManager.methodbase_get_type_from_handle;
6918 Type t = TypeManager.CoreLookupType ("System.Reflection", "MethodBase", Kind.Class, true);
6919 Type handle_type = TypeManager.CoreLookupType ("System", "RuntimeMethodHandle", Kind.Class, true);
6921 if (t == null || handle_type == null)
6924 mi = TypeManager.GetPredefinedMethod (t, "GetMethodFromHandle", loc,
6926 new Type[] { handle_type, TypeManager.runtime_handle_type } :
6927 new Type[] { handle_type } );
6930 TypeManager.methodbase_get_type_from_handle_generic = mi;
6932 TypeManager.methodbase_get_type_from_handle = mi;
6935 eclass = ExprClass.Value;
6939 public override void Emit (EmitContext ec)
6941 bool is_generic = TypeManager.IsGenericType (method.DeclaringType);
6944 mi = TypeManager.methodbase_get_type_from_handle_generic;
6945 ec.ig.Emit (OpCodes.Ldtoken, method.DeclaringType);
6947 mi = TypeManager.methodbase_get_type_from_handle;
6950 ec.ig.Emit (OpCodes.Call, mi);
6954 internal class TypeOfField : Expression
6956 readonly FieldInfo field;
6958 public TypeOfField (FieldInfo field, Location loc)
6964 public override Expression DoResolve (EmitContext ec)
6966 if (TypeManager.fieldinfo_get_field_from_handle == null) {
6967 Type t = TypeManager.CoreLookupType ("System.Reflection", "FieldInfo", Kind.Class, true);
6968 Type handle_type = TypeManager.CoreLookupType ("System", "RuntimeFieldHandle", Kind.Class, true);
6970 if (t != null && handle_type != null)
6971 TypeManager.fieldinfo_get_field_from_handle = TypeManager.GetPredefinedMethod (t,
6972 "GetFieldFromHandle", loc, handle_type);
6975 type = typeof (FieldInfo);
6976 eclass = ExprClass.Value;
6980 public override void Emit (EmitContext ec)
6982 ec.ig.Emit (OpCodes.Ldtoken, field);
6983 ec.ig.Emit (OpCodes.Call, TypeManager.fieldinfo_get_field_from_handle);
6988 /// Implements the sizeof expression
6990 public class SizeOf : Expression {
6991 readonly Expression QueriedType;
6994 public SizeOf (Expression queried_type, Location l)
6996 this.QueriedType = queried_type;
7000 public override Expression CreateExpressionTree (EmitContext ec)
7002 Error_PointerInsideExpressionTree ();
7006 public override Expression DoResolve (EmitContext ec)
7008 TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
7012 type_queried = texpr.Type;
7013 if (TypeManager.IsEnumType (type_queried))
7014 type_queried = TypeManager.GetEnumUnderlyingType (type_queried);
7016 if (type_queried == TypeManager.void_type) {
7017 Expression.Error_VoidInvalidInTheContext (loc);
7021 int size_of = GetTypeSize (type_queried);
7023 return new IntConstant (size_of, loc);
7026 if (!TypeManager.VerifyUnManaged (type_queried, loc)){
7031 Report.Error (233, loc,
7032 "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
7033 TypeManager.CSharpName (type_queried));
7036 type = TypeManager.int32_type;
7037 eclass = ExprClass.Value;
7041 public override void Emit (EmitContext ec)
7043 int size = GetTypeSize (type_queried);
7046 ec.ig.Emit (OpCodes.Sizeof, type_queried);
7048 IntConstant.EmitInt (ec.ig, size);
7051 protected override void CloneTo (CloneContext clonectx, Expression t)
7057 /// Implements the qualified-alias-member (::) expression.
7059 public class QualifiedAliasMember : MemberAccess
7061 readonly string alias;
7063 public QualifiedAliasMember (string alias, string identifier, TypeArguments targs, Location l)
7064 : base (null, identifier, targs, l)
7069 public QualifiedAliasMember (string alias, string identifier, Location l)
7070 : base (null, identifier, l)
7075 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
7077 if (alias == "global") {
7078 expr = RootNamespace.Global;
7079 return base.ResolveAsTypeStep (ec, silent);
7082 int errors = Report.Errors;
7083 expr = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
7085 if (errors == Report.Errors)
7086 Report.Error (432, loc, "Alias `{0}' not found", alias);
7090 FullNamedExpression fne = base.ResolveAsTypeStep (ec, silent);
7094 if (expr.eclass == ExprClass.Type) {
7096 Report.Error (431, loc,
7097 "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
7105 public override Expression DoResolve (EmitContext ec)
7107 return ResolveAsTypeStep (ec, false);
7110 protected override void Error_IdentifierNotFound (IResolveContext rc, FullNamedExpression expr_type, string identifier)
7112 Report.Error (687, loc,
7113 "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
7114 GetSignatureForError ());
7117 public override string GetSignatureForError ()
7120 if (targs != null) {
7121 name = TypeManager.RemoveGenericArity (Name) + "<" +
7122 targs.GetSignatureForError () + ">";
7125 return alias + "::" + name;
7128 protected override void CloneTo (CloneContext clonectx, Expression t)
7135 /// Implements the member access expression
7137 public class MemberAccess : ATypeNameExpression {
7138 protected Expression expr;
7140 public MemberAccess (Expression expr, string id)
7141 : base (id, expr.Location)
7146 public MemberAccess (Expression expr, string identifier, Location loc)
7147 : base (identifier, loc)
7152 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
7153 : base (identifier, args, loc)
7158 // TODO: this method has very poor performace for Enum fields and
7159 // probably for other constants as well
7160 Expression DoResolve (EmitContext ec, Expression right_side)
7163 throw new Exception ();
7166 // Resolve the expression with flow analysis turned off, we'll do the definite
7167 // assignment checks later. This is because we don't know yet what the expression
7168 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7169 // definite assignment check on the actual field and not on the whole struct.
7172 SimpleName original = expr as SimpleName;
7173 Expression expr_resolved = expr.Resolve (ec,
7174 ResolveFlags.VariableOrValue | ResolveFlags.Type |
7175 ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
7177 if (expr_resolved == null)
7180 string LookupIdentifier = MemberName.MakeName (Name, targs);
7182 if (expr_resolved is Namespace) {
7183 Namespace ns = (Namespace) expr_resolved;
7184 FullNamedExpression retval = ns.Lookup (ec.DeclContainer, LookupIdentifier, loc);
7186 if ((retval != null) && (targs != null))
7187 retval = new ConstructedType (retval, targs, loc).ResolveAsTypeStep (ec, false);
7191 ns.Error_NamespaceDoesNotExist (ec.DeclContainer, loc, Name);
7195 Type expr_type = expr_resolved.Type;
7196 if (expr_type.IsPointer || expr_type == TypeManager.void_type ||
7197 expr_resolved is NullLiteral || expr_type == TypeManager.anonymous_method_type) {
7198 Unary.Error_OperatorCannotBeApplied (loc, ".", expr_type);
7202 Constant c = expr_resolved as Constant;
7203 if (c != null && c.GetValue () == null) {
7204 Report.Warning (1720, 1, loc, "Expression will always cause a `{0}'",
7205 "System.NullReferenceException");
7208 if (targs != null) {
7209 if (!targs.Resolve (ec))
7213 Expression member_lookup;
7214 member_lookup = MemberLookup (
7215 ec.ContainerType, expr_type, expr_type, Name, loc);
7217 if ((member_lookup == null) && (targs != null)) {
7218 member_lookup = MemberLookup (
7219 ec.ContainerType, expr_type, expr_type, LookupIdentifier, loc);
7222 if (member_lookup == null) {
7223 ExprClass expr_eclass = expr_resolved.eclass;
7226 // Extension methods are not allowed on all expression types
7228 if (expr_eclass == ExprClass.Value || expr_eclass == ExprClass.Variable ||
7229 expr_eclass == ExprClass.IndexerAccess || expr_eclass == ExprClass.PropertyAccess ||
7230 expr_eclass == ExprClass.EventAccess) {
7231 ExtensionMethodGroupExpr ex_method_lookup = ec.TypeContainer.LookupExtensionMethod (expr_type, Name, loc);
7232 if (ex_method_lookup != null) {
7233 ex_method_lookup.ExtensionExpression = expr_resolved;
7235 if (targs != null) {
7236 ex_method_lookup.SetTypeArguments (targs);
7239 return ex_method_lookup.DoResolve (ec);
7243 expr = expr_resolved;
7244 Error_MemberLookupFailed (
7245 ec.ContainerType, expr_type, expr_type, Name, null,
7246 AllMemberTypes, AllBindingFlags);
7250 TypeExpr texpr = member_lookup as TypeExpr;
7251 if (texpr != null) {
7252 if (!(expr_resolved is TypeExpr) &&
7253 (original == null || !original.IdenticalNameAndTypeName (ec, expr_resolved, loc))) {
7254 Report.Error (572, loc, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7255 Name, member_lookup.GetSignatureForError ());
7259 if (!texpr.CheckAccessLevel (ec.DeclContainer)) {
7260 Report.SymbolRelatedToPreviousError (member_lookup.Type);
7261 ErrorIsInaccesible (loc, TypeManager.CSharpName (member_lookup.Type));
7266 ConstructedType ct = expr_resolved as ConstructedType;
7269 // When looking up a nested type in a generic instance
7270 // via reflection, we always get a generic type definition
7271 // and not a generic instance - so we have to do this here.
7273 // See gtest-172-lib.cs and gtest-172.cs for an example.
7275 ct = new ConstructedType (
7276 member_lookup.Type, ct.TypeArguments, loc);
7278 return ct.ResolveAsTypeStep (ec, false);
7281 return member_lookup;
7284 MemberExpr me = (MemberExpr) member_lookup;
7285 me = me.ResolveMemberAccess (ec, expr_resolved, loc, original);
7289 if (targs != null) {
7290 me.SetTypeArguments (targs);
7293 if (original != null && !TypeManager.IsValueType (expr_type)) {
7294 if (me.IsInstance) {
7295 LocalVariableReference var = expr_resolved as LocalVariableReference;
7296 if (var != null && !var.VerifyAssigned (ec))
7301 // The following DoResolve/DoResolveLValue will do the definite assignment
7304 if (right_side != null)
7305 return me.DoResolveLValue (ec, right_side);
7307 return me.DoResolve (ec);
7310 public override Expression DoResolve (EmitContext ec)
7312 return DoResolve (ec, null);
7315 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7317 return DoResolve (ec, right_side);
7320 public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
7322 return ResolveNamespaceOrType (ec, silent);
7325 public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
7327 FullNamedExpression new_expr = expr.ResolveAsTypeStep (rc, silent);
7329 if (new_expr == null)
7332 string LookupIdentifier = MemberName.MakeName (Name, targs);
7334 if (new_expr is Namespace) {
7335 Namespace ns = (Namespace) new_expr;
7336 FullNamedExpression retval = ns.Lookup (rc.DeclContainer, LookupIdentifier, loc);
7338 if ((retval != null) && (targs != null))
7339 retval = new ConstructedType (retval, targs, loc).ResolveAsTypeStep (rc, false);
7341 if (!silent && retval == null)
7342 ns.Error_NamespaceDoesNotExist (rc.DeclContainer, loc, LookupIdentifier);
7346 TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (rc, false);
7347 if (tnew_expr == null)
7350 Type expr_type = tnew_expr.Type;
7352 if (expr_type.IsPointer){
7353 Error (23, "The `.' operator can not be applied to pointer operands (" +
7354 TypeManager.CSharpName (expr_type) + ")");
7358 Expression member_lookup = MemberLookup (
7359 rc.DeclContainer.TypeBuilder, expr_type, expr_type, LookupIdentifier,
7360 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
7361 if (member_lookup == null) {
7365 Error_IdentifierNotFound (rc, new_expr, LookupIdentifier);
7369 TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
7374 TypeArguments the_args = targs;
7375 Type declaring_type = texpr.Type.DeclaringType;
7376 if (TypeManager.HasGenericArguments (declaring_type)) {
7377 while (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (expr_type), declaring_type)) {
7378 expr_type = expr_type.BaseType;
7381 TypeArguments new_args = new TypeArguments (loc);
7382 foreach (Type decl in TypeManager.GetTypeArguments (expr_type))
7383 new_args.Add (new TypeExpression (decl, loc));
7386 new_args.Add (targs);
7388 the_args = new_args;
7391 if (the_args != null) {
7392 ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
7393 return ctype.ResolveAsTypeStep (rc, false);
7400 protected virtual void Error_IdentifierNotFound (IResolveContext rc, FullNamedExpression expr_type, string identifier)
7402 Expression member_lookup = MemberLookup (
7403 rc.DeclContainer.TypeBuilder, expr_type.Type, expr_type.Type, SimpleName.RemoveGenericArity (identifier),
7404 MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
7406 if (member_lookup != null) {
7407 expr_type = member_lookup.ResolveAsTypeTerminal (rc, false);
7408 if (expr_type == null)
7411 Namespace.Error_TypeArgumentsCannotBeUsed (expr_type.Type, loc);
7415 member_lookup = MemberLookup (
7416 rc.DeclContainer.TypeBuilder, expr_type.Type, expr_type.Type, identifier,
7417 MemberTypes.All, BindingFlags.Public | BindingFlags.NonPublic, loc);
7419 if (member_lookup == null) {
7420 Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
7421 Name, expr_type.GetSignatureForError ());
7423 // TODO: Report.SymbolRelatedToPreviousError
7424 member_lookup.Error_UnexpectedKind (null, "type", loc);
7428 protected override void Error_TypeDoesNotContainDefinition (Type type, string name)
7430 if (RootContext.Version > LanguageVersion.ISO_2 &&
7431 ((expr.eclass & (ExprClass.Value | ExprClass.Variable)) != 0)) {
7432 Report.Error (1061, loc, "Type `{0}' does not contain a definition for `{1}' and no " +
7433 "extension method `{1}' of type `{0}' could be found " +
7434 "(are you missing a using directive or an assembly reference?)",
7435 TypeManager.CSharpName (type), name);
7439 base.Error_TypeDoesNotContainDefinition (type, name);
7442 public override string GetSignatureForError ()
7444 return expr.GetSignatureForError () + "." + base.GetSignatureForError ();
7447 protected override void CloneTo (CloneContext clonectx, Expression t)
7449 MemberAccess target = (MemberAccess) t;
7451 target.expr = expr.Clone (clonectx);
7456 /// Implements checked expressions
7458 public class CheckedExpr : Expression {
7460 public Expression Expr;
7462 public CheckedExpr (Expression e, Location l)
7468 public override Expression CreateExpressionTree (EmitContext ec)
7470 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
7471 return Expr.CreateExpressionTree (ec);
7474 public override Expression DoResolve (EmitContext ec)
7476 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
7477 Expr = Expr.Resolve (ec);
7482 if (Expr is Constant)
7485 eclass = Expr.eclass;
7490 public override void Emit (EmitContext ec)
7492 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
7496 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7498 using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
7499 Expr.EmitBranchable (ec, target, on_true);
7502 protected override void CloneTo (CloneContext clonectx, Expression t)
7504 CheckedExpr target = (CheckedExpr) t;
7506 target.Expr = Expr.Clone (clonectx);
7511 /// Implements the unchecked expression
7513 public class UnCheckedExpr : Expression {
7515 public Expression Expr;
7517 public UnCheckedExpr (Expression e, Location l)
7523 public override Expression CreateExpressionTree (EmitContext ec)
7525 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7526 return Expr.CreateExpressionTree (ec);
7529 public override Expression DoResolve (EmitContext ec)
7531 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7532 Expr = Expr.Resolve (ec);
7537 if (Expr is Constant)
7540 eclass = Expr.eclass;
7545 public override void Emit (EmitContext ec)
7547 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7551 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
7553 using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
7554 Expr.EmitBranchable (ec, target, on_true);
7557 protected override void CloneTo (CloneContext clonectx, Expression t)
7559 UnCheckedExpr target = (UnCheckedExpr) t;
7561 target.Expr = Expr.Clone (clonectx);
7566 /// An Element Access expression.
7568 /// During semantic analysis these are transformed into
7569 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7571 public class ElementAccess : Expression {
7572 public ArrayList Arguments;
7573 public Expression Expr;
7575 public ElementAccess (Expression e, ArrayList e_list)
7584 Arguments = new ArrayList ();
7585 foreach (Expression tmp in e_list)
7586 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
7590 bool CommonResolve (EmitContext ec)
7592 Expr = Expr.Resolve (ec);
7594 if (Arguments == null)
7597 foreach (Argument a in Arguments){
7598 if (!a.Resolve (ec, loc))
7602 return Expr != null;
7605 public override Expression CreateExpressionTree (EmitContext ec)
7607 ArrayList args = new ArrayList (Arguments.Count + 1);
7608 args.Add (new Argument (Expr.CreateExpressionTree (ec)));
7609 foreach (Argument a in Arguments)
7610 args.Add (new Argument (a.Expr.CreateExpressionTree (ec)));
7612 return CreateExpressionFactoryCall ("ArrayIndex", args);
7615 Expression MakePointerAccess (EmitContext ec, Type t)
7617 if (t == TypeManager.void_ptr_type){
7618 Error (242, "The array index operation is not valid on void pointers");
7621 if (Arguments.Count != 1){
7622 Error (196, "A pointer must be indexed by only one value");
7627 p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
7630 return new Indirection (p, loc).Resolve (ec);
7633 public override Expression DoResolve (EmitContext ec)
7635 if (!CommonResolve (ec))
7639 // We perform some simple tests, and then to "split" the emit and store
7640 // code we create an instance of a different class, and return that.
7642 // I am experimenting with this pattern.
7646 if (t == TypeManager.array_type){
7647 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `System.Array'");
7652 return (new ArrayAccess (this, loc)).Resolve (ec);
7654 return MakePointerAccess (ec, t);
7656 FieldExpr fe = Expr as FieldExpr;
7658 IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
7660 return MakePointerAccess (ec, ff.ElementType);
7663 return (new IndexerAccess (this, loc)).Resolve (ec);
7666 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7668 if (!CommonResolve (ec))
7673 return (new ArrayAccess (this, loc)).DoResolveLValue (ec, right_side);
7676 return MakePointerAccess (ec, type);
7678 if (Expr.eclass != ExprClass.Variable && type.IsValueType)
7679 Error_CannotModifyIntermediateExpressionValue (ec);
7681 return (new IndexerAccess (this, loc)).DoResolveLValue (ec, right_side);
7684 public override void Emit (EmitContext ec)
7686 throw new Exception ("Should never be reached");
7689 public override string GetSignatureForError ()
7691 return Expr.GetSignatureForError ();
7694 protected override void CloneTo (CloneContext clonectx, Expression t)
7696 ElementAccess target = (ElementAccess) t;
7698 target.Expr = Expr.Clone (clonectx);
7699 target.Arguments = new ArrayList (Arguments.Count);
7700 foreach (Argument a in Arguments)
7701 target.Arguments.Add (a.Clone (clonectx));
7706 /// Implements array access
7708 public class ArrayAccess : Expression, IAssignMethod, IMemoryLocation {
7710 // Points to our "data" repository
7714 LocalTemporary temp;
7718 public ArrayAccess (ElementAccess ea_data, Location l)
7721 eclass = ExprClass.Variable;
7725 public override Expression CreateExpressionTree (EmitContext ec)
7727 return ea.CreateExpressionTree (ec);
7730 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
7732 return DoResolve (ec);
7735 public override Expression DoResolve (EmitContext ec)
7738 ExprClass eclass = ea.Expr.eclass;
7740 // As long as the type is valid
7741 if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
7742 eclass == ExprClass.Value)) {
7743 ea.Expr.Error_UnexpectedKind ("variable or value");
7748 Type t = ea.Expr.Type;
7749 int rank = ea.Arguments.Count;
7750 if (t.GetArrayRank () != rank) {
7751 Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7752 ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
7756 if (rank != 1 && TypeManager.int_getlength_int == null) {
7757 TypeManager.int_getlength_int = TypeManager.GetPredefinedMethod (
7758 TypeManager.array_type, "GetLength", loc, TypeManager.int32_type);
7761 type = TypeManager.GetElementType (t);
7762 if (type.IsPointer && !ec.InUnsafe) {
7763 UnsafeError (ea.Location);
7767 foreach (Argument a in ea.Arguments) {
7768 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
7771 eclass = ExprClass.Variable;
7777 /// Emits the right opcode to load an object of Type `t'
7778 /// from an array of T
7780 void EmitLoadOpcode (ILGenerator ig, Type type, int rank)
7783 MethodInfo get = FetchGetMethod ();
7784 ig.Emit (OpCodes.Call, get);
7788 if (type == TypeManager.byte_type || type == TypeManager.bool_type)
7789 ig.Emit (OpCodes.Ldelem_U1);
7790 else if (type == TypeManager.sbyte_type)
7791 ig.Emit (OpCodes.Ldelem_I1);
7792 else if (type == TypeManager.short_type)
7793 ig.Emit (OpCodes.Ldelem_I2);
7794 else if (type == TypeManager.ushort_type || type == TypeManager.char_type)
7795 ig.Emit (OpCodes.Ldelem_U2);
7796 else if (type == TypeManager.int32_type)
7797 ig.Emit (OpCodes.Ldelem_I4);
7798 else if (type == TypeManager.uint32_type)
7799 ig.Emit (OpCodes.Ldelem_U4);
7800 else if (type == TypeManager.uint64_type)
7801 ig.Emit (OpCodes.Ldelem_I8);
7802 else if (type == TypeManager.int64_type)
7803 ig.Emit (OpCodes.Ldelem_I8);
7804 else if (type == TypeManager.float_type)
7805 ig.Emit (OpCodes.Ldelem_R4);
7806 else if (type == TypeManager.double_type)
7807 ig.Emit (OpCodes.Ldelem_R8);
7808 else if (type == TypeManager.intptr_type)
7809 ig.Emit (OpCodes.Ldelem_I);
7810 else if (TypeManager.IsEnumType (type)){
7811 EmitLoadOpcode (ig, TypeManager.GetEnumUnderlyingType (type), rank);
7812 } else if (type.IsValueType){
7813 ig.Emit (OpCodes.Ldelema, type);
7814 ig.Emit (OpCodes.Ldobj, type);
7816 } else if (type.IsGenericParameter) {
7817 ig.Emit (OpCodes.Ldelem, type);
7819 } else if (type.IsPointer)
7820 ig.Emit (OpCodes.Ldelem_I);
7822 ig.Emit (OpCodes.Ldelem_Ref);
7825 protected override void Error_NegativeArrayIndex (Location loc)
7827 Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
7831 /// Returns the right opcode to store an object of Type `t'
7832 /// from an array of T.
7834 static public OpCode GetStoreOpcode (Type t, out bool is_stobj, out bool has_type_arg)
7836 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7837 has_type_arg = false; is_stobj = false;
7838 t = TypeManager.TypeToCoreType (t);
7839 if (TypeManager.IsEnumType (t))
7840 t = TypeManager.GetEnumUnderlyingType (t);
7841 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
7842 t == TypeManager.bool_type)
7843 return OpCodes.Stelem_I1;
7844 else if (t == TypeManager.short_type || t == TypeManager.ushort_type ||
7845 t == TypeManager.char_type)
7846 return OpCodes.Stelem_I2;
7847 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
7848 return OpCodes.Stelem_I4;
7849 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
7850 return OpCodes.Stelem_I8;
7851 else if (t == TypeManager.float_type)
7852 return OpCodes.Stelem_R4;
7853 else if (t == TypeManager.double_type)
7854 return OpCodes.Stelem_R8;
7855 else if (t == TypeManager.intptr_type) {
7856 has_type_arg = true;
7858 return OpCodes.Stobj;
7859 } else if (t.IsValueType) {
7860 has_type_arg = true;
7862 return OpCodes.Stobj;
7864 } else if (t.IsGenericParameter) {
7865 has_type_arg = true;
7866 return OpCodes.Stelem;
7869 } else if (t.IsPointer)
7870 return OpCodes.Stelem_I;
7872 return OpCodes.Stelem_Ref;
7875 MethodInfo FetchGetMethod ()
7877 ModuleBuilder mb = CodeGen.Module.Builder;
7878 int arg_count = ea.Arguments.Count;
7879 Type [] args = new Type [arg_count];
7882 for (int i = 0; i < arg_count; i++){
7883 //args [i++] = a.Type;
7884 args [i] = TypeManager.int32_type;
7887 get = mb.GetArrayMethod (
7888 ea.Expr.Type, "Get",
7889 CallingConventions.HasThis |
7890 CallingConventions.Standard,
7896 MethodInfo FetchAddressMethod ()
7898 ModuleBuilder mb = CodeGen.Module.Builder;
7899 int arg_count = ea.Arguments.Count;
7900 Type [] args = new Type [arg_count];
7904 ret_type = TypeManager.GetReferenceType (type);
7906 for (int i = 0; i < arg_count; i++){
7907 //args [i++] = a.Type;
7908 args [i] = TypeManager.int32_type;
7911 address = mb.GetArrayMethod (
7912 ea.Expr.Type, "Address",
7913 CallingConventions.HasThis |
7914 CallingConventions.Standard,
7921 // Load the array arguments into the stack.
7923 void LoadArrayAndArguments (EmitContext ec)
7927 for (int i = 0; i < ea.Arguments.Count; ++i) {
7928 ((Argument)ea.Arguments [i]).Emit (ec);
7932 public void Emit (EmitContext ec, bool leave_copy)
7934 int rank = ea.Expr.Type.GetArrayRank ();
7935 ILGenerator ig = ec.ig;
7938 LoadFromPtr (ig, this.type);
7940 LoadArrayAndArguments (ec);
7941 EmitLoadOpcode (ig, type, rank);
7945 ig.Emit (OpCodes.Dup);
7946 temp = new LocalTemporary (this.type);
7951 public override void Emit (EmitContext ec)
7956 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
7958 int rank = ea.Expr.Type.GetArrayRank ();
7959 ILGenerator ig = ec.ig;
7960 Type t = source.Type;
7961 prepared = prepare_for_load;
7964 AddressOf (ec, AddressOp.LoadStore);
7965 ec.ig.Emit (OpCodes.Dup);
7967 LoadArrayAndArguments (ec);
7971 bool is_stobj, has_type_arg;
7972 OpCode op = GetStoreOpcode (t, out is_stobj, out has_type_arg);
7976 // The stobj opcode used by value types will need
7977 // an address on the stack, not really an array/array
7981 ig.Emit (OpCodes.Ldelema, t);
7986 ec.ig.Emit (OpCodes.Dup);
7987 temp = new LocalTemporary (this.type);
7992 StoreFromPtr (ig, t);
7994 ig.Emit (OpCodes.Stobj, t);
7995 else if (has_type_arg)
8002 ec.ig.Emit (OpCodes.Dup);
8003 temp = new LocalTemporary (this.type);
8008 StoreFromPtr (ig, t);
8010 int arg_count = ea.Arguments.Count;
8011 Type [] args = new Type [arg_count + 1];
8012 for (int i = 0; i < arg_count; i++) {
8013 //args [i++] = a.Type;
8014 args [i] = TypeManager.int32_type;
8016 args [arg_count] = type;
8018 MethodInfo set = CodeGen.Module.Builder.GetArrayMethod (
8019 ea.Expr.Type, "Set",
8020 CallingConventions.HasThis |
8021 CallingConventions.Standard,
8022 TypeManager.void_type, args);
8024 ig.Emit (OpCodes.Call, set);
8034 public void AddressOf (EmitContext ec, AddressOp mode)
8036 int rank = ea.Expr.Type.GetArrayRank ();
8037 ILGenerator ig = ec.ig;
8039 LoadArrayAndArguments (ec);
8042 ig.Emit (OpCodes.Ldelema, type);
8044 MethodInfo address = FetchAddressMethod ();
8045 ig.Emit (OpCodes.Call, address);
8049 public void EmitGetLength (EmitContext ec, int dim)
8051 int rank = ea.Expr.Type.GetArrayRank ();
8052 ILGenerator ig = ec.ig;
8056 ig.Emit (OpCodes.Ldlen);
8057 ig.Emit (OpCodes.Conv_I4);
8059 IntLiteral.EmitInt (ig, dim);
8060 ig.Emit (OpCodes.Callvirt, TypeManager.int_getlength_int);
8066 /// Expressions that represent an indexer call.
8068 public class IndexerAccess : Expression, IAssignMethod
8070 class IndexerMethodGroupExpr : MethodGroupExpr
8072 public IndexerMethodGroupExpr (Indexers indexers, Location loc)
8075 Methods = (MethodBase []) indexers.Methods.ToArray (typeof (MethodBase));
8078 public override string Name {
8084 protected override int GetApplicableParametersCount (MethodBase method, ParameterData parameters)
8087 // Here is the trick, decrease number of arguments by 1 when only
8088 // available property method is setter. This makes overload resolution
8089 // work correctly for indexers.
8092 if (method.Name [0] == 'g')
8093 return parameters.Count;
8095 return parameters.Count - 1;
8101 // Contains either property getter or setter
8102 public ArrayList Methods;
8103 public ArrayList Properties;
8109 void Append (Type caller_type, MemberInfo [] mi)
8114 foreach (PropertyInfo property in mi) {
8115 MethodInfo accessor = property.GetGetMethod (true);
8116 if (accessor == null)
8117 accessor = property.GetSetMethod (true);
8119 if (Methods == null) {
8120 Methods = new ArrayList ();
8121 Properties = new ArrayList ();
8124 Methods.Add (accessor);
8125 Properties.Add (property);
8129 static MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
8131 string p_name = TypeManager.IndexerPropertyName (lookup_type);
8133 return TypeManager.MemberLookup (
8134 caller_type, caller_type, lookup_type, MemberTypes.Property,
8135 BindingFlags.Public | BindingFlags.Instance |
8136 BindingFlags.DeclaredOnly, p_name, null);
8139 public static Indexers GetIndexersForType (Type caller_type, Type lookup_type)
8141 Indexers ix = new Indexers ();
8144 if (lookup_type.IsGenericParameter) {
8145 GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
8149 if (gc.HasClassConstraint)
8150 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
8152 Type[] ifaces = gc.InterfaceConstraints;
8153 foreach (Type itype in ifaces)
8154 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
8160 Type copy = lookup_type;
8161 while (copy != TypeManager.object_type && copy != null){
8162 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
8163 copy = copy.BaseType;
8166 if (lookup_type.IsInterface) {
8167 Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
8168 if (ifaces != null) {
8169 foreach (Type itype in ifaces)
8170 ix.Append (caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
8185 // Points to our "data" repository
8187 MethodInfo get, set;
8188 bool is_base_indexer;
8190 LocalTemporary temp;
8191 LocalTemporary prepared_value;
8192 Expression set_expr;
8194 protected Type indexer_type;
8195 protected Type current_type;
8196 protected Expression instance_expr;
8197 protected ArrayList arguments;
8199 public IndexerAccess (ElementAccess ea, Location loc)
8200 : this (ea.Expr, false, loc)
8202 this.arguments = ea.Arguments;
8205 protected IndexerAccess (Expression instance_expr, bool is_base_indexer,
8208 this.instance_expr = instance_expr;
8209 this.is_base_indexer = is_base_indexer;
8210 this.eclass = ExprClass.Value;
8214 static string GetAccessorName (AccessorType at)
8216 if (at == AccessorType.Set)
8219 if (at == AccessorType.Get)
8222 throw new NotImplementedException (at.ToString ());
8225 protected virtual bool CommonResolve (EmitContext ec)
8227 indexer_type = instance_expr.Type;
8228 current_type = ec.ContainerType;
8233 public override Expression DoResolve (EmitContext ec)
8235 return ResolveAccessor (ec, AccessorType.Get);
8238 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8240 if (right_side == EmptyExpression.OutAccess) {
8241 Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
8242 GetSignatureForError ());
8246 // if the indexer returns a value type, and we try to set a field in it
8247 if (right_side == EmptyExpression.LValueMemberAccess || right_side == EmptyExpression.LValueMemberOutAccess) {
8248 Error_CannotModifyIntermediateExpressionValue (ec);
8251 Expression e = ResolveAccessor (ec, AccessorType.Set);
8255 set_expr = Convert.ImplicitConversion (ec, right_side, type, loc);
8259 Expression ResolveAccessor (EmitContext ec, AccessorType accessorType)
8261 if (!CommonResolve (ec))
8264 Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
8265 if (ilist.Methods == null) {
8266 Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
8267 TypeManager.CSharpName (indexer_type));
8271 MethodGroupExpr mg = new IndexerMethodGroupExpr (ilist, loc);
8272 mg = mg.OverloadResolve (ec, ref arguments, false, loc);
8276 MethodInfo mi = (MethodInfo) mg;
8277 PropertyInfo pi = null;
8278 for (int i = 0; i < ilist.Methods.Count; ++i) {
8279 if (ilist.Methods [i] == mi) {
8280 pi = (PropertyInfo) ilist.Properties [i];
8285 type = TypeManager.TypeToCoreType (pi.PropertyType);
8286 if (type.IsPointer && !ec.InUnsafe)
8289 MethodInfo accessor;
8290 if (accessorType == AccessorType.Get) {
8291 accessor = get = pi.GetGetMethod (true);
8293 accessor = set = pi.GetSetMethod (true);
8294 if (accessor == null && pi.GetGetMethod (true) != null) {
8295 Report.SymbolRelatedToPreviousError (pi);
8296 Report.Error (200, loc, "The read only property or indexer `{0}' cannot be assigned to",
8297 TypeManager.GetFullNameSignature (pi));
8302 if (accessor == null) {
8303 Report.SymbolRelatedToPreviousError (pi);
8304 Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
8305 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
8310 // Only base will allow this invocation to happen.
8312 if (accessor.IsAbstract && this is BaseIndexerAccess) {
8313 Error_CannotCallAbstractBase (TypeManager.GetFullNameSignature (pi));
8316 bool must_do_cs1540_check;
8317 if (!IsAccessorAccessible (ec.ContainerType, accessor, out must_do_cs1540_check)) {
8319 set = pi.GetSetMethod (true);
8321 get = pi.GetGetMethod (true);
8323 if (set != null && get != null &&
8324 (set.Attributes & MethodAttributes.MemberAccessMask) != (get.Attributes & MethodAttributes.MemberAccessMask)) {
8325 Report.SymbolRelatedToPreviousError (accessor);
8326 Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
8327 TypeManager.GetFullNameSignature (pi), GetAccessorName (accessorType));
8329 Report.SymbolRelatedToPreviousError (pi);
8330 ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (pi));
8334 instance_expr.CheckMarshalByRefAccess (ec);
8335 eclass = ExprClass.IndexerAccess;
8339 public void Emit (EmitContext ec, bool leave_copy)
8342 prepared_value.Emit (ec);
8344 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
8345 arguments, loc, false, false);
8349 ec.ig.Emit (OpCodes.Dup);
8350 temp = new LocalTemporary (Type);
8356 // source is ignored, because we already have a copy of it from the
8357 // LValue resolution and we have already constructed a pre-cached
8358 // version of the arguments (ea.set_arguments);
8360 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
8362 prepared = prepare_for_load;
8363 Expression value = set_expr;
8366 Invocation.EmitCall (ec, is_base_indexer, instance_expr, get,
8367 arguments, loc, true, false);
8369 prepared_value = new LocalTemporary (type);
8370 prepared_value.Store (ec);
8372 prepared_value.Release (ec);
8375 ec.ig.Emit (OpCodes.Dup);
8376 temp = new LocalTemporary (Type);
8379 } else if (leave_copy) {
8380 temp = new LocalTemporary (Type);
8386 arguments.Add (new Argument (value, Argument.AType.Expression));
8387 Invocation.EmitCall (ec, is_base_indexer, instance_expr, set, arguments, loc, false, prepared);
8395 public override void Emit (EmitContext ec)
8400 public override string GetSignatureForError ()
8402 return TypeManager.CSharpSignature (get != null ? get : set, false);
8405 protected override void CloneTo (CloneContext clonectx, Expression t)
8407 IndexerAccess target = (IndexerAccess) t;
8409 if (arguments != null){
8410 target.arguments = new ArrayList ();
8411 foreach (Argument a in arguments)
8412 target.arguments.Add (a.Clone (clonectx));
8414 if (instance_expr != null)
8415 target.instance_expr = instance_expr.Clone (clonectx);
8420 /// The base operator for method names
8422 public class BaseAccess : Expression {
8423 public readonly string Identifier;
8426 public BaseAccess (string member, Location l)
8428 this.Identifier = member;
8432 public BaseAccess (string member, TypeArguments args, Location l)
8438 public override Expression DoResolve (EmitContext ec)
8440 Expression c = CommonResolve (ec);
8446 // MethodGroups use this opportunity to flag an error on lacking ()
8448 if (!(c is MethodGroupExpr))
8449 return c.Resolve (ec);
8453 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
8455 Expression c = CommonResolve (ec);
8461 // MethodGroups use this opportunity to flag an error on lacking ()
8463 if (! (c is MethodGroupExpr))
8464 return c.DoResolveLValue (ec, right_side);
8469 Expression CommonResolve (EmitContext ec)
8471 Expression member_lookup;
8472 Type current_type = ec.ContainerType;
8473 Type base_type = current_type.BaseType;
8476 Error (1511, "Keyword `base' is not available in a static method");
8480 if (ec.IsInFieldInitializer){
8481 Error (1512, "Keyword `base' is not available in the current context");
8485 member_lookup = MemberLookup (ec.ContainerType, null, base_type, Identifier,
8486 AllMemberTypes, AllBindingFlags, loc);
8487 if (member_lookup == null) {
8488 Error_MemberLookupFailed (ec.ContainerType, base_type, base_type, Identifier,
8489 null, AllMemberTypes, AllBindingFlags);
8496 left = new TypeExpression (base_type, loc);
8498 left = ec.GetThis (loc);
8500 MemberExpr me = (MemberExpr) member_lookup;
8501 me = me.ResolveMemberAccess (ec, left, loc, null);
8508 me.SetTypeArguments (args);
8514 public override void Emit (EmitContext ec)
8516 throw new Exception ("Should never be called");
8519 protected override void CloneTo (CloneContext clonectx, Expression t)
8521 BaseAccess target = (BaseAccess) t;
8524 target.args = args.Clone ();
8529 /// The base indexer operator
8531 public class BaseIndexerAccess : IndexerAccess {
8532 public BaseIndexerAccess (ArrayList args, Location loc)
8533 : base (null, true, loc)
8535 arguments = new ArrayList ();
8536 foreach (Expression tmp in args)
8537 arguments.Add (new Argument (tmp, Argument.AType.Expression));
8540 protected override bool CommonResolve (EmitContext ec)
8542 instance_expr = ec.GetThis (loc);
8544 current_type = ec.ContainerType.BaseType;
8545 indexer_type = current_type;
8547 foreach (Argument a in arguments){
8548 if (!a.Resolve (ec, loc))
8557 /// This class exists solely to pass the Type around and to be a dummy
8558 /// that can be passed to the conversion functions (this is used by
8559 /// foreach implementation to typecast the object return value from
8560 /// get_Current into the proper type. All code has been generated and
8561 /// we only care about the side effect conversions to be performed
8563 /// This is also now used as a placeholder where a no-action expression
8564 /// is needed (the `New' class).
8566 public class EmptyExpression : Expression {
8567 public static readonly EmptyExpression Null = new EmptyExpression ();
8569 public static readonly EmptyExpression OutAccess = new EmptyExpression ();
8570 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
8571 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression ();
8573 static EmptyExpression temp = new EmptyExpression ();
8574 public static EmptyExpression Grab ()
8576 EmptyExpression retval = temp == null ? new EmptyExpression () : temp;
8581 public static void Release (EmptyExpression e)
8586 // TODO: should be protected
8587 public EmptyExpression ()
8589 type = TypeManager.object_type;
8590 eclass = ExprClass.Value;
8591 loc = Location.Null;
8594 public EmptyExpression (Type t)
8597 eclass = ExprClass.Value;
8598 loc = Location.Null;
8601 public override Expression DoResolve (EmitContext ec)
8606 public override void Emit (EmitContext ec)
8608 // nothing, as we only exist to not do anything.
8611 public override void EmitSideEffect (EmitContext ec)
8616 // This is just because we might want to reuse this bad boy
8617 // instead of creating gazillions of EmptyExpressions.
8618 // (CanImplicitConversion uses it)
8620 public void SetType (Type t)
8627 // Empty statement expression
8629 public sealed class EmptyExpressionStatement : ExpressionStatement
8631 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
8633 private EmptyExpressionStatement ()
8635 type = TypeManager.object_type;
8636 eclass = ExprClass.Value;
8637 loc = Location.Null;
8640 public override void EmitStatement (EmitContext ec)
8645 public override Expression DoResolve (EmitContext ec)
8650 public override void Emit (EmitContext ec)
8656 public class UserCast : Expression {
8660 public UserCast (MethodInfo method, Expression source, Location l)
8662 this.method = method;
8663 this.source = source;
8664 type = TypeManager.TypeToCoreType (method.ReturnType);
8665 eclass = ExprClass.Value;
8669 public Expression Source {
8675 public override Expression CreateExpressionTree (EmitContext ec)
8677 ArrayList args = new ArrayList (2);
8678 args.Add (new Argument (source.CreateExpressionTree (ec)));
8679 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
8680 args.Add (new Argument (new TypeOfMethodInfo (method, loc)));
8681 return CreateExpressionFactoryCall ("Convert", args);
8684 public override Expression DoResolve (EmitContext ec)
8687 // We are born fully resolved
8692 public override void Emit (EmitContext ec)
8695 ec.ig.Emit (OpCodes.Call, method);
8700 // This class is used to "construct" the type during a typecast
8701 // operation. Since the Type.GetType class in .NET can parse
8702 // the type specification, we just use this to construct the type
8703 // one bit at a time.
8705 public class ComposedCast : TypeExpr {
8706 FullNamedExpression left;
8709 public ComposedCast (FullNamedExpression left, string dim)
8710 : this (left, dim, left.Location)
8714 public ComposedCast (FullNamedExpression left, string dim, Location l)
8721 public Expression RemoveNullable ()
8723 if (dim.EndsWith ("?")) {
8724 dim = dim.Substring (0, dim.Length - 1);
8725 if (dim.Length == 0)
8732 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
8734 TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
8738 Type ltype = lexpr.Type;
8739 if ((ltype == TypeManager.void_type) && (dim != "*")) {
8740 Error_VoidInvalidInTheContext (loc);
8745 if ((dim.Length > 0) && (dim [0] == '?')) {
8746 TypeExpr nullable = new Nullable.NullableType (left, loc);
8748 nullable = new ComposedCast (nullable, dim.Substring (1), loc);
8749 return nullable.ResolveAsTypeTerminal (ec, false);
8753 if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc))
8756 if (dim != "" && dim [0] == '[' &&
8757 (ltype == TypeManager.arg_iterator_type || ltype == TypeManager.typed_reference_type)) {
8758 Report.Error (611, loc, "Array elements cannot be of type `{0}'", TypeManager.CSharpName (ltype));
8763 type = TypeManager.GetConstructedType (ltype, dim);
8768 throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
8770 if (type.IsPointer && !ec.IsInUnsafeScope){
8775 eclass = ExprClass.Type;
8779 public override string GetSignatureForError ()
8781 return left.GetSignatureForError () + dim;
8784 protected override void CloneTo (CloneContext clonectx, Expression t)
8786 ComposedCast target = (ComposedCast) t;
8788 target.left = (FullNamedExpression)left.Clone (clonectx);
8792 public class FixedBufferPtr : Expression {
8795 public FixedBufferPtr (Expression array, Type array_type, Location l)
8800 type = TypeManager.GetPointerType (array_type);
8801 eclass = ExprClass.Value;
8804 public override Expression CreateExpressionTree (EmitContext ec)
8806 Error_PointerInsideExpressionTree ();
8810 public override void Emit(EmitContext ec)
8815 public override Expression DoResolve (EmitContext ec)
8818 // We are born fully resolved
8826 // This class is used to represent the address of an array, used
8827 // only by the Fixed statement, this generates "&a [0]" construct
8828 // for fixed (char *pa = a)
8830 public class ArrayPtr : FixedBufferPtr {
8833 public ArrayPtr (Expression array, Type array_type, Location l):
8834 base (array, array_type, l)
8836 this.array_type = array_type;
8839 public override void Emit (EmitContext ec)
8843 ILGenerator ig = ec.ig;
8844 IntLiteral.EmitInt (ig, 0);
8845 ig.Emit (OpCodes.Ldelema, array_type);
8850 // Encapsulates a conversion rules required for array indexes
8852 public class ArrayIndexCast : TypeCast
8854 public ArrayIndexCast (Expression expr)
8855 : base (expr, expr.Type)
8859 public override Expression CreateExpressionTree (EmitContext ec)
8861 ArrayList args = new ArrayList (2);
8862 args.Add (new Argument (child.CreateExpressionTree (ec)));
8863 args.Add (new Argument (new TypeOf (new TypeExpression (TypeManager.int32_type, loc), loc)));
8864 return CreateExpressionFactoryCall ("ConvertChecked", args);
8867 public override void Emit (EmitContext ec)
8871 if (type == TypeManager.int32_type)
8874 if (type == TypeManager.uint32_type)
8875 ec.ig.Emit (OpCodes.Conv_U);
8876 else if (type == TypeManager.int64_type)
8877 ec.ig.Emit (OpCodes.Conv_Ovf_I);
8878 else if (type == TypeManager.uint64_type)
8879 ec.ig.Emit (OpCodes.Conv_Ovf_I_Un);
8881 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
8886 // Implements the `stackalloc' keyword
8888 public class StackAlloc : Expression {
8893 public StackAlloc (Expression type, Expression count, Location l)
8900 public override Expression CreateExpressionTree (EmitContext ec)
8902 throw new NotSupportedException ("ET");
8905 public override Expression DoResolve (EmitContext ec)
8907 count = count.Resolve (ec);
8911 if (count.Type != TypeManager.int32_type){
8912 count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
8917 Constant c = count as Constant;
8918 if (c != null && c.IsNegative) {
8919 Report.Error (247, loc, "Cannot use a negative size with stackalloc");
8923 if (ec.InCatch || ec.InFinally) {
8924 Error (255, "Cannot use stackalloc in finally or catch");
8928 TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
8934 if (!TypeManager.VerifyUnManaged (otype, loc))
8937 type = TypeManager.GetPointerType (otype);
8938 eclass = ExprClass.Value;
8943 public override void Emit (EmitContext ec)
8945 int size = GetTypeSize (otype);
8946 ILGenerator ig = ec.ig;
8949 ig.Emit (OpCodes.Sizeof, otype);
8951 IntConstant.EmitInt (ig, size);
8953 ig.Emit (OpCodes.Mul);
8954 ig.Emit (OpCodes.Localloc);
8957 protected override void CloneTo (CloneContext clonectx, Expression t)
8959 StackAlloc target = (StackAlloc) t;
8960 target.count = count.Clone (clonectx);
8961 target.t = t.Clone (clonectx);
8966 // An object initializer expression
8968 public class ElementInitializer : Assign
8970 public readonly string Name;
8972 public ElementInitializer (string name, Expression initializer, Location loc)
8973 : base (null, initializer, loc)
8978 protected override void CloneTo (CloneContext clonectx, Expression t)
8980 ElementInitializer target = (ElementInitializer) t;
8981 target.source = source.Clone (clonectx);
8984 public override Expression CreateExpressionTree (EmitContext ec)
8986 ArrayList args = new ArrayList (2);
8987 FieldExpr fe = target as FieldExpr;
8989 args.Add (new Argument (fe.CreateTypeOfExpression ()));
8991 args.Add (new Argument (((PropertyExpr)target).CreateSetterTypeOfExpression ()));
8993 args.Add (new Argument (source.CreateExpressionTree (ec)));
8994 return CreateExpressionFactoryCall (
8995 source is CollectionOrObjectInitializers ? "ListBind" : "Bind",
8999 public override Expression DoResolve (EmitContext ec)
9002 return EmptyExpressionStatement.Instance;
9004 MemberExpr me = MemberLookupFinal (ec, ec.CurrentInitializerVariable.Type, ec.CurrentInitializerVariable.Type,
9005 Name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, loc) as MemberExpr;
9011 me.InstanceExpression = ec.CurrentInitializerVariable;
9013 if (source is CollectionOrObjectInitializers) {
9014 Expression previous = ec.CurrentInitializerVariable;
9015 ec.CurrentInitializerVariable = target;
9016 source = source.Resolve (ec);
9017 ec.CurrentInitializerVariable = previous;
9021 eclass = source.eclass;
9026 Expression expr = base.DoResolve (ec);
9031 // Ignore field initializers with default value
9033 Constant c = source as Constant;
9034 if (c != null && c.IsDefaultInitializer (type) && target.eclass == ExprClass.Variable)
9035 return EmptyExpressionStatement.Instance;
9040 protected override Expression Error_MemberLookupFailed (MemberInfo[] members)
9042 MemberInfo member = members [0];
9043 if (member.MemberType != MemberTypes.Property && member.MemberType != MemberTypes.Field)
9044 Report.Error (1913, loc, "Member `{0}' cannot be initialized. An object " +
9045 "initializer may only be used for fields, or properties", TypeManager.GetFullNameSignature (member));
9047 Report.Error (1914, loc, " Static field or property `{0}' cannot be assigned in an object initializer",
9048 TypeManager.GetFullNameSignature (member));
9053 public override void EmitStatement (EmitContext ec)
9055 if (source is CollectionOrObjectInitializers)
9058 base.EmitStatement (ec);
9063 // A collection initializer expression
9065 public class CollectionElementInitializer : Invocation
9067 public class ElementInitializerArgument : Argument
9069 public ElementInitializerArgument (Expression e)
9075 public CollectionElementInitializer (Expression argument)
9076 : base (null, new ArrayList (1), true)
9078 Arguments.Add (argument);
9079 this.loc = argument.Location;
9082 public CollectionElementInitializer (ArrayList arguments, Location loc)
9083 : base (null, arguments, true)
9088 public override Expression CreateExpressionTree (EmitContext ec)
9090 ArrayList args = new ArrayList (2);
9091 args.Add (new Argument (mg.CreateExpressionTree (ec)));
9093 ArrayList expr_initializers = new ArrayList (Arguments.Count);
9094 foreach (Argument a in Arguments)
9095 expr_initializers.Add (a.Expr.CreateExpressionTree (ec));
9097 args.Add (new Argument (new ArrayCreation (
9098 CreateExpressionTypeExpression (loc), "[]", expr_initializers, loc)));
9099 return CreateExpressionFactoryCall ("ElementInit", args);
9102 protected override void CloneTo (CloneContext clonectx, Expression t)
9104 CollectionElementInitializer target = (CollectionElementInitializer) t;
9106 target.Arguments = new ArrayList (Arguments.Count);
9107 foreach (Expression e in Arguments)
9108 target.Arguments.Add (e.Clone (clonectx));
9111 public override Expression DoResolve (EmitContext ec)
9113 if (eclass != ExprClass.Invalid)
9116 // TODO: We could call a constructor which takes element count argument,
9117 // for known types like List<T>, Dictionary<T, U>
9119 for (int i = 0; i < Arguments.Count; ++i) {
9120 Expression expr = ((Expression) Arguments [i]).Resolve (ec);
9124 Arguments [i] = new ElementInitializerArgument (expr);
9127 base.expr = new MemberAccess (ec.CurrentInitializerVariable, "Add", loc);
9129 return base.DoResolve (ec);
9134 // A block of object or collection initializers
9136 public class CollectionOrObjectInitializers : ExpressionStatement
9138 ArrayList initializers;
9140 public static readonly CollectionOrObjectInitializers Empty =
9141 new CollectionOrObjectInitializers (new ArrayList (0), Location.Null);
9143 public CollectionOrObjectInitializers (ArrayList initializers, Location loc)
9145 this.initializers = initializers;
9149 public bool IsEmpty {
9151 return initializers.Count == 0;
9155 public bool IsCollectionInitializer {
9157 return type == typeof (CollectionOrObjectInitializers);
9161 protected override void CloneTo (CloneContext clonectx, Expression target)
9163 CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
9165 t.initializers = new ArrayList (initializers.Count);
9166 foreach (Expression e in initializers)
9167 t.initializers.Add (e.Clone (clonectx));
9170 public override Expression CreateExpressionTree (EmitContext ec)
9172 ArrayList expr_initializers = new ArrayList (initializers.Count);
9173 foreach (Expression e in initializers) {
9174 Expression expr = e.CreateExpressionTree (ec);
9176 expr_initializers.Add (expr);
9179 return new ImplicitlyTypedArrayCreation ("[]", expr_initializers, loc);
9182 public override Expression DoResolve (EmitContext ec)
9184 if (eclass != ExprClass.Invalid)
9187 bool is_elements_initialization = false;
9188 ArrayList element_names = null;
9189 for (int i = 0; i < initializers.Count; ++i) {
9190 Expression initializer = (Expression) initializers [i];
9191 ElementInitializer element_initializer = initializer as ElementInitializer;
9194 if (element_initializer != null) {
9195 is_elements_initialization = true;
9196 element_names = new ArrayList (initializers.Count);
9197 element_names.Add (element_initializer.Name);
9199 if (!TypeManager.ImplementsInterface (ec.CurrentInitializerVariable.Type,
9200 TypeManager.ienumerable_type)) {
9201 Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
9202 "object initializer because type `{1}' does not implement `{2}' interface",
9203 ec.CurrentInitializerVariable.GetSignatureForError (),
9204 TypeManager.CSharpName (ec.CurrentInitializerVariable.Type),
9205 TypeManager.CSharpName (TypeManager.ienumerable_type));
9210 if (is_elements_initialization == (element_initializer == null)) {
9211 Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
9212 is_elements_initialization ? "object initializer" : "collection initializer");
9216 if (is_elements_initialization) {
9217 if (element_names.Contains (element_initializer.Name)) {
9218 Report.Error (1912, element_initializer.Location,
9219 "An object initializer includes more than one member `{0}' initialization",
9220 element_initializer.Name);
9222 element_names.Add (element_initializer.Name);
9227 Expression e = initializer.Resolve (ec);
9228 if (e == EmptyExpressionStatement.Instance)
9229 initializers.RemoveAt (i--);
9231 initializers [i] = e;
9234 type = is_elements_initialization ? typeof (ElementInitializer) : typeof (CollectionOrObjectInitializers);
9235 eclass = ExprClass.Variable;
9239 public override void Emit (EmitContext ec)
9244 public override void EmitStatement (EmitContext ec)
9246 foreach (ExpressionStatement e in initializers)
9247 e.EmitStatement (ec);
9252 // New expression with element/object initializers
9254 public class NewInitialize : New
9257 // This class serves as a proxy for variable initializer target instances.
9258 // A real variable is assigned later when we resolve left side of an
9261 sealed class InitializerTargetExpression : Expression, IMemoryLocation
9263 NewInitialize new_instance;
9265 public InitializerTargetExpression (NewInitialize newInstance)
9267 this.type = newInstance.type;
9268 this.loc = newInstance.loc;
9269 this.eclass = newInstance.eclass;
9270 this.new_instance = newInstance;
9273 public override Expression CreateExpressionTree (EmitContext ec)
9275 // Should not be reached
9276 throw new NotSupportedException ("ET");
9279 public override Expression DoResolve (EmitContext ec)
9284 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
9289 public override void Emit (EmitContext ec)
9291 new_instance.value_target.Emit (ec);
9294 #region IMemoryLocation Members
9296 public void AddressOf (EmitContext ec, AddressOp mode)
9298 ((IMemoryLocation)new_instance.value_target).AddressOf (ec, mode);
9304 CollectionOrObjectInitializers initializers;
9306 public NewInitialize (Expression requested_type, ArrayList arguments, CollectionOrObjectInitializers initializers, Location l)
9307 : base (requested_type, arguments, l)
9309 this.initializers = initializers;
9312 protected override void CloneTo (CloneContext clonectx, Expression t)
9314 base.CloneTo (clonectx, t);
9316 NewInitialize target = (NewInitialize) t;
9317 target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
9320 public override Expression CreateExpressionTree (EmitContext ec)
9322 ArrayList args = new ArrayList (2);
9323 args.Add (new Argument (base.CreateExpressionTree (ec)));
9324 args.Add (new Argument (initializers.CreateExpressionTree (ec)));
9326 return CreateExpressionFactoryCall (
9327 initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
9331 public override Expression DoResolve (EmitContext ec)
9333 if (eclass != ExprClass.Invalid)
9336 Expression e = base.DoResolve (ec);
9340 // Empty initializer can be optimized to simple new
9341 if (initializers.IsEmpty)
9344 Expression previous = ec.CurrentInitializerVariable;
9345 ec.CurrentInitializerVariable = new InitializerTargetExpression (this);
9346 initializers.Resolve (ec);
9347 ec.CurrentInitializerVariable = previous;
9351 public override void Emit (EmitContext ec)
9356 // If target is a value, let's use it
9358 VariableReference variable = value_target as VariableReference;
9359 if (variable != null) {
9361 StoreFromPtr (ec.ig, type);
9363 variable.Variable.EmitAssign (ec);
9365 if (value_target == null || value_target_set)
9366 value_target = new LocalTemporary (type);
9368 ((LocalTemporary) value_target).Store (ec);
9371 initializers.Emit (ec);
9373 if (variable == null)
9374 value_target.Emit (ec);
9377 public override void EmitStatement (EmitContext ec)
9379 if (initializers.IsEmpty) {
9380 base.EmitStatement (ec);
9386 if (value_target == null) {
9387 LocalTemporary variable = new LocalTemporary (type);
9388 variable.Store (ec);
9389 value_target = variable;
9392 initializers.EmitStatement (ec);
9395 public override bool HasInitializer {
9397 return !initializers.IsEmpty;
9402 public class AnonymousTypeDeclaration : Expression
9404 ArrayList parameters;
9405 readonly TypeContainer parent;
9406 static readonly ArrayList EmptyParameters = new ArrayList (0);
9408 public AnonymousTypeDeclaration (ArrayList parameters, TypeContainer parent, Location loc)
9410 this.parameters = parameters;
9411 this.parent = parent;
9415 protected override void CloneTo (CloneContext clonectx, Expression target)
9417 if (parameters == null)
9420 AnonymousTypeDeclaration t = (AnonymousTypeDeclaration) target;
9421 t.parameters = new ArrayList (parameters.Count);
9422 foreach (AnonymousTypeParameter atp in parameters)
9423 t.parameters.Add (atp.Clone (clonectx));
9426 AnonymousTypeClass CreateAnonymousType (ArrayList parameters)
9428 AnonymousTypeClass type = RootContext.ToplevelTypes.GetAnonymousType (parameters);
9432 type = AnonymousTypeClass.Create (parent, parameters, loc);
9437 type.DefineMembers ();
9441 RootContext.ToplevelTypes.AddAnonymousType (type);
9445 public override Expression DoResolve (EmitContext ec)
9447 AnonymousTypeClass anonymous_type;
9449 if (parameters == null) {
9450 anonymous_type = CreateAnonymousType (EmptyParameters);
9451 return new New (new TypeExpression (anonymous_type.TypeBuilder, loc),
9452 null, loc).Resolve (ec);
9456 ArrayList arguments = new ArrayList (parameters.Count);
9457 TypeExpression [] t_args = new TypeExpression [parameters.Count];
9458 for (int i = 0; i < parameters.Count; ++i) {
9459 Expression e = ((AnonymousTypeParameter) parameters [i]).Resolve (ec);
9465 arguments.Add (new Argument (e));
9466 t_args [i] = new TypeExpression (e.Type, e.Location);
9472 anonymous_type = CreateAnonymousType (parameters);
9473 if (anonymous_type == null)
9476 ConstructedType te = new ConstructedType (anonymous_type.TypeBuilder,
9477 new TypeArguments (loc, t_args), loc);
9479 return new New (te, arguments, loc).Resolve (ec);
9482 public override void Emit (EmitContext ec)
9484 throw new InternalErrorException ("Should not be reached");
9488 public class AnonymousTypeParameter : Expression
9490 public readonly string Name;
9491 Expression initializer;
9493 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
9497 this.initializer = initializer;
9500 public AnonymousTypeParameter (Parameter parameter)
9502 this.Name = parameter.Name;
9503 this.loc = parameter.Location;
9504 this.initializer = new SimpleName (Name, loc);
9507 protected override void CloneTo (CloneContext clonectx, Expression target)
9509 AnonymousTypeParameter t = (AnonymousTypeParameter) target;
9510 t.initializer = initializer.Clone (clonectx);
9513 public override Expression CreateExpressionTree (EmitContext ec)
9515 throw new NotSupportedException ("ET");
9518 public override bool Equals (object o)
9520 AnonymousTypeParameter other = o as AnonymousTypeParameter;
9521 return other != null && Name == other.Name;
9524 public override int GetHashCode ()
9526 return Name.GetHashCode ();
9529 public override Expression DoResolve (EmitContext ec)
9531 Expression e = initializer.Resolve (ec);
9536 if (type == TypeManager.void_type || type == TypeManager.null_type ||
9537 type == TypeManager.anonymous_method_type || type.IsPointer) {
9538 Error_InvalidInitializer (e);
9545 protected virtual void Error_InvalidInitializer (Expression initializer)
9547 Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
9548 Name, initializer.GetSignatureForError ());
9551 public override void Emit (EmitContext ec)
9553 throw new InternalErrorException ("Should not be reached");