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