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