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