Merge pull request #5014 from vkargov/vk-kasha
[mono.git] / mcs / mcs / expression.cs
1 //
2 // expression.cs: Expression representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 // Copyright 2011 Xamarin Inc.
11 //
12
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using SLE = System.Linq.Expressions;
17 using System.Text;
18
19 #if STATIC
20 using MetaType = IKVM.Reflection.Type;
21 using IKVM.Reflection;
22 using IKVM.Reflection.Emit;
23 #else
24 using MetaType = System.Type;
25 using System.Reflection;
26 using System.Reflection.Emit;
27 #endif
28
29 namespace Mono.CSharp
30 {
31         //
32         // This is an user operator expression, automatically created during
33         // resolve phase
34         //
35         public class UserOperatorCall : Expression {
36                 protected readonly Arguments arguments;
37                 protected readonly MethodSpec oper;
38                 readonly Func<ResolveContext, Expression, Expression> expr_tree;
39
40                 public UserOperatorCall (MethodSpec oper, Arguments args, Func<ResolveContext, Expression, Expression> expr_tree, Location loc)
41                 {
42                         this.oper = oper;
43                         this.arguments = args;
44                         this.expr_tree = expr_tree;
45
46                         type = oper.ReturnType;
47                         eclass = ExprClass.Value;
48                         this.loc = loc;
49                 }
50
51                 public override bool ContainsEmitWithAwait ()
52                 {
53                         return arguments.ContainsEmitWithAwait ();
54                 }
55
56                 public override Expression CreateExpressionTree (ResolveContext ec)
57                 {
58                         if (expr_tree != null)
59                                 return expr_tree (ec, new TypeOfMethod (oper, loc));
60
61                         Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
62                                 new NullLiteral (loc),
63                                 new TypeOfMethod (oper, loc));
64
65                         return CreateExpressionFactoryCall (ec, "Call", args);
66                 }
67
68                 protected override void CloneTo (CloneContext context, Expression target)
69                 {
70                         // Nothing to clone
71                 }
72                 
73                 protected override Expression DoResolve (ResolveContext ec)
74                 {
75                         //
76                         // We are born fully resolved
77                         //
78                         return this;
79                 }
80
81                 public override void Emit (EmitContext ec)
82                 {
83                         var call = new CallEmitter ();
84                         call.Emit (ec, oper, arguments, loc);
85                 }
86
87                 public override void FlowAnalysis (FlowAnalysisContext fc)
88                 {
89                         arguments.FlowAnalysis (fc);
90                 }
91
92                 public override SLE.Expression MakeExpression (BuilderContext ctx)
93                 {
94 #if STATIC
95                         return base.MakeExpression (ctx);
96 #else
97                         return SLE.Expression.Call ((MethodInfo) oper.GetMetaInfo (), Arguments.MakeExpression (arguments, ctx));
98 #endif
99                 }
100         }
101
102         public class ParenthesizedExpression : ShimExpression
103         {
104                 public ParenthesizedExpression (Expression expr, Location loc)
105                         : base (expr)
106                 {
107                         this.loc = loc;
108                 }
109
110                 protected override Expression DoResolve (ResolveContext rc)
111                 {
112                         Expression res = null;
113                         using (rc.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false)) {
114                                 res = expr.Resolve (rc);
115                         }
116
117                         var constant = res as Constant;
118                         if (constant != null && constant.IsLiteral) {
119                                 if (res is NullLiteral)
120                                         return res;
121                                 
122                                 return Constant.CreateConstantFromValue (res.Type, constant.GetValue (), expr.Location);
123                         }
124
125                         return res;
126                 }
127
128                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
129                 {
130                         return expr.DoResolveLValue (ec, right_side);
131                 }
132                 
133                 public override object Accept (StructuralVisitor visitor)
134                 {
135                         return visitor.Visit (this);
136                 }
137
138                 public override bool HasConditionalAccess ()
139                 {
140                         return false;
141                 }
142         }
143         
144         //
145         //   Unary implements unary expressions.
146         //
147         public class Unary : Expression
148         {
149                 public enum Operator : byte {
150                         UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
151                         AddressOf,  TOP
152                 }
153
154                 public readonly Operator Oper;
155                 public Expression Expr;
156                 ConvCast.Mode enum_conversion;
157
158                 public Unary (Operator op, Expression expr, Location loc)
159                 {
160                         Oper = op;
161                         Expr = expr;
162                         this.loc = loc;
163                 }
164
165                 // <summary>
166                 //   This routine will attempt to simplify the unary expression when the
167                 //   argument is a constant.
168                 // </summary>
169                 Constant TryReduceConstant (ResolveContext ec, Constant constant)
170                 {
171                         var e = constant;
172
173                         while (e is EmptyConstantCast)
174                                 e = ((EmptyConstantCast) e).child;
175                         
176                         if (e is SideEffectConstant) {
177                                 Constant r = TryReduceConstant (ec, ((SideEffectConstant) e).value);
178                                 return r == null ? null : new SideEffectConstant (r, e, r.Location);
179                         }
180
181                         TypeSpec expr_type = e.Type;
182                         
183                         switch (Oper){
184                         case Operator.UnaryPlus:
185                                 // Unary numeric promotions
186                                 switch (expr_type.BuiltinType) {
187                                 case BuiltinTypeSpec.Type.Byte:
188                                         return new IntConstant (ec.BuiltinTypes, ((ByteConstant) e).Value, e.Location);
189                                 case BuiltinTypeSpec.Type.SByte:
190                                         return new IntConstant (ec.BuiltinTypes, ((SByteConstant) e).Value, e.Location);
191                                 case BuiltinTypeSpec.Type.Short:
192                                         return new IntConstant (ec.BuiltinTypes, ((ShortConstant) e).Value, e.Location);
193                                 case BuiltinTypeSpec.Type.UShort:
194                                         return new IntConstant (ec.BuiltinTypes, ((UShortConstant) e).Value, e.Location);
195                                 case BuiltinTypeSpec.Type.Char:
196                                         return new IntConstant (ec.BuiltinTypes, ((CharConstant) e).Value, e.Location);
197                                 
198                                 // Predefined operators
199                                 case BuiltinTypeSpec.Type.Int:
200                                 case BuiltinTypeSpec.Type.UInt:
201                                 case BuiltinTypeSpec.Type.Long:
202                                 case BuiltinTypeSpec.Type.ULong:
203                                 case BuiltinTypeSpec.Type.Float:
204                                 case BuiltinTypeSpec.Type.Double:
205                                 case BuiltinTypeSpec.Type.Decimal:
206                                         return e;
207                                 }
208                                 
209                                 return null;
210                                 
211                         case Operator.UnaryNegation:
212                                 // Unary numeric promotions
213                                 switch (expr_type.BuiltinType) {
214                                 case BuiltinTypeSpec.Type.Byte:
215                                         return new IntConstant (ec.BuiltinTypes, -((ByteConstant) e).Value, e.Location);
216                                 case BuiltinTypeSpec.Type.SByte:
217                                         return new IntConstant (ec.BuiltinTypes, -((SByteConstant) e).Value, e.Location);
218                                 case BuiltinTypeSpec.Type.Short:
219                                         return new IntConstant (ec.BuiltinTypes, -((ShortConstant) e).Value, e.Location);
220                                 case BuiltinTypeSpec.Type.UShort:
221                                         return new IntConstant (ec.BuiltinTypes, -((UShortConstant) e).Value, e.Location);
222                                 case BuiltinTypeSpec.Type.Char:
223                                         return new IntConstant (ec.BuiltinTypes, -((CharConstant) e).Value, e.Location);
224
225                                 // Predefined operators
226                                 case BuiltinTypeSpec.Type.Int:
227                                         int ivalue = ((IntConstant) e).Value;
228                                         if (ivalue == int.MinValue) {
229                                                 if (ec.ConstantCheckState) {
230                                                         ConstantFold.Error_CompileTimeOverflow (ec, loc);
231                                                         return null;
232                                                 }
233                                                 return e;
234                                         }
235                                         return new IntConstant (ec.BuiltinTypes, -ivalue, e.Location);
236
237                                 case BuiltinTypeSpec.Type.Long:
238                                         long lvalue = ((LongConstant) e).Value;
239                                         if (lvalue == long.MinValue) {
240                                                 if (ec.ConstantCheckState) {
241                                                         ConstantFold.Error_CompileTimeOverflow (ec, loc);
242                                                         return null;
243                                                 }
244                                                 return e;
245                                         }
246                                         return new LongConstant (ec.BuiltinTypes, -lvalue, e.Location);
247
248                                 case BuiltinTypeSpec.Type.UInt:
249                                         UIntLiteral uil = constant as UIntLiteral;
250                                         if (uil != null) {
251                                                 if (uil.Value == int.MaxValue + (uint) 1)
252                                                         return new IntLiteral (ec.BuiltinTypes, int.MinValue, e.Location);
253                                                 return new LongLiteral (ec.BuiltinTypes, -uil.Value, e.Location);
254                                         }
255                                         return new LongConstant (ec.BuiltinTypes, -((UIntConstant) e).Value, e.Location);
256
257
258                                 case BuiltinTypeSpec.Type.ULong:
259                                         ULongLiteral ull = constant as ULongLiteral;
260                                         if (ull != null && ull.Value == 9223372036854775808)
261                                                 return new LongLiteral (ec.BuiltinTypes, long.MinValue, e.Location);
262                                         return null;
263
264                                 case BuiltinTypeSpec.Type.Float:
265                                         FloatLiteral fl = constant as FloatLiteral;
266                                         // For better error reporting
267                                         if (fl != null)
268                                                 return new FloatLiteral (ec.BuiltinTypes, -fl.Value, e.Location);
269
270                                         return new FloatConstant (ec.BuiltinTypes, -((FloatConstant) e).Value, e.Location);
271
272                                 case BuiltinTypeSpec.Type.Double:
273                                         DoubleLiteral dl = constant as DoubleLiteral;
274                                         // For better error reporting
275                                         if (dl != null)
276                                                 return new DoubleLiteral (ec.BuiltinTypes, -dl.Value, e.Location);
277
278                                         return new DoubleConstant (ec.BuiltinTypes, -((DoubleConstant) e).Value, e.Location);
279
280                                 case BuiltinTypeSpec.Type.Decimal:
281                                         return new DecimalConstant (ec.BuiltinTypes, -((DecimalConstant) e).Value, e.Location);
282                                 }
283
284                                 return null;
285                                 
286                         case Operator.LogicalNot:
287                                 if (expr_type.BuiltinType != BuiltinTypeSpec.Type.Bool)
288                                         return null;
289                                 
290                                 bool b = (bool)e.GetValue ();
291                                 return new BoolConstant (ec.BuiltinTypes, !b, e.Location);
292                                 
293                         case Operator.OnesComplement:
294                                 // Unary numeric promotions
295                                 switch (expr_type.BuiltinType) {
296                                 case BuiltinTypeSpec.Type.Byte:
297                                         return new IntConstant (ec.BuiltinTypes, ~((ByteConstant) e).Value, e.Location);
298                                 case BuiltinTypeSpec.Type.SByte:
299                                         return new IntConstant (ec.BuiltinTypes, ~((SByteConstant) e).Value, e.Location);
300                                 case BuiltinTypeSpec.Type.Short:
301                                         return new IntConstant (ec.BuiltinTypes, ~((ShortConstant) e).Value, e.Location);
302                                 case BuiltinTypeSpec.Type.UShort:
303                                         return new IntConstant (ec.BuiltinTypes, ~((UShortConstant) e).Value, e.Location);
304                                 case BuiltinTypeSpec.Type.Char:
305                                         return new IntConstant (ec.BuiltinTypes, ~((CharConstant) e).Value, e.Location);
306                                 
307                                 // Predefined operators
308                                 case BuiltinTypeSpec.Type.Int:
309                                         return new IntConstant (ec.BuiltinTypes, ~((IntConstant)e).Value, e.Location);
310                                 case BuiltinTypeSpec.Type.UInt:
311                                         return new UIntConstant (ec.BuiltinTypes, ~((UIntConstant) e).Value, e.Location);
312                                 case BuiltinTypeSpec.Type.Long:
313                                         return new LongConstant (ec.BuiltinTypes, ~((LongConstant) e).Value, e.Location);
314                                 case BuiltinTypeSpec.Type.ULong:
315                                         return new ULongConstant (ec.BuiltinTypes, ~((ULongConstant) e).Value, e.Location);
316                                 }
317                                 if (e is EnumConstant) {
318                                         var res = TryReduceConstant (ec, ((EnumConstant)e).Child);
319                                         if (res != null) {
320                                                 //
321                                                 // Numeric promotion upgraded types to int but for enum constant
322                                                 // original underlying constant type is needed
323                                                 //
324                                                 if (res.Type.BuiltinType == BuiltinTypeSpec.Type.Int) {
325                                                         int v = ((IntConstant) res).Value;
326                                                         switch (((EnumConstant) e).Child.Type.BuiltinType) {
327                                                                 case BuiltinTypeSpec.Type.UShort:
328                                                                 res = new UShortConstant (ec.BuiltinTypes, (ushort) v, e.Location);
329                                                                 break;
330                                                                 case BuiltinTypeSpec.Type.Short:
331                                                                 res = new ShortConstant (ec.BuiltinTypes, (short) v, e.Location);
332                                                                 break;
333                                                                 case BuiltinTypeSpec.Type.Byte:
334                                                                 res = new ByteConstant (ec.BuiltinTypes, (byte) v, e.Location);
335                                                                 break;
336                                                                 case BuiltinTypeSpec.Type.SByte:
337                                                                 res = new SByteConstant (ec.BuiltinTypes, (sbyte) v, e.Location);
338                                                                 break;
339                                                         }
340                                                 }
341
342                                                 res = new EnumConstant (res, expr_type);
343                                         }
344                                         return res;
345                                 }
346                                 return null;
347                         }
348                         throw new Exception ("Can not constant fold: " + Oper.ToString());
349                 }
350                 
351                 protected virtual Expression ResolveOperator (ResolveContext ec, Expression expr)
352                 {
353                         eclass = ExprClass.Value;
354
355                         TypeSpec expr_type = expr.Type;
356                         Expression best_expr;
357
358                         TypeSpec[] predefined = ec.BuiltinTypes.OperatorsUnary [(int) Oper];
359
360                         //
361                         // Primitive types first
362                         //
363                         if (BuiltinTypeSpec.IsPrimitiveType (expr_type)) {
364                                 best_expr = ResolvePrimitivePredefinedType (ec, expr, predefined);
365                                 if (best_expr == null)
366                                         return null;
367
368                                 type = best_expr.Type;
369                                 Expr = best_expr;
370                                 return this;
371                         }
372
373                         //
374                         // E operator ~(E x);
375                         //
376                         if (Oper == Operator.OnesComplement && expr_type.IsEnum)
377                                 return ResolveEnumOperator (ec, expr, predefined);
378
379                         return ResolveUserType (ec, expr, predefined);
380                 }
381
382                 protected virtual Expression ResolveEnumOperator (ResolveContext ec, Expression expr, TypeSpec[] predefined)
383                 {
384                         TypeSpec underlying_type = EnumSpec.GetUnderlyingType (expr.Type);
385                         Expression best_expr = ResolvePrimitivePredefinedType (ec, EmptyCast.Create (expr, underlying_type), predefined);
386                         if (best_expr == null)
387                                 return null;
388
389                         Expr = best_expr;
390                         enum_conversion = Binary.GetEnumResultCast (underlying_type);
391                         type = expr.Type;
392                         return EmptyCast.Create (this, type);
393                 }
394
395                 public override bool ContainsEmitWithAwait ()
396                 {
397                         return Expr.ContainsEmitWithAwait ();
398                 }
399
400                 public override Expression CreateExpressionTree (ResolveContext ec)
401                 {
402                         return CreateExpressionTree (ec, null);
403                 }
404
405                 Expression CreateExpressionTree (ResolveContext ec, Expression user_op)
406                 {
407                         string method_name;
408                         switch (Oper) {
409                         case Operator.AddressOf:
410                                 Error_PointerInsideExpressionTree (ec);
411                                 return null;
412                         case Operator.UnaryNegation:
413                                 if (ec.HasSet (ResolveContext.Options.CheckedScope) && user_op == null && !IsFloat (type))
414                                         method_name = "NegateChecked";
415                                 else
416                                         method_name = "Negate";
417                                 break;
418                         case Operator.OnesComplement:
419                         case Operator.LogicalNot:
420                                 method_name = "Not";
421                                 break;
422                         case Operator.UnaryPlus:
423                                 method_name = "UnaryPlus";
424                                 break;
425                         default:
426                                 throw new InternalErrorException ("Unknown unary operator " + Oper.ToString ());
427                         }
428
429                         Arguments args = new Arguments (2);
430                         args.Add (new Argument (Expr.CreateExpressionTree (ec)));
431                         if (user_op != null)
432                                 args.Add (new Argument (user_op));
433
434                         return CreateExpressionFactoryCall (ec, method_name, args);
435                 }
436
437                 public static TypeSpec[][] CreatePredefinedOperatorsTable (BuiltinTypes types)
438                 {
439                         var predefined_operators = new TypeSpec[(int) Operator.TOP][];
440
441                         //
442                         // 7.6.1 Unary plus operator
443                         //
444                         predefined_operators [(int) Operator.UnaryPlus] = new TypeSpec [] {
445                                 types.Int, types.UInt,
446                                 types.Long, types.ULong,
447                                 types.Float, types.Double,
448                                 types.Decimal
449                         };
450
451                         //
452                         // 7.6.2 Unary minus operator
453                         //
454                         predefined_operators [(int) Operator.UnaryNegation] = new TypeSpec [] {
455                                 types.Int,  types.Long,
456                                 types.Float, types.Double,
457                                 types.Decimal
458                         };
459
460                         //
461                         // 7.6.3 Logical negation operator
462                         //
463                         predefined_operators [(int) Operator.LogicalNot] = new TypeSpec [] {
464                                 types.Bool
465                         };
466
467                         //
468                         // 7.6.4 Bitwise complement operator
469                         //
470                         predefined_operators [(int) Operator.OnesComplement] = new TypeSpec [] {
471                                 types.Int, types.UInt,
472                                 types.Long, types.ULong
473                         };
474
475                         return predefined_operators;
476                 }
477
478                 //
479                 // Unary numeric promotions
480                 //
481                 static Expression DoNumericPromotion (ResolveContext rc, Operator op, Expression expr)
482                 {
483                         TypeSpec expr_type = expr.Type;
484                         if (op == Operator.UnaryPlus || op == Operator.UnaryNegation || op == Operator.OnesComplement) {
485                                 switch (expr_type.BuiltinType) {
486                                 case BuiltinTypeSpec.Type.Byte:
487                                 case BuiltinTypeSpec.Type.SByte:
488                                 case BuiltinTypeSpec.Type.Short:
489                                 case BuiltinTypeSpec.Type.UShort:
490                                 case BuiltinTypeSpec.Type.Char:
491                                         return Convert.ImplicitNumericConversion (expr, rc.BuiltinTypes.Int);
492                                 }
493                         }
494
495                         if (op == Operator.UnaryNegation && expr_type.BuiltinType == BuiltinTypeSpec.Type.UInt)
496                                 return Convert.ImplicitNumericConversion (expr, rc.BuiltinTypes.Long);
497
498                         return expr;
499                 }
500
501                 protected override Expression DoResolve (ResolveContext ec)
502                 {
503                         if (Oper == Operator.AddressOf) {
504                                 return ResolveAddressOf (ec);
505                         }
506
507                         Expr = Expr.Resolve (ec);
508                         if (Expr == null)
509                                 return null;
510
511                         if (Expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
512                                 Arguments args = new Arguments (1);
513                                 args.Add (new Argument (Expr));
514                                 return new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc).Resolve (ec);
515                         }
516
517                         if (Expr.Type.IsNullableType)
518                                 return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
519
520                         //
521                         // Attempt to use a constant folding operation.
522                         //
523                         Constant cexpr = Expr as Constant;
524                         if (cexpr != null) {
525                                 cexpr = TryReduceConstant (ec, cexpr);
526                                 if (cexpr != null)
527                                         return cexpr;
528                         }
529
530                         Expression expr = ResolveOperator (ec, Expr);
531                         if (expr == null)
532                                 Error_OperatorCannotBeApplied (ec, loc, OperName (Oper), Expr.Type);
533                         
534                         //
535                         // Reduce unary operator on predefined types
536                         //
537                         if (expr == this && Oper == Operator.UnaryPlus)
538                                 return Expr;
539
540                         return expr;
541                 }
542
543                 public override Expression DoResolveLValue (ResolveContext ec, Expression right)
544                 {
545                         return null;
546                 }
547
548                 public override void Emit (EmitContext ec)
549                 {
550                         EmitOperator (ec, type);
551                 }
552
553                 protected void EmitOperator (EmitContext ec, TypeSpec type)
554                 {
555                         switch (Oper) {
556                         case Operator.UnaryPlus:
557                                 Expr.Emit (ec);
558                                 break;
559                                 
560                         case Operator.UnaryNegation:
561                                 if (ec.HasSet (EmitContext.Options.CheckedScope) && !IsFloat (type)) {
562                                         if (ec.HasSet (BuilderContext.Options.AsyncBody) && Expr.ContainsEmitWithAwait ())
563                                                 Expr = Expr.EmitToField (ec);
564
565                                         ec.EmitInt (0);
566                                         if (type.BuiltinType == BuiltinTypeSpec.Type.Long)
567                                                 ec.Emit (OpCodes.Conv_U8);
568                                         Expr.Emit (ec);
569                                         ec.Emit (OpCodes.Sub_Ovf);
570                                 } else {
571                                         Expr.Emit (ec);
572                                         ec.Emit (OpCodes.Neg);
573                                 }
574                                 
575                                 break;
576                                 
577                         case Operator.LogicalNot:
578                                 Expr.Emit (ec);
579                                 ec.EmitInt (0);
580                                 ec.Emit (OpCodes.Ceq);
581                                 break;
582                                 
583                         case Operator.OnesComplement:
584                                 Expr.Emit (ec);
585                                 ec.Emit (OpCodes.Not);
586                                 break;
587                                 
588                         case Operator.AddressOf:
589                                 ((IMemoryLocation)Expr).AddressOf (ec, AddressOp.LoadStore);
590                                 break;
591                                 
592                         default:
593                                 throw new Exception ("This should not happen: Operator = "
594                                                      + Oper.ToString ());
595                         }
596
597                         //
598                         // Same trick as in Binary expression
599                         //
600                         if (enum_conversion != 0) {
601                                 using (ec.With (BuilderContext.Options.CheckedScope, false)) {
602                                         ConvCast.Emit (ec, enum_conversion);
603                                 }
604                         }
605                 }
606
607                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
608                 {
609                         if (Oper == Operator.LogicalNot)
610                                 Expr.EmitBranchable (ec, target, !on_true);
611                         else
612                                 base.EmitBranchable (ec, target, on_true);
613                 }
614
615                 public override void EmitSideEffect (EmitContext ec)
616                 {
617                         Expr.EmitSideEffect (ec);
618                 }
619
620                 public static void Error_Ambiguous (ResolveContext rc, string oper, TypeSpec type, Location loc)
621                 {
622                         rc.Report.Error (35, loc, "Operator `{0}' is ambiguous on an operand of type `{1}'",
623                                 oper, type.GetSignatureForError ());
624                 }
625
626                 public override void FlowAnalysis (FlowAnalysisContext fc)
627                 {
628                         FlowAnalysis (fc, false);
629                 }
630
631                 public override void FlowAnalysisConditional (FlowAnalysisContext fc)
632                 {
633                         FlowAnalysis (fc, true);
634                 }
635
636                 void FlowAnalysis (FlowAnalysisContext fc, bool conditional)
637                 {
638                         if (Oper == Operator.AddressOf) {
639                                 var vr = Expr as VariableReference;
640                                 if (vr != null && vr.VariableInfo != null)
641                                         fc.SetVariableAssigned (vr.VariableInfo);
642
643                                 return;
644                         }
645
646                         if (Oper == Operator.LogicalNot && conditional) {
647                                 Expr.FlowAnalysisConditional (fc);
648
649                                 var temp = fc.DefiniteAssignmentOnTrue;
650                                 fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse;
651                                 fc.DefiniteAssignmentOnFalse = temp;
652                         } else {
653                                 Expr.FlowAnalysis (fc);
654                         }
655                 }
656
657                 //
658                 // Converts operator to System.Linq.Expressions.ExpressionType enum name
659                 //
660                 string GetOperatorExpressionTypeName ()
661                 {
662                         switch (Oper) {
663                         case Operator.OnesComplement:
664                                 return "OnesComplement";
665                         case Operator.LogicalNot:
666                                 return "Not";
667                         case Operator.UnaryNegation:
668                                 return "Negate";
669                         case Operator.UnaryPlus:
670                                 return "UnaryPlus";
671                         default:
672                                 throw new NotImplementedException ("Unknown express type operator " + Oper.ToString ());
673                         }
674                 }
675
676                 static bool IsFloat (TypeSpec t)
677                 {
678                         return t.BuiltinType == BuiltinTypeSpec.Type.Double || t.BuiltinType == BuiltinTypeSpec.Type.Float;
679                 }
680
681                 //
682                 // Returns a stringified representation of the Operator
683                 //
684                 public static string OperName (Operator oper)
685                 {
686                         switch (oper) {
687                         case Operator.UnaryPlus:
688                                 return "+";
689                         case Operator.UnaryNegation:
690                                 return "-";
691                         case Operator.LogicalNot:
692                                 return "!";
693                         case Operator.OnesComplement:
694                                 return "~";
695                         case Operator.AddressOf:
696                                 return "&";
697                         }
698
699                         throw new NotImplementedException (oper.ToString ());
700                 }
701
702                 public override SLE.Expression MakeExpression (BuilderContext ctx)
703                 {
704                         var expr = Expr.MakeExpression (ctx);
705                         bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
706
707                         switch (Oper) {
708                         case Operator.UnaryNegation:
709                                 return is_checked ? SLE.Expression.NegateChecked (expr) : SLE.Expression.Negate (expr);
710                         case Operator.LogicalNot:
711                                 return SLE.Expression.Not (expr);
712                         case Operator.OnesComplement:
713                                 return SLE.Expression.OnesComplement (expr);
714                         default:
715                                 throw new NotImplementedException (Oper.ToString ());
716                         }
717                 }
718
719                 Expression ResolveAddressOf (ResolveContext ec)
720                 {
721                         if (ec.CurrentIterator != null) {
722                                 UnsafeInsideIteratorError (ec, loc);
723                         } else if (!ec.IsUnsafe) {
724                                 UnsafeError (ec, loc);
725                         }
726
727                         Expr = Expr.DoResolveLValue (ec, EmptyExpression.UnaryAddress);
728                         if (Expr == null || Expr.eclass != ExprClass.Variable) {
729                                 ec.Report.Error (211, loc, "Cannot take the address of the given expression");
730                                 return null;
731                         }
732
733                         if (!TypeManager.VerifyUnmanaged (ec.Module, Expr.Type, loc)) {
734                                 return null;
735                         }
736
737                         IVariableReference vr = Expr as IVariableReference;
738                         bool is_fixed;
739                         if (vr != null) {
740                                 is_fixed = vr.IsFixed;
741                                 vr.SetHasAddressTaken ();
742
743                                 if (vr.IsHoisted && ec.CurrentIterator == null) {
744                                         AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, vr, loc);
745                                 }
746                         } else {
747                                 IFixedExpression fe = Expr as IFixedExpression;
748                                 is_fixed = fe != null && fe.IsFixed;
749                         }
750
751                         if (!is_fixed && !ec.HasSet (ResolveContext.Options.FixedInitializerScope)) {
752                                 ec.Report.Error (212, loc, "You can only take the address of unfixed expression inside of a fixed statement initializer");
753                         }
754
755                         type = PointerContainer.MakeType (ec.Module, Expr.Type);
756                         eclass = ExprClass.Value;
757                         return this;
758                 }
759
760                 Expression ResolvePrimitivePredefinedType (ResolveContext rc, Expression expr, TypeSpec[] predefined)
761                 {
762                         expr = DoNumericPromotion (rc, Oper, expr);
763                         TypeSpec expr_type = expr.Type;
764                         foreach (TypeSpec t in predefined) {
765                                 if (t == expr_type)
766                                         return expr;
767                         }
768                         return null;
769                 }
770
771                 //
772                 // Perform user-operator overload resolution
773                 //
774                 protected virtual Expression ResolveUserOperator (ResolveContext ec, Expression expr)
775                 {
776                         CSharp.Operator.OpType op_type;
777                         switch (Oper) {
778                         case Operator.LogicalNot:
779                                 op_type = CSharp.Operator.OpType.LogicalNot; break;
780                         case Operator.OnesComplement:
781                                 op_type = CSharp.Operator.OpType.OnesComplement; break;
782                         case Operator.UnaryNegation:
783                                 op_type = CSharp.Operator.OpType.UnaryNegation; break;
784                         case Operator.UnaryPlus:
785                                 op_type = CSharp.Operator.OpType.UnaryPlus; break;
786                         default:
787                                 throw new InternalErrorException (Oper.ToString ());
788                         }
789
790                         var methods = MemberCache.GetUserOperator (expr.Type, op_type, false);
791                         if (methods == null)
792                                 return null;
793
794                         Arguments args = new Arguments (1);
795                         args.Add (new Argument (expr));
796
797                         var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
798                         var oper = res.ResolveOperator (ec, ref args);
799
800                         if (oper == null)
801                                 return null;
802
803                         Expr = args [0].Expr;
804                         return new UserOperatorCall (oper, args, CreateExpressionTree, expr.Location);
805                 }
806
807                 //
808                 // Unary user type overload resolution
809                 //
810                 Expression ResolveUserType (ResolveContext ec, Expression expr, TypeSpec[] predefined)
811                 {
812                         Expression best_expr = ResolveUserOperator (ec, expr);
813                         if (best_expr != null)
814                                 return best_expr;
815
816                         foreach (TypeSpec t in predefined) {
817                                 Expression oper_expr = Convert.ImplicitUserConversion (ec, expr, t, expr.Location);
818                                 if (oper_expr == null)
819                                         continue;
820
821                                 if (oper_expr == ErrorExpression.Instance)
822                                         return oper_expr;
823
824                                 //
825                                 // decimal type is predefined but has user-operators
826                                 //
827                                 if (oper_expr.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal)
828                                         oper_expr = ResolveUserType (ec, oper_expr, predefined);
829                                 else
830                                         oper_expr = ResolvePrimitivePredefinedType (ec, oper_expr, predefined);
831
832                                 if (oper_expr == null)
833                                         continue;
834
835                                 if (best_expr == null) {
836                                         best_expr = oper_expr;
837                                         continue;
838                                 }
839
840                                 int result = OverloadResolver.BetterTypeConversion (ec, best_expr.Type, t);
841                                 if (result == 0) {
842                                         if ((oper_expr is UserOperatorCall || oper_expr is UserCast) && (best_expr is UserOperatorCall || best_expr is UserCast)) {
843                                                 Error_Ambiguous (ec, OperName (Oper), expr.Type, loc);
844                                         } else {
845                                                 Error_OperatorCannotBeApplied (ec, loc, OperName (Oper), expr.Type);
846                                         }
847
848                                         break;
849                                 }
850
851                                 if (result == 2)
852                                         best_expr = oper_expr;
853                         }
854                         
855                         if (best_expr == null)
856                                 return null;
857                         
858                         //
859                         // HACK: Decimal user-operator is included in standard operators
860                         //
861                         if (best_expr.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal)
862                                 return best_expr;
863
864                         Expr = best_expr;
865                         type = best_expr.Type;
866                         return this;                    
867                 }
868
869                 protected override void CloneTo (CloneContext clonectx, Expression t)
870                 {
871                         Unary target = (Unary) t;
872
873                         target.Expr = Expr.Clone (clonectx);
874                 }
875                 
876                 public override object Accept (StructuralVisitor visitor)
877                 {
878                         return visitor.Visit (this);
879                 }
880
881         }
882
883         //
884         // Unary operators are turned into Indirection expressions
885         // after semantic analysis (this is so we can take the address
886         // of an indirection).
887         //
888         public class Indirection : Expression, IMemoryLocation, IAssignMethod, IFixedExpression {
889                 Expression expr;
890                 LocalTemporary temporary;
891                 bool prepared;
892                 
893                 public Indirection (Expression expr, Location l)
894                 {
895                         this.expr = expr;
896                         loc = l;
897                 }
898
899                 public Expression Expr {
900                         get {
901                                 return expr;
902                         }
903                 }
904
905                 public bool IsFixed {
906                         get { return true; }
907                 }
908
909                 public override Location StartLocation {
910                         get {
911                                 return expr.StartLocation;
912                         }
913                 }
914
915                 protected override void CloneTo (CloneContext clonectx, Expression t)
916                 {
917                         Indirection target = (Indirection) t;
918                         target.expr = expr.Clone (clonectx);
919                 }
920
921                 public override bool ContainsEmitWithAwait ()
922                 {
923                         throw new NotImplementedException ();
924                 }
925
926                 public override Expression CreateExpressionTree (ResolveContext ec)
927                 {
928                         Error_PointerInsideExpressionTree (ec);
929                         return null;
930                 }
931                 
932                 public override void Emit (EmitContext ec)
933                 {
934                         if (!prepared)
935                                 expr.Emit (ec);
936                         
937                         ec.EmitLoadFromPtr (Type);
938                 }
939
940                 public void Emit (EmitContext ec, bool leave_copy)
941                 {
942                         Emit (ec);
943                         if (leave_copy) {
944                                 ec.Emit (OpCodes.Dup);
945                                 temporary = new LocalTemporary (expr.Type);
946                                 temporary.Store (ec);
947                         }
948                 }
949                 
950                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
951                 {
952                         prepared = isCompound;
953                         
954                         expr.Emit (ec);
955
956                         if (isCompound)
957                                 ec.Emit (OpCodes.Dup);
958                         
959                         source.Emit (ec);
960                         if (leave_copy) {
961                                 ec.Emit (OpCodes.Dup);
962                                 temporary = new LocalTemporary (source.Type);
963                                 temporary.Store (ec);
964                         }
965                         
966                         ec.EmitStoreFromPtr (type);
967                         
968                         if (temporary != null) {
969                                 temporary.Emit (ec);
970                                 temporary.Release (ec);
971                         }
972                 }
973                 
974                 public void AddressOf (EmitContext ec, AddressOp Mode)
975                 {
976                         expr.Emit (ec);
977                 }
978
979                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
980                 {
981                         return DoResolve (ec);
982                 }
983
984                 protected override Expression DoResolve (ResolveContext ec)
985                 {
986                         expr = expr.Resolve (ec);
987                         if (expr == null)
988                                 return null;
989
990                         if (ec.CurrentIterator != null) {
991                                 UnsafeInsideIteratorError (ec, loc);
992                         } else if (!ec.IsUnsafe) {
993                                 UnsafeError (ec, loc);
994                         }
995
996                         var pc = expr.Type as PointerContainer;
997
998                         if (pc == null) {
999                                 ec.Report.Error (193, loc, "The * or -> operator must be applied to a pointer");
1000                                 return null;
1001                         }
1002
1003                         type = pc.Element;
1004
1005                         if (type.Kind == MemberKind.Void) {
1006                                 Error_VoidPointerOperation (ec);
1007                                 return null;
1008                         }
1009
1010                         eclass = ExprClass.Variable;
1011                         return this;
1012                 }
1013
1014                 public override object Accept (StructuralVisitor visitor)
1015                 {
1016                         return visitor.Visit (this);
1017                 }
1018         }
1019         
1020         /// <summary>
1021         ///   Unary Mutator expressions (pre and post ++ and --)
1022         /// </summary>
1023         ///
1024         /// <remarks>
1025         ///   UnaryMutator implements ++ and -- expressions.   It derives from
1026         ///   ExpressionStatement becuase the pre/post increment/decrement
1027         ///   operators can be used in a statement context.
1028         ///
1029         /// FIXME: Idea, we could split this up in two classes, one simpler
1030         /// for the common case, and one with the extra fields for more complex
1031         /// classes (indexers require temporary access;  overloaded require method)
1032         ///
1033         /// </remarks>
1034         public class UnaryMutator : ExpressionStatement
1035         {
1036                 class DynamicPostMutator : Expression, IAssignMethod
1037                 {
1038                         LocalTemporary temp;
1039                         Expression expr;
1040
1041                         public DynamicPostMutator (Expression expr)
1042                         {
1043                                 this.expr = expr;
1044                                 this.type = expr.Type;
1045                                 this.loc = expr.Location;
1046                         }
1047
1048                         public override Expression CreateExpressionTree (ResolveContext ec)
1049                         {
1050                                 throw new NotImplementedException ("ET");
1051                         }
1052
1053                         protected override Expression DoResolve (ResolveContext rc)
1054                         {
1055                                 eclass = expr.eclass;
1056                                 return this;
1057                         }
1058
1059                         public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
1060                         {
1061                                 expr.DoResolveLValue (ec, right_side);
1062                                 return DoResolve (ec);
1063                         }
1064
1065                         public override void Emit (EmitContext ec)
1066                         {
1067                                 temp.Emit (ec);
1068                         }
1069
1070                         public void Emit (EmitContext ec, bool leave_copy)
1071                         {
1072                                 throw new NotImplementedException ();
1073                         }
1074
1075                         //
1076                         // Emits target assignment using unmodified source value
1077                         //
1078                         public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
1079                         {
1080                                 //
1081                                 // Allocate temporary variable to keep original value before it's modified
1082                                 //
1083                                 temp = new LocalTemporary (type);
1084                                 expr.Emit (ec);
1085                                 temp.Store (ec);
1086
1087                                 ((IAssignMethod) expr).EmitAssign (ec, source, false, isCompound);
1088
1089                                 if (leave_copy)
1090                                         Emit (ec);
1091
1092                                 temp.Release (ec);
1093                                 temp = null;
1094                         }
1095                 }
1096
1097                 [Flags]
1098                 public enum Mode : byte {
1099                         IsIncrement    = 0,
1100                         IsDecrement    = 1,
1101                         IsPre          = 0,
1102                         IsPost         = 2,
1103                         
1104                         PreIncrement   = 0,
1105                         PreDecrement   = IsDecrement,
1106                         PostIncrement  = IsPost,
1107                         PostDecrement  = IsPost | IsDecrement
1108                 }
1109
1110                 Mode mode;
1111                 bool is_expr, recurse;
1112
1113                 protected Expression expr;
1114
1115                 // Holds the real operation
1116                 Expression operation;
1117
1118                 public UnaryMutator (Mode m, Expression e, Location loc)
1119                 {
1120                         mode = m;
1121                         this.loc = loc;
1122                         expr = e;
1123                 }
1124
1125                 public Mode UnaryMutatorMode {
1126                         get {
1127                                 return mode;
1128                         }
1129                 }
1130                 
1131                 public Expression Expr {
1132                         get {
1133                                 return expr;
1134                         }
1135                 }
1136
1137                 public override Location StartLocation {
1138                         get {
1139                                 return (mode & Mode.IsPost) != 0 ? expr.Location : loc;
1140                         }
1141                 }
1142
1143                 public override bool ContainsEmitWithAwait ()
1144                 {
1145                         return expr.ContainsEmitWithAwait ();
1146                 }
1147
1148                 public override Expression CreateExpressionTree (ResolveContext ec)
1149                 {
1150                         return new SimpleAssign (this, this).CreateExpressionTree (ec);
1151                 }
1152
1153                 public static TypeSpec[] CreatePredefinedOperatorsTable (BuiltinTypes types)
1154                 {
1155                         //
1156                         // Predefined ++ and -- operators exist for the following types: 
1157                         // sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal
1158                         //
1159                         return new TypeSpec[] {
1160                                 types.Int,
1161                                 types.Long,
1162
1163                                 types.SByte,
1164                                 types.Byte,
1165                                 types.Short,
1166                                 types.UInt,
1167                                 types.ULong,
1168                                 types.Char,
1169                                 types.Float,
1170                                 types.Double,
1171                                 types.Decimal
1172                         };
1173                 }
1174
1175                 protected override Expression DoResolve (ResolveContext ec)
1176                 {
1177                         expr = expr.Resolve (ec);
1178                         
1179                         if (expr == null || expr.Type == InternalType.ErrorType)
1180                                 return null;
1181
1182                         if (expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1183                                 //
1184                                 // Handle postfix unary operators using local
1185                                 // temporary variable
1186                                 //
1187                                 if ((mode & Mode.IsPost) != 0)
1188                                         expr = new DynamicPostMutator (expr);
1189
1190                                 Arguments args = new Arguments (1);
1191                                 args.Add (new Argument (expr));
1192                                 return new SimpleAssign (expr, new DynamicUnaryConversion (GetOperatorExpressionTypeName (), args, loc)).Resolve (ec);
1193                         }
1194
1195                         if (expr.Type.IsNullableType)
1196                                 return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
1197
1198                         return DoResolveOperation (ec);
1199                 }
1200
1201                 protected Expression DoResolveOperation (ResolveContext ec)
1202                 {
1203                         eclass = ExprClass.Value;
1204                         type = expr.Type;
1205
1206                         if (expr is RuntimeValueExpression) {
1207                                 operation = expr;
1208                         } else {
1209                                 // Use itself at the top of the stack
1210                                 operation = new EmptyExpression (type);
1211                         }
1212
1213                         //
1214                         // The operand of the prefix/postfix increment decrement operators
1215                         // should be an expression that is classified as a variable,
1216                         // a property access or an indexer access
1217                         //
1218                         // TODO: Move to parser, expr is ATypeNameExpression
1219                         if (expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.IndexerAccess || expr.eclass == ExprClass.PropertyAccess) {
1220                                 expr = expr.ResolveLValue (ec, expr);
1221                         } else {
1222                                 ec.Report.Error (1059, loc, "The operand of an increment or decrement operator must be a variable, property or indexer");
1223                         }
1224
1225                         //
1226                         // Step 1: Try to find a user operator, it has priority over predefined ones
1227                         //
1228                         var user_op = IsDecrement ? Operator.OpType.Decrement : Operator.OpType.Increment;
1229                         var methods = MemberCache.GetUserOperator (type, user_op, false);
1230
1231                         if (methods != null) {
1232                                 Arguments args = new Arguments (1);
1233                                 args.Add (new Argument (expr));
1234
1235                                 var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
1236                                 var method = res.ResolveOperator (ec, ref args);
1237                                 if (method == null)
1238                                         return null;
1239
1240                                 args[0].Expr = operation;
1241                                 operation = new UserOperatorCall (method, args, null, loc);
1242                                 operation = Convert.ImplicitConversionRequired (ec, operation, type, loc);
1243                                 return this;
1244                         }
1245
1246                         //
1247                         // Step 2: Try predefined types
1248                         //
1249
1250                         Expression source = null;
1251                         bool primitive_type;
1252
1253                         //
1254                         // Predefined without user conversion first for speed-up
1255                         //
1256                         // Predefined ++ and -- operators exist for the following types: 
1257                         // sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal
1258                         //
1259                         switch (type.BuiltinType) {
1260                         case BuiltinTypeSpec.Type.Byte:
1261                         case BuiltinTypeSpec.Type.SByte:
1262                         case BuiltinTypeSpec.Type.Short:
1263                         case BuiltinTypeSpec.Type.UShort:
1264                         case BuiltinTypeSpec.Type.Int:
1265                         case BuiltinTypeSpec.Type.UInt:
1266                         case BuiltinTypeSpec.Type.Long:
1267                         case BuiltinTypeSpec.Type.ULong:
1268                         case BuiltinTypeSpec.Type.Char:
1269                         case BuiltinTypeSpec.Type.Float:
1270                         case BuiltinTypeSpec.Type.Double:
1271                         case BuiltinTypeSpec.Type.Decimal:
1272                                 source = operation;
1273                                 primitive_type = true;
1274                                 break;
1275                         default:
1276                                 primitive_type = false;
1277
1278                                 // ++/-- on pointer variables of all types except void*
1279                                 if (type.IsPointer) {
1280                                         if (((PointerContainer) type).Element.Kind == MemberKind.Void) {
1281                                                 Error_VoidPointerOperation (ec);
1282                                                 return null;
1283                                         }
1284
1285                                         source = operation;
1286                                 } else {
1287                                         Expression best_source = null;
1288                                         foreach (var t in ec.BuiltinTypes.OperatorsUnaryMutator) {
1289                                                 source = Convert.ImplicitUserConversion (ec, operation, t, loc);
1290
1291                                                 // LAMESPEC: It should error on ambiguous operators but that would make us incompatible
1292                                                 if (source == null)
1293                                                         continue;
1294
1295                                                 if (best_source == null) {
1296                                                         best_source = source;
1297                                                         continue;
1298                                                 }
1299
1300                                                 var better = OverloadResolver.BetterTypeConversion (ec, best_source.Type, source.Type);
1301                                                 if (better == 1)
1302                                                         continue;
1303
1304                                                 if (better == 2) {
1305                                                         best_source = source;
1306                                                         continue;
1307                                                 }
1308
1309                                                 Unary.Error_Ambiguous (ec, OperName (mode), type, loc);
1310                                                 break;
1311                                         }
1312
1313                                         source = best_source;
1314                                 }
1315
1316                                 // ++/-- on enum types
1317                                 if (source == null && type.IsEnum)
1318                                         source = operation;
1319
1320                                 if (source == null) {
1321                                         expr.Error_OperatorCannotBeApplied (ec, loc, Operator.GetName (user_op), type);
1322                                         return null;
1323                                 }
1324
1325                                 break;
1326                         }
1327
1328                         var one = new IntConstant (ec.BuiltinTypes, 1, loc);
1329                         var op = IsDecrement ? Binary.Operator.Subtraction : Binary.Operator.Addition;
1330                         operation = new Binary (op, source, one);
1331                         operation = operation.Resolve (ec);
1332                         if (operation == null)
1333                                 throw new NotImplementedException ("should not be reached");
1334
1335                         if (operation.Type != type) {
1336                                 if (primitive_type)
1337                                         operation = Convert.ExplicitNumericConversion (ec, operation, type);
1338                                 else
1339                                         operation = Convert.ImplicitConversionRequired (ec, operation, type, loc);
1340                         }
1341
1342                         return this;
1343                 }
1344
1345                 void EmitCode (EmitContext ec, bool is_expr)
1346                 {
1347                         recurse = true;
1348                         this.is_expr = is_expr;
1349                         ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
1350                 }
1351
1352                 public override void Emit (EmitContext ec)
1353                 {
1354                         //
1355                         // We use recurse to allow ourselfs to be the source
1356                         // of an assignment. This little hack prevents us from
1357                         // having to allocate another expression
1358                         //
1359                         if (recurse) {
1360                                 ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
1361
1362                                 EmitOperation (ec);
1363
1364                                 recurse = false;
1365                                 return;
1366                         }
1367
1368                         EmitCode (ec, true);
1369                 }
1370
1371                 protected virtual void EmitOperation (EmitContext ec)
1372                 {
1373                         operation.Emit (ec);
1374                 }
1375
1376                 public override void EmitStatement (EmitContext ec)
1377                 {
1378                         EmitCode (ec, false);
1379                 }
1380
1381                 public override void FlowAnalysis (FlowAnalysisContext fc)
1382                 {
1383                         expr.FlowAnalysis (fc);
1384                 }
1385
1386                 //
1387                 // Converts operator to System.Linq.Expressions.ExpressionType enum name
1388                 //
1389                 string GetOperatorExpressionTypeName ()
1390                 {
1391                         return IsDecrement ? "Decrement" : "Increment";
1392                 }
1393
1394                 bool IsDecrement {
1395                         get { return (mode & Mode.IsDecrement) != 0; }
1396                 }
1397
1398
1399                 public override SLE.Expression MakeExpression (BuilderContext ctx)
1400                 {
1401                         var target = ((RuntimeValueExpression) expr).MetaObject.Expression;
1402                         var source = SLE.Expression.Convert (operation.MakeExpression (ctx), target.Type);
1403                         return SLE.Expression.Assign (target, source);
1404                 }
1405
1406                 public static string OperName (Mode oper)
1407                 {
1408                         return (oper & Mode.IsDecrement) != 0 ? "--" : "++";
1409                 }
1410
1411                 protected override void CloneTo (CloneContext clonectx, Expression t)
1412                 {
1413                         UnaryMutator target = (UnaryMutator) t;
1414
1415                         target.expr = expr.Clone (clonectx);
1416                 }
1417
1418                 public override object Accept (StructuralVisitor visitor)
1419                 {
1420                         return visitor.Visit (this);
1421                 }
1422
1423         }
1424
1425         //
1426         // Base class for the `is' and `as' operators
1427         //
1428         public abstract class Probe : Expression
1429         {
1430                 public Expression ProbeType;
1431                 protected Expression expr;
1432                 protected TypeSpec probe_type_expr;
1433                 
1434                 protected Probe (Expression expr, Expression probe_type, Location l)
1435                 {
1436                         ProbeType = probe_type;
1437                         loc = l;
1438                         this.expr = expr;
1439                 }
1440
1441                 public Expression Expr {
1442                         get {
1443                                 return expr;
1444                         }
1445                 }
1446
1447                 public override bool ContainsEmitWithAwait ()
1448                 {
1449                         return expr.ContainsEmitWithAwait ();
1450                 }
1451
1452                 protected Expression ResolveCommon (ResolveContext rc)
1453                 {
1454                         expr = expr.Resolve (rc);
1455                         if (expr == null)
1456                                 return null;
1457
1458                         ResolveProbeType (rc);
1459                         if (probe_type_expr == null)
1460                                 return this;
1461
1462                         if (probe_type_expr.IsStatic) {
1463                                 rc.Report.Error (7023, loc, "The second operand of `is' or `as' operator cannot be static type `{0}'",
1464                                         probe_type_expr.GetSignatureForError ());
1465                                 return null;
1466                         }
1467                         
1468                         if (expr.Type.IsPointer || probe_type_expr.IsPointer) {
1469                                 rc.Report.Error (244, loc, "The `{0}' operator cannot be applied to an operand of pointer type",
1470                                         OperatorName);
1471                                 return null;
1472                         }
1473
1474                         if (expr.Type == InternalType.AnonymousMethod || expr.Type == InternalType.MethodGroup) {
1475                                 rc.Report.Error (837, loc, "The `{0}' operator cannot be applied to a lambda expression, anonymous method, or method group",
1476                                         OperatorName);
1477                                 return null;
1478                         }
1479
1480                         return this;
1481                 }
1482
1483                 protected virtual void ResolveProbeType (ResolveContext rc)
1484                 {
1485                         probe_type_expr = ProbeType.ResolveAsType (rc);
1486                 }
1487
1488                 public override void EmitSideEffect (EmitContext ec)
1489                 {
1490                         expr.EmitSideEffect (ec);
1491                 }
1492
1493                 public override void EmitPrepare (EmitContext ec)
1494                 {
1495                         expr.EmitPrepare (ec);
1496                 }
1497
1498                 public override void FlowAnalysis (FlowAnalysisContext fc)
1499                 {
1500                         expr.FlowAnalysis (fc);
1501                 }
1502
1503                 public override bool HasConditionalAccess ()
1504                 {
1505                         return expr.HasConditionalAccess ();
1506                 }
1507
1508                 protected abstract string OperatorName { get; }
1509
1510                 protected override void CloneTo (CloneContext clonectx, Expression t)
1511                 {
1512                         Probe target = (Probe) t;
1513
1514                         target.expr = expr.Clone (clonectx);
1515                         target.ProbeType = ProbeType.Clone (clonectx);
1516                 }
1517
1518         }
1519
1520         /// <summary>
1521         ///   Implementation of the `is' operator.
1522         /// </summary>
1523         public class Is : Probe
1524         {
1525                 Nullable.Unwrap expr_unwrap;
1526                 MethodSpec number_mg;
1527                 Arguments number_args;
1528
1529                 public Is (Expression expr, Expression probe_type, Location l)
1530                         : base (expr, probe_type, l)
1531                 {
1532                 }
1533
1534                 protected override string OperatorName {
1535                         get { return "is"; }
1536                 }
1537
1538                 public LocalVariable Variable { get; set; }
1539
1540                 public override Expression CreateExpressionTree (ResolveContext ec)
1541                 {
1542                         if (Variable != null)
1543                                 ec.Report.Error (8122, loc, "An expression tree cannot contain a pattern matching operator");
1544
1545                         Arguments args = Arguments.CreateForExpressionTree (ec, null,
1546                                 expr.CreateExpressionTree (ec),
1547                                 new TypeOf (probe_type_expr, loc));
1548
1549                         return CreateExpressionFactoryCall (ec, "TypeIs", args);
1550                 }
1551
1552                 Expression CreateConstantResult (ResolveContext rc, bool result)
1553                 {
1554                         if (result)
1555                                 rc.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
1556                                         probe_type_expr.GetSignatureForError ());
1557                         else
1558                                 rc.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
1559                                         probe_type_expr.GetSignatureForError ());
1560
1561                         var c = new BoolConstant (rc.BuiltinTypes, result, loc);
1562                         return expr.IsSideEffectFree ?
1563                                 ReducedExpression.Create (c, this) :
1564                                 new SideEffectConstant (c, this, loc);
1565                 }
1566
1567                 public override void Emit (EmitContext ec)
1568                 {
1569                         if (probe_type_expr == null) {
1570                                 if (ProbeType is WildcardPattern) {
1571                                         expr.EmitSideEffect (ec);
1572                                         ProbeType.Emit (ec);
1573                                 } else {
1574                                         EmitPatternMatch (ec);
1575                                 }
1576                                 return;
1577                         }
1578
1579                         EmitLoad (ec);
1580
1581                         if (expr_unwrap == null) {
1582                                 ec.EmitNull ();
1583                                 ec.Emit (OpCodes.Cgt_Un);
1584                         }
1585                 }
1586
1587                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
1588                 {
1589                         if (probe_type_expr == null) {
1590                                 EmitPatternMatch (ec);
1591                         } else {
1592                                 EmitLoad (ec);
1593                         }
1594
1595                         ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
1596                 }
1597
1598                 public override void EmitPrepare (EmitContext ec)
1599                 {
1600                         base.EmitPrepare (ec);
1601
1602                         if (Variable != null)
1603                                 Variable.CreateBuilder (ec);
1604                 }
1605
1606                 void EmitPatternMatch (EmitContext ec)
1607                 {
1608                         var no_match = ec.DefineLabel ();
1609                         var end = ec.DefineLabel ();
1610
1611                         if (expr_unwrap != null) {
1612                                 expr_unwrap.EmitCheck (ec);
1613
1614                                 if (ProbeType.IsNull) {
1615                                         ec.EmitInt (0);
1616                                         ec.Emit (OpCodes.Ceq);
1617                                         return;
1618                                 }
1619
1620                                 ec.Emit (OpCodes.Brfalse_S, no_match);
1621                                 expr_unwrap.Emit (ec);
1622                                 ProbeType.Emit (ec);
1623                                 ec.Emit (OpCodes.Ceq);
1624                                 ec.Emit (OpCodes.Br_S, end);
1625                                 ec.MarkLabel (no_match);
1626                                 ec.EmitInt (0);
1627                                 ec.MarkLabel (end);
1628                                 return;
1629                         }
1630
1631                         if (number_args != null && number_args.Count == 3) {
1632                                 var ce = new CallEmitter ();
1633                                 ce.Emit (ec, number_mg, number_args, loc);
1634                                 return;
1635                         }
1636
1637                         var probe_type = ProbeType.Type;
1638
1639                         Expr.Emit (ec);
1640                         ec.Emit (OpCodes.Isinst, probe_type);
1641                         ec.Emit (OpCodes.Dup);
1642                         ec.Emit (OpCodes.Brfalse, no_match);
1643
1644                         bool complex_pattern = ProbeType is ComplexPatternExpression;
1645                         Label prev = ec.RecursivePatternLabel;
1646                         if (complex_pattern)
1647                                 ec.RecursivePatternLabel = ec.DefineLabel ();
1648
1649                         if (number_mg != null) {
1650                                 var ce = new CallEmitter ();
1651                                 ce.Emit (ec, number_mg, number_args, loc);
1652                         } else {
1653                                 if (TypeSpec.IsValueType (probe_type))
1654                                         ec.Emit (OpCodes.Unbox_Any, probe_type);
1655
1656                                 ProbeType.Emit (ec);
1657                                 if (complex_pattern) {
1658                                         ec.EmitInt (1);
1659                                 } else {
1660                                         ec.Emit (OpCodes.Ceq);
1661                                 }
1662                         }
1663                         ec.Emit (OpCodes.Br_S, end);
1664                         ec.MarkLabel (no_match);
1665
1666                         ec.Emit (OpCodes.Pop);
1667
1668                         if (complex_pattern)
1669                                 ec.MarkLabel (ec.RecursivePatternLabel);
1670
1671                         ec.RecursivePatternLabel = prev;
1672
1673                         ec.EmitInt (0);
1674                         ec.MarkLabel (end);
1675                 }
1676
1677                 void EmitLoad (EmitContext ec)
1678                 {
1679                         Label no_value_label = new Label ();
1680
1681                         if (expr_unwrap != null) {
1682                                 expr_unwrap.EmitCheck (ec);
1683
1684                                 if (Variable == null)
1685                                         return;
1686
1687                                 ec.Emit (OpCodes.Dup);
1688                                 no_value_label = ec.DefineLabel ();
1689                                 ec.Emit (OpCodes.Brfalse_S, no_value_label);
1690                                 expr_unwrap.Emit (ec);
1691                         } else {
1692                                 expr.Emit (ec);
1693
1694                                 // Only to make verifier happy
1695                                 if (probe_type_expr.IsGenericParameter && TypeSpec.IsValueType (expr.Type))
1696                                         ec.Emit (OpCodes.Box, expr.Type);
1697
1698                                 ec.Emit (OpCodes.Isinst, probe_type_expr);
1699                         }
1700
1701                         if (Variable != null) {
1702                                 bool value_on_stack;
1703                                 if (probe_type_expr.IsGenericParameter || probe_type_expr.IsNullableType) {
1704                                         ec.Emit (OpCodes.Dup);
1705                                         ec.Emit (OpCodes.Unbox_Any, probe_type_expr);
1706                                         value_on_stack = true;
1707                                 } else {
1708                                         value_on_stack = false;
1709                                 }
1710
1711                                 //
1712                                 // It's ok to have variable builder create out of order. It simplified emit
1713                                 // of statements like while (condition) { }
1714                                 //
1715                                 if (!Variable.Created)
1716                                         Variable.CreateBuilder (ec);
1717                                 
1718                                 Variable.EmitAssign (ec);
1719
1720                                 if (expr_unwrap != null) {
1721                                         ec.MarkLabel (no_value_label);
1722                                 } else if (!value_on_stack) {
1723                                         Variable.Emit (ec);
1724                                 }
1725                         }
1726                 }
1727
1728                 protected override Expression DoResolve (ResolveContext rc)
1729                 {
1730                         if (ResolveCommon (rc) == null)
1731                                 return null;
1732
1733                         type = rc.BuiltinTypes.Bool;
1734                         eclass = ExprClass.Value;
1735
1736                         if (probe_type_expr == null)
1737                                 return ResolveMatchingExpression (rc);
1738
1739                         var res = ResolveResultExpression (rc);
1740                         if (Variable != null) {
1741                                 if (res is Constant)
1742                                         throw new NotImplementedException ("constant in type pattern matching");
1743
1744                                 Variable.Type = probe_type_expr;
1745                                 var bc = rc as BlockContext;
1746                                 if (bc != null)
1747                                         Variable.PrepareAssignmentAnalysis (bc);
1748                         }
1749
1750                         return res;
1751                 }
1752
1753                 public override void FlowAnalysis (FlowAnalysisContext fc)
1754                 {
1755                         base.FlowAnalysis (fc);
1756
1757                         if (Variable != null)
1758                                 fc.SetVariableAssigned (Variable.VariableInfo, true);
1759                 }
1760
1761                 public override void FlowAnalysisConditional (FlowAnalysisContext fc)
1762                 {
1763                         if (Variable == null) {
1764                                 base.FlowAnalysisConditional (fc);
1765                                 return;
1766                         }
1767
1768                         expr.FlowAnalysis (fc);
1769
1770                         fc.DefiniteAssignmentOnTrue = fc.BranchDefiniteAssignment ();
1771                         fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment;
1772
1773                         fc.SetVariableAssigned (Variable.VariableInfo, fc.DefiniteAssignmentOnTrue);
1774                 }
1775
1776                 protected override void ResolveProbeType (ResolveContext rc)
1777                 {
1778                         if (!(ProbeType is TypeExpr) && rc.Module.Compiler.Settings.Version == LanguageVersion.Experimental) {
1779                                 if (ProbeType is PatternExpression) {
1780                                         ProbeType.Resolve (rc);
1781                                         return;
1782                                 }
1783
1784                                 //
1785                                 // Have to use session recording because we don't have reliable type probing
1786                                 // mechanism (similar issue as in attributes resolving)
1787                                 //
1788                                 // TODO: This is still wrong because ResolveAsType can be destructive
1789                                 //
1790                                 var type_printer = new SessionReportPrinter ();
1791                                 var prev_recorder = rc.Report.SetPrinter (type_printer);
1792
1793                                 probe_type_expr = ProbeType.ResolveAsType (rc);
1794                                 type_printer.EndSession ();
1795
1796                                 if (probe_type_expr != null) {
1797                                         type_printer.Merge (rc.Report.Printer);
1798                                         rc.Report.SetPrinter (prev_recorder);
1799                                         return;
1800                                 }
1801
1802                                 var vexpr = ProbeType as VarExpr;
1803                                 if (vexpr != null && vexpr.InferType (rc, expr)) {
1804                                         probe_type_expr = vexpr.Type;
1805                                         rc.Report.SetPrinter (prev_recorder);
1806                                         return;
1807                                 }
1808
1809                                 var expr_printer = new SessionReportPrinter ();
1810                                 rc.Report.SetPrinter (expr_printer);
1811                                 ProbeType = ProbeType.Resolve (rc);
1812                                 expr_printer.EndSession ();
1813
1814                                 if (ProbeType != null) {
1815                                         expr_printer.Merge (rc.Report.Printer);
1816                                 } else {
1817                                         type_printer.Merge (rc.Report.Printer);
1818                                 }
1819
1820                                 rc.Report.SetPrinter (prev_recorder);
1821                                 return;
1822                         }
1823
1824                         base.ResolveProbeType (rc);
1825                 }
1826
1827                 Expression ResolveMatchingExpression (ResolveContext rc)
1828                 {
1829                         var mc = ProbeType as Constant;
1830                         if (mc != null) {
1831                                 if (!Convert.ImplicitConversionExists (rc, ProbeType, Expr.Type)) {
1832                                         ProbeType.Error_ValueCannotBeConverted (rc, Expr.Type, false);
1833                                         return null;
1834                                 }
1835
1836                                 if (mc.IsNull)
1837                                         return new Binary (Binary.Operator.Equality, Expr, mc).Resolve (rc);
1838
1839                                 var c = Expr as Constant;
1840                                 if (c != null) {
1841                                         c = ConstantFold.BinaryFold (rc, Binary.Operator.Equality, c, mc, loc);
1842                                         if (c != null)
1843                                                 return c;
1844                                 }
1845
1846                                 if (Expr.Type.IsNullableType) {
1847                                         expr_unwrap = new Nullable.Unwrap (Expr);
1848                                         expr_unwrap.Resolve (rc);
1849                                         ProbeType = Convert.ImplicitConversion (rc, ProbeType, expr_unwrap.Type, loc);
1850                                 } else if (ProbeType.Type == Expr.Type) {
1851                                         // TODO: Better error handling
1852                                         return new Binary (Binary.Operator.Equality, Expr, mc, loc).Resolve (rc);
1853                                 } else if (ProbeType.Type.IsEnum || (ProbeType.Type.BuiltinType >= BuiltinTypeSpec.Type.Byte && ProbeType.Type.BuiltinType <= BuiltinTypeSpec.Type.Decimal)) {
1854                                         var helper = rc.Module.CreatePatterMatchingHelper ();
1855                                         number_mg = helper.NumberMatcher.Spec;
1856
1857                                         //
1858                                         // There are actually 3 arguments but the first one is already on the stack
1859                                         //
1860                                         number_args = new Arguments (3);
1861                                         if (!ProbeType.Type.IsEnum)
1862                                                 number_args.Add (new Argument (Expr));
1863
1864                                         number_args.Add (new Argument (Convert.ImplicitConversion (rc, ProbeType, rc.BuiltinTypes.Object, loc)));
1865                                         number_args.Add (new Argument (new BoolLiteral (rc.BuiltinTypes, ProbeType.Type.IsEnum, loc)));
1866                                 }
1867
1868                                 return this;
1869                         }
1870
1871                         if (ProbeType is PatternExpression) {
1872                                 if (!(ProbeType is WildcardPattern) && !Convert.ImplicitConversionExists (rc, ProbeType, Expr.Type)) {
1873                                         ProbeType.Error_ValueCannotBeConverted (rc, Expr.Type, false);
1874                                 }
1875
1876                                 return this;
1877                         }
1878
1879                         // TODO: Better error message
1880                         rc.Report.Error (150, ProbeType.Location, "A constant value is expected");
1881                         return this;
1882                 }
1883
1884                 Expression ResolveResultExpression (ResolveContext ec)
1885                 {
1886                         if (Variable != null) {
1887                                 if (expr is NullLiteral) {
1888                                         ec.Report.Error (8117, loc, "Cannot use null as pattern matching operand");
1889                                         return this;
1890                                 }
1891
1892                                 CheckExpressionVariable (ec);
1893                         }
1894
1895                         TypeSpec d = expr.Type;
1896                         bool d_is_nullable = false;
1897
1898                         //
1899                         // If E is a method group or the null literal, or if the type of E is a reference
1900                         // type or a nullable type and the value of E is null, the result is false
1901                         //
1902                         if (expr.IsNull)
1903                                 return CreateConstantResult (ec, false);
1904
1905                         if (d.IsNullableType) {
1906                                 var ut = Nullable.NullableInfo.GetUnderlyingType (d);
1907                                 if (!ut.IsGenericParameter) {
1908                                         d = ut;
1909                                         d_is_nullable = true;
1910                                 }
1911                         }
1912                                 
1913                         TypeSpec t = probe_type_expr;
1914                         bool t_is_nullable = false;
1915                         if (t.IsNullableType) {
1916                                 if (Variable != null) {
1917                                         ec.Report.Error (8116, loc, "The nullable type `{0}' pattern matching is not allowed. Consider using underlying type `{1}'",
1918                                                                          t.GetSignatureForError (), Nullable.NullableInfo.GetUnderlyingType (t).GetSignatureForError ());
1919                                 }
1920
1921                                 var ut = Nullable.NullableInfo.GetUnderlyingType (t);
1922                                 if (!ut.IsGenericParameter) {
1923                                         t = ut;
1924                                         t_is_nullable = true;
1925                                 }
1926                         }
1927
1928                         if (t.IsStruct) {
1929                                 if (d == t) {
1930                                         //
1931                                         // D and T are the same value types but D can be null
1932                                         //
1933                                         if (d_is_nullable && !t_is_nullable) {
1934                                                 expr_unwrap = Nullable.Unwrap.Create (expr, true);
1935                                                 return this;
1936                                         }
1937                                         
1938                                         //
1939                                         // The result is true if D and T are the same value types
1940                                         //
1941                                         return CreateConstantResult (ec, true);
1942                                 }
1943
1944                                 var tp = d as TypeParameterSpec;
1945                                 if (tp != null)
1946                                         return ResolveGenericParameter (ec, t, tp);
1947
1948                                 //
1949                                 // An unboxing conversion exists
1950                                 //
1951                                 if (Convert.ExplicitReferenceConversionExists (d, t))
1952                                         return this;
1953
1954                                 //
1955                                 // open generic type
1956                                 //
1957                                 if (d is InflatedTypeSpec && InflatedTypeSpec.ContainsTypeParameter (d))
1958                                         return this;
1959                         } else {
1960                                 var tps = t as TypeParameterSpec;
1961                                 if (tps != null)
1962                                         return ResolveGenericParameter (ec, d, tps);
1963
1964                                 if (t.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1965                                         if (Variable != null) {
1966                                                 ec.Report.Error (8208, loc, "The type `{0}' pattern matching is not allowed", t.GetSignatureForError ());
1967                                         } else {
1968                                                 ec.Report.Warning (1981, 3, loc,
1969                                                         "Using `{0}' to test compatibility with `{1}' is identical to testing compatibility with `object'",
1970                                                         OperatorName, t.GetSignatureForError ());
1971                                         }
1972                                 }
1973
1974                                 if (TypeManager.IsGenericParameter (d))
1975                                         return ResolveGenericParameter (ec, t, (TypeParameterSpec) d);
1976
1977                                 if (TypeSpec.IsValueType (d)) {
1978                                         if (Convert.ImplicitBoxingConversion (null, d, t) != null) {
1979                                                 if (d_is_nullable && !t_is_nullable) {
1980                                                         expr_unwrap = Nullable.Unwrap.Create (expr, false);
1981                                                         return this;
1982                                                 }
1983
1984                                                 return CreateConstantResult (ec, true);
1985                                         }
1986                                 } else {
1987                                         if (Convert.ImplicitReferenceConversionExists (d, t)) {
1988                                                 var c = expr as Constant;
1989                                                 if (c != null)
1990                                                         return CreateConstantResult (ec, !c.IsNull);
1991
1992                                                 //
1993                                                 // Do not optimize for imported type or dynamic type
1994                                                 //
1995                                                 if (d.MemberDefinition.IsImported && d.BuiltinType != BuiltinTypeSpec.Type.None &&
1996                                                         d.MemberDefinition.DeclaringAssembly != t.MemberDefinition.DeclaringAssembly) {
1997                                                         return this;
1998                                                 }
1999
2000                                                 if (d.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
2001                                                         return this;
2002                                                 
2003                                                 //
2004                                                 // Turn is check into simple null check for implicitly convertible reference types
2005                                                 //
2006                                                 return ReducedExpression.Create (
2007                                                         new Binary (Binary.Operator.Inequality, expr, new NullLiteral (loc), Binary.State.UserOperatorsExcluded).Resolve (ec),
2008                                                         this).Resolve (ec);
2009                                         }
2010
2011                                         if (Convert.ExplicitReferenceConversionExists (d, t))
2012                                                 return this;
2013
2014                                         //
2015                                         // open generic type
2016                                         //
2017                                         if ((d is InflatedTypeSpec || d.IsArray) && InflatedTypeSpec.ContainsTypeParameter (d))
2018                                                 return this;
2019                                 }
2020                         }
2021
2022                         return CreateConstantResult (ec, false);
2023                 }
2024
2025                 Expression ResolveGenericParameter (ResolveContext ec, TypeSpec d, TypeParameterSpec t)
2026                 {
2027                         if (t.IsReferenceType) {
2028                                 if (d.IsStruct)
2029                                         return CreateConstantResult (ec, false);
2030                         }
2031
2032                         if (expr.Type.IsGenericParameter) {
2033                                 if (expr.Type == d && TypeSpec.IsValueType (t) && TypeSpec.IsValueType (d))
2034                                         return CreateConstantResult (ec, true);
2035
2036                                 expr = new BoxedCast (expr, d);
2037                         }
2038
2039                         return this;
2040                 }
2041                 
2042                 public override object Accept (StructuralVisitor visitor)
2043                 {
2044                         return visitor.Visit (this);
2045                 }
2046         }
2047
2048         class WildcardPattern : PatternExpression
2049         {
2050                 public WildcardPattern (Location loc)
2051                         : base (loc)
2052                 {
2053                 }
2054
2055                 protected override Expression DoResolve (ResolveContext rc)
2056                 {
2057                         eclass = ExprClass.Value;
2058                         type = rc.BuiltinTypes.Object;
2059                         return this;
2060                 }
2061
2062                 public override void Emit (EmitContext ec)
2063                 {
2064                         ec.EmitInt (1);
2065                 }
2066         }
2067
2068         class RecursivePattern : ComplexPatternExpression
2069         {
2070                 MethodGroupExpr operator_mg;
2071                 Arguments operator_args;
2072
2073                 public RecursivePattern (ATypeNameExpression typeExpresion, Arguments arguments, Location loc)
2074                         : base (typeExpresion, loc)
2075                 {
2076                         Arguments = arguments;
2077                 }
2078
2079                 public Arguments Arguments { get; private set; }
2080
2081                 protected override Expression DoResolve (ResolveContext rc)
2082                 {
2083                         type = TypeExpression.ResolveAsType (rc);
2084                         if (type == null)
2085                                 return null;
2086
2087                         var operators = MemberCache.GetUserOperator (type, Operator.OpType.Is, true);
2088                         if (operators == null) {
2089                                 Error_TypeDoesNotContainDefinition (rc, type, Operator.GetName (Operator.OpType.Is) + " operator");
2090                                 return null;
2091                         }
2092
2093                         var ops = FindMatchingOverloads (operators);
2094                         if (ops == null) {
2095                                 // TODO: better error message
2096                                 Error_TypeDoesNotContainDefinition (rc, type, Operator.GetName (Operator.OpType.Is) + " operator");
2097                                 return null;
2098                         }
2099
2100                         bool dynamic_args;
2101                         Arguments.Resolve (rc, out dynamic_args);
2102                         if (dynamic_args)
2103                                 throw new NotImplementedException ("dynamic argument");
2104
2105                         var op = FindBestOverload (rc, ops);
2106                         if (op == null) {
2107                                 // TODO: better error message
2108                                 Error_TypeDoesNotContainDefinition (rc, type, Operator.GetName (Operator.OpType.Is) + " operator");
2109                                 return null;
2110                         }
2111
2112                         var op_types = op.Parameters.Types;
2113                         operator_args = new Arguments (op_types.Length);
2114                         operator_args.Add (new Argument (new EmptyExpression (type)));
2115
2116                         for (int i = 0; i < Arguments.Count; ++i) {
2117                                 // TODO: Needs releasing optimization
2118                                 var lt = new LocalTemporary (op_types [i + 1]);
2119                                 operator_args.Add (new Argument (lt, Argument.AType.Out));
2120
2121                                 if (comparisons == null)
2122                                         comparisons = new Expression[Arguments.Count];
2123
2124                                 int arg_comp_index;
2125                                 Expression expr;
2126
2127                                 var arg = Arguments [i];
2128                                 var named = arg as NamedArgument;
2129                                 if (named != null) {
2130                                         arg_comp_index = op.Parameters.GetParameterIndexByName (named.Name) - 1;
2131                                         expr = Arguments [arg_comp_index].Expr;
2132                                 } else {
2133                                         arg_comp_index = i;
2134                                         expr = arg.Expr;
2135                                 }
2136
2137                                 comparisons [arg_comp_index] = ResolveComparison (rc, expr, lt);
2138                         }
2139
2140                         operator_mg = MethodGroupExpr.CreatePredefined (op, type, loc);
2141
2142                         eclass = ExprClass.Value;
2143                         return this;
2144                 }
2145
2146                 List<MethodSpec> FindMatchingOverloads (IList<MemberSpec> members)
2147                 {
2148                         int arg_count = Arguments.Count + 1;
2149                         List<MethodSpec> best = null;
2150                         foreach (MethodSpec method in members) {
2151                                 var pm = method.Parameters;
2152                                 if (pm.Count != arg_count)
2153                                         continue;
2154
2155                                 // TODO: Needs more thorough operator checks elsewhere to avoid doing this every time
2156                                 bool ok = true;
2157                                 for (int ii = 1; ii < pm.Count; ++ii) {
2158                                         if ((pm.FixedParameters [ii].ModFlags & Parameter.Modifier.OUT) == 0) {
2159                                                 ok = false;
2160                                                 break;
2161                                         }
2162                                 }
2163
2164                                 if (!ok)
2165                                         continue;
2166
2167                                 if (best == null)
2168                                         best = new List<MethodSpec> ();
2169
2170                                 best.Add (method);
2171                         }
2172
2173                         return best;
2174                 }
2175
2176                 MethodSpec FindBestOverload (ResolveContext rc, List<MethodSpec> methods)
2177                 {
2178                         for (int ii = 0; ii < Arguments.Count; ++ii) {
2179                                 var arg = Arguments [ii];
2180                                 var expr = arg.Expr;
2181                                 if (expr is WildcardPattern)
2182                                         continue;
2183
2184                                 var na = arg as NamedArgument;
2185                                 for (int i = 0; i < methods.Count; ++i) {
2186                                         var pd = methods [i].Parameters;
2187
2188                                         int index;
2189                                         if (na != null) {
2190                                                 index = pd.GetParameterIndexByName (na.Name);
2191                                                 if (index < 1) {
2192                                                         methods.RemoveAt (i--);
2193                                                         continue;
2194                                                 }
2195                                         } else {
2196                                                 index = ii + 1;
2197                                         }
2198
2199                                         var m = pd.Types [index];
2200                                         if (!Convert.ImplicitConversionExists (rc, expr, m))
2201                                                 methods.RemoveAt (i--);
2202                                 }
2203                         }
2204
2205                         if (methods.Count != 1)
2206                                 return null;
2207
2208                         return methods [0];
2209                 }
2210
2211                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2212                 {
2213                         operator_mg.EmitCall (ec, operator_args, false);
2214                         ec.Emit (OpCodes.Brfalse, target);
2215
2216                         base.EmitBranchable (ec, target, on_true);
2217                 }
2218
2219                 static Expression ResolveComparison (ResolveContext rc, Expression expr, LocalTemporary lt)
2220                 {
2221                         if (expr is WildcardPattern)
2222                                 return new EmptyExpression (expr.Type);
2223
2224                         var recursive = expr as RecursivePattern;
2225                         expr = Convert.ImplicitConversionRequired (rc, expr, lt.Type, expr.Location);
2226                         if (expr == null)
2227                                 return null;
2228
2229                         if (recursive != null) {
2230                                 recursive.SetParentInstance (lt);
2231                                 return expr;
2232                         }
2233
2234                         // TODO: Better error handling
2235                         return new Binary (Binary.Operator.Equality, lt, expr, expr.Location).Resolve (rc);
2236                 }
2237
2238                 public void SetParentInstance (Expression instance)
2239                 {
2240                         operator_args [0] = new Argument (instance);
2241                 }
2242         }
2243
2244         class PropertyPattern : ComplexPatternExpression
2245         {
2246                 LocalTemporary instance;
2247
2248                 public PropertyPattern (ATypeNameExpression typeExpresion, List<PropertyPatternMember> members, Location loc)
2249                         : base (typeExpresion, loc)
2250                 {
2251                         Members = members;
2252                 }
2253
2254                 public List<PropertyPatternMember> Members { get; private set; }
2255
2256                 protected override Expression DoResolve (ResolveContext rc)
2257                 {
2258                         type = TypeExpression.ResolveAsType (rc);
2259                         if (type == null)
2260                                 return null;
2261
2262                         comparisons = new Expression[Members.Count];
2263
2264                         // TODO: optimize when source is VariableReference, it'd save dup+pop
2265                         instance = new LocalTemporary (type);
2266
2267                         for (int i = 0; i < Members.Count; i++) {
2268                                 var lookup = Members [i];
2269
2270                                 var member = MemberLookup (rc, false, type, lookup.Name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
2271                                 if (member == null) {
2272                                         member = MemberLookup (rc, true, type, lookup.Name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
2273                                         if (member != null) {
2274                                                 Expression.ErrorIsInaccesible (rc, member.GetSignatureForError (), loc);
2275                                                 continue;
2276                                         }
2277                                 }
2278
2279                                 if (member == null) {
2280                                         Expression.Error_TypeDoesNotContainDefinition (rc, Location, Type, lookup.Name);
2281                                         continue;
2282                                 }
2283
2284                                 var pe = member as PropertyExpr;
2285                                 if (pe == null || member is FieldExpr) {
2286                                         rc.Report.Error (-2001, lookup.Location, "`{0}' is not a valid pattern member", lookup.Name);
2287                                         continue;
2288                                 }
2289
2290                                 // TODO: Obsolete checks
2291                                 // TODO: check accessibility
2292                                 if (pe != null && !pe.PropertyInfo.HasGet) {
2293                                         rc.Report.Error (-2002, lookup.Location, "Property `{0}.get' accessor is required", pe.GetSignatureForError ());
2294                                         continue;
2295                                 }
2296
2297                                 var expr = lookup.Expr.Resolve (rc);
2298                                 if (expr == null)
2299                                         continue;
2300
2301                                 var me = (MemberExpr)member;
2302                                 me.InstanceExpression = instance;
2303
2304                                 comparisons [i] = ResolveComparison (rc, expr, me);
2305                         }
2306
2307                         eclass = ExprClass.Value;
2308                         return this;
2309                 }
2310
2311                 static Expression ResolveComparison (ResolveContext rc, Expression expr, Expression instance)
2312                 {
2313                         if (expr is WildcardPattern)
2314                                 return new EmptyExpression (expr.Type);
2315
2316                         return new Is (instance, expr, expr.Location).Resolve (rc);
2317                 }
2318
2319                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2320                 {
2321                         instance.Store (ec);
2322
2323                         base.EmitBranchable (ec, target, on_true);
2324                 }
2325         }
2326
2327         class PropertyPatternMember
2328         {
2329                 public PropertyPatternMember (string name, Expression expr, Location loc)
2330                 {
2331                         Name = name;
2332                         Expr = expr;
2333                         Location = loc;
2334                 }
2335
2336                 public string Name { get; private set; }
2337                 public Expression Expr { get; private set; }
2338                 public Location Location { get; private set; }
2339         }
2340
2341         abstract class PatternExpression : Expression
2342         {
2343                 protected PatternExpression (Location loc)
2344                 {
2345                         this.loc = loc;
2346                 }
2347
2348                 public override Expression CreateExpressionTree (ResolveContext ec)
2349                 {
2350                         throw new NotImplementedException ();
2351                 }
2352         }
2353
2354         abstract class ComplexPatternExpression : PatternExpression
2355         {
2356                 protected Expression[] comparisons;
2357
2358                 protected ComplexPatternExpression (ATypeNameExpression typeExpresion, Location loc)
2359                         : base (loc)
2360                 {
2361                         TypeExpression = typeExpresion;
2362                 }
2363
2364                 public ATypeNameExpression TypeExpression { get; private set; }
2365
2366                 public override void Emit (EmitContext ec)
2367                 {
2368                         EmitBranchable (ec, ec.RecursivePatternLabel, false);
2369                 }
2370
2371                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2372                 {
2373                         if (comparisons != null) {
2374                                 foreach (var comp in comparisons) {
2375                                         comp.EmitBranchable (ec, target, false);
2376                                 }
2377                         }
2378                 }
2379         }
2380
2381         /// <summary>
2382         ///   Implementation of the `as' operator.
2383         /// </summary>
2384         public class As : Probe {
2385
2386                 public As (Expression expr, Expression probe_type, Location l)
2387                         : base (expr, probe_type, l)
2388                 {
2389                 }
2390
2391                 protected override string OperatorName {
2392                         get { return "as"; }
2393                 }
2394
2395                 public override Expression CreateExpressionTree (ResolveContext ec)
2396                 {
2397                         Arguments args = Arguments.CreateForExpressionTree (ec, null,
2398                                 expr.CreateExpressionTree (ec),
2399                                 new TypeOf (probe_type_expr, loc));
2400
2401                         return CreateExpressionFactoryCall (ec, "TypeAs", args);
2402                 }
2403
2404                 public override void Emit (EmitContext ec)
2405                 {
2406                         expr.Emit (ec);
2407
2408                         ec.Emit (OpCodes.Isinst, type);
2409
2410                         if (TypeManager.IsGenericParameter (type) || type.IsNullableType)
2411                                 ec.Emit (OpCodes.Unbox_Any, type);
2412                 }
2413
2414                 protected override Expression DoResolve (ResolveContext ec)
2415                 {
2416                         if (ResolveCommon (ec) == null)
2417                                 return null;
2418
2419                         type = probe_type_expr;
2420                         eclass = ExprClass.Value;
2421                         TypeSpec etype = expr.Type;
2422
2423                         if (type == null) {
2424                                 type = InternalType.ErrorType;
2425                                 return this;
2426                         }
2427
2428                         if (!TypeSpec.IsReferenceType (type) && !type.IsNullableType) {
2429                                 if (TypeManager.IsGenericParameter (type)) {
2430                                         ec.Report.Error (413, loc,
2431                                                 "The `as' operator cannot be used with a non-reference type parameter `{0}'. Consider adding `class' or a reference type constraint",
2432                                                 probe_type_expr.GetSignatureForError ());
2433                                 } else {
2434                                         ec.Report.Error (77, loc,
2435                                                 "The `as' operator cannot be used with a non-nullable value type `{0}'",
2436                                                 type.GetSignatureForError ());
2437                                 }
2438                                 return null;
2439                         }
2440
2441                         if (expr.IsNull && type.IsNullableType) {
2442                                 return Nullable.LiftedNull.CreateFromExpression (ec, this);
2443                         }
2444
2445                         // If the compile-time type of E is dynamic, unlike the cast operator the as operator is not dynamically bound
2446                         if (etype.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2447                                 return this;
2448                         }
2449                         
2450                         Expression e = Convert.ImplicitConversionStandard (ec, expr, type, loc);
2451                         if (e != null) {
2452                                 e = EmptyCast.Create (e, type);
2453                                 return ReducedExpression.Create (e, this).Resolve (ec);
2454                         }
2455
2456                         if (Convert.ExplicitReferenceConversionExists (etype, type)){
2457                                 if (TypeManager.IsGenericParameter (etype))
2458                                         expr = new BoxedCast (expr, etype);
2459
2460                                 return this;
2461                         }
2462
2463                         if (InflatedTypeSpec.ContainsTypeParameter (etype) || InflatedTypeSpec.ContainsTypeParameter (type)) {
2464                                 expr = new BoxedCast (expr, etype);
2465                                 return this;
2466                         }
2467
2468                         if (etype != InternalType.ErrorType) {
2469                                 ec.Report.Error (39, loc, "Cannot convert type `{0}' to `{1}' via a built-in conversion",
2470                                         etype.GetSignatureForError (), type.GetSignatureForError ());
2471                         }
2472
2473                         return null;
2474                 }
2475
2476                 public override object Accept (StructuralVisitor visitor)
2477                 {
2478                         return visitor.Visit (this);
2479                 }
2480         }
2481         
2482         //
2483         // This represents a typecast in the source language.
2484         //
2485         public class Cast : ShimExpression {
2486                 Expression target_type;
2487
2488                 public Cast (Expression cast_type, Expression expr, Location loc)
2489                         : base (expr)
2490                 {
2491                         this.target_type = cast_type;
2492                         this.loc = loc;
2493                 }
2494
2495                 public Expression TargetType {
2496                         get { return target_type; }
2497                 }
2498
2499                 protected override Expression DoResolve (ResolveContext ec)
2500                 {
2501                         expr = expr.Resolve (ec);
2502                         if (expr == null)
2503                                 return null;
2504
2505                         type = target_type.ResolveAsType (ec);
2506                         if (type == null)
2507                                 return null;
2508
2509                         if (type.IsStatic) {
2510                                 ec.Report.Error (716, loc, "Cannot convert to static type `{0}'", type.GetSignatureForError ());
2511                                 return null;
2512                         }
2513
2514                         if (type.IsPointer) {
2515                                 if (ec.CurrentIterator != null) {
2516                                         UnsafeInsideIteratorError (ec, loc);
2517                                 } else if (!ec.IsUnsafe) {
2518                                         UnsafeError (ec, loc);
2519                                 }
2520                         }
2521
2522                         eclass = ExprClass.Value;
2523                         
2524                         Constant c = expr as Constant;
2525                         if (c != null) {
2526                                 c = c.Reduce (ec, type);
2527                                 if (c != null)
2528                                         return c;
2529                         }
2530
2531                         var res = Convert.ExplicitConversion (ec, expr, type, loc);
2532                         if (res == expr)
2533                                 return EmptyCast.Create (res, type);
2534
2535                         return res;
2536                 }
2537                 
2538                 protected override void CloneTo (CloneContext clonectx, Expression t)
2539                 {
2540                         Cast target = (Cast) t;
2541
2542                         target.target_type = target_type.Clone (clonectx);
2543                         target.expr = expr.Clone (clonectx);
2544                 }
2545
2546                 public override object Accept (StructuralVisitor visitor)
2547                 {
2548                         return visitor.Visit (this);
2549                 }
2550         }
2551
2552         public class ImplicitCast : ShimExpression
2553         {
2554                 bool arrayAccess;
2555
2556                 public ImplicitCast (Expression expr, TypeSpec target, bool arrayAccess)
2557                         : base (expr)
2558                 {
2559                         this.loc = expr.Location;
2560                         this.type = target;
2561                         this.arrayAccess = arrayAccess;
2562                 }
2563
2564                 protected override Expression DoResolve (ResolveContext ec)
2565                 {
2566                         expr = expr.Resolve (ec);
2567                         if (expr == null)
2568                                 return null;
2569
2570                         if (arrayAccess)
2571                                 expr = ConvertExpressionToArrayIndex (ec, expr);
2572                         else
2573                                 expr = Convert.ImplicitConversionRequired (ec, expr, type, loc);
2574
2575                         return expr;
2576                 }
2577         }
2578
2579         public class DeclarationExpression : Expression, IMemoryLocation
2580         {
2581                 LocalVariableReference lvr;
2582
2583                 public DeclarationExpression (FullNamedExpression variableType, LocalVariable variable)
2584                 {
2585                         VariableType = variableType;
2586                         Variable = variable;
2587                         this.loc = variable.Location;
2588                 }
2589
2590                 public LocalVariable Variable { get; set; }
2591                 public Expression Initializer { get; set; }
2592                 public FullNamedExpression VariableType { get; set; }
2593
2594                 public void AddressOf (EmitContext ec, AddressOp mode)
2595                 {
2596                         Variable.CreateBuilder (ec);
2597
2598                         if (Initializer != null) {
2599                                 lvr.EmitAssign (ec, Initializer, false, false);
2600                         }
2601
2602                         lvr.AddressOf (ec, mode);
2603                 }
2604
2605                 protected override void CloneTo (CloneContext clonectx, Expression t)
2606                 {
2607                         var target = (DeclarationExpression) t;
2608
2609                         target.VariableType = (FullNamedExpression) VariableType.Clone (clonectx);
2610
2611                         if (Initializer != null)
2612                                 target.Initializer = Initializer.Clone (clonectx);
2613                 }
2614
2615                 public override Expression CreateExpressionTree (ResolveContext rc)
2616                 {
2617                         rc.Report.Error (8198, loc, "An expression tree cannot contain out variable declaration");
2618                         return null;
2619                 }
2620
2621                 bool DoResolveCommon (ResolveContext rc)
2622                 {
2623                         CheckExpressionVariable (rc);
2624
2625                         var var_expr = VariableType as VarExpr;
2626                         if (var_expr != null) {
2627                                 type = InternalType.VarOutType;
2628                         } else {
2629                                 type = VariableType.ResolveAsType (rc);
2630                                 if (type == null)
2631                                         return false;
2632                         }
2633
2634                         if (Initializer != null) {
2635                                 Initializer = Initializer.Resolve (rc);
2636
2637                                 if (var_expr != null && Initializer != null && var_expr.InferType (rc, Initializer)) {
2638                                         type = var_expr.Type;
2639                                 }
2640                         }
2641
2642                         Variable.Type = type;
2643                         lvr = new LocalVariableReference (Variable, loc);
2644
2645                         eclass = ExprClass.Variable;
2646                         return true;
2647                 }
2648
2649                 protected override Expression DoResolve (ResolveContext rc)
2650                 {
2651                         if (DoResolveCommon (rc))
2652                                 lvr.Resolve (rc);
2653
2654                         return this;
2655                 }
2656
2657                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
2658                 {
2659                         if (lvr == null && DoResolveCommon (rc))
2660                                 lvr.ResolveLValue (rc, right_side);
2661
2662                         return this;
2663                 }
2664
2665                 public override void Emit (EmitContext ec)
2666                 {
2667                         throw new NotImplementedException ();
2668                 }
2669         }
2670         
2671         //
2672         // C# 2.0 Default value expression
2673         //
2674         public class DefaultValueExpression : Expression
2675         {
2676                 Expression expr;
2677
2678                 public DefaultValueExpression (Expression expr, Location loc)
2679                 {
2680                         this.expr = expr;
2681                         this.loc = loc;
2682                 }
2683
2684                 public Expression Expr {
2685                         get {
2686                                 return this.expr; 
2687                         }
2688                 }
2689
2690                 public override bool IsSideEffectFree {
2691                         get {
2692                                 return true;
2693                         }
2694                 }
2695
2696                 public override bool ContainsEmitWithAwait ()
2697                 {
2698                         return false;
2699                 }
2700
2701                 public override Expression CreateExpressionTree (ResolveContext ec)
2702                 {
2703                         Arguments args = new Arguments (2);
2704                         args.Add (new Argument (this));
2705                         args.Add (new Argument (new TypeOf (type, loc)));
2706                         return CreateExpressionFactoryCall (ec, "Constant", args);
2707                 }
2708
2709                 protected override Expression DoResolve (ResolveContext ec)
2710                 {
2711                         type = expr.ResolveAsType (ec);
2712                         if (type == null)
2713                                 return null;
2714
2715                         if (type.IsStatic) {
2716                                 ec.Report.Error (-244, loc, "The `default value' operator cannot be applied to an operand of a static type");
2717                         }
2718
2719                         if (type.IsPointer)
2720                                 return new NullLiteral (Location).ConvertImplicitly (type);
2721
2722                         if (TypeSpec.IsReferenceType (type))
2723                                 return new NullConstant (type, loc);
2724
2725                         Constant c = New.Constantify (type, expr.Location);
2726                         if (c != null)
2727                                 return c;
2728
2729                         eclass = ExprClass.Variable;
2730                         return this;
2731                 }
2732
2733                 public override void Emit (EmitContext ec)
2734                 {
2735                         LocalTemporary temp_storage = new LocalTemporary(type);
2736
2737                         temp_storage.AddressOf(ec, AddressOp.LoadStore);
2738                         ec.Emit(OpCodes.Initobj, type);
2739                         temp_storage.Emit(ec);
2740                         temp_storage.Release (ec);
2741                 }
2742
2743 #if !STATIC
2744                 public override SLE.Expression MakeExpression (BuilderContext ctx)
2745                 {
2746                         return SLE.Expression.Default (type.GetMetaInfo ());
2747                 }
2748 #endif
2749
2750                 protected override void CloneTo (CloneContext clonectx, Expression t)
2751                 {
2752                         DefaultValueExpression target = (DefaultValueExpression) t;
2753                         
2754                         target.expr = expr.Clone (clonectx);
2755                 }
2756                 
2757                 public override object Accept (StructuralVisitor visitor)
2758                 {
2759                         return visitor.Visit (this);
2760                 }
2761         }
2762
2763         /// <summary>
2764         ///   Binary operators
2765         /// </summary>
2766         public class Binary : Expression, IDynamicBinder
2767         {
2768                 public class PredefinedOperator
2769                 {
2770                         protected readonly TypeSpec left;
2771                         protected readonly TypeSpec right;
2772                         protected readonly TypeSpec left_unwrap;
2773                         protected readonly TypeSpec right_unwrap;
2774                         public readonly Operator OperatorsMask;
2775                         public TypeSpec ReturnType;
2776
2777                         public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
2778                                 : this (ltype, rtype, op_mask, ltype)
2779                         {
2780                         }
2781
2782                         public PredefinedOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
2783                                 : this (type, type, op_mask, return_type)
2784                         {
2785                         }
2786
2787                         public PredefinedOperator (TypeSpec type, Operator op_mask)
2788                                 : this (type, type, op_mask, type)
2789                         {
2790                         }
2791
2792                         public PredefinedOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec return_type)
2793                         {
2794                                 if ((op_mask & Operator.ValuesOnlyMask) != 0)
2795                                         throw new InternalErrorException ("Only masked values can be used");
2796
2797                                 if ((op_mask & Operator.NullableMask) != 0) {
2798                                         left_unwrap = Nullable.NullableInfo.GetUnderlyingType (ltype);
2799                                         right_unwrap = Nullable.NullableInfo.GetUnderlyingType (rtype);
2800                                 } else {
2801                                         left_unwrap = ltype;
2802                                         right_unwrap = rtype;
2803                                 }
2804
2805                                 this.left = ltype;
2806                                 this.right = rtype;
2807                                 this.OperatorsMask = op_mask;
2808                                 this.ReturnType = return_type;
2809                         }
2810
2811                         public bool IsLifted {
2812                                 get {
2813                                         return (OperatorsMask & Operator.NullableMask) != 0;
2814                                 }
2815                         }
2816
2817                         public virtual Expression ConvertResult (ResolveContext rc, Binary b)
2818                         {
2819                                 Constant c;
2820
2821                                 var left_expr = b.left;
2822                                 var right_expr = b.right;
2823
2824                                 b.type = ReturnType;
2825
2826                                 if (IsLifted) {
2827                                         if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion)) {
2828                                                 b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
2829                                                 b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
2830                                         }
2831
2832                                         if (right_expr.IsNull) {
2833                                                 if ((b.oper & Operator.EqualityMask) != 0) {
2834                                                         if (!left_expr.Type.IsNullableType && BuiltinTypeSpec.IsPrimitiveType (left_expr.Type))
2835                                                                 return b.CreateLiftedValueTypeResult (rc, left_expr.Type);
2836                                                 } else if ((b.oper & Operator.BitwiseMask) != 0) {
2837                                                         if (left_unwrap.BuiltinType != BuiltinTypeSpec.Type.Bool)
2838                                                                 return Nullable.LiftedNull.CreateFromExpression (rc, b);
2839                                                 } else {
2840                                                         b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
2841                                                         b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
2842
2843                                                         if ((b.Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
2844                                                                 return Nullable.LiftedNull.CreateFromExpression (rc, b);
2845
2846                                                         return b.CreateLiftedValueTypeResult (rc, left);
2847                                                 }
2848                                         } else if (left_expr.IsNull) {
2849                                                 if ((b.oper & Operator.EqualityMask) != 0) {
2850                                                         if (!right_expr.Type.IsNullableType && BuiltinTypeSpec.IsPrimitiveType (right_expr.Type))
2851                                                                 return b.CreateLiftedValueTypeResult (rc, right_expr.Type);
2852                                                 } else if ((b.oper & Operator.BitwiseMask) != 0) {
2853                                                         if (right_unwrap.BuiltinType != BuiltinTypeSpec.Type.Bool)
2854                                                                 return Nullable.LiftedNull.CreateFromExpression (rc, b);
2855                                                 } else {
2856                                                         b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
2857                                                         b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
2858
2859                                                         if ((b.Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
2860                                                                 return Nullable.LiftedNull.CreateFromExpression (rc, b);
2861
2862                                                         return b.CreateLiftedValueTypeResult (rc, right);
2863                                                 }
2864                                         }
2865                                 }
2866
2867                                 //
2868                                 // A user operators does not support multiple user conversions, but decimal type
2869                                 // is considered to be predefined type therefore we apply predefined operators rules
2870                                 // and then look for decimal user-operator implementation
2871                                 //
2872                                 if (left.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
2873                                         b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
2874                                         b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
2875
2876                                         return b.ResolveUserOperator (rc, b.left, b.right);
2877                                 }
2878
2879                                 c = right_expr as Constant;
2880                                 if (c != null) {
2881                                         if (c.IsDefaultValue) {
2882                                                 //
2883                                                 // Optimizes
2884                                                 // 
2885                                                 // (expr + 0) to expr
2886                                                 // (expr - 0) to expr
2887                                                 // (bool? | false) to bool?
2888                                                 //
2889                                                 if (b.oper == Operator.Addition || b.oper == Operator.Subtraction ||
2890                                                         (b.oper == Operator.BitwiseOr && left_unwrap.BuiltinType == BuiltinTypeSpec.Type.Bool && c is BoolConstant)) {
2891                                                         b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
2892                                                         return ReducedExpression.Create (b.left, b).Resolve (rc);
2893                                                 }
2894
2895                                                 //
2896                                                 // Optimizes (value &/&& 0) to 0
2897                                                 //
2898                                                 if ((b.oper == Operator.BitwiseAnd || b.oper == Operator.LogicalAnd) && !IsLifted) {
2899                                                         Constant side_effect = new SideEffectConstant (c, b.left, c.Location);
2900                                                         return ReducedExpression.Create (side_effect, b);
2901                                                 }
2902                                         } else {
2903                                                 //
2904                                                 // Optimizes (bool? & true) to bool?
2905                                                 //
2906                                                 if (IsLifted && left_unwrap.BuiltinType == BuiltinTypeSpec.Type.Bool && b.oper == Operator.BitwiseAnd) {
2907                                                         return ReducedExpression.Create (b.left, b).Resolve (rc);
2908                                                 }
2909                                         }
2910
2911                                         if ((b.oper == Operator.Multiply || b.oper == Operator.Division) && c.IsOneInteger)
2912                                                 return ReducedExpression.Create (b.left, b).Resolve (rc);
2913
2914                                         if ((b.oper & Operator.ShiftMask) != 0 && c is IntConstant) {
2915                                                 b.right = new IntConstant (rc.BuiltinTypes, ((IntConstant) c).Value & GetShiftMask (left_unwrap), b.right.Location);
2916                                         }
2917                                 }
2918
2919                                 c = b.left as Constant;
2920                                 if (c != null) {
2921                                         if (c.IsDefaultValue) {
2922                                                 //
2923                                                 // Optimizes
2924                                                 // 
2925                                                 // (0 + expr) to expr
2926                                                 // (false | bool?) to bool?
2927                                                 //
2928                                                 if (b.oper == Operator.Addition ||
2929                                                         (b.oper == Operator.BitwiseOr && right_unwrap.BuiltinType == BuiltinTypeSpec.Type.Bool && c is BoolConstant)) {
2930                                                         b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
2931                                                         return ReducedExpression.Create (b.right, b).Resolve (rc);
2932                                                 }
2933
2934                                                 //
2935                                                 // Optimizes (false && expr) to false
2936                                                 //
2937                                                 if (b.oper == Operator.LogicalAnd && c.Type.BuiltinType == BuiltinTypeSpec.Type.Bool) {
2938                                                         // No rhs side-effects
2939                                                         Expression.Warning_UnreachableExpression (rc, b.right.StartLocation);
2940                                                         return ReducedExpression.Create (c, b);
2941                                                 }
2942
2943                                                 //
2944                                                 // Optimizes (0 & value) to 0
2945                                                 //
2946                                                 if (b.oper == Operator.BitwiseAnd && !IsLifted) {
2947                                                         Constant side_effect = new SideEffectConstant (c, b.right, c.Location);
2948                                                         return ReducedExpression.Create (side_effect, b);
2949                                                 }
2950                                         } else {
2951                                                 //
2952                                                 // Optimizes (true & bool?) to bool?
2953                                                 //
2954                                                 if (IsLifted && left_unwrap.BuiltinType == BuiltinTypeSpec.Type.Bool && b.oper == Operator.BitwiseAnd) {
2955                                                         return ReducedExpression.Create (b.right, b).Resolve (rc);
2956                                                 }
2957
2958                                                 //
2959                                                 // Optimizes (true || expr) to true
2960                                                 //
2961                                                 if (b.oper == Operator.LogicalOr && c.Type.BuiltinType == BuiltinTypeSpec.Type.Bool) {
2962                                                         // No rhs side-effects
2963                                                         Expression.Warning_UnreachableExpression (rc, b.right.StartLocation);
2964                                                         return ReducedExpression.Create (c, b);
2965                                                 }
2966                                         }
2967
2968                                         if (b.oper == Operator.Multiply && c.IsOneInteger)
2969                                                 return ReducedExpression.Create (b.right, b).Resolve (rc);
2970                                 }
2971
2972                                 if (IsLifted) {
2973                                         var lifted = new Nullable.LiftedBinaryOperator (b);
2974
2975                                         TypeSpec ltype, rtype;
2976                                         if (b.left.Type.IsNullableType) {
2977                                                 lifted.UnwrapLeft = new Nullable.Unwrap (b.left);
2978                                                 ltype = left_unwrap;
2979                                         } else {
2980                                                 ltype = left;
2981                                         }
2982
2983                                         if (b.right.Type.IsNullableType) {
2984                                                 lifted.UnwrapRight = new Nullable.Unwrap (b.right);
2985                                                 rtype = right_unwrap;
2986                                         } else {
2987                                                 rtype = right;
2988                                         }
2989
2990                                         lifted.Left = b.left.IsNull ?
2991                                                 Nullable.LiftedNull.Create (ltype, b.left.Location) :
2992                                                 Convert.ImplicitConversion (rc, lifted.UnwrapLeft ?? b.left, ltype, b.left.Location);
2993
2994                                         lifted.Right = b.right.IsNull ?
2995                                                 Nullable.LiftedNull.Create (rtype, b.right.Location) :
2996                                                 Convert.ImplicitConversion (rc, lifted.UnwrapRight ?? b.right, rtype, b.right.Location);
2997
2998                                         return lifted.Resolve (rc);
2999                                 }
3000
3001                                 b.left = Convert.ImplicitConversion (rc, b.left, left, b.left.Location);
3002                                 b.right = Convert.ImplicitConversion (rc, b.right, right, b.right.Location);
3003
3004                                 return b;
3005                         }
3006
3007                         public bool IsPrimitiveApplicable (TypeSpec ltype, TypeSpec rtype)
3008                         {
3009                                 //
3010                                 // We are dealing with primitive types only
3011                                 //
3012                                 return left == ltype && ltype == rtype;
3013                         }
3014
3015                         public virtual bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
3016                         {
3017                                 // Quick path
3018                                 if (left == lexpr.Type && right == rexpr.Type)
3019                                         return true;
3020
3021                                 return Convert.ImplicitConversionExists (ec, lexpr, left) &&
3022                                         Convert.ImplicitConversionExists (ec, rexpr, right);
3023                         }
3024
3025                         public PredefinedOperator ResolveBetterOperator (ResolveContext ec, PredefinedOperator best_operator)
3026                         {
3027                                 if ((OperatorsMask & Operator.DecomposedMask) != 0)
3028                                         return best_operator;
3029
3030                                 if ((best_operator.OperatorsMask & Operator.DecomposedMask) != 0)
3031                                         return this;
3032
3033                                 int result = 0;
3034                                 if (left != null && best_operator.left != null) {
3035                                         result = OverloadResolver.BetterTypeConversion (ec, best_operator.left_unwrap, left_unwrap);
3036                                 }
3037
3038                                 //
3039                                 // When second argument is same as the first one, the result is same
3040                                 //
3041                                 if (right != null && (left != right || best_operator.left != best_operator.right)) {
3042                                         result |= OverloadResolver.BetterTypeConversion (ec, best_operator.right_unwrap, right_unwrap);
3043                                 }
3044
3045                                 if (result == 0 || result > 2)
3046                                         return null;
3047
3048                                 return result == 1 ? best_operator : this;
3049                         }
3050                 }
3051
3052                 sealed class PredefinedStringOperator : PredefinedOperator
3053                 {
3054                         public PredefinedStringOperator (TypeSpec type, Operator op_mask, TypeSpec retType)
3055                                 : base (type, type, op_mask, retType)
3056                         {
3057                         }
3058
3059                         public PredefinedStringOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec retType)
3060                                 : base (ltype, rtype, op_mask, retType)
3061                         {
3062                         }
3063
3064                         public override Expression ConvertResult (ResolveContext ec, Binary b)
3065                         {
3066                                 //
3067                                 // Use original expression for nullable arguments
3068                                 //
3069                                 Nullable.Unwrap unwrap = b.left as Nullable.Unwrap;
3070                                 if (unwrap != null)
3071                                         b.left = unwrap.Original;
3072
3073                                 unwrap = b.right as Nullable.Unwrap;
3074                                 if (unwrap != null)
3075                                         b.right = unwrap.Original;
3076
3077                                 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
3078                                 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
3079
3080                                 //
3081                                 // Start a new concat expression using converted expression
3082                                 //
3083                                 return StringConcat.Create (ec, b.left, b.right, b.loc);
3084                         }
3085                 }
3086
3087                 sealed class PredefinedEqualityOperator : PredefinedOperator
3088                 {
3089                         MethodSpec equal_method, inequal_method;
3090
3091                         public PredefinedEqualityOperator (TypeSpec arg, TypeSpec retType)
3092                                 : base (arg, arg, Operator.EqualityMask, retType)
3093                         {
3094                         }
3095
3096                         public override Expression ConvertResult (ResolveContext ec, Binary b)
3097                         {
3098                                 b.type = ReturnType;
3099
3100                                 b.left = Convert.ImplicitConversion (ec, b.left, left, b.left.Location);
3101                                 b.right = Convert.ImplicitConversion (ec, b.right, right, b.right.Location);
3102
3103                                 Arguments args = new Arguments (2);
3104                                 args.Add (new Argument (b.left));
3105                                 args.Add (new Argument (b.right));
3106
3107                                 MethodSpec method;
3108                                 if (b.oper == Operator.Equality) {
3109                                         if (equal_method == null) {
3110                                                 if (left.BuiltinType == BuiltinTypeSpec.Type.String)
3111                                                         equal_method = ec.Module.PredefinedMembers.StringEqual.Resolve (b.loc);
3112                                                 else if (left.BuiltinType == BuiltinTypeSpec.Type.Delegate)
3113                                                         equal_method = ec.Module.PredefinedMembers.DelegateEqual.Resolve (b.loc);
3114                                                 else
3115                                                         throw new NotImplementedException (left.GetSignatureForError ());
3116                                         }
3117
3118                                         method = equal_method;
3119                                 } else {
3120                                         if (inequal_method == null) {
3121                                                 if (left.BuiltinType == BuiltinTypeSpec.Type.String)
3122                                                         inequal_method = ec.Module.PredefinedMembers.StringInequal.Resolve (b.loc);
3123                                                 else if (left.BuiltinType == BuiltinTypeSpec.Type.Delegate)
3124                                                         inequal_method = ec.Module.PredefinedMembers.DelegateInequal.Resolve (b.loc);
3125                                                 else
3126                                                         throw new NotImplementedException (left.GetSignatureForError ());
3127                                         }
3128
3129                                         method = inequal_method;
3130                                 }
3131
3132                                 return new UserOperatorCall (method, args, b.CreateExpressionTree, b.loc);
3133                         }
3134                 }
3135
3136                 class PredefinedPointerOperator : PredefinedOperator
3137                 {
3138                         public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask)
3139                                 : base (ltype, rtype, op_mask)
3140                         {
3141                         }
3142
3143                         public PredefinedPointerOperator (TypeSpec ltype, TypeSpec rtype, Operator op_mask, TypeSpec retType)
3144                                 : base (ltype, rtype, op_mask, retType)
3145                         {
3146                         }
3147
3148                         public PredefinedPointerOperator (TypeSpec type, Operator op_mask, TypeSpec return_type)
3149                                 : base (type, op_mask, return_type)
3150                         {
3151                         }
3152
3153                         public override bool IsApplicable (ResolveContext ec, Expression lexpr, Expression rexpr)
3154                         {
3155                                 if (left == null) {
3156                                         if (!lexpr.Type.IsPointer)
3157                                                 return false;
3158                                 } else {
3159                                         if (!Convert.ImplicitConversionExists (ec, lexpr, left))
3160                                                 return false;
3161                                 }
3162
3163                                 if (right == null) {
3164                                         if (!rexpr.Type.IsPointer)
3165                                                 return false;
3166                                 } else {
3167                                         if (!Convert.ImplicitConversionExists (ec, rexpr, right))
3168                                                 return false;
3169                                 }
3170
3171                                 return true;
3172                         }
3173
3174                         public override Expression ConvertResult (ResolveContext ec, Binary b)
3175                         {
3176                                 if (left != null) {
3177                                         b.left = Convert.UserDefinedConversion (ec, b.left, left, Convert.UserConversionRestriction.ImplicitOnly, b.loc) ?? EmptyCast.Create (b.left, left);
3178                                 } else if (right != null) {
3179                                         b.right = Convert.UserDefinedConversion (ec, b.right, right, Convert.UserConversionRestriction.ImplicitOnly, b.loc) ?? EmptyCast.Create (b.right, right);
3180                                 }
3181
3182                                 TypeSpec r_type = ReturnType;
3183                                 Expression left_arg, right_arg;
3184                                 if (r_type == null) {
3185                                         if (left == null) {
3186                                                 left_arg = b.left;
3187                                                 right_arg = b.right;
3188                                                 r_type = b.left.Type;
3189                                         } else {
3190                                                 left_arg = b.right;
3191                                                 right_arg = b.left;
3192                                                 r_type = b.right.Type;
3193                                         }
3194                                 } else {
3195                                         left_arg = b.left;
3196                                         right_arg = b.right;
3197                                 }
3198
3199                                 return new PointerArithmetic (b.oper, left_arg, right_arg, r_type, b.loc).Resolve (ec);
3200                         }
3201                 }
3202
3203                 [Flags]
3204                 public enum Operator {
3205                         Multiply        = 0 | ArithmeticMask,
3206                         Division        = 1 | ArithmeticMask,
3207                         Modulus         = 2 | ArithmeticMask,
3208                         Addition        = 3 | ArithmeticMask | AdditionMask,
3209                         Subtraction = 4 | ArithmeticMask | SubtractionMask,
3210
3211                         LeftShift       = 5 | ShiftMask,
3212                         RightShift      = 6 | ShiftMask,
3213
3214                         LessThan        = 7 | ComparisonMask | RelationalMask,
3215                         GreaterThan     = 8 | ComparisonMask | RelationalMask,
3216                         LessThanOrEqual         = 9 | ComparisonMask | RelationalMask,
3217                         GreaterThanOrEqual      = 10 | ComparisonMask | RelationalMask,
3218                         Equality        = 11 | ComparisonMask | EqualityMask,
3219                         Inequality      = 12 | ComparisonMask | EqualityMask,
3220
3221                         BitwiseAnd      = 13 | BitwiseMask,
3222                         ExclusiveOr     = 14 | BitwiseMask,
3223                         BitwiseOr       = 15 | BitwiseMask,
3224
3225                         LogicalAnd      = 16 | LogicalMask,
3226                         LogicalOr       = 17 | LogicalMask,
3227
3228                         //
3229                         // Operator masks
3230                         //
3231                         ValuesOnlyMask  = ArithmeticMask - 1,
3232                         ArithmeticMask  = 1 << 5,
3233                         ShiftMask               = 1 << 6,
3234                         ComparisonMask  = 1 << 7,
3235                         EqualityMask    = 1 << 8,
3236                         BitwiseMask             = 1 << 9,
3237                         LogicalMask             = 1 << 10,
3238                         AdditionMask    = 1 << 11,
3239                         SubtractionMask = 1 << 12,
3240                         RelationalMask  = 1 << 13,
3241
3242                         DecomposedMask  = 1 << 19,
3243                         NullableMask    = 1 << 20
3244                 }
3245
3246                 [Flags]
3247                 public enum State : byte
3248                 {
3249                         None = 0,
3250                         Compound = 1 << 1,
3251                         UserOperatorsExcluded = 1 << 2
3252                 }
3253
3254                 readonly Operator oper;
3255                 Expression left, right;
3256                 State state;
3257                 ConvCast.Mode enum_conversion;
3258
3259                 public Binary (Operator oper, Expression left, Expression right, bool isCompound)
3260                         : this (oper, left, right, State.Compound)
3261                 {
3262                 }
3263
3264                 public Binary (Operator oper, Expression left, Expression right, State state)
3265                         : this (oper, left, right)
3266                 {
3267                         this.state = state;
3268                 }
3269
3270                 public Binary (Operator oper, Expression left, Expression right)
3271                         : this (oper, left, right, left.Location)
3272                 {
3273                 }
3274
3275                 public Binary (Operator oper, Expression left, Expression right, Location loc)
3276                 {
3277                         this.oper = oper;
3278                         this.left = left;
3279                         this.right = right;
3280                         this.loc = loc;
3281                 }
3282
3283                 #region Properties
3284
3285                 public bool IsCompound {
3286                         get {
3287                                 return (state & State.Compound) != 0;
3288                         }
3289                 }
3290
3291                 public Operator Oper {
3292                         get {
3293                                 return oper;
3294                         }
3295                 }
3296
3297                 public Expression Left {
3298                         get {
3299                                 return this.left;
3300                         }
3301                 }
3302
3303                 public Expression Right {
3304                         get {
3305                                 return this.right;
3306                         }
3307                 }
3308
3309                 public override Location StartLocation {
3310                         get {
3311                                 return left.StartLocation;
3312                         }
3313                 }
3314
3315                 #endregion
3316
3317                 /// <summary>
3318                 ///   Returns a stringified representation of the Operator
3319                 /// </summary>
3320                 string OperName (Operator oper)
3321                 {
3322                         string s;
3323                         switch (oper){
3324                         case Operator.Multiply:
3325                                 s = "*";
3326                                 break;
3327                         case Operator.Division:
3328                                 s = "/";
3329                                 break;
3330                         case Operator.Modulus:
3331                                 s = "%";
3332                                 break;
3333                         case Operator.Addition:
3334                                 s = "+";
3335                                 break;
3336                         case Operator.Subtraction:
3337                                 s = "-";
3338                                 break;
3339                         case Operator.LeftShift:
3340                                 s = "<<";
3341                                 break;
3342                         case Operator.RightShift:
3343                                 s = ">>";
3344                                 break;
3345                         case Operator.LessThan:
3346                                 s = "<";
3347                                 break;
3348                         case Operator.GreaterThan:
3349                                 s = ">";
3350                                 break;
3351                         case Operator.LessThanOrEqual:
3352                                 s = "<=";
3353                                 break;
3354                         case Operator.GreaterThanOrEqual:
3355                                 s = ">=";
3356                                 break;
3357                         case Operator.Equality:
3358                                 s = "==";
3359                                 break;
3360                         case Operator.Inequality:
3361                                 s = "!=";
3362                                 break;
3363                         case Operator.BitwiseAnd:
3364                                 s = "&";
3365                                 break;
3366                         case Operator.BitwiseOr:
3367                                 s = "|";
3368                                 break;
3369                         case Operator.ExclusiveOr:
3370                                 s = "^";
3371                                 break;
3372                         case Operator.LogicalOr:
3373                                 s = "||";
3374                                 break;
3375                         case Operator.LogicalAnd:
3376                                 s = "&&";
3377                                 break;
3378                         default:
3379                                 s = oper.ToString ();
3380                                 break;
3381                         }
3382
3383                         if (IsCompound)
3384                                 return s + "=";
3385
3386                         return s;
3387                 }
3388
3389                 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, Operator oper, Location loc)
3390                 {
3391                         new Binary (oper, left, right).Error_OperatorCannotBeApplied (ec, left, right);
3392                 }
3393
3394                 public static void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right, string oper, Location loc)
3395                 {
3396                         if (left.Type == InternalType.ErrorType || right.Type == InternalType.ErrorType)
3397                                 return;
3398
3399                         string l, r;
3400                         l = left.Type.GetSignatureForError ();
3401                         r = right.Type.GetSignatureForError ();
3402
3403                         ec.Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
3404                                 oper, l, r);
3405                 }
3406                 
3407                 void Error_OperatorCannotBeApplied (ResolveContext ec, Expression left, Expression right)
3408                 {
3409                         Error_OperatorCannotBeApplied (ec, left, right, OperName (oper), loc);
3410                 }
3411
3412                 public override void FlowAnalysis (FlowAnalysisContext fc)
3413                 {
3414                         //
3415                         // Optimized version when on-true/on-false data are not needed
3416                         //
3417                         if ((oper & Operator.LogicalMask) == 0) {
3418                                 left.FlowAnalysis (fc);
3419                                 right.FlowAnalysis (fc);
3420                                 return;
3421                         }
3422
3423                         left.FlowAnalysisConditional (fc);
3424                         var left_fc_ontrue = fc.DefiniteAssignmentOnTrue;
3425                         var left_fc_onfalse = fc.DefiniteAssignmentOnFalse;
3426
3427                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment = new DefiniteAssignmentBitSet (
3428                                 oper == Operator.LogicalOr ? left_fc_onfalse : left_fc_ontrue);
3429                         right.FlowAnalysisConditional (fc);
3430
3431                         if (oper == Operator.LogicalOr)
3432                                 fc.DefiniteAssignment = (left_fc_onfalse | (fc.DefiniteAssignmentOnFalse & fc.DefiniteAssignmentOnTrue)) & left_fc_ontrue;
3433                         else
3434                                 fc.DefiniteAssignment = (left_fc_ontrue | (fc.DefiniteAssignmentOnFalse & fc.DefiniteAssignmentOnTrue)) & left_fc_onfalse;
3435                 }
3436
3437                 public override void FlowAnalysisConditional (FlowAnalysisContext fc)
3438                 {
3439                         if ((oper & Operator.LogicalMask) == 0) {
3440                                 base.FlowAnalysisConditional (fc);
3441                                 return;
3442                         }
3443
3444                         left.FlowAnalysisConditional (fc);
3445                         var left_fc_ontrue = fc.DefiniteAssignmentOnTrue;
3446                         var left_fc_onfalse = fc.DefiniteAssignmentOnFalse;
3447
3448                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment = new DefiniteAssignmentBitSet (
3449                                 oper == Operator.LogicalOr ? left_fc_onfalse : left_fc_ontrue);
3450                         right.FlowAnalysisConditional (fc);
3451
3452                         var lc = left as Constant;
3453                         if (oper == Operator.LogicalOr) {
3454                                 fc.DefiniteAssignmentOnFalse = left_fc_onfalse | fc.DefiniteAssignmentOnFalse;
3455                                 if (lc != null && lc.IsDefaultValue)
3456                                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse;
3457                                 else
3458                                         fc.DefiniteAssignmentOnTrue = new DefiniteAssignmentBitSet (left_fc_ontrue & (left_fc_onfalse | fc.DefiniteAssignmentOnTrue));
3459                         } else {
3460                                 fc.DefiniteAssignmentOnTrue = left_fc_ontrue | fc.DefiniteAssignmentOnTrue;
3461                                 if (lc != null && !lc.IsDefaultValue)
3462                                         fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignmentOnTrue;
3463                                 else
3464                                         fc.DefiniteAssignmentOnFalse = new DefiniteAssignmentBitSet ((left_fc_ontrue | fc.DefiniteAssignmentOnFalse) & left_fc_onfalse);
3465                         }
3466                 }
3467
3468                 //
3469                 // Converts operator to System.Linq.Expressions.ExpressionType enum name
3470                 //
3471                 string GetOperatorExpressionTypeName ()
3472                 {
3473                         switch (oper) {
3474                         case Operator.Addition:
3475                                 return IsCompound ? "AddAssign" : "Add";
3476                         case Operator.BitwiseAnd:
3477                                 return IsCompound ? "AndAssign" : "And";
3478                         case Operator.BitwiseOr:
3479                                 return IsCompound ? "OrAssign" : "Or";
3480                         case Operator.Division:
3481                                 return IsCompound ? "DivideAssign" : "Divide";
3482                         case Operator.ExclusiveOr:
3483                                 return IsCompound ? "ExclusiveOrAssign" : "ExclusiveOr";
3484                         case Operator.Equality:
3485                                 return "Equal";
3486                         case Operator.GreaterThan:
3487                                 return "GreaterThan";
3488                         case Operator.GreaterThanOrEqual:
3489                                 return "GreaterThanOrEqual";
3490                         case Operator.Inequality:
3491                                 return "NotEqual";
3492                         case Operator.LeftShift:
3493                                 return IsCompound ? "LeftShiftAssign" : "LeftShift";
3494                         case Operator.LessThan:
3495                                 return "LessThan";
3496                         case Operator.LessThanOrEqual:
3497                                 return "LessThanOrEqual";
3498                         case Operator.LogicalAnd:
3499                                 return "And";
3500                         case Operator.LogicalOr:
3501                                 return "Or";
3502                         case Operator.Modulus:
3503                                 return IsCompound ? "ModuloAssign" : "Modulo";
3504                         case Operator.Multiply:
3505                                 return IsCompound ? "MultiplyAssign" : "Multiply";
3506                         case Operator.RightShift:
3507                                 return IsCompound ? "RightShiftAssign" : "RightShift";
3508                         case Operator.Subtraction:
3509                                 return IsCompound ? "SubtractAssign" : "Subtract";
3510                         default:
3511                                 throw new NotImplementedException ("Unknown expression type operator " + oper.ToString ());
3512                         }
3513                 }
3514
3515                 public static CSharp.Operator.OpType ConvertBinaryToUserOperator (Operator op)
3516                 {
3517                         switch (op) {
3518                         case Operator.Addition:
3519                                 return CSharp.Operator.OpType.Addition;
3520                         case Operator.BitwiseAnd:
3521                         case Operator.LogicalAnd:
3522                                 return CSharp.Operator.OpType.BitwiseAnd;
3523                         case Operator.BitwiseOr:
3524                         case Operator.LogicalOr:
3525                                 return CSharp.Operator.OpType.BitwiseOr;
3526                         case Operator.Division:
3527                                 return CSharp.Operator.OpType.Division;
3528                         case Operator.Equality:
3529                                 return CSharp.Operator.OpType.Equality;
3530                         case Operator.ExclusiveOr:
3531                                 return CSharp.Operator.OpType.ExclusiveOr;
3532                         case Operator.GreaterThan:
3533                                 return CSharp.Operator.OpType.GreaterThan;
3534                         case Operator.GreaterThanOrEqual:
3535                                 return CSharp.Operator.OpType.GreaterThanOrEqual;
3536                         case Operator.Inequality:
3537                                 return CSharp.Operator.OpType.Inequality;
3538                         case Operator.LeftShift:
3539                                 return CSharp.Operator.OpType.LeftShift;
3540                         case Operator.LessThan:
3541                                 return CSharp.Operator.OpType.LessThan;
3542                         case Operator.LessThanOrEqual:
3543                                 return CSharp.Operator.OpType.LessThanOrEqual;
3544                         case Operator.Modulus:
3545                                 return CSharp.Operator.OpType.Modulus;
3546                         case Operator.Multiply:
3547                                 return CSharp.Operator.OpType.Multiply;
3548                         case Operator.RightShift:
3549                                 return CSharp.Operator.OpType.RightShift;
3550                         case Operator.Subtraction:
3551                                 return CSharp.Operator.OpType.Subtraction;
3552                         default:
3553                                 throw new InternalErrorException (op.ToString ());
3554                         }
3555                 }
3556
3557                 public override bool ContainsEmitWithAwait ()
3558                 {
3559                         return left.ContainsEmitWithAwait () || right.ContainsEmitWithAwait ();
3560                 }
3561
3562                 public static void EmitOperatorOpcode (EmitContext ec, Operator oper, TypeSpec l, Expression right)
3563                 {
3564                         OpCode opcode;
3565
3566                         switch (oper){
3567                         case Operator.Multiply:
3568                                 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
3569                                         if (l.BuiltinType == BuiltinTypeSpec.Type.Int || l.BuiltinType == BuiltinTypeSpec.Type.Long)
3570                                                 opcode = OpCodes.Mul_Ovf;
3571                                         else if (!IsFloat (l))
3572                                                 opcode = OpCodes.Mul_Ovf_Un;
3573                                         else
3574                                                 opcode = OpCodes.Mul;
3575                                 } else
3576                                         opcode = OpCodes.Mul;
3577                                 
3578                                 break;
3579                                 
3580                         case Operator.Division:
3581                                 if (IsUnsigned (l))
3582                                         opcode = OpCodes.Div_Un;
3583                                 else
3584                                         opcode = OpCodes.Div;
3585                                 break;
3586                                 
3587                         case Operator.Modulus:
3588                                 if (IsUnsigned (l))
3589                                         opcode = OpCodes.Rem_Un;
3590                                 else
3591                                         opcode = OpCodes.Rem;
3592                                 break;
3593
3594                         case Operator.Addition:
3595                                 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
3596                                         if (l.BuiltinType == BuiltinTypeSpec.Type.Int || l.BuiltinType == BuiltinTypeSpec.Type.Long)
3597                                                 opcode = OpCodes.Add_Ovf;
3598                                         else if (!IsFloat (l))
3599                                                 opcode = OpCodes.Add_Ovf_Un;
3600                                         else
3601                                                 opcode = OpCodes.Add;
3602                                 } else
3603                                         opcode = OpCodes.Add;
3604                                 break;
3605
3606                         case Operator.Subtraction:
3607                                 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
3608                                         if (l.BuiltinType == BuiltinTypeSpec.Type.Int || l.BuiltinType == BuiltinTypeSpec.Type.Long)
3609                                                 opcode = OpCodes.Sub_Ovf;
3610                                         else if (!IsFloat (l))
3611                                                 opcode = OpCodes.Sub_Ovf_Un;
3612                                         else
3613                                                 opcode = OpCodes.Sub;
3614                                 } else
3615                                         opcode = OpCodes.Sub;
3616                                 break;
3617
3618                         case Operator.RightShift:
3619                                 if (!(right is IntConstant)) {
3620                                         ec.EmitInt (GetShiftMask (l));
3621                                         ec.Emit (OpCodes.And);
3622                                 }
3623
3624                                 if (IsUnsigned (l))
3625                                         opcode = OpCodes.Shr_Un;
3626                                 else
3627                                         opcode = OpCodes.Shr;
3628                                 break;
3629                                 
3630                         case Operator.LeftShift:
3631                                 if (!(right is IntConstant)) {
3632                                         ec.EmitInt (GetShiftMask (l));
3633                                         ec.Emit (OpCodes.And);
3634                                 }
3635
3636                                 opcode = OpCodes.Shl;
3637                                 break;
3638
3639                         case Operator.Equality:
3640                                 opcode = OpCodes.Ceq;
3641                                 break;
3642
3643                         case Operator.Inequality:
3644                                 ec.Emit (OpCodes.Ceq);
3645                                 ec.EmitInt (0);
3646                                 
3647                                 opcode = OpCodes.Ceq;
3648                                 break;
3649
3650                         case Operator.LessThan:
3651                                 if (IsUnsigned (l))
3652                                         opcode = OpCodes.Clt_Un;
3653                                 else
3654                                         opcode = OpCodes.Clt;
3655                                 break;
3656
3657                         case Operator.GreaterThan:
3658                                 if (IsUnsigned (l))
3659                                         opcode = OpCodes.Cgt_Un;
3660                                 else
3661                                         opcode = OpCodes.Cgt;
3662                                 break;
3663
3664                         case Operator.LessThanOrEqual:
3665                                 if (IsUnsigned (l) || IsFloat (l))
3666                                         ec.Emit (OpCodes.Cgt_Un);
3667                                 else
3668                                         ec.Emit (OpCodes.Cgt);
3669                                 ec.EmitInt (0);
3670                                 
3671                                 opcode = OpCodes.Ceq;
3672                                 break;
3673
3674                         case Operator.GreaterThanOrEqual:
3675                                 if (IsUnsigned (l) || IsFloat (l))
3676                                         ec.Emit (OpCodes.Clt_Un);
3677                                 else
3678                                         ec.Emit (OpCodes.Clt);
3679                                 
3680                                 ec.EmitInt (0);
3681                                 
3682                                 opcode = OpCodes.Ceq;
3683                                 break;
3684
3685                         case Operator.BitwiseOr:
3686                                 opcode = OpCodes.Or;
3687                                 break;
3688
3689                         case Operator.BitwiseAnd:
3690                                 opcode = OpCodes.And;
3691                                 break;
3692
3693                         case Operator.ExclusiveOr:
3694                                 opcode = OpCodes.Xor;
3695                                 break;
3696
3697                         default:
3698                                 throw new InternalErrorException (oper.ToString ());
3699                         }
3700
3701                         ec.Emit (opcode);
3702                 }
3703
3704                 static int GetShiftMask (TypeSpec type)
3705                 {
3706                         return type.BuiltinType == BuiltinTypeSpec.Type.Int || type.BuiltinType == BuiltinTypeSpec.Type.UInt ? 0x1f : 0x3f;
3707                 }
3708
3709                 static bool IsUnsigned (TypeSpec t)
3710                 {
3711                         switch (t.BuiltinType) {
3712                         case BuiltinTypeSpec.Type.Char:
3713                         case BuiltinTypeSpec.Type.UInt:
3714                         case BuiltinTypeSpec.Type.ULong:
3715                         case BuiltinTypeSpec.Type.UShort:
3716                         case BuiltinTypeSpec.Type.Byte:
3717                                 return true;
3718                         }
3719
3720                         return t.IsPointer;
3721                 }
3722
3723                 static bool IsFloat (TypeSpec t)
3724                 {
3725                         return t.BuiltinType == BuiltinTypeSpec.Type.Float || t.BuiltinType == BuiltinTypeSpec.Type.Double;
3726                 }
3727
3728                 public Expression ResolveOperator (ResolveContext rc)
3729                 {
3730                         eclass = ExprClass.Value;
3731
3732                         TypeSpec l = left.Type;
3733                         TypeSpec r = right.Type;
3734                         Expression expr;
3735                         bool primitives_only = false;
3736
3737                         //
3738                         // Handles predefined primitive types
3739                         //
3740                         if ((BuiltinTypeSpec.IsPrimitiveType (l) || (l.IsNullableType && BuiltinTypeSpec.IsPrimitiveType (Nullable.NullableInfo.GetUnderlyingType (l)))) &&
3741                                 (BuiltinTypeSpec.IsPrimitiveType (r) || (r.IsNullableType && BuiltinTypeSpec.IsPrimitiveType (Nullable.NullableInfo.GetUnderlyingType (r))))) {
3742                                 if ((oper & Operator.ShiftMask) == 0) {
3743                                         if (!DoBinaryOperatorPromotion (rc))
3744                                                 return null;
3745
3746                                         primitives_only = BuiltinTypeSpec.IsPrimitiveType (l) && BuiltinTypeSpec.IsPrimitiveType (r);
3747                                 }
3748                         } else {
3749                                 // Pointers
3750                                 if (l.IsPointer || r.IsPointer)
3751                                         return ResolveOperatorPointer (rc, l, r);
3752
3753                                 // User operators
3754                                 if ((state & State.UserOperatorsExcluded) == 0) {
3755                                         expr = ResolveUserOperator (rc, left, right);
3756                                         if (expr != null)
3757                                                 return expr;
3758                                 }
3759
3760                                 bool lenum = l.IsEnum;
3761                                 bool renum = r.IsEnum;
3762                                 if ((oper & (Operator.ComparisonMask | Operator.BitwiseMask)) != 0) {
3763                                         //
3764                                         // Enumerations
3765                                         //
3766                                         if (IsEnumOrNullableEnum (l) || IsEnumOrNullableEnum (r)) {
3767                                                 expr = ResolveSingleEnumOperators (rc, lenum, renum, l, r);
3768
3769                                                 if (expr == null)
3770                                                         return null;
3771
3772                                                 if ((oper & Operator.BitwiseMask) != 0) {
3773                                                         expr = EmptyCast.Create (expr, type);
3774                                                         enum_conversion = GetEnumResultCast (type);
3775
3776                                                         if (oper == Operator.BitwiseAnd && left.Type.IsEnum && right.Type.IsEnum) {
3777                                                                 expr = OptimizeAndOperation (expr);
3778                                                         }
3779                                                 }
3780
3781                                                 left = ConvertEnumOperandToUnderlyingType (rc, left, r.IsNullableType);
3782                                                 right = ConvertEnumOperandToUnderlyingType (rc, right, l.IsNullableType);
3783                                                 return expr;
3784                                         }
3785                                 } else if ((oper == Operator.Addition || oper == Operator.Subtraction)) {
3786                                         if (IsEnumOrNullableEnum (l) || IsEnumOrNullableEnum (r)) {
3787                                                 //
3788                                                 // Enumerations
3789                                                 //
3790                                                 expr = ResolveEnumOperators (rc, lenum, renum, l, r);
3791
3792                                                 //
3793                                                 // We cannot break here there is also Enum + String possible match
3794                                                 // which is not ambiguous with predefined enum operators
3795                                                 //
3796                                                 if (expr != null) {
3797                                                         left = ConvertEnumOperandToUnderlyingType (rc, left, false);
3798                                                         right = ConvertEnumOperandToUnderlyingType (rc, right, false);
3799
3800                                                         return expr;
3801                                                 }
3802                                         } else if (l.IsDelegate || r.IsDelegate) {
3803                                                 //
3804                                                 // Delegates
3805                                                 //
3806                                                 expr = ResolveOperatorDelegate (rc, l, r);
3807
3808                                                 // TODO: Can this be ambiguous
3809                                                 if (expr != null)
3810                                                         return expr;
3811                                         }
3812                                 }
3813                         }
3814                         
3815                         //
3816                         // Equality operators are more complicated
3817                         //
3818                         if ((oper & Operator.EqualityMask) != 0) {
3819                                 return ResolveEquality (rc, l, r, primitives_only);
3820                         }
3821
3822                         expr = ResolveOperatorPredefined (rc, rc.BuiltinTypes.OperatorsBinaryStandard, primitives_only);
3823                         if (expr != null)
3824                                 return expr;
3825
3826                         if (primitives_only)
3827                                 return null;
3828
3829                         //
3830                         // Lifted operators have lower priority
3831                         //
3832                         return ResolveOperatorPredefined (rc, rc.Module.OperatorsBinaryLifted, false);
3833                 }
3834
3835                 static bool IsEnumOrNullableEnum (TypeSpec type)
3836                 {
3837                         return type.IsEnum || (type.IsNullableType && Nullable.NullableInfo.GetUnderlyingType (type).IsEnum);
3838                 }
3839
3840
3841                 // at least one of 'left' or 'right' is an enumeration constant (EnumConstant or SideEffectConstant or ...)
3842                 // if 'left' is not an enumeration constant, create one from the type of 'right'
3843                 Constant EnumLiftUp (ResolveContext ec, Constant left, Constant right)
3844                 {
3845                         switch (oper) {
3846                         case Operator.BitwiseOr:
3847                         case Operator.BitwiseAnd:
3848                         case Operator.ExclusiveOr:
3849                         case Operator.Equality:
3850                         case Operator.Inequality:
3851                         case Operator.LessThan:
3852                         case Operator.LessThanOrEqual:
3853                         case Operator.GreaterThan:
3854                         case Operator.GreaterThanOrEqual:
3855                                 if (left.Type.IsEnum)
3856                                         return left;
3857                                 
3858                                 if (left.IsZeroInteger)
3859                                         return left.Reduce (ec, right.Type);
3860                                 
3861                                 break;
3862                                 
3863                         case Operator.Addition:
3864                         case Operator.Subtraction:
3865                                 return left;
3866                                 
3867                         case Operator.Multiply:
3868                         case Operator.Division:
3869                         case Operator.Modulus:
3870                         case Operator.LeftShift:
3871                         case Operator.RightShift:
3872                                 if (right.Type.IsEnum || left.Type.IsEnum)
3873                                         break;
3874                                 return left;
3875                         }
3876
3877                         return null;
3878                 }
3879
3880                 //
3881                 // The `|' operator used on types which were extended is dangerous
3882                 //
3883                 void CheckBitwiseOrOnSignExtended (ResolveContext ec)
3884                 {
3885                         OpcodeCast lcast = left as OpcodeCast;
3886                         if (lcast != null) {
3887                                 if (IsUnsigned (lcast.UnderlyingType))
3888                                         lcast = null;
3889                         }
3890
3891                         OpcodeCast rcast = right as OpcodeCast;
3892                         if (rcast != null) {
3893                                 if (IsUnsigned (rcast.UnderlyingType))
3894                                         rcast = null;
3895                         }
3896
3897                         if (lcast == null && rcast == null)
3898                                 return;
3899
3900                         // FIXME: consider constants
3901
3902                         var ltype = lcast != null ? lcast.UnderlyingType : rcast.UnderlyingType;
3903                         ec.Report.Warning (675, 3, loc,
3904                                 "The operator `|' used on the sign-extended type `{0}'. Consider casting to a smaller unsigned type first",
3905                                 ltype.GetSignatureForError ());
3906                 }
3907
3908                 public static PredefinedOperator[] CreatePointerOperatorsTable (BuiltinTypes types)
3909                 {
3910                         return new PredefinedOperator[] {
3911                                 //
3912                                 // Pointer arithmetic:
3913                                 //
3914                                 // T* operator + (T* x, int y);         T* operator - (T* x, int y);
3915                                 // T* operator + (T* x, uint y);        T* operator - (T* x, uint y);
3916                                 // T* operator + (T* x, long y);        T* operator - (T* x, long y);
3917                                 // T* operator + (T* x, ulong y);       T* operator - (T* x, ulong y);
3918                                 //
3919                                 new PredefinedPointerOperator (null, types.Int, Operator.AdditionMask | Operator.SubtractionMask),
3920                                 new PredefinedPointerOperator (null, types.UInt, Operator.AdditionMask | Operator.SubtractionMask),
3921                                 new PredefinedPointerOperator (null, types.Long, Operator.AdditionMask | Operator.SubtractionMask),
3922                                 new PredefinedPointerOperator (null, types.ULong, Operator.AdditionMask | Operator.SubtractionMask),
3923
3924                                 //
3925                                 // T* operator + (int y,   T* x);
3926                                 // T* operator + (uint y,  T *x);
3927                                 // T* operator + (long y,  T *x);
3928                                 // T* operator + (ulong y, T *x);
3929                                 //
3930                                 new PredefinedPointerOperator (types.Int, null, Operator.AdditionMask, null),
3931                                 new PredefinedPointerOperator (types.UInt, null, Operator.AdditionMask, null),
3932                                 new PredefinedPointerOperator (types.Long, null, Operator.AdditionMask, null),
3933                                 new PredefinedPointerOperator (types.ULong, null, Operator.AdditionMask, null),
3934
3935                                 //
3936                                 // long operator - (T* x, T *y)
3937                                 //
3938                                 new PredefinedPointerOperator (null, Operator.SubtractionMask, types.Long)
3939                         };
3940                 }
3941
3942                 public static PredefinedOperator[] CreateStandardOperatorsTable (BuiltinTypes types)
3943                 {
3944                         TypeSpec bool_type = types.Bool;
3945
3946                         return new [] {
3947                                 new PredefinedOperator (types.Int, Operator.ArithmeticMask | Operator.BitwiseMask | Operator.ShiftMask),
3948                                 new PredefinedOperator (types.UInt, Operator.ArithmeticMask | Operator.BitwiseMask),
3949                                 new PredefinedOperator (types.Long, Operator.ArithmeticMask | Operator.BitwiseMask),
3950                                 new PredefinedOperator (types.ULong, Operator.ArithmeticMask | Operator.BitwiseMask),
3951                                 new PredefinedOperator (types.Float, Operator.ArithmeticMask),
3952                                 new PredefinedOperator (types.Double, Operator.ArithmeticMask),
3953                                 new PredefinedOperator (types.Decimal, Operator.ArithmeticMask),
3954
3955                                 new PredefinedOperator (types.Int, Operator.ComparisonMask, bool_type),
3956                                 new PredefinedOperator (types.UInt, Operator.ComparisonMask, bool_type),
3957                                 new PredefinedOperator (types.Long, Operator.ComparisonMask, bool_type),
3958                                 new PredefinedOperator (types.ULong, Operator.ComparisonMask, bool_type),
3959                                 new PredefinedOperator (types.Float, Operator.ComparisonMask, bool_type),
3960                                 new PredefinedOperator (types.Double, Operator.ComparisonMask, bool_type),
3961                                 new PredefinedOperator (types.Decimal, Operator.ComparisonMask, bool_type),
3962
3963                                 new PredefinedStringOperator (types.String, Operator.AdditionMask, types.String),
3964                                 // Remaining string operators are in lifted tables
3965
3966                                 new PredefinedOperator (bool_type, Operator.BitwiseMask | Operator.LogicalMask | Operator.EqualityMask, bool_type),
3967
3968                                 new PredefinedOperator (types.UInt, types.Int, Operator.ShiftMask),
3969                                 new PredefinedOperator (types.Long, types.Int, Operator.ShiftMask),
3970                                 new PredefinedOperator (types.ULong, types.Int, Operator.ShiftMask)
3971                         };
3972
3973                 }
3974                 public static PredefinedOperator[] CreateStandardLiftedOperatorsTable (ModuleContainer module)
3975                 {
3976                         var types = module.Compiler.BuiltinTypes;
3977
3978                         //
3979                         // Not strictly lifted but need to be in second group otherwise expressions like
3980                         // int + null would resolve to +(object, string) instead of +(int?, int?)
3981                         //
3982                         var string_operators = new [] {
3983                                 new PredefinedStringOperator (types.String, types.Object, Operator.AdditionMask, types.String),
3984                                 new PredefinedStringOperator (types.Object, types.String, Operator.AdditionMask, types.String),
3985                         };
3986
3987                         var nullable = module.PredefinedTypes.Nullable.TypeSpec;
3988                         if (nullable == null)
3989                                 return string_operators;
3990
3991                         var bool_type = types.Bool;
3992
3993                         var nullable_bool = nullable.MakeGenericType (module, new[] { bool_type });
3994                         var nullable_int = nullable.MakeGenericType (module, new[] { types.Int });
3995                         var nullable_uint = nullable.MakeGenericType (module, new[] { types.UInt });
3996                         var nullable_long = nullable.MakeGenericType (module, new[] { types.Long });
3997                         var nullable_ulong = nullable.MakeGenericType (module, new[] { types.ULong });
3998                         var nullable_float = nullable.MakeGenericType (module, new[] { types.Float });
3999                         var nullable_double = nullable.MakeGenericType (module, new[] { types.Double });
4000                         var nullable_decimal = nullable.MakeGenericType (module, new[] { types.Decimal });
4001
4002                         return new[] {
4003                                 new PredefinedOperator (nullable_int, Operator.NullableMask | Operator.ArithmeticMask | Operator.BitwiseMask | Operator.ShiftMask),
4004                                 new PredefinedOperator (nullable_uint, Operator.NullableMask | Operator.ArithmeticMask | Operator.BitwiseMask),
4005                                 new PredefinedOperator (nullable_long, Operator.NullableMask | Operator.ArithmeticMask | Operator.BitwiseMask),
4006                                 new PredefinedOperator (nullable_ulong, Operator.NullableMask | Operator.ArithmeticMask | Operator.BitwiseMask),
4007                                 new PredefinedOperator (nullable_float, Operator.NullableMask | Operator.ArithmeticMask),
4008                                 new PredefinedOperator (nullable_double, Operator.NullableMask | Operator.ArithmeticMask),
4009                                 new PredefinedOperator (nullable_decimal, Operator.NullableMask | Operator.ArithmeticMask),
4010
4011                                 new PredefinedOperator (nullable_int, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4012                                 new PredefinedOperator (nullable_uint, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4013                                 new PredefinedOperator (nullable_long, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4014                                 new PredefinedOperator (nullable_ulong, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4015                                 new PredefinedOperator (nullable_float, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4016                                 new PredefinedOperator (nullable_double, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4017                                 new PredefinedOperator (nullable_decimal, Operator.NullableMask | Operator.ComparisonMask, bool_type),
4018
4019                                 new PredefinedOperator (nullable_bool, Operator.NullableMask | Operator.BitwiseMask, nullable_bool),
4020
4021                                 new PredefinedOperator (nullable_uint, nullable_int, Operator.NullableMask | Operator.ShiftMask),
4022                                 new PredefinedOperator (nullable_long, nullable_int, Operator.NullableMask | Operator.ShiftMask),
4023                                 new PredefinedOperator (nullable_ulong, nullable_int, Operator.NullableMask | Operator.ShiftMask),
4024
4025                                 string_operators [0],
4026                                 string_operators [1]
4027                         };
4028                 }
4029
4030                 public static PredefinedOperator[] CreateEqualityOperatorsTable (BuiltinTypes types)
4031                 {
4032                         TypeSpec bool_type = types.Bool;
4033
4034                         return new[] {
4035                                 new PredefinedEqualityOperator (types.String, bool_type),
4036                                 new PredefinedEqualityOperator (types.Delegate, bool_type),
4037                                 new PredefinedOperator (bool_type, Operator.EqualityMask, bool_type),
4038                                 new PredefinedOperator (types.Int, Operator.EqualityMask, bool_type),
4039                                 new PredefinedOperator (types.UInt, Operator.EqualityMask, bool_type),
4040                                 new PredefinedOperator (types.Long, Operator.EqualityMask, bool_type),
4041                                 new PredefinedOperator (types.ULong, Operator.EqualityMask, bool_type),
4042                                 new PredefinedOperator (types.Float, Operator.EqualityMask, bool_type),
4043                                 new PredefinedOperator (types.Double, Operator.EqualityMask, bool_type),
4044                                 new PredefinedOperator (types.Decimal, Operator.EqualityMask, bool_type),
4045                         };
4046                 }
4047
4048                 public static PredefinedOperator[] CreateEqualityLiftedOperatorsTable (ModuleContainer module)
4049                 {
4050                         var nullable = module.PredefinedTypes.Nullable.TypeSpec;
4051
4052                         if (nullable == null)
4053                                 return new PredefinedOperator [0];
4054
4055                         var types = module.Compiler.BuiltinTypes;
4056                         var bool_type = types.Bool;
4057                         var nullable_bool = nullable.MakeGenericType (module, new [] { bool_type });
4058                         var nullable_int = nullable.MakeGenericType (module, new[] { types.Int });
4059                         var nullable_uint = nullable.MakeGenericType (module, new[] { types.UInt });
4060                         var nullable_long = nullable.MakeGenericType (module, new[] { types.Long });
4061                         var nullable_ulong = nullable.MakeGenericType (module, new[] { types.ULong });
4062                         var nullable_float = nullable.MakeGenericType (module, new[] { types.Float });
4063                         var nullable_double = nullable.MakeGenericType (module, new[] { types.Double });
4064                         var nullable_decimal = nullable.MakeGenericType (module, new[] { types.Decimal });
4065
4066                         return new [] {
4067                                 new PredefinedOperator (nullable_bool, Operator.NullableMask | Operator.EqualityMask, bool_type),
4068                                 new PredefinedOperator (nullable_int, Operator.NullableMask | Operator.EqualityMask, bool_type),
4069                                 new PredefinedOperator (nullable_uint, Operator.NullableMask | Operator.EqualityMask, bool_type),
4070                                 new PredefinedOperator (nullable_long, Operator.NullableMask | Operator.EqualityMask, bool_type),
4071                                 new PredefinedOperator (nullable_ulong, Operator.NullableMask | Operator.EqualityMask, bool_type),
4072                                 new PredefinedOperator (nullable_float, Operator.NullableMask | Operator.EqualityMask, bool_type),
4073                                 new PredefinedOperator (nullable_double, Operator.NullableMask | Operator.EqualityMask, bool_type),
4074                                 new PredefinedOperator (nullable_decimal, Operator.NullableMask | Operator.EqualityMask, bool_type)
4075                         };
4076                 }
4077
4078                 //
4079                 // 7.2.6.2 Binary numeric promotions
4080                 //
4081                 bool DoBinaryOperatorPromotion (ResolveContext rc)
4082                 {
4083                         TypeSpec ltype = left.Type;
4084                         if (ltype.IsNullableType) {
4085                                 ltype = Nullable.NullableInfo.GetUnderlyingType (ltype);
4086                         }
4087
4088                         //
4089                         // This is numeric promotion code only
4090                         //
4091                         if (ltype.BuiltinType == BuiltinTypeSpec.Type.Bool)
4092                                 return true;
4093
4094                         TypeSpec rtype = right.Type;
4095                         if (rtype.IsNullableType) {
4096                                 rtype = Nullable.NullableInfo.GetUnderlyingType (rtype);
4097                         }
4098
4099                         var lb = ltype.BuiltinType;
4100                         var rb = rtype.BuiltinType;
4101                         TypeSpec type;
4102                         Expression expr;
4103
4104                         if (lb == BuiltinTypeSpec.Type.Decimal || rb == BuiltinTypeSpec.Type.Decimal) {
4105                                 type = rc.BuiltinTypes.Decimal;
4106                         } else if (lb == BuiltinTypeSpec.Type.Double || rb == BuiltinTypeSpec.Type.Double) {
4107                                 type = rc.BuiltinTypes.Double;
4108                         } else if (lb == BuiltinTypeSpec.Type.Float || rb == BuiltinTypeSpec.Type.Float) {
4109                                 type = rc.BuiltinTypes.Float;
4110                         } else if (lb == BuiltinTypeSpec.Type.ULong || rb == BuiltinTypeSpec.Type.ULong) {
4111                                 type = rc.BuiltinTypes.ULong;
4112
4113                                 if (IsSignedType (lb)) {
4114                                         expr = ConvertSignedConstant (left, type);
4115                                         if (expr == null)
4116                                                 return false;
4117                                         left = expr;
4118                                 } else if (IsSignedType (rb)) {
4119                                         expr = ConvertSignedConstant (right, type);
4120                                         if (expr == null)
4121                                                 return false;
4122                                         right = expr;
4123                                 }
4124
4125                         } else if (lb == BuiltinTypeSpec.Type.Long || rb == BuiltinTypeSpec.Type.Long) {
4126                                 type = rc.BuiltinTypes.Long;
4127                         } else if (lb == BuiltinTypeSpec.Type.UInt || rb == BuiltinTypeSpec.Type.UInt) {
4128                                 type = rc.BuiltinTypes.UInt;
4129
4130                                 if (IsSignedType (lb)) {
4131                                         expr = ConvertSignedConstant (left, type);
4132                                         if (expr == null)
4133                                                 type = rc.BuiltinTypes.Long;
4134                                 } else if (IsSignedType (rb)) {
4135                                         expr = ConvertSignedConstant (right, type);
4136                                         if (expr == null)
4137                                                 type = rc.BuiltinTypes.Long;
4138                                 }
4139                         } else {
4140                                 type = rc.BuiltinTypes.Int;
4141                         }
4142
4143                         if (ltype != type) {
4144                                 expr = PromoteExpression (rc, left, type);
4145                                 if (expr == null)
4146                                         return false;
4147
4148                                 left = expr;
4149                         }
4150
4151                         if (rtype != type) {
4152                                 expr = PromoteExpression (rc, right, type);
4153                                 if (expr == null)
4154                                         return false;
4155
4156                                 right = expr;
4157                         }
4158
4159                         return true;
4160                 }
4161
4162                 static bool IsSignedType (BuiltinTypeSpec.Type type)
4163                 {
4164                         switch (type) {
4165                         case BuiltinTypeSpec.Type.Int:
4166                         case BuiltinTypeSpec.Type.Short:
4167                         case BuiltinTypeSpec.Type.SByte:
4168                         case BuiltinTypeSpec.Type.Long:
4169                                 return true;
4170                         default:
4171                                 return false;
4172                         }
4173                 }
4174
4175                 static Expression ConvertSignedConstant (Expression expr, TypeSpec type)
4176                 {
4177                         var c = expr as Constant;
4178                         if (c == null)
4179                                 return null;
4180
4181                         return c.ConvertImplicitly (type);
4182                 }
4183
4184                 static Expression PromoteExpression (ResolveContext rc, Expression expr, TypeSpec type)
4185                 {
4186                         if (expr.Type.IsNullableType) {
4187                                 return Convert.ImplicitConversionStandard (rc, expr,
4188                                         rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc, new[] { type }), expr.Location);
4189                         }
4190
4191                         var c = expr as Constant;
4192                         if (c != null)
4193                                 return c.ConvertImplicitly (type);
4194
4195                         return Convert.ImplicitNumericConversion (expr, type);
4196                 }
4197
4198                 protected override Expression DoResolve (ResolveContext ec)
4199                 {
4200                         if (left == null)
4201                                 return null;
4202
4203                         if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
4204                                 left = ((ParenthesizedExpression) left).Expr;
4205                                 left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
4206                                 if (left == null)
4207                                         return null;
4208
4209                                 if (left.eclass == ExprClass.Type) {
4210                                         ec.Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
4211                                         return null;
4212                                 }
4213                         } else
4214                                 left = left.Resolve (ec);
4215
4216                         if (left == null)
4217                                 return null;
4218
4219                         right = right.Resolve (ec);
4220                         if (right == null)
4221                                 return null;
4222
4223                         Constant lc = left as Constant;
4224                         Constant rc = right as Constant;
4225
4226                         // The conversion rules are ignored in enum context but why
4227                         if (!ec.HasSet (ResolveContext.Options.EnumScope) && lc != null && rc != null && (left.Type.IsEnum || right.Type.IsEnum)) {
4228                                 lc = EnumLiftUp (ec, lc, rc);
4229                                 if (lc != null)
4230                                         rc = EnumLiftUp (ec, rc, lc);
4231                         }
4232
4233                         if (rc != null && lc != null) {
4234                                 int prev_e = ec.Report.Errors;
4235                                 Expression e = ConstantFold.BinaryFold (ec, oper, lc, rc, loc);
4236                                 if (e != null || ec.Report.Errors != prev_e)
4237                                         return e;
4238                         }
4239
4240                         // Comparison warnings
4241                         if ((oper & Operator.ComparisonMask) != 0) {
4242                                 if (left.Equals (right)) {
4243                                         ec.Report.Warning (1718, 3, loc, "A comparison made to same variable. Did you mean to compare something else?");
4244                                 }
4245                                 CheckOutOfRangeComparison (ec, lc, right.Type);
4246                                 CheckOutOfRangeComparison (ec, rc, left.Type);
4247                         }
4248
4249                         if (left.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic || right.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
4250                                 return DoResolveDynamic (ec);
4251
4252                         return DoResolveCore (ec, left, right);
4253                 }
4254
4255                 Expression DoResolveDynamic (ResolveContext rc)
4256                 {
4257                         var lt = left.Type;
4258                         var rt = right.Type;
4259                         if (lt.Kind == MemberKind.Void || lt == InternalType.MethodGroup || lt == InternalType.AnonymousMethod ||
4260                                 rt.Kind == MemberKind.Void || rt == InternalType.MethodGroup || rt == InternalType.AnonymousMethod) {
4261                                 Error_OperatorCannotBeApplied (rc, left, right);
4262                                 return null;
4263                         }
4264
4265                         Arguments args;
4266
4267                         //
4268                         // Special handling for logical boolean operators which require rhs not to be
4269                         // evaluated based on lhs value
4270                         //
4271                         if ((oper & Operator.LogicalMask) != 0) {
4272                                 Expression cond_left, cond_right, expr;
4273
4274                                 args = new Arguments (2);
4275
4276                                 if (lt.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
4277                                         LocalVariable temp = LocalVariable.CreateCompilerGenerated (lt, rc.CurrentBlock, loc);
4278
4279                                         var cond_args = new Arguments (1);
4280                                         cond_args.Add (new Argument (new SimpleAssign (temp.CreateReferenceExpression (rc, loc), left).Resolve (rc)));
4281
4282                                         //
4283                                         // dynamic && bool => IsFalse (temp = left) ? temp : temp && right;
4284                                         // dynamic || bool => IsTrue (temp = left) ? temp : temp || right;
4285                                         //
4286                                         left = temp.CreateReferenceExpression (rc, loc);
4287                                         if (oper == Operator.LogicalAnd) {
4288                                                 expr = DynamicUnaryConversion.CreateIsFalse (rc, cond_args, loc);
4289                                                 cond_left = left;
4290                                         } else {
4291                                                 expr = DynamicUnaryConversion.CreateIsTrue (rc, cond_args, loc);
4292                                                 cond_left = left;
4293                                         }
4294
4295                                         args.Add (new Argument (left));
4296                                         args.Add (new Argument (right));
4297                                         cond_right = new DynamicExpressionStatement (this, args, loc);
4298                                 } else {
4299                                         LocalVariable temp = LocalVariable.CreateCompilerGenerated (rc.BuiltinTypes.Bool, rc.CurrentBlock, loc);
4300
4301                                         if (!Convert.ImplicitConversionExists (rc, left, temp.Type) && (oper == Operator.LogicalAnd ? GetOperatorFalse (rc, left, loc) : GetOperatorTrue (rc, left, loc)) == null) {
4302                                                 rc.Report.Error (7083, left.Location,
4303                                                         "Expression must be implicitly convertible to Boolean or its type `{0}' must define operator `{1}'",
4304                                                         lt.GetSignatureForError (), oper == Operator.LogicalAnd ? "false" : "true");
4305                                                 return null;
4306                                         }
4307
4308                                         args.Add (new Argument (temp.CreateReferenceExpression (rc, loc).Resolve (rc)));
4309                                         args.Add (new Argument (right));
4310                                         right = new DynamicExpressionStatement (this, args, loc);
4311
4312                                         //
4313                                         // bool && dynamic => (temp = left) ? temp && right : temp;
4314                                         // bool || dynamic => (temp = left) ? temp : temp || right;
4315                                         //
4316                                         if (oper == Operator.LogicalAnd) {
4317                                                 cond_left = right;
4318                                                 cond_right = temp.CreateReferenceExpression (rc, loc);
4319                                         } else {
4320                                                 cond_left = temp.CreateReferenceExpression (rc, loc);
4321                                                 cond_right = right;
4322                                         }
4323
4324                                         expr = new BooleanExpression (new SimpleAssign (temp.CreateReferenceExpression (rc, loc), left));
4325                                 }
4326
4327                                 return new Conditional (expr, cond_left, cond_right, loc).Resolve (rc);
4328                         }
4329
4330                         args = new Arguments (2);
4331                         args.Add (new Argument (left));
4332                         args.Add (new Argument (right));
4333                         return new DynamicExpressionStatement (this, args, loc).Resolve (rc);
4334                 }
4335
4336                 Expression DoResolveCore (ResolveContext ec, Expression left_orig, Expression right_orig)
4337                 {
4338                         Expression expr = ResolveOperator (ec);
4339                         if (expr == null)
4340                                 Error_OperatorCannotBeApplied (ec, left_orig, right_orig);
4341
4342                         if (left == null || right == null)
4343                                 throw new InternalErrorException ("Invalid conversion");
4344
4345                         if (oper == Operator.BitwiseOr)
4346                                 CheckBitwiseOrOnSignExtended (ec);
4347
4348                         return expr;
4349                 }
4350
4351                 public override SLE.Expression MakeExpression (BuilderContext ctx)
4352                 {
4353                         return MakeExpression (ctx, left, right);
4354                 }
4355
4356                 public SLE.Expression MakeExpression (BuilderContext ctx, Expression left, Expression right)
4357                 {
4358                         var le = left.MakeExpression (ctx);
4359                         var re = right.MakeExpression (ctx);
4360                         bool is_checked = ctx.HasSet (BuilderContext.Options.CheckedScope);
4361
4362                         switch (oper) {
4363                         case Operator.Addition:
4364                                 return is_checked ? SLE.Expression.AddChecked (le, re) : SLE.Expression.Add (le, re);
4365                         case Operator.BitwiseAnd:
4366                                 return SLE.Expression.And (le, re);
4367                         case Operator.BitwiseOr:
4368                                 return SLE.Expression.Or (le, re);
4369                         case Operator.Division:
4370                                 return SLE.Expression.Divide (le, re);
4371                         case Operator.Equality:
4372                                 return SLE.Expression.Equal (le, re);
4373                         case Operator.ExclusiveOr:
4374                                 return SLE.Expression.ExclusiveOr (le, re);
4375                         case Operator.GreaterThan:
4376                                 return SLE.Expression.GreaterThan (le, re);
4377                         case Operator.GreaterThanOrEqual:
4378                                 return SLE.Expression.GreaterThanOrEqual (le, re);
4379                         case Operator.Inequality:
4380                                 return SLE.Expression.NotEqual (le, re);
4381                         case Operator.LeftShift:
4382                                 return SLE.Expression.LeftShift (le, re);
4383                         case Operator.LessThan:
4384                                 return SLE.Expression.LessThan (le, re);
4385                         case Operator.LessThanOrEqual:
4386                                 return SLE.Expression.LessThanOrEqual (le, re);
4387                         case Operator.LogicalAnd:
4388                                 return SLE.Expression.AndAlso (le, re);
4389                         case Operator.LogicalOr:
4390                                 return SLE.Expression.OrElse (le, re);
4391                         case Operator.Modulus:
4392                                 return SLE.Expression.Modulo (le, re);
4393                         case Operator.Multiply:
4394                                 return is_checked ? SLE.Expression.MultiplyChecked (le, re) : SLE.Expression.Multiply (le, re);
4395                         case Operator.RightShift:
4396                                 return SLE.Expression.RightShift (le, re);
4397                         case Operator.Subtraction:
4398                                 return is_checked ? SLE.Expression.SubtractChecked (le, re) : SLE.Expression.Subtract (le, re);
4399                         default:
4400                                 throw new NotImplementedException (oper.ToString ());
4401                         }
4402                 }
4403
4404                 //
4405                 // D operator + (D x, D y)
4406                 // D operator - (D x, D y)
4407                 //
4408                 Expression ResolveOperatorDelegate (ResolveContext ec, TypeSpec l, TypeSpec r)
4409                 {
4410                         if (l != r && !TypeSpecComparer.Variant.IsEqual (r, l)) {
4411                                 Expression tmp;
4412                                 if (right.eclass == ExprClass.MethodGroup || r == InternalType.AnonymousMethod || r == InternalType.NullLiteral) {
4413                                         tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
4414                                         if (tmp == null)
4415                                                 return null;
4416                                         right = tmp;
4417                                         r = right.Type;
4418                                 } else if (left.eclass == ExprClass.MethodGroup || (l == InternalType.AnonymousMethod || l == InternalType.NullLiteral)) {
4419                                         tmp = Convert.ImplicitConversionRequired (ec, left, r, loc);
4420                                         if (tmp == null)
4421                                                 return null;
4422                                         left = tmp;
4423                                         l = left.Type;
4424                                 } else {
4425                                         return null;
4426                                 }
4427                         }
4428
4429                         MethodSpec method = null;
4430                         Arguments args = new Arguments (2);
4431                         args.Add (new Argument (left));
4432                         args.Add (new Argument (right));
4433
4434                         if (oper == Operator.Addition) {
4435                                 method = ec.Module.PredefinedMembers.DelegateCombine.Resolve (loc);
4436                         } else if (oper == Operator.Subtraction) {
4437                                 method = ec.Module.PredefinedMembers.DelegateRemove.Resolve (loc);
4438                         }
4439
4440                         if (method == null)
4441                                 return new EmptyExpression (ec.BuiltinTypes.Decimal);
4442
4443                         Expression expr = new UserOperatorCall (method, args, CreateExpressionTree, loc);
4444                         return new ClassCast (expr, l);
4445                 }
4446
4447                 //
4448                 // Resolves enumeration operators where only single predefined overload exists, handles lifted versions too
4449                 //
4450                 Expression ResolveSingleEnumOperators (ResolveContext rc, bool lenum, bool renum, TypeSpec ltype, TypeSpec rtype)
4451                 {
4452                         //
4453                         // bool operator == (E x, E y);
4454                         // bool operator != (E x, E y);
4455                         // bool operator < (E x, E y);
4456                         // bool operator > (E x, E y);
4457                         // bool operator <= (E x, E y);
4458                         // bool operator >= (E x, E y);
4459                         //
4460                         // E operator & (E x, E y);
4461                         // E operator | (E x, E y);
4462                         // E operator ^ (E x, E y);
4463                         //
4464                         Expression expr;
4465                         if ((oper & Operator.ComparisonMask) != 0) {
4466                                 type = rc.BuiltinTypes.Bool;
4467                         } else {
4468                                 if (lenum)
4469                                         type = ltype;
4470                                 else if (renum)
4471                                         type = rtype;
4472                                 else if (ltype.IsNullableType && Nullable.NullableInfo.GetUnderlyingType (ltype).IsEnum)
4473                                         type = ltype;
4474                                 else
4475                                         type = rtype;
4476                         }
4477
4478                         if (ltype == rtype) {
4479                                 if (lenum || renum)
4480                                         return this;
4481
4482                                 var lifted = new Nullable.LiftedBinaryOperator (this);
4483                                 lifted.Left = left;
4484                                 lifted.Right = right;
4485                                 return lifted.Resolve (rc);
4486                         }
4487
4488                         if (renum && !ltype.IsNullableType) {
4489                                 expr = Convert.ImplicitConversion (rc, left, rtype, loc);
4490                                 if (expr != null) {
4491                                         left = expr;
4492                                         return this;
4493                                 }
4494                         } else if (lenum && !rtype.IsNullableType) {
4495                                 expr = Convert.ImplicitConversion (rc, right, ltype, loc);
4496                                 if (expr != null) {
4497                                         right = expr;
4498                                         return this;
4499                                 }
4500                         }
4501
4502                         //
4503                         // Now try lifted version of predefined operator
4504                         //
4505                         var nullable_type = rc.Module.PredefinedTypes.Nullable.TypeSpec;
4506                         if (nullable_type != null) {
4507                                 if (renum && !ltype.IsNullableType) {
4508                                         var lifted_type = nullable_type.MakeGenericType (rc.Module, new[] { rtype });
4509
4510                                         expr = Convert.ImplicitConversion (rc, left, lifted_type, loc);
4511                                         if (expr != null) {
4512                                                 left = expr;
4513                                                 right = Convert.ImplicitConversion (rc, right, lifted_type, loc);
4514                                         }
4515
4516                                         if ((oper & Operator.BitwiseMask) != 0)
4517                                                 type = lifted_type;
4518
4519                                         if (left.IsNull) {
4520                                                 if ((oper & Operator.BitwiseMask) != 0)
4521                                                         return Nullable.LiftedNull.CreateFromExpression (rc, this);
4522
4523                                                 return CreateLiftedValueTypeResult (rc, rtype);
4524                                         }
4525
4526                                         if (expr != null) {
4527                                                 var lifted = new Nullable.LiftedBinaryOperator (this);
4528                                                 lifted.Left = expr;
4529                                                 lifted.Right = right;
4530                                                 return lifted.Resolve (rc);
4531                                         }
4532                                 } else if (lenum && !rtype.IsNullableType) {
4533                                         var lifted_type = nullable_type.MakeGenericType (rc.Module, new[] { ltype });
4534
4535                                         expr = Convert.ImplicitConversion (rc, right, lifted_type, loc);
4536                                         if (expr != null) {
4537                                                 right = expr;
4538                                                 left = Convert.ImplicitConversion (rc, left, lifted_type, loc);
4539                                         }
4540
4541                                         if ((oper & Operator.BitwiseMask) != 0)
4542                                                 type = lifted_type;
4543
4544                                         if (right.IsNull) {
4545                                                 if ((oper & Operator.BitwiseMask) != 0)
4546                                                         return Nullable.LiftedNull.CreateFromExpression (rc, this);
4547
4548                                                 return CreateLiftedValueTypeResult (rc, ltype);
4549                                         }
4550
4551                                         if (expr != null) {
4552                                                 var lifted = new Nullable.LiftedBinaryOperator (this);
4553                                                 lifted.Left = left;
4554                                                 lifted.Right = expr;
4555                                                 return lifted.Resolve (rc);
4556                                         }
4557                                 } else if (rtype.IsNullableType && Nullable.NullableInfo.GetUnderlyingType (rtype).IsEnum) {
4558                                         Nullable.Unwrap unwrap = null;
4559                                         if (left.IsNull || right.IsNull) {
4560                                                 if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion))
4561                                                         left = Convert.ImplicitConversion (rc, left, rtype, left.Location);
4562
4563                                                 if ((oper & Operator.RelationalMask) != 0)
4564                                                         return CreateLiftedValueTypeResult (rc, rtype);
4565
4566                                                 if ((oper & Operator.BitwiseMask) != 0)
4567                                                         return Nullable.LiftedNull.CreateFromExpression (rc, this);
4568
4569                                                 if (right.IsNull)
4570                                                         return CreateLiftedValueTypeResult (rc, left.Type);
4571
4572                                                 // Equality operators are valid between E? and null
4573                                                 expr = left;
4574                                                 unwrap = new Nullable.Unwrap (right);
4575                                         } else {
4576                                                 expr = Convert.ImplicitConversion (rc, left, Nullable.NullableInfo.GetUnderlyingType (rtype), loc);
4577                                                 if (expr == null)
4578                                                         return null;
4579
4580                                                 if ((oper & Operator.BitwiseMask) != 0)
4581                                                         type = rtype;
4582                                         }
4583
4584                                         if (expr != null) {
4585                                                 var lifted = new Nullable.LiftedBinaryOperator (this);
4586                                                 lifted.Left = expr;
4587                                                 lifted.Right = right;
4588                                                 lifted.UnwrapRight = unwrap;
4589                                                 return lifted.Resolve (rc);
4590                                         }
4591                                 } else if (ltype.IsNullableType && Nullable.NullableInfo.GetUnderlyingType (ltype).IsEnum) {
4592                                         Nullable.Unwrap unwrap = null;
4593                                         if (right.IsNull || left.IsNull) {
4594                                                 if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion))
4595                                                         right = Convert.ImplicitConversion (rc, right, ltype, right.Location);
4596
4597                                                 if ((oper & Operator.RelationalMask) != 0)
4598                                                         return CreateLiftedValueTypeResult (rc, ltype);
4599
4600                                                 if ((oper & Operator.BitwiseMask) != 0)
4601                                                         return Nullable.LiftedNull.CreateFromExpression (rc, this);
4602
4603                                                 if (left.IsNull)
4604                                                         return CreateLiftedValueTypeResult (rc, right.Type);
4605
4606                                                 // Equality operators are valid between E? and null
4607                                                 expr = right;
4608                                                 unwrap = new Nullable.Unwrap (left);
4609                                         } else {
4610                                                 expr = Convert.ImplicitConversion (rc, right, Nullable.NullableInfo.GetUnderlyingType (ltype), loc);
4611                                                 if (expr == null)
4612                                                         return null;
4613
4614                                                 if ((oper & Operator.BitwiseMask) != 0)
4615                                                         type = ltype;
4616                                         }
4617
4618                                         if (expr != null) {
4619                                                 var lifted = new Nullable.LiftedBinaryOperator (this);
4620                                                 lifted.Left = left;
4621                                                 lifted.UnwrapLeft = unwrap;
4622                                                 lifted.Right = expr;
4623                                                 return lifted.Resolve (rc);
4624                                         }
4625                                 }
4626                         }
4627
4628                         return null;
4629                 }
4630
4631                 static Expression ConvertEnumOperandToUnderlyingType (ResolveContext rc, Expression expr, bool liftType)
4632                 {
4633                         TypeSpec underlying_type;
4634                         if (expr.Type.IsNullableType) {
4635                                 var nt = Nullable.NullableInfo.GetUnderlyingType (expr.Type);
4636                                 if (nt.IsEnum)
4637                                         underlying_type = EnumSpec.GetUnderlyingType (nt);
4638                                 else
4639                                         underlying_type = nt;
4640                         } else if (expr.Type.IsEnum) {
4641                                 underlying_type = EnumSpec.GetUnderlyingType (expr.Type);
4642                         } else {
4643                                 underlying_type = expr.Type;
4644                         }
4645
4646                         switch (underlying_type.BuiltinType) {
4647                         case BuiltinTypeSpec.Type.SByte:
4648                         case BuiltinTypeSpec.Type.Byte:
4649                         case BuiltinTypeSpec.Type.Short:
4650                         case BuiltinTypeSpec.Type.UShort:
4651                                 underlying_type = rc.BuiltinTypes.Int;
4652                                 break;
4653                         }
4654
4655                         if (expr.Type.IsNullableType || liftType)
4656                                 underlying_type = rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc.Module, new[] { underlying_type });
4657
4658                         if (expr.Type == underlying_type)
4659                                 return expr;
4660
4661                         return EmptyCast.Create (expr, underlying_type);
4662                 }
4663
4664                 Expression ResolveEnumOperators (ResolveContext rc, bool lenum, bool renum, TypeSpec ltype, TypeSpec rtype)
4665                 {
4666                         //
4667                         // U operator - (E e, E f)
4668                         // E operator - (E e, U x)  // Internal decomposition operator
4669                         // E operator - (U x, E e)      // Internal decomposition operator
4670                         //
4671                         // E operator + (E e, U x)
4672                         // E operator + (U x, E e)
4673                         //
4674
4675                         TypeSpec enum_type;
4676
4677                         if (lenum)
4678                                 enum_type = ltype;
4679                         else if (renum)
4680                                 enum_type = rtype;
4681                         else if (ltype.IsNullableType && Nullable.NullableInfo.GetUnderlyingType (ltype).IsEnum)
4682                                 enum_type = ltype;
4683                         else
4684                                 enum_type = rtype;
4685
4686                         Expression expr;
4687                         if (!enum_type.IsNullableType) {
4688                                 expr = ResolveOperatorPredefined (rc, rc.Module.GetPredefinedEnumAritmeticOperators (enum_type, false), false);
4689                                 if (expr != null) {
4690                                         if (oper == Operator.Subtraction)
4691                                                 expr = ConvertEnumSubtractionResult (rc, expr);
4692                                         else
4693                                                 expr = ConvertEnumAdditionalResult (expr, enum_type);
4694
4695                                         enum_conversion = GetEnumResultCast (expr.Type);
4696
4697                                         return expr;
4698                                 }
4699
4700                                 var nullable = rc.Module.PredefinedTypes.Nullable;
4701
4702                                 //
4703                                 // Don't try nullable version when nullable type is undefined
4704                                 //
4705                                 if (!nullable.IsDefined)
4706                                         return null;
4707
4708                                 enum_type = nullable.TypeSpec.MakeGenericType (rc.Module, new[] { enum_type });
4709                         }
4710
4711                         expr = ResolveOperatorPredefined (rc, rc.Module.GetPredefinedEnumAritmeticOperators (enum_type, true), false);
4712                         if (expr != null) {
4713                                 if (oper == Operator.Subtraction)
4714                                         expr = ConvertEnumSubtractionResult (rc, expr);
4715                                 else
4716                                         expr = ConvertEnumAdditionalResult (expr, enum_type);
4717
4718                                 enum_conversion = GetEnumResultCast (expr.Type);
4719                         }
4720
4721                         return expr;
4722                 }
4723
4724                 static Expression ConvertEnumAdditionalResult (Expression expr, TypeSpec enumType)
4725                 {
4726                         return EmptyCast.Create (expr, enumType);
4727                 }
4728
4729                 Expression ConvertEnumSubtractionResult (ResolveContext rc, Expression expr)
4730                 {
4731                         //
4732                         // Enumeration subtraction has different result type based on
4733                         // best overload
4734                         //
4735                         TypeSpec result_type;
4736                         if (left.Type == right.Type) {
4737                                 var c = right as EnumConstant;
4738                                 if (c != null && c.IsZeroInteger && !right.Type.IsEnum) {
4739                                         //
4740                                         // LAMESPEC: This is quite unexpected for expression E - 0 the return type is
4741                                         // E which is not what expressions E - 1 or 0 - E return
4742                                         //
4743                                         result_type = left.Type;
4744                                 } else {
4745                                         result_type = left.Type.IsNullableType ?
4746                                                 Nullable.NullableInfo.GetEnumUnderlyingType (rc.Module, left.Type) :
4747                                                 EnumSpec.GetUnderlyingType (left.Type);
4748                                 }
4749                         } else {
4750                                 if (IsEnumOrNullableEnum (left.Type)) {
4751                                         result_type = left.Type;
4752                                 } else {
4753                                         result_type = right.Type;
4754                                 }
4755
4756                                 if (expr is Nullable.LiftedBinaryOperator && !result_type.IsNullableType)
4757                                         result_type = rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc.Module, new[] { result_type });
4758                         }
4759
4760                         return EmptyCast.Create (expr, result_type);
4761                 }
4762
4763                 public static ConvCast.Mode GetEnumResultCast (TypeSpec type)
4764                 {
4765                         if (type.IsNullableType)
4766                                 type = Nullable.NullableInfo.GetUnderlyingType (type);
4767
4768                         if (type.IsEnum)
4769                                 type = EnumSpec.GetUnderlyingType (type);
4770
4771                         switch (type.BuiltinType) {
4772                         case BuiltinTypeSpec.Type.SByte:
4773                                 return ConvCast.Mode.I4_I1;
4774                         case BuiltinTypeSpec.Type.Byte:
4775                                 return ConvCast.Mode.I4_U1;
4776                         case BuiltinTypeSpec.Type.Short:
4777                                 return ConvCast.Mode.I4_I2;
4778                         case BuiltinTypeSpec.Type.UShort:
4779                                 return ConvCast.Mode.I4_U2;
4780                         }
4781
4782                         return 0;
4783                 }
4784
4785                 //
4786                 // Equality operators rules
4787                 //
4788                 Expression ResolveEquality (ResolveContext ec, TypeSpec l, TypeSpec r, bool primitives_only)
4789                 {
4790                         Expression result;
4791                         type = ec.BuiltinTypes.Bool;
4792                         bool no_arg_conv = false;
4793
4794                         if (!primitives_only) {
4795
4796                                 //
4797                                 // a, Both operands are reference-type values or the value null
4798                                 // b, One operand is a value of type T where T is a type-parameter and
4799                                 // the other operand is the value null. Furthermore T does not have the
4800                                 // value type constraint
4801                                 //
4802                                 // LAMESPEC: Very confusing details in the specification, basically any
4803                                 // reference like type-parameter is allowed
4804                                 //
4805                                 var tparam_l = l as TypeParameterSpec;
4806                                 var tparam_r = r as TypeParameterSpec;
4807                                 if (tparam_l != null) {
4808                                         if (right is NullLiteral) {
4809                                                 if (tparam_l.GetEffectiveBase ().BuiltinType == BuiltinTypeSpec.Type.ValueType)
4810                                                         return null;
4811
4812                                                 left = new BoxedCast (left, ec.BuiltinTypes.Object);
4813                                                 return this;
4814                                         }
4815
4816                                         if (!tparam_l.IsReferenceType)
4817                                                 return null;
4818
4819                                         l = tparam_l.GetEffectiveBase ();
4820                                         left = new BoxedCast (left, l);
4821                                 } else if (left is NullLiteral && tparam_r == null) {
4822                                         if (TypeSpec.IsReferenceType (r))
4823                                                 return this;
4824
4825                                         if (r.Kind == MemberKind.InternalCompilerType)
4826                                                 return null;
4827                                 }
4828
4829                                 if (tparam_r != null) {
4830                                         if (left is NullLiteral) {
4831                                                 if (tparam_r.GetEffectiveBase ().BuiltinType == BuiltinTypeSpec.Type.ValueType)
4832                                                         return null;
4833
4834                                                 right = new BoxedCast (right, ec.BuiltinTypes.Object);
4835                                                 return this;
4836                                         }
4837
4838                                         if (!tparam_r.IsReferenceType)
4839                                                 return null;
4840
4841                                         r = tparam_r.GetEffectiveBase ();
4842                                         right = new BoxedCast (right, r);
4843                                 } else if (right is NullLiteral) {
4844                                         if (TypeSpec.IsReferenceType (l))
4845                                                 return this;
4846
4847                                         if (l.Kind == MemberKind.InternalCompilerType)
4848                                                 return null;
4849                                 }
4850
4851                                 //
4852                                 // LAMESPEC: method groups can be compared when they convert to other side delegate
4853                                 //
4854                                 if (l.IsDelegate) {
4855                                         if (right.eclass == ExprClass.MethodGroup) {
4856                                                 result = Convert.ImplicitConversion (ec, right, l, loc);
4857                                                 if (result == null)
4858                                                         return null;
4859
4860                                                 right = result;
4861                                                 r = l;
4862                                         } else if (r.IsDelegate && l != r) {
4863                                                 return null;
4864                                         }
4865                                 } else if (left.eclass == ExprClass.MethodGroup && r.IsDelegate) {
4866                                         result = Convert.ImplicitConversionRequired (ec, left, r, loc);
4867                                         if (result == null)
4868                                                 return null;
4869
4870                                         left = result;
4871                                         l = r;
4872                                 } else {
4873                                         no_arg_conv = l == r && !l.IsStruct;
4874                                 }
4875                         }
4876
4877                         //
4878                         // bool operator != (string a, string b)
4879                         // bool operator == (string a, string b)
4880                         //
4881                         // bool operator != (Delegate a, Delegate b)
4882                         // bool operator == (Delegate a, Delegate b)
4883                         //
4884                         // bool operator != (bool a, bool b)
4885                         // bool operator == (bool a, bool b)
4886                         //
4887                         // LAMESPEC: Reference equality comparison can apply to value/reference types when
4888                         // they implement an implicit conversion to any of types above. This does
4889                         // not apply when both operands are of same reference type
4890                         //
4891                         if (r.BuiltinType != BuiltinTypeSpec.Type.Object && l.BuiltinType != BuiltinTypeSpec.Type.Object) {
4892                                 result = ResolveOperatorPredefined (ec, ec.BuiltinTypes.OperatorsBinaryEquality, no_arg_conv);  
4893                                 if (result != null)
4894                                         return result;
4895
4896                                 //
4897                                 // Now try lifted version of predefined operators
4898                                 //
4899                                 if (no_arg_conv && !l.IsNullableType) {
4900                                         //
4901                                         // Optimizes cases which won't match
4902                                         //
4903                                 } else {
4904                                         result = ResolveOperatorPredefined (ec, ec.Module.OperatorsBinaryEqualityLifted, no_arg_conv);
4905                                         if (result != null)
4906                                                 return result;
4907                                 }
4908
4909                                 //
4910                                 // The == and != operators permit one operand to be a value of a nullable
4911                                 // type and the other to be the null literal, even if no predefined or user-defined
4912                                 // operator (in unlifted or lifted form) exists for the operation.
4913                                 //
4914                                 if ((l.IsNullableType && right.IsNull) || (r.IsNullableType && left.IsNull)) {
4915                                         var lifted = new Nullable.LiftedBinaryOperator (this);
4916                                         lifted.Left = left;
4917                                         lifted.Right = right;
4918                                         return lifted.Resolve (ec);
4919                                 }
4920                         }
4921
4922                         //
4923                         // bool operator != (object a, object b)
4924                         // bool operator == (object a, object b)
4925                         //
4926                         // An explicit reference conversion exists from the
4927                         // type of either operand to the type of the other operand.
4928                         //
4929
4930                         // Optimize common path
4931                         if (l == r) {
4932                                 return l.Kind == MemberKind.InternalCompilerType || l.Kind == MemberKind.Struct ? null : this;
4933                         }
4934
4935                         if (!Convert.ExplicitReferenceConversionExists (l, r) &&
4936                                 !Convert.ExplicitReferenceConversionExists (r, l))
4937                                 return null;
4938
4939                         // Reject allowed explicit conversions like int->object
4940                         if (!TypeSpec.IsReferenceType (l) || !TypeSpec.IsReferenceType (r))
4941                                 return null;
4942
4943                         if (l.BuiltinType == BuiltinTypeSpec.Type.String || l.BuiltinType == BuiltinTypeSpec.Type.Delegate || l.IsDelegate || MemberCache.GetUserOperator (l, CSharp.Operator.OpType.Equality, false) != null)
4944                                 ec.Report.Warning (253, 2, loc,
4945                                         "Possible unintended reference comparison. Consider casting the right side expression to type `{0}' to get value comparison",
4946                                         l.GetSignatureForError ());
4947
4948                         if (r.BuiltinType == BuiltinTypeSpec.Type.String || r.BuiltinType == BuiltinTypeSpec.Type.Delegate || r.IsDelegate || MemberCache.GetUserOperator (r, CSharp.Operator.OpType.Equality, false) != null)
4949                                 ec.Report.Warning (252, 2, loc,
4950                                         "Possible unintended reference comparison. Consider casting the left side expression to type `{0}' to get value comparison",
4951                                         r.GetSignatureForError ());
4952
4953                         return this;
4954                 }
4955
4956
4957                 Expression ResolveOperatorPointer (ResolveContext ec, TypeSpec l, TypeSpec r)
4958                 {
4959                         //
4960                         // bool operator == (void* x, void* y);
4961                         // bool operator != (void* x, void* y);
4962                         // bool operator < (void* x, void* y);
4963                         // bool operator > (void* x, void* y);
4964                         // bool operator <= (void* x, void* y);
4965                         // bool operator >= (void* x, void* y);
4966                         //
4967                         if ((oper & Operator.ComparisonMask) != 0) {
4968                                 Expression temp;
4969                                 if (!l.IsPointer) {
4970                                         temp = Convert.ImplicitConversion (ec, left, r, left.Location);
4971                                         if (temp == null)
4972                                                 return null;
4973                                         left = temp;
4974                                 }
4975
4976                                 if (!r.IsPointer) {
4977                                         temp = Convert.ImplicitConversion (ec, right, l, right.Location);
4978                                         if (temp == null)
4979                                                 return null;
4980                                         right = temp;
4981                                 }
4982
4983                                 type = ec.BuiltinTypes.Bool;
4984                                 return this;
4985                         }
4986
4987                         return ResolveOperatorPredefined (ec, ec.BuiltinTypes.OperatorsBinaryUnsafe, false);
4988                 }
4989
4990                 //
4991                 // Build-in operators method overloading
4992                 //
4993                 Expression ResolveOperatorPredefined (ResolveContext ec, PredefinedOperator [] operators, bool primitives_only)
4994                 {
4995                         PredefinedOperator best_operator = null;
4996                         TypeSpec l = left.Type;
4997                         TypeSpec r = right.Type;
4998                         Operator oper_mask = oper & ~Operator.ValuesOnlyMask;
4999
5000                         foreach (PredefinedOperator po in operators) {
5001                                 if ((po.OperatorsMask & oper_mask) == 0)
5002                                         continue;
5003
5004                                 if (primitives_only) {
5005                                         if (!po.IsPrimitiveApplicable (l, r))
5006                                                 continue;
5007                                 } else {
5008                                         if (!po.IsApplicable (ec, left, right))
5009                                                 continue;
5010                                 }
5011
5012                                 if (best_operator == null) {
5013                                         best_operator = po;
5014                                         if (primitives_only)
5015                                                 break;
5016
5017                                         continue;
5018                                 }
5019
5020                                 best_operator = po.ResolveBetterOperator (ec, best_operator);
5021
5022                                 if (best_operator == null) {
5023                                         ec.Report.Error (34, loc, "Operator `{0}' is ambiguous on operands of type `{1}' and `{2}'",
5024                                                 OperName (oper), l.GetSignatureForError (), r.GetSignatureForError ());
5025
5026                                         best_operator = po;
5027                                         break;
5028                                 }
5029                         }
5030
5031                         if (best_operator == null)
5032                                 return null;
5033
5034                         return best_operator.ConvertResult (ec, this);
5035                 }
5036
5037                 //
5038                 // Optimize & constant expressions with 0 value
5039                 //
5040                 Expression OptimizeAndOperation (Expression expr)
5041                 {
5042                         Constant rc = right as Constant;
5043                         Constant lc = left as Constant;
5044                         if ((lc != null && lc.IsDefaultValue) || (rc != null && rc.IsDefaultValue)) {
5045                                 //
5046                                 // The result is a constant with side-effect
5047                                 //
5048                                 Constant side_effect = rc == null ?
5049                                         new SideEffectConstant (lc, right, loc) :
5050                                         new SideEffectConstant (rc, left, loc);
5051
5052                                 return ReducedExpression.Create (side_effect, expr);
5053                         }
5054
5055                         return expr;
5056                 }
5057
5058                 //
5059                 // Value types can be compared with the null literal because of the lifting
5060                 // language rules. However the result is always true or false.
5061                 //
5062                 public Expression CreateLiftedValueTypeResult (ResolveContext rc, TypeSpec valueType)
5063                 {
5064                         if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion)) {
5065                                 type = rc.BuiltinTypes.Bool;
5066                                 return this;
5067                         }
5068
5069                         // FIXME: Handle side effect constants
5070                         Constant c = new BoolConstant (rc.BuiltinTypes, Oper == Operator.Inequality, loc);
5071
5072                         if ((Oper & Operator.EqualityMask) != 0) {
5073                                 rc.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is always `{1}'",
5074                                         valueType.GetSignatureForError (), c.GetValueAsLiteral ());
5075                         } else {
5076                                 rc.Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
5077                                         valueType.GetSignatureForError (), c.GetValueAsLiteral ());
5078                         }
5079
5080                         return c;
5081                 }
5082
5083                 //
5084                 // Performs user-operator overloading
5085                 //
5086                 Expression ResolveUserOperator (ResolveContext rc, Expression left, Expression right)
5087                 {
5088                         Expression oper_expr;
5089
5090                         var op = ConvertBinaryToUserOperator (oper);
5091                         var l = left.Type;
5092                         if (l.IsNullableType)
5093                                 l = Nullable.NullableInfo.GetUnderlyingType (l);
5094                         var r = right.Type;
5095                         if (r.IsNullableType)
5096                                 r = Nullable.NullableInfo.GetUnderlyingType (r);
5097
5098                         IList<MemberSpec> left_operators = MemberCache.GetUserOperator (l, op, false);
5099                         IList<MemberSpec> right_operators = null;
5100
5101                         if (l != r) {
5102                                 right_operators = MemberCache.GetUserOperator (r, op, false);
5103                                 if (right_operators == null && left_operators == null)
5104                                         return null;
5105                         } else if (left_operators == null) {
5106                                 return null;
5107                         }
5108
5109                         Arguments args = new Arguments (2);
5110                         Argument larg = new Argument (left);
5111                         args.Add (larg);        
5112                         Argument rarg = new Argument (right);
5113                         args.Add (rarg);
5114
5115                         //
5116                         // User-defined operator implementations always take precedence
5117                         // over predefined operator implementations
5118                         //
5119                         if (left_operators != null && right_operators != null) {
5120                                 left_operators = CombineUserOperators (left_operators, right_operators);
5121                         } else if (right_operators != null) {
5122                                 left_operators = right_operators;
5123                         }
5124
5125                         const OverloadResolver.Restrictions restr = OverloadResolver.Restrictions.ProbingOnly |
5126                                 OverloadResolver.Restrictions.NoBaseMembers | OverloadResolver.Restrictions.BaseMembersIncluded;
5127
5128                         var res = new OverloadResolver (left_operators, restr, loc);
5129
5130                         var oper_method = res.ResolveOperator (rc, ref args);
5131                         if (oper_method == null) {
5132                                 //
5133                                 // Logical && and || cannot be lifted
5134                                 //
5135                                 if ((oper & Operator.LogicalMask) != 0)
5136                                         return null;
5137
5138                                 //
5139                                 // Apply lifted user operators only for liftable types. Implicit conversion
5140                                 // to nullable types is not allowed
5141                                 //
5142                                 if (!IsLiftedOperatorApplicable ())
5143                                         return null;
5144
5145                                 // TODO: Cache the result in module container
5146                                 var lifted_methods = CreateLiftedOperators (rc, left_operators);
5147                                 if (lifted_methods == null)
5148                                         return null;
5149
5150                                 res = new OverloadResolver (lifted_methods, restr | OverloadResolver.Restrictions.ProbingOnly, loc);
5151
5152                                 oper_method = res.ResolveOperator (rc, ref args);
5153                                 if (oper_method == null)
5154                                         return null;
5155
5156                                 MethodSpec best_original = null;
5157                                 foreach (MethodSpec ms in left_operators) {
5158                                         if (ms.MemberDefinition == oper_method.MemberDefinition) {
5159                                                 best_original = ms;
5160                                                 break;
5161                                         }
5162                                 }
5163
5164                                 if (rc.HasSet (ResolveContext.Options.ExpressionTreeConversion)) {
5165                                         //
5166                                         // Expression trees use lifted notation in this case
5167                                         //
5168                                         this.left = Convert.ImplicitConversion (rc, left, oper_method.Parameters.Types[0], left.Location);
5169                                         this.right = Convert.ImplicitConversion (rc, right, oper_method.Parameters.Types[1], left.Location);
5170                                 }
5171
5172                                 var ptypes = best_original.Parameters.Types;
5173
5174                                 if (left.IsNull || right.IsNull) {
5175                                         //
5176                                         // The lifted operator produces a null value if one or both operands are null
5177                                         //
5178                                         if ((oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0) {
5179                                                 type = oper_method.ReturnType;
5180                                                 return Nullable.LiftedNull.CreateFromExpression (rc, this);
5181                                         }
5182
5183                                         //
5184                                         // The lifted operator produces the value false if one or both operands are null for
5185                                         // relational operators.
5186                                         //
5187                                         if ((oper & Operator.RelationalMask) != 0) {
5188                                                 //
5189                                                 // CSC BUG: This should be different warning, csc reports CS0458 with bool? which is wrong
5190                                                 // because return type is actually bool
5191                                                 //
5192                                                 return CreateLiftedValueTypeResult (rc, left.IsNull ? ptypes [1] : ptypes [0]);
5193                                         }
5194
5195                                         if ((oper & Operator.EqualityMask) != 0 && ((left.IsNull && !right.Type.IsNullableType) || !left.Type.IsNullableType)) {
5196                                                 return CreateLiftedValueTypeResult (rc, left.IsNull ? ptypes [1] : ptypes [0]);
5197                                         }
5198                                 }
5199
5200                                 type = oper_method.ReturnType;
5201                                 var lifted = new Nullable.LiftedBinaryOperator (this);
5202                                 lifted.UserOperator = best_original;
5203
5204                                 if (left.Type.IsNullableType && !ptypes[0].IsNullableType) {
5205                                         lifted.UnwrapLeft = new Nullable.Unwrap (left);
5206                                 }
5207
5208                                 if (right.Type.IsNullableType && !ptypes[1].IsNullableType) {
5209                                         lifted.UnwrapRight = new Nullable.Unwrap (right);
5210                                 }
5211
5212                                 lifted.Left = Convert.ImplicitConversion (rc, lifted.UnwrapLeft ?? left, ptypes[0], left.Location);
5213                                 lifted.Right = Convert.ImplicitConversion (rc, lifted.UnwrapRight ?? right, ptypes[1], right.Location);
5214
5215                                 return lifted.Resolve (rc);
5216                         }
5217                         
5218                         if ((oper & Operator.LogicalMask) != 0) {
5219                                 // TODO: CreateExpressionTree is allocated every time           
5220                                 oper_expr = new ConditionalLogicalOperator (oper_method, args, CreateExpressionTree,
5221                                         oper == Operator.LogicalAnd, loc).Resolve (rc);
5222                         } else {
5223                                 oper_expr = new UserOperatorCall (oper_method, args, CreateExpressionTree, loc);
5224                         }
5225
5226                         this.left = larg.Expr;
5227                         this.right = rarg.Expr;
5228
5229                         return oper_expr;
5230                 }
5231
5232                 bool IsLiftedOperatorApplicable ()
5233                 {
5234                         if (left.Type.IsNullableType) {
5235                                 if ((oper & Operator.EqualityMask) != 0)
5236                                         return !right.IsNull;
5237
5238                                 return true;
5239                         }
5240
5241                         if (right.Type.IsNullableType) {
5242                                 if ((oper & Operator.EqualityMask) != 0)
5243                                         return !left.IsNull;
5244
5245                                 return true;
5246                         }
5247
5248                         if (TypeSpec.IsValueType (left.Type))
5249                                 return right.IsNull;
5250
5251                         if (TypeSpec.IsValueType (right.Type))
5252                                 return left.IsNull;
5253
5254                         return false;
5255                 }
5256
5257                 List<MemberSpec> CreateLiftedOperators (ResolveContext rc, IList<MemberSpec> operators)
5258                 {
5259                         var nullable_type = rc.Module.PredefinedTypes.Nullable.TypeSpec;
5260                         if (nullable_type == null)
5261                                 return null;
5262
5263                         //
5264                         // Lifted operators permit predefined and user-defined operators that operate
5265                         // on non-nullable value types to also be used with nullable forms of those types.
5266                         // Lifted operators are constructed from predefined and user-defined operators
5267                         // that meet certain requirements
5268                         //
5269                         List<MemberSpec> lifted = null;
5270                         foreach (MethodSpec oper in operators) {
5271                                 TypeSpec rt;
5272                                 if ((Oper & Operator.ComparisonMask) != 0) {
5273                                         //
5274                                         // Result type must be of type bool for lifted comparison operators
5275                                         //
5276                                         rt = oper.ReturnType;
5277                                         if (rt.BuiltinType != BuiltinTypeSpec.Type.Bool)
5278                                                 continue;
5279                                 } else {
5280                                         if (!TypeSpec.IsNonNullableValueType (oper.ReturnType))
5281                                                 continue;
5282
5283                                         rt = null;
5284                                 }
5285
5286                                 var ptypes = oper.Parameters.Types;
5287                                 if (!TypeSpec.IsNonNullableValueType (ptypes [0]) || !TypeSpec.IsNonNullableValueType (ptypes [1]))
5288                                         continue;
5289
5290                                 //
5291                                 // LAMESPEC: I am not sure why but for equality operators to be lifted
5292                                 // both types have to match
5293                                 //
5294                                 if ((Oper & Operator.EqualityMask) != 0 && ptypes [0] != ptypes [1])
5295                                         continue;
5296
5297                                 if (lifted == null)
5298                                         lifted = new List<MemberSpec> ();
5299
5300                                 //
5301                                 // The lifted form is constructed by adding a single ? modifier to each operand and
5302                                 // result type except for comparison operators where return type is bool
5303                                 //
5304                                 if (rt == null)
5305                                         rt = nullable_type.MakeGenericType (rc.Module, new[] { oper.ReturnType });
5306
5307                                 var parameters = ParametersCompiled.CreateFullyResolved (
5308                                         nullable_type.MakeGenericType (rc.Module, new [] { ptypes[0] }),
5309                                         nullable_type.MakeGenericType (rc.Module, new [] { ptypes[1] }));
5310
5311                                 var lifted_op = new MethodSpec (oper.Kind, oper.DeclaringType, oper.MemberDefinition,
5312                                         rt, parameters, oper.Modifiers);
5313
5314                                 lifted.Add (lifted_op);
5315                         }
5316
5317                         return lifted;
5318                 }
5319
5320                 //
5321                 // Merge two sets of user operators into one, they are mostly distinguish
5322                 // except when they share base type and it contains an operator
5323                 //
5324                 static IList<MemberSpec> CombineUserOperators (IList<MemberSpec> left, IList<MemberSpec> right)
5325                 {
5326                         var combined = new List<MemberSpec> (left.Count + right.Count);
5327                         combined.AddRange (left);
5328                         foreach (var r in right) {
5329                                 bool same = false;
5330                                 foreach (var l in left) {
5331                                         if (l.DeclaringType == r.DeclaringType) {
5332                                                 same = true;
5333                                                 break;
5334                                         }
5335                                 }
5336
5337                                 if (!same)
5338                                         combined.Add (r);
5339                         }
5340
5341                         return combined;
5342                 }
5343
5344                 void CheckOutOfRangeComparison (ResolveContext ec, Constant c, TypeSpec type)
5345                 {
5346                         if (c is IntegralConstant || c is CharConstant) {
5347                                 try {
5348                                         c.ConvertExplicitly (true, type);
5349                                 } catch (OverflowException) {
5350                                         ec.Report.Warning (652, 2, loc,
5351                                                 "A comparison between a constant and a variable is useless. The constant is out of the range of the variable type `{0}'",
5352                                                 type.GetSignatureForError ());
5353                                 }
5354                         }
5355                 }
5356
5357                 /// <remarks>
5358                 ///   EmitBranchable is called from Statement.EmitBoolExpression in the
5359                 ///   context of a conditional bool expression.  This function will return
5360                 ///   false if it is was possible to use EmitBranchable, or true if it was.
5361                 ///
5362                 ///   The expression's code is generated, and we will generate a branch to `target'
5363                 ///   if the resulting expression value is equal to isTrue
5364                 /// </remarks>
5365                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
5366                 {
5367                         if (ec.HasSet (BuilderContext.Options.AsyncBody) && right.ContainsEmitWithAwait ()) {
5368                                 left = left.EmitToField (ec);
5369
5370                                 if ((oper & Operator.LogicalMask) == 0) {
5371                                         right = right.EmitToField (ec);
5372                                 }
5373                         }
5374
5375                         //
5376                         // This is more complicated than it looks, but its just to avoid
5377                         // duplicated tests: basically, we allow ==, !=, >, <, >= and <=
5378                         // but on top of that we want for == and != to use a special path
5379                         // if we are comparing against null
5380                         //
5381                         if ((oper & Operator.EqualityMask) != 0 && (left is Constant || right is Constant)) {
5382                                 bool my_on_true = oper == Operator.Inequality ? on_true : !on_true;
5383                                 
5384                                 //
5385                                 // put the constant on the rhs, for simplicity
5386                                 //
5387                                 if (left is Constant) {
5388                                         Expression swap = right;
5389                                         right = left;
5390                                         left = swap;
5391                                 }
5392                                 
5393                                 //
5394                                 // brtrue/brfalse works with native int only
5395                                 //
5396                                 if (((Constant) right).IsZeroInteger && right.Type.BuiltinType != BuiltinTypeSpec.Type.Long && right.Type.BuiltinType != BuiltinTypeSpec.Type.ULong) {
5397                                         left.EmitBranchable (ec, target, my_on_true);
5398                                         return;
5399                                 }
5400                                 if (right.Type.BuiltinType == BuiltinTypeSpec.Type.Bool) {
5401                                         // right is a boolean, and it's not 'false' => it is 'true'
5402                                         left.EmitBranchable (ec, target, !my_on_true);
5403                                         return;
5404                                 }
5405
5406                         } else if (oper == Operator.LogicalAnd) {
5407
5408                                 if (on_true) {
5409                                         Label tests_end = ec.DefineLabel ();
5410                                         
5411                                         left.EmitBranchable (ec, tests_end, false);
5412                                         right.EmitBranchable (ec, target, true);
5413                                         ec.MarkLabel (tests_end);                                       
5414                                 } else {
5415                                         //
5416                                         // This optimizes code like this 
5417                                         // if (true && i > 4)
5418                                         //
5419                                         if (!(left is Constant))
5420                                                 left.EmitBranchable (ec, target, false);
5421
5422                                         if (!(right is Constant)) 
5423                                                 right.EmitBranchable (ec, target, false);
5424                                 }
5425                                 
5426                                 return;
5427                                 
5428                         } else if (oper == Operator.LogicalOr){
5429                                 if (on_true) {
5430                                         left.EmitBranchable (ec, target, true);
5431                                         right.EmitBranchable (ec, target, true);
5432                                         
5433                                 } else {
5434                                         Label tests_end = ec.DefineLabel ();
5435                                         left.EmitBranchable (ec, tests_end, true);
5436                                         right.EmitBranchable (ec, target, false);
5437                                         ec.MarkLabel (tests_end);
5438                                 }
5439                                 
5440                                 return;
5441
5442                         } else if ((oper & Operator.ComparisonMask) == 0) {
5443                                 base.EmitBranchable (ec, target, on_true);
5444                                 return;
5445                         }
5446                         
5447                         left.Emit (ec);
5448                         right.Emit (ec);
5449
5450                         TypeSpec t = left.Type;
5451                         bool is_float = IsFloat (t);
5452                         bool is_unsigned = is_float || IsUnsigned (t);
5453                         
5454                         switch (oper){
5455                         case Operator.Equality:
5456                                 if (on_true)
5457                                         ec.Emit (OpCodes.Beq, target);
5458                                 else
5459                                         ec.Emit (OpCodes.Bne_Un, target);
5460                                 break;
5461
5462                         case Operator.Inequality:
5463                                 if (on_true)
5464                                         ec.Emit (OpCodes.Bne_Un, target);
5465                                 else
5466                                         ec.Emit (OpCodes.Beq, target);
5467                                 break;
5468
5469                         case Operator.LessThan:
5470                                 if (on_true)
5471                                         if (is_unsigned && !is_float)
5472                                                 ec.Emit (OpCodes.Blt_Un, target);
5473                                         else
5474                                                 ec.Emit (OpCodes.Blt, target);
5475                                 else
5476                                         if (is_unsigned)
5477                                                 ec.Emit (OpCodes.Bge_Un, target);
5478                                         else
5479                                                 ec.Emit (OpCodes.Bge, target);
5480                                 break;
5481
5482                         case Operator.GreaterThan:
5483                                 if (on_true)
5484                                         if (is_unsigned && !is_float)
5485                                                 ec.Emit (OpCodes.Bgt_Un, target);
5486                                         else
5487                                                 ec.Emit (OpCodes.Bgt, target);
5488                                 else
5489                                         if (is_unsigned)
5490                                                 ec.Emit (OpCodes.Ble_Un, target);
5491                                         else
5492                                                 ec.Emit (OpCodes.Ble, target);
5493                                 break;
5494
5495                         case Operator.LessThanOrEqual:
5496                                 if (on_true)
5497                                         if (is_unsigned && !is_float)
5498                                                 ec.Emit (OpCodes.Ble_Un, target);
5499                                         else
5500                                                 ec.Emit (OpCodes.Ble, target);
5501                                 else
5502                                         if (is_unsigned)
5503                                                 ec.Emit (OpCodes.Bgt_Un, target);
5504                                         else
5505                                                 ec.Emit (OpCodes.Bgt, target);
5506                                 break;
5507
5508
5509                         case Operator.GreaterThanOrEqual:
5510                                 if (on_true)
5511                                         if (is_unsigned && !is_float)
5512                                                 ec.Emit (OpCodes.Bge_Un, target);
5513                                         else
5514                                                 ec.Emit (OpCodes.Bge, target);
5515                                 else
5516                                         if (is_unsigned)
5517                                                 ec.Emit (OpCodes.Blt_Un, target);
5518                                         else
5519                                                 ec.Emit (OpCodes.Blt, target);
5520                                 break;
5521                         default:
5522                                 throw new InternalErrorException (oper.ToString ());
5523                         }
5524                 }
5525                 
5526                 public override void Emit (EmitContext ec)
5527                 {
5528                         if (ec.HasSet (BuilderContext.Options.AsyncBody) && right.ContainsEmitWithAwait ()) {
5529                                 left = left.EmitToField (ec);
5530
5531                                 if ((oper & Operator.LogicalMask) == 0) {
5532                                         right = right.EmitToField (ec);
5533                                 }
5534                         }
5535
5536                         //
5537                         // Handle short-circuit operators differently
5538                         // than the rest
5539                         //
5540                         if ((oper & Operator.LogicalMask) != 0) {
5541                                 Label load_result = ec.DefineLabel ();
5542                                 Label end = ec.DefineLabel ();
5543
5544                                 bool is_or = oper == Operator.LogicalOr;
5545                                 left.EmitBranchable (ec, load_result, is_or);
5546                                 right.Emit (ec);
5547                                 ec.Emit (OpCodes.Br_S, end);
5548                                 
5549                                 ec.MarkLabel (load_result);
5550                                 ec.EmitInt (is_or ? 1 : 0);
5551                                 ec.MarkLabel (end);
5552                                 return;
5553                         }
5554
5555                         //
5556                         // Optimize zero-based operations which cannot be optimized at expression level
5557                         //
5558                         if (oper == Operator.Subtraction) {
5559                                 var lc = left as IntegralConstant;
5560                                 if (lc != null && lc.IsDefaultValue) {
5561                                         right.Emit (ec);
5562                                         ec.Emit (OpCodes.Neg);
5563                                         return;
5564                                 }
5565                         }
5566
5567                         EmitOperator (ec, left, right);
5568                 }
5569
5570                 public void EmitOperator (EmitContext ec, Expression left, Expression right)
5571                 {
5572                         left.Emit (ec);
5573                         right.Emit (ec);
5574
5575                         EmitOperatorOpcode (ec, oper, left.Type, right);
5576
5577                         //
5578                         // Emit result enumerable conversion this way because it's quite complicated get it
5579                         // to resolved tree because expression tree cannot see it.
5580                         //
5581                         if (enum_conversion != 0)
5582                                 ConvCast.Emit (ec, enum_conversion);
5583                 }
5584
5585                 public override void EmitSideEffect (EmitContext ec)
5586                 {
5587                         if ((oper & Operator.LogicalMask) != 0 ||
5588                                 (ec.HasSet (EmitContext.Options.CheckedScope) && (oper == Operator.Multiply || oper == Operator.Addition || oper == Operator.Subtraction))) {
5589                                 base.EmitSideEffect (ec);
5590                         } else {
5591                                 left.EmitSideEffect (ec);
5592                                 right.EmitSideEffect (ec);
5593                         }
5594                 }
5595
5596                 public override Expression EmitToField (EmitContext ec)
5597                 {
5598                         if ((oper & Operator.LogicalMask) == 0) {
5599                                 var await_expr = left as Await;
5600                                 if (await_expr != null && right.IsSideEffectFree) {
5601                                         await_expr.Statement.EmitPrologue (ec);
5602                                         left = await_expr.Statement.GetResultExpression (ec);
5603                                         return this;
5604                                 }
5605
5606                                 await_expr = right as Await;
5607                                 if (await_expr != null && left.IsSideEffectFree) {
5608                                         await_expr.Statement.EmitPrologue (ec);
5609                                         right = await_expr.Statement.GetResultExpression (ec);
5610                                         return this;
5611                                 }
5612                         }
5613
5614                         return base.EmitToField (ec);
5615                 }
5616
5617                 protected override void CloneTo (CloneContext clonectx, Expression t)
5618                 {
5619                         Binary target = (Binary) t;
5620
5621                         target.left = left.Clone (clonectx);
5622                         target.right = right.Clone (clonectx);
5623                 }
5624
5625                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
5626                 {
5627                         Arguments binder_args = new Arguments (4);
5628
5629                         MemberAccess sle = new MemberAccess (new MemberAccess (
5630                                 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
5631
5632                         CSharpBinderFlags flags = 0;
5633                         if (ec.HasSet (ResolveContext.Options.CheckedScope))
5634                                 flags = CSharpBinderFlags.CheckedContext;
5635
5636                         if ((oper & Operator.LogicalMask) != 0)
5637                                 flags |= CSharpBinderFlags.BinaryOperationLogical;
5638
5639                         binder_args.Add (new Argument (new EnumConstant (new IntLiteral (ec.BuiltinTypes, (int) flags, loc), ec.Module.PredefinedTypes.BinderFlags.Resolve ())));
5640                         binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), GetOperatorExpressionTypeName (), loc)));
5641                         binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));                                                                      
5642                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
5643
5644                         return new Invocation (new MemberAccess (new TypeExpression (ec.Module.PredefinedTypes.Binder.TypeSpec, loc), "BinaryOperation", loc), binder_args);
5645                 }
5646                 
5647                 public override Expression CreateExpressionTree (ResolveContext ec)
5648                 {
5649                         return CreateExpressionTree (ec, null);
5650                 }
5651
5652                 public Expression CreateExpressionTree (ResolveContext ec, Expression method)           
5653                 {
5654                         string method_name;
5655                         bool lift_arg = false;
5656                         
5657                         switch (oper) {
5658                         case Operator.Addition:
5659                                 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
5660                                         method_name = "AddChecked";
5661                                 else
5662                                         method_name = "Add";
5663                                 break;
5664                         case Operator.BitwiseAnd:
5665                                 method_name = "And";
5666                                 break;
5667                         case Operator.BitwiseOr:
5668                                 method_name = "Or";
5669                                 break;
5670                         case Operator.Division:
5671                                 method_name = "Divide";
5672                                 break;
5673                         case Operator.Equality:
5674                                 method_name = "Equal";
5675                                 lift_arg = true;
5676                                 break;
5677                         case Operator.ExclusiveOr:
5678                                 method_name = "ExclusiveOr";
5679                                 break;                          
5680                         case Operator.GreaterThan:
5681                                 method_name = "GreaterThan";
5682                                 lift_arg = true;
5683                                 break;
5684                         case Operator.GreaterThanOrEqual:
5685                                 method_name = "GreaterThanOrEqual";
5686                                 lift_arg = true;
5687                                 break;
5688                         case Operator.Inequality:
5689                                 method_name = "NotEqual";
5690                                 lift_arg = true;
5691                                 break;
5692                         case Operator.LeftShift:
5693                                 method_name = "LeftShift";
5694                                 break;
5695                         case Operator.LessThan:
5696                                 method_name = "LessThan";
5697                                 lift_arg = true;
5698                                 break;
5699                         case Operator.LessThanOrEqual:
5700                                 method_name = "LessThanOrEqual";
5701                                 lift_arg = true;
5702                                 break;
5703                         case Operator.LogicalAnd:
5704                                 method_name = "AndAlso";
5705                                 break;
5706                         case Operator.LogicalOr:
5707                                 method_name = "OrElse";
5708                                 break;
5709                         case Operator.Modulus:
5710                                 method_name = "Modulo";
5711                                 break;
5712                         case Operator.Multiply:
5713                                 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
5714                                         method_name = "MultiplyChecked";
5715                                 else
5716                                         method_name = "Multiply";
5717                                 break;
5718                         case Operator.RightShift:
5719                                 method_name = "RightShift";
5720                                 break;
5721                         case Operator.Subtraction:
5722                                 if (method == null && ec.HasSet (ResolveContext.Options.CheckedScope) && !IsFloat (type))
5723                                         method_name = "SubtractChecked";
5724                                 else
5725                                         method_name = "Subtract";
5726                                 break;
5727
5728                         default:
5729                                 throw new InternalErrorException ("Unknown expression tree binary operator " + oper);
5730                         }
5731
5732                         Arguments args = new Arguments (2);
5733                         args.Add (new Argument (left.CreateExpressionTree (ec)));
5734                         args.Add (new Argument (right.CreateExpressionTree (ec)));
5735                         if (method != null) {
5736                                 if (lift_arg)
5737                                         args.Add (new Argument (new BoolLiteral (ec.BuiltinTypes, false, loc)));
5738
5739                                 args.Add (new Argument (method));
5740                         }
5741                         
5742                         return CreateExpressionFactoryCall (ec, method_name, args);
5743                 }
5744                 
5745                 public override object Accept (StructuralVisitor visitor)
5746                 {
5747                         return visitor.Visit (this);
5748                 }
5749
5750         }
5751         
5752         //
5753         // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
5754         // b, c, d... may be strings or objects.
5755         //
5756         public class StringConcat : Expression
5757         {
5758                 Arguments arguments;
5759                 
5760                 StringConcat (Location loc)
5761                 {
5762                         this.loc = loc;
5763                         arguments = new Arguments (2);
5764                 }
5765
5766                 public override bool ContainsEmitWithAwait ()
5767                 {
5768                         return arguments.ContainsEmitWithAwait ();
5769                 }
5770
5771                 public static StringConcat Create (ResolveContext rc, Expression left, Expression right, Location loc)
5772                 {
5773                         if (left.eclass == ExprClass.Unresolved || right.eclass == ExprClass.Unresolved)
5774                                 throw new ArgumentException ();
5775
5776                         var s = new StringConcat (loc);
5777                         s.type = rc.BuiltinTypes.String;
5778                         s.eclass = ExprClass.Value;
5779
5780                         s.Append (rc, left);
5781                         s.Append (rc, right);
5782                         return s;
5783                 }
5784
5785                 public override Expression CreateExpressionTree (ResolveContext ec)
5786                 {
5787                         Argument arg = arguments [0];
5788                         return CreateExpressionAddCall (ec, arg, arg.CreateExpressionTree (ec), 1);
5789                 }
5790
5791                 //
5792                 // Creates nested calls tree from an array of arguments used for IL emit
5793                 //
5794                 Expression CreateExpressionAddCall (ResolveContext ec, Argument left, Expression left_etree, int pos)
5795                 {
5796                         Arguments concat_args = new Arguments (2);
5797                         Arguments add_args = new Arguments (3);
5798
5799                         concat_args.Add (left);
5800                         add_args.Add (new Argument (left_etree));
5801
5802                         concat_args.Add (arguments [pos]);
5803                         add_args.Add (new Argument (arguments [pos].CreateExpressionTree (ec)));
5804
5805                         var methods = GetConcatMethodCandidates ();
5806                         if (methods == null)
5807                                 return null;
5808
5809                         var res = new OverloadResolver (methods, OverloadResolver.Restrictions.NoBaseMembers, loc);
5810                         var method = res.ResolveMember<MethodSpec> (ec, ref concat_args);
5811                         if (method == null)
5812                                 return null;
5813
5814                         add_args.Add (new Argument (new TypeOfMethod (method, loc)));
5815
5816                         Expression expr = CreateExpressionFactoryCall (ec, "Add", add_args);
5817                         if (++pos == arguments.Count)
5818                                 return expr;
5819
5820                         left = new Argument (new EmptyExpression (method.ReturnType));
5821                         return CreateExpressionAddCall (ec, left, expr, pos);
5822                 }
5823
5824                 protected override Expression DoResolve (ResolveContext ec)
5825                 {
5826                         return this;
5827                 }
5828                 
5829                 void Append (ResolveContext rc, Expression operand)
5830                 {
5831                         //
5832                         // Constant folding
5833                         //
5834                         StringConstant sc = operand as StringConstant;
5835                         if (sc != null) {
5836                                 if (arguments.Count != 0) {
5837                                         Argument last_argument = arguments [arguments.Count - 1];
5838                                         StringConstant last_expr_constant = last_argument.Expr as StringConstant;
5839                                         if (last_expr_constant != null) {
5840                                                 last_argument.Expr = new StringConstant (rc.BuiltinTypes, last_expr_constant.Value + sc.Value, sc.Location);
5841                                                 return;
5842                                         }
5843                                 }
5844                         } else {
5845                                 //
5846                                 // Multiple (3+) concatenation are resolved as multiple StringConcat instances
5847                                 //
5848                                 StringConcat concat_oper = operand as StringConcat;
5849                                 if (concat_oper != null) {
5850                                         arguments.AddRange (concat_oper.arguments);
5851                                         return;
5852                                 }
5853                         }
5854
5855                         arguments.Add (new Argument (operand));
5856                 }
5857
5858                 IList<MemberSpec> GetConcatMethodCandidates ()
5859                 {
5860                         return MemberCache.FindMembers (type, "Concat", true);
5861                 }
5862
5863                 public override void Emit (EmitContext ec)
5864                 {
5865                         // Optimize by removing any extra null arguments, they are no-op
5866                         for (int i = 0; i < arguments.Count; ++i) {
5867                                 if (arguments[i].Expr is NullConstant)
5868                                         arguments.RemoveAt (i--);
5869                         }
5870
5871                         var members = GetConcatMethodCandidates ();
5872                         var res = new OverloadResolver (members, OverloadResolver.Restrictions.NoBaseMembers, loc);
5873                         var method = res.ResolveMember<MethodSpec> (new ResolveContext (ec.MemberContext), ref arguments);
5874                         if (method != null) {
5875                                 var call = new CallEmitter ();
5876                                 call.EmitPredefined (ec, method, arguments, false);
5877                         }
5878                 }
5879
5880                 public override void FlowAnalysis (FlowAnalysisContext fc)
5881                 {
5882                         arguments.FlowAnalysis (fc);
5883                 }
5884
5885                 public override SLE.Expression MakeExpression (BuilderContext ctx)
5886                 {
5887                         if (arguments.Count != 2)
5888                                 throw new NotImplementedException ("arguments.Count != 2");
5889
5890                         var concat = typeof (string).GetMethod ("Concat", new[] { typeof (object), typeof (object) });
5891                         return SLE.Expression.Add (arguments[0].Expr.MakeExpression (ctx), arguments[1].Expr.MakeExpression (ctx), concat);
5892                 }
5893         }
5894
5895         //
5896         // User-defined conditional logical operator
5897         //
5898         public class ConditionalLogicalOperator : UserOperatorCall
5899         {
5900                 readonly bool is_and;
5901                 Expression oper_expr;
5902
5903                 public ConditionalLogicalOperator (MethodSpec oper, Arguments arguments, Func<ResolveContext, Expression, Expression> expr_tree, bool is_and, Location loc)
5904                         : base (oper, arguments, expr_tree, loc)
5905                 {
5906                         this.is_and = is_and;
5907                         eclass = ExprClass.Unresolved;
5908                 }
5909                 
5910                 protected override Expression DoResolve (ResolveContext ec)
5911                 {
5912                         AParametersCollection pd = oper.Parameters;
5913                         if (!TypeSpecComparer.IsEqual (type, pd.Types[0]) || !TypeSpecComparer.IsEqual (type, pd.Types[1])) {
5914                                 ec.Report.Error (217, loc,
5915                                         "A user-defined operator `{0}' must have each parameter type and return type of the same type in order to be applicable as a short circuit operator",
5916                                         oper.GetSignatureForError ());
5917                                 return null;
5918                         }
5919
5920                         Expression left_dup = new EmptyExpression (type);
5921                         Expression op_true = GetOperatorTrue (ec, left_dup, loc);
5922                         Expression op_false = GetOperatorFalse (ec, left_dup, loc);
5923                         if (op_true == null || op_false == null) {
5924                                 ec.Report.Error (218, loc,
5925                                         "The type `{0}' must have operator `true' and operator `false' defined when `{1}' is used as a short circuit operator",
5926                                         type.GetSignatureForError (), oper.GetSignatureForError ());
5927                                 return null;
5928                         }
5929
5930                         oper_expr = is_and ? op_false : op_true;
5931                         eclass = ExprClass.Value;
5932                         return this;
5933                 }
5934
5935                 public override void Emit (EmitContext ec)
5936                 {
5937                         Label end_target = ec.DefineLabel ();
5938
5939                         //
5940                         // Emit and duplicate left argument
5941                         //
5942                         bool right_contains_await = ec.HasSet (BuilderContext.Options.AsyncBody) && arguments[1].Expr.ContainsEmitWithAwait ();
5943                         if (right_contains_await) {
5944                                 arguments[0] = arguments[0].EmitToField (ec, false);
5945                                 arguments[0].Expr.Emit (ec);
5946                         } else {
5947                                 arguments[0].Expr.Emit (ec);
5948                                 ec.Emit (OpCodes.Dup);
5949                                 arguments.RemoveAt (0);
5950                         }
5951
5952                         oper_expr.EmitBranchable (ec, end_target, true);
5953
5954                         base.Emit (ec);
5955
5956                         if (right_contains_await) {
5957                                 //
5958                                 // Special handling when right expression contains await and left argument
5959                                 // could not be left on stack before logical branch
5960                                 //
5961                                 Label skip_left_load = ec.DefineLabel ();
5962                                 ec.Emit (OpCodes.Br_S, skip_left_load);
5963                                 ec.MarkLabel (end_target);
5964                                 arguments[0].Expr.Emit (ec);
5965                                 ec.MarkLabel (skip_left_load);
5966                         } else {
5967                                 ec.MarkLabel (end_target);
5968                         }
5969                 }
5970         }
5971
5972         public class PointerArithmetic : Expression {
5973                 Expression left, right;
5974                 readonly Binary.Operator op;
5975
5976                 //
5977                 // We assume that `l' is always a pointer
5978                 //
5979                 public PointerArithmetic (Binary.Operator op, Expression l, Expression r, TypeSpec t, Location loc)
5980                 {
5981                         type = t;
5982                         this.loc = loc;
5983                         left = l;
5984                         right = r;
5985                         this.op = op;
5986                 }
5987
5988                 public override bool ContainsEmitWithAwait ()
5989                 {
5990                         throw new NotImplementedException ();
5991                 }
5992
5993                 public override Expression CreateExpressionTree (ResolveContext ec)
5994                 {
5995                         Error_PointerInsideExpressionTree (ec);
5996                         return null;
5997                 }
5998
5999                 protected override Expression DoResolve (ResolveContext ec)
6000                 {
6001                         eclass = ExprClass.Variable;
6002
6003                         var pc = left.Type as PointerContainer;
6004                         if (pc != null && pc.Element.Kind == MemberKind.Void) {
6005                                 Error_VoidPointerOperation (ec);
6006                                 return null;
6007                         }
6008                         
6009                         return this;
6010                 }
6011
6012                 public override void Emit (EmitContext ec)
6013                 {
6014                         TypeSpec op_type = left.Type;
6015                         
6016                         // It must be either array or fixed buffer
6017                         TypeSpec element;
6018                         if (TypeManager.HasElementType (op_type)) {
6019                                 element = TypeManager.GetElementType (op_type);
6020                         } else {
6021                                 FieldExpr fe = left as FieldExpr;
6022                                 if (fe != null)
6023                                         element = ((FixedFieldSpec) (fe.Spec)).ElementType;
6024                                 else
6025                                         element = op_type;
6026                         }
6027
6028                         int size = BuiltinTypeSpec.GetSize(element);
6029                         TypeSpec rtype = right.Type;
6030                         
6031                         if ((op & Binary.Operator.SubtractionMask) != 0 && rtype.IsPointer){
6032                                 //
6033                                 // handle (pointer - pointer)
6034                                 //
6035                                 left.Emit (ec);
6036                                 right.Emit (ec);
6037                                 ec.Emit (OpCodes.Sub);
6038
6039                                 if (size != 1){
6040                                         if (size == 0)
6041                                                 ec.Emit (OpCodes.Sizeof, element);
6042                                         else 
6043                                                 ec.EmitInt (size);
6044                                         ec.Emit (OpCodes.Div);
6045                                 }
6046                                 ec.Emit (OpCodes.Conv_I8);
6047                         } else {
6048                                 //
6049                                 // handle + and - on (pointer op int)
6050                                 //
6051                                 Constant left_const = left as Constant;
6052                                 if (left_const != null) {
6053                                         //
6054                                         // Optimize ((T*)null) pointer operations
6055                                         //
6056                                         if (left_const.IsDefaultValue) {
6057                                                 left = EmptyExpression.Null;
6058                                         } else {
6059                                                 left_const = null;
6060                                         }
6061                                 }
6062
6063                                 left.Emit (ec);
6064
6065                                 var right_const = right as Constant;
6066                                 if (right_const != null) {
6067                                         //
6068                                         // Optimize 0-based arithmetic
6069                                         //
6070                                         if (right_const.IsDefaultValue)
6071                                                 return;
6072
6073                                         if (size != 0)
6074                                                 right = new IntConstant (ec.BuiltinTypes, size, right.Location);
6075                                         else
6076                                                 right = new SizeOf (new TypeExpression (element, right.Location), right.Location);
6077                                         
6078                                         // TODO: Should be the checks resolve context sensitive?
6079                                         ResolveContext rc = new ResolveContext (ec.MemberContext, ResolveContext.Options.UnsafeScope);
6080                                         right = new Binary (Binary.Operator.Multiply, right, right_const).Resolve (rc);
6081                                         if (right == null)
6082                                                 return;
6083                                 }
6084
6085                                 right.Emit (ec);
6086                                 if (right_const == null) {
6087                                         switch (rtype.BuiltinType) {
6088                                         case BuiltinTypeSpec.Type.SByte:
6089                                         case BuiltinTypeSpec.Type.Byte:
6090                                         case BuiltinTypeSpec.Type.Short:
6091                                         case BuiltinTypeSpec.Type.UShort:
6092                                         case BuiltinTypeSpec.Type.Int:
6093                                                 ec.Emit (OpCodes.Conv_I);
6094                                                 break;
6095                                         case BuiltinTypeSpec.Type.UInt:
6096                                                 ec.Emit (OpCodes.Conv_U);
6097                                                 break;
6098                                         }
6099                                 }
6100
6101                                 if (right_const == null && size != 1){
6102                                         if (size == 0)
6103                                                 ec.Emit (OpCodes.Sizeof, element);
6104                                         else 
6105                                                 ec.EmitInt (size);
6106                                         if (rtype.BuiltinType == BuiltinTypeSpec.Type.Long || rtype.BuiltinType == BuiltinTypeSpec.Type.ULong)
6107                                                 ec.Emit (OpCodes.Conv_I8);
6108
6109                                         Binary.EmitOperatorOpcode (ec, Binary.Operator.Multiply, rtype, right);
6110                                 }
6111
6112                                 if (left_const == null) {
6113                                         if (rtype.BuiltinType == BuiltinTypeSpec.Type.Long)
6114                                                 ec.Emit (OpCodes.Conv_I);
6115                                         else if (rtype.BuiltinType == BuiltinTypeSpec.Type.ULong)
6116                                                 ec.Emit (OpCodes.Conv_U);
6117
6118                                         Binary.EmitOperatorOpcode (ec, op, op_type, right);
6119                                 }
6120                         }
6121                 }
6122         }
6123
6124         //
6125         // A boolean-expression is an expression that yields a result
6126         // of type bool
6127         //
6128         public class BooleanExpression : ShimExpression
6129         {
6130                 public BooleanExpression (Expression expr)
6131                         : base (expr)
6132                 {
6133                         this.loc = expr.Location;
6134                 }
6135
6136                 public override Expression CreateExpressionTree (ResolveContext ec)
6137                 {
6138                         // TODO: We should emit IsTrue (v4) instead of direct user operator
6139                         // call but that would break csc compatibility
6140                         return base.CreateExpressionTree (ec);
6141                 }
6142
6143                 protected override Expression DoResolve (ResolveContext ec)
6144                 {
6145                         // A boolean-expression is required to be of a type
6146                         // that can be implicitly converted to bool or of
6147                         // a type that implements operator true
6148
6149                         expr = expr.Resolve (ec);
6150                         if (expr == null)
6151                                 return null;
6152
6153                         Assign ass = expr as Assign;
6154                         if (ass != null && ass.Source is Constant) {
6155                                 ec.Report.Warning (665, 3, loc,
6156                                         "Assignment in conditional expression is always constant. Did you mean to use `==' instead ?");
6157                         }
6158
6159                         if (expr.Type.BuiltinType == BuiltinTypeSpec.Type.Bool)
6160                                 return expr;
6161
6162                         if (expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
6163                                 Arguments args = new Arguments (1);
6164                                 args.Add (new Argument (expr));
6165                                 return DynamicUnaryConversion.CreateIsTrue (ec, args, loc).Resolve (ec);
6166                         }
6167
6168                         type = ec.BuiltinTypes.Bool;
6169                         Expression converted = Convert.ImplicitConversion (ec, expr, type, loc);
6170                         if (converted != null)
6171                                 return converted;
6172
6173                         //
6174                         // If no implicit conversion to bool exists, try using `operator true'
6175                         //
6176                         converted = GetOperatorTrue (ec, expr, loc);
6177                         if (converted == null) {
6178                                 expr.Error_ValueCannotBeConverted (ec, type, false);
6179                                 return null;
6180                         }
6181
6182                         return converted;
6183                 }
6184                 
6185                 public override object Accept (StructuralVisitor visitor)
6186                 {
6187                         return visitor.Visit (this);
6188                 }
6189         }
6190
6191         public class BooleanExpressionFalse : Unary
6192         {
6193                 public BooleanExpressionFalse (Expression expr)
6194                         : base (Operator.LogicalNot, expr, expr.Location)
6195                 {
6196                 }
6197
6198                 protected override Expression ResolveOperator (ResolveContext ec, Expression expr)
6199                 {
6200                         return GetOperatorFalse (ec, expr, loc) ?? base.ResolveOperator (ec, expr);
6201                 }
6202         }
6203         
6204         /// <summary>
6205         ///   Implements the ternary conditional operator (?:)
6206         /// </summary>
6207         public class Conditional : Expression {
6208                 Expression expr, true_expr, false_expr;
6209
6210                 public Conditional (Expression expr, Expression true_expr, Expression false_expr, Location loc)
6211                 {
6212                         this.expr = expr;
6213                         this.true_expr = true_expr;
6214                         this.false_expr = false_expr;
6215                         this.loc = loc;
6216                 }
6217
6218                 #region Properties
6219
6220                 public Expression Expr {
6221                         get {
6222                                 return expr;
6223                         }
6224                 }
6225
6226                 public Expression TrueExpr {
6227                         get {
6228                                 return true_expr;
6229                         }
6230                 }
6231
6232                 public Expression FalseExpr {
6233                         get {
6234                                 return false_expr;
6235                         }
6236                 }
6237
6238                 #endregion
6239
6240                 public override bool ContainsEmitWithAwait ()
6241                 {
6242                         return Expr.ContainsEmitWithAwait () || true_expr.ContainsEmitWithAwait () || false_expr.ContainsEmitWithAwait ();
6243                 }
6244
6245                 public override Expression CreateExpressionTree (ResolveContext ec)
6246                 {
6247                         Arguments args = new Arguments (3);
6248                         args.Add (new Argument (expr.CreateExpressionTree (ec)));
6249                         args.Add (new Argument (true_expr.CreateExpressionTree (ec)));
6250                         args.Add (new Argument (false_expr.CreateExpressionTree (ec)));
6251                         return CreateExpressionFactoryCall (ec, "Condition", args);
6252                 }
6253
6254                 protected override Expression DoResolve (ResolveContext ec)
6255                 {
6256                         expr = expr.Resolve (ec);
6257                         true_expr = true_expr.Resolve (ec);
6258                         false_expr = false_expr.Resolve (ec);
6259
6260                         if (true_expr == null || false_expr == null || expr == null)
6261                                 return null;
6262
6263                         eclass = ExprClass.Value;
6264                         TypeSpec true_type = true_expr.Type;
6265                         TypeSpec false_type = false_expr.Type;
6266                         type = true_type;
6267
6268                         //
6269                         // First, if an implicit conversion exists from true_expr
6270                         // to false_expr, then the result type is of type false_expr.Type
6271                         //
6272                         if (!TypeSpecComparer.IsEqual (true_type, false_type)) {
6273                                 Expression conv = Convert.ImplicitConversion (ec, true_expr, false_type, loc);
6274                                 if (conv != null && true_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic) {
6275                                         //
6276                                         // Check if both can convert implicitly to each other's type
6277                                         //
6278                                         type = false_type;
6279
6280                                         if (false_type.BuiltinType != BuiltinTypeSpec.Type.Dynamic) {
6281                                                 var conv_false_expr = Convert.ImplicitConversion (ec, false_expr, true_type, loc);
6282                                                 //
6283                                                 // LAMESPEC: There seems to be hardcoded promotition to int type when
6284                                                 // both sides are numeric constants and one side is int constant and
6285                                                 // other side is numeric constant convertible to int.
6286                                                 //
6287                                                 // var res = condition ? (short)1 : 1;
6288                                                 //
6289                                                 // Type of res is int even if according to the spec the conversion is
6290                                                 // ambiguous because 1 literal can be converted to short.
6291                                                 //
6292                                                 if (conv_false_expr != null) {
6293                                                         if (conv_false_expr.Type.BuiltinType == BuiltinTypeSpec.Type.Int && conv is Constant) {
6294                                                                 type = true_type;
6295                                                                 conv_false_expr = null;
6296                                                         } else if (type.BuiltinType == BuiltinTypeSpec.Type.Int && conv_false_expr is Constant) {
6297                                                                 conv_false_expr = null;
6298                                                         }
6299                                                 }
6300
6301                                                 if (conv_false_expr != null) {
6302                                                         ec.Report.Error (172, true_expr.Location,
6303                                                                 "Type of conditional expression cannot be determined as `{0}' and `{1}' convert implicitly to each other",
6304                                                                         true_type.GetSignatureForError (), false_type.GetSignatureForError ());
6305                                                 }
6306                                         }
6307
6308                                         true_expr = conv;
6309                                         if (true_expr.Type != type)
6310                                                 true_expr = EmptyCast.Create (true_expr, type);
6311                                 } else if ((conv = Convert.ImplicitConversion (ec, false_expr, true_type, loc)) != null) {
6312                                         false_expr = conv;
6313                                 } else {
6314                                         if (false_type != InternalType.ErrorType) {
6315                                                 ec.Report.Error (173, true_expr.Location,
6316                                                         "Type of conditional expression cannot be determined because there is no implicit conversion between `{0}' and `{1}'",
6317                                                         true_type.GetSignatureForError (), false_type.GetSignatureForError ());
6318                                         }
6319                                         return null;
6320                                 }
6321                         }
6322
6323                         Constant c = expr as Constant;
6324                         if (c != null) {
6325                                 bool is_false = c.IsDefaultValue;
6326
6327                                 //
6328                                 // Don't issue the warning for constant expressions
6329                                 //
6330                                 if (!(is_false ? true_expr is Constant : false_expr is Constant)) {
6331                                         // CSC: Missing warning
6332                                         Warning_UnreachableExpression (ec, is_false ? true_expr.Location : false_expr.Location);
6333                                 }
6334
6335                                 return ReducedExpression.Create (
6336                                         is_false ? false_expr : true_expr, this,
6337                                         false_expr is Constant && true_expr is Constant).Resolve (ec);
6338                         }
6339
6340                         return this;
6341                 }
6342
6343                 public override void Emit (EmitContext ec)
6344                 {
6345                         Label false_target = ec.DefineLabel ();
6346                         Label end_target = ec.DefineLabel ();
6347
6348                         expr.EmitBranchable (ec, false_target, false);
6349                         true_expr.Emit (ec);
6350
6351                         //
6352                         // Verifier doesn't support interface merging. When there are two types on
6353                         // the stack without common type hint and the common type is an interface.
6354                         // Use temporary local to give verifier hint on what type to unify the stack
6355                         //
6356                         if (type.IsInterface && true_expr is EmptyCast && false_expr is EmptyCast) {
6357                                 var temp = ec.GetTemporaryLocal (type);
6358                                 ec.Emit (OpCodes.Stloc, temp);
6359                                 ec.Emit (OpCodes.Ldloc, temp);
6360                                 ec.FreeTemporaryLocal (temp, type);
6361                         }
6362
6363                         ec.Emit (OpCodes.Br, end_target);
6364                         ec.MarkLabel (false_target);
6365                         false_expr.Emit (ec);
6366                         ec.MarkLabel (end_target);
6367                 }
6368
6369                 public override void FlowAnalysis (FlowAnalysisContext fc)
6370                 {
6371                         expr.FlowAnalysisConditional (fc);
6372                         var expr_true = fc.DefiniteAssignmentOnTrue;
6373                         var expr_false = fc.DefiniteAssignmentOnFalse;
6374
6375                         fc.BranchDefiniteAssignment (expr_true);
6376                         true_expr.FlowAnalysis (fc);
6377                         var true_fc = fc.DefiniteAssignment;
6378
6379                         fc.BranchDefiniteAssignment (expr_false);
6380                         false_expr.FlowAnalysis (fc);
6381
6382                         fc.DefiniteAssignment &= true_fc;
6383                 }
6384
6385                 public override void FlowAnalysisConditional (FlowAnalysisContext fc)
6386                 {
6387                         expr.FlowAnalysisConditional (fc);
6388                         var expr_true = fc.DefiniteAssignmentOnTrue;
6389                         var expr_false = fc.DefiniteAssignmentOnFalse;
6390
6391                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment = new DefiniteAssignmentBitSet (expr_true);
6392                         true_expr.FlowAnalysisConditional (fc);
6393                         var true_fc = fc.DefiniteAssignment;
6394                         var true_da_true = fc.DefiniteAssignmentOnTrue;
6395                         var true_da_false = fc.DefiniteAssignmentOnFalse;
6396
6397                         fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment = new DefiniteAssignmentBitSet (expr_false);
6398                         false_expr.FlowAnalysisConditional (fc);
6399
6400                         fc.DefiniteAssignment &= true_fc;
6401                         fc.DefiniteAssignmentOnTrue = true_da_true & fc.DefiniteAssignmentOnTrue;
6402                         fc.DefiniteAssignmentOnFalse = true_da_false & fc.DefiniteAssignmentOnFalse;
6403                 }
6404
6405                 protected override void CloneTo (CloneContext clonectx, Expression t)
6406                 {
6407                         Conditional target = (Conditional) t;
6408
6409                         target.expr = expr.Clone (clonectx);
6410                         target.true_expr = true_expr.Clone (clonectx);
6411                         target.false_expr = false_expr.Clone (clonectx);
6412                 }
6413         }
6414
6415         public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation, IVariableReference
6416         {
6417                 LocalTemporary temp;
6418
6419                 #region Abstract
6420                 public abstract HoistedVariable GetHoistedVariable (AnonymousExpression ae);
6421                 public abstract void SetHasAddressTaken ();
6422
6423                 public abstract bool IsLockedByStatement { get; set; }
6424
6425                 public abstract bool IsFixed { get; }
6426                 public abstract bool IsRef { get; }
6427                 public abstract string Name { get; }
6428
6429                 //
6430                 // Variable IL data, it has to be protected to encapsulate hoisted variables
6431                 //
6432                 protected abstract ILocalVariable Variable { get; }
6433                 
6434                 //
6435                 // Variable flow-analysis data
6436                 //
6437                 public abstract VariableInfo VariableInfo { get; }
6438                 #endregion
6439
6440                 public virtual void AddressOf (EmitContext ec, AddressOp mode)
6441                 {
6442                         HoistedVariable hv = GetHoistedVariable (ec);
6443                         if (hv != null) {
6444                                 hv.AddressOf (ec, mode);
6445                                 return;
6446                         }
6447
6448                         Variable.EmitAddressOf (ec);
6449                 }
6450
6451                 public override bool ContainsEmitWithAwait ()
6452                 {
6453                         return false;
6454                 }
6455
6456                 public override Expression CreateExpressionTree (ResolveContext ec)
6457                 {
6458                         HoistedVariable hv = GetHoistedVariable (ec);
6459                         if (hv != null)
6460                                 return hv.CreateExpressionTree ();
6461
6462                         Arguments arg = new Arguments (1);
6463                         arg.Add (new Argument (this));
6464                         return CreateExpressionFactoryCall (ec, "Constant", arg);
6465                 }
6466
6467                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
6468                 {
6469                         if (IsLockedByStatement) {
6470                                 rc.Report.Warning (728, 2, loc,
6471                                         "Possibly incorrect assignment to `{0}' which is the argument to a using or lock statement",
6472                                         Name);
6473                         }
6474
6475                         return this;
6476                 }
6477
6478                 public override void Emit (EmitContext ec)
6479                 {
6480                         Emit (ec, false);
6481                 }
6482
6483                 public override void EmitSideEffect (EmitContext ec)
6484                 {
6485                         // do nothing
6486                 }
6487
6488                 //
6489                 // This method is used by parameters that are references, that are
6490                 // being passed as references:  we only want to pass the pointer (that
6491                 // is already stored in the parameter, not the address of the pointer,
6492                 // and not the value of the variable).
6493                 //
6494                 public void EmitLoad (EmitContext ec)
6495                 {
6496                         Variable.Emit (ec);
6497                 }
6498
6499                 public void Emit (EmitContext ec, bool leave_copy)
6500                 {
6501                         HoistedVariable hv = GetHoistedVariable (ec);
6502                         if (hv != null) {
6503                                 hv.Emit (ec, leave_copy);
6504                                 return;
6505                         }
6506
6507                         EmitLoad (ec);
6508
6509                         if (IsRef) {
6510                                 //
6511                                 // If we are a reference, we loaded on the stack a pointer
6512                                 // Now lets load the real value
6513                                 //
6514                                 ec.EmitLoadFromPtr (type);
6515                         }
6516
6517                         if (leave_copy) {
6518                                 ec.Emit (OpCodes.Dup);
6519
6520                                 if (IsRef) {
6521                                         temp = new LocalTemporary (Type);
6522                                         temp.Store (ec);
6523                                 }
6524                         }
6525                 }
6526
6527                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
6528                                         bool prepare_for_load)
6529                 {
6530                         HoistedVariable hv = GetHoistedVariable (ec);
6531                         if (hv != null) {
6532                                 hv.EmitAssign (ec, source, leave_copy, prepare_for_load);
6533                                 return;
6534                         }
6535
6536                         New n_source = source as New;
6537                         if (n_source != null && n_source.CanEmitOptimizedLocalTarget (ec)) {
6538                                 if (!n_source.Emit (ec, this)) {
6539                                         if (leave_copy) {
6540                                                 EmitLoad (ec);
6541                                                 if (IsRef)
6542                                                         ec.EmitLoadFromPtr (type);
6543                                         }
6544                                         return;
6545                                 }
6546                         } else {
6547                                 if (IsRef)
6548                                         EmitLoad (ec);
6549
6550                                 source.Emit (ec);
6551                         }
6552
6553                         if (leave_copy) {
6554                                 ec.Emit (OpCodes.Dup);
6555                                 if (IsRef) {
6556                                         temp = new LocalTemporary (Type);
6557                                         temp.Store (ec);
6558                                 }
6559                         }
6560
6561                         if (IsRef)
6562                                 ec.EmitStoreFromPtr (type);
6563                         else
6564                                 Variable.EmitAssign (ec);
6565
6566                         if (temp != null) {
6567                                 temp.Emit (ec);
6568                                 temp.Release (ec);
6569                         }
6570                 }
6571
6572                 public override Expression EmitToField (EmitContext ec)
6573                 {
6574                         HoistedVariable hv = GetHoistedVariable (ec);
6575                         if (hv != null) {
6576                                 return hv.EmitToField (ec);
6577                         }
6578
6579                         return base.EmitToField (ec);
6580                 }
6581
6582                 public HoistedVariable GetHoistedVariable (ResolveContext rc)
6583                 {
6584                         return GetHoistedVariable (rc.CurrentAnonymousMethod);
6585                 }
6586
6587                 public HoistedVariable GetHoistedVariable (EmitContext ec)
6588                 {
6589                         return GetHoistedVariable (ec.CurrentAnonymousMethod);
6590                 }
6591
6592                 public override string GetSignatureForError ()
6593                 {
6594                         return Name;
6595                 }
6596
6597                 public bool IsHoisted {
6598                         get { return GetHoistedVariable ((AnonymousExpression) null) != null; }
6599                 }
6600         }
6601
6602         //
6603         // Resolved reference to a local variable
6604         //
6605         public class LocalVariableReference : VariableReference
6606         {
6607                 public LocalVariable local_info;
6608
6609                 public LocalVariableReference (LocalVariable li, Location l)
6610                 {
6611                         this.local_info = li;
6612                         loc = l;
6613                 }
6614
6615                 public override VariableInfo VariableInfo {
6616                         get { return local_info.VariableInfo; }
6617                 }
6618
6619                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6620                 {
6621                         return local_info.HoistedVariant;
6622                 }
6623
6624                 #region Properties
6625
6626                 //              
6627                 // A local variable is always fixed
6628                 //
6629                 public override bool IsFixed {
6630                         get {
6631                                 return true;
6632                         }
6633                 }
6634
6635                 public override bool IsLockedByStatement {
6636                         get {
6637                                 return local_info.IsLocked;
6638                         }
6639                         set {
6640                                 local_info.IsLocked = value;
6641                         }
6642                 }
6643
6644                 public override bool IsRef {
6645                         get { return false; }
6646                 }
6647
6648                 public override string Name {
6649                         get { return local_info.Name; }
6650                 }
6651
6652                 #endregion
6653
6654                 public override void FlowAnalysis (FlowAnalysisContext fc)
6655                 {
6656                         VariableInfo variable_info = VariableInfo;
6657                         if (variable_info == null)
6658                                 return;
6659
6660                         if (fc.IsDefinitelyAssigned (variable_info))
6661                                 return;
6662
6663                         fc.Report.Error (165, loc, "Use of unassigned local variable `{0}'", Name);
6664                         variable_info.SetAssigned (fc.DefiniteAssignment, true);
6665                 }
6666
6667                 public override void SetHasAddressTaken ()
6668                 {
6669                         local_info.SetHasAddressTaken ();
6670                 }
6671
6672                 void DoResolveBase (ResolveContext ec)
6673                 {
6674                         eclass = ExprClass.Variable;
6675                         type = local_info.Type;
6676
6677                         //
6678                         // If we are referencing a variable from the external block
6679                         // flag it for capturing
6680                         //
6681                         if (ec.MustCaptureVariable (local_info)) {
6682                                 if (local_info.AddressTaken) {
6683                                         AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
6684                                 } else if (local_info.IsFixed) {
6685                                         ec.Report.Error (1764, loc,
6686                                                 "Cannot use fixed local `{0}' inside an anonymous method, lambda expression or query expression",
6687                                                 GetSignatureForError ());
6688                                 }
6689
6690                                 if (ec.IsVariableCapturingRequired) {
6691                                         AnonymousMethodStorey storey = local_info.Block.Explicit.CreateAnonymousMethodStorey (ec);
6692                                         storey.CaptureLocalVariable (ec, local_info);
6693                                 }
6694                         }
6695                 }
6696
6697                 protected override Expression DoResolve (ResolveContext ec)
6698                 {
6699                         local_info.SetIsUsed ();
6700
6701                         DoResolveBase (ec);
6702
6703                         if (local_info.Type == InternalType.VarOutType) {
6704                                 ec.Report.Error (8048, loc, "Cannot use uninitialized variable `{0}'",
6705                                         GetSignatureForError ());
6706
6707                                 type = InternalType.ErrorType;
6708                         }
6709
6710                         return this;
6711                 }
6712
6713                 public override Expression DoResolveLValue (ResolveContext ec, Expression rhs)
6714                 {
6715                         //
6716                         // Don't be too pedantic when variable is used as out param or for some broken code
6717                         // which uses property/indexer access to run some initialization
6718                         //
6719                         if (rhs == EmptyExpression.OutAccess || rhs.eclass == ExprClass.PropertyAccess || rhs.eclass == ExprClass.IndexerAccess)
6720                                 local_info.SetIsUsed ();
6721
6722                         if (local_info.IsReadonly && !ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.UsingInitializerScope)) {
6723                                 if (rhs == EmptyExpression.LValueMemberAccess) {
6724                                         // CS1654 already reported
6725                                 } else {
6726                                         int code;
6727                                         string msg;
6728                                         if (rhs == EmptyExpression.OutAccess) {
6729                                                 code = 1657; msg = "Cannot pass `{0}' as a ref or out argument because it is a `{1}'";
6730                                         } else if (rhs == EmptyExpression.LValueMemberOutAccess) {
6731                                                 code = 1655; msg = "Cannot pass members of `{0}' as ref or out arguments because it is a `{1}'";
6732                                         } else if (rhs == EmptyExpression.UnaryAddress) {
6733                                                 code = 459; msg = "Cannot take the address of {1} `{0}'";
6734                                         } else {
6735                                                 code = 1656; msg = "Cannot assign to `{0}' because it is a `{1}'";
6736                                         }
6737                                         ec.Report.Error (code, loc, msg, Name, local_info.GetReadOnlyContext ());
6738                                 }
6739                         }
6740
6741                         if (eclass == ExprClass.Unresolved)
6742                                 DoResolveBase (ec);
6743
6744                         return base.DoResolveLValue (ec, rhs);
6745                 }
6746
6747                 public override int GetHashCode ()
6748                 {
6749                         return local_info.GetHashCode ();
6750                 }
6751
6752                 public override bool Equals (object obj)
6753                 {
6754                         LocalVariableReference lvr = obj as LocalVariableReference;
6755                         if (lvr == null)
6756                                 return false;
6757
6758                         return local_info == lvr.local_info;
6759                 }
6760
6761                 protected override ILocalVariable Variable {
6762                         get { return local_info; }
6763                 }
6764
6765                 public override string ToString ()
6766                 {
6767                         return String.Format ("{0} ({1}:{2})", GetType (), Name, loc);
6768                 }
6769
6770                 protected override void CloneTo (CloneContext clonectx, Expression t)
6771                 {
6772                         // Nothing
6773                 }
6774         }
6775
6776         /// <summary>
6777         ///   This represents a reference to a parameter in the intermediate
6778         ///   representation.
6779         /// </summary>
6780         public class ParameterReference : VariableReference
6781         {
6782                 protected ParametersBlock.ParameterInfo pi;
6783
6784                 public ParameterReference (ParametersBlock.ParameterInfo pi, Location loc)
6785                 {
6786                         this.pi = pi;
6787                         this.loc = loc;
6788                 }
6789
6790                 #region Properties
6791
6792                 public override bool IsLockedByStatement {
6793                         get {
6794                                 return pi.IsLocked;
6795                         }
6796                         set     {
6797                                 pi.IsLocked = value;
6798                         }
6799                 }
6800
6801                 public override bool IsRef {
6802                         get { return (pi.Parameter.ModFlags & Parameter.Modifier.RefOutMask) != 0; }
6803                 }
6804
6805                 bool HasOutModifier {
6806                         get { return (pi.Parameter.ModFlags & Parameter.Modifier.OUT) != 0; }
6807                 }
6808
6809                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
6810                 {
6811                         return pi.Parameter.HoistedVariant;
6812                 }
6813
6814                 //
6815                 // A ref or out parameter is classified as a moveable variable, even 
6816                 // if the argument given for the parameter is a fixed variable
6817                 //              
6818                 public override bool IsFixed {
6819                         get { return !IsRef; }
6820                 }
6821
6822                 public override string Name {
6823                         get { return Parameter.Name; }
6824                 }
6825
6826                 public Parameter Parameter {
6827                         get { return pi.Parameter; }
6828                 }
6829
6830                 public override VariableInfo VariableInfo {
6831                         get { return pi.VariableInfo; }
6832                 }
6833
6834                 protected override ILocalVariable Variable {
6835                         get { return Parameter; }
6836                 }
6837
6838                 #endregion
6839
6840                 public override void AddressOf (EmitContext ec, AddressOp mode)
6841                 {
6842                         //
6843                         // ParameterReferences might already be a reference
6844                         //
6845                         if (IsRef) {
6846                                 EmitLoad (ec);
6847                                 return;
6848                         }
6849
6850                         base.AddressOf (ec, mode);
6851                 }
6852
6853                 public override void SetHasAddressTaken ()
6854                 {
6855                         Parameter.HasAddressTaken = true;
6856                 }
6857
6858                 bool DoResolveBase (ResolveContext ec)
6859                 {
6860                         if (eclass != ExprClass.Unresolved)
6861                                 return true;
6862
6863                         type = pi.ParameterType;
6864                         eclass = ExprClass.Variable;
6865
6866                         //
6867                         // If we are referencing a parameter from the external block
6868                         // flag it for capturing
6869                         //
6870                         if (ec.MustCaptureVariable (pi)) {
6871                                 if (Parameter.HasAddressTaken)
6872                                         AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);
6873
6874                                 if (IsRef) {
6875                                         ec.Report.Error (1628, loc,
6876                                                 "Parameter `{0}' cannot be used inside `{1}' when using `ref' or `out' modifier",
6877                                                 Name, ec.CurrentAnonymousMethod.ContainerType);
6878                                 }
6879
6880                                 if (ec.IsVariableCapturingRequired && !pi.Block.ParametersBlock.IsExpressionTree) {
6881                                         AnonymousMethodStorey storey = pi.Block.Explicit.CreateAnonymousMethodStorey (ec);
6882                                         storey.CaptureParameter (ec, pi, this);
6883                                 }
6884                         }
6885
6886                         return true;
6887                 }
6888
6889                 public override int GetHashCode ()
6890                 {
6891                         return Name.GetHashCode ();
6892                 }
6893
6894                 public override bool Equals (object obj)
6895                 {
6896                         ParameterReference pr = obj as ParameterReference;
6897                         if (pr == null)
6898                                 return false;
6899
6900                         return Name == pr.Name;
6901                 }
6902         
6903                 protected override void CloneTo (CloneContext clonectx, Expression target)
6904                 {
6905                         // Nothing to clone
6906                         return;
6907                 }
6908
6909                 public override Expression CreateExpressionTree (ResolveContext ec)
6910                 {
6911                         HoistedVariable hv = GetHoistedVariable (ec);
6912                         if (hv != null)
6913                                 return hv.CreateExpressionTree ();
6914
6915                         return Parameter.ExpressionTreeVariableReference ();
6916                 }
6917
6918                 protected override Expression DoResolve (ResolveContext ec)
6919                 {
6920                         if (!DoResolveBase (ec))
6921                                 return null;
6922
6923                         return this;
6924                 }
6925
6926                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6927                 {
6928                         if (!DoResolveBase (ec))
6929                                 return null;
6930
6931                         if (Parameter.HoistedVariant != null)
6932                                 Parameter.HoistedVariant.IsAssigned = true;
6933
6934                         return base.DoResolveLValue (ec, right_side);
6935                 }
6936
6937                 public override void FlowAnalysis (FlowAnalysisContext fc)
6938                 {
6939                         VariableInfo variable_info = VariableInfo;
6940                         if (variable_info == null)
6941                                 return;
6942
6943                         if (fc.IsDefinitelyAssigned (variable_info))
6944                                 return;
6945
6946                         fc.Report.Error (269, loc, "Use of unassigned out parameter `{0}'", Name);
6947                         fc.SetVariableAssigned (variable_info);
6948                 }
6949         }
6950
6951         /// <summary>
6952         ///   Invocation of methods or delegates.
6953         /// </summary>
6954         public class Invocation : ExpressionStatement
6955         {
6956                 public class Predefined : Invocation
6957                 {
6958                         public Predefined (MethodGroupExpr expr, Arguments arguments)
6959                                 : base (expr, arguments)
6960                         {
6961                                 this.mg = expr;
6962                         }
6963
6964                         protected override MethodGroupExpr DoResolveOverload (ResolveContext rc)
6965                         {
6966                                 mg.BestCandidate.CheckObsoleteness (rc, loc);
6967
6968                                 return mg;
6969                         }
6970                 }
6971
6972                 protected Arguments arguments;
6973                 protected Expression expr;
6974                 protected MethodGroupExpr mg;
6975                 bool conditional_access_receiver;
6976                 
6977                 public Invocation (Expression expr, Arguments arguments)
6978                 {
6979                         this.expr = expr;               
6980                         this.arguments = arguments;
6981                         if (expr != null) {
6982                                 loc = expr.Location;
6983                         }
6984                 }
6985
6986                 #region Properties
6987                 public Arguments Arguments {
6988                         get {
6989                                 return arguments;
6990                         }
6991                 }
6992                 
6993                 public Expression Exp {
6994                         get {
6995                                 return expr;
6996                         }
6997                 }
6998
6999                 public MethodGroupExpr MethodGroup {
7000                         get {
7001                                 return mg;
7002                         }
7003                 }
7004
7005                 public override Location StartLocation {
7006                         get {
7007                                 return expr.StartLocation;
7008                         }
7009                 }
7010
7011                 #endregion
7012
7013                 public override MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
7014                 {
7015                         if (MethodGroup == null)
7016                                 return null;
7017
7018                         var candidate = MethodGroup.BestCandidate;
7019                         if (candidate == null || !(candidate.IsStatic || Exp is This))
7020                                 return null;
7021
7022                         var args_count = arguments == null ? 0 : arguments.Count;
7023                         if (args_count != body.Parameters.Count)
7024                                 return null;
7025
7026                         var lambda_parameters = body.Block.Parameters.FixedParameters;
7027                         for (int i = 0; i < args_count; ++i) {
7028                                 var pr = arguments[i].Expr as ParameterReference;
7029                                 if (pr == null)
7030                                         return null;
7031
7032                                 if (lambda_parameters[i] != pr.Parameter)
7033                                         return null;
7034
7035                                 if ((lambda_parameters[i].ModFlags & Parameter.Modifier.RefOutMask) != (pr.Parameter.ModFlags & Parameter.Modifier.RefOutMask))
7036                                         return null;
7037                         }
7038
7039                         var emg = MethodGroup as ExtensionMethodGroupExpr;
7040                         if (emg != null) {
7041                                 var mg = MethodGroupExpr.CreatePredefined (candidate, candidate.DeclaringType, MethodGroup.Location);
7042                                 if (candidate.IsGeneric) {
7043                                         var targs = new TypeExpression [candidate.Arity];
7044                                         for (int i = 0; i < targs.Length; ++i) {
7045                                                 targs[i] = new TypeExpression (candidate.TypeArguments[i], MethodGroup.Location);
7046                                         }
7047
7048                                         mg.SetTypeArguments (null, new TypeArguments (targs));
7049                                 }
7050
7051                                 return mg;
7052                         }
7053
7054                         return MethodGroup;
7055                 }
7056
7057                 protected override void CloneTo (CloneContext clonectx, Expression t)
7058                 {
7059                         Invocation target = (Invocation) t;
7060
7061                         if (arguments != null)
7062                                 target.arguments = arguments.Clone (clonectx);
7063
7064                         target.expr = expr.Clone (clonectx);
7065                 }
7066
7067                 public override bool ContainsEmitWithAwait ()
7068                 {
7069                         if (arguments != null && arguments.ContainsEmitWithAwait ())
7070                                 return true;
7071
7072                         return mg.ContainsEmitWithAwait ();
7073                 }
7074
7075                 public override Expression CreateExpressionTree (ResolveContext ec)
7076                 {
7077                         Expression instance = mg.IsInstance ?
7078                                 mg.InstanceExpression.CreateExpressionTree (ec) :
7079                                 new NullLiteral (loc);
7080
7081                         var args = Arguments.CreateForExpressionTree (ec, arguments,
7082                                 instance,
7083                                 mg.CreateExpressionTree (ec));
7084
7085                         return CreateExpressionFactoryCall (ec, "Call", args);
7086                 }
7087
7088                 void ResolveConditionalAccessReceiver (ResolveContext rc)
7089                 {
7090                         if (!rc.HasSet (ResolveContext.Options.DontSetConditionalAccessReceiver) && expr.HasConditionalAccess ()) {
7091                                 conditional_access_receiver = true;
7092                         }
7093                 }
7094
7095                 bool statement_resolve;
7096                 public override ExpressionStatement ResolveStatement (BlockContext bc)
7097                 {
7098                         statement_resolve = true;
7099                         var es = base.ResolveStatement (bc);
7100                         statement_resolve = false;
7101
7102                         return es;
7103                 }
7104
7105                 protected override Expression DoResolve (ResolveContext rc)
7106                 {
7107                         ResolveConditionalAccessReceiver (rc);
7108                         return DoResolveInvocation (rc);
7109                 }
7110
7111                 Expression DoResolveInvocation (ResolveContext ec)
7112                 {
7113                         Expression member_expr;
7114                         var atn = expr as ATypeNameExpression;
7115
7116                         var flags = default (ResolveContext.FlagsHandle);
7117                         if (conditional_access_receiver)
7118                                 flags = ec.Set (ResolveContext.Options.DontSetConditionalAccessReceiver);
7119
7120                         if (atn != null) {
7121                                 member_expr = atn.LookupNameExpression (ec, MemberLookupRestrictions.InvocableOnly | MemberLookupRestrictions.ReadAccess);
7122                                 if (member_expr != null) {
7123                                         var name_of = member_expr as NameOf;
7124                                         if (name_of != null) {
7125                                                 return name_of.ResolveOverload (ec, arguments);
7126                                         }
7127
7128                                         member_expr = member_expr.Resolve (ec);
7129                                 }
7130                         } else {
7131                                 member_expr = expr.Resolve (ec);
7132                         }
7133
7134                         if (conditional_access_receiver)
7135                                 flags.Dispose ();
7136
7137                         if (member_expr == null)
7138                                 return null;
7139
7140                         //
7141                         // Next, evaluate all the expressions in the argument list
7142                         //
7143                         bool dynamic_arg = false;
7144                         if (arguments != null) {
7145                                 using (ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false)) {
7146                                         arguments.Resolve (ec, out dynamic_arg);
7147                                 }
7148                         }
7149
7150                         TypeSpec expr_type = member_expr.Type;
7151                         if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
7152                                 return DoResolveDynamic (ec, member_expr);
7153
7154                         mg = member_expr as MethodGroupExpr;
7155                         Expression invoke = null;
7156
7157                         if (mg == null) {
7158                                 if (expr_type != null && expr_type.IsDelegate) {
7159                                         invoke = new DelegateInvocation (member_expr, arguments, conditional_access_receiver, loc);
7160                                         invoke = invoke.Resolve (ec);
7161                                         if (invoke == null || !dynamic_arg)
7162                                                 return invoke;
7163                                 } else {
7164                                         if (member_expr is RuntimeValueExpression) {
7165                                                 ec.Report.Error (Report.RuntimeErrorId, loc, "Cannot invoke a non-delegate type `{0}'",
7166                                                         member_expr.Type.GetSignatureForError ());
7167                                                 return null;
7168                                         }
7169
7170                                         MemberExpr me = member_expr as MemberExpr;
7171                                         if (me == null) {
7172                                                 member_expr.Error_UnexpectedKind (ec, ResolveFlags.MethodGroup, loc);
7173                                                 return null;
7174                                         }
7175
7176                                         ec.Report.Error (1955, loc, "The member `{0}' cannot be used as method or delegate",
7177                                                         member_expr.GetSignatureForError ());
7178                                         return null;
7179                                 }
7180                         }
7181
7182                         if (invoke == null) {
7183                                 mg = DoResolveOverload (ec);
7184                                 if (mg == null)
7185                                         return null;
7186                         }
7187
7188                         if (dynamic_arg)
7189                                 return DoResolveDynamic (ec, member_expr);
7190
7191                         var method = mg.BestCandidate;
7192                         type = mg.BestCandidateReturnType;
7193                         if (conditional_access_receiver && !statement_resolve)
7194                                 type = LiftMemberType (ec, type);
7195
7196                         if (arguments == null && method.DeclaringType.BuiltinType == BuiltinTypeSpec.Type.Object && method.Name == Destructor.MetadataName) {
7197                                 if (mg.IsBase)
7198                                         ec.Report.Error (250, loc, "Do not directly call your base class Finalize method. It is called automatically from your destructor");
7199                                 else
7200                                         ec.Report.Error (245, loc, "Destructors and object.Finalize cannot be called directly. Consider calling IDisposable.Dispose if available");
7201                                 return null;
7202                         }
7203
7204                         IsSpecialMethodInvocation (ec, method, loc);
7205                         
7206                         eclass = ExprClass.Value;
7207                         return this;
7208                 }
7209
7210                 protected virtual Expression DoResolveDynamic (ResolveContext ec, Expression memberExpr)
7211                 {
7212                         Arguments args;
7213                         DynamicMemberBinder dmb = memberExpr as DynamicMemberBinder;
7214                         if (dmb != null) {
7215                                 args = dmb.Arguments;
7216                                 if (arguments != null)
7217                                         args.AddRange (arguments);
7218                         } else if (mg == null) {
7219                                 if (arguments == null)
7220                                         args = new Arguments (1);
7221                                 else
7222                                         args = arguments;
7223
7224                                 args.Insert (0, new Argument (memberExpr));
7225                                 this.expr = null;
7226                         } else {
7227                                 if (mg.IsBase) {
7228                                         ec.Report.Error (1971, loc,
7229                                                 "The base call to method `{0}' cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access",
7230                                                 mg.Name);
7231                                         return null;
7232                                 }
7233
7234                                 if (arguments == null)
7235                                         args = new Arguments (1);
7236                                 else
7237                                         args = arguments;
7238
7239                                 MemberAccess ma = expr as MemberAccess;
7240                                 if (ma != null) {
7241                                         var inst = mg.InstanceExpression;
7242                                         var left_type = inst as TypeExpr;
7243                                         if (left_type != null) {
7244                                                 args.Insert (0, new Argument (new TypeOf (left_type.Type, loc).Resolve (ec), Argument.AType.DynamicTypeName));
7245                                         } else if (inst != null) {
7246                                                 //
7247                                                 // Any value type has to be pass as by-ref to get back the same
7248                                                 // instance on which the member was called
7249                                                 //
7250                                                 var mod = inst is IMemoryLocation && TypeSpec.IsValueType (inst.Type) ?
7251                                                         Argument.AType.Ref : Argument.AType.None;
7252                                                 args.Insert (0, new Argument (inst.Resolve (ec), mod));
7253                                         }
7254                                 } else {        // is SimpleName
7255                                         if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer)) {
7256                                                 args.Insert (0, new Argument (new TypeOf (ec.CurrentType, loc).Resolve (ec), Argument.AType.DynamicTypeName));
7257                                         } else {
7258                                                 args.Insert (0, new Argument (new This (loc).Resolve (ec)));
7259                                         }
7260                                 }
7261                         }
7262
7263                         return new DynamicInvocation (expr as ATypeNameExpression, args, conditional_access_receiver, loc).Resolve (ec);
7264                 }
7265
7266                 protected virtual MethodGroupExpr DoResolveOverload (ResolveContext ec)
7267                 {
7268                         return mg.OverloadResolve (ec, ref arguments, null, OverloadResolver.Restrictions.None);
7269                 }
7270
7271                 public override void FlowAnalysis (FlowAnalysisContext fc)
7272                 {
7273                         if (mg.IsConditionallyExcluded)
7274                                 return;
7275
7276                         var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
7277
7278                         mg.FlowAnalysis (fc);
7279
7280                         if (arguments != null)
7281                                 arguments.FlowAnalysis (fc);
7282
7283                         if (conditional_access_receiver)
7284                                 fc.DefiniteAssignment = da;
7285                 }
7286
7287                 public override string GetSignatureForError ()
7288                 {
7289                         return mg.GetSignatureForError ();
7290                 }
7291
7292                 public override bool HasConditionalAccess ()
7293                 {
7294                         return expr.HasConditionalAccess ();
7295                 }
7296
7297                 //
7298                 // If a member is a method or event, or if it is a constant, field or property of either a delegate type
7299                 // or the type dynamic, then the member is invocable
7300                 //
7301                 public static bool IsMemberInvocable (MemberSpec member)
7302                 {
7303                         switch (member.Kind) {
7304                         case MemberKind.Event:
7305                                 return true;
7306                         case MemberKind.Field:
7307                         case MemberKind.Property:
7308                                 var m = member as IInterfaceMemberSpec;
7309                                 return m.MemberType.IsDelegate || m.MemberType.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
7310                         default:
7311                                 return false;
7312                         }
7313                 }
7314
7315                 public static bool IsSpecialMethodInvocation (ResolveContext ec, MethodSpec method, Location loc)
7316                 {
7317                         if (!method.IsReservedMethod)
7318                                 return false;
7319
7320                         if (ec.HasSet (ResolveContext.Options.InvokeSpecialName) || ec.CurrentMemberDefinition.IsCompilerGenerated)
7321                                 return false;
7322
7323                         ec.Report.SymbolRelatedToPreviousError (method);
7324                         ec.Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
7325                                 method.GetSignatureForError ());
7326         
7327                         return true;
7328                 }
7329
7330                 public override void Emit (EmitContext ec)
7331                 {
7332                         if (mg.IsConditionallyExcluded)
7333                                 return;
7334
7335                         if (conditional_access_receiver)
7336                                 mg.EmitCall (ec, arguments, type, false);
7337                         else
7338                                 mg.EmitCall (ec, arguments, false);
7339                 }
7340                 
7341                 public override void EmitStatement (EmitContext ec)
7342                 {
7343                         if (mg.IsConditionallyExcluded)
7344                                 return;
7345
7346                         if (conditional_access_receiver)
7347                                 mg.EmitCall (ec, arguments, type, true);
7348                         else
7349                                 mg.EmitCall (ec, arguments, true);
7350                 }
7351
7352                 public override SLE.Expression MakeExpression (BuilderContext ctx)
7353                 {
7354                         return MakeExpression (ctx, mg.InstanceExpression, mg.BestCandidate, arguments);
7355                 }
7356
7357                 public static SLE.Expression MakeExpression (BuilderContext ctx, Expression instance, MethodSpec mi, Arguments args)
7358                 {
7359 #if STATIC
7360                         throw new NotSupportedException ();
7361 #else
7362                         var instance_expr = instance == null ? null : instance.MakeExpression (ctx);
7363                         return SLE.Expression.Call (instance_expr, (MethodInfo) mi.GetMetaInfo (), Arguments.MakeExpression (args, ctx));
7364 #endif
7365                 }
7366
7367                 public override object Accept (StructuralVisitor visitor)
7368                 {
7369                         return visitor.Visit (this);
7370                 }
7371         }
7372
7373         //
7374         // Implements simple new expression 
7375         //
7376         public class New : ExpressionStatement, IMemoryLocation
7377         {
7378                 protected Arguments arguments;
7379
7380                 //
7381                 // During bootstrap, it contains the RequestedType,
7382                 // but if `type' is not null, it *might* contain a NewDelegate
7383                 // (because of field multi-initialization)
7384                 //
7385                 protected Expression RequestedType;
7386
7387                 protected MethodSpec method;
7388
7389                 public New (Expression requested_type, Arguments arguments, Location l)
7390                 {
7391                         RequestedType = requested_type;
7392                         this.arguments = arguments;
7393                         loc = l;
7394                 }
7395
7396                 #region Properties
7397                 public Arguments Arguments {
7398                         get {
7399                                 return arguments;
7400                         }
7401                 }
7402
7403                 //
7404                 // Returns true for resolved `new S()' when S does not declare parameterless constructor
7405                 //
7406                 public bool IsGeneratedStructConstructor {
7407                         get {
7408                                 return arguments == null && method == null && type.IsStruct && GetType () == typeof (New);
7409                         }
7410                 }
7411
7412                 public Expression TypeExpression {
7413                         get {
7414                                 return RequestedType;
7415                         }
7416                 }
7417
7418                 #endregion
7419
7420                 /// <summary>
7421                 /// Converts complex core type syntax like 'new int ()' to simple constant
7422                 /// </summary>
7423                 public static Constant Constantify (TypeSpec t, Location loc)
7424                 {
7425                         switch (t.BuiltinType) {
7426                         case BuiltinTypeSpec.Type.Int:
7427                                 return new IntConstant (t, 0, loc);
7428                         case BuiltinTypeSpec.Type.UInt:
7429                                 return new UIntConstant (t, 0, loc);
7430                         case BuiltinTypeSpec.Type.Long:
7431                                 return new LongConstant (t, 0, loc);
7432                         case BuiltinTypeSpec.Type.ULong:
7433                                 return new ULongConstant (t, 0, loc);
7434                         case BuiltinTypeSpec.Type.Float:
7435                                 return new FloatConstant (t, 0, loc);
7436                         case BuiltinTypeSpec.Type.Double:
7437                                 return new DoubleConstant (t, 0, loc);
7438                         case BuiltinTypeSpec.Type.Short:
7439                                 return new ShortConstant (t, 0, loc);
7440                         case BuiltinTypeSpec.Type.UShort:
7441                                 return new UShortConstant (t, 0, loc);
7442                         case BuiltinTypeSpec.Type.SByte:
7443                                 return new SByteConstant (t, 0, loc);
7444                         case BuiltinTypeSpec.Type.Byte:
7445                                 return new ByteConstant (t, 0, loc);
7446                         case BuiltinTypeSpec.Type.Char:
7447                                 return new CharConstant (t, '\0', loc);
7448                         case BuiltinTypeSpec.Type.Bool:
7449                                 return new BoolConstant (t, false, loc);
7450                         case BuiltinTypeSpec.Type.Decimal:
7451                                 return new DecimalConstant (t, 0, loc);
7452                         }
7453
7454                         if (t.IsEnum)
7455                                 return new EnumConstant (Constantify (EnumSpec.GetUnderlyingType (t), loc), t);
7456
7457                         if (t.IsNullableType)
7458                                 return Nullable.LiftedNull.Create (t, loc);
7459
7460                         return null;
7461                 }
7462
7463                 public override bool ContainsEmitWithAwait ()
7464                 {
7465                         return arguments != null && arguments.ContainsEmitWithAwait ();
7466                 }
7467
7468                 //
7469                 // Checks whether the type is an interface that has the
7470                 // [ComImport, CoClass] attributes and must be treated
7471                 // specially
7472                 //
7473                 public Expression CheckComImport (ResolveContext ec)
7474                 {
7475                         if (!type.IsInterface)
7476                                 return null;
7477
7478                         //
7479                         // Turn the call into:
7480                         // (the-interface-stated) (new class-referenced-in-coclassattribute ())
7481                         //
7482                         var real_class = type.MemberDefinition.GetAttributeCoClass ();
7483                         if (real_class == null)
7484                                 return null;
7485
7486                         New proxy = new New (new TypeExpression (real_class, loc), arguments, loc);
7487                         Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
7488                         return cast.Resolve (ec);
7489                 }
7490
7491                 public override Expression CreateExpressionTree (ResolveContext ec)
7492                 {
7493                         Arguments args;
7494                         if (method == null) {
7495                                 args = new Arguments (1);
7496                                 args.Add (new Argument (new TypeOf (type, loc)));
7497                         } else {
7498                                 args = Arguments.CreateForExpressionTree (ec,
7499                                         arguments, new TypeOfMethod (method, loc));
7500                         }
7501
7502                         return CreateExpressionFactoryCall (ec, "New", args);
7503                 }
7504                 
7505                 protected override Expression DoResolve (ResolveContext ec)
7506                 {
7507                         type = RequestedType.ResolveAsType (ec);
7508                         if (type == null)
7509                                 return null;
7510
7511                         eclass = ExprClass.Value;
7512
7513                         if (type.IsPointer) {
7514                                 ec.Report.Error (1919, loc, "Unsafe type `{0}' cannot be used in an object creation expression",
7515                                         type.GetSignatureForError ());
7516                                 return null;
7517                         }
7518
7519                         if (arguments == null) {
7520                                 Constant c = Constantify (type, RequestedType.Location);
7521                                 if (c != null)
7522                                         return ReducedExpression.Create (c, this);
7523                         }
7524
7525                         if (type.IsDelegate) {
7526                                 return (new NewDelegate (type, arguments, loc)).Resolve (ec);
7527                         }
7528
7529                         var tparam = type as TypeParameterSpec;
7530                         if (tparam != null) {
7531                                 //
7532                                 // Check whether the type of type parameter can be constructed. BaseType can be a struct for method overrides
7533                                 // where type parameter constraint is inflated to struct
7534                                 //
7535                                 if ((tparam.SpecialConstraint & (SpecialConstraint.Struct | SpecialConstraint.Constructor)) == 0 && !TypeSpec.IsValueType (tparam)) {
7536                                         ec.Report.Error (304, loc,
7537                                                 "Cannot create an instance of the variable type `{0}' because it does not have the new() constraint",
7538                                                 type.GetSignatureForError ());
7539                                 }
7540
7541                                 if ((arguments != null) && (arguments.Count != 0)) {
7542                                         ec.Report.Error (417, loc,
7543                                                 "`{0}': cannot provide arguments when creating an instance of a variable type",
7544                                                 type.GetSignatureForError ());
7545                                 }
7546
7547                                 return this;
7548                         }
7549
7550                         if (type.IsStatic) {
7551                                 ec.Report.SymbolRelatedToPreviousError (type);
7552                                 ec.Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", type.GetSignatureForError ());
7553                                 return null;
7554                         }
7555
7556                         if (type.IsInterface || type.IsAbstract){
7557                                 if (!TypeManager.IsGenericType (type)) {
7558                                         RequestedType = CheckComImport (ec);
7559                                         if (RequestedType != null)
7560                                                 return RequestedType;
7561                                 }
7562                                 
7563                                 ec.Report.SymbolRelatedToPreviousError (type);
7564                                 ec.Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", type.GetSignatureForError ());
7565                                 return null;
7566                         }
7567
7568                         bool dynamic;
7569                         if (arguments != null) {
7570                                 arguments.Resolve (ec, out dynamic);
7571                         } else {
7572                                 dynamic = false;
7573                         }
7574
7575                         method = ConstructorLookup (ec, type, ref arguments, loc);
7576
7577                         if (dynamic) {
7578                                 arguments.Insert (0, new Argument (new TypeOf (type, loc).Resolve (ec), Argument.AType.DynamicTypeName));
7579                                 return new DynamicConstructorBinder (type, arguments, loc).Resolve (ec);
7580                         }
7581
7582                         return this;
7583                 }
7584
7585                 void DoEmitTypeParameter (EmitContext ec)
7586                 {
7587                         var m = ec.Module.PredefinedMembers.ActivatorCreateInstance.Resolve (loc);
7588                         if (m == null)
7589                                 return;
7590
7591                         var ctor_factory = m.MakeGenericMethod (ec.MemberContext, type);
7592                         ec.Emit (OpCodes.Call, ctor_factory);
7593                 }
7594
7595                 //
7596                 // This Emit can be invoked in two contexts:
7597                 //    * As a mechanism that will leave a value on the stack (new object)
7598                 //    * As one that wont (init struct)
7599                 //
7600                 // If we are dealing with a ValueType, we have a few
7601                 // situations to deal with:
7602                 //
7603                 //    * The target is a ValueType, and we have been provided
7604                 //      the instance (this is easy, we are being assigned).
7605                 //
7606                 //    * The target of New is being passed as an argument,
7607                 //      to a boxing operation or a function that takes a
7608                 //      ValueType.
7609                 //
7610                 //      In this case, we need to create a temporary variable
7611                 //      that is the argument of New.
7612                 //
7613                 // Returns whether a value is left on the stack
7614                 //
7615                 // *** Implementation note ***
7616                 //
7617                 // To benefit from this optimization, each assignable expression
7618                 // has to manually cast to New and call this Emit.
7619                 //
7620                 // TODO: It's worth to implement it for arrays and fields
7621                 //
7622                 public virtual bool Emit (EmitContext ec, IMemoryLocation target)
7623                 {
7624                         bool is_value_type = type.IsStructOrEnum;
7625                         VariableReference vr = target as VariableReference;
7626
7627                         bool prepare_await = ec.HasSet (BuilderContext.Options.AsyncBody) && arguments?.ContainsEmitWithAwait () == true;
7628
7629                         if (target != null && is_value_type && (vr != null || method == null)) {
7630                                 if (prepare_await) {
7631                                         arguments = arguments.Emit (ec, false, true);
7632                                         prepare_await = false;
7633                                 }
7634                                 
7635                                 target.AddressOf (ec, AddressOp.Store);
7636                         } else if (vr != null && vr.IsRef) {
7637                                 vr.EmitLoad (ec);
7638                         }
7639
7640                         if (arguments != null) {
7641                                 if (prepare_await)
7642                                         arguments = arguments.Emit (ec, false, true);
7643
7644                                 arguments.Emit (ec);
7645                         }
7646
7647                         if (is_value_type) {
7648                                 if (method == null) {
7649                                         ec.Emit (OpCodes.Initobj, type);
7650                                         return false;
7651                                 }
7652
7653                                 if (vr != null) {
7654                                         ec.MarkCallEntry (loc);
7655                                         ec.Emit (OpCodes.Call, method);
7656                                         return false;
7657                                 }
7658                         }
7659                         
7660                         if (type is TypeParameterSpec) {
7661                                 DoEmitTypeParameter (ec);
7662                                 return true;
7663                         }
7664
7665                         ec.MarkCallEntry (loc);
7666                         ec.Emit (OpCodes.Newobj, method);
7667                         return true;
7668                 }
7669
7670                 public override void Emit (EmitContext ec)
7671                 {
7672                         LocalTemporary v = null;
7673                         if (method == null && type.IsStructOrEnum) {
7674                                 // TODO: Use temporary variable from pool
7675                                 v = new LocalTemporary (type);
7676                         }
7677
7678                         if (!Emit (ec, v))
7679                                 v.Emit (ec);
7680                 }
7681
7682                 public override void EmitStatement (EmitContext ec)
7683                 {
7684                         LocalTemporary v = null;
7685                         if (method == null && TypeSpec.IsValueType (type)) {
7686                                 // TODO: Use temporary variable from pool
7687                                 v = new LocalTemporary (type);
7688                         }
7689
7690                         if (Emit (ec, v))
7691                                 ec.Emit (OpCodes.Pop);
7692                 }
7693
7694                 public virtual bool CanEmitOptimizedLocalTarget (EmitContext ec)
7695                 {
7696                         return true;
7697                 }
7698
7699                 public override void FlowAnalysis (FlowAnalysisContext fc)
7700                 {
7701                         if (arguments != null)
7702                                 arguments.FlowAnalysis (fc);
7703                 }
7704
7705                 public void AddressOf (EmitContext ec, AddressOp mode)
7706                 {
7707                         EmitAddressOf (ec, mode);
7708                 }
7709
7710                 protected virtual IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp mode)
7711                 {
7712                         LocalTemporary value_target = new LocalTemporary (type);
7713
7714                         if (type is TypeParameterSpec) {
7715                                 DoEmitTypeParameter (ec);
7716                                 value_target.Store (ec);
7717                                 value_target.AddressOf (ec, mode);
7718                                 return value_target;
7719                         }
7720
7721                         value_target.AddressOf (ec, AddressOp.Store);
7722
7723                         if (method == null) {
7724                                 ec.Emit (OpCodes.Initobj, type);
7725                         } else {
7726                                 if (arguments != null)
7727                                         arguments.Emit (ec);
7728
7729                                 ec.Emit (OpCodes.Call, method);
7730                         }
7731                         
7732                         value_target.AddressOf (ec, mode);
7733                         return value_target;
7734                 }
7735
7736                 protected override void CloneTo (CloneContext clonectx, Expression t)
7737                 {
7738                         New target = (New) t;
7739
7740                         target.RequestedType = RequestedType.Clone (clonectx);
7741                         if (arguments != null){
7742                                 target.arguments = arguments.Clone (clonectx);
7743                         }
7744                 }
7745
7746                 public override SLE.Expression MakeExpression (BuilderContext ctx)
7747                 {
7748 #if STATIC
7749                         return base.MakeExpression (ctx);
7750 #else
7751                         return SLE.Expression.New ((ConstructorInfo) method.GetMetaInfo (), Arguments.MakeExpression (arguments, ctx));
7752 #endif
7753                 }
7754                 
7755                 public override object Accept (StructuralVisitor visitor)
7756                 {
7757                         return visitor.Visit (this);
7758                 }
7759         }
7760
7761         //
7762         // Array initializer expression, the expression is allowed in
7763         // variable or field initialization only which makes it tricky as
7764         // the type has to be infered based on the context either from field
7765         // type or variable type (think of multiple declarators)
7766         //
7767         public class ArrayInitializer : Expression
7768         {
7769                 List<Expression> elements;
7770                 BlockVariable variable;
7771
7772                 public ArrayInitializer (List<Expression> init, Location loc)
7773                 {
7774                         elements = init;
7775                         this.loc = loc;
7776                 }
7777
7778                 public ArrayInitializer (int count, Location loc)
7779                         : this (new List<Expression> (count), loc)
7780                 {
7781                 }
7782
7783                 public ArrayInitializer (Location loc)
7784                         : this (4, loc)
7785                 {
7786                 }
7787
7788                 #region Properties
7789
7790                 public int Count {
7791                         get { return elements.Count; }
7792                 }
7793
7794                 public List<Expression> Elements {
7795                         get {
7796                                 return elements;
7797                         }
7798                 }
7799
7800                 public Expression this [int index] {
7801                         get {
7802                                 return elements [index];
7803                         }
7804                 }
7805
7806                 public BlockVariable VariableDeclaration {
7807                         get {
7808                                 return variable;
7809                         }
7810                         set {
7811                                 variable = value;
7812                         }
7813                 }
7814
7815                 #endregion
7816
7817                 public void Add (Expression expr)
7818                 {
7819                         elements.Add (expr);
7820                 }
7821
7822                 public override bool ContainsEmitWithAwait ()
7823                 {
7824                         throw new NotSupportedException ();
7825                 }
7826
7827                 public override Expression CreateExpressionTree (ResolveContext ec)
7828                 {
7829                         throw new NotSupportedException ("ET");
7830                 }
7831
7832                 protected override void CloneTo (CloneContext clonectx, Expression t)
7833                 {
7834                         var target = (ArrayInitializer) t;
7835
7836                         target.elements = new List<Expression> (elements.Count);
7837                         foreach (var element in elements)
7838                                 target.elements.Add (element.Clone (clonectx));
7839                 }
7840
7841                 protected override Expression DoResolve (ResolveContext rc)
7842                 {
7843                         var current_field = rc.CurrentMemberDefinition as FieldBase;
7844                         TypeExpression type;
7845                         if (current_field != null && rc.CurrentAnonymousMethod == null) {
7846                                 type = new TypeExpression (current_field.MemberType, current_field.Location);
7847                         } else if (variable != null) {
7848                                 if (variable.TypeExpression is VarExpr) {
7849                                         rc.Report.Error (820, loc, "An implicitly typed local variable declarator cannot use an array initializer");
7850                                         return EmptyExpression.Null;
7851                                 }
7852
7853                                 type = new TypeExpression (variable.Variable.Type, variable.Variable.Location);
7854                         } else {
7855                                 throw new NotImplementedException ("Unexpected array initializer context");
7856                         }
7857
7858                         return new ArrayCreation (type, this).Resolve (rc);
7859                 }
7860
7861                 public override void Emit (EmitContext ec)
7862                 {
7863                         throw new InternalErrorException ("Missing Resolve call");
7864                 }
7865
7866                 public override void FlowAnalysis (FlowAnalysisContext fc)
7867                 {
7868                         throw new InternalErrorException ("Missing Resolve call");
7869                 }
7870                 
7871                 public override object Accept (StructuralVisitor visitor)
7872                 {
7873                         return visitor.Visit (this);
7874                 }
7875         }
7876
7877         /// <summary>
7878         ///   14.5.10.2: Represents an array creation expression.
7879         /// </summary>
7880         ///
7881         /// <remarks>
7882         ///   There are two possible scenarios here: one is an array creation
7883         ///   expression that specifies the dimensions and optionally the
7884         ///   initialization data and the other which does not need dimensions
7885         ///   specified but where initialization data is mandatory.
7886         /// </remarks>
7887         public class ArrayCreation : Expression
7888         {
7889                 FullNamedExpression requested_base_type;
7890                 ArrayInitializer initializers;
7891
7892                 //
7893                 // The list of Argument types.
7894                 // This is used to construct the `newarray' or constructor signature
7895                 //
7896                 protected List<Expression> arguments;
7897                 
7898                 protected TypeSpec array_element_type;
7899                 int num_arguments;
7900                 protected int dimensions;
7901                 protected readonly ComposedTypeSpecifier rank;
7902                 Expression first_emit;
7903                 LocalTemporary first_emit_temp;
7904
7905                 protected List<Expression> array_data;
7906
7907                 Dictionary<int, int> bounds;
7908
7909 #if STATIC
7910                 // The number of constants in array initializers
7911                 int const_initializers_count;
7912                 bool only_constant_initializers;
7913 #endif
7914                 public ArrayCreation (FullNamedExpression requested_base_type, List<Expression> exprs, ComposedTypeSpecifier rank, ArrayInitializer initializers, Location l)
7915                         : this (requested_base_type, rank, initializers, l)
7916                 {
7917                         arguments = new List<Expression> (exprs);
7918                         num_arguments = arguments.Count;
7919                 }
7920
7921                 //
7922                 // For expressions like int[] foo = new int[] { 1, 2, 3 };
7923                 //
7924                 public ArrayCreation (FullNamedExpression requested_base_type, ComposedTypeSpecifier rank, ArrayInitializer initializers, Location loc)
7925                 {
7926                         this.requested_base_type = requested_base_type;
7927                         this.rank = rank;
7928                         this.initializers = initializers;
7929                         this.loc = loc;
7930
7931                         if (rank != null)
7932                                 num_arguments = rank.Dimension;
7933                 }
7934
7935                 //
7936                 // For compiler generated single dimensional arrays only
7937                 //
7938                 public ArrayCreation (FullNamedExpression requested_base_type, ArrayInitializer initializers, Location loc)
7939                         : this (requested_base_type, ComposedTypeSpecifier.SingleDimension, initializers, loc)
7940                 {
7941                 }
7942
7943                 //
7944                 // For expressions like int[] foo = { 1, 2, 3 };
7945                 //
7946                 public ArrayCreation (FullNamedExpression requested_base_type, ArrayInitializer initializers)
7947                         : this (requested_base_type, null, initializers, initializers.Location)
7948                 {
7949                 }
7950
7951                 public bool NoEmptyInterpolation { get; set; }
7952
7953                 public ComposedTypeSpecifier Rank {
7954                         get {
7955                                 return this.rank;
7956                         }
7957                 }
7958                 
7959                 public FullNamedExpression TypeExpression {
7960                         get {
7961                                 return this.requested_base_type;
7962                         }
7963                 }
7964                 
7965                 public ArrayInitializer Initializers {
7966                         get {
7967                                 return this.initializers;
7968                         }
7969                 }
7970
7971                 bool CheckIndices (ResolveContext ec, ArrayInitializer probe, int idx, bool specified_dims, int child_bounds)
7972                 {
7973                         if (initializers != null && bounds == null) {
7974                                 //
7975                                 // We use this to store all the data values in the order in which we
7976                                 // will need to store them in the byte blob later
7977                                 //
7978                                 array_data = new List<Expression> (probe.Count);
7979                                 bounds = new Dictionary<int, int> ();
7980                         }
7981
7982                         if (specified_dims) { 
7983                                 Expression a = arguments [idx];
7984                                 a = a.Resolve (ec);
7985                                 if (a == null)
7986                                         return false;
7987
7988                                 a = ConvertExpressionToArrayIndex (ec, a);
7989                                 if (a == null)
7990                                         return false;
7991
7992                                 arguments[idx] = a;
7993
7994                                 if (initializers != null) {
7995                                         Constant c = a as Constant;
7996                                         if (c == null && a is ArrayIndexCast)
7997                                                 c = ((ArrayIndexCast) a).Child as Constant;
7998
7999                                         if (c == null) {
8000                                                 ec.Report.Error (150, a.Location, "A constant value is expected");
8001                                                 return false;
8002                                         }
8003
8004                                         int value;
8005                                         try {
8006                                                 value = System.Convert.ToInt32 (c.GetValue ());
8007                                         } catch {
8008                                                 ec.Report.Error (150, a.Location, "A constant value is expected");
8009                                                 return false;
8010                                         }
8011
8012                                         // TODO: probe.Count does not fit ulong in
8013                                         if (value != probe.Count) {
8014                                                 ec.Report.Error (847, loc, "An array initializer of length `{0}' was expected", value.ToString ());
8015                                                 return false;
8016                                         }
8017
8018                                         bounds[idx] = value;
8019                                 }
8020                         }
8021
8022                         if (initializers == null)
8023                                 return true;
8024
8025                         for (int i = 0; i < probe.Count; ++i) {
8026                                 var o = probe [i];
8027                                 if (o is ArrayInitializer) {
8028                                         var sub_probe = o as ArrayInitializer;
8029                                         if (idx + 1 >= dimensions){
8030                                                 ec.Report.Error (623, loc, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
8031                                                 return false;
8032                                         }
8033
8034                                         // When we don't have explicitly specified dimensions, record whatever dimension we first encounter at each level
8035                                         if (!bounds.ContainsKey(idx + 1))
8036                                                 bounds[idx + 1] = sub_probe.Count;
8037
8038                                         if (bounds[idx + 1] != sub_probe.Count) {
8039                                                 ec.Report.Error(847, sub_probe.Location, "An array initializer of length `{0}' was expected", bounds[idx + 1].ToString());
8040                                                 return false;
8041                                         }
8042
8043                                         bool ret = CheckIndices (ec, sub_probe, idx + 1, specified_dims, child_bounds - 1);
8044                                         if (!ret)
8045                                                 return false;
8046                                 } else if (child_bounds > 1) {
8047                                         ec.Report.Error (846, o.Location, "A nested array initializer was expected");
8048                                 } else {
8049                                         Expression element = ResolveArrayElement (ec, o);
8050                                         if (element == null)
8051                                                 continue;
8052 #if STATIC
8053                                         // Initializers with the default values can be ignored
8054                                         Constant c = element as Constant;
8055                                         if (c != null) {
8056                                                 if (!c.IsDefaultInitializer (array_element_type)) {
8057                                                         ++const_initializers_count;
8058                                                 }
8059                                         } else {
8060                                                 only_constant_initializers = false;
8061                                         }
8062 #endif                                  
8063                                         array_data.Add (element);
8064                                 }
8065                         }
8066
8067                         return true;
8068                 }
8069
8070                 public override bool ContainsEmitWithAwait ()
8071                 {
8072                         foreach (var arg in arguments) {
8073                                 if (arg.ContainsEmitWithAwait ())
8074                                         return true;
8075                         }
8076
8077                         return InitializersContainAwait ();
8078                 }
8079
8080                 public override Expression CreateExpressionTree (ResolveContext ec)
8081                 {
8082                         Arguments args;
8083
8084                         if (array_data == null) {
8085                                 args = new Arguments (arguments.Count + 1);
8086                                 args.Add (new Argument (new TypeOf (array_element_type, loc)));
8087                                 foreach (Expression a in arguments)
8088                                         args.Add (new Argument (a.CreateExpressionTree (ec)));
8089
8090                                 return CreateExpressionFactoryCall (ec, "NewArrayBounds", args);
8091                         }
8092
8093                         if (dimensions > 1) {
8094                                 ec.Report.Error (838, loc, "An expression tree cannot contain a multidimensional array initializer");
8095                                 return null;
8096                         }
8097
8098                         args = new Arguments (array_data == null ? 1 : array_data.Count + 1);
8099                         args.Add (new Argument (new TypeOf (array_element_type, loc)));
8100                         if (array_data != null) {
8101                                 for (int i = 0; i < array_data.Count; ++i) {
8102                                         Expression e = array_data [i];
8103                                         args.Add (new Argument (e.CreateExpressionTree (ec)));
8104                                 }
8105                         }
8106
8107                         return CreateExpressionFactoryCall (ec, "NewArrayInit", args);
8108                 }               
8109                 
8110                 void UpdateIndices (ResolveContext rc)
8111                 {
8112                         int i = 0;
8113                         for (var probe = initializers; probe != null;) {
8114                                 Expression e = new IntConstant (rc.BuiltinTypes, probe.Count, Location.Null);
8115                                 arguments.Add (e);
8116                                 bounds[i++] = probe.Count;
8117
8118                                 if (probe.Count > 0 && probe [0] is ArrayInitializer) {
8119                                         probe = (ArrayInitializer) probe[0];
8120                                 } else if (dimensions > i) {
8121                                         continue;
8122                                 } else {
8123                                         return;
8124                                 }
8125                         }
8126                 }
8127
8128                 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
8129                 {
8130                         ec.Report.Error (248, loc, "Cannot create an array with a negative size");
8131                 }
8132
8133                 public override void FlowAnalysis (FlowAnalysisContext fc)
8134                 {
8135                         foreach (var arg in arguments)
8136                                 arg.FlowAnalysis (fc);
8137
8138                         if (array_data != null) {
8139                                 foreach (var ad in array_data)
8140                                         ad.FlowAnalysis (fc);
8141                         }
8142                 }
8143
8144                 bool InitializersContainAwait ()
8145                 {
8146                         if (array_data == null)
8147                                 return false;
8148
8149                         foreach (var expr in array_data) {
8150                                 if (expr.ContainsEmitWithAwait ())
8151                                         return true;
8152                         }
8153
8154                         return false;
8155                 }
8156
8157                 protected virtual Expression ResolveArrayElement (ResolveContext ec, Expression element)
8158                 {
8159                         element = element.Resolve (ec);
8160                         if (element == null)
8161                                 return null;
8162
8163                         var te = element as CompoundAssign.TargetExpression;
8164                         if (te != null) {
8165                                 for (int i = 1; i < initializers.Count; ++i) {
8166                                         if (initializers [i].ContainsEmitWithAwait ()) {
8167                                                 te.RequiresEmitWithAwait = true;
8168                                                 break;
8169                                         }
8170                                 }
8171
8172                                 if (!te.RequiresEmitWithAwait) {
8173                                         if (first_emit != null)
8174                                                 throw new InternalErrorException ("Can only handle one mutator at a time");
8175                                         first_emit = element;
8176                                         element = first_emit_temp = new LocalTemporary (element.Type);
8177                                 }
8178                         }
8179
8180                         return Convert.ImplicitConversionRequired (
8181                                 ec, element, array_element_type, loc);
8182                 }
8183
8184                 protected bool ResolveInitializers (ResolveContext ec)
8185                 {
8186 #if STATIC
8187                         only_constant_initializers = true;
8188 #endif
8189
8190                         if (arguments != null) {
8191                                 bool res = true;
8192                                 for (int i = 0; i < arguments.Count; ++i) {
8193                                         res &= CheckIndices (ec, initializers, i, true, dimensions);
8194                                         if (initializers != null)
8195                                                 break;
8196                                 }
8197
8198                                 return res;
8199                         }
8200
8201                         arguments = new List<Expression> ();
8202
8203                         if (!CheckIndices (ec, initializers, 0, false, dimensions))
8204                                 return false;
8205                                 
8206                         UpdateIndices (ec);
8207                                 
8208                         return true;
8209                 }
8210
8211                 //
8212                 // Resolved the type of the array
8213                 //
8214                 bool ResolveArrayType (ResolveContext ec)
8215                 {
8216                         //
8217                         // Lookup the type
8218                         //
8219                         FullNamedExpression array_type_expr;
8220                         if (num_arguments > 0) {
8221                                 array_type_expr = new ComposedCast (requested_base_type, rank);
8222                         } else {
8223                                 array_type_expr = requested_base_type;
8224                         }
8225
8226                         type = array_type_expr.ResolveAsType (ec);
8227                         if (array_type_expr == null)
8228                                 return false;
8229
8230                         var ac = type as ArrayContainer;
8231                         if (ac == null) {
8232                                 ec.Report.Error (622, loc, "Can only use array initializer expressions to assign to array types. Try using a new expression instead");
8233                                 return false;
8234                         }
8235
8236                         array_element_type = ac.Element;
8237                         dimensions = ac.Rank;
8238
8239                         return true;
8240                 }
8241
8242                 protected override Expression DoResolve (ResolveContext ec)
8243                 {
8244                         if (type != null)
8245                                 return this;
8246
8247                         if (!ResolveArrayType (ec))
8248                                 return null;
8249
8250                         //
8251                         // validate the initializers and fill in any missing bits
8252                         //
8253                         if (!ResolveInitializers (ec))
8254                                 return null;
8255
8256                         eclass = ExprClass.Value;
8257                         return this;
8258                 }
8259
8260                 byte [] MakeByteBlob ()
8261                 {
8262                         int factor;
8263                         byte [] data;
8264                         byte [] element;
8265                         int count = array_data.Count;
8266
8267                         TypeSpec element_type = array_element_type;
8268                         if (element_type.IsEnum)
8269                                 element_type = EnumSpec.GetUnderlyingType (element_type);
8270
8271                         factor = BuiltinTypeSpec.GetSize (element_type);
8272                         if (factor == 0)
8273                                 throw new Exception ("unrecognized type in MakeByteBlob: " + element_type);
8274
8275                         data = new byte [(count * factor + 3) & ~3];
8276                         int idx = 0;
8277
8278                         for (int i = 0; i < count; ++i) {
8279                                 var c = array_data[i] as Constant;
8280                                 if (c == null) {
8281                                         idx += factor;
8282                                         continue;
8283                                 }
8284
8285                                 object v = c.GetValue ();
8286
8287                                 switch (element_type.BuiltinType) {
8288                                 case BuiltinTypeSpec.Type.Long:
8289                                         long lval = (long) v;
8290
8291                                         for (int j = 0; j < factor; ++j) {
8292                                                 data[idx + j] = (byte) (lval & 0xFF);
8293                                                 lval = (lval >> 8);
8294                                         }
8295                                         break;
8296                                 case BuiltinTypeSpec.Type.ULong:
8297                                         ulong ulval = (ulong) v;
8298
8299                                         for (int j = 0; j < factor; ++j) {
8300                                                 data[idx + j] = (byte) (ulval & 0xFF);
8301                                                 ulval = (ulval >> 8);
8302                                         }
8303                                         break;
8304                                 case BuiltinTypeSpec.Type.Float:
8305                                         var fval = SingleConverter.SingleToInt32Bits((float) v);
8306
8307                                         data[idx] = (byte) (fval & 0xff);
8308                                         data[idx + 1] = (byte) ((fval >> 8) & 0xff);
8309                                         data[idx + 2] = (byte) ((fval >> 16) & 0xff);
8310                                         data[idx + 3] = (byte) (fval >> 24);
8311                                         break;
8312                                 case BuiltinTypeSpec.Type.Double:
8313                                         element = BitConverter.GetBytes ((double) v);
8314
8315                                         for (int j = 0; j < factor; ++j)
8316                                                 data[idx + j] = element[j];
8317
8318                                         // FIXME: Handle the ARM float format.
8319                                         if (!BitConverter.IsLittleEndian)
8320                                                 System.Array.Reverse (data, idx, 8);
8321                                         break;
8322                                 case BuiltinTypeSpec.Type.Char:
8323                                         int chval = (int) ((char) v);
8324
8325                                         data[idx] = (byte) (chval & 0xff);
8326                                         data[idx + 1] = (byte) (chval >> 8);
8327                                         break;
8328                                 case BuiltinTypeSpec.Type.Short:
8329                                         int sval = (int) ((short) v);
8330
8331                                         data[idx] = (byte) (sval & 0xff);
8332                                         data[idx + 1] = (byte) (sval >> 8);
8333                                         break;
8334                                 case BuiltinTypeSpec.Type.UShort:
8335                                         int usval = (int) ((ushort) v);
8336
8337                                         data[idx] = (byte) (usval & 0xff);
8338                                         data[idx + 1] = (byte) (usval >> 8);
8339                                         break;
8340                                 case BuiltinTypeSpec.Type.Int:
8341                                         int val = (int) v;
8342
8343                                         data[idx] = (byte) (val & 0xff);
8344                                         data[idx + 1] = (byte) ((val >> 8) & 0xff);
8345                                         data[idx + 2] = (byte) ((val >> 16) & 0xff);
8346                                         data[idx + 3] = (byte) (val >> 24);
8347                                         break;
8348                                 case BuiltinTypeSpec.Type.UInt:
8349                                         uint uval = (uint) v;
8350
8351                                         data[idx] = (byte) (uval & 0xff);
8352                                         data[idx + 1] = (byte) ((uval >> 8) & 0xff);
8353                                         data[idx + 2] = (byte) ((uval >> 16) & 0xff);
8354                                         data[idx + 3] = (byte) (uval >> 24);
8355                                         break;
8356                                 case BuiltinTypeSpec.Type.SByte:
8357                                         data[idx] = (byte) (sbyte) v;
8358                                         break;
8359                                 case BuiltinTypeSpec.Type.Byte:
8360                                         data[idx] = (byte) v;
8361                                         break;
8362                                 case BuiltinTypeSpec.Type.Bool:
8363                                         data[idx] = (byte) ((bool) v ? 1 : 0);
8364                                         break;
8365                                 case BuiltinTypeSpec.Type.Decimal:
8366                                         int[] bits = Decimal.GetBits ((decimal) v);
8367                                         int p = idx;
8368
8369                                         // FIXME: For some reason, this doesn't work on the MS runtime.
8370                                         int[] nbits = new int[4];
8371                                         nbits[0] = bits[3];
8372                                         nbits[1] = bits[2];
8373                                         nbits[2] = bits[0];
8374                                         nbits[3] = bits[1];
8375
8376                                         for (int j = 0; j < 4; j++) {
8377                                                 data[p++] = (byte) (nbits[j] & 0xff);
8378                                                 data[p++] = (byte) ((nbits[j] >> 8) & 0xff);
8379                                                 data[p++] = (byte) ((nbits[j] >> 16) & 0xff);
8380                                                 data[p++] = (byte) (nbits[j] >> 24);
8381                                         }
8382                                         break;
8383                                 default:
8384                                         throw new Exception ("Unrecognized type in MakeByteBlob: " + element_type);
8385                                 }
8386
8387                                 idx += factor;
8388                         }
8389
8390                         return data;
8391                 }
8392
8393                 public override SLE.Expression MakeExpression (BuilderContext ctx)
8394                 {
8395 #if STATIC
8396                         return base.MakeExpression (ctx);
8397 #else
8398                         var initializers = new SLE.Expression [array_data.Count];
8399                         for (var i = 0; i < initializers.Length; i++) {
8400                                 if (array_data [i] == null)
8401                                         initializers [i] = SLE.Expression.Default (array_element_type.GetMetaInfo ());
8402                                 else
8403                                         initializers [i] = array_data [i].MakeExpression (ctx);
8404                         }
8405
8406                         return SLE.Expression.NewArrayInit (array_element_type.GetMetaInfo (), initializers);
8407 #endif
8408                 }
8409 #if STATIC
8410                 //
8411                 // Emits the initializers for the array
8412                 //
8413                 void EmitStaticInitializers (EmitContext ec, FieldExpr stackArray)
8414                 {
8415                         var m = ec.Module.PredefinedMembers.RuntimeHelpersInitializeArray.Resolve (loc);
8416                         if (m == null)
8417                                 return;
8418
8419                         //
8420                         // First, the static data
8421                         //
8422                         byte [] data = MakeByteBlob ();
8423                         var fb = ec.CurrentTypeDefinition.Module.MakeStaticData (data, loc);
8424
8425                         if (stackArray == null) {
8426                                 ec.Emit (OpCodes.Dup);
8427                         } else {
8428                                 stackArray.Emit (ec);
8429                         }
8430
8431                         ec.Emit (OpCodes.Ldtoken, fb);
8432                         ec.Emit (OpCodes.Call, m);
8433                 }
8434 #endif
8435
8436                 //
8437                 // Emits pieces of the array that can not be computed at compile
8438                 // time (variables and string locations).
8439                 //
8440                 // This always expect the top value on the stack to be the array
8441                 //
8442                 void EmitDynamicInitializers (EmitContext ec, bool emitConstants, StackFieldExpr stackArray)
8443                 {
8444                         int dims = bounds.Count;
8445                         var current_pos = new int [dims];
8446
8447                         for (int i = 0; i < array_data.Count; i++){
8448
8449                                 Expression e = array_data [i];
8450                                 var c = e as Constant;
8451
8452                                 // Constant can be initialized via StaticInitializer
8453                                 if (c == null || (c != null && emitConstants && !c.IsDefaultInitializer (array_element_type))) {
8454
8455                                         var etype = e.Type;
8456
8457                                         if (stackArray != null) {
8458                                                 if (e.ContainsEmitWithAwait ()) {
8459                                                         e = e.EmitToField (ec);
8460                                                 }
8461
8462                                                 stackArray.EmitLoad (ec);
8463                                         } else {
8464                                                 ec.Emit (OpCodes.Dup);
8465                                         }
8466
8467                                         for (int idx = 0; idx < dims; idx++) 
8468                                                 ec.EmitInt (current_pos [idx]);
8469
8470                                         //
8471                                         // If we are dealing with a struct, get the
8472                                         // address of it, so we can store it.
8473                                         //
8474                                         if (dims == 1 && etype.IsStruct && !BuiltinTypeSpec.IsPrimitiveType (etype))
8475                                                 ec.Emit (OpCodes.Ldelema, etype);
8476
8477                                         e.Emit (ec);
8478
8479                                         ec.EmitArrayStore ((ArrayContainer) type);
8480                                 }
8481                                 
8482                                 //
8483                                 // Advance counter
8484                                 //
8485                                 for (int j = dims - 1; j >= 0; j--){
8486                                         current_pos [j]++;
8487                                         if (current_pos [j] < bounds [j])
8488                                                 break;
8489                                         current_pos [j] = 0;
8490                                 }
8491                         }
8492
8493                         if (stackArray != null)
8494                                 stackArray.PrepareCleanup (ec);
8495                 }
8496
8497                 public override void Emit (EmitContext ec)
8498                 {
8499                         if (!NoEmptyInterpolation && EmitOptimizedEmpty (ec))
8500                                 return;
8501
8502                         var await_field = EmitToFieldSource (ec);
8503                         if (await_field != null)
8504                                 await_field.Emit (ec);
8505                 }
8506
8507                 bool EmitOptimizedEmpty (EmitContext ec)
8508                 {
8509                         if (arguments.Count != 1 || dimensions != 1)
8510                                 return false;
8511
8512                         var c = arguments [0] as Constant;
8513                         if (c == null || !c.IsZeroInteger)
8514                                 return false;
8515
8516                         var m = ec.Module.PredefinedMembers.ArrayEmpty.Get ();
8517                         if (m == null || ec.CurrentType.MemberDefinition.DeclaringAssembly == m.DeclaringType.MemberDefinition.DeclaringAssembly)
8518                                 return false;
8519
8520                         m = m.MakeGenericMethod (ec.MemberContext, array_element_type);
8521                         ec.Emit (OpCodes.Call, m);
8522                         return true;
8523                 }
8524
8525                 protected sealed override FieldExpr EmitToFieldSource (EmitContext ec)
8526                 {
8527                         if (first_emit != null) {
8528                                 first_emit.Emit (ec);
8529                                 first_emit_temp.Store (ec);
8530                         }
8531
8532                         StackFieldExpr await_stack_field;
8533                         if (ec.HasSet (BuilderContext.Options.AsyncBody) && InitializersContainAwait ()) {
8534                                 await_stack_field = ec.GetTemporaryField (type);
8535                                 ec.EmitThis ();
8536                         } else {
8537                                 await_stack_field = null;
8538                         }
8539
8540                         EmitExpressionsList (ec, arguments);
8541
8542                         ec.EmitArrayNew ((ArrayContainer) type);
8543                         
8544                         if (initializers == null)
8545                                 return await_stack_field;
8546
8547                         if (await_stack_field != null)
8548                                 await_stack_field.EmitAssignFromStack (ec);
8549
8550 #if STATIC
8551                         //
8552                         // Emit static initializer for arrays which contain more than 2 items and
8553                         // the static initializer will initialize at least 25% of array values or there
8554                         // is more than 10 items to be initialized
8555                         //
8556                         // NOTE: const_initializers_count does not contain default constant values.
8557                         //
8558                         if (const_initializers_count > 2 && (array_data.Count > 10 || const_initializers_count * 4 > (array_data.Count)) &&
8559                                 (BuiltinTypeSpec.IsPrimitiveType (array_element_type) || array_element_type.IsEnum)) {
8560                                 EmitStaticInitializers (ec, await_stack_field);
8561
8562                                 if (!only_constant_initializers)
8563                                         EmitDynamicInitializers (ec, false, await_stack_field);
8564                         } else
8565 #endif
8566                         {
8567                                 EmitDynamicInitializers (ec, true, await_stack_field);
8568                         }
8569
8570                         if (first_emit_temp != null)
8571                                 first_emit_temp.Release (ec);
8572
8573                         return await_stack_field;
8574                 }
8575
8576                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
8577                 {
8578                         // no multi dimensional or jagged arrays
8579                         if (arguments.Count != 1 || array_element_type.IsArray) {
8580                                 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
8581                                 return;
8582                         }
8583
8584                         // No array covariance, except for array -> object
8585                         if (type != targetType) {
8586                                 if (targetType.BuiltinType != BuiltinTypeSpec.Type.Object) {
8587                                         base.EncodeAttributeValue (rc, enc, targetType, parameterType);
8588                                         return;
8589                                 }
8590
8591                                 if (enc.Encode (type) == AttributeEncoder.EncodedTypeProperties.DynamicType) {
8592                                         Attribute.Error_AttributeArgumentIsDynamic (rc, loc);
8593                                         return;
8594                                 }
8595                         }
8596
8597                         // Single dimensional array of 0 size
8598                         if (array_data == null) {
8599                                 IntConstant ic = arguments[0] as IntConstant;
8600                                 if (ic == null || !ic.IsDefaultValue) {
8601                                         base.EncodeAttributeValue (rc, enc, targetType, parameterType);
8602                                 } else {
8603                                         enc.Encode (0);
8604                                 }
8605
8606                                 return;
8607                         }
8608
8609                         enc.Encode (array_data.Count);
8610                         foreach (var element in array_data) {
8611                                 element.EncodeAttributeValue (rc, enc, array_element_type, parameterType);
8612                         }
8613                 }
8614                 
8615                 protected override void CloneTo (CloneContext clonectx, Expression t)
8616                 {
8617                         ArrayCreation target = (ArrayCreation) t;
8618
8619                         if (requested_base_type != null)
8620                                 target.requested_base_type = (FullNamedExpression)requested_base_type.Clone (clonectx);
8621
8622                         if (arguments != null){
8623                                 target.arguments = new List<Expression> (arguments.Count);
8624                                 foreach (Expression e in arguments)
8625                                         target.arguments.Add (e.Clone (clonectx));
8626                         }
8627
8628                         if (initializers != null)
8629                                 target.initializers = (ArrayInitializer) initializers.Clone (clonectx);
8630                 }
8631                 
8632                 public override object Accept (StructuralVisitor visitor)
8633                 {
8634                         return visitor.Visit (this);
8635                 }
8636         }
8637         
8638         //
8639         // Represents an implicitly typed array epxression
8640         //
8641         class ImplicitlyTypedArrayCreation : ArrayCreation
8642         {
8643                 TypeInferenceContext best_type_inference;
8644
8645                 public ImplicitlyTypedArrayCreation (ComposedTypeSpecifier rank, ArrayInitializer initializers, Location loc)
8646                         : base (null, rank, initializers, loc)
8647                 {                       
8648                 }
8649
8650                 public ImplicitlyTypedArrayCreation (ArrayInitializer initializers, Location loc)
8651                         : base (null, initializers, loc)
8652                 {
8653                 }
8654
8655                 protected override Expression DoResolve (ResolveContext ec)
8656                 {
8657                         if (type != null)
8658                                 return this;
8659
8660                         dimensions = rank.Dimension;
8661
8662                         best_type_inference = new TypeInferenceContext ();
8663
8664                         if (!ResolveInitializers (ec))
8665                                 return null;
8666
8667                         best_type_inference.FixAllTypes (ec);
8668                         array_element_type = best_type_inference.InferredTypeArguments[0];
8669                         best_type_inference = null;
8670
8671                         if (array_element_type == null ||
8672                                 array_element_type == InternalType.NullLiteral || array_element_type == InternalType.MethodGroup || array_element_type == InternalType.AnonymousMethod ||
8673                                 arguments.Count != rank.Dimension) {
8674                                 ec.Report.Error (826, loc,
8675                                         "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
8676                                 return null;
8677                         }
8678
8679                         //
8680                         // At this point we found common base type for all initializer elements
8681                         // but we have to be sure that all static initializer elements are of
8682                         // same type
8683                         //
8684                         UnifyInitializerElement (ec);
8685
8686                         type = ArrayContainer.MakeType (ec.Module, array_element_type, dimensions);
8687                         eclass = ExprClass.Value;
8688                         return this;
8689                 }
8690
8691                 //
8692                 // Converts static initializer only
8693                 //
8694                 void UnifyInitializerElement (ResolveContext ec)
8695                 {
8696                         for (int i = 0; i < array_data.Count; ++i) {
8697                                 Expression e = array_data[i];
8698                                 if (e != null)
8699                                         array_data [i] = Convert.ImplicitConversion (ec, e, array_element_type, Location.Null);
8700                         }
8701                 }
8702
8703                 protected override Expression ResolveArrayElement (ResolveContext ec, Expression element)
8704                 {
8705                         element = element.Resolve (ec);
8706                         if (element != null)
8707                                 best_type_inference.AddCommonTypeBound (element.Type);
8708
8709                         return element;
8710                 }
8711         }       
8712         
8713         sealed class CompilerGeneratedThis : This
8714         {
8715                 public CompilerGeneratedThis (TypeSpec type, Location loc)
8716                         : base (loc)
8717                 {
8718                         this.type = type;
8719                 }
8720
8721                 protected override Expression DoResolve (ResolveContext rc)
8722                 {
8723                         eclass = ExprClass.Variable;
8724
8725                         var block = rc.CurrentBlock;
8726                         if (block != null) {
8727                                 var top = block.ParametersBlock.TopBlock;
8728                                 if (top.ThisVariable != null)
8729                                         variable_info = top.ThisVariable.VariableInfo;
8730
8731                         }
8732
8733                         return this;
8734                 }
8735
8736                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
8737                 {
8738                         return DoResolve (rc);
8739                 }
8740
8741                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
8742                 {
8743                         return null;
8744                 }
8745         }
8746         
8747         /// <summary>
8748         ///   Represents the `this' construct
8749         /// </summary>
8750
8751         public class This : VariableReference
8752         {
8753                 sealed class ThisVariable : ILocalVariable
8754                 {
8755                         public static readonly ILocalVariable Instance = new ThisVariable ();
8756
8757                         public void Emit (EmitContext ec)
8758                         {
8759                                 ec.EmitThis ();
8760                         }
8761
8762                         public void EmitAssign (EmitContext ec)
8763                         {
8764                                 throw new InvalidOperationException ();
8765                         }
8766
8767                         public void EmitAddressOf (EmitContext ec)
8768                         {
8769                                 ec.EmitThis ();
8770                         }
8771                 }
8772
8773                 protected VariableInfo variable_info;
8774
8775                 public This (Location loc)
8776                 {
8777                         this.loc = loc;
8778                 }
8779
8780                 #region Properties
8781
8782                 public override string Name {
8783                         get { return "this"; }
8784                 }
8785
8786                 public override bool IsLockedByStatement {
8787                         get {
8788                                 return false;
8789                         }
8790                         set {
8791                         }
8792                 }
8793
8794                 public override bool IsRef {
8795                         get { return type.IsStruct; }
8796                 }
8797
8798                 public override bool IsSideEffectFree {
8799                         get {
8800                                 return true;
8801                         }
8802                 }
8803
8804                 protected override ILocalVariable Variable {
8805                         get { return ThisVariable.Instance; }
8806                 }
8807
8808                 public override VariableInfo VariableInfo {
8809                         get { return variable_info; }
8810                 }
8811
8812                 public override bool IsFixed {
8813                         get { return false; }
8814                 }
8815
8816                 #endregion
8817
8818                 void CheckStructThisDefiniteAssignment (FlowAnalysisContext fc)
8819                 {
8820                         //
8821                         // It's null for all cases when we don't need to check `this'
8822                         // definitive assignment
8823                         //
8824                         if (variable_info == null)
8825                                 return;
8826
8827                         if (fc.IsDefinitelyAssigned (variable_info))
8828                                 return;
8829
8830                         fc.Report.Error (188, loc, "The `this' object cannot be used before all of its fields are assigned to");
8831                 }
8832
8833                 protected virtual void Error_ThisNotAvailable (ResolveContext ec)
8834                 {
8835                         if (ec.IsStatic && !ec.HasSet (ResolveContext.Options.ConstantScope)) {
8836                                 ec.Report.Error (26, loc, "Keyword `this' is not valid in a static property, static method, or static field initializer");
8837                         } else if (ec.CurrentAnonymousMethod != null) {
8838                                 ec.Report.Error (1673, loc,
8839                                         "Anonymous methods inside structs cannot access instance members of `this'. " +
8840                                         "Consider copying `this' to a local variable outside the anonymous method and using the local instead");
8841                         } else {
8842                                 ec.Report.Error (27, loc, "Keyword `this' is not available in the current context");
8843                         }
8844                 }
8845
8846                 public override void FlowAnalysis (FlowAnalysisContext fc)
8847                 {
8848                         CheckStructThisDefiniteAssignment (fc);
8849                 }
8850
8851                 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
8852                 {
8853                         if (ae == null)
8854                                 return null;
8855
8856                         AnonymousMethodStorey storey = ae.Storey;
8857                         return storey != null ? storey.HoistedThis : null;
8858                 }
8859
8860                 public static bool IsThisAvailable (ResolveContext ec, bool ignoreAnonymous)
8861                 {
8862                         if (ec.IsStatic || ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.BaseInitializer | ResolveContext.Options.ConstantScope))
8863                                 return false;
8864
8865                         if (ignoreAnonymous || ec.CurrentAnonymousMethod == null)
8866                                 return true;
8867
8868                         if (ec.CurrentType.IsStruct && !(ec.CurrentAnonymousMethod is StateMachineInitializer))
8869                                 return false;
8870
8871                         return true;
8872                 }
8873
8874                 public virtual void ResolveBase (ResolveContext ec)
8875                 {
8876                         eclass = ExprClass.Variable;
8877                         type = ec.CurrentType;
8878
8879                         if (!IsThisAvailable (ec, false)) {
8880                                 Error_ThisNotAvailable (ec);
8881                                 return;
8882                         }
8883
8884                         var block = ec.CurrentBlock;
8885                         if (block != null) {
8886                                 var top = block.ParametersBlock.TopBlock;
8887                                 if (top.ThisVariable != null)
8888                                         variable_info = top.ThisVariable.VariableInfo;
8889
8890                                 AnonymousExpression am = ec.CurrentAnonymousMethod;
8891                                 if (am != null && ec.IsVariableCapturingRequired && !block.Explicit.HasCapturedThis) {
8892                                         //
8893                                         // Hoisted this is almost like hoisted variable but not exactly. When
8894                                         // there is no variable hoisted we can simply emit an instance method
8895                                         // without lifting this into a storey. Unfotunatelly this complicates
8896                                         // things in other cases because we don't know where this will be hoisted
8897                                         // until top-level block is fully resolved
8898                                         //
8899                                         top.AddThisReferenceFromChildrenBlock (block.Explicit);
8900                                         am.SetHasThisAccess ();
8901                                 }
8902                         }
8903                 }
8904
8905                 protected override Expression DoResolve (ResolveContext ec)
8906                 {
8907                         ResolveBase (ec);
8908                         return this;
8909                 }
8910
8911                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
8912                 {
8913                         if (eclass == ExprClass.Unresolved)
8914                                 ResolveBase (ec);
8915
8916                         if (type.IsClass){
8917                                 if (right_side == EmptyExpression.UnaryAddress)
8918                                         ec.Report.Error (459, loc, "Cannot take the address of `this' because it is read-only");
8919                                 else if (right_side == EmptyExpression.OutAccess)
8920                                         ec.Report.Error (1605, loc, "Cannot pass `this' as a ref or out argument because it is read-only");
8921                                 else
8922                                         ec.Report.Error (1604, loc, "Cannot assign to `this' because it is read-only");
8923                         }
8924
8925                         return this;
8926                 }
8927
8928                 public override int GetHashCode()
8929                 {
8930                         throw new NotImplementedException ();
8931                 }
8932
8933                 public override bool Equals (object obj)
8934                 {
8935                         This t = obj as This;
8936                         if (t == null)
8937                                 return false;
8938
8939                         return true;
8940                 }
8941
8942                 protected override void CloneTo (CloneContext clonectx, Expression t)
8943                 {
8944                         // Nothing
8945                 }
8946
8947                 public override void SetHasAddressTaken ()
8948                 {
8949                         // Nothing
8950                 }
8951                 
8952                 public override object Accept (StructuralVisitor visitor)
8953                 {
8954                         return visitor.Visit (this);
8955                 }
8956         }
8957
8958         /// <summary>
8959         ///   Represents the `__arglist' construct
8960         /// </summary>
8961         public class ArglistAccess : Expression
8962         {
8963                 public ArglistAccess (Location loc)
8964                 {
8965                         this.loc = loc;
8966                 }
8967
8968                 protected override void CloneTo (CloneContext clonectx, Expression target)
8969                 {
8970                         // nothing.
8971                 }
8972
8973                 public override bool ContainsEmitWithAwait ()
8974                 {
8975                         return false;
8976                 }
8977
8978                 public override Expression CreateExpressionTree (ResolveContext ec)
8979                 {
8980                         throw new NotSupportedException ("ET");
8981                 }
8982
8983                 protected override Expression DoResolve (ResolveContext ec)
8984                 {
8985                         eclass = ExprClass.Variable;
8986                         type = ec.Module.PredefinedTypes.RuntimeArgumentHandle.Resolve ();
8987
8988                         if (ec.HasSet (ResolveContext.Options.FieldInitializerScope) || !ec.CurrentBlock.ParametersBlock.Parameters.HasArglist) {
8989                                 ec.Report.Error (190, loc,
8990                                         "The __arglist construct is valid only within a variable argument method");
8991                         }
8992
8993                         return this;
8994                 }
8995
8996                 public override void Emit (EmitContext ec)
8997                 {
8998                         ec.Emit (OpCodes.Arglist);
8999                 }
9000
9001                 public override object Accept (StructuralVisitor visitor)
9002                 {
9003                         return visitor.Visit (this);
9004                 }
9005         }
9006
9007         /// <summary>
9008         ///   Represents the `__arglist (....)' construct
9009         /// </summary>
9010         public class Arglist : Expression
9011         {
9012                 Arguments arguments;
9013
9014                 public Arglist (Location loc)
9015                         : this (null, loc)
9016                 {
9017                 }
9018
9019                 public Arglist (Arguments args, Location l)
9020                 {
9021                         arguments = args;
9022                         loc = l;
9023                 }
9024
9025                 public Arguments Arguments {
9026                         get {
9027                                 return arguments;
9028                         }
9029                 }
9030
9031                 public MetaType[] ArgumentTypes {
9032                     get {
9033                                 if (arguments == null)
9034                                         return MetaType.EmptyTypes;
9035
9036                                 var retval = new MetaType[arguments.Count];
9037                                 for (int i = 0; i < retval.Length; i++)
9038                                         retval[i] = arguments[i].Expr.Type.GetMetaInfo ();
9039
9040                         return retval;
9041                     }
9042                 }
9043
9044                 public override bool ContainsEmitWithAwait ()
9045                 {
9046                         throw new NotImplementedException ();
9047                 }
9048                 
9049                 public override Expression CreateExpressionTree (ResolveContext ec)
9050                 {
9051                         ec.Report.Error (1952, loc, "An expression tree cannot contain a method with variable arguments");
9052                         return null;
9053                 }
9054
9055                 protected override Expression DoResolve (ResolveContext ec)
9056                 {
9057                         eclass = ExprClass.Variable;
9058                         type = InternalType.Arglist;
9059                         if (arguments != null) {
9060                                 bool dynamic;   // Can be ignored as there is always only 1 overload
9061                                 arguments.Resolve (ec, out dynamic);
9062                         }
9063
9064                         return this;
9065                 }
9066
9067                 public override void Emit (EmitContext ec)
9068                 {
9069                         if (arguments != null)
9070                                 arguments.Emit (ec);
9071                 }
9072
9073                 protected override void CloneTo (CloneContext clonectx, Expression t)
9074                 {
9075                         Arglist target = (Arglist) t;
9076
9077                         if (arguments != null)
9078                                 target.arguments = arguments.Clone (clonectx);
9079                 }
9080
9081                 public override object Accept (StructuralVisitor visitor)
9082                 {
9083                         return visitor.Visit (this);
9084                 }
9085         }
9086
9087         public class RefValueExpr : ShimExpression, IAssignMethod, IMemoryLocation
9088         {
9089                 FullNamedExpression texpr;
9090
9091                 public RefValueExpr (Expression expr, FullNamedExpression texpr, Location loc)
9092                         : base (expr)
9093                 {
9094                         this.texpr = texpr;
9095                         this.loc = loc;
9096                 }
9097
9098                 public FullNamedExpression TypeExpression {
9099                         get {
9100                                 return texpr;
9101                         }
9102                 }
9103
9104                 public override bool ContainsEmitWithAwait ()
9105                 {
9106                         return false;
9107                 }
9108
9109                 public void AddressOf (EmitContext ec, AddressOp mode)
9110                 {
9111                         expr.Emit (ec);
9112                         ec.Emit (OpCodes.Refanyval, type);
9113                 }
9114
9115                 protected override Expression DoResolve (ResolveContext rc)
9116                 {
9117                         expr = expr.Resolve (rc);
9118                         type = texpr.ResolveAsType (rc);
9119                         if (expr == null || type == null)
9120                                 return null;
9121
9122                         expr = Convert.ImplicitConversionRequired (rc, expr, rc.Module.PredefinedTypes.TypedReference.Resolve (), loc);
9123                         eclass = ExprClass.Variable;
9124                         return this;
9125                 }
9126
9127                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
9128                 {
9129                         return DoResolve (rc);
9130                 }
9131
9132                 public override void Emit (EmitContext ec)
9133                 {
9134                         expr.Emit (ec);
9135                         ec.Emit (OpCodes.Refanyval, type);
9136                         ec.EmitLoadFromPtr (type);
9137                 }
9138
9139                 public void Emit (EmitContext ec, bool leave_copy)
9140                 {
9141                         throw new NotImplementedException ();
9142                 }
9143
9144                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
9145                 {
9146                         expr.Emit (ec);
9147                         ec.Emit (OpCodes.Refanyval, type);
9148                         source.Emit (ec);
9149
9150                         LocalTemporary temporary = null;
9151                         if (leave_copy) {
9152                                 ec.Emit (OpCodes.Dup);
9153                                 temporary = new LocalTemporary (source.Type);
9154                                 temporary.Store (ec);
9155                         }
9156
9157                         ec.EmitStoreFromPtr (type);
9158
9159                         if (temporary != null) {
9160                                 temporary.Emit (ec);
9161                                 temporary.Release (ec);
9162                         }
9163                 }
9164
9165                 public override object Accept (StructuralVisitor visitor)
9166                 {
9167                         return visitor.Visit (this);
9168                 }
9169         }
9170
9171         public class RefTypeExpr : ShimExpression
9172         {
9173                 public RefTypeExpr (Expression expr, Location loc)
9174                         : base (expr)
9175                 {
9176                         this.loc = loc;
9177                 }
9178
9179                 protected override Expression DoResolve (ResolveContext rc)
9180                 {
9181                         expr = expr.Resolve (rc);
9182                         if (expr == null)
9183                                 return null;
9184
9185                         expr = Convert.ImplicitConversionRequired (rc, expr, rc.Module.PredefinedTypes.TypedReference.Resolve (), loc);
9186                         if (expr == null)
9187                                 return null;
9188
9189                         type = rc.BuiltinTypes.Type;
9190                         eclass = ExprClass.Value;
9191                         return this;
9192                 }
9193
9194                 public override void Emit (EmitContext ec)
9195                 {
9196                         expr.Emit (ec);
9197                         ec.Emit (OpCodes.Refanytype);
9198                         var m = ec.Module.PredefinedMembers.TypeGetTypeFromHandle.Resolve (loc);
9199                         if (m != null)
9200                                 ec.Emit (OpCodes.Call, m);
9201                 }
9202                 
9203                 public override object Accept (StructuralVisitor visitor)
9204                 {
9205                         return visitor.Visit (this);
9206                 }
9207         }
9208
9209         public class MakeRefExpr : ShimExpression
9210         {
9211                 public MakeRefExpr (Expression expr, Location loc)
9212                         : base (expr)
9213                 {
9214                         this.loc = loc;
9215                 }
9216
9217                 public override bool ContainsEmitWithAwait ()
9218                 {
9219                         throw new NotImplementedException ();
9220                 }
9221
9222                 protected override Expression DoResolve (ResolveContext rc)
9223                 {
9224                         expr = expr.ResolveLValue (rc, EmptyExpression.LValueMemberAccess);
9225                         type = rc.Module.PredefinedTypes.TypedReference.Resolve ();
9226                         eclass = ExprClass.Value;
9227                         return this;
9228                 }
9229
9230                 public override void Emit (EmitContext ec)
9231                 {
9232                         ((IMemoryLocation) expr).AddressOf (ec, AddressOp.Load);
9233                         ec.Emit (OpCodes.Mkrefany, expr.Type);
9234                 }
9235                 
9236                 public override object Accept (StructuralVisitor visitor)
9237                 {
9238                         return visitor.Visit (this);
9239                 }
9240         }
9241
9242         /// <summary>
9243         ///   Implements the typeof operator
9244         /// </summary>
9245         public class TypeOf : Expression {
9246                 FullNamedExpression QueriedType;
9247                 TypeSpec typearg;
9248
9249                 public TypeOf (FullNamedExpression queried_type, Location l)
9250                 {
9251                         QueriedType = queried_type;
9252                         loc = l;
9253                 }
9254
9255                 //
9256                 // Use this constructor for any compiler generated typeof expression
9257                 //
9258                 public TypeOf (TypeSpec type, Location loc)
9259                 {
9260                         this.typearg = type;
9261                         this.loc = loc;
9262                 }
9263
9264                 #region Properties
9265
9266                 public override bool IsSideEffectFree {
9267                         get {
9268                                 return true;
9269                         }
9270                 }
9271
9272                 public TypeSpec TypeArgument {
9273                         get {
9274                                 return typearg;
9275                         }
9276                 }
9277
9278                 public FullNamedExpression TypeExpression {
9279                         get {
9280                                 return QueriedType;
9281                         }
9282                 }
9283
9284                 #endregion
9285
9286
9287                 protected override void CloneTo (CloneContext clonectx, Expression t)
9288                 {
9289                         TypeOf target = (TypeOf) t;
9290                         if (QueriedType != null)
9291                                 target.QueriedType = (FullNamedExpression) QueriedType.Clone (clonectx);
9292                 }
9293
9294                 public override bool ContainsEmitWithAwait ()
9295                 {
9296                         return false;
9297                 }
9298
9299                 public override Expression CreateExpressionTree (ResolveContext ec)
9300                 {
9301                         Arguments args = new Arguments (2);
9302                         args.Add (new Argument (this));
9303                         args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
9304                         return CreateExpressionFactoryCall (ec, "Constant", args);
9305                 }
9306
9307                 protected override Expression DoResolve (ResolveContext ec)
9308                 {
9309                         if (eclass != ExprClass.Unresolved)
9310                                 return this;
9311
9312                         if (typearg == null) {
9313                                 //
9314                                 // Pointer types are allowed without explicit unsafe, they are just tokens
9315                                 //
9316                                 using (ec.Set (ResolveContext.Options.UnsafeScope)) {
9317                                         typearg = QueriedType.ResolveAsType (ec, true);
9318                                 }
9319
9320                                 if (typearg == null)
9321                                         return null;
9322
9323                                 if (typearg.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
9324                                         ec.Report.Error (1962, QueriedType.Location,
9325                                                 "The typeof operator cannot be used on the dynamic type");
9326                                 }
9327                         }
9328
9329                         type = ec.BuiltinTypes.Type;
9330
9331                         // Even though what is returned is a type object, it's treated as a value by the compiler.
9332                         // In particular, 'typeof (Foo).X' is something totally different from 'Foo.X'.
9333                         eclass = ExprClass.Value;
9334                         return this;
9335                 }
9336
9337                 static bool ContainsDynamicType (TypeSpec type)
9338                 {
9339                         if (type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
9340                                 return true;
9341
9342                         var element_container = type as ElementTypeSpec;
9343                         if (element_container != null)
9344                                 return ContainsDynamicType (element_container.Element);
9345
9346                         foreach (var t in type.TypeArguments) {
9347                                 if (ContainsDynamicType (t)) {
9348                                         return true;
9349                                 }
9350                         }
9351
9352                         return false;
9353                 }
9354
9355                 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
9356                 {
9357                         // Target type is not System.Type therefore must be object
9358                         // and we need to use different encoding sequence
9359                         if (targetType != type)
9360                                 enc.Encode (type);
9361
9362                         if (typearg is InflatedTypeSpec) {
9363                                 var gt = typearg;
9364                                 do {
9365                                         if (InflatedTypeSpec.ContainsTypeParameter (gt)) {
9366                                                 rc.Module.Compiler.Report.Error (416, loc, "`{0}': an attribute argument cannot use type parameters",
9367                                                         typearg.GetSignatureForError ());
9368                                                 return;
9369                                         }
9370
9371                                         gt = gt.DeclaringType;
9372                                 } while (gt != null);
9373                         }
9374
9375                         if (ContainsDynamicType (typearg)) {
9376                                 Attribute.Error_AttributeArgumentIsDynamic (rc, loc);
9377                                 return;
9378                         }
9379
9380                         enc.EncodeTypeName (typearg);
9381                 }
9382
9383                 public override void Emit (EmitContext ec)
9384                 {
9385                         ec.Emit (OpCodes.Ldtoken, typearg);
9386                         var m = ec.Module.PredefinedMembers.TypeGetTypeFromHandle.Resolve (loc);
9387                         if (m != null)
9388                                 ec.Emit (OpCodes.Call, m);
9389                 }
9390                 
9391                 public override object Accept (StructuralVisitor visitor)
9392                 {
9393                         return visitor.Visit (this);
9394                 }
9395         }
9396
9397         sealed class TypeOfMethod : TypeOfMember<MethodSpec>
9398         {
9399                 public TypeOfMethod (MethodSpec method, Location loc)
9400                         : base (method, loc)
9401                 {
9402                 }
9403
9404                 protected override Expression DoResolve (ResolveContext ec)
9405                 {
9406                         if (member.IsConstructor) {
9407                                 type = ec.Module.PredefinedTypes.ConstructorInfo.Resolve ();
9408                         } else {
9409                                 type = ec.Module.PredefinedTypes.MethodInfo.Resolve ();
9410                         }
9411
9412                         if (type == null)
9413                                 return null;
9414
9415                         return base.DoResolve (ec);
9416                 }
9417
9418                 public override void Emit (EmitContext ec)
9419                 {
9420                         ec.Emit (OpCodes.Ldtoken, member);
9421
9422                         base.Emit (ec);
9423                         ec.Emit (OpCodes.Castclass, type);
9424                 }
9425
9426                 protected override PredefinedMember<MethodSpec> GetTypeFromHandle (EmitContext ec)
9427                 {
9428                         return ec.Module.PredefinedMembers.MethodInfoGetMethodFromHandle;
9429                 }
9430
9431                 protected override PredefinedMember<MethodSpec> GetTypeFromHandleGeneric (EmitContext ec)
9432                 {
9433                         return ec.Module.PredefinedMembers.MethodInfoGetMethodFromHandle2;
9434                 }
9435         }
9436
9437         abstract class TypeOfMember<T> : Expression where T : MemberSpec
9438         {
9439                 protected readonly T member;
9440
9441                 protected TypeOfMember (T member, Location loc)
9442                 {
9443                         this.member = member;
9444                         this.loc = loc;
9445                 }
9446
9447                 public override bool IsSideEffectFree {
9448                         get {
9449                                 return true;
9450                         }
9451                 }
9452
9453                 public override bool ContainsEmitWithAwait ()
9454                 {
9455                         return false;
9456                 }
9457
9458                 public override Expression CreateExpressionTree (ResolveContext ec)
9459                 {
9460                         Arguments args = new Arguments (2);
9461                         args.Add (new Argument (this));
9462                         args.Add (new Argument (new TypeOf (type, loc)));
9463                         return CreateExpressionFactoryCall (ec, "Constant", args);
9464                 }
9465
9466                 protected override Expression DoResolve (ResolveContext ec)
9467                 {
9468                         eclass = ExprClass.Value;
9469                         return this;
9470                 }
9471
9472                 public override void Emit (EmitContext ec)
9473                 {
9474                         bool is_generic = member.DeclaringType.IsGenericOrParentIsGeneric;
9475                         PredefinedMember<MethodSpec> p;
9476                         if (is_generic) {
9477                                 p = GetTypeFromHandleGeneric (ec);
9478                                 ec.Emit (OpCodes.Ldtoken, member.DeclaringType);
9479                         } else {
9480                                 p = GetTypeFromHandle (ec);
9481                         }
9482
9483                         var mi = p.Resolve (loc);
9484                         if (mi != null)
9485                                 ec.Emit (OpCodes.Call, mi);
9486                 }
9487
9488                 protected abstract PredefinedMember<MethodSpec> GetTypeFromHandle (EmitContext ec);
9489                 protected abstract PredefinedMember<MethodSpec> GetTypeFromHandleGeneric (EmitContext ec);
9490         }
9491
9492         sealed class TypeOfField : TypeOfMember<FieldSpec>
9493         {
9494                 public TypeOfField (FieldSpec field, Location loc)
9495                         : base (field, loc)
9496                 {
9497                 }
9498
9499                 protected override Expression DoResolve (ResolveContext ec)
9500                 {
9501                         type = ec.Module.PredefinedTypes.FieldInfo.Resolve ();
9502                         if (type == null)
9503                                 return null;
9504
9505                         return base.DoResolve (ec);
9506                 }
9507
9508                 public override void Emit (EmitContext ec)
9509                 {
9510                         ec.Emit (OpCodes.Ldtoken, member);
9511                         base.Emit (ec);
9512                 }
9513
9514                 protected override PredefinedMember<MethodSpec> GetTypeFromHandle (EmitContext ec)
9515                 {
9516                         return ec.Module.PredefinedMembers.FieldInfoGetFieldFromHandle;
9517                 }
9518
9519                 protected override PredefinedMember<MethodSpec> GetTypeFromHandleGeneric (EmitContext ec)
9520                 {
9521                         return ec.Module.PredefinedMembers.FieldInfoGetFieldFromHandle2;
9522                 }
9523         }
9524
9525         /// <summary>
9526         ///   Implements the sizeof expression
9527         /// </summary>
9528         public class SizeOf : Expression {
9529                 readonly Expression texpr;
9530                 TypeSpec type_queried;
9531                 
9532                 public SizeOf (Expression queried_type, Location l)
9533                 {
9534                         this.texpr = queried_type;
9535                         loc = l;
9536                 }
9537
9538                 public override bool IsSideEffectFree {
9539                         get {
9540                                 return true;
9541                         }
9542                 }
9543
9544                 public Expression TypeExpression {
9545                         get {
9546                                 return texpr;
9547                         }
9548                 }
9549
9550                 public override bool ContainsEmitWithAwait ()
9551                 {
9552                         return false;
9553                 }
9554
9555                 public override Expression CreateExpressionTree (ResolveContext ec)
9556                 {
9557                         Error_PointerInsideExpressionTree (ec);
9558                         return null;
9559                 }
9560
9561                 protected override Expression DoResolve (ResolveContext ec)
9562                 {
9563                         type_queried = texpr.ResolveAsType (ec);
9564                         if (type_queried == null)
9565                                 return null;
9566
9567                         if (type_queried.IsEnum)
9568                                 type_queried = EnumSpec.GetUnderlyingType (type_queried);
9569
9570                         int size_of = BuiltinTypeSpec.GetSize (type_queried);
9571                         if (size_of > 0) {
9572                                 return new IntConstant (ec.BuiltinTypes, size_of, loc);
9573                         }
9574
9575                         if (!TypeManager.VerifyUnmanaged (ec.Module, type_queried, loc)){
9576                                 return null;
9577                         }
9578
9579                         if (!ec.IsUnsafe) {
9580                                 ec.Report.Error (233, loc,
9581                                         "`{0}' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)",
9582                                         type_queried.GetSignatureForError ());
9583                         }
9584                         
9585                         type = ec.BuiltinTypes.Int;
9586                         eclass = ExprClass.Value;
9587                         return this;
9588                 }
9589
9590                 public override void Emit (EmitContext ec)
9591                 {
9592                         ec.Emit (OpCodes.Sizeof, type_queried);
9593                 }
9594
9595                 protected override void CloneTo (CloneContext clonectx, Expression t)
9596                 {
9597                 }
9598                 
9599                 public override object Accept (StructuralVisitor visitor)
9600                 {
9601                         return visitor.Visit (this);
9602                 }
9603         }
9604
9605         /// <summary>
9606         ///   Implements the qualified-alias-member (::) expression.
9607         /// </summary>
9608         public class QualifiedAliasMember : MemberAccess
9609         {
9610                 readonly string alias;
9611                 public static readonly string GlobalAlias = "global";
9612
9613                 public QualifiedAliasMember (string alias, string identifier, Location l)
9614                         : base (null, identifier, l)
9615                 {
9616                         this.alias = alias;
9617                 }
9618
9619                 public QualifiedAliasMember (string alias, string identifier, TypeArguments targs, Location l)
9620                         : base (null, identifier, targs, l)
9621                 {
9622                         this.alias = alias;
9623                 }
9624
9625                 public QualifiedAliasMember (string alias, string identifier, int arity, Location l)
9626                         : base (null, identifier, arity, l)
9627                 {
9628                         this.alias = alias;
9629                 }
9630
9631                 public string Alias {
9632                         get {
9633                                 return alias;
9634                         }
9635                 }
9636
9637                 public FullNamedExpression CreateExpressionFromAlias (IMemberContext mc)
9638                 {
9639                         if (alias == GlobalAlias)
9640                                 return new NamespaceExpression (mc.Module.GlobalRootNamespace, loc);
9641
9642                         int errors = mc.Module.Compiler.Report.Errors;
9643                         var expr = mc.LookupNamespaceAlias (alias);
9644                         if (expr == null) {
9645                                 if (errors == mc.Module.Compiler.Report.Errors)
9646                                         mc.Module.Compiler.Report.Error (432, loc, "Alias `{0}' not found", alias);
9647
9648                                 return null;
9649                         }
9650
9651                         return expr;
9652                 }
9653
9654                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
9655                 {
9656                         expr = CreateExpressionFromAlias (mc);
9657                         if (expr == null)
9658                                 return null;
9659
9660                         return base.ResolveAsTypeOrNamespace (mc, allowUnboundTypeArguments);
9661                 }
9662
9663                 protected override Expression DoResolve (ResolveContext rc)
9664                 {
9665                         return ResolveAsTypeOrNamespace (rc, false);
9666                 }
9667
9668                 public override string GetSignatureForError ()
9669                 {
9670                         string name = Name;
9671                         if (targs != null) {
9672                                 name = Name + "<" + targs.GetSignatureForError () + ">";
9673                         }
9674
9675                         return alias + "::" + name;
9676                 }
9677
9678                 public override bool HasConditionalAccess ()
9679                 {
9680                         return false;
9681                 }
9682
9683                 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
9684                 {
9685                         if ((restrictions & MemberLookupRestrictions.InvocableOnly) != 0) {
9686                                 rc.Module.Compiler.Report.Error (687, loc,
9687                                         "The namespace alias qualifier `::' cannot be used to invoke a method. Consider using `.' instead",
9688                                         GetSignatureForError ());
9689
9690                                 return null;
9691                         }
9692
9693                         return DoResolve (rc);
9694                 }
9695
9696                 protected override void CloneTo (CloneContext clonectx, Expression t)
9697                 {
9698                         // Nothing 
9699                 }
9700                 
9701                 public override object Accept (StructuralVisitor visitor)
9702                 {
9703                         return visitor.Visit (this);
9704                 }
9705         }
9706
9707         /// <summary>
9708         ///   Implements the member access expression
9709         /// </summary>
9710         public class MemberAccess : ATypeNameExpression
9711         {
9712                 protected Expression expr;
9713
9714                 public MemberAccess (Expression expr, string id)
9715                         : base (id, expr.Location)
9716                 {
9717                         this.expr = expr;
9718                 }
9719
9720                 public MemberAccess (Expression expr, string identifier, Location loc)
9721                         : base (identifier, loc)
9722                 {
9723                         this.expr = expr;
9724                 }
9725
9726                 public MemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
9727                         : base (identifier, args, loc)
9728                 {
9729                         this.expr = expr;
9730                 }
9731
9732                 public MemberAccess (Expression expr, string identifier, int arity, Location loc)
9733                         : base (identifier, arity, loc)
9734                 {
9735                         this.expr = expr;
9736                 }
9737
9738                 public Expression LeftExpression {
9739                         get {
9740                                 return expr;
9741                         }
9742                 }
9743
9744                 public override Location StartLocation {
9745                         get {
9746                                 return expr == null ? loc : expr.StartLocation;
9747                         }
9748                 }
9749
9750                 protected override Expression DoResolve (ResolveContext rc)
9751                 {
9752                         var e = LookupNameExpression (rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.DontSetConditionalAccess);
9753                         if (e != null)
9754                                 e = e.Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.Type | ResolveFlags.MethodGroup);
9755
9756                         return e;
9757                 }
9758
9759                 public override Expression DoResolveLValue (ResolveContext rc, Expression rhs)
9760                 {
9761                         var e = LookupNameExpression (rc, MemberLookupRestrictions.None);
9762
9763                         if (e is TypeExpr) {
9764                                 e.Error_UnexpectedKind (rc, ResolveFlags.VariableOrValue, loc);
9765                                 return null;
9766                         }
9767
9768                         if (e != null)
9769                                 e = e.ResolveLValue (rc, rhs);
9770
9771                         return e;
9772                 }
9773
9774                 protected virtual void Error_OperatorCannotBeApplied (ResolveContext rc, TypeSpec type)
9775                 {
9776                         if (type == InternalType.NullLiteral && rc.IsRuntimeBinder)
9777                                 rc.Report.Error (Report.RuntimeErrorId, loc, "Cannot perform member binding on `null' value");
9778                         else
9779                                 expr.Error_OperatorCannotBeApplied (rc, loc, ".", type);
9780                 }
9781
9782                 public override bool HasConditionalAccess ()
9783                 {
9784                         return LeftExpression.HasConditionalAccess ();
9785                 }
9786
9787                 public static bool IsValidDotExpression (TypeSpec type)
9788                 {
9789                         const MemberKind dot_kinds = MemberKind.Class | MemberKind.Struct | MemberKind.Delegate | MemberKind.Enum |
9790                                 MemberKind.Interface | MemberKind.TypeParameter | MemberKind.ArrayType;
9791
9792                         return (type.Kind & dot_kinds) != 0 || type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
9793                 }
9794
9795                 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
9796                 {
9797                         var sn = expr as SimpleName;
9798                         const ResolveFlags flags = ResolveFlags.VariableOrValue | ResolveFlags.Type;
9799
9800                         if (sn != null) {
9801                                 expr = sn.LookupNameExpression (rc, MemberLookupRestrictions.ReadAccess | MemberLookupRestrictions.ExactArity);
9802
9803                                 //
9804                                 // Resolve expression which does have type set as we need expression type
9805                                 // with disable flow analysis as we don't know whether left side expression
9806                                 // is used as variable or type
9807                                 //
9808                                 if (expr is VariableReference || expr is ConstantExpr || expr is Linq.TransparentMemberAccess || expr is EventExpr) {
9809                                         expr = expr.Resolve (rc);
9810                                 } else if (expr is TypeParameterExpr) {
9811                                         expr.Error_UnexpectedKind (rc, flags, sn.Location);
9812                                         expr = null;
9813                                 }
9814                         } else {
9815                                 if ((restrictions & MemberLookupRestrictions.DontSetConditionalAccess) != 0) {
9816                                         using (rc.Set (ResolveContext.Options.DontSetConditionalAccessReceiver)) {
9817                                                 expr = expr.Resolve (rc, flags);
9818                                         }
9819                                 } else {
9820                                         expr = expr.Resolve (rc, flags);
9821                                 }
9822                         }
9823
9824                         if (expr == null)
9825                                 return null;
9826
9827                         var ns = expr as NamespaceExpression;
9828                         if (ns != null) {
9829                                 var retval = ns.LookupTypeOrNamespace (rc, Name, Arity, LookupMode.Normal, loc);
9830
9831                                 if (retval == null) {
9832                                         ns.Error_NamespaceDoesNotExist (rc, Name, Arity, loc);
9833                                         return null;
9834                                 }
9835
9836                                 if (Arity > 0) {
9837                                         if (HasTypeArguments)
9838                                                 return new GenericTypeExpr (retval.Type, targs, loc);
9839
9840                                         targs.Resolve (rc, false);
9841                                 }
9842
9843                                 return retval;
9844                         }
9845
9846                         var cma = this as ConditionalMemberAccess;
9847
9848                         MemberExpr me;
9849                         TypeSpec expr_type = expr.Type;
9850                         if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
9851                                 me = expr as MemberExpr;
9852                                 if (me != null)
9853                                         me.ResolveInstanceExpression (rc, null);
9854
9855                                 Arguments args = new Arguments (1);
9856                                 args.Add (new Argument (expr));
9857
9858                                 if (cma != null)
9859                                         return new DynamicConditionalMemberBinder (Name, args, loc);
9860
9861                                 return new DynamicMemberBinder (Name, args, loc);
9862                         }
9863
9864                         if (cma != null) {
9865                                 if (!IsNullPropagatingValid (expr.Type)) {
9866                                         expr.Error_OperatorCannotBeApplied (rc, loc, "?", expr.Type);
9867                                         return null;
9868                                 }
9869
9870                                 if (expr_type.IsNullableType) {
9871                                         expr = Nullable.Unwrap.Create (expr.Resolve (rc), true);
9872                                         expr_type = expr.Type;
9873                                 }
9874                         }
9875
9876                         if (!IsValidDotExpression (expr_type)) {
9877                                 Error_OperatorCannotBeApplied (rc, expr_type);
9878                                 return null;
9879                         }
9880
9881                         var lookup_arity = Arity;
9882                         bool errorMode = false;
9883                         Expression member_lookup;
9884                         while (true) {
9885                                 member_lookup = MemberLookup (rc, errorMode, expr_type, Name, lookup_arity, restrictions, loc);
9886                                 if (member_lookup == null) {
9887                                         //
9888                                         // Try to look for extension method when member lookup failed
9889                                         //
9890                                         if (MethodGroupExpr.IsExtensionMethodArgument (expr)) {
9891                                                 var methods = rc.LookupExtensionMethod (Name, lookup_arity);
9892                                                 if (methods != null) {
9893                                                         var emg = new ExtensionMethodGroupExpr (methods, expr, loc);
9894                                                         if (HasTypeArguments) {
9895                                                                 if (!targs.Resolve (rc, false))
9896                                                                         return null;
9897
9898                                                                 emg.SetTypeArguments (rc, targs);
9899                                                         }
9900
9901                                                         if (cma != null)
9902                                                                 emg.ConditionalAccess = true;
9903
9904                                                         // TODO: it should really skip the checks bellow
9905                                                         return emg.Resolve (rc);
9906                                                 }
9907                                         }
9908                                 }
9909
9910                                 if (errorMode) {
9911                                         if (member_lookup == null) {
9912                                                 var dep = expr_type.GetMissingDependencies ();
9913                                                 if (dep != null) {
9914                                                         ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
9915                                                 } else if (expr is TypeExpr) {
9916                                                         base.Error_TypeDoesNotContainDefinition (rc, expr_type, Name);
9917                                                 } else {
9918                                                         Error_TypeDoesNotContainDefinition (rc, expr_type, Name);
9919                                                 }
9920
9921                                                 return null;
9922                                         }
9923
9924                                         if (member_lookup is MethodGroupExpr || member_lookup is PropertyExpr) {
9925                                                 // Leave it to overload resolution to report correct error
9926                                         } else if (!(member_lookup is TypeExpr)) {
9927                                                 // TODO: rc.SymbolRelatedToPreviousError
9928                                                 ErrorIsInaccesible (rc, member_lookup.GetSignatureForError (), loc);
9929                                         }
9930                                         break;
9931                                 }
9932
9933                                 if (member_lookup != null)
9934                                         break;
9935
9936                                 lookup_arity = 0;
9937                                 restrictions &= ~MemberLookupRestrictions.InvocableOnly;
9938                                 errorMode = true;
9939                         }
9940
9941                         TypeExpr texpr = member_lookup as TypeExpr;
9942                         if (texpr != null) {
9943                                 if (!(expr is TypeExpr) && (sn == null || expr.ProbeIdenticalTypeName (rc, expr, sn) == expr)) {
9944                                         rc.Report.Error (572, loc, "`{0}': cannot reference a type through an expression. Consider using `{1}' instead",
9945                                                 Name, texpr.GetSignatureForError ());
9946                                 }
9947
9948                                 if (!texpr.Type.IsAccessible (rc)) {
9949                                         rc.Report.SymbolRelatedToPreviousError (member_lookup.Type);
9950                                         ErrorIsInaccesible (rc, member_lookup.Type.GetSignatureForError (), loc);
9951                                         return null;
9952                                 }
9953
9954                                 if (HasTypeArguments) {
9955                                         return new GenericTypeExpr (member_lookup.Type, targs, loc);
9956                                 }
9957
9958                                 return member_lookup;
9959                         }
9960
9961                         me = member_lookup as MemberExpr;
9962
9963                         if (sn != null && me.IsStatic && (expr = me.ProbeIdenticalTypeName (rc, expr, sn)) != expr) {
9964                                 sn = null;
9965                         }
9966
9967                         if (cma != null) {
9968                                 me.ConditionalAccess = true;
9969                         }
9970
9971                         me = me.ResolveMemberAccess (rc, expr, sn);
9972
9973                         if (Arity > 0) {
9974                                 if (!targs.Resolve (rc, false))
9975                                         return null;
9976
9977                                 me.SetTypeArguments (rc, targs);
9978                         }
9979
9980                         return me;
9981                 }
9982
9983                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext rc, bool allowUnboundTypeArguments)
9984                 {
9985                         FullNamedExpression fexpr = expr as FullNamedExpression;
9986                         if (fexpr == null) {
9987                                 expr.ResolveAsType (rc);
9988                                 return null;
9989                         }
9990
9991                         FullNamedExpression expr_resolved = fexpr.ResolveAsTypeOrNamespace (rc, allowUnboundTypeArguments);
9992
9993                         if (expr_resolved == null)
9994                                 return null;
9995
9996                         var ns = expr_resolved as NamespaceExpression;
9997                         if (ns != null) {
9998                                 FullNamedExpression retval = ns.LookupTypeOrNamespace (rc, Name, Arity, LookupMode.Normal, loc);
9999
10000                                 if (retval == null) {
10001                                         ns.Error_NamespaceDoesNotExist (rc, Name, Arity, loc);
10002                                 } else if (Arity > 0) {
10003                                         if (HasTypeArguments) {
10004                                                 retval = new GenericTypeExpr (retval.Type, targs, loc);
10005                                                 if (retval.ResolveAsType (rc) == null)
10006                                                         return null;
10007                                         } else {
10008                                                 targs.Resolve (rc, allowUnboundTypeArguments);
10009
10010                                                 retval = new GenericOpenTypeExpr (retval.Type, loc);
10011                                         }
10012                                 }
10013
10014                                 return retval;
10015                         }
10016
10017                         var tnew_expr = expr_resolved.ResolveAsType (rc);
10018                         if (tnew_expr == null)
10019                                 return null;
10020
10021                         TypeSpec expr_type = tnew_expr;
10022                         if (TypeManager.IsGenericParameter (expr_type)) {
10023                                 rc.Module.Compiler.Report.Error (704, loc, "A nested type cannot be specified through a type parameter `{0}'",
10024                                         tnew_expr.GetSignatureForError ());
10025                                 return null;
10026                         }
10027
10028                         var qam = this as QualifiedAliasMember;
10029                         if (qam != null) {
10030                                 rc.Module.Compiler.Report.Error (431, loc,
10031                                         "Alias `{0}' cannot be used with `::' since it denotes a type. Consider replacing `::' with `.'",
10032                                         qam.Alias);
10033
10034                         }
10035
10036                         TypeSpec nested = null;
10037                         while (expr_type != null) {
10038                                 nested = MemberCache.FindNestedType (expr_type, Name, Arity, false);
10039                                 if (nested == null) {
10040                                         if (expr_type == tnew_expr) {
10041                                                 Error_IdentifierNotFound (rc, expr_type);
10042                                                 return null;
10043                                         }
10044
10045                                         expr_type = tnew_expr;
10046                                         nested = MemberCache.FindNestedType (expr_type, Name, Arity, false);
10047                                         ErrorIsInaccesible (rc, nested.GetSignatureForError (), loc);
10048                                         break;
10049                                 }
10050
10051                                 if (nested.IsAccessible (rc))
10052                                         break;
10053
10054                                 //
10055                                 // Keep looking after inaccessible candidate but only if
10056                                 // we are not in same context as the definition itself
10057                                 //
10058                                 if (expr_type.MemberDefinition == rc.CurrentMemberDefinition)
10059                                         break;
10060
10061                                 expr_type = expr_type.BaseType;
10062                         }
10063                         
10064                         TypeExpr texpr;
10065                         if (Arity > 0) {
10066                                 if (HasTypeArguments) {
10067                                         texpr = new GenericTypeExpr (nested, targs, loc);
10068                                 } else {
10069                                         targs.Resolve (rc, allowUnboundTypeArguments && !(expr_resolved is GenericTypeExpr));
10070
10071                                         texpr = new GenericOpenTypeExpr (nested, loc);
10072                                 }
10073                         } else if (expr_resolved is GenericOpenTypeExpr) {
10074                                 texpr = new GenericOpenTypeExpr (nested, loc);
10075                         } else {
10076                                 texpr = new TypeExpression (nested, loc);
10077                         }
10078
10079                         if (texpr.ResolveAsType (rc) == null)
10080                                 return null;
10081
10082                         return texpr;
10083                 }
10084
10085                 public void Error_IdentifierNotFound (IMemberContext rc, TypeSpec expr_type)
10086                 {
10087                         var nested = MemberCache.FindNestedType (expr_type, Name, -System.Math.Max (1, Arity), false);
10088
10089                         if (nested != null) {
10090                                 Error_TypeArgumentsCannotBeUsed (rc, nested, expr.Location);
10091                                 return;
10092                         }
10093
10094                         var any_other_member = MemberLookup (rc, false, expr_type, Name, 0, MemberLookupRestrictions.None, loc);
10095                         if (any_other_member != null) {
10096                                 Error_UnexpectedKind (rc, any_other_member, "type", any_other_member.ExprClassName, loc);
10097                                 return;
10098                         }
10099
10100                         rc.Module.Compiler.Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
10101                                 Name, expr_type.GetSignatureForError ());
10102                 }
10103
10104                 protected override void Error_InvalidExpressionStatement (Report report, Location loc)
10105                 {
10106                         base.Error_InvalidExpressionStatement (report, LeftExpression.Location);
10107                 }
10108
10109                 public override void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
10110                 {
10111                         if (ec.Module.Compiler.Settings.Version > LanguageVersion.ISO_2 && !ec.IsRuntimeBinder && MethodGroupExpr.IsExtensionMethodArgument (expr)) {
10112                                 ec.Report.SymbolRelatedToPreviousError (type);
10113
10114                                 var cand = ec.Module.GlobalRootNamespace.FindExtensionMethodNamespaces (ec, name, Arity);
10115                                 string missing;
10116                                 // a using directive or an assembly reference
10117                                 if (cand != null) {
10118                                         missing = "`" + string.Join ("' or `", cand.ToArray ()) + "' using directive";
10119                                 } else {
10120                                         missing = "an assembly reference";
10121                                 }
10122
10123                                 ec.Report.Error (1061, loc,
10124                                         "Type `{0}' does not contain a definition for `{1}' and no extension method `{1}' of type `{0}' could be found. Are you missing {2}?",
10125                                         type.GetSignatureForError (), name, missing);
10126                                 return;
10127                         }
10128
10129                         base.Error_TypeDoesNotContainDefinition (ec, type, name);
10130                 }
10131
10132                 public override string GetSignatureForError ()
10133                 {
10134                         return expr.GetSignatureForError () + "." + base.GetSignatureForError ();
10135                 }
10136
10137                 protected override void CloneTo (CloneContext clonectx, Expression t)
10138                 {
10139                         MemberAccess target = (MemberAccess) t;
10140
10141                         target.expr = expr.Clone (clonectx);
10142                 }
10143                 
10144                 public override object Accept (StructuralVisitor visitor)
10145                 {
10146                         return visitor.Visit (this);
10147                 }
10148         }
10149
10150         public class ConditionalMemberAccess : MemberAccess
10151         {
10152                 public ConditionalMemberAccess (Expression expr, string identifier, TypeArguments args, Location loc)
10153                         : base (expr, identifier, args, loc)
10154                 {
10155                 }
10156
10157                 public override bool HasConditionalAccess ()
10158                 {
10159                         return true;
10160                 }
10161         }
10162
10163         /// <summary>
10164         ///   Implements checked expressions
10165         /// </summary>
10166         public class CheckedExpr : Expression {
10167
10168                 public Expression Expr;
10169
10170                 public CheckedExpr (Expression e, Location l)
10171                 {
10172                         Expr = e;
10173                         loc = l;
10174                 }
10175
10176                 public override bool ContainsEmitWithAwait ()
10177                 {
10178                         return Expr.ContainsEmitWithAwait ();
10179                 }
10180                 
10181                 public override Expression CreateExpressionTree (ResolveContext ec)
10182                 {
10183                         using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
10184                                 return Expr.CreateExpressionTree (ec);
10185                 }
10186
10187                 protected override Expression DoResolve (ResolveContext ec)
10188                 {
10189                         using (ec.With (ResolveContext.Options.AllCheckStateFlags, true))
10190                                 Expr = Expr.Resolve (ec);
10191                         
10192                         if (Expr == null)
10193                                 return null;
10194
10195                         if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
10196                                 return Expr;
10197                         
10198                         eclass = Expr.eclass;
10199                         type = Expr.Type;
10200                         return this;
10201                 }
10202
10203                 public override void Emit (EmitContext ec)
10204                 {
10205                         using (ec.With (EmitContext.Options.CheckedScope, true))
10206                                 Expr.Emit (ec);
10207                 }
10208
10209                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
10210                 {
10211                         using (ec.With (EmitContext.Options.CheckedScope, true))
10212                                 Expr.EmitBranchable (ec, target, on_true);
10213                 }
10214
10215                 public override void FlowAnalysis (FlowAnalysisContext fc)
10216                 {
10217                         Expr.FlowAnalysis (fc);
10218                 }
10219
10220                 public override SLE.Expression MakeExpression (BuilderContext ctx)
10221                 {
10222                         using (ctx.With (BuilderContext.Options.CheckedScope, true)) {
10223                                 return Expr.MakeExpression (ctx);
10224                         }
10225                 }
10226
10227                 protected override void CloneTo (CloneContext clonectx, Expression t)
10228                 {
10229                         CheckedExpr target = (CheckedExpr) t;
10230
10231                         target.Expr = Expr.Clone (clonectx);
10232                 }
10233
10234                 public override object Accept (StructuralVisitor visitor)
10235                 {
10236                         return visitor.Visit (this);
10237                 }
10238         }
10239
10240         /// <summary>
10241         ///   Implements the unchecked expression
10242         /// </summary>
10243         public class UnCheckedExpr : Expression {
10244
10245                 public Expression Expr;
10246
10247                 public UnCheckedExpr (Expression e, Location l)
10248                 {
10249                         Expr = e;
10250                         loc = l;
10251                 }
10252
10253                 public override bool ContainsEmitWithAwait ()
10254                 {
10255                         return Expr.ContainsEmitWithAwait ();
10256                 }
10257                 
10258                 public override Expression CreateExpressionTree (ResolveContext ec)
10259                 {
10260                         using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
10261                                 return Expr.CreateExpressionTree (ec);
10262                 }
10263
10264                 protected override Expression DoResolve (ResolveContext ec)
10265                 {
10266                         using (ec.With (ResolveContext.Options.AllCheckStateFlags, false))
10267                                 Expr = Expr.Resolve (ec);
10268
10269                         if (Expr == null)
10270                                 return null;
10271
10272                         if (Expr is Constant || Expr is MethodGroupExpr || Expr is AnonymousMethodExpression || Expr is DefaultValueExpression)
10273                                 return Expr;
10274                         
10275                         eclass = Expr.eclass;
10276                         type = Expr.Type;
10277                         return this;
10278                 }
10279
10280                 public override void Emit (EmitContext ec)
10281                 {
10282                         using (ec.With (EmitContext.Options.CheckedScope, false))
10283                                 Expr.Emit (ec);
10284                 }
10285
10286                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
10287                 {
10288                         using (ec.With (EmitContext.Options.CheckedScope, false))
10289                                 Expr.EmitBranchable (ec, target, on_true);
10290                 }
10291
10292                 public override void FlowAnalysis (FlowAnalysisContext fc)
10293                 {
10294                         Expr.FlowAnalysis (fc);
10295                 }
10296
10297                 protected override void CloneTo (CloneContext clonectx, Expression t)
10298                 {
10299                         UnCheckedExpr target = (UnCheckedExpr) t;
10300
10301                         target.Expr = Expr.Clone (clonectx);
10302                 }
10303
10304                 public override object Accept (StructuralVisitor visitor)
10305                 {
10306                         return visitor.Visit (this);
10307                 }
10308         }
10309
10310         /// <summary>
10311         ///   An Element Access expression.
10312         ///
10313         ///   During semantic analysis these are transformed into 
10314         ///   IndexerAccess, ArrayAccess or a PointerArithmetic.
10315         /// </summary>
10316         public class ElementAccess : Expression
10317         {
10318                 public Arguments Arguments;
10319                 public Expression Expr;
10320                 bool conditional_access_receiver;
10321
10322                 public ElementAccess (Expression e, Arguments args, Location loc)
10323                 {
10324                         Expr = e;
10325                         this.loc = loc;
10326                         this.Arguments = args;
10327                 }
10328
10329                 public bool ConditionalAccess { get; set; }
10330
10331                 public override Location StartLocation {
10332                         get {
10333                                 return Expr.StartLocation;
10334                         }
10335                 }
10336
10337                 public override bool ContainsEmitWithAwait ()
10338                 {
10339                         return Expr.ContainsEmitWithAwait () || Arguments.ContainsEmitWithAwait ();
10340                 }
10341
10342                 //
10343                 // We perform some simple tests, and then to "split" the emit and store
10344                 // code we create an instance of a different class, and return that.
10345                 //
10346                 Expression CreateAccessExpression (ResolveContext ec, bool conditionalAccessReceiver)
10347                 {
10348                         if (conditionalAccessReceiver)
10349                                 ec.Set (ResolveContext.Options.DontSetConditionalAccessReceiver);
10350                         
10351                         Expr = Expr.Resolve (ec);
10352
10353                         if (conditionalAccessReceiver)
10354                                 ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false);
10355
10356                         if (Expr == null)
10357                                 return null;
10358
10359                         type = Expr.Type;
10360
10361                         if (ConditionalAccess && !IsNullPropagatingValid (type)) {
10362                                 Error_OperatorCannotBeApplied (ec, loc, "?", type);
10363                                 return null;
10364                         }
10365
10366                         if (type.IsArray) {
10367                                 var aa = new ArrayAccess (this, loc) {
10368                                         ConditionalAccess = ConditionalAccess,
10369                                 };
10370
10371                                 if (conditionalAccessReceiver)
10372                                         aa.SetConditionalAccessReceiver ();
10373
10374                                 return aa;
10375                         }
10376
10377                         if (type.IsPointer)
10378                                 return Expr.MakePointerAccess (ec, type, Arguments);
10379
10380                         FieldExpr fe = Expr as FieldExpr;
10381                         if (fe != null) {
10382                                 var ff = fe.Spec as FixedFieldSpec;
10383                                 if (ff != null) {
10384                                         return Expr.MakePointerAccess (ec, ff.ElementType, Arguments);
10385                                 }
10386                         }
10387
10388                         var indexers = MemberCache.FindMembers (type, MemberCache.IndexerNameAlias, false);
10389                         if (indexers != null || type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
10390                                 var indexer = new IndexerExpr (indexers, type, this) {
10391                                         ConditionalAccess = ConditionalAccess
10392                                 };
10393
10394                                 if (conditionalAccessReceiver)
10395                                         indexer.SetConditionalAccessReceiver ();
10396
10397                                 return indexer;
10398                         }
10399
10400                         Error_CannotApplyIndexing (ec, type, loc);
10401
10402                         return null;
10403                 }
10404
10405                 public override Expression CreateExpressionTree (ResolveContext ec)
10406                 {
10407                         Arguments args = Arguments.CreateForExpressionTree (ec, Arguments,
10408                                 Expr.CreateExpressionTree (ec));
10409
10410                         return CreateExpressionFactoryCall (ec, "ArrayIndex", args);
10411                 }
10412
10413                 public static void Error_CannotApplyIndexing (ResolveContext rc, TypeSpec type, Location loc)
10414                 {
10415                         if (type != InternalType.ErrorType) {
10416                                 rc.Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
10417                                         type.GetSignatureForError ());
10418                         }
10419                 }
10420
10421                 public override bool HasConditionalAccess ()
10422                 {
10423                         return ConditionalAccess || Expr.HasConditionalAccess ();
10424                 }
10425
10426                 void ResolveConditionalAccessReceiver (ResolveContext rc)
10427                 {
10428                         if (!rc.HasSet (ResolveContext.Options.DontSetConditionalAccessReceiver) && HasConditionalAccess ()) {
10429                                 conditional_access_receiver = true;
10430                         }
10431                 }
10432
10433                 protected override Expression DoResolve (ResolveContext rc)
10434                 {
10435                         ResolveConditionalAccessReceiver (rc);
10436
10437                         var expr = CreateAccessExpression (rc, conditional_access_receiver);
10438                         if (expr == null)
10439                                 return null;
10440
10441                         return expr.Resolve (rc);
10442                 }
10443
10444                 public override Expression DoResolveLValue (ResolveContext ec, Expression rhs)
10445                 {
10446                         var res = CreateAccessExpression (ec, false);
10447                         if (res == null)
10448                                 return null;
10449
10450                         return res.ResolveLValue (ec, rhs);
10451                 }
10452                 
10453                 public override void Emit (EmitContext ec)
10454                 {
10455                         throw new Exception ("Should never be reached");
10456                 }
10457
10458                 public override void FlowAnalysis (FlowAnalysisContext fc)
10459                 {
10460                         Expr.FlowAnalysis (fc);
10461
10462                         Arguments.FlowAnalysis (fc);
10463                 }
10464
10465                 public override string GetSignatureForError ()
10466                 {
10467                         return Expr.GetSignatureForError ();
10468                 }
10469
10470                 protected override void CloneTo (CloneContext clonectx, Expression t)
10471                 {
10472                         ElementAccess target = (ElementAccess) t;
10473
10474                         target.Expr = Expr.Clone (clonectx);
10475                         if (Arguments != null)
10476                                 target.Arguments = Arguments.Clone (clonectx);
10477                 }
10478                 
10479                 public override object Accept (StructuralVisitor visitor)
10480                 {
10481                         return visitor.Visit (this);
10482                 }
10483         }
10484
10485         /// <summary>
10486         ///   Implements array access 
10487         /// </summary>
10488         public class ArrayAccess : Expression, IDynamicAssign, IMemoryLocation {
10489                 //
10490                 // Points to our "data" repository
10491                 //
10492                 ElementAccess ea;
10493
10494                 LocalTemporary temp;
10495                 bool prepared;
10496                 bool? has_await_args;
10497                 bool conditional_access_receiver;
10498                 
10499                 public ArrayAccess (ElementAccess ea_data, Location l)
10500                 {
10501                         ea = ea_data;
10502                         loc = l;
10503                 }
10504
10505                 public bool ConditionalAccess { get; set; }
10506
10507                 public void AddressOf (EmitContext ec, AddressOp mode)
10508                 {
10509                         var ac = (ArrayContainer) ea.Expr.Type;
10510
10511                         if (!has_await_args.HasValue && ec.HasSet (BuilderContext.Options.AsyncBody) && ea.Arguments.ContainsEmitWithAwait ()) {
10512                                 LoadInstanceAndArguments (ec, false, true);
10513                         }
10514
10515                         LoadInstanceAndArguments (ec, false, false);
10516
10517                         if (ac.Element.IsGenericParameter && mode == AddressOp.Load)
10518                                 ec.Emit (OpCodes.Readonly);
10519
10520                         ec.EmitArrayAddress (ac);
10521                 }
10522
10523                 public override Expression CreateExpressionTree (ResolveContext ec)
10524                 {
10525                         if (ConditionalAccess)
10526                                 Error_NullShortCircuitInsideExpressionTree (ec);
10527
10528                         return ea.CreateExpressionTree (ec);
10529                 }
10530
10531                 public override bool ContainsEmitWithAwait ()
10532                 {
10533                         return ea.ContainsEmitWithAwait ();
10534                 }
10535
10536                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
10537                 {
10538                         if (HasConditionalAccess ())
10539                                 Error_NullPropagatingLValue (ec);
10540
10541                         return DoResolve (ec);
10542                 }
10543
10544                 protected override Expression DoResolve (ResolveContext ec)
10545                 {
10546                         // dynamic is used per argument in ConvertExpressionToArrayIndex case
10547                         bool dynamic;
10548                         ea.Arguments.Resolve (ec, out dynamic);
10549
10550                         var ac = ea.Expr.Type as ArrayContainer;
10551                         int rank = ea.Arguments.Count;
10552                         if (ac.Rank != rank) {
10553                                 ec.Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
10554                                           rank.ToString (), ac.Rank.ToString ());
10555                                 return null;
10556                         }
10557
10558                         type = ac.Element;
10559                         if (type.IsPointer) {
10560                                 if (ec.CurrentIterator != null) {
10561                                         UnsafeInsideIteratorError (ec, ea.Location);
10562                                 } else if (!ec.IsUnsafe) {
10563                                         UnsafeError (ec, ea.Location);
10564                                 }
10565                         }
10566
10567                         if (conditional_access_receiver)
10568                                 type = LiftMemberType (ec, type);
10569
10570                         foreach (Argument a in ea.Arguments) {
10571                                 var na = a as NamedArgument;
10572                                 if (na != null)
10573                                         ElementAccess.Error_NamedArgument (na, ec.Report);
10574
10575                                 a.Expr = ConvertExpressionToArrayIndex (ec, a.Expr);
10576                         }
10577                         
10578                         eclass = ExprClass.Variable;
10579
10580                         return this;
10581                 }
10582
10583                 protected override void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
10584                 {
10585                         ec.Report.Warning (251, 2, loc, "Indexing an array with a negative index (array indices always start at zero)");
10586                 }
10587
10588                 public override void FlowAnalysis (FlowAnalysisContext fc)
10589                 {
10590                         var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
10591
10592                         ea.FlowAnalysis (fc);
10593
10594                         if (conditional_access_receiver)
10595                                 fc.DefiniteAssignment = da;
10596                 }
10597
10598                 public override bool HasConditionalAccess ()
10599                 {
10600                         return ConditionalAccess || ea.Expr.HasConditionalAccess ();
10601                 }
10602
10603                 //
10604                 // Load the array arguments into the stack.
10605                 //
10606                 void LoadInstanceAndArguments (EmitContext ec, bool duplicateArguments, bool prepareAwait)
10607                 {
10608                         if (prepareAwait) {
10609                                 ea.Expr = ea.Expr.EmitToField (ec);
10610                         } else {
10611                                 var ie = new InstanceEmitter (ea.Expr, false);
10612                                 ie.Emit (ec, ConditionalAccess);
10613
10614                                 if (duplicateArguments) {
10615                                         ec.Emit (OpCodes.Dup);
10616
10617                                         var copy = new LocalTemporary (ea.Expr.Type);
10618                                         copy.Store (ec);
10619                                         ea.Expr = copy;
10620                                 }
10621                         }
10622
10623                         var dup_args = ea.Arguments.Emit (ec, duplicateArguments, prepareAwait);
10624                         if (dup_args != null)
10625                                 ea.Arguments = dup_args;
10626                 }
10627
10628                 public void Emit (EmitContext ec, bool leave_copy)
10629                 {
10630                         if (prepared) {
10631                                 ec.EmitLoadFromPtr (type);
10632                         } else {
10633                                 if (!has_await_args.HasValue && ec.HasSet (BuilderContext.Options.AsyncBody) && ea.Arguments.ContainsEmitWithAwait ()) {
10634                                         LoadInstanceAndArguments (ec, false, true);
10635                                 }
10636
10637                                 if (conditional_access_receiver)
10638                                         ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
10639
10640                                 var ac = (ArrayContainer) ea.Expr.Type;
10641                                 LoadInstanceAndArguments (ec, false, false);
10642                                 ec.EmitArrayLoad (ac);
10643
10644                                 if (conditional_access_receiver)
10645                                         ec.CloseConditionalAccess (type.IsNullableType && type != ac.Element ? type : null);
10646                         }       
10647
10648                         if (leave_copy) {
10649                                 ec.Emit (OpCodes.Dup);
10650                                 temp = new LocalTemporary (this.type);
10651                                 temp.Store (ec);
10652                         }
10653                 }
10654                 
10655                 public override void Emit (EmitContext ec)
10656                 {
10657                         Emit (ec, false);
10658                 }
10659
10660                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
10661                 {
10662                         var ac = (ArrayContainer) ea.Expr.Type;
10663                         TypeSpec t = source.Type;
10664
10665                         has_await_args = ec.HasSet (BuilderContext.Options.AsyncBody) && (ea.Arguments.ContainsEmitWithAwait () || source.ContainsEmitWithAwait ());
10666
10667                         //
10668                         // When we are dealing with a struct, get the address of it to avoid value copy
10669                         // Same cannot be done for reference type because array covariance and the
10670                         // check in ldelema requires to specify the type of array element stored at the index
10671                         //
10672                         if (t.IsStruct && ((isCompound && !(source is DynamicExpressionStatement)) || !BuiltinTypeSpec.IsPrimitiveType (t))) {
10673                                 LoadInstanceAndArguments (ec, false, has_await_args.Value);
10674
10675                                 if (has_await_args.Value) {
10676                                         if (source.ContainsEmitWithAwait ()) {
10677                                                 source = source.EmitToField (ec);
10678                                                 isCompound = false;
10679                                                 prepared = true;
10680                                         }
10681
10682                                         LoadInstanceAndArguments (ec, isCompound, false);
10683                                 } else {
10684                                         prepared = true;
10685                                 }
10686
10687                                 ec.EmitArrayAddress (ac);
10688
10689                                 if (isCompound) {
10690                                         ec.Emit (OpCodes.Dup);
10691                                         prepared = true;
10692                                 }
10693                         } else {
10694                                 LoadInstanceAndArguments (ec, isCompound, has_await_args.Value);
10695
10696                                 if (has_await_args.Value) {
10697                                         if (source.ContainsEmitWithAwait ())
10698                                                 source = source.EmitToField (ec);
10699
10700                                         LoadInstanceAndArguments (ec, false, false);
10701                                 }
10702                         }
10703
10704                         source.Emit (ec);
10705
10706                         if (isCompound) {
10707                                 var lt = ea.Expr as LocalTemporary;
10708                                 if (lt != null)
10709                                         lt.Release (ec);
10710                         }
10711
10712                         if (leave_copy) {
10713                                 ec.Emit (OpCodes.Dup);
10714                                 temp = new LocalTemporary (this.type);
10715                                 temp.Store (ec);
10716                         }
10717
10718                         if (prepared) {
10719                                 ec.EmitStoreFromPtr (t);
10720                         } else {
10721                                 ec.EmitArrayStore (ac);
10722                         }
10723                         
10724                         if (temp != null) {
10725                                 temp.Emit (ec);
10726                                 temp.Release (ec);
10727                         }
10728                 }
10729
10730                 public override Expression EmitToField (EmitContext ec)
10731                 {
10732                         //
10733                         // Have to be specialized for arrays to get access to
10734                         // underlying element. Instead of another result copy we
10735                         // need direct access to element 
10736                         //
10737                         // Consider:
10738                         //
10739                         // CallRef (ref a[await Task.Factory.StartNew (() => 1)]);
10740                         //
10741                         ea.Expr = ea.Expr.EmitToField (ec);
10742                         ea.Arguments = ea.Arguments.Emit (ec, false, true);
10743                         return this;
10744                 }
10745
10746                 public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
10747                 {
10748                         return SLE.Expression.ArrayAccess (ea.Expr.MakeExpression (ctx), MakeExpressionArguments (ctx));
10749                 }
10750
10751                 public override SLE.Expression MakeExpression (BuilderContext ctx)
10752                 {
10753                         return SLE.Expression.ArrayIndex (ea.Expr.MakeExpression (ctx), MakeExpressionArguments (ctx));
10754                 }
10755
10756                 SLE.Expression[] MakeExpressionArguments (BuilderContext ctx)
10757                 {
10758                         using (ctx.With (BuilderContext.Options.CheckedScope, true)) {
10759                                 return Arguments.MakeExpression (ea.Arguments, ctx);
10760                         }
10761                 }
10762
10763                 public void SetConditionalAccessReceiver ()
10764                 {
10765                         conditional_access_receiver = true;
10766                 }
10767         }
10768
10769         //
10770         // Indexer access expression
10771         //
10772         class IndexerExpr : PropertyOrIndexerExpr<IndexerSpec>, OverloadResolver.IBaseMembersProvider
10773         {
10774                 IList<MemberSpec> indexers;
10775                 Arguments arguments;
10776                 TypeSpec queried_type;
10777                 
10778                 public IndexerExpr (IList<MemberSpec> indexers, TypeSpec queriedType, ElementAccess ea)
10779                         : this (indexers, queriedType, ea.Expr, ea.Arguments, ea.Location)
10780                 {
10781                 }
10782
10783                 public IndexerExpr (IList<MemberSpec> indexers, TypeSpec queriedType, Expression instance, Arguments args, Location loc)
10784                         : base (loc)
10785                 {
10786                         this.indexers = indexers;
10787                         this.queried_type = queriedType;
10788                         this.InstanceExpression = instance;
10789                         this.arguments = args;
10790                 }
10791
10792                 #region Properties
10793
10794                 protected override Arguments Arguments {
10795                         get {
10796                                 return arguments;
10797                         }
10798                         set {
10799                                 arguments = value;
10800                         }
10801                 }
10802
10803                 protected override TypeSpec DeclaringType {
10804                         get {
10805                                 return best_candidate.DeclaringType;
10806                         }
10807                 }
10808
10809                 public override bool IsInstance {
10810                         get {
10811                                 return true;
10812                         }
10813                 }
10814
10815                 public override bool IsStatic {
10816                         get {
10817                                 return false;
10818                         }
10819                 }
10820
10821                 public override string KindName {
10822                         get { return "indexer"; }
10823                 }
10824
10825                 public override string Name {
10826                         get {
10827                                 return "this";
10828                         }
10829                 }
10830
10831                 #endregion
10832
10833                 public override bool ContainsEmitWithAwait ()
10834                 {
10835                         return base.ContainsEmitWithAwait () || arguments.ContainsEmitWithAwait ();
10836                 }
10837
10838                 public override Expression CreateExpressionTree (ResolveContext ec)
10839                 {
10840                         if (ConditionalAccess) {
10841                                 Error_NullShortCircuitInsideExpressionTree (ec);
10842                         }
10843
10844                         Arguments args = Arguments.CreateForExpressionTree (ec, arguments,
10845                                 InstanceExpression.CreateExpressionTree (ec),
10846                                 new TypeOfMethod (Getter, loc));
10847
10848                         return CreateExpressionFactoryCall (ec, "Call", args);
10849                 }
10850         
10851                 public override void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
10852                 {
10853                         LocalTemporary await_source_arg = null;
10854
10855                         if (isCompound) {
10856                                 emitting_compound_assignment = true;
10857                                 if (source is DynamicExpressionStatement) {
10858                                         Emit (ec, false);
10859                                 } else {
10860                                         source.Emit (ec);
10861                                 }
10862                                 emitting_compound_assignment = false;
10863
10864                                 if (has_await_arguments) {
10865                                         await_source_arg = new LocalTemporary (Type);
10866                                         await_source_arg.Store (ec);
10867
10868                                         arguments.Add (new Argument (await_source_arg));
10869
10870                                         if (leave_copy) {
10871                                                 temp = await_source_arg;
10872                                         }
10873
10874                                         has_await_arguments = false;
10875                                 } else {
10876                                         arguments = null;
10877
10878                                         if (leave_copy) {
10879                                                 ec.Emit (OpCodes.Dup);
10880                                                 temp = new LocalTemporary (Type);
10881                                                 temp.Store (ec);
10882                                         }
10883                                 }
10884                         } else {
10885                                 if (leave_copy) {
10886                                         if (ec.HasSet (BuilderContext.Options.AsyncBody) && (arguments.ContainsEmitWithAwait () || source.ContainsEmitWithAwait ())) {
10887                                                 source = source.EmitToField (ec);
10888                                         } else {
10889                                                 temp = new LocalTemporary (Type);
10890                                                 source.Emit (ec);
10891                                                 temp.Store (ec);
10892                                                 source = temp;
10893                                         }
10894                                 }
10895
10896                                 arguments.Add (new Argument (source));
10897                         }
10898
10899                         var call = new CallEmitter ();
10900                         call.InstanceExpression = InstanceExpression;
10901                         if (arguments == null)
10902                                 call.InstanceExpressionOnStack = true;
10903
10904                         call.Emit (ec, Setter, arguments, loc);
10905
10906                         if (temp != null) {
10907                                 temp.Emit (ec);
10908                                 temp.Release (ec);
10909                         } else if (leave_copy) {
10910                                 source.Emit (ec);
10911                         }
10912
10913                         if (await_source_arg != null) {
10914                                 await_source_arg.Release (ec);
10915                         }
10916                 }
10917
10918                 public override void FlowAnalysis (FlowAnalysisContext fc)
10919                 {
10920                         var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
10921
10922                         base.FlowAnalysis (fc);
10923                         arguments.FlowAnalysis (fc);
10924
10925                         if (conditional_access_receiver)
10926                                 fc.DefiniteAssignment = da;
10927                 }
10928
10929                 public override string GetSignatureForError ()
10930                 {
10931                         return best_candidate.GetSignatureForError ();
10932                 }
10933                 
10934                 public override SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
10935                 {
10936 #if STATIC
10937                         throw new NotSupportedException ();
10938 #else
10939                         var value = new[] { source.MakeExpression (ctx) };
10940                         var args = Arguments.MakeExpression (arguments, ctx).Concat (value);
10941                         return SLE.Expression.Block (
10942                                         SLE.Expression.Call (InstanceExpression.MakeExpression (ctx), (MethodInfo) Setter.GetMetaInfo (), args),
10943                                         value [0]);
10944 #endif
10945                 }
10946
10947                 public override SLE.Expression MakeExpression (BuilderContext ctx)
10948                 {
10949 #if STATIC
10950                         return base.MakeExpression (ctx);
10951 #else
10952                         var args = Arguments.MakeExpression (arguments, ctx);
10953                         return SLE.Expression.Call (InstanceExpression.MakeExpression (ctx), (MethodInfo) Getter.GetMetaInfo (), args);
10954 #endif
10955                 }
10956
10957                 protected override Expression OverloadResolve (ResolveContext rc, Expression right_side)
10958                 {
10959                         if (best_candidate != null)
10960                                 return this;
10961
10962                         eclass = ExprClass.IndexerAccess;
10963
10964                         bool dynamic;
10965                         using (rc.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false)) {
10966                                 arguments.Resolve (rc, out dynamic);
10967                         }
10968
10969                         if (indexers == null && InstanceExpression.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
10970                                 dynamic = true;
10971                         } else {
10972                                 var res = new OverloadResolver (indexers, OverloadResolver.Restrictions.None, loc);
10973                                 res.BaseMembersProvider = this;
10974                                 res.InstanceQualifier = this;
10975
10976                                 // TODO: Do I need 2 argument sets?
10977                                 best_candidate = res.ResolveMember<IndexerSpec> (rc, ref arguments);
10978                                 if (best_candidate != null)
10979                                         type = res.BestCandidateReturnType;
10980                                 else if (!res.BestCandidateIsDynamic)
10981                                         return null;
10982                         }
10983
10984                         //
10985                         // It has dynamic arguments
10986                         //
10987                         if (dynamic) {
10988                                 Arguments args = new Arguments (arguments.Count + 1);
10989                                 if (IsBase) {
10990                                         rc.Report.Error (1972, loc,
10991                                                 "The indexer base access cannot be dynamically dispatched. Consider casting the dynamic arguments or eliminating the base access");
10992                                 } else {
10993                                         args.Add (new Argument (InstanceExpression));
10994                                 }
10995                                 args.AddRange (arguments);
10996
10997                                 best_candidate = null;
10998                                 return new DynamicIndexBinder (args, conditional_access_receiver, ConditionalAccess, loc);
10999                         }
11000
11001                         //
11002                         // Try to avoid resolving left expression again
11003                         //
11004                         if (right_side != null)
11005                                 ResolveInstanceExpression (rc, right_side);
11006
11007                         return this;
11008                 }
11009
11010                 protected override void CloneTo (CloneContext clonectx, Expression t)
11011                 {
11012                         IndexerExpr target = (IndexerExpr) t;
11013
11014                         if (arguments != null)
11015                                 target.arguments = arguments.Clone (clonectx);
11016                 }
11017
11018                 public void SetConditionalAccessReceiver ()
11019                 {
11020                         conditional_access_receiver = true;
11021                 }
11022
11023                 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
11024                 {
11025                         Error_TypeArgumentsCannotBeUsed (ec, "indexer", GetSignatureForError (), loc);
11026                 }
11027
11028                 #region IBaseMembersProvider Members
11029
11030                 IList<MemberSpec> OverloadResolver.IBaseMembersProvider.GetBaseMembers (TypeSpec type)
11031                 {
11032                         var baseType = type.BaseType;
11033                         var members = baseType == null ? null : MemberCache.FindMembers (baseType, MemberCache.IndexerNameAlias, false);
11034
11035                         if (members == null && !type.IsInterface) {
11036                                 var tps = queried_type as TypeParameterSpec;
11037                                 if (tps != null)
11038                                         members = MemberCache.FindInterfaceMembers (tps, MemberCache.IndexerNameAlias);
11039                         }
11040
11041                         return members;
11042                 }
11043
11044                 IParametersMember OverloadResolver.IBaseMembersProvider.GetOverrideMemberParameters (MemberSpec member)
11045                 {
11046                         if (queried_type == member.DeclaringType)
11047                                 return null;
11048
11049                         var filter = new MemberFilter (MemberCache.IndexerNameAlias, 0, MemberKind.Indexer, ((IndexerSpec) member).Parameters, null);
11050                         return MemberCache.FindMember (queried_type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as IParametersMember;
11051                 }
11052
11053                 MethodGroupExpr OverloadResolver.IBaseMembersProvider.LookupExtensionMethod (ResolveContext rc)
11054                 {
11055                         return null;
11056                 }
11057
11058                 #endregion
11059         }
11060
11061         //
11062         // A base access expression
11063         //
11064         public class BaseThis : This
11065         {
11066                 public BaseThis (Location loc)
11067                         : base (loc)
11068                 {
11069                 }
11070
11071                 public BaseThis (TypeSpec type, Location loc)
11072                         : base (loc)
11073                 {
11074                         this.type = type;
11075                         eclass = ExprClass.Variable;
11076                 }
11077
11078                 #region Properties
11079
11080                 public override string Name {
11081                         get {
11082                                 return "base";
11083                         }
11084                 }
11085
11086                 #endregion
11087
11088                 public override Expression CreateExpressionTree (ResolveContext ec)
11089                 {
11090                         ec.Report.Error (831, loc, "An expression tree may not contain a base access");
11091                         return base.CreateExpressionTree (ec);
11092                 }
11093
11094                 public override void Emit (EmitContext ec)
11095                 {
11096                         base.Emit (ec);
11097
11098                         if (type == ec.Module.Compiler.BuiltinTypes.ValueType) {
11099                                 var context_type = ec.CurrentType;
11100                                 ec.Emit (OpCodes.Ldobj, context_type);
11101                                 ec.Emit (OpCodes.Box, context_type);
11102                         }
11103                 }
11104
11105                 protected override void Error_ThisNotAvailable (ResolveContext ec)
11106                 {
11107                         if (ec.IsStatic) {
11108                                 ec.Report.Error (1511, loc, "Keyword `base' is not available in a static method");
11109                         } else {
11110                                 ec.Report.Error (1512, loc, "Keyword `base' is not available in the current context");
11111                         }
11112                 }
11113
11114                 public override void ResolveBase (ResolveContext ec)
11115                 {
11116                         base.ResolveBase (ec);
11117                         type = ec.CurrentType.BaseType;
11118                 }
11119
11120                 public override object Accept (StructuralVisitor visitor)
11121                 {
11122                         return visitor.Visit (this);
11123                 }
11124         }
11125
11126         /// <summary>
11127         ///   This class exists solely to pass the Type around and to be a dummy
11128         ///   that can be passed to the conversion functions (this is used by
11129         ///   foreach implementation to typecast the object return value from
11130         ///   get_Current into the proper type.  All code has been generated and
11131         ///   we only care about the side effect conversions to be performed
11132         ///
11133         ///   This is also now used as a placeholder where a no-action expression
11134         ///   is needed (the `New' class).
11135         /// </summary>
11136         public class EmptyExpression : Expression
11137         {
11138                 sealed class OutAccessExpression : EmptyExpression
11139                 {
11140                         public OutAccessExpression (TypeSpec t)
11141                                 : base (t)
11142                         {
11143                         }
11144
11145                         public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
11146                         {
11147                                 rc.Report.Error (206, right_side.Location,
11148                                         "A property, indexer or dynamic member access may not be passed as `ref' or `out' parameter");
11149
11150                                 return null;
11151                         }
11152                 }
11153
11154                 public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression (InternalType.FakeInternalType);
11155                 public static readonly EmptyExpression LValueMemberOutAccess = new EmptyExpression (InternalType.FakeInternalType);
11156                 public static readonly EmptyExpression UnaryAddress = new EmptyExpression (InternalType.FakeInternalType);
11157                 public static readonly EmptyExpression EventAddition = new EmptyExpression (InternalType.FakeInternalType);
11158                 public static readonly EmptyExpression EventSubtraction = new EmptyExpression (InternalType.FakeInternalType);
11159                 public static readonly EmptyExpression MissingValue = new EmptyExpression (InternalType.FakeInternalType);
11160                 public static readonly Expression Null = new EmptyExpression (InternalType.FakeInternalType);
11161                 public static readonly EmptyExpression OutAccess = new OutAccessExpression (InternalType.FakeInternalType);
11162
11163                 public EmptyExpression (TypeSpec t)
11164                 {
11165                         type = t;
11166                         eclass = ExprClass.Value;
11167                         loc = Location.Null;
11168                 }
11169
11170                 protected override void CloneTo (CloneContext clonectx, Expression target)
11171                 {
11172                 }
11173
11174                 public override bool ContainsEmitWithAwait ()
11175                 {
11176                         return false;
11177                 }
11178
11179                 public override Expression CreateExpressionTree (ResolveContext ec)
11180                 {
11181                         throw new NotSupportedException ("ET");
11182                 }
11183                 
11184                 protected override Expression DoResolve (ResolveContext ec)
11185                 {
11186                         return this;
11187                 }
11188
11189                 public override void Emit (EmitContext ec)
11190                 {
11191                         // nothing, as we only exist to not do anything.
11192                 }
11193
11194                 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
11195                 {
11196                 }
11197
11198                 public override void EmitSideEffect (EmitContext ec)
11199                 {
11200                 }
11201
11202                 public override object Accept (StructuralVisitor visitor)
11203                 {
11204                         return visitor.Visit (this);
11205                 }
11206         }
11207         
11208         sealed class EmptyAwaitExpression : EmptyExpression
11209         {
11210                 public EmptyAwaitExpression (TypeSpec type)
11211                         : base (type)
11212                 {
11213                 }
11214                 
11215                 public override bool ContainsEmitWithAwait ()
11216                 {
11217                         return true;
11218                 }
11219         }
11220         
11221         //
11222         // Empty statement expression
11223         //
11224         public sealed class EmptyExpressionStatement : ExpressionStatement
11225         {
11226                 public static readonly EmptyExpressionStatement Instance = new EmptyExpressionStatement ();
11227
11228                 private EmptyExpressionStatement ()
11229                 {
11230                         loc = Location.Null;
11231                 }
11232
11233                 public override bool ContainsEmitWithAwait ()
11234                 {
11235                         return false;
11236                 }
11237
11238                 public override Expression CreateExpressionTree (ResolveContext ec)
11239                 {
11240                         return null;
11241                 }
11242
11243                 public override void EmitStatement (EmitContext ec)
11244                 {
11245                         // Do nothing
11246                 }
11247
11248                 protected override Expression DoResolve (ResolveContext ec)
11249                 {
11250                         eclass = ExprClass.Value;
11251                         type = ec.BuiltinTypes.Object;
11252                         return this;
11253                 }
11254
11255                 public override void Emit (EmitContext ec)
11256                 {
11257                         // Do nothing
11258                 }
11259                 
11260                 public override object Accept (StructuralVisitor visitor)
11261                 {
11262                         return visitor.Visit (this);
11263                 }
11264         }
11265
11266         public class ErrorExpression : EmptyExpression
11267         {
11268                 public static readonly ErrorExpression Instance = new ErrorExpression ();
11269
11270                 private ErrorExpression ()
11271                         : base (InternalType.ErrorType)
11272                 {
11273                 }
11274
11275                 public override Expression CreateExpressionTree (ResolveContext ec)
11276                 {
11277                         return this;
11278                 }
11279
11280                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
11281                 {
11282                         return this;
11283                 }
11284
11285                 public override void Error_ValueAssignment (ResolveContext rc, Expression rhs)
11286                 {
11287                 }
11288
11289                 public override void Error_UnexpectedKind (ResolveContext ec, ResolveFlags flags, Location loc)
11290                 {
11291                 }
11292
11293                 public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
11294                 {
11295                 }
11296
11297                 public override void Error_OperatorCannotBeApplied (ResolveContext rc, Location loc, string oper, TypeSpec t)
11298                 {
11299                 }
11300                 
11301                 public override object Accept (StructuralVisitor visitor)
11302                 {
11303                         return visitor.Visit (this);
11304                 }
11305         }
11306
11307         public class UserCast : Expression {
11308                 MethodSpec method;
11309                 Expression source;
11310                 
11311                 public UserCast (MethodSpec method, Expression source, Location l)
11312                 {
11313                         if (source == null)
11314                                 throw new ArgumentNullException ("source");
11315
11316                         this.method = method;
11317                         this.source = source;
11318                         type = method.ReturnType;
11319                         loc = l;
11320                 }
11321
11322                 public Expression Source {
11323                         get {
11324                                 return source;
11325                         }
11326                         set {
11327                                 source = value;
11328                         }
11329                 }
11330
11331                 public override bool ContainsEmitWithAwait ()
11332                 {
11333                         return source.ContainsEmitWithAwait ();
11334                 }
11335
11336                 public override Expression CreateExpressionTree (ResolveContext ec)
11337                 {
11338                         Arguments args = new Arguments (3);
11339                         args.Add (new Argument (source.CreateExpressionTree (ec)));
11340                         args.Add (new Argument (new TypeOf (type, loc)));
11341                         args.Add (new Argument (new TypeOfMethod (method, loc)));
11342                         return CreateExpressionFactoryCall (ec, "Convert", args);
11343                 }
11344                         
11345                 protected override Expression DoResolve (ResolveContext ec)
11346                 {
11347                         method.CheckObsoleteness (ec, source.Location);
11348
11349                         eclass = ExprClass.Value;
11350                         return this;
11351                 }
11352
11353                 public override void Emit (EmitContext ec)
11354                 {
11355                         source.Emit (ec);
11356                         ec.MarkCallEntry (loc);
11357                         ec.Emit (OpCodes.Call, method);
11358                 }
11359
11360                 public override void FlowAnalysis (FlowAnalysisContext fc)
11361                 {
11362                         source.FlowAnalysis (fc);
11363                 }
11364
11365                 public override string GetSignatureForError ()
11366                 {
11367                         return TypeManager.CSharpSignature (method);
11368                 }
11369
11370                 public override SLE.Expression MakeExpression (BuilderContext ctx)
11371                 {
11372 #if STATIC
11373                         return base.MakeExpression (ctx);
11374 #else
11375                         return SLE.Expression.Convert (source.MakeExpression (ctx), type.GetMetaInfo (), (MethodInfo) method.GetMetaInfo ());
11376 #endif
11377                 }
11378         }
11379
11380         //
11381         // Holds additional type specifiers like ?, *, []
11382         //
11383         public class ComposedTypeSpecifier
11384         {
11385                 public static readonly ComposedTypeSpecifier SingleDimension = new ComposedTypeSpecifier (1, Location.Null);
11386
11387                 public readonly int Dimension;
11388                 public readonly Location Location;
11389
11390                 public ComposedTypeSpecifier (int specifier, Location loc)
11391                 {
11392                         this.Dimension = specifier;
11393                         this.Location = loc;
11394                 }
11395
11396                 #region Properties
11397                 public bool IsNullable {
11398                         get {
11399                                 return Dimension == -1;
11400                         }
11401                 }
11402
11403                 public bool IsPointer {
11404                         get {
11405                                 return Dimension == -2;
11406                         }
11407                 }
11408
11409                 public ComposedTypeSpecifier Next { get; set; }
11410
11411                 #endregion
11412
11413                 public static ComposedTypeSpecifier CreateArrayDimension (int dimension, Location loc)
11414                 {
11415                         return new ComposedTypeSpecifier (dimension, loc);
11416                 }
11417
11418                 public static ComposedTypeSpecifier CreateNullable (Location loc)
11419                 {
11420                         return new ComposedTypeSpecifier (-1, loc);
11421                 }
11422
11423                 public static ComposedTypeSpecifier CreatePointer (Location loc)
11424                 {
11425                         return new ComposedTypeSpecifier (-2, loc);
11426                 }
11427
11428                 public string GetSignatureForError ()
11429                 {
11430                         string s =
11431                                 IsPointer ? "*" :
11432                                 IsNullable ? "?" :
11433                                 ArrayContainer.GetPostfixSignature (Dimension);
11434
11435                         return Next != null ? s + Next.GetSignatureForError () : s;
11436                 }
11437         }
11438
11439         // <summary>
11440         //   This class is used to "construct" the type during a typecast
11441         //   operation.  Since the Type.GetType class in .NET can parse
11442         //   the type specification, we just use this to construct the type
11443         //   one bit at a time.
11444         // </summary>
11445         public class ComposedCast : TypeExpr {
11446                 FullNamedExpression left;
11447                 ComposedTypeSpecifier spec;
11448                 
11449                 public ComposedCast (FullNamedExpression left, ComposedTypeSpecifier spec)
11450                 {
11451                         if (spec == null)
11452                                 throw new ArgumentNullException ("spec");
11453
11454                         this.left = left;
11455                         this.spec = spec;
11456                         this.loc = left.Location;
11457                 }
11458
11459                 public override TypeSpec ResolveAsType (IMemberContext ec, bool allowUnboundTypeArguments)
11460                 {
11461                         type = left.ResolveAsType (ec);
11462                         if (type == null)
11463                                 return null;
11464
11465                         eclass = ExprClass.Type;
11466
11467                         var single_spec = spec;
11468
11469                         if (single_spec.IsNullable) {
11470                                 type = new Nullable.NullableType (type, loc).ResolveAsType (ec);
11471                                 if (type == null)
11472                                         return null;
11473
11474                                 single_spec = single_spec.Next;
11475                         } else if (single_spec.IsPointer) {
11476                                 //
11477                                 // Declared fields cannot have unmanaged check done before all types are defined
11478                                 //
11479                                 if (!(ec.CurrentMemberDefinition is Field) && !TypeManager.VerifyUnmanaged (ec.Module, type, loc))
11480                                         return null;
11481
11482                                 var rc = ec as ResolveContext;
11483                                 if (rc?.CurrentIterator != null) {
11484                                         UnsafeInsideIteratorError (ec.Module.Compiler.Report, loc);
11485                                 } else if (!ec.IsUnsafe) {
11486                                         UnsafeError (ec.Module.Compiler.Report, loc);
11487                                 }
11488
11489                                 do {
11490                                         type = PointerContainer.MakeType (ec.Module, type);
11491                                         single_spec = single_spec.Next;
11492                                 } while (single_spec != null && single_spec.IsPointer);
11493                         }
11494
11495                         if (single_spec != null && single_spec.Dimension > 0) {
11496                                 if (type.IsSpecialRuntimeType) {
11497                                         ec.Module.Compiler.Report.Error (611, loc, "Array elements cannot be of type `{0}'", type.GetSignatureForError ());
11498                                 } else if (type.IsStatic) {
11499                                         ec.Module.Compiler.Report.SymbolRelatedToPreviousError (type);
11500                                         ec.Module.Compiler.Report.Error (719, loc, "Array elements cannot be of static type `{0}'",
11501                                                 type.GetSignatureForError ());
11502                                 } else {
11503                                         MakeArray (ec.Module, single_spec);
11504                                 }
11505                         }
11506
11507                         return type;
11508                 }
11509
11510                 void MakeArray (ModuleContainer module, ComposedTypeSpecifier spec)
11511                 {
11512                         if (spec.Next != null)
11513                                 MakeArray (module, spec.Next);
11514
11515                         type = ArrayContainer.MakeType (module, type, spec.Dimension);
11516                 }
11517
11518                 public override string GetSignatureForError ()
11519                 {
11520                         return left.GetSignatureForError () + spec.GetSignatureForError ();
11521                 }
11522
11523                 public override object Accept (StructuralVisitor visitor)
11524                 {
11525                         return visitor.Visit (this);
11526                 }
11527         }
11528
11529         class FixedBufferPtr : Expression
11530         {
11531                 readonly Expression array;
11532
11533                 public FixedBufferPtr (Expression array, TypeSpec array_type, Location l)
11534                 {
11535                         this.type = array_type;
11536                         this.array = array;
11537                         this.loc = l;
11538                 }
11539
11540                 public override bool ContainsEmitWithAwait ()
11541                 {
11542                         throw new NotImplementedException ();
11543                 }
11544
11545                 public override Expression CreateExpressionTree (ResolveContext ec)
11546                 {
11547                         Error_PointerInsideExpressionTree (ec);
11548                         return null;
11549                 }
11550
11551                 public override void Emit(EmitContext ec)
11552                 {
11553                         array.Emit (ec);
11554                 }
11555
11556                 protected override Expression DoResolve (ResolveContext ec)
11557                 {
11558                         type = PointerContainer.MakeType (ec.Module, type);
11559                         eclass = ExprClass.Value;
11560                         return this;
11561                 }
11562         }
11563
11564
11565         //
11566         // This class is used to represent the address of an array, used
11567         // only by the Fixed statement, this generates "&a [0]" construct
11568         // for fixed (char *pa = a)
11569         //
11570         class ArrayPtr : FixedBufferPtr
11571         {
11572                 public ArrayPtr (Expression array, TypeSpec array_type, Location l):
11573                         base (array, array_type, l)
11574                 {
11575                 }
11576
11577                 public override void Emit (EmitContext ec)
11578                 {
11579                         base.Emit (ec);
11580                         
11581                         ec.EmitInt (0);
11582                         ec.Emit (OpCodes.Ldelema, ((PointerContainer) type).Element);
11583                 }
11584         }
11585
11586         //
11587         // Encapsulates a conversion rules required for array indexes
11588         //
11589         public class ArrayIndexCast : TypeCast
11590         {
11591                 public ArrayIndexCast (Expression expr, TypeSpec returnType)
11592                         : base (expr, returnType)
11593                 {
11594                         if (expr.Type == returnType) // int -> int
11595                                 throw new ArgumentException ("unnecessary array index conversion");
11596                 }
11597
11598                 public override Expression CreateExpressionTree (ResolveContext ec)
11599                 {
11600                         using (ec.Set (ResolveContext.Options.CheckedScope)) {
11601                                 return base.CreateExpressionTree (ec);
11602                         }
11603                 }
11604
11605                 public override void Emit (EmitContext ec)
11606                 {
11607                         child.Emit (ec);
11608
11609                         switch (child.Type.BuiltinType) {
11610                         case BuiltinTypeSpec.Type.UInt:
11611                                 ec.Emit (OpCodes.Conv_U);
11612                                 break;
11613                         case BuiltinTypeSpec.Type.Long:
11614                                 ec.Emit (OpCodes.Conv_Ovf_I);
11615                                 break;
11616                         case BuiltinTypeSpec.Type.ULong:
11617                                 ec.Emit (OpCodes.Conv_Ovf_I_Un);
11618                                 break;
11619                         default:
11620                                 throw new InternalErrorException ("Cannot emit cast to unknown array element type", type);
11621                         }
11622                 }
11623         }
11624
11625         //
11626         // Implements the `stackalloc' keyword
11627         //
11628         public class StackAlloc : Expression {
11629                 TypeSpec otype;
11630                 Expression texpr;
11631                 Expression count;
11632                 
11633                 public StackAlloc (Expression type, Expression count, Location l)
11634                 {
11635                         texpr = type;
11636                         this.count = count;
11637                         loc = l;
11638                 }
11639
11640                 public Expression TypeExpression {
11641                         get {
11642                                 return texpr;
11643                         }
11644                 }
11645
11646                 public Expression CountExpression {
11647                         get {
11648                                 return this.count;
11649                         }
11650                 }
11651
11652                 public override bool ContainsEmitWithAwait ()
11653                 {
11654                         return false;
11655                 }
11656
11657                 public override Expression CreateExpressionTree (ResolveContext ec)
11658                 {
11659                         throw new NotSupportedException ("ET");
11660                 }
11661
11662                 protected override Expression DoResolve (ResolveContext ec)
11663                 {
11664                         count = count.Resolve (ec);
11665                         if (count == null)
11666                                 return null;
11667                         
11668                         if (count.Type.BuiltinType != BuiltinTypeSpec.Type.UInt){
11669                                 count = Convert.ImplicitConversionRequired (ec, count, ec.BuiltinTypes.Int, loc);
11670                                 if (count == null)
11671                                         return null;
11672                         }
11673
11674                         Constant c = count as Constant;
11675                         if (c != null && c.IsNegative) {
11676                                 ec.Report.Error (247, loc, "Cannot use a negative size with stackalloc");
11677                         }
11678
11679                         if (ec.HasAny (ResolveContext.Options.CatchScope | ResolveContext.Options.FinallyScope)) {
11680                                 ec.Report.Error (255, loc, "Cannot use stackalloc in finally or catch");
11681                         }
11682
11683                         otype = texpr.ResolveAsType (ec);
11684                         if (otype == null)
11685                                 return null;
11686
11687                         if (!TypeManager.VerifyUnmanaged (ec.Module, otype, loc))
11688                                 return null;
11689
11690                         type = PointerContainer.MakeType (ec.Module, otype);
11691                         eclass = ExprClass.Value;
11692
11693                         return this;
11694                 }
11695
11696                 public override void Emit (EmitContext ec)
11697                 {
11698                         int size = BuiltinTypeSpec.GetSize (otype);
11699
11700                         count.Emit (ec);
11701
11702                         if (size == 0)
11703                                 ec.Emit (OpCodes.Sizeof, otype);
11704                         else
11705                                 ec.EmitInt (size);
11706
11707                         ec.Emit (OpCodes.Mul_Ovf_Un);
11708                         ec.Emit (OpCodes.Localloc);
11709                 }
11710
11711                 protected override void CloneTo (CloneContext clonectx, Expression t)
11712                 {
11713                         StackAlloc target = (StackAlloc) t;
11714                         target.count = count.Clone (clonectx);
11715                         target.texpr = texpr.Clone (clonectx);
11716                 }
11717                 
11718                 public override object Accept (StructuralVisitor visitor)
11719                 {
11720                         return visitor.Visit (this);
11721                 }
11722         }
11723
11724         //
11725         // An object initializer expression
11726         //
11727         public class ElementInitializer : Assign
11728         {
11729                 public readonly string Name;
11730
11731                 public ElementInitializer (string name, Expression initializer, Location loc)
11732                         : base (null, initializer, loc)
11733                 {
11734                         this.Name = name;
11735                 }
11736
11737                 public bool IsDictionaryInitializer {
11738                         get {
11739                                 return Name == null;
11740                         }
11741                 }
11742                 
11743                 protected override void CloneTo (CloneContext clonectx, Expression t)
11744                 {
11745                         ElementInitializer target = (ElementInitializer) t;
11746                         target.source = source.Clone (clonectx);
11747                 }
11748
11749                 public override Expression CreateExpressionTree (ResolveContext ec)
11750                 {
11751                         Arguments args = new Arguments (2);
11752                         FieldExpr fe = target as FieldExpr;
11753                         if (fe != null)
11754                                 args.Add (new Argument (fe.CreateTypeOfExpression ()));
11755                         else
11756                                 args.Add (new Argument (((PropertyExpr) target).CreateSetterTypeOfExpression (ec)));
11757
11758                         string mname;
11759                         Expression arg_expr;
11760                         var cinit = source as CollectionOrObjectInitializers;
11761                         if (cinit == null) {
11762                                 mname = "Bind";
11763                                 arg_expr = source.CreateExpressionTree (ec);
11764                         } else {
11765                                 mname = cinit.IsEmpty || cinit.Initializers[0] is ElementInitializer ? "MemberBind" : "ListBind";
11766                                 arg_expr = cinit.CreateExpressionTree (ec, !cinit.IsEmpty);
11767                         }
11768
11769                         args.Add (new Argument (arg_expr));
11770                         return CreateExpressionFactoryCall (ec, mname, args);
11771                 }
11772
11773                 protected override Expression DoResolve (ResolveContext ec)
11774                 {
11775                         if (source == null)
11776                                 return EmptyExpressionStatement.Instance;
11777
11778                         if (!ResolveElement (ec))
11779                                 return null;
11780
11781                         if (source is CollectionOrObjectInitializers) {
11782                                 target = target.Resolve (ec);
11783                                 if (target == null)
11784                                         return null;
11785                                 
11786                                 Expression previous = ec.CurrentInitializerVariable;
11787                                 ec.CurrentInitializerVariable = target;
11788                                 source = source.Resolve (ec);
11789                                 ec.CurrentInitializerVariable = previous;
11790                                 if (source == null)
11791                                         return null;
11792                                         
11793                                 eclass = source.eclass;
11794                                 type = source.Type;
11795
11796                                 return this;
11797                         }
11798
11799                         return base.DoResolve (ec);
11800                 }
11801         
11802                 public override void EmitStatement (EmitContext ec)
11803                 {
11804                         if (source is CollectionOrObjectInitializers)
11805                                 source.Emit (ec);
11806                         else
11807                                 base.EmitStatement (ec);
11808                 }
11809
11810                 protected virtual bool ResolveElement (ResolveContext rc)
11811                 {
11812                         var t = rc.CurrentInitializerVariable.Type;
11813                         if (t.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
11814                                 Arguments args = new Arguments (1);
11815                                 args.Add (new Argument (rc.CurrentInitializerVariable));
11816                                 target = new DynamicMemberBinder (Name, args, loc);
11817                         } else {
11818                                 var member = MemberLookup (rc, false, t, Name, 0, MemberLookupRestrictions.ExactArity, loc);
11819                                 if (member == null) {
11820                                         member = Expression.MemberLookup (rc, true, t, Name, 0, MemberLookupRestrictions.ExactArity, loc);
11821
11822                                         if (member != null) {
11823                                                 // TODO: ec.Report.SymbolRelatedToPreviousError (member);
11824                                                 ErrorIsInaccesible (rc, member.GetSignatureForError (), loc);
11825                                                 return false;
11826                                         }
11827                                 }
11828
11829                                 if (member == null) {
11830                                         Error_TypeDoesNotContainDefinition (rc, loc, t, Name);
11831                                         return false;
11832                                 }
11833
11834                                 var me = member as MemberExpr;
11835                                 if (me is EventExpr) {
11836                                         me = me.ResolveMemberAccess (rc, null, null);
11837                                 } else if (!(member is PropertyExpr || member is FieldExpr)) {
11838                                         rc.Report.Error (1913, loc,
11839                                                 "Member `{0}' cannot be initialized. An object initializer may only be used for fields, or properties",
11840                                                 member.GetSignatureForError ());
11841
11842                                         return false;
11843                                 }
11844
11845                                 if (me.IsStatic) {
11846                                         rc.Report.Error (1914, loc,
11847                                                 "Static field or property `{0}' cannot be assigned in an object initializer",
11848                                                 me.GetSignatureForError ());
11849                                 }
11850
11851                                 target = me;
11852                                 me.InstanceExpression = rc.CurrentInitializerVariable;
11853                         }
11854
11855                         return true;
11856                 }
11857         }
11858         
11859         //
11860         // A collection initializer expression
11861         //
11862         class CollectionElementInitializer : Invocation
11863         {
11864                 public class ElementInitializerArgument : Argument
11865                 {
11866                         public ElementInitializerArgument (Expression e)
11867                                 : base (e)
11868                         {
11869                         }
11870                 }
11871
11872                 sealed class AddMemberAccess : MemberAccess
11873                 {
11874                         public AddMemberAccess (Expression expr, Location loc)
11875                                 : base (expr, "Add", loc)
11876                         {
11877                         }
11878
11879                         public override void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
11880                         {
11881                                 if (TypeManager.HasElementType (type))
11882                                         return;
11883
11884                                 base.Error_TypeDoesNotContainDefinition (ec, type, name);
11885                         }
11886                 }
11887
11888                 public CollectionElementInitializer (Expression argument)
11889                         : base (null, new Arguments (1))
11890                 {
11891                         base.arguments.Add (new ElementInitializerArgument (argument));
11892                         this.loc = argument.Location;
11893                 }
11894
11895                 public CollectionElementInitializer (List<Expression> arguments, Location loc)
11896                         : base (null, new Arguments (arguments.Count))
11897                 {
11898                         foreach (Expression e in arguments)
11899                                 base.arguments.Add (new ElementInitializerArgument (e));
11900
11901                         this.loc = loc;
11902                 }
11903
11904                 public CollectionElementInitializer (Location loc)
11905                         : base (null, null)
11906                 {
11907                         this.loc = loc;
11908                 }
11909
11910                 public override Expression CreateExpressionTree (ResolveContext ec)
11911                 {
11912                         Arguments args = new Arguments (2);
11913                         args.Add (new Argument (mg.CreateExpressionTree (ec)));
11914
11915                         var expr_initializers = new ArrayInitializer (arguments.Count, loc);
11916                         foreach (Argument a in arguments) {
11917                                 if (a.ArgType == Argument.AType.ExtensionType) {
11918                                         ec.Report.Error (8075, a.Expr.Location, "An expression tree cannot contain a collection initializer with extension method");
11919                                         continue;
11920                                 }
11921                                 expr_initializers.Add (a.CreateExpressionTree (ec));
11922                         }
11923
11924                         args.Add (new Argument (new ArrayCreation (
11925                                 CreateExpressionTypeExpression (ec, loc), expr_initializers, loc)));
11926                         return CreateExpressionFactoryCall (ec, "ElementInit", args);
11927                 }
11928
11929                 protected override void CloneTo (CloneContext clonectx, Expression t)
11930                 {
11931                         CollectionElementInitializer target = (CollectionElementInitializer) t;
11932                         if (arguments != null)
11933                                 target.arguments = arguments.Clone (clonectx);
11934                 }
11935
11936                 protected override Expression DoResolve (ResolveContext ec)
11937                 {
11938                         base.expr = new AddMemberAccess (ec.CurrentInitializerVariable, loc);
11939
11940                         return base.DoResolve (ec);
11941                 }
11942         }
11943
11944         class DictionaryElementInitializer : ElementInitializer
11945         {
11946                 readonly Arguments args;
11947
11948                 public DictionaryElementInitializer (Arguments arguments, Expression initializer, Location loc)
11949                         : base (null, initializer, loc)
11950                 {
11951                         this.args = arguments;
11952                 }
11953
11954                 public override Expression CreateExpressionTree (ResolveContext ec)
11955                 {
11956                         ec.Report.Error (8074, loc, "Expression tree cannot contain a dictionary initializer");
11957                         return null;
11958                 }
11959
11960                 protected override bool ResolveElement (ResolveContext rc)
11961                 {
11962                         var init = rc.CurrentInitializerVariable;
11963                         var type = init.Type;
11964
11965                         if (type.IsArray) {
11966                                 target = new ArrayAccess (new ElementAccess (init, args, loc), loc);
11967                                 return true;
11968                         }
11969
11970                         if (type.IsPointer) {
11971                                 target = init.MakePointerAccess (rc, type, args);
11972                                 return true;
11973                         }
11974
11975                         var indexers = MemberCache.FindMembers (type, MemberCache.IndexerNameAlias, false);
11976                         if (indexers == null && type.BuiltinType != BuiltinTypeSpec.Type.Dynamic) {
11977                                 ElementAccess.Error_CannotApplyIndexing (rc, type, loc);
11978                                 return false;
11979                         }
11980
11981                         target = new IndexerExpr (indexers, type, init, args, loc);
11982                         return true;
11983                 }
11984         }
11985         
11986         //
11987         // A block of object or collection initializers
11988         //
11989         public class CollectionOrObjectInitializers : ExpressionStatement
11990         {
11991                 IList<Expression> initializers;
11992                 bool is_collection_initialization;
11993
11994                 public CollectionOrObjectInitializers (Location loc)
11995                         : this (new Expression[0], loc)
11996                 {
11997                 }
11998
11999                 public CollectionOrObjectInitializers (IList<Expression> initializers, Location loc)
12000                 {
12001                         this.initializers = initializers;
12002                         this.loc = loc;
12003                 }
12004
12005                 public IList<Expression> Initializers {
12006                         get {
12007                                 return initializers;
12008                         }
12009                 }
12010                 
12011                 public bool IsEmpty {
12012                         get {
12013                                 return initializers.Count == 0;
12014                         }
12015                 }
12016
12017                 public bool IsCollectionInitializer {
12018                         get {
12019                                 return is_collection_initialization;
12020                         }
12021                 }
12022
12023                 protected override void CloneTo (CloneContext clonectx, Expression target)
12024                 {
12025                         CollectionOrObjectInitializers t = (CollectionOrObjectInitializers) target;
12026
12027                         t.initializers = new List<Expression> (initializers.Count);
12028                         foreach (var e in initializers)
12029                                 t.initializers.Add (e.Clone (clonectx));
12030                 }
12031
12032                 public override bool ContainsEmitWithAwait ()
12033                 {
12034                         foreach (var e in initializers) {
12035                                 if (e.ContainsEmitWithAwait ())
12036                                         return true;
12037                         }
12038
12039                         return false;
12040                 }
12041
12042                 public override Expression CreateExpressionTree (ResolveContext ec)
12043                 {
12044                         return CreateExpressionTree (ec, false);
12045                 }
12046
12047                 public Expression CreateExpressionTree (ResolveContext ec, bool inferType)
12048                 {
12049                         var expr_initializers = new ArrayInitializer (initializers.Count, loc);
12050                         foreach (Expression e in initializers) {
12051                                 Expression expr = e.CreateExpressionTree (ec);
12052                                 if (expr != null)
12053                                         expr_initializers.Add (expr);
12054                         }
12055
12056                         if (inferType)
12057                                 return new ImplicitlyTypedArrayCreation (expr_initializers, loc);
12058
12059                         return new ArrayCreation (new TypeExpression (ec.Module.PredefinedTypes.MemberBinding.Resolve (), loc), expr_initializers, loc); 
12060                 }
12061                 
12062                 protected override Expression DoResolve (ResolveContext ec)
12063                 {
12064                         List<string> element_names = null;
12065                         for (int i = 0; i < initializers.Count; ++i) {
12066                                 Expression initializer = initializers [i];
12067                                 ElementInitializer element_initializer = initializer as ElementInitializer;
12068
12069                                 if (i == 0) {
12070                                         if (element_initializer != null) {
12071                                                 element_names = new List<string> (initializers.Count);
12072                                                 if (!element_initializer.IsDictionaryInitializer)
12073                                                         element_names.Add (element_initializer.Name);
12074                                         } else if (initializer is CompletingExpression) {
12075                                                 initializer.Resolve (ec);
12076                                                 throw new InternalErrorException ("This line should never be reached");
12077                                         } else {
12078                                                 var t = ec.CurrentInitializerVariable.Type;
12079                                                 // LAMESPEC: The collection must implement IEnumerable only, no dynamic support
12080                                                 if (!t.ImplementsInterface (ec.BuiltinTypes.IEnumerable, false) && t.BuiltinType != BuiltinTypeSpec.Type.Dynamic) {
12081                                                         ec.Report.Error (1922, loc, "A field or property `{0}' cannot be initialized with a collection " +
12082                                                                 "object initializer because type `{1}' does not implement `{2}' interface",
12083                                                                 ec.CurrentInitializerVariable.GetSignatureForError (),
12084                                                                 ec.CurrentInitializerVariable.Type.GetSignatureForError (),
12085                                                                 ec.BuiltinTypes.IEnumerable.GetSignatureForError ());
12086                                                         return null;
12087                                                 }
12088                                                 is_collection_initialization = true;
12089                                         }
12090                                 } else {
12091                                         if (is_collection_initialization != (element_initializer == null)) {
12092                                                 ec.Report.Error (747, initializer.Location, "Inconsistent `{0}' member declaration",
12093                                                         is_collection_initialization ? "collection initializer" : "object initializer");
12094                                                 continue;
12095                                         }
12096
12097                                         if (!is_collection_initialization && !element_initializer.IsDictionaryInitializer) {
12098                                                 if (element_names.Contains (element_initializer.Name)) {
12099                                                         ec.Report.Error (1912, element_initializer.Location,
12100                                                                 "An object initializer includes more than one member `{0}' initialization",
12101                                                                 element_initializer.Name);
12102                                                 } else {
12103                                                         element_names.Add (element_initializer.Name);
12104                                                 }
12105                                         }
12106                                 }
12107
12108                                 Expression e = initializer.Resolve (ec);
12109                                 if (e == EmptyExpressionStatement.Instance)
12110                                         initializers.RemoveAt (i--);
12111                                 else
12112                                         initializers [i] = e;
12113                         }
12114
12115                         type = ec.CurrentInitializerVariable.Type;
12116                         if (is_collection_initialization) {
12117                                 if (TypeManager.HasElementType (type)) {
12118                                         ec.Report.Error (1925, loc, "Cannot initialize object of type `{0}' with a collection initializer",
12119                                                 type.GetSignatureForError ());
12120                                 }
12121                         }
12122
12123                         eclass = ExprClass.Variable;
12124                         return this;
12125                 }
12126
12127                 public override void Emit (EmitContext ec)
12128                 {
12129                         EmitStatement (ec);
12130                 }
12131
12132                 public override void EmitStatement (EmitContext ec)
12133                 {
12134                         foreach (ExpressionStatement e in initializers) {
12135                                 // TODO: need location region
12136                                 ec.Mark (e.Location);
12137                                 e.EmitStatement (ec);
12138                         }
12139                 }
12140
12141                 public override void FlowAnalysis (FlowAnalysisContext fc)
12142                 {
12143                         foreach (var initializer in initializers) {
12144                                 if (initializer != null)
12145                                         initializer.FlowAnalysis (fc);
12146                         }
12147                 }
12148         }
12149         
12150         //
12151         // New expression with element/object initializers
12152         //
12153         public class NewInitialize : New
12154         {
12155                 //
12156                 // This class serves as a proxy for variable initializer target instances.
12157                 // A real variable is assigned later when we resolve left side of an
12158                 // assignment
12159                 //
12160                 sealed class InitializerTargetExpression : Expression, IMemoryLocation
12161                 {
12162                         NewInitialize new_instance;
12163
12164                         public InitializerTargetExpression (NewInitialize newInstance)
12165                         {
12166                                 this.type = newInstance.type;
12167                                 this.loc = newInstance.loc;
12168                                 this.eclass = newInstance.eclass;
12169                                 this.new_instance = newInstance;
12170                         }
12171
12172                         public override bool ContainsEmitWithAwait ()
12173                         {
12174                                 return false;
12175                         }
12176
12177                         public override Expression CreateExpressionTree (ResolveContext ec)
12178                         {
12179                                 // Should not be reached
12180                                 throw new NotSupportedException ("ET");
12181                         }
12182
12183                         protected override Expression DoResolve (ResolveContext ec)
12184                         {
12185                                 return this;
12186                         }
12187
12188                         public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
12189                         {
12190                                 return this;
12191                         }
12192
12193                         public override void Emit (EmitContext ec)
12194                         {
12195                                 Expression e = (Expression) new_instance.instance;
12196                                 e.Emit (ec);
12197                         }
12198
12199                         public override Expression EmitToField (EmitContext ec)
12200                         {
12201                                 return (Expression) new_instance.instance;
12202                         }
12203
12204                         #region IMemoryLocation Members
12205
12206                         public void AddressOf (EmitContext ec, AddressOp mode)
12207                         {
12208                                 new_instance.instance.AddressOf (ec, mode);
12209                         }
12210
12211                         #endregion
12212                 }
12213
12214                 CollectionOrObjectInitializers initializers;
12215                 IMemoryLocation instance;
12216                 DynamicExpressionStatement dynamic;
12217
12218                 public NewInitialize (FullNamedExpression requested_type, Arguments arguments, CollectionOrObjectInitializers initializers, Location l)
12219                         : base (requested_type, arguments, l)
12220                 {
12221                         this.initializers = initializers;
12222                 }
12223
12224                 public CollectionOrObjectInitializers Initializers {
12225                         get {
12226                                 return initializers;
12227                         }
12228                 }
12229
12230                 protected override void CloneTo (CloneContext clonectx, Expression t)
12231                 {
12232                         base.CloneTo (clonectx, t);
12233
12234                         NewInitialize target = (NewInitialize) t;
12235                         target.initializers = (CollectionOrObjectInitializers) initializers.Clone (clonectx);
12236                 }
12237
12238                 public override bool ContainsEmitWithAwait ()
12239                 {
12240                         return base.ContainsEmitWithAwait () || initializers.ContainsEmitWithAwait ();
12241                 }
12242
12243                 public override Expression CreateExpressionTree (ResolveContext ec)
12244                 {
12245                         Arguments args = new Arguments (2);
12246                         args.Add (new Argument (base.CreateExpressionTree (ec)));
12247                         if (!initializers.IsEmpty)
12248                                 args.Add (new Argument (initializers.CreateExpressionTree (ec, initializers.IsCollectionInitializer)));
12249
12250                         return CreateExpressionFactoryCall (ec,
12251                                 initializers.IsCollectionInitializer ? "ListInit" : "MemberInit",
12252                                 args);
12253                 }
12254
12255                 protected override Expression DoResolve (ResolveContext rc)
12256                 {
12257                         Expression e = base.DoResolve (rc);
12258                         if (type == null)
12259                                 return null;
12260
12261                         if (type.IsDelegate) {
12262                                 rc.Report.Error (1958, Initializers.Location,
12263                                         "Object and collection initializers cannot be used to instantiate a delegate");
12264                         }
12265
12266                         Expression previous = rc.CurrentInitializerVariable;
12267                         rc.CurrentInitializerVariable = new InitializerTargetExpression (this);
12268                         using (rc.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false)) {
12269                                 initializers.Resolve (rc);
12270                         }
12271                         rc.CurrentInitializerVariable = previous;
12272
12273                         dynamic = e as DynamicExpressionStatement;
12274                         if (dynamic != null)
12275                                 return this;
12276
12277                         return e;
12278                 }
12279
12280                 public override void Emit (EmitContext ec)
12281                 {
12282                         if (!CanEmitOptimizedLocalTarget (ec)) {
12283                                 var fe = ec.GetTemporaryField (type);
12284
12285                                 if (!Emit (ec, fe))
12286                                         fe.Emit (ec);
12287
12288                                 return;
12289                         }
12290
12291                         base.Emit (ec);
12292                 }
12293
12294                 public override bool Emit (EmitContext ec, IMemoryLocation target)
12295                 {
12296                         //
12297                         // Expression is initialized into temporary target then moved
12298                         // to real one for atomicity
12299                         //
12300                         IMemoryLocation temp_target = target;
12301
12302                         LocalTemporary temp = null;
12303                         bool by_ref = false;
12304                         if (!initializers.IsEmpty) {
12305                                 temp_target = target as LocalTemporary;
12306                                 if (temp_target == null)
12307                                         temp_target = target as StackFieldExpr;
12308
12309                                 if (temp_target == null) {
12310                                         var vr = target as VariableReference;
12311                                         if (vr != null && vr.IsRef) {
12312                                                 vr.EmitLoad (ec);
12313                                                 by_ref = true;
12314                                         }
12315                                 }
12316
12317                                 if (temp_target == null)
12318                                         temp_target = temp = new LocalTemporary (type);
12319                         }
12320
12321                         bool left_on_stack;
12322                         if (dynamic != null) {
12323                                 dynamic.Emit (ec);
12324                                 left_on_stack = true;
12325                         } else {
12326                                 left_on_stack = base.Emit (ec, temp_target);
12327                         }
12328
12329                         if (initializers.IsEmpty)
12330                                 return left_on_stack;
12331
12332                         StackFieldExpr sf = null;
12333
12334                         // Move a new instance (reference-type) to local temporary variable
12335                         if (left_on_stack) {
12336                                 if (by_ref) {
12337                                         temp_target = temp = new LocalTemporary (type);
12338                                 }
12339
12340                                 if (temp != null)
12341                                         temp.Store (ec);
12342
12343                                 if (ec.HasSet (BuilderContext.Options.AsyncBody) && initializers.ContainsEmitWithAwait ()) {
12344                                         if (temp == null)
12345                                                 throw new NotImplementedException ();
12346
12347                                         sf = ec.GetTemporaryField (type);
12348                                         sf.AutomaticallyReuse = false;
12349                                         sf.EmitAssign (ec, temp, false, false);
12350                                         temp_target = sf;
12351                                         temp.Release (ec);
12352                                         left_on_stack = false;
12353                                 }
12354                         }
12355
12356                         instance = temp_target;
12357
12358                         initializers.Emit (ec);
12359
12360                         ((Expression)temp_target).Emit (ec);
12361
12362                         if (temp != null)
12363                                 temp.Release (ec);
12364
12365                         if (sf != null)
12366                                 sf.PrepareCleanup (ec);
12367                         
12368                         return true;
12369                 }
12370
12371                 public override bool CanEmitOptimizedLocalTarget (EmitContext ec)
12372                 {
12373                         return !(method == null && TypeSpec.IsValueType (type) &&
12374                                         initializers.Initializers.Count > 1 && ec.HasSet (BuilderContext.Options.AsyncBody) &&
12375                                         initializers.ContainsEmitWithAwait ());
12376                 }
12377
12378                 protected override IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp Mode)
12379                 {
12380                         instance = base.EmitAddressOf (ec, Mode);
12381
12382                         if (!initializers.IsEmpty)
12383                                 initializers.Emit (ec);
12384
12385                         return instance;
12386                 }
12387
12388                 public override void FlowAnalysis (FlowAnalysisContext fc)
12389                 {
12390                         base.FlowAnalysis (fc);
12391                         initializers.FlowAnalysis (fc);
12392                 }
12393
12394                 public override object Accept (StructuralVisitor visitor)
12395                 {
12396                         return visitor.Visit (this);
12397                 }
12398         }
12399
12400         public class NewAnonymousType : New
12401         {
12402                 static readonly AnonymousTypeParameter[] EmptyParameters = new AnonymousTypeParameter[0];
12403
12404                 List<AnonymousTypeParameter> parameters;
12405                 readonly TypeContainer parent;
12406                 AnonymousTypeClass anonymous_type;
12407
12408                 public NewAnonymousType (List<AnonymousTypeParameter> parameters, TypeContainer parent, Location loc)
12409                          : base (null, null, loc)
12410                 {
12411                         this.parameters = parameters;
12412                         this.parent = parent;
12413                 }
12414
12415                 public List<AnonymousTypeParameter> Parameters {
12416                         get {
12417                                 return this.parameters;
12418                         }
12419                 }
12420
12421                 protected override void CloneTo (CloneContext clonectx, Expression target)
12422                 {
12423                         if (parameters == null)
12424                                 return;
12425
12426                         NewAnonymousType t = (NewAnonymousType) target;
12427                         t.parameters = new List<AnonymousTypeParameter> (parameters.Count);
12428                         foreach (AnonymousTypeParameter atp in parameters)
12429                                 t.parameters.Add ((AnonymousTypeParameter) atp.Clone (clonectx));
12430                 }
12431
12432                 AnonymousTypeClass CreateAnonymousType (ResolveContext ec, IList<AnonymousTypeParameter> parameters)
12433                 {
12434                         AnonymousTypeClass type = parent.Module.GetAnonymousType (parameters);
12435                         if (type != null)
12436                                 return type;
12437
12438                         type = AnonymousTypeClass.Create (parent, parameters, loc);
12439                         if (type == null)
12440                                 return null;
12441
12442                         int errors = ec.Report.Errors;
12443                         type.CreateContainer ();
12444                         type.DefineContainer ();
12445                         type.ExpandBaseInterfaces ();
12446                         type.Define ();
12447                         if ((ec.Report.Errors - errors) == 0) {
12448                                 parent.Module.AddAnonymousType (type);
12449                                 type.PrepareEmit ();
12450                         }
12451
12452                         return type;
12453                 }
12454
12455                 public override Expression CreateExpressionTree (ResolveContext ec)
12456                 {
12457                         if (parameters == null)
12458                                 return base.CreateExpressionTree (ec);
12459
12460                         var init = new ArrayInitializer (parameters.Count, loc);
12461                         foreach (var m in anonymous_type.Members) {
12462                                 var p = m as Property;
12463                                 if (p != null)
12464                                         init.Add (new TypeOfMethod (MemberCache.GetMember (type, p.Get.Spec), loc));
12465                         }
12466
12467                         var ctor_args = new ArrayInitializer (arguments.Count, loc);
12468                         foreach (Argument a in arguments)
12469                                 ctor_args.Add (a.CreateExpressionTree (ec));
12470
12471                         Arguments args = new Arguments (3);
12472                         args.Add (new Argument (new TypeOfMethod (method, loc)));
12473                         args.Add (new Argument (new ArrayCreation (CreateExpressionTypeExpression (ec, loc), ctor_args, loc)));
12474                         args.Add (new Argument (new ImplicitlyTypedArrayCreation (init, loc)));
12475
12476                         return CreateExpressionFactoryCall (ec, "New", args);
12477                 }
12478
12479                 protected override Expression DoResolve (ResolveContext ec)
12480                 {
12481                         if (ec.HasSet (ResolveContext.Options.ConstantScope)) {
12482                                 ec.Report.Error (836, loc, "Anonymous types cannot be used in this expression");
12483                                 return null;
12484                         }
12485
12486                         if (parameters == null) {
12487                                 anonymous_type = CreateAnonymousType (ec, EmptyParameters);
12488                                 RequestedType = new TypeExpression (anonymous_type.Definition, loc);
12489                                 return base.DoResolve (ec);
12490                         }
12491
12492                         bool error = false;
12493                         arguments = new Arguments (parameters.Count);
12494                         var t_args = new TypeSpec [parameters.Count];
12495                         for (int i = 0; i < parameters.Count; ++i) {
12496                                 Expression e = parameters [i].Resolve (ec);
12497                                 if (e == null) {
12498                                         error = true;
12499                                         continue;
12500                                 }
12501
12502                                 arguments.Add (new Argument (e));
12503                                 t_args [i] = e.Type;
12504                         }
12505
12506                         if (error)
12507                                 return null;
12508
12509                         anonymous_type = CreateAnonymousType (ec, parameters);
12510                         if (anonymous_type == null)
12511                                 return null;
12512
12513                         type = anonymous_type.Definition.MakeGenericType (ec.Module, t_args);
12514                         method = (MethodSpec) MemberCache.FindMember (type, MemberFilter.Constructor (null), BindingRestriction.DeclaredOnly);
12515                         eclass = ExprClass.Value;
12516                         return this;
12517                 }
12518                 
12519                 public override object Accept (StructuralVisitor visitor)
12520                 {
12521                         return visitor.Visit (this);
12522                 }
12523         }
12524
12525         public class AnonymousTypeParameter : ShimExpression
12526         {
12527                 public readonly string Name;
12528
12529                 public AnonymousTypeParameter (Expression initializer, string name, Location loc)
12530                         : base (initializer)
12531                 {
12532                         this.Name = name;
12533                         this.loc = loc;
12534                 }
12535                 
12536                 public AnonymousTypeParameter (Parameter parameter)
12537                         : base (new SimpleName (parameter.Name, parameter.Location))
12538                 {
12539                         this.Name = parameter.Name;
12540                         this.loc = parameter.Location;
12541                 }               
12542
12543                 public override bool Equals (object o)
12544                 {
12545                         AnonymousTypeParameter other = o as AnonymousTypeParameter;
12546                         return other != null && Name == other.Name;
12547                 }
12548
12549                 public override int GetHashCode ()
12550                 {
12551                         return Name.GetHashCode ();
12552                 }
12553
12554                 protected override Expression DoResolve (ResolveContext ec)
12555                 {
12556                         Expression e = expr.Resolve (ec);
12557                         if (e == null)
12558                                 return null;
12559
12560                         if (e.eclass == ExprClass.MethodGroup) {
12561                                 Error_InvalidInitializer (ec, e.ExprClassName);
12562                                 return null;
12563                         }
12564
12565                         type = e.Type;
12566                         if (type.Kind == MemberKind.Void || type == InternalType.NullLiteral || type == InternalType.AnonymousMethod || type.IsPointer) {
12567                                 Error_InvalidInitializer (ec, type.GetSignatureForError ());
12568                                 return null;
12569                         }
12570
12571                         return e;
12572                 }
12573
12574                 protected virtual void Error_InvalidInitializer (ResolveContext ec, string initializer)
12575                 {
12576                         ec.Report.Error (828, loc, "An anonymous type property `{0}' cannot be initialized with `{1}'",
12577                                 Name, initializer);
12578                 }
12579         }
12580
12581         public class CatchFilterExpression : BooleanExpression
12582         {
12583                 public CatchFilterExpression (Expression expr, Location loc)
12584                         : base (expr)
12585                 {
12586                         this.loc = loc;
12587                 }
12588         }
12589
12590         public class InterpolatedString : Expression
12591         {
12592                 readonly StringLiteral start, end;
12593                 List<Expression> interpolations;
12594                 Arguments arguments;
12595
12596                 public InterpolatedString (StringLiteral start, List<Expression> interpolations, StringLiteral end)
12597                 {
12598                         this.start = start;
12599                         this.end = end;
12600                         this.interpolations = interpolations;
12601                         loc = start.Location;
12602                 }
12603
12604                 protected override void CloneTo (CloneContext clonectx, Expression t)
12605                 {
12606                         InterpolatedString target = (InterpolatedString) t;
12607
12608                         if (interpolations != null) {
12609                                 target.interpolations = new List<Expression> ();
12610                                 foreach (var interpolation in interpolations) {
12611                                         target.interpolations.Add (interpolation.Clone (clonectx));
12612                                 }
12613                         }
12614                 }
12615
12616                 public Expression ConvertTo (ResolveContext rc, TypeSpec type)
12617                 {
12618                         var factory = rc.Module.PredefinedTypes.FormattableStringFactory.Resolve ();
12619                         if (factory == null)
12620                                 return null;
12621
12622                         var ma = new MemberAccess (new TypeExpression (factory, loc), "Create", loc);
12623                         var res = new Invocation (ma, arguments).Resolve (rc);
12624                         if (res != null && res.Type != type)
12625                                 res = Convert.ExplicitConversion (rc, res, type, loc);
12626
12627                         return res;
12628                 }
12629
12630                 public override bool ContainsEmitWithAwait ()
12631                 {
12632                         if (interpolations == null)
12633                                 return false;
12634
12635                         foreach (var expr in interpolations) {
12636                                 if (expr.ContainsEmitWithAwait ())
12637                                         return true;
12638                         }
12639
12640                         return false;
12641                 }
12642
12643                 public override Expression CreateExpressionTree (ResolveContext rc)
12644                 {
12645                         var best = ResolveBestFormatOverload (rc);
12646                         if (best == null)
12647                                 return null;
12648                         
12649                         Expression instance = new NullLiteral (loc);
12650                         var args = Arguments.CreateForExpressionTree (rc, arguments, instance, new TypeOfMethod (best, loc));
12651                         return CreateExpressionFactoryCall (rc, "Call", args);  
12652                 }
12653
12654                 protected override Expression DoResolve (ResolveContext rc)
12655                 {
12656                         string str;
12657
12658                         if (interpolations == null) {
12659                                 str = start.Value;
12660                                 arguments = new Arguments (1);
12661                         } else {
12662                                 arguments = new Arguments (interpolations.Count);
12663
12664                                 var sb = new StringBuilder (start.Value);
12665                                 for (int i = 0; i < interpolations.Count; ++i) {
12666                                         if (i % 2 == 0) {
12667                                                 sb.Append ('{').Append (i / 2);
12668                                                 var isi = (InterpolatedStringInsert)interpolations [i];
12669                                                 if (isi.Alignment != null) {
12670                                                         sb.Append (',');
12671                                                         var value = isi.ResolveAligment (rc);
12672                                                         if (value != null)
12673                                                                 sb.Append (value.Value);
12674                                                 }
12675
12676                                                 if (isi.Format != null) {
12677                                                         sb.Append (':');
12678                                                         sb.Append (isi.Format);
12679                                                 }
12680
12681                                                 sb.Append ('}');
12682                                                 arguments.Add (new Argument (isi.Resolve (rc)));
12683                                         } else {
12684                                                 sb.Append (((StringLiteral)interpolations [i]).Value);
12685                                         }
12686                                 }
12687
12688                                 sb.Append (end.Value);
12689                                 str = sb.ToString ();
12690                         }
12691
12692                         arguments.Insert (0, new Argument (new StringLiteral (rc.BuiltinTypes, str, start.Location)));
12693
12694                         eclass = ExprClass.Value;
12695                         type = rc.BuiltinTypes.String;
12696                         return this;
12697                 }
12698
12699                 public override void Emit (EmitContext ec)
12700                 {
12701                         // No interpolation, convert to simple string result (needs to match string.Format unescaping)
12702                         if (interpolations == null) {
12703                                 var str = start.Value.Replace ("{{", "{").Replace ("}}", "}");
12704                                 if (str != start.Value)
12705                                         new StringConstant (ec.BuiltinTypes, str, loc).Emit (ec);
12706                                 else
12707                                         start.Emit (ec);
12708
12709                                 return;
12710                         }
12711
12712                         var best = ResolveBestFormatOverload (new ResolveContext (ec.MemberContext));
12713                         if (best == null)
12714                                 return;
12715
12716                         var ca = new CallEmitter ();
12717                         ca.Emit (ec, best, arguments, loc);
12718                 }
12719
12720                 public override void FlowAnalysis (FlowAnalysisContext fc)
12721                 {
12722                         if (interpolations != null) {
12723                                 foreach (var expr in interpolations) {
12724                                         expr.FlowAnalysis (fc);
12725                                 }
12726                         }
12727                 }
12728
12729                 MethodSpec ResolveBestFormatOverload (ResolveContext rc)
12730                 {
12731                         var members = MemberCache.FindMembers (rc.BuiltinTypes.String, "Format", true);
12732                         var res = new OverloadResolver (members, OverloadResolver.Restrictions.NoBaseMembers, loc);
12733                         return res.ResolveMember<MethodSpec> (rc, ref arguments);
12734                 }
12735         }
12736
12737         public class InterpolatedStringInsert : CompositeExpression
12738         {
12739                 public InterpolatedStringInsert (Expression expr)
12740                         : base (expr)
12741                 {
12742                 }
12743
12744                 public Expression Alignment { get; set; }
12745                 public string Format { get; set; }
12746
12747                 protected override void CloneTo (CloneContext clonectx, Expression t)
12748                 {
12749                         var target = (InterpolatedStringInsert)t;
12750                         target.expr = expr.Clone (clonectx);
12751                         if (Alignment != null)
12752                                 target.Alignment = Alignment.Clone (clonectx);
12753                 }
12754
12755                 protected override Expression DoResolve (ResolveContext rc)
12756                 {
12757                         var expr = base.DoResolve (rc);
12758                         if (expr == null)
12759                                 return null;
12760
12761                         //
12762                         // For better error reporting, assumes the built-in implementation uses object
12763                         // as argument(s)
12764                         //
12765                         return Convert.ImplicitConversionRequired (rc, expr, rc.BuiltinTypes.Object, expr.Location);
12766                 }
12767
12768                 public override void FlowAnalysis (FlowAnalysisContext fc)
12769                 {
12770                         Child.FlowAnalysis (fc);
12771                 }
12772
12773                 public int? ResolveAligment (ResolveContext rc)
12774                 {
12775                         var c = Alignment.ResolveLabelConstant (rc);
12776                         if (c == null)
12777                                 return null;
12778                         
12779                         c = c.ImplicitConversionRequired (rc, rc.BuiltinTypes.Int);
12780                         if (c == null)
12781                                 return null;
12782                         
12783                         var value = (int) c.GetValueAsLong ();
12784                         if (value > 32767 || value < -32767) {
12785                                 rc.Report.Warning (8094, 1, Alignment.Location, 
12786                                         "Alignment value has a magnitude greater than 32767 and may result in a large formatted string");
12787                         }
12788
12789                         return value;
12790                 }
12791         }
12792
12793         class ThrowExpression : ExpressionStatement
12794         {
12795                 Expression expr;
12796
12797                 public ThrowExpression (Expression expr, Location loc)
12798                 {
12799                         this.expr = expr;
12800                         this.loc = loc;
12801                 }
12802
12803                 protected override void CloneTo (CloneContext clonectx, Expression t)
12804                 {
12805                         var target = (ThrowExpression)t;
12806                         target.expr = expr.Clone (clonectx);
12807                 }
12808
12809                 public override bool ContainsEmitWithAwait ()
12810                 {
12811                         return expr.ContainsEmitWithAwait ();
12812                 }
12813
12814                 public override Expression CreateExpressionTree (ResolveContext rc)
12815                 {
12816                         rc.Report.Error (8188, loc, "An expression tree cannot not contain a throw expression");
12817                         return expr;
12818                 }
12819
12820                 protected override Expression DoResolve (ResolveContext rc)
12821                 {
12822                         expr = expr.Resolve (rc, ResolveFlags.Type | ResolveFlags.VariableOrValue);
12823
12824                         if (expr == null)
12825                                 return null;
12826
12827                         expr = Throw.ConvertType (rc, expr);
12828
12829                         eclass = ExprClass.Value;
12830                         type = InternalType.ThrowExpr;
12831                         return this;
12832                 }
12833
12834                 public override void Emit (EmitContext ec)
12835                 {
12836                         EmitStatement (ec);
12837                 }
12838
12839                 public override void EmitStatement (EmitContext ec)
12840                 {
12841                         expr.Emit (ec);
12842
12843                         ec.Emit (OpCodes.Throw);
12844                 }
12845
12846                 public override void FlowAnalysis (FlowAnalysisContext fc)
12847                 {
12848                         expr.FlowAnalysis (fc);
12849                 }
12850
12851                 public override Reachability MarkReachable (Reachability rc)
12852                 {
12853                         return Reachability.CreateUnreachable ();
12854                 }
12855         }
12856 }