2001-11-15 Miguel de Icaza <miguel@ximian.com>
[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,
2483                                                                                 Location.Null);
2484                                         else
2485                                                 conv = ConvertImplicit (ec, a_expr, parameter_type,
2486                                                                         Location.Null);
2487
2488                                         if (conv == null) {
2489                                                 if (!Location.IsNull (loc)) {
2490                                                         Error (1502, loc,
2491                                                         "The best overloaded match for method '" + FullMethodDesc (method)+
2492                                                                "' has some invalid arguments");
2493                                                         Error (1503, loc,
2494                                                          "Argument " + (j+1) +
2495                                                          ": Cannot convert from '" + Argument.FullDesc (a) 
2496                                                          + "' to '" + pd.ParameterDesc (j) + "'");
2497                                                 }
2498                                                 return null;
2499                                         }
2500                                         
2501                         
2502                                         
2503                                         //
2504                                         // Update the argument with the implicit conversion
2505                                         //
2506                                         if (a_expr != conv)
2507                                                 a.Expr = conv;
2508
2509                                         // FIXME : For the case of params methods, we need to actually instantiate
2510                                         // an array and initialize it with the argument values etc etc.
2511
2512                                 }
2513                                 
2514                                 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2515                                     pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2516                                         if (!Location.IsNull (loc)) {
2517                                                 Error (1502, loc,
2518                                                        "The best overloaded match for method '" + FullMethodDesc (method)+
2519                                                        "' has some invalid arguments");
2520                                                 Error (1503, loc,
2521                                                        "Argument " + (j+1) +
2522                                                        ": Cannot convert from '" + Argument.FullDesc (a) 
2523                                                        + "' to '" + pd.ParameterDesc (j) + "'");
2524                                         }
2525                                         return null;
2526                                 }
2527                                 
2528                                 
2529                         }
2530                         
2531                         return method;
2532                 }
2533                 
2534                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2535                                                           ArrayList Arguments, Location loc)
2536                 {
2537                         return OverloadResolve (ec, me, Arguments, loc, false);
2538                 }
2539                         
2540                 public override Expression DoResolve (EmitContext ec)
2541                 {
2542                         //
2543                         // First, resolve the expression that is used to
2544                         // trigger the invocation
2545                         //
2546                         expr = expr.Resolve (ec);
2547                         if (expr == null)
2548                                 return null;
2549
2550                         if (!(expr is MethodGroupExpr)) {
2551                                 Type expr_type = expr.Type;
2552
2553                                 if (expr_type != null){
2554                                         bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2555                                         if (IsDelegate)
2556                                                 return (new DelegateInvocation (
2557                                                         this.expr, Arguments, loc)).Resolve (ec);
2558                                 }
2559                         }
2560
2561                         if (!(expr is MethodGroupExpr)){
2562                                 report118 (loc, this.expr, "method group");
2563                                 return null;
2564                         }
2565
2566                         //
2567                         // Next, evaluate all the expressions in the argument list
2568                         //
2569                         if (Arguments != null){
2570                                 for (int i = Arguments.Count; i > 0;){
2571                                         --i;
2572                                         Argument a = (Argument) Arguments [i];
2573
2574                                         if (!a.Resolve (ec, loc))
2575                                                 return null;
2576                                 }
2577                         }
2578
2579                         method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
2580
2581                         if (method == null){
2582                                 Error (-6, loc,
2583                                        "Could not find any applicable function for this argument list");
2584                                 return null;
2585                         }
2586
2587                         if (method is MethodInfo)
2588                                 type = ((MethodInfo)method).ReturnType;
2589
2590                         eclass = ExprClass.Value;
2591                         return this;
2592                 }
2593
2594                 // <summary>
2595                 //   Emits the list of arguments as an array
2596                 // </summary>
2597                 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
2598                 {
2599                         ILGenerator ig = ec.ig;
2600                         int count = arguments.Count - idx;
2601                         Argument a = (Argument) arguments [idx];
2602                         Type t = a.expr.Type;
2603                         string array_type = t.FullName + "[]";
2604                         LocalBuilder array;
2605                         
2606                         array = ec.GetTemporaryStorage (Type.GetType (array_type));
2607                         IntLiteral.EmitInt (ig, count);
2608                         ig.Emit (OpCodes.Newarr, t);
2609                         ig.Emit (OpCodes.Stloc, array);
2610
2611                         int top = arguments.Count;
2612                         for (int j = idx; j < top; j++){
2613                                 ig.Emit (OpCodes.Ldloc, array);
2614                                 IntLiteral.EmitInt (ig, j - idx);
2615                                 a.Emit (ec);
2616                                 ig.Emit (OpCodes.Stelem_Ref);
2617                         }
2618                         ig.Emit (OpCodes.Ldloc, array);
2619                 }
2620                 
2621                 // <summary>
2622                 //   Emits a list of resolved Arguments that are in the arguments
2623                 //   ArrayList.
2624                 // 
2625                 //   The MethodBase argument might be null if the
2626                 //   emission of the arguments is known not to contain
2627                 //   a `params' field (for example in constructors or other routines
2628                 //   that keep their arguments in this structure
2629                 // </summary>
2630                 
2631                 public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
2632                 {
2633                         ParameterData pd = null;
2634                         int top;
2635
2636                         if (arguments != null)
2637                                 top = arguments.Count;
2638                         else
2639                                 top = 0;
2640
2641                         if (mb != null)
2642                                  pd = GetParameterData (mb);
2643
2644                         for (int i = 0; i < top; i++){
2645                                 Argument a = (Argument) arguments [i];
2646
2647                                 if (pd != null){
2648                                         if (pd.ParameterModifier (i) == Parameter.Modifier.PARAMS){
2649                                                 EmitParams (ec, i, arguments);
2650                                                 return;
2651                                         }
2652                                 }
2653                                             
2654                                 a.Emit (ec);
2655                         }
2656                 }
2657
2658                 public static void EmitCall (EmitContext ec,
2659                                              bool is_static, Expression instance_expr,
2660                                              MethodBase method, ArrayList Arguments)
2661                 {
2662                         ILGenerator ig = ec.ig;
2663                         bool struct_call = false;
2664                                 
2665                         if (!is_static){
2666                                 //
2667                                 // If this is ourselves, push "this"
2668                                 //
2669                                 if (instance_expr == null){
2670                                         ig.Emit (OpCodes.Ldarg_0);
2671                                 } else {
2672                                         //
2673                                         // Push the instance expression
2674                                         //
2675                                         if (instance_expr.Type.IsSubclassOf (TypeManager.value_type)){
2676
2677                                                 struct_call = true;
2678
2679                                                 //
2680                                                 // If the expression implements IMemoryLocation, then
2681                                                 // we can optimize and use AddressOf on the
2682                                                 // return.
2683                                                 //
2684                                                 // If not we have to use some temporary storage for
2685                                                 // it.
2686                                                 if (instance_expr is IMemoryLocation)
2687                                                         ((IMemoryLocation) instance_expr).AddressOf (ec);
2688                                                 else {
2689                                                         Type t = instance_expr.Type;
2690                                                         
2691                                                         instance_expr.Emit (ec);
2692                                                         LocalBuilder temp = ec.GetTemporaryStorage (t);
2693                                                         ig.Emit (OpCodes.Stloc, temp);
2694                                                         ig.Emit (OpCodes.Ldloca, temp);
2695                                                 }
2696                                         } else 
2697                                                 instance_expr.Emit (ec);
2698                                 }
2699                         }
2700
2701                         if (Arguments != null)
2702                                 EmitArguments (ec, method, Arguments);
2703
2704                         if (is_static || struct_call){
2705                                 if (method is MethodInfo)
2706                                         ig.Emit (OpCodes.Call, (MethodInfo) method);
2707                                 else
2708                                         ig.Emit (OpCodes.Call, (ConstructorInfo) method);
2709                         } else {
2710                                 if (method is MethodInfo)
2711                                         ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
2712                                 else
2713                                         ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
2714                         }
2715                 }
2716                 
2717                 public override void Emit (EmitContext ec)
2718                 {
2719                         MethodGroupExpr mg = (MethodGroupExpr) this.expr;
2720                         EmitCall (ec, method.IsStatic, mg.InstanceExpression, method, Arguments);
2721                 }
2722                 
2723                 public override void EmitStatement (EmitContext ec)
2724                 {
2725                         Emit (ec);
2726
2727                         // 
2728                         // Pop the return value if there is one
2729                         //
2730                         if (method is MethodInfo){
2731                                 if (((MethodInfo)method).ReturnType != TypeManager.void_type)
2732                                         ec.ig.Emit (OpCodes.Pop);
2733                         }
2734                 }
2735         }
2736
2737         public class New : ExpressionStatement {
2738                 public readonly ArrayList Arguments;
2739                 public readonly string    RequestedType;
2740
2741                 Location loc;
2742                 MethodBase method = null;
2743
2744                 //
2745                 // If set, the new expression is for a value_target, and
2746                 // we will not leave anything on the stack.
2747                 //
2748                 Expression value_target;
2749                 
2750                 public New (string requested_type, ArrayList arguments, Location l)
2751                 {
2752                         RequestedType = requested_type;
2753                         Arguments = arguments;
2754                         loc = l;
2755                 }
2756
2757                 public Expression ValueTypeVariable {
2758                         get {
2759                                 return value_target;
2760                         }
2761
2762                         set {
2763                                 value_target = value;
2764                         }
2765                 }
2766
2767                 public override Expression DoResolve (EmitContext ec)
2768                 {
2769                         type = ec.TypeContainer.LookupType (RequestedType, false);
2770                         
2771                         if (type == null)
2772                                 return null;
2773                         
2774                         bool IsDelegate = TypeManager.IsDelegateType (type);
2775                         
2776                         if (IsDelegate)
2777                                 return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
2778                         
2779                         Expression ml;
2780                         
2781                         ml = MemberLookup (ec, type, ".ctor", false,
2782                                            MemberTypes.Constructor, AllBindingsFlags, loc);
2783                         
2784                         bool is_struct = false;
2785                         is_struct = type.IsSubclassOf (TypeManager.value_type);
2786                         
2787                         if (! (ml is MethodGroupExpr)){
2788                                 if (!is_struct){
2789                                         report118 (loc, ml, "method group");
2790                                         return null;
2791                                 }
2792                         }
2793                         
2794                         if (ml != null) {
2795                                 if (Arguments != null){
2796                                         for (int i = Arguments.Count; i > 0;){
2797                                                 --i;
2798                                                 Argument a = (Argument) Arguments [i];
2799                                                 
2800                                                 if (!a.Resolve (ec, loc))
2801                                                         return null;
2802                                         }
2803                                 }
2804
2805                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml,
2806                                                                      Arguments, loc);
2807                         }
2808                         
2809                         if (method == null && !is_struct) {
2810                                 Error (-6, loc,
2811                                        "New invocation: Can not find a constructor for " +
2812                                        "this argument list");
2813                                 return null;
2814                         }
2815                         
2816                         eclass = ExprClass.Value;
2817                         return this;
2818                 }
2819
2820                 //
2821                 // This DoEmit can be invoked in two contexts:
2822                 //    * As a mechanism that will leave a value on the stack (new object)
2823                 //    * As one that wont (init struct)
2824                 //
2825                 // You can control whether a value is required on the stack by passing
2826                 // need_value_on_stack.  The code *might* leave a value on the stack
2827                 // so it must be popped manually
2828                 //
2829                 // Returns whether a value is left on the stack
2830                 //
2831                 bool DoEmit (EmitContext ec, bool need_value_on_stack)
2832                 {
2833                         if (method == null){
2834                                 IMemoryLocation ml = (IMemoryLocation) value_target;
2835
2836                                 ml.AddressOf (ec);
2837                         } else {
2838                                 Invocation.EmitArguments (ec, method, Arguments);
2839                                 ec.ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
2840                                 return true;
2841                         }
2842
2843                         //
2844                         // It must be a value type, sanity check
2845                         //
2846                         if (value_target != null){
2847                                 ec.ig.Emit (OpCodes.Initobj, type);
2848
2849                                 if (need_value_on_stack){
2850                                         value_target.Emit (ec);
2851                                         return true;
2852                                 }
2853                                 return false;
2854                         }
2855
2856                         throw new Exception ("No method and no value type");
2857                 }
2858
2859                 public override void Emit (EmitContext ec)
2860                 {
2861                         DoEmit (ec, true);
2862                 }
2863                 
2864                 public override void EmitStatement (EmitContext ec)
2865                 {
2866                         if (DoEmit (ec, false))
2867                                 ec.ig.Emit (OpCodes.Pop);
2868                 }
2869         }
2870
2871         // <summary>
2872         //   Represents an array creation expression.
2873         // </summary>
2874         //
2875         // <remarks>
2876         //   There are two possible scenarios here: one is an array creation
2877         //   expression that specifies the dimensions and optionally the
2878         //   initialization data
2879         // </remarks>
2880         public class ArrayCreation : ExpressionStatement {
2881
2882                 string RequestedType;
2883                 string Rank;
2884                 ArrayList Initializers;
2885                 Location  loc;
2886                 ArrayList Arguments;
2887
2888                 MethodBase method = null;
2889                 Type array_element_type;
2890                 bool IsOneDimensional = false;
2891                 
2892                 bool IsBuiltinType = false;
2893
2894                 public ArrayCreation (string requested_type, ArrayList exprs,
2895                                       string rank, ArrayList initializers, Location l)
2896                 {
2897                         RequestedType = requested_type;
2898                         Rank          = rank;
2899                         Initializers  = initializers;
2900                         loc = l;
2901
2902                         Arguments = new ArrayList ();
2903
2904                         foreach (Expression e in exprs)
2905                                 Arguments.Add (new Argument (e, Argument.AType.Expression));
2906                         
2907                 }
2908
2909                 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2910                 {
2911                         RequestedType = requested_type;
2912                         Rank = rank;
2913                         Initializers = initializers;
2914                         loc = l;
2915                 }
2916
2917                 public static string FormArrayType (string base_type, int idx_count, string rank)
2918                 {
2919                         StringBuilder sb = new StringBuilder (base_type);
2920
2921                         sb.Append (rank);
2922                         
2923                         sb.Append ("[");
2924                         for (int i = 1; i < idx_count; i++)
2925                                 sb.Append (",");
2926                         sb.Append ("]");
2927                         
2928                         return sb.ToString ();
2929                 }
2930
2931                 public static string FormElementType (string base_type, int idx_count, string rank)
2932                 {
2933                         StringBuilder sb = new StringBuilder (base_type);
2934                         
2935                         sb.Append ("[");
2936                         for (int i = 1; i < idx_count; i++)
2937                                 sb.Append (",");
2938                         sb.Append ("]");
2939
2940                         sb.Append (rank);
2941
2942                         string val = sb.ToString ();
2943
2944                         return val.Substring (0, val.LastIndexOf ("["));
2945                 }
2946                 
2947
2948                 public override Expression DoResolve (EmitContext ec)
2949                 {
2950                         int arg_count;
2951                         
2952                         if (Arguments == null)
2953                                 arg_count = 0;
2954                         else
2955                                 arg_count = Arguments.Count;
2956                         
2957                         string array_type = FormArrayType (RequestedType, arg_count, Rank);
2958
2959                         string element_type = FormElementType (RequestedType, arg_count, Rank);
2960
2961                         type = ec.TypeContainer.LookupType (array_type, false);
2962                         
2963                         array_element_type = ec.TypeContainer.LookupType (element_type, false);
2964                         
2965                         if (type == null)
2966                                 return null;
2967                         
2968                         if (arg_count == 1) {
2969                                 IsOneDimensional = true;
2970                                 eclass = ExprClass.Value;
2971                                 return this;
2972                         }
2973
2974                         IsBuiltinType = TypeManager.IsBuiltinType (type);
2975                         
2976                         if (IsBuiltinType) {
2977                                 
2978                                 Expression ml;
2979                                 
2980                                 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
2981                                                    AllBindingsFlags, loc);
2982                                 
2983                                 if (!(ml is MethodGroupExpr)){
2984                                         report118 (loc, ml, "method group");
2985                                         return null;
2986                                 }
2987                                 
2988                                 if (ml == null) {
2989                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
2990                                                       "this argument list");
2991                                         return null;
2992                                 }
2993                                 
2994                                 if (Arguments != null) {
2995                                         for (int i = arg_count; i > 0;){
2996                                                 --i;
2997                                                 Argument a = (Argument) Arguments [i];
2998                                                 
2999                                                 if (!a.Resolve (ec, loc))
3000                                                         return null;
3001                                         }
3002                                 }
3003                                 
3004                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3005                                 
3006                                 if (method == null) {
3007                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3008                                                       "this argument list");
3009                                         return null;
3010                                 }
3011                                 
3012                                 eclass = ExprClass.Value;
3013                                 return this;
3014                                 
3015                         } else {
3016
3017                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3018
3019                                 ArrayList args = new ArrayList ();
3020                                 if (Arguments != null){
3021                                         for (int i = arg_count; i > 0;){
3022                                                 --i;
3023                                                 Argument a = (Argument) Arguments [i];
3024                                                 
3025                                                 if (!a.Resolve (ec, loc))
3026                                                         return null;
3027                                                 
3028                                                 args.Add (a.Type);
3029                                         }
3030                                 }
3031                                 
3032                                 Type [] arg_types = null;
3033                                 
3034                                 if (args.Count > 0)
3035                                         arg_types = new Type [args.Count];
3036                                 
3037                                 args.CopyTo (arg_types, 0);
3038                                 
3039                                 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3040                                                             arg_types);
3041                                 
3042                                 if (method == null) {
3043                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3044                                                       "this argument list");
3045                                         return null;
3046                                 }
3047                                 
3048                                 eclass = ExprClass.Value;
3049                                 return this;
3050                                 
3051                         }
3052                 }
3053
3054                 public override void Emit (EmitContext ec)
3055                 {
3056                         ILGenerator ig = ec.ig;
3057                         
3058                         if (IsOneDimensional) {
3059                                 Invocation.EmitArguments (ec, null, Arguments);
3060                                 ig.Emit (OpCodes.Newarr, array_element_type);
3061                                 
3062                         } else {
3063                                 Invocation.EmitArguments (ec, method, Arguments);
3064
3065                                 if (IsBuiltinType)
3066                                         ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3067                                 else
3068                                         ig.Emit (OpCodes.Newobj, (MethodInfo) method);
3069                         }
3070
3071                         if (Initializers != null){
3072                                 FieldBuilder fb;
3073
3074                                 // FIXME: This is just sample data, need to fill with
3075                                 // real values.
3076                                 byte [] a = new byte [4] { 1, 2, 3, 4 };
3077                                 
3078                                 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
3079
3080                                 ig.Emit (OpCodes.Dup);
3081                                 ig.Emit (OpCodes.Ldtoken, fb);
3082                                 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
3083                         }
3084                 }
3085                 
3086                 public override void EmitStatement (EmitContext ec)
3087                 {
3088                         Emit (ec);
3089                         ec.ig.Emit (OpCodes.Pop);
3090                 }
3091                 
3092         }
3093         
3094         //
3095         // Represents the `this' construct
3096         //
3097         public class This : Expression, IAssignMethod, IMemoryLocation {
3098                 Location loc;
3099                 
3100                 public This (Location loc)
3101                 {
3102                         this.loc = loc;
3103                 }
3104
3105                 public override Expression DoResolve (EmitContext ec)
3106                 {
3107                         eclass = ExprClass.Variable;
3108                         type = ec.TypeContainer.TypeBuilder;
3109
3110                         if (ec.IsStatic){
3111                                 Report.Error (26, loc,
3112                                               "Keyword this not valid in static code");
3113                                 return null;
3114                         }
3115                         
3116                         return this;
3117                 }
3118
3119                 public Expression DoResolveLValue (EmitContext ec)
3120                 {
3121                         DoResolve (ec);
3122                         
3123                         if (ec.TypeContainer is Class){
3124                                 Report.Error (1604, loc, "Cannot assign to `this'");
3125                                 return null;
3126                         }
3127
3128                         return this;
3129                 }
3130
3131                 public override void Emit (EmitContext ec)
3132                 {
3133                         ec.ig.Emit (OpCodes.Ldarg_0);
3134                 }
3135
3136                 public void EmitAssign (EmitContext ec, Expression source)
3137                 {
3138                         source.Emit (ec);
3139                         ec.ig.Emit (OpCodes.Starg, 0);
3140                 }
3141
3142                 public void AddressOf (EmitContext ec)
3143                 {
3144                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3145                 }
3146         }
3147
3148         // <summary>
3149         //   Implements the typeof operator
3150         // </summary>
3151         public class TypeOf : Expression {
3152                 public readonly string QueriedType;
3153                 Type typearg;
3154                 
3155                 public TypeOf (string queried_type)
3156                 {
3157                         QueriedType = queried_type;
3158                 }
3159
3160                 public override Expression DoResolve (EmitContext ec)
3161                 {
3162                         typearg = ec.TypeContainer.LookupType (QueriedType, false);
3163
3164                         if (typearg == null)
3165                                 return null;
3166
3167                         type = TypeManager.type_type;
3168                         eclass = ExprClass.Type;
3169                         return this;
3170                 }
3171
3172                 public override void Emit (EmitContext ec)
3173                 {
3174                         ec.ig.Emit (OpCodes.Ldtoken, typearg);
3175                         ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3176                 }
3177         }
3178
3179         public class SizeOf : Expression {
3180                 public readonly string QueriedType;
3181                 
3182                 public SizeOf (string queried_type)
3183                 {
3184                         this.QueriedType = queried_type;
3185                 }
3186
3187                 public override Expression DoResolve (EmitContext ec)
3188                 {
3189                         // FIXME: Implement;
3190                         throw new Exception ("Unimplemented");
3191                         // return this;
3192                 }
3193
3194                 public override void Emit (EmitContext ec)
3195                 {
3196                         throw new Exception ("Implement me");
3197                 }
3198         }
3199
3200         public class MemberAccess : Expression {
3201                 public readonly string Identifier;
3202                 Expression expr;
3203                 Expression member_lookup;
3204                 Location loc;
3205                 
3206                 public MemberAccess (Expression expr, string id, Location l)
3207                 {
3208                         this.expr = expr;
3209                         Identifier = id;
3210                         loc = l;
3211                 }
3212
3213                 public Expression Expr {
3214                         get {
3215                                 return expr;
3216                         }
3217                 }
3218
3219                 void error176 (Location loc, string name)
3220                 {
3221                         Report.Error (176, loc, "Static member `" +
3222                                       name + "' cannot be accessed " +
3223                                       "with an instance reference, qualify with a " +
3224                                       "type name instead");
3225                 }
3226                 
3227                 public override Expression DoResolve (EmitContext ec)
3228                 {
3229                         //
3230                         // We are the sole users of ResolveWithSimpleName (ie, the only
3231                         // ones that can cope with it
3232                         //
3233                         expr = expr.ResolveWithSimpleName (ec);
3234
3235                         if (expr == null)
3236                                 return null;
3237
3238                         if (expr is SimpleName){
3239                                 SimpleName child_expr = (SimpleName) expr;
3240
3241                                 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3242
3243                                 return expr.Resolve (ec);
3244                         }
3245                                         
3246                         member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3247
3248                         if (member_lookup == null)
3249                                 return null;
3250                         
3251                         //
3252                         // Method Groups
3253                         //
3254                         if (member_lookup is MethodGroupExpr){
3255                                 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3256                                 
3257                                 //
3258                                 // Type.MethodGroup
3259                                 //
3260                                 if (expr is TypeExpr){
3261                                         if (!mg.RemoveInstanceMethods ()){
3262                                                 SimpleName.Error120 (loc, mg.Methods [0].Name); 
3263                                                 return null;
3264                                         }
3265
3266                                         return member_lookup;
3267                                 }
3268
3269                                 //
3270                                 // Instance.MethodGroup
3271                                 //
3272                                 if (!mg.RemoveStaticMethods ()){
3273                                         error176 (loc, mg.Methods [0].Name);
3274                                         return null;
3275                                 }
3276                                 
3277                                 mg.InstanceExpression = expr;
3278                                         
3279                                 return member_lookup;
3280                         }
3281
3282                         if (member_lookup is FieldExpr){
3283                                 FieldExpr fe = (FieldExpr) member_lookup;
3284                                 FieldInfo fi = fe.FieldInfo;
3285
3286                                 if (fi.IsLiteral) {
3287                                         Type t = fi.FieldType;
3288                                         object o;
3289
3290                                         if (fi is FieldBuilder)
3291                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3292                                         else
3293                                                 o = fi.GetValue (fi);
3294                                         
3295                                         if (t.IsSubclassOf (TypeManager.enum_type)) {
3296                                                 Expression enum_member = MemberLookup (ec, t, "value__", false, loc); 
3297                                                 Type underlying_type = enum_member.Type;
3298                                                 
3299                                                 Expression e = Literalize (o, underlying_type);
3300                                                 e.Resolve (ec);
3301                                         
3302                                                 return new EnumLiteral (e, t);
3303                                         }
3304
3305                                         Expression exp = Literalize (o, t);
3306                                         exp.Resolve (ec);
3307                                         
3308                                         return exp;
3309                                 }
3310                                 
3311                                 if (expr is TypeExpr){
3312                                         if (!fe.FieldInfo.IsStatic){
3313                                                 error176 (loc, fe.FieldInfo.Name);
3314                                                 return null;
3315                                         }
3316                                         return member_lookup;
3317                                 } else {
3318                                         if (fe.FieldInfo.IsStatic){
3319                                                 error176 (loc, fe.FieldInfo.Name);
3320                                                 return null;
3321                                         }
3322                                         fe.InstanceExpression = expr;
3323
3324                                         return fe;
3325                                 }
3326                         }
3327
3328                         if (member_lookup is PropertyExpr){
3329                                 PropertyExpr pe = (PropertyExpr) member_lookup;
3330
3331                                 if (expr is TypeExpr){
3332                                         if (!pe.IsStatic){
3333                                                 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3334                                                 return null;
3335                                         }
3336                                         return pe;
3337                                 } else {
3338                                         if (pe.IsStatic){
3339                                                 error176 (loc, pe.PropertyInfo.Name);
3340                                                 return null;
3341                                         }
3342                                         pe.InstanceExpression = expr;
3343
3344                                         return pe;
3345                                 }
3346                         }
3347                         
3348                         Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3349                         Environment.Exit (0);
3350                         return null;
3351                 }
3352
3353                 public override void Emit (EmitContext ec)
3354                 {
3355                         throw new Exception ("Should not happen I think");
3356                 }
3357
3358         }
3359
3360         public class CheckedExpr : Expression {
3361
3362                 public Expression Expr;
3363
3364                 public CheckedExpr (Expression e)
3365                 {
3366                         Expr = e;
3367                 }
3368
3369                 public override Expression DoResolve (EmitContext ec)
3370                 {
3371                         Expr = Expr.Resolve (ec);
3372
3373                         if (Expr == null)
3374                                 return null;
3375
3376                         eclass = Expr.ExprClass;
3377                         type = Expr.Type;
3378                         return this;
3379                 }
3380
3381                 public override void Emit (EmitContext ec)
3382                 {
3383                         bool last_check = ec.CheckState;
3384                         
3385                         ec.CheckState = true;
3386                         Expr.Emit (ec);
3387                         ec.CheckState = last_check;
3388                 }
3389                 
3390         }
3391
3392         public class UnCheckedExpr : Expression {
3393
3394                 public Expression Expr;
3395
3396                 public UnCheckedExpr (Expression e)
3397                 {
3398                         Expr = e;
3399                 }
3400
3401                 public override Expression DoResolve (EmitContext ec)
3402                 {
3403                         Expr = Expr.Resolve (ec);
3404
3405                         if (Expr == null)
3406                                 return null;
3407
3408                         eclass = Expr.ExprClass;
3409                         type = Expr.Type;
3410                         return this;
3411                 }
3412
3413                 public override void Emit (EmitContext ec)
3414                 {
3415                         bool last_check = ec.CheckState;
3416                         
3417                         ec.CheckState = false;
3418                         Expr.Emit (ec);
3419                         ec.CheckState = last_check;
3420                 }
3421                 
3422         }
3423
3424         public class ElementAccess : Expression {
3425                 public ArrayList  Arguments;
3426                 public Expression Expr;
3427                 public Location   loc;
3428                 
3429                 public ElementAccess (Expression e, ArrayList e_list, Location l)
3430                 {
3431                         Expr = e;
3432
3433                         Arguments = new ArrayList ();
3434                         foreach (Expression tmp in e_list)
3435                                 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3436                         
3437                         loc  = l;
3438                 }
3439
3440                 bool CommonResolve (EmitContext ec)
3441                 {
3442                         Expr = Expr.Resolve (ec);
3443
3444                         if (Expr == null) 
3445                                 return false;
3446
3447                         if (Arguments == null)
3448                                 return false;
3449
3450                         for (int i = Arguments.Count; i > 0;){
3451                                 --i;
3452                                 Argument a = (Argument) Arguments [i];
3453                                 
3454                                 if (!a.Resolve (ec, loc))
3455                                         return false;
3456                         }
3457
3458                         return true;
3459                 }
3460                                 
3461                 public override Expression DoResolve (EmitContext ec)
3462                 {
3463                         if (!CommonResolve (ec))
3464                                 return null;
3465
3466                         //
3467                         // We perform some simple tests, and then to "split" the emit and store
3468                         // code we create an instance of a different class, and return that.
3469                         //
3470                         // I am experimenting with this pattern.
3471                         //
3472                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3473                                 return (new ArrayAccess (this)).Resolve (ec);
3474                         else
3475                                 return (new IndexerAccess (this)).Resolve (ec);
3476                 }
3477
3478                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3479                 {
3480                         if (!CommonResolve (ec))
3481                                 return null;
3482
3483                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3484                                 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3485                         else
3486                                 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3487                 }
3488                 
3489                 public override void Emit (EmitContext ec)
3490                 {
3491                         throw new Exception ("Should never be reached");
3492                 }
3493         }
3494
3495         //
3496         // Implements array access 
3497         //
3498         public class ArrayAccess : Expression, IAssignMethod {
3499                 //
3500                 // Points to our "data" repository
3501                 //
3502                 ElementAccess ea;
3503                 
3504                 public ArrayAccess (ElementAccess ea_data)
3505                 {
3506                         ea = ea_data;
3507                         eclass = ExprClass.Variable;
3508                 }
3509
3510                 public override Expression DoResolve (EmitContext ec)
3511                 {
3512                         if (ea.Expr.ExprClass != ExprClass.Variable) {
3513                                 report118 (ea.loc, ea.Expr, "variable");
3514                                 return null;
3515                         }
3516
3517                         Type t = ea.Expr.Type;
3518
3519                         if (t.GetArrayRank () != ea.Arguments.Count){
3520                                 Report.Error (22, ea.loc,
3521                                               "Incorrect number of indexes for array " +
3522                                               " expected: " + t.GetArrayRank () + " got: " +
3523                                               ea.Arguments.Count);
3524                                 return null;
3525                         }
3526                         type = t.GetElementType ();
3527                         eclass = ExprClass.Variable;
3528
3529                         return this;
3530                 }
3531
3532                 // <summary>
3533                 //    Emits the right opcode to load an object of Type `t'
3534                 //    from an array of T
3535                 // </summary>
3536                 static public void EmitLoadOpcode (ILGenerator ig, Type type)
3537                 {
3538                         if (type == TypeManager.byte_type)
3539                                 ig.Emit (OpCodes.Ldelem_I1);
3540                         else if (type == TypeManager.sbyte_type)
3541                                 ig.Emit (OpCodes.Ldelem_U1);
3542                         else if (type == TypeManager.short_type)
3543                                 ig.Emit (OpCodes.Ldelem_I2);
3544                         else if (type == TypeManager.ushort_type)
3545                                 ig.Emit (OpCodes.Ldelem_U2);
3546                         else if (type == TypeManager.int32_type)
3547                                 ig.Emit (OpCodes.Ldelem_I4);
3548                         else if (type == TypeManager.uint32_type)
3549                                 ig.Emit (OpCodes.Ldelem_U4);
3550                         else if (type == TypeManager.uint64_type)
3551                                 ig.Emit (OpCodes.Ldelem_I8);
3552                         else if (type == TypeManager.int64_type)
3553                                 ig.Emit (OpCodes.Ldelem_I8);
3554                         else if (type == TypeManager.float_type)
3555                                 ig.Emit (OpCodes.Ldelem_R4);
3556                         else if (type == TypeManager.double_type)
3557                                 ig.Emit (OpCodes.Ldelem_R8);
3558                         else if (type == TypeManager.intptr_type)
3559                                 ig.Emit (OpCodes.Ldelem_I);
3560                         else
3561                                 ig.Emit (OpCodes.Ldelem_Ref);
3562                 }
3563                 
3564                 public override void Emit (EmitContext ec)
3565                 {
3566                         int rank = ea.Expr.Type.GetArrayRank ();
3567                         ILGenerator ig = ec.ig;
3568
3569                         ea.Expr.Emit (ec);
3570
3571                         foreach (Argument a in ea.Arguments)
3572                                 a.Expr.Emit (ec);
3573
3574                         if (rank == 1)
3575                                 EmitLoadOpcode (ig, type);
3576                         else {
3577                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3578                                 Type [] args = new Type [ea.Arguments.Count];
3579                                 MethodInfo get;
3580                                 
3581                                 int i = 0;
3582                                 
3583                                 foreach (Argument a in ea.Arguments)
3584                                         args [i++] = a.Type;
3585                                 
3586                                 get = mb.GetArrayMethod (
3587                                         ea.Expr.Type, "Get",
3588                                         CallingConventions.HasThis |
3589                                         CallingConventions.Standard,
3590                                         type, args);
3591                                 
3592                                 ig.Emit (OpCodes.Call, get);
3593                         }
3594                 }
3595
3596                 public void EmitAssign (EmitContext ec, Expression source)
3597                 {
3598                         int rank = ea.Expr.Type.GetArrayRank ();
3599                         ILGenerator ig = ec.ig;
3600
3601                         ea.Expr.Emit (ec);
3602
3603                         foreach (Argument a in ea.Arguments)
3604                                 a.Expr.Emit (ec);
3605
3606                         source.Emit (ec);
3607
3608                         Type t = source.Type;
3609                         if (rank == 1){
3610                                 if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3611                                         ig.Emit (OpCodes.Stelem_I1);
3612                                 else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3613                                         ig.Emit (OpCodes.Stelem_I2);
3614                                 else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3615                                         ig.Emit (OpCodes.Stelem_I4);
3616                                 else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3617                                         ig.Emit (OpCodes.Stelem_I8);
3618                                 else if (t == TypeManager.float_type)
3619                                         ig.Emit (OpCodes.Stelem_R4);
3620                                 else if (t == TypeManager.double_type)
3621                                         ig.Emit (OpCodes.Stelem_R8);
3622                                 else if (t == TypeManager.intptr_type)
3623                                         ig.Emit (OpCodes.Stelem_I);
3624                                 else
3625                                         ig.Emit (OpCodes.Stelem_Ref);
3626                         } else {
3627                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3628                                 Type [] args = new Type [ea.Arguments.Count + 1];
3629                                 MethodInfo set;
3630                                 
3631                                 int i = 0;
3632                                 
3633                                 foreach (Argument a in ea.Arguments)
3634                                         args [i++] = a.Type;
3635
3636                                 args [i] = type;
3637                                 
3638                                 set = mb.GetArrayMethod (
3639                                         ea.Expr.Type, "Set",
3640                                         CallingConventions.HasThis |
3641                                         CallingConventions.Standard,
3642                                         TypeManager.void_type, args);
3643                                 
3644                                 ig.Emit (OpCodes.Call, set);
3645                         }
3646                 }
3647         }
3648         class Indexers {
3649                 public ArrayList getters, setters;
3650                 static Hashtable map;
3651
3652                 static Indexers ()
3653                 {
3654                         map = new Hashtable ();
3655                 }
3656
3657                 Indexers (MemberInfo [] mi)
3658                 {
3659                         foreach (PropertyInfo property in mi){
3660                                 MethodInfo get, set;
3661                                 
3662                                 get = property.GetGetMethod (true);
3663                                 if (get != null){
3664                                         if (getters == null)
3665                                                 getters = new ArrayList ();
3666
3667                                         getters.Add (get);
3668                                 }
3669                                 
3670                                 set = property.GetSetMethod (true);
3671                                 if (set != null){
3672                                         if (setters == null)
3673                                                 setters = new ArrayList ();
3674                                         setters.Add (set);
3675                                 }
3676                         }
3677                 }
3678                 
3679                 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc) 
3680                 {
3681                         Indexers ix = (Indexers) map [t];
3682                         string p_name = TypeManager.IndexerPropertyName (t);
3683                         
3684                         if (ix != null)
3685                                 return ix;
3686
3687                         MemberInfo [] mi = tm.FindMembers (
3688                                 t, MemberTypes.Property,
3689                                 BindingFlags.Public | BindingFlags.Instance,
3690                                 Type.FilterName, p_name);
3691
3692                         if (mi == null || mi.Length == 0){
3693                                 Report.Error (21, loc,
3694                                               "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3695                                               "any indexers defined");
3696                                 return null;
3697                         }
3698                         
3699                         ix = new Indexers (mi);
3700                         map [t] = ix;
3701
3702                         return ix;
3703                 }
3704         }
3705
3706         // <summary>
3707         //   Expressions that represent an indexer call.
3708         // </summary>
3709         public class IndexerAccess : Expression, IAssignMethod {
3710                 //
3711                 // Points to our "data" repository
3712                 //
3713                 ElementAccess ea;
3714                 MethodInfo get, set;
3715                 Indexers ilist;
3716                 ArrayList set_arguments;
3717                 
3718                 public IndexerAccess (ElementAccess ea_data)
3719                 {
3720                         ea = ea_data;
3721                         eclass = ExprClass.Value;
3722                 }
3723
3724                 public override Expression DoResolve (EmitContext ec)
3725                 {
3726                         Type indexer_type = ea.Expr.Type;
3727                         
3728                         //
3729                         // Step 1: Query for all `Item' *properties*.  Notice
3730                         // that the actual methods are pointed from here.
3731                         //
3732                         // This is a group of properties, piles of them.  
3733
3734                         if (ilist == null)
3735                                 ilist = Indexers.GetIndexersForType (
3736                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3737
3738
3739                         //
3740                         // Step 2: find the proper match
3741                         //
3742                         if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3743                                 get = (MethodInfo) Invocation.OverloadResolve (
3744                                         ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3745
3746                         if (get == null){
3747                                 Report.Error (154, ea.loc,
3748                                               "indexer can not be used in this context, because " +
3749                                               "it lacks a `get' accessor");
3750                                 return null;
3751                         }
3752
3753                         type = get.ReturnType;
3754                         eclass = ExprClass.IndexerAccess;
3755                         return this;
3756                 }
3757
3758                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3759                 {
3760                         Type indexer_type = ea.Expr.Type;
3761                         Type right_type = right_side.Type;
3762
3763                         if (ilist == null)
3764                                 ilist = Indexers.GetIndexersForType (
3765                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3766
3767                         if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3768                                 set_arguments = (ArrayList) ea.Arguments.Clone ();
3769                                 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3770
3771                                 set = (MethodInfo) Invocation.OverloadResolve (
3772                                         ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3773                         }
3774                         
3775                         if (set == null){
3776                                 Report.Error (200, ea.loc,
3777                                               "indexer X.this [" + TypeManager.CSharpName (right_type) +
3778                                               "] lacks a `set' accessor");
3779                                         return null;
3780                         }
3781
3782                         type = TypeManager.void_type;
3783                         eclass = ExprClass.IndexerAccess;
3784                         return this;
3785                 }
3786                 
3787                 public override void Emit (EmitContext ec)
3788                 {
3789                         Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3790                 }
3791
3792                 //
3793                 // source is ignored, because we already have a copy of it from the
3794                 // LValue resolution and we have already constructed a pre-cached
3795                 // version of the arguments (ea.set_arguments);
3796                 //
3797                 public void EmitAssign (EmitContext ec, Expression source)
3798                 {
3799                         Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3800                 }
3801         }
3802         
3803         public class BaseAccess : Expression {
3804
3805                 public enum BaseAccessType : byte {
3806                         Member,
3807                         Indexer
3808                 };
3809                 
3810                 public readonly BaseAccessType BAType;
3811                 public readonly string         Member;
3812                 public readonly ArrayList      Arguments;
3813
3814                 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3815                 {
3816                         BAType = t;
3817                         Member = member;
3818                         Arguments = args;
3819                         
3820                 }
3821
3822                 public override Expression DoResolve (EmitContext ec)
3823                 {
3824                         // FIXME: Implement;
3825                         throw new Exception ("Unimplemented");
3826                         // return this;
3827                 }
3828
3829                 public override void Emit (EmitContext ec)
3830                 {
3831                         throw new Exception ("Unimplemented");
3832                 }
3833         }
3834
3835         // <summary>
3836         //   This class exists solely to pass the Type around and to be a dummy
3837         //   that can be passed to the conversion functions (this is used by
3838         //   foreach implementation to typecast the object return value from
3839         //   get_Current into the proper type.  All code has been generated and
3840         //   we only care about the side effect conversions to be performed
3841         // </summary>
3842         
3843         public class EmptyExpression : Expression {
3844                 public EmptyExpression ()
3845                 {
3846                         type = TypeManager.object_type;
3847                         eclass = ExprClass.Value;
3848                 }
3849
3850                 public EmptyExpression (Type t)
3851                 {
3852                         type = t;
3853                         eclass = ExprClass.Value;
3854                 }
3855                 
3856                 public override Expression DoResolve (EmitContext ec)
3857                 {
3858                         return this;
3859                 }
3860
3861                 public override void Emit (EmitContext ec)
3862                 {
3863                         // nothing, as we only exist to not do anything.
3864                 }
3865         }
3866
3867         public class UserCast : Expression {
3868                 MethodBase method;
3869                 Expression source;
3870                 
3871                 public UserCast (MethodInfo method, Expression source)
3872                 {
3873                         this.method = method;
3874                         this.source = source;
3875                         type = method.ReturnType;
3876                         eclass = ExprClass.Value;
3877                 }
3878
3879                 public override Expression DoResolve (EmitContext ec)
3880                 {
3881                         //
3882                         // We are born fully resolved
3883                         //
3884                         return this;
3885                 }
3886
3887                 public override void Emit (EmitContext ec)
3888                 {
3889                         ILGenerator ig = ec.ig;
3890
3891                         source.Emit (ec);
3892                         
3893                         if (method is MethodInfo)
3894                                 ig.Emit (OpCodes.Call, (MethodInfo) method);
3895                         else
3896                                 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
3897
3898                 }
3899
3900         }
3901 }