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