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