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