2001-11-18 Ravi Pratap <ravi@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                                 ParameterInfo [] pi = mb.GetParameters ();
1941                                 ReflectionParameters rp = new ReflectionParameters (pi);
1942                                 method_parameter_cache [mb] = rp;
1943
1944                                 return (ParameterData) rp;
1945                         }
1946                 }
1947
1948                 // <summary>
1949                 //   Tells whether a user defined conversion from Type `from' to
1950                 //   Type `to' exists.
1951                 //
1952                 //   FIXME: we could implement a cache here. 
1953                 // </summary>
1954                 static bool ConversionExists (EmitContext ec, Type from, Type to, Location loc)
1955                 {
1956                         // Locate user-defined implicit operators
1957
1958                         Expression mg;
1959                         
1960                         mg = MemberLookup (ec, to, "op_Implicit", false, loc);
1961
1962                         if (mg != null) {
1963                                 MethodGroupExpr me = (MethodGroupExpr) mg;
1964                                 
1965                                 for (int i = me.Methods.Length; i > 0;) {
1966                                         i--;
1967                                         MethodBase mb = me.Methods [i];
1968                                         ParameterData pd = GetParameterData (mb);
1969                                         
1970                                         if (from == pd.ParameterType (0))
1971                                                 return true;
1972                                 }
1973                         }
1974
1975                         mg = MemberLookup (ec, from, "op_Implicit", false, loc);
1976
1977                         if (mg != null) {
1978                                 MethodGroupExpr me = (MethodGroupExpr) mg;
1979
1980                                 for (int i = me.Methods.Length; i > 0;) {
1981                                         i--;
1982                                         MethodBase mb = me.Methods [i];
1983                                         MethodInfo mi = (MethodInfo) mb;
1984                                         
1985                                         if (mi.ReturnType == to)
1986                                                 return true;
1987                                 }
1988                         }
1989                         
1990                         return false;
1991                 }
1992                 
1993                 // <summary>
1994                 //  Determines "better conversion" as specified in 7.4.2.3
1995                 //  Returns : 1 if a->p is better
1996                 //            0 if a->q or neither is better 
1997                 // </summary>
1998                 static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, bool use_standard,
1999                                              Location loc)
2000                 {
2001                         Type argument_type = a.Type;
2002                         Expression argument_expr = a.Expr;
2003
2004                         if (argument_type == null)
2005                                 throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
2006
2007                         if (p == q)
2008                                 return 0;
2009                         
2010                         if (argument_type == p)
2011                                 return 1;
2012
2013                         if (argument_type == q)
2014                                 return 0;
2015
2016                         //
2017                         // Now probe whether an implicit constant expression conversion
2018                         // can be used.
2019                         //
2020                         // An implicit constant expression conversion permits the following
2021                         // conversions:
2022                         //
2023                         //    * A constant-expression of type `int' can be converted to type
2024                         //      sbyte, byute, short, ushort, uint, ulong provided the value of
2025                         //      of the expression is withing the range of the destination type.
2026                         //
2027                         //    * A constant-expression of type long can be converted to type
2028                         //      ulong, provided the value of the constant expression is not negative
2029                         //
2030                         // FIXME: Note that this assumes that constant folding has
2031                         // taken place.  We dont do constant folding yet.
2032                         //
2033
2034                         if (argument_expr is IntLiteral){
2035                                 IntLiteral ei = (IntLiteral) argument_expr;
2036                                 int value = ei.Value;
2037                                 
2038                                 if (p == TypeManager.sbyte_type){
2039                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
2040                                                 return 1;
2041                                 } else if (p == TypeManager.byte_type){
2042                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2043                                                 return 1;
2044                                 } else if (p == TypeManager.short_type){
2045                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
2046                                                 return 1;
2047                                 } else if (p == TypeManager.ushort_type){
2048                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2049                                                 return 1;
2050                                 } else if (p == TypeManager.uint32_type){
2051                                         //
2052                                         // we can optimize this case: a positive int32
2053                                         // always fits on a uint32
2054                                         //
2055                                         if (value >= 0)
2056                                                 return 1;
2057                                 } else if (p == TypeManager.uint64_type){
2058                                         //
2059                                         // we can optimize this case: a positive int32
2060                                         // always fits on a uint64
2061                                         //
2062                                         if (value >= 0)
2063                                                 return 1;
2064                                 }
2065                         } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
2066                                 LongLiteral ll = (LongLiteral) argument_expr;
2067                                 
2068                                 if (p == TypeManager.uint64_type){
2069                                         if (ll.Value > 0)
2070                                                 return 1;
2071                                 }
2072                         }
2073
2074                         if (q == null) {
2075
2076                                 Expression tmp;
2077
2078                                 if (use_standard)
2079                                         tmp = ConvertImplicitStandard (ec, argument_expr, p, loc);
2080                                 else
2081                                         tmp = ConvertImplicit (ec, argument_expr, p, loc);
2082
2083                                 if (tmp != null)
2084                                         return 1;
2085                                 else
2086                                         return 0;
2087
2088                         }
2089
2090                         if (ConversionExists (ec, p, q, loc) == true &&
2091                             ConversionExists (ec, q, p, loc) == false)
2092                                 return 1;
2093
2094                         if (p == TypeManager.sbyte_type)
2095                                 if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
2096                                     q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2097                                         return 1;
2098
2099                         if (p == TypeManager.short_type)
2100                                 if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
2101                                     q == TypeManager.uint64_type)
2102                                         return 1;
2103
2104                         if (p == TypeManager.int32_type)
2105                                 if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
2106                                         return 1;
2107
2108                         if (p == TypeManager.int64_type)
2109                                 if (q == TypeManager.uint64_type)
2110                                         return 1;
2111
2112                         return 0;
2113                 }
2114                 
2115                 // <summary>
2116                 //  Determines "Better function" and returns an integer indicating :
2117                 //  0 if candidate ain't better
2118                 //  1 if candidate is better than the current best match
2119                 // </summary>
2120                 static int BetterFunction (EmitContext ec, ArrayList args,
2121                                            MethodBase candidate, MethodBase best,
2122                                            bool use_standard, Location loc)
2123                 {
2124                         ParameterData candidate_pd = GetParameterData (candidate);
2125                         ParameterData best_pd;
2126                         int argument_count;
2127
2128                         if (args == null)
2129                                 argument_count = 0;
2130                         else
2131                                 argument_count = args.Count;
2132
2133                         if (candidate_pd.Count == 0 && argument_count == 0)
2134                                 return 1;
2135
2136                         if (best == null) {
2137                                 if (candidate_pd.Count == argument_count) {
2138                                         int x = 0;
2139                                         for (int j = argument_count; j > 0;) {
2140                                                 j--;
2141                                                 
2142                                                 Argument a = (Argument) args [j];
2143                                                 
2144                                                 x = BetterConversion (
2145                                                         ec, a, candidate_pd.ParameterType (j), null,
2146                                                         use_standard, loc);
2147                                                 
2148                                                 if (x <= 0)
2149                                                         break;
2150                                         }
2151                                         
2152                                         if (x > 0)
2153                                                 return 1;
2154                                         else
2155                                                 return 0;
2156                                         
2157                                 } else
2158                                         return 0;
2159                         }
2160
2161                         best_pd = GetParameterData (best);
2162
2163                         if (candidate_pd.Count == argument_count && best_pd.Count == argument_count) {
2164                                 int rating1 = 0, rating2 = 0;
2165                                 
2166                                 for (int j = argument_count; j > 0;) {
2167                                         j--;
2168                                         int x, y;
2169                                         
2170                                         Argument a = (Argument) args [j];
2171
2172                                         x = BetterConversion (ec, a, candidate_pd.ParameterType (j),
2173                                                               best_pd.ParameterType (j), use_standard, loc);
2174                                         y = BetterConversion (ec, a, best_pd.ParameterType (j),
2175                                                               candidate_pd.ParameterType (j), use_standard,
2176                                                               loc);
2177                                         
2178                                         rating1 += x;
2179                                         rating2 += y;
2180                                 }
2181
2182                                 if (rating1 > rating2)
2183                                         return 1;
2184                                 else
2185                                         return 0;
2186                         } else
2187                                 return 0;
2188                         
2189                 }
2190
2191                 public static string FullMethodDesc (MethodBase mb)
2192                 {
2193                         StringBuilder sb = new StringBuilder (mb.Name);
2194                         ParameterData pd = GetParameterData (mb);
2195
2196                         int count = pd.Count;
2197                         sb.Append (" (");
2198                         
2199                         for (int i = count; i > 0; ) {
2200                                 i--;
2201                                 
2202                                 sb.Append (pd.ParameterDesc (count - i - 1));
2203                                 if (i != 0)
2204                                         sb.Append (", ");
2205                         }
2206                         
2207                         sb.Append (")");
2208                         return sb.ToString ();
2209                 }
2210
2211                 public static MethodGroupExpr MakeUnionSet (Expression mg1, Expression mg2)
2212                 {
2213                         MemberInfo [] miset;
2214                         MethodGroupExpr union;
2215                         
2216                         if (mg1 != null && mg2 != null) {
2217                                 
2218                                 MethodGroupExpr left_set = null, right_set = null;
2219                                 int length1 = 0, length2 = 0;
2220                                 
2221                                 left_set = (MethodGroupExpr) mg1;
2222                                 length1 = left_set.Methods.Length;
2223                                 
2224                                 right_set = (MethodGroupExpr) mg2;
2225                                 length2 = right_set.Methods.Length;
2226
2227                                 ArrayList common = new ArrayList ();
2228                                 
2229                                 for (int i = 0; i < left_set.Methods.Length; i++) {
2230                                         for (int j = 0; j < right_set.Methods.Length; j++) {
2231                                                 if (left_set.Methods [i] == right_set.Methods [j]) 
2232                                                         common.Add (left_set.Methods [i]);
2233                                         }
2234                                 }
2235                                 
2236                                 miset = new MemberInfo [length1 + length2 - common.Count];
2237
2238                                 left_set.Methods.CopyTo (miset, 0);
2239
2240                                 int k = 0;
2241                                 
2242                                 for (int j = 0; j < right_set.Methods.Length; j++)
2243                                         if (!common.Contains (right_set.Methods [j]))
2244                                                 miset [length1 + k++] = right_set.Methods [j];
2245                                 
2246                                 union = new MethodGroupExpr (miset);
2247
2248                                 return union;
2249
2250                         } else if (mg1 == null && mg2 != null) {
2251                                 
2252                                 MethodGroupExpr me = (MethodGroupExpr) mg2; 
2253                                 
2254                                 miset = new MemberInfo [me.Methods.Length];
2255                                 me.Methods.CopyTo (miset, 0);
2256
2257                                 union = new MethodGroupExpr (miset);
2258                                 
2259                                 return union;
2260
2261                         } else if (mg2 == null && mg1 != null) {
2262                                 
2263                                 MethodGroupExpr me = (MethodGroupExpr) mg1; 
2264                                 
2265                                 miset = new MemberInfo [me.Methods.Length];
2266                                 me.Methods.CopyTo (miset, 0);
2267
2268                                 union = new MethodGroupExpr (miset);
2269                                 
2270                                 return union;
2271                         }
2272                         
2273                         return null;
2274                 }
2275
2276                 // <summary>
2277                 //  Determines is the candidate method, if a params method, is applicable
2278                 //  in its expanded form to the given set of arguments
2279                 // </summary>
2280                 static bool IsParamsMethodApplicable (ArrayList arguments, MethodBase candidate)
2281                 {
2282                         int arg_count;
2283                         
2284                         if (arguments == null)
2285                                 arg_count = 0;
2286                         else
2287                                 arg_count = arguments.Count;
2288                         
2289                         ParameterData pd = GetParameterData (candidate);
2290                         
2291                         int pd_count = pd.Count;
2292
2293                         if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
2294                                 return false;
2295
2296                         if (pd_count - 1 > arg_count)
2297                                 return false;
2298
2299                         // If we have come this far, the case which remains is when the number of parameters
2300                         // is less than or equal to the argument count. So, we now check if the element type
2301                         // of the params array is compatible with each argument type
2302                         //
2303
2304                         Type element_type = pd.ParameterType (pd_count - 1).GetElementType ();
2305
2306                         for (int i = pd_count - 1; i < arg_count - 1; i++) {
2307                                 Argument a = (Argument) arguments [i];
2308                                 if (!StandardConversionExists (a.Type, element_type))
2309                                         return false;
2310                         }
2311                         
2312                         return true;
2313                 }
2314
2315                 // <summary>
2316                 //  Determines if the candidate method is applicable (section 14.4.2.1)
2317                 //  to the given set of arguments
2318                 // </summary>
2319                 static bool IsApplicable (ArrayList arguments, MethodBase candidate)
2320                 {
2321                         int arg_count;
2322
2323                         if (arguments == null)
2324                                 arg_count = 0;
2325                         else
2326                                 arg_count = arguments.Count;
2327
2328                         ParameterData pd = GetParameterData (candidate);
2329
2330                         int pd_count = pd.Count;
2331
2332                         if (arg_count != pd.Count)
2333                                 return false;
2334
2335                         for (int i = arg_count; i > 0; ) {
2336                                 i--;
2337
2338                                 Argument a = (Argument) arguments [i];
2339
2340                                 Parameter.Modifier a_mod = a.GetParameterModifier ();
2341                                 Parameter.Modifier p_mod = pd.ParameterModifier (i);
2342
2343                                 if (a_mod == p_mod) {
2344                                         
2345                                         if (a_mod == Parameter.Modifier.NONE)
2346                                                 if (!StandardConversionExists (a.Type, pd.ParameterType (i)))
2347                                                         return false;
2348                                         
2349                                         if (a_mod == Parameter.Modifier.REF ||
2350                                             a_mod == Parameter.Modifier.OUT)
2351                                                 if (pd.ParameterType (i) != a.Type)
2352                                                         return false;
2353                                 } else
2354                                         return false;
2355                         }
2356
2357                         return true;
2358                 }
2359                 
2360                 
2361
2362                 // <summary>
2363                 //   Find the Applicable Function Members (7.4.2.1)
2364                 //
2365                 //   me: Method Group expression with the members to select.
2366                 //       it might contain constructors or methods (or anything
2367                 //       that maps to a method).
2368                 //
2369                 //   Arguments: ArrayList containing resolved Argument objects.
2370                 //
2371                 //   loc: The location if we want an error to be reported, or a Null
2372                 //        location for "probing" purposes.
2373                 //
2374                 //   use_standard: controls whether OverloadResolve should use the 
2375                 //   ConvertImplicit or ConvertImplicitStandard during overload resolution.
2376                 //
2377                 //   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
2378                 //            that is the best match of me on Arguments.
2379                 //
2380                 // </summary>
2381                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2382                                                           ArrayList Arguments, Location loc,
2383                                                           bool use_standard)
2384                 {
2385                         ArrayList afm = new ArrayList ();
2386                         int best_match_idx = -1;
2387                         MethodBase method = null;
2388                         int argument_count;
2389                         
2390                         for (int i = me.Methods.Length; i > 0; ){
2391                                 i--;
2392                                 MethodBase candidate  = me.Methods [i];
2393                                 int x;
2394
2395                                 // Check if candidate is applicable (section 14.4.2.1)
2396                                 if (!IsApplicable (Arguments, candidate))
2397                                         continue;
2398
2399                                 x = BetterFunction (ec, Arguments, candidate, method, use_standard, loc);
2400                                 
2401                                 if (x == 0)
2402                                         continue;
2403                                 else {
2404                                         best_match_idx = i;
2405                                         method = me.Methods [best_match_idx];
2406                                 }
2407                         }
2408
2409                         if (Arguments == null)
2410                                 argument_count = 0;
2411                         else
2412                                 argument_count = Arguments.Count;
2413                         
2414                         //
2415                         // Now we see if we can find params functions, applicable in their expanded form
2416                         // since if they were applicable in their normal form, they would have been selected
2417                         // above anyways
2418                         //
2419                         if (best_match_idx == -1) {
2420
2421                                 for (int i = me.Methods.Length; i > 0; ) {
2422                                         i--;
2423                                         MethodBase candidate = me.Methods [i];
2424
2425                                         if (IsParamsMethodApplicable (Arguments, candidate)) {
2426                                                 best_match_idx = i;
2427                                                 method = me.Methods [best_match_idx];
2428                                                 break;
2429                                         }
2430                                 }
2431                         }
2432
2433                         //
2434                         // Now we see if we can at least find a method with the same number of arguments
2435                         //
2436                         ParameterData pd;
2437                         
2438                         if (best_match_idx == -1) {
2439                                 
2440                                 for (int i = me.Methods.Length; i > 0;) {
2441                                         i--;
2442                                         MethodBase mb = me.Methods [i];
2443                                         pd = GetParameterData (mb);
2444                                         
2445                                         if (pd.Count == argument_count) {
2446                                                 best_match_idx = i;
2447                                                 method = me.Methods [best_match_idx];
2448                                                 break;
2449                                         } else
2450                                                 continue;
2451                                 }
2452                         }
2453
2454                         if (method == null)
2455                                 return null;
2456                         
2457                         // And now convert implicitly, each argument to the required type
2458                         
2459                         pd = GetParameterData (method);
2460                         int pd_count = pd.Count;
2461
2462                         for (int j = 0; j < argument_count; j++) {
2463
2464                                 Argument a = (Argument) Arguments [j];
2465                                 Expression a_expr = a.Expr;
2466                                 Type parameter_type = pd.ParameterType (j);
2467
2468                                 //
2469                                 // Note that we need to compare against the element type
2470                                 // when we have a params method
2471                                 //
2472                                 if (pd.ParameterModifier (pd_count - 1) == Parameter.Modifier.PARAMS) {
2473                                         if (j >= pd_count - 1) 
2474                                                 parameter_type = pd.ParameterType (pd_count - 1).GetElementType ();
2475                                 }
2476
2477                                 if (a.Type != parameter_type){
2478                                         Expression conv;
2479                                         
2480                                         if (use_standard)
2481                                                 conv = ConvertImplicitStandard (ec, a_expr, parameter_type, Location.Null);
2482                                         else
2483                                                 conv = ConvertImplicit (ec, a_expr, parameter_type, Location.Null);
2484
2485                                         if (conv == null) {
2486                                                 if (!Location.IsNull (loc)) {
2487                                                         Error (1502, loc,
2488                                                         "The best overloaded match for method '" + FullMethodDesc (method)+
2489                                                                "' has some invalid arguments");
2490                                                         Error (1503, loc,
2491                                                          "Argument " + (j+1) +
2492                                                          ": Cannot convert from '" + Argument.FullDesc (a) 
2493                                                          + "' to '" + pd.ParameterDesc (j) + "'");
2494                                                 }
2495                                                 return null;
2496                                         }
2497                                         
2498                         
2499                                         
2500                                         //
2501                                         // Update the argument with the implicit conversion
2502                                         //
2503                                         if (a_expr != conv)
2504                                                 a.Expr = conv;
2505
2506                                         // FIXME : For the case of params methods, we need to actually instantiate
2507                                         // an array and initialize it with the argument values etc etc.
2508
2509                                 }
2510                                 
2511                                 if (a.GetParameterModifier () != pd.ParameterModifier (j) &&
2512                                     pd.ParameterModifier (j) != Parameter.Modifier.PARAMS) {
2513                                         if (!Location.IsNull (loc)) {
2514                                                 Error (1502, loc,
2515                                                        "The best overloaded match for method '" + FullMethodDesc (method)+
2516                                                        "' has some invalid arguments");
2517                                                 Error (1503, loc,
2518                                                        "Argument " + (j+1) +
2519                                                        ": Cannot convert from '" + Argument.FullDesc (a) 
2520                                                        + "' to '" + pd.ParameterDesc (j) + "'");
2521                                         }
2522                                         return null;
2523                                 }
2524                                 
2525                                 
2526                         }
2527                         
2528                         return method;
2529                 }
2530                 
2531                 public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
2532                                                           ArrayList Arguments, Location loc)
2533                 {
2534                         return OverloadResolve (ec, me, Arguments, loc, false);
2535                 }
2536                         
2537                 public override Expression DoResolve (EmitContext ec)
2538                 {
2539                         //
2540                         // First, resolve the expression that is used to
2541                         // trigger the invocation
2542                         //
2543                         expr = expr.Resolve (ec);
2544                         if (expr == null)
2545                                 return null;
2546
2547                         if (!(expr is MethodGroupExpr)) {
2548                                 Type expr_type = expr.Type;
2549
2550                                 if (expr_type != null){
2551                                         bool IsDelegate = TypeManager.IsDelegateType (expr_type);
2552                                         if (IsDelegate)
2553                                                 return (new DelegateInvocation (
2554                                                         this.expr, Arguments, loc)).Resolve (ec);
2555                                 }
2556                         }
2557
2558                         if (!(expr is MethodGroupExpr)){
2559                                 report118 (loc, this.expr, "method group");
2560                                 return null;
2561                         }
2562
2563                         //
2564                         // Next, evaluate all the expressions in the argument list
2565                         //
2566                         if (Arguments != null){
2567                                 for (int i = Arguments.Count; i > 0;){
2568                                         --i;
2569                                         Argument a = (Argument) Arguments [i];
2570
2571                                         if (!a.Resolve (ec, loc))
2572                                                 return null;
2573                                 }
2574                         }
2575
2576                         method = OverloadResolve (ec, (MethodGroupExpr) this.expr, Arguments, loc);
2577
2578                         if (method == null){
2579                                 Error (-6, loc,
2580                                        "Could not find any applicable function for this argument list");
2581                                 return null;
2582                         }
2583
2584                         if (method is MethodInfo)
2585                                 type = ((MethodInfo)method).ReturnType;
2586
2587                         eclass = ExprClass.Value;
2588                         return this;
2589                 }
2590
2591                 // <summary>
2592                 //   Emits the list of arguments as an array
2593                 // </summary>
2594                 static void EmitParams (EmitContext ec, int idx, ArrayList arguments)
2595                 {
2596                         ILGenerator ig = ec.ig;
2597                         int count = arguments.Count - idx;
2598                         Argument a = (Argument) arguments [idx];
2599                         Type t = a.expr.Type;
2600                         string array_type = t.FullName + "[]";
2601                         LocalBuilder array;
2602                         
2603                         array = ig.DeclareLocal (Type.GetType (array_type));
2604                         IntLiteral.EmitInt (ig, count);
2605                         ig.Emit (OpCodes.Newarr, t);
2606                         ig.Emit (OpCodes.Stloc, array);
2607
2608                         int top = arguments.Count;
2609                         for (int j = idx; j < top; j++){
2610                                 a = (Argument) arguments [j];
2611                                 
2612                                 ig.Emit (OpCodes.Ldloc, array);
2613                                 IntLiteral.EmitInt (ig, j - idx);
2614                                 a.Emit (ec);
2615                                 
2616                                 ArrayAccess.EmitStoreOpcode (ig, t);
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 = ig.DeclareLocal (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                 int dimensions = 0;
2895
2896                 public ArrayCreation (string requested_type, ArrayList exprs,
2897                                       string rank, ArrayList initializers, Location l)
2898                 {
2899                         RequestedType = requested_type;
2900                         Rank          = rank;
2901                         Initializers  = initializers;
2902                         loc = l;
2903
2904                         Arguments = new ArrayList ();
2905
2906                         foreach (Expression e in exprs)
2907                                 Arguments.Add (new Argument (e, Argument.AType.Expression));
2908                         
2909                 }
2910
2911                 public ArrayCreation (string requested_type, string rank, ArrayList initializers, Location l)
2912                 {
2913                         RequestedType = requested_type;
2914                         Initializers = initializers;
2915                         loc = l;
2916
2917                         Rank = rank.Substring (0, rank.LastIndexOf ("["));
2918
2919                         string tmp = rank.Substring (rank.LastIndexOf ("["));
2920
2921                         dimensions = tmp.Length - 1;
2922                 }
2923
2924                 public static string FormArrayType (string base_type, int idx_count, string rank)
2925                 {
2926                         StringBuilder sb = new StringBuilder (base_type);
2927
2928                         sb.Append (rank);
2929                         
2930                         sb.Append ("[");
2931                         for (int i = 1; i < idx_count; i++)
2932                                 sb.Append (",");
2933                         sb.Append ("]");
2934                         
2935                         return sb.ToString ();
2936                 }
2937
2938                 public static string FormElementType (string base_type, int idx_count, string rank)
2939                 {
2940                         StringBuilder sb = new StringBuilder (base_type);
2941                         
2942                         sb.Append ("[");
2943                         for (int i = 1; i < idx_count; i++)
2944                                 sb.Append (",");
2945                         sb.Append ("]");
2946
2947                         sb.Append (rank);
2948
2949                         string val = sb.ToString ();
2950
2951                         return val.Substring (0, val.LastIndexOf ("["));
2952                 }
2953
2954                 void error178 ()
2955                 {
2956                         Report.Error (178, loc, "Incorrectly structured array initializer");
2957                 }
2958
2959                 bool ValidateInitializers (EmitContext ec)
2960                 {
2961                         if (Initializers == null)
2962                                 return true;
2963
2964                         Type underlying_type = ec.TypeContainer.LookupType (RequestedType, false);
2965
2966                         ArrayList probe = Initializers;
2967
2968                         if (Arguments != null) {
2969                                 for (int i = 0; i < Arguments.Count; i++) {
2970                                         Argument a = (Argument) Arguments [i];
2971                                         
2972                                         Expression e = Expression.Reduce (ec, a.Expr);
2973                                         
2974                                         if (!(e is Literal)) {
2975                                                 Report.Error (150, loc, "A constant value is expected");
2976                                                 return false;
2977                                         }
2978                                         
2979                                         int value = (int) ((Literal) e).GetValue ();
2980                 
2981                                         if (probe == null) {
2982                                                 error178 ();
2983                                                 return false;
2984                                         }
2985                                                 
2986                                         if (value != probe.Count) {
2987                                                 error178 ();
2988                                                 return false;
2989                                         }
2990
2991                                         if (probe [0] is ArrayList)
2992                                                 probe = (ArrayList) probe [0];
2993                                         else {
2994                                                 for (int j = 0; j < probe.Count; ++j) {
2995                                                         Expression tmp = (Expression) probe [j];
2996                                                         
2997                                                         tmp = tmp.Resolve (ec);
2998
2999                                                         Expression conv = ConvertImplicitRequired (ec, tmp,
3000                                                                                                    underlying_type, loc);
3001
3002                                                         if (conv == null)
3003                                                                 return false;
3004                                                 }
3005
3006                                                 probe = null;
3007                                         }
3008                                 }
3009
3010                         } else {
3011                                 //
3012                                 // Here is where we update dimension info in the case
3013                                 // that the user skips doing that
3014                                 //
3015
3016                                 Arguments = new ArrayList ();
3017                                 
3018                                 for (probe = Initializers; probe != null; ) {
3019                                         Expression e = new IntLiteral (probe.Count);
3020
3021                                         Arguments.Add (new Argument (e, Argument.AType.Expression));
3022
3023                                         if (probe [0] is ArrayList)
3024                                                 probe = (ArrayList) probe [0];
3025                                         else {
3026                                                 for (int j = 0; j < probe.Count; ++j) {
3027                                                         Expression tmp = (Expression) probe [j];
3028                                                         
3029                                                         tmp = tmp.Resolve (ec);
3030
3031                                                         Expression conv = ConvertImplicitRequired (ec, tmp,
3032                                                                                                    underlying_type, loc);
3033
3034                                                         if (conv == null)
3035                                                                 return false;
3036                                                 }
3037                                                 
3038                                                 probe = null;
3039                                         }
3040                                 }
3041
3042                                 if (Arguments.Count != dimensions) {
3043                                         error178 ();
3044                                         return false;
3045                                 }
3046                         }
3047
3048                         return true;
3049                 }
3050                 
3051                 public override Expression DoResolve (EmitContext ec)
3052                 {
3053                         int arg_count;
3054
3055                         if (!ValidateInitializers (ec))
3056                                 return null;
3057
3058                         if (Arguments == null)
3059                                 arg_count = 0;
3060                         else
3061                                 arg_count = Arguments.Count;
3062                         
3063                         string array_type = FormArrayType (RequestedType, arg_count, Rank);
3064
3065                         string element_type = FormElementType (RequestedType, arg_count, Rank);
3066
3067                         type = ec.TypeContainer.LookupType (array_type, false);
3068                         
3069                         array_element_type = ec.TypeContainer.LookupType (element_type, false);
3070                         
3071                         if (type == null)
3072                                 return null;
3073                         
3074                         if (arg_count == 1) {
3075                                 IsOneDimensional = true;
3076                                 eclass = ExprClass.Value;
3077                                 return this;
3078                         }
3079
3080                         IsBuiltinType = TypeManager.IsBuiltinType (type);
3081                         
3082                         if (IsBuiltinType) {
3083                                 
3084                                 Expression ml;
3085                                 
3086                                 ml = MemberLookup (ec, type, ".ctor", false, MemberTypes.Constructor,
3087                                                    AllBindingsFlags, loc);
3088                                 
3089                                 if (!(ml is MethodGroupExpr)){
3090                                         report118 (loc, ml, "method group");
3091                                         return null;
3092                                 }
3093                                 
3094                                 if (ml == null) {
3095                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3096                                                       "this argument list");
3097                                         return null;
3098                                 }
3099                                 
3100                                 if (Arguments != null) {
3101                                         for (int i = arg_count; i > 0;){
3102                                                 --i;
3103                                                 Argument a = (Argument) Arguments [i];
3104                                                 
3105                                                 if (!a.Resolve (ec, loc))
3106                                                         return null;
3107                                         }
3108                                 }
3109                                 
3110                                 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
3111                                 
3112                                 if (method == null) {
3113                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3114                                                       "this argument list");
3115                                         return null;
3116                                 }
3117                                 
3118                                 eclass = ExprClass.Value;
3119                                 return this;
3120                                 
3121                         } else {
3122
3123                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3124
3125                                 ArrayList args = new ArrayList ();
3126                                 if (Arguments != null){
3127                                         for (int i = arg_count; i > 0;){
3128                                                 --i;
3129                                                 Argument a = (Argument) Arguments [i];
3130                                                 
3131                                                 if (!a.Resolve (ec, loc))
3132                                                         return null;
3133                                                 
3134                                                 args.Add (a.Type);
3135                                         }
3136                                 }
3137                                 
3138                                 Type [] arg_types = null;
3139                                 
3140                                 if (args.Count > 0)
3141                                         arg_types = new Type [args.Count];
3142                                 
3143                                 args.CopyTo (arg_types, 0);
3144                                 
3145                                 method = mb.GetArrayMethod (type, ".ctor", CallingConventions.HasThis, null,
3146                                                             arg_types);
3147                                 
3148                                 if (method == null) {
3149                                         Report.Error (-6, loc, "New invocation: Can not find a constructor for " +
3150                                                       "this argument list");
3151                                         return null;
3152                                 }
3153                                 
3154                                 eclass = ExprClass.Value;
3155                                 return this;
3156                                 
3157                         }
3158                 }
3159
3160                 public override void Emit (EmitContext ec)
3161                 {
3162                         ILGenerator ig = ec.ig;
3163                         
3164                         if (IsOneDimensional) {
3165                                 Invocation.EmitArguments (ec, null, Arguments);
3166                                 ig.Emit (OpCodes.Newarr, array_element_type);
3167                                 
3168                         } else {
3169                                 Invocation.EmitArguments (ec, null, Arguments);
3170
3171                                 if (IsBuiltinType)
3172                                         ig.Emit (OpCodes.Newobj, (ConstructorInfo) method);
3173                                 else
3174                                         ig.Emit (OpCodes.Newobj, (MethodInfo) method);
3175                         }
3176
3177                         if (Initializers != null){
3178                                 FieldBuilder fb;
3179
3180                                 // FIXME: This is just sample data, need to fill with
3181                                 // real values.
3182                                 byte [] a = new byte [4] { 1, 2, 3, 4 };
3183                                 
3184                                 fb = ec.TypeContainer.RootContext.MakeStaticData (a);
3185
3186                                 ig.Emit (OpCodes.Dup);
3187                                 ig.Emit (OpCodes.Ldtoken, fb);
3188                                 ig.Emit (OpCodes.Call, TypeManager.void_initializearray_array_fieldhandle);
3189                         }
3190                 }
3191                 
3192                 public override void EmitStatement (EmitContext ec)
3193                 {
3194                         Emit (ec);
3195                         ec.ig.Emit (OpCodes.Pop);
3196                 }
3197                 
3198         }
3199         
3200         //
3201         // Represents the `this' construct
3202         //
3203         public class This : Expression, IAssignMethod, IMemoryLocation {
3204                 Location loc;
3205                 
3206                 public This (Location loc)
3207                 {
3208                         this.loc = loc;
3209                 }
3210
3211                 public override Expression DoResolve (EmitContext ec)
3212                 {
3213                         eclass = ExprClass.Variable;
3214                         type = ec.TypeContainer.TypeBuilder;
3215
3216                         if (ec.IsStatic){
3217                                 Report.Error (26, loc,
3218                                               "Keyword this not valid in static code");
3219                                 return null;
3220                         }
3221                         
3222                         return this;
3223                 }
3224
3225                 public Expression DoResolveLValue (EmitContext ec)
3226                 {
3227                         DoResolve (ec);
3228                         
3229                         if (ec.TypeContainer is Class){
3230                                 Report.Error (1604, loc, "Cannot assign to `this'");
3231                                 return null;
3232                         }
3233
3234                         return this;
3235                 }
3236
3237                 public override void Emit (EmitContext ec)
3238                 {
3239                         ec.ig.Emit (OpCodes.Ldarg_0);
3240                 }
3241
3242                 public void EmitAssign (EmitContext ec, Expression source)
3243                 {
3244                         source.Emit (ec);
3245                         ec.ig.Emit (OpCodes.Starg, 0);
3246                 }
3247
3248                 public void AddressOf (EmitContext ec)
3249                 {
3250                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
3251                 }
3252         }
3253
3254         // <summary>
3255         //   Implements the typeof operator
3256         // </summary>
3257         public class TypeOf : Expression {
3258                 public readonly string QueriedType;
3259                 Type typearg;
3260                 
3261                 public TypeOf (string queried_type)
3262                 {
3263                         QueriedType = queried_type;
3264                 }
3265
3266                 public override Expression DoResolve (EmitContext ec)
3267                 {
3268                         typearg = ec.TypeContainer.LookupType (QueriedType, false);
3269
3270                         if (typearg == null)
3271                                 return null;
3272
3273                         type = TypeManager.type_type;
3274                         eclass = ExprClass.Type;
3275                         return this;
3276                 }
3277
3278                 public override void Emit (EmitContext ec)
3279                 {
3280                         ec.ig.Emit (OpCodes.Ldtoken, typearg);
3281                         ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
3282                 }
3283         }
3284
3285         public class SizeOf : Expression {
3286                 public readonly string QueriedType;
3287                 
3288                 public SizeOf (string queried_type)
3289                 {
3290                         this.QueriedType = queried_type;
3291                 }
3292
3293                 public override Expression DoResolve (EmitContext ec)
3294                 {
3295                         // FIXME: Implement;
3296                         throw new Exception ("Unimplemented");
3297                         // return this;
3298                 }
3299
3300                 public override void Emit (EmitContext ec)
3301                 {
3302                         throw new Exception ("Implement me");
3303                 }
3304         }
3305
3306         public class MemberAccess : Expression {
3307                 public readonly string Identifier;
3308                 Expression expr;
3309                 Expression member_lookup;
3310                 Location loc;
3311                 
3312                 public MemberAccess (Expression expr, string id, Location l)
3313                 {
3314                         this.expr = expr;
3315                         Identifier = id;
3316                         loc = l;
3317                 }
3318
3319                 public Expression Expr {
3320                         get {
3321                                 return expr;
3322                         }
3323                 }
3324
3325                 void error176 (Location loc, string name)
3326                 {
3327                         Report.Error (176, loc, "Static member `" +
3328                                       name + "' cannot be accessed " +
3329                                       "with an instance reference, qualify with a " +
3330                                       "type name instead");
3331                 }
3332                 
3333                 public override Expression DoResolve (EmitContext ec)
3334                 {
3335                         //
3336                         // We are the sole users of ResolveWithSimpleName (ie, the only
3337                         // ones that can cope with it
3338                         //
3339                         expr = expr.ResolveWithSimpleName (ec);
3340
3341                         if (expr == null)
3342                                 return null;
3343
3344                         if (expr is SimpleName){
3345                                 SimpleName child_expr = (SimpleName) expr;
3346
3347                                 expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
3348
3349                                 return expr.Resolve (ec);
3350                         }
3351                                         
3352                         member_lookup = MemberLookup (ec, expr.Type, Identifier, false, loc);
3353
3354                         if (member_lookup == null)
3355                                 return null;
3356                         
3357                         //
3358                         // Method Groups
3359                         //
3360                         if (member_lookup is MethodGroupExpr){
3361                                 MethodGroupExpr mg = (MethodGroupExpr) member_lookup;
3362                                 
3363                                 //
3364                                 // Type.MethodGroup
3365                                 //
3366                                 if (expr is TypeExpr){
3367                                         if (!mg.RemoveInstanceMethods ()){
3368                                                 SimpleName.Error120 (loc, mg.Methods [0].Name); 
3369                                                 return null;
3370                                         }
3371
3372                                         return member_lookup;
3373                                 }
3374
3375                                 //
3376                                 // Instance.MethodGroup
3377                                 //
3378                                 if (!mg.RemoveStaticMethods ()){
3379                                         error176 (loc, mg.Methods [0].Name);
3380                                         return null;
3381                                 }
3382                                 
3383                                 mg.InstanceExpression = expr;
3384                                         
3385                                 return member_lookup;
3386                         }
3387
3388                         if (member_lookup is FieldExpr){
3389                                 FieldExpr fe = (FieldExpr) member_lookup;
3390                                 FieldInfo fi = fe.FieldInfo;
3391
3392                                 if (fi.IsLiteral) {
3393                                         Type t = fi.FieldType;
3394                                         object o;
3395
3396                                         if (fi is FieldBuilder)
3397                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3398                                         else
3399                                                 o = fi.GetValue (fi);
3400                                         
3401                                         if (t.IsSubclassOf (TypeManager.enum_type)) {
3402                                                 Expression enum_member = MemberLookup (ec, t, "value__", false, loc); 
3403                                                 Type underlying_type = enum_member.Type;
3404                                                 
3405                                                 Expression e = Literalize (o, underlying_type);
3406                                                 e.Resolve (ec);
3407                                         
3408                                                 return new EnumLiteral (e, t);
3409                                         }
3410
3411                                         Expression exp = Literalize (o, t);
3412                                         exp.Resolve (ec);
3413                                         
3414                                         return exp;
3415                                 }
3416                                 
3417                                 if (expr is TypeExpr){
3418                                         if (!fe.FieldInfo.IsStatic){
3419                                                 error176 (loc, fe.FieldInfo.Name);
3420                                                 return null;
3421                                         }
3422                                         return member_lookup;
3423                                 } else {
3424                                         if (fe.FieldInfo.IsStatic){
3425                                                 error176 (loc, fe.FieldInfo.Name);
3426                                                 return null;
3427                                         }
3428                                         fe.InstanceExpression = expr;
3429
3430                                         return fe;
3431                                 }
3432                         }
3433
3434                         if (member_lookup is PropertyExpr){
3435                                 PropertyExpr pe = (PropertyExpr) member_lookup;
3436
3437                                 if (expr is TypeExpr){
3438                                         if (!pe.IsStatic){
3439                                                 SimpleName.Error120 (loc, pe.PropertyInfo.Name);
3440                                                 return null;
3441                                         }
3442                                         return pe;
3443                                 } else {
3444                                         if (pe.IsStatic){
3445                                                 error176 (loc, pe.PropertyInfo.Name);
3446                                                 return null;
3447                                         }
3448                                         pe.InstanceExpression = expr;
3449
3450                                         return pe;
3451                                 }
3452                         }
3453                         
3454                         Console.WriteLine ("Support for [" + member_lookup + "] is not present yet");
3455                         Environment.Exit (0);
3456                         return null;
3457                 }
3458
3459                 public override void Emit (EmitContext ec)
3460                 {
3461                         throw new Exception ("Should not happen I think");
3462                 }
3463
3464         }
3465
3466         public class CheckedExpr : Expression {
3467
3468                 public Expression Expr;
3469
3470                 public CheckedExpr (Expression e)
3471                 {
3472                         Expr = e;
3473                 }
3474
3475                 public override Expression DoResolve (EmitContext ec)
3476                 {
3477                         Expr = Expr.Resolve (ec);
3478
3479                         if (Expr == null)
3480                                 return null;
3481
3482                         eclass = Expr.ExprClass;
3483                         type = Expr.Type;
3484                         return this;
3485                 }
3486
3487                 public override void Emit (EmitContext ec)
3488                 {
3489                         bool last_check = ec.CheckState;
3490                         
3491                         ec.CheckState = true;
3492                         Expr.Emit (ec);
3493                         ec.CheckState = last_check;
3494                 }
3495                 
3496         }
3497
3498         public class UnCheckedExpr : Expression {
3499
3500                 public Expression Expr;
3501
3502                 public UnCheckedExpr (Expression e)
3503                 {
3504                         Expr = e;
3505                 }
3506
3507                 public override Expression DoResolve (EmitContext ec)
3508                 {
3509                         Expr = Expr.Resolve (ec);
3510
3511                         if (Expr == null)
3512                                 return null;
3513
3514                         eclass = Expr.ExprClass;
3515                         type = Expr.Type;
3516                         return this;
3517                 }
3518
3519                 public override void Emit (EmitContext ec)
3520                 {
3521                         bool last_check = ec.CheckState;
3522                         
3523                         ec.CheckState = false;
3524                         Expr.Emit (ec);
3525                         ec.CheckState = last_check;
3526                 }
3527                 
3528         }
3529
3530         public class ElementAccess : Expression {
3531                 public ArrayList  Arguments;
3532                 public Expression Expr;
3533                 public Location   loc;
3534                 
3535                 public ElementAccess (Expression e, ArrayList e_list, Location l)
3536                 {
3537                         Expr = e;
3538
3539                         Arguments = new ArrayList ();
3540                         foreach (Expression tmp in e_list)
3541                                 Arguments.Add (new Argument (tmp, Argument.AType.Expression));
3542                         
3543                         loc  = l;
3544                 }
3545
3546                 bool CommonResolve (EmitContext ec)
3547                 {
3548                         Expr = Expr.Resolve (ec);
3549
3550                         if (Expr == null) 
3551                                 return false;
3552
3553                         if (Arguments == null)
3554                                 return false;
3555
3556                         for (int i = Arguments.Count; i > 0;){
3557                                 --i;
3558                                 Argument a = (Argument) Arguments [i];
3559                                 
3560                                 if (!a.Resolve (ec, loc))
3561                                         return false;
3562                         }
3563
3564                         return true;
3565                 }
3566                                 
3567                 public override Expression DoResolve (EmitContext ec)
3568                 {
3569                         if (!CommonResolve (ec))
3570                                 return null;
3571
3572                         //
3573                         // We perform some simple tests, and then to "split" the emit and store
3574                         // code we create an instance of a different class, and return that.
3575                         //
3576                         // I am experimenting with this pattern.
3577                         //
3578                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3579                                 return (new ArrayAccess (this)).Resolve (ec);
3580                         else
3581                                 return (new IndexerAccess (this)).Resolve (ec);
3582                 }
3583
3584                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3585                 {
3586                         if (!CommonResolve (ec))
3587                                 return null;
3588
3589                         if (Expr.Type.IsSubclassOf (TypeManager.array_type))
3590                                 return (new ArrayAccess (this)).ResolveLValue (ec, right_side);
3591                         else
3592                                 return (new IndexerAccess (this)).ResolveLValue (ec, right_side);
3593                 }
3594                 
3595                 public override void Emit (EmitContext ec)
3596                 {
3597                         throw new Exception ("Should never be reached");
3598                 }
3599         }
3600
3601         //
3602         // Implements array access 
3603         //
3604         public class ArrayAccess : Expression, IAssignMethod {
3605                 //
3606                 // Points to our "data" repository
3607                 //
3608                 ElementAccess ea;
3609                 
3610                 public ArrayAccess (ElementAccess ea_data)
3611                 {
3612                         ea = ea_data;
3613                         eclass = ExprClass.Variable;
3614                 }
3615
3616                 public override Expression DoResolve (EmitContext ec)
3617                 {
3618                         if (ea.Expr.ExprClass != ExprClass.Variable) {
3619                                 report118 (ea.loc, ea.Expr, "variable");
3620                                 return null;
3621                         }
3622
3623                         Type t = ea.Expr.Type;
3624
3625                         if (t.GetArrayRank () != ea.Arguments.Count){
3626                                 Report.Error (22, ea.loc,
3627                                               "Incorrect number of indexes for array " +
3628                                               " expected: " + t.GetArrayRank () + " got: " +
3629                                               ea.Arguments.Count);
3630                                 return null;
3631                         }
3632                         type = t.GetElementType ();
3633                         eclass = ExprClass.Variable;
3634
3635                         return this;
3636                 }
3637
3638                 // <summary>
3639                 //    Emits the right opcode to load an object of Type `t'
3640                 //    from an array of T
3641                 // </summary>
3642                 static public void EmitLoadOpcode (ILGenerator ig, Type type)
3643                 {
3644                         if (type == TypeManager.byte_type)
3645                                 ig.Emit (OpCodes.Ldelem_I1);
3646                         else if (type == TypeManager.sbyte_type)
3647                                 ig.Emit (OpCodes.Ldelem_U1);
3648                         else if (type == TypeManager.short_type)
3649                                 ig.Emit (OpCodes.Ldelem_I2);
3650                         else if (type == TypeManager.ushort_type)
3651                                 ig.Emit (OpCodes.Ldelem_U2);
3652                         else if (type == TypeManager.int32_type)
3653                                 ig.Emit (OpCodes.Ldelem_I4);
3654                         else if (type == TypeManager.uint32_type)
3655                                 ig.Emit (OpCodes.Ldelem_U4);
3656                         else if (type == TypeManager.uint64_type)
3657                                 ig.Emit (OpCodes.Ldelem_I8);
3658                         else if (type == TypeManager.int64_type)
3659                                 ig.Emit (OpCodes.Ldelem_I8);
3660                         else if (type == TypeManager.float_type)
3661                                 ig.Emit (OpCodes.Ldelem_R4);
3662                         else if (type == TypeManager.double_type)
3663                                 ig.Emit (OpCodes.Ldelem_R8);
3664                         else if (type == TypeManager.intptr_type)
3665                                 ig.Emit (OpCodes.Ldelem_I);
3666                         else
3667                                 ig.Emit (OpCodes.Ldelem_Ref);
3668                 }
3669
3670                 // <summary>
3671                 //    Emits the right opcode to store an object of Type `t'
3672                 //    from an array of T.  
3673                 // </summary>
3674                 static public void EmitStoreOpcode (ILGenerator ig, Type t)
3675                 {
3676                         if (t == TypeManager.byte_type || t == TypeManager.sbyte_type)
3677                                 ig.Emit (OpCodes.Stelem_I1);
3678                         else if (t == TypeManager.short_type || t == TypeManager.ushort_type)
3679                                 ig.Emit (OpCodes.Stelem_I2);
3680                         else if (t == TypeManager.int32_type || t == TypeManager.uint32_type)
3681                                 ig.Emit (OpCodes.Stelem_I4);
3682                         else if (t == TypeManager.int64_type || t == TypeManager.uint64_type)
3683                                 ig.Emit (OpCodes.Stelem_I8);
3684                         else if (t == TypeManager.float_type)
3685                                 ig.Emit (OpCodes.Stelem_R4);
3686                         else if (t == TypeManager.double_type)
3687                                 ig.Emit (OpCodes.Stelem_R8);
3688                         else if (t == TypeManager.intptr_type)
3689                                 ig.Emit (OpCodes.Stelem_I);
3690                         else
3691                                 ig.Emit (OpCodes.Stelem_Ref);
3692                 }
3693                 
3694                 public override void Emit (EmitContext ec)
3695                 {
3696                         int rank = ea.Expr.Type.GetArrayRank ();
3697                         ILGenerator ig = ec.ig;
3698
3699                         ea.Expr.Emit (ec);
3700
3701                         foreach (Argument a in ea.Arguments)
3702                                 a.Expr.Emit (ec);
3703
3704                         if (rank == 1)
3705                                 EmitLoadOpcode (ig, type);
3706                         else {
3707                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3708                                 Type [] args = new Type [ea.Arguments.Count];
3709                                 MethodInfo get;
3710                                 
3711                                 int i = 0;
3712                                 
3713                                 foreach (Argument a in ea.Arguments)
3714                                         args [i++] = a.Type;
3715                                 
3716                                 get = mb.GetArrayMethod (
3717                                         ea.Expr.Type, "Get",
3718                                         CallingConventions.HasThis |
3719                                         CallingConventions.Standard,
3720                                         type, args);
3721                                 
3722                                 ig.Emit (OpCodes.Call, get);
3723                         }
3724                 }
3725
3726                 public void EmitAssign (EmitContext ec, Expression source)
3727                 {
3728                         int rank = ea.Expr.Type.GetArrayRank ();
3729                         ILGenerator ig = ec.ig;
3730
3731                         ea.Expr.Emit (ec);
3732
3733                         foreach (Argument a in ea.Arguments)
3734                                 a.Expr.Emit (ec);
3735
3736                         source.Emit (ec);
3737
3738                         Type t = source.Type;
3739                         if (rank == 1)
3740                                 EmitStoreOpcode (ig, t);
3741                         else {
3742                                 ModuleBuilder mb = ec.TypeContainer.RootContext.ModuleBuilder;
3743                                 Type [] args = new Type [ea.Arguments.Count + 1];
3744                                 MethodInfo set;
3745                                 
3746                                 int i = 0;
3747                                 
3748                                 foreach (Argument a in ea.Arguments)
3749                                         args [i++] = a.Type;
3750
3751                                 args [i] = type;
3752                                 
3753                                 set = mb.GetArrayMethod (
3754                                         ea.Expr.Type, "Set",
3755                                         CallingConventions.HasThis |
3756                                         CallingConventions.Standard,
3757                                         TypeManager.void_type, args);
3758                                 
3759                                 ig.Emit (OpCodes.Call, set);
3760                         }
3761                 }
3762         }
3763         class Indexers {
3764                 public ArrayList getters, setters;
3765                 static Hashtable map;
3766
3767                 static Indexers ()
3768                 {
3769                         map = new Hashtable ();
3770                 }
3771
3772                 Indexers (MemberInfo [] mi)
3773                 {
3774                         foreach (PropertyInfo property in mi){
3775                                 MethodInfo get, set;
3776                                 
3777                                 get = property.GetGetMethod (true);
3778                                 if (get != null){
3779                                         if (getters == null)
3780                                                 getters = new ArrayList ();
3781
3782                                         getters.Add (get);
3783                                 }
3784                                 
3785                                 set = property.GetSetMethod (true);
3786                                 if (set != null){
3787                                         if (setters == null)
3788                                                 setters = new ArrayList ();
3789                                         setters.Add (set);
3790                                 }
3791                         }
3792                 }
3793                 
3794                 static public Indexers GetIndexersForType (Type t, TypeManager tm, Location loc) 
3795                 {
3796                         Indexers ix = (Indexers) map [t];
3797                         string p_name = TypeManager.IndexerPropertyName (t);
3798                         
3799                         if (ix != null)
3800                                 return ix;
3801
3802                         MemberInfo [] mi = tm.FindMembers (
3803                                 t, MemberTypes.Property,
3804                                 BindingFlags.Public | BindingFlags.Instance,
3805                                 Type.FilterName, p_name);
3806
3807                         if (mi == null || mi.Length == 0){
3808                                 Report.Error (21, loc,
3809                                               "Type `" + TypeManager.CSharpName (t) + "' does not have " +
3810                                               "any indexers defined");
3811                                 return null;
3812                         }
3813                         
3814                         ix = new Indexers (mi);
3815                         map [t] = ix;
3816
3817                         return ix;
3818                 }
3819         }
3820
3821         // <summary>
3822         //   Expressions that represent an indexer call.
3823         // </summary>
3824         public class IndexerAccess : Expression, IAssignMethod {
3825                 //
3826                 // Points to our "data" repository
3827                 //
3828                 ElementAccess ea;
3829                 MethodInfo get, set;
3830                 Indexers ilist;
3831                 ArrayList set_arguments;
3832                 
3833                 public IndexerAccess (ElementAccess ea_data)
3834                 {
3835                         ea = ea_data;
3836                         eclass = ExprClass.Value;
3837                 }
3838
3839                 public override Expression DoResolve (EmitContext ec)
3840                 {
3841                         Type indexer_type = ea.Expr.Type;
3842                         
3843                         //
3844                         // Step 1: Query for all `Item' *properties*.  Notice
3845                         // that the actual methods are pointed from here.
3846                         //
3847                         // This is a group of properties, piles of them.  
3848
3849                         if (ilist == null)
3850                                 ilist = Indexers.GetIndexersForType (
3851                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3852
3853
3854                         //
3855                         // Step 2: find the proper match
3856                         //
3857                         if (ilist != null && ilist.getters != null && ilist.getters.Count > 0)
3858                                 get = (MethodInfo) Invocation.OverloadResolve (
3859                                         ec, new MethodGroupExpr (ilist.getters), ea.Arguments, ea.loc);
3860
3861                         if (get == null){
3862                                 Report.Error (154, ea.loc,
3863                                               "indexer can not be used in this context, because " +
3864                                               "it lacks a `get' accessor");
3865                                 return null;
3866                         }
3867
3868                         type = get.ReturnType;
3869                         eclass = ExprClass.IndexerAccess;
3870                         return this;
3871                 }
3872
3873                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3874                 {
3875                         Type indexer_type = ea.Expr.Type;
3876                         Type right_type = right_side.Type;
3877
3878                         if (ilist == null)
3879                                 ilist = Indexers.GetIndexersForType (
3880                                         indexer_type, ec.TypeContainer.RootContext.TypeManager, ea.loc);
3881
3882                         if (ilist != null && ilist.setters != null && ilist.setters.Count > 0){
3883                                 set_arguments = (ArrayList) ea.Arguments.Clone ();
3884                                 set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
3885
3886                                 set = (MethodInfo) Invocation.OverloadResolve (
3887                                         ec, new MethodGroupExpr (ilist.setters), set_arguments, ea.loc);
3888                         }
3889                         
3890                         if (set == null){
3891                                 Report.Error (200, ea.loc,
3892                                               "indexer X.this [" + TypeManager.CSharpName (right_type) +
3893                                               "] lacks a `set' accessor");
3894                                         return null;
3895                         }
3896
3897                         type = TypeManager.void_type;
3898                         eclass = ExprClass.IndexerAccess;
3899                         return this;
3900                 }
3901                 
3902                 public override void Emit (EmitContext ec)
3903                 {
3904                         Invocation.EmitCall (ec, false, ea.Expr, get, ea.Arguments);
3905                 }
3906
3907                 //
3908                 // source is ignored, because we already have a copy of it from the
3909                 // LValue resolution and we have already constructed a pre-cached
3910                 // version of the arguments (ea.set_arguments);
3911                 //
3912                 public void EmitAssign (EmitContext ec, Expression source)
3913                 {
3914                         Invocation.EmitCall (ec, false, ea.Expr, set, set_arguments);
3915                 }
3916         }
3917         
3918         public class BaseAccess : Expression {
3919
3920                 public enum BaseAccessType : byte {
3921                         Member,
3922                         Indexer
3923                 };
3924                 
3925                 public readonly BaseAccessType BAType;
3926                 public readonly string         Member;
3927                 public readonly ArrayList      Arguments;
3928
3929                 public BaseAccess (BaseAccessType t, string member, ArrayList args)
3930                 {
3931                         BAType = t;
3932                         Member = member;
3933                         Arguments = args;
3934                         
3935                 }
3936
3937                 public override Expression DoResolve (EmitContext ec)
3938                 {
3939                         // FIXME: Implement;
3940                         throw new Exception ("Unimplemented");
3941                         // return this;
3942                 }
3943
3944                 public override void Emit (EmitContext ec)
3945                 {
3946                         throw new Exception ("Unimplemented");
3947                 }
3948         }
3949
3950         // <summary>
3951         //   This class exists solely to pass the Type around and to be a dummy
3952         //   that can be passed to the conversion functions (this is used by
3953         //   foreach implementation to typecast the object return value from
3954         //   get_Current into the proper type.  All code has been generated and
3955         //   we only care about the side effect conversions to be performed
3956         // </summary>
3957         
3958         public class EmptyExpression : Expression {
3959                 public EmptyExpression ()
3960                 {
3961                         type = TypeManager.object_type;
3962                         eclass = ExprClass.Value;
3963                 }
3964
3965                 public EmptyExpression (Type t)
3966                 {
3967                         type = t;
3968                         eclass = ExprClass.Value;
3969                 }
3970                 
3971                 public override Expression DoResolve (EmitContext ec)
3972                 {
3973                         return this;
3974                 }
3975
3976                 public override void Emit (EmitContext ec)
3977                 {
3978                         // nothing, as we only exist to not do anything.
3979                 }
3980         }
3981
3982         public class UserCast : Expression {
3983                 MethodBase method;
3984                 Expression source;
3985                 
3986                 public UserCast (MethodInfo method, Expression source)
3987                 {
3988                         this.method = method;
3989                         this.source = source;
3990                         type = method.ReturnType;
3991                         eclass = ExprClass.Value;
3992                 }
3993
3994                 public override Expression DoResolve (EmitContext ec)
3995                 {
3996                         //
3997                         // We are born fully resolved
3998                         //
3999                         return this;
4000                 }
4001
4002                 public override void Emit (EmitContext ec)
4003                 {
4004                         ILGenerator ig = ec.ig;
4005
4006                         source.Emit (ec);
4007                         
4008                         if (method is MethodInfo)
4009                                 ig.Emit (OpCodes.Call, (MethodInfo) method);
4010                         else
4011                                 ig.Emit (OpCodes.Call, (ConstructorInfo) method);
4012
4013                 }
4014
4015         }
4016 }