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