2 // expression.cs: Expression representation for the IL tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
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 Arguments arguments
;
28 protected readonly MethodGroupExpr mg
;
29 readonly ExpressionTreeExpression expr_tree
;
31 public UserOperatorCall (MethodGroupExpr mg
, Arguments 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 Arguments args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
48 new NullLiteral (loc
),
49 mg
.CreateExpressionTree (ec
));
51 return CreateExpressionFactoryCall ("Call", args
);
54 protected override void CloneTo (CloneContext context
, Expression target
)
59 public override Expression
DoResolve (EmitContext ec
)
62 // We are born fully resolved
67 public override void Emit (EmitContext ec
)
69 mg
.EmitCall (ec
, arguments
);
72 public MethodGroupExpr Method
{
76 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
78 arguments
.MutateHoistedGenericType (storey
);
79 mg
.MutateHoistedGenericType (storey
);
83 public class ParenthesizedExpression
: Expression
85 public Expression Expr
;
87 public ParenthesizedExpression (Expression expr
)
93 public override Expression
CreateExpressionTree (EmitContext ec
)
95 throw new NotSupportedException ("ET");
98 public override Expression
DoResolve (EmitContext ec
)
100 Expr
= Expr
.Resolve (ec
);
104 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
106 return Expr
.DoResolveLValue (ec
, right_side
);
109 public override void Emit (EmitContext ec
)
111 throw new Exception ("Should not happen");
114 protected override void CloneTo (CloneContext clonectx
, Expression t
)
116 ParenthesizedExpression target
= (ParenthesizedExpression
) t
;
118 target
.Expr
= Expr
.Clone (clonectx
);
123 // Unary implements unary expressions.
125 public class Unary
: Expression
127 public enum Operator
: byte {
128 UnaryPlus
, UnaryNegation
, LogicalNot
, OnesComplement
,
132 static Type
[] [] predefined_operators
;
134 public readonly Operator Oper
;
135 public Expression Expr
;
136 Expression enum_conversion
;
138 public Unary (Operator op
, Expression expr
)
146 // This routine will attempt to simplify the unary expression when the
147 // argument is a constant.
149 Constant
TryReduceConstant (EmitContext ec
, Constant e
)
151 if (e
is EmptyConstantCast
)
152 return TryReduceConstant (ec
, ((EmptyConstantCast
) e
).child
);
154 if (e
is SideEffectConstant
) {
155 Constant r
= TryReduceConstant (ec
, ((SideEffectConstant
) e
).value);
156 return r
== null ? null : new SideEffectConstant (r
, e
, r
.Location
);
159 Type expr_type
= e
.Type
;
162 case Operator
.UnaryPlus
:
163 // Unary numeric promotions
164 if (expr_type
== TypeManager
.byte_type
)
165 return new IntConstant (((ByteConstant
)e
).Value
, e
.Location
);
166 if (expr_type
== TypeManager
.sbyte_type
)
167 return new IntConstant (((SByteConstant
)e
).Value
, e
.Location
);
168 if (expr_type
== TypeManager
.short_type
)
169 return new IntConstant (((ShortConstant
)e
).Value
, e
.Location
);
170 if (expr_type
== TypeManager
.ushort_type
)
171 return new IntConstant (((UShortConstant
)e
).Value
, e
.Location
);
172 if (expr_type
== TypeManager
.char_type
)
173 return new IntConstant (((CharConstant
)e
).Value
, e
.Location
);
175 // Predefined operators
176 if (expr_type
== TypeManager
.int32_type
|| expr_type
== TypeManager
.uint32_type
||
177 expr_type
== TypeManager
.int64_type
|| expr_type
== TypeManager
.uint64_type
||
178 expr_type
== TypeManager
.float_type
|| expr_type
== TypeManager
.double_type
||
179 expr_type
== TypeManager
.decimal_type
) {
185 case Operator
.UnaryNegation
:
186 // Unary numeric promotions
187 if (expr_type
== TypeManager
.byte_type
)
188 return new IntConstant (-((ByteConstant
)e
).Value
, e
.Location
);
189 if (expr_type
== TypeManager
.sbyte_type
)
190 return new IntConstant (-((SByteConstant
)e
).Value
, e
.Location
);
191 if (expr_type
== TypeManager
.short_type
)
192 return new IntConstant (-((ShortConstant
)e
).Value
, e
.Location
);
193 if (expr_type
== TypeManager
.ushort_type
)
194 return new IntConstant (-((UShortConstant
)e
).Value
, e
.Location
);
195 if (expr_type
== TypeManager
.char_type
)
196 return new IntConstant (-((CharConstant
)e
).Value
, e
.Location
);
198 // Predefined operators
199 if (expr_type
== TypeManager
.int32_type
) {
200 int value = ((IntConstant
)e
).Value
;
201 if (value == int.MinValue
) {
202 if (ec
.ConstantCheckState
) {
203 ConstantFold
.Error_CompileTimeOverflow (loc
);
208 return new IntConstant (-value, e
.Location
);
210 if (expr_type
== TypeManager
.int64_type
) {
211 long value = ((LongConstant
)e
).Value
;
212 if (value == long.MinValue
) {
213 if (ec
.ConstantCheckState
) {
214 ConstantFold
.Error_CompileTimeOverflow (loc
);
219 return new LongConstant (-value, e
.Location
);
222 if (expr_type
== TypeManager
.uint32_type
) {
223 UIntLiteral uil
= e
as UIntLiteral
;
225 if (uil
.Value
== 2147483648)
226 return new IntLiteral (int.MinValue
, e
.Location
);
227 return new LongLiteral (-uil
.Value
, e
.Location
);
229 return new LongConstant (-((UIntConstant
)e
).Value
, e
.Location
);
232 if (expr_type
== TypeManager
.uint64_type
) {
233 ULongLiteral ull
= e
as ULongLiteral
;
234 if (ull
!= null && ull
.Value
== 9223372036854775808)
235 return new LongLiteral (long.MinValue
, e
.Location
);
239 if (expr_type
== TypeManager
.float_type
) {
240 FloatLiteral fl
= e
as FloatLiteral
;
241 // For better error reporting
243 return new FloatLiteral (-fl
.Value
, e
.Location
);
245 return new FloatConstant (-((FloatConstant
)e
).Value
, e
.Location
);
247 if (expr_type
== TypeManager
.double_type
) {
248 DoubleLiteral dl
= e
as DoubleLiteral
;
249 // For better error reporting
251 return new DoubleLiteral (-dl
.Value
, e
.Location
);
253 return new DoubleConstant (-((DoubleConstant
)e
).Value
, e
.Location
);
255 if (expr_type
== TypeManager
.decimal_type
)
256 return new DecimalConstant (-((DecimalConstant
)e
).Value
, e
.Location
);
260 case Operator
.LogicalNot
:
261 if (expr_type
!= TypeManager
.bool_type
)
264 bool b
= (bool)e
.GetValue ();
265 return new BoolConstant (!b
, e
.Location
);
267 case Operator
.OnesComplement
:
268 // Unary numeric promotions
269 if (expr_type
== TypeManager
.byte_type
)
270 return new IntConstant (~
((ByteConstant
)e
).Value
, e
.Location
);
271 if (expr_type
== TypeManager
.sbyte_type
)
272 return new IntConstant (~
((SByteConstant
)e
).Value
, e
.Location
);
273 if (expr_type
== TypeManager
.short_type
)
274 return new IntConstant (~
((ShortConstant
)e
).Value
, e
.Location
);
275 if (expr_type
== TypeManager
.ushort_type
)
276 return new IntConstant (~
((UShortConstant
)e
).Value
, e
.Location
);
277 if (expr_type
== TypeManager
.char_type
)
278 return new IntConstant (~
((CharConstant
)e
).Value
, e
.Location
);
280 // Predefined operators
281 if (expr_type
== TypeManager
.int32_type
)
282 return new IntConstant (~
((IntConstant
)e
).Value
, e
.Location
);
283 if (expr_type
== TypeManager
.uint32_type
)
284 return new UIntConstant (~
((UIntConstant
)e
).Value
, e
.Location
);
285 if (expr_type
== TypeManager
.int64_type
)
286 return new LongConstant (~
((LongConstant
)e
).Value
, e
.Location
);
287 if (expr_type
== TypeManager
.uint64_type
){
288 return new ULongConstant (~
((ULongConstant
)e
).Value
, e
.Location
);
290 if (e
is EnumConstant
) {
291 e
= TryReduceConstant (ec
, ((EnumConstant
)e
).Child
);
293 e
= new EnumConstant (e
, expr_type
);
298 throw new Exception ("Can not constant fold: " + Oper
.ToString());
301 protected Expression
ResolveOperator (EmitContext ec
, Expression expr
)
303 eclass
= ExprClass
.Value
;
305 if (predefined_operators
== null)
306 CreatePredefinedOperatorsTable ();
308 Type expr_type
= expr
.Type
;
309 Expression best_expr
;
312 // Primitive types first
314 if (TypeManager
.IsPrimitiveType (expr_type
)) {
315 best_expr
= ResolvePrimitivePredefinedType (expr
);
316 if (best_expr
== null)
319 type
= best_expr
.Type
;
325 // E operator ~(E x);
327 if (Oper
== Operator
.OnesComplement
&& TypeManager
.IsEnumType (expr_type
))
328 return ResolveEnumOperator (ec
, expr
);
330 return ResolveUserType (ec
, expr
);
333 protected virtual Expression
ResolveEnumOperator (EmitContext ec
, Expression expr
)
335 Type underlying_type
= TypeManager
.GetEnumUnderlyingType (expr
.Type
);
336 Expression best_expr
= ResolvePrimitivePredefinedType (EmptyCast
.Create (expr
, underlying_type
));
337 if (best_expr
== null)
341 enum_conversion
= Convert
.ExplicitNumericConversion (new EmptyExpression (best_expr
.Type
), underlying_type
);
343 return EmptyCast
.Create (this, type
);
346 public override Expression
CreateExpressionTree (EmitContext ec
)
348 return CreateExpressionTree (ec
, null);
351 Expression
CreateExpressionTree (EmitContext ec
, MethodGroupExpr user_op
)
355 case Operator
.AddressOf
:
356 Error_PointerInsideExpressionTree ();
358 case Operator
.UnaryNegation
:
359 if (ec
.CheckState
&& user_op
== null && !IsFloat (type
))
360 method_name
= "NegateChecked";
362 method_name
= "Negate";
364 case Operator
.OnesComplement
:
365 case Operator
.LogicalNot
:
368 case Operator
.UnaryPlus
:
369 method_name
= "UnaryPlus";
372 throw new InternalErrorException ("Unknown unary operator " + Oper
.ToString ());
375 Arguments args
= new Arguments (2);
376 args
.Add (new Argument (Expr
.CreateExpressionTree (ec
)));
378 args
.Add (new Argument (user_op
.CreateExpressionTree (ec
)));
379 return CreateExpressionFactoryCall (method_name
, args
);
382 static void CreatePredefinedOperatorsTable ()
384 predefined_operators
= new Type
[(int) Operator
.TOP
] [];
387 // 7.6.1 Unary plus operator
389 predefined_operators
[(int) Operator
.UnaryPlus
] = new Type
[] {
390 TypeManager
.int32_type
, TypeManager
.uint32_type
,
391 TypeManager
.int64_type
, TypeManager
.uint64_type
,
392 TypeManager
.float_type
, TypeManager
.double_type
,
393 TypeManager
.decimal_type
397 // 7.6.2 Unary minus operator
399 predefined_operators
[(int) Operator
.UnaryNegation
] = new Type
[] {
400 TypeManager
.int32_type
,
401 TypeManager
.int64_type
,
402 TypeManager
.float_type
, TypeManager
.double_type
,
403 TypeManager
.decimal_type
407 // 7.6.3 Logical negation operator
409 predefined_operators
[(int) Operator
.LogicalNot
] = new Type
[] {
410 TypeManager
.bool_type
414 // 7.6.4 Bitwise complement operator
416 predefined_operators
[(int) Operator
.OnesComplement
] = new Type
[] {
417 TypeManager
.int32_type
, TypeManager
.uint32_type
,
418 TypeManager
.int64_type
, TypeManager
.uint64_type
423 // Unary numeric promotions
425 static Expression
DoNumericPromotion (Operator op
, Expression expr
)
427 Type expr_type
= expr
.Type
;
428 if ((op
== Operator
.UnaryPlus
|| op
== Operator
.UnaryNegation
|| op
== Operator
.OnesComplement
) &&
429 expr_type
== TypeManager
.byte_type
|| expr_type
== TypeManager
.sbyte_type
||
430 expr_type
== TypeManager
.short_type
|| expr_type
== TypeManager
.ushort_type
||
431 expr_type
== TypeManager
.char_type
)
432 return Convert
.ImplicitNumericConversion (expr
, TypeManager
.int32_type
);
434 if (op
== Operator
.UnaryNegation
&& expr_type
== TypeManager
.uint32_type
)
435 return Convert
.ImplicitNumericConversion (expr
, TypeManager
.int64_type
);
440 public override Expression
DoResolve (EmitContext ec
)
442 if (Oper
== Operator
.AddressOf
) {
443 return ResolveAddressOf (ec
);
446 Expr
= Expr
.Resolve (ec
);
450 if (TypeManager
.IsDynamicType (Expr
.Type
)) {
451 Arguments args
= new Arguments (1);
452 args
.Add (new Argument (Expr
));
453 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args
, loc
).DoResolve (ec
);
456 if (TypeManager
.IsNullableType (Expr
.Type
))
457 return new Nullable
.LiftedUnaryOperator (Oper
, Expr
).Resolve (ec
);
460 // Attempt to use a constant folding operation.
462 Constant cexpr
= Expr
as Constant
;
464 cexpr
= TryReduceConstant (ec
, cexpr
);
469 Expression expr
= ResolveOperator (ec
, Expr
);
471 Error_OperatorCannotBeApplied (loc
, OperName (Oper
), Expr
.Type
);
474 // Reduce unary operator on predefined types
476 if (expr
== this && Oper
== Operator
.UnaryPlus
)
482 public override Expression
DoResolveLValue (EmitContext ec
, Expression right
)
487 public override void Emit (EmitContext ec
)
489 EmitOperator (ec
, type
);
492 protected void EmitOperator (EmitContext ec
, Type type
)
494 ILGenerator ig
= ec
.ig
;
497 case Operator
.UnaryPlus
:
501 case Operator
.UnaryNegation
:
502 if (ec
.CheckState
&& !IsFloat (type
)) {
503 ig
.Emit (OpCodes
.Ldc_I4_0
);
504 if (type
== TypeManager
.int64_type
)
505 ig
.Emit (OpCodes
.Conv_U8
);
507 ig
.Emit (OpCodes
.Sub_Ovf
);
510 ig
.Emit (OpCodes
.Neg
);
515 case Operator
.LogicalNot
:
517 ig
.Emit (OpCodes
.Ldc_I4_0
);
518 ig
.Emit (OpCodes
.Ceq
);
521 case Operator
.OnesComplement
:
523 ig
.Emit (OpCodes
.Not
);
526 case Operator
.AddressOf
:
527 ((IMemoryLocation
)Expr
).AddressOf (ec
, AddressOp
.LoadStore
);
531 throw new Exception ("This should not happen: Operator = "
536 // Same trick as in Binary expression
538 if (enum_conversion
!= null)
539 enum_conversion
.Emit (ec
);
542 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
544 if (Oper
== Operator
.LogicalNot
)
545 Expr
.EmitBranchable (ec
, target
, !on_true
);
547 base.EmitBranchable (ec
, target
, on_true
);
550 public override void EmitSideEffect (EmitContext ec
)
552 Expr
.EmitSideEffect (ec
);
555 public static void Error_OperatorCannotBeApplied (Location loc
, string oper
, Type t
)
557 Report
.Error (23, loc
, "The `{0}' operator cannot be applied to operand of type `{1}'",
558 oper
, TypeManager
.CSharpName (t
));
562 // Converts operator to System.Linq.Expressions.ExpressionType enum name
564 string GetOperatorExpressionTypeName ()
567 case Operator
.UnaryPlus
:
570 throw new NotImplementedException ("Unknown express type operator " + Oper
.ToString ());
574 static bool IsFloat (Type t
)
576 return t
== TypeManager
.float_type
|| t
== TypeManager
.double_type
;
580 // Returns a stringified representation of the Operator
582 public static string OperName (Operator oper
)
585 case Operator
.UnaryPlus
:
587 case Operator
.UnaryNegation
:
589 case Operator
.LogicalNot
:
591 case Operator
.OnesComplement
:
593 case Operator
.AddressOf
:
597 throw new NotImplementedException (oper
.ToString ());
600 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
602 type
= storey
.MutateType (type
);
603 Expr
.MutateHoistedGenericType (storey
);
606 Expression
ResolveAddressOf (EmitContext ec
)
611 Expr
= Expr
.DoResolveLValue (ec
, EmptyExpression
.UnaryAddress
);
612 if (Expr
== null || Expr
.eclass
!= ExprClass
.Variable
) {
613 Error (211, "Cannot take the address of the given expression");
617 if (!TypeManager
.VerifyUnManaged (Expr
.Type
, loc
)) {
621 IVariableReference vr
= Expr
as IVariableReference
;
624 VariableInfo vi
= vr
.VariableInfo
;
626 if (vi
.LocalInfo
!= null)
627 vi
.LocalInfo
.Used
= true;
630 // A variable is considered definitely assigned if you take its address.
635 is_fixed
= vr
.IsFixed
;
636 vr
.SetHasAddressTaken ();
639 AnonymousMethodExpression
.Error_AddressOfCapturedVar (vr
, loc
);
642 IFixedExpression fe
= Expr
as IFixedExpression
;
643 is_fixed
= fe
!= null && fe
.IsFixed
;
646 if (!is_fixed
&& !ec
.InFixedInitializer
) {
647 Error (212, "You can only take the address of unfixed expression inside of a fixed statement initializer");
650 type
= TypeManager
.GetPointerType (Expr
.Type
);
651 eclass
= ExprClass
.Value
;
655 Expression
ResolvePrimitivePredefinedType (Expression expr
)
657 expr
= DoNumericPromotion (Oper
, expr
);
658 Type expr_type
= expr
.Type
;
659 Type
[] predefined
= predefined_operators
[(int) Oper
];
660 foreach (Type t
in predefined
) {
668 // Perform user-operator overload resolution
670 protected virtual Expression
ResolveUserOperator (EmitContext ec
, Expression expr
)
672 CSharp
.Operator
.OpType op_type
;
674 case Operator
.LogicalNot
:
675 op_type
= CSharp
.Operator
.OpType
.LogicalNot
; break;
676 case Operator
.OnesComplement
:
677 op_type
= CSharp
.Operator
.OpType
.OnesComplement
; break;
678 case Operator
.UnaryNegation
:
679 op_type
= CSharp
.Operator
.OpType
.UnaryNegation
; break;
680 case Operator
.UnaryPlus
:
681 op_type
= CSharp
.Operator
.OpType
.UnaryPlus
; break;
683 throw new InternalErrorException (Oper
.ToString ());
686 string op_name
= CSharp
.Operator
.GetMetadataName (op_type
);
687 MethodGroupExpr user_op
= MemberLookup (ec
.ContainerType
, expr
.Type
, op_name
, MemberTypes
.Method
, AllBindingFlags
, expr
.Location
) as MethodGroupExpr
;
691 Arguments args
= new Arguments (1);
692 args
.Add (new Argument (expr
));
693 user_op
= user_op
.OverloadResolve (ec
, ref args
, false, expr
.Location
);
698 Expr
= args
[0].Expr
;
699 return new UserOperatorCall (user_op
, args
, CreateExpressionTree
, expr
.Location
);
703 // Unary user type overload resolution
705 Expression
ResolveUserType (EmitContext ec
, Expression expr
)
707 Expression best_expr
= ResolveUserOperator (ec
, expr
);
708 if (best_expr
!= null)
711 Type
[] predefined
= predefined_operators
[(int) Oper
];
712 foreach (Type t
in predefined
) {
713 Expression oper_expr
= Convert
.UserDefinedConversion (ec
, expr
, t
, expr
.Location
, false);
714 if (oper_expr
== null)
718 // decimal type is predefined but has user-operators
720 if (oper_expr
.Type
== TypeManager
.decimal_type
)
721 oper_expr
= ResolveUserType (ec
, oper_expr
);
723 oper_expr
= ResolvePrimitivePredefinedType (oper_expr
);
725 if (oper_expr
== null)
728 if (best_expr
== null) {
729 best_expr
= oper_expr
;
733 int result
= MethodGroupExpr
.BetterTypeConversion (ec
, best_expr
.Type
, t
);
735 Report
.Error (35, loc
, "Operator `{0}' is ambiguous on an operand of type `{1}'",
736 OperName (Oper
), TypeManager
.CSharpName (expr
.Type
));
741 best_expr
= oper_expr
;
744 if (best_expr
== null)
748 // HACK: Decimal user-operator is included in standard operators
750 if (best_expr
.Type
== TypeManager
.decimal_type
)
754 type
= best_expr
.Type
;
758 protected override void CloneTo (CloneContext clonectx
, Expression t
)
760 Unary target
= (Unary
) t
;
762 target
.Expr
= Expr
.Clone (clonectx
);
767 // Unary operators are turned into Indirection expressions
768 // after semantic analysis (this is so we can take the address
769 // of an indirection).
771 public class Indirection
: Expression
, IMemoryLocation
, IAssignMethod
, IFixedExpression
{
773 LocalTemporary temporary
;
776 public Indirection (Expression expr
, Location l
)
782 public override Expression
CreateExpressionTree (EmitContext ec
)
784 Error_PointerInsideExpressionTree ();
788 protected override void CloneTo (CloneContext clonectx
, Expression t
)
790 Indirection target
= (Indirection
) t
;
791 target
.expr
= expr
.Clone (clonectx
);
794 public override void Emit (EmitContext ec
)
799 LoadFromPtr (ec
.ig
, Type
);
802 public void Emit (EmitContext ec
, bool leave_copy
)
806 ec
.ig
.Emit (OpCodes
.Dup
);
807 temporary
= new LocalTemporary (expr
.Type
);
808 temporary
.Store (ec
);
812 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
814 prepared
= prepare_for_load
;
818 if (prepare_for_load
)
819 ec
.ig
.Emit (OpCodes
.Dup
);
823 ec
.ig
.Emit (OpCodes
.Dup
);
824 temporary
= new LocalTemporary (expr
.Type
);
825 temporary
.Store (ec
);
828 StoreFromPtr (ec
.ig
, type
);
830 if (temporary
!= null) {
832 temporary
.Release (ec
);
836 public void AddressOf (EmitContext ec
, AddressOp Mode
)
841 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
843 return DoResolve (ec
);
846 public override Expression
DoResolve (EmitContext ec
)
848 expr
= expr
.Resolve (ec
);
855 if (!expr
.Type
.IsPointer
) {
856 Error (193, "The * or -> operator must be applied to a pointer");
860 if (expr
.Type
== TypeManager
.void_ptr_type
) {
861 Error (242, "The operation in question is undefined on void pointers");
865 type
= TypeManager
.GetElementType (expr
.Type
);
866 eclass
= ExprClass
.Variable
;
870 public bool IsFixed
{
874 public override string ToString ()
876 return "*(" + expr
+ ")";
881 /// Unary Mutator expressions (pre and post ++ and --)
885 /// UnaryMutator implements ++ and -- expressions. It derives from
886 /// ExpressionStatement becuase the pre/post increment/decrement
887 /// operators can be used in a statement context.
889 /// FIXME: Idea, we could split this up in two classes, one simpler
890 /// for the common case, and one with the extra fields for more complex
891 /// classes (indexers require temporary access; overloaded require method)
894 public class UnaryMutator
: ExpressionStatement
{
896 public enum Mode
: byte {
903 PreDecrement
= IsDecrement
,
904 PostIncrement
= IsPost
,
905 PostDecrement
= IsPost
| IsDecrement
909 bool is_expr
= false;
910 bool recurse
= false;
915 // This is expensive for the simplest case.
917 UserOperatorCall method
;
919 public UnaryMutator (Mode m
, Expression e
)
926 static string OperName (Mode mode
)
928 return (mode
== Mode
.PreIncrement
|| mode
== Mode
.PostIncrement
) ?
933 /// Returns whether an object of type `t' can be incremented
934 /// or decremented with add/sub (ie, basically whether we can
935 /// use pre-post incr-decr operations on it, but it is not a
936 /// System.Decimal, which we require operator overloading to catch)
938 static bool IsIncrementableNumber (Type t
)
940 return (t
== TypeManager
.sbyte_type
) ||
941 (t
== TypeManager
.byte_type
) ||
942 (t
== TypeManager
.short_type
) ||
943 (t
== TypeManager
.ushort_type
) ||
944 (t
== TypeManager
.int32_type
) ||
945 (t
== TypeManager
.uint32_type
) ||
946 (t
== TypeManager
.int64_type
) ||
947 (t
== TypeManager
.uint64_type
) ||
948 (t
== TypeManager
.char_type
) ||
949 (TypeManager
.IsSubclassOf (t
, TypeManager
.enum_type
)) ||
950 (t
== TypeManager
.float_type
) ||
951 (t
== TypeManager
.double_type
) ||
952 (t
.IsPointer
&& t
!= TypeManager
.void_ptr_type
);
955 Expression
ResolveOperator (EmitContext ec
)
960 // The operand of the prefix/postfix increment decrement operators
961 // should be an expression that is classified as a variable,
962 // a property access or an indexer access
964 if (expr
.eclass
== ExprClass
.Variable
|| expr
.eclass
== ExprClass
.IndexerAccess
|| expr
.eclass
== ExprClass
.PropertyAccess
) {
965 expr
= expr
.ResolveLValue (ec
, expr
);
967 Report
.Error (1059, loc
, "The operand of an increment or decrement operator must be a variable, property or indexer");
971 // Step 1: Perform Operator Overload location
976 if (mode
== Mode
.PreIncrement
|| mode
== Mode
.PostIncrement
)
977 op_name
= Operator
.GetMetadataName (Operator
.OpType
.Increment
);
979 op_name
= Operator
.GetMetadataName (Operator
.OpType
.Decrement
);
981 mg
= MemberLookup (ec
.ContainerType
, type
, op_name
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
984 Arguments args
= new Arguments (1);
985 args
.Add (new Argument (expr
));
986 mg
= mg
.OverloadResolve (ec
, ref args
, false, loc
);
990 method
= new UserOperatorCall (mg
, args
, null, loc
);
991 Convert
.ImplicitConversionRequired (ec
, method
, type
, loc
);
995 if (!IsIncrementableNumber (type
)) {
996 Error (187, "No such operator '" + OperName (mode
) + "' defined for type '" +
997 TypeManager
.CSharpName (type
) + "'");
1004 public override Expression
CreateExpressionTree (EmitContext ec
)
1006 return new SimpleAssign (this, this).CreateExpressionTree (ec
);
1009 public override Expression
DoResolve (EmitContext ec
)
1011 expr
= expr
.Resolve (ec
);
1016 eclass
= ExprClass
.Value
;
1018 if (TypeManager
.IsNullableType (expr
.Type
))
1019 return new Nullable
.LiftedUnaryMutator (mode
, expr
, loc
).Resolve (ec
);
1021 return ResolveOperator (ec
);
1025 // Loads the proper "1" into the stack based on the type, then it emits the
1026 // opcode for the operation requested
1028 void LoadOneAndEmitOp (EmitContext ec
, Type t
)
1031 // Measure if getting the typecode and using that is more/less efficient
1032 // that comparing types. t.GetTypeCode() is an internal call.
1034 ILGenerator ig
= ec
.ig
;
1036 if (t
== TypeManager
.uint64_type
|| t
== TypeManager
.int64_type
)
1037 LongConstant
.EmitLong (ig
, 1);
1038 else if (t
== TypeManager
.double_type
)
1039 ig
.Emit (OpCodes
.Ldc_R8
, 1.0);
1040 else if (t
== TypeManager
.float_type
)
1041 ig
.Emit (OpCodes
.Ldc_R4
, 1.0F
);
1042 else if (t
.IsPointer
){
1043 Type et
= TypeManager
.GetElementType (t
);
1044 int n
= GetTypeSize (et
);
1047 ig
.Emit (OpCodes
.Sizeof
, et
);
1049 IntConstant
.EmitInt (ig
, n
);
1050 ig
.Emit (OpCodes
.Conv_I
);
1053 ig
.Emit (OpCodes
.Ldc_I4_1
);
1056 // Now emit the operation
1059 Binary
.Operator op
= (mode
& Mode
.IsDecrement
) != 0 ? Binary
.Operator
.Subtraction
: Binary
.Operator
.Addition
;
1060 Binary
.EmitOperatorOpcode (ec
, op
, t
);
1062 if (t
== TypeManager
.sbyte_type
){
1064 ig
.Emit (OpCodes
.Conv_Ovf_I1
);
1066 ig
.Emit (OpCodes
.Conv_I1
);
1067 } else if (t
== TypeManager
.byte_type
){
1069 ig
.Emit (OpCodes
.Conv_Ovf_U1
);
1071 ig
.Emit (OpCodes
.Conv_U1
);
1072 } else if (t
== TypeManager
.short_type
){
1074 ig
.Emit (OpCodes
.Conv_Ovf_I2
);
1076 ig
.Emit (OpCodes
.Conv_I2
);
1077 } else if (t
== TypeManager
.ushort_type
|| t
== TypeManager
.char_type
){
1079 ig
.Emit (OpCodes
.Conv_Ovf_U2
);
1081 ig
.Emit (OpCodes
.Conv_U2
);
1086 void EmitCode (EmitContext ec
, bool is_expr
)
1089 this.is_expr
= is_expr
;
1090 ((IAssignMethod
) expr
).EmitAssign (ec
, this, is_expr
&& (mode
== Mode
.PreIncrement
|| mode
== Mode
.PreDecrement
), true);
1093 public override void Emit (EmitContext ec
)
1096 // We use recurse to allow ourselfs to be the source
1097 // of an assignment. This little hack prevents us from
1098 // having to allocate another expression
1101 ((IAssignMethod
) expr
).Emit (ec
, is_expr
&& (mode
== Mode
.PostIncrement
|| mode
== Mode
.PostDecrement
));
1103 LoadOneAndEmitOp (ec
, expr
.Type
);
1105 ec
.ig
.Emit (OpCodes
.Call
, (MethodInfo
)method
.Method
);
1110 EmitCode (ec
, true);
1113 public override void EmitStatement (EmitContext ec
)
1115 EmitCode (ec
, false);
1118 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1120 UnaryMutator target
= (UnaryMutator
) t
;
1122 target
.expr
= expr
.Clone (clonectx
);
1127 /// Base class for the `Is' and `As' classes.
1131 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1134 public abstract class Probe
: Expression
{
1135 public Expression ProbeType
;
1136 protected Expression expr
;
1137 protected TypeExpr probe_type_expr
;
1139 public Probe (Expression expr
, Expression probe_type
, Location l
)
1141 ProbeType
= probe_type
;
1146 public Expression Expr
{
1152 public override Expression
DoResolve (EmitContext ec
)
1154 probe_type_expr
= ProbeType
.ResolveAsTypeTerminal (ec
, false);
1155 if (probe_type_expr
== null)
1158 expr
= expr
.Resolve (ec
);
1162 if ((probe_type_expr
.Type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
1163 Report
.Error (-244, loc
, "The `{0}' operator cannot be applied to an operand of a static type",
1167 if (expr
.Type
.IsPointer
|| probe_type_expr
.Type
.IsPointer
) {
1168 Report
.Error (244, loc
, "The `{0}' operator cannot be applied to an operand of pointer type",
1173 if (expr
.Type
== InternalType
.AnonymousMethod
) {
1174 Report
.Error (837, loc
, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1182 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1184 expr
.MutateHoistedGenericType (storey
);
1185 probe_type_expr
.MutateHoistedGenericType (storey
);
1188 protected abstract string OperatorName { get; }
1190 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1192 Probe target
= (Probe
) t
;
1194 target
.expr
= expr
.Clone (clonectx
);
1195 target
.ProbeType
= ProbeType
.Clone (clonectx
);
1201 /// Implementation of the `is' operator.
1203 public class Is
: Probe
{
1204 Nullable
.Unwrap expr_unwrap
;
1206 public Is (Expression expr
, Expression probe_type
, Location l
)
1207 : base (expr
, probe_type
, l
)
1211 public override Expression
CreateExpressionTree (EmitContext ec
)
1213 Arguments args
= Arguments
.CreateForExpressionTree (ec
, null,
1214 expr
.CreateExpressionTree (ec
),
1215 new TypeOf (probe_type_expr
, loc
));
1217 return CreateExpressionFactoryCall ("TypeIs", args
);
1220 public override void Emit (EmitContext ec
)
1222 ILGenerator ig
= ec
.ig
;
1223 if (expr_unwrap
!= null) {
1224 expr_unwrap
.EmitCheck (ec
);
1229 ig
.Emit (OpCodes
.Isinst
, probe_type_expr
.Type
);
1230 ig
.Emit (OpCodes
.Ldnull
);
1231 ig
.Emit (OpCodes
.Cgt_Un
);
1234 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
1236 ILGenerator ig
= ec
.ig
;
1237 if (expr_unwrap
!= null) {
1238 expr_unwrap
.EmitCheck (ec
);
1241 ig
.Emit (OpCodes
.Isinst
, probe_type_expr
.Type
);
1243 ig
.Emit (on_true
? OpCodes
.Brtrue
: OpCodes
.Brfalse
, target
);
1246 Expression
CreateConstantResult (bool result
)
1249 Report
.Warning (183, 1, loc
, "The given expression is always of the provided (`{0}') type",
1250 TypeManager
.CSharpName (probe_type_expr
.Type
));
1252 Report
.Warning (184, 1, loc
, "The given expression is never of the provided (`{0}') type",
1253 TypeManager
.CSharpName (probe_type_expr
.Type
));
1255 return ReducedExpression
.Create (new BoolConstant (result
, loc
), this);
1258 public override Expression
DoResolve (EmitContext ec
)
1260 if (base.DoResolve (ec
) == null)
1264 bool d_is_nullable
= false;
1267 // If E is a method group or the null literal, or if the type of E is a reference
1268 // type or a nullable type and the value of E is null, the result is false
1270 if (expr
.IsNull
|| expr
.eclass
== ExprClass
.MethodGroup
)
1271 return CreateConstantResult (false);
1273 if (TypeManager
.IsNullableType (d
) && !TypeManager
.ContainsGenericParameters (d
)) {
1274 d
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (d
) [0]);
1275 d_is_nullable
= true;
1278 type
= TypeManager
.bool_type
;
1279 eclass
= ExprClass
.Value
;
1280 Type t
= probe_type_expr
.Type
;
1281 bool t_is_nullable
= false;
1282 if (TypeManager
.IsNullableType (t
) && !TypeManager
.ContainsGenericParameters (t
)) {
1283 t
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (t
) [0]);
1284 t_is_nullable
= true;
1287 if (TypeManager
.IsStruct (t
)) {
1290 // D and T are the same value types but D can be null
1292 if (d_is_nullable
&& !t_is_nullable
) {
1293 expr_unwrap
= Nullable
.Unwrap
.Create (expr
, false);
1298 // The result is true if D and T are the same value types
1300 return CreateConstantResult (true);
1303 if (TypeManager
.IsGenericParameter (d
))
1304 return ResolveGenericParameter (t
, d
);
1307 // An unboxing conversion exists
1309 if (Convert
.ExplicitReferenceConversionExists (d
, t
))
1312 if (TypeManager
.IsGenericParameter (t
))
1313 return ResolveGenericParameter (d
, t
);
1315 if (TypeManager
.IsStruct (d
)) {
1317 if (Convert
.ImplicitBoxingConversionExists (expr
, t
, out temp
))
1318 return CreateConstantResult (true);
1320 if (TypeManager
.IsGenericParameter (d
))
1321 return ResolveGenericParameter (t
, d
);
1323 if (TypeManager
.ContainsGenericParameters (d
))
1326 if (Convert
.ImplicitReferenceConversionExists (expr
, t
) ||
1327 Convert
.ExplicitReferenceConversionExists (d
, t
)) {
1333 return CreateConstantResult (false);
1336 Expression
ResolveGenericParameter (Type d
, Type t
)
1338 GenericConstraints constraints
= TypeManager
.GetTypeParameterConstraints (t
);
1339 if (constraints
!= null) {
1340 if (constraints
.IsReferenceType
&& TypeManager
.IsStruct (d
))
1341 return CreateConstantResult (false);
1343 if (constraints
.IsValueType
&& !TypeManager
.IsStruct (d
))
1344 return CreateConstantResult (TypeManager
.IsEqual (d
, t
));
1347 if (TypeManager
.IsGenericParameter (expr
.Type
))
1348 expr
= new BoxedCast (expr
, d
);
1353 protected override string OperatorName
{
1354 get { return "is"; }
1359 /// Implementation of the `as' operator.
1361 public class As
: Probe
{
1363 Expression resolved_type
;
1365 public As (Expression expr
, Expression probe_type
, Location l
)
1366 : base (expr
, probe_type
, l
)
1370 public override Expression
CreateExpressionTree (EmitContext ec
)
1372 Arguments args
= Arguments
.CreateForExpressionTree (ec
, null,
1373 expr
.CreateExpressionTree (ec
),
1374 new TypeOf (probe_type_expr
, loc
));
1376 return CreateExpressionFactoryCall ("TypeAs", args
);
1379 public override void Emit (EmitContext ec
)
1381 ILGenerator ig
= ec
.ig
;
1386 ig
.Emit (OpCodes
.Isinst
, type
);
1389 if (TypeManager
.IsGenericParameter (type
) || TypeManager
.IsNullableType (type
))
1390 ig
.Emit (OpCodes
.Unbox_Any
, type
);
1394 public override Expression
DoResolve (EmitContext ec
)
1396 // Because expr is modified
1397 if (eclass
!= ExprClass
.Invalid
)
1400 if (resolved_type
== null) {
1401 resolved_type
= base.DoResolve (ec
);
1403 if (resolved_type
== null)
1407 type
= probe_type_expr
.Type
;
1408 eclass
= ExprClass
.Value
;
1409 Type etype
= expr
.Type
;
1411 if (!TypeManager
.IsReferenceType (type
) && !TypeManager
.IsNullableType (type
)) {
1412 if (probe_type_expr
is TypeParameterExpr
) {
1413 Report
.Error (413, loc
,
1414 "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
1415 probe_type_expr
.GetSignatureForError ());
1417 Report
.Error (77, loc
,
1418 "The `as' operator cannot be used with a non-nullable value type `{0}'",
1419 TypeManager
.CSharpName (type
));
1424 if (expr
.IsNull
&& TypeManager
.IsNullableType (type
)) {
1425 return Nullable
.LiftedNull
.CreateFromExpression (this);
1428 Expression e
= Convert
.ImplicitConversion (ec
, expr
, type
, loc
);
1435 if (Convert
.ExplicitReferenceConversionExists (etype
, type
)){
1436 if (TypeManager
.IsGenericParameter (etype
))
1437 expr
= new BoxedCast (expr
, etype
);
1443 if (TypeManager
.ContainsGenericParameters (etype
) ||
1444 TypeManager
.ContainsGenericParameters (type
)) {
1445 expr
= new BoxedCast (expr
, etype
);
1450 Report
.Error (39, loc
, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1451 TypeManager
.CSharpName (etype
), TypeManager
.CSharpName (type
));
1456 protected override string OperatorName
{
1457 get { return "as"; }
1460 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1462 type
= storey
.MutateType (type
);
1463 base.MutateHoistedGenericType (storey
);
1466 public override bool GetAttributableValue (EmitContext ec
, Type value_type
, out object value)
1468 return expr
.GetAttributableValue (ec
, value_type
, out value);
1473 /// This represents a typecast in the source language.
1475 /// FIXME: Cast expressions have an unusual set of parsing
1476 /// rules, we need to figure those out.
1478 public class Cast
: Expression
{
1479 Expression target_type
;
1482 public Cast (Expression cast_type
, Expression expr
)
1483 : this (cast_type
, expr
, cast_type
.Location
)
1487 public Cast (Expression cast_type
, Expression expr
, Location loc
)
1489 this.target_type
= cast_type
;
1494 public Expression TargetType
{
1495 get { return target_type; }
1498 public Expression Expr
{
1499 get { return expr; }
1502 public override Expression
CreateExpressionTree (EmitContext ec
)
1504 throw new NotSupportedException ("ET");
1507 public override Expression
DoResolve (EmitContext ec
)
1509 expr
= expr
.Resolve (ec
);
1513 TypeExpr target
= target_type
.ResolveAsTypeTerminal (ec
, false);
1519 if (type
.IsAbstract
&& type
.IsSealed
) {
1520 Report
.Error (716, loc
, "Cannot convert to static type `{0}'", TypeManager
.CSharpName (type
));
1524 eclass
= ExprClass
.Value
;
1526 Constant c
= expr
as Constant
;
1528 c
= c
.TryReduce (ec
, type
, loc
);
1533 if (type
.IsPointer
&& !ec
.InUnsafe
) {
1537 expr
= Convert
.ExplicitConversion (ec
, expr
, type
, loc
);
1541 public override void Emit (EmitContext ec
)
1543 throw new Exception ("Should not happen");
1546 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1548 Cast target
= (Cast
) t
;
1550 target
.target_type
= target_type
.Clone (clonectx
);
1551 target
.expr
= expr
.Clone (clonectx
);
1556 // C# 2.0 Default value expression
1558 public class DefaultValueExpression
: Expression
1560 sealed class DefaultValueNullLiteral
: NullLiteral
1562 public DefaultValueNullLiteral (DefaultValueExpression expr
)
1563 : base (expr
.type
, expr
.loc
)
1567 public override void Error_ValueCannotBeConverted (EmitContext ec
, Location loc
, Type t
, bool expl
)
1569 Error_ValueCannotBeConvertedCore (ec
, loc
, t
, expl
);
1576 public DefaultValueExpression (Expression expr
, Location loc
)
1582 public override Expression
CreateExpressionTree (EmitContext ec
)
1584 Arguments args
= new Arguments (2);
1585 args
.Add (new Argument (this));
1586 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
1587 return CreateExpressionFactoryCall ("Constant", args
);
1590 public override Expression
DoResolve (EmitContext ec
)
1592 TypeExpr texpr
= expr
.ResolveAsTypeTerminal (ec
, false);
1598 if ((type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
1599 Report
.Error (-244, loc
, "The `default value' operator cannot be applied to an operand of a static type");
1603 return new NullLiteral (Location
).ConvertImplicitly (type
);
1605 if (TypeManager
.IsReferenceType (type
))
1606 return new DefaultValueNullLiteral (this);
1608 Constant c
= New
.Constantify (type
);
1612 eclass
= ExprClass
.Variable
;
1616 public override void Emit (EmitContext ec
)
1618 LocalTemporary temp_storage
= new LocalTemporary(type
);
1620 temp_storage
.AddressOf(ec
, AddressOp
.LoadStore
);
1621 ec
.ig
.Emit(OpCodes
.Initobj
, type
);
1622 temp_storage
.Emit(ec
);
1625 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1627 type
= storey
.MutateType (type
);
1630 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1632 DefaultValueExpression target
= (DefaultValueExpression
) t
;
1634 target
.expr
= expr
.Clone (clonectx
);
1639 /// Binary operators
1641 public class Binary
: Expression
, IDynamicBinder
1644 protected class PredefinedOperator
{
1645 protected readonly Type left
;
1646 protected readonly Type right
;
1647 public readonly Operator OperatorsMask
;
1648 public Type ReturnType
;
1650 public PredefinedOperator (Type ltype
, Type rtype
, Operator op_mask
)
1651 : this (ltype
, rtype
, op_mask
, ltype
)
1655 public PredefinedOperator (Type type
, Operator op_mask
, Type return_type
)
1656 : this (type
, type
, op_mask
, return_type
)
1660 public PredefinedOperator (Type type
, Operator op_mask
)
1661 : this (type
, type
, op_mask
, type
)
1665 public PredefinedOperator (Type ltype
, Type rtype
, Operator op_mask
, Type return_type
)
1667 if ((op_mask
& Operator
.ValuesOnlyMask
) != 0)
1668 throw new InternalErrorException ("Only masked values can be used");
1672 this.OperatorsMask
= op_mask
;
1673 this.ReturnType
= return_type
;
1676 public virtual Expression
ConvertResult (EmitContext ec
, Binary b
)
1678 b
.type
= ReturnType
;
1680 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1681 b
.right
= Convert
.ImplicitConversion (ec
, b
.right
, right
, b
.right
.Location
);
1684 // A user operators does not support multiple user conversions, but decimal type
1685 // is considered to be predefined type therefore we apply predefined operators rules
1686 // and then look for decimal user-operator implementation
1688 if (left
== TypeManager
.decimal_type
)
1689 return b
.ResolveUserOperator (ec
, b
.left
.Type
, b
.right
.Type
);
1694 public bool IsPrimitiveApplicable (Type ltype
, Type rtype
)
1697 // We are dealing with primitive types only
1699 return left
== ltype
&& ltype
== rtype
;
1702 public virtual bool IsApplicable (EmitContext ec
, Expression lexpr
, Expression rexpr
)
1704 if (TypeManager
.IsEqual (left
, lexpr
.Type
) &&
1705 TypeManager
.IsEqual (right
, rexpr
.Type
))
1708 return Convert
.ImplicitConversionExists (ec
, lexpr
, left
) &&
1709 Convert
.ImplicitConversionExists (ec
, rexpr
, right
);
1712 public PredefinedOperator
ResolveBetterOperator (EmitContext ec
, PredefinedOperator best_operator
)
1715 if (left
!= null && best_operator
.left
!= null) {
1716 result
= MethodGroupExpr
.BetterTypeConversion (ec
, best_operator
.left
, left
);
1720 // When second arguments are same as the first one, the result is same
1722 if (right
!= null && (left
!= right
|| best_operator
.left
!= best_operator
.right
)) {
1723 result
|= MethodGroupExpr
.BetterTypeConversion (ec
, best_operator
.right
, right
);
1726 if (result
== 0 || result
> 2)
1729 return result
== 1 ? best_operator
: this;
1733 class PredefinedStringOperator
: PredefinedOperator
{
1734 public PredefinedStringOperator (Type type
, Operator op_mask
)
1735 : base (type
, op_mask
, type
)
1737 ReturnType
= TypeManager
.string_type
;
1740 public PredefinedStringOperator (Type ltype
, Type rtype
, Operator op_mask
)
1741 : base (ltype
, rtype
, op_mask
)
1743 ReturnType
= TypeManager
.string_type
;
1746 public override Expression
ConvertResult (EmitContext ec
, Binary b
)
1749 // Use original expression for nullable arguments
1751 Nullable
.Unwrap unwrap
= b
.left
as Nullable
.Unwrap
;
1753 b
.left
= unwrap
.Original
;
1755 unwrap
= b
.right
as Nullable
.Unwrap
;
1757 b
.right
= unwrap
.Original
;
1759 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1760 b
.right
= Convert
.ImplicitConversion (ec
, b
.right
, right
, b
.right
.Location
);
1763 // Start a new concat expression using converted expression
1765 return new StringConcat (ec
, b
.loc
, b
.left
, b
.right
).Resolve (ec
);
1769 class PredefinedShiftOperator
: PredefinedOperator
{
1770 public PredefinedShiftOperator (Type ltype
, Operator op_mask
) :
1771 base (ltype
, TypeManager
.int32_type
, op_mask
)
1775 public override Expression
ConvertResult (EmitContext ec
, Binary b
)
1777 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1779 Expression expr_tree_expr
= EmptyCast
.Create (b
.right
, TypeManager
.int32_type
);
1781 int right_mask
= left
== TypeManager
.int32_type
|| left
== TypeManager
.uint32_type
? 0x1f : 0x3f;
1784 // b = b.left >> b.right & (0x1f|0x3f)
1786 b
.right
= new Binary (Operator
.BitwiseAnd
,
1787 b
.right
, new IntConstant (right_mask
, b
.right
.Location
)).Resolve (ec
);
1790 // Expression tree representation does not use & mask
1792 b
.right
= ReducedExpression
.Create (b
.right
, expr_tree_expr
).Resolve (ec
);
1793 b
.type
= ReturnType
;
1798 class PredefinedPointerOperator
: PredefinedOperator
{
1799 public PredefinedPointerOperator (Type ltype
, Type rtype
, Operator op_mask
)
1800 : base (ltype
, rtype
, op_mask
)
1804 public PredefinedPointerOperator (Type ltype
, Type rtype
, Operator op_mask
, Type retType
)
1805 : base (ltype
, rtype
, op_mask
, retType
)
1809 public PredefinedPointerOperator (Type type
, Operator op_mask
, Type return_type
)
1810 : base (type
, op_mask
, return_type
)
1814 public override bool IsApplicable (EmitContext ec
, Expression lexpr
, Expression rexpr
)
1817 if (!lexpr
.Type
.IsPointer
)
1820 if (!Convert
.ImplicitConversionExists (ec
, lexpr
, left
))
1824 if (right
== null) {
1825 if (!rexpr
.Type
.IsPointer
)
1828 if (!Convert
.ImplicitConversionExists (ec
, rexpr
, right
))
1835 public override Expression
ConvertResult (EmitContext ec
, Binary b
)
1838 b
.left
= EmptyCast
.Create (b
.left
, left
);
1839 } else if (right
!= null) {
1840 b
.right
= EmptyCast
.Create (b
.right
, right
);
1843 Type r_type
= ReturnType
;
1844 Expression left_arg
, right_arg
;
1845 if (r_type
== null) {
1848 right_arg
= b
.right
;
1849 r_type
= b
.left
.Type
;
1853 r_type
= b
.right
.Type
;
1857 right_arg
= b
.right
;
1860 return new PointerArithmetic (b
.oper
, left_arg
, right_arg
, r_type
, b
.loc
).Resolve (ec
);
1865 public enum Operator
{
1866 Multiply
= 0 | ArithmeticMask
,
1867 Division
= 1 | ArithmeticMask
,
1868 Modulus
= 2 | ArithmeticMask
,
1869 Addition
= 3 | ArithmeticMask
| AdditionMask
,
1870 Subtraction
= 4 | ArithmeticMask
| SubtractionMask
,
1872 LeftShift
= 5 | ShiftMask
,
1873 RightShift
= 6 | ShiftMask
,
1875 LessThan
= 7 | ComparisonMask
| RelationalMask
,
1876 GreaterThan
= 8 | ComparisonMask
| RelationalMask
,
1877 LessThanOrEqual
= 9 | ComparisonMask
| RelationalMask
,
1878 GreaterThanOrEqual
= 10 | ComparisonMask
| RelationalMask
,
1879 Equality
= 11 | ComparisonMask
| EqualityMask
,
1880 Inequality
= 12 | ComparisonMask
| EqualityMask
,
1882 BitwiseAnd
= 13 | BitwiseMask
,
1883 ExclusiveOr
= 14 | BitwiseMask
,
1884 BitwiseOr
= 15 | BitwiseMask
,
1886 LogicalAnd
= 16 | LogicalMask
,
1887 LogicalOr
= 17 | LogicalMask
,
1892 ValuesOnlyMask
= ArithmeticMask
- 1,
1893 ArithmeticMask
= 1 << 5,
1895 ComparisonMask
= 1 << 7,
1896 EqualityMask
= 1 << 8,
1897 BitwiseMask
= 1 << 9,
1898 LogicalMask
= 1 << 10,
1899 AdditionMask
= 1 << 11,
1900 SubtractionMask
= 1 << 12,
1901 RelationalMask
= 1 << 13
1904 readonly Operator oper
;
1905 protected Expression left
, right
;
1906 readonly bool is_compound
;
1907 Expression enum_conversion
;
1909 static PredefinedOperator
[] standard_operators
;
1910 static PredefinedOperator
[] pointer_operators
;
1912 public Binary (Operator oper
, Expression left
, Expression right
, bool isCompound
)
1913 : this (oper
, left
, right
)
1915 this.is_compound
= isCompound
;
1918 public Binary (Operator oper
, Expression left
, Expression right
)
1923 this.loc
= left
.Location
;
1926 public Operator Oper
{
1933 /// Returns a stringified representation of the Operator
1935 string OperName (Operator oper
)
1939 case Operator
.Multiply
:
1942 case Operator
.Division
:
1945 case Operator
.Modulus
:
1948 case Operator
.Addition
:
1951 case Operator
.Subtraction
:
1954 case Operator
.LeftShift
:
1957 case Operator
.RightShift
:
1960 case Operator
.LessThan
:
1963 case Operator
.GreaterThan
:
1966 case Operator
.LessThanOrEqual
:
1969 case Operator
.GreaterThanOrEqual
:
1972 case Operator
.Equality
:
1975 case Operator
.Inequality
:
1978 case Operator
.BitwiseAnd
:
1981 case Operator
.BitwiseOr
:
1984 case Operator
.ExclusiveOr
:
1987 case Operator
.LogicalOr
:
1990 case Operator
.LogicalAnd
:
1994 s
= oper
.ToString ();
2004 public static void Error_OperatorCannotBeApplied (Expression left
, Expression right
, Operator oper
, Location loc
)
2006 new Binary (oper
, left
, right
).Error_OperatorCannotBeApplied (left
, right
);
2009 public static void Error_OperatorCannotBeApplied (Expression left
, Expression right
, string oper
, Location loc
)
2012 l
= TypeManager
.CSharpName (left
.Type
);
2013 r
= TypeManager
.CSharpName (right
.Type
);
2015 Report
.Error (19, loc
, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2019 protected void Error_OperatorCannotBeApplied (Expression left
, Expression right
)
2021 Error_OperatorCannotBeApplied (left
, right
, OperName (oper
), loc
);
2025 // Converts operator to System.Linq.Expressions.ExpressionType enum name
2027 string GetOperatorExpressionTypeName ()
2030 case Operator
.Addition
:
2031 return is_compound
? "AddAssign" : "Add";
2032 case Operator
.Equality
:
2034 case Operator
.Multiply
:
2035 return is_compound
? "MultiplyAssign" : "Multiply";
2037 throw new NotImplementedException ("Unknown expression type operator " + oper
.ToString ());
2041 static string GetOperatorMetadataName (Operator op
)
2043 CSharp
.Operator
.OpType op_type
;
2045 case Operator
.Addition
:
2046 op_type
= CSharp
.Operator
.OpType
.Addition
; break;
2047 case Operator
.BitwiseAnd
:
2048 op_type
= CSharp
.Operator
.OpType
.BitwiseAnd
; break;
2049 case Operator
.BitwiseOr
:
2050 op_type
= CSharp
.Operator
.OpType
.BitwiseOr
; break;
2051 case Operator
.Division
:
2052 op_type
= CSharp
.Operator
.OpType
.Division
; break;
2053 case Operator
.Equality
:
2054 op_type
= CSharp
.Operator
.OpType
.Equality
; break;
2055 case Operator
.ExclusiveOr
:
2056 op_type
= CSharp
.Operator
.OpType
.ExclusiveOr
; break;
2057 case Operator
.GreaterThan
:
2058 op_type
= CSharp
.Operator
.OpType
.GreaterThan
; break;
2059 case Operator
.GreaterThanOrEqual
:
2060 op_type
= CSharp
.Operator
.OpType
.GreaterThanOrEqual
; break;
2061 case Operator
.Inequality
:
2062 op_type
= CSharp
.Operator
.OpType
.Inequality
; break;
2063 case Operator
.LeftShift
:
2064 op_type
= CSharp
.Operator
.OpType
.LeftShift
; break;
2065 case Operator
.LessThan
:
2066 op_type
= CSharp
.Operator
.OpType
.LessThan
; break;
2067 case Operator
.LessThanOrEqual
:
2068 op_type
= CSharp
.Operator
.OpType
.LessThanOrEqual
; break;
2069 case Operator
.Modulus
:
2070 op_type
= CSharp
.Operator
.OpType
.Modulus
; break;
2071 case Operator
.Multiply
:
2072 op_type
= CSharp
.Operator
.OpType
.Multiply
; break;
2073 case Operator
.RightShift
:
2074 op_type
= CSharp
.Operator
.OpType
.RightShift
; break;
2075 case Operator
.Subtraction
:
2076 op_type
= CSharp
.Operator
.OpType
.Subtraction
; break;
2078 throw new InternalErrorException (op
.ToString ());
2081 return CSharp
.Operator
.GetMetadataName (op_type
);
2084 public static void EmitOperatorOpcode (EmitContext ec
, Operator oper
, Type l
)
2087 ILGenerator ig
= ec
.ig
;
2090 case Operator
.Multiply
:
2092 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2093 opcode
= OpCodes
.Mul_Ovf
;
2094 else if (!IsFloat (l
))
2095 opcode
= OpCodes
.Mul_Ovf_Un
;
2097 opcode
= OpCodes
.Mul
;
2099 opcode
= OpCodes
.Mul
;
2103 case Operator
.Division
:
2105 opcode
= OpCodes
.Div_Un
;
2107 opcode
= OpCodes
.Div
;
2110 case Operator
.Modulus
:
2112 opcode
= OpCodes
.Rem_Un
;
2114 opcode
= OpCodes
.Rem
;
2117 case Operator
.Addition
:
2119 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2120 opcode
= OpCodes
.Add_Ovf
;
2121 else if (!IsFloat (l
))
2122 opcode
= OpCodes
.Add_Ovf_Un
;
2124 opcode
= OpCodes
.Add
;
2126 opcode
= OpCodes
.Add
;
2129 case Operator
.Subtraction
:
2131 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2132 opcode
= OpCodes
.Sub_Ovf
;
2133 else if (!IsFloat (l
))
2134 opcode
= OpCodes
.Sub_Ovf_Un
;
2136 opcode
= OpCodes
.Sub
;
2138 opcode
= OpCodes
.Sub
;
2141 case Operator
.RightShift
:
2143 opcode
= OpCodes
.Shr_Un
;
2145 opcode
= OpCodes
.Shr
;
2148 case Operator
.LeftShift
:
2149 opcode
= OpCodes
.Shl
;
2152 case Operator
.Equality
:
2153 opcode
= OpCodes
.Ceq
;
2156 case Operator
.Inequality
:
2157 ig
.Emit (OpCodes
.Ceq
);
2158 ig
.Emit (OpCodes
.Ldc_I4_0
);
2160 opcode
= OpCodes
.Ceq
;
2163 case Operator
.LessThan
:
2165 opcode
= OpCodes
.Clt_Un
;
2167 opcode
= OpCodes
.Clt
;
2170 case Operator
.GreaterThan
:
2172 opcode
= OpCodes
.Cgt_Un
;
2174 opcode
= OpCodes
.Cgt
;
2177 case Operator
.LessThanOrEqual
:
2178 if (IsUnsigned (l
) || IsFloat (l
))
2179 ig
.Emit (OpCodes
.Cgt_Un
);
2181 ig
.Emit (OpCodes
.Cgt
);
2182 ig
.Emit (OpCodes
.Ldc_I4_0
);
2184 opcode
= OpCodes
.Ceq
;
2187 case Operator
.GreaterThanOrEqual
:
2188 if (IsUnsigned (l
) || IsFloat (l
))
2189 ig
.Emit (OpCodes
.Clt_Un
);
2191 ig
.Emit (OpCodes
.Clt
);
2193 ig
.Emit (OpCodes
.Ldc_I4_0
);
2195 opcode
= OpCodes
.Ceq
;
2198 case Operator
.BitwiseOr
:
2199 opcode
= OpCodes
.Or
;
2202 case Operator
.BitwiseAnd
:
2203 opcode
= OpCodes
.And
;
2206 case Operator
.ExclusiveOr
:
2207 opcode
= OpCodes
.Xor
;
2211 throw new InternalErrorException (oper
.ToString ());
2217 static bool IsUnsigned (Type t
)
2222 return (t
== TypeManager
.uint32_type
|| t
== TypeManager
.uint64_type
||
2223 t
== TypeManager
.ushort_type
|| t
== TypeManager
.byte_type
);
2226 static bool IsFloat (Type t
)
2228 return t
== TypeManager
.float_type
|| t
== TypeManager
.double_type
;
2231 Expression
ResolveOperator (EmitContext ec
)
2234 Type r
= right
.Type
;
2236 bool primitives_only
= false;
2238 if (standard_operators
== null)
2239 CreateStandardOperatorsTable ();
2242 // Handles predefined primitive types
2244 if (TypeManager
.IsPrimitiveType (l
) && TypeManager
.IsPrimitiveType (r
)) {
2245 if ((oper
& Operator
.ShiftMask
) == 0) {
2246 if (l
!= TypeManager
.bool_type
&& !DoBinaryOperatorPromotion (ec
))
2249 primitives_only
= true;
2253 if (l
.IsPointer
|| r
.IsPointer
)
2254 return ResolveOperatorPointer (ec
, l
, r
);
2257 bool lenum
= TypeManager
.IsEnumType (l
);
2258 bool renum
= TypeManager
.IsEnumType (r
);
2259 if (lenum
|| renum
) {
2260 expr
= ResolveOperatorEnum (ec
, lenum
, renum
, l
, r
);
2262 // TODO: Can this be ambiguous
2268 if ((oper
== Operator
.Addition
|| oper
== Operator
.Subtraction
|| (oper
& Operator
.EqualityMask
) != 0) &&
2269 (TypeManager
.IsDelegateType (l
) || TypeManager
.IsDelegateType (r
))) {
2271 expr
= ResolveOperatorDelegate (ec
, l
, r
);
2273 // TODO: Can this be ambiguous
2279 expr
= ResolveUserOperator (ec
, l
, r
);
2283 // Predefined reference types equality
2284 if ((oper
& Operator
.EqualityMask
) != 0) {
2285 expr
= ResolveOperatorEqualityRerefence (ec
, l
, r
);
2291 return ResolveOperatorPredefined (ec
, standard_operators
, primitives_only
, null);
2294 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
2295 // if 'left' is not an enumeration constant, create one from the type of 'right'
2296 Constant
EnumLiftUp (EmitContext ec
, Constant left
, Constant right
, Location loc
)
2299 case Operator
.BitwiseOr
:
2300 case Operator
.BitwiseAnd
:
2301 case Operator
.ExclusiveOr
:
2302 case Operator
.Equality
:
2303 case Operator
.Inequality
:
2304 case Operator
.LessThan
:
2305 case Operator
.LessThanOrEqual
:
2306 case Operator
.GreaterThan
:
2307 case Operator
.GreaterThanOrEqual
:
2308 if (TypeManager
.IsEnumType (left
.Type
))
2311 if (left
.IsZeroInteger
)
2312 return left
.TryReduce (ec
, right
.Type
, loc
);
2316 case Operator
.Addition
:
2317 case Operator
.Subtraction
:
2320 case Operator
.Multiply
:
2321 case Operator
.Division
:
2322 case Operator
.Modulus
:
2323 case Operator
.LeftShift
:
2324 case Operator
.RightShift
:
2325 if (TypeManager
.IsEnumType (right
.Type
) || TypeManager
.IsEnumType (left
.Type
))
2329 Error_OperatorCannotBeApplied (this.left
, this.right
);
2334 // The `|' operator used on types which were extended is dangerous
2336 void CheckBitwiseOrOnSignExtended ()
2338 OpcodeCast lcast
= left
as OpcodeCast
;
2339 if (lcast
!= null) {
2340 if (IsUnsigned (lcast
.UnderlyingType
))
2344 OpcodeCast rcast
= right
as OpcodeCast
;
2345 if (rcast
!= null) {
2346 if (IsUnsigned (rcast
.UnderlyingType
))
2350 if (lcast
== null && rcast
== null)
2353 // FIXME: consider constants
2355 Report
.Warning (675, 3, loc
,
2356 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2357 TypeManager
.CSharpName (lcast
!= null ? lcast
.UnderlyingType
: rcast
.UnderlyingType
));
2360 static void CreatePointerOperatorsTable ()
2362 ArrayList temp
= new ArrayList ();
2365 // Pointer arithmetic:
2367 // T* operator + (T* x, int y); T* operator - (T* x, int y);
2368 // T* operator + (T* x, uint y); T* operator - (T* x, uint y);
2369 // T* operator + (T* x, long y); T* operator - (T* x, long y);
2370 // T* operator + (T* x, ulong y); T* operator - (T* x, ulong y);
2372 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.int32_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2373 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.uint32_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2374 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.int64_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2375 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.uint64_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2378 // T* operator + (int y, T* x);
2379 // T* operator + (uint y, T *x);
2380 // T* operator + (long y, T *x);
2381 // T* operator + (ulong y, T *x);
2383 temp
.Add (new PredefinedPointerOperator (TypeManager
.int32_type
, null, Operator
.AdditionMask
, null));
2384 temp
.Add (new PredefinedPointerOperator (TypeManager
.uint32_type
, null, Operator
.AdditionMask
, null));
2385 temp
.Add (new PredefinedPointerOperator (TypeManager
.int64_type
, null, Operator
.AdditionMask
, null));
2386 temp
.Add (new PredefinedPointerOperator (TypeManager
.uint64_type
, null, Operator
.AdditionMask
, null));
2389 // long operator - (T* x, T *y)
2391 temp
.Add (new PredefinedPointerOperator (null, Operator
.SubtractionMask
, TypeManager
.int64_type
));
2393 pointer_operators
= (PredefinedOperator
[]) temp
.ToArray (typeof (PredefinedOperator
));
2396 static void CreateStandardOperatorsTable ()
2398 ArrayList temp
= new ArrayList ();
2399 Type bool_type
= TypeManager
.bool_type
;
2401 temp
.Add (new PredefinedOperator (TypeManager
.int32_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2402 temp
.Add (new PredefinedOperator (TypeManager
.uint32_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2403 temp
.Add (new PredefinedOperator (TypeManager
.int64_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2404 temp
.Add (new PredefinedOperator (TypeManager
.uint64_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2405 temp
.Add (new PredefinedOperator (TypeManager
.float_type
, Operator
.ArithmeticMask
));
2406 temp
.Add (new PredefinedOperator (TypeManager
.double_type
, Operator
.ArithmeticMask
));
2407 temp
.Add (new PredefinedOperator (TypeManager
.decimal_type
, Operator
.ArithmeticMask
));
2409 temp
.Add (new PredefinedOperator (TypeManager
.int32_type
, Operator
.ComparisonMask
, bool_type
));
2410 temp
.Add (new PredefinedOperator (TypeManager
.uint32_type
, Operator
.ComparisonMask
, bool_type
));
2411 temp
.Add (new PredefinedOperator (TypeManager
.int64_type
, Operator
.ComparisonMask
, bool_type
));
2412 temp
.Add (new PredefinedOperator (TypeManager
.uint64_type
, Operator
.ComparisonMask
, bool_type
));
2413 temp
.Add (new PredefinedOperator (TypeManager
.float_type
, Operator
.ComparisonMask
, bool_type
));
2414 temp
.Add (new PredefinedOperator (TypeManager
.double_type
, Operator
.ComparisonMask
, bool_type
));
2415 temp
.Add (new PredefinedOperator (TypeManager
.decimal_type
, Operator
.ComparisonMask
, bool_type
));
2417 temp
.Add (new PredefinedOperator (TypeManager
.string_type
, Operator
.EqualityMask
, bool_type
));
2419 temp
.Add (new PredefinedStringOperator (TypeManager
.string_type
, Operator
.AdditionMask
));
2420 temp
.Add (new PredefinedStringOperator (TypeManager
.string_type
, TypeManager
.object_type
, Operator
.AdditionMask
));
2421 temp
.Add (new PredefinedStringOperator (TypeManager
.object_type
, TypeManager
.string_type
, Operator
.AdditionMask
));
2423 temp
.Add (new PredefinedOperator (bool_type
,
2424 Operator
.BitwiseMask
| Operator
.LogicalMask
| Operator
.EqualityMask
, bool_type
));
2426 temp
.Add (new PredefinedShiftOperator (TypeManager
.int32_type
, Operator
.ShiftMask
));
2427 temp
.Add (new PredefinedShiftOperator (TypeManager
.uint32_type
, Operator
.ShiftMask
));
2428 temp
.Add (new PredefinedShiftOperator (TypeManager
.int64_type
, Operator
.ShiftMask
));
2429 temp
.Add (new PredefinedShiftOperator (TypeManager
.uint64_type
, Operator
.ShiftMask
));
2431 standard_operators
= (PredefinedOperator
[]) temp
.ToArray (typeof (PredefinedOperator
));
2435 // Rules used during binary numeric promotion
2437 static bool DoNumericPromotion (ref Expression prim_expr
, ref Expression second_expr
, Type type
)
2442 Constant c
= prim_expr
as Constant
;
2444 temp
= c
.ConvertImplicitly (type
);
2451 if (type
== TypeManager
.uint32_type
) {
2452 etype
= prim_expr
.Type
;
2453 if (etype
== TypeManager
.int32_type
|| etype
== TypeManager
.short_type
|| etype
== TypeManager
.sbyte_type
) {
2454 type
= TypeManager
.int64_type
;
2456 if (type
!= second_expr
.Type
) {
2457 c
= second_expr
as Constant
;
2459 temp
= c
.ConvertImplicitly (type
);
2461 temp
= Convert
.ImplicitNumericConversion (second_expr
, type
);
2467 } else if (type
== TypeManager
.uint64_type
) {
2469 // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
2471 if (type
== TypeManager
.int32_type
|| type
== TypeManager
.int64_type
||
2472 type
== TypeManager
.sbyte_type
|| type
== TypeManager
.sbyte_type
)
2476 temp
= Convert
.ImplicitNumericConversion (prim_expr
, type
);
2485 // 7.2.6.2 Binary numeric promotions
2487 public bool DoBinaryOperatorPromotion (EmitContext ec
)
2489 Type ltype
= left
.Type
;
2490 Type rtype
= right
.Type
;
2493 foreach (Type t
in ConstantFold
.binary_promotions
) {
2495 return t
== rtype
|| DoNumericPromotion (ref right
, ref left
, t
);
2498 return t
== ltype
|| DoNumericPromotion (ref left
, ref right
, t
);
2501 Type int32
= TypeManager
.int32_type
;
2502 if (ltype
!= int32
) {
2503 Constant c
= left
as Constant
;
2505 temp
= c
.ConvertImplicitly (int32
);
2507 temp
= Convert
.ImplicitNumericConversion (left
, int32
);
2514 if (rtype
!= int32
) {
2515 Constant c
= right
as Constant
;
2517 temp
= c
.ConvertImplicitly (int32
);
2519 temp
= Convert
.ImplicitNumericConversion (right
, int32
);
2529 public override Expression
DoResolve (EmitContext ec
)
2534 if ((oper
== Operator
.Subtraction
) && (left
is ParenthesizedExpression
)) {
2535 left
= ((ParenthesizedExpression
) left
).Expr
;
2536 left
= left
.Resolve (ec
, ResolveFlags
.VariableOrValue
| ResolveFlags
.Type
);
2540 if (left
.eclass
== ExprClass
.Type
) {
2541 Report
.Error (75, loc
, "To cast a negative value, you must enclose the value in parentheses");
2545 left
= left
.Resolve (ec
);
2550 Constant lc
= left
as Constant
;
2552 if (lc
!= null && lc
.Type
== TypeManager
.bool_type
&&
2553 ((oper
== Operator
.LogicalAnd
&& lc
.IsDefaultValue
) ||
2554 (oper
== Operator
.LogicalOr
&& !lc
.IsDefaultValue
))) {
2556 // FIXME: resolve right expression as unreachable
2557 // right.Resolve (ec);
2559 Report
.Warning (429, 4, loc
, "Unreachable expression code detected");
2563 right
= right
.Resolve (ec
);
2567 eclass
= ExprClass
.Value
;
2568 Constant rc
= right
as Constant
;
2570 // The conversion rules are ignored in enum context but why
2571 if (!ec
.InEnumContext
&& lc
!= null && rc
!= null && (TypeManager
.IsEnumType (left
.Type
) || TypeManager
.IsEnumType (right
.Type
))) {
2572 lc
= EnumLiftUp (ec
, lc
, rc
, loc
);
2574 rc
= EnumLiftUp (ec
, rc
, lc
, loc
);
2577 if (rc
!= null && lc
!= null) {
2578 int prev_e
= Report
.Errors
;
2579 Expression e
= ConstantFold
.BinaryFold (
2580 ec
, oper
, lc
, rc
, loc
);
2581 if (e
!= null || Report
.Errors
!= prev_e
)
2584 if ((oper
== Operator
.BitwiseAnd
|| oper
== Operator
.LogicalAnd
) &&
2585 ((lc
!= null && lc
.IsDefaultValue
) || (rc
!= null && rc
.IsDefaultValue
))) {
2587 if ((ResolveOperator (ec
)) == null) {
2588 Error_OperatorCannotBeApplied (left
, right
);
2593 // The result is a constant with side-effect
2595 Constant side_effect
= rc
== null ?
2596 new SideEffectConstant (lc
, right
, loc
) :
2597 new SideEffectConstant (rc
, left
, loc
);
2599 return ReducedExpression
.Create (side_effect
, this);
2603 // Comparison warnings
2604 if ((oper
& Operator
.ComparisonMask
) != 0) {
2605 if (left
.Equals (right
)) {
2606 Report
.Warning (1718, 3, loc
, "A comparison made to same variable. Did you mean to compare something else?");
2608 CheckUselessComparison (lc
, right
.Type
);
2609 CheckUselessComparison (rc
, left
.Type
);
2612 if (TypeManager
.IsDynamicType (left
.Type
) || TypeManager
.IsDynamicType (right
.Type
)) {
2613 Arguments args
= new Arguments (2);
2614 args
.Add (new Argument (left
));
2615 args
.Add (new Argument (right
));
2616 return new DynamicExpressionStatement (this, args
, loc
).Resolve (ec
);
2619 if (RootContext
.Version
>= LanguageVersion
.ISO_2
&&
2620 ((TypeManager
.IsNullableType (left
.Type
) && (right
is NullLiteral
|| TypeManager
.IsNullableType (right
.Type
) || TypeManager
.IsValueType (right
.Type
))) ||
2621 (TypeManager
.IsValueType (left
.Type
) && right
is NullLiteral
) ||
2622 (TypeManager
.IsNullableType (right
.Type
) && (left
is NullLiteral
|| TypeManager
.IsNullableType (left
.Type
) || TypeManager
.IsValueType (left
.Type
))) ||
2623 (TypeManager
.IsValueType (right
.Type
) && left
is NullLiteral
)))
2624 return new Nullable
.LiftedBinaryOperator (oper
, left
, right
, loc
).Resolve (ec
);
2626 return DoResolveCore (ec
, left
, right
);
2629 protected Expression
DoResolveCore (EmitContext ec
, Expression left_orig
, Expression right_orig
)
2631 Expression expr
= ResolveOperator (ec
);
2633 Error_OperatorCannotBeApplied (left_orig
, right_orig
);
2635 if (left
== null || right
== null)
2636 throw new InternalErrorException ("Invalid conversion");
2638 if (oper
== Operator
.BitwiseOr
)
2639 CheckBitwiseOrOnSignExtended ();
2644 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
2646 left
.MutateHoistedGenericType (storey
);
2647 right
.MutateHoistedGenericType (storey
);
2651 // D operator + (D x, D y)
2652 // D operator - (D x, D y)
2653 // bool operator == (D x, D y)
2654 // bool operator != (D x, D y)
2656 Expression
ResolveOperatorDelegate (EmitContext ec
, Type l
, Type r
)
2658 bool is_equality
= (oper
& Operator
.EqualityMask
) != 0;
2659 if (!TypeManager
.IsEqual (l
, r
) && !TypeManager
.IsVariantOf (r
, l
)) {
2661 if (right
.eclass
== ExprClass
.MethodGroup
|| (r
== InternalType
.AnonymousMethod
&& !is_equality
)) {
2662 tmp
= Convert
.ImplicitConversionRequired (ec
, right
, l
, loc
);
2667 } else if (left
.eclass
== ExprClass
.MethodGroup
|| (l
== InternalType
.AnonymousMethod
&& !is_equality
)) {
2668 tmp
= Convert
.ImplicitConversionRequired (ec
, left
, r
, loc
);
2679 // Resolve delegate equality as a user operator
2682 return ResolveUserOperator (ec
, l
, r
);
2685 Arguments args
= new Arguments (2);
2686 args
.Add (new Argument (left
));
2687 args
.Add (new Argument (right
));
2689 if (oper
== Operator
.Addition
) {
2690 if (TypeManager
.delegate_combine_delegate_delegate
== null) {
2691 TypeManager
.delegate_combine_delegate_delegate
= TypeManager
.GetPredefinedMethod (
2692 TypeManager
.delegate_type
, "Combine", loc
, TypeManager
.delegate_type
, TypeManager
.delegate_type
);
2695 method
= TypeManager
.delegate_combine_delegate_delegate
;
2697 if (TypeManager
.delegate_remove_delegate_delegate
== null) {
2698 TypeManager
.delegate_remove_delegate_delegate
= TypeManager
.GetPredefinedMethod (
2699 TypeManager
.delegate_type
, "Remove", loc
, TypeManager
.delegate_type
, TypeManager
.delegate_type
);
2702 method
= TypeManager
.delegate_remove_delegate_delegate
;
2705 MethodGroupExpr mg
= new MethodGroupExpr (new MemberInfo
[] { method }
, TypeManager
.delegate_type
, loc
);
2706 mg
= mg
.OverloadResolve (ec
, ref args
, false, loc
);
2708 return new ClassCast (new UserOperatorCall (mg
, args
, CreateExpressionTree
, loc
), l
);
2712 // Enumeration operators
2714 Expression
ResolveOperatorEnum (EmitContext ec
, bool lenum
, bool renum
, Type ltype
, Type rtype
)
2717 // bool operator == (E x, E y);
2718 // bool operator != (E x, E y);
2719 // bool operator < (E x, E y);
2720 // bool operator > (E x, E y);
2721 // bool operator <= (E x, E y);
2722 // bool operator >= (E x, E y);
2724 // E operator & (E x, E y);
2725 // E operator | (E x, E y);
2726 // E operator ^ (E x, E y);
2728 // U operator - (E e, E f)
2729 // E operator - (E e, U x)
2731 // E operator + (U x, E e)
2732 // E operator + (E e, U x)
2734 if (!((oper
& (Operator
.ComparisonMask
| Operator
.BitwiseMask
)) != 0 ||
2735 (oper
== Operator
.Subtraction
&& lenum
) || (oper
== Operator
.Addition
&& lenum
!= renum
)))
2738 Expression ltemp
= left
;
2739 Expression rtemp
= right
;
2740 Type underlying_type
;
2743 if ((oper
& Operator
.ComparisonMask
| Operator
.BitwiseMask
) != 0) {
2745 expr
= Convert
.ImplicitConversion (ec
, left
, rtype
, loc
);
2751 expr
= Convert
.ImplicitConversion (ec
, right
, ltype
, loc
);
2759 if (TypeManager
.IsEqual (ltype
, rtype
)) {
2760 underlying_type
= TypeManager
.GetEnumUnderlyingType (ltype
);
2762 if (left
is Constant
)
2763 left
= ((Constant
) left
).ConvertExplicitly (false, underlying_type
);
2765 left
= EmptyCast
.Create (left
, underlying_type
);
2767 if (right
is Constant
)
2768 right
= ((Constant
) right
).ConvertExplicitly (false, underlying_type
);
2770 right
= EmptyCast
.Create (right
, underlying_type
);
2772 underlying_type
= TypeManager
.GetEnumUnderlyingType (ltype
);
2774 if (oper
!= Operator
.Subtraction
&& oper
!= Operator
.Addition
) {
2775 Constant c
= right
as Constant
;
2776 if (c
== null || !c
.IsDefaultValue
)
2779 if (!Convert
.ImplicitStandardConversionExists (right
, underlying_type
))
2782 right
= Convert
.ImplicitConversionStandard (ec
, right
, underlying_type
, right
.Location
);
2785 if (left
is Constant
)
2786 left
= ((Constant
) left
).ConvertExplicitly (false, underlying_type
);
2788 left
= EmptyCast
.Create (left
, underlying_type
);
2791 underlying_type
= TypeManager
.GetEnumUnderlyingType (rtype
);
2793 if (oper
!= Operator
.Addition
) {
2794 Constant c
= left
as Constant
;
2795 if (c
== null || !c
.IsDefaultValue
)
2798 if (!Convert
.ImplicitStandardConversionExists (left
, underlying_type
))
2801 left
= Convert
.ImplicitConversionStandard (ec
, left
, underlying_type
, left
.Location
);
2804 if (right
is Constant
)
2805 right
= ((Constant
) right
).ConvertExplicitly (false, underlying_type
);
2807 right
= EmptyCast
.Create (right
, underlying_type
);
2814 // C# specification uses explicit cast syntax which means binary promotion
2815 // should happen, however it seems that csc does not do that
2817 if (!DoBinaryOperatorPromotion (ec
)) {
2823 Type res_type
= null;
2824 if ((oper
& Operator
.BitwiseMask
) != 0 || oper
== Operator
.Subtraction
|| oper
== Operator
.Addition
) {
2825 Type promoted_type
= lenum
? left
.Type
: right
.Type
;
2826 enum_conversion
= Convert
.ExplicitNumericConversion (
2827 new EmptyExpression (promoted_type
), underlying_type
);
2829 if (oper
== Operator
.Subtraction
&& renum
&& lenum
)
2830 res_type
= underlying_type
;
2831 else if (oper
== Operator
.Addition
&& renum
)
2837 expr
= ResolveOperatorPredefined (ec
, standard_operators
, true, res_type
);
2838 if (!is_compound
|| expr
== null)
2842 // TODO: Need to corectly implemented Coumpound Assigment for all operators
2845 if (Convert
.ImplicitConversionExists (ec
, left
, rtype
))
2848 if (!Convert
.ImplicitConversionExists (ec
, ltemp
, rtype
))
2851 expr
= Convert
.ExplicitConversion (ec
, expr
, rtype
, loc
);
2856 // 7.9.6 Reference type equality operators
2858 Binary
ResolveOperatorEqualityRerefence (EmitContext ec
, Type l
, Type r
)
2861 // operator != (object a, object b)
2862 // operator == (object a, object b)
2865 // TODO: this method is almost equivalent to Convert.ImplicitReferenceConversion
2867 if (left
.eclass
== ExprClass
.MethodGroup
|| right
.eclass
== ExprClass
.MethodGroup
)
2870 type
= TypeManager
.bool_type
;
2871 GenericConstraints constraints
;
2873 bool lgen
= TypeManager
.IsGenericParameter (l
);
2875 if (TypeManager
.IsEqual (l
, r
)) {
2878 // Only allow to compare same reference type parameter
2880 if (TypeManager
.IsReferenceType (l
)) {
2881 left
= new BoxedCast (left
, TypeManager
.object_type
);
2882 right
= new BoxedCast (right
, TypeManager
.object_type
);
2889 if (l
== InternalType
.AnonymousMethod
)
2892 if (TypeManager
.IsValueType (l
))
2898 bool rgen
= TypeManager
.IsGenericParameter (r
);
2901 // a, Both operands are reference-type values or the value null
2902 // b, One operand is a value of type T where T is a type-parameter and
2903 // the other operand is the value null. Furthermore T does not have the
2904 // value type constrain
2906 if (left
is NullLiteral
|| right
is NullLiteral
) {
2908 constraints
= TypeManager
.GetTypeParameterConstraints (l
);
2909 if (constraints
!= null && constraints
.HasValueTypeConstraint
)
2912 left
= new BoxedCast (left
, TypeManager
.object_type
);
2917 constraints
= TypeManager
.GetTypeParameterConstraints (r
);
2918 if (constraints
!= null && constraints
.HasValueTypeConstraint
)
2921 right
= new BoxedCast (right
, TypeManager
.object_type
);
2927 // An interface is converted to the object before the
2928 // standard conversion is applied. It's not clear from the
2929 // standard but it looks like it works like that.
2932 if (!TypeManager
.IsReferenceType (l
))
2934 left
= new BoxedCast (left
, TypeManager
.object_type
);
2935 } else if (l
.IsInterface
) {
2936 l
= TypeManager
.object_type
;
2937 } else if (TypeManager
.IsStruct (l
)) {
2942 if (!TypeManager
.IsReferenceType (r
))
2944 right
= new BoxedCast (right
, TypeManager
.object_type
);
2945 } else if (r
.IsInterface
) {
2946 r
= TypeManager
.object_type
;
2947 } else if (TypeManager
.IsStruct (r
)) {
2952 const string ref_comparison
= "Possible unintended reference comparison. " +
2953 "Consider casting the {0} side of the expression to `string' to compare the values";
2956 // A standard implicit conversion exists from the type of either
2957 // operand to the type of the other operand
2959 if (Convert
.ImplicitReferenceConversionExists (left
, r
)) {
2960 if (l
== TypeManager
.string_type
)
2961 Report
.Warning (253, 2, loc
, ref_comparison
, "right");
2966 if (Convert
.ImplicitReferenceConversionExists (right
, l
)) {
2967 if (r
== TypeManager
.string_type
)
2968 Report
.Warning (252, 2, loc
, ref_comparison
, "left");
2977 Expression
ResolveOperatorPointer (EmitContext ec
, Type l
, Type r
)
2980 // bool operator == (void* x, void* y);
2981 // bool operator != (void* x, void* y);
2982 // bool operator < (void* x, void* y);
2983 // bool operator > (void* x, void* y);
2984 // bool operator <= (void* x, void* y);
2985 // bool operator >= (void* x, void* y);
2987 if ((oper
& Operator
.ComparisonMask
) != 0) {
2990 temp
= Convert
.ImplicitConversion (ec
, left
, r
, left
.Location
);
2997 temp
= Convert
.ImplicitConversion (ec
, right
, l
, right
.Location
);
3003 type
= TypeManager
.bool_type
;
3007 if (pointer_operators
== null)
3008 CreatePointerOperatorsTable ();
3010 return ResolveOperatorPredefined (ec
, pointer_operators
, false, null);
3014 // Build-in operators method overloading
3016 protected virtual Expression
ResolveOperatorPredefined (EmitContext ec
, PredefinedOperator
[] operators
, bool primitives_only
, Type enum_type
)
3018 PredefinedOperator best_operator
= null;
3020 Type r
= right
.Type
;
3021 Operator oper_mask
= oper
& ~Operator
.ValuesOnlyMask
;
3023 foreach (PredefinedOperator po
in operators
) {
3024 if ((po
.OperatorsMask
& oper_mask
) == 0)
3027 if (primitives_only
) {
3028 if (!po
.IsPrimitiveApplicable (l
, r
))
3031 if (!po
.IsApplicable (ec
, left
, right
))
3035 if (best_operator
== null) {
3037 if (primitives_only
)
3043 best_operator
= po
.ResolveBetterOperator (ec
, best_operator
);
3045 if (best_operator
== null) {
3046 Report
.Error (34, loc
, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
3047 OperName (oper
), left
.GetSignatureForError (), right
.GetSignatureForError ());
3054 if (best_operator
== null)
3057 Expression expr
= best_operator
.ConvertResult (ec
, this);
3058 if (enum_type
== null)
3062 // HACK: required by enum_conversion
3064 expr
.Type
= enum_type
;
3065 return EmptyCast
.Create (expr
, enum_type
);
3069 // Performs user-operator overloading
3071 protected virtual Expression
ResolveUserOperator (EmitContext ec
, Type l
, Type r
)
3074 if (oper
== Operator
.LogicalAnd
)
3075 user_oper
= Operator
.BitwiseAnd
;
3076 else if (oper
== Operator
.LogicalOr
)
3077 user_oper
= Operator
.BitwiseOr
;
3081 string op
= GetOperatorMetadataName (user_oper
);
3083 MethodGroupExpr left_operators
= MemberLookup (ec
.ContainerType
, l
, op
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
3084 MethodGroupExpr right_operators
= null;
3086 if (!TypeManager
.IsEqual (r
, l
)) {
3087 right_operators
= MemberLookup (ec
.ContainerType
, r
, op
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
3088 if (right_operators
== null && left_operators
== null)
3090 } else if (left_operators
== null) {
3094 Arguments args
= new Arguments (2);
3095 Argument larg
= new Argument (left
);
3097 Argument rarg
= new Argument (right
);
3100 MethodGroupExpr union
;
3103 // User-defined operator implementations always take precedence
3104 // over predefined operator implementations
3106 if (left_operators
!= null && right_operators
!= null) {
3107 if (IsPredefinedUserOperator (l
, user_oper
)) {
3108 union
= right_operators
.OverloadResolve (ec
, ref args
, true, loc
);
3110 union
= left_operators
;
3111 } else if (IsPredefinedUserOperator (r
, user_oper
)) {
3112 union
= left_operators
.OverloadResolve (ec
, ref args
, true, loc
);
3114 union
= right_operators
;
3116 union
= MethodGroupExpr
.MakeUnionSet (left_operators
, right_operators
, loc
);
3118 } else if (left_operators
!= null) {
3119 union
= left_operators
;
3121 union
= right_operators
;
3124 union
= union
.OverloadResolve (ec
, ref args
, true, loc
);
3128 Expression oper_expr
;
3130 // TODO: CreateExpressionTree is allocated every time
3131 if (user_oper
!= oper
) {
3132 oper_expr
= new ConditionalLogicalOperator (union
, args
, CreateExpressionTree
,
3133 oper
== Operator
.LogicalAnd
, loc
).Resolve (ec
);
3135 oper_expr
= new UserOperatorCall (union
, args
, CreateExpressionTree
, loc
);
3138 // This is used to check if a test 'x == null' can be optimized to a reference equals,
3139 // and not invoke user operator
3141 if ((oper
& Operator
.EqualityMask
) != 0) {
3142 if ((left
is NullLiteral
&& IsBuildInEqualityOperator (r
)) ||
3143 (right
is NullLiteral
&& IsBuildInEqualityOperator (l
))) {
3144 type
= TypeManager
.bool_type
;
3145 if (left
is NullLiteral
|| right
is NullLiteral
)
3146 oper_expr
= ReducedExpression
.Create (this, oper_expr
).Resolve (ec
);
3147 } else if (l
!= r
) {
3148 MethodInfo mi
= (MethodInfo
) union
;
3151 // Two System.Delegate(s) are never equal
3153 if (mi
.DeclaringType
== TypeManager
.multicast_delegate_type
)
3164 public override TypeExpr
ResolveAsTypeTerminal (IResolveContext ec
, bool silent
)
3169 private void CheckUselessComparison (Constant c
, Type type
)
3171 if (c
== null || !IsTypeIntegral (type
)
3172 || c
is StringConstant
3173 || c
is BoolConstant
3174 || c
is FloatConstant
3175 || c
is DoubleConstant
3176 || c
is DecimalConstant
3182 if (c
is ULongConstant
) {
3183 ulong uvalue
= ((ULongConstant
) c
).Value
;
3184 if (uvalue
> long.MaxValue
) {
3185 if (type
== TypeManager
.byte_type
||
3186 type
== TypeManager
.sbyte_type
||
3187 type
== TypeManager
.short_type
||
3188 type
== TypeManager
.ushort_type
||
3189 type
== TypeManager
.int32_type
||
3190 type
== TypeManager
.uint32_type
||
3191 type
== TypeManager
.int64_type
||
3192 type
== TypeManager
.char_type
)
3193 WarnUselessComparison (type
);
3196 value = (long) uvalue
;
3198 else if (c
is ByteConstant
)
3199 value = ((ByteConstant
) c
).Value
;
3200 else if (c
is SByteConstant
)
3201 value = ((SByteConstant
) c
).Value
;
3202 else if (c
is ShortConstant
)
3203 value = ((ShortConstant
) c
).Value
;
3204 else if (c
is UShortConstant
)
3205 value = ((UShortConstant
) c
).Value
;
3206 else if (c
is IntConstant
)
3207 value = ((IntConstant
) c
).Value
;
3208 else if (c
is UIntConstant
)
3209 value = ((UIntConstant
) c
).Value
;
3210 else if (c
is LongConstant
)
3211 value = ((LongConstant
) c
).Value
;
3212 else if (c
is CharConstant
)
3213 value = ((CharConstant
)c
).Value
;
3218 if (IsValueOutOfRange (value, type
))
3219 WarnUselessComparison (type
);
3222 static bool IsValueOutOfRange (long value, Type type
)
3224 if (IsTypeUnsigned (type
) && value < 0)
3226 return type
== TypeManager
.sbyte_type
&& (value >= 0x80 || value < -0x80) ||
3227 type
== TypeManager
.byte_type
&& value >= 0x100 ||
3228 type
== TypeManager
.short_type
&& (value >= 0x8000 || value < -0x8000) ||
3229 type
== TypeManager
.ushort_type
&& value >= 0x10000 ||
3230 type
== TypeManager
.int32_type
&& (value >= 0x80000000 || value < -0x80000000) ||
3231 type
== TypeManager
.uint32_type
&& value >= 0x100000000;
3234 static bool IsBuildInEqualityOperator (Type t
)
3236 return t
== TypeManager
.object_type
|| t
== TypeManager
.string_type
||
3237 t
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (t
);
3240 static bool IsPredefinedUserOperator (Type t
, Operator op
)
3243 // Some predefined types have user operators
3245 return (op
& Operator
.EqualityMask
) != 0 && (t
== TypeManager
.string_type
|| t
== TypeManager
.decimal_type
);
3248 private static bool IsTypeIntegral (Type type
)
3250 return type
== TypeManager
.uint64_type
||
3251 type
== TypeManager
.int64_type
||
3252 type
== TypeManager
.uint32_type
||
3253 type
== TypeManager
.int32_type
||
3254 type
== TypeManager
.ushort_type
||
3255 type
== TypeManager
.short_type
||
3256 type
== TypeManager
.sbyte_type
||
3257 type
== TypeManager
.byte_type
||
3258 type
== TypeManager
.char_type
;
3261 private static bool IsTypeUnsigned (Type type
)
3263 return type
== TypeManager
.uint64_type
||
3264 type
== TypeManager
.uint32_type
||
3265 type
== TypeManager
.ushort_type
||
3266 type
== TypeManager
.byte_type
||
3267 type
== TypeManager
.char_type
;
3270 private void WarnUselessComparison (Type type
)
3272 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}'",
3273 TypeManager
.CSharpName (type
));
3277 /// EmitBranchable is called from Statement.EmitBoolExpression in the
3278 /// context of a conditional bool expression. This function will return
3279 /// false if it is was possible to use EmitBranchable, or true if it was.
3281 /// The expression's code is generated, and we will generate a branch to `target'
3282 /// if the resulting expression value is equal to isTrue
3284 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
3286 ILGenerator ig
= ec
.ig
;
3289 // This is more complicated than it looks, but its just to avoid
3290 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
3291 // but on top of that we want for == and != to use a special path
3292 // if we are comparing against null
3294 if ((oper
== Operator
.Equality
|| oper
== Operator
.Inequality
) && (left
is Constant
|| right
is Constant
)) {
3295 bool my_on_true
= oper
== Operator
.Inequality
? on_true
: !on_true
;
3298 // put the constant on the rhs, for simplicity
3300 if (left
is Constant
) {
3301 Expression swap
= right
;
3306 if (((Constant
) right
).IsZeroInteger
) {
3307 left
.EmitBranchable (ec
, target
, my_on_true
);
3310 if (right
.Type
== TypeManager
.bool_type
) {
3311 // right is a boolean, and it's not 'false' => it is 'true'
3312 left
.EmitBranchable (ec
, target
, !my_on_true
);
3316 } else if (oper
== Operator
.LogicalAnd
) {
3319 Label tests_end
= ig
.DefineLabel ();
3321 left
.EmitBranchable (ec
, tests_end
, false);
3322 right
.EmitBranchable (ec
, target
, true);
3323 ig
.MarkLabel (tests_end
);
3326 // This optimizes code like this
3327 // if (true && i > 4)
3329 if (!(left
is Constant
))
3330 left
.EmitBranchable (ec
, target
, false);
3332 if (!(right
is Constant
))
3333 right
.EmitBranchable (ec
, target
, false);
3338 } else if (oper
== Operator
.LogicalOr
){
3340 left
.EmitBranchable (ec
, target
, true);
3341 right
.EmitBranchable (ec
, target
, true);
3344 Label tests_end
= ig
.DefineLabel ();
3345 left
.EmitBranchable (ec
, tests_end
, true);
3346 right
.EmitBranchable (ec
, target
, false);
3347 ig
.MarkLabel (tests_end
);
3352 } else if (!(oper
== Operator
.LessThan
|| oper
== Operator
.GreaterThan
||
3353 oper
== Operator
.LessThanOrEqual
|| oper
== Operator
.GreaterThanOrEqual
||
3354 oper
== Operator
.Equality
|| oper
== Operator
.Inequality
)) {
3355 base.EmitBranchable (ec
, target
, on_true
);
3363 bool is_float
= IsFloat (t
);
3364 bool is_unsigned
= is_float
|| IsUnsigned (t
);
3367 case Operator
.Equality
:
3369 ig
.Emit (OpCodes
.Beq
, target
);
3371 ig
.Emit (OpCodes
.Bne_Un
, target
);
3374 case Operator
.Inequality
:
3376 ig
.Emit (OpCodes
.Bne_Un
, target
);
3378 ig
.Emit (OpCodes
.Beq
, target
);
3381 case Operator
.LessThan
:
3383 if (is_unsigned
&& !is_float
)
3384 ig
.Emit (OpCodes
.Blt_Un
, target
);
3386 ig
.Emit (OpCodes
.Blt
, target
);
3389 ig
.Emit (OpCodes
.Bge_Un
, target
);
3391 ig
.Emit (OpCodes
.Bge
, target
);
3394 case Operator
.GreaterThan
:
3396 if (is_unsigned
&& !is_float
)
3397 ig
.Emit (OpCodes
.Bgt_Un
, target
);
3399 ig
.Emit (OpCodes
.Bgt
, target
);
3402 ig
.Emit (OpCodes
.Ble_Un
, target
);
3404 ig
.Emit (OpCodes
.Ble
, target
);
3407 case Operator
.LessThanOrEqual
:
3409 if (is_unsigned
&& !is_float
)
3410 ig
.Emit (OpCodes
.Ble_Un
, target
);
3412 ig
.Emit (OpCodes
.Ble
, target
);
3415 ig
.Emit (OpCodes
.Bgt_Un
, target
);
3417 ig
.Emit (OpCodes
.Bgt
, target
);
3421 case Operator
.GreaterThanOrEqual
:
3423 if (is_unsigned
&& !is_float
)
3424 ig
.Emit (OpCodes
.Bge_Un
, target
);
3426 ig
.Emit (OpCodes
.Bge
, target
);
3429 ig
.Emit (OpCodes
.Blt_Un
, target
);
3431 ig
.Emit (OpCodes
.Blt
, target
);
3434 throw new InternalErrorException (oper
.ToString ());
3438 public override void Emit (EmitContext ec
)
3440 EmitOperator (ec
, left
.Type
);
3443 protected virtual void EmitOperator (EmitContext ec
, Type l
)
3445 ILGenerator ig
= ec
.ig
;
3448 // Handle short-circuit operators differently
3451 if ((oper
& Operator
.LogicalMask
) != 0) {
3452 Label load_result
= ig
.DefineLabel ();
3453 Label end
= ig
.DefineLabel ();
3455 bool is_or
= oper
== Operator
.LogicalOr
;
3456 left
.EmitBranchable (ec
, load_result
, is_or
);
3458 ig
.Emit (OpCodes
.Br_S
, end
);
3460 ig
.MarkLabel (load_result
);
3461 ig
.Emit (is_or
? OpCodes
.Ldc_I4_1
: OpCodes
.Ldc_I4_0
);
3469 // Optimize zero-based operations
3471 // TODO: Implement more optimizations, but it should probably go to PredefinedOperators
3473 if ((oper
& Operator
.ShiftMask
) != 0 || oper
== Operator
.Addition
|| oper
== Operator
.Subtraction
) {
3474 Constant rc
= right
as Constant
;
3475 if (rc
!= null && rc
.IsDefaultValue
) {
3481 EmitOperatorOpcode (ec
, oper
, l
);
3484 // Nullable enum could require underlying type cast and we cannot simply wrap binary
3485 // expression because that would wrap lifted binary operation
3487 if (enum_conversion
!= null)
3488 enum_conversion
.Emit (ec
);
3491 public override void EmitSideEffect (EmitContext ec
)
3493 if ((oper
& Operator
.LogicalMask
) != 0 ||
3494 (ec
.CheckState
&& (oper
== Operator
.Multiply
|| oper
== Operator
.Addition
|| oper
== Operator
.Subtraction
))) {
3495 base.EmitSideEffect (ec
);
3497 left
.EmitSideEffect (ec
);
3498 right
.EmitSideEffect (ec
);
3502 protected override void CloneTo (CloneContext clonectx
, Expression t
)
3504 Binary target
= (Binary
) t
;
3506 target
.left
= left
.Clone (clonectx
);
3507 target
.right
= right
.Clone (clonectx
);
3510 public Expression
CreateCallSiteBinder (EmitContext ec
, Arguments args
)
3512 Arguments binder_args
= new Arguments (4);
3514 MemberAccess sle
= new MemberAccess (new MemberAccess (
3515 new QualifiedAliasMember (QualifiedAliasMember
.GlobalAlias
, "System", loc
), "Linq", loc
), "Expressions", loc
);
3517 MemberAccess binder
= DynamicExpressionStatement
.GetBinderNamespace (loc
);
3519 binder_args
.Add (new Argument (new MemberAccess (new MemberAccess (sle
, "ExpressionType", loc
), GetOperatorExpressionTypeName (), loc
)));
3520 binder_args
.Add (new Argument (new BoolLiteral (ec
.CheckState
, loc
)));
3522 bool member_access
= left
is DynamicMemberBinder
|| right
is DynamicMemberBinder
;
3523 binder_args
.Add (new Argument (new BoolLiteral (member_access
, loc
)));
3524 binder_args
.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args
.CreateDynamicBinderArguments (), loc
)));
3526 return new New (new MemberAccess (binder
, "CSharpBinaryOperationBinder", loc
), binder_args
, loc
);
3529 public override Expression
CreateExpressionTree (EmitContext ec
)
3531 return CreateExpressionTree (ec
, null);
3534 Expression
CreateExpressionTree (EmitContext ec
, MethodGroupExpr method
)
3537 bool lift_arg
= false;
3540 case Operator
.Addition
:
3541 if (method
== null && ec
.CheckState
&& !IsFloat (type
))
3542 method_name
= "AddChecked";
3544 method_name
= "Add";
3546 case Operator
.BitwiseAnd
:
3547 method_name
= "And";
3549 case Operator
.BitwiseOr
:
3552 case Operator
.Division
:
3553 method_name
= "Divide";
3555 case Operator
.Equality
:
3556 method_name
= "Equal";
3559 case Operator
.ExclusiveOr
:
3560 method_name
= "ExclusiveOr";
3562 case Operator
.GreaterThan
:
3563 method_name
= "GreaterThan";
3566 case Operator
.GreaterThanOrEqual
:
3567 method_name
= "GreaterThanOrEqual";
3570 case Operator
.Inequality
:
3571 method_name
= "NotEqual";
3574 case Operator
.LeftShift
:
3575 method_name
= "LeftShift";
3577 case Operator
.LessThan
:
3578 method_name
= "LessThan";
3581 case Operator
.LessThanOrEqual
:
3582 method_name
= "LessThanOrEqual";
3585 case Operator
.LogicalAnd
:
3586 method_name
= "AndAlso";
3588 case Operator
.LogicalOr
:
3589 method_name
= "OrElse";
3591 case Operator
.Modulus
:
3592 method_name
= "Modulo";
3594 case Operator
.Multiply
:
3595 if (method
== null && ec
.CheckState
&& !IsFloat (type
))
3596 method_name
= "MultiplyChecked";
3598 method_name
= "Multiply";
3600 case Operator
.RightShift
:
3601 method_name
= "RightShift";
3603 case Operator
.Subtraction
:
3604 if (method
== null && ec
.CheckState
&& !IsFloat (type
))
3605 method_name
= "SubtractChecked";
3607 method_name
= "Subtract";
3611 throw new InternalErrorException ("Unknown expression tree binary operator " + oper
);
3614 Arguments args
= new Arguments (2);
3615 args
.Add (new Argument (left
.CreateExpressionTree (ec
)));
3616 args
.Add (new Argument (right
.CreateExpressionTree (ec
)));
3617 if (method
!= null) {
3619 args
.Add (new Argument (new BoolConstant (false, loc
)));
3621 args
.Add (new Argument (method
.CreateExpressionTree (ec
)));
3624 return CreateExpressionFactoryCall (method_name
, args
);
3629 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3630 // b, c, d... may be strings or objects.
3632 public class StringConcat
: Expression
{
3633 Arguments arguments
;
3635 public StringConcat (EmitContext ec
, Location loc
, Expression left
, Expression right
)
3638 type
= TypeManager
.string_type
;
3639 eclass
= ExprClass
.Value
;
3641 arguments
= new Arguments (2);
3646 public override Expression
CreateExpressionTree (EmitContext ec
)
3648 Argument arg
= arguments
[0];
3649 return CreateExpressionAddCall (ec
, arg
, arg
.CreateExpressionTree (ec
), 1);
3653 // Creates nested calls tree from an array of arguments used for IL emit
3655 Expression
CreateExpressionAddCall (EmitContext ec
, Argument left
, Expression left_etree
, int pos
)
3657 Arguments concat_args
= new Arguments (2);
3658 Arguments add_args
= new Arguments (3);
3660 concat_args
.Add (left
);
3661 add_args
.Add (new Argument (left_etree
));
3663 concat_args
.Add (arguments
[pos
]);
3664 add_args
.Add (new Argument (arguments
[pos
].CreateExpressionTree (ec
)));
3666 MethodGroupExpr method
= CreateConcatMemberExpression ().Resolve (ec
) as MethodGroupExpr
;
3670 method
= method
.OverloadResolve (ec
, ref concat_args
, false, loc
);
3674 add_args
.Add (new Argument (method
.CreateExpressionTree (ec
)));
3676 Expression expr
= CreateExpressionFactoryCall ("Add", add_args
);
3677 if (++pos
== arguments
.Count
)
3680 left
= new Argument (new EmptyExpression (((MethodInfo
)method
).ReturnType
));
3681 return CreateExpressionAddCall (ec
, left
, expr
, pos
);
3684 public override Expression
DoResolve (EmitContext ec
)
3689 public void Append (EmitContext ec
, Expression operand
)
3694 StringConstant sc
= operand
as StringConstant
;
3696 if (arguments
.Count
!= 0) {
3697 Argument last_argument
= arguments
[arguments
.Count
- 1];
3698 StringConstant last_expr_constant
= last_argument
.Expr
as StringConstant
;
3699 if (last_expr_constant
!= null) {
3700 last_argument
.Expr
= new StringConstant (
3701 last_expr_constant
.Value
+ sc
.Value
, sc
.Location
);
3707 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3709 StringConcat concat_oper
= operand
as StringConcat
;
3710 if (concat_oper
!= null) {
3711 arguments
.AddRange (concat_oper
.arguments
);
3716 arguments
.Add (new Argument (operand
));
3719 Expression
CreateConcatMemberExpression ()
3721 return new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc
), "String", loc
), "Concat", loc
);
3724 public override void Emit (EmitContext ec
)
3726 Expression concat
= new Invocation (CreateConcatMemberExpression (), arguments
, true);
3727 concat
= concat
.Resolve (ec
);
3732 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
3734 arguments
.MutateHoistedGenericType (storey
);
3739 // User-defined conditional logical operator
3741 public class ConditionalLogicalOperator
: UserOperatorCall
{
3742 readonly bool is_and
;
3745 public ConditionalLogicalOperator (MethodGroupExpr oper_method
, Arguments arguments
,
3746 ExpressionTreeExpression expr_tree
, bool is_and
, Location loc
)
3747 : base (oper_method
, arguments
, expr_tree
, loc
)
3749 this.is_and
= is_and
;
3752 public override Expression
DoResolve (EmitContext ec
)
3754 MethodInfo method
= (MethodInfo
)mg
;
3755 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
3756 AParametersCollection pd
= TypeManager
.GetParameterData (method
);
3757 if (!TypeManager
.IsEqual (type
, type
) || !TypeManager
.IsEqual (type
, pd
.Types
[0]) || !TypeManager
.IsEqual (type
, pd
.Types
[1])) {
3758 Report
.Error (217, loc
,
3759 "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",
3760 TypeManager
.CSharpSignature (method
));
3764 Expression left_dup
= new EmptyExpression (type
);
3765 Expression op_true
= GetOperatorTrue (ec
, left_dup
, loc
);
3766 Expression op_false
= GetOperatorFalse (ec
, left_dup
, loc
);
3767 if (op_true
== null || op_false
== null) {
3768 Report
.Error (218, loc
,
3769 "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
3770 TypeManager
.CSharpName (type
), TypeManager
.CSharpSignature (method
));
3774 oper
= is_and
? op_false
: op_true
;
3775 eclass
= ExprClass
.Value
;
3779 public override void Emit (EmitContext ec
)
3781 ILGenerator ig
= ec
.ig
;
3782 Label end_target
= ig
.DefineLabel ();
3785 // Emit and duplicate left argument
3787 arguments
[0].Expr
.Emit (ec
);
3788 ig
.Emit (OpCodes
.Dup
);
3789 arguments
.RemoveAt (0);
3791 oper
.EmitBranchable (ec
, end_target
, true);
3793 ig
.MarkLabel (end_target
);
3797 public class PointerArithmetic
: Expression
{
3798 Expression left
, right
;
3802 // We assume that `l' is always a pointer
3804 public PointerArithmetic (Binary
.Operator op
, Expression l
, Expression r
, Type t
, Location loc
)
3813 public override Expression
CreateExpressionTree (EmitContext ec
)
3815 Error_PointerInsideExpressionTree ();
3819 public override Expression
DoResolve (EmitContext ec
)
3821 eclass
= ExprClass
.Variable
;
3823 if (left
.Type
== TypeManager
.void_ptr_type
) {
3824 Error (242, "The operation in question is undefined on void pointers");
3831 public override void Emit (EmitContext ec
)
3833 Type op_type
= left
.Type
;
3834 ILGenerator ig
= ec
.ig
;
3836 // It must be either array or fixed buffer
3838 if (TypeManager
.HasElementType (op_type
)) {
3839 element
= TypeManager
.GetElementType (op_type
);
3841 FieldExpr fe
= left
as FieldExpr
;
3843 element
= AttributeTester
.GetFixedBuffer (fe
.FieldInfo
).ElementType
;
3848 int size
= GetTypeSize (element
);
3849 Type rtype
= right
.Type
;
3851 if ((op
& Binary
.Operator
.SubtractionMask
) != 0 && rtype
.IsPointer
){
3853 // handle (pointer - pointer)
3857 ig
.Emit (OpCodes
.Sub
);
3861 ig
.Emit (OpCodes
.Sizeof
, element
);
3863 IntLiteral
.EmitInt (ig
, size
);
3864 ig
.Emit (OpCodes
.Div
);
3866 ig
.Emit (OpCodes
.Conv_I8
);
3869 // handle + and - on (pointer op int)
3871 Constant left_const
= left
as Constant
;
3872 if (left_const
!= null) {
3874 // Optimize ((T*)null) pointer operations
3876 if (left_const
.IsDefaultValue
) {
3877 left
= EmptyExpression
.Null
;
3885 Constant right_const
= right
as Constant
;
3886 if (right_const
!= null) {
3888 // Optimize 0-based arithmetic
3890 if (right_const
.IsDefaultValue
)
3894 right
= ConstantFold
.BinaryFold (ec
, Binary
.Operator
.Multiply
, new IntConstant (size
, right
.Location
), right_const
, loc
);
3898 ig
.Emit (OpCodes
.Sizeof
, element
);
3899 right
= EmptyExpression
.Null
;
3904 if (rtype
== TypeManager
.sbyte_type
|| rtype
== TypeManager
.byte_type
||
3905 rtype
== TypeManager
.short_type
|| rtype
== TypeManager
.ushort_type
) {
3906 ig
.Emit (OpCodes
.Conv_I
);
3907 } else if (rtype
== TypeManager
.uint32_type
) {
3908 ig
.Emit (OpCodes
.Conv_U
);
3911 if (right_const
== null && size
!= 1){
3913 ig
.Emit (OpCodes
.Sizeof
, element
);
3915 IntLiteral
.EmitInt (ig
, size
);
3916 if (rtype
== TypeManager
.int64_type
|| rtype
== TypeManager
.uint64_type
)
3917 ig
.Emit (OpCodes
.Conv_I8
);
3919 Binary
.EmitOperatorOpcode (ec
, Binary
.Operator
.Multiply
, rtype
);
3922 if (left_const
== null) {
3923 if (rtype
== TypeManager
.int64_type
)
3924 ig
.Emit (OpCodes
.Conv_I
);
3925 else if (rtype
== TypeManager
.uint64_type
)
3926 ig
.Emit (OpCodes
.Conv_U
);
3928 Binary
.EmitOperatorOpcode (ec
, op
, op_type
);
3935 /// Implements the ternary conditional operator (?:)
3937 public class Conditional
: Expression
{
3938 Expression expr
, true_expr
, false_expr
;
3940 public Conditional (Expression expr
, Expression true_expr
, Expression false_expr
)
3943 this.true_expr
= true_expr
;
3944 this.false_expr
= false_expr
;
3945 this.loc
= expr
.Location
;
3948 public Expression Expr
{
3954 public Expression TrueExpr
{
3960 public Expression FalseExpr
{
3966 public override Expression
CreateExpressionTree (EmitContext ec
)
3968 Arguments args
= new Arguments (3);
3969 args
.Add (new Argument (expr
.CreateExpressionTree (ec
)));
3970 args
.Add (new Argument (true_expr
.CreateExpressionTree (ec
)));
3971 args
.Add (new Argument (false_expr
.CreateExpressionTree (ec
)));
3972 return CreateExpressionFactoryCall ("Condition", args
);
3975 public override Expression
DoResolve (EmitContext ec
)
3977 expr
= Expression
.ResolveBoolean (ec
, expr
, loc
);
3979 Assign ass
= expr
as Assign
;
3980 if (ass
!= null && ass
.Source
is Constant
) {
3981 Report
.Warning (665, 3, loc
, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
3984 true_expr
= true_expr
.Resolve (ec
);
3985 false_expr
= false_expr
.Resolve (ec
);
3987 if (true_expr
== null || false_expr
== null || expr
== null)
3990 eclass
= ExprClass
.Value
;
3991 Type true_type
= true_expr
.Type
;
3992 Type false_type
= false_expr
.Type
;
3996 // First, if an implicit conversion exists from true_expr
3997 // to false_expr, then the result type is of type false_expr.Type
3999 if (!TypeManager
.IsEqual (true_type
, false_type
)) {
4000 Expression conv
= Convert
.ImplicitConversion (ec
, true_expr
, false_type
, loc
);
4003 // Check if both can convert implicitl to each other's type
4005 if (Convert
.ImplicitConversion (ec
, false_expr
, true_type
, loc
) != null) {
4007 "Can not compute type of conditional expression " +
4008 "as `" + TypeManager
.CSharpName (true_expr
.Type
) +
4009 "' and `" + TypeManager
.CSharpName (false_expr
.Type
) +
4010 "' convert implicitly to each other");
4015 } else if ((conv
= Convert
.ImplicitConversion (ec
, false_expr
, true_type
, loc
)) != null) {
4018 Report
.Error (173, loc
,
4019 "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
4020 true_expr
.GetSignatureForError (), false_expr
.GetSignatureForError ());
4025 // Dead code optimalization
4026 Constant c
= expr
as Constant
;
4028 bool is_false
= c
.IsDefaultValue
;
4029 Report
.Warning (429, 4, is_false
? true_expr
.Location
: false_expr
.Location
, "Unreachable expression code detected");
4030 return ReducedExpression
.Create (is_false
? false_expr
: true_expr
, this).Resolve (ec
);
4036 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
4038 expr
.MutateHoistedGenericType (storey
);
4039 true_expr
.MutateHoistedGenericType (storey
);
4040 false_expr
.MutateHoistedGenericType (storey
);
4041 type
= storey
.MutateType (type
);
4044 public override TypeExpr
ResolveAsTypeTerminal (IResolveContext ec
, bool silent
)
4049 public override void Emit (EmitContext ec
)
4051 ILGenerator ig
= ec
.ig
;
4052 Label false_target
= ig
.DefineLabel ();
4053 Label end_target
= ig
.DefineLabel ();
4055 expr
.EmitBranchable (ec
, false_target
, false);
4056 true_expr
.Emit (ec
);
4058 if (type
.IsInterface
) {
4059 LocalBuilder temp
= ec
.GetTemporaryLocal (type
);
4060 ig
.Emit (OpCodes
.Stloc
, temp
);
4061 ig
.Emit (OpCodes
.Ldloc
, temp
);
4062 ec
.FreeTemporaryLocal (temp
, type
);
4065 ig
.Emit (OpCodes
.Br
, end_target
);
4066 ig
.MarkLabel (false_target
);
4067 false_expr
.Emit (ec
);
4068 ig
.MarkLabel (end_target
);
4071 protected override void CloneTo (CloneContext clonectx
, Expression t
)
4073 Conditional target
= (Conditional
) t
;
4075 target
.expr
= expr
.Clone (clonectx
);
4076 target
.true_expr
= true_expr
.Clone (clonectx
);
4077 target
.false_expr
= false_expr
.Clone (clonectx
);
4081 public abstract class VariableReference
: Expression
, IAssignMethod
, IMemoryLocation
, IVariableReference
{
4082 LocalTemporary temp
;
4085 public abstract HoistedVariable
GetHoistedVariable (EmitContext ec
);
4086 public abstract bool IsFixed { get; }
4087 public abstract bool IsRef { get; }
4088 public abstract string Name { get; }
4089 public abstract void SetHasAddressTaken ();
4092 // Variable IL data, it has to be protected to encapsulate hoisted variables
4094 protected abstract ILocalVariable Variable { get; }
4097 // Variable flow-analysis data
4099 public abstract VariableInfo VariableInfo { get; }
4102 public void AddressOf (EmitContext ec
, AddressOp mode
)
4104 HoistedVariable hv
= GetHoistedVariable (ec
);
4106 hv
.AddressOf (ec
, mode
);
4110 Variable
.EmitAddressOf (ec
);
4113 public override void Emit (EmitContext ec
)
4118 public override void EmitSideEffect (EmitContext ec
)
4124 // This method is used by parameters that are references, that are
4125 // being passed as references: we only want to pass the pointer (that
4126 // is already stored in the parameter, not the address of the pointer,
4127 // and not the value of the variable).
4129 public void EmitLoad (EmitContext ec
)
4134 public void Emit (EmitContext ec
, bool leave_copy
)
4136 Report
.Debug (64, "VARIABLE EMIT", this, Variable
, type
, IsRef
, loc
);
4138 HoistedVariable hv
= GetHoistedVariable (ec
);
4140 hv
.Emit (ec
, leave_copy
);
4148 // If we are a reference, we loaded on the stack a pointer
4149 // Now lets load the real value
4151 LoadFromPtr (ec
.ig
, type
);
4155 ec
.ig
.Emit (OpCodes
.Dup
);
4158 temp
= new LocalTemporary (Type
);
4164 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
,
4165 bool prepare_for_load
)
4167 HoistedVariable hv
= GetHoistedVariable (ec
);
4169 hv
.EmitAssign (ec
, source
, leave_copy
, prepare_for_load
);
4173 New n_source
= source
as New
;
4174 if (n_source
!= null) {
4175 if (!n_source
.Emit (ec
, this)) {
4188 ec
.ig
.Emit (OpCodes
.Dup
);
4190 temp
= new LocalTemporary (Type
);
4196 StoreFromPtr (ec
.ig
, type
);
4198 Variable
.EmitAssign (ec
);
4206 public bool IsHoisted
{
4207 get { return GetHoistedVariable (null) != null; }
4210 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
4212 type
= storey
.MutateType (type
);
4219 public class LocalVariableReference
: VariableReference
{
4220 readonly string name
;
4222 public LocalInfo local_info
;
4224 bool resolved
; // TODO: merge with eclass
4226 public LocalVariableReference (Block block
, string name
, Location l
)
4234 // Setting `is_readonly' to false will allow you to create a writable
4235 // reference to a read-only variable. This is used by foreach and using.
4237 public LocalVariableReference (Block block
, string name
, Location l
,
4238 LocalInfo local_info
, bool is_readonly
)
4239 : this (block
, name
, l
)
4241 this.local_info
= local_info
;
4242 this.is_readonly
= is_readonly
;
4245 public override VariableInfo VariableInfo
{
4246 get { return local_info.VariableInfo; }
4249 public override HoistedVariable
GetHoistedVariable (EmitContext ec
)
4251 return local_info
.HoistedVariableReference
;
4255 // A local variable is always fixed
4257 public override bool IsFixed
{
4258 get { return true; }
4261 public override bool IsRef
{
4262 get { return false; }
4265 public bool IsReadOnly
{
4266 get { return is_readonly; }
4269 public override string Name
{
4270 get { return name; }
4273 public bool VerifyAssigned (EmitContext ec
)
4275 VariableInfo variable_info
= local_info
.VariableInfo
;
4276 return variable_info
== null || variable_info
.IsAssigned (ec
, loc
);
4279 void ResolveLocalInfo ()
4281 if (local_info
== null) {
4282 local_info
= Block
.GetLocalInfo (Name
);
4283 type
= local_info
.VariableType
;
4284 is_readonly
= local_info
.ReadOnly
;
4288 public override void SetHasAddressTaken ()
4290 local_info
.AddressTaken
= true;
4293 public override Expression
CreateExpressionTree (EmitContext ec
)
4295 HoistedVariable hv
= GetHoistedVariable (ec
);
4297 return hv
.CreateExpressionTree (ec
);
4299 Arguments arg
= new Arguments (1);
4300 arg
.Add (new Argument (this));
4301 return CreateExpressionFactoryCall ("Constant", arg
);
4304 Expression
DoResolveBase (EmitContext ec
)
4306 type
= local_info
.VariableType
;
4308 Expression e
= Block
.GetConstantExpression (Name
);
4310 return e
.Resolve (ec
);
4312 VerifyAssigned (ec
);
4315 // If we are referencing a variable from the external block
4316 // flag it for capturing
4318 if (ec
.MustCaptureVariable (local_info
)) {
4319 if (local_info
.AddressTaken
)
4320 AnonymousMethodExpression
.Error_AddressOfCapturedVar (this, loc
);
4322 if (ec
.IsVariableCapturingRequired
) {
4323 AnonymousMethodStorey storey
= local_info
.Block
.Explicit
.CreateAnonymousMethodStorey (ec
);
4324 storey
.CaptureLocalVariable (ec
, local_info
);
4328 resolved
|= ec
.DoFlowAnalysis
;
4329 eclass
= ExprClass
.Variable
;
4333 public override Expression
DoResolve (EmitContext ec
)
4338 ResolveLocalInfo ();
4339 local_info
.Used
= true;
4341 if (type
== null && local_info
.Type
is VarExpr
) {
4342 local_info
.VariableType
= TypeManager
.object_type
;
4343 Error_VariableIsUsedBeforeItIsDeclared (Name
);
4347 return DoResolveBase (ec
);
4350 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
4352 ResolveLocalInfo ();
4355 if (right_side
== EmptyExpression
.OutAccess
)
4356 local_info
.Used
= true;
4358 // Infer implicitly typed local variable
4360 VarExpr ve
= local_info
.Type
as VarExpr
;
4362 if (!ve
.InferType (ec
, right_side
))
4364 type
= local_info
.VariableType
= ve
.Type
;
4371 if (right_side
== EmptyExpression
.OutAccess
) {
4372 code
= 1657; msg
= "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
4373 } else if (right_side
== EmptyExpression
.LValueMemberAccess
) {
4374 code
= 1654; msg
= "Cannot assign to members of `{0}' because it is a `{1}'";
4375 } else if (right_side
== EmptyExpression
.LValueMemberOutAccess
) {
4376 code
= 1655; msg
= "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
4377 } else if (right_side
== EmptyExpression
.UnaryAddress
) {
4378 code
= 459; msg
= "Cannot take the address of {1} `{0}'";
4380 code
= 1656; msg
= "Cannot assign to `{0}' because it is a `{1}'";
4382 Report
.Error (code
, loc
, msg
, Name
, local_info
.GetReadOnlyContext ());
4383 } else if (VariableInfo
!= null) {
4384 VariableInfo
.SetAssigned (ec
);
4387 return DoResolveBase (ec
);
4390 public override int GetHashCode ()
4392 return Name
.GetHashCode ();
4395 public override bool Equals (object obj
)
4397 LocalVariableReference lvr
= obj
as LocalVariableReference
;
4401 return Name
== lvr
.Name
&& Block
== lvr
.Block
;
4404 protected override ILocalVariable Variable
{
4405 get { return local_info; }
4408 public override string ToString ()
4410 return String
.Format ("{0} ({1}:{2})", GetType (), Name
, loc
);
4413 protected override void CloneTo (CloneContext clonectx
, Expression t
)
4415 LocalVariableReference target
= (LocalVariableReference
) t
;
4417 target
.Block
= clonectx
.LookupBlock (Block
);
4418 if (local_info
!= null)
4419 target
.local_info
= clonectx
.LookupVariable (local_info
);
4424 /// This represents a reference to a parameter in the intermediate
4427 public class ParameterReference
: VariableReference
{
4428 readonly ToplevelParameterInfo pi
;
4430 public ParameterReference (ToplevelParameterInfo pi
, Location loc
)
4436 public override bool IsRef
{
4437 get { return (pi.Parameter.ModFlags & Parameter.Modifier.ISBYREF) != 0; }
4440 bool HasOutModifier
{
4441 get { return pi.Parameter.ModFlags == Parameter.Modifier.OUT; }
4444 public override HoistedVariable
GetHoistedVariable (EmitContext ec
)
4446 return pi
.Parameter
.HoistedVariableReference
;
4450 // A ref or out parameter is classified as a moveable variable, even
4451 // if the argument given for the parameter is a fixed variable
4453 public override bool IsFixed
{
4454 get { return !IsRef; }
4457 public override string Name
{
4458 get { return Parameter.Name; }
4461 public Parameter Parameter
{
4462 get { return pi.Parameter; }
4465 public override VariableInfo VariableInfo
{
4466 get { return pi.VariableInfo; }
4469 protected override ILocalVariable Variable
{
4470 get { return Parameter; }
4473 public bool IsAssigned (EmitContext ec
, Location loc
)
4475 // HACK: Variables are not captured in probing mode
4476 if (ec
.IsInProbingMode
)
4479 if (!ec
.DoFlowAnalysis
|| !HasOutModifier
|| ec
.CurrentBranching
.IsAssigned (VariableInfo
))
4482 Report
.Error (269, loc
, "Use of unassigned out parameter `{0}'", Name
);
4486 public override void SetHasAddressTaken ()
4488 Parameter
.HasAddressTaken
= true;
4491 void SetAssigned (EmitContext ec
)
4493 if (HasOutModifier
&& ec
.DoFlowAnalysis
)
4494 ec
.CurrentBranching
.SetAssigned (VariableInfo
);
4497 bool DoResolveBase (EmitContext ec
)
4499 type
= pi
.ParameterType
;
4500 eclass
= ExprClass
.Variable
;
4502 AnonymousExpression am
= ec
.CurrentAnonymousMethod
;
4506 Block b
= ec
.CurrentBlock
;
4508 IParameterData
[] p
= b
.Toplevel
.Parameters
.FixedParameters
;
4509 for (int i
= 0; i
< p
.Length
; ++i
) {
4510 if (p
[i
] != Parameter
)
4514 // Skip closest anonymous method parameters
4516 if (b
== ec
.CurrentBlock
&& !am
.IsIterator
)
4520 Report
.Error (1628, loc
,
4521 "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
4522 Name
, am
.ContainerType
);
4530 b
= b
.Toplevel
.Parent
;
4533 if (pi
.Parameter
.HasAddressTaken
)
4534 AnonymousMethodExpression
.Error_AddressOfCapturedVar (this, loc
);
4536 if (ec
.IsVariableCapturingRequired
) {
4537 AnonymousMethodStorey storey
= pi
.Block
.CreateAnonymousMethodStorey (ec
);
4538 storey
.CaptureParameter (ec
, this);
4544 public override int GetHashCode ()
4546 return Name
.GetHashCode ();
4549 public override bool Equals (object obj
)
4551 ParameterReference pr
= obj
as ParameterReference
;
4555 return Name
== pr
.Name
;
4558 protected override void CloneTo (CloneContext clonectx
, Expression target
)
4563 public override Expression
CreateExpressionTree (EmitContext ec
)
4565 HoistedVariable hv
= GetHoistedVariable (ec
);
4567 return hv
.CreateExpressionTree (ec
);
4569 return Parameter
.ExpressionTreeVariableReference ();
4573 // Notice that for ref/out parameters, the type exposed is not the
4574 // same type exposed externally.
4577 // externally we expose "int&"
4578 // here we expose "int".
4580 // We record this in "is_ref". This means that the type system can treat
4581 // the type as it is expected, but when we generate the code, we generate
4582 // the alternate kind of code.
4584 public override Expression
DoResolve (EmitContext ec
)
4586 if (!DoResolveBase (ec
))
4589 // HACK: Variables are not captured in probing mode
4590 if (ec
.IsInProbingMode
)
4593 if (HasOutModifier
&& ec
.DoFlowAnalysis
&&
4594 (!ec
.OmitStructFlowAnalysis
|| !VariableInfo
.TypeInfo
.IsStruct
) && !IsAssigned (ec
, loc
))
4600 override public Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
4602 if (!DoResolveBase (ec
))
4605 // HACK: parameters are not captured when probing is on
4606 if (!ec
.IsInProbingMode
)
4612 static public void EmitLdArg (ILGenerator ig
, int x
)
4615 case 0: ig
.Emit (OpCodes
.Ldarg_0
); break;
4616 case 1: ig
.Emit (OpCodes
.Ldarg_1
); break;
4617 case 2: ig
.Emit (OpCodes
.Ldarg_2
); break;
4618 case 3: ig
.Emit (OpCodes
.Ldarg_3
); break;
4620 if (x
> byte.MaxValue
)
4621 ig
.Emit (OpCodes
.Ldarg
, x
);
4623 ig
.Emit (OpCodes
.Ldarg_S
, (byte) x
);
4630 /// Invocation of methods or delegates.
4632 public class Invocation
: ExpressionStatement
4634 protected Arguments arguments
;
4635 protected Expression expr
;
4636 protected MethodGroupExpr mg
;
4637 bool arguments_resolved
;
4640 // arguments is an ArrayList, but we do not want to typecast,
4641 // as it might be null.
4643 public Invocation (Expression expr
, Arguments arguments
)
4645 SimpleName sn
= expr
as SimpleName
;
4647 this.expr
= sn
.GetMethodGroup ();
4651 this.arguments
= arguments
;
4653 loc
= expr
.Location
;
4656 public Invocation (Expression expr
, Arguments arguments
, bool arguments_resolved
)
4657 : this (expr
, arguments
)
4659 this.arguments_resolved
= arguments_resolved
;
4662 public override Expression
CreateExpressionTree (EmitContext ec
)
4667 // Special conversion for nested expression trees
4669 if (TypeManager
.DropGenericTypeArguments (type
) == TypeManager
.expression_type
) {
4670 args
= new Arguments (1);
4671 args
.Add (new Argument (this));
4672 return CreateExpressionFactoryCall ("Quote", args
);
4675 Expression instance
= mg
.IsInstance
?
4676 mg
.InstanceExpression
.CreateExpressionTree (ec
) :
4677 new NullLiteral (loc
);
4679 args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
4681 mg
.CreateExpressionTree (ec
));
4684 MemberExpr
.Error_BaseAccessInExpressionTree (loc
);
4686 return CreateExpressionFactoryCall ("Call", args
);
4689 public override Expression
DoResolve (EmitContext ec
)
4691 // Don't resolve already resolved expression
4692 if (eclass
!= ExprClass
.Invalid
)
4695 Expression expr_resolved
= expr
.Resolve (ec
, ResolveFlags
.VariableOrValue
| ResolveFlags
.MethodGroup
);
4696 if (expr_resolved
== null)
4700 // Next, evaluate all the expressions in the argument list
4702 bool dynamic_arg
= false;
4703 if (arguments
!= null && !arguments_resolved
)
4704 arguments
.Resolve (ec
, out dynamic_arg
);
4706 Type expr_type
= expr_resolved
.Type
;
4707 mg
= expr_resolved
as MethodGroupExpr
;
4709 if (dynamic_arg
|| TypeManager
.IsDynamicType (expr_type
)) {
4710 if (mg
!= null && mg
.IsBase
) {
4711 Report
.Error (1971, loc
,
4712 "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
4717 Arguments args
= ((DynamicMemberBinder
) expr_resolved
).Arguments
;
4718 return new DynamicInvocation (expr
as MemberAccess
, args
, loc
).Resolve (ec
);
4722 if (expr_type
!= null && TypeManager
.IsDelegateType (expr_type
)){
4723 return (new DelegateInvocation (
4724 expr_resolved
, arguments
, loc
)).Resolve (ec
);
4727 MemberExpr me
= expr_resolved
as MemberExpr
;
4729 expr_resolved
.Error_UnexpectedKind (ResolveFlags
.MethodGroup
, loc
);
4733 mg
= ec
.TypeContainer
.LookupExtensionMethod (me
.Type
, me
.Name
, loc
);
4735 Report
.Error (1955, loc
, "The member `{0}' cannot be used as method or delegate",
4736 expr_resolved
.GetSignatureForError ());
4740 ((ExtensionMethodGroupExpr
)mg
).ExtensionExpression
= me
.InstanceExpression
;
4743 mg
= DoResolveOverload (ec
);
4747 MethodInfo method
= (MethodInfo
)mg
;
4748 if (method
!= null) {
4749 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
4751 // TODO: this is a copy of mg.ResolveMemberAccess method
4752 Expression iexpr
= mg
.InstanceExpression
;
4753 if (method
.IsStatic
) {
4754 if (iexpr
== null ||
4755 iexpr
is This
|| iexpr
is EmptyExpression
||
4756 mg
.IdenticalTypeName
) {
4757 mg
.InstanceExpression
= null;
4759 MemberExpr
.error176 (loc
, mg
.GetSignatureForError ());
4763 if (iexpr
== null || iexpr
== EmptyExpression
.Null
) {
4764 SimpleName
.Error_ObjectRefRequired (ec
, loc
, mg
.GetSignatureForError ());
4769 if (type
.IsPointer
){
4777 // Only base will allow this invocation to happen.
4779 if (mg
.IsBase
&& method
.IsAbstract
){
4780 Error_CannotCallAbstractBase (TypeManager
.CSharpSignature (method
));
4784 if (arguments
== null && method
.DeclaringType
== TypeManager
.object_type
&& method
.Name
== Destructor
.MetadataName
) {
4786 Report
.Error (250, loc
, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
4788 Report
.Error (245, loc
, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
4792 IsSpecialMethodInvocation (method
, loc
);
4794 if (mg
.InstanceExpression
!= null)
4795 mg
.InstanceExpression
.CheckMarshalByRefAccess (ec
);
4797 eclass
= ExprClass
.Value
;
4801 protected virtual MethodGroupExpr
DoResolveOverload (EmitContext ec
)
4803 return mg
.OverloadResolve (ec
, ref arguments
, false, loc
);
4806 public static bool IsSpecialMethodInvocation (MethodBase method
, Location loc
)
4808 if (!TypeManager
.IsSpecialMethod (method
))
4811 Report
.SymbolRelatedToPreviousError (method
);
4812 Report
.Error (571, loc
, "`{0}': cannot explicitly call operator or accessor",
4813 TypeManager
.CSharpSignature (method
, true));
4818 static Type
[] GetVarargsTypes (MethodBase mb
, Arguments arguments
)
4820 AParametersCollection pd
= TypeManager
.GetParameterData (mb
);
4822 Argument a
= arguments
[pd
.Count
- 1];
4823 Arglist list
= (Arglist
) a
.Expr
;
4825 return list
.ArgumentTypes
;
4829 /// This checks the ConditionalAttribute on the method
4831 public static bool IsMethodExcluded (MethodBase method
, Location loc
)
4833 if (method
.IsConstructor
)
4836 method
= TypeManager
.DropGenericMethodArguments (method
);
4837 if (method
.DeclaringType
.Module
== RootContext
.ToplevelTypes
.Builder
) {
4838 IMethodData md
= TypeManager
.GetMethod (method
);
4840 return md
.IsExcluded ();
4842 // For some methods (generated by delegate class) GetMethod returns null
4843 // because they are not included in builder_to_method table
4847 return AttributeTester
.IsConditionalMethodExcluded (method
, loc
);
4851 /// is_base tells whether we want to force the use of the `call'
4852 /// opcode instead of using callvirt. Call is required to call
4853 /// a specific method, while callvirt will always use the most
4854 /// recent method in the vtable.
4856 /// is_static tells whether this is an invocation on a static method
4858 /// instance_expr is an expression that represents the instance
4859 /// it must be non-null if is_static is false.
4861 /// method is the method to invoke.
4863 /// Arguments is the list of arguments to pass to the method or constructor.
4865 public static void EmitCall (EmitContext ec
, bool is_base
,
4866 Expression instance_expr
,
4867 MethodBase method
, Arguments Arguments
, Location loc
)
4869 EmitCall (ec
, is_base
, instance_expr
, method
, Arguments
, loc
, false, false);
4872 // `dup_args' leaves an extra copy of the arguments on the stack
4873 // `omit_args' does not leave any arguments at all.
4874 // So, basically, you could make one call with `dup_args' set to true,
4875 // and then another with `omit_args' set to true, and the two calls
4876 // would have the same set of arguments. However, each argument would
4877 // only have been evaluated once.
4878 public static void EmitCall (EmitContext ec
, bool is_base
,
4879 Expression instance_expr
,
4880 MethodBase method
, Arguments Arguments
, Location loc
,
4881 bool dup_args
, bool omit_args
)
4883 ILGenerator ig
= ec
.ig
;
4884 bool struct_call
= false;
4885 bool this_call
= false;
4886 LocalTemporary this_arg
= null;
4888 Type decl_type
= method
.DeclaringType
;
4890 if (IsMethodExcluded (method
, loc
))
4893 bool is_static
= method
.IsStatic
;
4895 this_call
= instance_expr
is This
;
4896 if (TypeManager
.IsStruct (decl_type
) || TypeManager
.IsEnumType (decl_type
))
4900 // If this is ourselves, push "this"
4904 Type iexpr_type
= instance_expr
.Type
;
4907 // Push the instance expression
4909 if (TypeManager
.IsValueType (iexpr_type
) || TypeManager
.IsGenericParameter (iexpr_type
)) {
4911 // Special case: calls to a function declared in a
4912 // reference-type with a value-type argument need
4913 // to have their value boxed.
4914 if (TypeManager
.IsStruct (decl_type
) ||
4915 TypeManager
.IsGenericParameter (iexpr_type
)) {
4917 // If the expression implements IMemoryLocation, then
4918 // we can optimize and use AddressOf on the
4921 // If not we have to use some temporary storage for
4923 if (instance_expr
is IMemoryLocation
) {
4924 ((IMemoryLocation
)instance_expr
).
4925 AddressOf (ec
, AddressOp
.LoadStore
);
4927 LocalTemporary temp
= new LocalTemporary (iexpr_type
);
4928 instance_expr
.Emit (ec
);
4930 temp
.AddressOf (ec
, AddressOp
.Load
);
4933 // avoid the overhead of doing this all the time.
4935 t
= TypeManager
.GetReferenceType (iexpr_type
);
4937 instance_expr
.Emit (ec
);
4939 // FIXME: should use instance_expr is IMemoryLocation + constraint.
4940 // to help JIT to produce better code
4941 ig
.Emit (OpCodes
.Box
, instance_expr
.Type
);
4942 t
= TypeManager
.object_type
;
4945 instance_expr
.Emit (ec
);
4946 t
= instance_expr
.Type
;
4950 ig
.Emit (OpCodes
.Dup
);
4951 if (Arguments
!= null && Arguments
.Count
!= 0) {
4952 this_arg
= new LocalTemporary (t
);
4953 this_arg
.Store (ec
);
4959 if (!omit_args
&& Arguments
!= null)
4960 Arguments
.Emit (ec
, dup_args
, this_arg
);
4963 if (is_static
|| struct_call
|| is_base
|| (this_call
&& !method
.IsVirtual
)) {
4964 call_op
= OpCodes
.Call
;
4966 call_op
= OpCodes
.Callvirt
;
4969 if ((instance_expr
!= null) && (instance_expr
.Type
.IsGenericParameter
))
4970 ig
.Emit (OpCodes
.Constrained
, instance_expr
.Type
);
4974 if ((method
.CallingConvention
& CallingConventions
.VarArgs
) != 0) {
4975 Type
[] varargs_types
= GetVarargsTypes (method
, Arguments
);
4976 ig
.EmitCall (call_op
, (MethodInfo
) method
, varargs_types
);
4983 // and DoFoo is not virtual, you can omit the callvirt,
4984 // because you don't need the null checking behavior.
4986 if (method
is MethodInfo
)
4987 ig
.Emit (call_op
, (MethodInfo
) method
);
4989 ig
.Emit (call_op
, (ConstructorInfo
) method
);
4992 public override void Emit (EmitContext ec
)
4994 mg
.EmitCall (ec
, arguments
);
4997 public override void EmitStatement (EmitContext ec
)
5002 // Pop the return value if there is one
5004 if (TypeManager
.TypeToCoreType (type
) != TypeManager
.void_type
)
5005 ec
.ig
.Emit (OpCodes
.Pop
);
5008 protected override void CloneTo (CloneContext clonectx
, Expression t
)
5010 Invocation target
= (Invocation
) t
;
5012 if (arguments
!= null)
5013 target
.arguments
= arguments
.Clone (clonectx
);
5015 target
.expr
= expr
.Clone (clonectx
);
5018 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
5020 mg
.MutateHoistedGenericType (storey
);
5021 type
= storey
.MutateType (type
);
5022 if (arguments
!= null) {
5023 arguments
.MutateHoistedGenericType (storey
);
5029 // It's either a cast or delegate invocation
5031 public class InvocationOrCast : ExpressionStatement
5034 Expression argument;
5036 public InvocationOrCast (Expression expr, Expression argument)
5039 this.argument = argument;
5040 this.loc = expr.Location;
5043 public override Expression CreateExpressionTree (EmitContext ec)
5045 throw new NotSupportedException ("ET");
5048 public override Expression DoResolve (EmitContext ec)
5050 Expression e = ResolveCore (ec);
5054 return e.Resolve (ec);
5057 Expression ResolveCore (EmitContext ec)
5060 // First try to resolve it as a cast.
5062 TypeExpr te = expr.ResolveAsBaseTerminal (ec, true);
5064 return new Cast (te, argument, loc);
5068 // This can either be a type or a delegate invocation.
5069 // Let's just resolve it and see what we'll get.
5071 expr = expr.Resolve (ec, ResolveFlags.Type | ResolveFlags.VariableOrValue);
5076 // Ok, so it's a Cast.
5078 if (expr.eclass == ExprClass.Type || expr.eclass == ExprClass.TypeParameter) {
5079 return new Cast (expr, argument, loc);
5082 if (expr.eclass == ExprClass.Namespace) {
5083 expr.Error_UnexpectedKind (null, "type", loc);
5088 // It's a delegate invocation.
5090 if (!TypeManager.IsDelegateType (expr.Type)) {
5091 Error (149, "Method name expected");
5095 ArrayList args = new ArrayList (1);
5096 args.Add (new Argument (argument, Argument.AType.Expression));
5097 return new DelegateInvocation (expr, args, loc);
5100 public override ExpressionStatement ResolveStatement (EmitContext ec)
5102 Expression e = ResolveCore (ec);
5106 ExpressionStatement s = e as ExpressionStatement;
5108 Error_InvalidExpressionStatement ();
5112 return s.ResolveStatement (ec);
5115 public override void Emit (EmitContext ec)
5117 throw new Exception ("Cannot happen");
5120 public override void EmitStatement (EmitContext ec)
5122 throw new Exception ("Cannot happen");
5125 protected override void CloneTo (CloneContext clonectx, Expression t)
5127 InvocationOrCast target = (InvocationOrCast) t;
5129 target.expr = expr.Clone (clonectx);
5130 target.argument = argument.Clone (clonectx);
5136 /// Implements the new expression
5138 public class New
: ExpressionStatement
, IMemoryLocation
{
5139 Arguments Arguments
;
5142 // During bootstrap, it contains the RequestedType,
5143 // but if `type' is not null, it *might* contain a NewDelegate
5144 // (because of field multi-initialization)
5146 Expression RequestedType
;
5148 MethodGroupExpr method
;
5150 bool is_type_parameter
;
5152 public New (Expression requested_type
, Arguments arguments
, Location l
)
5154 RequestedType
= requested_type
;
5155 Arguments
= arguments
;
5160 /// Converts complex core type syntax like 'new int ()' to simple constant
5162 public static Constant
Constantify (Type t
)
5164 if (t
== TypeManager
.int32_type
)
5165 return new IntConstant (0, Location
.Null
);
5166 if (t
== TypeManager
.uint32_type
)
5167 return new UIntConstant (0, Location
.Null
);
5168 if (t
== TypeManager
.int64_type
)
5169 return new LongConstant (0, Location
.Null
);
5170 if (t
== TypeManager
.uint64_type
)
5171 return new ULongConstant (0, Location
.Null
);
5172 if (t
== TypeManager
.float_type
)
5173 return new FloatConstant (0, Location
.Null
);
5174 if (t
== TypeManager
.double_type
)
5175 return new DoubleConstant (0, Location
.Null
);
5176 if (t
== TypeManager
.short_type
)
5177 return new ShortConstant (0, Location
.Null
);
5178 if (t
== TypeManager
.ushort_type
)
5179 return new UShortConstant (0, Location
.Null
);
5180 if (t
== TypeManager
.sbyte_type
)
5181 return new SByteConstant (0, Location
.Null
);
5182 if (t
== TypeManager
.byte_type
)
5183 return new ByteConstant (0, Location
.Null
);
5184 if (t
== TypeManager
.char_type
)
5185 return new CharConstant ('\0', Location
.Null
);
5186 if (t
== TypeManager
.bool_type
)
5187 return new BoolConstant (false, Location
.Null
);
5188 if (t
== TypeManager
.decimal_type
)
5189 return new DecimalConstant (0, Location
.Null
);
5190 if (TypeManager
.IsEnumType (t
))
5191 return new EnumConstant (Constantify (TypeManager
.GetEnumUnderlyingType (t
)), t
);
5192 if (TypeManager
.IsNullableType (t
))
5193 return Nullable
.LiftedNull
.Create (t
, Location
.Null
);
5199 // Checks whether the type is an interface that has the
5200 // [ComImport, CoClass] attributes and must be treated
5203 public Expression
CheckComImport (EmitContext ec
)
5205 if (!type
.IsInterface
)
5209 // Turn the call into:
5210 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
5212 Type real_class
= AttributeTester
.GetCoClassAttribute (type
);
5213 if (real_class
== null)
5216 New proxy
= new New (new TypeExpression (real_class
, loc
), Arguments
, loc
);
5217 Cast cast
= new Cast (new TypeExpression (type
, loc
), proxy
, loc
);
5218 return cast
.Resolve (ec
);
5221 public override Expression
CreateExpressionTree (EmitContext ec
)
5224 if (method
== null) {
5225 args
= new Arguments (1);
5226 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
5228 args
= Arguments
.CreateForExpressionTree (ec
, Arguments
,
5229 method
.CreateExpressionTree (ec
));
5232 return CreateExpressionFactoryCall ("New", args
);
5235 public override Expression
DoResolve (EmitContext ec
)
5238 // The New DoResolve might be called twice when initializing field
5239 // expressions (see EmitFieldInitializers, the call to
5240 // GetInitializerExpression will perform a resolve on the expression,
5241 // and later the assign will trigger another resolution
5243 // This leads to bugs (#37014)
5246 if (RequestedType
is NewDelegate
)
5247 return RequestedType
;
5251 TypeExpr texpr
= RequestedType
.ResolveAsTypeTerminal (ec
, false);
5257 if (type
.IsPointer
) {
5258 Report
.Error (1919, loc
, "Unsafe type `{0}' cannot be used in an object creation expression",
5259 TypeManager
.CSharpName (type
));
5263 if (Arguments
== null) {
5264 Constant c
= Constantify (type
);
5266 return ReducedExpression
.Create (c
, this);
5269 if (TypeManager
.IsDelegateType (type
)) {
5270 return (new NewDelegate (type
, Arguments
, loc
)).Resolve (ec
);
5273 if (TypeManager
.IsGenericParameter (type
)) {
5274 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (type
);
5276 if ((gc
== null) || (!gc
.HasConstructorConstraint
&& !gc
.IsValueType
)) {
5277 Error (304, String
.Format (
5278 "Cannot create an instance of the " +
5279 "variable type '{0}' because it " +
5280 "doesn't have the new() constraint",
5285 if ((Arguments
!= null) && (Arguments
.Count
!= 0)) {
5286 Error (417, String
.Format (
5287 "`{0}': cannot provide arguments " +
5288 "when creating an instance of a " +
5289 "variable type.", type
));
5293 if (TypeManager
.activator_create_instance
== null) {
5294 Type activator_type
= TypeManager
.CoreLookupType ("System", "Activator", Kind
.Class
, true);
5295 if (activator_type
!= null) {
5296 TypeManager
.activator_create_instance
= TypeManager
.GetPredefinedMethod (
5297 activator_type
, "CreateInstance", loc
, Type
.EmptyTypes
);
5301 is_type_parameter
= true;
5302 eclass
= ExprClass
.Value
;
5306 if (type
.IsAbstract
&& type
.IsSealed
) {
5307 Report
.SymbolRelatedToPreviousError (type
);
5308 Report
.Error (712, loc
, "Cannot create an instance of the static class `{0}'", TypeManager
.CSharpName (type
));
5312 if (type
.IsInterface
|| type
.IsAbstract
){
5313 if (!TypeManager
.IsGenericType (type
)) {
5314 RequestedType
= CheckComImport (ec
);
5315 if (RequestedType
!= null)
5316 return RequestedType
;
5319 Report
.SymbolRelatedToPreviousError (type
);
5320 Report
.Error (144, loc
, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager
.CSharpName (type
));
5324 bool is_struct
= TypeManager
.IsStruct (type
);
5325 eclass
= ExprClass
.Value
;
5328 // SRE returns a match for .ctor () on structs (the object constructor),
5329 // so we have to manually ignore it.
5331 if (is_struct
&& Arguments
== null)
5334 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5335 Expression ml
= MemberLookupFinal (ec
, type
, type
, ConstructorInfo
.ConstructorName
,
5336 MemberTypes
.Constructor
, AllBindingFlags
| BindingFlags
.DeclaredOnly
, loc
);
5338 if (Arguments
!= null) {
5340 Arguments
.Resolve (ec
, out dynamic);
5343 Arguments
.Insert (0, new Argument (new TypeOf (texpr
, loc
).Resolve (ec
)));
5344 return new DynamicInvocation (new SimpleName (ConstructorInfo
.ConstructorName
, loc
), Arguments
, type
, loc
).Resolve (ec
);
5351 method
= ml
as MethodGroupExpr
;
5352 if (method
== null) {
5353 ml
.Error_UnexpectedKind (ec
.DeclContainer
, "method group", loc
);
5357 method
= method
.OverloadResolve (ec
, ref Arguments
, false, loc
);
5364 bool DoEmitTypeParameter (EmitContext ec
)
5367 ILGenerator ig
= ec
.ig
;
5368 // IMemoryLocation ml;
5370 MethodInfo ci
= TypeManager
.activator_create_instance
.MakeGenericMethod (
5371 new Type
[] { type }
);
5373 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (type
);
5374 if (gc
.HasReferenceTypeConstraint
|| gc
.HasClassConstraint
) {
5375 ig
.Emit (OpCodes
.Call
, ci
);
5379 // Allow DoEmit() to be called multiple times.
5380 // We need to create a new LocalTemporary each time since
5381 // you can't share LocalBuilders among ILGeneators.
5382 LocalTemporary temp
= new LocalTemporary (type
);
5384 Label label_activator
= ig
.DefineLabel ();
5385 Label label_end
= ig
.DefineLabel ();
5387 temp
.AddressOf (ec
, AddressOp
.Store
);
5388 ig
.Emit (OpCodes
.Initobj
, type
);
5391 ig
.Emit (OpCodes
.Box
, type
);
5392 ig
.Emit (OpCodes
.Brfalse
, label_activator
);
5394 temp
.AddressOf (ec
, AddressOp
.Store
);
5395 ig
.Emit (OpCodes
.Initobj
, type
);
5397 ig
.Emit (OpCodes
.Br_S
, label_end
);
5399 ig
.MarkLabel (label_activator
);
5401 ig
.Emit (OpCodes
.Call
, ci
);
5402 ig
.MarkLabel (label_end
);
5405 throw new InternalErrorException ();
5410 // This Emit can be invoked in two contexts:
5411 // * As a mechanism that will leave a value on the stack (new object)
5412 // * As one that wont (init struct)
5414 // If we are dealing with a ValueType, we have a few
5415 // situations to deal with:
5417 // * The target is a ValueType, and we have been provided
5418 // the instance (this is easy, we are being assigned).
5420 // * The target of New is being passed as an argument,
5421 // to a boxing operation or a function that takes a
5424 // In this case, we need to create a temporary variable
5425 // that is the argument of New.
5427 // Returns whether a value is left on the stack
5429 // *** Implementation note ***
5431 // To benefit from this optimization, each assignable expression
5432 // has to manually cast to New and call this Emit.
5434 // TODO: It's worth to implement it for arrays and fields
5436 public virtual bool Emit (EmitContext ec
, IMemoryLocation target
)
5438 bool is_value_type
= TypeManager
.IsValueType (type
);
5439 ILGenerator ig
= ec
.ig
;
5440 VariableReference vr
= target
as VariableReference
;
5442 if (target
!= null && is_value_type
&& (vr
!= null || method
== null)) {
5443 target
.AddressOf (ec
, AddressOp
.Store
);
5444 } else if (vr
!= null && vr
.IsRef
) {
5448 if (Arguments
!= null)
5449 Arguments
.Emit (ec
);
5451 if (is_value_type
) {
5452 if (method
== null) {
5453 ig
.Emit (OpCodes
.Initobj
, type
);
5458 ig
.Emit (OpCodes
.Call
, (ConstructorInfo
) method
);
5463 if (is_type_parameter
)
5464 return DoEmitTypeParameter (ec
);
5466 ConstructorInfo ci
= (ConstructorInfo
) method
;
5468 if (TypeManager
.IsGenericType (type
))
5469 ci
= TypeBuilder
.GetConstructor (type
, ci
);
5472 ig
.Emit (OpCodes
.Newobj
, ci
);
5476 public override void Emit (EmitContext ec
)
5478 LocalTemporary v
= null;
5479 if (method
== null && TypeManager
.IsValueType (type
)) {
5480 // TODO: Use temporary variable from pool
5481 v
= new LocalTemporary (type
);
5488 public override void EmitStatement (EmitContext ec
)
5490 LocalTemporary v
= null;
5491 if (method
== null && TypeManager
.IsValueType (type
)) {
5492 // TODO: Use temporary variable from pool
5493 v
= new LocalTemporary (type
);
5497 ec
.ig
.Emit (OpCodes
.Pop
);
5500 public bool IsDefaultValueType
{
5502 return TypeManager
.IsValueType (type
) && !HasInitializer
&& Arguments
== null;
5506 public virtual bool HasInitializer
{
5512 public void AddressOf (EmitContext ec
, AddressOp mode
)
5514 EmitAddressOf (ec
, mode
);
5517 protected virtual IMemoryLocation
EmitAddressOf (EmitContext ec
, AddressOp mode
)
5519 LocalTemporary value_target
= new LocalTemporary (type
);
5521 if (is_type_parameter
) {
5522 DoEmitTypeParameter (ec
);
5523 value_target
.Store (ec
);
5524 value_target
.AddressOf (ec
, mode
);
5525 return value_target
;
5528 if (!TypeManager
.IsStruct (type
)){
5530 // We throw an exception. So far, I believe we only need to support
5532 // foreach (int j in new StructType ())
5535 throw new Exception ("AddressOf should not be used for classes");
5538 value_target
.AddressOf (ec
, AddressOp
.Store
);
5540 if (method
== null) {
5541 ec
.ig
.Emit (OpCodes
.Initobj
, type
);
5543 if (Arguments
!= null)
5544 Arguments
.Emit (ec
);
5546 ec
.ig
.Emit (OpCodes
.Call
, (ConstructorInfo
) method
);
5549 value_target
.AddressOf (ec
, mode
);
5550 return value_target
;
5553 protected override void CloneTo (CloneContext clonectx
, Expression t
)
5555 New target
= (New
) t
;
5557 target
.RequestedType
= RequestedType
.Clone (clonectx
);
5558 if (Arguments
!= null){
5559 target
.Arguments
= Arguments
.Clone (clonectx
);
5563 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
5565 if (method
!= null) {
5566 method
.MutateHoistedGenericType (storey
);
5567 if (Arguments
!= null) {
5568 Arguments
.MutateHoistedGenericType (storey
);
5572 type
= storey
.MutateType (type
);
5577 /// 14.5.10.2: Represents an array creation expression.
5581 /// There are two possible scenarios here: one is an array creation
5582 /// expression that specifies the dimensions and optionally the
5583 /// initialization data and the other which does not need dimensions
5584 /// specified but where initialization data is mandatory.
5586 public class ArrayCreation
: Expression
{
5587 FullNamedExpression requested_base_type
;
5588 ArrayList initializers
;
5591 // The list of Argument types.
5592 // This is used to construct the `newarray' or constructor signature
5594 protected ArrayList arguments
;
5596 protected Type array_element_type
;
5597 bool expect_initializers
= false;
5598 int num_arguments
= 0;
5599 protected int dimensions
;
5600 protected readonly string rank
;
5602 protected ArrayList array_data
;
5606 // The number of constants in array initializers
5607 int const_initializers_count
;
5608 bool only_constant_initializers
;
5610 public ArrayCreation (FullNamedExpression requested_base_type
, ArrayList exprs
, string rank
, ArrayList initializers
, Location l
)
5612 this.requested_base_type
= requested_base_type
;
5613 this.initializers
= initializers
;
5617 arguments
= new ArrayList (exprs
.Count
);
5619 foreach (Expression e
in exprs
) {
5625 public ArrayCreation (FullNamedExpression requested_base_type
, string rank
, ArrayList initializers
, Location l
)
5627 this.requested_base_type
= requested_base_type
;
5628 this.initializers
= initializers
;
5632 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5634 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5636 //dimensions = tmp.Length - 1;
5637 expect_initializers
= true;
5640 public static void Error_IncorrectArrayInitializer (Location loc
)
5642 Report
.Error (178, loc
, "Invalid rank specifier: expected `,' or `]'");
5645 protected override void Error_NegativeArrayIndex (Location loc
)
5647 Report
.Error (248, loc
, "Cannot create an array with a negative size");
5650 bool CheckIndices (EmitContext ec
, ArrayList probe
, int idx
, bool specified_dims
, int child_bounds
)
5652 if (specified_dims
) {
5653 Expression a
= (Expression
) arguments
[idx
];
5658 Constant c
= a
as Constant
;
5660 c
= c
.ImplicitConversionRequired (ec
, TypeManager
.int32_type
, a
.Location
);
5664 Report
.Error (150, a
.Location
, "A constant value is expected");
5668 int value = (int) c
.GetValue ();
5670 if (value != probe
.Count
) {
5671 Report
.Error (847, loc
, "An array initializer of length `{0}' was expected", value);
5675 bounds
[idx
] = value;
5678 only_constant_initializers
= true;
5679 for (int i
= 0; i
< probe
.Count
; ++i
) {
5680 object o
= probe
[i
];
5681 if (o
is ArrayList
) {
5682 ArrayList sub_probe
= o
as ArrayList
;
5683 if (idx
+ 1 >= dimensions
){
5684 Error (623, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5688 bool ret
= CheckIndices (ec
, sub_probe
, idx
+ 1, specified_dims
, child_bounds
- 1);
5691 } else if (child_bounds
> 1) {
5692 Report
.Error (846, ((Expression
) o
).Location
, "A nested array initializer was expected");
5694 Expression element
= ResolveArrayElement (ec
, (Expression
) o
);
5695 if (element
== null)
5698 // Initializers with the default values can be ignored
5699 Constant c
= element
as Constant
;
5701 if (c
.IsDefaultInitializer (array_element_type
)) {
5705 ++const_initializers_count
;
5708 only_constant_initializers
= false;
5711 array_data
.Add (element
);
5718 public override Expression
CreateExpressionTree (EmitContext ec
)
5722 if (array_data
== null) {
5723 args
= new Arguments (arguments
.Count
+ 1);
5724 args
.Add (new Argument (new TypeOf (new TypeExpression (array_element_type
, loc
), loc
)));
5725 foreach (Expression a
in arguments
) {
5726 if (arguments
.Count
== 1) {
5727 Constant c
= a
as Constant
;
5728 if (c
.IsDefaultValue
)
5729 return CreateExpressionFactoryCall ("NewArrayInit", args
);
5731 args
.Add (new Argument (a
.CreateExpressionTree (ec
)));
5734 return CreateExpressionFactoryCall ("NewArrayBounds", args
);
5737 if (dimensions
> 1) {
5738 Report
.Error (838, loc
, "An expression tree cannot contain a multidimensional array initializer");
5742 args
= new Arguments (array_data
== null ? 1 : array_data
.Count
+ 1);
5743 args
.Add (new Argument (new TypeOf (new TypeExpression (array_element_type
, loc
), loc
)));
5744 if (array_data
!= null) {
5745 for (int i
= 0; i
< array_data
.Count
; ++i
) {
5746 Expression e
= (Expression
) array_data
[i
];
5748 e
= Convert
.ImplicitConversion (ec
, (Expression
) initializers
[i
], array_element_type
, loc
);
5750 args
.Add (new Argument (e
.CreateExpressionTree (ec
)));
5754 return CreateExpressionFactoryCall ("NewArrayInit", args
);
5757 public void UpdateIndices ()
5760 for (ArrayList probe
= initializers
; probe
!= null;) {
5761 if (probe
.Count
> 0 && probe
[0] is ArrayList
) {
5762 Expression e
= new IntConstant (probe
.Count
, Location
.Null
);
5765 bounds
[i
++] = probe
.Count
;
5767 probe
= (ArrayList
) probe
[0];
5770 Expression e
= new IntConstant (probe
.Count
, Location
.Null
);
5773 bounds
[i
++] = probe
.Count
;
5780 Expression first_emit
;
5781 LocalTemporary first_emit_temp
;
5783 protected virtual Expression
ResolveArrayElement (EmitContext ec
, Expression element
)
5785 element
= element
.Resolve (ec
);
5786 if (element
== null)
5789 if (element
is CompoundAssign
.TargetExpression
) {
5790 if (first_emit
!= null)
5791 throw new InternalErrorException ("Can only handle one mutator at a time");
5792 first_emit
= element
;
5793 element
= first_emit_temp
= new LocalTemporary (element
.Type
);
5796 return Convert
.ImplicitConversionRequired (
5797 ec
, element
, array_element_type
, loc
);
5800 protected bool ResolveInitializers (EmitContext ec
)
5802 if (initializers
== null) {
5803 return !expect_initializers
;
5807 // We use this to store all the date values in the order in which we
5808 // will need to store them in the byte blob later
5810 array_data
= new ArrayList ();
5811 bounds
= new System
.Collections
.Specialized
.HybridDictionary ();
5813 if (arguments
!= null)
5814 return CheckIndices (ec
, initializers
, 0, true, dimensions
);
5816 arguments
= new ArrayList ();
5818 if (!CheckIndices (ec
, initializers
, 0, false, dimensions
))
5827 // Resolved the type of the array
5829 bool ResolveArrayType (EmitContext ec
)
5831 if (requested_base_type
== null) {
5832 Report
.Error (622, loc
, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
5836 if (requested_base_type
is VarExpr
) {
5837 Report
.Error (820, loc
, "An implicitly typed local variable declarator cannot use an array initializer");
5841 StringBuilder array_qualifier
= new StringBuilder (rank
);
5844 // `In the first form allocates an array instace of the type that results
5845 // from deleting each of the individual expression from the expression list'
5847 if (num_arguments
> 0) {
5848 array_qualifier
.Append ("[");
5849 for (int i
= num_arguments
-1; i
> 0; i
--)
5850 array_qualifier
.Append (",");
5851 array_qualifier
.Append ("]");
5857 TypeExpr array_type_expr
;
5858 array_type_expr
= new ComposedCast (requested_base_type
, array_qualifier
.ToString (), loc
);
5859 array_type_expr
= array_type_expr
.ResolveAsTypeTerminal (ec
, false);
5860 if (array_type_expr
== null)
5863 type
= array_type_expr
.Type
;
5864 array_element_type
= TypeManager
.GetElementType (type
);
5865 dimensions
= type
.GetArrayRank ();
5870 public override Expression
DoResolve (EmitContext ec
)
5875 if (!ResolveArrayType (ec
))
5879 // First step is to validate the initializers and fill
5880 // in any missing bits
5882 if (!ResolveInitializers (ec
))
5885 for (int i
= 0; i
< arguments
.Count
; ++i
) {
5886 Expression e
= ((Expression
) arguments
[i
]).Resolve (ec
);
5890 arguments
[i
] = ConvertExpressionToArrayIndex (ec
, e
);
5893 eclass
= ExprClass
.Value
;
5897 MethodInfo
GetArrayMethod (int arguments
)
5899 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
5901 Type
[] arg_types
= new Type
[arguments
];
5902 for (int i
= 0; i
< arguments
; i
++)
5903 arg_types
[i
] = TypeManager
.int32_type
;
5905 MethodInfo mi
= mb
.GetArrayMethod (type
, ".ctor", CallingConventions
.HasThis
, null,
5909 Report
.Error (-6, "New invocation: Can not find a constructor for " +
5910 "this argument list");
5917 byte [] MakeByteBlob ()
5922 int count
= array_data
.Count
;
5924 if (TypeManager
.IsEnumType (array_element_type
))
5925 array_element_type
= TypeManager
.GetEnumUnderlyingType (array_element_type
);
5927 factor
= GetTypeSize (array_element_type
);
5929 throw new Exception ("unrecognized type in MakeByteBlob: " + array_element_type
);
5931 data
= new byte [(count
* factor
+ 3) & ~
3];
5934 for (int i
= 0; i
< count
; ++i
) {
5935 object v
= array_data
[i
];
5937 if (v
is EnumConstant
)
5938 v
= ((EnumConstant
) v
).Child
;
5940 if (v
is Constant
&& !(v
is StringConstant
))
5941 v
= ((Constant
) v
).GetValue ();
5947 if (array_element_type
== TypeManager
.int64_type
){
5948 if (!(v
is Expression
)){
5949 long val
= (long) v
;
5951 for (int j
= 0; j
< factor
; ++j
) {
5952 data
[idx
+ j
] = (byte) (val
& 0xFF);
5956 } else if (array_element_type
== TypeManager
.uint64_type
){
5957 if (!(v
is Expression
)){
5958 ulong val
= (ulong) v
;
5960 for (int j
= 0; j
< factor
; ++j
) {
5961 data
[idx
+ j
] = (byte) (val
& 0xFF);
5965 } else if (array_element_type
== TypeManager
.float_type
) {
5966 if (!(v
is Expression
)){
5967 element
= BitConverter
.GetBytes ((float) v
);
5969 for (int j
= 0; j
< factor
; ++j
)
5970 data
[idx
+ j
] = element
[j
];
5971 if (!BitConverter
.IsLittleEndian
)
5972 System
.Array
.Reverse (data
, idx
, 4);
5974 } else if (array_element_type
== TypeManager
.double_type
) {
5975 if (!(v
is Expression
)){
5976 element
= BitConverter
.GetBytes ((double) v
);
5978 for (int j
= 0; j
< factor
; ++j
)
5979 data
[idx
+ j
] = element
[j
];
5981 // FIXME: Handle the ARM float format.
5982 if (!BitConverter
.IsLittleEndian
)
5983 System
.Array
.Reverse (data
, idx
, 8);
5985 } else if (array_element_type
== TypeManager
.char_type
){
5986 if (!(v
is Expression
)){
5987 int val
= (int) ((char) v
);
5989 data
[idx
] = (byte) (val
& 0xff);
5990 data
[idx
+1] = (byte) (val
>> 8);
5992 } else if (array_element_type
== TypeManager
.short_type
){
5993 if (!(v
is Expression
)){
5994 int val
= (int) ((short) v
);
5996 data
[idx
] = (byte) (val
& 0xff);
5997 data
[idx
+1] = (byte) (val
>> 8);
5999 } else if (array_element_type
== TypeManager
.ushort_type
){
6000 if (!(v
is Expression
)){
6001 int val
= (int) ((ushort) v
);
6003 data
[idx
] = (byte) (val
& 0xff);
6004 data
[idx
+1] = (byte) (val
>> 8);
6006 } else if (array_element_type
== TypeManager
.int32_type
) {
6007 if (!(v
is Expression
)){
6010 data
[idx
] = (byte) (val
& 0xff);
6011 data
[idx
+1] = (byte) ((val
>> 8) & 0xff);
6012 data
[idx
+2] = (byte) ((val
>> 16) & 0xff);
6013 data
[idx
+3] = (byte) (val
>> 24);
6015 } else if (array_element_type
== TypeManager
.uint32_type
) {
6016 if (!(v
is Expression
)){
6017 uint val
= (uint) v
;
6019 data
[idx
] = (byte) (val
& 0xff);
6020 data
[idx
+1] = (byte) ((val
>> 8) & 0xff);
6021 data
[idx
+2] = (byte) ((val
>> 16) & 0xff);
6022 data
[idx
+3] = (byte) (val
>> 24);
6024 } else if (array_element_type
== TypeManager
.sbyte_type
) {
6025 if (!(v
is Expression
)){
6026 sbyte val
= (sbyte) v
;
6027 data
[idx
] = (byte) val
;
6029 } else if (array_element_type
== TypeManager
.byte_type
) {
6030 if (!(v
is Expression
)){
6031 byte val
= (byte) v
;
6032 data
[idx
] = (byte) val
;
6034 } else if (array_element_type
== TypeManager
.bool_type
) {
6035 if (!(v
is Expression
)){
6036 bool val
= (bool) v
;
6037 data
[idx
] = (byte) (val
? 1 : 0);
6039 } else if (array_element_type
== TypeManager
.decimal_type
){
6040 if (!(v
is Expression
)){
6041 int [] bits
= Decimal
.GetBits ((decimal) v
);
6044 // FIXME: For some reason, this doesn't work on the MS runtime.
6045 int [] nbits
= new int [4];
6046 nbits
[0] = bits
[3];
6047 nbits
[1] = bits
[2];
6048 nbits
[2] = bits
[0];
6049 nbits
[3] = bits
[1];
6051 for (int j
= 0; j
< 4; j
++){
6052 data
[p
++] = (byte) (nbits
[j
] & 0xff);
6053 data
[p
++] = (byte) ((nbits
[j
] >> 8) & 0xff);
6054 data
[p
++] = (byte) ((nbits
[j
] >> 16) & 0xff);
6055 data
[p
++] = (byte) (nbits
[j
] >> 24);
6059 throw new Exception ("Unrecognized type in MakeByteBlob: " + array_element_type
);
6067 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
6069 array_element_type
= storey
.MutateType (array_element_type
);
6070 type
= storey
.MutateType (type
);
6071 if (arguments
!= null) {
6072 foreach (Expression e
in arguments
)
6073 e
.MutateHoistedGenericType (storey
);
6076 if (array_data
!= null) {
6077 foreach (Expression e
in array_data
) {
6078 // Don't mutate values optimized away
6082 e
.MutateHoistedGenericType (storey
);
6088 // Emits the initializers for the array
6090 void EmitStaticInitializers (EmitContext ec
)
6092 // FIXME: This should go to Resolve !
6093 if (TypeManager
.void_initializearray_array_fieldhandle
== null) {
6094 TypeManager
.void_initializearray_array_fieldhandle
= TypeManager
.GetPredefinedMethod (
6095 TypeManager
.runtime_helpers_type
, "InitializeArray", loc
,
6096 TypeManager
.array_type
, TypeManager
.runtime_field_handle_type
);
6097 if (TypeManager
.void_initializearray_array_fieldhandle
== null)
6102 // First, the static data
6105 ILGenerator ig
= ec
.ig
;
6107 byte [] data
= MakeByteBlob ();
6109 fb
= RootContext
.MakeStaticData (data
);
6111 ig
.Emit (OpCodes
.Dup
);
6112 ig
.Emit (OpCodes
.Ldtoken
, fb
);
6113 ig
.Emit (OpCodes
.Call
,
6114 TypeManager
.void_initializearray_array_fieldhandle
);
6118 // Emits pieces of the array that can not be computed at compile
6119 // time (variables and string locations).
6121 // This always expect the top value on the stack to be the array
6123 void EmitDynamicInitializers (EmitContext ec
, bool emitConstants
)
6125 ILGenerator ig
= ec
.ig
;
6126 int dims
= bounds
.Count
;
6127 int [] current_pos
= new int [dims
];
6129 MethodInfo
set = null;
6132 Type
[] args
= new Type
[dims
+ 1];
6134 for (int j
= 0; j
< dims
; j
++)
6135 args
[j
] = TypeManager
.int32_type
;
6136 args
[dims
] = array_element_type
;
6138 set = RootContext
.ToplevelTypes
.Builder
.GetArrayMethod (
6140 CallingConventions
.HasThis
| CallingConventions
.Standard
,
6141 TypeManager
.void_type
, args
);
6144 for (int i
= 0; i
< array_data
.Count
; i
++){
6146 Expression e
= (Expression
)array_data
[i
];
6148 // Constant can be initialized via StaticInitializer
6149 if (e
!= null && !(!emitConstants
&& e
is Constant
)) {
6150 Type etype
= e
.Type
;
6152 ig
.Emit (OpCodes
.Dup
);
6154 for (int idx
= 0; idx
< dims
; idx
++)
6155 IntConstant
.EmitInt (ig
, current_pos
[idx
]);
6158 // If we are dealing with a struct, get the
6159 // address of it, so we can store it.
6161 if ((dims
== 1) && TypeManager
.IsStruct (etype
) &&
6162 (!TypeManager
.IsBuiltinOrEnum (etype
) ||
6163 etype
== TypeManager
.decimal_type
)) {
6165 ig
.Emit (OpCodes
.Ldelema
, etype
);
6171 bool is_stobj
, has_type_arg
;
6172 OpCode op
= ArrayAccess
.GetStoreOpcode (etype
, out is_stobj
, out has_type_arg
);
6174 ig
.Emit (OpCodes
.Stobj
, etype
);
6175 else if (has_type_arg
)
6176 ig
.Emit (op
, etype
);
6180 ig
.Emit (OpCodes
.Call
, set);
6187 for (int j
= dims
- 1; j
>= 0; j
--){
6189 if (current_pos
[j
] < (int) bounds
[j
])
6191 current_pos
[j
] = 0;
6196 public override void Emit (EmitContext ec
)
6198 ILGenerator ig
= ec
.ig
;
6200 if (first_emit
!= null) {
6201 first_emit
.Emit (ec
);
6202 first_emit_temp
.Store (ec
);
6205 foreach (Expression e
in arguments
)
6208 if (arguments
.Count
== 1)
6209 ig
.Emit (OpCodes
.Newarr
, array_element_type
);
6211 ig
.Emit (OpCodes
.Newobj
, GetArrayMethod (arguments
.Count
));
6214 if (initializers
== null)
6217 // Emit static initializer for arrays which have contain more than 4 items and
6218 // the static initializer will initialize at least 25% of array values.
6219 // NOTE: const_initializers_count does not contain default constant values.
6220 if (const_initializers_count
>= 4 && const_initializers_count
* 4 > (array_data
.Count
) &&
6221 TypeManager
.IsPrimitiveType (array_element_type
)) {
6222 EmitStaticInitializers (ec
);
6224 if (!only_constant_initializers
)
6225 EmitDynamicInitializers (ec
, false);
6227 EmitDynamicInitializers (ec
, true);
6230 if (first_emit_temp
!= null)
6231 first_emit_temp
.Release (ec
);
6234 public override bool GetAttributableValue (EmitContext ec
, Type value_type
, out object value)
6236 if (arguments
.Count
!= 1) {
6237 // Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6238 return base.GetAttributableValue (ec
, null, out value);
6241 if (array_data
== null) {
6242 Constant c
= (Constant
) arguments
[0];
6243 if (c
.IsDefaultValue
) {
6244 value = Array
.CreateInstance (array_element_type
, 0);
6247 // Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6248 return base.GetAttributableValue (ec
, null, out value);
6251 Array ret
= Array
.CreateInstance (array_element_type
, array_data
.Count
);
6252 object element_value
;
6253 for (int i
= 0; i
< ret
.Length
; ++i
)
6255 Expression e
= (Expression
)array_data
[i
];
6257 // Is null when an initializer is optimized (value == predefined value)
6261 if (!e
.GetAttributableValue (ec
, array_element_type
, out element_value
)) {
6265 ret
.SetValue (element_value
, i
);
6271 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6273 ArrayCreation target
= (ArrayCreation
) t
;
6275 if (requested_base_type
!= null)
6276 target
.requested_base_type
= (FullNamedExpression
)requested_base_type
.Clone (clonectx
);
6278 if (arguments
!= null){
6279 target
.arguments
= new ArrayList (arguments
.Count
);
6280 foreach (Expression e
in arguments
)
6281 target
.arguments
.Add (e
.Clone (clonectx
));
6284 if (initializers
!= null){
6285 target
.initializers
= new ArrayList (initializers
.Count
);
6286 foreach (object initializer
in initializers
)
6287 if (initializer
is ArrayList
) {
6288 ArrayList this_al
= (ArrayList
)initializer
;
6289 ArrayList al
= new ArrayList (this_al
.Count
);
6290 target
.initializers
.Add (al
);
6291 foreach (Expression e
in this_al
)
6292 al
.Add (e
.Clone (clonectx
));
6294 target
.initializers
.Add (((Expression
)initializer
).Clone (clonectx
));
6301 // Represents an implicitly typed array epxression
6303 public class ImplicitlyTypedArrayCreation
: ArrayCreation
6305 public ImplicitlyTypedArrayCreation (string rank
, ArrayList initializers
, Location loc
)
6306 : base (null, rank
, initializers
, loc
)
6308 if (RootContext
.Version
<= LanguageVersion
.ISO_2
)
6309 Report
.FeatureIsNotAvailable (loc
, "implicitly typed arrays");
6311 if (rank
.Length
> 2) {
6312 while (rank
[++dimensions
] == ',');
6318 public override Expression
DoResolve (EmitContext ec
)
6323 if (!ResolveInitializers (ec
))
6326 if (array_element_type
== null || array_element_type
== TypeManager
.null_type
||
6327 array_element_type
== TypeManager
.void_type
|| array_element_type
== InternalType
.AnonymousMethod
||
6328 arguments
.Count
!= dimensions
) {
6329 Error_NoBestType ();
6334 // At this point we found common base type for all initializer elements
6335 // but we have to be sure that all static initializer elements are of
6338 UnifyInitializerElement (ec
);
6340 type
= TypeManager
.GetConstructedType (array_element_type
, rank
);
6341 eclass
= ExprClass
.Value
;
6345 void Error_NoBestType ()
6347 Report
.Error (826, loc
,
6348 "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
6352 // Converts static initializer only
6354 void UnifyInitializerElement (EmitContext ec
)
6356 for (int i
= 0; i
< array_data
.Count
; ++i
) {
6357 Expression e
= (Expression
)array_data
[i
];
6359 array_data
[i
] = Convert
.ImplicitConversion (ec
, e
, array_element_type
, Location
.Null
);
6363 protected override Expression
ResolveArrayElement (EmitContext ec
, Expression element
)
6365 element
= element
.Resolve (ec
);
6366 if (element
== null)
6369 if (array_element_type
== null) {
6370 if (element
.Type
!= TypeManager
.null_type
)
6371 array_element_type
= element
.Type
;
6376 if (Convert
.ImplicitConversionExists (ec
, element
, array_element_type
)) {
6380 if (Convert
.ImplicitConversionExists (ec
, new TypeExpression (array_element_type
, loc
), element
.Type
)) {
6381 array_element_type
= element
.Type
;
6385 Error_NoBestType ();
6390 public sealed class CompilerGeneratedThis
: This
6392 public static This Instance
= new CompilerGeneratedThis ();
6394 private CompilerGeneratedThis ()
6395 : base (Location
.Null
)
6399 public CompilerGeneratedThis (Type type
, Location loc
)
6405 public override Expression
DoResolve (EmitContext ec
)
6407 eclass
= ExprClass
.Variable
;
6409 type
= ec
.ContainerType
;
6413 public override HoistedVariable
GetHoistedVariable (EmitContext ec
)
6420 /// Represents the `this' construct
6423 public class This
: VariableReference
6425 sealed class ThisVariable
: ILocalVariable
6427 public static readonly ILocalVariable Instance
= new ThisVariable ();
6429 public void Emit (EmitContext ec
)
6431 ec
.ig
.Emit (OpCodes
.Ldarg_0
);
6434 public void EmitAssign (EmitContext ec
)
6436 throw new InvalidOperationException ();
6439 public void EmitAddressOf (EmitContext ec
)
6441 ec
.ig
.Emit (OpCodes
.Ldarg_0
);
6446 VariableInfo variable_info
;
6449 public This (Block block
, Location loc
)
6455 public This (Location loc
)
6460 public override VariableInfo VariableInfo
{
6461 get { return variable_info; }
6464 public override bool IsFixed
{
6465 get { return false; }
6468 public override HoistedVariable
GetHoistedVariable (EmitContext ec
)
6470 // Is null when probing IsHoisted
6474 if (ec
.CurrentAnonymousMethod
== null)
6477 AnonymousMethodStorey storey
= ec
.CurrentAnonymousMethod
.Storey
;
6478 while (storey
!= null) {
6479 AnonymousMethodStorey temp
= storey
.Parent
as AnonymousMethodStorey
;
6481 return storey
.HoistedThis
;
6489 public override bool IsRef
{
6490 get { return is_struct; }
6493 protected override ILocalVariable Variable
{
6494 get { return ThisVariable.Instance; }
6497 public static bool IsThisAvailable (EmitContext ec
)
6499 if (ec
.IsStatic
|| ec
.IsInFieldInitializer
)
6502 if (ec
.CurrentAnonymousMethod
== null)
6505 if (ec
.TypeContainer
is Struct
&& ec
.CurrentIterator
== null)
6511 public bool ResolveBase (EmitContext ec
)
6513 if (eclass
!= ExprClass
.Invalid
)
6516 eclass
= ExprClass
.Variable
;
6518 if (ec
.TypeContainer
.CurrentType
!= null)
6519 type
= ec
.TypeContainer
.CurrentType
;
6521 type
= ec
.ContainerType
;
6523 if (!IsThisAvailable (ec
)) {
6525 Error (26, "Keyword `this' is not valid in a static property, static method, or static field initializer");
6527 Report
.Error (1673, loc
,
6528 "Anonymous methods inside structs cannot access instance members of `this'. " +
6529 "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
6533 is_struct
= ec
.TypeContainer
is Struct
;
6535 if (block
!= null) {
6536 if (block
.Toplevel
.ThisVariable
!= null)
6537 variable_info
= block
.Toplevel
.ThisVariable
.VariableInfo
;
6539 AnonymousExpression am
= ec
.CurrentAnonymousMethod
;
6540 if (am
!= null && ec
.IsVariableCapturingRequired
) {
6541 am
.SetHasThisAccess ();
6549 // Called from Invocation to check if the invocation is correct
6551 public override void CheckMarshalByRefAccess (EmitContext ec
)
6553 if ((variable_info
!= null) && !(TypeManager
.IsStruct (type
) && ec
.OmitStructFlowAnalysis
) &&
6554 !variable_info
.IsAssigned (ec
)) {
6555 Error (188, "The `this' object cannot be used before all of its " +
6556 "fields are assigned to");
6557 variable_info
.SetAssigned (ec
);
6561 public override Expression
CreateExpressionTree (EmitContext ec
)
6563 Arguments args
= new Arguments (1);
6564 args
.Add (new Argument (this));
6566 // Use typeless constant for ldarg.0 to save some
6567 // space and avoid problems with anonymous stories
6568 return CreateExpressionFactoryCall ("Constant", args
);
6571 public override Expression
DoResolve (EmitContext ec
)
6573 if (!ResolveBase (ec
))
6577 if (ec
.IsInFieldInitializer
) {
6578 Error (27, "Keyword `this' is not available in the current context");
6585 override public Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
6587 if (!ResolveBase (ec
))
6590 if (variable_info
!= null)
6591 variable_info
.SetAssigned (ec
);
6593 if (ec
.TypeContainer
is Class
){
6594 if (right_side
== EmptyExpression
.UnaryAddress
)
6595 Report
.Error (459, loc
, "Cannot take the address of `this' because it is read-only");
6596 else if (right_side
== EmptyExpression
.OutAccess
)
6597 Report
.Error (1605, loc
, "Cannot pass `this' as a ref or out argument because it is read-only");
6599 Report
.Error (1604, loc
, "Cannot assign to `this' because it is read-only");
6605 public override int GetHashCode()
6607 return block
.GetHashCode ();
6610 public override string Name
{
6611 get { return "this"; }
6614 public override bool Equals (object obj
)
6616 This t
= obj
as This
;
6620 return block
== t
.block
;
6623 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6625 This target
= (This
) t
;
6627 target
.block
= clonectx
.LookupBlock (block
);
6630 public override void SetHasAddressTaken ()
6637 /// Represents the `__arglist' construct
6639 public class ArglistAccess
: Expression
6641 public ArglistAccess (Location loc
)
6646 public override Expression
CreateExpressionTree (EmitContext ec
)
6648 throw new NotSupportedException ("ET");
6651 public override Expression
DoResolve (EmitContext ec
)
6653 eclass
= ExprClass
.Variable
;
6654 type
= TypeManager
.runtime_argument_handle_type
;
6656 if (ec
.IsInFieldInitializer
|| !ec
.CurrentBlock
.Toplevel
.Parameters
.HasArglist
)
6658 Error (190, "The __arglist construct is valid only within " +
6659 "a variable argument method");
6666 public override void Emit (EmitContext ec
)
6668 ec
.ig
.Emit (OpCodes
.Arglist
);
6671 protected override void CloneTo (CloneContext clonectx
, Expression target
)
6678 /// Represents the `__arglist (....)' construct
6680 class Arglist
: Expression
6682 Arguments Arguments
;
6684 public Arglist (Location loc
)
6689 public Arglist (Arguments args
, Location l
)
6695 public Type
[] ArgumentTypes
{
6697 if (Arguments
== null)
6698 return Type
.EmptyTypes
;
6700 Type
[] retval
= new Type
[Arguments
.Count
];
6701 for (int i
= 0; i
< retval
.Length
; i
++)
6702 retval
[i
] = Arguments
[i
].Expr
.Type
;
6708 public override Expression
CreateExpressionTree (EmitContext ec
)
6710 Report
.Error (1952, loc
, "An expression tree cannot contain a method with variable arguments");
6714 public override Expression
DoResolve (EmitContext ec
)
6716 eclass
= ExprClass
.Variable
;
6717 type
= InternalType
.Arglist
;
6718 if (Arguments
!= null) {
6719 bool dynamic; // Can be ignored as there is always only 1 overload
6720 Arguments
.Resolve (ec
, out dynamic);
6726 public override void Emit (EmitContext ec
)
6728 if (Arguments
!= null)
6729 Arguments
.Emit (ec
);
6732 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
6734 if (Arguments
!= null)
6735 Arguments
.MutateHoistedGenericType (storey
);
6738 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6740 Arglist target
= (Arglist
) t
;
6742 if (Arguments
!= null)
6743 target
.Arguments
= Arguments
.Clone (clonectx
);
6748 /// Implements the typeof operator
6750 public class TypeOf
: Expression
{
6751 Expression QueriedType
;
6752 protected Type typearg
;
6754 public TypeOf (Expression queried_type
, Location l
)
6756 QueriedType
= queried_type
;
6760 public override Expression
CreateExpressionTree (EmitContext ec
)
6762 Arguments args
= new Arguments (2);
6763 args
.Add (new Argument (this));
6764 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
6765 return CreateExpressionFactoryCall ("Constant", args
);
6768 public override Expression
DoResolve (EmitContext ec
)
6770 if (eclass
!= ExprClass
.Invalid
)
6773 TypeExpr texpr
= QueriedType
.ResolveAsTypeTerminal (ec
, false);
6777 typearg
= texpr
.Type
;
6779 if (typearg
== TypeManager
.void_type
) {
6780 Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6784 if (typearg
.IsPointer
&& !ec
.InUnsafe
){
6789 type
= TypeManager
.type_type
;
6791 return DoResolveBase ();
6794 protected Expression
DoResolveBase ()
6796 if (TypeManager
.system_type_get_type_from_handle
== null) {
6797 TypeManager
.system_type_get_type_from_handle
= TypeManager
.GetPredefinedMethod (
6798 TypeManager
.type_type
, "GetTypeFromHandle", loc
, TypeManager
.runtime_handle_type
);
6801 // Even though what is returned is a type object, it's treated as a value by the compiler.
6802 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
6803 eclass
= ExprClass
.Value
;
6807 public override void Emit (EmitContext ec
)
6809 ec
.ig
.Emit (OpCodes
.Ldtoken
, typearg
);
6810 ec
.ig
.Emit (OpCodes
.Call
, TypeManager
.system_type_get_type_from_handle
);
6813 public override bool GetAttributableValue (EmitContext ec
, Type value_type
, out object value)
6815 if (TypeManager
.ContainsGenericParameters (typearg
) &&
6816 !TypeManager
.IsGenericTypeDefinition (typearg
)) {
6817 Report
.SymbolRelatedToPreviousError (typearg
);
6818 Report
.Error (416, loc
, "`{0}': an attribute argument cannot use type parameters",
6819 TypeManager
.CSharpName (typearg
));
6824 if (value_type
== TypeManager
.object_type
) {
6825 value = (object)typearg
;
6832 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
6834 typearg
= storey
.MutateType (typearg
);
6837 public Type TypeArgument
{
6843 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6845 TypeOf target
= (TypeOf
) t
;
6846 if (QueriedType
!= null)
6847 target
.QueriedType
= QueriedType
.Clone (clonectx
);
6852 /// Implements the `typeof (void)' operator
6854 public class TypeOfVoid
: TypeOf
{
6855 public TypeOfVoid (Location l
) : base (null, l
)
6860 public override Expression
DoResolve (EmitContext ec
)
6862 type
= TypeManager
.type_type
;
6863 typearg
= TypeManager
.void_type
;
6865 return DoResolveBase ();
6869 class TypeOfMethod
: TypeOfMember
6871 public TypeOfMethod (MethodBase method
, Location loc
)
6872 : base (method
, loc
)
6876 public override Expression
DoResolve (EmitContext ec
)
6878 if (member
is MethodInfo
) {
6879 type
= TypeManager
.methodinfo_type
;
6881 type
= TypeManager
.methodinfo_type
= TypeManager
.CoreLookupType ("System.Reflection", "MethodInfo", Kind
.Class
, true);
6883 type
= TypeManager
.ctorinfo_type
;
6885 type
= TypeManager
.ctorinfo_type
= TypeManager
.CoreLookupType ("System.Reflection", "ConstructorInfo", Kind
.Class
, true);
6888 return base.DoResolve (ec
);
6891 public override void Emit (EmitContext ec
)
6893 if (member
is ConstructorInfo
)
6894 ec
.ig
.Emit (OpCodes
.Ldtoken
, (ConstructorInfo
) member
);
6896 ec
.ig
.Emit (OpCodes
.Ldtoken
, (MethodInfo
) member
);
6899 ec
.ig
.Emit (OpCodes
.Castclass
, type
);
6902 protected override string GetMethodName
{
6903 get { return "GetMethodFromHandle"; }
6906 protected override string RuntimeHandleName
{
6907 get { return "RuntimeMethodHandle"; }
6910 protected override MethodInfo TypeFromHandle
{
6912 return TypeManager
.methodbase_get_type_from_handle
;
6915 TypeManager
.methodbase_get_type_from_handle
= value;
6919 protected override MethodInfo TypeFromHandleGeneric
{
6921 return TypeManager
.methodbase_get_type_from_handle_generic
;
6924 TypeManager
.methodbase_get_type_from_handle_generic
= value;
6928 protected override string TypeName
{
6929 get { return "MethodBase"; }
6933 abstract class TypeOfMember
: Expression
6935 protected readonly MemberInfo member
;
6937 protected TypeOfMember (MemberInfo member
, Location loc
)
6939 this.member
= member
;
6943 public override Expression
CreateExpressionTree (EmitContext ec
)
6945 Arguments args
= new Arguments (2);
6946 args
.Add (new Argument (this));
6947 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
6948 return CreateExpressionFactoryCall ("Constant", args
);
6951 public override Expression
DoResolve (EmitContext ec
)
6953 bool is_generic
= TypeManager
.IsGenericType (member
.DeclaringType
);
6954 MethodInfo mi
= is_generic
? TypeFromHandleGeneric
: TypeFromHandle
;
6957 Type t
= TypeManager
.CoreLookupType ("System.Reflection", TypeName
, Kind
.Class
, true);
6958 Type handle_type
= TypeManager
.CoreLookupType ("System", RuntimeHandleName
, Kind
.Class
, true);
6960 if (t
== null || handle_type
== null)
6963 mi
= TypeManager
.GetPredefinedMethod (t
, GetMethodName
, loc
,
6965 new Type
[] { handle_type, TypeManager.runtime_handle_type }
:
6966 new Type
[] { handle_type }
);
6969 TypeFromHandleGeneric
= mi
;
6971 TypeFromHandle
= mi
;
6974 eclass
= ExprClass
.Value
;
6978 public override void Emit (EmitContext ec
)
6980 bool is_generic
= TypeManager
.IsGenericType (member
.DeclaringType
);
6983 mi
= TypeFromHandleGeneric
;
6984 ec
.ig
.Emit (OpCodes
.Ldtoken
, member
.DeclaringType
);
6986 mi
= TypeFromHandle
;
6989 ec
.ig
.Emit (OpCodes
.Call
, mi
);
6992 protected abstract string GetMethodName { get; }
6993 protected abstract string RuntimeHandleName { get; }
6994 protected abstract MethodInfo TypeFromHandle { get; set; }
6995 protected abstract MethodInfo TypeFromHandleGeneric { get; set; }
6996 protected abstract string TypeName { get; }
6999 class TypeOfField
: TypeOfMember
7001 public TypeOfField (FieldInfo field
, Location loc
)
7006 public override Expression
DoResolve (EmitContext ec
)
7008 if (TypeManager
.fieldinfo_type
== null)
7009 TypeManager
.fieldinfo_type
= TypeManager
.CoreLookupType ("System.Reflection", TypeName
, Kind
.Class
, true);
7011 type
= TypeManager
.fieldinfo_type
;
7012 return base.DoResolve (ec
);
7015 public override void Emit (EmitContext ec
)
7017 ec
.ig
.Emit (OpCodes
.Ldtoken
, (FieldInfo
) member
);
7021 protected override string GetMethodName
{
7022 get { return "GetFieldFromHandle"; }
7025 protected override string RuntimeHandleName
{
7026 get { return "RuntimeFieldHandle"; }
7029 protected override MethodInfo TypeFromHandle
{
7031 return TypeManager
.fieldinfo_get_field_from_handle
;
7034 TypeManager
.fieldinfo_get_field_from_handle
= value;
7038 protected override MethodInfo TypeFromHandleGeneric
{
7040 return TypeManager
.fieldinfo_get_field_from_handle_generic
;
7043 TypeManager
.fieldinfo_get_field_from_handle_generic
= value;
7047 protected override string TypeName
{
7048 get { return "FieldInfo"; }
7053 /// Implements the sizeof expression
7055 public class SizeOf
: Expression
{
7056 readonly Expression QueriedType
;
7059 public SizeOf (Expression queried_type
, Location l
)
7061 this.QueriedType
= queried_type
;
7065 public override Expression
CreateExpressionTree (EmitContext ec
)
7067 Error_PointerInsideExpressionTree ();
7071 public override Expression
DoResolve (EmitContext ec
)
7073 TypeExpr texpr
= QueriedType
.ResolveAsTypeTerminal (ec
, false);
7077 type_queried
= texpr
.Type
;
7078 if (TypeManager
.IsEnumType (type_queried
))
7079 type_queried
= TypeManager
.GetEnumUnderlyingType (type_queried
);
7081 int size_of
= GetTypeSize (type_queried
);
7083 return new IntConstant (size_of
, loc
);
7086 if (!TypeManager
.VerifyUnManaged (type_queried
, loc
)){
7091 Report
.Error (233, loc
,
7092 "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
7093 TypeManager
.CSharpName (type_queried
));
7096 type
= TypeManager
.int32_type
;
7097 eclass
= ExprClass
.Value
;
7101 public override void Emit (EmitContext ec
)
7103 int size
= GetTypeSize (type_queried
);
7106 ec
.ig
.Emit (OpCodes
.Sizeof
, type_queried
);
7108 IntConstant
.EmitInt (ec
.ig
, size
);
7111 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7117 /// Implements the qualified-alias-member (::) expression.
7119 public class QualifiedAliasMember
: MemberAccess
7121 readonly string alias;
7122 public static readonly string GlobalAlias
= "global";
7124 public QualifiedAliasMember (string alias, string identifier
, TypeArguments targs
, Location l
)
7125 : base (null, identifier
, targs
, l
)
7130 public QualifiedAliasMember (string alias, string identifier
, Location l
)
7131 : base (null, identifier
, l
)
7136 public override FullNamedExpression
ResolveAsTypeStep (IResolveContext ec
, bool silent
)
7138 if (alias == GlobalAlias
) {
7139 expr
= GlobalRootNamespace
.Instance
;
7140 return base.ResolveAsTypeStep (ec
, silent
);
7143 int errors
= Report
.Errors
;
7144 expr
= ec
.DeclContainer
.NamespaceEntry
.LookupAlias (alias);
7146 if (errors
== Report
.Errors
)
7147 Report
.Error (432, loc
, "Alias `{0}' not found", alias);
7151 FullNamedExpression fne
= base.ResolveAsTypeStep (ec
, silent
);
7155 if (expr
.eclass
== ExprClass
.Type
) {
7157 Report
.Error (431, loc
,
7158 "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
7166 public override Expression
DoResolve (EmitContext ec
)
7168 return ResolveAsTypeStep (ec
, false);
7171 protected override void Error_IdentifierNotFound (IResolveContext rc
, FullNamedExpression expr_type
, string identifier
)
7173 Report
.Error (687, loc
,
7174 "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
7175 GetSignatureForError ());
7178 public override string GetSignatureForError ()
7181 if (targs
!= null) {
7182 name
= TypeManager
.RemoveGenericArity (Name
) + "<" +
7183 targs
.GetSignatureForError () + ">";
7186 return alias + "::" + name
;
7189 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7196 /// Implements the member access expression
7198 public class MemberAccess
: ATypeNameExpression
{
7199 protected Expression expr
;
7201 public MemberAccess (Expression expr
, string id
)
7202 : base (id
, expr
.Location
)
7207 public MemberAccess (Expression expr
, string identifier
, Location loc
)
7208 : base (identifier
, loc
)
7213 public MemberAccess (Expression expr
, string identifier
, TypeArguments args
, Location loc
)
7214 : base (identifier
, args
, loc
)
7219 Expression
DoResolve (EmitContext ec
, Expression right_side
)
7222 throw new Exception ();
7225 // Resolve the expression with flow analysis turned off, we'll do the definite
7226 // assignment checks later. This is because we don't know yet what the expression
7227 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7228 // definite assignment check on the actual field and not on the whole struct.
7231 SimpleName original
= expr
as SimpleName
;
7232 Expression expr_resolved
= expr
.Resolve (ec
,
7233 ResolveFlags
.VariableOrValue
| ResolveFlags
.Type
|
7234 ResolveFlags
.Intermediate
| ResolveFlags
.DisableStructFlowAnalysis
);
7236 if (expr_resolved
== null)
7239 string LookupIdentifier
= MemberName
.MakeName (Name
, targs
);
7241 Namespace ns
= expr_resolved
as Namespace
;
7243 FullNamedExpression retval
= ns
.Lookup (ec
.DeclContainer
, LookupIdentifier
, loc
);
7246 ns
.Error_NamespaceDoesNotExist (ec
.DeclContainer
, loc
, LookupIdentifier
);
7247 else if (targs
!= null)
7248 retval
= new GenericTypeExpr (retval
.Type
, targs
, loc
).ResolveAsTypeStep (ec
, false);
7253 Type expr_type
= expr_resolved
.Type
;
7254 if (TypeManager
.IsDynamicType (expr_type
)) {
7255 Arguments args
= new Arguments (2);
7256 args
.Add (new Argument (expr_resolved
.Resolve (ec
)));
7257 if (right_side
!= null)
7258 args
.Add (new Argument (right_side
));
7260 return new DynamicMemberBinder (right_side
!= null, Name
, args
, loc
).Resolve (ec
);
7263 if (expr_type
.IsPointer
|| expr_type
== TypeManager
.void_type
||
7264 expr_type
== TypeManager
.null_type
|| expr_type
== InternalType
.AnonymousMethod
) {
7265 Unary
.Error_OperatorCannotBeApplied (loc
, ".", expr_type
);
7269 Constant c
= expr_resolved
as Constant
;
7270 if (c
!= null && c
.GetValue () == null) {
7271 Report
.Warning (1720, 1, loc
, "Expression will always cause a `{0}'",
7272 "System.NullReferenceException");
7275 if (targs
!= null) {
7276 if (!targs
.Resolve (ec
))
7280 Expression member_lookup
;
7281 member_lookup
= MemberLookup (
7282 ec
.ContainerType
, expr_type
, expr_type
, Name
, loc
);
7284 if (member_lookup
== null && targs
!= null) {
7285 member_lookup
= MemberLookup (
7286 ec
.ContainerType
, expr_type
, expr_type
, LookupIdentifier
, loc
);
7289 if (member_lookup
== null) {
7290 ExprClass expr_eclass
= expr_resolved
.eclass
;
7293 // Extension methods are not allowed on all expression types
7295 if (expr_eclass
== ExprClass
.Value
|| expr_eclass
== ExprClass
.Variable
||
7296 expr_eclass
== ExprClass
.IndexerAccess
|| expr_eclass
== ExprClass
.PropertyAccess
||
7297 expr_eclass
== ExprClass
.EventAccess
) {
7298 ExtensionMethodGroupExpr ex_method_lookup
= ec
.TypeContainer
.LookupExtensionMethod (expr_type
, Name
, loc
);
7299 if (ex_method_lookup
!= null) {
7300 ex_method_lookup
.ExtensionExpression
= expr_resolved
;
7302 if (targs
!= null) {
7303 ex_method_lookup
.SetTypeArguments (targs
);
7306 return ex_method_lookup
.DoResolve (ec
);
7310 expr
= expr_resolved
;
7311 member_lookup
= Error_MemberLookupFailed (
7312 ec
.ContainerType
, expr_type
, expr_type
, Name
, null,
7313 AllMemberTypes
, AllBindingFlags
);
7314 if (member_lookup
== null)
7318 TypeExpr texpr
= member_lookup
as TypeExpr
;
7319 if (texpr
!= null) {
7320 if (!(expr_resolved
is TypeExpr
) &&
7321 (original
== null || !original
.IdenticalNameAndTypeName (ec
, expr_resolved
, loc
))) {
7322 Report
.Error (572, loc
, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7323 Name
, member_lookup
.GetSignatureForError ());
7327 if (!texpr
.CheckAccessLevel (ec
.DeclContainer
)) {
7328 Report
.SymbolRelatedToPreviousError (member_lookup
.Type
);
7329 ErrorIsInaccesible (loc
, TypeManager
.CSharpName (member_lookup
.Type
));
7333 GenericTypeExpr ct
= expr_resolved
as GenericTypeExpr
;
7336 // When looking up a nested type in a generic instance
7337 // via reflection, we always get a generic type definition
7338 // and not a generic instance - so we have to do this here.
7340 // See gtest-172-lib.cs and gtest-172.cs for an example.
7342 ct
= new GenericTypeExpr (
7343 member_lookup
.Type
, ct
.TypeArguments
, loc
);
7345 return ct
.ResolveAsTypeStep (ec
, false);
7348 return member_lookup
;
7351 MemberExpr me
= (MemberExpr
) member_lookup
;
7352 me
= me
.ResolveMemberAccess (ec
, expr_resolved
, loc
, original
);
7356 if (targs
!= null) {
7357 me
.SetTypeArguments (targs
);
7360 if (original
!= null && !TypeManager
.IsValueType (expr_type
)) {
7361 if (me
.IsInstance
) {
7362 LocalVariableReference
var = expr_resolved
as LocalVariableReference
;
7363 if (var != null && !var.VerifyAssigned (ec
))
7368 // The following DoResolve/DoResolveLValue will do the definite assignment
7371 if (right_side
!= null)
7372 return me
.DoResolveLValue (ec
, right_side
);
7374 return me
.DoResolve (ec
);
7377 public override Expression
DoResolve (EmitContext ec
)
7379 return DoResolve (ec
, null);
7382 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
7384 return DoResolve (ec
, right_side
);
7387 public override FullNamedExpression
ResolveAsTypeStep (IResolveContext ec
, bool silent
)
7389 return ResolveNamespaceOrType (ec
, silent
);
7392 public FullNamedExpression
ResolveNamespaceOrType (IResolveContext rc
, bool silent
)
7394 FullNamedExpression expr_resolved
= expr
.ResolveAsTypeStep (rc
, silent
);
7396 if (expr_resolved
== null)
7399 string LookupIdentifier
= MemberName
.MakeName (Name
, targs
);
7401 Namespace ns
= expr_resolved
as Namespace
;
7403 FullNamedExpression retval
= ns
.Lookup (rc
.DeclContainer
, LookupIdentifier
, loc
);
7405 if (retval
== null && !silent
)
7406 ns
.Error_NamespaceDoesNotExist (rc
.DeclContainer
, loc
, LookupIdentifier
);
7407 else if (targs
!= null)
7408 retval
= new GenericTypeExpr (retval
.Type
, targs
, loc
).ResolveAsTypeStep (rc
, silent
);
7413 TypeExpr tnew_expr
= expr_resolved
.ResolveAsTypeTerminal (rc
, false);
7414 if (tnew_expr
== null)
7417 if (tnew_expr
is TypeParameterExpr
) {
7418 Report
.Error (704, loc
, "A nested type cannot be specified through a type parameter `{0}'",
7419 tnew_expr
.GetSignatureForError ());
7423 Type expr_type
= tnew_expr
.Type
;
7424 Expression member_lookup
= MemberLookup (
7425 rc
.DeclContainer
.TypeBuilder
, expr_type
, expr_type
, LookupIdentifier
,
7426 MemberTypes
.NestedType
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7427 if (member_lookup
== null) {
7431 Error_IdentifierNotFound (rc
, expr_resolved
, LookupIdentifier
);
7435 TypeExpr texpr
= member_lookup
.ResolveAsTypeTerminal (rc
, false);
7439 TypeArguments the_args
= targs
;
7440 Type declaring_type
= texpr
.Type
.DeclaringType
;
7441 if (TypeManager
.HasGenericArguments (declaring_type
) && !TypeManager
.IsGenericTypeDefinition (expr_type
)) {
7442 while (!TypeManager
.IsEqual (TypeManager
.DropGenericTypeArguments (expr_type
), declaring_type
)) {
7443 expr_type
= expr_type
.BaseType
;
7446 TypeArguments new_args
= new TypeArguments ();
7447 foreach (Type decl
in TypeManager
.GetTypeArguments (expr_type
))
7448 new_args
.Add (new TypeExpression (TypeManager
.TypeToCoreType (decl
), loc
));
7451 new_args
.Add (targs
);
7453 the_args
= new_args
;
7456 if (the_args
!= null) {
7457 GenericTypeExpr ctype
= new GenericTypeExpr (texpr
.Type
, the_args
, loc
);
7458 return ctype
.ResolveAsTypeStep (rc
, false);
7464 protected virtual void Error_IdentifierNotFound (IResolveContext rc
, FullNamedExpression expr_type
, string identifier
)
7466 Expression member_lookup
= MemberLookup (
7467 rc
.DeclContainer
.TypeBuilder
, expr_type
.Type
, expr_type
.Type
, SimpleName
.RemoveGenericArity (identifier
),
7468 MemberTypes
.NestedType
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7470 if (member_lookup
!= null) {
7471 expr_type
= member_lookup
.ResolveAsTypeTerminal (rc
, false);
7472 if (expr_type
== null)
7475 Namespace
.Error_TypeArgumentsCannotBeUsed (expr_type
, loc
);
7479 member_lookup
= MemberLookup (
7480 rc
.DeclContainer
.TypeBuilder
, expr_type
.Type
, expr_type
.Type
, identifier
,
7481 MemberTypes
.All
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7483 if (member_lookup
== null) {
7484 Report
.Error (426, loc
, "The nested type `{0}' does not exist in the type `{1}'",
7485 Name
, expr_type
.GetSignatureForError ());
7487 // TODO: Report.SymbolRelatedToPreviousError
7488 member_lookup
.Error_UnexpectedKind (null, "type", loc
);
7492 protected override void Error_TypeDoesNotContainDefinition (Type type
, string name
)
7494 if (RootContext
.Version
> LanguageVersion
.ISO_2
&&
7495 ((expr
.eclass
& (ExprClass
.Value
| ExprClass
.Variable
)) != 0)) {
7496 Report
.Error (1061, loc
, "Type `{0}' does not contain a definition for `{1}' and no " +
7497 "extension method `{1}' of type `{0}' could be found " +
7498 "(are you missing a using directive or an assembly reference?)",
7499 TypeManager
.CSharpName (type
), name
);
7503 base.Error_TypeDoesNotContainDefinition (type
, name
);
7506 public override string GetSignatureForError ()
7508 return expr
.GetSignatureForError () + "." + base.GetSignatureForError ();
7511 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7513 MemberAccess target
= (MemberAccess
) t
;
7515 target
.expr
= expr
.Clone (clonectx
);
7520 /// Implements checked expressions
7522 public class CheckedExpr
: Expression
{
7524 public Expression Expr
;
7526 public CheckedExpr (Expression e
, Location l
)
7532 public override Expression
CreateExpressionTree (EmitContext ec
)
7534 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, true))
7535 return Expr
.CreateExpressionTree (ec
);
7538 public override Expression
DoResolve (EmitContext ec
)
7540 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, true))
7541 Expr
= Expr
.Resolve (ec
);
7546 if (Expr
is Constant
|| Expr
is MethodGroupExpr
|| Expr
is AnonymousMethodExpression
|| Expr
is DefaultValueExpression
)
7549 eclass
= Expr
.eclass
;
7554 public override void Emit (EmitContext ec
)
7556 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, true))
7560 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
7562 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, true))
7563 Expr
.EmitBranchable (ec
, target
, on_true
);
7566 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
7568 Expr
.MutateHoistedGenericType (storey
);
7571 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7573 CheckedExpr target
= (CheckedExpr
) t
;
7575 target
.Expr
= Expr
.Clone (clonectx
);
7580 /// Implements the unchecked expression
7582 public class UnCheckedExpr
: Expression
{
7584 public Expression Expr
;
7586 public UnCheckedExpr (Expression e
, Location l
)
7592 public override Expression
CreateExpressionTree (EmitContext ec
)
7594 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, false))
7595 return Expr
.CreateExpressionTree (ec
);
7598 public override Expression
DoResolve (EmitContext ec
)
7600 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, false))
7601 Expr
= Expr
.Resolve (ec
);
7606 if (Expr
is Constant
|| Expr
is MethodGroupExpr
|| Expr
is AnonymousMethodExpression
|| Expr
is DefaultValueExpression
)
7609 eclass
= Expr
.eclass
;
7614 public override void Emit (EmitContext ec
)
7616 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, false))
7620 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
7622 using (ec
.With (EmitContext
.Flags
.AllCheckStateFlags
, false))
7623 Expr
.EmitBranchable (ec
, target
, on_true
);
7626 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
7628 Expr
.MutateHoistedGenericType (storey
);
7631 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7633 UnCheckedExpr target
= (UnCheckedExpr
) t
;
7635 target
.Expr
= Expr
.Clone (clonectx
);
7640 /// An Element Access expression.
7642 /// During semantic analysis these are transformed into
7643 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7645 public class ElementAccess
: Expression
{
7646 public Arguments Arguments
;
7647 public Expression Expr
;
7649 public ElementAccess (Expression e
, Arguments args
)
7653 this.Arguments
= args
;
7656 public override Expression
CreateExpressionTree (EmitContext ec
)
7658 Arguments args
= Arguments
.CreateForExpressionTree (ec
, Arguments
,
7659 Expr
.CreateExpressionTree (ec
));
7661 return CreateExpressionFactoryCall ("ArrayIndex", args
);
7664 Expression
MakePointerAccess (EmitContext ec
, Type t
)
7666 if (Arguments
.Count
!= 1){
7667 Error (196, "A pointer must be indexed by only one value");
7671 if (Arguments
[0] is NamedArgument
)
7672 Error_NamedArgument ((NamedArgument
) Arguments
[0]);
7674 Expression p
= new PointerArithmetic (Binary
.Operator
.Addition
, Expr
, Arguments
[0].Expr
.Resolve (ec
), t
, loc
);
7675 return new Indirection (p
, loc
).Resolve (ec
);
7678 public override Expression
DoResolve (EmitContext ec
)
7680 Expr
= Expr
.Resolve (ec
);
7685 // We perform some simple tests, and then to "split" the emit and store
7686 // code we create an instance of a different class, and return that.
7688 // I am experimenting with this pattern.
7692 if (t
== TypeManager
.array_type
){
7693 Report
.Error (21, loc
, "Cannot apply indexing with [] to an expression of type `System.Array'");
7698 return (new ArrayAccess (this, loc
)).Resolve (ec
);
7700 return MakePointerAccess (ec
, t
);
7702 FieldExpr fe
= Expr
as FieldExpr
;
7704 IFixedBuffer ff
= AttributeTester
.GetFixedBuffer (fe
.FieldInfo
);
7706 return MakePointerAccess (ec
, ff
.ElementType
);
7709 return (new IndexerAccess (this, loc
)).Resolve (ec
);
7712 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
7714 Expr
= Expr
.Resolve (ec
);
7720 return (new ArrayAccess (this, loc
)).DoResolveLValue (ec
, right_side
);
7723 return MakePointerAccess (ec
, type
);
7725 if (Expr
.eclass
!= ExprClass
.Variable
&& TypeManager
.IsStruct (type
))
7726 Error_CannotModifyIntermediateExpressionValue (ec
);
7728 return (new IndexerAccess (this, loc
)).DoResolveLValue (ec
, right_side
);
7731 public override void Emit (EmitContext ec
)
7733 throw new Exception ("Should never be reached");
7736 public static void Error_NamedArgument (NamedArgument na
)
7738 Report
.Error (1742, na
.Name
.Location
, "An element access expression cannot use named argument");
7741 public override string GetSignatureForError ()
7743 return Expr
.GetSignatureForError ();
7746 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7748 ElementAccess target
= (ElementAccess
) t
;
7750 target
.Expr
= Expr
.Clone (clonectx
);
7751 if (Arguments
!= null)
7752 target
.Arguments
= Arguments
.Clone (clonectx
);
7757 /// Implements array access
7759 public class ArrayAccess
: Expression
, IAssignMethod
, IMemoryLocation
{
7761 // Points to our "data" repository
7765 LocalTemporary temp
;
7769 public ArrayAccess (ElementAccess ea_data
, Location l
)
7775 public override Expression
CreateExpressionTree (EmitContext ec
)
7777 return ea
.CreateExpressionTree (ec
);
7780 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
7782 return DoResolve (ec
);
7785 public override Expression
DoResolve (EmitContext ec
)
7788 ExprClass eclass
= ea
.Expr
.eclass
;
7790 // As long as the type is valid
7791 if (!(eclass
== ExprClass
.Variable
|| eclass
== ExprClass
.PropertyAccess
||
7792 eclass
== ExprClass
.Value
)) {
7793 ea
.Expr
.Error_UnexpectedKind ("variable or value");
7798 if (eclass
!= ExprClass
.Invalid
)
7801 // dynamic is used per argument in ConvertExpressionToArrayIndex case
7803 ea
.Arguments
.Resolve (ec
, out dynamic);
7805 Type t
= ea
.Expr
.Type
;
7806 int rank
= ea
.Arguments
.Count
;
7807 if (t
.GetArrayRank () != rank
) {
7808 Report
.Error (22, ea
.Location
, "Wrong number of indexes `{0}' inside [], expected `{1}'",
7809 ea
.Arguments
.Count
.ToString (), t
.GetArrayRank ().ToString ());
7813 type
= TypeManager
.GetElementType (t
);
7814 if (type
.IsPointer
&& !ec
.InUnsafe
) {
7815 UnsafeError (ea
.Location
);
7818 foreach (Argument a
in ea
.Arguments
) {
7819 if (a
is NamedArgument
)
7820 ElementAccess
.Error_NamedArgument ((NamedArgument
) a
);
7822 a
.Expr
= ConvertExpressionToArrayIndex (ec
, a
.Expr
);
7825 eclass
= ExprClass
.Variable
;
7831 /// Emits the right opcode to load an object of Type `t'
7832 /// from an array of T
7834 void EmitLoadOpcode (ILGenerator ig
, Type type
, int rank
)
7837 MethodInfo
get = FetchGetMethod ();
7838 ig
.Emit (OpCodes
.Call
, get);
7842 if (type
== TypeManager
.byte_type
|| type
== TypeManager
.bool_type
)
7843 ig
.Emit (OpCodes
.Ldelem_U1
);
7844 else if (type
== TypeManager
.sbyte_type
)
7845 ig
.Emit (OpCodes
.Ldelem_I1
);
7846 else if (type
== TypeManager
.short_type
)
7847 ig
.Emit (OpCodes
.Ldelem_I2
);
7848 else if (type
== TypeManager
.ushort_type
|| type
== TypeManager
.char_type
)
7849 ig
.Emit (OpCodes
.Ldelem_U2
);
7850 else if (type
== TypeManager
.int32_type
)
7851 ig
.Emit (OpCodes
.Ldelem_I4
);
7852 else if (type
== TypeManager
.uint32_type
)
7853 ig
.Emit (OpCodes
.Ldelem_U4
);
7854 else if (type
== TypeManager
.uint64_type
)
7855 ig
.Emit (OpCodes
.Ldelem_I8
);
7856 else if (type
== TypeManager
.int64_type
)
7857 ig
.Emit (OpCodes
.Ldelem_I8
);
7858 else if (type
== TypeManager
.float_type
)
7859 ig
.Emit (OpCodes
.Ldelem_R4
);
7860 else if (type
== TypeManager
.double_type
)
7861 ig
.Emit (OpCodes
.Ldelem_R8
);
7862 else if (type
== TypeManager
.intptr_type
)
7863 ig
.Emit (OpCodes
.Ldelem_I
);
7864 else if (TypeManager
.IsEnumType (type
)){
7865 EmitLoadOpcode (ig
, TypeManager
.GetEnumUnderlyingType (type
), rank
);
7866 } else if (TypeManager
.IsStruct (type
)){
7867 ig
.Emit (OpCodes
.Ldelema
, type
);
7868 ig
.Emit (OpCodes
.Ldobj
, type
);
7870 } else if (type
.IsGenericParameter
) {
7871 ig
.Emit (OpCodes
.Ldelem
, type
);
7873 } else if (type
.IsPointer
)
7874 ig
.Emit (OpCodes
.Ldelem_I
);
7876 ig
.Emit (OpCodes
.Ldelem_Ref
);
7879 protected override void Error_NegativeArrayIndex (Location loc
)
7881 Report
.Warning (251, 2, loc
, "Indexing an array with a negative index (array indices always start at zero)");
7885 /// Returns the right opcode to store an object of Type `t'
7886 /// from an array of T.
7888 static public OpCode
GetStoreOpcode (Type t
, out bool is_stobj
, out bool has_type_arg
)
7890 //Console.WriteLine (new System.Diagnostics.StackTrace ());
7891 has_type_arg
= false; is_stobj
= false;
7892 t
= TypeManager
.TypeToCoreType (t
);
7893 if (TypeManager
.IsEnumType (t
))
7894 t
= TypeManager
.GetEnumUnderlyingType (t
);
7895 if (t
== TypeManager
.byte_type
|| t
== TypeManager
.sbyte_type
||
7896 t
== TypeManager
.bool_type
)
7897 return OpCodes
.Stelem_I1
;
7898 else if (t
== TypeManager
.short_type
|| t
== TypeManager
.ushort_type
||
7899 t
== TypeManager
.char_type
)
7900 return OpCodes
.Stelem_I2
;
7901 else if (t
== TypeManager
.int32_type
|| t
== TypeManager
.uint32_type
)
7902 return OpCodes
.Stelem_I4
;
7903 else if (t
== TypeManager
.int64_type
|| t
== TypeManager
.uint64_type
)
7904 return OpCodes
.Stelem_I8
;
7905 else if (t
== TypeManager
.float_type
)
7906 return OpCodes
.Stelem_R4
;
7907 else if (t
== TypeManager
.double_type
)
7908 return OpCodes
.Stelem_R8
;
7909 else if (t
== TypeManager
.intptr_type
) {
7910 has_type_arg
= true;
7912 return OpCodes
.Stobj
;
7913 } else if (TypeManager
.IsStruct (t
)) {
7914 has_type_arg
= true;
7916 return OpCodes
.Stobj
;
7918 } else if (t
.IsGenericParameter
) {
7919 has_type_arg
= true;
7920 return OpCodes
.Stelem
;
7923 } else if (t
.IsPointer
)
7924 return OpCodes
.Stelem_I
;
7926 return OpCodes
.Stelem_Ref
;
7929 MethodInfo
FetchGetMethod ()
7931 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
7932 int arg_count
= ea
.Arguments
.Count
;
7933 Type
[] args
= new Type
[arg_count
];
7936 for (int i
= 0; i
< arg_count
; i
++){
7937 //args [i++] = a.Type;
7938 args
[i
] = TypeManager
.int32_type
;
7941 get = mb
.GetArrayMethod (
7942 ea
.Expr
.Type
, "Get",
7943 CallingConventions
.HasThis
|
7944 CallingConventions
.Standard
,
7950 MethodInfo
FetchAddressMethod ()
7952 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
7953 int arg_count
= ea
.Arguments
.Count
;
7954 Type
[] args
= new Type
[arg_count
];
7958 ret_type
= TypeManager
.GetReferenceType (type
);
7960 for (int i
= 0; i
< arg_count
; i
++){
7961 //args [i++] = a.Type;
7962 args
[i
] = TypeManager
.int32_type
;
7965 address
= mb
.GetArrayMethod (
7966 ea
.Expr
.Type
, "Address",
7967 CallingConventions
.HasThis
|
7968 CallingConventions
.Standard
,
7975 // Load the array arguments into the stack.
7977 void LoadArrayAndArguments (EmitContext ec
)
7981 for (int i
= 0; i
< ea
.Arguments
.Count
; ++i
) {
7982 ea
.Arguments
[i
].Emit (ec
);
7986 public void Emit (EmitContext ec
, bool leave_copy
)
7988 int rank
= ea
.Expr
.Type
.GetArrayRank ();
7989 ILGenerator ig
= ec
.ig
;
7992 LoadFromPtr (ig
, this.type
);
7994 LoadArrayAndArguments (ec
);
7995 EmitLoadOpcode (ig
, type
, rank
);
7999 ig
.Emit (OpCodes
.Dup
);
8000 temp
= new LocalTemporary (this.type
);
8005 public override void Emit (EmitContext ec
)
8010 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
8012 int rank
= ea
.Expr
.Type
.GetArrayRank ();
8013 ILGenerator ig
= ec
.ig
;
8014 Type t
= source
.Type
;
8015 prepared
= prepare_for_load
;
8018 AddressOf (ec
, AddressOp
.LoadStore
);
8019 ec
.ig
.Emit (OpCodes
.Dup
);
8021 LoadArrayAndArguments (ec
);
8025 bool is_stobj
, has_type_arg
;
8026 OpCode op
= GetStoreOpcode (t
, out is_stobj
, out has_type_arg
);
8030 // The stobj opcode used by value types will need
8031 // an address on the stack, not really an array/array
8035 ig
.Emit (OpCodes
.Ldelema
, t
);
8040 ec
.ig
.Emit (OpCodes
.Dup
);
8041 temp
= new LocalTemporary (this.type
);
8046 StoreFromPtr (ig
, t
);
8048 ig
.Emit (OpCodes
.Stobj
, t
);
8049 else if (has_type_arg
)
8056 ec
.ig
.Emit (OpCodes
.Dup
);
8057 temp
= new LocalTemporary (this.type
);
8062 StoreFromPtr (ig
, t
);
8064 int arg_count
= ea
.Arguments
.Count
;
8065 Type
[] args
= new Type
[arg_count
+ 1];
8066 for (int i
= 0; i
< arg_count
; i
++) {
8067 //args [i++] = a.Type;
8068 args
[i
] = TypeManager
.int32_type
;
8070 args
[arg_count
] = type
;
8072 MethodInfo
set = RootContext
.ToplevelTypes
.Builder
.GetArrayMethod (
8073 ea
.Expr
.Type
, "Set",
8074 CallingConventions
.HasThis
|
8075 CallingConventions
.Standard
,
8076 TypeManager
.void_type
, args
);
8078 ig
.Emit (OpCodes
.Call
, set);
8088 public void EmitNew (EmitContext ec
, New source
, bool leave_copy
)
8090 if (!source
.Emit (ec
, this)) {
8092 throw new NotImplementedException ();
8097 throw new NotImplementedException ();
8100 public void AddressOf (EmitContext ec
, AddressOp mode
)
8102 int rank
= ea
.Expr
.Type
.GetArrayRank ();
8103 ILGenerator ig
= ec
.ig
;
8105 LoadArrayAndArguments (ec
);
8108 ig
.Emit (OpCodes
.Ldelema
, type
);
8110 MethodInfo address
= FetchAddressMethod ();
8111 ig
.Emit (OpCodes
.Call
, address
);
8115 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
8117 type
= storey
.MutateType (type
);
8118 ea
.Expr
.Type
= storey
.MutateType (ea
.Expr
.Type
);
8123 /// Expressions that represent an indexer call.
8125 public class IndexerAccess
: Expression
, IAssignMethod
8127 class IndexerMethodGroupExpr
: MethodGroupExpr
8129 public IndexerMethodGroupExpr (Indexers indexers
, Location loc
)
8132 Methods
= (MethodBase
[]) indexers
.Methods
.ToArray (typeof (MethodBase
));
8135 public override string Name
{
8141 protected override int GetApplicableParametersCount (MethodBase method
, AParametersCollection parameters
)
8144 // Here is the trick, decrease number of arguments by 1 when only
8145 // available property method is setter. This makes overload resolution
8146 // work correctly for indexers.
8149 if (method
.Name
[0] == 'g')
8150 return parameters
.Count
;
8152 return parameters
.Count
- 1;
8158 // Contains either property getter or setter
8159 public ArrayList Methods
;
8160 public ArrayList Properties
;
8166 void Append (Type caller_type
, MemberInfo
[] mi
)
8171 foreach (PropertyInfo property
in mi
) {
8172 MethodInfo accessor
= property
.GetGetMethod (true);
8173 if (accessor
== null)
8174 accessor
= property
.GetSetMethod (true);
8176 if (Methods
== null) {
8177 Methods
= new ArrayList ();
8178 Properties
= new ArrayList ();
8181 Methods
.Add (accessor
);
8182 Properties
.Add (property
);
8186 static MemberInfo
[] GetIndexersForTypeOrInterface (Type caller_type
, Type lookup_type
)
8188 string p_name
= TypeManager
.IndexerPropertyName (lookup_type
);
8190 return TypeManager
.MemberLookup (
8191 caller_type
, caller_type
, lookup_type
, MemberTypes
.Property
,
8192 BindingFlags
.Public
| BindingFlags
.Instance
|
8193 BindingFlags
.DeclaredOnly
, p_name
, null);
8196 public static Indexers
GetIndexersForType (Type caller_type
, Type lookup_type
)
8198 Indexers ix
= new Indexers ();
8200 if (TypeManager
.IsGenericParameter (lookup_type
)) {
8201 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (lookup_type
);
8205 if (gc
.HasClassConstraint
) {
8206 Type class_contraint
= gc
.ClassConstraint
;
8207 while (class_contraint
!= TypeManager
.object_type
&& class_contraint
!= null) {
8208 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, class_contraint
));
8209 class_contraint
= class_contraint
.BaseType
;
8213 Type
[] ifaces
= gc
.InterfaceConstraints
;
8214 foreach (Type itype
in ifaces
)
8215 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, itype
));
8220 Type copy
= lookup_type
;
8221 while (copy
!= TypeManager
.object_type
&& copy
!= null){
8222 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, copy
));
8223 copy
= copy
.BaseType
;
8226 if (lookup_type
.IsInterface
) {
8227 Type
[] ifaces
= TypeManager
.GetInterfaces (lookup_type
);
8228 if (ifaces
!= null) {
8229 foreach (Type itype
in ifaces
)
8230 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, itype
));
8245 // Points to our "data" repository
8247 MethodInfo
get, set;
8248 bool is_base_indexer
;
8250 LocalTemporary temp
;
8251 LocalTemporary prepared_value
;
8252 Expression set_expr
;
8254 protected Type indexer_type
;
8255 protected Type current_type
;
8256 protected Expression instance_expr
;
8257 protected Arguments arguments
;
8259 public IndexerAccess (ElementAccess ea
, Location loc
)
8260 : this (ea
.Expr
, false, loc
)
8262 this.arguments
= ea
.Arguments
;
8265 protected IndexerAccess (Expression instance_expr
, bool is_base_indexer
,
8268 this.instance_expr
= instance_expr
;
8269 this.is_base_indexer
= is_base_indexer
;
8270 this.eclass
= ExprClass
.Value
;
8274 static string GetAccessorName (AccessorType at
)
8276 if (at
== AccessorType
.Set
)
8279 if (at
== AccessorType
.Get
)
8282 throw new NotImplementedException (at
.ToString ());
8285 public override Expression
CreateExpressionTree (EmitContext ec
)
8287 Arguments args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
8288 instance_expr
.CreateExpressionTree (ec
),
8289 new TypeOfMethod (get, loc
));
8291 return CreateExpressionFactoryCall ("Call", args
);
8294 protected virtual void CommonResolve (EmitContext ec
)
8296 indexer_type
= instance_expr
.Type
;
8297 current_type
= ec
.ContainerType
;
8300 public override Expression
DoResolve (EmitContext ec
)
8302 return ResolveAccessor (ec
, AccessorType
.Get
, null);
8305 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
8307 if (right_side
== EmptyExpression
.OutAccess
) {
8308 Report
.Error (206, loc
, "A property or indexer `{0}' may not be passed as an out or ref parameter",
8309 GetSignatureForError ());
8313 // if the indexer returns a value type, and we try to set a field in it
8314 if (right_side
== EmptyExpression
.LValueMemberAccess
|| right_side
== EmptyExpression
.LValueMemberOutAccess
) {
8315 Error_CannotModifyIntermediateExpressionValue (ec
);
8318 Expression e
= ResolveAccessor (ec
, AccessorType
.Set
, right_side
);
8322 set_expr
= Convert
.ImplicitConversion (ec
, right_side
, type
, loc
);
8326 Expression
ResolveAccessor (EmitContext ec
, AccessorType accessorType
, Expression right_side
)
8329 arguments
.Resolve (ec
, out dynamic);
8330 if (dynamic || TypeManager
.IsDynamicType (indexer_type
)) {
8331 int additional
= right_side
== null ? 1 : 2;
8332 Arguments args
= new Arguments (arguments
.Count
+ additional
);
8333 if (is_base_indexer
) {
8334 Report
.Error (1972, loc
, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
8336 args
.Add (new Argument (instance_expr
));
8338 args
.AddRange (arguments
);
8339 if (right_side
!= null)
8340 args
.Add (new Argument (right_side
));
8342 return new DynamicIndexBinder (accessorType
== AccessorType
.Set
, args
, loc
).Resolve (ec
);
8347 Indexers ilist
= Indexers
.GetIndexersForType (current_type
, indexer_type
);
8348 if (ilist
.Methods
== null) {
8349 Report
.Error (21, loc
, "Cannot apply indexing with [] to an expression of type `{0}'",
8350 TypeManager
.CSharpName (indexer_type
));
8354 MethodGroupExpr mg
= new IndexerMethodGroupExpr (ilist
, loc
);
8355 mg
= mg
.OverloadResolve (ec
, ref arguments
, false, loc
);
8359 MethodInfo mi
= (MethodInfo
) mg
;
8360 PropertyInfo pi
= null;
8361 for (int i
= 0; i
< ilist
.Methods
.Count
; ++i
) {
8362 if (ilist
.Methods
[i
] == mi
) {
8363 pi
= (PropertyInfo
) ilist
.Properties
[i
];
8368 type
= TypeManager
.TypeToCoreType (pi
.PropertyType
);
8369 if (type
.IsPointer
&& !ec
.InUnsafe
)
8372 MethodInfo accessor
;
8373 if (accessorType
== AccessorType
.Get
) {
8374 accessor
= get = pi
.GetGetMethod (true);
8376 accessor
= set = pi
.GetSetMethod (true);
8377 if (accessor
== null && pi
.GetGetMethod (true) != null) {
8378 Report
.SymbolRelatedToPreviousError (pi
);
8379 Report
.Error (200, loc
, "The read only property or indexer `{0}' cannot be assigned to",
8380 TypeManager
.GetFullNameSignature (pi
));
8385 if (accessor
== null) {
8386 Report
.SymbolRelatedToPreviousError (pi
);
8387 Report
.Error (154, loc
, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
8388 TypeManager
.GetFullNameSignature (pi
), GetAccessorName (accessorType
));
8393 // Only base will allow this invocation to happen.
8395 if (accessor
.IsAbstract
&& this is BaseIndexerAccess
) {
8396 Error_CannotCallAbstractBase (TypeManager
.GetFullNameSignature (pi
));
8399 bool must_do_cs1540_check
;
8400 if (!IsAccessorAccessible (ec
.ContainerType
, accessor
, out must_do_cs1540_check
)) {
8402 set = pi
.GetSetMethod (true);
8404 get = pi
.GetGetMethod (true);
8406 if (set != null && get != null &&
8407 (set.Attributes
& MethodAttributes
.MemberAccessMask
) != (get.Attributes
& MethodAttributes
.MemberAccessMask
)) {
8408 Report
.SymbolRelatedToPreviousError (accessor
);
8409 Report
.Error (271, loc
, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
8410 TypeManager
.GetFullNameSignature (pi
), GetAccessorName (accessorType
));
8412 Report
.SymbolRelatedToPreviousError (pi
);
8413 ErrorIsInaccesible (loc
, TypeManager
.GetFullNameSignature (pi
));
8417 instance_expr
.CheckMarshalByRefAccess (ec
);
8418 eclass
= ExprClass
.IndexerAccess
;
8422 public void Emit (EmitContext ec
, bool leave_copy
)
8425 prepared_value
.Emit (ec
);
8427 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, get,
8428 arguments
, loc
, false, false);
8432 ec
.ig
.Emit (OpCodes
.Dup
);
8433 temp
= new LocalTemporary (Type
);
8439 // source is ignored, because we already have a copy of it from the
8440 // LValue resolution and we have already constructed a pre-cached
8441 // version of the arguments (ea.set_arguments);
8443 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
8445 prepared
= prepare_for_load
;
8446 Expression
value = set_expr
;
8449 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, get,
8450 arguments
, loc
, true, false);
8452 prepared_value
= new LocalTemporary (type
);
8453 prepared_value
.Store (ec
);
8455 prepared_value
.Release (ec
);
8458 ec
.ig
.Emit (OpCodes
.Dup
);
8459 temp
= new LocalTemporary (Type
);
8462 } else if (leave_copy
) {
8463 temp
= new LocalTemporary (Type
);
8470 arguments
.Add (new Argument (value));
8472 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, set, arguments
, loc
, false, prepared
);
8480 public override void Emit (EmitContext ec
)
8485 public override string GetSignatureForError ()
8487 return TypeManager
.CSharpSignature (get != null ? get : set, false);
8490 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
8493 get = storey
.MutateGenericMethod (get);
8495 set = storey
.MutateGenericMethod (set);
8497 instance_expr
.MutateHoistedGenericType (storey
);
8498 if (arguments
!= null)
8499 arguments
.MutateHoistedGenericType (storey
);
8501 type
= storey
.MutateType (type
);
8504 protected override void CloneTo (CloneContext clonectx
, Expression t
)
8506 IndexerAccess target
= (IndexerAccess
) t
;
8508 if (arguments
!= null)
8509 target
.arguments
= arguments
.Clone (clonectx
);
8511 if (instance_expr
!= null)
8512 target
.instance_expr
= instance_expr
.Clone (clonectx
);
8517 /// The base operator for method names
8519 public class BaseAccess
: Expression
{
8520 public readonly string Identifier
;
8523 public BaseAccess (string member
, Location l
)
8525 this.Identifier
= member
;
8529 public BaseAccess (string member
, TypeArguments args
, Location l
)
8535 public override Expression
CreateExpressionTree (EmitContext ec
)
8537 throw new NotSupportedException ("ET");
8540 public override Expression
DoResolve (EmitContext ec
)
8542 Expression c
= CommonResolve (ec
);
8548 // MethodGroups use this opportunity to flag an error on lacking ()
8550 if (!(c
is MethodGroupExpr
))
8551 return c
.Resolve (ec
);
8555 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
8557 Expression c
= CommonResolve (ec
);
8563 // MethodGroups use this opportunity to flag an error on lacking ()
8565 if (! (c
is MethodGroupExpr
))
8566 return c
.DoResolveLValue (ec
, right_side
);
8571 Expression
CommonResolve (EmitContext ec
)
8573 Expression member_lookup
;
8574 Type current_type
= ec
.ContainerType
;
8575 Type base_type
= current_type
.BaseType
;
8577 if (!This
.IsThisAvailable (ec
)) {
8579 Error (1511, "Keyword `base' is not available in a static method");
8581 Error (1512, "Keyword `base' is not available in the current context");
8586 member_lookup
= MemberLookup (ec
.ContainerType
, null, base_type
, Identifier
,
8587 AllMemberTypes
, AllBindingFlags
, loc
);
8588 if (member_lookup
== null) {
8589 Error_MemberLookupFailed (ec
.ContainerType
, base_type
, base_type
, Identifier
,
8590 null, AllMemberTypes
, AllBindingFlags
);
8597 left
= new TypeExpression (base_type
, loc
);
8599 left
= ec
.GetThis (loc
);
8601 MemberExpr me
= (MemberExpr
) member_lookup
;
8602 me
= me
.ResolveMemberAccess (ec
, left
, loc
, null);
8609 me
.SetTypeArguments (args
);
8615 public override void Emit (EmitContext ec
)
8617 throw new Exception ("Should never be called");
8620 protected override void CloneTo (CloneContext clonectx
, Expression t
)
8622 BaseAccess target
= (BaseAccess
) t
;
8625 target
.args
= args
.Clone ();
8630 /// The base indexer operator
8632 public class BaseIndexerAccess
: IndexerAccess
{
8633 public BaseIndexerAccess (Arguments args
, Location loc
)
8634 : base (null, true, loc
)
8636 this.arguments
= args
;
8639 protected override void CommonResolve (EmitContext ec
)
8641 instance_expr
= ec
.GetThis (loc
);
8643 current_type
= ec
.ContainerType
.BaseType
;
8644 indexer_type
= current_type
;
8647 public override Expression
CreateExpressionTree (EmitContext ec
)
8649 MemberExpr
.Error_BaseAccessInExpressionTree (loc
);
8650 return base.CreateExpressionTree (ec
);
8655 /// This class exists solely to pass the Type around and to be a dummy
8656 /// that can be passed to the conversion functions (this is used by
8657 /// foreach implementation to typecast the object return value from
8658 /// get_Current into the proper type. All code has been generated and
8659 /// we only care about the side effect conversions to be performed
8661 /// This is also now used as a placeholder where a no-action expression
8662 /// is needed (the `New' class).
8664 public class EmptyExpression
: Expression
{
8665 public static readonly Expression Null
= new EmptyExpression ();
8667 public static readonly EmptyExpression OutAccess
= new EmptyExpression ();
8668 public static readonly EmptyExpression LValueMemberAccess
= new EmptyExpression ();
8669 public static readonly EmptyExpression LValueMemberOutAccess
= new EmptyExpression ();
8670 public static readonly EmptyExpression UnaryAddress
= new EmptyExpression ();
8672 static EmptyExpression temp
= new EmptyExpression ();
8673 public static EmptyExpression
Grab ()
8675 EmptyExpression retval
= temp
== null ? new EmptyExpression () : temp
;
8680 public static void Release (EmptyExpression e
)
8687 // FIXME: Don't set to object
8688 type
= TypeManager
.object_type
;
8689 eclass
= ExprClass
.Value
;
8690 loc
= Location
.Null
;
8693 public EmptyExpression (Type t
)
8696 eclass
= ExprClass
.Value
;
8697 loc
= Location
.Null
;
8700 public override Expression
CreateExpressionTree (EmitContext ec
)
8702 throw new NotSupportedException ("ET");
8705 public override Expression
DoResolve (EmitContext ec
)
8710 public override void Emit (EmitContext ec
)
8712 // nothing, as we only exist to not do anything.
8715 public override void EmitSideEffect (EmitContext ec
)
8720 // This is just because we might want to reuse this bad boy
8721 // instead of creating gazillions of EmptyExpressions.
8722 // (CanImplicitConversion uses it)
8724 public void SetType (Type t
)
8731 // Empty statement expression
8733 public sealed class EmptyExpressionStatement
: ExpressionStatement
8735 public static readonly EmptyExpressionStatement Instance
= new EmptyExpressionStatement ();
8737 private EmptyExpressionStatement ()
8739 eclass
= ExprClass
.Value
;
8740 loc
= Location
.Null
;
8743 public override Expression
CreateExpressionTree (EmitContext ec
)
8748 public override void EmitStatement (EmitContext ec
)
8753 public override Expression
DoResolve (EmitContext ec
)
8755 type
= TypeManager
.object_type
;
8759 public override void Emit (EmitContext ec
)
8765 public class UserCast
: Expression
{
8769 public UserCast (MethodInfo method
, Expression source
, Location l
)
8771 this.method
= method
;
8772 this.source
= source
;
8773 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
8777 public Expression Source
{
8783 public override Expression
CreateExpressionTree (EmitContext ec
)
8785 Arguments args
= new Arguments (3);
8786 args
.Add (new Argument (source
.CreateExpressionTree (ec
)));
8787 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
8788 args
.Add (new Argument (new TypeOfMethod (method
, loc
)));
8789 return CreateExpressionFactoryCall ("Convert", args
);
8792 public override Expression
DoResolve (EmitContext ec
)
8794 ObsoleteAttribute oa
= AttributeTester
.GetMethodObsoleteAttribute (method
);
8796 AttributeTester
.Report_ObsoleteMessage (oa
, GetSignatureForError (), loc
);
8798 eclass
= ExprClass
.Value
;
8802 public override void Emit (EmitContext ec
)
8805 ec
.ig
.Emit (OpCodes
.Call
, method
);
8808 public override string GetSignatureForError ()
8810 return TypeManager
.CSharpSignature (method
);
8813 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
8815 source
.MutateHoistedGenericType (storey
);
8816 method
= storey
.MutateGenericMethod (method
);
8821 // This class is used to "construct" the type during a typecast
8822 // operation. Since the Type.GetType class in .NET can parse
8823 // the type specification, we just use this to construct the type
8824 // one bit at a time.
8826 public class ComposedCast
: TypeExpr
{
8827 FullNamedExpression left
;
8830 public ComposedCast (FullNamedExpression left
, string dim
)
8831 : this (left
, dim
, left
.Location
)
8835 public ComposedCast (FullNamedExpression left
, string dim
, Location l
)
8842 protected override TypeExpr
DoResolveAsTypeStep (IResolveContext ec
)
8844 TypeExpr lexpr
= left
.ResolveAsTypeTerminal (ec
, false);
8848 Type ltype
= lexpr
.Type
;
8849 if ((dim
.Length
> 0) && (dim
[0] == '?')) {
8850 TypeExpr nullable
= new Nullable
.NullableType (lexpr
, loc
);
8852 nullable
= new ComposedCast (nullable
, dim
.Substring (1), loc
);
8853 return nullable
.ResolveAsTypeTerminal (ec
, false);
8856 if (dim
== "*" && !TypeManager
.VerifyUnManaged (ltype
, loc
))
8859 if (dim
.Length
!= 0 && dim
[0] == '[') {
8860 if (TypeManager
.IsSpecialType (ltype
)) {
8861 Report
.Error (611, loc
, "Array elements cannot be of type `{0}'", TypeManager
.CSharpName (ltype
));
8865 if ((ltype
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
8866 Report
.SymbolRelatedToPreviousError (ltype
);
8867 Report
.Error (719, loc
, "Array elements cannot be of static type `{0}'",
8868 TypeManager
.CSharpName (ltype
));
8873 type
= TypeManager
.GetConstructedType (ltype
, dim
);
8878 throw new InternalErrorException ("Couldn't create computed type " + ltype
+ dim
);
8880 if (type
.IsPointer
&& !ec
.IsInUnsafeScope
){
8884 eclass
= ExprClass
.Type
;
8888 public override string GetSignatureForError ()
8890 return left
.GetSignatureForError () + dim
;
8893 public override TypeExpr
ResolveAsTypeTerminal (IResolveContext ec
, bool silent
)
8895 return ResolveAsBaseTerminal (ec
, silent
);
8899 public class FixedBufferPtr
: Expression
{
8902 public FixedBufferPtr (Expression array
, Type array_type
, Location l
)
8907 type
= TypeManager
.GetPointerType (array_type
);
8908 eclass
= ExprClass
.Value
;
8911 public override Expression
CreateExpressionTree (EmitContext ec
)
8913 Error_PointerInsideExpressionTree ();
8917 public override void Emit(EmitContext ec
)
8922 public override Expression
DoResolve (EmitContext ec
)
8925 // We are born fully resolved
8933 // This class is used to represent the address of an array, used
8934 // only by the Fixed statement, this generates "&a [0]" construct
8935 // for fixed (char *pa = a)
8937 public class ArrayPtr
: FixedBufferPtr
{
8940 public ArrayPtr (Expression array
, Type array_type
, Location l
):
8941 base (array
, array_type
, l
)
8943 this.array_type
= array_type
;
8946 public override void Emit (EmitContext ec
)
8950 ILGenerator ig
= ec
.ig
;
8951 IntLiteral
.EmitInt (ig
, 0);
8952 ig
.Emit (OpCodes
.Ldelema
, array_type
);
8957 // Encapsulates a conversion rules required for array indexes
8959 public class ArrayIndexCast
: TypeCast
8961 public ArrayIndexCast (Expression expr
)
8962 : base (expr
, expr
.Type
)
8966 public override Expression
CreateExpressionTree (EmitContext ec
)
8968 Arguments args
= new Arguments (2);
8969 args
.Add (new Argument (child
.CreateExpressionTree (ec
)));
8970 args
.Add (new Argument (new TypeOf (new TypeExpression (TypeManager
.int32_type
, loc
), loc
)));
8971 return CreateExpressionFactoryCall ("ConvertChecked", args
);
8974 public override void Emit (EmitContext ec
)
8978 if (type
== TypeManager
.int32_type
)
8981 if (type
== TypeManager
.uint32_type
)
8982 ec
.ig
.Emit (OpCodes
.Conv_U
);
8983 else if (type
== TypeManager
.int64_type
)
8984 ec
.ig
.Emit (OpCodes
.Conv_Ovf_I
);
8985 else if (type
== TypeManager
.uint64_type
)
8986 ec
.ig
.Emit (OpCodes
.Conv_Ovf_I_Un
);
8988 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type
);
8993 // Implements the `stackalloc' keyword
8995 public class StackAlloc
: Expression
{
9000 public StackAlloc (Expression type
, Expression count
, Location l
)
9007 public override Expression
CreateExpressionTree (EmitContext ec
)
9009 throw new NotSupportedException ("ET");
9012 public override Expression
DoResolve (EmitContext ec
)
9014 count
= count
.Resolve (ec
);
9018 if (count
.Type
!= TypeManager
.uint32_type
){
9019 count
= Convert
.ImplicitConversionRequired (ec
, count
, TypeManager
.int32_type
, loc
);
9024 Constant c
= count
as Constant
;
9025 if (c
!= null && c
.IsNegative
) {
9026 Report
.Error (247, loc
, "Cannot use a negative size with stackalloc");
9030 if (ec
.InCatch
|| ec
.InFinally
) {
9031 Error (255, "Cannot use stackalloc in finally or catch");
9035 TypeExpr texpr
= t
.ResolveAsTypeTerminal (ec
, false);
9041 if (!TypeManager
.VerifyUnManaged (otype
, loc
))
9044 type
= TypeManager
.GetPointerType (otype
);
9045 eclass
= ExprClass
.Value
;
9050 public override void Emit (EmitContext ec
)
9052 int size
= GetTypeSize (otype
);
9053 ILGenerator ig
= ec
.ig
;
9058 ig
.Emit (OpCodes
.Sizeof
, otype
);
9060 IntConstant
.EmitInt (ig
, size
);
9062 ig
.Emit (OpCodes
.Mul_Ovf_Un
);
9063 ig
.Emit (OpCodes
.Localloc
);
9066 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9068 StackAlloc target
= (StackAlloc
) t
;
9069 target
.count
= count
.Clone (clonectx
);
9070 target
.t
= t
.Clone (clonectx
);
9075 // An object initializer expression
9077 public class ElementInitializer
: Assign
9079 public readonly string Name
;
9081 public ElementInitializer (string name
, Expression initializer
, Location loc
)
9082 : base (null, initializer
, loc
)
9087 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9089 ElementInitializer target
= (ElementInitializer
) t
;
9090 target
.source
= source
.Clone (clonectx
);
9093 public override Expression
CreateExpressionTree (EmitContext ec
)
9095 Arguments args
= new Arguments (2);
9096 FieldExpr fe
= target
as FieldExpr
;
9098 args
.Add (new Argument (fe
.CreateTypeOfExpression ()));
9100 args
.Add (new Argument (((PropertyExpr
)target
).CreateSetterTypeOfExpression ()));
9102 args
.Add (new Argument (source
.CreateExpressionTree (ec
)));
9103 return CreateExpressionFactoryCall (
9104 source
is CollectionOrObjectInitializers
? "ListBind" : "Bind",
9108 public override Expression
DoResolve (EmitContext ec
)
9111 return EmptyExpressionStatement
.Instance
;
9113 MemberExpr me
= MemberLookupFinal (ec
, ec
.CurrentInitializerVariable
.Type
, ec
.CurrentInitializerVariable
.Type
,
9114 Name
, MemberTypes
.Field
| MemberTypes
.Property
, BindingFlags
.Public
| BindingFlags
.Instance
, loc
) as MemberExpr
;
9120 me
.InstanceExpression
= ec
.CurrentInitializerVariable
;
9122 if (source
is CollectionOrObjectInitializers
) {
9123 Expression previous
= ec
.CurrentInitializerVariable
;
9124 ec
.CurrentInitializerVariable
= target
;
9125 source
= source
.Resolve (ec
);
9126 ec
.CurrentInitializerVariable
= previous
;
9130 eclass
= source
.eclass
;
9135 Expression expr
= base.DoResolve (ec
);
9140 // Ignore field initializers with default value
9142 Constant c
= source
as Constant
;
9143 if (c
!= null && c
.IsDefaultInitializer (type
) && target
.eclass
== ExprClass
.Variable
)
9144 return EmptyExpressionStatement
.Instance
.DoResolve (ec
);
9149 protected override Expression
Error_MemberLookupFailed (Type type
, MemberInfo
[] members
)
9151 MemberInfo member
= members
[0];
9152 if (member
.MemberType
!= MemberTypes
.Property
&& member
.MemberType
!= MemberTypes
.Field
)
9153 Report
.Error (1913, loc
, "Member `{0}' cannot be initialized. An object " +
9154 "initializer may only be used for fields, or properties", TypeManager
.GetFullNameSignature (member
));
9156 Report
.Error (1914, loc
, " Static field or property `{0}' cannot be assigned in an object initializer",
9157 TypeManager
.GetFullNameSignature (member
));
9162 public override void EmitStatement (EmitContext ec
)
9164 if (source
is CollectionOrObjectInitializers
)
9167 base.EmitStatement (ec
);
9172 // A collection initializer expression
9174 class CollectionElementInitializer
: Invocation
9176 public class ElementInitializerArgument
: Argument
9178 public ElementInitializerArgument (Expression e
)
9184 sealed class AddMemberAccess
: MemberAccess
9186 public AddMemberAccess (Expression expr
, Location loc
)
9187 : base (expr
, "Add", loc
)
9191 protected override void Error_TypeDoesNotContainDefinition (Type type
, string name
)
9193 if (TypeManager
.HasElementType (type
))
9196 base.Error_TypeDoesNotContainDefinition (type
, name
);
9200 public CollectionElementInitializer (Expression argument
)
9201 : base (null, new Arguments (1))
9203 base.arguments
.Add (new ElementInitializerArgument (argument
));
9204 this.loc
= argument
.Location
;
9207 public CollectionElementInitializer (ArrayList arguments
, Location loc
)
9208 : base (null, new Arguments (arguments
.Count
))
9210 foreach (Expression e
in arguments
)
9211 base.arguments
.Add (new ElementInitializerArgument (e
));
9216 public override Expression
CreateExpressionTree (EmitContext ec
)
9218 Arguments args
= new Arguments (2);
9219 args
.Add (new Argument (mg
.CreateExpressionTree (ec
)));
9221 ArrayList expr_initializers
= new ArrayList (arguments
.Count
);
9222 foreach (Argument a
in arguments
)
9223 expr_initializers
.Add (a
.CreateExpressionTree (ec
));
9225 args
.Add (new Argument (new ArrayCreation (
9226 CreateExpressionTypeExpression (loc
), "[]", expr_initializers
, loc
)));
9227 return CreateExpressionFactoryCall ("ElementInit", args
);
9230 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9232 CollectionElementInitializer target
= (CollectionElementInitializer
) t
;
9233 if (arguments
!= null)
9234 target
.arguments
= arguments
.Clone (clonectx
);
9237 public override Expression
DoResolve (EmitContext ec
)
9239 if (eclass
!= ExprClass
.Invalid
)
9242 base.expr
= new AddMemberAccess (ec
.CurrentInitializerVariable
, loc
);
9244 return base.DoResolve (ec
);
9249 // A block of object or collection initializers
9251 public class CollectionOrObjectInitializers
: ExpressionStatement
9253 ArrayList initializers
;
9254 bool is_collection_initialization
;
9256 public static readonly CollectionOrObjectInitializers Empty
=
9257 new CollectionOrObjectInitializers (new ArrayList (0), Location
.Null
);
9259 public CollectionOrObjectInitializers (ArrayList initializers
, Location loc
)
9261 this.initializers
= initializers
;
9265 public bool IsEmpty
{
9267 return initializers
.Count
== 0;
9271 public bool IsCollectionInitializer
{
9273 return is_collection_initialization
;
9277 protected override void CloneTo (CloneContext clonectx
, Expression target
)
9279 CollectionOrObjectInitializers t
= (CollectionOrObjectInitializers
) target
;
9281 t
.initializers
= new ArrayList (initializers
.Count
);
9282 foreach (Expression e
in initializers
)
9283 t
.initializers
.Add (e
.Clone (clonectx
));
9286 public override Expression
CreateExpressionTree (EmitContext ec
)
9288 ArrayList expr_initializers
= new ArrayList (initializers
.Count
);
9289 foreach (Expression e
in initializers
) {
9290 Expression expr
= e
.CreateExpressionTree (ec
);
9292 expr_initializers
.Add (expr
);
9295 return new ImplicitlyTypedArrayCreation ("[]", expr_initializers
, loc
);
9298 public override Expression
DoResolve (EmitContext ec
)
9300 if (eclass
!= ExprClass
.Invalid
)
9303 ArrayList element_names
= null;
9304 for (int i
= 0; i
< initializers
.Count
; ++i
) {
9305 Expression initializer
= (Expression
) initializers
[i
];
9306 ElementInitializer element_initializer
= initializer
as ElementInitializer
;
9309 if (element_initializer
!= null) {
9310 element_names
= new ArrayList (initializers
.Count
);
9311 element_names
.Add (element_initializer
.Name
);
9312 } else if (initializer
is CompletingExpression
){
9313 initializer
.Resolve (ec
);
9314 throw new InternalErrorException ("This line should never be reached");
9316 if (!TypeManager
.ImplementsInterface (ec
.CurrentInitializerVariable
.Type
, TypeManager
.ienumerable_type
)) {
9317 Report
.Error (1922, loc
, "A field or property `{0}' cannot be initialized with a collection " +
9318 "object initializer because type `{1}' does not implement `{2}' interface",
9319 ec
.CurrentInitializerVariable
.GetSignatureForError (),
9320 TypeManager
.CSharpName (ec
.CurrentInitializerVariable
.Type
),
9321 TypeManager
.CSharpName (TypeManager
.ienumerable_type
));
9324 is_collection_initialization
= true;
9327 if (is_collection_initialization
!= (element_initializer
== null)) {
9328 Report
.Error (747, initializer
.Location
, "Inconsistent `{0}' member declaration",
9329 is_collection_initialization
? "collection initializer" : "object initializer");
9333 if (!is_collection_initialization
) {
9334 if (element_names
.Contains (element_initializer
.Name
)) {
9335 Report
.Error (1912, element_initializer
.Location
,
9336 "An object initializer includes more than one member `{0}' initialization",
9337 element_initializer
.Name
);
9339 element_names
.Add (element_initializer
.Name
);
9344 Expression e
= initializer
.Resolve (ec
);
9345 if (e
== EmptyExpressionStatement
.Instance
)
9346 initializers
.RemoveAt (i
--);
9348 initializers
[i
] = e
;
9351 type
= ec
.CurrentInitializerVariable
.Type
;
9352 if (is_collection_initialization
) {
9353 if (TypeManager
.HasElementType (type
)) {
9354 Report
.Error (1925, loc
, "Cannot initialize object of type `{0}' with a collection initializer",
9355 TypeManager
.CSharpName (type
));
9359 eclass
= ExprClass
.Variable
;
9363 public override void Emit (EmitContext ec
)
9368 public override void EmitStatement (EmitContext ec
)
9370 foreach (ExpressionStatement e
in initializers
)
9371 e
.EmitStatement (ec
);
9374 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
9376 foreach (Expression e
in initializers
)
9377 e
.MutateHoistedGenericType (storey
);
9382 // New expression with element/object initializers
9384 public class NewInitialize
: New
9387 // This class serves as a proxy for variable initializer target instances.
9388 // A real variable is assigned later when we resolve left side of an
9391 sealed class InitializerTargetExpression
: Expression
, IMemoryLocation
9393 NewInitialize new_instance
;
9395 public InitializerTargetExpression (NewInitialize newInstance
)
9397 this.type
= newInstance
.type
;
9398 this.loc
= newInstance
.loc
;
9399 this.eclass
= newInstance
.eclass
;
9400 this.new_instance
= newInstance
;
9403 public override Expression
CreateExpressionTree (EmitContext ec
)
9405 // Should not be reached
9406 throw new NotSupportedException ("ET");
9409 public override Expression
DoResolve (EmitContext ec
)
9414 public override Expression
DoResolveLValue (EmitContext ec
, Expression right_side
)
9419 public override void Emit (EmitContext ec
)
9421 Expression e
= (Expression
) new_instance
.instance
;
9425 #region IMemoryLocation Members
9427 public void AddressOf (EmitContext ec
, AddressOp mode
)
9429 new_instance
.instance
.AddressOf (ec
, mode
);
9435 CollectionOrObjectInitializers initializers
;
9436 IMemoryLocation instance
;
9438 public NewInitialize (Expression requested_type
, Arguments arguments
, CollectionOrObjectInitializers initializers
, Location l
)
9439 : base (requested_type
, arguments
, l
)
9441 this.initializers
= initializers
;
9444 protected override IMemoryLocation
EmitAddressOf (EmitContext ec
, AddressOp Mode
)
9446 instance
= base.EmitAddressOf (ec
, Mode
);
9448 if (!initializers
.IsEmpty
)
9449 initializers
.Emit (ec
);
9454 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9456 base.CloneTo (clonectx
, t
);
9458 NewInitialize target
= (NewInitialize
) t
;
9459 target
.initializers
= (CollectionOrObjectInitializers
) initializers
.Clone (clonectx
);
9462 public override Expression
CreateExpressionTree (EmitContext ec
)
9464 Arguments args
= new Arguments (2);
9465 args
.Add (new Argument (base.CreateExpressionTree (ec
)));
9466 if (!initializers
.IsEmpty
)
9467 args
.Add (new Argument (initializers
.CreateExpressionTree (ec
)));
9469 return CreateExpressionFactoryCall (
9470 initializers
.IsCollectionInitializer
? "ListInit" : "MemberInit",
9474 public override Expression
DoResolve (EmitContext ec
)
9476 if (eclass
!= ExprClass
.Invalid
)
9479 Expression e
= base.DoResolve (ec
);
9483 Expression previous
= ec
.CurrentInitializerVariable
;
9484 ec
.CurrentInitializerVariable
= new InitializerTargetExpression (this);
9485 initializers
.Resolve (ec
);
9486 ec
.CurrentInitializerVariable
= previous
;
9490 public override bool Emit (EmitContext ec
, IMemoryLocation target
)
9492 bool left_on_stack
= base.Emit (ec
, target
);
9494 if (initializers
.IsEmpty
)
9495 return left_on_stack
;
9497 LocalTemporary temp
= target
as LocalTemporary
;
9499 if (!left_on_stack
) {
9500 VariableReference vr
= target
as VariableReference
;
9502 // FIXME: This still does not work correctly for pre-set variables
9503 if (vr
!= null && vr
.IsRef
)
9504 target
.AddressOf (ec
, AddressOp
.Load
);
9506 ((Expression
) target
).Emit (ec
);
9507 left_on_stack
= true;
9510 temp
= new LocalTemporary (type
);
9517 initializers
.Emit (ec
);
9519 if (left_on_stack
) {
9524 return left_on_stack
;
9527 public override bool HasInitializer
{
9529 return !initializers
.IsEmpty
;
9533 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
9535 base.MutateHoistedGenericType (storey
);
9536 initializers
.MutateHoistedGenericType (storey
);
9540 public class AnonymousTypeDeclaration
: Expression
9542 ArrayList parameters
;
9543 readonly TypeContainer parent
;
9544 static readonly ArrayList EmptyParameters
= new ArrayList (0);
9546 public AnonymousTypeDeclaration (ArrayList parameters
, TypeContainer parent
, Location loc
)
9548 this.parameters
= parameters
;
9549 this.parent
= parent
;
9553 protected override void CloneTo (CloneContext clonectx
, Expression target
)
9555 if (parameters
== null)
9558 AnonymousTypeDeclaration t
= (AnonymousTypeDeclaration
) target
;
9559 t
.parameters
= new ArrayList (parameters
.Count
);
9560 foreach (AnonymousTypeParameter atp
in parameters
)
9561 t
.parameters
.Add (atp
.Clone (clonectx
));
9564 AnonymousTypeClass
CreateAnonymousType (ArrayList parameters
)
9566 AnonymousTypeClass type
= parent
.Module
.GetAnonymousType (parameters
);
9570 type
= AnonymousTypeClass
.Create (parent
, parameters
, loc
);
9577 if (Report
.Errors
== 0)
9580 parent
.Module
.AddAnonymousType (type
);
9584 public override Expression
CreateExpressionTree (EmitContext ec
)
9586 throw new NotSupportedException ("ET");
9589 public override Expression
DoResolve (EmitContext ec
)
9591 AnonymousTypeClass anonymous_type
;
9593 if (!ec
.IsAnonymousMethodAllowed
) {
9594 Report
.Error (836, loc
, "Anonymous types cannot be used in this expression");
9598 if (parameters
== null) {
9599 anonymous_type
= CreateAnonymousType (EmptyParameters
);
9600 return new New (new TypeExpression (anonymous_type
.TypeBuilder
, loc
),
9601 null, loc
).Resolve (ec
);
9605 Arguments arguments
= new Arguments (parameters
.Count
);
9606 TypeExpression
[] t_args
= new TypeExpression
[parameters
.Count
];
9607 for (int i
= 0; i
< parameters
.Count
; ++i
) {
9608 Expression e
= ((AnonymousTypeParameter
) parameters
[i
]).Resolve (ec
);
9614 arguments
.Add (new Argument (e
));
9615 t_args
[i
] = new TypeExpression (e
.Type
, e
.Location
);
9621 anonymous_type
= CreateAnonymousType (parameters
);
9622 if (anonymous_type
== null)
9625 GenericTypeExpr te
= new GenericTypeExpr (anonymous_type
.TypeBuilder
,
9626 new TypeArguments (t_args
), loc
);
9628 return new New (te
, arguments
, loc
).Resolve (ec
);
9631 public override void Emit (EmitContext ec
)
9633 throw new InternalErrorException ("Should not be reached");
9637 public class AnonymousTypeParameter
: Expression
9639 public readonly string Name
;
9640 Expression initializer
;
9642 public AnonymousTypeParameter (Expression initializer
, string name
, Location loc
)
9646 this.initializer
= initializer
;
9649 public AnonymousTypeParameter (Parameter parameter
)
9651 this.Name
= parameter
.Name
;
9652 this.loc
= parameter
.Location
;
9653 this.initializer
= new SimpleName (Name
, loc
);
9656 protected override void CloneTo (CloneContext clonectx
, Expression target
)
9658 AnonymousTypeParameter t
= (AnonymousTypeParameter
) target
;
9659 t
.initializer
= initializer
.Clone (clonectx
);
9662 public override Expression
CreateExpressionTree (EmitContext ec
)
9664 throw new NotSupportedException ("ET");
9667 public override bool Equals (object o
)
9669 AnonymousTypeParameter other
= o
as AnonymousTypeParameter
;
9670 return other
!= null && Name
== other
.Name
;
9673 public override int GetHashCode ()
9675 return Name
.GetHashCode ();
9678 public override Expression
DoResolve (EmitContext ec
)
9680 Expression e
= initializer
.Resolve (ec
);
9684 if (e
.eclass
== ExprClass
.MethodGroup
) {
9685 Error_InvalidInitializer (e
.ExprClassName
);
9690 if (type
== TypeManager
.void_type
|| type
== TypeManager
.null_type
||
9691 type
== InternalType
.AnonymousMethod
|| type
.IsPointer
) {
9692 Error_InvalidInitializer (e
.GetSignatureForError ());
9699 protected virtual void Error_InvalidInitializer (string initializer
)
9701 Report
.Error (828, loc
, "An anonymous type property `{0}' cannot be initialized with `{1}'",
9705 public override void Emit (EmitContext ec
)
9707 throw new InternalErrorException ("Should not be reached");