Fix typos.
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10
11 namespace Mono.CSharp {
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         /// <remarks>
20         ///   The ExprClass class contains the is used to pass the 
21         ///   classification of an expression (value, variable, namespace,
22         ///   type, method group, property access, event access, indexer access,
23         ///   nothing).
24         /// </remarks>
25         public enum ExprClass : byte {
26                 Invalid,
27                 
28                 Value,
29                 Variable,
30                 Namespace,
31                 Type,
32                 MethodGroup,
33                 PropertyAccess,
34                 EventAccess,
35                 IndexerAccess,
36                 Nothing, 
37         }
38
39         //
40         // This is just as a hint to AddressOf of what will be done with the
41         // address.
42         [Flags]
43         public enum AddressOp {
44                 Store = 1,
45                 Load  = 2,
46                 LoadStore = 3
47         };
48         
49         /// <summary>
50         ///   This interface is implemented by variables
51         /// </summary>
52         public interface IMemoryLocation {
53                 /// <summary>
54                 ///   The AddressOf method should generate code that loads
55                 ///   the address of the object and leaves it on the stack.
56                 ///
57                 ///   The `mode' argument is used to notify the expression
58                 ///   of whether this will be used to read from the address or
59                 ///   write to the address.
60                 ///
61                 ///   This is just a hint that can be used to provide good error
62                 ///   reporting, and should have no other side effects. 
63                 /// </summary>
64                 void AddressOf (EmitContext ec, AddressOp mode);
65         }
66
67         /// <remarks>
68         ///   Base class for expressions
69         /// </remarks>
70         public abstract class Expression {
71                 public ExprClass eclass;
72                 protected Type      type;
73                 
74                 public Type Type {
75                         get {
76                                 return type;
77                         }
78
79                         set {
80                                 type = value;
81                         }
82                 }
83
84                 /// <summary>
85                 ///   Utility wrapper routine for Error, just to beautify the code
86                 /// </summary>
87                 static protected void Error (int error, string s)
88                 {
89                         Report.Error (error, s);
90                 }
91
92                 static protected void Error (int error, Location loc, string s)
93                 {
94                         Report.Error (error, loc, s);
95                 }
96                 
97                 /// <summary>
98                 ///   Utility wrapper routine for Warning, just to beautify the code
99                 /// </summary>
100                 static protected void Warning (int warning, string s)
101                 {
102                         Report.Warning (warning, s);
103                 }
104
105                 static public void Error_CannotConvertType (Location loc, Type source, Type target)
106                 {
107                         Report.Error (30, loc, "Cannot convert type '" +
108                                       TypeManager.CSharpName (source) + "' to '" +
109                                       TypeManager.CSharpName (target) + "'");
110                 }
111
112                 /// <summary>
113                 ///   Performs semantic analysis on the Expression
114                 /// </summary>
115                 ///
116                 /// <remarks>
117                 ///   The Resolve method is invoked to perform the semantic analysis
118                 ///   on the node.
119                 ///
120                 ///   The return value is an expression (it can be the
121                 ///   same expression in some cases) or a new
122                 ///   expression that better represents this node.
123                 ///   
124                 ///   For example, optimizations of Unary (LiteralInt)
125                 ///   would return a new LiteralInt with a negated
126                 ///   value.
127                 ///   
128                 ///   If there is an error during semantic analysis,
129                 ///   then an error should be reported (using Report)
130                 ///   and a null value should be returned.
131                 ///   
132                 ///   There are two side effects expected from calling
133                 ///   Resolve(): the the field variable "eclass" should
134                 ///   be set to any value of the enumeration
135                 ///   `ExprClass' and the type variable should be set
136                 ///   to a valid type (this is the type of the
137                 ///   expression).
138                 /// </remarks>
139                 public abstract Expression DoResolve (EmitContext ec);
140
141                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
142                 {
143                         return DoResolve (ec);
144                 }
145                 
146                 /// <summary>
147                 ///   Resolves an expression and performs semantic analysis on it.
148                 /// </summary>
149                 ///
150                 /// <remarks>
151                 ///   Currently Resolve wraps DoResolve to perform sanity
152                 ///   checking and assertion checking on what we expect from Resolve.
153                 /// </remarks>
154                 public Expression Resolve (EmitContext ec)
155                 {
156                         Expression e = DoResolve (ec);
157
158                         if (e != null){
159
160                                 if (e is SimpleName){
161                                         SimpleName s = (SimpleName) e;
162
163                                         Report.Error (
164                                                       103, s.Location,
165                                                       "The name `" + s.Name + "' could not be found in `" +
166                                                       ec.DeclSpace.Name + "'");
167                                         return null;
168                                 }
169                                 
170                                 if (e.eclass == ExprClass.Invalid)
171                                         throw new Exception ("Expression " + e.GetType () +
172                                                              " ExprClass is Invalid after resolve");
173
174                                 if (e.eclass != ExprClass.MethodGroup)
175                                         if (e.type == null)
176                                                 throw new Exception (
177                                                         "Expression " + e.GetType () +
178                                                         " did not set its type after Resolve\n" +
179                                                         "called from: " + this.GetType ());
180                         }
181
182                         return e;
183                 }
184
185                 /// <summary>
186                 ///   Performs expression resolution and semantic analysis, but
187                 ///   allows SimpleNames to be returned.
188                 /// </summary>
189                 ///
190                 /// <remarks>
191                 ///   This is used by MemberAccess to construct long names that can not be
192                 ///   partially resolved (namespace-qualified names for example).
193                 /// </remarks>
194                 public Expression ResolveWithSimpleName (EmitContext ec)
195                 {
196                         Expression e;
197
198                         if (this is SimpleName)
199                                 e = ((SimpleName) this).DoResolveAllowStatic (ec);
200                         else 
201                                 e = DoResolve (ec);
202
203                         if (e != null){
204                                 if (e is SimpleName)
205                                         return e;
206
207                                 if (e.eclass == ExprClass.Invalid)
208                                         throw new Exception ("Expression " + e +
209                                                              " ExprClass is Invalid after resolve");
210
211                                 if (e.eclass != ExprClass.MethodGroup)
212                                         if (e.type == null)
213                                                 throw new Exception ("Expression " + e +
214                                                                      " did not set its type after Resolve");
215                         }
216
217                         return e;
218                 }
219                 
220                 /// <summary>
221                 ///   Resolves an expression for LValue assignment
222                 /// </summary>
223                 ///
224                 /// <remarks>
225                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
226                 ///   checking and assertion checking on what we expect from Resolve
227                 /// </remarks>
228                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
229                 {
230                         Expression e = DoResolveLValue (ec, right_side);
231
232                         if (e != null){
233                                 if (e is SimpleName){
234                                         SimpleName s = (SimpleName) e;
235
236                                         Report.Error (
237                                                 103, s.Location,
238                                                 "The name `" + s.Name + "' could not be found in `" +
239                                                 ec.DeclSpace.Name + "'");
240                                         return null;
241                                 }
242
243                                 if (e.eclass == ExprClass.Invalid)
244                                         throw new Exception ("Expression " + e +
245                                                              " ExprClass is Invalid after resolve");
246
247                                 if (e.eclass != ExprClass.MethodGroup)
248                                         if (e.type == null)
249                                                 throw new Exception ("Expression " + e +
250                                                                      " did not set its type after Resolve");
251                         }
252
253                         return e;
254                 }
255                 
256                 /// <summary>
257                 ///   Emits the code for the expression
258                 /// </summary>
259                 ///
260                 /// <remarks>
261                 ///   The Emit method is invoked to generate the code
262                 ///   for the expression.  
263                 /// </remarks>
264                 public abstract void Emit (EmitContext ec);
265
266                 /// <summary>
267                 ///   Protected constructor.  Only derivate types should
268                 ///   be able to be created
269                 /// </summary>
270
271                 protected Expression ()
272                 {
273                         eclass = ExprClass.Invalid;
274                         type = null;
275                 }
276
277                 /// <summary>
278                 ///   Returns a literalized version of a literal FieldInfo
279                 /// </summary>
280                 ///
281                 /// <remarks>
282                 ///   The possible return values are:
283                 ///      IntConstant, UIntConstant
284                 ///      LongLiteral, ULongConstant
285                 ///      FloatConstant, DoubleConstant
286                 ///      StringConstant
287                 ///
288                 ///   The value returned is already resolved.
289                 /// </remarks>
290                 public static Constant Constantify (object v, Type t)
291                 {
292                         if (t == TypeManager.int32_type)
293                                 return new IntConstant ((int) v);
294                         else if (t == TypeManager.uint32_type)
295                                 return new UIntConstant ((uint) v);
296                         else if (t == TypeManager.int64_type)
297                                 return new LongConstant ((long) v);
298                         else if (t == TypeManager.uint64_type)
299                                 return new ULongConstant ((ulong) v);
300                         else if (t == TypeManager.float_type)
301                                 return new FloatConstant ((float) v);
302                         else if (t == TypeManager.double_type)
303                                 return new DoubleConstant ((double) v);
304                         else if (t == TypeManager.string_type)
305                                 return new StringConstant ((string) v);
306                         else if (t == TypeManager.short_type)
307                                 return new ShortConstant ((short)v);
308                         else if (t == TypeManager.ushort_type)
309                                 return new UShortConstant ((ushort)v);
310                         else if (t == TypeManager.sbyte_type)
311                                 return new SByteConstant (((sbyte)v));
312                         else if (t == TypeManager.byte_type)
313                                 return new ByteConstant ((byte)v);
314                         else if (t == TypeManager.char_type)
315                                 return new CharConstant ((char)v);
316                         else if (TypeManager.IsEnumType (t)){
317                                 Expression e = Constantify (v, v.GetType ());
318
319                                 return new EnumConstant ((Constant) e, t);
320                         } else
321                                 throw new Exception ("Unknown type for constant (" + t +
322                                                      "), details: " + v);
323                 }
324
325                 /// <summary>
326                 ///   Returns a fully formed expression after a MemberLookup
327                 /// </summary>
328                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
329                 {
330                         if (mi is EventInfo)
331                                 return new EventExpr ((EventInfo) mi, loc);
332                         else if (mi is FieldInfo)
333                                 return new FieldExpr ((FieldInfo) mi, loc);
334                         else if (mi is PropertyInfo)
335                                 return new PropertyExpr ((PropertyInfo) mi, loc);
336                         else if (mi is Type){
337                                 return new TypeExpr ((System.Type) mi);
338                         }
339
340                         return null;
341                 }
342
343                 //
344                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
345                 //
346                 // This code could use some optimizations, but we need to do some
347                 // measurements.  For example, we could use a delegate to `flag' when
348                 // something can not any longer be a method-group (because it is something
349                 // else).
350                 //
351                 // Return values:
352                 //     If the return value is an Array, then it is an array of
353                 //     MethodBases
354                 //   
355                 //     If the return value is an MemberInfo, it is anything, but a Method
356                 //
357                 //     null on error.
358                 //
359                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
360                 // the arguments here and have MemberLookup return only the methods that
361                 // match the argument count/type, unlike we are doing now (we delay this
362                 // decision).
363                 //
364                 // This is so we can catch correctly attempts to invoke instance methods
365                 // from a static body (scan for error 120 in ResolveSimpleName).
366                 //
367                 //
368                 // FIXME: Potential optimization, have a static ArrayList
369                 //
370
371                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
372                                                        MemberTypes mt, BindingFlags bf, Location loc)
373                 {
374                         MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
375
376                         if (mi == null)
377                                 return null;
378
379                         int count = mi.Length;
380
381                         if (count > 1)
382                                 return new MethodGroupExpr (mi, loc);
383
384                         if (mi [0] is MethodBase)
385                                 return new MethodGroupExpr (mi, loc);
386
387                         return ExprClassFromMemberInfo (ec, mi [0], loc);
388                 }
389
390                 public const MemberTypes AllMemberTypes =
391                         MemberTypes.Constructor |
392                         MemberTypes.Event       |
393                         MemberTypes.Field       |
394                         MemberTypes.Method      |
395                         MemberTypes.NestedType  |
396                         MemberTypes.Property;
397                 
398                 public const BindingFlags AllBindingFlags =
399                         BindingFlags.Public |
400                         BindingFlags.Static |
401                         BindingFlags.Instance;
402
403                 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
404                 {
405                         return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
406                 }
407
408                 /// <summary>
409                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
410                 ///   to find a final definition.  If the final definition is not found, we
411                 ///   look for private members and display a useful debugging message if we
412                 ///   find it.
413                 /// </summary>
414                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
415                                                             Location loc)
416                 {
417                         Expression e;
418
419                         e = MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
420
421                         if (e != null)
422                                 return e;
423                         
424                         e = MemberLookup (ec, t, name, AllMemberTypes,
425                                           AllBindingFlags | BindingFlags.NonPublic, loc);
426                         if (e == null){
427                                 Report.Error (
428                                         117, loc, "`" + t + "' does not contain a definition " +
429                                         "for `" + name + "'");
430                         } else {
431                                 Report.Error (
432                                         122, loc, "`" + t + "." + name +
433                                         "' is inaccessible due to its protection level");
434                         }
435                         
436                         return null;
437                 }
438                 
439                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
440                 {
441                         Type expr_type = expr.Type;
442
443                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
444                                 // if we are a method group, emit a warning
445
446                                 expr.Emit (null);
447                         }
448                         
449                         if (target_type == TypeManager.object_type) {
450                                 //
451                                 // A pointer type cannot be converted to object
452                                 // 
453                                 if (expr_type.IsPointer)
454                                         return null;
455
456                                 if (expr_type.IsValueType)
457                                         return new BoxedCast (expr);
458                                 if (expr_type.IsClass || expr_type.IsInterface)
459                                         return new EmptyCast (expr, target_type);
460                         } else if (expr_type.IsSubclassOf (target_type)) {
461                                 return new EmptyCast (expr, target_type);
462                         } else {
463
464                                 // This code is kind of mirrored inside StandardConversionExists
465                                 // with the small distinction that we only probe there
466                                 //
467                                 // Always ensure that the code here and there is in sync
468                                 
469                                 // from the null type to any reference-type.
470                                 if (expr is NullLiteral && !target_type.IsValueType)
471                                         return new EmptyCast (expr, target_type);
472
473                                 // from any class-type S to any interface-type T.
474                                 if (expr_type.IsClass && target_type.IsInterface) {
475                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
476                                                 return new EmptyCast (expr, target_type);
477                                         else
478                                                 return null;
479                                 }
480
481                                 // from any interface type S to interface-type T.
482                                 if (expr_type.IsInterface && target_type.IsInterface) {
483
484                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
485                                                 return new EmptyCast (expr, target_type);
486                                         else
487                                                 return null;
488                                 }
489                                 
490                                 // from an array-type S to an array-type of type T
491                                 if (expr_type.IsArray && target_type.IsArray) {
492                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
493
494                                                 Type expr_element_type = expr_type.GetElementType ();
495
496                                                 if (MyEmptyExpr == null)
497                                                         MyEmptyExpr = new EmptyExpression ();
498                                                 
499                                                 MyEmptyExpr.SetType (expr_element_type);
500                                                 Type target_element_type = target_type.GetElementType ();
501
502                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
503                                                         if (StandardConversionExists (MyEmptyExpr,
504                                                                                       target_element_type))
505                                                                 return new EmptyCast (expr, target_type);
506                                         }
507                                 }
508                                 
509                                 
510                                 // from an array-type to System.Array
511                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
512                                         return new EmptyCast (expr, target_type);
513                                 
514                                 // from any delegate type to System.Delegate
515                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
516                                     target_type == TypeManager.delegate_type)
517                                         return new EmptyCast (expr, target_type);
518                                         
519                                 // from any array-type or delegate type into System.ICloneable.
520                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
521                                         if (target_type == TypeManager.icloneable_type)
522                                                 return new EmptyCast (expr, target_type);
523                                 
524                                 return null;
525
526                         }
527                         
528                         return null;
529                 }
530
531                 /// <summary>
532                 ///   Handles expressions like this: decimal d; d = 1;
533                 ///   and changes them into: decimal d; d = new System.Decimal (1);
534                 /// </summary>
535                 static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
536                 {
537                         ArrayList args = new ArrayList ();
538
539                         args.Add (new Argument (expr, Argument.AType.Expression));
540
541                         Expression ne = new New (target.FullName, args,
542                                                  new Location (-1));
543
544                         return ne.Resolve (ec);
545                 }
546
547                 /// <summary>
548                 ///   Implicit Numeric Conversions.
549                 ///
550                 ///   expr is the expression to convert, returns a new expression of type
551                 ///   target_type or null if an implicit conversion is not possible.
552                 /// </summary>
553                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
554                                                                     Type target_type, Location loc)
555                 {
556                         Type expr_type = expr.Type;
557                         
558                         //
559                         // Attempt to do the implicit constant expression conversions
560
561                         if (expr is IntConstant){
562                                 Expression e;
563                                 
564                                 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
565
566                                 if (e != null)
567                                         return e;
568                         } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
569                                 //
570                                 // Try the implicit constant expression conversion
571                                 // from long to ulong, instead of a nice routine,
572                                 // we just inline it
573                                 //
574                                 long v = ((LongConstant) expr).Value;
575                                 if (v > 0)
576                                         return new ULongConstant ((ulong) v);
577                         }
578
579                         //
580                         // If we have an enumeration, extract the underlying type,
581                         // use this during the comparission, but wrap around the original
582                         // target_type
583                         //
584                         Type real_target_type = target_type;
585
586                         if (TypeManager.IsEnumType (real_target_type))
587                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
588
589                         if (expr_type == real_target_type)
590                                 return new EmptyCast (expr, target_type);
591                         
592                         if (expr_type == TypeManager.sbyte_type){
593                                 //
594                                 // From sbyte to short, int, long, float, double.
595                                 //
596                                 if (real_target_type == TypeManager.int32_type)
597                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
598                                 if (real_target_type == TypeManager.int64_type)
599                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
600                                 if (real_target_type == TypeManager.double_type)
601                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
602                                 if (real_target_type == TypeManager.float_type)
603                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
604                                 if (real_target_type == TypeManager.short_type)
605                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
606                                 if (real_target_type == TypeManager.decimal_type)
607                                         return InternalTypeConstructor (ec, expr, target_type);
608                         } else if (expr_type == TypeManager.byte_type){
609                                 //
610                                 // From byte to short, ushort, int, uint, long, ulong, float, double
611                                 // 
612                                 if ((real_target_type == TypeManager.short_type) ||
613                                     (real_target_type == TypeManager.ushort_type) ||
614                                     (real_target_type == TypeManager.int32_type) ||
615                                     (real_target_type == TypeManager.uint32_type))
616                                         return new EmptyCast (expr, target_type);
617
618                                 if (real_target_type == TypeManager.uint64_type)
619                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
620                                 if (real_target_type == TypeManager.int64_type)
621                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
622                                 if (real_target_type == TypeManager.float_type)
623                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
624                                 if (real_target_type == TypeManager.double_type)
625                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
626                                 if (real_target_type == TypeManager.decimal_type)
627                                         return InternalTypeConstructor (ec, expr, target_type);
628                         } else if (expr_type == TypeManager.short_type){
629                                 //
630                                 // From short to int, long, float, double
631                                 // 
632                                 if (real_target_type == TypeManager.int32_type)
633                                         return new EmptyCast (expr, target_type);
634                                 if (real_target_type == TypeManager.int64_type)
635                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
636                                 if (real_target_type == TypeManager.double_type)
637                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
638                                 if (real_target_type == TypeManager.float_type)
639                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
640                                 if (real_target_type == TypeManager.decimal_type)
641                                         return InternalTypeConstructor (ec, expr, target_type);
642                         } else if (expr_type == TypeManager.ushort_type){
643                                 //
644                                 // From ushort to int, uint, long, ulong, float, double
645                                 //
646                                 if (real_target_type == TypeManager.uint32_type)
647                                         return new EmptyCast (expr, target_type);
648
649                                 if (real_target_type == TypeManager.uint64_type)
650                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
651                                 if (real_target_type == TypeManager.int32_type)
652                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
653                                 if (real_target_type == TypeManager.int64_type)
654                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
655                                 if (real_target_type == TypeManager.double_type)
656                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
657                                 if (real_target_type == TypeManager.float_type)
658                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
659                                 if (real_target_type == TypeManager.decimal_type)
660                                         return InternalTypeConstructor (ec, expr, target_type);
661                         } else if (expr_type == TypeManager.int32_type){
662                                 //
663                                 // From int to long, float, double
664                                 //
665                                 if (real_target_type == TypeManager.int64_type)
666                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
667                                 if (real_target_type == TypeManager.double_type)
668                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
669                                 if (real_target_type == TypeManager.float_type)
670                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
671                                 if (real_target_type == TypeManager.decimal_type)
672                                         return InternalTypeConstructor (ec, expr, target_type);
673                         } else if (expr_type == TypeManager.uint32_type){
674                                 //
675                                 // From uint to long, ulong, float, double
676                                 //
677                                 if (real_target_type == TypeManager.int64_type)
678                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
679                                 if (real_target_type == TypeManager.uint64_type)
680                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
681                                 if (real_target_type == TypeManager.double_type)
682                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
683                                                                OpCodes.Conv_R8);
684                                 if (real_target_type == TypeManager.float_type)
685                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
686                                                                OpCodes.Conv_R4);
687                                 if (real_target_type == TypeManager.decimal_type)
688                                         return InternalTypeConstructor (ec, expr, target_type);
689                         } else if ((expr_type == TypeManager.uint64_type) ||
690                                    (expr_type == TypeManager.int64_type)){
691                                 //
692                                 // From long/ulong to float, double
693                                 //
694                                 if (real_target_type == TypeManager.double_type)
695                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
696                                                                OpCodes.Conv_R8);
697                                 if (real_target_type == TypeManager.float_type)
698                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
699                                                                OpCodes.Conv_R4);        
700                                 if (real_target_type == TypeManager.decimal_type)
701                                         return InternalTypeConstructor (ec, expr, target_type);
702                         } else if (expr_type == TypeManager.char_type){
703                                 //
704                                 // From char to ushort, int, uint, long, ulong, float, double
705                                 // 
706                                 if ((real_target_type == TypeManager.ushort_type) ||
707                                     (real_target_type == TypeManager.int32_type) ||
708                                     (real_target_type == TypeManager.uint32_type))
709                                         return new EmptyCast (expr, target_type);
710                                 if (real_target_type == TypeManager.uint64_type)
711                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
712                                 if (real_target_type == TypeManager.int64_type)
713                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
714                                 if (real_target_type == TypeManager.float_type)
715                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
716                                 if (real_target_type == TypeManager.double_type)
717                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
718                                 if (real_target_type == TypeManager.decimal_type)
719                                         return InternalTypeConstructor (ec, expr, target_type);
720                         } else if (expr_type == TypeManager.float_type){
721                                 //
722                                 // float to double
723                                 //
724                                 if (real_target_type == TypeManager.double_type)
725                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
726                         }
727
728                         return null;
729                 }
730
731                 /// <summary>
732                 ///  Determines if a standard implicit conversion exists from
733                 ///  expr_type to target_type
734                 /// </summary>
735                 public static bool StandardConversionExists (Expression expr, Type target_type)
736                 {
737                         Type expr_type = expr.Type;
738                         
739                         if (expr_type == target_type)
740                                 return true;
741
742                         // First numeric conversions 
743                         
744                         if (expr_type == TypeManager.sbyte_type){
745                                 //
746                                 // From sbyte to short, int, long, float, double.
747                                 //
748                                 if ((target_type == TypeManager.int32_type) || 
749                                     (target_type == TypeManager.int64_type) ||
750                                     (target_type == TypeManager.double_type) ||
751                                     (target_type == TypeManager.float_type)  ||
752                                     (target_type == TypeManager.short_type) ||
753                                     (target_type == TypeManager.decimal_type))
754                                         return true;
755                                 
756                         } else if (expr_type == TypeManager.byte_type){
757                                 //
758                                 // From byte to short, ushort, int, uint, long, ulong, float, double
759                                 // 
760                                 if ((target_type == TypeManager.short_type) ||
761                                     (target_type == TypeManager.ushort_type) ||
762                                     (target_type == TypeManager.int32_type) ||
763                                     (target_type == TypeManager.uint32_type) ||
764                                     (target_type == TypeManager.uint64_type) ||
765                                     (target_type == TypeManager.int64_type) ||
766                                     (target_type == TypeManager.float_type) ||
767                                     (target_type == TypeManager.double_type) ||
768                                     (target_type == TypeManager.decimal_type))
769                                         return true;
770         
771                         } else if (expr_type == TypeManager.short_type){
772                                 //
773                                 // From short to int, long, float, double
774                                 // 
775                                 if ((target_type == TypeManager.int32_type) ||
776                                     (target_type == TypeManager.int64_type) ||
777                                     (target_type == TypeManager.double_type) ||
778                                     (target_type == TypeManager.float_type) ||
779                                     (target_type == TypeManager.decimal_type))
780                                         return true;
781                                         
782                         } else if (expr_type == TypeManager.ushort_type){
783                                 //
784                                 // From ushort to int, uint, long, ulong, float, double
785                                 //
786                                 if ((target_type == TypeManager.uint32_type) ||
787                                     (target_type == TypeManager.uint64_type) ||
788                                     (target_type == TypeManager.int32_type) ||
789                                     (target_type == TypeManager.int64_type) ||
790                                     (target_type == TypeManager.double_type) ||
791                                     (target_type == TypeManager.float_type) ||
792                                     (target_type == TypeManager.decimal_type))
793                                         return true;
794                                     
795                         } else if (expr_type == TypeManager.int32_type){
796                                 //
797                                 // From int to long, float, double
798                                 //
799                                 if ((target_type == TypeManager.int64_type) ||
800                                     (target_type == TypeManager.double_type) ||
801                                     (target_type == TypeManager.float_type) ||
802                                     (target_type == TypeManager.decimal_type))
803                                         return true;
804                                         
805                         } else if (expr_type == TypeManager.uint32_type){
806                                 //
807                                 // From uint to long, ulong, float, double
808                                 //
809                                 if ((target_type == TypeManager.int64_type) ||
810                                     (target_type == TypeManager.uint64_type) ||
811                                     (target_type == TypeManager.double_type) ||
812                                     (target_type == TypeManager.float_type) ||
813                                     (target_type == TypeManager.decimal_type))
814                                         return true;
815                                         
816                         } else if ((expr_type == TypeManager.uint64_type) ||
817                                    (expr_type == TypeManager.int64_type)) {
818                                 //
819                                 // From long/ulong to float, double
820                                 //
821                                 if ((target_type == TypeManager.double_type) ||
822                                     (target_type == TypeManager.float_type) ||
823                                     (target_type == TypeManager.decimal_type))
824                                         return true;
825                                     
826                         } else if (expr_type == TypeManager.char_type){
827                                 //
828                                 // From char to ushort, int, uint, long, ulong, float, double
829                                 // 
830                                 if ((target_type == TypeManager.ushort_type) ||
831                                     (target_type == TypeManager.int32_type) ||
832                                     (target_type == TypeManager.uint32_type) ||
833                                     (target_type == TypeManager.uint64_type) ||
834                                     (target_type == TypeManager.int64_type) ||
835                                     (target_type == TypeManager.float_type) ||
836                                     (target_type == TypeManager.double_type) ||
837                                     (target_type == TypeManager.decimal_type))
838                                         return true;
839
840                         } else if (expr_type == TypeManager.float_type){
841                                 //
842                                 // float to double
843                                 //
844                                 if (target_type == TypeManager.double_type)
845                                         return true;
846                         }       
847                         
848                         // Next reference conversions
849
850                         if (target_type == TypeManager.object_type) {
851                                 if ((expr_type.IsClass) ||
852                                     (expr_type.IsValueType))
853                                         return true;
854                                 
855                         } else if (expr_type.IsSubclassOf (target_type)) {
856                                 return true;
857                                 
858                         } else {
859                                 // Please remember that all code below actually comes
860                                 // from ImplicitReferenceConversion so make sure code remains in sync
861                                 
862                                 // from any class-type S to any interface-type T.
863                                 if (expr_type.IsClass && target_type.IsInterface) {
864                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
865                                                 return true;
866                                 }
867                                 
868                                 // from any interface type S to interface-type T.
869                                 // FIXME : Is it right to use IsAssignableFrom ?
870                                 if (expr_type.IsInterface && target_type.IsInterface)
871                                         if (target_type.IsAssignableFrom (expr_type))
872                                                 return true;
873                                 
874                                 // from an array-type S to an array-type of type T
875                                 if (expr_type.IsArray && target_type.IsArray) {
876                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
877                                                 
878                                                 Type expr_element_type = expr_type.GetElementType ();
879
880                                                 if (MyEmptyExpr == null)
881                                                         MyEmptyExpr = new EmptyExpression ();
882                                                 
883                                                 MyEmptyExpr.SetType (expr_element_type);
884                                                 Type target_element_type = target_type.GetElementType ();
885                                                 
886                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
887                                                         if (StandardConversionExists (MyEmptyExpr,
888                                                                                       target_element_type))
889                                                                 return true;
890                                         }
891                                 }
892                                 
893                                 // from an array-type to System.Array
894                                 if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
895                                         return true;
896                                 
897                                 // from any delegate type to System.Delegate
898                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
899                                     target_type == TypeManager.delegate_type)
900                                         if (target_type.IsAssignableFrom (expr_type))
901                                                 return true;
902                                         
903                                 // from any array-type or delegate type into System.ICloneable.
904                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
905                                         if (target_type == TypeManager.icloneable_type)
906                                                 return true;
907                                 
908                                 // from the null type to any reference-type.
909                                 if (expr is NullLiteral && !target_type.IsValueType)
910                                         return true;
911                                 
912                         }
913
914                         return false;
915                 }
916
917                 static EmptyExpression MyEmptyExpr;
918                 /// <summary>
919                 ///   Tells whether an implicit conversion exists from expr_type to
920                 ///   target_type
921                 /// </summary>
922                 public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
923                                                       Location l)
924                 {
925                         if (MyEmptyExpr == null)
926                                 MyEmptyExpr = new EmptyExpression (expr_type);
927                         else
928                                 MyEmptyExpr.SetType (expr_type);
929
930                         return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
931                 }
932                 
933                 /// <summary>
934                 ///  Finds "most encompassed type" according to the spec (13.4.2)
935                 ///  amongst the methods in the MethodGroupExpr which convert from a
936                 ///  type encompassing source_type
937                 /// </summary>
938                 static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
939                 {
940                         Type best = null;
941                         
942                         for (int i = me.Methods.Length; i > 0; ) {
943                                 i--;
944
945                                 MethodBase mb = me.Methods [i];
946                                 ParameterData pd = Invocation.GetParameterData (mb);
947                                 Type param_type = pd.ParameterType (0);
948
949                                 Expression source = new EmptyExpression (source_type);
950                                 Expression param = new EmptyExpression (param_type);
951
952                                 if (StandardConversionExists (source, param_type)) {
953                                         if (best == null)
954                                                 best = param_type;
955                                         
956                                         if (StandardConversionExists (param, best))
957                                                 best = param_type;
958                                 }
959                         }
960
961                         return best;
962                 }
963                 
964                 /// <summary>
965                 ///  Finds "most encompassing type" according to the spec (13.4.2)
966                 ///  amongst the methods in the MethodGroupExpr which convert to a
967                 ///  type encompassed by target_type
968                 /// </summary>
969                 static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
970                 {
971                         Type best = null;
972                         
973                         for (int i = me.Methods.Length; i > 0; ) {
974                                 i--;
975                                 
976                                 MethodInfo mi = (MethodInfo) me.Methods [i];
977                                 Type ret_type = mi.ReturnType;
978
979                                 Expression ret = new EmptyExpression (ret_type);
980                                 
981                                 if (StandardConversionExists (ret, target)) {
982                                         if (best == null)
983                                                 best = ret_type;
984
985                                         if (!StandardConversionExists (ret, best))
986                                                 best = ret_type;
987                                 }
988                                 
989                         }
990                         
991                         return best;
992
993                 }
994                 
995
996                 /// <summary>
997                 ///  User-defined Implicit conversions
998                 /// </summary>
999                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1000                                                                  Type target, Location loc)
1001                 {
1002                         return UserDefinedConversion (ec, source, target, loc, false);
1003                 }
1004
1005                 /// <summary>
1006                 ///  User-defined Explicit conversions
1007                 /// </summary>
1008                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1009                                                                  Type target, Location loc)
1010                 {
1011                         return UserDefinedConversion (ec, source, target, loc, true);
1012                 }
1013                 
1014                 /// <summary>
1015                 ///   User-defined conversions
1016                 /// </summary>
1017                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1018                                                                 Type target, Location loc,
1019                                                                 bool look_for_explicit)
1020                 {
1021                         Expression mg1 = null, mg2 = null;
1022                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1023                         Expression e;
1024                         MethodBase method = null;
1025                         Type source_type = source.Type;
1026
1027                         string op_name;
1028                         
1029                         // If we have a boolean type, we need to check for the True operator
1030
1031                         // FIXME : How does the False operator come into the picture ?
1032                         // FIXME : This doesn't look complete and very correct !
1033                         if (target == TypeManager.bool_type)
1034                                 op_name = "op_True";
1035                         else
1036                                 op_name = "op_Implicit";
1037
1038 #if old
1039                         //
1040                         // FIXME: This whole process can be optimized to check if the
1041                         // return is non-null and make the union as we go.
1042                         //
1043                         mg1 = MemberLookup (ec, source_type, op_name, loc);
1044
1045                         if (source_type.BaseType != null)
1046                                 mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1047                         
1048                         mg3 = MemberLookup (ec, target, op_name, loc);
1049
1050                         if (target.BaseType != null)
1051                                 mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
1052
1053                         MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2, loc);
1054                         MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4, loc);
1055
1056                         MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2, loc);
1057 #else
1058                         MethodGroupExpr union3;
1059                         
1060                         mg1 = MemberLookup (ec, source_type, op_name, loc);
1061                         if (source_type.BaseType != null)
1062                                 mg2 = MemberLookup (ec, source_type.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1063
1064                         if (mg1 == null)
1065                                 union3 = (MethodGroupExpr) mg2;
1066                         else if (mg2 == null)
1067                                 union3 = (MethodGroupExpr) mg1;
1068                         else
1069                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1070
1071                         mg1 = MemberLookup (ec, target, op_name, MemberTypes.Method, AllBindingFlags, loc);
1072                         if (mg1 != null){
1073                                 if (union3 != null)
1074                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1075                                 else
1076                                         union3 = (MethodGroupExpr) mg1;
1077                         }
1078                         if (target.BaseType != null)
1079                                 mg1 = MemberLookup (ec, target.BaseType, op_name, MemberTypes.Method, AllBindingFlags, loc);
1080                         
1081                         if (mg1 != null){
1082                                 if (union3 != null)
1083                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1084                                 else
1085                                         union3 = (MethodGroupExpr) mg1;
1086                         }
1087 #endif
1088                         MethodGroupExpr union4 = null;
1089
1090                         if (look_for_explicit) {
1091
1092                                 op_name = "op_Explicit";
1093                                 
1094                                 mg5 = MemberLookup (ec, source_type, op_name, loc);
1095
1096                                 if (source_type.BaseType != null)
1097                                         mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
1098                                 
1099                                 mg7 = MemberLookup (ec, target, op_name, loc);
1100                                 
1101                                 if (target.BaseType != null)
1102                                         mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
1103                                 
1104                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1105                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1106
1107                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1108                         }
1109                         
1110                         MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4, loc);
1111
1112                         if (union != null) {
1113
1114                                 Type most_specific_source, most_specific_target;
1115
1116                                 most_specific_source = FindMostEncompassedType (union, source_type);
1117                                 if (most_specific_source == null)
1118                                         return null;
1119
1120                                 most_specific_target = FindMostEncompassingType (union, target);
1121                                 if (most_specific_target == null) 
1122                                         return null;
1123                                 
1124                                 int count = 0;
1125                                 
1126                                 for (int i = union.Methods.Length; i > 0;) {
1127                                         i--;
1128
1129                                         MethodBase mb = union.Methods [i];
1130                                         ParameterData pd = Invocation.GetParameterData (mb);
1131                                         MethodInfo mi = (MethodInfo) union.Methods [i];
1132
1133                                         if (pd.ParameterType (0) == most_specific_source &&
1134                                             mi.ReturnType == most_specific_target) {
1135                                                 method = mb;
1136                                                 count++;
1137                                         }
1138                                 }
1139
1140                                 if (method == null || count > 1) {
1141                                         Report.Error (-11, loc, "Ambiguous user defined conversion");
1142                                         return null;
1143                                 }
1144                                 
1145                                 //
1146                                 // This will do the conversion to the best match that we
1147                                 // found.  Now we need to perform an implict standard conversion
1148                                 // if the best match was not the type that we were requested
1149                                 // by target.
1150                                 //
1151                                 if (look_for_explicit)
1152                                         source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1153                                 else
1154                                         source = ConvertImplicitStandard (ec, source,
1155                                                                           most_specific_source, loc);
1156
1157                                 if (source == null)
1158                                         return null;
1159
1160                                 e =  new UserCast ((MethodInfo) method, source);
1161                                 
1162                                 if (e.Type != target){
1163                                         if (!look_for_explicit)
1164                                                 e = ConvertImplicitStandard (ec, e, target, loc);
1165                                         else
1166                                                 e = ConvertExplicitStandard (ec, e, target, loc);
1167
1168                                         return e;
1169                                 } else
1170                                         return e;
1171                         }
1172                         
1173                         return null;
1174                 }
1175                 
1176                 /// <summary>
1177                 ///   Converts implicitly the resolved expression `expr' into the
1178                 ///   `target_type'.  It returns a new expression that can be used
1179                 ///   in a context that expects a `target_type'. 
1180                 /// </summary>
1181                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1182                                                           Type target_type, Location loc)
1183                 {
1184                         Type expr_type = expr.Type;
1185                         Expression e;
1186
1187                         if (expr_type == target_type)
1188                                 return expr;
1189
1190                         if (target_type == null)
1191                                 throw new Exception ("Target type is null");
1192
1193                         e = ConvertImplicitStandard (ec, expr, target_type, loc);
1194                         if (e != null)
1195                                 return e;
1196
1197                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1198                         if (e != null)
1199                                 return e;
1200
1201                         return null;
1202                 }
1203
1204                 
1205                 /// <summary>
1206                 ///   Attempts to apply the `Standard Implicit
1207                 ///   Conversion' rules to the expression `expr' into
1208                 ///   the `target_type'.  It returns a new expression
1209                 ///   that can be used in a context that expects a
1210                 ///   `target_type'.
1211                 ///
1212                 ///   This is different from `ConvertImplicit' in that the
1213                 ///   user defined implicit conversions are excluded. 
1214                 /// </summary>
1215                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1216                                                                   Type target_type, Location loc)
1217                 {
1218                         Type expr_type = expr.Type;
1219                         Expression e;
1220
1221                         if (expr_type == target_type)
1222                                 return expr;
1223
1224                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1225                         if (e != null)
1226                                 return e;
1227
1228                         e = ImplicitReferenceConversion (expr, target_type);
1229                         if (e != null)
1230                                 return e;
1231
1232                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1233                                 IntLiteral i = (IntLiteral) expr;
1234
1235                                 if (i.Value == 0)
1236                                         return new EmptyCast (expr, target_type);
1237                         }
1238
1239                         if (ec.InUnsafe) {
1240                                 if (expr_type.IsPointer){
1241                                         if (target_type == TypeManager.void_ptr_type)
1242                                                 return new EmptyCast (expr, target_type);
1243
1244                                         //
1245                                         // yep, comparing pointer types cant be done with
1246                                         // t1 == t2, we have to compare their element types.
1247                                         //
1248                                         if (target_type.IsPointer){
1249                                                 if (target_type.GetElementType()==expr_type.GetElementType())
1250                                                         return expr;
1251                                         }
1252                                 }
1253                                 
1254                                 if (target_type.IsPointer){
1255                                         if (expr is NullLiteral)
1256                                                 return new EmptyCast (expr, target_type);
1257                                 }
1258                         }
1259
1260                         return null;
1261                 }
1262
1263                 /// <summary>
1264                 ///   Attemps to perform an implict constant conversion of the IntConstant
1265                 ///   into a different data type using casts (See Implicit Constant
1266                 ///   Expression Conversions)
1267                 /// </summary>
1268                 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1269                 {
1270                         int value = ic.Value;
1271
1272                         //
1273                         // FIXME: This could return constants instead of EmptyCasts
1274                         //
1275                         if (target_type == TypeManager.sbyte_type){
1276                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1277                                         return new SByteConstant ((sbyte) value);
1278                         } else if (target_type == TypeManager.byte_type){
1279                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1280                                         return new ByteConstant ((byte) value);
1281                         } else if (target_type == TypeManager.short_type){
1282                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1283                                         return new ShortConstant ((short) value);
1284                         } else if (target_type == TypeManager.ushort_type){
1285                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1286                                         return new UShortConstant ((ushort) value);
1287                         } else if (target_type == TypeManager.uint32_type){
1288                                 if (value >= 0)
1289                                         return new UIntConstant ((uint) value);
1290                         } else if (target_type == TypeManager.uint64_type){
1291                                 //
1292                                 // we can optimize this case: a positive int32
1293                                 // always fits on a uint64.  But we need an opcode
1294                                 // to do it.
1295                                 //
1296                                 if (value >= 0)
1297                                         return new ULongConstant ((ulong) value);
1298                         }
1299                         
1300                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
1301                                 return new EnumConstant (ic, target_type);
1302
1303                         return null;
1304                 }
1305
1306                 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1307                 {
1308                         string msg = "Cannot convert implicitly from `"+
1309                                 TypeManager.CSharpName (source) + "' to `" +
1310                                 TypeManager.CSharpName (target) + "'";
1311
1312                         Error (29, loc, msg);
1313                 }
1314
1315                 /// <summary>
1316                 ///   Attemptes to implicityly convert `target' into `type', using
1317                 ///   ConvertImplicit.  If there is no implicit conversion, then
1318                 ///   an error is signaled
1319                 /// </summary>
1320                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1321                                                                   Type target_type, Location loc)
1322                 {
1323                         Expression e;
1324                         
1325                         e = ConvertImplicit (ec, source, target_type, loc);
1326                         if (e != null)
1327                                 return e;
1328
1329                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1330                                 Error (664, loc,
1331                                        "Double literal cannot be implicitly converted to " +
1332                                        "float type, use F suffix to create a float literal");
1333                         }
1334                         
1335                         Error_CannotConvertImplicit (loc, source.Type, target_type);
1336
1337                         return null;
1338                 }
1339
1340                 /// <summary>
1341                 ///   Performs the explicit numeric conversions
1342                 /// </summary>
1343                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr,
1344                                                           Type target_type)
1345                 {
1346                         Type expr_type = expr.Type;
1347
1348                         //
1349                         // If we have an enumeration, extract the underlying type,
1350                         // use this during the comparission, but wrap around the original
1351                         // target_type
1352                         //
1353                         Type real_target_type = target_type;
1354
1355                         if (TypeManager.IsEnumType (real_target_type))
1356                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1357
1358                         if (expr_type == TypeManager.sbyte_type){
1359                                 //
1360                                 // From sbyte to byte, ushort, uint, ulong, char
1361                                 //
1362                                 if (real_target_type == TypeManager.byte_type)
1363                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1364                                 if (real_target_type == TypeManager.ushort_type)
1365                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1366                                 if (real_target_type == TypeManager.uint32_type)
1367                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1368                                 if (real_target_type == TypeManager.uint64_type)
1369                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1370                                 if (real_target_type == TypeManager.char_type)
1371                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1372                         } else if (expr_type == TypeManager.byte_type){
1373                                 //
1374                                 // From byte to sbyte and char
1375                                 //
1376                                 if (real_target_type == TypeManager.sbyte_type)
1377                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1378                                 if (real_target_type == TypeManager.char_type)
1379                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1380                         } else if (expr_type == TypeManager.short_type){
1381                                 //
1382                                 // From short to sbyte, byte, ushort, uint, ulong, char
1383                                 //
1384                                 if (real_target_type == TypeManager.sbyte_type)
1385                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1386                                 if (real_target_type == TypeManager.byte_type)
1387                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1388                                 if (real_target_type == TypeManager.ushort_type)
1389                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1390                                 if (real_target_type == TypeManager.uint32_type)
1391                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1392                                 if (real_target_type == TypeManager.uint64_type)
1393                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1394                                 if (real_target_type == TypeManager.char_type)
1395                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1396                         } else if (expr_type == TypeManager.ushort_type){
1397                                 //
1398                                 // From ushort to sbyte, byte, short, char
1399                                 //
1400                                 if (real_target_type == TypeManager.sbyte_type)
1401                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1402                                 if (real_target_type == TypeManager.byte_type)
1403                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1404                                 if (real_target_type == TypeManager.short_type)
1405                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1406                                 if (real_target_type == TypeManager.char_type)
1407                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1408                         } else if (expr_type == TypeManager.int32_type){
1409                                 //
1410                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1411                                 //
1412                                 if (real_target_type == TypeManager.sbyte_type)
1413                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1414                                 if (real_target_type == TypeManager.byte_type)
1415                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1416                                 if (real_target_type == TypeManager.short_type)
1417                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1418                                 if (real_target_type == TypeManager.ushort_type)
1419                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1420                                 if (real_target_type == TypeManager.uint32_type)
1421                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1422                                 if (real_target_type == TypeManager.uint64_type)
1423                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1424                                 if (real_target_type == TypeManager.char_type)
1425                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1426                         } else if (expr_type == TypeManager.uint32_type){
1427                                 //
1428                                 // From uint to sbyte, byte, short, ushort, int, char
1429                                 //
1430                                 if (real_target_type == TypeManager.sbyte_type)
1431                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1432                                 if (real_target_type == TypeManager.byte_type)
1433                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1434                                 if (real_target_type == TypeManager.short_type)
1435                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1436                                 if (real_target_type == TypeManager.ushort_type)
1437                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1438                                 if (real_target_type == TypeManager.int32_type)
1439                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1440                                 if (real_target_type == TypeManager.char_type)
1441                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1442                         } else if (expr_type == TypeManager.int64_type){
1443                                 //
1444                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1445                                 //
1446                                 if (real_target_type == TypeManager.sbyte_type)
1447                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1448                                 if (real_target_type == TypeManager.byte_type)
1449                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1450                                 if (real_target_type == TypeManager.short_type)
1451                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1452                                 if (real_target_type == TypeManager.ushort_type)
1453                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1454                                 if (real_target_type == TypeManager.int32_type)
1455                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1456                                 if (real_target_type == TypeManager.uint32_type)
1457                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1458                                 if (real_target_type == TypeManager.uint64_type)
1459                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1460                                 if (real_target_type == TypeManager.char_type)
1461                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1462                         } else if (expr_type == TypeManager.uint64_type){
1463                                 //
1464                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1465                                 //
1466                                 if (real_target_type == TypeManager.sbyte_type)
1467                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1468                                 if (real_target_type == TypeManager.byte_type)
1469                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1470                                 if (real_target_type == TypeManager.short_type)
1471                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1472                                 if (real_target_type == TypeManager.ushort_type)
1473                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1474                                 if (real_target_type == TypeManager.int32_type)
1475                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1476                                 if (real_target_type == TypeManager.uint32_type)
1477                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1478                                 if (real_target_type == TypeManager.int64_type)
1479                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1480                                 if (real_target_type == TypeManager.char_type)
1481                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1482                         } else if (expr_type == TypeManager.char_type){
1483                                 //
1484                                 // From char to sbyte, byte, short
1485                                 //
1486                                 if (real_target_type == TypeManager.sbyte_type)
1487                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1488                                 if (real_target_type == TypeManager.byte_type)
1489                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1490                                 if (real_target_type == TypeManager.short_type)
1491                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1492                         } else if (expr_type == TypeManager.float_type){
1493                                 //
1494                                 // From float to sbyte, byte, short,
1495                                 // ushort, int, uint, long, ulong, char
1496                                 // or decimal
1497                                 //
1498                                 if (real_target_type == TypeManager.sbyte_type)
1499                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1500                                 if (real_target_type == TypeManager.byte_type)
1501                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1502                                 if (real_target_type == TypeManager.short_type)
1503                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1504                                 if (real_target_type == TypeManager.ushort_type)
1505                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1506                                 if (real_target_type == TypeManager.int32_type)
1507                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1508                                 if (real_target_type == TypeManager.uint32_type)
1509                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1510                                 if (real_target_type == TypeManager.int64_type)
1511                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1512                                 if (real_target_type == TypeManager.uint64_type)
1513                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1514                                 if (real_target_type == TypeManager.char_type)
1515                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1516                                 if (real_target_type == TypeManager.decimal_type)
1517                                         return InternalTypeConstructor (ec, expr, target_type);
1518                         } else if (expr_type == TypeManager.double_type){
1519                                 //
1520                                 // From double to byte, byte, short,
1521                                 // ushort, int, uint, long, ulong,
1522                                 // char, float or decimal
1523                                 //
1524                                 if (real_target_type == TypeManager.sbyte_type)
1525                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1526                                 if (real_target_type == TypeManager.byte_type)
1527                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1528                                 if (real_target_type == TypeManager.short_type)
1529                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1530                                 if (real_target_type == TypeManager.ushort_type)
1531                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1532                                 if (real_target_type == TypeManager.int32_type)
1533                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1534                                 if (real_target_type == TypeManager.uint32_type)
1535                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1536                                 if (real_target_type == TypeManager.int64_type)
1537                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1538                                 if (real_target_type == TypeManager.uint64_type)
1539                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1540                                 if (real_target_type == TypeManager.char_type)
1541                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1542                                 if (real_target_type == TypeManager.float_type)
1543                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1544                                 if (real_target_type == TypeManager.decimal_type)
1545                                         return InternalTypeConstructor (ec, expr, target_type);
1546                         } 
1547
1548                         // decimal is taken care of by the op_Explicit methods.
1549
1550                         return null;
1551                 }
1552
1553                 /// <summary>
1554                 ///  Returns whether an explicit reference conversion can be performed
1555                 ///  from source_type to target_type
1556                 /// </summary>
1557                 static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1558                 {
1559                         bool target_is_value_type = target_type.IsValueType;
1560                         
1561                         if (source_type == target_type)
1562                                 return true;
1563                         
1564                         //
1565                         // From object to any reference type
1566                         //
1567                         if (source_type == TypeManager.object_type && !target_is_value_type)
1568                                 return true;
1569                                         
1570                         //
1571                         // From any class S to any class-type T, provided S is a base class of T
1572                         //
1573                         if (target_type.IsSubclassOf (source_type))
1574                                 return true;
1575
1576                         //
1577                         // From any interface type S to any interface T provided S is not derived from T
1578                         //
1579                         if (source_type.IsInterface && target_type.IsInterface){
1580                                 if (!target_type.IsSubclassOf (source_type))
1581                                         return true;
1582                         }
1583                             
1584                         //
1585                         // From any class type S to any interface T, provides S is not sealed
1586                         // and provided S does not implement T.
1587                         //
1588                         if (target_type.IsInterface && !source_type.IsSealed &&
1589                             !target_type.IsAssignableFrom (source_type))
1590                                 return true;
1591
1592                         //
1593                         // From any interface-type S to to any class type T, provided T is not
1594                         // sealed, or provided T implements S.
1595                         //
1596                         if (source_type.IsInterface &&
1597                             (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
1598                                 return true;
1599
1600                         // From an array type S with an element type Se to an array type T with an 
1601                         // element type Te provided all the following are true:
1602                         //     * S and T differe only in element type, in other words, S and T
1603                         //       have the same number of dimensions.
1604                         //     * Both Se and Te are reference types
1605                         //     * An explicit referenc conversions exist from Se to Te
1606                         //
1607                         if (source_type.IsArray && target_type.IsArray) {
1608                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1609                                         
1610                                         Type source_element_type = source_type.GetElementType ();
1611                                         Type target_element_type = target_type.GetElementType ();
1612                                         
1613                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1614                                                 if (ExplicitReferenceConversionExists (source_element_type,
1615                                                                                        target_element_type))
1616                                                         return true;
1617                                 }
1618                         }
1619                         
1620
1621                         // From System.Array to any array-type
1622                         if (source_type == TypeManager.array_type &&
1623                             target_type.IsSubclassOf (TypeManager.array_type)){
1624                                 return true;
1625                         }
1626
1627                         //
1628                         // From System delegate to any delegate-type
1629                         //
1630                         if (source_type == TypeManager.delegate_type &&
1631                             target_type.IsSubclassOf (TypeManager.delegate_type))
1632                                 return true;
1633
1634                         //
1635                         // From ICloneable to Array or Delegate types
1636                         //
1637                         if (source_type == TypeManager.icloneable_type &&
1638                             (target_type == TypeManager.array_type ||
1639                              target_type == TypeManager.delegate_type))
1640                                 return true;
1641                         
1642                         return false;
1643                 }
1644
1645                 /// <summary>
1646                 ///   Implements Explicit Reference conversions
1647                 /// </summary>
1648                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
1649                 {
1650                         Type source_type = source.Type;
1651                         bool target_is_value_type = target_type.IsValueType;
1652                         
1653                         //
1654                         // From object to any reference type
1655                         //
1656                         if (source_type == TypeManager.object_type && !target_is_value_type)
1657                                 return new ClassCast (source, target_type);
1658
1659
1660                         //
1661                         // From any class S to any class-type T, provided S is a base class of T
1662                         //
1663                         if (target_type.IsSubclassOf (source_type))
1664                                 return new ClassCast (source, target_type);
1665
1666                         //
1667                         // From any interface type S to any interface T provided S is not derived from T
1668                         //
1669                         if (source_type.IsInterface && target_type.IsInterface){
1670                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1671                                         return null;
1672                                 else
1673                                         return new ClassCast (source, target_type);
1674                         }
1675                             
1676                         //
1677                         // From any class type S to any interface T, provides S is not sealed
1678                         // and provided S does not implement T.
1679                         //
1680                         if (target_type.IsInterface && !source_type.IsSealed) {
1681                                 
1682                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1683                                         return null;
1684                                 else
1685                                         return new ClassCast (source, target_type);
1686                                 
1687                         }
1688
1689                         //
1690                         // From any interface-type S to to any class type T, provided T is not
1691                         // sealed, or provided T implements S.
1692                         //
1693                         if (source_type.IsInterface) {
1694
1695                                 if (target_type.IsSealed)
1696                                         return null;
1697                                 
1698                                 if (TypeManager.ImplementsInterface (target_type, source_type))
1699                                         return new ClassCast (source, target_type);
1700                                 else
1701                                         return null;
1702                         }
1703                         
1704                         // From an array type S with an element type Se to an array type T with an 
1705                         // element type Te provided all the following are true:
1706                         //     * S and T differe only in element type, in other words, S and T
1707                         //       have the same number of dimensions.
1708                         //     * Both Se and Te are reference types
1709                         //     * An explicit referenc conversions exist from Se to Te
1710                         //
1711                         if (source_type.IsArray && target_type.IsArray) {
1712                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1713                                         
1714                                         Type source_element_type = source_type.GetElementType ();
1715                                         Type target_element_type = target_type.GetElementType ();
1716                                         
1717                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1718                                                 if (ExplicitReferenceConversionExists (source_element_type,
1719                                                                                        target_element_type))
1720                                                         return new ClassCast (source, target_type);
1721                                 }
1722                         }
1723                         
1724
1725                         // From System.Array to any array-type
1726                         if (source_type == TypeManager.array_type &&
1727                             target_type.IsSubclassOf (TypeManager.array_type)){
1728                                 return new ClassCast (source, target_type);
1729                         }
1730
1731                         //
1732                         // From System delegate to any delegate-type
1733                         //
1734                         if (source_type == TypeManager.delegate_type &&
1735                             target_type.IsSubclassOf (TypeManager.delegate_type))
1736                                 return new ClassCast (source, target_type);
1737
1738                         //
1739                         // From ICloneable to Array or Delegate types
1740                         //
1741                         if (source_type == TypeManager.icloneable_type &&
1742                             (target_type == TypeManager.array_type ||
1743                              target_type == TypeManager.delegate_type))
1744                                 return new ClassCast (source, target_type);
1745                         
1746                         return null;
1747                 }
1748                 
1749                 /// <summary>
1750                 ///   Performs an explicit conversion of the expression `expr' whose
1751                 ///   type is expr.Type to `target_type'.
1752                 /// </summary>
1753                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
1754                                                           Type target_type, Location loc)
1755                 {
1756                         Type expr_type = expr.Type;
1757                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
1758
1759                         if (ne != null)
1760                                 return ne;
1761
1762                         ne = ConvertNumericExplicit (ec, expr, target_type);
1763                         if (ne != null)
1764                                 return ne;
1765
1766                         //
1767                         // Unboxing conversion.
1768                         //
1769                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1770                                 return new UnboxCast (expr, target_type);
1771
1772                         //
1773                         // Enum types
1774                         //
1775                         if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
1776                                 Expression e;
1777
1778                                 //
1779                                 // FIXME: Is there any reason we should have EnumConstant
1780                                 // dealt with here instead of just using always the
1781                                 // UnderlyingSystemType to wrap the type?
1782                                 //
1783                                 if (expr is EnumConstant)
1784                                         e = ((EnumConstant) expr).Child;
1785                                 else {
1786                                         e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1787                                 }
1788                                 
1789                                 Expression t = ConvertImplicit (ec, e, target_type, loc);
1790                                 if (t != null)
1791                                         return t;
1792                                 
1793                                 return ConvertNumericExplicit (ec, e, target_type);
1794                         }
1795                         
1796                         ne = ConvertReferenceExplicit (expr, target_type);
1797                         if (ne != null)
1798                                 return ne;
1799
1800                         if (ec.InUnsafe){
1801                                 if (target_type.IsPointer){
1802                                         if (expr_type.IsPointer)
1803                                                 return new EmptyCast (expr, target_type);
1804                                         
1805                                         if (expr_type == TypeManager.sbyte_type ||
1806                                             expr_type == TypeManager.byte_type ||
1807                                             expr_type == TypeManager.short_type ||
1808                                             expr_type == TypeManager.ushort_type ||
1809                                             expr_type == TypeManager.int32_type ||
1810                                             expr_type == TypeManager.uint32_type ||
1811                                             expr_type == TypeManager.uint64_type ||
1812                                             expr_type == TypeManager.int64_type)
1813                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1814                                 }
1815                                 if (expr_type.IsPointer){
1816                                         if (target_type == TypeManager.sbyte_type ||
1817                                             target_type == TypeManager.byte_type ||
1818                                             target_type == TypeManager.short_type ||
1819                                             target_type == TypeManager.ushort_type ||
1820                                             target_type == TypeManager.int32_type ||
1821                                             target_type == TypeManager.uint32_type ||
1822                                             target_type == TypeManager.uint64_type ||
1823                                             target_type == TypeManager.int64_type){
1824                                                 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
1825                                                 Expression ci, ce;
1826
1827                                                 ci = ConvertImplicitStandard (ec, e, target_type, loc);
1828
1829                                                 if (ci != null)
1830                                                         return ci;
1831
1832                                                 ce = ConvertNumericExplicit (ec, e, target_type);
1833                                                 if (ce != null)
1834                                                         return ce;
1835                                                 //
1836                                                 // We should always be able to go from an uint32
1837                                                 // implicitly or explicitly to the other integral
1838                                                 // types
1839                                                 //
1840                                                 throw new Exception ("Internal compiler error");
1841                                         }
1842                                 }
1843                         }
1844                         
1845                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1846                         if (ne != null)
1847                                 return ne;
1848
1849                         Error_CannotConvertType (loc, expr_type, target_type);
1850                         return null;
1851                 }
1852
1853                 /// <summary>
1854                 ///   Same as ConverExplicit, only it doesn't include user defined conversions
1855                 /// </summary>
1856                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
1857                                                                   Type target_type, Location l)
1858                 {
1859                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
1860
1861                         if (ne != null)
1862                                 return ne;
1863
1864                         ne = ConvertNumericExplicit (ec, expr, target_type);
1865                         if (ne != null)
1866                                 return ne;
1867
1868                         ne = ConvertReferenceExplicit (expr, target_type);
1869                         if (ne != null)
1870                                 return ne;
1871
1872                         Error_CannotConvertType (l, expr.Type, target_type);
1873                         return null;
1874                 }
1875
1876                 static string ExprClassName (ExprClass c)
1877                 {
1878                         switch (c){
1879                         case ExprClass.Invalid:
1880                                 return "Invalid";
1881                         case ExprClass.Value:
1882                                 return "value";
1883                         case ExprClass.Variable:
1884                                 return "variable";
1885                         case ExprClass.Namespace:
1886                                 return "namespace";
1887                         case ExprClass.Type:
1888                                 return "type";
1889                         case ExprClass.MethodGroup:
1890                                 return "method group";
1891                         case ExprClass.PropertyAccess:
1892                                 return "property access";
1893                         case ExprClass.EventAccess:
1894                                 return "event access";
1895                         case ExprClass.IndexerAccess:
1896                                 return "indexer access";
1897                         case ExprClass.Nothing:
1898                                 return "null";
1899                         }
1900                         throw new Exception ("Should not happen");
1901                 }
1902                 
1903                 /// <summary>
1904                 ///   Reports that we were expecting `expr' to be of class `expected'
1905                 /// </summary>
1906                 protected void report118 (Location loc, Expression expr, string expected)
1907                 {
1908                         string kind = "Unknown";
1909                         
1910                         if (expr != null)
1911                                 kind = ExprClassName (expr.eclass);
1912
1913                         Error (118, loc, "Expression denotes a `" + kind +
1914                                "' where a `" + expected + "' was expected");
1915                 }
1916
1917                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
1918                 {
1919                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
1920                                       TypeManager.CSharpName (t));
1921                 }
1922
1923                 public static void UnsafeError (Location loc)
1924                 {
1925                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
1926                 }
1927                 
1928                 /// <summary>
1929                 ///   Converts the IntConstant, UIntConstant, LongConstant or
1930                 ///   ULongConstant into the integral target_type.   Notice
1931                 ///   that we do not return an `Expression' we do return
1932                 ///   a boxed integral type.
1933                 ///
1934                 ///   FIXME: Since I added the new constants, we need to
1935                 ///   also support conversions from CharConstant, ByteConstant,
1936                 ///   SByteConstant, UShortConstant, ShortConstant
1937                 ///
1938                 ///   This is used by the switch statement, so the domain
1939                 ///   of work is restricted to the literals above, and the
1940                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
1941                 ///   short, uint64 and int64
1942                 /// </summary>
1943                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
1944                 {
1945                         string s = "";
1946
1947                         if (c.Type == target_type)
1948                                 return ((Constant) c).GetValue ();
1949
1950                         //
1951                         // Make into one of the literals we handle, we dont really care
1952                         // about this value as we will just return a few limited types
1953                         // 
1954                         if (c is EnumConstant)
1955                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
1956
1957                         if (c is IntConstant){
1958                                 int v = ((IntConstant) c).Value;
1959                                 
1960                                 if (target_type == TypeManager.uint32_type){
1961                                         if (v >= 0)
1962                                                 return (uint) v;
1963                                 } else if (target_type == TypeManager.char_type){
1964                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1965                                                 return (char) v;
1966                                 } else if (target_type == TypeManager.byte_type){
1967                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1968                                                 return (byte) v;
1969                                 } else if (target_type == TypeManager.sbyte_type){
1970                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1971                                                 return (sbyte) v;
1972                                 } else if (target_type == TypeManager.short_type){
1973                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1974                                                 return (short) v;
1975                                 } else if (target_type == TypeManager.ushort_type){
1976                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1977                                                 return (ushort) v;
1978                                 } else if (target_type == TypeManager.int64_type)
1979                                         return (long) v;
1980                                 else if (target_type == TypeManager.uint64_type){
1981                                         if (v > 0)
1982                                                 return (ulong) v;
1983                                 }
1984
1985                                 s = v.ToString ();
1986                         } else if (c is UIntConstant){
1987                                 uint v = ((UIntConstant) c).Value;
1988
1989                                 if (target_type == TypeManager.int32_type){
1990                                         if (v <= Int32.MaxValue)
1991                                                 return (int) v;
1992                                 } else if (target_type == TypeManager.char_type){
1993                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1994                                                 return (char) v;
1995                                 } else if (target_type == TypeManager.byte_type){
1996                                         if (v <= Byte.MaxValue)
1997                                                 return (byte) v;
1998                                 } else if (target_type == TypeManager.sbyte_type){
1999                                         if (v <= SByte.MaxValue)
2000                                                 return (sbyte) v;
2001                                 } else if (target_type == TypeManager.short_type){
2002                                         if (v <= UInt16.MaxValue)
2003                                                 return (short) v;
2004                                 } else if (target_type == TypeManager.ushort_type){
2005                                         if (v <= UInt16.MaxValue)
2006                                                 return (ushort) v;
2007                                 } else if (target_type == TypeManager.int64_type)
2008                                         return (long) v;
2009                                 else if (target_type == TypeManager.uint64_type)
2010                                         return (ulong) v;
2011                                 s = v.ToString ();
2012                         } else if (c is LongConstant){ 
2013                                 long v = ((LongConstant) c).Value;
2014
2015                                 if (target_type == TypeManager.int32_type){
2016                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2017                                                 return (int) v;
2018                                 } else if (target_type == TypeManager.uint32_type){
2019                                         if (v >= 0 && v <= UInt32.MaxValue)
2020                                                 return (uint) v;
2021                                 } else if (target_type == TypeManager.char_type){
2022                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2023                                                 return (char) v;
2024                                 } else if (target_type == TypeManager.byte_type){
2025                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2026                                                 return (byte) v;
2027                                 } else if (target_type == TypeManager.sbyte_type){
2028                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2029                                                 return (sbyte) v;
2030                                 } else if (target_type == TypeManager.short_type){
2031                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2032                                                 return (short) v;
2033                                 } else if (target_type == TypeManager.ushort_type){
2034                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2035                                                 return (ushort) v;
2036                                 } else if (target_type == TypeManager.uint64_type){
2037                                         if (v > 0)
2038                                                 return (ulong) v;
2039                                 }
2040                                 s = v.ToString ();
2041                         } else if (c is ULongConstant){
2042                                 ulong v = ((ULongConstant) c).Value;
2043
2044                                 if (target_type == TypeManager.int32_type){
2045                                         if (v <= Int32.MaxValue)
2046                                                 return (int) v;
2047                                 } else if (target_type == TypeManager.uint32_type){
2048                                         if (v <= UInt32.MaxValue)
2049                                                 return (uint) v;
2050                                 } else if (target_type == TypeManager.char_type){
2051                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2052                                                 return (char) v;
2053                                 } else if (target_type == TypeManager.byte_type){
2054                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2055                                                 return (byte) v;
2056                                 } else if (target_type == TypeManager.sbyte_type){
2057                                         if (v <= (int) SByte.MaxValue)
2058                                                 return (sbyte) v;
2059                                 } else if (target_type == TypeManager.short_type){
2060                                         if (v <= UInt16.MaxValue)
2061                                                 return (short) v;
2062                                 } else if (target_type == TypeManager.ushort_type){
2063                                         if (v <= UInt16.MaxValue)
2064                                                 return (ushort) v;
2065                                 } else if (target_type == TypeManager.int64_type){
2066                                         if (v <= Int64.MaxValue)
2067                                                 return (long) v;
2068                                 }
2069                                 s = v.ToString ();
2070                         } else if (c is ByteConstant){
2071                                 byte v = ((ByteConstant) c).Value;
2072                                 
2073                                 if (target_type == TypeManager.int32_type)
2074                                         return (int) v;
2075                                 else if (target_type == TypeManager.uint32_type)
2076                                         return (uint) v;
2077                                 else if (target_type == TypeManager.char_type)
2078                                         return (char) v;
2079                                 else if (target_type == TypeManager.sbyte_type){
2080                                         if (v <= SByte.MaxValue)
2081                                                 return (sbyte) v;
2082                                 } else if (target_type == TypeManager.short_type)
2083                                         return (short) v;
2084                                 else if (target_type == TypeManager.ushort_type)
2085                                         return (ushort) v;
2086                                 else if (target_type == TypeManager.int64_type)
2087                                         return (long) v;
2088                                 else if (target_type == TypeManager.uint64_type)
2089                                         return (ulong) v;
2090                                 s = v.ToString ();
2091                         } else if (c is SByteConstant){
2092                                 sbyte v = ((SByteConstant) c).Value;
2093                                 
2094                                 if (target_type == TypeManager.int32_type)
2095                                         return (int) v;
2096                                 else if (target_type == TypeManager.uint32_type){
2097                                         if (v >= 0)
2098                                                 return (uint) v;
2099                                 } else if (target_type == TypeManager.char_type){
2100                                         if (v >= 0)
2101                                                 return (char) v;
2102                                 } else if (target_type == TypeManager.byte_type){
2103                                         if (v >= 0)
2104                                                 return (byte) v;
2105                                 } else if (target_type == TypeManager.short_type)
2106                                         return (short) v;
2107                                 else if (target_type == TypeManager.ushort_type){
2108                                         if (v >= 0)
2109                                                 return (ushort) v;
2110                                 } else if (target_type == TypeManager.int64_type)
2111                                         return (long) v;
2112                                 else if (target_type == TypeManager.uint64_type){
2113                                         if (v >= 0)
2114                                                 return (ulong) v;
2115                                 }
2116                                 s = v.ToString ();
2117                         } else if (c is ShortConstant){
2118                                 short v = ((ShortConstant) c).Value;
2119                                 
2120                                 if (target_type == TypeManager.int32_type){
2121                                         return (int) v;
2122                                 } else if (target_type == TypeManager.uint32_type){
2123                                         if (v >= 0)
2124                                                 return (uint) v;
2125                                 } else if (target_type == TypeManager.char_type){
2126                                         if (v >= 0)
2127                                                 return (char) v;
2128                                 } else if (target_type == TypeManager.byte_type){
2129                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2130                                                 return (byte) v;
2131                                 } else if (target_type == TypeManager.sbyte_type){
2132                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2133                                                 return (sbyte) v;
2134                                 } else if (target_type == TypeManager.ushort_type){
2135                                         if (v >= 0)
2136                                                 return (ushort) v;
2137                                 } else if (target_type == TypeManager.int64_type)
2138                                         return (long) v;
2139                                 else if (target_type == TypeManager.uint64_type)
2140                                         return (ulong) v;
2141
2142                                 s = v.ToString ();
2143                         } else if (c is UShortConstant){
2144                                 ushort v = ((UShortConstant) c).Value;
2145                                 
2146                                 if (target_type == TypeManager.int32_type)
2147                                         return (int) v;
2148                                 else if (target_type == TypeManager.uint32_type)
2149                                         return (uint) v;
2150                                 else if (target_type == TypeManager.char_type){
2151                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2152                                                 return (char) v;
2153                                 } else if (target_type == TypeManager.byte_type){
2154                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2155                                                 return (byte) v;
2156                                 } else if (target_type == TypeManager.sbyte_type){
2157                                         if (v <= SByte.MaxValue)
2158                                                 return (byte) v;
2159                                 } else if (target_type == TypeManager.short_type){
2160                                         if (v <= Int16.MaxValue)
2161                                                 return (short) v;
2162                                 } else if (target_type == TypeManager.int64_type)
2163                                         return (long) v;
2164                                 else if (target_type == TypeManager.uint64_type)
2165                                         return (ulong) v;
2166
2167                                 s = v.ToString ();
2168                         } else if (c is CharConstant){
2169                                 char v = ((CharConstant) c).Value;
2170                                 
2171                                 if (target_type == TypeManager.int32_type)
2172                                         return (int) v;
2173                                 else if (target_type == TypeManager.uint32_type)
2174                                         return (uint) v;
2175                                 else if (target_type == TypeManager.byte_type){
2176                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2177                                                 return (byte) v;
2178                                 } else if (target_type == TypeManager.sbyte_type){
2179                                         if (v <= SByte.MaxValue)
2180                                                 return (sbyte) v;
2181                                 } else if (target_type == TypeManager.short_type){
2182                                         if (v <= Int16.MaxValue)
2183                                                 return (short) v;
2184                                 } else if (target_type == TypeManager.ushort_type)
2185                                         return (short) v;
2186                                 else if (target_type == TypeManager.int64_type)
2187                                         return (long) v;
2188                                 else if (target_type == TypeManager.uint64_type)
2189                                         return (ulong) v;
2190
2191                                 s = v.ToString ();
2192                         }
2193                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
2194                         return null;
2195                 }
2196
2197                 //
2198                 // Load the object from the pointer.  The `IsReference' is used
2199                 // to control whether we should use Ldind_Ref or LdObj if the
2200                 // value is not a `core' type.
2201                 //
2202                 // Maybe we should try to extract this infromation form the type?
2203                 // TODO: Maybe this is a bug.  The reason we have this flag is because
2204                 // I had almost identical code in ParameterReference (for handling
2205                 // references) and in UnboxCast.
2206                 //
2207                 public static void LoadFromPtr (ILGenerator ig, Type t, bool IsReference)
2208                 {
2209                         if (t == TypeManager.int32_type)
2210                                 ig.Emit (OpCodes.Ldind_I4);
2211                         else if (t == TypeManager.uint32_type)
2212                                 ig.Emit (OpCodes.Ldind_U4);
2213                         else if (t == TypeManager.short_type)
2214                                 ig.Emit (OpCodes.Ldind_I2);
2215                         else if (t == TypeManager.ushort_type)
2216                                 ig.Emit (OpCodes.Ldind_U2);
2217                         else if (t == TypeManager.char_type)
2218                                 ig.Emit (OpCodes.Ldind_U2);
2219                         else if (t == TypeManager.byte_type)
2220                                 ig.Emit (OpCodes.Ldind_U1);
2221                         else if (t == TypeManager.sbyte_type)
2222                                 ig.Emit (OpCodes.Ldind_I1);
2223                         else if (t == TypeManager.uint64_type)
2224                                 ig.Emit (OpCodes.Ldind_I8);
2225                         else if (t == TypeManager.int64_type)
2226                                 ig.Emit (OpCodes.Ldind_I8);
2227                         else if (t == TypeManager.float_type)
2228                                 ig.Emit (OpCodes.Ldind_R4);
2229                         else if (t == TypeManager.double_type)
2230                                 ig.Emit (OpCodes.Ldind_R8);
2231                         else if (t == TypeManager.bool_type)
2232                                 ig.Emit (OpCodes.Ldind_I1);
2233                         else if (t == TypeManager.intptr_type)
2234                                 ig.Emit (OpCodes.Ldind_I);
2235                         else if (TypeManager.IsEnumType (t)){
2236                                 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t), IsReference);
2237                         } else {
2238                                 if (IsReference)
2239                                         ig.Emit (OpCodes.Ldind_Ref);
2240                                 else 
2241                                         ig.Emit (OpCodes.Ldobj, t);
2242                         }
2243                 }
2244
2245                 //
2246                 // The stack contains the pointer and the value of type `type'
2247                 //
2248                 public static void StoreFromPtr (ILGenerator ig, Type type)
2249                 {
2250                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2251                                 ig.Emit (OpCodes.Stind_I4);
2252                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2253                                 ig.Emit (OpCodes.Stind_I8);
2254                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2255                                  type == TypeManager.ushort_type)
2256                                 ig.Emit (OpCodes.Stind_I2);
2257                         else if (type == TypeManager.float_type)
2258                                 ig.Emit (OpCodes.Stind_R4);
2259                         else if (type == TypeManager.double_type)
2260                                 ig.Emit (OpCodes.Stind_R8);
2261                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2262                                  type == TypeManager.bool_type)
2263                                 ig.Emit (OpCodes.Stind_I1);
2264                         else if (type == TypeManager.intptr_type)
2265                                 ig.Emit (OpCodes.Stind_I);
2266                         else
2267                                 ig.Emit (OpCodes.Stind_Ref);
2268                 }
2269                 
2270                 //
2271                 // Returns the size of type `t' if known, otherwise, 0
2272                 //
2273                 public static int GetTypeSize (Type t)
2274                 {
2275                         if (t == TypeManager.int32_type ||
2276                             t == TypeManager.uint32_type ||
2277                             t == TypeManager.float_type)
2278                                 return 4;
2279                         else if (t == TypeManager.int64_type ||
2280                                  t == TypeManager.uint64_type ||
2281                                  t == TypeManager.double_type)
2282                                 return 8;
2283                         else if (t == TypeManager.byte_type ||
2284                                  t == TypeManager.sbyte_type ||
2285                                  t == TypeManager.bool_type)    
2286                                 return 1;
2287                         else if (t == TypeManager.short_type ||
2288                                  t == TypeManager.char_type ||
2289                                  t == TypeManager.ushort_type)
2290                                 return 2;
2291                         else
2292                                 return 0;
2293                 }
2294         }
2295
2296         /// <summary>
2297         ///   This is just a base class for expressions that can
2298         ///   appear on statements (invocations, object creation,
2299         ///   assignments, post/pre increment and decrement).  The idea
2300         ///   being that they would support an extra Emition interface that
2301         ///   does not leave a result on the stack.
2302         /// </summary>
2303         public abstract class ExpressionStatement : Expression {
2304
2305                 /// <summary>
2306                 ///   Requests the expression to be emitted in a `statement'
2307                 ///   context.  This means that no new value is left on the
2308                 ///   stack after invoking this method (constrasted with
2309                 ///   Emit that will always leave a value on the stack).
2310                 /// </summary>
2311                 public abstract void EmitStatement (EmitContext ec);
2312         }
2313
2314         /// <summary>
2315         ///   This kind of cast is used to encapsulate the child
2316         ///   whose type is child.Type into an expression that is
2317         ///   reported to return "return_type".  This is used to encapsulate
2318         ///   expressions which have compatible types, but need to be dealt
2319         ///   at higher levels with.
2320         ///
2321         ///   For example, a "byte" expression could be encapsulated in one
2322         ///   of these as an "unsigned int".  The type for the expression
2323         ///   would be "unsigned int".
2324         ///
2325         /// </summary>
2326         public class EmptyCast : Expression {
2327                 protected Expression child;
2328
2329                 public EmptyCast (Expression child, Type return_type)
2330                 {
2331                         eclass = child.eclass;
2332                         type = return_type;
2333                         this.child = child;
2334                 }
2335
2336                 public override Expression DoResolve (EmitContext ec)
2337                 {
2338                         // This should never be invoked, we are born in fully
2339                         // initialized state.
2340
2341                         return this;
2342                 }
2343
2344                 public override void Emit (EmitContext ec)
2345                 {
2346                         child.Emit (ec);
2347                 }
2348         }
2349
2350         /// <summary>
2351         ///  This class is used to wrap literals which belong inside Enums
2352         /// </summary>
2353         public class EnumConstant : Constant {
2354                 public Constant Child;
2355
2356                 public EnumConstant (Constant child, Type enum_type)
2357                 {
2358                         eclass = child.eclass;
2359                         this.Child = child;
2360                         type = enum_type;
2361                 }
2362                 
2363                 public override Expression DoResolve (EmitContext ec)
2364                 {
2365                         // This should never be invoked, we are born in fully
2366                         // initialized state.
2367
2368                         return this;
2369                 }
2370
2371                 public override void Emit (EmitContext ec)
2372                 {
2373                         Child.Emit (ec);
2374                 }
2375
2376                 public override object GetValue ()
2377                 {
2378                         return Child.GetValue ();
2379                 }
2380
2381                 //
2382                 // Converts from one of the valid underlying types for an enumeration
2383                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2384                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2385                 //
2386                 public Constant WidenToCompilerConstant ()
2387                 {
2388                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2389                         object v = ((Constant) Child).GetValue ();;
2390                         
2391                         if (t == TypeManager.int32_type)
2392                                 return new IntConstant ((int) v);
2393                         if (t == TypeManager.uint32_type)
2394                                 return new UIntConstant ((uint) v);
2395                         if (t == TypeManager.int64_type)
2396                                 return new LongConstant ((long) v);
2397                         if (t == TypeManager.uint64_type)
2398                                 return new ULongConstant ((ulong) v);
2399                         if (t == TypeManager.short_type)
2400                                 return new ShortConstant ((short) v);
2401                         if (t == TypeManager.ushort_type)
2402                                 return new UShortConstant ((ushort) v);
2403                         if (t == TypeManager.byte_type)
2404                                 return new ByteConstant ((byte) v);
2405                         if (t == TypeManager.sbyte_type)
2406                                 return new SByteConstant ((sbyte) v);
2407
2408                         throw new Exception ("Invalid enumeration underlying type: " + t);
2409                 }
2410
2411                 //
2412                 // Extracts the value in the enumeration on its native representation
2413                 //
2414                 public object GetPlainValue ()
2415                 {
2416                         Type t = TypeManager.EnumToUnderlying (Child.Type);
2417                         object v = ((Constant) Child).GetValue ();;
2418                         
2419                         if (t == TypeManager.int32_type)
2420                                 return (int) v;
2421                         if (t == TypeManager.uint32_type)
2422                                 return (uint) v;
2423                         if (t == TypeManager.int64_type)
2424                                 return (long) v;
2425                         if (t == TypeManager.uint64_type)
2426                                 return (ulong) v;
2427                         if (t == TypeManager.short_type)
2428                                 return (short) v;
2429                         if (t == TypeManager.ushort_type)
2430                                 return (ushort) v;
2431                         if (t == TypeManager.byte_type)
2432                                 return (byte) v;
2433                         if (t == TypeManager.sbyte_type)
2434                                 return (sbyte) v;
2435
2436                         return null;
2437                 }
2438                 
2439                 public override string AsString ()
2440                 {
2441                         return Child.AsString ();
2442                 }
2443
2444                 public override DoubleConstant ConvertToDouble ()
2445                 {
2446                         return Child.ConvertToDouble ();
2447                 }
2448
2449                 public override FloatConstant ConvertToFloat ()
2450                 {
2451                         return Child.ConvertToFloat ();
2452                 }
2453
2454                 public override ULongConstant ConvertToULong ()
2455                 {
2456                         return Child.ConvertToULong ();
2457                 }
2458
2459                 public override LongConstant ConvertToLong ()
2460                 {
2461                         return Child.ConvertToLong ();
2462                 }
2463
2464                 public override UIntConstant ConvertToUInt ()
2465                 {
2466                         return Child.ConvertToUInt ();
2467                 }
2468
2469                 public override IntConstant ConvertToInt ()
2470                 {
2471                         return Child.ConvertToInt ();
2472                 }
2473         }
2474
2475         /// <summary>
2476         ///   This kind of cast is used to encapsulate Value Types in objects.
2477         ///
2478         ///   The effect of it is to box the value type emitted by the previous
2479         ///   operation.
2480         /// </summary>
2481         public class BoxedCast : EmptyCast {
2482
2483                 public BoxedCast (Expression expr)
2484                         : base (expr, TypeManager.object_type)
2485                 {
2486                 }
2487
2488                 public override Expression DoResolve (EmitContext ec)
2489                 {
2490                         // This should never be invoked, we are born in fully
2491                         // initialized state.
2492
2493                         return this;
2494                 }
2495
2496                 public override void Emit (EmitContext ec)
2497                 {
2498                         base.Emit (ec);
2499                         
2500                         ec.ig.Emit (OpCodes.Box, child.Type);
2501                 }
2502         }
2503
2504         public class UnboxCast : EmptyCast {
2505                 public UnboxCast (Expression expr, Type return_type)
2506                         : base (expr, return_type)
2507                 {
2508                 }
2509
2510                 public override Expression DoResolve (EmitContext ec)
2511                 {
2512                         // This should never be invoked, we are born in fully
2513                         // initialized state.
2514
2515                         return this;
2516                 }
2517
2518                 public override void Emit (EmitContext ec)
2519                 {
2520                         Type t = type;
2521                         ILGenerator ig = ec.ig;
2522                         
2523                         base.Emit (ec);
2524                         ig.Emit (OpCodes.Unbox, t);
2525
2526                         LoadFromPtr (ig, t, false);
2527                 }
2528         }
2529         
2530         /// <summary>
2531         ///   This is used to perform explicit numeric conversions.
2532         ///
2533         ///   Explicit numeric conversions might trigger exceptions in a checked
2534         ///   context, so they should generate the conv.ovf opcodes instead of
2535         ///   conv opcodes.
2536         /// </summary>
2537         public class ConvCast : EmptyCast {
2538                 public enum Mode : byte {
2539                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
2540                         U1_I1, U1_CH,
2541                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
2542                         U2_I1, U2_U1, U2_I2, U2_CH,
2543                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
2544                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
2545                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
2546                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
2547                         CH_I1, CH_U1, CH_I2,
2548                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
2549                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
2550                 }
2551
2552                 Mode mode;
2553                 bool checked_state;
2554                 
2555                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
2556                         : base (child, return_type)
2557                 {
2558                         mode = m;
2559                         checked_state = ec.CheckState;
2560                 }
2561
2562                 public override Expression DoResolve (EmitContext ec)
2563                 {
2564                         // This should never be invoked, we are born in fully
2565                         // initialized state.
2566
2567                         return this;
2568                 }
2569
2570                 public override void Emit (EmitContext ec)
2571                 {
2572                         ILGenerator ig = ec.ig;
2573                         
2574                         base.Emit (ec);
2575
2576                         if (checked_state){
2577                                 switch (mode){
2578                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2579                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2580                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2581                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2582                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2583
2584                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2585                                 case Mode.U1_CH: /* nothing */ break;
2586
2587                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2588                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2589                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2590                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2591                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2592                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2593
2594                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2595                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2596                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2597                                 case Mode.U2_CH: /* nothing */ break;
2598
2599                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2600                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2601                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2602                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2603                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2604                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2605                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2606
2607                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2608                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2609                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2610                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2611                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2612                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2613
2614                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2615                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2616                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2617                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2618                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2619                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2620                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2621                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2622
2623                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2624                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2625                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2626                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2627                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2628                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2629                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2630                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2631
2632                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2633                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2634                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2635
2636                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2637                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2638                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2639                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2640                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2641                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2642                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2643                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2644                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2645
2646                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
2647                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
2648                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
2649                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2650                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
2651                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
2652                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
2653                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
2654                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
2655                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2656                                 }
2657                         } else {
2658                                 switch (mode){
2659                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
2660                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
2661                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
2662                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
2663                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
2664
2665                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
2666                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
2667
2668                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
2669                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
2670                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
2671                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
2672                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
2673                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
2674
2675                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
2676                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
2677                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
2678                                 case Mode.U2_CH: /* nothing */ break;
2679
2680                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
2681                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
2682                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
2683                                 case Mode.I4_U4: /* nothing */ break;
2684                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
2685                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
2686                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
2687
2688                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
2689                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
2690                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
2691                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
2692                                 case Mode.U4_I4: /* nothing */ break;
2693                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
2694
2695                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
2696                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
2697                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
2698                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
2699                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
2700                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
2701                                 case Mode.I8_U8: /* nothing */ break;
2702                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
2703
2704                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
2705                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
2706                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
2707                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
2708                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
2709                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
2710                                 case Mode.U8_I8: /* nothing */ break;
2711                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
2712
2713                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
2714                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
2715                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
2716
2717                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
2718                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
2719                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
2720                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
2721                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
2722                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
2723                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
2724                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
2725                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
2726
2727                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
2728                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
2729                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
2730                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
2731                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
2732                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
2733                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
2734                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
2735                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
2736                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
2737                                 }
2738                         }
2739                 }
2740         }
2741         
2742         public class OpcodeCast : EmptyCast {
2743                 OpCode op, op2;
2744                 bool second_valid;
2745                 
2746                 public OpcodeCast (Expression child, Type return_type, OpCode op)
2747                         : base (child, return_type)
2748                         
2749                 {
2750                         this.op = op;
2751                         second_valid = false;
2752                 }
2753
2754                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
2755                         : base (child, return_type)
2756                         
2757                 {
2758                         this.op = op;
2759                         this.op2 = op2;
2760                         second_valid = true;
2761                 }
2762
2763                 public override Expression DoResolve (EmitContext ec)
2764                 {
2765                         // This should never be invoked, we are born in fully
2766                         // initialized state.
2767
2768                         return this;
2769                 }
2770
2771                 public override void Emit (EmitContext ec)
2772                 {
2773                         base.Emit (ec);
2774                         ec.ig.Emit (op);
2775
2776                         if (second_valid)
2777                                 ec.ig.Emit (op2);
2778                 }                       
2779         }
2780
2781         /// <summary>
2782         ///   This kind of cast is used to encapsulate a child and cast it
2783         ///   to the class requested
2784         /// </summary>
2785         public class ClassCast : EmptyCast {
2786                 public ClassCast (Expression child, Type return_type)
2787                         : base (child, return_type)
2788                         
2789                 {
2790                 }
2791
2792                 public override Expression DoResolve (EmitContext ec)
2793                 {
2794                         // This should never be invoked, we are born in fully
2795                         // initialized state.
2796
2797                         return this;
2798                 }
2799
2800                 public override void Emit (EmitContext ec)
2801                 {
2802                         base.Emit (ec);
2803
2804                         ec.ig.Emit (OpCodes.Castclass, type);
2805                 }                       
2806                 
2807         }
2808         
2809         /// <summary>
2810         ///   SimpleName expressions are initially formed of a single
2811         ///   word and it only happens at the beginning of the expression.
2812         /// </summary>
2813         ///
2814         /// <remarks>
2815         ///   The expression will try to be bound to a Field, a Method
2816         ///   group or a Property.  If those fail we pass the name to our
2817         ///   caller and the SimpleName is compounded to perform a type
2818         ///   lookup.  The idea behind this process is that we want to avoid
2819         ///   creating a namespace map from the assemblies, as that requires
2820         ///   the GetExportedTypes function to be called and a hashtable to
2821         ///   be constructed which reduces startup time.  If later we find
2822         ///   that this is slower, we should create a `NamespaceExpr' expression
2823         ///   that fully participates in the resolution process. 
2824         ///   
2825         ///   For example `System.Console.WriteLine' is decomposed into
2826         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
2827         ///   
2828         ///   The first SimpleName wont produce a match on its own, so it will
2829         ///   be turned into:
2830         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
2831         ///   
2832         ///   System.Console will produce a TypeExpr match.
2833         ///   
2834         ///   The downside of this is that we might be hitting `LookupType' too many
2835         ///   times with this scheme.
2836         /// </remarks>
2837         public class SimpleName : Expression {
2838                 public readonly string Name;
2839                 public readonly Location Location;
2840                 
2841                 public SimpleName (string name, Location l)
2842                 {
2843                         Name = name;
2844                         Location = l;
2845                 }
2846
2847                 public static void Error120 (Location l, string name)
2848                 {
2849                         Report.Error (
2850                                 120, l,
2851                                 "An object reference is required " +
2852                                 "for the non-static field `"+name+"'");
2853                 }
2854                 
2855                 //
2856                 // Checks whether we are trying to access an instance
2857                 // property, method or field from a static body.
2858                 //
2859                 Expression MemberStaticCheck (Expression e)
2860                 {
2861                         if (e is FieldExpr){
2862                                 FieldInfo fi = ((FieldExpr) e).FieldInfo;
2863                                 
2864                                 if (!fi.IsStatic){
2865                                         Error120 (Location, Name);
2866                                         return null;
2867                                 }
2868                         } else if (e is MethodGroupExpr){
2869                                 MethodGroupExpr mg = (MethodGroupExpr) e;
2870
2871                                 if (!mg.RemoveInstanceMethods ()){
2872                                         Error120 (Location, mg.Methods [0].Name);
2873                                         return null;
2874                                 }
2875                                 return e;
2876                         } else if (e is PropertyExpr){
2877                                 if (!((PropertyExpr) e).IsStatic){
2878                                         Error120 (Location, Name);
2879                                         return null;
2880                                 }
2881                         } else if (e is EventExpr) {
2882                                 if (!((EventExpr) e).IsStatic) {
2883                                         Error120 (Location, Name);
2884                                         return null;
2885                                 }
2886                         }
2887
2888                         return e;
2889                 }
2890                 
2891                 public override Expression DoResolve (EmitContext ec)
2892                 {
2893                         return SimpleNameResolve (ec, false);
2894                 }
2895
2896                 public Expression DoResolveAllowStatic (EmitContext ec)
2897                 {
2898                         return SimpleNameResolve (ec, true);
2899                 }
2900
2901                 /// <remarks>
2902                 ///   7.5.2: Simple Names. 
2903                 ///
2904                 ///   Local Variables and Parameters are handled at
2905                 ///   parse time, so they never occur as SimpleNames.
2906                 ///
2907                 ///   The `allow_static' flag is used by MemberAccess only
2908                 ///   and it is used to inform us that it is ok for us to 
2909                 ///   avoid the static check, because MemberAccess might end
2910                 ///   up resolving the Name as a Type name and the access as
2911                 ///   a static type access.
2912                 ///
2913                 ///   ie: Type Type; .... { Type.GetType (""); }
2914                 ///
2915                 ///   Type is both an instance variable and a Type;  Type.GetType
2916                 ///   is the static method not an instance method of type.
2917                 /// </remarks>
2918                 Expression SimpleNameResolve (EmitContext ec, bool allow_static)
2919                 {
2920                         Expression e = null;
2921
2922                         //
2923                         // Stage 1: Performed by the parser (binding to locals or parameters).
2924                         //
2925                         if (!ec.OnlyLookupTypes){
2926                                 Block current_block = ec.CurrentBlock;
2927                                 if (current_block != null && current_block.IsVariableDefined (Name)){
2928                                         LocalVariableReference var;
2929                                         
2930                                         var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
2931                                         
2932                                         return var.Resolve (ec);
2933                                 }
2934                         
2935                                 //
2936                                 // Stage 2: Lookup members 
2937                                 //
2938
2939                                 //
2940                                 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
2941                                 // Hence we have two different cases
2942                                 //
2943
2944                                 DeclSpace lookup_ds = ec.DeclSpace;
2945                                 do {
2946                                         if (lookup_ds.TypeBuilder == null)
2947                                                 break;
2948
2949                                         e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
2950                                         if (e != null)
2951                                                 break;
2952
2953                                         //
2954                                         // Classes/structs keep looking, enums break
2955                                         //
2956                                         if (lookup_ds is TypeContainer)
2957                                                 lookup_ds = ((TypeContainer) lookup_ds).Parent;
2958                                         else
2959                                                 break;
2960                                 } while (lookup_ds != null);
2961                                 
2962                                 if (e == null && ec.ContainerType != null)
2963                                         e = MemberLookup (ec, ec.ContainerType, Name, Location);
2964                         }
2965
2966                         // Continuation of stage 2
2967                         if (e == null){
2968                                 //
2969                                 // Stage 3: Lookup symbol in the various namespaces. 
2970                                 //
2971                                 DeclSpace ds = ec.DeclSpace;
2972                                 Type t;
2973                                 string alias_value;
2974
2975                                 if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
2976                                         return new TypeExpr (t);
2977                                 
2978                                 //
2979                                 // Stage 2 part b: Lookup up if we are an alias to a type
2980                                 // or a namespace.
2981                                 //
2982                                 // Since we are cheating: we only do the Alias lookup for
2983                                 // namespaces if the name does not include any dots in it
2984                                 //
2985                                 
2986                                 if (Name.IndexOf ('.') == -1 && (alias_value = ec.TypeContainer.LookupAlias (Name)) != null) {
2987                                 // System.Console.WriteLine (Name + " --> " + alias_value);
2988                                         if ((t = RootContext.LookupType (ds, alias_value, true, Location))
2989                                             != null)
2990                                                 return new TypeExpr (t);
2991                                         
2992                                 // we have alias value, but it isn't Type, so try if it's namespace
2993                                         return new SimpleName (alias_value, Location);
2994                                 }
2995                                 
2996                                 // No match, maybe our parent can compose us
2997                                 // into something meaningful.
2998                                 return this;
2999                         }
3000                         
3001                         //
3002                         // Stage 2 continues here. 
3003                         // 
3004                         if (e is TypeExpr)
3005                                 return e;
3006
3007                         if (ec.OnlyLookupTypes)
3008                                 return null;
3009                         
3010                         if (e is FieldExpr){
3011                                 FieldExpr fe = (FieldExpr) e;
3012                                 FieldInfo fi = fe.FieldInfo;
3013
3014                                 if (fi.FieldType.IsPointer && !ec.InUnsafe){
3015                                         UnsafeError (Location);
3016                                 }
3017                                 
3018                                 if (ec.IsStatic){
3019                                         if (!allow_static && !fi.IsStatic){
3020                                                 Error120 (Location, Name);
3021                                                 return null;
3022                                         }
3023                                 } else {
3024                                         // If we are not in static code and this
3025                                         // field is not static, set the instance to `this'.
3026
3027                                         if (!fi.IsStatic)
3028                                                 fe.InstanceExpression = ec.This;
3029                                 }
3030
3031                                 
3032                                 if (fi is FieldBuilder) {
3033                                         Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
3034                                         
3035                                         if (c != null) {
3036                                                 object o = c.LookupConstantValue (ec);
3037                                                 object real_value = ((Constant)c.Expr).GetValue ();
3038                                                 return Constantify (real_value, fi.FieldType);
3039                                         }
3040                                 }
3041
3042                                 if (fi.IsLiteral) {
3043                                         Type t = fi.FieldType;
3044                                         Type decl_type = fi.DeclaringType;
3045                                         object o;
3046
3047                                         if (fi is FieldBuilder)
3048                                                 o = TypeManager.GetValue ((FieldBuilder) fi);
3049                                         else
3050                                                 o = fi.GetValue (fi);
3051                                         
3052                                         if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
3053                                                 Expression enum_member = MemberLookup (
3054                                                         ec, decl_type, "value__", MemberTypes.Field,
3055                                                         AllBindingFlags, Location); 
3056
3057                                                 Enum en = TypeManager.LookupEnum (decl_type);
3058
3059                                                 Constant c;
3060                                                 if (en != null)
3061                                                         c = Constantify (o, en.UnderlyingType);
3062                                                 else 
3063                                                         c = Constantify (o, enum_member.Type);
3064                                                 
3065                                                 return new EnumConstant (c, decl_type);
3066                                         }
3067                                         
3068                                         Expression exp = Constantify (o, t);
3069                                 }
3070                                         
3071                                 return e;
3072                         }                               
3073
3074                         if (e is EventExpr) {
3075                                 //
3076                                 // If the event is local to this class, we transform ourselves into
3077                                 // a FieldExpr
3078                                 //
3079                                 EventExpr ee = (EventExpr) e;
3080
3081                                 Expression ml = MemberLookup (
3082                                         ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
3083                                         MemberTypes.Event, AllBindingFlags, Location);
3084
3085                                 if (ml != null) {
3086                                         MemberInfo mi = ec.TypeContainer.GetFieldFromEvent ((EventExpr) ml);
3087
3088                                         if (mi == null) {
3089                                                 //
3090                                                 // If this happens, then we have an event with its own
3091                                                 // accessors and private field etc so there's no need
3092                                                 // to transform ourselves : we should instead flag an error
3093                                                 //
3094                                                 Assign.error70 (ee.EventInfo, Location);
3095                                                 return null;
3096                                         }
3097
3098                                         ml = ExprClassFromMemberInfo (ec, mi, Location);
3099                                         
3100                                         if (ml == null) {
3101                                                 Report.Error (-200, Location, "Internal error!!");
3102                                                 return null;
3103                                         }
3104
3105                                         Expression instance_expr;
3106                                         
3107                                         FieldInfo fi = ((FieldExpr) ml).FieldInfo;
3108
3109                                         if (fi.IsStatic)
3110                                                 instance_expr = null;
3111                                         else
3112                                                 instance_expr = ec.This;
3113
3114                                         instance_expr = instance_expr.Resolve (ec);
3115
3116                                         if (instance_expr != null)
3117                                                 instance_expr = instance_expr.Resolve (ec);
3118                                         
3119                                         return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
3120                                 }
3121                         }
3122                                 
3123                         
3124                         if (ec.IsStatic){
3125                                 if (allow_static)
3126                                         return e;
3127
3128                                 return MemberStaticCheck (e);
3129                         } else
3130                                 return e;
3131                 }
3132
3133                 public override void Emit (EmitContext ec)
3134                 {
3135                         //
3136                         // If this is ever reached, then we failed to
3137                         // find the name as a namespace
3138                         //
3139
3140                         Error (103, Location, "The name `" + Name +
3141                                "' does not exist in the class `" +
3142                                ec.DeclSpace.Name + "'");
3143                 }
3144         }
3145         
3146         /// <summary>
3147         ///   Fully resolved expression that evaluates to a type
3148         /// </summary>
3149         public class TypeExpr : Expression {
3150                 public TypeExpr (Type t)
3151                 {
3152                         Type = t;
3153                         eclass = ExprClass.Type;
3154                 }
3155
3156                 override public Expression DoResolve (EmitContext ec)
3157                 {
3158                         return this;
3159                 }
3160
3161                 override public void Emit (EmitContext ec)
3162                 {
3163                         throw new Exception ("Implement me");
3164                 }
3165         }
3166
3167         /// <summary>
3168         ///   MethodGroup Expression.
3169         ///  
3170         ///   This is a fully resolved expression that evaluates to a type
3171         /// </summary>
3172         public class MethodGroupExpr : Expression {
3173                 public MethodBase [] Methods;
3174                 Location loc;
3175                 Expression instance_expression = null;
3176                 
3177                 public MethodGroupExpr (MemberInfo [] mi, Location l)
3178                 {
3179                         Methods = new MethodBase [mi.Length];
3180                         mi.CopyTo (Methods, 0);
3181                         eclass = ExprClass.MethodGroup;
3182                         type = TypeManager.object_type;
3183                         loc = l;
3184                 }
3185
3186                 public MethodGroupExpr (ArrayList list, Location l)
3187                 {
3188                         Methods = new MethodBase [list.Count];
3189
3190                         try {
3191                                 list.CopyTo (Methods, 0);
3192                         } catch {
3193                                 foreach (MemberInfo m in list){
3194                                         if (!(m is MethodBase)){
3195                                                 Console.WriteLine ("Name " + m.Name);
3196                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3197                                         }
3198                                 }
3199                                 throw;
3200                         }
3201                         loc = l;
3202                         eclass = ExprClass.MethodGroup;
3203                         type = TypeManager.object_type;
3204                 }
3205                 
3206                 //
3207                 // `A method group may have associated an instance expression' 
3208                 // 
3209                 public Expression InstanceExpression {
3210                         get {
3211                                 return instance_expression;
3212                         }
3213
3214                         set {
3215                                 instance_expression = value;
3216                         }
3217                 }
3218                 
3219                 override public Expression DoResolve (EmitContext ec)
3220                 {
3221                         return this;
3222                 }
3223
3224                 public void ReportUsageError ()
3225                 {
3226                         Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3227                                       Methods [0].Name + "()' is referenced without parentheses");
3228                 }
3229
3230                 override public void Emit (EmitContext ec)
3231                 {
3232                         ReportUsageError ();
3233                 }
3234
3235                 bool RemoveMethods (bool keep_static)
3236                 {
3237                         ArrayList smethods = new ArrayList ();
3238                         int top = Methods.Length;
3239                         int i;
3240                         
3241                         for (i = 0; i < top; i++){
3242                                 MethodBase mb = Methods [i];
3243
3244                                 if (mb.IsStatic == keep_static)
3245                                         smethods.Add (mb);
3246                         }
3247
3248                         if (smethods.Count == 0)
3249                                 return false;
3250
3251                         Methods = new MethodBase [smethods.Count];
3252                         smethods.CopyTo (Methods, 0);
3253
3254                         return true;
3255                 }
3256                 
3257                 /// <summary>
3258                 ///   Removes any instance methods from the MethodGroup, returns
3259                 ///   false if the resulting set is empty.
3260                 /// </summary>
3261                 public bool RemoveInstanceMethods ()
3262                 {
3263                         return RemoveMethods (true);
3264                 }
3265
3266                 /// <summary>
3267                 ///   Removes any static methods from the MethodGroup, returns
3268                 ///   false if the resulting set is empty.
3269                 /// </summary>
3270                 public bool RemoveStaticMethods ()
3271                 {
3272                         return RemoveMethods (false);
3273                 }
3274         }
3275
3276         /// <summary>
3277         ///   Fully resolved expression that evaluates to a Field
3278         /// </summary>
3279         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
3280                 public readonly FieldInfo FieldInfo;
3281                 public Expression InstanceExpression;
3282                 Location loc;
3283                 
3284                 public FieldExpr (FieldInfo fi, Location l)
3285                 {
3286                         FieldInfo = fi;
3287                         eclass = ExprClass.Variable;
3288                         type = fi.FieldType;
3289                         loc = l;
3290                 }
3291
3292                 override public Expression DoResolve (EmitContext ec)
3293                 {
3294                         if (!FieldInfo.IsStatic){
3295                                 if (InstanceExpression == null){
3296                                         throw new Exception ("non-static FieldExpr without instance var\n" +
3297                                                              "You have to assign the Instance variable\n" +
3298                                                              "Of the FieldExpr to set this\n");
3299                                 }
3300
3301                                 InstanceExpression = InstanceExpression.Resolve (ec);
3302                                 if (InstanceExpression == null)
3303                                         return null;
3304                         }
3305
3306                         return this;
3307                 }
3308
3309                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3310                 {
3311                         Expression e = DoResolve (ec);
3312
3313                         if (e == null)
3314                                 return null;
3315                         
3316                         if (!FieldInfo.IsInitOnly)
3317                                 return this;
3318
3319                         //
3320                         // InitOnly fields can only be assigned in constructors
3321                         //
3322
3323                         if (ec.IsConstructor)
3324                                 return this;
3325
3326                         Report.Error (191, loc,
3327                                       "Readonly field can not be assigned outside " +
3328                                       "of constructor or variable initializer");
3329                         
3330                         return null;
3331                 }
3332
3333                 override public void Emit (EmitContext ec)
3334                 {
3335                         ILGenerator ig = ec.ig;
3336                         bool is_volatile = false;
3337                                 
3338                         if (FieldInfo is FieldBuilder){
3339                                 Field f = TypeManager.GetField (FieldInfo);
3340                                 if (f != null){
3341                                         if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3342                                                 is_volatile = true;
3343                                 
3344                                         f.status |= Field.Status.USED;
3345                                 }
3346                         }
3347                         
3348                         if (FieldInfo.IsStatic){
3349                                 if (is_volatile)
3350                                         ig.Emit (OpCodes.Volatile);
3351                                 
3352                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3353                         } else {
3354                                 if (InstanceExpression.Type.IsValueType){
3355                                         IMemoryLocation ml;
3356                                         LocalTemporary tempo = null;
3357                                         
3358                                         if (!(InstanceExpression is IMemoryLocation)){
3359                                                 tempo = new LocalTemporary (
3360                                                         ec, InstanceExpression.Type);
3361
3362                                                 InstanceExpression.Emit (ec);
3363                                                 tempo.Store (ec);
3364                                                 ml = tempo;
3365                                         } else
3366                                                 ml = (IMemoryLocation) InstanceExpression;
3367
3368                                         ml.AddressOf (ec, AddressOp.Load);
3369                                 } else 
3370                                         InstanceExpression.Emit (ec);
3371
3372                                 if (is_volatile)
3373                                         ig.Emit (OpCodes.Volatile);
3374                                 
3375                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
3376                         }
3377                 }
3378
3379                 public void EmitAssign (EmitContext ec, Expression source)
3380                 {
3381                         bool is_static = FieldInfo.IsStatic;
3382                         ILGenerator ig = ec.ig;
3383                         
3384                         if (!is_static){
3385                                 Expression instance = InstanceExpression;
3386
3387                                 if (instance.Type.IsValueType){
3388                                         if (instance is IMemoryLocation){
3389                                                 IMemoryLocation ml = (IMemoryLocation) instance;
3390
3391                                                 ml.AddressOf (ec, AddressOp.Store);
3392                                         } else
3393                                                 throw new Exception ("The " + instance + " of type " +
3394                                                                      instance.Type +
3395                                                                      " represents a ValueType and does " +
3396                                                                      "not implement IMemoryLocation");
3397                                 } else
3398                                         instance.Emit (ec);
3399                         }
3400                         source.Emit (ec);
3401
3402                         if (FieldInfo is FieldBuilder){
3403                                 Field f = TypeManager.GetField (FieldInfo);
3404                                 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3405                                         ig.Emit (OpCodes.Volatile);
3406                         }
3407                         
3408                         if (is_static)
3409                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
3410                         else 
3411                                 ig.Emit (OpCodes.Stfld, FieldInfo);
3412
3413                         if (FieldInfo is FieldBuilder){
3414                                 Field f = TypeManager.GetField (FieldInfo);
3415
3416                                 f.status |= Field.Status.ASSIGNED;
3417                         }
3418                 }
3419                 
3420                 public void AddressOf (EmitContext ec, AddressOp mode)
3421                 {
3422                         ILGenerator ig = ec.ig;
3423                         
3424                         if (FieldInfo is FieldBuilder){
3425                                 Field f = TypeManager.GetField (FieldInfo);
3426                                 if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
3427                                         ig.Emit (OpCodes.Volatile);
3428                         }
3429
3430                         if (FieldInfo is FieldBuilder){
3431                                 Field f = TypeManager.GetField (FieldInfo);
3432
3433                                 if ((mode & AddressOp.Store) != 0)
3434                                         f.status |= Field.Status.ASSIGNED;
3435                                 if ((mode & AddressOp.Load) != 0)
3436                                         f.status |= Field.Status.USED;
3437                         }
3438
3439                         //
3440                         // Handle initonly fields specially: make a copy and then
3441                         // get the address of the copy.
3442                         //
3443                         if (FieldInfo.IsInitOnly){
3444                                 LocalBuilder local;
3445                                 
3446                                 Emit (ec);
3447                                 local = ig.DeclareLocal (type);
3448                                 ig.Emit (OpCodes.Stloc, local);
3449                                 ig.Emit (OpCodes.Ldloca, local);
3450                                 return;
3451                         } 
3452
3453                         if (FieldInfo.IsStatic)
3454                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3455                         else {
3456                                 InstanceExpression.Emit (ec);
3457                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
3458                         }
3459                 }
3460         }
3461         
3462         /// <summary>
3463         ///   Expression that evaluates to a Property.  The Assign class
3464         ///   might set the `Value' expression if we are in an assignment.
3465         ///
3466         ///   This is not an LValue because we need to re-write the expression, we
3467         ///   can not take data from the stack and store it.  
3468         /// </summary>
3469         public class PropertyExpr : ExpressionStatement, IAssignMethod {
3470                 public readonly PropertyInfo PropertyInfo;
3471                 public readonly bool IsStatic;
3472                 public bool IsBase;
3473                 MethodInfo [] Accessors;
3474                 Location loc;
3475                 
3476                 Expression instance_expr;
3477                 
3478                 public PropertyExpr (PropertyInfo pi, Location l)
3479                 {
3480                         PropertyInfo = pi;
3481                         eclass = ExprClass.PropertyAccess;
3482                         IsStatic = false;
3483                         loc = l;
3484                         Accessors = TypeManager.GetAccessors (pi);
3485
3486                         if (Accessors != null)
3487                                 for (int i = 0; i < Accessors.Length; i++){
3488                                         if (Accessors [i] != null)
3489                                                 if (Accessors [i].IsStatic)
3490                                                         IsStatic = true;
3491                                 }
3492                         else
3493                                 Accessors = new MethodInfo [2];
3494                         
3495                         type = pi.PropertyType;
3496                 }
3497
3498                 //
3499                 // The instance expression associated with this expression
3500                 //
3501                 public Expression InstanceExpression {
3502                         set {
3503                                 instance_expr = value;
3504                         }
3505
3506                         get {
3507                                 return instance_expr;
3508                         }
3509                 }
3510
3511                 public bool VerifyAssignable ()
3512                 {
3513                         if (!PropertyInfo.CanWrite){
3514                                 Report.Error (200, loc, 
3515                                               "The property `" + PropertyInfo.Name +
3516                                               "' can not be assigned to, as it has not set accessor");
3517                                 return false;
3518                         }
3519
3520                         return true;
3521                 }
3522
3523                 override public Expression DoResolve (EmitContext ec)
3524                 {
3525                         if (!PropertyInfo.CanRead){
3526                                 Report.Error (154, loc, 
3527                                               "The property `" + PropertyInfo.Name +
3528                                               "' can not be used in " +
3529                                               "this context because it lacks a get accessor");
3530                                 return null;
3531                         }
3532
3533                         type = PropertyInfo.PropertyType;
3534
3535                         return this;
3536                 }
3537
3538                 override public void Emit (EmitContext ec)
3539                 {
3540                         MethodInfo method = Accessors [0];
3541
3542                         //
3543                         // Special case: length of single dimension array is turned into ldlen
3544                         //
3545                         if (method == TypeManager.int_array_get_length){
3546                                 Type iet = instance_expr.Type;
3547
3548                                 if (iet.GetArrayRank () == 1){
3549                                         instance_expr.Emit (ec);
3550                                         ec.ig.Emit (OpCodes.Ldlen);
3551                                         return;
3552                                 }
3553                         }
3554
3555                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
3556                         
3557                 }
3558
3559                 //
3560                 // Implements the IAssignMethod interface for assignments
3561                 //
3562                 public void EmitAssign (EmitContext ec, Expression source)
3563                 {
3564                         Argument arg = new Argument (source, Argument.AType.Expression);
3565                         ArrayList args = new ArrayList ();
3566
3567                         args.Add (arg);
3568                         Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args);
3569                 }
3570
3571                 override public void EmitStatement (EmitContext ec)
3572                 {
3573                         Emit (ec);
3574                         ec.ig.Emit (OpCodes.Pop);
3575                 }
3576         }
3577
3578         /// <summary>
3579         ///   Fully resolved expression that evaluates to an Event
3580         /// </summary>
3581         public class EventExpr : Expression {
3582                 public readonly EventInfo EventInfo;
3583                 Location loc;
3584                 public Expression InstanceExpression;
3585
3586                 public readonly bool IsStatic;
3587
3588                 MethodInfo add_accessor, remove_accessor;
3589                 
3590                 public EventExpr (EventInfo ei, Location loc)
3591                 {
3592                         EventInfo = ei;
3593                         this.loc = loc;
3594                         eclass = ExprClass.EventAccess;
3595
3596                         add_accessor = TypeManager.GetAddMethod (ei);
3597                         remove_accessor = TypeManager.GetRemoveMethod (ei);
3598                         
3599                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
3600                                         IsStatic = true;
3601
3602                         if (EventInfo is MyEventBuilder)
3603                                 type = ((MyEventBuilder) EventInfo).EventType;
3604                         else
3605                                 type = EventInfo.EventHandlerType;
3606                 }
3607
3608                 override public Expression DoResolve (EmitContext ec)
3609                 {
3610                         // We are born fully resolved
3611                         return this;
3612                 }
3613
3614                 override public void Emit (EmitContext ec)
3615                 {
3616                         throw new Exception ("Should not happen I think");
3617                 }
3618
3619                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3620                 {
3621                         Expression handler = ((Binary) source).Right;
3622                         
3623                         Argument arg = new Argument (handler, Argument.AType.Expression);
3624                         ArrayList args = new ArrayList ();
3625                                 
3626                         args.Add (arg);
3627                         
3628                         if (((Binary) source).Oper == Binary.Operator.Addition)
3629                                 Invocation.EmitCall (
3630                                         ec, false, IsStatic, InstanceExpression, add_accessor, args);
3631                         else
3632                                 Invocation.EmitCall (
3633                                         ec, false, IsStatic, InstanceExpression, remove_accessor, args);
3634                 }
3635         }
3636 }