234cf58b6c47730931ca7e1cb9524b91c19d5e11
[mono.git] / mcs / mcs / expression.cs
1 //
2 // expression.cs: Expression representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10
11 namespace CIR {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
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                 StaticCallExpr (MethodInfo m, ArrayList a)
29                 {
30                         mi = m;
31                         args = a;
32
33                         type = m.ReturnType;
34                         eclass = ExprClass.Value;
35                 }
36
37                 public override Expression DoResolve (EmitContext ec)
38                 {
39                         //
40                         // We are born fully resolved
41                         //
42                         return this;
43                 }
44
45                 public override void Emit (EmitContext ec)
46                 {
47                         if (args != null) 
48                                 Invocation.EmitArguments (ec, mi, args);
49
50                         ec.ig.Emit (OpCodes.Call, mi);
51                         return;
52                 }
53                 
54                 static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
55                                                          Expression e, Location loc)
56                 {
57                         ArrayList args;
58                         MethodBase method;
59                         
60                         args = new ArrayList (1);
61                         args.Add (new Argument (e, Argument.AType.Expression));
62                         method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
63
64                         if (method == null)
65                                 return null;
66
67                         return new StaticCallExpr ((MethodInfo) method, args);
68                 }
69
70                 public override void EmitStatement (EmitContext ec)
71                 {
72                         Emit (ec);
73                         if (type != TypeManager.void_type)
74                                 ec.ig.Emit (OpCodes.Pop);
75                 }
76         }
77         
78         // <summary>
79         //   Unary expressions.  
80         // </summary>
81         //
82         // <remarks>
83         //   Unary implements unary expressions.   It derives from
84         //   ExpressionStatement becuase the pre/post increment/decrement
85         //   operators can be used in a statement context.
86         // </remarks>
87         public class Unary : Expression {
88                 public enum Operator : byte {
89                         UnaryPlus, UnaryNegation, LogicalNot, OnesComplement,
90                         Indirection, AddressOf, 
91                 }
92
93                 Operator   oper;
94                 Expression expr;
95                 Location   loc;
96                 
97                 public Unary (Operator op, Expression expr, Location loc)
98                 {
99                         this.oper = op;
100                         this.expr = expr;
101                         this.loc = loc;
102                 }
103
104                 public Expression Expr {
105                         get {
106                                 return expr;
107                         }
108
109                         set {
110                                 expr = value;
111                         }
112                 }
113
114                 public Operator Oper {
115                         get {
116                                 return oper;
117                         }
118
119                         set {
120                                 oper = value;
121                         }
122                 }
123
124                 // <summary>
125                 //   Returns a stringified representation of the Operator
126                 // </summary>
127                 string OperName ()
128                 {
129                         switch (oper){
130                         case Operator.UnaryPlus:
131                                 return "+";
132                         case Operator.UnaryNegation:
133                                 return "-";
134                         case Operator.LogicalNot:
135                                 return "!";
136                         case Operator.OnesComplement:
137                                 return "~";
138                         case Operator.AddressOf:
139                                 return "&";
140                         case Operator.Indirection:
141                                 return "*";
142                         }
143
144                         return oper.ToString ();
145                 }
146
147                 void error23 (Type t)
148                 {
149                         Report.Error (
150                                 23, loc, "Operator " + OperName () +
151                                 " cannot be applied to operand of type `" +
152                                 TypeManager.CSharpName (t) + "'");
153                 }
154
155                 static Expression TryReduceNegative (Expression expr)
156                 {
157                         Expression e = null;
158                         
159                         if (expr is IntLiteral)
160                                 e = new IntLiteral (-((IntLiteral) expr).Value);
161                         else if (expr is UIntLiteral)
162                                 e = new LongLiteral (-((UIntLiteral) expr).Value);
163                         else if (expr is LongLiteral)
164                                 e = new LongLiteral (-((LongLiteral) expr).Value);
165                         else if (expr is FloatLiteral)
166                                 e = new FloatLiteral (-((FloatLiteral) expr).Value);
167                         else if (expr is DoubleLiteral)
168                                 e = new DoubleLiteral (-((DoubleLiteral) expr).Value);
169                         else if (expr is DecimalLiteral)
170                                 e = new DecimalLiteral (-((DecimalLiteral) expr).Value);
171
172                         return e;
173                 }
174                 
175                 Expression ResolveOperator (EmitContext ec)
176                 {
177                         Type expr_type = expr.Type;
178
179                         //
180                         // Step 1: Perform Operator Overload location
181                         //
182                         Expression mg;
183                         string op_name;
184                         
185                         op_name = "op_" + oper;
186
187                         mg = MemberLookup (ec, expr_type, op_name, false, loc);
188                         
189                         if (mg == null && expr_type.BaseType != null)
190                                 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
191                         
192                         if (mg != null) {
193                                 Expression e = StaticCallExpr.MakeSimpleCall (
194                                         ec, (MethodGroupExpr) mg, expr, loc);
195
196                                 if (e == null){
197                                         error23 (expr_type);
198                                         return null;
199                                 }
200                                 
201                                 return e;
202                         }
203
204                         //
205                         // Step 2: Default operations on CLI native types.
206                         //
207
208                         // Only perform numeric promotions on:
209                         // +, - 
210
211                         if (expr_type == null)
212                                 return null;
213                         
214                         if (oper == Operator.LogicalNot){
215                                 if (expr_type != TypeManager.bool_type) {
216                                         error23 (expr.Type);
217                                         return null;
218                                 }
219                                 
220                                 type = TypeManager.bool_type;
221                                 return this;
222                         }
223
224                         if (oper == Operator.OnesComplement) {
225                                 if (!((expr_type == TypeManager.int32_type) ||
226                                       (expr_type == TypeManager.uint32_type) ||
227                                       (expr_type == TypeManager.int64_type) ||
228                                       (expr_type == TypeManager.uint64_type) ||
229                                       (expr_type.IsSubclassOf (TypeManager.enum_type)))){
230                                         error23 (expr.Type);
231                                         return null;
232                                 }
233                                 type = expr_type;
234                                 return this;
235                         }
236
237                         if (oper == Operator.UnaryPlus) {
238                                 //
239                                 // A plus in front of something is just a no-op, so return the child.
240                                 //
241                                 return expr;
242                         }
243
244                         //
245                         // Deals with -literals
246                         // int     operator- (int x)
247                         // long    operator- (long x)
248                         // float   operator- (float f)
249                         // double  operator- (double d)
250                         // decimal operator- (decimal d)
251                         //
252                         if (oper == Operator.UnaryNegation){
253                                 //
254                                 // Fold a "- Constant" into a negative constant
255                                 //
256                         
257                                 Expression e = null;
258
259                                 //
260                                 // Is this a constant? 
261                                 //
262                                 e = TryReduceNegative (expr);
263                                 
264                                 if (e != null){
265                                         e = e.Resolve (ec);
266                                         return e;
267                                 }
268
269                                 //
270                                 // Not a constant we can optimize, perform numeric 
271                                 // promotions to int, long, double.
272                                 //
273                                 //
274                                 // The following is inneficient, because we call
275                                 // ConvertImplicit too many times.
276                                 //
277                                 // It is also not clear if we should convert to Float
278                                 // or Double initially.
279                                 //
280                                 if (expr_type == TypeManager.uint32_type){
281                                         //
282                                         // FIXME: handle exception to this rule that
283                                         // permits the int value -2147483648 (-2^31) to
284                                         // bt written as a decimal interger literal
285                                         //
286                                         type = TypeManager.int64_type;
287                                         expr = ConvertImplicit (ec, expr, type, loc);
288                                         return this;
289                                 }
290
291                                 if (expr_type == TypeManager.uint64_type){
292                                         //
293                                         // FIXME: Handle exception of `long value'
294                                         // -92233720368547758087 (-2^63) to be written as
295                                         // decimal integer literal.
296                                         //
297                                         error23 (expr_type);
298                                         return null;
299                                 }
300
301                                 e = ConvertImplicit (ec, expr, TypeManager.int32_type, loc);
302                                 if (e != null){
303                                         expr = e;
304                                         type = e.Type;
305                                         return this;
306                                 } 
307
308                                 e = ConvertImplicit (ec, expr, TypeManager.int64_type, loc);
309                                 if (e != null){
310                                         expr = e;
311                                         type = e.Type;
312                                         return this;
313                                 }
314
315                                 e = ConvertImplicit (ec, expr, TypeManager.double_type, loc);
316                                 if (e != null){
317                                         expr = e;
318                                         type = e.Type;
319                                         return this;
320                                 }
321
322                                 error23 (expr_type);
323                                 return null;
324                         }
325
326                         if (oper == Operator.AddressOf){
327                                 if (expr.ExprClass != ExprClass.Variable){
328                                         Error (211, loc, "Cannot take the address of non-variables");
329                                         return null;
330                                 }
331                                 type = Type.GetType (expr.Type.ToString () + "*");
332
333                                 return this;
334                         }
335                         
336                         Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
337                                TypeManager.CSharpName (expr_type) + "'");
338                         return null;
339                 }
340
341                 public override Expression DoResolve (EmitContext ec)
342                 {
343                         expr = expr.Resolve (ec);
344                         
345                         if (expr == null)
346                                 return null;
347
348                         eclass = ExprClass.Value;
349                         return ResolveOperator (ec);
350                 }
351
352                 public override void Emit (EmitContext ec)
353                 {
354                         ILGenerator ig = ec.ig;
355                         Type expr_type = expr.Type;
356                         
357                         switch (oper) {
358                         case Operator.UnaryPlus:
359                                 throw new Exception ("This should be caught by Resolve");
360                                 
361                         case Operator.UnaryNegation:
362                                 expr.Emit (ec);
363                                 ig.Emit (OpCodes.Neg);
364                                 break;
365                                 
366                         case Operator.LogicalNot:
367                                 expr.Emit (ec);
368                                 ig.Emit (OpCodes.Ldc_I4_0);
369                                 ig.Emit (OpCodes.Ceq);
370                                 break;
371                                 
372                         case Operator.OnesComplement:
373                                 expr.Emit (ec);
374                                 ig.Emit (OpCodes.Not);
375                                 break;
376                                 
377                         case Operator.AddressOf:
378                                 ((IMemoryLocation)expr).AddressOf (ec);
379                                 break;
380                                 
381                         case Operator.Indirection:
382                                 throw new Exception ("Not implemented yet");
383                                 
384                         default:
385                                 throw new Exception ("This should not happen: Operator = "
386                                                      + oper.ToString ());
387                         }
388                 }
389
390                 // <summary>
391                 //   This will emit the child expression for `ec' avoiding the logical
392                 //   not.  The parent will take care of changing brfalse/brtrue
393                 // </summary>
394                 public void EmitLogicalNot (EmitContext ec)
395                 {
396                         if (oper != Operator.LogicalNot)
397                                 throw new Exception ("EmitLogicalNot can only be called with !expr");
398
399                         expr.Emit (ec);
400                 }
401                 
402                 public override Expression Reduce (EmitContext ec)
403                 {
404                         Expression e;
405                         
406                         //
407                         // First, reduce our child.  Note that although we handle 
408                         //
409                         expr = expr.Reduce (ec);
410                         if (!(expr is Literal))
411                                 return expr;
412                         
413                         switch (oper){
414                         case Operator.UnaryPlus:
415                                 return expr;
416                                 
417                         case Operator.UnaryNegation:
418                                 e = TryReduceNegative (expr);
419                                 if (e == null)
420                                         break;
421                                 return e;
422                                 
423                         case Operator.LogicalNot:
424                                 BoolLiteral b = (BoolLiteral) expr;
425
426                                 return new BoolLiteral (!(b.Value));
427                                 
428                         case Operator.OnesComplement:
429                                 Type et = expr.Type;
430                                 
431                                 if (et == TypeManager.int32_type)
432                                         return new IntLiteral (~ ((IntLiteral) expr).Value);
433                                 if (et == TypeManager.uint32_type)
434                                         return new UIntLiteral (~ ((UIntLiteral) expr).Value);
435                                 if (et == TypeManager.int64_type)
436                                         return new LongLiteral (~ ((LongLiteral) expr).Value);
437                                 if (et == TypeManager.uint64_type)
438                                         return new ULongLiteral (~ ((ULongLiteral) expr).Value);
439                                 break;
440                         }
441                         return this;
442                 }
443         }
444
445         // <summary>
446         //   Unary Mutator expressions (pre and post ++ and --)
447         // </summary>
448         //
449         // <remarks>
450         //   UnaryMutator implements ++ and -- expressions.   It derives from
451         //   ExpressionStatement becuase the pre/post increment/decrement
452         //   operators can be used in a statement context.
453         // </remarks>
454         //
455         // FIXME: Idea, we could split this up in two classes, one simpler
456         // for the common case, and one with the extra fields for more complex
457         // classes (indexers require temporary access;  overloaded require method)
458         //
459         // Maybe we should have classes PreIncrement, PostIncrement, PreDecrement,
460         // PostDecrement, that way we could save the `Mode' byte as well.  
461         //
462         public class UnaryMutator : ExpressionStatement {
463                 public enum Mode : byte {
464                         PreIncrement, PreDecrement, PostIncrement, PostDecrement
465                 }
466                 
467                 Mode mode;
468                 Location loc;
469                 Expression expr;
470                 LocalTemporary temp_storage;
471
472                 //
473                 // This is expensive for the simplest case.
474                 //
475                 Expression method;
476                         
477                 public UnaryMutator (Mode m, Expression e, Location l)
478                 {
479                         mode = m;
480                         loc = l;
481                         expr = e;
482                 }
483
484                 string OperName ()
485                 {
486                         return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
487                                 "++" : "--";
488                 }
489                 
490                 void error23 (Type t)
491                 {
492                         Report.Error (
493                                 23, loc, "Operator " + OperName () + 
494                                 " cannot be applied to operand of type `" +
495                                 TypeManager.CSharpName (t) + "'");
496                 }
497
498                 // <summary>
499                 //   Returns whether an object of type `t' can be incremented
500                 //   or decremented with add/sub (ie, basically whether we can
501                 //   use pre-post incr-decr operations on it, but it is not a
502                 //   System.Decimal, which we require operator overloading to catch)
503                 // </summary>
504                 static bool IsIncrementableNumber (Type t)
505                 {
506                         return (t == TypeManager.sbyte_type) ||
507                                 (t == TypeManager.byte_type) ||
508                                 (t == TypeManager.short_type) ||
509                                 (t == TypeManager.ushort_type) ||
510                                 (t == TypeManager.int32_type) ||
511                                 (t == TypeManager.uint32_type) ||
512                                 (t == TypeManager.int64_type) ||
513                                 (t == TypeManager.uint64_type) ||
514                                 (t == TypeManager.char_type) ||
515                                 (t.IsSubclassOf (TypeManager.enum_type)) ||
516                                 (t == TypeManager.float_type) ||
517                                 (t == TypeManager.double_type);
518                 }
519
520                 Expression ResolveOperator (EmitContext ec)
521                 {
522                         Type expr_type = expr.Type;
523
524                         //
525                         // Step 1: Perform Operator Overload location
526                         //
527                         Expression mg;
528                         string op_name;
529                         
530                         if (mode == Mode.PreIncrement || mode == Mode.PostIncrement)
531                                 op_name = "op_Increment";
532                         else 
533                                 op_name = "op_Decrement";
534
535                         mg = MemberLookup (ec, expr_type, op_name, false, loc);
536
537                         if (mg == null && expr_type.BaseType != null)
538                                 mg = MemberLookup (ec, expr_type.BaseType, op_name, false, loc);
539                         
540                         if (mg != null) {
541                                 method = StaticCallExpr.MakeSimpleCall (
542                                         ec, (MethodGroupExpr) mg, expr, loc);
543
544                                 type = method.Type;
545                                 return this;
546                         }
547
548                         //
549                         // The operand of the prefix/postfix increment decrement operators
550                         // should be an expression that is classified as a variable,
551                         // a property access or an indexer access
552                         //
553                         type = expr_type;
554                         if (expr.ExprClass == ExprClass.Variable){
555                                 if (IsIncrementableNumber (expr_type) ||
556                                     expr_type == TypeManager.decimal_type){
557                                         return this;
558                                 }
559                         } else if (expr.ExprClass == ExprClass.IndexerAccess){
560                                 IndexerAccess ia = (IndexerAccess) expr;
561                                 
562                                 temp_storage = new LocalTemporary (ec, expr.Type);
563                                 
564                                 expr = ia.ResolveLValue (ec, temp_storage);
565                                 if (expr == null)
566                                         return null;
567
568                                 return this;
569                         } else if (expr.ExprClass == ExprClass.PropertyAccess){
570                                 PropertyExpr pe = (PropertyExpr) expr;
571
572                                 if (pe.VerifyAssignable ())
573                                         return this;
574
575                                 return null;
576                         } else {
577                                 report118 (loc, expr, "variable, indexer or property access");
578                                 return null;
579                         }
580
581                         Error (187, loc, "No such operator '" + OperName () + "' defined for type '" +
582                                TypeManager.CSharpName (expr_type) + "'");
583                         return null;
584                 }
585
586                 public override Expression DoResolve (EmitContext ec)
587                 {
588                         expr = expr.Resolve (ec);
589                         
590                         if (expr == null)
591                                 return null;
592
593                         eclass = ExprClass.Value;
594                         return ResolveOperator (ec);
595                 }
596                 
597
598                 //
599                 // FIXME: We need some way of avoiding the use of temp_storage
600                 // for some types of storage (parameters, local variables,
601                 // static fields) and single-dimension array access.
602                 //
603                 void EmitCode (EmitContext ec, bool is_expr)
604                 {
605                         ILGenerator ig = ec.ig;
606                         IAssignMethod ia = (IAssignMethod) expr;
607
608                         if (temp_storage == null)
609                                 temp_storage = new LocalTemporary (ec, expr.Type);
610                         
611                         switch (mode){
612                         case Mode.PreIncrement:
613                         case Mode.PreDecrement:
614                                 if (method == null){
615                                         expr.Emit (ec);
616
617                                         ig.Emit (OpCodes.Ldc_I4_1);
618                                 
619                                         if (mode == Mode.PreDecrement)
620                                                 ig.Emit (OpCodes.Sub);
621                                         else
622                                                 ig.Emit (OpCodes.Add);
623                                 } else
624                                         method.Emit (ec);
625                                 
626                                 temp_storage.Store (ec);
627                                 ia.EmitAssign (ec, temp_storage);
628                                 if (is_expr)
629                                         temp_storage.Emit (ec);
630                                 break;
631                                 
632                         case Mode.PostIncrement:
633                         case Mode.PostDecrement:
634                                 if (is_expr)
635                                         expr.Emit (ec);
636                                 
637                                 if (method == null){
638                                         if (!is_expr)
639                                                 expr.Emit (ec);
640                                         else
641                                                 ig.Emit (OpCodes.Dup);
642
643                                         ig.Emit (OpCodes.Ldc_I4_1);
644                                 
645                                         if (mode == Mode.PostDecrement)
646                                                 ig.Emit (OpCodes.Sub);
647                                         else
648                                                 ig.Emit (OpCodes.Add);
649                                 } else {
650                                         method.Emit (ec);
651                                 }
652                                 
653                                 temp_storage.Store (ec);
654                                 ia.EmitAssign (ec, temp_storage);
655                                 break;
656                         }
657                 }
658
659                 public override void Emit (EmitContext ec)
660                 {
661                         EmitCode (ec, true);
662                         
663                 }
664                 
665                 public override void EmitStatement (EmitContext ec)
666                 {
667                         EmitCode (ec, false);
668                 }
669
670         }
671         
672         public class Probe : Expression {
673                 public readonly string ProbeType;
674                 public readonly Operator Oper;
675                 Expression expr;
676                 Type probe_type;
677                 
678                 public enum Operator : byte {
679                         Is, As
680                 }
681                 
682                 public Probe (Operator oper, Expression expr, string probe_type)
683                 {
684                         Oper = oper;
685                         ProbeType = probe_type;
686                         this.expr = expr;
687                 }
688
689                 public Expression Expr {
690                         get {
691                                 return expr;
692                         }
693                 }
694                 
695                 public override Expression DoResolve (EmitContext ec)
696                 {
697                         probe_type = ec.TypeContainer.LookupType (ProbeType, false);
698
699                         if (probe_type == null)
700                                 return null;
701
702                         expr = expr.Resolve (ec);
703                         
704                         type = TypeManager.bool_type;
705                         eclass = ExprClass.Value;
706
707                         return this;
708                 }
709
710                 public override void Emit (EmitContext ec)
711                 {
712                         ILGenerator ig = ec.ig;
713                         
714                         expr.Emit (ec);
715                         
716                         if (Oper == Operator.Is){
717                                 ig.Emit (OpCodes.Isinst, probe_type);
718                                 ig.Emit (OpCodes.Ldnull);
719                                 ig.Emit (OpCodes.Cgt_Un);
720                         } else {
721                                 ig.Emit (OpCodes.Isinst, probe_type);
722                         }
723                 }
724         }
725
726         // <summary>
727         //   This represents a typecast in the source language.
728         //
729         //   FIXME: Cast expressions have an unusual set of parsing
730         //   rules, we need to figure those out.
731         // </summary>
732         public class Cast : Expression {
733                 string target_type;
734                 Expression expr;
735                 Location   loc;
736                         
737                 public Cast (string cast_type, Expression expr, Location loc)
738                 {
739                         this.target_type = cast_type;
740                         this.expr = expr;
741                         this.loc = loc;
742                 }
743
744                 public string TargetType {
745                         get {
746                                 return target_type;
747                         }
748                 }
749
750                 public Expression Expr {
751                         get {
752                                 return expr;
753                         }
754                         set {
755                                 expr = value;
756                         }
757                 }
758                 
759                 public override Expression DoResolve (EmitContext ec)
760                 {
761                         expr = expr.Resolve (ec);
762                         if (expr == null)
763                                 return null;
764                         
765                         type = ec.TypeContainer.LookupType (target_type, false);
766                         eclass = ExprClass.Value;
767                         
768                         if (type == null)
769                                 return null;
770
771                         expr = ConvertExplicit (ec, expr, type, loc);
772
773                         return expr;
774                 }
775
776                 public override void Emit (EmitContext ec)
777                 {
778                         //
779                         // This one will never happen
780                         //
781                         throw new Exception ("Should not happen");
782                 }
783         }
784
785         public class Binary : Expression {
786                 public enum Operator : byte {
787                         Multiply, Division, Modulus,
788                         Addition, Subtraction,
789                         LeftShift, RightShift,
790                         LessThan, GreaterThan, LessThanOrEqual, GreaterThanOrEqual, 
791                         Equality, Inequality,
792                         BitwiseAnd,
793                         ExclusiveOr,
794                         BitwiseOr,
795                         LogicalAnd,
796                         LogicalOr
797                 }
798
799                 Operator oper;
800                 Expression left, right;
801                 MethodBase method;
802                 ArrayList  Arguments;
803                 Location   loc;
804                 
805
806                 public Binary (Operator oper, Expression left, Expression right, Location loc)
807                 {
808                         this.oper = oper;
809                         this.left = left;
810                         this.right = right;
811                         this.loc = loc;
812                 }
813
814                 public Operator Oper {
815                         get {
816                                 return oper;
817                         }
818                         set {
819                                 oper = value;
820                         }
821                 }
822                 
823                 public Expression Left {
824                         get {
825                                 return left;
826                         }
827                         set {
828                                 left = value;
829                         }
830                 }
831
832                 public Expression Right {
833                         get {
834                                 return right;
835                         }
836                         set {
837                                 right = value;
838                         }
839                 }
840
841
842                 // <summary>
843                 //   Returns a stringified representation of the Operator
844                 // </summary>
845                 string OperName ()
846                 {
847                         switch (oper){
848                         case Operator.Multiply:
849                                 return "*";
850                         case Operator.Division:
851                                 return "/";
852                         case Operator.Modulus:
853                                 return "%";
854                         case Operator.Addition:
855                                 return "+";
856                         case Operator.Subtraction:
857                                 return "-";
858                         case Operator.LeftShift:
859                                 return "<<";
860                         case Operator.RightShift:
861                                 return ">>";
862                         case Operator.LessThan:
863                                 return "<";
864                         case Operator.GreaterThan:
865                                 return ">";
866                         case Operator.LessThanOrEqual:
867                                 return "<=";
868                         case Operator.GreaterThanOrEqual:
869                                 return ">=";
870                         case Operator.Equality:
871                                 return "==";
872                         case Operator.Inequality:
873                                 return "!=";
874                         case Operator.BitwiseAnd:
875                                 return "&";
876                         case Operator.BitwiseOr:
877                                 return "|";
878                         case Operator.ExclusiveOr:
879                                 return "^";
880                         case Operator.LogicalOr:
881                                 return "||";
882                         case Operator.LogicalAnd:
883                                 return "&&";
884                         }
885
886                         return oper.ToString ();
887                 }
888
889                 Expression ForceConversion (EmitContext ec, Expression expr, Type target_type)
890                 {
891                         if (expr.Type == target_type)
892                                 return expr;
893
894                         return ConvertImplicit (ec, expr, target_type, new Location (-1));
895                 }
896                 
897                 //
898                 // Note that handling the case l == Decimal || r == Decimal
899                 // is taken care of by the Step 1 Operator Overload resolution.
900                 //
901                 bool DoNumericPromotions (EmitContext ec, Type l, Type r)
902                 {
903                         if (l == TypeManager.double_type || r == TypeManager.double_type){
904                                 //
905                                 // If either operand is of type double, the other operand is
906                                 // conveted to type double.
907                                 //
908                                 if (r != TypeManager.double_type)
909                                         right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
910                                 if (l != TypeManager.double_type)
911                                         left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
912                                 
913                                 type = TypeManager.double_type;
914                         } else if (l == TypeManager.float_type || r == TypeManager.float_type){
915                                 //
916                                 // if either operand is of type float, th eother operand is
917                                 // converd to type float.
918                                 //
919                                 if (r != TypeManager.double_type)
920                                         right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
921                                 if (l != TypeManager.double_type)
922                                         left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
923                                 type = TypeManager.float_type;
924                         } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
925                                 Expression e;
926                                 Type other;
927                                 //
928                                 // If either operand is of type ulong, the other operand is
929                                 // converted to type ulong.  or an error ocurrs if the other
930                                 // operand is of type sbyte, short, int or long
931                                 //
932                                 
933                                 if (l == TypeManager.uint64_type){
934                                         if (r != TypeManager.uint64_type && right is IntLiteral){
935                                                 e = TryImplicitIntConversion (l, (IntLiteral) right);
936                                                 if (e != null)
937                                                         right = e;
938                                         }
939                                         other = right.Type;
940                                 } else {
941                                         if (left is IntLiteral){
942                                                 e = TryImplicitIntConversion (r, (IntLiteral) left);
943                                                 if (e != null)
944                                                         left = e;
945                                         }
946                                         other = left.Type;
947                                 }
948
949                                 if ((other == TypeManager.sbyte_type) ||
950                                     (other == TypeManager.short_type) ||
951                                     (other == TypeManager.int32_type) ||
952                                     (other == TypeManager.int64_type)){
953                                         string oper = OperName ();
954                                         
955                                         Error (34, loc, "Operator `" + OperName ()
956                                                + "' is ambiguous on operands of type `"
957                                                + TypeManager.CSharpName (l) + "' "
958                                                + "and `" + TypeManager.CSharpName (r)
959                                                + "'");
960                                 }
961                                 type = TypeManager.uint64_type;
962                         } else if (l == TypeManager.int64_type || r == TypeManager.int64_type){
963                                 //
964                                 // If either operand is of type long, the other operand is converted
965                                 // to type long.
966                                 //
967                                 if (l != TypeManager.int64_type)
968                                         left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
969                                 if (r != TypeManager.int64_type)
970                                         right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
971
972                                 type = TypeManager.int64_type;
973                         } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
974                                 //
975                                 // If either operand is of type uint, and the other
976                                 // operand is of type sbyte, short or int, othe operands are
977                                 // converted to type long.
978                                 //
979                                 Type other = null;
980                                 
981                                 if (l == TypeManager.uint32_type)
982                                         other = r;
983                                 else if (r == TypeManager.uint32_type)
984                                         other = l;
985
986                                 if ((other == TypeManager.sbyte_type) ||
987                                     (other == TypeManager.short_type) ||
988                                     (other == TypeManager.int32_type)){
989                                         left = ForceConversion (ec, left, TypeManager.int64_type);
990                                         right = ForceConversion (ec, right, TypeManager.int64_type);
991                                         type = TypeManager.int64_type;
992                                 } else {
993                                         //
994                                         // if either operand is of type uint, the other
995                                         // operand is converd to type uint
996                                         //
997                                         left = ForceConversion (ec, left, TypeManager.uint32_type);
998                                         right = ForceConversion (ec, right, TypeManager.uint32_type);
999                                         type = TypeManager.uint32_type;
1000                                 } 
1001                         } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
1002                                 if (l != TypeManager.decimal_type)
1003                                         left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
1004                                 if (r != TypeManager.decimal_type)
1005                                         right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
1006
1007                                 type = TypeManager.decimal_type;
1008                         } else {
1009                                 Expression l_tmp, r_tmp;
1010
1011                                 l_tmp = ForceConversion (ec, left, TypeManager.int32_type);
1012                                 if (l_tmp == null)
1013                                         return false;
1014                                 
1015                                 r_tmp = ForceConversion (ec, right, TypeManager.int32_type);
1016                                 if (r_tmp == null)
1017                                         return false;
1018
1019                                 left = l_tmp;
1020                                 right = r_tmp;
1021                                 
1022                                 type = TypeManager.int32_type;
1023                         }
1024
1025                         return true;
1026                 }
1027
1028                 void error19 ()
1029                 {
1030                         Error (19, loc,
1031                                "Operator " + OperName () + " cannot be applied to operands of type `" +
1032                                TypeManager.CSharpName (left.Type) + "' and `" +
1033                                TypeManager.CSharpName (right.Type) + "'");
1034                                                      
1035                 }
1036                 
1037                 Expression CheckShiftArguments (EmitContext ec)
1038                 {
1039                         Expression e;
1040                         Type l = left.Type;
1041                         Type r = right.Type;
1042
1043                         e = ForceConversion (ec, right, TypeManager.int32_type);
1044                         if (e == null){
1045                                 error19 ();
1046                                 return null;
1047                         }
1048                         right = e;
1049
1050                         if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
1051                             ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
1052                             ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
1053                             ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
1054                                 left = e;
1055                                 type = e.Type;
1056
1057                                 return this;
1058                         }
1059                         error19 ();
1060                         return null;
1061                 }
1062                 
1063                 Expression ResolveOperator (EmitContext ec)
1064                 {
1065                         Type l = left.Type;
1066                         Type r = right.Type;
1067
1068                         //
1069                         // Step 1: Perform Operator Overload location
1070                         //
1071                         Expression left_expr, right_expr;
1072                         
1073                         string op = "op_" + oper;
1074
1075                         left_expr = MemberLookup (ec, l, op, false, loc);
1076                         if (left_expr == null && l.BaseType != null)
1077                                 left_expr = MemberLookup (ec, l.BaseType, op, false, loc);
1078                         
1079                         right_expr = MemberLookup (ec, r, op, false, loc);
1080                         if (right_expr == null && r.BaseType != null)
1081                                 right_expr = MemberLookup (ec, r.BaseType, op, false, loc);
1082                         
1083                         MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
1084                         
1085                         if (union != null) {
1086                                 Arguments = new ArrayList ();
1087                                 Arguments.Add (new Argument (left, Argument.AType.Expression));
1088                                 Arguments.Add (new Argument (right, Argument.AType.Expression));
1089
1090                                 method = Invocation.OverloadResolve (ec, union, Arguments, loc);
1091                                 if (method != null) {
1092                                         MethodInfo mi = (MethodInfo) method;
1093                                         type = mi.ReturnType;
1094                                         return this;
1095                                 } else {
1096                                         error19 ();
1097                                         return null;
1098                                 }
1099                         }       
1100
1101                         //
1102                         // Step 2: Default operations on CLI native types.
1103                         //
1104                         
1105                         // Only perform numeric promotions on:
1106                         // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
1107                         //
1108                         if (oper == Operator.Addition){
1109                                 //
1110                                 // If any of the arguments is a string, cast to string
1111                                 //
1112                                 if (l == TypeManager.string_type){
1113                                         if (r == TypeManager.string_type){
1114                                                 if (left is Literal && right is Literal){
1115                                                         StringLiteral ls = (StringLiteral) left;
1116                                                         StringLiteral rs = (StringLiteral) right;
1117                                                         
1118                                                         return new StringLiteral (ls.Value + rs.Value);
1119                                                 }
1120                                                 
1121                                                 // string + string
1122                                                 method = TypeManager.string_concat_string_string;
1123                                         } else {
1124                                                 // string + object
1125                                                 method = TypeManager.string_concat_object_object;
1126                                                 right = ConvertImplicit (ec, right,
1127                                                                          TypeManager.object_type, loc);
1128                                         }
1129                                         type = TypeManager.string_type;
1130
1131                                         Arguments = new ArrayList ();
1132                                         Arguments.Add (new Argument (left, Argument.AType.Expression));
1133                                         Arguments.Add (new Argument (right, Argument.AType.Expression));
1134
1135                                         return this;
1136                                         
1137                                 } else if (r == TypeManager.string_type){
1138                                         // object + string
1139                                         method = TypeManager.string_concat_object_object;
1140                                         Arguments = new ArrayList ();
1141                                         Arguments.Add (new Argument (left, Argument.AType.Expression));
1142                                         Arguments.Add (new Argument (right, Argument.AType.Expression));
1143
1144                                         left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1145                                         type = TypeManager.string_type;
1146
1147                                         return this;
1148                                 }
1149
1150                                 //
1151                                 // FIXME: is Delegate operator + (D x, D y) handled?
1152                                 //
1153                         }
1154                         
1155                         if (oper == Operator.LeftShift || oper == Operator.RightShift)
1156                                 return CheckShiftArguments (ec);
1157
1158                         if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
1159                                 if (l != TypeManager.bool_type || r != TypeManager.bool_type){
1160                                         error19 ();
1161                                         return null;
1162                                 }
1163
1164                                 type = TypeManager.bool_type;
1165                                 return this;
1166                         } 
1167
1168                         if (oper == Operator.Equality || oper == Operator.Inequality){
1169                                 if (l == TypeManager.bool_type || r == TypeManager.bool_type){
1170                                         if (r != TypeManager.bool_type || l != TypeManager.bool_type){
1171                                                 error19 ();
1172                                                 return null;
1173                                         }
1174                                         
1175                                         type = TypeManager.bool_type;
1176                                         return this;
1177                                 }
1178
1179                         }
1180
1181                         //
1182                         // We are dealing with numbers
1183                         //
1184
1185                         if (!DoNumericPromotions (ec, l, r)){
1186                                 // Attempt:
1187                                 //
1188                                 // operator != (object a, object b)
1189                                 // operator == (object a, object b)
1190                                 //
1191
1192                                 if (oper == Operator.Equality || oper == Operator.Inequality){
1193                                         Expression li, ri;
1194                                         li = ConvertImplicit (ec, left, TypeManager.object_type, loc);
1195                                         if (li != null){
1196                                                 ri = ConvertImplicit (ec, right, TypeManager.object_type,
1197                                                                       loc);
1198                                                 if (ri != null){
1199                                                         left = li;
1200                                                         right = ri;
1201                                                         
1202                                                         type = TypeManager.bool_type;
1203                                                         return this;
1204                                                 }
1205                                         }
1206                                 }
1207
1208                                 error19 ();
1209                                 return null;
1210                         }
1211
1212                         if (left == null || right == null)
1213                                 return null;
1214
1215                         
1216                         if (oper == Operator.BitwiseAnd ||
1217                             oper == Operator.BitwiseOr ||
1218                             oper == Operator.ExclusiveOr){
1219                                 if (!((l == TypeManager.int32_type) ||
1220                                       (l == TypeManager.uint32_type) ||
1221                                       (l == TypeManager.int64_type) ||
1222                                       (l == TypeManager.uint64_type))){
1223                                         error19 ();
1224                                         return null;
1225                                 }
1226                                 type = l;
1227                         }
1228
1229                         if (oper == Operator.Equality ||
1230                             oper == Operator.Inequality ||
1231                             oper == Operator.LessThanOrEqual ||
1232                             oper == Operator.LessThan ||
1233                             oper == Operator.GreaterThanOrEqual ||
1234                             oper == Operator.GreaterThan){
1235                                 type = TypeManager.bool_type;
1236                         }
1237
1238                         return this;
1239                 }
1240                 
1241                 public override Expression DoResolve (EmitContext ec)
1242                 {
1243                         left = left.Resolve (ec);
1244                         right = right.Resolve (ec);
1245
1246                         if (left == null || right == null)
1247                                 return null;
1248
1249                         if (left.Type == null)
1250                                 throw new Exception (
1251                                         "Resolve returned non null, but did not set the type! (" +
1252                                         left + ") at Line: " + loc.Row);
1253                         if (right.Type == null)
1254                                 throw new Exception (
1255                                         "Resolve returned non null, but did not set the type! (" +
1256                                         right + ") at Line: "+ loc.Row);
1257
1258                         eclass = ExprClass.Value;
1259
1260                         return ResolveOperator (ec);
1261                 }
1262
1263                 public bool IsBranchable ()
1264                 {
1265                         if (oper == Operator.Equality ||
1266                             oper == Operator.Inequality ||
1267                             oper == Operator.LessThan ||
1268                             oper == Operator.GreaterThan ||
1269                             oper == Operator.LessThanOrEqual ||
1270                             oper == Operator.GreaterThanOrEqual){
1271                                 return true;
1272                         } else
1273                                 return false;
1274                 }
1275
1276                 // <summary>
1277                 //   This entry point is used by routines that might want
1278                 //   to emit a brfalse/brtrue after an expression, and instead
1279                 //   they could use a more compact notation.
1280                 //
1281                 //   Typically the code would generate l.emit/r.emit, followed
1282                 //   by the comparission and then a brtrue/brfalse.  The comparissions
1283                 //   are sometimes inneficient (there are not as complete as the branches
1284                 //   look for the hacks in Emit using double ceqs).
1285                 //
1286                 //   So for those cases we provide EmitBranchable that can emit the
1287                 //   branch with the test
1288                 // </summary>
1289                 public void EmitBranchable (EmitContext ec, int target)
1290                 {
1291                         OpCode opcode;
1292                         bool close_target = false;
1293                         
1294                         left.Emit (ec);
1295                         right.Emit (ec);
1296                         
1297                         switch (oper){
1298                         case Operator.Equality:
1299                                 if (close_target)
1300                                         opcode = OpCodes.Beq_S;
1301                                 else
1302                                         opcode = OpCodes.Beq;
1303                                 break;
1304
1305                         case Operator.Inequality:
1306                                 if (close_target)
1307                                         opcode = OpCodes.Bne_Un_S;
1308                                 else
1309                                         opcode = OpCodes.Bne_Un;
1310                                 break;
1311
1312                         case Operator.LessThan:
1313                                 if (close_target)
1314                                         opcode = OpCodes.Blt_S;
1315                                 else
1316                                         opcode = OpCodes.Blt;
1317                                 break;
1318
1319                         case Operator.GreaterThan:
1320                                 if (close_target)
1321                                         opcode = OpCodes.Bgt_S;
1322                                 else
1323                                         opcode = OpCodes.Bgt;
1324                                 break;
1325
1326                         case Operator.LessThanOrEqual:
1327                                 if (close_target)
1328                                         opcode = OpCodes.Ble_S;
1329                                 else
1330                                         opcode = OpCodes.Ble;
1331                                 break;
1332
1333                         case Operator.GreaterThanOrEqual:
1334                                 if (close_target)
1335                                         opcode = OpCodes.Bge_S;
1336                                 else
1337                                         opcode = OpCodes.Ble;
1338                                 break;
1339
1340                         default:
1341                                 throw new Exception ("EmitBranchable called on non-EmitBranchable operator: "
1342                                                      + oper.ToString ());
1343                         }
1344
1345                         ec.ig.Emit (opcode, target);
1346                 }
1347                 
1348                 public override void Emit (EmitContext ec)
1349                 {
1350                         ILGenerator ig = ec.ig;
1351                         Type l = left.Type;
1352                         Type r = right.Type;
1353                         OpCode opcode;
1354
1355                         if (method != null) {
1356
1357                                 // Note that operators are static anyway
1358                                 
1359                                 if (Arguments != null) 
1360                                         Invocation.EmitArguments (ec, method, Arguments);
1361                                 
1362                                 if (method is MethodInfo)
1363                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
1364                                 else
1365                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
1366
1367                                 return;
1368                         }
1369                         
1370                         left.Emit (ec);
1371                         right.Emit (ec);
1372
1373                         switch (oper){
1374                         case Operator.Multiply:
1375                                 if (ec.CheckState){
1376                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1377                                                 opcode = OpCodes.Mul_Ovf;
1378                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1379                                                 opcode = OpCodes.Mul_Ovf_Un;
1380                                         else
1381                                                 opcode = OpCodes.Mul;
1382                                 } else
1383                                         opcode = OpCodes.Mul;
1384
1385                                 break;
1386
1387                         case Operator.Division:
1388                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1389                                         opcode = OpCodes.Div_Un;
1390                                 else
1391                                         opcode = OpCodes.Div;
1392                                 break;
1393
1394                         case Operator.Modulus:
1395                                 if (l == TypeManager.uint32_type || l == TypeManager.uint64_type)
1396                                         opcode = OpCodes.Rem_Un;
1397                                 else
1398                                         opcode = OpCodes.Rem;
1399                                 break;
1400
1401                         case Operator.Addition:
1402                                 if (ec.CheckState){
1403                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1404                                                 opcode = OpCodes.Add_Ovf;
1405                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1406                                                 opcode = OpCodes.Add_Ovf_Un;
1407                                         else
1408                                                 opcode = OpCodes.Mul;
1409                                 } else
1410                                         opcode = OpCodes.Add;
1411                                 break;
1412
1413                         case Operator.Subtraction:
1414                                 if (ec.CheckState){
1415                                         if (l == TypeManager.int32_type || l == TypeManager.int64_type)
1416                                                 opcode = OpCodes.Sub_Ovf;
1417                                         else if (l==TypeManager.uint32_type || l==TypeManager.uint64_type)
1418                                                 opcode = OpCodes.Sub_Ovf_Un;
1419                                         else
1420                                                 opcode = OpCodes.Sub;
1421                                 } else
1422                                         opcode = OpCodes.Sub;
1423                                 break;
1424
1425                         case Operator.RightShift:
1426                                 opcode = OpCodes.Shr;
1427                                 break;
1428                                 
1429                         case Operator.LeftShift:
1430                                 opcode = OpCodes.Shl;
1431                                 break;
1432
1433                         case Operator.Equality:
1434                                 opcode = OpCodes.Ceq;
1435                                 break;
1436
1437                         case Operator.Inequality:
1438                                 ec.ig.Emit (OpCodes.Ceq);
1439                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
1440                                 
1441                                 opcode = OpCodes.Ceq;
1442                                 break;
1443
1444                         case Operator.LessThan:
1445                                 opcode = OpCodes.Clt;
1446                                 break;
1447
1448                         case Operator.GreaterThan:
1449                                 opcode = OpCodes.Cgt;
1450                                 break;
1451
1452                         case Operator.LessThanOrEqual:
1453                                 ec.ig.Emit (OpCodes.Cgt);
1454                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
1455                                 
1456                                 opcode = OpCodes.Ceq;
1457                                 break;
1458
1459                         case Operator.GreaterThanOrEqual:
1460                                 ec.ig.Emit (OpCodes.Clt);
1461                                 ec.ig.Emit (OpCodes.Ldc_I4_1);
1462                                 
1463                                 opcode = OpCodes.Sub;
1464                                 break;
1465
1466                         case Operator.LogicalOr:
1467                         case Operator.BitwiseOr:
1468                                 opcode = OpCodes.Or;
1469                                 break;
1470
1471                         case Operator.LogicalAnd:
1472                         case Operator.BitwiseAnd:
1473                                 opcode = OpCodes.And;
1474                                 break;
1475
1476                         case Operator.ExclusiveOr:
1477                                 opcode = OpCodes.Xor;
1478                                 break;
1479
1480                         default:
1481                                 throw new Exception ("This should not happen: Operator = "
1482                                                      + oper.ToString ());
1483                         }
1484
1485                         ig.Emit (opcode);
1486                 }
1487
1488                 // <summary>
1489                 //   Constant expression reducer for binary operations
1490                 // </summary>
1491                 public override Expression Reduce (EmitContext ec)
1492                 {
1493                         
1494                         left = left.Reduce (ec);
1495                         right = right.Reduce (ec);
1496
1497                         if (!(left is Literal && right is Literal))
1498                                 return this;
1499
1500                         if (method == TypeManager.string_concat_string_string){
1501                                 StringLiteral ls = (StringLiteral) left;
1502                                 StringLiteral rs = (StringLiteral) right;
1503                                 
1504                                 return new StringLiteral (ls.Value + rs.Value);
1505                         }
1506
1507                         // FINISH ME.
1508                         
1509                         return this;
1510                 }
1511         }
1512
1513         public class Conditional : Expression {
1514                 Expression expr, trueExpr, falseExpr;
1515                 Location loc;
1516                 
1517                 public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
1518                 {
1519                         this.expr = expr;
1520                         this.trueExpr = trueExpr;
1521                         this.falseExpr = falseExpr;
1522                         this.loc = l;
1523                 }
1524
1525                 public Expression Expr {
1526                         get {
1527                                 return expr;
1528                         }
1529                 }
1530
1531                 public Expression TrueExpr {
1532                         get {
1533                                 return trueExpr;
1534                         }
1535                 }
1536
1537                 public Expression FalseExpr {
1538                         get {
1539                                 return falseExpr;
1540                         }
1541                 }
1542
1543                 public override Expression DoResolve (EmitContext ec)
1544                 {
1545                         expr = expr.Resolve (ec);
1546
1547                         if (expr.Type != TypeManager.bool_type)
1548                                 expr = Expression.ConvertImplicitRequired (
1549                                         ec, expr, TypeManager.bool_type, loc);
1550                         
1551                         trueExpr = trueExpr.Resolve (ec);
1552                         falseExpr = falseExpr.Resolve (ec);
1553
1554                         if (expr == null || trueExpr == null || falseExpr == null)
1555                                 return null;
1556
1557                         if (trueExpr.Type == falseExpr.Type)
1558                                 type = trueExpr.Type;
1559                         else {
1560                                 Expression conv;
1561
1562                                 //
1563                                 // First, if an implicit conversion exists from trueExpr
1564                                 // to falseExpr, then the result type is of type falseExpr.Type
1565                                 //
1566                                 conv = ConvertImplicit (ec, trueExpr, falseExpr.Type, loc);
1567                                 if (conv != null){
1568                                         type = falseExpr.Type;
1569                                         trueExpr = conv;
1570                                 } else if ((conv = ConvertImplicit(ec, falseExpr,trueExpr.Type,loc))!= null){
1571                                         type = trueExpr.Type;
1572                                         falseExpr = conv;
1573                                 } else {
1574                                         Error (173, loc, "The type of the conditional expression can " +
1575                                                "not be computed because there is no implicit conversion" +
1576                                                " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
1577                                                " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
1578                                         return null;
1579                                 }
1580                         }
1581
1582                         if (expr is BoolLiteral){
1583                                 BoolLiteral bl = (BoolLiteral) expr;
1584
1585                                 if (bl.Value)
1586                                         return trueExpr;
1587                                 else
1588                                         return falseExpr;
1589                         }
1590                         
1591                         eclass = ExprClass.Value;
1592                         return this;
1593                 }
1594
1595                 public override void Emit (EmitContext ec)
1596                 {
1597                         ILGenerator ig = ec.ig;
1598                         Label false_target = ig.DefineLabel ();
1599                         Label end_target = ig.DefineLabel ();
1600
1601                         expr.Emit (ec);
1602                         ig.Emit (OpCodes.Brfalse, false_target);
1603                         trueExpr.Emit (ec);
1604                         ig.Emit (OpCodes.Br, end_target);
1605                         ig.MarkLabel (false_target);
1606                         falseExpr.Emit (ec);
1607                         ig.MarkLabel (end_target);
1608                 }
1609
1610                 public override Expression Reduce (EmitContext ec)
1611                 {
1612                         expr = expr.Reduce (ec);
1613                         trueExpr = trueExpr.Reduce (ec);
1614                         falseExpr = falseExpr.Reduce (ec);
1615
1616                         if (!(expr is Literal && trueExpr is Literal && falseExpr is Literal))
1617                                 return this;
1618
1619                         BoolLiteral bl = (BoolLiteral) expr;
1620
1621                         if (bl.Value)
1622                                 return trueExpr;
1623                         else
1624                                 return falseExpr;
1625                 }
1626         }
1627
1628         public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation {
1629                 public readonly string Name;
1630                 public readonly Block Block;
1631
1632                 VariableInfo variable_info;
1633                 
1634                 public LocalVariableReference (Block block, string name)
1635                 {
1636                         Block = block;
1637                         Name = name;
1638                         eclass = ExprClass.Variable;
1639                 }
1640
1641                 public VariableInfo VariableInfo {
1642                         get {
1643                                 if (variable_info == null)
1644                                         variable_info = Block.GetVariableInfo (Name);
1645                                 return variable_info;
1646                         }
1647                 }
1648                 
1649                 public override Expression DoResolve (EmitContext ec)
1650                 {
1651                         VariableInfo vi = VariableInfo;
1652
1653                         type = vi.VariableType;
1654                         return this;
1655                 }
1656
1657                 public override void Emit (EmitContext ec)
1658                 {
1659                         VariableInfo vi = VariableInfo;
1660                         ILGenerator ig = ec.ig;
1661                         int idx = vi.Idx;
1662
1663                         vi.Used = true;
1664
1665                         switch (idx){
1666                         case 0:
1667                                 ig.Emit (OpCodes.Ldloc_0);
1668                                 break;
1669                                 
1670                         case 1:
1671                                 ig.Emit (OpCodes.Ldloc_1);
1672                                 break;
1673                                 
1674                         case 2:
1675                                 ig.Emit (OpCodes.Ldloc_2);
1676                                 break;
1677                                 
1678                         case 3:
1679                                 ig.Emit (OpCodes.Ldloc_3);
1680                                 break;
1681                                 
1682                         default:
1683                                 if (idx <= 255)
1684                                         ig.Emit (OpCodes.Ldloc_S, (byte) idx);
1685                                 else
1686                                         ig.Emit (OpCodes.Ldloc, idx);
1687                                 break;
1688                         }
1689                 }
1690                 
1691                 public static void Store (ILGenerator ig, int idx)
1692                 {
1693                         switch (idx){
1694                         case 0:
1695                                 ig.Emit (OpCodes.Stloc_0);
1696                                 break;
1697                                 
1698                         case 1:
1699                                 ig.Emit (OpCodes.Stloc_1);
1700                                 break;
1701                                 
1702                         case 2:
1703                                 ig.Emit (OpCodes.Stloc_2);
1704                                 break;
1705                                 
1706                         case 3:
1707                                 ig.Emit (OpCodes.Stloc_3);
1708                                 break;
1709                                 
1710                         default:
1711                                 if (idx <= 255)
1712                                         ig.Emit (OpCodes.Stloc_S, (byte) idx);
1713                                 else
1714                                         ig.Emit (OpCodes.Stloc, idx);
1715                                 break;
1716                         }
1717                 }
1718
1719                 public void EmitAssign (EmitContext ec, Expression source)
1720                 {
1721                         ILGenerator ig = ec.ig;
1722                         VariableInfo vi = VariableInfo;
1723
1724                         vi.Assigned = true;
1725
1726                         source.Emit (ec);
1727                         
1728                         // Funny seems the code below generates optimal code for us, but
1729                         // seems to take too long to generate what we need.
1730                         // ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
1731
1732                         Store (ig, vi.Idx);
1733                 }
1734                 
1735                 public void AddressOf (EmitContext ec)
1736                 {
1737                         VariableInfo vi = VariableInfo;
1738                         int idx = vi.Idx;
1739
1740                         vi.Used = true;
1741                         vi.Assigned = true;
1742                         
1743                         if (idx <= 255)
1744                                 ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
1745                         else
1746                                 ec.ig.Emit (OpCodes.Ldloca, idx);
1747                 }
1748         }
1749
1750         public class ParameterReference : Expression, IAssignMethod, IMemoryLocation {
1751                 public readonly Parameters Pars;
1752                 public readonly String Name;
1753                 public readonly int Idx;
1754                 int arg_idx;
1755                 
1756                 public ParameterReference (Parameters pars, int idx, string name)
1757                 {
1758                         Pars = pars;
1759                         Idx  = idx;
1760                         Name = name;
1761                         eclass = ExprClass.Variable;
1762                 }
1763
1764                 public override Expression DoResolve (EmitContext ec)
1765                 {
1766                         Type [] types = Pars.GetParameterInfo (ec.TypeContainer);
1767
1768                         type = types [Idx];
1769
1770                         arg_idx = Idx;
1771                         if (!ec.IsStatic)
1772                                 arg_idx++;
1773                         
1774                         return this;
1775                 }
1776
1777                 public override void Emit (EmitContext ec)
1778                 {
1779                         if (arg_idx <= 255)
1780                                 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
1781                         else
1782                                 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
1783                 }
1784
1785                 public void EmitAssign (EmitContext ec, Expression source)
1786                 {
1787                         source.Emit (ec);
1788                         
1789                         if (arg_idx <= 255)
1790                                 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
1791                         else
1792                                 ec.ig.Emit (OpCodes.Starg, arg_idx);
1793                         
1794                 }
1795
1796                 public void AddressOf (EmitContext ec)
1797                 {
1798                         if (arg_idx <= 255)
1799                                 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
1800                         else
1801                                 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
1802                 }
1803         }
1804         
1805         // <summary>
1806         //   Used for arguments to New(), Invocation()
1807         // </summary>
1808         public class Argument {
1809                 public enum AType : byte {
1810                         Expression,
1811                         Ref,
1812                         Out
1813                 };
1814
1815                 public readonly AType ArgType;
1816                 public Expression expr;
1817                 
1818                 public Argument (Expression expr, AType type)
1819                 {
1820                         this.expr = expr;
1821                         this.ArgType = type;
1822                 }
1823
1824                 public Expression Expr {
1825                         get {
1826                                 return expr;
1827                         }
1828
1829                         set {
1830                                 expr = value;
1831                         }
1832                 }
1833
1834                 public Type Type {
1835                         get {
1836                                 return expr.Type;
1837                         }
1838                 }
1839
1840                 public Parameter.Modifier GetParameterModifier ()
1841                 {
1842                         if (ArgType == AType.Ref)
1843                                 return Parameter.Modifier.REF;
1844
1845                         if (ArgType == AType.Out)
1846                                 return Parameter.Modifier.OUT;
1847
1848                         return Parameter.Modifier.NONE;
1849                 }
1850
1851                 public static string FullDesc (Argument a)
1852                 {
1853                         return (a.ArgType == AType.Ref ? "ref " :
1854                                 (a.ArgType == AType.Out ? "out " : "")) +
1855                                 TypeManager.CSharpName (a.Expr.Type);
1856                 }
1857                 
1858                 public bool Resolve (EmitContext ec, Location loc)
1859                 {
1860                         expr = expr.Resolve (ec);
1861
1862                         if (ArgType == AType.Expression)
1863                                 return expr != null;
1864
1865                         if (expr.ExprClass != ExprClass.Variable){
1866                                 Report.Error (206, loc,
1867                                               "A property or indexer can not be passed as an out or ref " +
1868                                               "parameter");
1869                                 return false;
1870                         }
1871                                 
1872                         return expr != null;
1873                 }
1874
1875                 public void Emit (EmitContext ec)
1876                 {
1877                         if (ArgType == AType.Ref || ArgType == AType.Out)
1878                                 ((IMemoryLocation)expr).AddressOf (ec);
1879                         else
1880                                 expr.Emit (ec);
1881                 }
1882         }
1883
1884         // <summary>
1885         //   Invocation of methods or delegates.
1886         // </summary>
1887         public class Invocation : ExpressionStatement {
1888                 public readonly ArrayList Arguments;
1889                 Location loc;
1890
1891                 Expression expr;
1892                 MethodBase method = null;
1893                         
1894                 static Hashtable method_parameter_cache;
1895
1896                 static Invocation ()
1897                 {
1898                         method_parameter_cache = new Hashtable ();
1899                 }
1900                         
1901                 //
1902                 // arguments is an ArrayList, but we do not want to typecast,
1903                 // as it might be null.
1904                 //
1905                 // FIXME: only allow expr to be a method invocation or a
1906                 // delegate invocation (7.5.5)
1907                 //
1908                 public Invocation (Expression expr, ArrayList arguments, Location l)
1909                 {
1910                         this.expr = expr;
1911                         Arguments = arguments;
1912                         loc = l;
1913                 }
1914
1915                 public Expression Expr {
1916                         get {
1917                                 return expr;
1918                         }
1919                 }
1920
1921                 // <summary>
1922                 //   Returns the Parameters (a ParameterData interface) for the
1923                 //   Method `mb'
1924                 // </summary>
1925                 public static ParameterData GetParameterData (MethodBase mb)
1926                 {
1927                         object pd = method_parameter_cache [mb];
1928                         object ip;
1929                         
1930                         if (pd != null)
1931                                 return (ParameterData) pd;
1932
1933                         
1934                         ip = TypeContainer.LookupParametersByBuilder (mb);
1935                         if (ip != null){
1936                                 method_parameter_cache [mb] = ip;
1937
1938                                 return (ParameterData) ip;
1939                         } else {
1940                                 Console.WriteLine ("Getting parameters for: " + mb);
1941                                 ParameterInfo [] pi = mb.GetParameters ();
1942                                 ReflectionParameters rp = new ReflectionParameters (pi);
1943                                 method_parameter_cache [mb] = rp;
1944
1945                                 return (ParameterData) rp;
1946                         }
1947                 }
1948
1949                 // <summary>
1950                 //   Tells whether a user defined conversion from Type `from' to
1951                 //   Type `to' exists.
1952                 //
1953                 //   FIXME: we could implement a cache here. 
1954                 // </summary>
1955                 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1956                 {
1957                         // Locate user-defined implicit operators
1958
1959                         Expression mg;
1960                         
1961                         mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1962
1963                         if (mg != null) {
1964                                 MethodGroupExpr me = (MethodGroupExpr) mg;
1965                                 
1966                                 for (int i = me.Methods.Length; i > 0;) {
1967                                         i--;
1968                                         MethodBase mb = me.Methods [i];
1969                                         ParameterData pd = GetParameterData (mb);
1970                                         
1971                                         if (from == pd.ParameterType (0))
1972                                                 return true;
1973                                 }
1974                         }
1975
1976                         mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1977
1978                         if (mg != null) {
1979                                 MethodGroupExpr me = (MethodGroupExpr) mg;
1980
1981                                 for (int i = me.Methods.Length; i > 0;) {
1982                                         i--;
1983                                         MethodBase mb = me.Methods [i];
1984                                         MethodInfo mi = (MethodInfo) mb;
1985                                         
1986                                         if (mi.ReturnType == to)
1987                                                 return true;
1988                                 }
1989                         }
1990                         
1991                         return false;
1992                 }
1993                 
1994                 // <summary>
1995                 //  Determines "better conversion" as specified in 7.4.2.3
1996                 //  Returns : 1 if a->p is better
1997                 //            0 if a->q or neither is better 
1998                 // </summary>
1999                 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
2000                                              Location loc)
2001                 {
2002                         Type argument_type = a.Type;
2003                         Expression argument_expr = a.Expr;
2004
2005                         if (argument_type == null)
2006                                 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2007
2008                         if (p == q)
2009                                 return 0;
2010                         
2011                         if (argument_type == p)
2012                                 return 1;
2013
2014                         if (argument_type == q)
2015                                 return 0;
2016
2017                         //
2018                         // Now probe whether an implicit constant expression conversion
2019                         // can be used.
2020                         //
2021                         // An implicit constant expression conversion permits the following
2022                         // conversions:
2023                         //
2024                         //    * A constant-expression of type `int' can be converted to type
2025                         //      sbyte, byute, short, ushort, uint, ulong provided the value of
2026                         //      of the expression is withing the range of the destination type.
2027                         //
2028                         //    * A constant-expression of type long can be converted to type
2029                         //      ulong, provided the value of the constant expression is not negative
2030                         //
2031                         // FIXME: Note that this assumes that constant folding has
2032                         // taken place.  We dont do constant folding yet.
2033                         //
2034
2035                         if (argument_expr is IntLiteral){
2036                                 IntLiteral ei = (IntLiteral) argument_expr;
2037                                 int value = ei.Value;
2038                                 
2039                                 if (p == TypeManager.sbyte_type){
2040                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
2041                                                 return 1;
2042                                 } else if (p == TypeManager.byte_type){
2043                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2044                                                 return 1;
2045                                 } else if (p == TypeManager.short_type){
2046                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
2047                                                 return 1;
2048                                 } else if (p == TypeManager.ushort_type){
2049                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2050                                                 return 1;
2051                                 } else if (p == TypeManager.uint32_type){
2052                                         //
2053                                         // we can optimize this case: a positive int32
2054                                         // always fits on a uint32
2055                                         //
2056                                         if (value >= 0)
2057                                                 return 1;
2058                                 } else if (p == TypeManager.uint64_type){
2059                                         //
2060                                         // we can optimize this case: a positive int32
2061                                         // always fits on a uint64
2062                                         //
2063                                         if (value >= 0)
2064                                                 return 1;
2065                                 }
2066                         } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2067                                 LongLiteral ll = (LongLiteral) argument_expr;
2068                                 
2069                                 if (p == TypeManager.uint64_type){
2070                                         if (ll.Value > 0)
2071                                                 return 1;
2072                                 }
2073                         }
2074
2075                         if (q == null) {
2076
2077                                 Expression tmp;
2078
2079                                 if (use_standard)
2080                                         tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
2081                                 else
2082                                         tmp = ConvertImplicit (ec, argument_expr, p, loc);
2083
2084                                 if (tmp != null)
2085                                         return 1;
2086                                 else
2087                                         return 0;
2088
2089                         }
2090
2091                         if (ConversionExists (ec, p, q, loc) == true &&
2092                             ConversionExists (ec, q, p, loc) == false)
2093                                 return 1;
2094
2095                         if (p == TypeManager.sbyte_type)
2096                                 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2097                                     q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2098                                         return 1;
2099
2100                         if (p == TypeManager.short_type)
2101                                 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2102                                     q == TypeManager.uint64_type)
2103                                         return 1;
2104
2105                         if (p == TypeManager.int32_type)
2106                                 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2107                                         return 1;
2108
2109                         if (p == TypeManager.int64_type)
2110                                 if (q == TypeManager.uint64_type)
2111                                         return 1;
2112
2113                         return 0;
2114                 }
2115                 
2116                 // <summary>
2117                 //  Determines "Better function" and returns an integer indicating :
2118                 //  0 if candidate ain't better
2119                 //  1 if candidate is better than the current best match
2120                 // </summary>
2121                 static int BetterFunction (EmitContext ec, ArrayList args,
2122                                            MethodBase candidate, MethodBase best,
2123                                            bool use_standard, Location loc)
2124                 {
2125                         ParameterData candidate_pd = GetParameterData (candidate);
2126                         ParameterData best_pd;
2127                         int argument_count;
2128
2129                         if (args == null)
2130                                 argument_count = 0;
2131                         else
2132                                 argument_count = args.Count;
2133
2134                         if (candidate_pd.Count == 0 && argument_count == 0)
2135                                 return 1;
2136
2137                         if (best == null) {
2138                                 if (candidate_pd.Count == argument_count) {
2139                                         int x = 0;
2140                                         for (int j = argument_count; j > 0;) {
2141                                                 j--;
2142                                                 
2143                                                 Argument a = (Argument) args [j];
2144                                                 
2145                                                 x = BetterConversion (
2146                                                         ec, a, candidate_pd.ParameterType (j), null,
2147                                                         use_standard, loc);
2148                                                 
2149                                                 if (x <= 0)
2150                                                         break;
2151                                         }
2152                                         
2153                                         if (x > 0)
2154                                                 return 1;
2155                                         else
2156                                                 return 0;
2157                                         
2158                                 } else
2159                                         return 0;
2160                         }
2161
2162                         best_pd = GetParameterData (best);
2163
2164                         if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2165                                 int rating1 = 0, rating2 = 0;
2166                                 
2167                                 for (int j = argument_count; j > 0;) {
2168                                         j--;
2169                                         int x, y;
2170                                         
2171                                         Argument a = (Argument) args [j];
2172
2173                                         x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2174                                                               best_pd.ParameterType (j), use_standard, loc);
2175                                         y = BetterConversion (ec, a, best_pd.ParameterType (j),
2176                                                               candidate_pd.ParameterType (j), use_standard,
2177                                                               loc);
2178                                         
2179                                         rating1 += x;
2180                                         rating2 += y;
2181                                 }
2182
2183                                 if (rating1 > rating2)
2184                                         return 1;
2185                                 else
2186                                         return 0;
2187                         } else
2188                                 return 0;
2189                         
2190                 }
2191
2192                 public static string FullMethodDesc (MethodBase mb)
2193                 {
2194                         StringBuilder sb = new StringBuilder (mb.Name);
2195                         ParameterData pd = GetParameterData (mb);
2196
2197                         int count = pd.Count;
2198                         sb.Append (" (");
2199                         
2200                         for (int i = count; i > 0; ) {
2201                                 i--;
2202                                 
2203                                 sb.Append (pd.ParameterDesc (count - i - 1));
2204                                 if (i != 0)
2205                                         sb.Append (", ");
2206                         }
2207                         
2208                         sb.Append (")");
2209                         return sb.ToString ();
2210                 }
2211
2212                 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2213                 {
2214                         MemberInfo [] miset;
2215                         MethodGroupExpr union;
2216                         
2217                         if (mg1 != null && mg2 != null) {
2218                                 
2219                                 MethodGroupExpr left_set = null, right_set = null;
2220                                 int length1 = 0, length2 = 0;
2221                                 
2222                                 left_set = (MethodGroupExpr) mg1;
2223                                 length1 = left_set.Methods.Length;
2224                                 
2225                                 right_set = (MethodGroupExpr) mg2;
2226                                 length2 = right_set.Methods.Length;
2227
2228                                 ArrayList common = new ArrayList ();
2229                                 
2230                                 for (int i = 0; i < left_set.Methods.Length; i++) {
2231                                         for (int j = 0; j < right_set.Methods.Length; j++) {
2232                                                 if (left_set.Methods [i] == right_set.Methods [j]) 
2233                                                         common.Add (left_set.Methods [i]);
2234                                         }
2235                                 }
2236                                 
2237                                 miset = new MemberInfo [length1 + length2 - common.Count];
2238
2239                                 left_set.Methods.CopyTo (miset, 0);
2240
2241                                 int k = 0;
2242                                 
2243                                 for (int j = 0; j < right_set.Methods.Length; j++)
2244                                         if (!common.Contains (right_set.Methods [j]))
2245                                                 miset [length1 + k++] = right_set.Methods [j];
2246                                 
2247                                 union = new MethodGroupExpr (miset);
2248
2249                                 return union;
2250
2251                         } else if (mg1 == null && mg2 != null) {
2252                                 
2253                                 MethodGroupExpr me = (MethodGroupExpr) mg2; 
2254                                 
2255                                 miset = new MemberInfo [me.Methods.Length];
2256                                 me.Methods.CopyTo (miset, 0);
2257
2258                                 union = new MethodGroupExpr (miset);
2259                                 
2260                                 return union;
2261
2262                         } else if (mg2 == null && mg1 != null) {
2263                                 
2264                                 MethodGroupExpr me = (MethodGroupExpr) mg1; 
2265                                 
2266                                 miset = new MemberInfo [me.Methods.Length];
2267                                 me.Methods.CopyTo (miset, 0);
2268
2269                                 union = new MethodGroupExpr (miset);
2270                                 
2271                                 return union;
2272                         }
2273                         
2274                         return null;
2275                 }
2276
2277                 // <summary>
2278                 //  Determines is the candidate method, if a params method, is applicable
2279                 //  in its expanded form to the given set of arguments
2280                 // </summary>
2281                 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2282                 {
2283                         int arg_count;
2284                         
2285                         if (arguments == null)
2286                                 arg_count = 0;
2287                         else
2288                                 arg_count = arguments.Count;
2289                         
2290                         ParameterData pd = GetParameterData (candidate);
2291                         
2292                         int pd_count = pd.Count;
2293
2294                         if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2295                                 return false;
2296
2297                         if (pd_count - 1 > arg_count)
2298                                 return false;
2299
2300                         // If we have come this far, the case which remains is when the number of parameters
2301                         // is less than or equal to the argument count. So, we now check if the element type
2302                         // of the params array is compatible with each argument type
2303                         //
2304
2305                         Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2306
2307                         for (int i = pd_count - 1; i < arg_count - 1; i++) {
2308                                 Argument a = (Argument) arguments [i];
2309                                 if (!StandardConversionExists (a.Type, element_type))
2310                                         return false;
2311                         }
2312                         
2313                         return true;
2314                 }
2315
2316                 // <summary>
2317                 //  Determines if the candidate method is applicable (section 14.4.2.1)
2318                 //  to the given set of arguments
2319                 // </summary>
2320                 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2321                 {
2322                         int arg_count;
2323
2324                         if (arguments == null)
2325                                 arg_count = 0;
2326                         else
2327                                 arg_count = arguments.Count;
2328
2329                         ParameterData pd = GetParameterData (candidate);
2330
2331                         int pd_count = pd.Count;
2332
2333                         if (arg_count != pd.Count)
2334                                 return false;
2335
2336                         for (int i = arg_count; i > 0; ) {
2337                                 i--;
2338
2339                                 Argument a = (Argument) arguments [i];
2340
2341                                 Parameter.Modifier a_mod = a.GetParameterModifier ();
2342                                 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2343
2344                                 if (a_mod == p_mod) {
2345                                         
2346                                         if (a_mod == Parameter.Modifier.NONE)
2347                                                 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2348                                                         return false;
2349                                         
2350                                         if (a_mod == Parameter.Modifier.REF ||
2351                                             a_mod == Parameter.Modifier.OUT)
2352                                                 if (pd.ParameterType (i) != a.Type)
2353                                                         return false;
2354                                 } else
2355                                         return false;
2356                         }
2357
2358                         return true;
2359                 }
2360                 
2361                 
2362
2363                 // <summary>
2364                 //   Find the Applicable Function Members (7.4.2.1)
2365                 //
2366                 //   me: Method Group expression with the members to select.
2367                 //       it might contain constructors or methods (or anything
2368                 //       that maps to a method).
2369                 //
2370                 //   Arguments: ArrayList containing resolved Argument objects.
2371                 //
2372                 //   loc: The location if we want an error to be reported, or a Null
2373                 //        location for "probing" purposes.
2374                 //
2375                 //   use_standard: controls whether OverloadResolve should use the 
2376                 //   ConvertImplicit or ConvertImplicitStandard during overload resolution.
2377                 //
2378                 //   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2379                 //            that is the best match of me on Arguments.
2380                 //
2381                 // </summary>
2382                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2383                                                           ArrayList Arguments, Location loc,
2384                                                           bool use_standard)
2385                 {
2386                         ArrayList afm = new ArrayList ();
2387                         int best_match_idx = -1;
2388                         MethodBase method = null;
2389                         int argument_count;
2390                         
2391                         for (int i = me.Methods.Length; i > 0; ){
2392                                 i--;
2393                                 MethodBase candidate  = me.Methods [i];
2394                                 int x;
2395
2396                                 // Check if candidate is applicable (section 14.4.2.1)
2397                                 if (!IsApplicable (Arguments, candidate))
2398                                         continue;
2399
2400                                 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2401                                 
2402                                 if (x == 0)
2403                                         continue;
2404                                 else {
2405                                         best_match_idx = i;
2406                                         method = me.Methods [best_match_idx];
2407                                 }
2408                         }
2409
2410                         if (Arguments == null)
2411                                 argument_count = 0;
2412                         else
2413                                 argument_count = Arguments.Count;
2414                         
2415                         //
2416                         // Now we see if we can find params functions, applicable in their expanded form
2417                         // since if they were applicable in their normal form, they would have been selected
2418                         // above anyways
2419                         //
2420                         if (best_match_idx == -1) {
2421
2422                                 for (int i = me.Methods.Length; i > 0; ) {
2423                                         i--;
2424                                         MethodBase candidate = me.Methods [i];
2425
2426                                         if (IsParamsMethodApplicable (Arguments, candidate)) {
2427                                                 best_match_idx = i;
2428                                                 method = me.Methods [best_match_idx];
2429                                                 break;
2430                                         }
2431                                 }
2432                         }
2433
2434                         //
2435                         // Now we see if we can at least find a method with the same number of arguments
2436                         //
2437                         ParameterData pd;
2438                         
2439                         if (best_match_idx == -1) {
2440                                 
2441                                 for (int i = me.Methods.Length; i > 0;) {
2442                                         i--;
2443                                         MethodBase mb = me.Methods [i];
2444                                         pd = GetParameterData (mb);
2445                                         
2446                                         if (pd.Count == argument_count) {
2447                                                 best_match_idx = i;
2448                                                 method = me.Methods [best_match_idx];
2449                                                 break;
2450                                         } else
2451                                                 continue;
2452                                 }
2453                         }
2454
2455                         if (method == null)
2456                                 return null;
2457                         
2458                         // And now convert implicitly, each argument to the required type
2459                         
2460                         pd = GetParameterData (method);
2461                         int pd_count = pd.Count;
2462
2463                         for (int j = 0; j < argument_count; j++) {
2464
2465                                 Argument a = (Argument) Arguments [j];
2466                                 Expression a_expr = a.Expr;
2467                                 Type parameter_type = pd.ParameterType (j);
2468
2469                                 //
2470                                 // Note that we need to compare against the element type
2471                                 // when we have a params method
2472                                 //
2473                                 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2474                                         if (j >= pd_count - 1) 
2475                                                 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2476                                 }
2477
2478                                 if (a.Type != parameter_type){
2479                                         Expression conv;
2480                                         
2481                                         if (use_standard)
2482                                                 conv = ConvertImplicitStandard (ec, a_expr, parameter_type, Location.Null);
2483                                         else
2484                                                 conv = ConvertImplicit (ec, a_expr, parameter_type, Location.Null);
2485
2486                                         if (conv == null) {
2487                                                 if (!Location.IsNull (loc)) {
2488                                                         Error (1502, loc,
2489                                                         "The best overloaded match for method '" + FullMethodDesc (method)+
2490                                                                "' has some invalid arguments");
2491                                                         Error (1503, loc,
2492                                                          "Argument " + (j+1) +
2493                                                          ": Cannot convert from '" + Argument.FullDesc (a) 
2494                                                          + "' to '" + pd.ParameterDesc (j) + "'");
2495                                                 }
2496                                                 return null;
2497                                         }
2498                                         
2499                         
2500                                         
2501                                         //
2502                                         // Update the argument with the implicit conversion
2503                                         //
2504                                         if (a_expr != conv)
2505                                                 a.Expr = conv;
2506
2507                                         // FIXME : For the case of params methods, we need to actually instantiate
2508                                         // an array and initialize it with the argument values etc etc.
2509
2510                                 }
2511                                 
2512                                 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2513                                     pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2514                                         if (!Location.IsNull (loc)) {
2515                                                 Error (1502, loc,
2516                                                        "The best overloaded match for method '" + FullMethodDesc (method)+
2517                                                        "' has some invalid arguments");
2518                                                 Error (1503, loc,
2519                                                        "Argument " + (j+1) +
2520                                                        ": Cannot convert from '" + Argument.FullDesc (a) 
2521                                                        + "' to '" + pd.ParameterDesc (j) + "'");
2522                                         }
2523                                         return null;
2524                                 }
2525                                 
2526                                 
2527                         }
2528                         
2529                         return method;
2530                 }
2531                 
2532                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2533                                                           ArrayList Arguments, Location loc)
2534                 {
2535                         return OverloadResolve (ec, me, Arguments, loc, false);
2536                 }
2537                         
2538                 public override Expression DoResolve (EmitContext ec)
2539                 {
2540                         //
2541                         // First, resolve the expression that is used to
2542                         // trigger the invocation
2543                         //
2544                         expr = expr.Resolve (ec);
2545                         if (expr == null)
2546                                 return null;
2547
2548                         if (!(expr is MethodGroupExpr)) {
2549                                 Type expr_type = expr.Type;
2550
2551                                 if (expr_type != null){
2552                                         bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2553                                         if (IsDelegate)
2554                                                 return (new DelegateInvocation (
2555                                                         this.expr, Arguments, loc)).Resolve (ec);
2556                                 }
2557                         }
2558
2559                         if (!(expr is MethodGroupExpr)){
2560                                 report118 (loc, this.expr, "method group");
2561                                 return null;
2562                         }
2563
2564                         //
2565                         // Next, evaluate all the expressions in the argument list
2566                         //
2567                         if (Arguments != null){
2568                                 for (int i = Arguments.Count; i > 0;){
2569                                         --i;
2570                                         Argument a = (Argument) Arguments [i];
2571
2572                                         if (!a.Resolve (ec, loc))
2573                                                 return null;
2574                                 }
2575                         }
2576
2577                         method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
2578
2579                         if (method == null){
2580                                 Error (-6, loc,
2581                                        "Could not find any applicable function for this argument list");
2582                                 return null;
2583                         }
2584
2585                         if (method is MethodInfo)
2586                                 type = ((MethodInfo)method).ReturnType;
2587
2588                         eclass = ExprClass.Value;
2589                         return this;
2590                 }
2591
2592                 // <summary>
2593                 //   Emits the list of arguments as an array
2594                 // </summary>
2595                 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
2596                 {
2597                         ILGenerator ig = ec.ig;
2598                         int count = arguments.Count - idx;
2599                         Argument a = (Argument) arguments [idx];
2600                         Type t = a.expr.Type;
2601                         string array_type = t.FullName + "[]";
2602                         LocalBuilder array;
2603                         
2604                         array = ec.GetTemporaryStorage (Type.GetType (array_type));
2605                         IntLiteral.EmitInt (ig, count);
2606                         ig.Emit (OpCodes.Newarr, t);
2607                         ig.Emit (OpCodes.Stloc, array);
2608
2609                         int top = arguments.Count;
2610                         for (int j = idx; j < top; j++){
2611                                 ig.Emit (OpCodes.Ldloc, array);
2612                                 IntLiteral.EmitInt (ig, j - idx);
2613                                 a.Emit (ec);
2614                                 ig.Emit (OpCodes.Stelem_Ref);
2615                         }
2616                         ig.Emit (OpCodes.Ldloc, array);
2617                 }
2618                 
2619                 // <summary>
2620                 //   Emits a list of resolved Arguments that are in the arguments
2621                 //   ArrayList.
2622                 // 
2623                 //   The MethodBase argument might be null if the
2624                 //   emission of the arguments is known not to contain
2625                 //   a `params' field (for example in constructors or other routines
2626                 //   that keep their arguments in this structure
2627                 // </summary>
2628                 
2629                 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
2630                 {
2631                         ParameterData pd = null;
2632                         int top;
2633
2634                         if (arguments != null)
2635                                 top = arguments.Count;
2636                         else
2637                                 top = 0;
2638
2639                         if (mb != null)
2640                                  pd = GetParameterData (mb);
2641
2642                         for (int i = 0; i < top; i++){
2643                                 Argument a = (Argument) arguments [i];
2644
2645                                 if (pd != null){
2646                                         if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
2647                                                 EmitParams (ec, i, arguments);
2648                                                 return;
2649                                         }
2650                                 }
2651                                             
2652                                 a.Emit (ec);
2653                         }
2654                 }
2655
2656                 public static void EmitCall (EmitContext ec,
2657                                              bool is_static, Expression instance_expr,
2658                                              MethodBase method, ArrayList Arguments)
2659                 {
2660                         ILGenerator ig = ec.ig;
2661                         bool struct_call = false;
2662                                 
2663                         if (!is_static){
2664                                 //
2665                                 // If this is ourselves, push "this"
2666                                 //
2667                                 if (instance_expr == null){
2668                                         ig.Emit (OpCodes.Ldarg_0);
2669                                 } else {
2670                                         //
2671                                         // Push the instance expression
2672                                         //
2673                                         if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
2674
2675                                                 struct_call = true;
2676
2677                                                 //
2678                                                 // If the expression implements IMemoryLocation, then
2679                                                 // we can optimize and use AddressOf on the
2680                                                 // return.
2681                                                 //
2682                                                 // If not we have to use some temporary storage for
2683                                                 // it.
2684                                                 if (instance_expr is IMemoryLocation)
2685                                                         ((IMemoryLocation) instance_expr).AddressOf (ec);
2686                                                 else {
2687                                                         Type t = instance_expr.Type;
2688                                                         
2689                                                         instance_expr.Emit (ec);
2690                                                         LocalBuilder temp = ec.GetTemporaryStorage (t);
2691                                                         ig.Emit (OpCodes.Stloc, temp);
2692                                                         ig.Emit (OpCodes.Ldloca, temp);
2693                                                 }
2694                                         } else 
2695                                                 instance_expr.Emit (ec);
2696                                 }
2697                         }
2698
2699                         if (Arguments != null)
2700                                 EmitArguments (ec, method, Arguments);
2701
2702                         if (is_static || struct_call){
2703                                 if (method is MethodInfo)
2704                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
2705                                 else
2706                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2707                         } else {
2708                                 if (method is MethodInfo)
2709                                         ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2710                                 else
2711                                         ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
2712                         }
2713                 }
2714                 
2715                 public override void Emit (EmitContext ec)
2716                 {
2717                         MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2718                         EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
2719                 }
2720                 
2721                 public override void EmitStatement (EmitContext ec)
2722                 {
2723                         Emit (ec);
2724
2725                         // 
2726                         // Pop the return value if there is one
2727                         //
2728                         if (method is MethodInfo){
2729                                 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
2730                                         ec.ig.Emit (OpCodes.Pop);
2731                         }
2732                 }
2733         }
2734
2735         public class New : ExpressionStatement {
2736                 public readonly ArrayList Arguments;
2737                 public readonly string    RequestedType;
2738
2739                 Location loc;
2740                 MethodBase method = null;
2741
2742                 //
2743                 // If set, the new expression is for a value_target, and
2744                 // we will not leave anything on the stack.
2745                 //
2746                 Expression value_target;
2747                 
2748                 public New (string requested_type, ArrayList arguments, Location l)
2749                 {
2750                         RequestedType = requested_type;
2751                         Arguments = arguments;
2752                         loc = l;
2753                 }
2754
2755                 public Expression ValueTypeVariable {
2756                         get {
2757                                 return value_target;
2758                         }
2759
2760                         set {
2761                                 value_target = value;
2762                         }
2763                 }
2764
2765                 public override Expression DoResolve (EmitContext ec)
2766                 {
2767                         type = ec.TypeContainer.LookupType (RequestedType, false);
2768                         
2769                         if (type == null)
2770                                 return null;
2771                         
2772                         bool IsDelegate = TypeManager.IsDelegateType (type);
2773                         
2774                         if (IsDelegate)
2775                                 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
2776                         
2777                         Expression ml;
2778                         
2779                         ml = MemberLookup (ec, type, ".ctor", false,
2780                                            MemberTypes.Constructor, AllBindingsFlags, loc);
2781                         
2782                         bool is_struct = false;
2783                         is_struct = type.IsSubclassOf (TypeManager.value_type);
2784                         
2785                         if (! (ml is MethodGroupExpr)){
2786                                 if (!is_struct){
2787                                         report118 (loc, ml, "method group");
2788                                         return null;
2789                                 }
2790                         }
2791                         
2792                         if (ml != null) {
2793                                 if (Arguments != null){
2794                                         for (int i = Arguments.Count; i > 0;){
2795                                                 --i;
2796                                                 Argument a = (Argument) Arguments [i];
2797                                                 
2798                                                 if (!a.Resolve (ec, loc))
2799                                                         return null;
2800                                         }
2801                                 }
2802
2803                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
2804                                                                      Arguments, loc);
2805                         }
2806                         
2807                         if (method == null && !is_struct) {
2808                                 Error (-6, loc,
2809                                        "New invocation: Can not find a constructor for " +
2810                                        "this argument list");
2811                                 return null;
2812                         }
2813                         
2814                         eclass = ExprClass.Value;
2815                         return this;
2816                 }
2817
2818                 //
2819                 // This DoEmit can be invoked in two contexts:
2820                 //    * As a mechanism that will leave a value on the stack (new object)
2821                 //    * As one that wont (init struct)
2822                 //
2823                 // You can control whether a value is required on the stack by passing
2824                 // need_value_on_stack.  The code *might* leave a value on the stack
2825                 // so it must be popped manually
2826                 //
2827                 // Returns whether a value is left on the stack
2828                 //
2829                 bool DoEmit (EmitContext ec, bool need_value_on_stack)
2830                 {
2831                         if (method == null){
2832                                 IMemoryLocation ml = (IMemoryLocation) value_target;
2833
2834                                 ml.AddressOf (ec);
2835                         } else {
2836                                 Invocation.EmitArguments (ec, method, Arguments);
2837                                 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2838                                 return true;
2839                         }
2840
2841                         //
2842                         // It must be a value type, sanity check
2843                         //
2844                         if (value_target != null){
2845                                 ec.ig.Emit (OpCodes.Initobj, type);
2846
2847                                 if (need_value_on_stack){
2848                                         value_target.Emit (ec);
2849                                         return true;
2850                                 }
2851                                 return false;
2852                         }
2853
2854                         throw new Exception ("No method and no value type");
2855                 }
2856
2857                 public override void Emit (EmitContext ec)
2858                 {
2859                         DoEmit (ec, true);
2860                 }
2861                 
2862                 public override void EmitStatement (EmitContext ec)
2863                 {
2864                         if (DoEmit (ec, false))
2865                                 ec.ig.Emit (OpCodes.Pop);
2866                 }
2867         }
2868
2869         // <summary>
2870         //   Represents an array creation expression.
2871         // </summary>
2872         //
2873         // <remarks>
2874         //   There are two possible scenarios here: one is an array creation
2875         //   expression that specifies the dimensions and optionally the
2876         //   initialization data
2877         // </remarks>
2878         public class ArrayCreation : ExpressionStatement {
2879
2880                 string RequestedType;
2881                 string Rank;
2882                 ArrayList Initializers;
2883                 Location  loc;
2884                 ArrayList Arguments;
2885
2886                 MethodBase method = null;
2887                 Type array_element_type;
2888                 bool IsOneDimensional = false;
2889                 
2890                 bool IsBuiltinType = false;
2891
2892                 public ArrayCreation (string requested_type, ArrayList exprs,
2893                                       string rank, ArrayList initializers, Location l)
2894                 {
2895                         RequestedType = requested_type;
2896                         Rank          = rank;
2897                         Initializers  = initializers;
2898                         loc = l;
2899
2900                         Arguments = new ArrayList ();
2901
2902                         foreach (Expression e in exprs)
2903                                 Arguments.Add (new Argument (e, Argument.AType.Expression));
2904                         
2905                 }
2906
2907                 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2908                 {
2909                         RequestedType = requested_type;
2910                         Rank = rank;
2911                         Initializers = initializers;
2912                         loc = l;
2913                 }
2914
2915                 public static string FormArrayType (string base_type, int idx_count, string rank)
2916                 {
2917                         StringBuilder sb = new StringBuilder (base_type);
2918
2919                         sb.Append (rank);
2920                         
2921                         sb.Append ("[");
2922                         for (int i = 1; i < idx_count; i++)
2923                                 sb.Append (",");
2924                         sb.Append ("]");
2925                         
2926                         return sb.ToString ();
2927                 }
2928
2929                 public static string FormElementType (string base_type, int idx_count, string rank)
2930                 {
2931                         StringBuilder sb = new StringBuilder (base_type);
2932                         
2933                         sb.Append ("[");
2934                         for (int i = 1; i < idx_count; i++)
2935                                 sb.Append (",");
2936                         sb.Append ("]");
2937
2938                         sb.Append (rank);
2939
2940                         string val = sb.ToString ();
2941
2942                         return val.Substring (0, val.LastIndexOf ("["));
2943                 }
2944                 
2945
2946                 public override Expression DoResolve (EmitContext ec)
2947                 {
2948                         int arg_count;
2949                         
2950                         if (Arguments == null)
2951                                 arg_count = 0;
2952                         else
2953                                 arg_count = Arguments.Count;
2954                         
2955                         string array_type = FormArrayType (RequestedType, arg_count, Rank);
2956
2957                         string element_type = FormElementType (RequestedType, arg_count, Rank);
2958
2959                         type = ec.TypeContainer.LookupType (array_type, false);
2960                         
2961                         array_element_type = ec.TypeContainer.LookupType (element_type, false);
2962                         
2963                         if (type == null)
2964                                 return null;
2965                         
2966                         if (arg_count == 1) {
2967                                 IsOneDimensional = true;
2968                                 eclass = ExprClass.Value;
2969                                 return this;
2970                         }
2971
2972                         IsBuiltinType = TypeManager.IsBuiltinType (type);
2973                         
2974                         if (IsBuiltinType) {
2975                                 
2976                                 Expression ml;
2977                                 
2978                                 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
2979                                                    AllBindingsFlags, loc);
2980                                 
2981                                 if (!(ml is MethodGroupExpr)){
2982                                         report118 (loc, ml, "method group");
2983                                         return null;
2984                                 }
2985                                 
2986                                 if (ml == null) {
2987                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
2988                                                       "this argument list");
2989                                         return null;
2990                                 }
2991                                 
2992                                 if (Arguments != null) {
2993                                         for (int i = arg_count; i > 0;){
2994                                                 --i;
2995                                                 Argument a = (Argument) Arguments [i];
2996                                                 
2997                                                 if (!a.Resolve (ec, loc))
2998                                                         return null;
2999                                         }
3000                                 }
3001                                 
3002                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3003                                 
3004                                 if (method == null) {
3005                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3006                                                       "this argument list");
3007                                         return null;
3008                                 }
3009                                 
3010                                 eclass = ExprClass.Value;
3011                                 return this;
3012                                 
3013                         } else {
3014
3015                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3016
3017                                 ArrayList args = new ArrayList ();
3018                                 if (Arguments != null){
3019                                         for (int i = arg_count; i > 0;){
3020                                                 --i;
3021                                                 Argument a = (Argument) Arguments [i];
3022                                                 
3023                                                 if (!a.Resolve (ec, loc))
3024                                                         return null;
3025                                                 
3026                                                 args.Add (a.Type);
3027                                         }
3028                                 }
3029                                 
3030                                 Type [] arg_types = null;
3031                                 
3032                                 if (args.Count > 0)
3033                                         arg_types = new Type [args.Count];
3034                                 
3035                                 args.CopyTo (arg_types, 0);
3036                                 
3037                                 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3038                                                             arg_types);
3039                                 
3040                                 if (method == null) {
3041                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3042                                                       "this argument list");
3043                                         return null;
3044                                 }
3045                                 
3046                                 eclass = ExprClass.Value;
3047                                 return this;
3048                                 
3049                         }
3050                 }
3051
3052                 public override void Emit (EmitContext ec)
3053                 {
3054                         ILGenerator ig = ec.ig;
3055                         
3056                         if (IsOneDimensional) {
3057                                 Invocation.EmitArguments (ec, null, Arguments);
3058                                 ig.Emit (OpCodes.Newarr, array_element_type);
3059                                 
3060                         } else {
3061                                 Invocation.EmitArguments (ec, null, Arguments);
3062
3063                                 if (IsBuiltinType)
3064                                         ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3065                                 else
3066                                         ig.Emit (OpCodes.Newobj, (MethodInfo) method);
3067                         }
3068
3069                         if (Initializers != null){
3070                                 FieldBuilder fb;
3071
3072                                 // FIXME: This is just sample data, need to fill with
3073                                 // real values.
3074                                 byte [] a = new byte [4] { 1, 2, 3, 4 };
3075                                 
3076                                 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
3077
3078                                 ig.Emit (OpCodes.Dup);
3079                                 ig.Emit (OpCodes.Ldtoken, fb);
3080                                 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
3081                         }
3082                 }
3083                 
3084                 public override void EmitStatement (EmitContext ec)
3085                 {
3086                         Emit (ec);
3087                         ec.ig.Emit (OpCodes.Pop);
3088                 }
3089                 
3090         }
3091         
3092         //
3093         // Represents the `this' construct
3094         //
3095         public class This : Expression, IAssignMethod, IMemoryLocation {
3096                 Location loc;
3097                 
3098                 public This (Location loc)
3099                 {
3100                         this.loc = loc;
3101                 }
3102
3103                 public override Expression DoResolve (EmitContext ec)
3104                 {
3105                         eclass = ExprClass.Variable;
3106                         type = ec.TypeContainer.TypeBuilder;
3107
3108                         if (ec.IsStatic){
3109                                 Report.Error (26, loc,
3110                                               "Keyword this not valid in static code");
3111                                 return null;
3112                         }
3113                         
3114                         return this;
3115                 }
3116
3117                 public Expression DoResolveLValue (EmitContext ec)
3118                 {
3119                         DoResolve (ec);
3120                         
3121                         if (ec.TypeContainer is Class){
3122                                 Report.Error (1604, loc, "Cannot assign to `this'");
3123                                 return null;
3124                         }
3125
3126                         return this;
3127                 }
3128
3129                 public override void Emit (EmitContext ec)
3130                 {
3131                         ec.ig.Emit (OpCodes.Ldarg_0);
3132                 }
3133
3134                 public void EmitAssign (EmitContext ec, Expression source)
3135                 {
3136                         source.Emit (ec);
3137                         ec.ig.Emit (OpCodes.Starg, 0);
3138                 }
3139
3140                 public void AddressOf (EmitContext ec)
3141                 {
3142                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3143                 }
3144         }
3145
3146         // <summary>
3147         //   Implements the typeof operator
3148         // </summary>
3149         public class TypeOf : Expression {
3150                 public readonly string QueriedType;
3151                 Type typearg;
3152                 
3153                 public TypeOf (string queried_type)
3154                 {
3155                         QueriedType = queried_type;
3156                 }
3157
3158                 public override Expression DoResolve (EmitContext ec)
3159                 {
3160                         typearg = ec.TypeContainer.LookupType (QueriedType, false);
3161
3162                         if (typearg == null)
3163                                 return null;
3164
3165                         type = TypeManager.type_type;
3166                         eclass = ExprClass.Type;
3167                         return this;
3168                 }
3169
3170                 public override void Emit (EmitContext ec)
3171                 {
3172                         ec.ig.Emit (OpCodes.Ldtoken, typearg);
3173                         ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3174                 }
3175         }
3176
3177         public class SizeOf : Expression {
3178                 public readonly string QueriedType;
3179                 
3180                 public SizeOf (string queried_type)
3181                 {
3182                         this.QueriedType = queried_type;
3183                 }
3184
3185                 public override Expression DoResolve (EmitContext ec)
3186                 {
3187                         // FIXME: Implement;
3188                         throw new Exception ("Unimplemented");
3189                         // return this;
3190                 }
3191
3192                 public override void Emit (EmitContext ec)
3193                 {
3194                         throw new Exception ("Implement me");
3195                 }
3196         }
3197
3198         public class MemberAccess : Expression {
3199                 public readonly string Identifier;
3200                 Expression expr;
3201                 Expression member_lookup;
3202                 Location loc;
3203                 
3204                 public MemberAccess (Expression expr, string id, Location l)
3205                 {
3206                         this.expr = expr;
3207                         Identifier = id;
3208                         loc = l;
3209                 }
3210
3211                 public Expression Expr {
3212                         get {
3213                                 return expr;
3214                         }
3215                 }
3216
3217                 void error176 (Location loc, string name)
3218                 {
3219                         Report.Error (176, loc, "Static member `" +
3220                                       name + "' cannot be accessed " +
3221                                       "with an instance reference, qualify with a " +
3222                                       "type name instead");
3223                 }
3224                 
3225                 public override Expression DoResolve (EmitContext ec)
3226                 {
3227                         //
3228                         // We are the sole users of ResolveWithSimpleName (ie, the only
3229                         // ones that can cope with it
3230                         //
3231                         expr = expr.ResolveWithSimpleName (ec);
3232
3233                         if (expr == null)
3234                                 return null;
3235
3236                         if (expr is SimpleName){
3237                                 SimpleName child_expr = (SimpleName) expr;
3238
3239                                 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3240
3241                                 return expr.Resolve (ec);
3242                         }
3243                                         
3244                         member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3245
3246                         if (member_lookup == null)
3247                                 return null;
3248                         
3249                         //
3250                         // Method Groups
3251                         //
3252                         if (member_lookup is MethodGroupExpr){
3253                                 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3254                                 
3255                                 //
3256                                 // Type.MethodGroup
3257                                 //
3258                                 if (expr is TypeExpr){
3259                                         if (!mg.RemoveInstanceMethods ()){
3260                                                 SimpleName.Error120 (loc, mg.Methods [0].Name); 
3261                                                 return null;
3262                                         }
3263
3264                                         return member_lookup;
3265                                 }
3266
3267                                 //
3268                                 // Instance.MethodGroup
3269                                 //
3270                                 if (!mg.RemoveStaticMethods ()){
3271                                         error176 (loc, mg.Methods [0].Name);
3272                                         return null;
3273                                 }
3274                                 
3275                                 mg.InstanceExpression = expr;
3276                                         
3277                                 return member_lookup;
3278                         }
3279
3280                         if (member_lookup is FieldExpr){
3281                                 FieldExpr fe = (FieldExpr) member_lookup;
3282                                 FieldInfo fi = fe.FieldInfo;
3283
3284                                 if (fi.IsLiteral) {
3285                                         Type t = fi.FieldType;
3286                                         object o;
3287
3288                                         if (fi is FieldBuilder)
3289                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3290                                         else
3291                                                 o = fi.GetValue (fi);
3292                                         
3293                                         if (t.IsSubclassOf (TypeManager.enum_type)) {
3294                                                 Expression enum_member = MemberLookup (ec, t, "value__", false, loc); 
3295                                                 Type underlying_type = enum_member.Type;
3296                                                 
3297                                                 Expression e = Literalize (o, underlying_type);
3298                                                 e.Resolve (ec);
3299                                         
3300                                                 return new EnumLiteral (e, t);
3301                                         }
3302
3303                                         Expression exp = Literalize (o, t);
3304                                         exp.Resolve (ec);
3305                                         
3306                                         return exp;
3307                                 }
3308                                 
3309                                 if (expr is TypeExpr){
3310                                         if (!fe.FieldInfo.IsStatic){
3311                                                 error176 (loc, fe.FieldInfo.Name);
3312                                                 return null;
3313                                         }
3314                                         return member_lookup;
3315                                 } else {
3316                                         if (fe.FieldInfo.IsStatic){
3317                                                 error176 (loc, fe.FieldInfo.Name);
3318                                                 return null;
3319                                         }
3320                                         fe.InstanceExpression = expr;
3321
3322                                         return fe;
3323                                 }
3324                         }
3325
3326                         if (member_lookup is PropertyExpr){
3327                                 PropertyExpr pe = (PropertyExpr) member_lookup;
3328
3329                                 if (expr is TypeExpr){
3330                                         if (!pe.IsStatic){
3331                                                 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3332                                                 return null;
3333                                         }
3334                                         return pe;
3335                                 } else {
3336                                         if (pe.IsStatic){
3337                                                 error176 (loc, pe.PropertyInfo.Name);
3338                                                 return null;
3339                                         }
3340                                         pe.InstanceExpression = expr;
3341
3342                                         return pe;
3343                                 }
3344                         }
3345                         
3346                         Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3347                         Environment.Exit (0);
3348                         return null;
3349                 }
3350
3351                 public override void Emit (EmitContext ec)
3352                 {
3353                         throw new Exception ("Should not happen I think");
3354                 }
3355
3356         }
3357
3358         public class CheckedExpr : Expression {
3359
3360                 public Expression Expr;
3361
3362                 public CheckedExpr (Expression e)
3363                 {
3364                         Expr = e;
3365                 }
3366
3367                 public override Expression DoResolve (EmitContext ec)
3368                 {
3369                         Expr = Expr.Resolve (ec);
3370
3371                         if (Expr == null)
3372                                 return null;
3373
3374                         eclass = Expr.ExprClass;
3375                         type = Expr.Type;
3376                         return this;
3377                 }
3378
3379                 public override void Emit (EmitContext ec)
3380                 {
3381                         bool last_check = ec.CheckState;
3382                         
3383                         ec.CheckState = true;
3384                         Expr.Emit (ec);
3385                         ec.CheckState = last_check;
3386                 }
3387                 
3388         }
3389
3390         public class UnCheckedExpr : Expression {
3391
3392                 public Expression Expr;
3393
3394                 public UnCheckedExpr (Expression e)
3395                 {
3396                         Expr = e;
3397                 }
3398
3399                 public override Expression DoResolve (EmitContext ec)
3400                 {
3401                         Expr = Expr.Resolve (ec);
3402
3403                         if (Expr == null)
3404                                 return null;
3405
3406                         eclass = Expr.ExprClass;
3407                         type = Expr.Type;
3408                         return this;
3409                 }
3410
3411                 public override void Emit (EmitContext ec)
3412                 {
3413                         bool last_check = ec.CheckState;
3414                         
3415                         ec.CheckState = false;
3416                         Expr.Emit (ec);
3417                         ec.CheckState = last_check;
3418                 }
3419                 
3420         }
3421
3422         public class ElementAccess : Expression {
3423                 public ArrayList  Arguments;
3424                 public Expression Expr;
3425                 public Location   loc;
3426                 
3427                 public ElementAccess (Expression e, ArrayList e_list, Location l)
3428                 {
3429                         Expr = e;
3430
3431                         Arguments = new ArrayList ();
3432                         foreach (Expression tmp in e_list)
3433                                 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3434                         
3435                         loc  = l;
3436                 }
3437
3438                 bool CommonResolve (EmitContext ec)
3439                 {
3440                         Expr = Expr.Resolve (ec);
3441
3442                         if (Expr == null) 
3443                                 return false;
3444
3445                         if (Arguments == null)
3446                                 return false;
3447
3448                         for (int i = Arguments.Count; i > 0;){
3449                                 --i;
3450                                 Argument a = (Argument) Arguments [i];
3451                                 
3452                                 if (!a.Resolve (ec, loc))
3453                                         return false;
3454                         }
3455
3456                         return true;
3457                 }
3458                                 
3459                 public override Expression DoResolve (EmitContext ec)
3460                 {
3461                         if (!CommonResolve (ec))
3462                                 return null;
3463
3464                         //
3465                         // We perform some simple tests, and then to "split" the emit and store
3466                         // code we create an instance of a different class, and return that.
3467                         //
3468                         // I am experimenting with this pattern.
3469                         //
3470                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3471                                 return (new ArrayAccess (this)).Resolve (ec);
3472                         else
3473                                 return (new IndexerAccess (this)).Resolve (ec);
3474                 }
3475
3476                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3477                 {
3478                         if (!CommonResolve (ec))
3479                                 return null;
3480
3481                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3482                                 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3483                         else
3484                                 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3485                 }
3486                 
3487                 public override void Emit (EmitContext ec)
3488                 {
3489                         throw new Exception ("Should never be reached");
3490                 }
3491         }
3492
3493         //
3494         // Implements array access 
3495         //
3496         public class ArrayAccess : Expression, IAssignMethod {
3497                 //
3498                 // Points to our "data" repository
3499                 //
3500                 ElementAccess ea;
3501                 
3502                 public ArrayAccess (ElementAccess ea_data)
3503                 {
3504                         ea = ea_data;
3505                         eclass = ExprClass.Variable;
3506                 }
3507
3508                 public override Expression DoResolve (EmitContext ec)
3509                 {
3510                         if (ea.Expr.ExprClass != ExprClass.Variable) {
3511                                 report118 (ea.loc, ea.Expr, "variable");
3512                                 return null;
3513                         }
3514
3515                         Type t = ea.Expr.Type;
3516
3517                         if (t.GetArrayRank () != ea.Arguments.Count){
3518                                 Report.Error (22, ea.loc,
3519                                               "Incorrect number of indexes for array " +
3520                                               " expected: " + t.GetArrayRank () + " got: " +
3521                                               ea.Arguments.Count);
3522                                 return null;
3523                         }
3524                         type = t.GetElementType ();
3525                         eclass = ExprClass.Variable;
3526
3527                         return this;
3528                 }
3529
3530                 // <summary>
3531                 //    Emits the right opcode to load an object of Type `t'
3532                 //    from an array of T
3533                 // </summary>
3534                 static public void EmitLoadOpcode (ILGenerator ig, Type type)
3535                 {
3536                         if (type == TypeManager.byte_type)
3537                                 ig.Emit (OpCodes.Ldelem_I1);
3538                         else if (type == TypeManager.sbyte_type)
3539                                 ig.Emit (OpCodes.Ldelem_U1);
3540                         else if (type == TypeManager.short_type)
3541                                 ig.Emit (OpCodes.Ldelem_I2);
3542                         else if (type == TypeManager.ushort_type)
3543                                 ig.Emit (OpCodes.Ldelem_U2);
3544                         else if (type == TypeManager.int32_type)
3545                                 ig.Emit (OpCodes.Ldelem_I4);
3546                         else if (type == TypeManager.uint32_type)
3547                                 ig.Emit (OpCodes.Ldelem_U4);
3548                         else if (type == TypeManager.uint64_type)
3549                                 ig.Emit (OpCodes.Ldelem_I8);
3550                         else if (type == TypeManager.int64_type)
3551                                 ig.Emit (OpCodes.Ldelem_I8);
3552                         else if (type == TypeManager.float_type)
3553                                 ig.Emit (OpCodes.Ldelem_R4);
3554                         else if (type == TypeManager.double_type)
3555                                 ig.Emit (OpCodes.Ldelem_R8);
3556                         else if (type == TypeManager.intptr_type)
3557                                 ig.Emit (OpCodes.Ldelem_I);
3558                         else
3559                                 ig.Emit (OpCodes.Ldelem_Ref);
3560                 }
3561                 
3562                 public override void Emit (EmitContext ec)
3563                 {
3564                         int rank = ea.Expr.Type.GetArrayRank ();
3565                         ILGenerator ig = ec.ig;
3566
3567                         ea.Expr.Emit (ec);
3568
3569                         foreach (Argument a in ea.Arguments)
3570                                 a.Expr.Emit (ec);
3571
3572                         if (rank == 1)
3573                                 EmitLoadOpcode (ig, type);
3574                         else {
3575                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3576                                 Type [] args = new Type [ea.Arguments.Count];
3577                                 MethodInfo get;
3578                                 
3579                                 int i = 0;
3580                                 
3581                                 foreach (Argument a in ea.Arguments)
3582                                         args [i++] = a.Type;
3583                                 
3584                                 get = mb.GetArrayMethod (
3585                                         ea.Expr.Type, "Get",
3586                                         CallingConventions.HasThis |
3587                                         CallingConventions.Standard,
3588                                         type, args);
3589                                 
3590                                 ig.Emit (OpCodes.Call, get);
3591                         }
3592                 }
3593
3594                 public void EmitAssign (EmitContext ec, Expression source)
3595                 {
3596                         int rank = ea.Expr.Type.GetArrayRank ();
3597                         ILGenerator ig = ec.ig;
3598
3599                         ea.Expr.Emit (ec);
3600
3601                         foreach (Argument a in ea.Arguments)
3602                                 a.Expr.Emit (ec);
3603
3604                         source.Emit (ec);
3605
3606                         Type t = source.Type;
3607                         if (rank == 1){
3608                                 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3609                                         ig.Emit (OpCodes.Stelem_I1);
3610                                 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3611                                         ig.Emit (OpCodes.Stelem_I2);
3612                                 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3613                                         ig.Emit (OpCodes.Stelem_I4);
3614                                 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3615                                         ig.Emit (OpCodes.Stelem_I8);
3616                                 else if (t == TypeManager.float_type)
3617                                         ig.Emit (OpCodes.Stelem_R4);
3618                                 else if (t == TypeManager.double_type)
3619                                         ig.Emit (OpCodes.Stelem_R8);
3620                                 else if (t == TypeManager.intptr_type)
3621                                         ig.Emit (OpCodes.Stelem_I);
3622                                 else
3623                                         ig.Emit (OpCodes.Stelem_Ref);
3624                         } else {
3625                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3626                                 Type [] args = new Type [ea.Arguments.Count + 1];
3627                                 MethodInfo set;
3628                                 
3629                                 int i = 0;
3630                                 
3631                                 foreach (Argument a in ea.Arguments)
3632                                         args [i++] = a.Type;
3633
3634                                 args [i] = type;
3635                                 
3636                                 set = mb.GetArrayMethod (
3637                                         ea.Expr.Type, "Set",
3638                                         CallingConventions.HasThis |
3639                                         CallingConventions.Standard,
3640                                         TypeManager.void_type, args);
3641                                 
3642                                 ig.Emit (OpCodes.Call, set);
3643                         }
3644                 }
3645         }
3646         class Indexers {
3647                 public ArrayList getters, setters;
3648                 static Hashtable map;
3649
3650                 static Indexers ()
3651                 {
3652                         map = new Hashtable ();
3653                 }
3654
3655                 Indexers (MemberInfo [] mi)
3656                 {
3657                         foreach (PropertyInfo property in mi){
3658                                 MethodInfo get, set;
3659                                 
3660                                 get = property.GetGetMethod (true);
3661                                 if (get != null){
3662                                         if (getters == null)
3663                                                 getters = new ArrayList ();
3664
3665                                         getters.Add (get);
3666                                 }
3667                                 
3668                                 set = property.GetSetMethod (true);
3669                                 if (set != null){
3670                                         if (setters == null)
3671                                                 setters = new ArrayList ();
3672                                         setters.Add (set);
3673                                 }
3674                         }
3675                 }
3676                 
3677                 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc) 
3678                 {
3679                         Indexers ix = (Indexers) map [t];
3680                         string p_name = TypeManager.IndexerPropertyName (t);
3681                         
3682                         if (ix != null)
3683                                 return ix;
3684
3685                         MemberInfo [] mi = tm.FindMembers (
3686                                 t, MemberTypes.Property,
3687                                 BindingFlags.Public | BindingFlags.Instance,
3688                                 Type.FilterName, p_name);
3689
3690                         if (mi == null || mi.Length == 0){
3691                                 Report.Error (21, loc,
3692                                               "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3693                                               "any indexers defined");
3694                                 return null;
3695                         }
3696                         
3697                         ix = new Indexers (mi);
3698                         map [t] = ix;
3699
3700                         return ix;
3701                 }
3702         }
3703
3704         // <summary>
3705         //   Expressions that represent an indexer call.
3706         // </summary>
3707         public class IndexerAccess : Expression, IAssignMethod {
3708                 //
3709                 // Points to our "data" repository
3710                 //
3711                 ElementAccess ea;
3712                 MethodInfo get, set;
3713                 Indexers ilist;
3714                 ArrayList set_arguments;
3715                 
3716                 public IndexerAccess (ElementAccess ea_data)
3717                 {
3718                         ea = ea_data;
3719                         eclass = ExprClass.Value;
3720                 }
3721
3722                 public override Expression DoResolve (EmitContext ec)
3723                 {
3724                         Type indexer_type = ea.Expr.Type;
3725                         
3726                         //
3727                         // Step 1: Query for all `Item' *properties*.  Notice
3728                         // that the actual methods are pointed from here.
3729                         //
3730                         // This is a group of properties, piles of them.  
3731
3732                         if (ilist == null)
3733                                 ilist = Indexers.GetIndexersForType (
3734                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3735
3736
3737                         //
3738                         // Step 2: find the proper match
3739                         //
3740                         if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3741                                 get = (MethodInfo) Invocation.OverloadResolve (
3742                                         ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3743
3744                         if (get == null){
3745                                 Report.Error (154, ea.loc,
3746                                               "indexer can not be used in this context, because " +
3747                                               "it lacks a `get' accessor");
3748                                 return null;
3749                         }
3750
3751                         type = get.ReturnType;
3752                         eclass = ExprClass.IndexerAccess;
3753                         return this;
3754                 }
3755
3756                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3757                 {
3758                         Type indexer_type = ea.Expr.Type;
3759                         Type right_type = right_side.Type;
3760
3761                         if (ilist == null)
3762                                 ilist = Indexers.GetIndexersForType (
3763                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3764
3765                         if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3766                                 set_arguments = (ArrayList) ea.Arguments.Clone ();
3767                                 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3768
3769                                 set = (MethodInfo) Invocation.OverloadResolve (
3770                                         ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3771                         }
3772                         
3773                         if (set == null){
3774                                 Report.Error (200, ea.loc,
3775                                               "indexer X.this [" + TypeManager.CSharpName (right_type) +
3776                                               "] lacks a `set' accessor");
3777                                         return null;
3778                         }
3779
3780                         type = TypeManager.void_type;
3781                         eclass = ExprClass.IndexerAccess;
3782                         return this;
3783                 }
3784                 
3785                 public override void Emit (EmitContext ec)
3786                 {
3787                         Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3788                 }
3789
3790                 //
3791                 // source is ignored, because we already have a copy of it from the
3792                 // LValue resolution and we have already constructed a pre-cached
3793                 // version of the arguments (ea.set_arguments);
3794                 //
3795                 public void EmitAssign (EmitContext ec, Expression source)
3796                 {
3797                         Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3798                 }
3799         }
3800         
3801         public class BaseAccess : Expression {
3802
3803                 public enum BaseAccessType : byte {
3804                         Member,
3805                         Indexer
3806                 };
3807                 
3808                 public readonly BaseAccessType BAType;
3809                 public readonly string         Member;
3810                 public readonly ArrayList      Arguments;
3811
3812                 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3813                 {
3814                         BAType = t;
3815                         Member = member;
3816                         Arguments = args;
3817                         
3818                 }
3819
3820                 public override Expression DoResolve (EmitContext ec)
3821                 {
3822                         // FIXME: Implement;
3823                         throw new Exception ("Unimplemented");
3824                         // return this;
3825                 }
3826
3827                 public override void Emit (EmitContext ec)
3828                 {
3829                         throw new Exception ("Unimplemented");
3830                 }
3831         }
3832
3833         // <summary>
3834         //   This class exists solely to pass the Type around and to be a dummy
3835         //   that can be passed to the conversion functions (this is used by
3836         //   foreach implementation to typecast the object return value from
3837         //   get_Current into the proper type.  All code has been generated and
3838         //   we only care about the side effect conversions to be performed
3839         // </summary>
3840         
3841         public class EmptyExpression : Expression {
3842                 public EmptyExpression ()
3843                 {
3844                         type = TypeManager.object_type;
3845                         eclass = ExprClass.Value;
3846                 }
3847
3848                 public EmptyExpression (Type t)
3849                 {
3850                         type = t;
3851                         eclass = ExprClass.Value;
3852                 }
3853                 
3854                 public override Expression DoResolve (EmitContext ec)
3855                 {
3856                         return this;
3857                 }
3858
3859                 public override void Emit (EmitContext ec)
3860                 {
3861                         // nothing, as we only exist to not do anything.
3862                 }
3863         }
3864
3865         public class UserCast : Expression {
3866                 MethodBase method;
3867                 Expression source;
3868                 
3869                 public UserCast (MethodInfo method, Expression source)
3870                 {
3871                         this.method = method;
3872                         this.source = source;
3873                         type = method.ReturnType;
3874                         eclass = ExprClass.Value;
3875                 }
3876
3877                 public override Expression DoResolve (EmitContext ec)
3878                 {
3879                         //
3880                         // We are born fully resolved
3881                         //
3882                         return this;
3883                 }
3884
3885                 public override void Emit (EmitContext ec)
3886                 {
3887                         ILGenerator ig = ec.ig;
3888
3889                         source.Emit (ec);
3890                         
3891                         if (method is MethodInfo)
3892                                 ig.Emit (OpCodes.Call, (MethodInfo) method);
3893                         else
3894                                 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3895
3896                 }
3897
3898         }
3899 }