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
.Generic
;
16 using System
.Reflection
;
17 using System
.Reflection
.Emit
;
20 using SLE
= System
.Linq
.Expressions
;
23 // This is an user operator expression, automatically created during
26 public class UserOperatorCall
: Expression
{
27 public delegate Expression
ExpressionTreeExpression (ResolveContext ec
, MethodGroupExpr mg
);
29 protected readonly Arguments arguments
;
30 protected readonly MethodGroupExpr mg
;
31 readonly ExpressionTreeExpression expr_tree
;
33 public UserOperatorCall (MethodGroupExpr mg
, Arguments args
, ExpressionTreeExpression expr_tree
, Location loc
)
36 this.arguments
= args
;
37 this.expr_tree
= expr_tree
;
39 type
= TypeManager
.TypeToCoreType (((MethodInfo
) mg
).ReturnType
);
40 eclass
= ExprClass
.Value
;
44 public override Expression
CreateExpressionTree (ResolveContext ec
)
46 if (expr_tree
!= null)
47 return expr_tree (ec
, mg
);
49 Arguments args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
50 new NullLiteral (loc
),
51 mg
.CreateExpressionTree (ec
));
53 return CreateExpressionFactoryCall (ec
, "Call", args
);
56 protected override void CloneTo (CloneContext context
, Expression target
)
61 protected override Expression
DoResolve (ResolveContext ec
)
64 // We are born fully resolved
69 public override void Emit (EmitContext ec
)
71 mg
.EmitCall (ec
, arguments
);
74 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
76 return SLE
.Expression
.Call ((MethodInfo
) mg
, Arguments
.MakeExpression (arguments
, ctx
));
79 public MethodGroupExpr Method
{
83 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
85 arguments
.MutateHoistedGenericType (storey
);
86 mg
.MutateHoistedGenericType (storey
);
90 public class ParenthesizedExpression
: ShimExpression
92 public ParenthesizedExpression (Expression expr
)
98 protected override Expression
DoResolve (ResolveContext ec
)
100 return expr
.Resolve (ec
);
103 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
105 return expr
.DoResolveLValue (ec
, right_side
);
110 // Unary implements unary expressions.
112 public class Unary
: Expression
114 public enum Operator
: byte {
115 UnaryPlus
, UnaryNegation
, LogicalNot
, OnesComplement
,
119 static Type
[] [] predefined_operators
;
121 public readonly Operator Oper
;
122 public Expression Expr
;
123 Expression enum_conversion
;
125 public Unary (Operator op
, Expression expr
)
133 // This routine will attempt to simplify the unary expression when the
134 // argument is a constant.
136 Constant
TryReduceConstant (ResolveContext ec
, Constant e
)
138 if (e
is EmptyConstantCast
)
139 return TryReduceConstant (ec
, ((EmptyConstantCast
) e
).child
);
141 if (e
is SideEffectConstant
) {
142 Constant r
= TryReduceConstant (ec
, ((SideEffectConstant
) e
).value);
143 return r
== null ? null : new SideEffectConstant (r
, e
, r
.Location
);
146 Type expr_type
= e
.Type
;
149 case Operator
.UnaryPlus
:
150 // Unary numeric promotions
151 if (expr_type
== TypeManager
.byte_type
)
152 return new IntConstant (((ByteConstant
)e
).Value
, e
.Location
);
153 if (expr_type
== TypeManager
.sbyte_type
)
154 return new IntConstant (((SByteConstant
)e
).Value
, e
.Location
);
155 if (expr_type
== TypeManager
.short_type
)
156 return new IntConstant (((ShortConstant
)e
).Value
, e
.Location
);
157 if (expr_type
== TypeManager
.ushort_type
)
158 return new IntConstant (((UShortConstant
)e
).Value
, e
.Location
);
159 if (expr_type
== TypeManager
.char_type
)
160 return new IntConstant (((CharConstant
)e
).Value
, e
.Location
);
162 // Predefined operators
163 if (expr_type
== TypeManager
.int32_type
|| expr_type
== TypeManager
.uint32_type
||
164 expr_type
== TypeManager
.int64_type
|| expr_type
== TypeManager
.uint64_type
||
165 expr_type
== TypeManager
.float_type
|| expr_type
== TypeManager
.double_type
||
166 expr_type
== TypeManager
.decimal_type
) {
172 case Operator
.UnaryNegation
:
173 // Unary numeric promotions
174 if (expr_type
== TypeManager
.byte_type
)
175 return new IntConstant (-((ByteConstant
)e
).Value
, e
.Location
);
176 if (expr_type
== TypeManager
.sbyte_type
)
177 return new IntConstant (-((SByteConstant
)e
).Value
, e
.Location
);
178 if (expr_type
== TypeManager
.short_type
)
179 return new IntConstant (-((ShortConstant
)e
).Value
, e
.Location
);
180 if (expr_type
== TypeManager
.ushort_type
)
181 return new IntConstant (-((UShortConstant
)e
).Value
, e
.Location
);
182 if (expr_type
== TypeManager
.char_type
)
183 return new IntConstant (-((CharConstant
)e
).Value
, e
.Location
);
185 // Predefined operators
186 if (expr_type
== TypeManager
.int32_type
) {
187 int value = ((IntConstant
)e
).Value
;
188 if (value == int.MinValue
) {
189 if (ec
.ConstantCheckState
) {
190 ConstantFold
.Error_CompileTimeOverflow (ec
, loc
);
195 return new IntConstant (-value, e
.Location
);
197 if (expr_type
== TypeManager
.int64_type
) {
198 long value = ((LongConstant
)e
).Value
;
199 if (value == long.MinValue
) {
200 if (ec
.ConstantCheckState
) {
201 ConstantFold
.Error_CompileTimeOverflow (ec
, loc
);
206 return new LongConstant (-value, e
.Location
);
209 if (expr_type
== TypeManager
.uint32_type
) {
210 UIntLiteral uil
= e
as UIntLiteral
;
212 if (uil
.Value
== 2147483648)
213 return new IntLiteral (int.MinValue
, e
.Location
);
214 return new LongLiteral (-uil
.Value
, e
.Location
);
216 return new LongConstant (-((UIntConstant
)e
).Value
, e
.Location
);
219 if (expr_type
== TypeManager
.uint64_type
) {
220 ULongLiteral ull
= e
as ULongLiteral
;
221 if (ull
!= null && ull
.Value
== 9223372036854775808)
222 return new LongLiteral (long.MinValue
, e
.Location
);
226 if (expr_type
== TypeManager
.float_type
) {
227 FloatLiteral fl
= e
as FloatLiteral
;
228 // For better error reporting
230 return new FloatLiteral (-fl
.Value
, e
.Location
);
232 return new FloatConstant (-((FloatConstant
)e
).Value
, e
.Location
);
234 if (expr_type
== TypeManager
.double_type
) {
235 DoubleLiteral dl
= e
as DoubleLiteral
;
236 // For better error reporting
238 return new DoubleLiteral (-dl
.Value
, e
.Location
);
240 return new DoubleConstant (-((DoubleConstant
)e
).Value
, e
.Location
);
242 if (expr_type
== TypeManager
.decimal_type
)
243 return new DecimalConstant (-((DecimalConstant
)e
).Value
, e
.Location
);
247 case Operator
.LogicalNot
:
248 if (expr_type
!= TypeManager
.bool_type
)
251 bool b
= (bool)e
.GetValue ();
252 return new BoolConstant (!b
, e
.Location
);
254 case Operator
.OnesComplement
:
255 // Unary numeric promotions
256 if (expr_type
== TypeManager
.byte_type
)
257 return new IntConstant (~
((ByteConstant
)e
).Value
, e
.Location
);
258 if (expr_type
== TypeManager
.sbyte_type
)
259 return new IntConstant (~
((SByteConstant
)e
).Value
, e
.Location
);
260 if (expr_type
== TypeManager
.short_type
)
261 return new IntConstant (~
((ShortConstant
)e
).Value
, e
.Location
);
262 if (expr_type
== TypeManager
.ushort_type
)
263 return new IntConstant (~
((UShortConstant
)e
).Value
, e
.Location
);
264 if (expr_type
== TypeManager
.char_type
)
265 return new IntConstant (~
((CharConstant
)e
).Value
, e
.Location
);
267 // Predefined operators
268 if (expr_type
== TypeManager
.int32_type
)
269 return new IntConstant (~
((IntConstant
)e
).Value
, e
.Location
);
270 if (expr_type
== TypeManager
.uint32_type
)
271 return new UIntConstant (~
((UIntConstant
)e
).Value
, e
.Location
);
272 if (expr_type
== TypeManager
.int64_type
)
273 return new LongConstant (~
((LongConstant
)e
).Value
, e
.Location
);
274 if (expr_type
== TypeManager
.uint64_type
){
275 return new ULongConstant (~
((ULongConstant
)e
).Value
, e
.Location
);
277 if (e
is EnumConstant
) {
278 e
= TryReduceConstant (ec
, ((EnumConstant
)e
).Child
);
280 e
= new EnumConstant (e
, expr_type
);
285 throw new Exception ("Can not constant fold: " + Oper
.ToString());
288 protected Expression
ResolveOperator (ResolveContext ec
, Expression expr
)
290 eclass
= ExprClass
.Value
;
292 if (predefined_operators
== null)
293 CreatePredefinedOperatorsTable ();
295 Type expr_type
= expr
.Type
;
296 Expression best_expr
;
299 // Primitive types first
301 if (TypeManager
.IsPrimitiveType (expr_type
)) {
302 best_expr
= ResolvePrimitivePredefinedType (expr
);
303 if (best_expr
== null)
306 type
= best_expr
.Type
;
312 // E operator ~(E x);
314 if (Oper
== Operator
.OnesComplement
&& TypeManager
.IsEnumType (expr_type
))
315 return ResolveEnumOperator (ec
, expr
);
317 return ResolveUserType (ec
, expr
);
320 protected virtual Expression
ResolveEnumOperator (ResolveContext ec
, Expression expr
)
322 Type underlying_type
= TypeManager
.GetEnumUnderlyingType (expr
.Type
);
323 Expression best_expr
= ResolvePrimitivePredefinedType (EmptyCast
.Create (expr
, underlying_type
));
324 if (best_expr
== null)
328 enum_conversion
= Convert
.ExplicitNumericConversion (new EmptyExpression (best_expr
.Type
), underlying_type
);
330 return EmptyCast
.Create (this, type
);
333 public override Expression
CreateExpressionTree (ResolveContext ec
)
335 return CreateExpressionTree (ec
, null);
338 Expression
CreateExpressionTree (ResolveContext ec
, MethodGroupExpr user_op
)
342 case Operator
.AddressOf
:
343 Error_PointerInsideExpressionTree (ec
);
345 case Operator
.UnaryNegation
:
346 if (ec
.HasSet (ResolveContext
.Options
.CheckedScope
) && user_op
== null && !IsFloat (type
))
347 method_name
= "NegateChecked";
349 method_name
= "Negate";
351 case Operator
.OnesComplement
:
352 case Operator
.LogicalNot
:
355 case Operator
.UnaryPlus
:
356 method_name
= "UnaryPlus";
359 throw new InternalErrorException ("Unknown unary operator " + Oper
.ToString ());
362 Arguments args
= new Arguments (2);
363 args
.Add (new Argument (Expr
.CreateExpressionTree (ec
)));
365 args
.Add (new Argument (user_op
.CreateExpressionTree (ec
)));
366 return CreateExpressionFactoryCall (ec
, method_name
, args
);
369 static void CreatePredefinedOperatorsTable ()
371 predefined_operators
= new Type
[(int) Operator
.TOP
] [];
374 // 7.6.1 Unary plus operator
376 predefined_operators
[(int) Operator
.UnaryPlus
] = new Type
[] {
377 TypeManager
.int32_type
, TypeManager
.uint32_type
,
378 TypeManager
.int64_type
, TypeManager
.uint64_type
,
379 TypeManager
.float_type
, TypeManager
.double_type
,
380 TypeManager
.decimal_type
384 // 7.6.2 Unary minus operator
386 predefined_operators
[(int) Operator
.UnaryNegation
] = new Type
[] {
387 TypeManager
.int32_type
,
388 TypeManager
.int64_type
,
389 TypeManager
.float_type
, TypeManager
.double_type
,
390 TypeManager
.decimal_type
394 // 7.6.3 Logical negation operator
396 predefined_operators
[(int) Operator
.LogicalNot
] = new Type
[] {
397 TypeManager
.bool_type
401 // 7.6.4 Bitwise complement operator
403 predefined_operators
[(int) Operator
.OnesComplement
] = new Type
[] {
404 TypeManager
.int32_type
, TypeManager
.uint32_type
,
405 TypeManager
.int64_type
, TypeManager
.uint64_type
410 // Unary numeric promotions
412 static Expression
DoNumericPromotion (Operator op
, Expression expr
)
414 Type expr_type
= expr
.Type
;
415 if ((op
== Operator
.UnaryPlus
|| op
== Operator
.UnaryNegation
|| op
== Operator
.OnesComplement
) &&
416 expr_type
== TypeManager
.byte_type
|| expr_type
== TypeManager
.sbyte_type
||
417 expr_type
== TypeManager
.short_type
|| expr_type
== TypeManager
.ushort_type
||
418 expr_type
== TypeManager
.char_type
)
419 return Convert
.ImplicitNumericConversion (expr
, TypeManager
.int32_type
);
421 if (op
== Operator
.UnaryNegation
&& expr_type
== TypeManager
.uint32_type
)
422 return Convert
.ImplicitNumericConversion (expr
, TypeManager
.int64_type
);
427 protected override Expression
DoResolve (ResolveContext ec
)
429 if (Oper
== Operator
.AddressOf
) {
430 return ResolveAddressOf (ec
);
433 Expr
= Expr
.Resolve (ec
);
437 if (TypeManager
.IsDynamicType (Expr
.Type
)) {
438 Arguments args
= new Arguments (1);
439 args
.Add (new Argument (Expr
));
440 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args
, loc
).Resolve (ec
);
443 if (TypeManager
.IsNullableType (Expr
.Type
))
444 return new Nullable
.LiftedUnaryOperator (Oper
, Expr
).Resolve (ec
);
447 // Attempt to use a constant folding operation.
449 Constant cexpr
= Expr
as Constant
;
451 cexpr
= TryReduceConstant (ec
, cexpr
);
453 return cexpr
.Resolve (ec
);
456 Expression expr
= ResolveOperator (ec
, Expr
);
458 Error_OperatorCannotBeApplied (ec
, loc
, OperName (Oper
), Expr
.Type
);
461 // Reduce unary operator on predefined types
463 if (expr
== this && Oper
== Operator
.UnaryPlus
)
469 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right
)
474 public override void Emit (EmitContext ec
)
476 EmitOperator (ec
, type
);
479 protected void EmitOperator (EmitContext ec
, Type type
)
481 ILGenerator ig
= ec
.ig
;
484 case Operator
.UnaryPlus
:
488 case Operator
.UnaryNegation
:
489 if (ec
.HasSet (EmitContext
.Options
.CheckedScope
) && !IsFloat (type
)) {
490 ig
.Emit (OpCodes
.Ldc_I4_0
);
491 if (type
== TypeManager
.int64_type
)
492 ig
.Emit (OpCodes
.Conv_U8
);
494 ig
.Emit (OpCodes
.Sub_Ovf
);
497 ig
.Emit (OpCodes
.Neg
);
502 case Operator
.LogicalNot
:
504 ig
.Emit (OpCodes
.Ldc_I4_0
);
505 ig
.Emit (OpCodes
.Ceq
);
508 case Operator
.OnesComplement
:
510 ig
.Emit (OpCodes
.Not
);
513 case Operator
.AddressOf
:
514 ((IMemoryLocation
)Expr
).AddressOf (ec
, AddressOp
.LoadStore
);
518 throw new Exception ("This should not happen: Operator = "
523 // Same trick as in Binary expression
525 if (enum_conversion
!= null)
526 enum_conversion
.Emit (ec
);
529 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
531 if (Oper
== Operator
.LogicalNot
)
532 Expr
.EmitBranchable (ec
, target
, !on_true
);
534 base.EmitBranchable (ec
, target
, on_true
);
537 public override void EmitSideEffect (EmitContext ec
)
539 Expr
.EmitSideEffect (ec
);
542 public static void Error_OperatorCannotBeApplied (ResolveContext ec
, Location loc
, string oper
, Type t
)
544 ec
.Report
.Error (23, loc
, "The `{0}' operator cannot be applied to operand of type `{1}'",
545 oper
, TypeManager
.CSharpName (t
));
549 // Converts operator to System.Linq.Expressions.ExpressionType enum name
551 string GetOperatorExpressionTypeName ()
554 case Operator
.OnesComplement
:
555 return "OnesComplement";
556 case Operator
.LogicalNot
:
558 case Operator
.UnaryNegation
:
560 case Operator
.UnaryPlus
:
563 throw new NotImplementedException ("Unknown express type operator " + Oper
.ToString ());
567 static bool IsFloat (Type t
)
569 return t
== TypeManager
.float_type
|| t
== TypeManager
.double_type
;
573 // Returns a stringified representation of the Operator
575 public static string OperName (Operator oper
)
578 case Operator
.UnaryPlus
:
580 case Operator
.UnaryNegation
:
582 case Operator
.LogicalNot
:
584 case Operator
.OnesComplement
:
586 case Operator
.AddressOf
:
590 throw new NotImplementedException (oper
.ToString ());
593 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
595 var expr
= Expr
.MakeExpression (ctx
);
596 bool is_checked
= ctx
.HasSet (BuilderContext
.Options
.CheckedScope
);
599 case Operator
.UnaryNegation
:
600 return is_checked
? SLE
.Expression
.NegateChecked (expr
) : SLE
.Expression
.Negate (expr
);
601 case Operator
.LogicalNot
:
602 return SLE
.Expression
.Not (expr
);
604 case Operator
.OnesComplement
:
605 return SLE
.Expression
.OnesComplement (expr
);
608 throw new NotImplementedException (Oper
.ToString ());
612 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
614 type
= storey
.MutateType (type
);
615 Expr
.MutateHoistedGenericType (storey
);
618 Expression
ResolveAddressOf (ResolveContext ec
)
621 UnsafeError (ec
, loc
);
623 Expr
= Expr
.DoResolveLValue (ec
, EmptyExpression
.UnaryAddress
);
624 if (Expr
== null || Expr
.eclass
!= ExprClass
.Variable
) {
625 ec
.Report
.Error (211, loc
, "Cannot take the address of the given expression");
629 if (!TypeManager
.VerifyUnmanaged (ec
.Compiler
, Expr
.Type
, loc
)) {
633 IVariableReference vr
= Expr
as IVariableReference
;
636 VariableInfo vi
= vr
.VariableInfo
;
638 if (vi
.LocalInfo
!= null)
639 vi
.LocalInfo
.Used
= true;
642 // A variable is considered definitely assigned if you take its address.
647 is_fixed
= vr
.IsFixed
;
648 vr
.SetHasAddressTaken ();
651 AnonymousMethodExpression
.Error_AddressOfCapturedVar (ec
, vr
, loc
);
654 IFixedExpression fe
= Expr
as IFixedExpression
;
655 is_fixed
= fe
!= null && fe
.IsFixed
;
658 if (!is_fixed
&& !ec
.HasSet (ResolveContext
.Options
.FixedInitializerScope
)) {
659 ec
.Report
.Error (212, loc
, "You can only take the address of unfixed expression inside of a fixed statement initializer");
662 type
= TypeManager
.GetPointerType (Expr
.Type
);
663 eclass
= ExprClass
.Value
;
667 Expression
ResolvePrimitivePredefinedType (Expression expr
)
669 expr
= DoNumericPromotion (Oper
, expr
);
670 Type expr_type
= expr
.Type
;
671 Type
[] predefined
= predefined_operators
[(int) Oper
];
672 foreach (Type t
in predefined
) {
680 // Perform user-operator overload resolution
682 protected virtual Expression
ResolveUserOperator (ResolveContext ec
, Expression expr
)
684 CSharp
.Operator
.OpType op_type
;
686 case Operator
.LogicalNot
:
687 op_type
= CSharp
.Operator
.OpType
.LogicalNot
; break;
688 case Operator
.OnesComplement
:
689 op_type
= CSharp
.Operator
.OpType
.OnesComplement
; break;
690 case Operator
.UnaryNegation
:
691 op_type
= CSharp
.Operator
.OpType
.UnaryNegation
; break;
692 case Operator
.UnaryPlus
:
693 op_type
= CSharp
.Operator
.OpType
.UnaryPlus
; break;
695 throw new InternalErrorException (Oper
.ToString ());
698 string op_name
= CSharp
.Operator
.GetMetadataName (op_type
);
699 MethodGroupExpr user_op
= MemberLookup (ec
.Compiler
, ec
.CurrentType
, expr
.Type
, op_name
, MemberTypes
.Method
, AllBindingFlags
, expr
.Location
) as MethodGroupExpr
;
703 Arguments args
= new Arguments (1);
704 args
.Add (new Argument (expr
));
705 user_op
= user_op
.OverloadResolve (ec
, ref args
, false, expr
.Location
);
710 Expr
= args
[0].Expr
;
711 return new UserOperatorCall (user_op
, args
, CreateExpressionTree
, expr
.Location
);
715 // Unary user type overload resolution
717 Expression
ResolveUserType (ResolveContext ec
, Expression expr
)
719 Expression best_expr
= ResolveUserOperator (ec
, expr
);
720 if (best_expr
!= null)
723 Type
[] predefined
= predefined_operators
[(int) Oper
];
724 foreach (Type t
in predefined
) {
725 Expression oper_expr
= Convert
.UserDefinedConversion (ec
, expr
, t
, expr
.Location
, false, false);
726 if (oper_expr
== null)
730 // decimal type is predefined but has user-operators
732 if (oper_expr
.Type
== TypeManager
.decimal_type
)
733 oper_expr
= ResolveUserType (ec
, oper_expr
);
735 oper_expr
= ResolvePrimitivePredefinedType (oper_expr
);
737 if (oper_expr
== null)
740 if (best_expr
== null) {
741 best_expr
= oper_expr
;
745 int result
= MethodGroupExpr
.BetterTypeConversion (ec
, best_expr
.Type
, t
);
747 ec
.Report
.Error (35, loc
, "Operator `{0}' is ambiguous on an operand of type `{1}'",
748 OperName (Oper
), TypeManager
.CSharpName (expr
.Type
));
753 best_expr
= oper_expr
;
756 if (best_expr
== null)
760 // HACK: Decimal user-operator is included in standard operators
762 if (best_expr
.Type
== TypeManager
.decimal_type
)
766 type
= best_expr
.Type
;
770 protected override void CloneTo (CloneContext clonectx
, Expression t
)
772 Unary target
= (Unary
) t
;
774 target
.Expr
= Expr
.Clone (clonectx
);
779 // Unary operators are turned into Indirection expressions
780 // after semantic analysis (this is so we can take the address
781 // of an indirection).
783 public class Indirection
: Expression
, IMemoryLocation
, IAssignMethod
, IFixedExpression
{
785 LocalTemporary temporary
;
788 public Indirection (Expression expr
, Location l
)
794 public override Expression
CreateExpressionTree (ResolveContext ec
)
796 Error_PointerInsideExpressionTree (ec
);
800 protected override void CloneTo (CloneContext clonectx
, Expression t
)
802 Indirection target
= (Indirection
) t
;
803 target
.expr
= expr
.Clone (clonectx
);
806 public override void Emit (EmitContext ec
)
811 LoadFromPtr (ec
.ig
, Type
);
814 public void Emit (EmitContext ec
, bool leave_copy
)
818 ec
.ig
.Emit (OpCodes
.Dup
);
819 temporary
= new LocalTemporary (expr
.Type
);
820 temporary
.Store (ec
);
824 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
826 prepared
= prepare_for_load
;
830 if (prepare_for_load
)
831 ec
.ig
.Emit (OpCodes
.Dup
);
835 ec
.ig
.Emit (OpCodes
.Dup
);
836 temporary
= new LocalTemporary (expr
.Type
);
837 temporary
.Store (ec
);
840 StoreFromPtr (ec
.ig
, type
);
842 if (temporary
!= null) {
844 temporary
.Release (ec
);
848 public void AddressOf (EmitContext ec
, AddressOp Mode
)
853 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
855 return DoResolve (ec
);
858 protected override Expression
DoResolve (ResolveContext ec
)
860 expr
= expr
.Resolve (ec
);
865 UnsafeError (ec
, loc
);
867 if (!expr
.Type
.IsPointer
) {
868 ec
.Report
.Error (193, loc
, "The * or -> operator must be applied to a pointer");
872 if (expr
.Type
== TypeManager
.void_ptr_type
) {
873 ec
.Report
.Error (242, loc
, "The operation in question is undefined on void pointers");
877 type
= TypeManager
.GetElementType (expr
.Type
);
878 eclass
= ExprClass
.Variable
;
882 public bool IsFixed
{
886 public override string ToString ()
888 return "*(" + expr
+ ")";
893 /// Unary Mutator expressions (pre and post ++ and --)
897 /// UnaryMutator implements ++ and -- expressions. It derives from
898 /// ExpressionStatement becuase the pre/post increment/decrement
899 /// operators can be used in a statement context.
901 /// FIXME: Idea, we could split this up in two classes, one simpler
902 /// for the common case, and one with the extra fields for more complex
903 /// classes (indexers require temporary access; overloaded require method)
906 public class UnaryMutator
: ExpressionStatement
908 class DynamicPostMutator
: Expression
, IAssignMethod
913 public DynamicPostMutator (Expression expr
)
916 this.type
= expr
.Type
;
917 this.loc
= expr
.Location
;
920 public override Expression
CreateExpressionTree (ResolveContext ec
)
922 throw new NotImplementedException ("ET");
925 protected override Expression
DoResolve (ResolveContext rc
)
927 eclass
= expr
.eclass
;
931 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
933 expr
.DoResolveLValue (ec
, right_side
);
934 return DoResolve (ec
);
937 public override void Emit (EmitContext ec
)
942 public void Emit (EmitContext ec
, bool leave_copy
)
944 throw new NotImplementedException ();
948 // Emits target assignment using unmodified source value
950 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
953 // Allocate temporary variable to keep original value before it's modified
955 temp
= new LocalTemporary (type
);
959 ((IAssignMethod
) expr
).EmitAssign (ec
, source
, false, prepare_for_load
);
970 public enum Mode
: byte {
977 PreDecrement
= IsDecrement
,
978 PostIncrement
= IsPost
,
979 PostDecrement
= IsPost
| IsDecrement
983 bool is_expr
, recurse
;
987 // Holds the real operation
988 Expression operation
;
990 public UnaryMutator (Mode m
, Expression e
)
997 public override Expression
CreateExpressionTree (ResolveContext ec
)
999 return new SimpleAssign (this, this).CreateExpressionTree (ec
);
1002 protected override Expression
DoResolve (ResolveContext ec
)
1004 expr
= expr
.Resolve (ec
);
1009 if (TypeManager
.IsDynamicType (expr
.Type
)) {
1011 // Handle postfix unary operators using local
1012 // temporary variable
1014 if ((mode
& Mode
.IsPost
) != 0)
1015 expr
= new DynamicPostMutator (expr
);
1017 Arguments args
= new Arguments (1);
1018 args
.Add (new Argument (expr
));
1019 return new SimpleAssign (expr
, new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args
, loc
)).Resolve (ec
);
1022 if (TypeManager
.IsNullableType (expr
.Type
))
1023 return new Nullable
.LiftedUnaryMutator (mode
, expr
, loc
).Resolve (ec
);
1025 eclass
= ExprClass
.Value
;
1027 return ResolveOperator (ec
);
1030 void EmitCode (EmitContext ec
, bool is_expr
)
1033 this.is_expr
= is_expr
;
1034 ((IAssignMethod
) expr
).EmitAssign (ec
, this, is_expr
&& (mode
== Mode
.PreIncrement
|| mode
== Mode
.PreDecrement
), true);
1037 public override void Emit (EmitContext ec
)
1040 // We use recurse to allow ourselfs to be the source
1041 // of an assignment. This little hack prevents us from
1042 // having to allocate another expression
1045 ((IAssignMethod
) expr
).Emit (ec
, is_expr
&& (mode
== Mode
.PostIncrement
|| mode
== Mode
.PostDecrement
));
1047 operation
.Emit (ec
);
1053 EmitCode (ec
, true);
1056 public override void EmitStatement (EmitContext ec
)
1058 EmitCode (ec
, false);
1062 // Converts operator to System.Linq.Expressions.ExpressionType enum name
1064 string GetOperatorExpressionTypeName ()
1066 return IsDecrement
? "Decrement" : "Increment";
1070 get { return (mode & Mode.IsDecrement) != 0; }
1074 // Returns whether an object of type `t' can be incremented
1075 // or decremented with add/sub (ie, basically whether we can
1076 // use pre-post incr-decr operations on it, but it is not a
1077 // System.Decimal, which we require operator overloading to catch)
1079 static bool IsPredefinedOperator (Type t
)
1081 return (TypeManager
.IsPrimitiveType (t
) && t
!= TypeManager
.bool_type
) ||
1082 TypeManager
.IsEnumType (t
) ||
1083 t
.IsPointer
&& t
!= TypeManager
.void_ptr_type
;
1087 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
1089 var target
= ((RuntimeValueExpression
) expr
).MetaObject
.Expression
;
1090 var source
= SLE
.Expression
.Convert (operation
.MakeExpression (ctx
), target
.Type
);
1091 return SLE
.Expression
.Assign (target
, source
);
1095 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1097 UnaryMutator target
= (UnaryMutator
) t
;
1099 target
.expr
= expr
.Clone (clonectx
);
1102 Expression
ResolveOperator (ResolveContext ec
)
1104 if (expr
is RuntimeValueExpression
) {
1107 // Use itself at the top of the stack
1108 operation
= new EmptyExpression (type
);
1112 // The operand of the prefix/postfix increment decrement operators
1113 // should be an expression that is classified as a variable,
1114 // a property access or an indexer access
1116 if (expr
.eclass
== ExprClass
.Variable
|| expr
.eclass
== ExprClass
.IndexerAccess
|| expr
.eclass
== ExprClass
.PropertyAccess
) {
1117 expr
= expr
.ResolveLValue (ec
, expr
);
1119 ec
.Report
.Error (1059, loc
, "The operand of an increment or decrement operator must be a variable, property or indexer");
1123 // 1. Check predefined types
1125 if (IsPredefinedOperator (type
)) {
1126 // TODO: Move to IntConstant once I get rid of int32_type
1127 var one
= new IntConstant (1, loc
);
1129 // TODO: Cache this based on type when using EmptyExpression in
1131 Binary
.Operator op
= IsDecrement
? Binary
.Operator
.Subtraction
: Binary
.Operator
.Addition
;
1132 operation
= new Binary (op
, operation
, one
);
1133 operation
= operation
.Resolve (ec
);
1134 if (operation
!= null && operation
.Type
!= type
)
1135 operation
= Convert
.ExplicitNumericConversion (operation
, type
);
1141 // Step 2: Perform Operator Overload location
1147 op_name
= Operator
.GetMetadataName (Operator
.OpType
.Decrement
);
1149 op_name
= Operator
.GetMetadataName (Operator
.OpType
.Increment
);
1151 mg
= MemberLookup (ec
.Compiler
, ec
.CurrentType
, type
, op_name
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
1154 Arguments args
= new Arguments (1);
1155 args
.Add (new Argument (expr
));
1156 mg
= mg
.OverloadResolve (ec
, ref args
, false, loc
);
1160 args
[0].Expr
= operation
;
1161 operation
= new UserOperatorCall (mg
, args
, null, loc
);
1162 operation
= Convert
.ImplicitConversionRequired (ec
, operation
, type
, loc
);
1166 string name
= IsDecrement
?
1167 Operator
.GetName (Operator
.OpType
.Decrement
) :
1168 Operator
.GetName (Operator
.OpType
.Increment
);
1170 Unary
.Error_OperatorCannotBeApplied (ec
, loc
, name
, type
);
1176 /// Base class for the `Is' and `As' classes.
1180 /// FIXME: Split this in two, and we get to save the `Operator' Oper
1183 public abstract class Probe
: Expression
{
1184 public Expression ProbeType
;
1185 protected Expression expr
;
1186 protected TypeExpr probe_type_expr
;
1188 public Probe (Expression expr
, Expression probe_type
, Location l
)
1190 ProbeType
= probe_type
;
1195 public Expression Expr
{
1201 protected override Expression
DoResolve (ResolveContext ec
)
1203 probe_type_expr
= ProbeType
.ResolveAsTypeTerminal (ec
, false);
1204 if (probe_type_expr
== null)
1207 expr
= expr
.Resolve (ec
);
1211 if ((probe_type_expr
.Type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
1212 ec
.Report
.Error (-244, loc
, "The `{0}' operator cannot be applied to an operand of a static type",
1216 if (expr
.Type
.IsPointer
|| probe_type_expr
.Type
.IsPointer
) {
1217 ec
.Report
.Error (244, loc
, "The `{0}' operator cannot be applied to an operand of pointer type",
1222 if (expr
.Type
== InternalType
.AnonymousMethod
) {
1223 ec
.Report
.Error (837, loc
, "The `{0}' operator cannot be applied to a lambda expression or anonymous method",
1231 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1233 expr
.MutateHoistedGenericType (storey
);
1234 probe_type_expr
.MutateHoistedGenericType (storey
);
1237 protected abstract string OperatorName { get; }
1239 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1241 Probe target
= (Probe
) t
;
1243 target
.expr
= expr
.Clone (clonectx
);
1244 target
.ProbeType
= ProbeType
.Clone (clonectx
);
1250 /// Implementation of the `is' operator.
1252 public class Is
: Probe
{
1253 Nullable
.Unwrap expr_unwrap
;
1255 public Is (Expression expr
, Expression probe_type
, Location l
)
1256 : base (expr
, probe_type
, l
)
1260 public override Expression
CreateExpressionTree (ResolveContext ec
)
1262 Arguments args
= Arguments
.CreateForExpressionTree (ec
, null,
1263 expr
.CreateExpressionTree (ec
),
1264 new TypeOf (probe_type_expr
, loc
));
1266 return CreateExpressionFactoryCall (ec
, "TypeIs", args
);
1269 public override void Emit (EmitContext ec
)
1271 ILGenerator ig
= ec
.ig
;
1272 if (expr_unwrap
!= null) {
1273 expr_unwrap
.EmitCheck (ec
);
1278 ig
.Emit (OpCodes
.Isinst
, probe_type_expr
.Type
);
1279 ig
.Emit (OpCodes
.Ldnull
);
1280 ig
.Emit (OpCodes
.Cgt_Un
);
1283 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
1285 ILGenerator ig
= ec
.ig
;
1286 if (expr_unwrap
!= null) {
1287 expr_unwrap
.EmitCheck (ec
);
1290 ig
.Emit (OpCodes
.Isinst
, probe_type_expr
.Type
);
1292 ig
.Emit (on_true
? OpCodes
.Brtrue
: OpCodes
.Brfalse
, target
);
1295 Expression
CreateConstantResult (ResolveContext ec
, bool result
)
1298 ec
.Report
.Warning (183, 1, loc
, "The given expression is always of the provided (`{0}') type",
1299 TypeManager
.CSharpName (probe_type_expr
.Type
));
1301 ec
.Report
.Warning (184, 1, loc
, "The given expression is never of the provided (`{0}') type",
1302 TypeManager
.CSharpName (probe_type_expr
.Type
));
1304 return ReducedExpression
.Create (new BoolConstant (result
, loc
).Resolve (ec
), this);
1307 protected override Expression
DoResolve (ResolveContext ec
)
1309 if (base.DoResolve (ec
) == null)
1313 bool d_is_nullable
= false;
1316 // If E is a method group or the null literal, or if the type of E is a reference
1317 // type or a nullable type and the value of E is null, the result is false
1319 if (expr
.IsNull
|| expr
.eclass
== ExprClass
.MethodGroup
)
1320 return CreateConstantResult (ec
, false);
1322 if (TypeManager
.IsNullableType (d
) && !TypeManager
.ContainsGenericParameters (d
)) {
1323 d
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (d
) [0]);
1324 d_is_nullable
= true;
1327 type
= TypeManager
.bool_type
;
1328 eclass
= ExprClass
.Value
;
1329 Type t
= probe_type_expr
.Type
;
1330 bool t_is_nullable
= false;
1331 if (TypeManager
.IsNullableType (t
) && !TypeManager
.ContainsGenericParameters (t
)) {
1332 t
= TypeManager
.TypeToCoreType (TypeManager
.GetTypeArguments (t
) [0]);
1333 t_is_nullable
= true;
1336 if (TypeManager
.IsStruct (t
)) {
1339 // D and T are the same value types but D can be null
1341 if (d_is_nullable
&& !t_is_nullable
) {
1342 expr_unwrap
= Nullable
.Unwrap
.Create (expr
, false);
1347 // The result is true if D and T are the same value types
1349 return CreateConstantResult (ec
, true);
1352 if (TypeManager
.IsGenericParameter (d
))
1353 return ResolveGenericParameter (ec
, t
, d
);
1356 // An unboxing conversion exists
1358 if (Convert
.ExplicitReferenceConversionExists (d
, t
))
1361 if (TypeManager
.IsGenericParameter (t
))
1362 return ResolveGenericParameter (ec
, d
, t
);
1364 if (TypeManager
.IsStruct (d
)) {
1366 if (Convert
.ImplicitBoxingConversionExists (expr
, t
, out temp
))
1367 return CreateConstantResult (ec
, true);
1369 if (TypeManager
.IsGenericParameter (d
))
1370 return ResolveGenericParameter (ec
, t
, d
);
1372 if (TypeManager
.ContainsGenericParameters (d
))
1375 if (Convert
.ImplicitReferenceConversionExists (expr
, t
) ||
1376 Convert
.ExplicitReferenceConversionExists (d
, t
)) {
1382 return CreateConstantResult (ec
, false);
1385 Expression
ResolveGenericParameter (ResolveContext ec
, Type d
, Type t
)
1387 GenericConstraints constraints
= TypeManager
.GetTypeParameterConstraints (t
);
1388 if (constraints
!= null) {
1389 if (constraints
.IsReferenceType
&& TypeManager
.IsStruct (d
))
1390 return CreateConstantResult (ec
, false);
1393 if (TypeManager
.IsGenericParameter (expr
.Type
)) {
1394 if (constraints
!= null && constraints
.IsValueType
&& expr
.Type
== t
)
1395 return CreateConstantResult (ec
, true);
1397 expr
= new BoxedCast (expr
, d
);
1403 protected override string OperatorName
{
1404 get { return "is"; }
1409 /// Implementation of the `as' operator.
1411 public class As
: Probe
{
1413 Expression resolved_type
;
1415 public As (Expression expr
, Expression probe_type
, Location l
)
1416 : base (expr
, probe_type
, l
)
1420 public override Expression
CreateExpressionTree (ResolveContext ec
)
1422 Arguments args
= Arguments
.CreateForExpressionTree (ec
, null,
1423 expr
.CreateExpressionTree (ec
),
1424 new TypeOf (probe_type_expr
, loc
));
1426 return CreateExpressionFactoryCall (ec
, "TypeAs", args
);
1429 public override void Emit (EmitContext ec
)
1431 ILGenerator ig
= ec
.ig
;
1436 ig
.Emit (OpCodes
.Isinst
, type
);
1438 if (TypeManager
.IsGenericParameter (type
) || TypeManager
.IsNullableType (type
))
1439 ig
.Emit (OpCodes
.Unbox_Any
, type
);
1442 protected override Expression
DoResolve (ResolveContext ec
)
1444 if (resolved_type
== null) {
1445 resolved_type
= base.DoResolve (ec
);
1447 if (resolved_type
== null)
1451 type
= probe_type_expr
.Type
;
1452 eclass
= ExprClass
.Value
;
1453 Type etype
= expr
.Type
;
1455 if (!TypeManager
.IsReferenceType (type
) && !TypeManager
.IsNullableType (type
)) {
1456 if (TypeManager
.IsGenericParameter (type
)) {
1457 ec
.Report
.Error (413, loc
,
1458 "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
1459 probe_type_expr
.GetSignatureForError ());
1461 ec
.Report
.Error (77, loc
,
1462 "The `as' operator cannot be used with a non-nullable value type `{0}'",
1463 TypeManager
.CSharpName (type
));
1468 if (expr
.IsNull
&& TypeManager
.IsNullableType (type
)) {
1469 return Nullable
.LiftedNull
.CreateFromExpression (ec
, this);
1472 Expression e
= Convert
.ImplicitConversion (ec
, expr
, type
, loc
);
1479 if (Convert
.ExplicitReferenceConversionExists (etype
, type
)){
1480 if (TypeManager
.IsGenericParameter (etype
))
1481 expr
= new BoxedCast (expr
, etype
);
1487 if (TypeManager
.ContainsGenericParameters (etype
) ||
1488 TypeManager
.ContainsGenericParameters (type
)) {
1489 expr
= new BoxedCast (expr
, etype
);
1494 ec
.Report
.Error (39, loc
, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
1495 TypeManager
.CSharpName (etype
), TypeManager
.CSharpName (type
));
1500 protected override string OperatorName
{
1501 get { return "as"; }
1504 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1506 type
= storey
.MutateType (type
);
1507 base.MutateHoistedGenericType (storey
);
1510 public override bool GetAttributableValue (ResolveContext ec
, Type value_type
, out object value)
1512 return expr
.GetAttributableValue (ec
, value_type
, out value);
1517 /// This represents a typecast in the source language.
1519 /// FIXME: Cast expressions have an unusual set of parsing
1520 /// rules, we need to figure those out.
1522 public class Cast
: ShimExpression
{
1523 Expression target_type
;
1525 public Cast (Expression cast_type
, Expression expr
)
1526 : this (cast_type
, expr
, cast_type
.Location
)
1530 public Cast (Expression cast_type
, Expression expr
, Location loc
)
1533 this.target_type
= cast_type
;
1537 public Expression TargetType
{
1538 get { return target_type; }
1541 protected override Expression
DoResolve (ResolveContext ec
)
1543 expr
= expr
.Resolve (ec
);
1547 TypeExpr target
= target_type
.ResolveAsTypeTerminal (ec
, false);
1553 if (type
.IsAbstract
&& type
.IsSealed
) {
1554 ec
.Report
.Error (716, loc
, "Cannot convert to static type `{0}'", TypeManager
.CSharpName (type
));
1558 eclass
= ExprClass
.Value
;
1560 Constant c
= expr
as Constant
;
1562 c
= c
.TryReduce (ec
, type
, loc
);
1567 if (type
.IsPointer
&& !ec
.IsUnsafe
) {
1568 UnsafeError (ec
, loc
);
1569 } else if (TypeManager
.IsDynamicType (expr
.Type
)) {
1570 Arguments arg
= new Arguments (1);
1571 arg
.Add (new Argument (expr
));
1572 return new DynamicConversion (type
, CSharpBinderFlags
.ConvertExplicit
, arg
, loc
).Resolve (ec
);
1575 expr
= Convert
.ExplicitConversion (ec
, expr
, type
, loc
);
1579 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1581 Cast target
= (Cast
) t
;
1583 target
.target_type
= target_type
.Clone (clonectx
);
1584 target
.expr
= expr
.Clone (clonectx
);
1588 public class ImplicitCast
: ShimExpression
1592 public ImplicitCast (Expression expr
, Type target
, bool arrayAccess
)
1595 this.loc
= expr
.Location
;
1597 this.arrayAccess
= arrayAccess
;
1600 protected override Expression
DoResolve (ResolveContext ec
)
1602 expr
= expr
.Resolve (ec
);
1607 expr
= ConvertExpressionToArrayIndex (ec
, expr
);
1609 expr
= Convert
.ImplicitConversionRequired (ec
, expr
, type
, loc
);
1616 // C# 2.0 Default value expression
1618 public class DefaultValueExpression
: Expression
1622 public DefaultValueExpression (Expression expr
, Location loc
)
1628 public override Expression
CreateExpressionTree (ResolveContext ec
)
1630 Arguments args
= new Arguments (2);
1631 args
.Add (new Argument (this));
1632 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
1633 return CreateExpressionFactoryCall (ec
, "Constant", args
);
1636 protected override Expression
DoResolve (ResolveContext ec
)
1638 TypeExpr texpr
= expr
.ResolveAsTypeTerminal (ec
, false);
1644 if ((type
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
1645 ec
.Report
.Error (-244, loc
, "The `default value' operator cannot be applied to an operand of a static type");
1649 return new NullLiteral (Location
).ConvertImplicitly (ec
, type
);
1651 if (TypeManager
.IsReferenceType (type
))
1652 return new NullConstant (type
, loc
);
1654 Constant c
= New
.Constantify (type
);
1656 return c
.Resolve (ec
);
1658 eclass
= ExprClass
.Variable
;
1662 public override void Emit (EmitContext ec
)
1664 LocalTemporary temp_storage
= new LocalTemporary(type
);
1666 temp_storage
.AddressOf(ec
, AddressOp
.LoadStore
);
1667 ec
.ig
.Emit(OpCodes
.Initobj
, type
);
1668 temp_storage
.Emit(ec
);
1671 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
1673 type
= storey
.MutateType (type
);
1676 protected override void CloneTo (CloneContext clonectx
, Expression t
)
1678 DefaultValueExpression target
= (DefaultValueExpression
) t
;
1680 target
.expr
= expr
.Clone (clonectx
);
1685 /// Binary operators
1687 public class Binary
: Expression
, IDynamicBinder
1690 protected class PredefinedOperator
{
1691 protected readonly Type left
;
1692 protected readonly Type right
;
1693 public readonly Operator OperatorsMask
;
1694 public Type ReturnType
;
1696 public PredefinedOperator (Type ltype
, Type rtype
, Operator op_mask
)
1697 : this (ltype
, rtype
, op_mask
, ltype
)
1701 public PredefinedOperator (Type type
, Operator op_mask
, Type return_type
)
1702 : this (type
, type
, op_mask
, return_type
)
1706 public PredefinedOperator (Type type
, Operator op_mask
)
1707 : this (type
, type
, op_mask
, type
)
1711 public PredefinedOperator (Type ltype
, Type rtype
, Operator op_mask
, Type return_type
)
1713 if ((op_mask
& Operator
.ValuesOnlyMask
) != 0)
1714 throw new InternalErrorException ("Only masked values can be used");
1718 this.OperatorsMask
= op_mask
;
1719 this.ReturnType
= return_type
;
1722 public virtual Expression
ConvertResult (ResolveContext ec
, Binary b
)
1724 b
.type
= ReturnType
;
1726 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1727 b
.right
= Convert
.ImplicitConversion (ec
, b
.right
, right
, b
.right
.Location
);
1730 // A user operators does not support multiple user conversions, but decimal type
1731 // is considered to be predefined type therefore we apply predefined operators rules
1732 // and then look for decimal user-operator implementation
1734 if (left
== TypeManager
.decimal_type
)
1735 return b
.ResolveUserOperator (ec
, b
.left
.Type
, b
.right
.Type
);
1737 var c
= b
.right
as IntegralConstant
;
1739 if (c
.IsDefaultValue
&& (b
.oper
== Operator
.Addition
|| b
.oper
== Operator
.BitwiseOr
|| b
.oper
== Operator
.Subtraction
))
1740 return ReducedExpression
.Create (b
.left
, b
);
1744 c
= b
.left
as IntegralConstant
;
1746 if (c
.IsDefaultValue
&& (b
.oper
== Operator
.Addition
|| b
.oper
== Operator
.BitwiseOr
))
1747 return ReducedExpression
.Create (b
.right
, b
);
1754 public bool IsPrimitiveApplicable (Type ltype
, Type rtype
)
1757 // We are dealing with primitive types only
1759 return left
== ltype
&& ltype
== rtype
;
1762 public virtual bool IsApplicable (ResolveContext ec
, Expression lexpr
, Expression rexpr
)
1764 if (TypeManager
.IsEqual (left
, lexpr
.Type
) &&
1765 TypeManager
.IsEqual (right
, rexpr
.Type
))
1768 return Convert
.ImplicitConversionExists (ec
, lexpr
, left
) &&
1769 Convert
.ImplicitConversionExists (ec
, rexpr
, right
);
1772 public PredefinedOperator
ResolveBetterOperator (ResolveContext ec
, PredefinedOperator best_operator
)
1775 if (left
!= null && best_operator
.left
!= null) {
1776 result
= MethodGroupExpr
.BetterTypeConversion (ec
, best_operator
.left
, left
);
1780 // When second arguments are same as the first one, the result is same
1782 if (right
!= null && (left
!= right
|| best_operator
.left
!= best_operator
.right
)) {
1783 result
|= MethodGroupExpr
.BetterTypeConversion (ec
, best_operator
.right
, right
);
1786 if (result
== 0 || result
> 2)
1789 return result
== 1 ? best_operator
: this;
1793 class PredefinedStringOperator
: PredefinedOperator
{
1794 public PredefinedStringOperator (Type type
, Operator op_mask
)
1795 : base (type
, op_mask
, type
)
1797 ReturnType
= TypeManager
.string_type
;
1800 public PredefinedStringOperator (Type ltype
, Type rtype
, Operator op_mask
)
1801 : base (ltype
, rtype
, op_mask
)
1803 ReturnType
= TypeManager
.string_type
;
1806 public override Expression
ConvertResult (ResolveContext ec
, Binary b
)
1809 // Use original expression for nullable arguments
1811 Nullable
.Unwrap unwrap
= b
.left
as Nullable
.Unwrap
;
1813 b
.left
= unwrap
.Original
;
1815 unwrap
= b
.right
as Nullable
.Unwrap
;
1817 b
.right
= unwrap
.Original
;
1819 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1820 b
.right
= Convert
.ImplicitConversion (ec
, b
.right
, right
, b
.right
.Location
);
1823 // Start a new concat expression using converted expression
1825 return StringConcat
.Create (ec
, b
.left
, b
.right
, b
.loc
);
1829 class PredefinedShiftOperator
: PredefinedOperator
{
1830 public PredefinedShiftOperator (Type ltype
, Operator op_mask
) :
1831 base (ltype
, TypeManager
.int32_type
, op_mask
)
1835 public override Expression
ConvertResult (ResolveContext ec
, Binary b
)
1837 b
.left
= Convert
.ImplicitConversion (ec
, b
.left
, left
, b
.left
.Location
);
1839 Expression expr_tree_expr
= Convert
.ImplicitConversion (ec
, b
.right
, TypeManager
.int32_type
, b
.right
.Location
);
1841 int right_mask
= left
== TypeManager
.int32_type
|| left
== TypeManager
.uint32_type
? 0x1f : 0x3f;
1844 // b = b.left >> b.right & (0x1f|0x3f)
1846 b
.right
= new Binary (Operator
.BitwiseAnd
,
1847 b
.right
, new IntConstant (right_mask
, b
.right
.Location
)).Resolve (ec
);
1850 // Expression tree representation does not use & mask
1852 b
.right
= ReducedExpression
.Create (b
.right
, expr_tree_expr
).Resolve (ec
);
1853 b
.type
= ReturnType
;
1856 // Optimize shift by 0
1858 var c
= b
.right
as Constant
;
1859 if (c
!= null && c
.IsDefaultValue
)
1860 return ReducedExpression
.Create (b
.left
, b
).Resolve (ec
);
1866 class PredefinedPointerOperator
: PredefinedOperator
{
1867 public PredefinedPointerOperator (Type ltype
, Type rtype
, Operator op_mask
)
1868 : base (ltype
, rtype
, op_mask
)
1872 public PredefinedPointerOperator (Type ltype
, Type rtype
, Operator op_mask
, Type retType
)
1873 : base (ltype
, rtype
, op_mask
, retType
)
1877 public PredefinedPointerOperator (Type type
, Operator op_mask
, Type return_type
)
1878 : base (type
, op_mask
, return_type
)
1882 public override bool IsApplicable (ResolveContext ec
, Expression lexpr
, Expression rexpr
)
1885 if (!lexpr
.Type
.IsPointer
)
1888 if (!Convert
.ImplicitConversionExists (ec
, lexpr
, left
))
1892 if (right
== null) {
1893 if (!rexpr
.Type
.IsPointer
)
1896 if (!Convert
.ImplicitConversionExists (ec
, rexpr
, right
))
1903 public override Expression
ConvertResult (ResolveContext ec
, Binary b
)
1906 b
.left
= EmptyCast
.Create (b
.left
, left
);
1907 } else if (right
!= null) {
1908 b
.right
= EmptyCast
.Create (b
.right
, right
);
1911 Type r_type
= ReturnType
;
1912 Expression left_arg
, right_arg
;
1913 if (r_type
== null) {
1916 right_arg
= b
.right
;
1917 r_type
= b
.left
.Type
;
1921 r_type
= b
.right
.Type
;
1925 right_arg
= b
.right
;
1928 return new PointerArithmetic (b
.oper
, left_arg
, right_arg
, r_type
, b
.loc
).Resolve (ec
);
1933 public enum Operator
{
1934 Multiply
= 0 | ArithmeticMask
,
1935 Division
= 1 | ArithmeticMask
,
1936 Modulus
= 2 | ArithmeticMask
,
1937 Addition
= 3 | ArithmeticMask
| AdditionMask
,
1938 Subtraction
= 4 | ArithmeticMask
| SubtractionMask
,
1940 LeftShift
= 5 | ShiftMask
,
1941 RightShift
= 6 | ShiftMask
,
1943 LessThan
= 7 | ComparisonMask
| RelationalMask
,
1944 GreaterThan
= 8 | ComparisonMask
| RelationalMask
,
1945 LessThanOrEqual
= 9 | ComparisonMask
| RelationalMask
,
1946 GreaterThanOrEqual
= 10 | ComparisonMask
| RelationalMask
,
1947 Equality
= 11 | ComparisonMask
| EqualityMask
,
1948 Inequality
= 12 | ComparisonMask
| EqualityMask
,
1950 BitwiseAnd
= 13 | BitwiseMask
,
1951 ExclusiveOr
= 14 | BitwiseMask
,
1952 BitwiseOr
= 15 | BitwiseMask
,
1954 LogicalAnd
= 16 | LogicalMask
,
1955 LogicalOr
= 17 | LogicalMask
,
1960 ValuesOnlyMask
= ArithmeticMask
- 1,
1961 ArithmeticMask
= 1 << 5,
1963 ComparisonMask
= 1 << 7,
1964 EqualityMask
= 1 << 8,
1965 BitwiseMask
= 1 << 9,
1966 LogicalMask
= 1 << 10,
1967 AdditionMask
= 1 << 11,
1968 SubtractionMask
= 1 << 12,
1969 RelationalMask
= 1 << 13
1972 readonly Operator oper
;
1973 protected Expression left
, right
;
1974 readonly bool is_compound
;
1975 Expression enum_conversion
;
1977 static PredefinedOperator
[] standard_operators
;
1978 static PredefinedOperator
[] pointer_operators
;
1980 public Binary (Operator oper
, Expression left
, Expression right
, bool isCompound
)
1981 : this (oper
, left
, right
)
1983 this.is_compound
= isCompound
;
1986 public Binary (Operator oper
, Expression left
, Expression right
)
1991 this.loc
= left
.Location
;
1994 public Operator Oper
{
2001 /// Returns a stringified representation of the Operator
2003 string OperName (Operator oper
)
2007 case Operator
.Multiply
:
2010 case Operator
.Division
:
2013 case Operator
.Modulus
:
2016 case Operator
.Addition
:
2019 case Operator
.Subtraction
:
2022 case Operator
.LeftShift
:
2025 case Operator
.RightShift
:
2028 case Operator
.LessThan
:
2031 case Operator
.GreaterThan
:
2034 case Operator
.LessThanOrEqual
:
2037 case Operator
.GreaterThanOrEqual
:
2040 case Operator
.Equality
:
2043 case Operator
.Inequality
:
2046 case Operator
.BitwiseAnd
:
2049 case Operator
.BitwiseOr
:
2052 case Operator
.ExclusiveOr
:
2055 case Operator
.LogicalOr
:
2058 case Operator
.LogicalAnd
:
2062 s
= oper
.ToString ();
2072 public static void Error_OperatorCannotBeApplied (ResolveContext ec
, Expression left
, Expression right
, Operator oper
, Location loc
)
2074 new Binary (oper
, left
, right
).Error_OperatorCannotBeApplied (ec
, left
, right
);
2077 public static void Error_OperatorCannotBeApplied (ResolveContext ec
, Expression left
, Expression right
, string oper
, Location loc
)
2080 l
= TypeManager
.CSharpName (left
.Type
);
2081 r
= TypeManager
.CSharpName (right
.Type
);
2083 ec
.Report
.Error (19, loc
, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
2087 protected void Error_OperatorCannotBeApplied (ResolveContext ec
, Expression left
, Expression right
)
2089 Error_OperatorCannotBeApplied (ec
, left
, right
, OperName (oper
), loc
);
2093 // Converts operator to System.Linq.Expressions.ExpressionType enum name
2095 string GetOperatorExpressionTypeName ()
2098 case Operator
.Addition
:
2099 return is_compound
? "AddAssign" : "Add";
2100 case Operator
.BitwiseAnd
:
2101 return is_compound
? "AndAssign" : "And";
2102 case Operator
.BitwiseOr
:
2103 return is_compound
? "OrAssign" : "Or";
2104 case Operator
.Division
:
2105 return is_compound
? "DivideAssign" : "Divide";
2106 case Operator
.ExclusiveOr
:
2107 return is_compound
? "ExclusiveOrAssign" : "ExclusiveOr";
2108 case Operator
.Equality
:
2110 case Operator
.GreaterThan
:
2111 return "GreaterThan";
2112 case Operator
.GreaterThanOrEqual
:
2113 return "GreaterThanOrEqual";
2114 case Operator
.Inequality
:
2116 case Operator
.LeftShift
:
2117 return is_compound
? "LeftShiftAssign" : "LeftShift";
2118 case Operator
.LessThan
:
2120 case Operator
.LessThanOrEqual
:
2121 return "LessThanOrEqual";
2122 case Operator
.LogicalAnd
:
2124 case Operator
.LogicalOr
:
2126 case Operator
.Modulus
:
2127 return is_compound
? "ModuloAssign" : "Modulo";
2128 case Operator
.Multiply
:
2129 return is_compound
? "MultiplyAssign" : "Multiply";
2130 case Operator
.RightShift
:
2131 return is_compound
? "RightShiftAssign" : "RightShift";
2132 case Operator
.Subtraction
:
2133 return is_compound
? "SubtractAssign" : "Subtract";
2135 throw new NotImplementedException ("Unknown expression type operator " + oper
.ToString ());
2139 static string GetOperatorMetadataName (Operator op
)
2141 CSharp
.Operator
.OpType op_type
;
2143 case Operator
.Addition
:
2144 op_type
= CSharp
.Operator
.OpType
.Addition
; break;
2145 case Operator
.BitwiseAnd
:
2146 op_type
= CSharp
.Operator
.OpType
.BitwiseAnd
; break;
2147 case Operator
.BitwiseOr
:
2148 op_type
= CSharp
.Operator
.OpType
.BitwiseOr
; break;
2149 case Operator
.Division
:
2150 op_type
= CSharp
.Operator
.OpType
.Division
; break;
2151 case Operator
.Equality
:
2152 op_type
= CSharp
.Operator
.OpType
.Equality
; break;
2153 case Operator
.ExclusiveOr
:
2154 op_type
= CSharp
.Operator
.OpType
.ExclusiveOr
; break;
2155 case Operator
.GreaterThan
:
2156 op_type
= CSharp
.Operator
.OpType
.GreaterThan
; break;
2157 case Operator
.GreaterThanOrEqual
:
2158 op_type
= CSharp
.Operator
.OpType
.GreaterThanOrEqual
; break;
2159 case Operator
.Inequality
:
2160 op_type
= CSharp
.Operator
.OpType
.Inequality
; break;
2161 case Operator
.LeftShift
:
2162 op_type
= CSharp
.Operator
.OpType
.LeftShift
; break;
2163 case Operator
.LessThan
:
2164 op_type
= CSharp
.Operator
.OpType
.LessThan
; break;
2165 case Operator
.LessThanOrEqual
:
2166 op_type
= CSharp
.Operator
.OpType
.LessThanOrEqual
; break;
2167 case Operator
.Modulus
:
2168 op_type
= CSharp
.Operator
.OpType
.Modulus
; break;
2169 case Operator
.Multiply
:
2170 op_type
= CSharp
.Operator
.OpType
.Multiply
; break;
2171 case Operator
.RightShift
:
2172 op_type
= CSharp
.Operator
.OpType
.RightShift
; break;
2173 case Operator
.Subtraction
:
2174 op_type
= CSharp
.Operator
.OpType
.Subtraction
; break;
2176 throw new InternalErrorException (op
.ToString ());
2179 return CSharp
.Operator
.GetMetadataName (op_type
);
2182 public static void EmitOperatorOpcode (EmitContext ec
, Operator oper
, Type l
)
2185 ILGenerator ig
= ec
.ig
;
2188 case Operator
.Multiply
:
2189 if (ec
.HasSet (EmitContext
.Options
.CheckedScope
)) {
2190 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2191 opcode
= OpCodes
.Mul_Ovf
;
2192 else if (!IsFloat (l
))
2193 opcode
= OpCodes
.Mul_Ovf_Un
;
2195 opcode
= OpCodes
.Mul
;
2197 opcode
= OpCodes
.Mul
;
2201 case Operator
.Division
:
2203 opcode
= OpCodes
.Div_Un
;
2205 opcode
= OpCodes
.Div
;
2208 case Operator
.Modulus
:
2210 opcode
= OpCodes
.Rem_Un
;
2212 opcode
= OpCodes
.Rem
;
2215 case Operator
.Addition
:
2216 if (ec
.HasSet (EmitContext
.Options
.CheckedScope
)) {
2217 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2218 opcode
= OpCodes
.Add_Ovf
;
2219 else if (!IsFloat (l
))
2220 opcode
= OpCodes
.Add_Ovf_Un
;
2222 opcode
= OpCodes
.Add
;
2224 opcode
= OpCodes
.Add
;
2227 case Operator
.Subtraction
:
2228 if (ec
.HasSet (EmitContext
.Options
.CheckedScope
)) {
2229 if (l
== TypeManager
.int32_type
|| l
== TypeManager
.int64_type
)
2230 opcode
= OpCodes
.Sub_Ovf
;
2231 else if (!IsFloat (l
))
2232 opcode
= OpCodes
.Sub_Ovf_Un
;
2234 opcode
= OpCodes
.Sub
;
2236 opcode
= OpCodes
.Sub
;
2239 case Operator
.RightShift
:
2241 opcode
= OpCodes
.Shr_Un
;
2243 opcode
= OpCodes
.Shr
;
2246 case Operator
.LeftShift
:
2247 opcode
= OpCodes
.Shl
;
2250 case Operator
.Equality
:
2251 opcode
= OpCodes
.Ceq
;
2254 case Operator
.Inequality
:
2255 ig
.Emit (OpCodes
.Ceq
);
2256 ig
.Emit (OpCodes
.Ldc_I4_0
);
2258 opcode
= OpCodes
.Ceq
;
2261 case Operator
.LessThan
:
2263 opcode
= OpCodes
.Clt_Un
;
2265 opcode
= OpCodes
.Clt
;
2268 case Operator
.GreaterThan
:
2270 opcode
= OpCodes
.Cgt_Un
;
2272 opcode
= OpCodes
.Cgt
;
2275 case Operator
.LessThanOrEqual
:
2276 if (IsUnsigned (l
) || IsFloat (l
))
2277 ig
.Emit (OpCodes
.Cgt_Un
);
2279 ig
.Emit (OpCodes
.Cgt
);
2280 ig
.Emit (OpCodes
.Ldc_I4_0
);
2282 opcode
= OpCodes
.Ceq
;
2285 case Operator
.GreaterThanOrEqual
:
2286 if (IsUnsigned (l
) || IsFloat (l
))
2287 ig
.Emit (OpCodes
.Clt_Un
);
2289 ig
.Emit (OpCodes
.Clt
);
2291 ig
.Emit (OpCodes
.Ldc_I4_0
);
2293 opcode
= OpCodes
.Ceq
;
2296 case Operator
.BitwiseOr
:
2297 opcode
= OpCodes
.Or
;
2300 case Operator
.BitwiseAnd
:
2301 opcode
= OpCodes
.And
;
2304 case Operator
.ExclusiveOr
:
2305 opcode
= OpCodes
.Xor
;
2309 throw new InternalErrorException (oper
.ToString ());
2315 static bool IsUnsigned (Type t
)
2320 return (t
== TypeManager
.uint32_type
|| t
== TypeManager
.uint64_type
||
2321 t
== TypeManager
.ushort_type
|| t
== TypeManager
.byte_type
);
2324 static bool IsFloat (Type t
)
2326 return t
== TypeManager
.float_type
|| t
== TypeManager
.double_type
;
2329 Expression
ResolveOperator (ResolveContext ec
)
2332 Type r
= right
.Type
;
2334 bool primitives_only
= false;
2336 if (standard_operators
== null)
2337 CreateStandardOperatorsTable ();
2340 // Handles predefined primitive types
2342 if (TypeManager
.IsPrimitiveType (l
) && TypeManager
.IsPrimitiveType (r
)) {
2343 if ((oper
& Operator
.ShiftMask
) == 0) {
2344 if (l
!= TypeManager
.bool_type
&& !DoBinaryOperatorPromotion (ec
))
2347 primitives_only
= true;
2351 if (l
.IsPointer
|| r
.IsPointer
)
2352 return ResolveOperatorPointer (ec
, l
, r
);
2355 bool lenum
= TypeManager
.IsEnumType (l
);
2356 bool renum
= TypeManager
.IsEnumType (r
);
2357 if (lenum
|| renum
) {
2358 expr
= ResolveOperatorEnum (ec
, lenum
, renum
, l
, r
);
2360 // TODO: Can this be ambiguous
2366 if ((oper
== Operator
.Addition
|| oper
== Operator
.Subtraction
|| (oper
& Operator
.EqualityMask
) != 0) &&
2367 (TypeManager
.IsDelegateType (l
) || TypeManager
.IsDelegateType (r
))) {
2369 expr
= ResolveOperatorDelegate (ec
, l
, r
);
2371 // TODO: Can this be ambiguous
2377 expr
= ResolveUserOperator (ec
, l
, r
);
2381 // Predefined reference types equality
2382 if ((oper
& Operator
.EqualityMask
) != 0) {
2383 expr
= ResolveOperatorEqualityRerefence (ec
, l
, r
);
2389 return ResolveOperatorPredefined (ec
, standard_operators
, primitives_only
, null);
2392 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
2393 // if 'left' is not an enumeration constant, create one from the type of 'right'
2394 Constant
EnumLiftUp (ResolveContext ec
, Constant left
, Constant right
, Location loc
)
2397 case Operator
.BitwiseOr
:
2398 case Operator
.BitwiseAnd
:
2399 case Operator
.ExclusiveOr
:
2400 case Operator
.Equality
:
2401 case Operator
.Inequality
:
2402 case Operator
.LessThan
:
2403 case Operator
.LessThanOrEqual
:
2404 case Operator
.GreaterThan
:
2405 case Operator
.GreaterThanOrEqual
:
2406 if (TypeManager
.IsEnumType (left
.Type
))
2409 if (left
.IsZeroInteger
)
2410 return left
.TryReduce (ec
, right
.Type
, loc
);
2414 case Operator
.Addition
:
2415 case Operator
.Subtraction
:
2418 case Operator
.Multiply
:
2419 case Operator
.Division
:
2420 case Operator
.Modulus
:
2421 case Operator
.LeftShift
:
2422 case Operator
.RightShift
:
2423 if (TypeManager
.IsEnumType (right
.Type
) || TypeManager
.IsEnumType (left
.Type
))
2427 Error_OperatorCannotBeApplied (ec
, this.left
, this.right
);
2432 // The `|' operator used on types which were extended is dangerous
2434 void CheckBitwiseOrOnSignExtended (ResolveContext ec
)
2436 OpcodeCast lcast
= left
as OpcodeCast
;
2437 if (lcast
!= null) {
2438 if (IsUnsigned (lcast
.UnderlyingType
))
2442 OpcodeCast rcast
= right
as OpcodeCast
;
2443 if (rcast
!= null) {
2444 if (IsUnsigned (rcast
.UnderlyingType
))
2448 if (lcast
== null && rcast
== null)
2451 // FIXME: consider constants
2453 ec
.Report
.Warning (675, 3, loc
,
2454 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
2455 TypeManager
.CSharpName (lcast
!= null ? lcast
.UnderlyingType
: rcast
.UnderlyingType
));
2458 static void CreatePointerOperatorsTable ()
2460 var temp
= new List
<PredefinedPointerOperator
> ();
2463 // Pointer arithmetic:
2465 // T* operator + (T* x, int y); T* operator - (T* x, int y);
2466 // T* operator + (T* x, uint y); T* operator - (T* x, uint y);
2467 // T* operator + (T* x, long y); T* operator - (T* x, long y);
2468 // T* operator + (T* x, ulong y); T* operator - (T* x, ulong y);
2470 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.int32_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2471 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.uint32_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2472 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.int64_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2473 temp
.Add (new PredefinedPointerOperator (null, TypeManager
.uint64_type
, Operator
.AdditionMask
| Operator
.SubtractionMask
));
2476 // T* operator + (int y, T* x);
2477 // T* operator + (uint y, T *x);
2478 // T* operator + (long y, T *x);
2479 // T* operator + (ulong y, T *x);
2481 temp
.Add (new PredefinedPointerOperator (TypeManager
.int32_type
, null, Operator
.AdditionMask
, null));
2482 temp
.Add (new PredefinedPointerOperator (TypeManager
.uint32_type
, null, Operator
.AdditionMask
, null));
2483 temp
.Add (new PredefinedPointerOperator (TypeManager
.int64_type
, null, Operator
.AdditionMask
, null));
2484 temp
.Add (new PredefinedPointerOperator (TypeManager
.uint64_type
, null, Operator
.AdditionMask
, null));
2487 // long operator - (T* x, T *y)
2489 temp
.Add (new PredefinedPointerOperator (null, Operator
.SubtractionMask
, TypeManager
.int64_type
));
2491 pointer_operators
= temp
.ToArray ();
2494 static void CreateStandardOperatorsTable ()
2496 var temp
= new List
<PredefinedOperator
> ();
2497 Type bool_type
= TypeManager
.bool_type
;
2499 temp
.Add (new PredefinedOperator (TypeManager
.int32_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2500 temp
.Add (new PredefinedOperator (TypeManager
.uint32_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2501 temp
.Add (new PredefinedOperator (TypeManager
.int64_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2502 temp
.Add (new PredefinedOperator (TypeManager
.uint64_type
, Operator
.ArithmeticMask
| Operator
.BitwiseMask
));
2503 temp
.Add (new PredefinedOperator (TypeManager
.float_type
, Operator
.ArithmeticMask
));
2504 temp
.Add (new PredefinedOperator (TypeManager
.double_type
, Operator
.ArithmeticMask
));
2505 temp
.Add (new PredefinedOperator (TypeManager
.decimal_type
, Operator
.ArithmeticMask
));
2507 temp
.Add (new PredefinedOperator (TypeManager
.int32_type
, Operator
.ComparisonMask
, bool_type
));
2508 temp
.Add (new PredefinedOperator (TypeManager
.uint32_type
, Operator
.ComparisonMask
, bool_type
));
2509 temp
.Add (new PredefinedOperator (TypeManager
.int64_type
, Operator
.ComparisonMask
, bool_type
));
2510 temp
.Add (new PredefinedOperator (TypeManager
.uint64_type
, Operator
.ComparisonMask
, bool_type
));
2511 temp
.Add (new PredefinedOperator (TypeManager
.float_type
, Operator
.ComparisonMask
, bool_type
));
2512 temp
.Add (new PredefinedOperator (TypeManager
.double_type
, Operator
.ComparisonMask
, bool_type
));
2513 temp
.Add (new PredefinedOperator (TypeManager
.decimal_type
, Operator
.ComparisonMask
, bool_type
));
2515 temp
.Add (new PredefinedOperator (TypeManager
.string_type
, Operator
.EqualityMask
, bool_type
));
2517 temp
.Add (new PredefinedStringOperator (TypeManager
.string_type
, Operator
.AdditionMask
));
2518 temp
.Add (new PredefinedStringOperator (TypeManager
.string_type
, TypeManager
.object_type
, Operator
.AdditionMask
));
2519 temp
.Add (new PredefinedStringOperator (TypeManager
.object_type
, TypeManager
.string_type
, Operator
.AdditionMask
));
2521 temp
.Add (new PredefinedOperator (bool_type
,
2522 Operator
.BitwiseMask
| Operator
.LogicalMask
| Operator
.EqualityMask
, bool_type
));
2524 temp
.Add (new PredefinedShiftOperator (TypeManager
.int32_type
, Operator
.ShiftMask
));
2525 temp
.Add (new PredefinedShiftOperator (TypeManager
.uint32_type
, Operator
.ShiftMask
));
2526 temp
.Add (new PredefinedShiftOperator (TypeManager
.int64_type
, Operator
.ShiftMask
));
2527 temp
.Add (new PredefinedShiftOperator (TypeManager
.uint64_type
, Operator
.ShiftMask
));
2529 standard_operators
= temp
.ToArray ();
2533 // Rules used during binary numeric promotion
2535 static bool DoNumericPromotion (ResolveContext rc
, ref Expression prim_expr
, ref Expression second_expr
, Type type
)
2540 Constant c
= prim_expr
as Constant
;
2542 temp
= c
.ConvertImplicitly (rc
, type
);
2549 if (type
== TypeManager
.uint32_type
) {
2550 etype
= prim_expr
.Type
;
2551 if (etype
== TypeManager
.int32_type
|| etype
== TypeManager
.short_type
|| etype
== TypeManager
.sbyte_type
) {
2552 type
= TypeManager
.int64_type
;
2554 if (type
!= second_expr
.Type
) {
2555 c
= second_expr
as Constant
;
2557 temp
= c
.ConvertImplicitly (rc
, type
);
2559 temp
= Convert
.ImplicitNumericConversion (second_expr
, type
);
2565 } else if (type
== TypeManager
.uint64_type
) {
2567 // A compile-time error occurs if the other operand is of type sbyte, short, int, or long
2569 if (type
== TypeManager
.int32_type
|| type
== TypeManager
.int64_type
||
2570 type
== TypeManager
.short_type
|| type
== TypeManager
.sbyte_type
)
2574 temp
= Convert
.ImplicitNumericConversion (prim_expr
, type
);
2583 // 7.2.6.2 Binary numeric promotions
2585 public bool DoBinaryOperatorPromotion (ResolveContext ec
)
2587 Type ltype
= left
.Type
;
2588 Type rtype
= right
.Type
;
2591 foreach (Type t
in ConstantFold
.binary_promotions
) {
2593 return t
== rtype
|| DoNumericPromotion (ec
, ref right
, ref left
, t
);
2596 return t
== ltype
|| DoNumericPromotion (ec
, ref left
, ref right
, t
);
2599 Type int32
= TypeManager
.int32_type
;
2600 if (ltype
!= int32
) {
2601 Constant c
= left
as Constant
;
2603 temp
= c
.ConvertImplicitly (ec
, int32
);
2605 temp
= Convert
.ImplicitNumericConversion (left
, int32
);
2612 if (rtype
!= int32
) {
2613 Constant c
= right
as Constant
;
2615 temp
= c
.ConvertImplicitly (ec
, int32
);
2617 temp
= Convert
.ImplicitNumericConversion (right
, int32
);
2627 protected override Expression
DoResolve (ResolveContext ec
)
2632 if ((oper
== Operator
.Subtraction
) && (left
is ParenthesizedExpression
)) {
2633 left
= ((ParenthesizedExpression
) left
).Expr
;
2634 left
= left
.Resolve (ec
, ResolveFlags
.VariableOrValue
| ResolveFlags
.Type
);
2638 if (left
.eclass
== ExprClass
.Type
) {
2639 ec
.Report
.Error (75, loc
, "To cast a negative value, you must enclose the value in parentheses");
2643 left
= left
.Resolve (ec
);
2648 Constant lc
= left
as Constant
;
2650 if (lc
!= null && lc
.Type
== TypeManager
.bool_type
&&
2651 ((oper
== Operator
.LogicalAnd
&& lc
.IsDefaultValue
) ||
2652 (oper
== Operator
.LogicalOr
&& !lc
.IsDefaultValue
))) {
2654 // FIXME: resolve right expression as unreachable
2655 // right.Resolve (ec);
2657 ec
.Report
.Warning (429, 4, loc
, "Unreachable expression code detected");
2661 right
= right
.Resolve (ec
);
2665 eclass
= ExprClass
.Value
;
2666 Constant rc
= right
as Constant
;
2668 // The conversion rules are ignored in enum context but why
2669 if (!ec
.HasSet (ResolveContext
.Options
.EnumScope
) && lc
!= null && rc
!= null && (TypeManager
.IsEnumType (left
.Type
) || TypeManager
.IsEnumType (right
.Type
))) {
2670 lc
= EnumLiftUp (ec
, lc
, rc
, loc
);
2672 rc
= EnumLiftUp (ec
, rc
, lc
, loc
);
2675 if (rc
!= null && lc
!= null) {
2676 int prev_e
= ec
.Report
.Errors
;
2677 Expression e
= ConstantFold
.BinaryFold (ec
, oper
, lc
, rc
, loc
);
2681 if (e
!= null || ec
.Report
.Errors
!= prev_e
)
2685 // Comparison warnings
2686 if ((oper
& Operator
.ComparisonMask
) != 0) {
2687 if (left
.Equals (right
)) {
2688 ec
.Report
.Warning (1718, 3, loc
, "A comparison made to same variable. Did you mean to compare something else?");
2690 CheckUselessComparison (ec
, lc
, right
.Type
);
2691 CheckUselessComparison (ec
, rc
, left
.Type
);
2694 if (TypeManager
.IsDynamicType (left
.Type
) || TypeManager
.IsDynamicType (right
.Type
)) {
2695 Arguments args
= new Arguments (2);
2696 args
.Add (new Argument (left
));
2697 args
.Add (new Argument (right
));
2698 return new DynamicExpressionStatement (this, args
, loc
).Resolve (ec
);
2701 if (RootContext
.Version
>= LanguageVersion
.ISO_2
&&
2702 ((TypeManager
.IsNullableType (left
.Type
) && (right
is NullLiteral
|| TypeManager
.IsNullableType (right
.Type
) || TypeManager
.IsValueType (right
.Type
))) ||
2703 (TypeManager
.IsValueType (left
.Type
) && right
is NullLiteral
) ||
2704 (TypeManager
.IsNullableType (right
.Type
) && (left
is NullLiteral
|| TypeManager
.IsNullableType (left
.Type
) || TypeManager
.IsValueType (left
.Type
))) ||
2705 (TypeManager
.IsValueType (right
.Type
) && left
is NullLiteral
)))
2706 return new Nullable
.LiftedBinaryOperator (oper
, left
, right
, loc
).Resolve (ec
);
2708 return DoResolveCore (ec
, left
, right
);
2711 protected Expression
DoResolveCore (ResolveContext ec
, Expression left_orig
, Expression right_orig
)
2713 Expression expr
= ResolveOperator (ec
);
2715 Error_OperatorCannotBeApplied (ec
, left_orig
, right_orig
);
2717 if (left
== null || right
== null)
2718 throw new InternalErrorException ("Invalid conversion");
2720 if (oper
== Operator
.BitwiseOr
)
2721 CheckBitwiseOrOnSignExtended (ec
);
2726 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
2728 var le
= left
.MakeExpression (ctx
);
2729 var re
= right
.MakeExpression (ctx
);
2730 bool is_checked
= ctx
.HasSet (BuilderContext
.Options
.CheckedScope
);
2733 case Operator
.Addition
:
2734 return is_checked
? SLE
.Expression
.AddChecked (le
, re
) : SLE
.Expression
.Add (le
, re
);
2735 case Operator
.BitwiseAnd
:
2736 return SLE
.Expression
.And (le
, re
);
2737 case Operator
.BitwiseOr
:
2738 return SLE
.Expression
.Or (le
, re
);
2739 case Operator
.Division
:
2740 return SLE
.Expression
.Divide (le
, re
);
2741 case Operator
.Equality
:
2742 return SLE
.Expression
.Equal (le
, re
);
2743 case Operator
.ExclusiveOr
:
2744 return SLE
.Expression
.ExclusiveOr (le
, re
);
2745 case Operator
.GreaterThan
:
2746 return SLE
.Expression
.GreaterThan (le
, re
);
2747 case Operator
.GreaterThanOrEqual
:
2748 return SLE
.Expression
.GreaterThanOrEqual (le
, re
);
2749 case Operator
.Inequality
:
2750 return SLE
.Expression
.NotEqual (le
, re
);
2751 case Operator
.LeftShift
:
2752 return SLE
.Expression
.LeftShift (le
, re
);
2753 case Operator
.LessThan
:
2754 return SLE
.Expression
.LessThan (le
, re
);
2755 case Operator
.LessThanOrEqual
:
2756 return SLE
.Expression
.LessThanOrEqual (le
, re
);
2757 case Operator
.LogicalAnd
:
2758 return SLE
.Expression
.AndAlso (le
, re
);
2759 case Operator
.LogicalOr
:
2760 return SLE
.Expression
.OrElse (le
, re
);
2761 case Operator
.Modulus
:
2762 return SLE
.Expression
.Modulo (le
, re
);
2763 case Operator
.Multiply
:
2764 return is_checked
? SLE
.Expression
.MultiplyChecked (le
, re
) : SLE
.Expression
.Multiply (le
, re
);
2765 case Operator
.RightShift
:
2766 return SLE
.Expression
.RightShift (le
, re
);
2767 case Operator
.Subtraction
:
2768 return is_checked
? SLE
.Expression
.SubtractChecked (le
, re
) : SLE
.Expression
.Subtract (le
, re
);
2770 throw new NotImplementedException (oper
.ToString ());
2774 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
2776 left
.MutateHoistedGenericType (storey
);
2777 right
.MutateHoistedGenericType (storey
);
2781 // D operator + (D x, D y)
2782 // D operator - (D x, D y)
2783 // bool operator == (D x, D y)
2784 // bool operator != (D x, D y)
2786 Expression
ResolveOperatorDelegate (ResolveContext ec
, Type l
, Type r
)
2788 bool is_equality
= (oper
& Operator
.EqualityMask
) != 0;
2789 if (!TypeManager
.IsEqual (l
, r
) && !TypeManager
.IsVariantOf (r
, l
)) {
2791 if (right
.eclass
== ExprClass
.MethodGroup
|| (r
== InternalType
.AnonymousMethod
&& !is_equality
)) {
2792 tmp
= Convert
.ImplicitConversionRequired (ec
, right
, l
, loc
);
2797 } else if (left
.eclass
== ExprClass
.MethodGroup
|| (l
== InternalType
.AnonymousMethod
&& !is_equality
)) {
2798 tmp
= Convert
.ImplicitConversionRequired (ec
, left
, r
, loc
);
2809 // Resolve delegate equality as a user operator
2812 return ResolveUserOperator (ec
, l
, r
);
2815 Arguments args
= new Arguments (2);
2816 args
.Add (new Argument (left
));
2817 args
.Add (new Argument (right
));
2819 if (oper
== Operator
.Addition
) {
2820 if (TypeManager
.delegate_combine_delegate_delegate
== null) {
2821 TypeManager
.delegate_combine_delegate_delegate
= TypeManager
.GetPredefinedMethod (
2822 TypeManager
.delegate_type
, "Combine", loc
, TypeManager
.delegate_type
, TypeManager
.delegate_type
);
2825 method
= TypeManager
.delegate_combine_delegate_delegate
;
2827 if (TypeManager
.delegate_remove_delegate_delegate
== null) {
2828 TypeManager
.delegate_remove_delegate_delegate
= TypeManager
.GetPredefinedMethod (
2829 TypeManager
.delegate_type
, "Remove", loc
, TypeManager
.delegate_type
, TypeManager
.delegate_type
);
2832 method
= TypeManager
.delegate_remove_delegate_delegate
;
2835 MethodGroupExpr mg
= new MethodGroupExpr (new MemberInfo
[] { method }
, TypeManager
.delegate_type
, loc
);
2836 mg
= mg
.OverloadResolve (ec
, ref args
, false, loc
);
2838 return new ClassCast (new UserOperatorCall (mg
, args
, CreateExpressionTree
, loc
), l
);
2842 // Enumeration operators
2844 Expression
ResolveOperatorEnum (ResolveContext ec
, bool lenum
, bool renum
, Type ltype
, Type rtype
)
2847 // bool operator == (E x, E y);
2848 // bool operator != (E x, E y);
2849 // bool operator < (E x, E y);
2850 // bool operator > (E x, E y);
2851 // bool operator <= (E x, E y);
2852 // bool operator >= (E x, E y);
2854 // E operator & (E x, E y);
2855 // E operator | (E x, E y);
2856 // E operator ^ (E x, E y);
2858 // U operator - (E e, E f)
2859 // E operator - (E e, U x)
2861 // E operator + (U x, E e)
2862 // E operator + (E e, U x)
2864 if (!((oper
& (Operator
.ComparisonMask
| Operator
.BitwiseMask
)) != 0 ||
2865 (oper
== Operator
.Subtraction
&& lenum
) ||
2866 (oper
== Operator
.Addition
&& (lenum
!= renum
|| type
!= null)))) // type != null for lifted null
2869 Expression ltemp
= left
;
2870 Expression rtemp
= right
;
2871 Type underlying_type
;
2874 if ((oper
& (Operator
.ComparisonMask
| Operator
.BitwiseMask
)) != 0) {
2876 expr
= Convert
.ImplicitConversion (ec
, left
, rtype
, loc
);
2882 expr
= Convert
.ImplicitConversion (ec
, right
, ltype
, loc
);
2890 if (TypeManager
.IsEqual (ltype
, rtype
)) {
2891 underlying_type
= TypeManager
.GetEnumUnderlyingType (ltype
);
2893 if (left
is Constant
)
2894 left
= ((Constant
) left
).ConvertExplicitly (false, underlying_type
).Resolve (ec
);
2896 left
= EmptyCast
.Create (left
, underlying_type
);
2898 if (right
is Constant
)
2899 right
= ((Constant
) right
).ConvertExplicitly (false, underlying_type
).Resolve (ec
);
2901 right
= EmptyCast
.Create (right
, underlying_type
);
2903 underlying_type
= TypeManager
.GetEnumUnderlyingType (ltype
);
2905 if (oper
!= Operator
.Subtraction
&& oper
!= Operator
.Addition
) {
2906 Constant c
= right
as Constant
;
2907 if (c
== null || !c
.IsDefaultValue
)
2910 if (!Convert
.ImplicitStandardConversionExists (right
, underlying_type
))
2913 right
= Convert
.ImplicitConversionStandard (ec
, right
, underlying_type
, right
.Location
);
2916 if (left
is Constant
)
2917 left
= ((Constant
) left
).ConvertExplicitly (false, underlying_type
).Resolve (ec
);
2919 left
= EmptyCast
.Create (left
, underlying_type
);
2922 underlying_type
= TypeManager
.GetEnumUnderlyingType (rtype
);
2924 if (oper
!= Operator
.Addition
) {
2925 Constant c
= left
as Constant
;
2926 if (c
== null || !c
.IsDefaultValue
)
2929 if (!Convert
.ImplicitStandardConversionExists (left
, underlying_type
))
2932 left
= Convert
.ImplicitConversionStandard (ec
, left
, underlying_type
, left
.Location
);
2935 if (right
is Constant
)
2936 right
= ((Constant
) right
).ConvertExplicitly (false, underlying_type
).Resolve (ec
);
2938 right
= EmptyCast
.Create (right
, underlying_type
);
2945 // C# specification uses explicit cast syntax which means binary promotion
2946 // should happen, however it seems that csc does not do that
2948 if (!DoBinaryOperatorPromotion (ec
)) {
2954 Type res_type
= null;
2955 if ((oper
& Operator
.BitwiseMask
) != 0 || oper
== Operator
.Subtraction
|| oper
== Operator
.Addition
) {
2956 Type promoted_type
= lenum
? left
.Type
: right
.Type
;
2957 enum_conversion
= Convert
.ExplicitNumericConversion (
2958 new EmptyExpression (promoted_type
), underlying_type
);
2960 if (oper
== Operator
.Subtraction
&& renum
&& lenum
)
2961 res_type
= underlying_type
;
2962 else if (oper
== Operator
.Addition
&& renum
)
2968 expr
= ResolveOperatorPredefined (ec
, standard_operators
, true, res_type
);
2969 if (!is_compound
|| expr
== null)
2977 // If the return type of the selected operator is implicitly convertible to the type of x
2979 if (Convert
.ImplicitConversionExists (ec
, expr
, ltype
))
2983 // Otherwise, if the selected operator is a predefined operator, if the return type of the
2984 // selected operator is explicitly convertible to the type of x, and if y is implicitly
2985 // convertible to the type of x or the operator is a shift operator, then the operation
2986 // is evaluated as x = (T)(x op y), where T is the type of x
2988 expr
= Convert
.ExplicitConversion (ec
, expr
, ltype
, loc
);
2992 if (Convert
.ImplicitConversionExists (ec
, ltemp
, ltype
))
2999 // 7.9.6 Reference type equality operators
3001 Binary
ResolveOperatorEqualityRerefence (ResolveContext ec
, Type l
, Type r
)
3004 // operator != (object a, object b)
3005 // operator == (object a, object b)
3008 // TODO: this method is almost equivalent to Convert.ImplicitReferenceConversion
3010 if (left
.eclass
== ExprClass
.MethodGroup
|| right
.eclass
== ExprClass
.MethodGroup
)
3013 type
= TypeManager
.bool_type
;
3014 GenericConstraints constraints
;
3016 bool lgen
= TypeManager
.IsGenericParameter (l
);
3018 if (TypeManager
.IsEqual (l
, r
)) {
3021 // Only allow to compare same reference type parameter
3023 if (TypeManager
.IsReferenceType (l
)) {
3024 left
= new BoxedCast (left
, TypeManager
.object_type
);
3025 right
= new BoxedCast (right
, TypeManager
.object_type
);
3032 if (l
== InternalType
.AnonymousMethod
)
3035 if (TypeManager
.IsValueType (l
))
3041 bool rgen
= TypeManager
.IsGenericParameter (r
);
3044 // a, Both operands are reference-type values or the value null
3045 // b, One operand is a value of type T where T is a type-parameter and
3046 // the other operand is the value null. Furthermore T does not have the
3047 // value type constrain
3049 if (left
is NullLiteral
|| right
is NullLiteral
) {
3051 constraints
= TypeManager
.GetTypeParameterConstraints (l
);
3052 if (constraints
!= null && constraints
.HasValueTypeConstraint
)
3055 left
= new BoxedCast (left
, TypeManager
.object_type
);
3060 constraints
= TypeManager
.GetTypeParameterConstraints (r
);
3061 if (constraints
!= null && constraints
.HasValueTypeConstraint
)
3064 right
= new BoxedCast (right
, TypeManager
.object_type
);
3070 // An interface is converted to the object before the
3071 // standard conversion is applied. It's not clear from the
3072 // standard but it looks like it works like that.
3075 if (!TypeManager
.IsReferenceType (l
))
3078 l
= TypeManager
.object_type
;
3079 left
= new BoxedCast (left
, l
);
3080 } else if (l
.IsInterface
) {
3081 l
= TypeManager
.object_type
;
3082 } else if (TypeManager
.IsStruct (l
)) {
3087 if (!TypeManager
.IsReferenceType (r
))
3090 r
= TypeManager
.object_type
;
3091 right
= new BoxedCast (right
, r
);
3092 } else if (r
.IsInterface
) {
3093 r
= TypeManager
.object_type
;
3094 } else if (TypeManager
.IsStruct (r
)) {
3099 const string ref_comparison
= "Possible unintended reference comparison. " +
3100 "Consider casting the {0} side of the expression to `string' to compare the values";
3103 // A standard implicit conversion exists from the type of either
3104 // operand to the type of the other operand
3106 if (Convert
.ImplicitReferenceConversionExists (left
, r
)) {
3107 if (l
== TypeManager
.string_type
)
3108 ec
.Report
.Warning (253, 2, loc
, ref_comparison
, "right");
3113 if (Convert
.ImplicitReferenceConversionExists (right
, l
)) {
3114 if (r
== TypeManager
.string_type
)
3115 ec
.Report
.Warning (252, 2, loc
, ref_comparison
, "left");
3124 Expression
ResolveOperatorPointer (ResolveContext ec
, Type l
, Type r
)
3127 // bool operator == (void* x, void* y);
3128 // bool operator != (void* x, void* y);
3129 // bool operator < (void* x, void* y);
3130 // bool operator > (void* x, void* y);
3131 // bool operator <= (void* x, void* y);
3132 // bool operator >= (void* x, void* y);
3134 if ((oper
& Operator
.ComparisonMask
) != 0) {
3137 temp
= Convert
.ImplicitConversion (ec
, left
, r
, left
.Location
);
3144 temp
= Convert
.ImplicitConversion (ec
, right
, l
, right
.Location
);
3150 type
= TypeManager
.bool_type
;
3154 if (pointer_operators
== null)
3155 CreatePointerOperatorsTable ();
3157 return ResolveOperatorPredefined (ec
, pointer_operators
, false, null);
3161 // Build-in operators method overloading
3163 protected virtual Expression
ResolveOperatorPredefined (ResolveContext ec
, PredefinedOperator
[] operators
, bool primitives_only
, Type enum_type
)
3165 PredefinedOperator best_operator
= null;
3167 Type r
= right
.Type
;
3168 Operator oper_mask
= oper
& ~Operator
.ValuesOnlyMask
;
3170 foreach (PredefinedOperator po
in operators
) {
3171 if ((po
.OperatorsMask
& oper_mask
) == 0)
3174 if (primitives_only
) {
3175 if (!po
.IsPrimitiveApplicable (l
, r
))
3178 if (!po
.IsApplicable (ec
, left
, right
))
3182 if (best_operator
== null) {
3184 if (primitives_only
)
3190 best_operator
= po
.ResolveBetterOperator (ec
, best_operator
);
3192 if (best_operator
== null) {
3193 ec
.Report
.Error (34, loc
, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
3194 OperName (oper
), TypeManager
.CSharpName (l
), TypeManager
.CSharpName (r
));
3201 if (best_operator
== null)
3204 Expression expr
= best_operator
.ConvertResult (ec
, this);
3207 // Optimize &/&& constant expressions with 0 value
3209 if (oper
== Operator
.BitwiseAnd
|| oper
== Operator
.LogicalAnd
) {
3210 Constant rc
= right
as Constant
;
3211 Constant lc
= left
as Constant
;
3212 if ((lc
!= null && lc
.IsDefaultValue
) || (rc
!= null && rc
.IsDefaultValue
)) {
3214 // The result is a constant with side-effect
3216 Constant side_effect
= rc
== null ?
3217 new SideEffectConstant (lc
, right
, loc
) :
3218 new SideEffectConstant (rc
, left
, loc
);
3220 return ReducedExpression
.Create (side_effect
.Resolve (ec
), expr
);
3224 if (enum_type
== null)
3228 // HACK: required by enum_conversion
3230 expr
.Type
= enum_type
;
3231 return EmptyCast
.Create (expr
, enum_type
);
3235 // Performs user-operator overloading
3237 protected virtual Expression
ResolveUserOperator (ResolveContext ec
, Type l
, Type r
)
3240 if (oper
== Operator
.LogicalAnd
)
3241 user_oper
= Operator
.BitwiseAnd
;
3242 else if (oper
== Operator
.LogicalOr
)
3243 user_oper
= Operator
.BitwiseOr
;
3247 string op
= GetOperatorMetadataName (user_oper
);
3249 MethodGroupExpr left_operators
= MemberLookup (ec
.Compiler
, ec
.CurrentType
, l
, op
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
3250 MethodGroupExpr right_operators
= null;
3252 if (!TypeManager
.IsEqual (r
, l
)) {
3253 right_operators
= MemberLookup (ec
.Compiler
, ec
.CurrentType
, r
, op
, MemberTypes
.Method
, AllBindingFlags
, loc
) as MethodGroupExpr
;
3254 if (right_operators
== null && left_operators
== null)
3256 } else if (left_operators
== null) {
3260 Arguments args
= new Arguments (2);
3261 Argument larg
= new Argument (left
);
3263 Argument rarg
= new Argument (right
);
3266 MethodGroupExpr union
;
3269 // User-defined operator implementations always take precedence
3270 // over predefined operator implementations
3272 if (left_operators
!= null && right_operators
!= null) {
3273 if (IsPredefinedUserOperator (l
, user_oper
)) {
3274 union
= right_operators
.OverloadResolve (ec
, ref args
, true, loc
);
3276 union
= left_operators
;
3277 } else if (IsPredefinedUserOperator (r
, user_oper
)) {
3278 union
= left_operators
.OverloadResolve (ec
, ref args
, true, loc
);
3280 union
= right_operators
;
3282 union
= MethodGroupExpr
.MakeUnionSet (left_operators
, right_operators
, loc
);
3284 } else if (left_operators
!= null) {
3285 union
= left_operators
;
3287 union
= right_operators
;
3290 union
= union
.OverloadResolve (ec
, ref args
, true, loc
);
3294 Expression oper_expr
;
3296 // TODO: CreateExpressionTree is allocated every time
3297 if (user_oper
!= oper
) {
3298 oper_expr
= new ConditionalLogicalOperator (union
, args
, CreateExpressionTree
,
3299 oper
== Operator
.LogicalAnd
, loc
).Resolve (ec
);
3301 oper_expr
= new UserOperatorCall (union
, args
, CreateExpressionTree
, loc
);
3304 // This is used to check if a test 'x == null' can be optimized to a reference equals,
3305 // and not invoke user operator
3307 if ((oper
& Operator
.EqualityMask
) != 0) {
3308 if ((left
is NullLiteral
&& IsBuildInEqualityOperator (r
)) ||
3309 (right
is NullLiteral
&& IsBuildInEqualityOperator (l
))) {
3310 type
= TypeManager
.bool_type
;
3311 if (left
is NullLiteral
|| right
is NullLiteral
)
3312 oper_expr
= ReducedExpression
.Create (this, oper_expr
);
3313 } else if (l
!= r
) {
3314 MethodInfo mi
= (MethodInfo
) union
;
3317 // Two System.Delegate(s) are never equal
3319 if (mi
.DeclaringType
== TypeManager
.multicast_delegate_type
)
3330 public override TypeExpr
ResolveAsTypeTerminal (IMemberContext ec
, bool silent
)
3335 private void CheckUselessComparison (ResolveContext ec
, Constant c
, Type type
)
3337 if (c
== null || !IsTypeIntegral (type
)
3338 || c
is StringConstant
3339 || c
is BoolConstant
3340 || c
is FloatConstant
3341 || c
is DoubleConstant
3342 || c
is DecimalConstant
3348 if (c
is ULongConstant
) {
3349 ulong uvalue
= ((ULongConstant
) c
).Value
;
3350 if (uvalue
> long.MaxValue
) {
3351 if (type
== TypeManager
.byte_type
||
3352 type
== TypeManager
.sbyte_type
||
3353 type
== TypeManager
.short_type
||
3354 type
== TypeManager
.ushort_type
||
3355 type
== TypeManager
.int32_type
||
3356 type
== TypeManager
.uint32_type
||
3357 type
== TypeManager
.int64_type
||
3358 type
== TypeManager
.char_type
)
3359 WarnUselessComparison (ec
, type
);
3362 value = (long) uvalue
;
3364 else if (c
is ByteConstant
)
3365 value = ((ByteConstant
) c
).Value
;
3366 else if (c
is SByteConstant
)
3367 value = ((SByteConstant
) c
).Value
;
3368 else if (c
is ShortConstant
)
3369 value = ((ShortConstant
) c
).Value
;
3370 else if (c
is UShortConstant
)
3371 value = ((UShortConstant
) c
).Value
;
3372 else if (c
is IntConstant
)
3373 value = ((IntConstant
) c
).Value
;
3374 else if (c
is UIntConstant
)
3375 value = ((UIntConstant
) c
).Value
;
3376 else if (c
is LongConstant
)
3377 value = ((LongConstant
) c
).Value
;
3378 else if (c
is CharConstant
)
3379 value = ((CharConstant
)c
).Value
;
3384 if (IsValueOutOfRange (value, type
))
3385 WarnUselessComparison (ec
, type
);
3388 static bool IsValueOutOfRange (long value, Type type
)
3390 if (IsTypeUnsigned (type
) && value < 0)
3392 return type
== TypeManager
.sbyte_type
&& (value >= 0x80 || value < -0x80) ||
3393 type
== TypeManager
.byte_type
&& value >= 0x100 ||
3394 type
== TypeManager
.short_type
&& (value >= 0x8000 || value < -0x8000) ||
3395 type
== TypeManager
.ushort_type
&& value >= 0x10000 ||
3396 type
== TypeManager
.int32_type
&& (value >= 0x80000000 || value < -0x80000000) ||
3397 type
== TypeManager
.uint32_type
&& value >= 0x100000000;
3400 static bool IsBuildInEqualityOperator (Type t
)
3402 return t
== TypeManager
.object_type
|| t
== TypeManager
.string_type
||
3403 t
== TypeManager
.delegate_type
|| TypeManager
.IsDelegateType (t
);
3406 static bool IsPredefinedUserOperator (Type t
, Operator op
)
3409 // Some predefined types have user operators
3411 return (op
& Operator
.EqualityMask
) != 0 && (t
== TypeManager
.string_type
|| t
== TypeManager
.decimal_type
);
3414 private static bool IsTypeIntegral (Type type
)
3416 return type
== TypeManager
.uint64_type
||
3417 type
== TypeManager
.int64_type
||
3418 type
== TypeManager
.uint32_type
||
3419 type
== TypeManager
.int32_type
||
3420 type
== TypeManager
.ushort_type
||
3421 type
== TypeManager
.short_type
||
3422 type
== TypeManager
.sbyte_type
||
3423 type
== TypeManager
.byte_type
||
3424 type
== TypeManager
.char_type
;
3427 private static bool IsTypeUnsigned (Type type
)
3429 return type
== TypeManager
.uint64_type
||
3430 type
== TypeManager
.uint32_type
||
3431 type
== TypeManager
.ushort_type
||
3432 type
== TypeManager
.byte_type
||
3433 type
== TypeManager
.char_type
;
3436 private void WarnUselessComparison (ResolveContext ec
, Type type
)
3438 ec
.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}'",
3439 TypeManager
.CSharpName (type
));
3443 /// EmitBranchable is called from Statement.EmitBoolExpression in the
3444 /// context of a conditional bool expression. This function will return
3445 /// false if it is was possible to use EmitBranchable, or true if it was.
3447 /// The expression's code is generated, and we will generate a branch to `target'
3448 /// if the resulting expression value is equal to isTrue
3450 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
3452 ILGenerator ig
= ec
.ig
;
3455 // This is more complicated than it looks, but its just to avoid
3456 // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
3457 // but on top of that we want for == and != to use a special path
3458 // if we are comparing against null
3460 if ((oper
== Operator
.Equality
|| oper
== Operator
.Inequality
) && (left
is Constant
|| right
is Constant
)) {
3461 bool my_on_true
= oper
== Operator
.Inequality
? on_true
: !on_true
;
3464 // put the constant on the rhs, for simplicity
3466 if (left
is Constant
) {
3467 Expression swap
= right
;
3472 if (((Constant
) right
).IsZeroInteger
) {
3473 left
.EmitBranchable (ec
, target
, my_on_true
);
3476 if (right
.Type
== TypeManager
.bool_type
) {
3477 // right is a boolean, and it's not 'false' => it is 'true'
3478 left
.EmitBranchable (ec
, target
, !my_on_true
);
3482 } else if (oper
== Operator
.LogicalAnd
) {
3485 Label tests_end
= ig
.DefineLabel ();
3487 left
.EmitBranchable (ec
, tests_end
, false);
3488 right
.EmitBranchable (ec
, target
, true);
3489 ig
.MarkLabel (tests_end
);
3492 // This optimizes code like this
3493 // if (true && i > 4)
3495 if (!(left
is Constant
))
3496 left
.EmitBranchable (ec
, target
, false);
3498 if (!(right
is Constant
))
3499 right
.EmitBranchable (ec
, target
, false);
3504 } else if (oper
== Operator
.LogicalOr
){
3506 left
.EmitBranchable (ec
, target
, true);
3507 right
.EmitBranchable (ec
, target
, true);
3510 Label tests_end
= ig
.DefineLabel ();
3511 left
.EmitBranchable (ec
, tests_end
, true);
3512 right
.EmitBranchable (ec
, target
, false);
3513 ig
.MarkLabel (tests_end
);
3518 } else if (!(oper
== Operator
.LessThan
|| oper
== Operator
.GreaterThan
||
3519 oper
== Operator
.LessThanOrEqual
|| oper
== Operator
.GreaterThanOrEqual
||
3520 oper
== Operator
.Equality
|| oper
== Operator
.Inequality
)) {
3521 base.EmitBranchable (ec
, target
, on_true
);
3529 bool is_float
= IsFloat (t
);
3530 bool is_unsigned
= is_float
|| IsUnsigned (t
);
3533 case Operator
.Equality
:
3535 ig
.Emit (OpCodes
.Beq
, target
);
3537 ig
.Emit (OpCodes
.Bne_Un
, target
);
3540 case Operator
.Inequality
:
3542 ig
.Emit (OpCodes
.Bne_Un
, target
);
3544 ig
.Emit (OpCodes
.Beq
, target
);
3547 case Operator
.LessThan
:
3549 if (is_unsigned
&& !is_float
)
3550 ig
.Emit (OpCodes
.Blt_Un
, target
);
3552 ig
.Emit (OpCodes
.Blt
, target
);
3555 ig
.Emit (OpCodes
.Bge_Un
, target
);
3557 ig
.Emit (OpCodes
.Bge
, target
);
3560 case Operator
.GreaterThan
:
3562 if (is_unsigned
&& !is_float
)
3563 ig
.Emit (OpCodes
.Bgt_Un
, target
);
3565 ig
.Emit (OpCodes
.Bgt
, target
);
3568 ig
.Emit (OpCodes
.Ble_Un
, target
);
3570 ig
.Emit (OpCodes
.Ble
, target
);
3573 case Operator
.LessThanOrEqual
:
3575 if (is_unsigned
&& !is_float
)
3576 ig
.Emit (OpCodes
.Ble_Un
, target
);
3578 ig
.Emit (OpCodes
.Ble
, target
);
3581 ig
.Emit (OpCodes
.Bgt_Un
, target
);
3583 ig
.Emit (OpCodes
.Bgt
, target
);
3587 case Operator
.GreaterThanOrEqual
:
3589 if (is_unsigned
&& !is_float
)
3590 ig
.Emit (OpCodes
.Bge_Un
, target
);
3592 ig
.Emit (OpCodes
.Bge
, target
);
3595 ig
.Emit (OpCodes
.Blt_Un
, target
);
3597 ig
.Emit (OpCodes
.Blt
, target
);
3600 throw new InternalErrorException (oper
.ToString ());
3604 public override void Emit (EmitContext ec
)
3606 EmitOperator (ec
, left
.Type
);
3609 protected virtual void EmitOperator (EmitContext ec
, Type l
)
3611 ILGenerator ig
= ec
.ig
;
3614 // Handle short-circuit operators differently
3617 if ((oper
& Operator
.LogicalMask
) != 0) {
3618 Label load_result
= ig
.DefineLabel ();
3619 Label end
= ig
.DefineLabel ();
3621 bool is_or
= oper
== Operator
.LogicalOr
;
3622 left
.EmitBranchable (ec
, load_result
, is_or
);
3624 ig
.Emit (OpCodes
.Br_S
, end
);
3626 ig
.MarkLabel (load_result
);
3627 ig
.Emit (is_or
? OpCodes
.Ldc_I4_1
: OpCodes
.Ldc_I4_0
);
3633 // Optimize zero-based operations which cannot be optimized at expression level
3635 if (oper
== Operator
.Subtraction
) {
3636 var lc
= left
as IntegralConstant
;
3637 if (lc
!= null && lc
.IsDefaultValue
) {
3639 ig
.Emit (OpCodes
.Neg
);
3646 EmitOperatorOpcode (ec
, oper
, l
);
3649 // Nullable enum could require underlying type cast and we cannot simply wrap binary
3650 // expression because that would wrap lifted binary operation
3652 if (enum_conversion
!= null)
3653 enum_conversion
.Emit (ec
);
3656 public override void EmitSideEffect (EmitContext ec
)
3658 if ((oper
& Operator
.LogicalMask
) != 0 ||
3659 (ec
.HasSet (EmitContext
.Options
.CheckedScope
) && (oper
== Operator
.Multiply
|| oper
== Operator
.Addition
|| oper
== Operator
.Subtraction
))) {
3660 base.EmitSideEffect (ec
);
3662 left
.EmitSideEffect (ec
);
3663 right
.EmitSideEffect (ec
);
3667 protected override void CloneTo (CloneContext clonectx
, Expression t
)
3669 Binary target
= (Binary
) t
;
3671 target
.left
= left
.Clone (clonectx
);
3672 target
.right
= right
.Clone (clonectx
);
3675 public Expression
CreateCallSiteBinder (ResolveContext ec
, Arguments args
)
3677 Arguments binder_args
= new Arguments (3);
3679 MemberAccess sle
= new MemberAccess (new MemberAccess (
3680 new QualifiedAliasMember (QualifiedAliasMember
.GlobalAlias
, "System", loc
), "Linq", loc
), "Expressions", loc
);
3682 CSharpBinderFlags flags
= 0;
3683 if (ec
.HasSet (ResolveContext
.Options
.CheckedScope
))
3684 flags
= CSharpBinderFlags
.CheckedContext
;
3686 if ((oper
& Operator
.LogicalMask
) != 0)
3687 flags
|= CSharpBinderFlags
.BinaryOperationLogical
;
3689 binder_args
.Add (new Argument (new EnumConstant (new IntLiteral ((int) flags
, loc
), TypeManager
.binder_flags
)));
3690 binder_args
.Add (new Argument (new MemberAccess (new MemberAccess (sle
, "ExpressionType", loc
), GetOperatorExpressionTypeName (), loc
)));
3691 binder_args
.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args
.CreateDynamicBinderArguments (ec
), loc
)));
3693 return new Invocation (DynamicExpressionStatement
.GetBinder ("BinaryOperation", loc
), binder_args
);
3696 public override Expression
CreateExpressionTree (ResolveContext ec
)
3698 return CreateExpressionTree (ec
, null);
3701 Expression
CreateExpressionTree (ResolveContext ec
, MethodGroupExpr method
)
3704 bool lift_arg
= false;
3707 case Operator
.Addition
:
3708 if (method
== null && ec
.HasSet (ResolveContext
.Options
.CheckedScope
) && !IsFloat (type
))
3709 method_name
= "AddChecked";
3711 method_name
= "Add";
3713 case Operator
.BitwiseAnd
:
3714 method_name
= "And";
3716 case Operator
.BitwiseOr
:
3719 case Operator
.Division
:
3720 method_name
= "Divide";
3722 case Operator
.Equality
:
3723 method_name
= "Equal";
3726 case Operator
.ExclusiveOr
:
3727 method_name
= "ExclusiveOr";
3729 case Operator
.GreaterThan
:
3730 method_name
= "GreaterThan";
3733 case Operator
.GreaterThanOrEqual
:
3734 method_name
= "GreaterThanOrEqual";
3737 case Operator
.Inequality
:
3738 method_name
= "NotEqual";
3741 case Operator
.LeftShift
:
3742 method_name
= "LeftShift";
3744 case Operator
.LessThan
:
3745 method_name
= "LessThan";
3748 case Operator
.LessThanOrEqual
:
3749 method_name
= "LessThanOrEqual";
3752 case Operator
.LogicalAnd
:
3753 method_name
= "AndAlso";
3755 case Operator
.LogicalOr
:
3756 method_name
= "OrElse";
3758 case Operator
.Modulus
:
3759 method_name
= "Modulo";
3761 case Operator
.Multiply
:
3762 if (method
== null && ec
.HasSet (ResolveContext
.Options
.CheckedScope
) && !IsFloat (type
))
3763 method_name
= "MultiplyChecked";
3765 method_name
= "Multiply";
3767 case Operator
.RightShift
:
3768 method_name
= "RightShift";
3770 case Operator
.Subtraction
:
3771 if (method
== null && ec
.HasSet (ResolveContext
.Options
.CheckedScope
) && !IsFloat (type
))
3772 method_name
= "SubtractChecked";
3774 method_name
= "Subtract";
3778 throw new InternalErrorException ("Unknown expression tree binary operator " + oper
);
3781 Arguments args
= new Arguments (2);
3782 args
.Add (new Argument (left
.CreateExpressionTree (ec
)));
3783 args
.Add (new Argument (right
.CreateExpressionTree (ec
)));
3784 if (method
!= null) {
3786 args
.Add (new Argument (new BoolConstant (false, loc
)));
3788 args
.Add (new Argument (method
.CreateExpressionTree (ec
)));
3791 return CreateExpressionFactoryCall (ec
, method_name
, args
);
3796 // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
3797 // b, c, d... may be strings or objects.
3799 public class StringConcat
: Expression
{
3800 Arguments arguments
;
3802 public StringConcat (Expression left
, Expression right
, Location loc
)
3805 type
= TypeManager
.string_type
;
3806 eclass
= ExprClass
.Value
;
3808 arguments
= new Arguments (2);
3811 public static StringConcat
Create (ResolveContext rc
, Expression left
, Expression right
, Location loc
)
3813 if (left
.eclass
== ExprClass
.Unresolved
|| right
.eclass
== ExprClass
.Unresolved
)
3814 throw new ArgumentException ();
3816 var s
= new StringConcat (left
, right
, loc
);
3817 s
.Append (rc
, left
);
3818 s
.Append (rc
, right
);
3822 public override Expression
CreateExpressionTree (ResolveContext ec
)
3824 Argument arg
= arguments
[0];
3825 return CreateExpressionAddCall (ec
, arg
, arg
.CreateExpressionTree (ec
), 1);
3829 // Creates nested calls tree from an array of arguments used for IL emit
3831 Expression
CreateExpressionAddCall (ResolveContext ec
, Argument left
, Expression left_etree
, int pos
)
3833 Arguments concat_args
= new Arguments (2);
3834 Arguments add_args
= new Arguments (3);
3836 concat_args
.Add (left
);
3837 add_args
.Add (new Argument (left_etree
));
3839 concat_args
.Add (arguments
[pos
]);
3840 add_args
.Add (new Argument (arguments
[pos
].CreateExpressionTree (ec
)));
3842 MethodGroupExpr method
= CreateConcatMemberExpression ().Resolve (ec
) as MethodGroupExpr
;
3846 method
= method
.OverloadResolve (ec
, ref concat_args
, false, loc
);
3850 add_args
.Add (new Argument (method
.CreateExpressionTree (ec
)));
3852 Expression expr
= CreateExpressionFactoryCall (ec
, "Add", add_args
);
3853 if (++pos
== arguments
.Count
)
3856 left
= new Argument (new EmptyExpression (((MethodInfo
)method
).ReturnType
));
3857 return CreateExpressionAddCall (ec
, left
, expr
, pos
);
3860 protected override Expression
DoResolve (ResolveContext ec
)
3865 void Append (ResolveContext rc
, Expression operand
)
3870 StringConstant sc
= operand
as StringConstant
;
3872 if (arguments
.Count
!= 0) {
3873 Argument last_argument
= arguments
[arguments
.Count
- 1];
3874 StringConstant last_expr_constant
= last_argument
.Expr
as StringConstant
;
3875 if (last_expr_constant
!= null) {
3876 last_argument
.Expr
= new StringConstant (
3877 last_expr_constant
.Value
+ sc
.Value
, sc
.Location
).Resolve (rc
);
3883 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
3885 StringConcat concat_oper
= operand
as StringConcat
;
3886 if (concat_oper
!= null) {
3887 arguments
.AddRange (concat_oper
.arguments
);
3892 arguments
.Add (new Argument (operand
));
3895 Expression
CreateConcatMemberExpression ()
3897 return new MemberAccess (new MemberAccess (new QualifiedAliasMember ("global", "System", loc
), "String", loc
), "Concat", loc
);
3900 public override void Emit (EmitContext ec
)
3902 Expression concat
= new Invocation (CreateConcatMemberExpression (), arguments
, true);
3903 concat
= concat
.Resolve (new ResolveContext (ec
.MemberContext
));
3908 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
3910 if (arguments
.Count
!= 2)
3911 throw new NotImplementedException ("arguments.Count != 2");
3913 var concat
= TypeManager
.string_type
.GetMethod ("Concat", new[] { typeof (object), typeof (object) }
);
3914 return SLE
.Expression
.Add (arguments
[0].Expr
.MakeExpression (ctx
), arguments
[1].Expr
.MakeExpression (ctx
), concat
);
3917 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
3919 arguments
.MutateHoistedGenericType (storey
);
3924 // User-defined conditional logical operator
3926 public class ConditionalLogicalOperator
: UserOperatorCall
{
3927 readonly bool is_and
;
3930 public ConditionalLogicalOperator (MethodGroupExpr oper_method
, Arguments arguments
,
3931 ExpressionTreeExpression expr_tree
, bool is_and
, Location loc
)
3932 : base (oper_method
, arguments
, expr_tree
, loc
)
3934 this.is_and
= is_and
;
3935 eclass
= ExprClass
.Unresolved
;
3938 protected override Expression
DoResolve (ResolveContext ec
)
3940 MethodInfo method
= (MethodInfo
)mg
;
3941 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
3942 AParametersCollection pd
= TypeManager
.GetParameterData (method
);
3943 if (!TypeManager
.IsEqual (type
, type
) || !TypeManager
.IsEqual (type
, pd
.Types
[0]) || !TypeManager
.IsEqual (type
, pd
.Types
[1])) {
3944 ec
.Report
.Error (217, loc
,
3945 "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",
3946 TypeManager
.CSharpSignature (method
));
3950 Expression left_dup
= new EmptyExpression (type
);
3951 Expression op_true
= GetOperatorTrue (ec
, left_dup
, loc
);
3952 Expression op_false
= GetOperatorFalse (ec
, left_dup
, loc
);
3953 if (op_true
== null || op_false
== null) {
3954 ec
.Report
.Error (218, loc
,
3955 "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
3956 TypeManager
.CSharpName (type
), TypeManager
.CSharpSignature (method
));
3960 oper
= is_and
? op_false
: op_true
;
3961 eclass
= ExprClass
.Value
;
3965 public override void Emit (EmitContext ec
)
3967 ILGenerator ig
= ec
.ig
;
3968 Label end_target
= ig
.DefineLabel ();
3971 // Emit and duplicate left argument
3973 arguments
[0].Expr
.Emit (ec
);
3974 ig
.Emit (OpCodes
.Dup
);
3975 arguments
.RemoveAt (0);
3977 oper
.EmitBranchable (ec
, end_target
, true);
3979 ig
.MarkLabel (end_target
);
3983 public class PointerArithmetic
: Expression
{
3984 Expression left
, right
;
3988 // We assume that `l' is always a pointer
3990 public PointerArithmetic (Binary
.Operator op
, Expression l
, Expression r
, Type t
, Location loc
)
3999 public override Expression
CreateExpressionTree (ResolveContext ec
)
4001 Error_PointerInsideExpressionTree (ec
);
4005 protected override Expression
DoResolve (ResolveContext ec
)
4007 eclass
= ExprClass
.Variable
;
4009 if (left
.Type
== TypeManager
.void_ptr_type
) {
4010 ec
.Report
.Error (242, loc
, "The operation in question is undefined on void pointers");
4017 public override void Emit (EmitContext ec
)
4019 Type op_type
= left
.Type
;
4020 ILGenerator ig
= ec
.ig
;
4022 // It must be either array or fixed buffer
4024 if (TypeManager
.HasElementType (op_type
)) {
4025 element
= TypeManager
.GetElementType (op_type
);
4027 FieldExpr fe
= left
as FieldExpr
;
4029 element
= AttributeTester
.GetFixedBuffer (fe
.FieldInfo
).ElementType
;
4034 int size
= GetTypeSize (element
);
4035 Type rtype
= right
.Type
;
4037 if ((op
& Binary
.Operator
.SubtractionMask
) != 0 && rtype
.IsPointer
){
4039 // handle (pointer - pointer)
4043 ig
.Emit (OpCodes
.Sub
);
4047 ig
.Emit (OpCodes
.Sizeof
, element
);
4049 IntLiteral
.EmitInt (ig
, size
);
4050 ig
.Emit (OpCodes
.Div
);
4052 ig
.Emit (OpCodes
.Conv_I8
);
4055 // handle + and - on (pointer op int)
4057 Constant left_const
= left
as Constant
;
4058 if (left_const
!= null) {
4060 // Optimize ((T*)null) pointer operations
4062 if (left_const
.IsDefaultValue
) {
4063 left
= EmptyExpression
.Null
;
4071 Constant right_const
= right
as Constant
;
4072 if (right_const
!= null) {
4074 // Optimize 0-based arithmetic
4076 if (right_const
.IsDefaultValue
)
4080 // TODO: Should be the checks resolve context sensitive?
4081 ResolveContext rc
= new ResolveContext (ec
.MemberContext
);
4082 right
= ConstantFold
.BinaryFold (rc
, Binary
.Operator
.Multiply
, new IntConstant (size
, right
.Location
).Resolve (rc
), right_const
, loc
);
4086 ig
.Emit (OpCodes
.Sizeof
, element
);
4087 right
= EmptyExpression
.Null
;
4092 if (rtype
== TypeManager
.sbyte_type
|| rtype
== TypeManager
.byte_type
||
4093 rtype
== TypeManager
.short_type
|| rtype
== TypeManager
.ushort_type
) {
4094 ig
.Emit (OpCodes
.Conv_I
);
4095 } else if (rtype
== TypeManager
.uint32_type
) {
4096 ig
.Emit (OpCodes
.Conv_U
);
4099 if (right_const
== null && size
!= 1){
4101 ig
.Emit (OpCodes
.Sizeof
, element
);
4103 IntLiteral
.EmitInt (ig
, size
);
4104 if (rtype
== TypeManager
.int64_type
|| rtype
== TypeManager
.uint64_type
)
4105 ig
.Emit (OpCodes
.Conv_I8
);
4107 Binary
.EmitOperatorOpcode (ec
, Binary
.Operator
.Multiply
, rtype
);
4110 if (left_const
== null) {
4111 if (rtype
== TypeManager
.int64_type
)
4112 ig
.Emit (OpCodes
.Conv_I
);
4113 else if (rtype
== TypeManager
.uint64_type
)
4114 ig
.Emit (OpCodes
.Conv_U
);
4116 Binary
.EmitOperatorOpcode (ec
, op
, op_type
);
4123 // A boolean-expression is an expression that yields a result
4126 public class BooleanExpression
: ShimExpression
4128 public BooleanExpression (Expression expr
)
4131 this.loc
= expr
.Location
;
4134 public override Expression
CreateExpressionTree (ResolveContext ec
)
4136 // TODO: We should emit IsTrue (v4) instead of direct user operator
4137 // call but that would break csc compatibility
4138 return base.CreateExpressionTree (ec
);
4141 protected override Expression
DoResolve (ResolveContext ec
)
4143 // A boolean-expression is required to be of a type
4144 // that can be implicitly converted to bool or of
4145 // a type that implements operator true
4147 expr
= expr
.Resolve (ec
);
4151 Assign ass
= expr
as Assign
;
4152 if (ass
!= null && ass
.Source
is Constant
) {
4153 ec
.Report
.Warning (665, 3, loc
,
4154 "Assignment in conditional expression is always constant. Did you mean to use `==' instead ?");
4157 if (expr
.Type
== TypeManager
.bool_type
)
4160 if (TypeManager
.IsDynamicType (expr
.Type
)) {
4161 Arguments args
= new Arguments (1);
4162 args
.Add (new Argument (expr
));
4163 return new DynamicUnaryConversion ("IsTrue", args
, loc
).Resolve (ec
);
4166 type
= TypeManager
.bool_type
;
4167 Expression converted
= Convert
.ImplicitConversion (ec
, expr
, type
, loc
);
4168 if (converted
!= null)
4172 // If no implicit conversion to bool exists, try using `operator true'
4174 converted
= GetOperatorTrue (ec
, expr
, loc
);
4175 if (converted
== null) {
4176 expr
.Error_ValueCannotBeConverted (ec
, loc
, type
, false);
4185 /// Implements the ternary conditional operator (?:)
4187 public class Conditional
: Expression
{
4188 Expression expr
, true_expr
, false_expr
;
4190 public Conditional (BooleanExpression expr
, Expression true_expr
, Expression false_expr
)
4193 this.true_expr
= true_expr
;
4194 this.false_expr
= false_expr
;
4195 this.loc
= expr
.Location
;
4198 public Expression Expr
{
4204 public Expression TrueExpr
{
4210 public Expression FalseExpr
{
4216 public override Expression
CreateExpressionTree (ResolveContext ec
)
4218 Arguments args
= new Arguments (3);
4219 args
.Add (new Argument (expr
.CreateExpressionTree (ec
)));
4220 args
.Add (new Argument (true_expr
.CreateExpressionTree (ec
)));
4221 args
.Add (new Argument (false_expr
.CreateExpressionTree (ec
)));
4222 return CreateExpressionFactoryCall (ec
, "Condition", args
);
4225 protected override Expression
DoResolve (ResolveContext ec
)
4227 expr
= expr
.Resolve (ec
);
4228 true_expr
= true_expr
.Resolve (ec
);
4229 false_expr
= false_expr
.Resolve (ec
);
4231 if (true_expr
== null || false_expr
== null || expr
== null)
4234 eclass
= ExprClass
.Value
;
4235 Type true_type
= true_expr
.Type
;
4236 Type false_type
= false_expr
.Type
;
4240 // First, if an implicit conversion exists from true_expr
4241 // to false_expr, then the result type is of type false_expr.Type
4243 if (!TypeManager
.IsEqual (true_type
, false_type
)) {
4244 Expression conv
= Convert
.ImplicitConversion (ec
, true_expr
, false_type
, loc
);
4247 // Check if both can convert implicitly to each other's type
4249 if (Convert
.ImplicitConversion (ec
, false_expr
, true_type
, loc
) != null) {
4250 ec
.Report
.Error (172, loc
,
4251 "Type of conditional expression cannot be determined as `{0}' and `{1}' convert implicitly to each other",
4252 TypeManager
.CSharpName (true_type
), TypeManager
.CSharpName (false_type
));
4257 } else if ((conv
= Convert
.ImplicitConversion (ec
, false_expr
, true_type
, loc
)) != null) {
4260 ec
.Report
.Error (173, loc
,
4261 "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
4262 TypeManager
.CSharpName (true_type
), TypeManager
.CSharpName (false_type
));
4267 // Dead code optimalization
4268 Constant c
= expr
as Constant
;
4270 bool is_false
= c
.IsDefaultValue
;
4271 ec
.Report
.Warning (429, 4, is_false
? true_expr
.Location
: false_expr
.Location
, "Unreachable expression code detected");
4272 return ReducedExpression
.Create (is_false
? false_expr
: true_expr
, this);
4278 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
4280 expr
.MutateHoistedGenericType (storey
);
4281 true_expr
.MutateHoistedGenericType (storey
);
4282 false_expr
.MutateHoistedGenericType (storey
);
4283 type
= storey
.MutateType (type
);
4286 public override TypeExpr
ResolveAsTypeTerminal (IMemberContext ec
, bool silent
)
4291 public override void Emit (EmitContext ec
)
4293 ILGenerator ig
= ec
.ig
;
4294 Label false_target
= ig
.DefineLabel ();
4295 Label end_target
= ig
.DefineLabel ();
4297 expr
.EmitBranchable (ec
, false_target
, false);
4298 true_expr
.Emit (ec
);
4300 if (type
.IsInterface
) {
4301 LocalBuilder temp
= ec
.GetTemporaryLocal (type
);
4302 ig
.Emit (OpCodes
.Stloc
, temp
);
4303 ig
.Emit (OpCodes
.Ldloc
, temp
);
4304 ec
.FreeTemporaryLocal (temp
, type
);
4307 ig
.Emit (OpCodes
.Br
, end_target
);
4308 ig
.MarkLabel (false_target
);
4309 false_expr
.Emit (ec
);
4310 ig
.MarkLabel (end_target
);
4313 protected override void CloneTo (CloneContext clonectx
, Expression t
)
4315 Conditional target
= (Conditional
) t
;
4317 target
.expr
= expr
.Clone (clonectx
);
4318 target
.true_expr
= true_expr
.Clone (clonectx
);
4319 target
.false_expr
= false_expr
.Clone (clonectx
);
4323 public abstract class VariableReference
: Expression
, IAssignMethod
, IMemoryLocation
, IVariableReference
{
4324 LocalTemporary temp
;
4327 public abstract HoistedVariable
GetHoistedVariable (AnonymousExpression ae
);
4328 public abstract bool IsFixed { get; }
4329 public abstract bool IsRef { get; }
4330 public abstract string Name { get; }
4331 public abstract void SetHasAddressTaken ();
4334 // Variable IL data, it has to be protected to encapsulate hoisted variables
4336 protected abstract ILocalVariable Variable { get; }
4339 // Variable flow-analysis data
4341 public abstract VariableInfo VariableInfo { get; }
4344 public virtual void AddressOf (EmitContext ec
, AddressOp mode
)
4346 HoistedVariable hv
= GetHoistedVariable (ec
);
4348 hv
.AddressOf (ec
, mode
);
4352 Variable
.EmitAddressOf (ec
);
4355 public HoistedVariable
GetHoistedVariable (ResolveContext rc
)
4357 return GetHoistedVariable (rc
.CurrentAnonymousMethod
);
4360 public HoistedVariable
GetHoistedVariable (EmitContext ec
)
4362 return GetHoistedVariable (ec
.CurrentAnonymousMethod
);
4365 public override string GetSignatureForError ()
4370 public override void Emit (EmitContext ec
)
4375 public override void EmitSideEffect (EmitContext ec
)
4381 // This method is used by parameters that are references, that are
4382 // being passed as references: we only want to pass the pointer (that
4383 // is already stored in the parameter, not the address of the pointer,
4384 // and not the value of the variable).
4386 public void EmitLoad (EmitContext ec
)
4391 public void Emit (EmitContext ec
, bool leave_copy
)
4393 Report
.Debug (64, "VARIABLE EMIT", this, Variable
, type
, IsRef
, loc
);
4395 HoistedVariable hv
= GetHoistedVariable (ec
);
4397 hv
.Emit (ec
, leave_copy
);
4405 // If we are a reference, we loaded on the stack a pointer
4406 // Now lets load the real value
4408 LoadFromPtr (ec
.ig
, type
);
4412 ec
.ig
.Emit (OpCodes
.Dup
);
4415 temp
= new LocalTemporary (Type
);
4421 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
,
4422 bool prepare_for_load
)
4424 HoistedVariable hv
= GetHoistedVariable (ec
);
4426 hv
.EmitAssign (ec
, source
, leave_copy
, prepare_for_load
);
4430 New n_source
= source
as New
;
4431 if (n_source
!= null) {
4432 if (!n_source
.Emit (ec
, this)) {
4445 ec
.ig
.Emit (OpCodes
.Dup
);
4447 temp
= new LocalTemporary (Type
);
4453 StoreFromPtr (ec
.ig
, type
);
4455 Variable
.EmitAssign (ec
);
4463 public bool IsHoisted
{
4464 get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
4467 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
4469 type
= storey
.MutateType (type
);
4476 public class LocalVariableReference
: VariableReference
{
4477 readonly string name
;
4479 public LocalInfo local_info
;
4482 public LocalVariableReference (Block block
, string name
, Location l
)
4490 // Setting `is_readonly' to false will allow you to create a writable
4491 // reference to a read-only variable. This is used by foreach and using.
4493 public LocalVariableReference (Block block
, string name
, Location l
,
4494 LocalInfo local_info
, bool is_readonly
)
4495 : this (block
, name
, l
)
4497 this.local_info
= local_info
;
4498 this.is_readonly
= is_readonly
;
4501 public override VariableInfo VariableInfo
{
4502 get { return local_info.VariableInfo; }
4505 public override HoistedVariable
GetHoistedVariable (AnonymousExpression ae
)
4507 return local_info
.HoistedVariant
;
4511 // A local variable is always fixed
4513 public override bool IsFixed
{
4514 get { return true; }
4517 public override bool IsRef
{
4518 get { return false; }
4521 public bool IsReadOnly
{
4522 get { return is_readonly; }
4525 public override string Name
{
4526 get { return name; }
4529 public bool VerifyAssigned (ResolveContext ec
)
4531 VariableInfo variable_info
= local_info
.VariableInfo
;
4532 return variable_info
== null || variable_info
.IsAssigned (ec
, loc
);
4535 void ResolveLocalInfo ()
4537 if (local_info
== null) {
4538 local_info
= Block
.GetLocalInfo (Name
);
4539 type
= local_info
.VariableType
;
4540 is_readonly
= local_info
.ReadOnly
;
4544 public override void SetHasAddressTaken ()
4546 local_info
.AddressTaken
= true;
4549 public override Expression
CreateExpressionTree (ResolveContext ec
)
4551 HoistedVariable hv
= GetHoistedVariable (ec
);
4553 return hv
.CreateExpressionTree ();
4555 Arguments arg
= new Arguments (1);
4556 arg
.Add (new Argument (this));
4557 return CreateExpressionFactoryCall (ec
, "Constant", arg
);
4560 Expression
DoResolveBase (ResolveContext ec
)
4562 Expression e
= Block
.GetConstantExpression (Name
);
4564 return e
.Resolve (ec
);
4566 VerifyAssigned (ec
);
4569 // If we are referencing a variable from the external block
4570 // flag it for capturing
4572 if (ec
.MustCaptureVariable (local_info
)) {
4573 if (local_info
.AddressTaken
)
4574 AnonymousMethodExpression
.Error_AddressOfCapturedVar (ec
, this, loc
);
4576 if (ec
.IsVariableCapturingRequired
) {
4577 AnonymousMethodStorey storey
= local_info
.Block
.Explicit
.CreateAnonymousMethodStorey (ec
);
4578 storey
.CaptureLocalVariable (ec
, local_info
);
4582 eclass
= ExprClass
.Variable
;
4583 type
= local_info
.VariableType
;
4587 protected override Expression
DoResolve (ResolveContext ec
)
4589 ResolveLocalInfo ();
4590 local_info
.Used
= true;
4592 if (type
== null && local_info
.Type
is VarExpr
) {
4593 local_info
.VariableType
= TypeManager
.object_type
;
4594 Error_VariableIsUsedBeforeItIsDeclared (ec
.Report
, Name
);
4598 return DoResolveBase (ec
);
4601 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
4603 ResolveLocalInfo ();
4606 if (right_side
== EmptyExpression
.OutAccess
.Instance
)
4607 local_info
.Used
= true;
4609 // Infer implicitly typed local variable
4611 VarExpr ve
= local_info
.Type
as VarExpr
;
4613 if (!ve
.InferType (ec
, right_side
))
4615 type
= local_info
.VariableType
= ve
.Type
;
4622 if (right_side
== EmptyExpression
.OutAccess
.Instance
) {
4623 code
= 1657; msg
= "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
4624 } else if (right_side
== EmptyExpression
.LValueMemberAccess
) {
4625 code
= 1654; msg
= "Cannot assign to members of `{0}' because it is a `{1}'";
4626 } else if (right_side
== EmptyExpression
.LValueMemberOutAccess
) {
4627 code
= 1655; msg
= "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
4628 } else if (right_side
== EmptyExpression
.UnaryAddress
) {
4629 code
= 459; msg
= "Cannot take the address of {1} `{0}'";
4631 code
= 1656; msg
= "Cannot assign to `{0}' because it is a `{1}'";
4633 ec
.Report
.Error (code
, loc
, msg
, Name
, local_info
.GetReadOnlyContext ());
4634 } else if (VariableInfo
!= null) {
4635 VariableInfo
.SetAssigned (ec
);
4638 return DoResolveBase (ec
);
4641 public override int GetHashCode ()
4643 return Name
.GetHashCode ();
4646 public override bool Equals (object obj
)
4648 LocalVariableReference lvr
= obj
as LocalVariableReference
;
4652 return Name
== lvr
.Name
&& Block
== lvr
.Block
;
4655 protected override ILocalVariable Variable
{
4656 get { return local_info; }
4659 public override string ToString ()
4661 return String
.Format ("{0} ({1}:{2})", GetType (), Name
, loc
);
4664 protected override void CloneTo (CloneContext clonectx
, Expression t
)
4666 LocalVariableReference target
= (LocalVariableReference
) t
;
4668 target
.Block
= clonectx
.LookupBlock (Block
);
4669 if (local_info
!= null)
4670 target
.local_info
= clonectx
.LookupVariable (local_info
);
4675 /// This represents a reference to a parameter in the intermediate
4678 public class ParameterReference
: VariableReference
{
4679 readonly ToplevelParameterInfo pi
;
4681 public ParameterReference (ToplevelParameterInfo pi
, Location loc
)
4687 public override bool IsRef
{
4688 get { return (pi.Parameter.ModFlags & Parameter.Modifier.ISBYREF) != 0; }
4691 bool HasOutModifier
{
4692 get { return pi.Parameter.ModFlags == Parameter.Modifier.OUT; }
4695 public override HoistedVariable
GetHoistedVariable (AnonymousExpression ae
)
4697 return pi
.Parameter
.HoistedVariant
;
4701 // A ref or out parameter is classified as a moveable variable, even
4702 // if the argument given for the parameter is a fixed variable
4704 public override bool IsFixed
{
4705 get { return !IsRef; }
4708 public override string Name
{
4709 get { return Parameter.Name; }
4712 public Parameter Parameter
{
4713 get { return pi.Parameter; }
4716 public override VariableInfo VariableInfo
{
4717 get { return pi.VariableInfo; }
4720 protected override ILocalVariable Variable
{
4721 get { return Parameter; }
4724 public bool IsAssigned (ResolveContext ec
, Location loc
)
4726 // HACK: Variables are not captured in probing mode
4727 if (ec
.IsInProbingMode
)
4730 if (!ec
.DoFlowAnalysis
|| !HasOutModifier
|| ec
.CurrentBranching
.IsAssigned (VariableInfo
))
4733 ec
.Report
.Error (269, loc
, "Use of unassigned out parameter `{0}'", Name
);
4737 public override void SetHasAddressTaken ()
4739 Parameter
.HasAddressTaken
= true;
4742 void SetAssigned (ResolveContext ec
)
4744 if (HasOutModifier
&& ec
.DoFlowAnalysis
)
4745 ec
.CurrentBranching
.SetAssigned (VariableInfo
);
4748 bool DoResolveBase (ResolveContext ec
)
4750 type
= pi
.ParameterType
;
4751 eclass
= ExprClass
.Variable
;
4753 AnonymousExpression am
= ec
.CurrentAnonymousMethod
;
4757 Block b
= ec
.CurrentBlock
;
4760 IParameterData
[] p
= b
.Toplevel
.Parameters
.FixedParameters
;
4761 for (int i
= 0; i
< p
.Length
; ++i
) {
4762 if (p
[i
] != Parameter
)
4766 // Don't capture local parameters
4768 if (b
== ec
.CurrentBlock
.Toplevel
&& !am
.IsIterator
)
4772 ec
.Report
.Error (1628, loc
,
4773 "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
4774 Name
, am
.ContainerType
);
4777 if (pi
.Parameter
.HasAddressTaken
)
4778 AnonymousMethodExpression
.Error_AddressOfCapturedVar (ec
, this, loc
);
4780 if (ec
.IsVariableCapturingRequired
&& !b
.Toplevel
.IsExpressionTree
) {
4781 AnonymousMethodStorey storey
= pi
.Block
.CreateAnonymousMethodStorey (ec
);
4782 storey
.CaptureParameter (ec
, this);
4794 public override int GetHashCode ()
4796 return Name
.GetHashCode ();
4799 public override bool Equals (object obj
)
4801 ParameterReference pr
= obj
as ParameterReference
;
4805 return Name
== pr
.Name
;
4808 public override void AddressOf (EmitContext ec
, AddressOp mode
)
4811 // ParameterReferences might already be a reference
4818 base.AddressOf (ec
, mode
);
4821 protected override void CloneTo (CloneContext clonectx
, Expression target
)
4826 public override Expression
CreateExpressionTree (ResolveContext ec
)
4828 HoistedVariable hv
= GetHoistedVariable (ec
);
4830 return hv
.CreateExpressionTree ();
4832 return Parameter
.ExpressionTreeVariableReference ();
4836 // Notice that for ref/out parameters, the type exposed is not the
4837 // same type exposed externally.
4840 // externally we expose "int&"
4841 // here we expose "int".
4843 // We record this in "is_ref". This means that the type system can treat
4844 // the type as it is expected, but when we generate the code, we generate
4845 // the alternate kind of code.
4847 protected override Expression
DoResolve (ResolveContext ec
)
4849 if (!DoResolveBase (ec
))
4852 // HACK: Variables are not captured in probing mode
4853 if (ec
.IsInProbingMode
)
4856 if (HasOutModifier
&& ec
.DoFlowAnalysis
&&
4857 (!ec
.OmitStructFlowAnalysis
|| !VariableInfo
.TypeInfo
.IsStruct
) && !IsAssigned (ec
, loc
))
4863 override public Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
4865 if (!DoResolveBase (ec
))
4868 // HACK: parameters are not captured when probing is on
4869 if (!ec
.IsInProbingMode
)
4875 static public void EmitLdArg (ILGenerator ig
, int x
)
4878 case 0: ig
.Emit (OpCodes
.Ldarg_0
); break;
4879 case 1: ig
.Emit (OpCodes
.Ldarg_1
); break;
4880 case 2: ig
.Emit (OpCodes
.Ldarg_2
); break;
4881 case 3: ig
.Emit (OpCodes
.Ldarg_3
); break;
4883 if (x
> byte.MaxValue
)
4884 ig
.Emit (OpCodes
.Ldarg
, x
);
4886 ig
.Emit (OpCodes
.Ldarg_S
, (byte) x
);
4893 /// Invocation of methods or delegates.
4895 public class Invocation
: ExpressionStatement
4897 protected Arguments arguments
;
4898 protected Expression expr
;
4899 protected MethodGroupExpr mg
;
4900 bool arguments_resolved
;
4903 // arguments is an ArrayList, but we do not want to typecast,
4904 // as it might be null.
4906 public Invocation (Expression expr
, Arguments arguments
)
4908 SimpleName sn
= expr
as SimpleName
;
4910 this.expr
= sn
.GetMethodGroup ();
4914 this.arguments
= arguments
;
4916 loc
= expr
.Location
;
4919 public Invocation (Expression expr
, Arguments arguments
, bool arguments_resolved
)
4920 : this (expr
, arguments
)
4922 this.arguments_resolved
= arguments_resolved
;
4925 public override Expression
CreateExpressionTree (ResolveContext ec
)
4927 Expression instance
= mg
.IsInstance
?
4928 mg
.InstanceExpression
.CreateExpressionTree (ec
) :
4929 new NullLiteral (loc
);
4931 var args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
4933 mg
.CreateExpressionTree (ec
));
4936 MemberExpr
.Error_BaseAccessInExpressionTree (ec
, loc
);
4938 return CreateExpressionFactoryCall (ec
, "Call", args
);
4941 protected override Expression
DoResolve (ResolveContext ec
)
4943 Expression member_expr
= expr
.Resolve (ec
, ResolveFlags
.VariableOrValue
| ResolveFlags
.MethodGroup
);
4944 if (member_expr
== null)
4948 // Next, evaluate all the expressions in the argument list
4950 bool dynamic_arg
= false;
4951 if (arguments
!= null && !arguments_resolved
)
4952 arguments
.Resolve (ec
, out dynamic_arg
);
4954 Type expr_type
= member_expr
.Type
;
4955 mg
= member_expr
as MethodGroupExpr
;
4957 bool dynamic_member
= TypeManager
.IsDynamicType (expr_type
);
4959 if (!dynamic_member
) {
4960 Expression invoke
= null;
4963 if (expr_type
!= null && TypeManager
.IsDelegateType (expr_type
)) {
4964 invoke
= new DelegateInvocation (member_expr
, arguments
, loc
);
4965 invoke
= invoke
.Resolve (ec
);
4966 if (invoke
== null || !dynamic_arg
)
4969 MemberExpr me
= member_expr
as MemberExpr
;
4971 member_expr
.Error_UnexpectedKind (ec
, ResolveFlags
.MethodGroup
, loc
);
4975 mg
= ec
.LookupExtensionMethod (me
.Type
, me
.Name
, loc
);
4977 ec
.Report
.Error (1955, loc
, "The member `{0}' cannot be used as method or delegate",
4978 member_expr
.GetSignatureForError ());
4982 ((ExtensionMethodGroupExpr
) mg
).ExtensionExpression
= me
.InstanceExpression
;
4986 if (invoke
== null) {
4987 mg
= DoResolveOverload (ec
);
4993 if (dynamic_arg
|| dynamic_member
)
4994 return DoResolveDynamic (ec
, member_expr
);
4996 MethodInfo method
= (MethodInfo
)mg
;
4997 if (method
!= null) {
4998 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
5000 // TODO: this is a copy of mg.ResolveMemberAccess method
5001 Expression iexpr
= mg
.InstanceExpression
;
5002 if (method
.IsStatic
) {
5003 if (iexpr
== null ||
5004 iexpr
is This
|| iexpr
is EmptyExpression
||
5005 mg
.IdenticalTypeName
) {
5006 mg
.InstanceExpression
= null;
5008 MemberExpr
.error176 (ec
, loc
, mg
.GetSignatureForError ());
5012 if (iexpr
== null || iexpr
== EmptyExpression
.Null
) {
5013 SimpleName
.Error_ObjectRefRequired (ec
, loc
, mg
.GetSignatureForError ());
5019 // Only base will allow this invocation to happen.
5021 if (mg
.IsBase
&& method
.IsAbstract
){
5022 Error_CannotCallAbstractBase (ec
, TypeManager
.CSharpSignature (method
));
5026 if (arguments
== null && method
.DeclaringType
== TypeManager
.object_type
&& method
.Name
== Destructor
.MetadataName
) {
5028 ec
.Report
.Error (250, loc
, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
5030 ec
.Report
.Error (245, loc
, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
5034 IsSpecialMethodInvocation (ec
, method
, loc
);
5036 if (mg
.InstanceExpression
!= null)
5037 mg
.InstanceExpression
.CheckMarshalByRefAccess (ec
);
5039 eclass
= ExprClass
.Value
;
5043 Expression
DoResolveDynamic (ResolveContext ec
, Expression memberExpr
)
5046 DynamicMemberBinder dmb
= memberExpr
as DynamicMemberBinder
;
5048 args
= dmb
.Arguments
;
5049 if (arguments
!= null)
5050 args
.AddRange (arguments
);
5051 } else if (mg
== null) {
5052 if (arguments
== null)
5053 args
= new Arguments (1);
5057 args
.Insert (0, new Argument (memberExpr
));
5061 ec
.Report
.Error (1971, loc
,
5062 "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
5069 if (mg
.IsStatic
!= mg
.IsInstance
) {
5071 args
= new Arguments (1);
5074 args
.Insert (0, new Argument (new TypeOf (new TypeExpression (mg
.DeclaringType
, loc
), loc
).Resolve (ec
), Argument
.AType
.DynamicTypeName
));
5076 MemberAccess ma
= expr
as MemberAccess
;
5078 args
.Insert (0, new Argument (ma
.Left
.Resolve (ec
)));
5080 args
.Insert (0, new Argument (new This (loc
).Resolve (ec
)));
5085 return new DynamicInvocation (expr
as ATypeNameExpression
, args
, loc
).Resolve (ec
);
5088 protected virtual MethodGroupExpr
DoResolveOverload (ResolveContext ec
)
5090 return mg
.OverloadResolve (ec
, ref arguments
, false, loc
);
5093 public static bool IsSpecialMethodInvocation (ResolveContext ec
, MethodBase method
, Location loc
)
5095 if (!TypeManager
.IsSpecialMethod (method
))
5098 if (ec
.HasSet (ResolveContext
.Options
.InvokeSpecialName
))
5101 ec
.Report
.SymbolRelatedToPreviousError (method
);
5102 ec
.Report
.Error (571, loc
, "`{0}': cannot explicitly call operator or accessor",
5103 TypeManager
.CSharpSignature (method
, true));
5108 static Type
[] GetVarargsTypes (MethodBase mb
, Arguments arguments
)
5110 AParametersCollection pd
= TypeManager
.GetParameterData (mb
);
5112 Argument a
= arguments
[pd
.Count
- 1];
5113 Arglist list
= (Arglist
) a
.Expr
;
5115 return list
.ArgumentTypes
;
5119 /// This checks the ConditionalAttribute on the method
5121 public static bool IsMethodExcluded (MethodBase method
, Location loc
)
5123 if (method
.IsConstructor
)
5126 method
= TypeManager
.DropGenericMethodArguments (method
);
5127 if (TypeManager
.IsBeingCompiled (method
)) {
5128 IMethodData md
= TypeManager
.GetMethod (method
);
5130 return md
.IsExcluded ();
5132 // For some methods (generated by delegate class) GetMethod returns null
5133 // because they are not included in builder_to_method table
5137 return AttributeTester
.IsConditionalMethodExcluded (method
, loc
);
5141 /// is_base tells whether we want to force the use of the `call'
5142 /// opcode instead of using callvirt. Call is required to call
5143 /// a specific method, while callvirt will always use the most
5144 /// recent method in the vtable.
5146 /// is_static tells whether this is an invocation on a static method
5148 /// instance_expr is an expression that represents the instance
5149 /// it must be non-null if is_static is false.
5151 /// method is the method to invoke.
5153 /// Arguments is the list of arguments to pass to the method or constructor.
5155 public static void EmitCall (EmitContext ec
, bool is_base
,
5156 Expression instance_expr
,
5157 MethodBase method
, Arguments Arguments
, Location loc
)
5159 EmitCall (ec
, is_base
, instance_expr
, method
, Arguments
, loc
, false, false);
5162 // `dup_args' leaves an extra copy of the arguments on the stack
5163 // `omit_args' does not leave any arguments at all.
5164 // So, basically, you could make one call with `dup_args' set to true,
5165 // and then another with `omit_args' set to true, and the two calls
5166 // would have the same set of arguments. However, each argument would
5167 // only have been evaluated once.
5168 public static void EmitCall (EmitContext ec
, bool is_base
,
5169 Expression instance_expr
,
5170 MethodBase method
, Arguments Arguments
, Location loc
,
5171 bool dup_args
, bool omit_args
)
5173 ILGenerator ig
= ec
.ig
;
5174 bool struct_call
= false;
5175 bool this_call
= false;
5176 LocalTemporary this_arg
= null;
5178 Type decl_type
= method
.DeclaringType
;
5180 if (IsMethodExcluded (method
, loc
))
5183 bool is_static
= method
.IsStatic
;
5185 this_call
= instance_expr
is This
;
5186 if (TypeManager
.IsStruct (decl_type
) || TypeManager
.IsEnumType (decl_type
))
5190 // If this is ourselves, push "this"
5194 Type iexpr_type
= instance_expr
.Type
;
5197 // Push the instance expression
5199 if (TypeManager
.IsValueType (iexpr_type
) || TypeManager
.IsGenericParameter (iexpr_type
)) {
5201 // Special case: calls to a function declared in a
5202 // reference-type with a value-type argument need
5203 // to have their value boxed.
5204 if (TypeManager
.IsStruct (decl_type
) ||
5205 TypeManager
.IsGenericParameter (iexpr_type
)) {
5207 // If the expression implements IMemoryLocation, then
5208 // we can optimize and use AddressOf on the
5211 // If not we have to use some temporary storage for
5213 if (instance_expr
is IMemoryLocation
) {
5214 ((IMemoryLocation
)instance_expr
).
5215 AddressOf (ec
, AddressOp
.LoadStore
);
5217 LocalTemporary temp
= new LocalTemporary (iexpr_type
);
5218 instance_expr
.Emit (ec
);
5220 temp
.AddressOf (ec
, AddressOp
.Load
);
5223 // avoid the overhead of doing this all the time.
5225 t
= TypeManager
.GetReferenceType (iexpr_type
);
5227 instance_expr
.Emit (ec
);
5229 // FIXME: should use instance_expr is IMemoryLocation + constraint.
5230 // to help JIT to produce better code
5231 ig
.Emit (OpCodes
.Box
, instance_expr
.Type
);
5232 t
= TypeManager
.object_type
;
5235 instance_expr
.Emit (ec
);
5236 t
= instance_expr
.Type
;
5240 ig
.Emit (OpCodes
.Dup
);
5241 if (Arguments
!= null && Arguments
.Count
!= 0) {
5242 this_arg
= new LocalTemporary (t
);
5243 this_arg
.Store (ec
);
5249 if (!omit_args
&& Arguments
!= null)
5250 Arguments
.Emit (ec
, dup_args
, this_arg
);
5253 if (is_static
|| struct_call
|| is_base
|| (this_call
&& !method
.IsVirtual
)) {
5254 call_op
= OpCodes
.Call
;
5256 call_op
= OpCodes
.Callvirt
;
5258 if ((instance_expr
!= null) && (instance_expr
.Type
.IsGenericParameter
))
5259 ig
.Emit (OpCodes
.Constrained
, instance_expr
.Type
);
5262 if ((method
.CallingConvention
& CallingConventions
.VarArgs
) != 0) {
5263 Type
[] varargs_types
= GetVarargsTypes (method
, Arguments
);
5264 ig
.EmitCall (call_op
, (MethodInfo
) method
, varargs_types
);
5271 // and DoFoo is not virtual, you can omit the callvirt,
5272 // because you don't need the null checking behavior.
5274 if (method
is MethodInfo
)
5275 ig
.Emit (call_op
, (MethodInfo
) method
);
5277 ig
.Emit (call_op
, (ConstructorInfo
) method
);
5280 public override void Emit (EmitContext ec
)
5282 mg
.EmitCall (ec
, arguments
);
5285 public override void EmitStatement (EmitContext ec
)
5290 // Pop the return value if there is one
5292 if (TypeManager
.TypeToCoreType (type
) != TypeManager
.void_type
)
5293 ec
.ig
.Emit (OpCodes
.Pop
);
5296 protected override void CloneTo (CloneContext clonectx
, Expression t
)
5298 Invocation target
= (Invocation
) t
;
5300 if (arguments
!= null)
5301 target
.arguments
= arguments
.Clone (clonectx
);
5303 target
.expr
= expr
.Clone (clonectx
);
5306 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
5308 return MakeExpression (ctx
, mg
.InstanceExpression
, (MethodInfo
) mg
, arguments
);
5311 public static SLE
.Expression
MakeExpression (BuilderContext ctx
, Expression instance
, MethodInfo mi
, Arguments args
)
5313 var instance_expr
= instance
== null ? null : instance
.MakeExpression (ctx
);
5314 return SLE
.Expression
.Call (instance_expr
, mi
, Arguments
.MakeExpression (args
, ctx
));
5317 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
5319 mg
.MutateHoistedGenericType (storey
);
5320 type
= storey
.MutateType (type
);
5321 if (arguments
!= null) {
5322 arguments
.MutateHoistedGenericType (storey
);
5328 /// Implements the new expression
5330 public class New
: ExpressionStatement
, IMemoryLocation
{
5331 protected Arguments Arguments
;
5334 // During bootstrap, it contains the RequestedType,
5335 // but if `type' is not null, it *might* contain a NewDelegate
5336 // (because of field multi-initialization)
5338 protected Expression RequestedType
;
5340 protected MethodGroupExpr method
;
5342 bool is_type_parameter
;
5344 public New (Expression requested_type
, Arguments arguments
, Location l
)
5346 RequestedType
= requested_type
;
5347 Arguments
= arguments
;
5352 /// Converts complex core type syntax like 'new int ()' to simple constant
5354 public static Constant
Constantify (Type t
)
5356 if (t
== TypeManager
.int32_type
)
5357 return new IntConstant (0, Location
.Null
);
5358 if (t
== TypeManager
.uint32_type
)
5359 return new UIntConstant (0, Location
.Null
);
5360 if (t
== TypeManager
.int64_type
)
5361 return new LongConstant (0, Location
.Null
);
5362 if (t
== TypeManager
.uint64_type
)
5363 return new ULongConstant (0, Location
.Null
);
5364 if (t
== TypeManager
.float_type
)
5365 return new FloatConstant (0, Location
.Null
);
5366 if (t
== TypeManager
.double_type
)
5367 return new DoubleConstant (0, Location
.Null
);
5368 if (t
== TypeManager
.short_type
)
5369 return new ShortConstant (0, Location
.Null
);
5370 if (t
== TypeManager
.ushort_type
)
5371 return new UShortConstant (0, Location
.Null
);
5372 if (t
== TypeManager
.sbyte_type
)
5373 return new SByteConstant (0, Location
.Null
);
5374 if (t
== TypeManager
.byte_type
)
5375 return new ByteConstant (0, Location
.Null
);
5376 if (t
== TypeManager
.char_type
)
5377 return new CharConstant ('\0', Location
.Null
);
5378 if (t
== TypeManager
.bool_type
)
5379 return new BoolConstant (false, Location
.Null
);
5380 if (t
== TypeManager
.decimal_type
)
5381 return new DecimalConstant (0, Location
.Null
);
5382 if (TypeManager
.IsEnumType (t
))
5383 return new EnumConstant (Constantify (TypeManager
.GetEnumUnderlyingType (t
)), t
);
5384 if (TypeManager
.IsNullableType (t
))
5385 return Nullable
.LiftedNull
.Create (t
, Location
.Null
);
5391 // Checks whether the type is an interface that has the
5392 // [ComImport, CoClass] attributes and must be treated
5395 public Expression
CheckComImport (ResolveContext ec
)
5397 if (!type
.IsInterface
)
5401 // Turn the call into:
5402 // (the-interface-stated) (new class-referenced-in-coclassattribute ())
5404 Type real_class
= AttributeTester
.GetCoClassAttribute (type
);
5405 if (real_class
== null)
5408 New proxy
= new New (new TypeExpression (real_class
, loc
), Arguments
, loc
);
5409 Cast cast
= new Cast (new TypeExpression (type
, loc
), proxy
, loc
);
5410 return cast
.Resolve (ec
);
5413 public override Expression
CreateExpressionTree (ResolveContext ec
)
5416 if (method
== null) {
5417 args
= new Arguments (1);
5418 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
5420 args
= Arguments
.CreateForExpressionTree (ec
,
5422 method
.CreateExpressionTree (ec
));
5425 return CreateExpressionFactoryCall (ec
, "New", args
);
5428 protected override Expression
DoResolve (ResolveContext ec
)
5431 // The New DoResolve might be called twice when initializing field
5432 // expressions (see EmitFieldInitializers, the call to
5433 // GetInitializerExpression will perform a resolve on the expression,
5434 // and later the assign will trigger another resolution
5436 // This leads to bugs (#37014)
5439 if (RequestedType
is NewDelegate
)
5440 return RequestedType
;
5444 TypeExpr texpr
= RequestedType
.ResolveAsTypeTerminal (ec
, false);
5450 if (type
.IsPointer
) {
5451 ec
.Report
.Error (1919, loc
, "Unsafe type `{0}' cannot be used in an object creation expression",
5452 TypeManager
.CSharpName (type
));
5456 if (Arguments
== null) {
5457 Constant c
= Constantify (type
);
5459 return ReducedExpression
.Create (c
.Resolve (ec
), this);
5462 if (TypeManager
.IsDelegateType (type
)) {
5463 return (new NewDelegate (type
, Arguments
, loc
)).Resolve (ec
);
5466 if (TypeManager
.IsGenericParameter (type
)) {
5467 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (type
);
5469 if ((gc
== null) || (!gc
.HasConstructorConstraint
&& !gc
.IsValueType
)) {
5470 ec
.Report
.Error (304, loc
,
5471 "Cannot create an instance of the variable type '{0}' because it doesn't have the new() constraint",
5472 TypeManager
.CSharpName (type
));
5476 if ((Arguments
!= null) && (Arguments
.Count
!= 0)) {
5477 ec
.Report
.Error (417, loc
,
5478 "`{0}': cannot provide arguments when creating an instance of a variable type",
5479 TypeManager
.CSharpName (type
));
5483 if (TypeManager
.activator_create_instance
== null) {
5484 Type activator_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System", "Activator", Kind
.Class
, true);
5485 if (activator_type
!= null) {
5486 TypeManager
.activator_create_instance
= TypeManager
.GetPredefinedMethod (
5487 activator_type
, "CreateInstance", loc
, Type
.EmptyTypes
);
5491 is_type_parameter
= true;
5492 eclass
= ExprClass
.Value
;
5496 if (type
.IsAbstract
&& type
.IsSealed
) {
5497 ec
.Report
.SymbolRelatedToPreviousError (type
);
5498 ec
.Report
.Error (712, loc
, "Cannot create an instance of the static class `{0}'", TypeManager
.CSharpName (type
));
5502 if (type
.IsInterface
|| type
.IsAbstract
){
5503 if (!TypeManager
.IsGenericType (type
)) {
5504 RequestedType
= CheckComImport (ec
);
5505 if (RequestedType
!= null)
5506 return RequestedType
;
5509 ec
.Report
.SymbolRelatedToPreviousError (type
);
5510 ec
.Report
.Error (144, loc
, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager
.CSharpName (type
));
5514 bool is_struct
= TypeManager
.IsStruct (type
);
5515 eclass
= ExprClass
.Value
;
5518 // SRE returns a match for .ctor () on structs (the object constructor),
5519 // so we have to manually ignore it.
5521 if (is_struct
&& Arguments
== null)
5524 // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
5525 Expression ml
= MemberLookupFinal (ec
, type
, type
, ConstructorInfo
.ConstructorName
,
5526 MemberTypes
.Constructor
, AllBindingFlags
| BindingFlags
.DeclaredOnly
, loc
);
5529 if (Arguments
!= null) {
5530 Arguments
.Resolve (ec
, out dynamic);
5538 method
= ml
as MethodGroupExpr
;
5539 if (method
== null) {
5540 ml
.Error_UnexpectedKind (ec
, ResolveFlags
.MethodGroup
, loc
);
5544 method
= method
.OverloadResolve (ec
, ref Arguments
, false, loc
);
5549 Arguments
.Insert (0, new Argument (new TypeOf (texpr
, loc
).Resolve (ec
), Argument
.AType
.DynamicTypeName
));
5550 return new DynamicConstructorBinder (type
, Arguments
, loc
).Resolve (ec
);
5556 bool DoEmitTypeParameter (EmitContext ec
)
5558 ILGenerator ig
= ec
.ig
;
5560 MethodInfo ci
= TypeManager
.activator_create_instance
.MakeGenericMethod (
5561 new Type
[] { type }
);
5563 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (type
);
5564 if (gc
.HasReferenceTypeConstraint
|| gc
.HasClassConstraint
) {
5565 ig
.Emit (OpCodes
.Call
, ci
);
5569 // Allow DoEmit() to be called multiple times.
5570 // We need to create a new LocalTemporary each time since
5571 // you can't share LocalBuilders among ILGeneators.
5572 LocalTemporary temp
= new LocalTemporary (type
);
5574 Label label_activator
= ig
.DefineLabel ();
5575 Label label_end
= ig
.DefineLabel ();
5577 temp
.AddressOf (ec
, AddressOp
.Store
);
5578 ig
.Emit (OpCodes
.Initobj
, type
);
5581 ig
.Emit (OpCodes
.Box
, type
);
5582 ig
.Emit (OpCodes
.Brfalse
, label_activator
);
5584 temp
.AddressOf (ec
, AddressOp
.Store
);
5585 ig
.Emit (OpCodes
.Initobj
, type
);
5587 ig
.Emit (OpCodes
.Br_S
, label_end
);
5589 ig
.MarkLabel (label_activator
);
5591 ig
.Emit (OpCodes
.Call
, ci
);
5592 ig
.MarkLabel (label_end
);
5597 // This Emit can be invoked in two contexts:
5598 // * As a mechanism that will leave a value on the stack (new object)
5599 // * As one that wont (init struct)
5601 // If we are dealing with a ValueType, we have a few
5602 // situations to deal with:
5604 // * The target is a ValueType, and we have been provided
5605 // the instance (this is easy, we are being assigned).
5607 // * The target of New is being passed as an argument,
5608 // to a boxing operation or a function that takes a
5611 // In this case, we need to create a temporary variable
5612 // that is the argument of New.
5614 // Returns whether a value is left on the stack
5616 // *** Implementation note ***
5618 // To benefit from this optimization, each assignable expression
5619 // has to manually cast to New and call this Emit.
5621 // TODO: It's worth to implement it for arrays and fields
5623 public virtual bool Emit (EmitContext ec
, IMemoryLocation target
)
5625 bool is_value_type
= TypeManager
.IsValueType (type
);
5626 ILGenerator ig
= ec
.ig
;
5627 VariableReference vr
= target
as VariableReference
;
5629 if (target
!= null && is_value_type
&& (vr
!= null || method
== null)) {
5630 target
.AddressOf (ec
, AddressOp
.Store
);
5631 } else if (vr
!= null && vr
.IsRef
) {
5635 if (Arguments
!= null)
5636 Arguments
.Emit (ec
);
5638 if (is_value_type
) {
5639 if (method
== null) {
5640 ig
.Emit (OpCodes
.Initobj
, type
);
5645 ig
.Emit (OpCodes
.Call
, (ConstructorInfo
) method
);
5650 if (is_type_parameter
)
5651 return DoEmitTypeParameter (ec
);
5653 ConstructorInfo ci
= (ConstructorInfo
) method
;
5655 if (TypeManager
.IsGenericType (type
) && type
.IsGenericTypeDefinition
)
5656 ci
= TypeBuilder
.GetConstructor (type
, ci
);
5659 ig
.Emit (OpCodes
.Newobj
, ci
);
5663 public override void Emit (EmitContext ec
)
5665 LocalTemporary v
= null;
5666 if (method
== null && TypeManager
.IsValueType (type
)) {
5667 // TODO: Use temporary variable from pool
5668 v
= new LocalTemporary (type
);
5675 public override void EmitStatement (EmitContext ec
)
5677 LocalTemporary v
= null;
5678 if (method
== null && TypeManager
.IsValueType (type
)) {
5679 // TODO: Use temporary variable from pool
5680 v
= new LocalTemporary (type
);
5684 ec
.ig
.Emit (OpCodes
.Pop
);
5687 public virtual bool HasInitializer
{
5693 public void AddressOf (EmitContext ec
, AddressOp mode
)
5695 EmitAddressOf (ec
, mode
);
5698 protected virtual IMemoryLocation
EmitAddressOf (EmitContext ec
, AddressOp mode
)
5700 LocalTemporary value_target
= new LocalTemporary (type
);
5702 if (is_type_parameter
) {
5703 DoEmitTypeParameter (ec
);
5704 value_target
.Store (ec
);
5705 value_target
.AddressOf (ec
, mode
);
5706 return value_target
;
5709 if (!TypeManager
.IsStruct (type
)){
5711 // We throw an exception. So far, I believe we only need to support
5713 // foreach (int j in new StructType ())
5716 throw new Exception ("AddressOf should not be used for classes");
5719 value_target
.AddressOf (ec
, AddressOp
.Store
);
5721 if (method
== null) {
5722 ec
.ig
.Emit (OpCodes
.Initobj
, type
);
5724 if (Arguments
!= null)
5725 Arguments
.Emit (ec
);
5727 ec
.ig
.Emit (OpCodes
.Call
, (ConstructorInfo
) method
);
5730 value_target
.AddressOf (ec
, mode
);
5731 return value_target
;
5734 protected override void CloneTo (CloneContext clonectx
, Expression t
)
5736 New target
= (New
) t
;
5738 target
.RequestedType
= RequestedType
.Clone (clonectx
);
5739 if (Arguments
!= null){
5740 target
.Arguments
= Arguments
.Clone (clonectx
);
5744 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
5746 return SLE
.Expression
.New ((ConstructorInfo
) method
, Arguments
.MakeExpression (Arguments
, ctx
));
5749 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
5751 if (method
!= null) {
5752 method
.MutateHoistedGenericType (storey
);
5753 if (Arguments
!= null) {
5754 Arguments
.MutateHoistedGenericType (storey
);
5758 type
= storey
.MutateType (type
);
5762 public class ArrayInitializer
: ShimExpression
5764 List
<Expression
> elements
;
5766 public ArrayInitializer (List
<Expression
> init
, Location loc
)
5772 public ArrayInitializer (int count
, Location loc
)
5775 elements
= new List
<Expression
> (count
);
5778 public ArrayInitializer (Location loc
)
5783 public void Add (Expression expr
)
5785 elements
.Add (expr
);
5788 protected override void CloneTo (CloneContext clonectx
, Expression t
)
5790 var target
= (ArrayInitializer
) t
;
5792 target
.elements
= new List
<Expression
> (elements
.Count
);
5793 foreach (var element
in elements
)
5794 target
.elements
.Add (element
.Clone (clonectx
));
5796 base.CloneTo (clonectx
, t
);
5800 get { return elements.Count; }
5803 protected override Expression
DoResolve (ResolveContext rc
)
5805 throw new NotImplementedException ();
5808 public Expression
this [int index
] {
5809 get { return elements [index]; }
5814 /// 14.5.10.2: Represents an array creation expression.
5818 /// There are two possible scenarios here: one is an array creation
5819 /// expression that specifies the dimensions and optionally the
5820 /// initialization data and the other which does not need dimensions
5821 /// specified but where initialization data is mandatory.
5823 class ArrayCreation
: Expression
5825 FullNamedExpression requested_base_type
;
5826 ArrayInitializer initializers
;
5829 // The list of Argument types.
5830 // This is used to construct the `newarray' or constructor signature
5832 protected List
<Expression
> arguments
;
5834 protected Type array_element_type
;
5835 bool expect_initializers
= false;
5836 int num_arguments
= 0;
5837 protected int dimensions
;
5838 protected readonly string rank
;
5839 Expression first_emit
;
5840 LocalTemporary first_emit_temp
;
5842 protected List
<Expression
> array_data
;
5844 Dictionary
<int, int> bounds
;
5846 // The number of constants in array initializers
5847 int const_initializers_count
;
5848 bool only_constant_initializers
;
5850 public ArrayCreation (FullNamedExpression requested_base_type
, List
<Expression
> exprs
, string rank
, ArrayInitializer initializers
, Location l
)
5852 this.requested_base_type
= requested_base_type
;
5853 this.initializers
= initializers
;
5857 arguments
= new List
<Expression
> (exprs
);
5858 num_arguments
= arguments
.Count
;
5861 public ArrayCreation (FullNamedExpression requested_base_type
, string rank
, ArrayInitializer initializers
, Location l
)
5863 this.requested_base_type
= requested_base_type
;
5864 this.initializers
= initializers
;
5868 //this.rank = rank.Substring (0, rank.LastIndexOf ('['));
5870 //string tmp = rank.Substring (rank.LastIndexOf ('['));
5872 //dimensions = tmp.Length - 1;
5873 expect_initializers
= true;
5876 protected override void Error_NegativeArrayIndex (ResolveContext ec
, Location loc
)
5878 ec
.Report
.Error (248, loc
, "Cannot create an array with a negative size");
5881 bool CheckIndices (ResolveContext ec
, ArrayInitializer probe
, int idx
, bool specified_dims
, int child_bounds
)
5883 if (specified_dims
) {
5884 Expression a
= arguments
[idx
];
5889 Constant c
= a
as Constant
;
5891 c
= c
.ImplicitConversionRequired (ec
, TypeManager
.int32_type
, a
.Location
);
5895 ec
.Report
.Error (150, a
.Location
, "A constant value is expected");
5899 int value = (int) c
.GetValue ();
5901 if (value != probe
.Count
) {
5902 ec
.Report
.Error (847, loc
, "An array initializer of length `{0}' was expected", value);
5906 bounds
[idx
] = value;
5909 only_constant_initializers
= true;
5910 for (int i
= 0; i
< probe
.Count
; ++i
) {
5912 if (o
is ArrayInitializer
) {
5913 var sub_probe
= o
as ArrayInitializer
;
5914 if (idx
+ 1 >= dimensions
){
5915 ec
.Report
.Error (623, loc
, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
5919 bool ret
= CheckIndices (ec
, sub_probe
, idx
+ 1, specified_dims
, child_bounds
- 1);
5922 } else if (child_bounds
> 1) {
5923 ec
.Report
.Error (846, o
.Location
, "A nested array initializer was expected");
5925 Expression element
= ResolveArrayElement (ec
, o
);
5926 if (element
== null)
5929 // Initializers with the default values can be ignored
5930 Constant c
= element
as Constant
;
5932 if (c
.IsDefaultInitializer (array_element_type
)) {
5936 ++const_initializers_count
;
5939 only_constant_initializers
= false;
5942 array_data
.Add (element
);
5949 public override Expression
CreateExpressionTree (ResolveContext ec
)
5953 if (array_data
== null) {
5954 args
= new Arguments (arguments
.Count
+ 1);
5955 args
.Add (new Argument (new TypeOf (new TypeExpression (array_element_type
, loc
), loc
)));
5956 foreach (Expression a
in arguments
)
5957 args
.Add (new Argument (a
.CreateExpressionTree (ec
)));
5959 return CreateExpressionFactoryCall (ec
, "NewArrayBounds", args
);
5962 if (dimensions
> 1) {
5963 ec
.Report
.Error (838, loc
, "An expression tree cannot contain a multidimensional array initializer");
5967 args
= new Arguments (array_data
== null ? 1 : array_data
.Count
+ 1);
5968 args
.Add (new Argument (new TypeOf (new TypeExpression (array_element_type
, loc
), loc
)));
5969 if (array_data
!= null) {
5970 for (int i
= 0; i
< array_data
.Count
; ++i
) {
5971 Expression e
= array_data
[i
];
5973 e
= Convert
.ImplicitConversion (ec
, initializers
[i
], array_element_type
, loc
);
5975 args
.Add (new Argument (e
.CreateExpressionTree (ec
)));
5979 return CreateExpressionFactoryCall (ec
, "NewArrayInit", args
);
5982 public void UpdateIndices ()
5985 for (var probe
= initializers
; probe
!= null;) {
5986 if (probe
.Count
> 0 && probe
[0] is ArrayInitializer
) {
5987 Expression e
= new IntConstant (probe
.Count
, Location
.Null
);
5990 bounds
[i
++] = probe
.Count
;
5992 probe
= (ArrayInitializer
) probe
[0];
5995 Expression e
= new IntConstant (probe
.Count
, Location
.Null
);
5998 bounds
[i
++] = probe
.Count
;
6004 protected virtual Expression
ResolveArrayElement (ResolveContext ec
, Expression element
)
6006 element
= element
.Resolve (ec
);
6007 if (element
== null)
6010 if (element
is CompoundAssign
.TargetExpression
) {
6011 if (first_emit
!= null)
6012 throw new InternalErrorException ("Can only handle one mutator at a time");
6013 first_emit
= element
;
6014 element
= first_emit_temp
= new LocalTemporary (element
.Type
);
6017 return Convert
.ImplicitConversionRequired (
6018 ec
, element
, array_element_type
, loc
);
6021 protected bool ResolveInitializers (ResolveContext ec
)
6023 if (initializers
== null) {
6024 return !expect_initializers
;
6028 // We use this to store all the date values in the order in which we
6029 // will need to store them in the byte blob later
6031 array_data
= new List
<Expression
> ();
6032 bounds
= new Dictionary
<int, int> ();
6034 if (arguments
!= null)
6035 return CheckIndices (ec
, initializers
, 0, true, dimensions
);
6037 arguments
= new List
<Expression
> ();
6039 if (!CheckIndices (ec
, initializers
, 0, false, dimensions
))
6048 // Resolved the type of the array
6050 bool ResolveArrayType (ResolveContext ec
)
6052 if (requested_base_type
is VarExpr
) {
6053 ec
.Report
.Error (820, loc
, "An implicitly typed local variable declarator cannot use an array initializer");
6057 StringBuilder array_qualifier
= new StringBuilder (rank
);
6060 // `In the first form allocates an array instace of the type that results
6061 // from deleting each of the individual expression from the expression list'
6063 if (num_arguments
> 0) {
6064 array_qualifier
.Append ("[");
6065 for (int i
= num_arguments
-1; i
> 0; i
--)
6066 array_qualifier
.Append (",");
6067 array_qualifier
.Append ("]");
6073 TypeExpr array_type_expr
;
6074 array_type_expr
= new ComposedCast (requested_base_type
, array_qualifier
.ToString (), loc
);
6075 array_type_expr
= array_type_expr
.ResolveAsTypeTerminal (ec
, false);
6076 if (array_type_expr
== null)
6079 type
= array_type_expr
.Type
;
6080 if (!type
.IsArray
) {
6081 ec
.Report
.Error (622, loc
, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
6085 array_element_type
= TypeManager
.GetElementType (type
);
6086 dimensions
= type
.GetArrayRank ();
6091 protected override Expression
DoResolve (ResolveContext ec
)
6096 if (!ResolveArrayType (ec
))
6100 // First step is to validate the initializers and fill
6101 // in any missing bits
6103 if (!ResolveInitializers (ec
))
6106 for (int i
= 0; i
< arguments
.Count
; ++i
) {
6107 Expression e
= arguments
[i
].Resolve (ec
);
6111 arguments
[i
] = ConvertExpressionToArrayIndex (ec
, e
);
6114 eclass
= ExprClass
.Value
;
6118 MethodInfo
GetArrayMethod (EmitContext ec
, int arguments
)
6120 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
6122 Type
[] arg_types
= new Type
[arguments
];
6123 for (int i
= 0; i
< arguments
; i
++)
6124 arg_types
[i
] = TypeManager
.int32_type
;
6126 MethodInfo mi
= mb
.GetArrayMethod (type
, ".ctor", CallingConventions
.HasThis
, null,
6130 ec
.Report
.Error (-6, "New invocation: Can not find a constructor for " +
6131 "this argument list");
6138 byte [] MakeByteBlob ()
6143 int count
= array_data
.Count
;
6145 Type element_type
= array_element_type
;
6146 if (TypeManager
.IsEnumType (element_type
))
6147 element_type
= TypeManager
.GetEnumUnderlyingType (element_type
);
6149 factor
= GetTypeSize (element_type
);
6151 throw new Exception ("unrecognized type in MakeByteBlob: " + element_type
);
6153 data
= new byte [(count
* factor
+ 3) & ~
3];
6156 for (int i
= 0; i
< count
; ++i
) {
6157 object v
= array_data
[i
];
6159 if (v
is EnumConstant
)
6160 v
= ((EnumConstant
) v
).Child
;
6162 if (v
is Constant
&& !(v
is StringConstant
))
6163 v
= ((Constant
) v
).GetValue ();
6169 if (element_type
== TypeManager
.int64_type
){
6170 if (!(v
is Expression
)){
6171 long val
= (long) v
;
6173 for (int j
= 0; j
< factor
; ++j
) {
6174 data
[idx
+ j
] = (byte) (val
& 0xFF);
6178 } else if (element_type
== TypeManager
.uint64_type
){
6179 if (!(v
is Expression
)){
6180 ulong val
= (ulong) v
;
6182 for (int j
= 0; j
< factor
; ++j
) {
6183 data
[idx
+ j
] = (byte) (val
& 0xFF);
6187 } else if (element_type
== TypeManager
.float_type
) {
6188 if (!(v
is Expression
)){
6189 element
= BitConverter
.GetBytes ((float) v
);
6191 for (int j
= 0; j
< factor
; ++j
)
6192 data
[idx
+ j
] = element
[j
];
6193 if (!BitConverter
.IsLittleEndian
)
6194 System
.Array
.Reverse (data
, idx
, 4);
6196 } else if (element_type
== TypeManager
.double_type
) {
6197 if (!(v
is Expression
)){
6198 element
= BitConverter
.GetBytes ((double) v
);
6200 for (int j
= 0; j
< factor
; ++j
)
6201 data
[idx
+ j
] = element
[j
];
6203 // FIXME: Handle the ARM float format.
6204 if (!BitConverter
.IsLittleEndian
)
6205 System
.Array
.Reverse (data
, idx
, 8);
6207 } else if (element_type
== TypeManager
.char_type
){
6208 if (!(v
is Expression
)){
6209 int val
= (int) ((char) v
);
6211 data
[idx
] = (byte) (val
& 0xff);
6212 data
[idx
+1] = (byte) (val
>> 8);
6214 } else if (element_type
== TypeManager
.short_type
){
6215 if (!(v
is Expression
)){
6216 int val
= (int) ((short) v
);
6218 data
[idx
] = (byte) (val
& 0xff);
6219 data
[idx
+1] = (byte) (val
>> 8);
6221 } else if (element_type
== TypeManager
.ushort_type
){
6222 if (!(v
is Expression
)){
6223 int val
= (int) ((ushort) v
);
6225 data
[idx
] = (byte) (val
& 0xff);
6226 data
[idx
+1] = (byte) (val
>> 8);
6228 } else if (element_type
== TypeManager
.int32_type
) {
6229 if (!(v
is Expression
)){
6232 data
[idx
] = (byte) (val
& 0xff);
6233 data
[idx
+1] = (byte) ((val
>> 8) & 0xff);
6234 data
[idx
+2] = (byte) ((val
>> 16) & 0xff);
6235 data
[idx
+3] = (byte) (val
>> 24);
6237 } else if (element_type
== TypeManager
.uint32_type
) {
6238 if (!(v
is Expression
)){
6239 uint val
= (uint) v
;
6241 data
[idx
] = (byte) (val
& 0xff);
6242 data
[idx
+1] = (byte) ((val
>> 8) & 0xff);
6243 data
[idx
+2] = (byte) ((val
>> 16) & 0xff);
6244 data
[idx
+3] = (byte) (val
>> 24);
6246 } else if (element_type
== TypeManager
.sbyte_type
) {
6247 if (!(v
is Expression
)){
6248 sbyte val
= (sbyte) v
;
6249 data
[idx
] = (byte) val
;
6251 } else if (element_type
== TypeManager
.byte_type
) {
6252 if (!(v
is Expression
)){
6253 byte val
= (byte) v
;
6254 data
[idx
] = (byte) val
;
6256 } else if (element_type
== TypeManager
.bool_type
) {
6257 if (!(v
is Expression
)){
6258 bool val
= (bool) v
;
6259 data
[idx
] = (byte) (val
? 1 : 0);
6261 } else if (element_type
== TypeManager
.decimal_type
){
6262 if (!(v
is Expression
)){
6263 int [] bits
= Decimal
.GetBits ((decimal) v
);
6266 // FIXME: For some reason, this doesn't work on the MS runtime.
6267 int [] nbits
= new int [4];
6268 nbits
[0] = bits
[3];
6269 nbits
[1] = bits
[2];
6270 nbits
[2] = bits
[0];
6271 nbits
[3] = bits
[1];
6273 for (int j
= 0; j
< 4; j
++){
6274 data
[p
++] = (byte) (nbits
[j
] & 0xff);
6275 data
[p
++] = (byte) ((nbits
[j
] >> 8) & 0xff);
6276 data
[p
++] = (byte) ((nbits
[j
] >> 16) & 0xff);
6277 data
[p
++] = (byte) (nbits
[j
] >> 24);
6281 throw new Exception ("Unrecognized type in MakeByteBlob: " + element_type
);
6291 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
6293 var initializers
= new SLE
.Expression
[array_data
.Count
];
6294 for (var i
= 0; i
< initializers
.Length
; i
++) {
6295 if (array_data
[i
] == null)
6296 initializers
[i
] = SLE
.Expression
.Default (array_element_type
);
6298 initializers
[i
] = array_data
[i
].MakeExpression (ctx
);
6301 return SLE
.Expression
.NewArrayInit (array_element_type
, initializers
);
6305 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
6307 array_element_type
= storey
.MutateType (array_element_type
);
6308 type
= storey
.MutateType (type
);
6309 if (arguments
!= null) {
6310 foreach (Expression e
in arguments
)
6311 e
.MutateHoistedGenericType (storey
);
6314 if (array_data
!= null) {
6315 foreach (Expression e
in array_data
) {
6316 // Don't mutate values optimized away
6320 e
.MutateHoistedGenericType (storey
);
6326 // Emits the initializers for the array
6328 void EmitStaticInitializers (EmitContext ec
)
6330 // FIXME: This should go to Resolve !
6331 if (TypeManager
.void_initializearray_array_fieldhandle
== null) {
6332 TypeManager
.void_initializearray_array_fieldhandle
= TypeManager
.GetPredefinedMethod (
6333 TypeManager
.runtime_helpers_type
, "InitializeArray", loc
,
6334 TypeManager
.array_type
, TypeManager
.runtime_field_handle_type
);
6335 if (TypeManager
.void_initializearray_array_fieldhandle
== null)
6340 // First, the static data
6343 ILGenerator ig
= ec
.ig
;
6345 byte [] data
= MakeByteBlob ();
6347 fb
= RootContext
.MakeStaticData (data
);
6349 ig
.Emit (OpCodes
.Dup
);
6350 ig
.Emit (OpCodes
.Ldtoken
, fb
);
6351 ig
.Emit (OpCodes
.Call
,
6352 TypeManager
.void_initializearray_array_fieldhandle
);
6356 // Emits pieces of the array that can not be computed at compile
6357 // time (variables and string locations).
6359 // This always expect the top value on the stack to be the array
6361 void EmitDynamicInitializers (EmitContext ec
, bool emitConstants
)
6363 ILGenerator ig
= ec
.ig
;
6364 int dims
= bounds
.Count
;
6365 int [] current_pos
= new int [dims
];
6367 MethodInfo
set = null;
6370 Type
[] args
= new Type
[dims
+ 1];
6372 for (int j
= 0; j
< dims
; j
++)
6373 args
[j
] = TypeManager
.int32_type
;
6374 args
[dims
] = array_element_type
;
6376 set = RootContext
.ToplevelTypes
.Builder
.GetArrayMethod (
6378 CallingConventions
.HasThis
| CallingConventions
.Standard
,
6379 TypeManager
.void_type
, args
);
6382 for (int i
= 0; i
< array_data
.Count
; i
++){
6384 Expression e
= array_data
[i
];
6386 // Constant can be initialized via StaticInitializer
6387 if (e
!= null && !(!emitConstants
&& e
is Constant
)) {
6388 Type etype
= e
.Type
;
6390 ig
.Emit (OpCodes
.Dup
);
6392 for (int idx
= 0; idx
< dims
; idx
++)
6393 IntConstant
.EmitInt (ig
, current_pos
[idx
]);
6396 // If we are dealing with a struct, get the
6397 // address of it, so we can store it.
6399 if ((dims
== 1) && TypeManager
.IsStruct (etype
) &&
6400 (!TypeManager
.IsBuiltinOrEnum (etype
) ||
6401 etype
== TypeManager
.decimal_type
)) {
6403 ig
.Emit (OpCodes
.Ldelema
, etype
);
6409 bool is_stobj
, has_type_arg
;
6410 OpCode op
= ArrayAccess
.GetStoreOpcode (etype
, out is_stobj
, out has_type_arg
);
6412 ig
.Emit (OpCodes
.Stobj
, etype
);
6413 else if (has_type_arg
)
6414 ig
.Emit (op
, etype
);
6418 ig
.Emit (OpCodes
.Call
, set);
6425 for (int j
= dims
- 1; j
>= 0; j
--){
6427 if (current_pos
[j
] < bounds
[j
])
6429 current_pos
[j
] = 0;
6434 public override void Emit (EmitContext ec
)
6436 ILGenerator ig
= ec
.ig
;
6438 if (first_emit
!= null) {
6439 first_emit
.Emit (ec
);
6440 first_emit_temp
.Store (ec
);
6443 foreach (Expression e
in arguments
)
6446 if (arguments
.Count
== 1)
6447 ig
.Emit (OpCodes
.Newarr
, TypeManager
.TypeToReflectionType (array_element_type
));
6449 ig
.Emit (OpCodes
.Newobj
, GetArrayMethod (ec
, arguments
.Count
));
6452 if (initializers
== null)
6455 // Emit static initializer for arrays which have contain more than 2 items and
6456 // the static initializer will initialize at least 25% of array values.
6457 // NOTE: const_initializers_count does not contain default constant values.
6458 if (const_initializers_count
> 2 && const_initializers_count
* 4 > (array_data
.Count
) &&
6459 (TypeManager
.IsPrimitiveType (array_element_type
) || TypeManager
.IsEnumType (array_element_type
))) {
6460 EmitStaticInitializers (ec
);
6462 if (!only_constant_initializers
)
6463 EmitDynamicInitializers (ec
, false);
6465 EmitDynamicInitializers (ec
, true);
6468 if (first_emit_temp
!= null)
6469 first_emit_temp
.Release (ec
);
6472 public override bool GetAttributableValue (ResolveContext ec
, Type value_type
, out object value)
6474 if (arguments
.Count
!= 1) {
6475 // ec.Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
6476 return base.GetAttributableValue (ec
, null, out value);
6479 if (array_data
== null) {
6480 Expression arg
= arguments
[0];
6482 if (arg
.GetAttributableValue (ec
, arg
.Type
, out arg_value
) && arg_value
is int && (int)arg_value
== 0) {
6483 value = Array
.CreateInstance (array_element_type
, 0);
6487 // ec.Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
6488 return base.GetAttributableValue (ec
, null, out value);
6491 Array ret
= Array
.CreateInstance (array_element_type
, array_data
.Count
);
6492 object element_value
;
6493 for (int i
= 0; i
< ret
.Length
; ++i
)
6495 Expression e
= array_data
[i
];
6497 // Is null when an initializer is optimized (value == predefined value)
6501 if (!e
.GetAttributableValue (ec
, array_element_type
, out element_value
)) {
6505 ret
.SetValue (element_value
, i
);
6511 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6513 ArrayCreation target
= (ArrayCreation
) t
;
6515 if (requested_base_type
!= null)
6516 target
.requested_base_type
= (FullNamedExpression
)requested_base_type
.Clone (clonectx
);
6518 if (arguments
!= null){
6519 target
.arguments
= new List
<Expression
> (arguments
.Count
);
6520 foreach (Expression e
in arguments
)
6521 target
.arguments
.Add (e
.Clone (clonectx
));
6524 if (initializers
!= null)
6525 target
.initializers
= (ArrayInitializer
) initializers
.Clone (clonectx
);
6530 // Represents an implicitly typed array epxression
6532 class ImplicitlyTypedArrayCreation
: ArrayCreation
6534 public ImplicitlyTypedArrayCreation (string rank
, ArrayInitializer initializers
, Location loc
)
6535 : base (null, rank
, initializers
, loc
)
6537 if (rank
.Length
> 2) {
6538 while (rank
[++dimensions
] == ',');
6544 protected override Expression
DoResolve (ResolveContext ec
)
6549 if (!ResolveInitializers (ec
))
6552 if (array_element_type
== null || array_element_type
== TypeManager
.null_type
||
6553 array_element_type
== TypeManager
.void_type
|| array_element_type
== InternalType
.AnonymousMethod
||
6554 array_element_type
== InternalType
.MethodGroup
||
6555 arguments
.Count
!= dimensions
) {
6556 Error_NoBestType (ec
);
6561 // At this point we found common base type for all initializer elements
6562 // but we have to be sure that all static initializer elements are of
6565 UnifyInitializerElement (ec
);
6567 type
= TypeManager
.GetConstructedType (array_element_type
, rank
);
6568 eclass
= ExprClass
.Value
;
6572 void Error_NoBestType (ResolveContext ec
)
6574 ec
.Report
.Error (826, loc
,
6575 "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
6579 // Converts static initializer only
6581 void UnifyInitializerElement (ResolveContext ec
)
6583 for (int i
= 0; i
< array_data
.Count
; ++i
) {
6584 Expression e
= (Expression
)array_data
[i
];
6586 array_data
[i
] = Convert
.ImplicitConversion (ec
, e
, array_element_type
, Location
.Null
);
6590 protected override Expression
ResolveArrayElement (ResolveContext ec
, Expression element
)
6592 element
= element
.Resolve (ec
);
6593 if (element
== null)
6596 if (array_element_type
== null) {
6597 if (element
.Type
!= TypeManager
.null_type
)
6598 array_element_type
= element
.Type
;
6603 if (Convert
.ImplicitConversionExists (ec
, element
, array_element_type
)) {
6607 if (Convert
.ImplicitConversionExists (ec
, new TypeExpression (array_element_type
, loc
), element
.Type
)) {
6608 array_element_type
= element
.Type
;
6612 Error_NoBestType (ec
);
6617 public sealed class CompilerGeneratedThis
: This
6619 public static This Instance
= new CompilerGeneratedThis ();
6621 private CompilerGeneratedThis ()
6622 : base (Location
.Null
)
6626 public CompilerGeneratedThis (Type type
, Location loc
)
6632 protected override Expression
DoResolve (ResolveContext ec
)
6634 eclass
= ExprClass
.Variable
;
6636 type
= ec
.CurrentType
;
6638 is_struct
= type
.IsValueType
;
6642 public override HoistedVariable
GetHoistedVariable (AnonymousExpression ae
)
6649 /// Represents the `this' construct
6652 public class This
: VariableReference
6654 sealed class ThisVariable
: ILocalVariable
6656 public static readonly ILocalVariable Instance
= new ThisVariable ();
6658 public void Emit (EmitContext ec
)
6660 ec
.ig
.Emit (OpCodes
.Ldarg_0
);
6663 public void EmitAssign (EmitContext ec
)
6665 throw new InvalidOperationException ();
6668 public void EmitAddressOf (EmitContext ec
)
6670 ec
.ig
.Emit (OpCodes
.Ldarg_0
);
6675 VariableInfo variable_info
;
6676 protected bool is_struct
;
6678 public This (Block block
, Location loc
)
6684 public This (Location loc
)
6689 public override VariableInfo VariableInfo
{
6690 get { return variable_info; }
6693 public override bool IsFixed
{
6694 get { return false; }
6697 public override HoistedVariable
GetHoistedVariable (AnonymousExpression ae
)
6702 AnonymousMethodStorey storey
= ae
.Storey
;
6703 while (storey
!= null) {
6704 AnonymousMethodStorey temp
= storey
.Parent
as AnonymousMethodStorey
;
6706 return storey
.HoistedThis
;
6714 public override bool IsRef
{
6715 get { return is_struct; }
6718 protected override ILocalVariable Variable
{
6719 get { return ThisVariable.Instance; }
6722 public static bool IsThisAvailable (ResolveContext ec
)
6724 if (ec
.IsStatic
|| ec
.HasAny (ResolveContext
.Options
.FieldInitializerScope
| ResolveContext
.Options
.BaseInitializer
| ResolveContext
.Options
.ConstantScope
))
6727 if (ec
.CurrentAnonymousMethod
== null)
6730 if (ec
.CurrentType
.IsValueType
&& ec
.CurrentIterator
== null)
6736 public bool ResolveBase (ResolveContext ec
)
6738 eclass
= ExprClass
.Variable
;
6739 type
= ec
.CurrentType
;
6741 if (!IsThisAvailable (ec
)) {
6742 if (ec
.IsStatic
&& !ec
.HasSet (ResolveContext
.Options
.ConstantScope
)) {
6743 ec
.Report
.Error (26, loc
, "Keyword `this' is not valid in a static property, static method, or static field initializer");
6744 } else if (ec
.CurrentAnonymousMethod
!= null) {
6745 ec
.Report
.Error (1673, loc
,
6746 "Anonymous methods inside structs cannot access instance members of `this'. " +
6747 "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
6749 ec
.Report
.Error (27, loc
, "Keyword `this' is not available in the current context");
6753 is_struct
= type
.IsValueType
;
6755 if (block
!= null) {
6756 if (block
.Toplevel
.ThisVariable
!= null)
6757 variable_info
= block
.Toplevel
.ThisVariable
.VariableInfo
;
6759 AnonymousExpression am
= ec
.CurrentAnonymousMethod
;
6760 if (am
!= null && ec
.IsVariableCapturingRequired
) {
6761 am
.SetHasThisAccess ();
6769 // Called from Invocation to check if the invocation is correct
6771 public override void CheckMarshalByRefAccess (ResolveContext ec
)
6773 if ((variable_info
!= null) && !(TypeManager
.IsStruct (type
) && ec
.OmitStructFlowAnalysis
) &&
6774 !variable_info
.IsAssigned (ec
)) {
6775 ec
.Report
.Error (188, loc
,
6776 "The `this' object cannot be used before all of its fields are assigned to");
6777 variable_info
.SetAssigned (ec
);
6781 public override Expression
CreateExpressionTree (ResolveContext ec
)
6783 Arguments args
= new Arguments (1);
6784 args
.Add (new Argument (this));
6786 // Use typeless constant for ldarg.0 to save some
6787 // space and avoid problems with anonymous stories
6788 return CreateExpressionFactoryCall (ec
, "Constant", args
);
6791 protected override Expression
DoResolve (ResolveContext ec
)
6797 override public Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
6799 if (!ResolveBase (ec
))
6802 if (variable_info
!= null)
6803 variable_info
.SetAssigned (ec
);
6805 if (ec
.CurrentType
.IsClass
){
6806 if (right_side
== EmptyExpression
.UnaryAddress
)
6807 ec
.Report
.Error (459, loc
, "Cannot take the address of `this' because it is read-only");
6808 else if (right_side
== EmptyExpression
.OutAccess
.Instance
)
6809 ec
.Report
.Error (1605, loc
, "Cannot pass `this' as a ref or out argument because it is read-only");
6811 ec
.Report
.Error (1604, loc
, "Cannot assign to `this' because it is read-only");
6817 public override int GetHashCode()
6819 return block
.GetHashCode ();
6822 public override string Name
{
6823 get { return "this"; }
6826 public override bool Equals (object obj
)
6828 This t
= obj
as This
;
6832 return block
== t
.block
;
6835 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6837 This target
= (This
) t
;
6839 target
.block
= clonectx
.LookupBlock (block
);
6842 public override void SetHasAddressTaken ()
6849 /// Represents the `__arglist' construct
6851 public class ArglistAccess
: Expression
6853 public ArglistAccess (Location loc
)
6858 public override Expression
CreateExpressionTree (ResolveContext ec
)
6860 throw new NotSupportedException ("ET");
6863 protected override Expression
DoResolve (ResolveContext ec
)
6865 eclass
= ExprClass
.Variable
;
6866 type
= TypeManager
.runtime_argument_handle_type
;
6868 if (ec
.HasSet (ResolveContext
.Options
.FieldInitializerScope
) || !ec
.CurrentBlock
.Toplevel
.Parameters
.HasArglist
) {
6869 ec
.Report
.Error (190, loc
,
6870 "The __arglist construct is valid only within a variable argument method");
6876 public override void Emit (EmitContext ec
)
6878 ec
.ig
.Emit (OpCodes
.Arglist
);
6881 protected override void CloneTo (CloneContext clonectx
, Expression target
)
6888 /// Represents the `__arglist (....)' construct
6890 class Arglist
: Expression
6892 Arguments Arguments
;
6894 public Arglist (Location loc
)
6899 public Arglist (Arguments args
, Location l
)
6905 public Type
[] ArgumentTypes
{
6907 if (Arguments
== null)
6908 return Type
.EmptyTypes
;
6910 Type
[] retval
= new Type
[Arguments
.Count
];
6911 for (int i
= 0; i
< retval
.Length
; i
++)
6912 retval
[i
] = Arguments
[i
].Expr
.Type
;
6918 public override Expression
CreateExpressionTree (ResolveContext ec
)
6920 ec
.Report
.Error (1952, loc
, "An expression tree cannot contain a method with variable arguments");
6924 protected override Expression
DoResolve (ResolveContext ec
)
6926 eclass
= ExprClass
.Variable
;
6927 type
= InternalType
.Arglist
;
6928 if (Arguments
!= null) {
6929 bool dynamic; // Can be ignored as there is always only 1 overload
6930 Arguments
.Resolve (ec
, out dynamic);
6936 public override void Emit (EmitContext ec
)
6938 if (Arguments
!= null)
6939 Arguments
.Emit (ec
);
6942 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
6944 if (Arguments
!= null)
6945 Arguments
.MutateHoistedGenericType (storey
);
6948 protected override void CloneTo (CloneContext clonectx
, Expression t
)
6950 Arglist target
= (Arglist
) t
;
6952 if (Arguments
!= null)
6953 target
.Arguments
= Arguments
.Clone (clonectx
);
6958 /// Implements the typeof operator
6960 public class TypeOf
: Expression
{
6961 Expression QueriedType
;
6962 protected Type typearg
;
6964 public TypeOf (Expression queried_type
, Location l
)
6966 QueriedType
= queried_type
;
6970 public override Expression
CreateExpressionTree (ResolveContext ec
)
6972 Arguments args
= new Arguments (2);
6973 args
.Add (new Argument (this));
6974 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
6975 return CreateExpressionFactoryCall (ec
, "Constant", args
);
6978 protected override Expression
DoResolve (ResolveContext ec
)
6980 TypeExpr texpr
= QueriedType
.ResolveAsTypeTerminal (ec
, false);
6984 typearg
= texpr
.Type
;
6986 if (typearg
== TypeManager
.void_type
) {
6987 ec
.Report
.Error (673, loc
, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
6988 } else if (typearg
.IsPointer
&& !ec
.IsUnsafe
){
6989 UnsafeError (ec
, loc
);
6990 } else if (texpr
is DynamicTypeExpr
) {
6991 ec
.Report
.Error (1962, QueriedType
.Location
,
6992 "The typeof operator cannot be used on the dynamic type");
6995 type
= TypeManager
.type_type
;
6997 return DoResolveBase ();
7000 protected Expression
DoResolveBase ()
7002 if (TypeManager
.system_type_get_type_from_handle
== null) {
7003 TypeManager
.system_type_get_type_from_handle
= TypeManager
.GetPredefinedMethod (
7004 TypeManager
.type_type
, "GetTypeFromHandle", loc
, TypeManager
.runtime_handle_type
);
7007 // Even though what is returned is a type object, it's treated as a value by the compiler.
7008 // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
7009 eclass
= ExprClass
.Value
;
7013 public override void Emit (EmitContext ec
)
7015 ec
.ig
.Emit (OpCodes
.Ldtoken
, TypeManager
.TypeToReflectionType (typearg
));
7016 ec
.ig
.Emit (OpCodes
.Call
, TypeManager
.system_type_get_type_from_handle
);
7019 public override bool GetAttributableValue (ResolveContext ec
, Type value_type
, out object value)
7021 if (TypeManager
.ContainsGenericParameters (typearg
) &&
7022 !TypeManager
.IsGenericTypeDefinition (typearg
)) {
7023 ec
.Report
.SymbolRelatedToPreviousError (typearg
);
7024 ec
.Report
.Error (416, loc
, "`{0}': an attribute argument cannot use type parameters",
7025 TypeManager
.CSharpName (typearg
));
7030 if (value_type
== TypeManager
.object_type
) {
7031 value = (object)typearg
;
7038 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
7040 typearg
= storey
.MutateType (typearg
);
7043 public Type TypeArgument
{
7049 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7051 TypeOf target
= (TypeOf
) t
;
7052 if (QueriedType
!= null)
7053 target
.QueriedType
= QueriedType
.Clone (clonectx
);
7058 /// Implements the `typeof (void)' operator
7060 public class TypeOfVoid
: TypeOf
{
7061 public TypeOfVoid (Location l
) : base (null, l
)
7066 protected override Expression
DoResolve (ResolveContext ec
)
7068 type
= TypeManager
.type_type
;
7069 typearg
= TypeManager
.void_type
;
7071 return DoResolveBase ();
7075 class TypeOfMethod
: TypeOfMember
7077 public TypeOfMethod (MethodBase method
, Location loc
)
7078 : base (method
, loc
)
7082 protected override Expression
DoResolve (ResolveContext ec
)
7084 if (member
is MethodInfo
) {
7085 type
= TypeManager
.methodinfo_type
;
7087 type
= TypeManager
.methodinfo_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System.Reflection", "MethodInfo", Kind
.Class
, true);
7089 type
= TypeManager
.ctorinfo_type
;
7091 type
= TypeManager
.ctorinfo_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System.Reflection", "ConstructorInfo", Kind
.Class
, true);
7094 return base.DoResolve (ec
);
7097 public override void Emit (EmitContext ec
)
7099 if (member
is ConstructorInfo
)
7100 ec
.ig
.Emit (OpCodes
.Ldtoken
, (ConstructorInfo
) member
);
7102 ec
.ig
.Emit (OpCodes
.Ldtoken
, (MethodInfo
) member
);
7105 ec
.ig
.Emit (OpCodes
.Castclass
, type
);
7108 protected override string GetMethodName
{
7109 get { return "GetMethodFromHandle"; }
7112 protected override string RuntimeHandleName
{
7113 get { return "RuntimeMethodHandle"; }
7116 protected override MethodInfo TypeFromHandle
{
7118 return TypeManager
.methodbase_get_type_from_handle
;
7121 TypeManager
.methodbase_get_type_from_handle
= value;
7125 protected override MethodInfo TypeFromHandleGeneric
{
7127 return TypeManager
.methodbase_get_type_from_handle_generic
;
7130 TypeManager
.methodbase_get_type_from_handle_generic
= value;
7134 protected override string TypeName
{
7135 get { return "MethodBase"; }
7139 abstract class TypeOfMember
: Expression
7141 protected readonly MemberInfo member
;
7143 protected TypeOfMember (MemberInfo member
, Location loc
)
7145 this.member
= member
;
7149 public override Expression
CreateExpressionTree (ResolveContext ec
)
7151 Arguments args
= new Arguments (2);
7152 args
.Add (new Argument (this));
7153 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
7154 return CreateExpressionFactoryCall (ec
, "Constant", args
);
7157 protected override Expression
DoResolve (ResolveContext ec
)
7159 bool is_generic
= TypeManager
.IsGenericType (member
.DeclaringType
);
7160 MethodInfo mi
= is_generic
? TypeFromHandleGeneric
: TypeFromHandle
;
7163 Type t
= TypeManager
.CoreLookupType (ec
.Compiler
, "System.Reflection", TypeName
, Kind
.Class
, true);
7164 Type handle_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System", RuntimeHandleName
, Kind
.Class
, true);
7166 if (t
== null || handle_type
== null)
7169 mi
= TypeManager
.GetPredefinedMethod (t
, GetMethodName
, loc
,
7171 new Type
[] { handle_type, TypeManager.runtime_handle_type }
:
7172 new Type
[] { handle_type }
);
7175 TypeFromHandleGeneric
= mi
;
7177 TypeFromHandle
= mi
;
7180 eclass
= ExprClass
.Value
;
7184 public override void Emit (EmitContext ec
)
7186 bool is_generic
= TypeManager
.IsGenericType (member
.DeclaringType
);
7189 mi
= TypeFromHandleGeneric
;
7190 ec
.ig
.Emit (OpCodes
.Ldtoken
, member
.DeclaringType
);
7192 mi
= TypeFromHandle
;
7195 ec
.ig
.Emit (OpCodes
.Call
, mi
);
7198 protected abstract string GetMethodName { get; }
7199 protected abstract string RuntimeHandleName { get; }
7200 protected abstract MethodInfo TypeFromHandle { get; set; }
7201 protected abstract MethodInfo TypeFromHandleGeneric { get; set; }
7202 protected abstract string TypeName { get; }
7205 class TypeOfField
: TypeOfMember
7207 public TypeOfField (FieldInfo field
, Location loc
)
7212 protected override Expression
DoResolve (ResolveContext ec
)
7214 if (TypeManager
.fieldinfo_type
== null)
7215 TypeManager
.fieldinfo_type
= TypeManager
.CoreLookupType (ec
.Compiler
, "System.Reflection", TypeName
, Kind
.Class
, true);
7217 type
= TypeManager
.fieldinfo_type
;
7218 return base.DoResolve (ec
);
7221 public override void Emit (EmitContext ec
)
7223 ec
.ig
.Emit (OpCodes
.Ldtoken
, (FieldInfo
) member
);
7227 protected override string GetMethodName
{
7228 get { return "GetFieldFromHandle"; }
7231 protected override string RuntimeHandleName
{
7232 get { return "RuntimeFieldHandle"; }
7235 protected override MethodInfo TypeFromHandle
{
7237 return TypeManager
.fieldinfo_get_field_from_handle
;
7240 TypeManager
.fieldinfo_get_field_from_handle
= value;
7244 protected override MethodInfo TypeFromHandleGeneric
{
7246 return TypeManager
.fieldinfo_get_field_from_handle_generic
;
7249 TypeManager
.fieldinfo_get_field_from_handle_generic
= value;
7253 protected override string TypeName
{
7254 get { return "FieldInfo"; }
7259 /// Implements the sizeof expression
7261 public class SizeOf
: Expression
{
7262 readonly Expression QueriedType
;
7265 public SizeOf (Expression queried_type
, Location l
)
7267 this.QueriedType
= queried_type
;
7271 public override Expression
CreateExpressionTree (ResolveContext ec
)
7273 Error_PointerInsideExpressionTree (ec
);
7277 protected override Expression
DoResolve (ResolveContext ec
)
7279 TypeExpr texpr
= QueriedType
.ResolveAsTypeTerminal (ec
, false);
7283 type_queried
= texpr
.Type
;
7284 if (TypeManager
.IsEnumType (type_queried
))
7285 type_queried
= TypeManager
.GetEnumUnderlyingType (type_queried
);
7287 int size_of
= GetTypeSize (type_queried
);
7289 return new IntConstant (size_of
, loc
).Resolve (ec
);
7292 if (!TypeManager
.VerifyUnmanaged (ec
.Compiler
, type_queried
, loc
)){
7297 ec
.Report
.Error (233, loc
,
7298 "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
7299 TypeManager
.CSharpName (type_queried
));
7302 type
= TypeManager
.int32_type
;
7303 eclass
= ExprClass
.Value
;
7307 public override void Emit (EmitContext ec
)
7309 ec
.ig
.Emit (OpCodes
.Sizeof
, type_queried
);
7312 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7318 /// Implements the qualified-alias-member (::) expression.
7320 public class QualifiedAliasMember
: MemberAccess
7322 readonly string alias;
7323 public static readonly string GlobalAlias
= "global";
7325 public QualifiedAliasMember (string alias, string identifier
, TypeArguments targs
, Location l
)
7326 : base (null, identifier
, targs
, l
)
7331 public QualifiedAliasMember (string alias, string identifier
, Location l
)
7332 : base (null, identifier
, l
)
7337 public override FullNamedExpression
ResolveAsTypeStep (IMemberContext ec
, bool silent
)
7339 if (alias == GlobalAlias
) {
7340 expr
= GlobalRootNamespace
.Instance
;
7341 return base.ResolveAsTypeStep (ec
, silent
);
7344 int errors
= ec
.Compiler
.Report
.Errors
;
7345 expr
= ec
.LookupNamespaceAlias (alias);
7347 if (errors
== ec
.Compiler
.Report
.Errors
)
7348 ec
.Compiler
.Report
.Error (432, loc
, "Alias `{0}' not found", alias);
7352 FullNamedExpression fne
= base.ResolveAsTypeStep (ec
, silent
);
7356 if (expr
.eclass
== ExprClass
.Type
) {
7358 ec
.Compiler
.Report
.Error (431, loc
,
7359 "Alias `{0}' cannot be used with '::' since it denotes a type. Consider replacing '::' with '.'", alias);
7367 protected override Expression
DoResolve (ResolveContext ec
)
7369 return ResolveAsTypeStep (ec
, false);
7372 protected override void Error_IdentifierNotFound (IMemberContext rc
, FullNamedExpression expr_type
, string identifier
)
7374 rc
.Compiler
.Report
.Error (687, loc
,
7375 "A namespace alias qualifier `{0}' did not resolve to a namespace or a type",
7376 GetSignatureForError ());
7379 public override string GetSignatureForError ()
7382 if (targs
!= null) {
7383 name
= TypeManager
.RemoveGenericArity (Name
) + "<" +
7384 targs
.GetSignatureForError () + ">";
7387 return alias + "::" + name
;
7390 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7397 /// Implements the member access expression
7399 public class MemberAccess
: ATypeNameExpression
{
7400 protected Expression expr
;
7402 public MemberAccess (Expression expr
, string id
)
7403 : base (id
, expr
.Location
)
7408 public MemberAccess (Expression expr
, string identifier
, Location loc
)
7409 : base (identifier
, loc
)
7414 public MemberAccess (Expression expr
, string identifier
, TypeArguments args
, Location loc
)
7415 : base (identifier
, args
, loc
)
7420 Expression
DoResolve (ResolveContext ec
, Expression right_side
)
7423 throw new Exception ();
7426 // Resolve the expression with flow analysis turned off, we'll do the definite
7427 // assignment checks later. This is because we don't know yet what the expression
7428 // will resolve to - it may resolve to a FieldExpr and in this case we must do the
7429 // definite assignment check on the actual field and not on the whole struct.
7432 SimpleName original
= expr
as SimpleName
;
7433 Expression expr_resolved
;
7434 using (ec
.Set (ResolveContext
.Options
.OmitStructFlowAnalysis
)) {
7435 expr_resolved
= expr
.Resolve (ec
, ResolveFlags
.VariableOrValue
| ResolveFlags
.Type
| ResolveFlags
.Intermediate
);
7438 if (expr_resolved
== null)
7441 string LookupIdentifier
= MemberName
.MakeName (Name
, targs
);
7443 Namespace ns
= expr_resolved
as Namespace
;
7445 FullNamedExpression retval
= ns
.Lookup (ec
.Compiler
, LookupIdentifier
, loc
);
7448 ns
.Error_NamespaceDoesNotExist (loc
, LookupIdentifier
, ec
);
7449 else if (targs
!= null)
7450 retval
= new GenericTypeExpr (retval
.Type
, targs
, loc
).ResolveAsTypeStep (ec
, false);
7455 Type expr_type
= expr_resolved
.Type
;
7456 if (TypeManager
.IsDynamicType (expr_type
)) {
7457 Arguments args
= new Arguments (1);
7458 args
.Add (new Argument (expr_resolved
.Resolve (ec
)));
7459 expr
= new DynamicMemberBinder (Name
, args
, loc
);
7460 if (right_side
!= null)
7461 return expr
.DoResolveLValue (ec
, right_side
);
7463 return expr
.Resolve (ec
);
7466 if (expr_type
.IsPointer
|| expr_type
== TypeManager
.void_type
||
7467 expr_type
== TypeManager
.null_type
|| expr_type
== InternalType
.AnonymousMethod
) {
7468 Unary
.Error_OperatorCannotBeApplied (ec
, loc
, ".", expr_type
);
7472 Constant c
= expr_resolved
as Constant
;
7473 if (c
!= null && c
.GetValue () == null) {
7474 ec
.Report
.Warning (1720, 1, loc
, "Expression will always cause a `{0}'",
7475 "System.NullReferenceException");
7478 if (targs
!= null) {
7479 if (!targs
.Resolve (ec
))
7483 Expression member_lookup
;
7484 member_lookup
= MemberLookup (ec
.Compiler
,
7485 ec
.CurrentType
, expr_type
, expr_type
, Name
, loc
);
7487 if (member_lookup
== null && targs
!= null) {
7488 member_lookup
= MemberLookup (ec
.Compiler
,
7489 ec
.CurrentType
, expr_type
, expr_type
, LookupIdentifier
, loc
);
7492 if (member_lookup
== null) {
7493 ExprClass expr_eclass
= expr_resolved
.eclass
;
7496 // Extension methods are not allowed on all expression types
7498 if (expr_eclass
== ExprClass
.Value
|| expr_eclass
== ExprClass
.Variable
||
7499 expr_eclass
== ExprClass
.IndexerAccess
|| expr_eclass
== ExprClass
.PropertyAccess
||
7500 expr_eclass
== ExprClass
.EventAccess
) {
7501 ExtensionMethodGroupExpr ex_method_lookup
= ec
.LookupExtensionMethod (expr_type
, Name
, loc
);
7502 if (ex_method_lookup
!= null) {
7503 ex_method_lookup
.ExtensionExpression
= expr_resolved
;
7505 if (targs
!= null) {
7506 ex_method_lookup
.SetTypeArguments (ec
, targs
);
7509 return ex_method_lookup
.Resolve (ec
);
7513 expr
= expr_resolved
;
7514 member_lookup
= Error_MemberLookupFailed (ec
,
7515 ec
.CurrentType
, expr_type
, expr_type
, Name
, null,
7516 AllMemberTypes
, AllBindingFlags
);
7517 if (member_lookup
== null)
7521 TypeExpr texpr
= member_lookup
as TypeExpr
;
7522 if (texpr
!= null) {
7523 if (!(expr_resolved
is TypeExpr
) &&
7524 (original
== null || !original
.IdenticalNameAndTypeName (ec
, expr_resolved
, loc
))) {
7525 ec
.Report
.Error (572, loc
, "`{0}': cannot reference a type through an expression; try `{1}' instead",
7526 Name
, member_lookup
.GetSignatureForError ());
7530 if (!texpr
.CheckAccessLevel (ec
.MemberContext
)) {
7531 ec
.Report
.SymbolRelatedToPreviousError (member_lookup
.Type
);
7532 ErrorIsInaccesible (loc
, TypeManager
.CSharpName (member_lookup
.Type
), ec
.Report
);
7536 GenericTypeExpr ct
= expr_resolved
as GenericTypeExpr
;
7539 // When looking up a nested type in a generic instance
7540 // via reflection, we always get a generic type definition
7541 // and not a generic instance - so we have to do this here.
7543 // See gtest-172-lib.cs and gtest-172.cs for an example.
7546 TypeArguments nested_targs
;
7547 if (HasTypeArguments
) {
7548 nested_targs
= ct
.TypeArguments
.Clone ();
7549 nested_targs
.Add (targs
);
7551 nested_targs
= ct
.TypeArguments
;
7554 ct
= new GenericTypeExpr (member_lookup
.Type
, nested_targs
, loc
);
7556 return ct
.ResolveAsTypeStep (ec
, false);
7559 return member_lookup
;
7562 MemberExpr me
= (MemberExpr
) member_lookup
;
7563 me
= me
.ResolveMemberAccess (ec
, expr_resolved
, loc
, original
);
7567 if (targs
!= null) {
7568 me
.SetTypeArguments (ec
, targs
);
7571 if (original
!= null && (!TypeManager
.IsValueType (expr_type
) || me
is PropertyExpr
)) {
7572 if (me
.IsInstance
) {
7573 LocalVariableReference
var = expr_resolved
as LocalVariableReference
;
7574 if (var != null && !var.VerifyAssigned (ec
))
7579 // The following DoResolve/DoResolveLValue will do the definite assignment
7582 if (right_side
!= null)
7583 return me
.DoResolveLValue (ec
, right_side
);
7585 return me
.Resolve (ec
);
7588 protected override Expression
DoResolve (ResolveContext ec
)
7590 return DoResolve (ec
, null);
7593 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
7595 return DoResolve (ec
, right_side
);
7598 public override FullNamedExpression
ResolveAsTypeStep (IMemberContext ec
, bool silent
)
7600 return ResolveNamespaceOrType (ec
, silent
);
7603 public FullNamedExpression
ResolveNamespaceOrType (IMemberContext rc
, bool silent
)
7605 FullNamedExpression expr_resolved
= expr
.ResolveAsTypeStep (rc
, silent
);
7607 if (expr_resolved
== null)
7610 string LookupIdentifier
= MemberName
.MakeName (Name
, targs
);
7612 Namespace ns
= expr_resolved
as Namespace
;
7614 FullNamedExpression retval
= ns
.Lookup (rc
.Compiler
, LookupIdentifier
, loc
);
7616 if (retval
== null && !silent
)
7617 ns
.Error_NamespaceDoesNotExist (loc
, LookupIdentifier
, rc
);
7618 else if (targs
!= null)
7619 retval
= new GenericTypeExpr (retval
.Type
, targs
, loc
).ResolveAsTypeStep (rc
, silent
);
7624 TypeExpr tnew_expr
= expr_resolved
.ResolveAsTypeTerminal (rc
, false);
7625 if (tnew_expr
== null)
7628 Type expr_type
= tnew_expr
.Type
;
7629 if (TypeManager
.IsGenericParameter (expr_type
)) {
7630 rc
.Compiler
.Report
.Error (704, loc
, "A nested type cannot be specified through a type parameter `{0}'",
7631 tnew_expr
.GetSignatureForError ());
7635 Expression member_lookup
= MemberLookup (rc
.Compiler
,
7636 rc
.CurrentType
, expr_type
, expr_type
, LookupIdentifier
,
7637 MemberTypes
.NestedType
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7638 if (member_lookup
== null) {
7642 Error_IdentifierNotFound (rc
, expr_resolved
, LookupIdentifier
);
7646 TypeExpr texpr
= member_lookup
.ResolveAsTypeTerminal (rc
, false);
7650 TypeArguments the_args
= targs
;
7651 Type declaring_type
= texpr
.Type
.DeclaringType
;
7652 if (TypeManager
.HasGenericArguments (declaring_type
) && !TypeManager
.IsGenericTypeDefinition (expr_type
)) {
7653 while (!TypeManager
.IsEqual (TypeManager
.DropGenericTypeArguments (expr_type
), declaring_type
)) {
7654 expr_type
= expr_type
.BaseType
;
7657 TypeArguments new_args
= new TypeArguments ();
7658 foreach (Type decl
in TypeManager
.GetTypeArguments (expr_type
))
7659 new_args
.Add (new TypeExpression (TypeManager
.TypeToCoreType (decl
), loc
));
7662 new_args
.Add (targs
);
7664 the_args
= new_args
;
7667 if (the_args
!= null) {
7668 GenericTypeExpr ctype
= new GenericTypeExpr (texpr
.Type
, the_args
, loc
);
7669 return ctype
.ResolveAsTypeStep (rc
, false);
7675 protected virtual void Error_IdentifierNotFound (IMemberContext rc
, FullNamedExpression expr_type
, string identifier
)
7677 Expression member_lookup
= MemberLookup (rc
.Compiler
,
7678 rc
.CurrentType
, expr_type
.Type
, expr_type
.Type
, SimpleName
.RemoveGenericArity (identifier
),
7679 MemberTypes
.NestedType
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7681 if (member_lookup
!= null) {
7682 expr_type
= member_lookup
.ResolveAsTypeTerminal (rc
, false);
7683 if (expr_type
== null)
7686 expr_type
.Error_TypeArgumentsCannotBeUsed (rc
.Compiler
.Report
, loc
);
7690 member_lookup
= MemberLookup (rc
.Compiler
,
7691 rc
.CurrentType
, expr_type
.Type
, expr_type
.Type
, identifier
,
7692 MemberTypes
.All
, BindingFlags
.Public
| BindingFlags
.NonPublic
, loc
);
7694 if (member_lookup
== null) {
7695 rc
.Compiler
.Report
.Error (426, loc
, "The nested type `{0}' does not exist in the type `{1}'",
7696 Name
, expr_type
.GetSignatureForError ());
7698 // TODO: Report.SymbolRelatedToPreviousError
7699 member_lookup
.Error_UnexpectedKind (rc
.Compiler
.Report
, null, "type", loc
);
7703 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec
, Type type
, string name
)
7705 if (RootContext
.Version
> LanguageVersion
.ISO_2
&& !ec
.Compiler
.IsRuntimeBinder
&&
7706 ((expr
.eclass
& (ExprClass
.Value
| ExprClass
.Variable
)) != 0)) {
7707 ec
.Report
.Error (1061, loc
, "Type `{0}' does not contain a definition for `{1}' and no " +
7708 "extension method `{1}' of type `{0}' could be found " +
7709 "(are you missing a using directive or an assembly reference?)",
7710 TypeManager
.CSharpName (type
), name
);
7714 base.Error_TypeDoesNotContainDefinition (ec
, type
, name
);
7717 public override string GetSignatureForError ()
7719 return expr
.GetSignatureForError () + "." + base.GetSignatureForError ();
7722 public Expression Left
{
7728 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7730 MemberAccess target
= (MemberAccess
) t
;
7732 target
.expr
= expr
.Clone (clonectx
);
7737 /// Implements checked expressions
7739 public class CheckedExpr
: Expression
{
7741 public Expression Expr
;
7743 public CheckedExpr (Expression e
, Location l
)
7749 public override Expression
CreateExpressionTree (ResolveContext ec
)
7751 using (ec
.With (ResolveContext
.Options
.AllCheckStateFlags
, true))
7752 return Expr
.CreateExpressionTree (ec
);
7755 protected override Expression
DoResolve (ResolveContext ec
)
7757 using (ec
.With (ResolveContext
.Options
.AllCheckStateFlags
, true))
7758 Expr
= Expr
.Resolve (ec
);
7763 if (Expr
is Constant
|| Expr
is MethodGroupExpr
|| Expr
is AnonymousMethodExpression
|| Expr
is DefaultValueExpression
)
7766 eclass
= Expr
.eclass
;
7771 public override void Emit (EmitContext ec
)
7773 using (ec
.With (EmitContext
.Options
.AllCheckStateFlags
, true))
7777 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
7779 using (ec
.With (EmitContext
.Options
.AllCheckStateFlags
, true))
7780 Expr
.EmitBranchable (ec
, target
, on_true
);
7783 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
7785 using (ctx
.With (BuilderContext
.Options
.AllCheckStateFlags
, true)) {
7786 return Expr
.MakeExpression (ctx
);
7790 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
7792 Expr
.MutateHoistedGenericType (storey
);
7795 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7797 CheckedExpr target
= (CheckedExpr
) t
;
7799 target
.Expr
= Expr
.Clone (clonectx
);
7804 /// Implements the unchecked expression
7806 public class UnCheckedExpr
: Expression
{
7808 public Expression Expr
;
7810 public UnCheckedExpr (Expression e
, Location l
)
7816 public override Expression
CreateExpressionTree (ResolveContext ec
)
7818 using (ec
.With (ResolveContext
.Options
.AllCheckStateFlags
, false))
7819 return Expr
.CreateExpressionTree (ec
);
7822 protected override Expression
DoResolve (ResolveContext ec
)
7824 using (ec
.With (ResolveContext
.Options
.AllCheckStateFlags
, false))
7825 Expr
= Expr
.Resolve (ec
);
7830 if (Expr
is Constant
|| Expr
is MethodGroupExpr
|| Expr
is AnonymousMethodExpression
|| Expr
is DefaultValueExpression
)
7833 eclass
= Expr
.eclass
;
7838 public override void Emit (EmitContext ec
)
7840 using (ec
.With (EmitContext
.Options
.AllCheckStateFlags
, false))
7844 public override void EmitBranchable (EmitContext ec
, Label target
, bool on_true
)
7846 using (ec
.With (EmitContext
.Options
.AllCheckStateFlags
, false))
7847 Expr
.EmitBranchable (ec
, target
, on_true
);
7850 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
7852 Expr
.MutateHoistedGenericType (storey
);
7855 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7857 UnCheckedExpr target
= (UnCheckedExpr
) t
;
7859 target
.Expr
= Expr
.Clone (clonectx
);
7864 /// An Element Access expression.
7866 /// During semantic analysis these are transformed into
7867 /// IndexerAccess, ArrayAccess or a PointerArithmetic.
7869 public class ElementAccess
: Expression
{
7870 public Arguments Arguments
;
7871 public Expression Expr
;
7873 public ElementAccess (Expression e
, Arguments args
)
7877 this.Arguments
= args
;
7880 public override Expression
CreateExpressionTree (ResolveContext ec
)
7882 Arguments args
= Arguments
.CreateForExpressionTree (ec
, Arguments
,
7883 Expr
.CreateExpressionTree (ec
));
7885 return CreateExpressionFactoryCall (ec
, "ArrayIndex", args
);
7888 Expression
MakePointerAccess (ResolveContext ec
, Type t
)
7890 if (Arguments
.Count
!= 1){
7891 ec
.Report
.Error (196, loc
, "A pointer must be indexed by only one value");
7895 if (Arguments
[0] is NamedArgument
)
7896 Error_NamedArgument ((NamedArgument
) Arguments
[0], ec
.Report
);
7898 Expression p
= new PointerArithmetic (Binary
.Operator
.Addition
, Expr
, Arguments
[0].Expr
.Resolve (ec
), t
, loc
);
7899 return new Indirection (p
, loc
).Resolve (ec
);
7902 protected override Expression
DoResolve (ResolveContext ec
)
7904 Expr
= Expr
.Resolve (ec
);
7909 // We perform some simple tests, and then to "split" the emit and store
7910 // code we create an instance of a different class, and return that.
7912 // I am experimenting with this pattern.
7916 if (t
== TypeManager
.array_type
){
7917 ec
.Report
.Error (21, loc
, "Cannot apply indexing with [] to an expression of type `System.Array'");
7922 return (new ArrayAccess (this, loc
)).Resolve (ec
);
7924 return MakePointerAccess (ec
, t
);
7926 FieldExpr fe
= Expr
as FieldExpr
;
7928 IFixedBuffer ff
= AttributeTester
.GetFixedBuffer (fe
.FieldInfo
);
7930 return MakePointerAccess (ec
, ff
.ElementType
);
7933 return (new IndexerAccess (this, loc
)).Resolve (ec
);
7936 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
7938 Expr
= Expr
.Resolve (ec
);
7944 return (new ArrayAccess (this, loc
)).DoResolveLValue (ec
, right_side
);
7947 return MakePointerAccess (ec
, type
);
7949 if (Expr
.eclass
!= ExprClass
.Variable
&& TypeManager
.IsStruct (type
))
7950 Error_CannotModifyIntermediateExpressionValue (ec
);
7952 return (new IndexerAccess (this, loc
)).DoResolveLValue (ec
, right_side
);
7955 public override void Emit (EmitContext ec
)
7957 throw new Exception ("Should never be reached");
7960 public static void Error_NamedArgument (NamedArgument na
, Report Report
)
7962 Report
.Error (1742, na
.Location
, "An element access expression cannot use named argument");
7965 public override string GetSignatureForError ()
7967 return Expr
.GetSignatureForError ();
7970 protected override void CloneTo (CloneContext clonectx
, Expression t
)
7972 ElementAccess target
= (ElementAccess
) t
;
7974 target
.Expr
= Expr
.Clone (clonectx
);
7975 if (Arguments
!= null)
7976 target
.Arguments
= Arguments
.Clone (clonectx
);
7981 /// Implements array access
7983 public class ArrayAccess
: Expression
, IDynamicAssign
, IMemoryLocation
{
7985 // Points to our "data" repository
7989 LocalTemporary temp
;
7993 public ArrayAccess (ElementAccess ea_data
, Location l
)
7999 public override Expression
CreateExpressionTree (ResolveContext ec
)
8001 return ea
.CreateExpressionTree (ec
);
8004 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
8006 return DoResolve (ec
);
8009 protected override Expression
DoResolve (ResolveContext ec
)
8011 // dynamic is used per argument in ConvertExpressionToArrayIndex case
8013 ea
.Arguments
.Resolve (ec
, out dynamic);
8015 Type t
= ea
.Expr
.Type
;
8016 int rank
= ea
.Arguments
.Count
;
8017 if (t
.GetArrayRank () != rank
) {
8018 ec
.Report
.Error (22, ea
.Location
, "Wrong number of indexes `{0}' inside [], expected `{1}'",
8019 ea
.Arguments
.Count
.ToString (), t
.GetArrayRank ().ToString ());
8023 type
= TypeManager
.GetElementType (t
);
8024 if (type
.IsPointer
&& !ec
.IsUnsafe
) {
8025 UnsafeError (ec
, ea
.Location
);
8028 foreach (Argument a
in ea
.Arguments
) {
8029 if (a
is NamedArgument
)
8030 ElementAccess
.Error_NamedArgument ((NamedArgument
) a
, ec
.Report
);
8032 a
.Expr
= ConvertExpressionToArrayIndex (ec
, a
.Expr
);
8035 eclass
= ExprClass
.Variable
;
8041 /// Emits the right opcode to load an object of Type `t'
8042 /// from an array of T
8044 void EmitLoadOpcode (ILGenerator ig
, Type type
, int rank
)
8047 MethodInfo
get = FetchGetMethod ();
8048 ig
.Emit (OpCodes
.Call
, get);
8052 if (type
== TypeManager
.byte_type
|| type
== TypeManager
.bool_type
)
8053 ig
.Emit (OpCodes
.Ldelem_U1
);
8054 else if (type
== TypeManager
.sbyte_type
)
8055 ig
.Emit (OpCodes
.Ldelem_I1
);
8056 else if (type
== TypeManager
.short_type
)
8057 ig
.Emit (OpCodes
.Ldelem_I2
);
8058 else if (type
== TypeManager
.ushort_type
|| type
== TypeManager
.char_type
)
8059 ig
.Emit (OpCodes
.Ldelem_U2
);
8060 else if (type
== TypeManager
.int32_type
)
8061 ig
.Emit (OpCodes
.Ldelem_I4
);
8062 else if (type
== TypeManager
.uint32_type
)
8063 ig
.Emit (OpCodes
.Ldelem_U4
);
8064 else if (type
== TypeManager
.uint64_type
)
8065 ig
.Emit (OpCodes
.Ldelem_I8
);
8066 else if (type
== TypeManager
.int64_type
)
8067 ig
.Emit (OpCodes
.Ldelem_I8
);
8068 else if (type
== TypeManager
.float_type
)
8069 ig
.Emit (OpCodes
.Ldelem_R4
);
8070 else if (type
== TypeManager
.double_type
)
8071 ig
.Emit (OpCodes
.Ldelem_R8
);
8072 else if (type
== TypeManager
.intptr_type
)
8073 ig
.Emit (OpCodes
.Ldelem_I
);
8074 else if (TypeManager
.IsEnumType (type
)){
8075 EmitLoadOpcode (ig
, TypeManager
.GetEnumUnderlyingType (type
), rank
);
8076 } else if (TypeManager
.IsStruct (type
)){
8077 ig
.Emit (OpCodes
.Ldelema
, type
);
8078 ig
.Emit (OpCodes
.Ldobj
, type
);
8079 } else if (type
.IsGenericParameter
) {
8080 ig
.Emit (OpCodes
.Ldelem
, type
);
8081 } else if (type
.IsPointer
)
8082 ig
.Emit (OpCodes
.Ldelem_I
);
8084 ig
.Emit (OpCodes
.Ldelem_Ref
);
8087 protected override void Error_NegativeArrayIndex (ResolveContext ec
, Location loc
)
8089 ec
.Report
.Warning (251, 2, loc
, "Indexing an array with a negative index (array indices always start at zero)");
8093 /// Returns the right opcode to store an object of Type `t'
8094 /// from an array of T.
8096 static public OpCode
GetStoreOpcode (Type t
, out bool is_stobj
, out bool has_type_arg
)
8098 has_type_arg
= false; is_stobj
= false;
8099 t
= TypeManager
.TypeToCoreType (t
);
8100 if (TypeManager
.IsEnumType (t
))
8101 t
= TypeManager
.GetEnumUnderlyingType (t
);
8102 if (t
== TypeManager
.byte_type
|| t
== TypeManager
.sbyte_type
||
8103 t
== TypeManager
.bool_type
)
8104 return OpCodes
.Stelem_I1
;
8105 else if (t
== TypeManager
.short_type
|| t
== TypeManager
.ushort_type
||
8106 t
== TypeManager
.char_type
)
8107 return OpCodes
.Stelem_I2
;
8108 else if (t
== TypeManager
.int32_type
|| t
== TypeManager
.uint32_type
)
8109 return OpCodes
.Stelem_I4
;
8110 else if (t
== TypeManager
.int64_type
|| t
== TypeManager
.uint64_type
)
8111 return OpCodes
.Stelem_I8
;
8112 else if (t
== TypeManager
.float_type
)
8113 return OpCodes
.Stelem_R4
;
8114 else if (t
== TypeManager
.double_type
)
8115 return OpCodes
.Stelem_R8
;
8116 else if (t
== TypeManager
.intptr_type
) {
8117 has_type_arg
= true;
8119 return OpCodes
.Stobj
;
8120 } else if (TypeManager
.IsStruct (t
)) {
8121 has_type_arg
= true;
8123 return OpCodes
.Stobj
;
8124 } else if (t
.IsGenericParameter
) {
8125 has_type_arg
= true;
8126 return OpCodes
.Stelem
;
8127 } else if (t
.IsPointer
)
8128 return OpCodes
.Stelem_I
;
8130 return OpCodes
.Stelem_Ref
;
8133 MethodInfo
FetchGetMethod ()
8135 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
8136 int arg_count
= ea
.Arguments
.Count
;
8137 Type
[] args
= new Type
[arg_count
];
8140 for (int i
= 0; i
< arg_count
; i
++){
8141 //args [i++] = a.Type;
8142 args
[i
] = TypeManager
.int32_type
;
8145 get = mb
.GetArrayMethod (
8146 ea
.Expr
.Type
, "Get",
8147 CallingConventions
.HasThis
|
8148 CallingConventions
.Standard
,
8154 MethodInfo
FetchAddressMethod ()
8156 ModuleBuilder mb
= RootContext
.ToplevelTypes
.Builder
;
8157 int arg_count
= ea
.Arguments
.Count
;
8158 Type
[] args
= new Type
[arg_count
];
8162 ret_type
= TypeManager
.GetReferenceType (type
);
8164 for (int i
= 0; i
< arg_count
; i
++){
8165 //args [i++] = a.Type;
8166 args
[i
] = TypeManager
.int32_type
;
8169 address
= mb
.GetArrayMethod (
8170 ea
.Expr
.Type
, "Address",
8171 CallingConventions
.HasThis
|
8172 CallingConventions
.Standard
,
8179 // Load the array arguments into the stack.
8181 void LoadArrayAndArguments (EmitContext ec
)
8185 for (int i
= 0; i
< ea
.Arguments
.Count
; ++i
) {
8186 ea
.Arguments
[i
].Emit (ec
);
8190 public void Emit (EmitContext ec
, bool leave_copy
)
8192 int rank
= ea
.Expr
.Type
.GetArrayRank ();
8193 ILGenerator ig
= ec
.ig
;
8196 LoadFromPtr (ig
, this.type
);
8198 LoadArrayAndArguments (ec
);
8199 EmitLoadOpcode (ig
, type
, rank
);
8203 ig
.Emit (OpCodes
.Dup
);
8204 temp
= new LocalTemporary (this.type
);
8209 public override void Emit (EmitContext ec
)
8214 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
8216 int rank
= ea
.Expr
.Type
.GetArrayRank ();
8217 ILGenerator ig
= ec
.ig
;
8218 Type t
= source
.Type
;
8219 prepared
= prepare_for_load
;
8222 AddressOf (ec
, AddressOp
.LoadStore
);
8223 ec
.ig
.Emit (OpCodes
.Dup
);
8225 LoadArrayAndArguments (ec
);
8229 bool is_stobj
, has_type_arg
;
8230 OpCode op
= GetStoreOpcode (t
, out is_stobj
, out has_type_arg
);
8234 // The stobj opcode used by value types will need
8235 // an address on the stack, not really an array/array
8239 ig
.Emit (OpCodes
.Ldelema
, t
);
8244 ec
.ig
.Emit (OpCodes
.Dup
);
8245 temp
= new LocalTemporary (this.type
);
8250 StoreFromPtr (ig
, t
);
8252 ig
.Emit (OpCodes
.Stobj
, t
);
8253 else if (has_type_arg
)
8260 ec
.ig
.Emit (OpCodes
.Dup
);
8261 temp
= new LocalTemporary (this.type
);
8266 StoreFromPtr (ig
, t
);
8268 int arg_count
= ea
.Arguments
.Count
;
8269 Type
[] args
= new Type
[arg_count
+ 1];
8270 for (int i
= 0; i
< arg_count
; i
++) {
8271 //args [i++] = a.Type;
8272 args
[i
] = TypeManager
.int32_type
;
8274 args
[arg_count
] = type
;
8276 MethodInfo
set = RootContext
.ToplevelTypes
.Builder
.GetArrayMethod (
8277 ea
.Expr
.Type
, "Set",
8278 CallingConventions
.HasThis
|
8279 CallingConventions
.Standard
,
8280 TypeManager
.void_type
, args
);
8282 ig
.Emit (OpCodes
.Call
, set);
8292 public void EmitNew (EmitContext ec
, New source
, bool leave_copy
)
8294 if (!source
.Emit (ec
, this)) {
8296 throw new NotImplementedException ();
8301 throw new NotImplementedException ();
8304 public void AddressOf (EmitContext ec
, AddressOp mode
)
8306 int rank
= ea
.Expr
.Type
.GetArrayRank ();
8307 ILGenerator ig
= ec
.ig
;
8309 LoadArrayAndArguments (ec
);
8312 ig
.Emit (OpCodes
.Ldelema
, type
);
8314 MethodInfo address
= FetchAddressMethod ();
8315 ig
.Emit (OpCodes
.Call
, address
);
8320 public SLE
.Expression
MakeAssignExpression (BuilderContext ctx
)
8322 return SLE
.Expression
.ArrayAccess (
8323 ea
.Expr
.MakeExpression (ctx
),
8324 Arguments
.MakeExpression (ea
.Arguments
, ctx
));
8328 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
8330 return SLE
.Expression
.ArrayIndex (
8331 ea
.Expr
.MakeExpression (ctx
),
8332 Arguments
.MakeExpression (ea
.Arguments
, ctx
));
8335 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
8337 type
= storey
.MutateType (type
);
8338 ea
.Expr
.Type
= storey
.MutateType (ea
.Expr
.Type
);
8343 /// Expressions that represent an indexer call.
8345 public class IndexerAccess
: Expression
, IDynamicAssign
8347 class IndexerMethodGroupExpr
: MethodGroupExpr
8349 public IndexerMethodGroupExpr (Indexers indexers
, Location loc
)
8352 Methods
= indexers
.Methods
.ToArray ();
8355 public override string Name
{
8361 protected override int GetApplicableParametersCount (MethodBase method
, AParametersCollection parameters
)
8364 // Here is the trick, decrease number of arguments by 1 when only
8365 // available property method is setter. This makes overload resolution
8366 // work correctly for indexers.
8369 if (method
.Name
[0] == 'g')
8370 return parameters
.Count
;
8372 return parameters
.Count
- 1;
8378 // Contains either property getter or setter
8379 public List
<MethodBase
> Methods
;
8380 public List
<PropertyInfo
> Properties
;
8386 void Append (Type caller_type
, MemberInfo
[] mi
)
8391 foreach (PropertyInfo property
in mi
) {
8392 MethodInfo accessor
= property
.GetGetMethod (true);
8393 if (accessor
== null)
8394 accessor
= property
.GetSetMethod (true);
8396 if (Methods
== null) {
8397 Methods
= new List
<MethodBase
> ();
8398 Properties
= new List
<PropertyInfo
> ();
8401 Methods
.Add (accessor
);
8402 Properties
.Add (property
);
8406 static MemberInfo
[] GetIndexersForTypeOrInterface (Type caller_type
, Type lookup_type
)
8408 string p_name
= TypeManager
.IndexerPropertyName (lookup_type
);
8410 return TypeManager
.MemberLookup (
8411 caller_type
, caller_type
, lookup_type
, MemberTypes
.Property
,
8412 BindingFlags
.Public
| BindingFlags
.Instance
|
8413 BindingFlags
.DeclaredOnly
, p_name
, null);
8416 public static Indexers
GetIndexersForType (Type caller_type
, Type lookup_type
)
8418 Indexers ix
= new Indexers ();
8420 if (TypeManager
.IsGenericParameter (lookup_type
)) {
8421 GenericConstraints gc
= TypeManager
.GetTypeParameterConstraints (lookup_type
);
8425 if (gc
.HasClassConstraint
) {
8426 Type class_contraint
= gc
.ClassConstraint
;
8427 while (class_contraint
!= TypeManager
.object_type
&& class_contraint
!= null) {
8428 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, class_contraint
));
8429 class_contraint
= class_contraint
.BaseType
;
8433 Type
[] ifaces
= gc
.InterfaceConstraints
;
8434 foreach (Type itype
in ifaces
)
8435 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, itype
));
8440 Type copy
= lookup_type
;
8441 while (copy
!= TypeManager
.object_type
&& copy
!= null){
8442 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, copy
));
8443 copy
= copy
.BaseType
;
8446 if (lookup_type
.IsInterface
) {
8447 Type
[] ifaces
= TypeManager
.GetInterfaces (lookup_type
);
8448 if (ifaces
!= null) {
8449 foreach (Type itype
in ifaces
)
8450 ix
.Append (caller_type
, GetIndexersForTypeOrInterface (caller_type
, itype
));
8459 // Points to our "data" repository
8461 MethodInfo
get, set;
8462 bool is_base_indexer
;
8464 LocalTemporary temp
;
8465 LocalTemporary prepared_value
;
8466 Expression set_expr
;
8468 protected Type indexer_type
;
8469 protected Type current_type
;
8470 protected Expression instance_expr
;
8471 protected Arguments arguments
;
8473 public IndexerAccess (ElementAccess ea
, Location loc
)
8474 : this (ea
.Expr
, false, loc
)
8476 this.arguments
= ea
.Arguments
;
8479 protected IndexerAccess (Expression instance_expr
, bool is_base_indexer
,
8482 this.instance_expr
= instance_expr
;
8483 this.is_base_indexer
= is_base_indexer
;
8487 static string GetAccessorName (bool isSet
)
8489 return isSet
? "set" : "get";
8492 public override Expression
CreateExpressionTree (ResolveContext ec
)
8494 Arguments args
= Arguments
.CreateForExpressionTree (ec
, arguments
,
8495 instance_expr
.CreateExpressionTree (ec
),
8496 new TypeOfMethod (get, loc
));
8498 return CreateExpressionFactoryCall (ec
, "Call", args
);
8501 protected virtual void CommonResolve (ResolveContext ec
)
8503 indexer_type
= instance_expr
.Type
;
8504 current_type
= ec
.CurrentType
;
8507 protected override Expression
DoResolve (ResolveContext ec
)
8509 return ResolveAccessor (ec
, null);
8512 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
8514 if (right_side
== EmptyExpression
.OutAccess
.Instance
) {
8515 right_side
.DoResolveLValue (ec
, this);
8519 // if the indexer returns a value type, and we try to set a field in it
8520 if (right_side
== EmptyExpression
.LValueMemberAccess
|| right_side
== EmptyExpression
.LValueMemberOutAccess
) {
8521 Error_CannotModifyIntermediateExpressionValue (ec
);
8524 return ResolveAccessor (ec
, right_side
);
8527 Expression
ResolveAccessor (ResolveContext ec
, Expression right_side
)
8535 arguments
.Resolve (ec
, out dynamic);
8537 if (TypeManager
.IsDynamicType (indexer_type
)) {
8542 ilist
= Indexers
.GetIndexersForType (current_type
, indexer_type
);
8543 if (ilist
.Methods
== null) {
8544 ec
.Report
.Error (21, loc
, "Cannot apply indexing with [] to an expression of type `{0}'",
8545 TypeManager
.CSharpName (indexer_type
));
8549 mg
= new IndexerMethodGroupExpr (ilist
, loc
);
8550 mg
= mg
.OverloadResolve (ec
, ref arguments
, false, loc
);
8556 Arguments args
= new Arguments (arguments
.Count
+ 1);
8557 if (is_base_indexer
) {
8558 ec
.Report
.Error (1972, loc
, "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
8560 args
.Add (new Argument (instance_expr
));
8562 args
.AddRange (arguments
);
8564 var expr
= new DynamicIndexBinder (args
, loc
);
8565 if (right_side
!= null)
8566 return expr
.ResolveLValue (ec
, right_side
);
8568 return expr
.Resolve (ec
);
8571 MethodInfo mi
= (MethodInfo
) mg
;
8572 PropertyInfo pi
= null;
8573 for (int i
= 0; i
< ilist
.Methods
.Count
; ++i
) {
8574 if (ilist
.Methods
[i
] == mi
) {
8575 pi
= (PropertyInfo
) ilist
.Properties
[i
];
8580 type
= TypeManager
.TypeToCoreType (pi
.PropertyType
);
8581 if (type
.IsPointer
&& !ec
.IsUnsafe
)
8582 UnsafeError (ec
, loc
);
8584 MethodInfo accessor
;
8585 if (right_side
== null) {
8586 accessor
= get = pi
.GetGetMethod (true);
8588 accessor
= set = pi
.GetSetMethod (true);
8589 if (accessor
== null && pi
.GetGetMethod (true) != null) {
8590 ec
.Report
.SymbolRelatedToPreviousError (pi
);
8591 ec
.Report
.Error (200, loc
, "The read only property or indexer `{0}' cannot be assigned to",
8592 TypeManager
.GetFullNameSignature (pi
));
8596 set_expr
= Convert
.ImplicitConversion (ec
, right_side
, type
, loc
);
8599 if (accessor
== null) {
8600 ec
.Report
.SymbolRelatedToPreviousError (pi
);
8601 ec
.Report
.Error (154, loc
, "The property or indexer `{0}' cannot be used in this context because it lacks a `{1}' accessor",
8602 TypeManager
.GetFullNameSignature (pi
), GetAccessorName (right_side
!= null));
8607 // Only base will allow this invocation to happen.
8609 if (accessor
.IsAbstract
&& this is BaseIndexerAccess
) {
8610 Error_CannotCallAbstractBase (ec
, TypeManager
.GetFullNameSignature (pi
));
8613 bool must_do_cs1540_check
;
8614 if (!IsAccessorAccessible (ec
.CurrentType
, accessor
, out must_do_cs1540_check
)) {
8616 set = pi
.GetSetMethod (true);
8618 get = pi
.GetGetMethod (true);
8620 if (set != null && get != null &&
8621 (set.Attributes
& MethodAttributes
.MemberAccessMask
) != (get.Attributes
& MethodAttributes
.MemberAccessMask
)) {
8622 ec
.Report
.SymbolRelatedToPreviousError (accessor
);
8623 ec
.Report
.Error (271, loc
, "The property or indexer `{0}' cannot be used in this context because a `{1}' accessor is inaccessible",
8624 TypeManager
.GetFullNameSignature (pi
), GetAccessorName (right_side
!= null));
8626 ec
.Report
.SymbolRelatedToPreviousError (pi
);
8627 ErrorIsInaccesible (loc
, TypeManager
.GetFullNameSignature (pi
), ec
.Report
);
8631 instance_expr
.CheckMarshalByRefAccess (ec
);
8632 eclass
= ExprClass
.IndexerAccess
;
8636 public override void Emit (EmitContext ec
)
8641 public void Emit (EmitContext ec
, bool leave_copy
)
8644 prepared_value
.Emit (ec
);
8646 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, get,
8647 arguments
, loc
, false, false);
8651 ec
.ig
.Emit (OpCodes
.Dup
);
8652 temp
= new LocalTemporary (Type
);
8658 // source is ignored, because we already have a copy of it from the
8659 // LValue resolution and we have already constructed a pre-cached
8660 // version of the arguments (ea.set_arguments);
8662 public void EmitAssign (EmitContext ec
, Expression source
, bool leave_copy
, bool prepare_for_load
)
8664 prepared
= prepare_for_load
;
8665 Expression
value = set_expr
;
8668 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, get,
8669 arguments
, loc
, true, false);
8671 prepared_value
= new LocalTemporary (type
);
8672 prepared_value
.Store (ec
);
8674 prepared_value
.Release (ec
);
8677 ec
.ig
.Emit (OpCodes
.Dup
);
8678 temp
= new LocalTemporary (Type
);
8681 } else if (leave_copy
) {
8682 temp
= new LocalTemporary (Type
);
8689 arguments
.Add (new Argument (value));
8691 Invocation
.EmitCall (ec
, is_base_indexer
, instance_expr
, set, arguments
, loc
, false, prepared
);
8699 public override string GetSignatureForError ()
8701 return TypeManager
.CSharpSignature (get != null ? get : set, false);
8705 public SLE
.Expression
MakeAssignExpression (BuilderContext ctx
)
8707 var value = new[] { set_expr.MakeExpression (ctx) }
;
8708 var args
= Arguments
.MakeExpression (arguments
, ctx
).Concat (value);
8710 return SLE
.Expression
.Block (
8711 SLE
.Expression
.Call (instance_expr
.MakeExpression (ctx
), set, args
),
8716 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
8718 var args
= Arguments
.MakeExpression (arguments
, ctx
);
8719 return SLE
.Expression
.Call (instance_expr
.MakeExpression (ctx
), get, args
);
8722 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
8725 get = storey
.MutateGenericMethod (get);
8727 set = storey
.MutateGenericMethod (set);
8729 instance_expr
.MutateHoistedGenericType (storey
);
8730 if (arguments
!= null)
8731 arguments
.MutateHoistedGenericType (storey
);
8733 type
= storey
.MutateType (type
);
8736 protected override void CloneTo (CloneContext clonectx
, Expression t
)
8738 IndexerAccess target
= (IndexerAccess
) t
;
8740 if (arguments
!= null)
8741 target
.arguments
= arguments
.Clone (clonectx
);
8743 if (instance_expr
!= null)
8744 target
.instance_expr
= instance_expr
.Clone (clonectx
);
8749 /// The base operator for method names
8751 public class BaseAccess
: Expression
{
8752 public readonly string Identifier
;
8755 public BaseAccess (string member
, Location l
)
8757 this.Identifier
= member
;
8761 public BaseAccess (string member
, TypeArguments args
, Location l
)
8767 public override Expression
CreateExpressionTree (ResolveContext ec
)
8769 throw new NotSupportedException ("ET");
8772 protected override Expression
DoResolve (ResolveContext ec
)
8774 Expression c
= CommonResolve (ec
);
8780 // MethodGroups use this opportunity to flag an error on lacking ()
8782 if (!(c
is MethodGroupExpr
))
8783 return c
.Resolve (ec
);
8787 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
8789 Expression c
= CommonResolve (ec
);
8795 // MethodGroups use this opportunity to flag an error on lacking ()
8797 if (! (c
is MethodGroupExpr
))
8798 return c
.DoResolveLValue (ec
, right_side
);
8803 Expression
CommonResolve (ResolveContext ec
)
8805 Expression member_lookup
;
8806 Type current_type
= ec
.CurrentType
;
8807 Type base_type
= current_type
.BaseType
;
8809 if (!This
.IsThisAvailable (ec
)) {
8811 ec
.Report
.Error (1511, loc
, "Keyword `base' is not available in a static method");
8813 ec
.Report
.Error (1512, loc
, "Keyword `base' is not available in the current context");
8818 member_lookup
= MemberLookup (ec
.Compiler
, ec
.CurrentType
, null, base_type
, Identifier
,
8819 AllMemberTypes
, AllBindingFlags
, loc
);
8820 if (member_lookup
== null) {
8821 Error_MemberLookupFailed (ec
, ec
.CurrentType
, base_type
, base_type
, Identifier
,
8822 null, AllMemberTypes
, AllBindingFlags
);
8829 left
= new TypeExpression (base_type
, loc
);
8831 left
= ec
.GetThis (loc
);
8833 MemberExpr me
= member_lookup
as MemberExpr
;
8835 if (member_lookup
is TypeExpression
){
8836 ec
.Report
.Error (582, loc
, "{0}: Can not reference a type through an expression, try `{1}' instead",
8837 Identifier
, member_lookup
.GetSignatureForError ());
8839 ec
.Report
.Error (582, loc
, "{0}: Can not reference a {1} through an expression",
8840 Identifier
, member_lookup
.ExprClassName
);
8846 me
= me
.ResolveMemberAccess (ec
, left
, loc
, null);
8853 me
.SetTypeArguments (ec
, args
);
8859 public override void Emit (EmitContext ec
)
8861 throw new Exception ("Should never be called");
8864 protected override void CloneTo (CloneContext clonectx
, Expression t
)
8866 BaseAccess target
= (BaseAccess
) t
;
8869 target
.args
= args
.Clone ();
8874 /// The base indexer operator
8876 public class BaseIndexerAccess
: IndexerAccess
{
8877 public BaseIndexerAccess (Arguments args
, Location loc
)
8878 : base (null, true, loc
)
8880 this.arguments
= args
;
8883 protected override void CommonResolve (ResolveContext ec
)
8885 instance_expr
= ec
.GetThis (loc
);
8887 current_type
= ec
.CurrentType
.BaseType
;
8888 indexer_type
= current_type
;
8891 public override Expression
CreateExpressionTree (ResolveContext ec
)
8893 MemberExpr
.Error_BaseAccessInExpressionTree (ec
, loc
);
8894 return base.CreateExpressionTree (ec
);
8899 /// This class exists solely to pass the Type around and to be a dummy
8900 /// that can be passed to the conversion functions (this is used by
8901 /// foreach implementation to typecast the object return value from
8902 /// get_Current into the proper type. All code has been generated and
8903 /// we only care about the side effect conversions to be performed
8905 /// This is also now used as a placeholder where a no-action expression
8906 /// is needed (the `New' class).
8908 public class EmptyExpression
: Expression
{
8909 public static readonly Expression Null
= new EmptyExpression ();
8911 public class OutAccess
: EmptyExpression
8913 public static readonly OutAccess Instance
= new OutAccess ();
8915 public override Expression
DoResolveLValue (ResolveContext rc
, Expression right_side
)
8917 rc
.Report
.Error (206, right_side
.Location
,
8918 "A property, indexer or dynamic member access may not be passed as `ref' or `out' parameter");
8924 public static readonly EmptyExpression LValueMemberAccess
= new EmptyExpression ();
8925 public static readonly EmptyExpression LValueMemberOutAccess
= new EmptyExpression ();
8926 public static readonly EmptyExpression UnaryAddress
= new EmptyExpression ();
8928 static EmptyExpression temp
= new EmptyExpression ();
8929 public static EmptyExpression
Grab ()
8931 EmptyExpression retval
= temp
== null ? new EmptyExpression () : temp
;
8936 public static void Release (EmptyExpression e
)
8943 // FIXME: Don't set to object
8944 type
= TypeManager
.object_type
;
8945 eclass
= ExprClass
.Value
;
8946 loc
= Location
.Null
;
8949 public EmptyExpression (Type t
)
8952 eclass
= ExprClass
.Value
;
8953 loc
= Location
.Null
;
8956 public override Expression
CreateExpressionTree (ResolveContext ec
)
8958 throw new NotSupportedException ("ET");
8961 protected override Expression
DoResolve (ResolveContext ec
)
8966 public override void Emit (EmitContext ec
)
8968 // nothing, as we only exist to not do anything.
8971 public override void EmitSideEffect (EmitContext ec
)
8976 // This is just because we might want to reuse this bad boy
8977 // instead of creating gazillions of EmptyExpressions.
8978 // (CanImplicitConversion uses it)
8980 public void SetType (Type t
)
8987 // Empty statement expression
8989 public sealed class EmptyExpressionStatement
: ExpressionStatement
8991 public static readonly EmptyExpressionStatement Instance
= new EmptyExpressionStatement ();
8993 private EmptyExpressionStatement ()
8995 loc
= Location
.Null
;
8998 public override Expression
CreateExpressionTree (ResolveContext ec
)
9003 public override void EmitStatement (EmitContext ec
)
9008 protected override Expression
DoResolve (ResolveContext ec
)
9010 eclass
= ExprClass
.Value
;
9011 type
= TypeManager
.object_type
;
9015 public override void Emit (EmitContext ec
)
9021 public class UserCast
: Expression
{
9025 public UserCast (MethodInfo method
, Expression source
, Location l
)
9027 this.method
= method
;
9028 this.source
= source
;
9029 type
= TypeManager
.TypeToCoreType (method
.ReturnType
);
9033 public Expression Source
{
9039 public override Expression
CreateExpressionTree (ResolveContext ec
)
9041 Arguments args
= new Arguments (3);
9042 args
.Add (new Argument (source
.CreateExpressionTree (ec
)));
9043 args
.Add (new Argument (new TypeOf (new TypeExpression (type
, loc
), loc
)));
9044 args
.Add (new Argument (new TypeOfMethod (method
, loc
)));
9045 return CreateExpressionFactoryCall (ec
, "Convert", args
);
9048 protected override Expression
DoResolve (ResolveContext ec
)
9050 ObsoleteAttribute oa
= AttributeTester
.GetMethodObsoleteAttribute (method
);
9052 AttributeTester
.Report_ObsoleteMessage (oa
, GetSignatureForError (), loc
, ec
.Report
);
9054 eclass
= ExprClass
.Value
;
9058 public override void Emit (EmitContext ec
)
9061 ec
.ig
.Emit (OpCodes
.Call
, method
);
9064 public override string GetSignatureForError ()
9066 return TypeManager
.CSharpSignature (method
);
9069 public override SLE
.Expression
MakeExpression (BuilderContext ctx
)
9071 return SLE
.Expression
.Convert (source
.MakeExpression (ctx
), type
, method
);
9074 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
9076 source
.MutateHoistedGenericType (storey
);
9077 method
= storey
.MutateGenericMethod (method
);
9082 // This class is used to "construct" the type during a typecast
9083 // operation. Since the Type.GetType class in .NET can parse
9084 // the type specification, we just use this to construct the type
9085 // one bit at a time.
9087 public class ComposedCast
: TypeExpr
{
9088 FullNamedExpression left
;
9091 public ComposedCast (FullNamedExpression left
, string dim
)
9092 : this (left
, dim
, left
.Location
)
9096 public ComposedCast (FullNamedExpression left
, string dim
, Location l
)
9103 protected override TypeExpr
DoResolveAsTypeStep (IMemberContext ec
)
9105 TypeExpr lexpr
= left
.ResolveAsTypeTerminal (ec
, false);
9109 Type ltype
= lexpr
.Type
;
9110 if ((dim
.Length
> 0) && (dim
[0] == '?')) {
9111 TypeExpr nullable
= new Nullable
.NullableType (lexpr
, loc
);
9113 nullable
= new ComposedCast (nullable
, dim
.Substring (1), loc
);
9114 return nullable
.ResolveAsTypeTerminal (ec
, false);
9117 if (dim
== "*" && !TypeManager
.VerifyUnmanaged (ec
.Compiler
, ltype
, loc
))
9120 if (dim
.Length
!= 0 && dim
[0] == '[') {
9121 if (TypeManager
.IsSpecialType (ltype
)) {
9122 ec
.Compiler
.Report
.Error (611, loc
, "Array elements cannot be of type `{0}'", TypeManager
.CSharpName (ltype
));
9126 if ((ltype
.Attributes
& Class
.StaticClassAttribute
) == Class
.StaticClassAttribute
) {
9127 ec
.Compiler
.Report
.SymbolRelatedToPreviousError (ltype
);
9128 ec
.Compiler
.Report
.Error (719, loc
, "Array elements cannot be of static type `{0}'",
9129 TypeManager
.CSharpName (ltype
));
9134 type
= TypeManager
.GetConstructedType (ltype
, dim
);
9139 throw new InternalErrorException ("Couldn't create computed type " + ltype
+ dim
);
9141 if (type
.IsPointer
&& !ec
.IsUnsafe
){
9142 UnsafeError (ec
.Compiler
.Report
, loc
);
9145 eclass
= ExprClass
.Type
;
9149 public override string GetSignatureForError ()
9151 return left
.GetSignatureForError () + dim
;
9154 public override TypeExpr
ResolveAsTypeTerminal (IMemberContext ec
, bool silent
)
9156 return ResolveAsBaseTerminal (ec
, silent
);
9160 public class FixedBufferPtr
: Expression
{
9163 public FixedBufferPtr (Expression array
, Type array_type
, Location l
)
9168 type
= TypeManager
.GetPointerType (array_type
);
9169 eclass
= ExprClass
.Value
;
9172 public override Expression
CreateExpressionTree (ResolveContext ec
)
9174 Error_PointerInsideExpressionTree (ec
);
9178 public override void Emit(EmitContext ec
)
9183 protected override Expression
DoResolve (ResolveContext ec
)
9186 // We are born fully resolved
9194 // This class is used to represent the address of an array, used
9195 // only by the Fixed statement, this generates "&a [0]" construct
9196 // for fixed (char *pa = a)
9198 public class ArrayPtr
: FixedBufferPtr
{
9201 public ArrayPtr (Expression array
, Type array_type
, Location l
):
9202 base (array
, array_type
, l
)
9204 this.array_type
= array_type
;
9207 public override void Emit (EmitContext ec
)
9211 ILGenerator ig
= ec
.ig
;
9212 IntLiteral
.EmitInt (ig
, 0);
9213 ig
.Emit (OpCodes
.Ldelema
, array_type
);
9218 // Encapsulates a conversion rules required for array indexes
9220 public class ArrayIndexCast
: TypeCast
9222 public ArrayIndexCast (Expression expr
)
9223 : base (expr
, TypeManager
.int32_type
)
9225 if (expr
.Type
== TypeManager
.int32_type
)
9226 throw new ArgumentException ("unnecessary array index conversion");
9229 public override Expression
CreateExpressionTree (ResolveContext ec
)
9231 using (ec
.Set (ResolveContext
.Options
.CheckedScope
)) {
9232 return base.CreateExpressionTree (ec
);
9236 public override void Emit (EmitContext ec
)
9240 var expr_type
= child
.Type
;
9242 if (expr_type
== TypeManager
.uint32_type
)
9243 ec
.ig
.Emit (OpCodes
.Conv_U
);
9244 else if (expr_type
== TypeManager
.int64_type
)
9245 ec
.ig
.Emit (OpCodes
.Conv_Ovf_I
);
9246 else if (expr_type
== TypeManager
.uint64_type
)
9247 ec
.ig
.Emit (OpCodes
.Conv_Ovf_I_Un
);
9249 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type
);
9252 public override bool GetAttributableValue (ResolveContext ec
, Type value_type
, out object value)
9254 return child
.GetAttributableValue (ec
, value_type
, out value);
9259 // Implements the `stackalloc' keyword
9261 public class StackAlloc
: Expression
{
9266 public StackAlloc (Expression type
, Expression count
, Location l
)
9273 public override Expression
CreateExpressionTree (ResolveContext ec
)
9275 throw new NotSupportedException ("ET");
9278 protected override Expression
DoResolve (ResolveContext ec
)
9280 count
= count
.Resolve (ec
);
9284 if (count
.Type
!= TypeManager
.uint32_type
){
9285 count
= Convert
.ImplicitConversionRequired (ec
, count
, TypeManager
.int32_type
, loc
);
9290 Constant c
= count
as Constant
;
9291 if (c
!= null && c
.IsNegative
) {
9292 ec
.Report
.Error (247, loc
, "Cannot use a negative size with stackalloc");
9295 if (ec
.HasAny (ResolveContext
.Options
.CatchScope
| ResolveContext
.Options
.FinallyScope
)) {
9296 ec
.Report
.Error (255, loc
, "Cannot use stackalloc in finally or catch");
9299 TypeExpr texpr
= t
.ResolveAsTypeTerminal (ec
, false);
9305 if (!TypeManager
.VerifyUnmanaged (ec
.Compiler
, otype
, loc
))
9308 type
= TypeManager
.GetPointerType (otype
);
9309 eclass
= ExprClass
.Value
;
9314 public override void Emit (EmitContext ec
)
9316 int size
= GetTypeSize (otype
);
9317 ILGenerator ig
= ec
.ig
;
9322 ig
.Emit (OpCodes
.Sizeof
, otype
);
9324 IntConstant
.EmitInt (ig
, size
);
9326 ig
.Emit (OpCodes
.Mul_Ovf_Un
);
9327 ig
.Emit (OpCodes
.Localloc
);
9330 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9332 StackAlloc target
= (StackAlloc
) t
;
9333 target
.count
= count
.Clone (clonectx
);
9334 target
.t
= t
.Clone (clonectx
);
9339 // An object initializer expression
9341 public class ElementInitializer
: Assign
9343 public readonly string Name
;
9345 public ElementInitializer (string name
, Expression initializer
, Location loc
)
9346 : base (null, initializer
, loc
)
9351 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9353 ElementInitializer target
= (ElementInitializer
) t
;
9354 target
.source
= source
.Clone (clonectx
);
9357 public override Expression
CreateExpressionTree (ResolveContext ec
)
9359 Arguments args
= new Arguments (2);
9360 FieldExpr fe
= target
as FieldExpr
;
9362 args
.Add (new Argument (fe
.CreateTypeOfExpression ()));
9364 args
.Add (new Argument (((PropertyExpr
)target
).CreateSetterTypeOfExpression ()));
9366 args
.Add (new Argument (source
.CreateExpressionTree (ec
)));
9367 return CreateExpressionFactoryCall (ec
,
9368 source
is CollectionOrObjectInitializers
? "ListBind" : "Bind",
9372 protected override Expression
DoResolve (ResolveContext ec
)
9375 return EmptyExpressionStatement
.Instance
;
9377 MemberExpr me
= MemberLookupFinal (ec
, ec
.CurrentInitializerVariable
.Type
, ec
.CurrentInitializerVariable
.Type
,
9378 Name
, MemberTypes
.Field
| MemberTypes
.Property
, BindingFlags
.Public
| BindingFlags
.Instance
, loc
) as MemberExpr
;
9384 me
.InstanceExpression
= ec
.CurrentInitializerVariable
;
9386 if (source
is CollectionOrObjectInitializers
) {
9387 Expression previous
= ec
.CurrentInitializerVariable
;
9388 ec
.CurrentInitializerVariable
= target
;
9389 source
= source
.Resolve (ec
);
9390 ec
.CurrentInitializerVariable
= previous
;
9394 eclass
= source
.eclass
;
9399 Expression expr
= base.DoResolve (ec
);
9404 // Ignore field initializers with default value
9406 Constant c
= source
as Constant
;
9407 if (c
!= null && c
.IsDefaultInitializer (type
) && target
.eclass
== ExprClass
.Variable
)
9408 return EmptyExpressionStatement
.Instance
.Resolve (ec
);
9413 protected override Expression
Error_MemberLookupFailed (ResolveContext ec
, Type type
, MemberInfo
[] members
)
9415 MemberInfo member
= members
[0];
9416 if (member
.MemberType
!= MemberTypes
.Property
&& member
.MemberType
!= MemberTypes
.Field
)
9417 ec
.Report
.Error (1913, loc
, "Member `{0}' cannot be initialized. An object " +
9418 "initializer may only be used for fields, or properties", TypeManager
.GetFullNameSignature (member
));
9420 ec
.Report
.Error (1914, loc
, " Static field or property `{0}' cannot be assigned in an object initializer",
9421 TypeManager
.GetFullNameSignature (member
));
9426 public override void EmitStatement (EmitContext ec
)
9428 if (source
is CollectionOrObjectInitializers
)
9431 base.EmitStatement (ec
);
9436 // A collection initializer expression
9438 class CollectionElementInitializer
: Invocation
9440 public class ElementInitializerArgument
: Argument
9442 public ElementInitializerArgument (Expression e
)
9448 sealed class AddMemberAccess
: MemberAccess
9450 public AddMemberAccess (Expression expr
, Location loc
)
9451 : base (expr
, "Add", loc
)
9455 protected override void Error_TypeDoesNotContainDefinition (ResolveContext ec
, Type type
, string name
)
9457 if (TypeManager
.HasElementType (type
))
9460 base.Error_TypeDoesNotContainDefinition (ec
, type
, name
);
9464 public CollectionElementInitializer (Expression argument
)
9465 : base (null, new Arguments (1))
9467 base.arguments
.Add (new ElementInitializerArgument (argument
));
9468 this.loc
= argument
.Location
;
9471 public CollectionElementInitializer (List
<Expression
> arguments
, Location loc
)
9472 : base (null, new Arguments (arguments
.Count
))
9474 foreach (Expression e
in arguments
)
9475 base.arguments
.Add (new ElementInitializerArgument (e
));
9480 public override Expression
CreateExpressionTree (ResolveContext ec
)
9482 Arguments args
= new Arguments (2);
9483 args
.Add (new Argument (mg
.CreateExpressionTree (ec
)));
9485 var expr_initializers
= new ArrayInitializer (arguments
.Count
, loc
);
9486 foreach (Argument a
in arguments
)
9487 expr_initializers
.Add (a
.CreateExpressionTree (ec
));
9489 args
.Add (new Argument (new ArrayCreation (
9490 CreateExpressionTypeExpression (ec
, loc
), "[]", expr_initializers
, loc
)));
9491 return CreateExpressionFactoryCall (ec
, "ElementInit", args
);
9494 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9496 CollectionElementInitializer target
= (CollectionElementInitializer
) t
;
9497 if (arguments
!= null)
9498 target
.arguments
= arguments
.Clone (clonectx
);
9501 protected override Expression
DoResolve (ResolveContext ec
)
9503 base.expr
= new AddMemberAccess (ec
.CurrentInitializerVariable
, loc
);
9505 return base.DoResolve (ec
);
9510 // A block of object or collection initializers
9512 public class CollectionOrObjectInitializers
: ExpressionStatement
9514 IList
<Expression
> initializers
;
9515 bool is_collection_initialization
;
9517 public static readonly CollectionOrObjectInitializers Empty
=
9518 new CollectionOrObjectInitializers (Array
.AsReadOnly (new Expression
[0]), Location
.Null
);
9520 public CollectionOrObjectInitializers (IList
<Expression
> initializers
, Location loc
)
9522 this.initializers
= initializers
;
9526 public bool IsEmpty
{
9528 return initializers
.Count
== 0;
9532 public bool IsCollectionInitializer
{
9534 return is_collection_initialization
;
9538 protected override void CloneTo (CloneContext clonectx
, Expression target
)
9540 CollectionOrObjectInitializers t
= (CollectionOrObjectInitializers
) target
;
9542 t
.initializers
= new List
<Expression
> (initializers
.Count
);
9543 foreach (var e
in initializers
)
9544 t
.initializers
.Add (e
.Clone (clonectx
));
9547 public override Expression
CreateExpressionTree (ResolveContext ec
)
9549 var expr_initializers
= new ArrayInitializer (initializers
.Count
, loc
);
9550 foreach (Expression e
in initializers
) {
9551 Expression expr
= e
.CreateExpressionTree (ec
);
9553 expr_initializers
.Add (expr
);
9556 return new ImplicitlyTypedArrayCreation ("[]", expr_initializers
, loc
);
9559 protected override Expression
DoResolve (ResolveContext ec
)
9561 List
<string> element_names
= null;
9562 for (int i
= 0; i
< initializers
.Count
; ++i
) {
9563 Expression initializer
= (Expression
) initializers
[i
];
9564 ElementInitializer element_initializer
= initializer
as ElementInitializer
;
9567 if (element_initializer
!= null) {
9568 element_names
= new List
<string> (initializers
.Count
);
9569 element_names
.Add (element_initializer
.Name
);
9570 } else if (initializer
is CompletingExpression
){
9571 initializer
.Resolve (ec
);
9572 throw new InternalErrorException ("This line should never be reached");
9574 if (!TypeManager
.ImplementsInterface (ec
.CurrentInitializerVariable
.Type
, TypeManager
.ienumerable_type
)) {
9575 ec
.Report
.Error (1922, loc
, "A field or property `{0}' cannot be initialized with a collection " +
9576 "object initializer because type `{1}' does not implement `{2}' interface",
9577 ec
.CurrentInitializerVariable
.GetSignatureForError (),
9578 TypeManager
.CSharpName (ec
.CurrentInitializerVariable
.Type
),
9579 TypeManager
.CSharpName (TypeManager
.ienumerable_type
));
9582 is_collection_initialization
= true;
9585 if (is_collection_initialization
!= (element_initializer
== null)) {
9586 ec
.Report
.Error (747, initializer
.Location
, "Inconsistent `{0}' member declaration",
9587 is_collection_initialization
? "collection initializer" : "object initializer");
9591 if (!is_collection_initialization
) {
9592 if (element_names
.Contains (element_initializer
.Name
)) {
9593 ec
.Report
.Error (1912, element_initializer
.Location
,
9594 "An object initializer includes more than one member `{0}' initialization",
9595 element_initializer
.Name
);
9597 element_names
.Add (element_initializer
.Name
);
9602 Expression e
= initializer
.Resolve (ec
);
9603 if (e
== EmptyExpressionStatement
.Instance
)
9604 initializers
.RemoveAt (i
--);
9606 initializers
[i
] = e
;
9609 type
= ec
.CurrentInitializerVariable
.Type
;
9610 if (is_collection_initialization
) {
9611 if (TypeManager
.HasElementType (type
)) {
9612 ec
.Report
.Error (1925, loc
, "Cannot initialize object of type `{0}' with a collection initializer",
9613 TypeManager
.CSharpName (type
));
9617 eclass
= ExprClass
.Variable
;
9621 public override void Emit (EmitContext ec
)
9626 public override void EmitStatement (EmitContext ec
)
9628 foreach (ExpressionStatement e
in initializers
)
9629 e
.EmitStatement (ec
);
9632 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
9634 foreach (Expression e
in initializers
)
9635 e
.MutateHoistedGenericType (storey
);
9640 // New expression with element/object initializers
9642 public class NewInitialize
: New
9645 // This class serves as a proxy for variable initializer target instances.
9646 // A real variable is assigned later when we resolve left side of an
9649 sealed class InitializerTargetExpression
: Expression
, IMemoryLocation
9651 NewInitialize new_instance
;
9653 public InitializerTargetExpression (NewInitialize newInstance
)
9655 this.type
= newInstance
.type
;
9656 this.loc
= newInstance
.loc
;
9657 this.eclass
= newInstance
.eclass
;
9658 this.new_instance
= newInstance
;
9661 public override Expression
CreateExpressionTree (ResolveContext ec
)
9663 // Should not be reached
9664 throw new NotSupportedException ("ET");
9667 protected override Expression
DoResolve (ResolveContext ec
)
9672 public override Expression
DoResolveLValue (ResolveContext ec
, Expression right_side
)
9677 public override void Emit (EmitContext ec
)
9679 Expression e
= (Expression
) new_instance
.instance
;
9683 #region IMemoryLocation Members
9685 public void AddressOf (EmitContext ec
, AddressOp mode
)
9687 new_instance
.instance
.AddressOf (ec
, mode
);
9693 CollectionOrObjectInitializers initializers
;
9694 IMemoryLocation instance
;
9696 public NewInitialize (Expression requested_type
, Arguments arguments
, CollectionOrObjectInitializers initializers
, Location l
)
9697 : base (requested_type
, arguments
, l
)
9699 this.initializers
= initializers
;
9702 protected override IMemoryLocation
EmitAddressOf (EmitContext ec
, AddressOp Mode
)
9704 instance
= base.EmitAddressOf (ec
, Mode
);
9706 if (!initializers
.IsEmpty
)
9707 initializers
.Emit (ec
);
9712 protected override void CloneTo (CloneContext clonectx
, Expression t
)
9714 base.CloneTo (clonectx
, t
);
9716 NewInitialize target
= (NewInitialize
) t
;
9717 target
.initializers
= (CollectionOrObjectInitializers
) initializers
.Clone (clonectx
);
9720 public override Expression
CreateExpressionTree (ResolveContext ec
)
9722 Arguments args
= new Arguments (2);
9723 args
.Add (new Argument (base.CreateExpressionTree (ec
)));
9724 if (!initializers
.IsEmpty
)
9725 args
.Add (new Argument (initializers
.CreateExpressionTree (ec
)));
9727 return CreateExpressionFactoryCall (ec
,
9728 initializers
.IsCollectionInitializer
? "ListInit" : "MemberInit",
9732 protected override Expression
DoResolve (ResolveContext ec
)
9734 Expression e
= base.DoResolve (ec
);
9738 Expression previous
= ec
.CurrentInitializerVariable
;
9739 ec
.CurrentInitializerVariable
= new InitializerTargetExpression (this);
9740 initializers
.Resolve (ec
);
9741 ec
.CurrentInitializerVariable
= previous
;
9745 public override bool Emit (EmitContext ec
, IMemoryLocation target
)
9747 bool left_on_stack
= base.Emit (ec
, target
);
9749 if (initializers
.IsEmpty
)
9750 return left_on_stack
;
9752 LocalTemporary temp
= target
as LocalTemporary
;
9754 if (!left_on_stack
) {
9755 VariableReference vr
= target
as VariableReference
;
9757 // FIXME: This still does not work correctly for pre-set variables
9758 if (vr
!= null && vr
.IsRef
)
9759 target
.AddressOf (ec
, AddressOp
.Load
);
9761 ((Expression
) target
).Emit (ec
);
9762 left_on_stack
= true;
9765 temp
= new LocalTemporary (type
);
9772 initializers
.Emit (ec
);
9774 if (left_on_stack
) {
9779 return left_on_stack
;
9782 public override bool HasInitializer
{
9784 return !initializers
.IsEmpty
;
9788 public override void MutateHoistedGenericType (AnonymousMethodStorey storey
)
9790 base.MutateHoistedGenericType (storey
);
9791 initializers
.MutateHoistedGenericType (storey
);
9795 public class NewAnonymousType
: New
9797 static readonly IList
<AnonymousTypeParameter
> EmptyParameters
= Array
.AsReadOnly (new AnonymousTypeParameter
[0]);
9799 List
<AnonymousTypeParameter
> parameters
;
9800 readonly TypeContainer parent
;
9801 AnonymousTypeClass anonymous_type
;
9803 public NewAnonymousType (List
<AnonymousTypeParameter
> parameters
, TypeContainer parent
, Location loc
)
9804 : base (null, null, loc
)
9806 this.parameters
= parameters
;
9807 this.parent
= parent
;
9810 protected override void CloneTo (CloneContext clonectx
, Expression target
)
9812 if (parameters
== null)
9815 NewAnonymousType t
= (NewAnonymousType
) target
;
9816 t
.parameters
= new List
<AnonymousTypeParameter
> (parameters
.Count
);
9817 foreach (AnonymousTypeParameter atp
in parameters
)
9818 t
.parameters
.Add ((AnonymousTypeParameter
) atp
.Clone (clonectx
));
9821 AnonymousTypeClass
CreateAnonymousType (ResolveContext ec
, IList
<AnonymousTypeParameter
> parameters
)
9823 AnonymousTypeClass type
= parent
.Module
.Compiled
.GetAnonymousType (parameters
);
9827 type
= AnonymousTypeClass
.Create (ec
.Compiler
, parent
, parameters
, loc
);
9834 if (ec
.Report
.Errors
== 0)
9837 parent
.Module
.Compiled
.AddAnonymousType (type
);
9841 public override Expression
CreateExpressionTree (ResolveContext ec
)
9843 if (parameters
== null)
9844 return base.CreateExpressionTree (ec
);
9846 var init
= new ArrayInitializer (parameters
.Count
, loc
);
9847 foreach (Property p
in anonymous_type
.Properties
)
9848 init
.Add (new TypeOfMethod (TypeBuilder
.GetMethod (type
, p
.GetBuilder
), loc
));
9850 var ctor_args
= new ArrayInitializer (Arguments
.Count
, loc
);
9851 foreach (Argument a
in Arguments
)
9852 ctor_args
.Add (a
.CreateExpressionTree (ec
));
9854 Arguments args
= new Arguments (3);
9855 args
.Add (new Argument (method
.CreateExpressionTree (ec
)));
9856 args
.Add (new Argument (new ArrayCreation (TypeManager
.expression_type_expr
, "[]", ctor_args
, loc
)));
9857 args
.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", init
, loc
)));
9859 return CreateExpressionFactoryCall (ec
, "New", args
);
9862 protected override Expression
DoResolve (ResolveContext ec
)
9864 if (ec
.HasSet (ResolveContext
.Options
.ConstantScope
)) {
9865 ec
.Report
.Error (836, loc
, "Anonymous types cannot be used in this expression");
9869 if (parameters
== null) {
9870 anonymous_type
= CreateAnonymousType (ec
, EmptyParameters
);
9871 RequestedType
= new TypeExpression (anonymous_type
.TypeBuilder
, loc
);
9872 return base.DoResolve (ec
);
9876 Arguments
= new Arguments (parameters
.Count
);
9877 TypeExpression
[] t_args
= new TypeExpression
[parameters
.Count
];
9878 for (int i
= 0; i
< parameters
.Count
; ++i
) {
9879 Expression e
= ((AnonymousTypeParameter
) parameters
[i
]).Resolve (ec
);
9885 Arguments
.Add (new Argument (e
));
9886 t_args
[i
] = new TypeExpression (e
.Type
, e
.Location
);
9892 anonymous_type
= CreateAnonymousType (ec
, parameters
);
9893 if (anonymous_type
== null)
9896 RequestedType
= new GenericTypeExpr (anonymous_type
.TypeBuilder
, new TypeArguments (t_args
), loc
);
9897 return base.DoResolve (ec
);
9901 public class AnonymousTypeParameter
: ShimExpression
9903 public readonly string Name
;
9905 public AnonymousTypeParameter (Expression initializer
, string name
, Location loc
)
9906 : base (initializer
)
9912 public AnonymousTypeParameter (Parameter parameter
)
9913 : base (new SimpleName (parameter
.Name
, parameter
.Location
))
9915 this.Name
= parameter
.Name
;
9916 this.loc
= parameter
.Location
;
9919 public override bool Equals (object o
)
9921 AnonymousTypeParameter other
= o
as AnonymousTypeParameter
;
9922 return other
!= null && Name
== other
.Name
;
9925 public override int GetHashCode ()
9927 return Name
.GetHashCode ();
9930 protected override Expression
DoResolve (ResolveContext ec
)
9932 Expression e
= expr
.Resolve (ec
);
9936 if (e
.eclass
== ExprClass
.MethodGroup
) {
9937 Error_InvalidInitializer (ec
, e
.ExprClassName
);
9942 if (type
== TypeManager
.void_type
|| type
== TypeManager
.null_type
||
9943 type
== InternalType
.AnonymousMethod
|| type
.IsPointer
) {
9944 Error_InvalidInitializer (ec
, e
.GetSignatureForError ());
9951 protected virtual void Error_InvalidInitializer (ResolveContext ec
, string initializer
)
9953 ec
.Report
.Error (828, loc
, "An anonymous type property `{0}' cannot be initialized with `{1}'",