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