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