ecore.cs : Added support for Implicit numeric conversions
[mono.git] / mcs / mbas / 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.MonoBASIC {
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         /// <remarks>
40         ///   This is used to tell Resolve in which types of expressions we're
41         ///   interested.
42         /// </remarks>
43         [Flags]
44         public enum ResolveFlags {
45                 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
46                 VariableOrValue         = 1,
47
48                 // Returns a type expression.
49                 Type                    = 2,
50
51                 // Returns a method group.
52                 MethodGroup             = 4,
53
54                 // Allows SimpleNames to be returned.
55                 // This is used by MemberAccess to construct long names that can not be
56                 // partially resolved (namespace-qualified names for example).
57                 SimpleName              = 8,
58
59                 // Mask of all the expression class flags.
60                 MaskExprClass           = 15,
61
62                 // Disable control flow analysis while resolving the expression.
63                 // This is used when resolving the instance expression of a field expression.
64                 DisableFlowAnalysis     = 16
65         }
66
67         //
68         // This is just as a hint to AddressOf of what will be done with the
69         // address.
70         [Flags]
71         public enum AddressOp {
72                 Store = 1,
73                 Load  = 2,
74                 LoadStore = 3
75         };
76         
77         /// <summary>
78         ///   This interface is implemented by variables
79         /// </summary>
80         public interface IMemoryLocation {
81                 /// <summary>
82                 ///   The AddressOf method should generate code that loads
83                 ///   the address of the object and leaves it on the stack.
84                 ///
85                 ///   The 'mode' argument is used to notify the expression
86                 ///   of whether this will be used to read from the address or
87                 ///   write to the address.
88                 ///
89                 ///   This is just a hint that can be used to provide good error
90                 ///   reporting, and should have no other side effects. 
91                 /// </summary>
92                 void AddressOf (EmitContext ec, AddressOp mode);
93         }
94
95         /// <summary>
96         ///   This interface is implemented by variables
97         /// </summary>
98         public interface IVariable {
99                 /// <summary>
100                 ///   Checks whether the variable has already been assigned at
101                 ///   the current position of the method's control flow and
102                 ///   reports an appropriate error message if not.
103                 ///
104                 ///   If the variable is a struct, then this call checks whether
105                 ///   all of its fields (including all private ones) have been
106                 ///   assigned.
107                 /// </summary>
108                 bool IsAssigned (EmitContext ec, Location loc);
109
110                 /// <summary>
111                 ///   Checks whether field 'name' in this struct has been assigned.
112                 /// </summary>
113                 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
114
115                 /// <summary>
116                 ///   Tells the flow analysis code that the variable has already
117                 ///   been assigned at the current code position.
118                 ///
119                 ///   If the variable is a struct, this call marks all its fields
120                 ///   (including private fields) as being assigned.
121                 /// </summary>
122                 void SetAssigned (EmitContext ec);
123
124                 /// <summary>
125                 ///   Tells the flow analysis code that field 'name' in this struct
126                 ///   has already been assigned atthe current code position.
127                 /// </summary>
128                 void SetFieldAssigned (EmitContext ec, string name);
129         }
130
131         /// <summary>
132         ///   This interface denotes an expression which evaluates to a member
133         ///   of a struct or a class.
134         /// </summary>
135         public interface IMemberExpr
136         {
137                 /// <summary>
138                 ///   The name of this member.
139                 /// </summary>
140                 string Name {
141                         get;
142                 }
143
144                 /// <summary>
145                 ///   Whether this is an instance member.
146                 /// </summary>
147                 bool IsInstance {
148                         get;
149                 }
150
151                 /// <summary>
152                 ///   Whether this is a static member.
153                 /// </summary>
154                 bool IsStatic {
155                         get;
156                 }
157
158                 /// <summary>
159                 ///   The type which declares this member.
160                 /// </summary>
161                 Type DeclaringType {
162                         get;
163                 }
164
165                 /// <summary>
166                 ///   The instance expression associated with this member, if it's a
167                 ///   non-static member.
168                 /// </summary>
169                 Expression InstanceExpression {
170                         get; set;
171                 }
172         }
173
174         /// <summary>
175         ///   Expression which resolves to a type.
176         /// </summary>
177         public interface ITypeExpression
178         {
179                 /// <summary>
180                 ///   Resolve the expression, but only lookup types.
181                 /// </summary>
182                 Expression DoResolveType (EmitContext ec);
183         }
184
185         /// <remarks>
186         ///   Base class for expressions
187         /// </remarks>
188         public abstract class Expression {
189                 public ExprClass eclass;
190                 protected Type type;
191                 protected Location loc;
192                 
193                 public Type Type {
194                         get {
195                                 return type;
196                         }
197
198                         set {
199                                 type = value;
200                         }
201                 }
202
203                 public Location Location {
204                         get {
205                                 return loc;
206                         }
207                 }
208
209                 /// <summary>
210                 ///   Utility wrapper routine for Error, just to beautify the code
211                 /// </summary>
212                 public void Error (int error, string s)
213                 {
214                         if (!Location.IsNull (loc))
215                                 Report.Error (error, loc, s);
216                         else
217                                 Report.Error (error, s);
218                 }
219
220                 /// <summary>
221                 ///   Utility wrapper routine for Warning, just to beautify the code
222                 /// </summary>
223                 public void Warning (int warning, string s)
224                 {
225                         if (!Location.IsNull (loc))
226                                 Report.Warning (warning, loc, s);
227                         else
228                                 Report.Warning (warning, s);
229                 }
230
231                 /// <summary>
232                 ///   Utility wrapper routine for Warning, only prints the warning if
233                 ///   warnings of level 'level' are enabled.
234                 /// </summary>
235                 public void Warning (int warning, int level, string s)
236                 {
237                         if (level <= RootContext.WarningLevel)
238                                 Warning (warning, s);
239                 }
240
241                 static public void Error_CannotConvertType (Location loc, Type source, Type target)
242                 {
243                         Report.Error (30, loc, "Cannot convert type '" +
244                                       TypeManager.MonoBASIC_Name (source) + "' to '" +
245                                       TypeManager.MonoBASIC_Name (target) + "'");
246                 }
247
248                 /// <summary>
249                 ///   Performs semantic analysis on the Expression
250                 /// </summary>
251                 ///
252                 /// <remarks>
253                 ///   The Resolve method is invoked to perform the semantic analysis
254                 ///   on the node.
255                 ///
256                 ///   The return value is an expression (it can be the
257                 ///   same expression in some cases) or a new
258                 ///   expression that better represents this node.
259                 ///   
260                 ///   For example, optimizations of Unary (LiteralInt)
261                 ///   would return a new LiteralInt with a negated
262                 ///   value.
263                 ///   
264                 ///   If there is an error during semantic analysis,
265                 ///   then an error should be reported (using Report)
266                 ///   and a null value should be returned.
267                 ///   
268                 ///   There are two side effects expected from calling
269                 ///   Resolve(): the the field variable "eclass" should
270                 ///   be set to any value of the enumeration
271                 ///   'ExprClass' and the type variable should be set
272                 ///   to a valid type (this is the type of the
273                 ///   expression).
274                 /// </remarks>
275                 public abstract Expression DoResolve (EmitContext ec);
276
277                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
278                 {
279                         return DoResolve (ec);
280                 }
281                 
282                 /// <summary>
283                 ///   Resolves an expression and performs semantic analysis on it.
284                 /// </summary>
285                 ///
286                 /// <remarks>
287                 ///   Currently Resolve wraps DoResolve to perform sanity
288                 ///   checking and assertion checking on what we expect from Resolve.
289                 /// </remarks>
290                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
291                 {
292                         // Are we doing a types-only search ?
293                         if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294                                 ITypeExpression type_expr = this as ITypeExpression;
295
296                                 if (type_expr == null)
297                                         return null;
298
299                                 return type_expr.DoResolveType (ec);
300                         }
301
302                         bool old_do_flow_analysis = ec.DoFlowAnalysis;
303                         if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304                                 ec.DoFlowAnalysis = false;
305                         
306                         Expression e;
307                         try {
308                                 if (this is SimpleName)
309                                         e = ((SimpleName) this).DoResolveAllowStatic (ec);
310                                 else 
311                                         e = DoResolve (ec);
312                         } finally {
313                                 ec.DoFlowAnalysis = old_do_flow_analysis;
314                         }
315
316                         if (e == null)
317                                 return null;
318
319                         if (e is SimpleName){
320                                 SimpleName s = (SimpleName) e;
321
322                                 if ((flags & ResolveFlags.SimpleName) == 0) {
323
324                                         object lookup = TypeManager.MemberLookup (
325                                                 ec.ContainerType, ec.ContainerType, AllMemberTypes,
326                                                 AllBindingFlags | BindingFlags.NonPublic, s.Name);
327                                         if (lookup != null)
328                                                 Error (30390, "'" + s.Name + "' " +
329                                                        "is inaccessible because of its protection level");
330                                         else
331                                                 Error (30451, "The name '" + s.Name + "' could not be " +
332                                                        "found in '" + ec.DeclSpace.Name + "'");
333                                         return null;
334                                 }
335
336                                 return s;
337                         }
338                         
339                         if ((e is TypeExpr) || (e is ComposedCast)) {
340                                 if ((flags & ResolveFlags.Type) == 0) {
341                                         e.Error118 (flags);
342                                         return null;
343                                 }
344
345                                 return e;
346                         }
347
348                         switch (e.eclass) {
349                         case ExprClass.Type:
350                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
351                                         e.Error118 (flags);
352                                         return null;
353                                 }
354                                 break;
355
356                         case ExprClass.MethodGroup:
357                                 if ((flags & ResolveFlags.MethodGroup) == 0) {
358                                         MethodGroupExpr mg = (MethodGroupExpr) e;
359                                         Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
360                                         Expression te = i.Resolve(ec);
361                                         //((MethodGroupExpr) e).ReportUsageError ();
362                                         //return null;
363                                         return te;
364                                 }
365                                 break;
366
367                         case ExprClass.Value:
368                         case ExprClass.Variable:
369                         case ExprClass.PropertyAccess:
370                         case ExprClass.EventAccess:
371                         case ExprClass.IndexerAccess:
372                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
373                                         e.Error118 (flags);
374                                         return null;
375                                 }
376                                 break;
377
378                         default:
379                                 throw new Exception ("Expression " + e.GetType () +
380                                                      " ExprClass is Invalid after resolve");
381                         }
382
383                         if (e.type == null)
384                                 throw new Exception (
385                                         "Expression " + e.GetType () +
386                                         " did not set its type after Resolve\n" +
387                                         "called from: " + this.GetType ());
388
389                         return e;
390                 }
391
392                 /// <summary>
393                 ///   Resolves an expression and performs semantic analysis on it.
394                 /// </summary>
395                 public Expression Resolve (EmitContext ec)
396                 {
397                         return Resolve (ec, ResolveFlags.VariableOrValue);
398                 }
399
400                 /// <summary>
401                 ///   Resolves an expression for LValue assignment
402                 /// </summary>
403                 ///
404                 /// <remarks>
405                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
406                 ///   checking and assertion checking on what we expect from Resolve
407                 /// </remarks>
408                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
409                 {
410                         Expression e = DoResolveLValue (ec, right_side);
411
412                         if (e != null){
413                                 if (e is SimpleName){
414                                         SimpleName s = (SimpleName) e;
415
416                                         Report.Error (
417                                                 30451, loc,
418                                                 "The name '" + s.Name + "' could not be found in '" +
419                                                 ec.DeclSpace.Name + "'");
420                                         return null;
421                                 }
422
423                                 if (e.eclass == ExprClass.Invalid)
424                                         throw new Exception ("Expression " + e +
425                                                              " ExprClass is Invalid after resolve");
426
427                                 if (e.eclass == ExprClass.MethodGroup) {
428                                         MethodGroupExpr mg = (MethodGroupExpr) e;
429                                         Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
430                                         Expression te = i.Resolve(ec);
431                                         return te;
432                                         //((MethodGroupExpr) e).ReportUsageError ();
433                                         //return null;
434                                 }
435
436                                 if (e.type == null)
437                                         throw new Exception ("Expression " + e +
438                                                              " did not set its type after Resolve");
439                         }
440
441                         return e;
442                 }
443                 
444                 /// <summary>
445                 ///   Emits the code for the expression
446                 /// </summary>
447                 ///
448                 /// <remarks>
449                 ///   The Emit method is invoked to generate the code
450                 ///   for the expression.  
451                 /// </remarks>
452                 public abstract void Emit (EmitContext ec);
453
454                 /// <summary>
455                 ///   Protected constructor.  Only derivate types should
456                 ///   be able to be created
457                 /// </summary>
458
459                 protected Expression ()
460                 {
461                         eclass = ExprClass.Invalid;
462                         type = null;
463                 }
464
465                 /// <summary>
466                 ///   Returns a literalized version of a literal FieldInfo
467                 /// </summary>
468                 ///
469                 /// <remarks>
470                 ///   The possible return values are:
471                 ///      IntConstant, UIntConstant
472                 ///      LongLiteral, ULongConstant
473                 ///      FloatConstant, DoubleConstant
474                 ///      StringConstant
475                 ///
476                 ///   The value returned is already resolved.
477                 /// </remarks>
478                 public static Constant Constantify (object v, Type t)
479                 {
480                         if (t == TypeManager.int32_type)
481                                 return new IntConstant ((int) v);
482                         else if (t == TypeManager.uint32_type)
483                                 return new UIntConstant ((uint) v);
484                         else if (t == TypeManager.int64_type)
485                                 return new LongConstant ((long) v);
486                         else if (t == TypeManager.uint64_type)
487                                 return new ULongConstant ((ulong) v);
488                         else if (t == TypeManager.float_type)
489                                 return new FloatConstant ((float) v);
490                         else if (t == TypeManager.double_type)
491                                 return new DoubleConstant ((double) v);
492                         else if (t == TypeManager.string_type)
493                                 return new StringConstant ((string) v);
494                         else if (t == TypeManager.short_type)
495                                 return new ShortConstant ((short)v);
496                         else if (t == TypeManager.ushort_type)
497                                 return new UShortConstant ((ushort)v);
498                         else if (t == TypeManager.sbyte_type)
499                                 return new SByteConstant (((sbyte)v));
500                         else if (t == TypeManager.byte_type)
501                                 return new ByteConstant ((byte)v);
502                         else if (t == TypeManager.char_type)
503                                 return new CharConstant ((char)v);
504                         else if (t == TypeManager.bool_type)
505                                 return new BoolConstant ((bool) v);
506                         else if (TypeManager.IsEnumType (t)){
507                                 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
508
509                                 return new EnumConstant (e, t);
510                         } else
511                                 throw new Exception ("Unknown type for constant (" + t +
512                                                      "), details: " + v);
513                 }
514
515                 /// <summary>
516                 ///   Returns a fully formed expression after a MemberLookup
517                 /// </summary>
518                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
519                 {
520                         if (mi is EventInfo)
521                                 return new EventExpr ((EventInfo) mi, loc);
522                         else if (mi is FieldInfo)
523                                 return new FieldExpr ((FieldInfo) mi, loc);
524                         else if (mi is PropertyInfo)
525                                 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
526                         else if (mi is Type){
527                                 return new TypeExpr ((System.Type) mi, loc);
528                         }
529
530                         return null;
531                 }
532
533                 //
534                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
535                 //
536                 // This code could use some optimizations, but we need to do some
537                 // measurements.  For example, we could use a delegate to 'flag' when
538                 // something can not any longer be a method-group (because it is something
539                 // else).
540                 //
541                 // Return values:
542                 //     If the return value is an Array, then it is an array of
543                 //     MethodBases
544                 //   
545                 //     If the return value is an MemberInfo, it is anything, but a Method
546                 //
547                 //     null on error.
548                 //
549                 // FIXME: When calling MemberLookup inside an 'Invocation', we should pass
550                 // the arguments here and have MemberLookup return only the methods that
551                 // match the argument count/type, unlike we are doing now (we delay this
552                 // decision).
553                 //
554                 // This is so we can catch correctly attempts to invoke instance methods
555                 // from a static body (scan for error 120 in ResolveSimpleName).
556                 //
557                 //
558                 // FIXME: Potential optimization, have a static ArrayList
559                 //
560
561                 public static Expression MemberLookup (EmitContext ec, Type t, string name,
562                                                        MemberTypes mt, BindingFlags bf, Location loc)
563                 {
564                         return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
565                 }
566
567                 //
568                 // Lookup type 't' for code in class 'invocation_type'.  Note that it's important
569                 // to set 'invocation_type' correctly since this method also checks whether the
570                 // invoking class is allowed to access the member in class 't'.  When you want to
571                 // explicitly do a lookup in the base class, you must set both 't' and 'invocation_type'
572                 // to the base class (although a derived class can access protected members of its base
573                 // class it cannot do so through an instance of the base class (error CS1540)).
574                 // 
575
576                 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
577                                                        string name, MemberTypes mt, BindingFlags bf,
578                                                        Location loc)
579                 {
580                         MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
581
582                         if (mi == null)
583                                 return null;
584
585                         int count = mi.Length;
586
587                         if (count > 1)
588                                 return new MethodGroupExpr (mi, loc);
589
590                         if (mi [0] is MethodBase)
591                                 return new MethodGroupExpr (mi, loc);
592
593                         return ExprClassFromMemberInfo (ec, mi [0], loc);
594                 }
595
596                 public const MemberTypes AllMemberTypes =
597                         MemberTypes.Constructor |
598                         MemberTypes.Event       |
599                         MemberTypes.Field       |
600                         MemberTypes.Method      |
601                         MemberTypes.NestedType  |
602                         MemberTypes.Property;
603                 
604                 public const BindingFlags AllBindingFlags =
605                         BindingFlags.Public |
606                         BindingFlags.Static |
607                         BindingFlags.Instance |
608                         BindingFlags.IgnoreCase;
609
610                 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
611                 {
612                         return MemberLookup (ec, ec.ContainerType, t, name, AllMemberTypes, AllBindingFlags, loc);
613                 }
614
615                 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
616                 {
617                         return MemberLookup (ec, ec.ContainerType, t, name,
618                                              MemberTypes.Method, AllBindingFlags, loc);
619                 }
620
621                 /// <summary>
622                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
623                 ///   to find a final definition.  If the final definition is not found, we
624                 ///   look for private members and display a useful debugging message if we
625                 ///   find it.
626                 /// </summary>
627                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name, 
628                                                             Location loc)
629                 {
630                         return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
631                 }
632
633                 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
634                                                             MemberTypes mt, BindingFlags bf, Location loc)
635                 {
636                         Expression e;
637
638                         int errors = Report.Errors;
639
640                         e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
641
642                         if (e != null)
643                                 return e;
644
645                         // Error has already been reported.
646                         if (errors < Report.Errors)
647                                 return null;
648                         
649                         e = MemberLookup (ec, t, name, AllMemberTypes,
650                                           AllBindingFlags | BindingFlags.NonPublic, loc);
651                         if (e == null){
652                                 Report.Error (
653                                         30456, loc, "'" + t + "' does not contain a definition " +
654                                         "for '" + name + "'");
655                         } else {
656                                         Report.Error (
657                                                 30390, loc, "'" + t + "." + name +
658                                                 "' is inaccessible due to its protection level");
659                         }
660                         
661                         return null;
662                 }
663
664                 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
665                 {
666                         EventInfo ei = event_expr.EventInfo;
667
668                         return TypeManager.GetPrivateFieldOfEvent (ei);
669                 }
670                 
671                 static EmptyExpression MyEmptyExpr;
672                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
673                 {
674                         Type expr_type = expr.Type;
675
676                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
677                                 // if we are a method group, emit a warning
678
679                                 expr.Emit (null);
680                         }
681
682                         //
683                         // notice that it is possible to write "ValueType v = 1", the ValueType here
684                         // is an abstract class, and not really a value type, so we apply the same rules.
685                         //
686                         if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
687                                 //
688                                 // A pointer type cannot be converted to object
689                                 // 
690                                 if (expr_type.IsPointer)
691                                         return null;
692
693                                 if (expr_type.IsValueType)
694                                         return new BoxedCast (expr);
695                                 if (expr_type.IsClass || expr_type.IsInterface)
696                                         return new EmptyCast (expr, target_type);
697                         } else if (expr_type.IsSubclassOf (target_type)) {
698                                 //
699                                 // Special case: enumeration to System.Enum.
700                                 // System.Enum is not a value type, it is a class, so we need
701                                 // a boxing conversion
702                                 //
703                                 if (expr_type.IsEnum)
704                                         return new BoxedCast (expr);
705                         
706                                 return new EmptyCast (expr, target_type);
707                         } else {
708
709                                 // This code is kind of mirrored inside StandardConversionExists
710                                 // with the small distinction that we only probe there
711                                 //
712                                 // Always ensure that the code here and there is in sync
713                                 
714                                 // from the null type to any reference-type.
715                                 if (expr is NullLiteral && !target_type.IsValueType)
716                                         return new EmptyCast (expr, target_type);
717
718                                 // from any class-type S to any interface-type T.
719                                 if (target_type.IsInterface) {
720                                         if (TypeManager.ImplementsInterface (expr_type, target_type)){
721                                                 if (expr_type.IsClass)
722                                                         return new EmptyCast (expr, target_type);
723                                                 else if (expr_type.IsValueType)
724                                                         return new BoxedCast (expr);
725                                         }
726                                 }
727
728                                 // from any interface type S to interface-type T.
729                                 if (expr_type.IsInterface && target_type.IsInterface) {
730                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
731                                                 return new EmptyCast (expr, target_type);
732                                         else
733                                                 return null;
734                                 }
735                                 
736                                 // from an array-type S to an array-type of type T
737                                 if (expr_type.IsArray && target_type.IsArray) {
738                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
739
740                                                 Type expr_element_type = expr_type.GetElementType ();
741
742                                                 if (MyEmptyExpr == null)
743                                                         MyEmptyExpr = new EmptyExpression ();
744                                                 
745                                                 MyEmptyExpr.SetType (expr_element_type);
746                                                 Type target_element_type = target_type.GetElementType ();
747
748                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
749                                                         if (StandardConversionExists (MyEmptyExpr,
750                                                                                       target_element_type))
751                                                                 return new EmptyCast (expr, target_type);
752                                         }
753                                 }
754                                 
755                                 
756                                 // from an array-type to System.Array
757                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
758                                         return new EmptyCast (expr, target_type);
759                                 
760                                 // from any delegate type to System.Delegate
761                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
762                                     target_type == TypeManager.delegate_type)
763                                         return new EmptyCast (expr, target_type);
764                                         
765                                 // from any array-type or delegate type into System.ICloneable.
766                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
767                                         if (target_type == TypeManager.icloneable_type)
768                                                 return new EmptyCast (expr, target_type);
769                                 
770                                 return null;
771
772                         }
773                         
774                         return null;
775                 }
776
777                 /// <summary>
778                 ///   Implicit Numeric Conversions.
779                 ///
780                 ///   expr is the expression to convert, returns a new expression of type
781                 ///   target_type or null if an implicit conversion is not possible.
782                 /// </summary>
783                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
784                                                                     Type target_type, Location loc)
785                 {
786                         Type expr_type = expr.Type;
787                         
788                         //
789                         // Attempt to do the implicit constant expression conversions
790
791                         if (expr is IntConstant){
792                                 Expression e;
793                                 
794                                 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
795
796                                 if (e != null)
797                                         return e;
798                         } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
799                                 //
800                                 // Try the implicit constant expression conversion
801                                 // from long to ulong, instead of a nice routine,
802                                 // we just inline it
803                                 //
804                                 long v = ((LongConstant) expr).Value;
805                                 if (v > 0)
806                                         return new ULongConstant ((ulong) v);
807                         }
808
809                         Type real_target_type = target_type;
810
811                         if (target_type == TypeManager.bool_type) {
812
813                                 if (expr_type == TypeManager.decimal_type) {
814                                         return RTConversionExpression (ec, "System.Convert", ".ToBoolean" , expr, loc);
815                                 }
816                                 return new NumericToBoolCast (expr, expr.Type);
817                         }
818
819                         if (expr_type == TypeManager.bool_type){
820                                 //
821                                 if (real_target_type == TypeManager.sbyte_type)
822                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
823                                 if (real_target_type == TypeManager.byte_type)
824                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
825                                 if (real_target_type == TypeManager.int32_type)
826                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
827                                 if (real_target_type == TypeManager.int64_type)
828                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
829                                 if (real_target_type == TypeManager.double_type)
830                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
831                                 if (real_target_type == TypeManager.float_type)
832                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
833                                 if (real_target_type == TypeManager.short_type)
834                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
835                                 if (real_target_type == TypeManager.decimal_type)
836                                         return RTConversionExpression(ec, "System.Convert", ".ToDecimal", expr, loc);
837                         } else if (expr_type == TypeManager.sbyte_type){
838                                 //
839                                 // From sbyte to short, int, long, float, double.
840                                 //
841                                 if (real_target_type == TypeManager.int32_type)
842                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
843                                 if (real_target_type == TypeManager.int64_type)
844                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
845                                 if (real_target_type == TypeManager.double_type)
846                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
847                                 if (real_target_type == TypeManager.float_type)
848                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
849                                 if (real_target_type == TypeManager.short_type)
850                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
851                         } else if (expr_type == TypeManager.byte_type){
852                                 //
853                                 // From byte to short, ushort, int, uint, long, ulong, float, double
854                                 // 
855                                 if ((real_target_type == TypeManager.short_type) ||
856                                     (real_target_type == TypeManager.ushort_type) ||
857                                     (real_target_type == TypeManager.int32_type) ||
858                                     (real_target_type == TypeManager.uint32_type))
859                                         return new EmptyCast (expr, target_type);
860
861                                 if (real_target_type == TypeManager.uint64_type)
862                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
863                                 if (real_target_type == TypeManager.int64_type)
864                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
865                                 if (real_target_type == TypeManager.float_type)
866                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
867                                 if (real_target_type == TypeManager.double_type)
868                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
869                         } else if (expr_type == TypeManager.short_type){
870                                 //
871                                 // From short to int, long, float, double
872                                 // 
873                                 if (real_target_type == TypeManager.int32_type)
874                                         return new EmptyCast (expr, target_type);
875                                 if (real_target_type == TypeManager.int64_type)
876                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
877                                 if (real_target_type == TypeManager.double_type)
878                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
879                                 if (real_target_type == TypeManager.float_type)
880                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
881                         } else if (expr_type == TypeManager.ushort_type){
882                                 //
883                                 // From ushort to int, uint, long, ulong, float, double
884                                 //
885                                 if (real_target_type == TypeManager.uint32_type)
886                                         return new EmptyCast (expr, target_type);
887
888                                 if (real_target_type == TypeManager.uint64_type)
889                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
890                                 if (real_target_type == TypeManager.int32_type)
891                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
892                                 if (real_target_type == TypeManager.int64_type)
893                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
894                                 if (real_target_type == TypeManager.double_type)
895                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
896                                 if (real_target_type == TypeManager.float_type)
897                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
898                         } else if (expr_type == TypeManager.int32_type){
899                                 //
900                                 // From int to long, float, double
901                                 //
902                                 if (real_target_type == TypeManager.int64_type)
903                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
904                                 if (real_target_type == TypeManager.double_type)
905                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
906                                 if (real_target_type == TypeManager.float_type)
907                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
908                         } else if (expr_type == TypeManager.uint32_type){
909                                 //
910                                 // From uint to long, ulong, float, double
911                                 //
912                                 if (real_target_type == TypeManager.int64_type)
913                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
914                                 if (real_target_type == TypeManager.uint64_type)
915                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
916                                 if (real_target_type == TypeManager.double_type)
917                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
918                                                                OpCodes.Conv_R8);
919                                 if (real_target_type == TypeManager.float_type)
920                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
921                                                                OpCodes.Conv_R4);
922                         } else if (expr_type == TypeManager.int64_type){
923                                 //
924                                 // From long/ulong to float, double
925                                 //
926                                 if (real_target_type == TypeManager.double_type)
927                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
928                                 if (real_target_type == TypeManager.float_type)
929                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
930                         } else if (expr_type == TypeManager.uint64_type){
931                                 //
932                                 // From ulong to float, double
933                                 //
934                                 if (real_target_type == TypeManager.double_type)
935                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
936                                                                OpCodes.Conv_R8);
937                                 if (real_target_type == TypeManager.float_type)
938                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
939                                                                OpCodes.Conv_R4);        
940                         } else if (expr_type == TypeManager.char_type){
941                                 //
942                                 // From char to ushort, int, uint, long, ulong, float, double
943                                 // 
944                                 if ((real_target_type == TypeManager.ushort_type) ||
945                                     (real_target_type == TypeManager.int32_type) ||
946                                     (real_target_type == TypeManager.uint32_type))
947                                         return new EmptyCast (expr, target_type);
948                                 if (real_target_type == TypeManager.uint64_type)
949                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
950                                 if (real_target_type == TypeManager.int64_type)
951                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
952                                 if (real_target_type == TypeManager.float_type)
953                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
954                                 if (real_target_type == TypeManager.double_type)
955                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
956                         } else if (expr_type == TypeManager.string_type){
957
958                                 if (real_target_type == TypeManager.bool_type)
959                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
960                                 if (real_target_type == TypeManager.decimal_type)
961                                         return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
962                         } else if (expr_type == TypeManager.float_type){
963                                 //
964                                 // float to double
965                                 //
966                                 if (real_target_type == TypeManager.decimal_type)
967                                         return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
968                                 if (real_target_type == TypeManager.double_type)
969                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
970
971                         } else if (expr_type == TypeManager.double_type){
972
973                                 if (real_target_type == TypeManager.decimal_type)
974                                         return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
975                         } else if (expr_type == TypeManager.decimal_type){
976
977                                 if (real_target_type == TypeManager.bool_type)
978                                         return RTConversionExpression (ec, "System.Convert", ".ToBoolean" , expr, loc);
979                                 if (real_target_type == TypeManager.short_type)
980                                         return RTConversionExpression (ec, "System.Convert", ".ToInt16" , expr, loc);
981                                 if (real_target_type == TypeManager.byte_type)
982                                         return RTConversionExpression (ec, "System.Convert", ".ToByte" , expr, loc);
983                                 if (real_target_type == TypeManager.int32_type)
984                                         return RTConversionExpression (ec, "System.Convert", ".ToInt32" , expr, loc);
985                                 if (real_target_type == TypeManager.int64_type)
986                                         return RTConversionExpression (ec, "System.Convert", ".ToInt64" , expr, loc);
987                                 if (real_target_type == TypeManager.float_type)
988                                         return RTConversionExpression (ec, "System.Convert", ".ToSingle" , expr, loc);
989                                 if (real_target_type == TypeManager.double_type)
990                                         return RTConversionExpression (ec, "System.Convert", ".ToDouble" , expr, loc);
991                                 if (real_target_type == TypeManager.char_type)
992                                         return RTConversionExpression (ec, "System.Convert", ".ToString" , expr, loc);
993                         }
994
995                         return null;
996                 }
997
998                 //
999                 // Tests whether an implicit reference conversion exists between expr_type
1000                 // and target_type
1001                 //
1002                 public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
1003                 {
1004                         //
1005                         // This is the boxed case.
1006                         //
1007                         if (target_type == TypeManager.object_type) {
1008                                 if ((expr_type.IsClass) ||
1009                                     (expr_type.IsValueType) ||
1010                                     (expr_type.IsInterface))
1011                                         return true;
1012                                 
1013                         } else if (expr_type.IsSubclassOf (target_type)) {
1014                                 return true;
1015                         } else {
1016                                 // Please remember that all code below actually comes
1017                                 // from ImplicitReferenceConversion so make sure code remains in sync
1018                                 
1019                                 // from any class-type S to any interface-type T.
1020                                 if (target_type.IsInterface) {
1021                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
1022                                                 return true;
1023                                 }
1024                                 
1025                                 // from any interface type S to interface-type T.
1026                                 if (expr_type.IsInterface && target_type.IsInterface)
1027                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
1028                                                 return true;
1029                                 
1030                                 // from an array-type S to an array-type of type T
1031                                 if (expr_type.IsArray && target_type.IsArray) {
1032                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1033                                                 
1034                                                 Type expr_element_type = expr_type.GetElementType ();
1035
1036                                                 if (MyEmptyExpr == null)
1037                                                         MyEmptyExpr = new EmptyExpression ();
1038                                                 
1039                                                 MyEmptyExpr.SetType (expr_element_type);
1040                                                 Type target_element_type = target_type.GetElementType ();
1041                                                 
1042                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1043                                                         if (StandardConversionExists (MyEmptyExpr,
1044                                                                                       target_element_type))
1045                                                                 return true;
1046                                         }
1047                                 }
1048                                 
1049                                 // from an array-type to System.Array
1050                                 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1051                                         return true;
1052                                 
1053                                 // from any delegate type to System.Delegate
1054                                 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1055                                     target_type == TypeManager.delegate_type)
1056                                         if (target_type.IsAssignableFrom (expr_type))
1057                                                 return true;
1058                                         
1059                                 // from any array-type or delegate type into System.ICloneable.
1060                                 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1061                                         if (target_type == TypeManager.icloneable_type)
1062                                                 return true;
1063                                 
1064                                 // from the null type to any reference-type.
1065                                 if (expr is NullLiteral && !target_type.IsValueType &&
1066                                     !TypeManager.IsEnumType (target_type))
1067                                         return true;
1068                                 
1069                         }
1070
1071                         return false;
1072                 }
1073
1074                 /// <summary>
1075                 ///  Same as StandardConversionExists except that it also looks at
1076                 ///  implicit user defined conversions - needed for overload resolution
1077                 /// </summary>
1078                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1079                 {
1080                         if (StandardConversionExists (expr, target_type) == true)
1081                                 return true;
1082
1083 #if false
1084                         Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1085
1086                         if (dummy != null)
1087                                 return true;
1088 #endif
1089
1090                         return false;
1091                 }
1092
1093                 /// <summary>
1094                 ///  Determines if a standard implicit conversion exists from
1095                 ///  expr_type to target_type
1096                 /// </summary>
1097                 public static bool StandardConversionExists (Expression expr, Type target_type)
1098                 {
1099                         return WideningConversionExists (expr, expr.type, target_type);
1100                 }
1101
1102                 public static bool WideningConversionExists (Type expr_type, Type target_type)
1103                 {
1104                         return WideningConversionExists (null, expr_type, target_type);
1105                 }
1106
1107                 public static bool WideningConversionExists (Expression expr, Type target_type)
1108                 {
1109                         return WideningConversionExists (expr, expr.Type, target_type);
1110                 }
1111
1112                 public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
1113                 {
1114
1115                         if (expr_type == null || expr_type == TypeManager.void_type)
1116                                 return false;
1117                         
1118                         if (expr_type == target_type)
1119                                 return true;
1120
1121                         // Conversions from enum to underlying type are widening.
1122                         if (expr_type.IsSubclassOf (TypeManager.enum_type))
1123                                 expr_type = TypeManager.EnumToUnderlying (expr_type);
1124
1125                         if (expr_type == target_type)
1126                                 return true;
1127
1128                         // First numeric conversions 
1129                         
1130                         if (expr_type == TypeManager.sbyte_type){
1131                                 //
1132                                 // From sbyte to short, int, long, float, double.
1133                                 //
1134                                 if ((target_type == TypeManager.int32_type) || 
1135                                     (target_type == TypeManager.int64_type) ||
1136                                     (target_type == TypeManager.double_type) ||
1137                                     (target_type == TypeManager.float_type)  ||
1138                                     (target_type == TypeManager.short_type) ||
1139                                     (target_type == TypeManager.decimal_type))
1140                                         return true;
1141                                 
1142                         } else if (expr_type == TypeManager.byte_type){
1143                                 //
1144                                 // From byte to short, ushort, int, uint, long, ulong, float, double
1145                                 // 
1146                                 if ((target_type == TypeManager.short_type) ||
1147                                     (target_type == TypeManager.bool_type) ||
1148                                     (target_type == TypeManager.ushort_type) ||
1149                                     (target_type == TypeManager.int32_type) ||
1150                                     (target_type == TypeManager.uint32_type) ||
1151                                     (target_type == TypeManager.uint64_type) ||
1152                                     (target_type == TypeManager.int64_type) ||
1153                                     (target_type == TypeManager.float_type) ||
1154                                     (target_type == TypeManager.double_type) ||
1155                                     (target_type == TypeManager.decimal_type))
1156                                         return true;
1157         
1158                         } else if (expr_type == TypeManager.short_type){
1159                                 //
1160                                 // From short to int, long, float, double
1161                                 // 
1162                                 if ((target_type == TypeManager.int32_type) ||
1163                                     (target_type == TypeManager.bool_type) ||
1164                                     (target_type == TypeManager.int64_type) ||
1165                                     (target_type == TypeManager.double_type) ||
1166                                     (target_type == TypeManager.float_type) ||
1167                                     (target_type == TypeManager.decimal_type))
1168                                         return true;
1169                                         
1170                         } else if (expr_type == TypeManager.ushort_type){
1171                                 //
1172                                 // From ushort to int, uint, long, ulong, float, double
1173                                 //
1174                                 if ((target_type == TypeManager.uint32_type) ||
1175                                     (target_type == TypeManager.uint64_type) ||
1176                                     (target_type == TypeManager.int32_type) ||
1177                                     (target_type == TypeManager.int64_type) ||
1178                                     (target_type == TypeManager.double_type) ||
1179                                     (target_type == TypeManager.float_type) ||
1180                                     (target_type == TypeManager.decimal_type))
1181                                         return true;
1182                                     
1183                         } else if (expr_type == TypeManager.int32_type){
1184                                 //
1185                                 // From int to long, float, double
1186                                 //
1187                                 if ((target_type == TypeManager.int64_type) ||
1188                                     (target_type == TypeManager.bool_type) ||
1189                                     (target_type == TypeManager.double_type) ||
1190                                     (target_type == TypeManager.float_type) ||
1191                                     (target_type == TypeManager.decimal_type))
1192                                         return true;
1193                                         
1194                         } else if (expr_type == TypeManager.uint32_type){
1195                                 //
1196                                 // From uint to long, ulong, float, double
1197                                 //
1198                                 if ((target_type == TypeManager.int64_type) ||
1199                                     (target_type == TypeManager.bool_type) ||
1200                                     (target_type == TypeManager.uint64_type) ||
1201                                     (target_type == TypeManager.double_type) ||
1202                                     (target_type == TypeManager.float_type) ||
1203                                     (target_type == TypeManager.decimal_type))
1204                                         return true;
1205                                         
1206                         } else if ((expr_type == TypeManager.uint64_type) ||
1207                                    (expr_type == TypeManager.int64_type)) {
1208                                 //
1209                                 // From long/ulong to float, double
1210                                 //
1211                                 if ((target_type == TypeManager.double_type) ||
1212                                     (target_type == TypeManager.bool_type) ||
1213                                     (target_type == TypeManager.float_type) ||
1214                                     (target_type == TypeManager.decimal_type))
1215                                         return true;
1216                                     
1217                         } else if (expr_type == TypeManager.char_type){
1218                                 //
1219                                 // From char to ushort, int, uint, long, ulong, float, double
1220                                 // 
1221                                 if ((target_type == TypeManager.ushort_type) ||
1222                                     (target_type == TypeManager.int32_type) ||
1223                                     (target_type == TypeManager.uint32_type) ||
1224                                     (target_type == TypeManager.uint64_type) ||
1225                                     (target_type == TypeManager.int64_type) ||
1226                                     (target_type == TypeManager.float_type) ||
1227                                     (target_type == TypeManager.double_type) ||
1228                                     (target_type == TypeManager.decimal_type))
1229                                         return true;
1230
1231                         } else if (expr_type == TypeManager.float_type){
1232                                 //
1233                                 // float to double, decimal
1234                                 //
1235                                 if (target_type == TypeManager.double_type)
1236                                         return true;
1237                         } else if (expr_type == TypeManager.double_type){
1238
1239                                 if ((target_type == TypeManager.bool_type))
1240                                         return true;
1241                         }               
1242                         
1243                         if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
1244                                 return true;
1245                         
1246                         if (expr is IntConstant){
1247                                 int value = ((IntConstant) expr).Value;
1248
1249                                 if (target_type == TypeManager.sbyte_type){
1250                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
1251                                                 return true;
1252                                 } else if (target_type == TypeManager.byte_type){
1253                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1254                                                 return true;
1255                                 } else if (target_type == TypeManager.short_type){
1256                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
1257                                                 return true;
1258                                 } else if (target_type == TypeManager.ushort_type){
1259                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1260                                                 return true;
1261                                 } else if (target_type == TypeManager.uint32_type){
1262                                         if (value >= 0)
1263                                                 return true;
1264                                 } else if (target_type == TypeManager.uint64_type){
1265                                          //
1266                                          // we can optimize this case: a positive int32
1267                                          // always fits on a uint64.  But we need an opcode
1268                                          // to do it.
1269                                          //
1270                                         if (value >= 0)
1271                                                 return true;
1272                                 }
1273                                 
1274                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1275                                         return true;
1276                         }
1277
1278                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
1279                                 //
1280                                 // Try the implicit constant expression conversion
1281                                 // from long to ulong, instead of a nice routine,
1282                                 // we just inline it
1283                                 //
1284                                 long v = ((LongConstant) expr).Value;
1285                                 if (v > 0)
1286                                         return true;
1287                         }
1288                         
1289                         if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1290                                 IntLiteral i = (IntLiteral) expr;
1291
1292                                 if (i.Value == 0)
1293                                         return true;
1294                         }
1295
1296                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1297                                 return true;
1298
1299                         return false;
1300                 }
1301
1302                 //
1303                 // Used internally by FindMostEncompassedType, this is used
1304                 // to avoid creating lots of objects in the tight loop inside
1305                 // FindMostEncompassedType
1306                 //
1307                 static EmptyExpression priv_fmet_param;
1308                 
1309                 /// <summary>
1310                 ///  Finds "most encompassed type" according to the spec (13.4.2)
1311                 ///  amongst the methods in the MethodGroupExpr
1312                 /// </summary>
1313                 static Type FindMostEncompassedType (ArrayList types)
1314                 {
1315                         Type best = null;
1316
1317                         if (priv_fmet_param == null)
1318                                 priv_fmet_param = new EmptyExpression ();
1319
1320                         foreach (Type t in types){
1321                                 priv_fmet_param.SetType (t);
1322                                 
1323                                 if (best == null) {
1324                                         best = t;
1325                                         continue;
1326                                 }
1327                                 
1328                                 if (StandardConversionExists (priv_fmet_param, best))
1329                                         best = t;
1330                         }
1331
1332                         return best;
1333                 }
1334
1335                 //
1336                 // Used internally by FindMostEncompassingType, this is used
1337                 // to avoid creating lots of objects in the tight loop inside
1338                 // FindMostEncompassingType
1339                 //
1340                 static EmptyExpression priv_fmee_ret;
1341                 
1342                 /// <summary>
1343                 ///  Finds "most encompassing type" according to the spec (13.4.2)
1344                 ///  amongst the types in the given set
1345                 /// </summary>
1346                 static Type FindMostEncompassingType (ArrayList types)
1347                 {
1348                         Type best = null;
1349
1350                         if (priv_fmee_ret == null)
1351                                 priv_fmee_ret = new EmptyExpression ();
1352
1353                         foreach (Type t in types){
1354                                 priv_fmee_ret.SetType (best);
1355
1356                                 if (best == null) {
1357                                         best = t;
1358                                         continue;
1359                                 }
1360
1361                                 if (StandardConversionExists (priv_fmee_ret, t))
1362                                         best = t;
1363                         }
1364                         
1365                         return best;
1366                 }
1367
1368                 //
1369                 // Used to avoid creating too many objects
1370                 //
1371                 static EmptyExpression priv_fms_expr;
1372                 
1373                 /// <summary>
1374                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
1375                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
1376                 ///   for explicit and implicit conversion operators.
1377                 /// </summary>
1378                 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1379                                                            bool apply_explicit_conv_rules,
1380                                                            Location loc)
1381                 {
1382                         ArrayList src_types_set = new ArrayList ();
1383                         
1384                         if (priv_fms_expr == null)
1385                                 priv_fms_expr = new EmptyExpression ();
1386
1387                         //
1388                         // If any operator converts from S then Sx = S
1389                         //
1390                         Type source_type = source.Type;
1391                         foreach (MethodBase mb in me.Methods){
1392                                 ParameterData pd = Invocation.GetParameterData (mb);
1393                                 Type param_type = pd.ParameterType (0);
1394
1395                                 if (param_type == source_type)
1396                                         return param_type;
1397
1398                                 if (apply_explicit_conv_rules) {
1399                                         //
1400                                         // From the spec :
1401                                         // Find the set of applicable user-defined conversion operators, U.  This set
1402                                         // consists of the
1403                                         // user-defined implicit or explicit conversion operators declared by
1404                                         // the classes or structs in D that convert from a type encompassing
1405                                         // or encompassed by S to a type encompassing or encompassed by T
1406                                         //
1407                                         priv_fms_expr.SetType (param_type);
1408                                         if (StandardConversionExists (priv_fms_expr, source_type))
1409                                                 src_types_set.Add (param_type);
1410                                         else {
1411                                                 if (StandardConversionExists (source, param_type))
1412                                                         src_types_set.Add (param_type);
1413                                         }
1414                                 } else {
1415                                         //
1416                                         // Only if S is encompassed by param_type
1417                                         //
1418                                         if (StandardConversionExists (source, param_type))
1419                                                 src_types_set.Add (param_type);
1420                                 }
1421                         }
1422                         
1423                         //
1424                         // Explicit Conv rules
1425                         //
1426                         if (apply_explicit_conv_rules) {
1427                                 ArrayList candidate_set = new ArrayList ();
1428
1429                                 foreach (Type param_type in src_types_set){
1430                                         if (StandardConversionExists (source, param_type))
1431                                                 candidate_set.Add (param_type);
1432                                 }
1433
1434                                 if (candidate_set.Count != 0)
1435                                         return FindMostEncompassedType (candidate_set);
1436                         }
1437
1438                         //
1439                         // Final case
1440                         //
1441                         if (apply_explicit_conv_rules)
1442                                 return FindMostEncompassingType (src_types_set);
1443                         else
1444                                 return FindMostEncompassedType (src_types_set);
1445                 }
1446
1447                 //
1448                 // Useful in avoiding proliferation of objects
1449                 //
1450                 static EmptyExpression priv_fmt_expr;
1451                 
1452                 /// <summary>
1453                 ///  Finds the most specific target Tx according to section 13.4.4
1454                 /// </summary>
1455                 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1456                                                            bool apply_explicit_conv_rules,
1457                                                            Location loc)
1458                 {
1459                         ArrayList tgt_types_set = new ArrayList ();
1460                         
1461                         if (priv_fmt_expr == null)
1462                                 priv_fmt_expr = new EmptyExpression ();
1463                         
1464                         //
1465                         // If any operator converts to T then Tx = T
1466                         //
1467                         foreach (MethodInfo mi in me.Methods){
1468                                 Type ret_type = mi.ReturnType;
1469
1470                                 if (ret_type == target)
1471                                         return ret_type;
1472
1473                                 if (apply_explicit_conv_rules) {
1474                                         //
1475                                         // From the spec :
1476                                         // Find the set of applicable user-defined conversion operators, U.
1477                                         //
1478                                         // This set consists of the
1479                                         // user-defined implicit or explicit conversion operators declared by
1480                                         // the classes or structs in D that convert from a type encompassing
1481                                         // or encompassed by S to a type encompassing or encompassed by T
1482                                         //
1483                                         priv_fms_expr.SetType (ret_type);
1484                                         if (StandardConversionExists (priv_fms_expr, target))
1485                                                 tgt_types_set.Add (ret_type);
1486                                         else {
1487                                                 priv_fms_expr.SetType (target);
1488                                                 if (StandardConversionExists (priv_fms_expr, ret_type))
1489                                                         tgt_types_set.Add (ret_type);
1490                                         }
1491                                 } else {
1492                                         //
1493                                         // Only if T is encompassed by param_type
1494                                         //
1495                                         priv_fms_expr.SetType (ret_type);
1496                                         if (StandardConversionExists (priv_fms_expr, target))
1497                                                 tgt_types_set.Add (ret_type);
1498                                 }
1499                         }
1500
1501                         //
1502                         // Explicit conv rules
1503                         //
1504                         if (apply_explicit_conv_rules) {
1505                                 ArrayList candidate_set = new ArrayList ();
1506
1507                                 foreach (Type ret_type in tgt_types_set){
1508                                         priv_fmt_expr.SetType (ret_type);
1509                                         
1510                                         if (StandardConversionExists (priv_fmt_expr, target))
1511                                                 candidate_set.Add (ret_type);
1512                                 }
1513
1514                                 if (candidate_set.Count != 0)
1515                                         return FindMostEncompassingType (candidate_set);
1516                         }
1517                         
1518                         //
1519                         // Okay, final case !
1520                         //
1521                         if (apply_explicit_conv_rules)
1522                                 return FindMostEncompassedType (tgt_types_set);
1523                         else 
1524                                 return FindMostEncompassingType (tgt_types_set);
1525                 }
1526                 
1527                 /// <summary>
1528                 ///  User-defined Implicit conversions
1529                 /// </summary>
1530                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1531                                                                  Type target, Location loc)
1532                 {
1533                         return UserDefinedConversion (ec, source, target, loc, false);
1534                 }
1535
1536                 /// <summary>
1537                 ///  User-defined Explicit conversions
1538                 /// </summary>
1539                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1540                                                                  Type target, Location loc)
1541                 {
1542                         return UserDefinedConversion (ec, source, target, loc, true);
1543                 }
1544
1545                 /// <summary>
1546                 ///   Computes the MethodGroup for the user-defined conversion
1547                 ///   operators from source_type to target_type.  'look_for_explicit'
1548                 ///   controls whether we should also include the list of explicit
1549                 ///   operators
1550                 /// </summary>
1551                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1552                                                                Type source_type, Type target_type,
1553                                                                Location loc, bool look_for_explicit)
1554                 {
1555                         Expression mg1 = null, mg2 = null;
1556                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1557                         string op_name;
1558
1559                         //
1560                         // FIXME : How does the False operator come into the picture ?
1561                         // This doesn't look complete and very correct !
1562                         //
1563                         if (target_type == TypeManager.bool_type && !look_for_explicit)
1564                                 op_name = "op_True";
1565                         else
1566                                 op_name = "op_Implicit";
1567
1568                         MethodGroupExpr union3;
1569                         
1570                         mg1 = MethodLookup (ec, source_type, op_name, loc);
1571                         if (source_type.BaseType != null)
1572                                 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1573
1574                         if (mg1 == null)
1575                                 union3 = (MethodGroupExpr) mg2;
1576                         else if (mg2 == null)
1577                                 union3 = (MethodGroupExpr) mg1;
1578                         else
1579                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1580
1581                         mg1 = MethodLookup (ec, target_type, op_name, loc);
1582                         if (mg1 != null){
1583                                 if (union3 != null)
1584                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1585                                 else
1586                                         union3 = (MethodGroupExpr) mg1;
1587                         }
1588
1589                         if (target_type.BaseType != null)
1590                                 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1591                         
1592                         if (mg1 != null){
1593                                 if (union3 != null)
1594                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1595                                 else
1596                                         union3 = (MethodGroupExpr) mg1;
1597                         }
1598
1599                         MethodGroupExpr union4 = null;
1600
1601                         if (look_for_explicit) {
1602                                 op_name = "op_Explicit";
1603
1604                                 mg5 = MemberLookup (ec, source_type, op_name, loc);
1605                                 if (source_type.BaseType != null)
1606                                         mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1607                                 
1608                                 mg7 = MemberLookup (ec, target_type, op_name, loc);
1609                                 if (target_type.BaseType != null)
1610                                         mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1611                                 
1612                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1613                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1614
1615                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1616                         }
1617                         
1618                         return Invocation.MakeUnionSet (union3, union4, loc);
1619                 }
1620                 
1621                 /// <summary>
1622                 ///   User-defined conversions
1623                 /// </summary>
1624                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1625                                                                 Type target, Location loc,
1626                                                                 bool look_for_explicit)
1627                 {
1628                         MethodGroupExpr union;
1629                         Type source_type = source.Type;
1630                         MethodBase method = null;
1631                         
1632                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1633                         if (union == null)
1634                                 return null;
1635                         
1636                         Type most_specific_source, most_specific_target;
1637
1638 #if BLAH
1639                         foreach (MethodBase m in union.Methods){
1640                                 Console.WriteLine ("Name: " + m.Name);
1641                                 Console.WriteLine ("    : " + ((MethodInfo)m).ReturnType);
1642                         }
1643 #endif
1644                         
1645                         most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1646                         if (most_specific_source == null)
1647                                 return null;
1648
1649                         most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1650                         if (most_specific_target == null) 
1651                                 return null;
1652
1653                         int count = 0;
1654
1655                         foreach (MethodBase mb in union.Methods){
1656                                 ParameterData pd = Invocation.GetParameterData (mb);
1657                                 MethodInfo mi = (MethodInfo) mb;
1658                                 
1659                                 if (pd.ParameterType (0) == most_specific_source &&
1660                                     mi.ReturnType == most_specific_target) {
1661                                         method = mb;
1662                                         count++;
1663                                 }
1664                         }
1665                         
1666                         if (method == null || count > 1)
1667                                 return null;
1668                         
1669                         
1670                         //
1671                         // This will do the conversion to the best match that we
1672                         // found.  Now we need to perform an implict standard conversion
1673                         // if the best match was not the type that we were requested
1674                         // by target.
1675                         //
1676                         if (look_for_explicit)
1677                                 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1678                         else
1679                                 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1680
1681                         if (source == null)
1682                                 return null;
1683
1684                         Expression e;
1685                         e =  new UserCast ((MethodInfo) method, source, loc);
1686                         if (e.Type != target){
1687                                 if (!look_for_explicit)
1688                                         e = ConvertImplicitStandard (ec, e, target, loc);
1689                                 else
1690                                         e = ConvertExplicitStandard (ec, e, target, loc);
1691                         } 
1692                         return e;
1693                 }
1694                 
1695                 /// <summary>
1696                 ///   Converts implicitly the resolved expression 'expr' into the
1697                 ///   'target_type'.  It returns a new expression that can be used
1698                 ///   in a context that expects a 'target_type'. 
1699                 /// </summary>
1700                 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1701                                                           Type target_type, Location loc)
1702                 {
1703                         Type expr_type = expr.Type;
1704                         Expression e;
1705
1706
1707                         if (expr_type == target_type)
1708                                 return expr;
1709
1710                         if (target_type == null)
1711                                 throw new Exception ("Target type is null");
1712
1713                         e = ConvertImplicitStandard (ec, expr, target_type, loc);
1714                         if (e != null)
1715                                 return e;
1716                                         
1717                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1718                         
1719                         if (e != null)
1720                                 return e;
1721                                 
1722                         e = NarrowingConversion (ec, expr, target_type, loc);
1723                         if (e != null)
1724                                 return e;                               
1725
1726                         return null;
1727                 }
1728
1729                 /// <summary>
1730                 ///   Converts the resolved expression 'expr' into the
1731                 ///   'target_type' using the Microsoft.VisualBasic runtime.
1732                 ///   It returns a new expression that can be used
1733                 ///   in a context that expects a 'target_type'. 
1734                 /// </summary>
1735                 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1736                 {
1737                         Expression etmp, e;
1738                         ArrayList args;
1739                         Argument arg;
1740                         
1741                         etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1742                         args = new ArrayList();
1743                         arg = new Argument (expr, Argument.AType.Expression);
1744                         args.Add (arg);
1745                         e = (Expression) new Invocation (etmp, args, loc);
1746                         e = e.Resolve(ec);      
1747                         return (e);             
1748                 }
1749
1750                 static private Expression RTConversionExpression (EmitContext ec, string ns, string method, Expression expr, Location loc)
1751                 {
1752                         Expression etmp, e;
1753                         ArrayList args;
1754                         Argument arg;
1755                         
1756                         etmp = Mono.MonoBASIC.Parser.DecomposeQI(ns+method, loc);
1757                         args = new ArrayList();
1758                         arg = new Argument (expr, Argument.AType.Expression);
1759                         args.Add (arg);
1760                         e = (Expression) new Invocation (etmp, args, loc);
1761                         e = e.Resolve(ec);      
1762                         return (e);             
1763                 }
1764
1765                 
1766                 static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
1767                 {
1768                         Type expr_type = expr.Type;
1769
1770                         if (target_type == TypeManager.sbyte_type){
1771                                 //
1772                                 // To sbyte from short, int, long, float, double.
1773                                 //
1774                                 if ((expr_type == TypeManager.int32_type) || 
1775                                     (expr_type == TypeManager.int64_type) ||
1776                                     (expr_type == TypeManager.double_type) ||
1777                                     (expr_type == TypeManager.float_type)  ||
1778                                     (expr_type == TypeManager.short_type) ||
1779                                     (expr_type == TypeManager.decimal_type))
1780                                         return true;
1781                                 
1782                         } else if (target_type == TypeManager.byte_type){
1783                                 //
1784                                 // To byte from short, ushort, int, uint, long, ulong, float, double
1785                                 // 
1786                                 if ((expr_type == TypeManager.short_type) ||
1787                                     (expr_type == TypeManager.ushort_type) ||
1788                                     (expr_type == TypeManager.int32_type) ||
1789                                     (expr_type == TypeManager.uint32_type) ||
1790                                     (expr_type == TypeManager.uint64_type) ||
1791                                     (expr_type == TypeManager.int64_type) ||
1792                                     (expr_type == TypeManager.float_type) ||
1793                                     (expr_type == TypeManager.double_type) ||
1794                                     (expr_type == TypeManager.decimal_type))
1795                                         return true;
1796         
1797                         } else if (target_type == TypeManager.short_type){
1798                                 //
1799                                 // To short from int, long, float, double
1800                                 // 
1801                                 if ((expr_type == TypeManager.int32_type) ||
1802                                     (expr_type == TypeManager.int64_type) ||
1803                                     (expr_type == TypeManager.double_type) ||
1804                                     (expr_type == TypeManager.float_type) ||
1805                                     (expr_type == TypeManager.decimal_type))
1806                                         return true;
1807                                         
1808                         } else if (target_type == TypeManager.ushort_type){
1809                                 //
1810                                 // To ushort from int, uint, long, ulong, float, double
1811                                 //
1812                                 if ((expr_type == TypeManager.uint32_type) ||
1813                                     (expr_type == TypeManager.uint64_type) ||
1814                                     (expr_type == TypeManager.int32_type) ||
1815                                     (expr_type == TypeManager.int64_type) ||
1816                                     (expr_type == TypeManager.double_type) ||
1817                                     (expr_type == TypeManager.float_type) ||
1818                                     (expr_type == TypeManager.decimal_type))
1819                                         return true;
1820                                     
1821                         } else if (target_type == TypeManager.int32_type){
1822                                 //
1823                                 // To int from long, float, double
1824                                 //
1825                                 if ((expr_type == TypeManager.int64_type) ||
1826                                     (expr_type == TypeManager.double_type) ||
1827                                     (expr_type == TypeManager.float_type) ||
1828                                     (expr_type == TypeManager.decimal_type))
1829                                         return true;
1830                                         
1831                         } else if (target_type == TypeManager.uint32_type){
1832                                 //
1833                                 // To uint from long, ulong, float, double
1834                                 //
1835                                 if ((expr_type == TypeManager.int64_type) ||
1836                                     (expr_type == TypeManager.uint64_type) ||
1837                                     (expr_type == TypeManager.double_type) ||
1838                                     (expr_type == TypeManager.float_type) ||
1839                                     (expr_type == TypeManager.decimal_type))
1840                                         return true;
1841                                         
1842                         } else if ((target_type == TypeManager.uint64_type) ||
1843                                    (target_type == TypeManager.int64_type)) {
1844                                 //
1845                                 // To long/ulong from float, double
1846                                 //
1847                                 if ((expr_type == TypeManager.double_type) ||
1848                                     (expr_type == TypeManager.float_type) ||
1849                                     (expr_type == TypeManager.decimal_type))
1850                                         return true;
1851                                     
1852                         } else if (target_type == TypeManager.char_type){
1853                                 //
1854                                 // To char from ushort, int, uint, long, ulong, float, double
1855                                 // 
1856                                 if ((expr_type == TypeManager.ushort_type) ||
1857                                     (expr_type == TypeManager.int32_type) ||
1858                                     (expr_type == TypeManager.uint32_type) ||
1859                                     (expr_type == TypeManager.uint64_type) ||
1860                                     (expr_type == TypeManager.int64_type) ||
1861                                     (expr_type == TypeManager.float_type) ||
1862                                     (expr_type == TypeManager.double_type) ||
1863                                     (expr_type == TypeManager.decimal_type))
1864                                         return true;
1865
1866                         } else if (target_type == TypeManager.float_type){
1867                                 //
1868                                 // To float from double
1869                                 //
1870                                 if (expr_type == TypeManager.double_type)
1871                                         return true;
1872                         }       
1873
1874                         return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;     
1875                 }
1876                 
1877                 static public Expression NarrowingConversion (EmitContext ec, Expression expr,
1878                                                                 Type target_type, Location loc)
1879                 {
1880                         Type expr_type = expr.Type;
1881
1882                         if (target_type == TypeManager.sbyte_type){
1883                                 //
1884                                 // To sbyte from short, int, long, float, double.
1885                                 //
1886                                 if ((expr_type == TypeManager.int32_type) || 
1887                                     (expr_type == TypeManager.int64_type) ||
1888                                     (expr_type == TypeManager.double_type) ||
1889                                     (expr_type == TypeManager.float_type)  ||
1890                                     (expr_type == TypeManager.short_type) ||
1891                                     (expr_type == TypeManager.decimal_type))
1892                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1893                                 
1894                         } else if (target_type == TypeManager.byte_type){
1895                                 //
1896                                 // To byte from short, ushort, int, uint, long, ulong, float, double
1897                                 // 
1898                                 if ((expr_type == TypeManager.short_type) ||
1899                                     (expr_type == TypeManager.ushort_type) ||
1900                                     (expr_type == TypeManager.int32_type) ||
1901                                     (expr_type == TypeManager.uint32_type) ||
1902                                     (expr_type == TypeManager.uint64_type) ||
1903                                     (expr_type == TypeManager.int64_type) ||
1904                                     (expr_type == TypeManager.float_type) ||
1905                                     (expr_type == TypeManager.double_type) ||
1906                                     (expr_type == TypeManager.decimal_type))
1907                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1908         
1909                         } else if (target_type == TypeManager.short_type){
1910                                 //
1911                                 // To short from int, long, float, double
1912                                 // 
1913                                 if ((expr_type == TypeManager.int32_type) ||
1914                                     (expr_type == TypeManager.int64_type) ||
1915                                     (expr_type == TypeManager.double_type) ||
1916                                     (expr_type == TypeManager.float_type) ||
1917                                     (expr_type == TypeManager.decimal_type))
1918
1919                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1920                                         
1921                         } else if (target_type == TypeManager.ushort_type){
1922                                 //
1923                                 // To ushort from int, uint, long, ulong, float, double
1924                                 //
1925                                 if ((expr_type == TypeManager.uint32_type) ||
1926                                     (expr_type == TypeManager.uint64_type) ||
1927                                     (expr_type == TypeManager.int32_type) ||
1928                                     (expr_type == TypeManager.int64_type) ||
1929                                     (expr_type == TypeManager.double_type) ||
1930                                     (expr_type == TypeManager.float_type) ||
1931                                     (expr_type == TypeManager.decimal_type))
1932                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1933                                     
1934                         } else if (target_type == TypeManager.int32_type){
1935                                 //
1936                                 // To int from long, float, double
1937                                 //
1938                                 if ((expr_type == TypeManager.int64_type) ||
1939                                     (expr_type == TypeManager.double_type) ||
1940                                     (expr_type == TypeManager.float_type) ||
1941                                     (expr_type == TypeManager.decimal_type))
1942                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1943                                         
1944                         } else if (target_type == TypeManager.uint32_type){
1945                                 //
1946                                 // To uint from long, ulong, float, double
1947                                 //
1948                                 if ((expr_type == TypeManager.int64_type) ||
1949                                     (expr_type == TypeManager.uint64_type) ||
1950                                     (expr_type == TypeManager.double_type) ||
1951                                     (expr_type == TypeManager.float_type) ||
1952                                     (expr_type == TypeManager.decimal_type))
1953                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1954                                         
1955                         } else if ((target_type == TypeManager.uint64_type) ||
1956                                    (target_type == TypeManager.int64_type)) {
1957                                 //
1958                                 // To long/ulong from float, double
1959                                 //
1960                                 if ((expr_type == TypeManager.double_type) ||
1961                                     (expr_type == TypeManager.float_type) ||
1962                                     (expr_type == TypeManager.decimal_type))
1963                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1964                                     
1965                         } else if (target_type == TypeManager.char_type){
1966                                 //
1967                                 // To char from ushort, int, uint, long, ulong, float, double
1968                                 // 
1969                                 if ((expr_type == TypeManager.ushort_type) ||
1970                                     (expr_type == TypeManager.int32_type) ||
1971                                     (expr_type == TypeManager.uint32_type) ||
1972                                     (expr_type == TypeManager.uint64_type) ||
1973                                     (expr_type == TypeManager.int64_type) ||
1974                                     (expr_type == TypeManager.float_type) ||
1975                                     (expr_type == TypeManager.double_type) ||
1976                                     (expr_type == TypeManager.decimal_type))
1977                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1978
1979                         } else if (target_type == TypeManager.float_type){
1980                                 //
1981                                 // To float from double
1982                                 //
1983                                 if (expr_type == TypeManager.double_type)
1984                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1985                         }       
1986
1987                         TypeCode dest_type = Type.GetTypeCode (target_type);
1988                         TypeCode src_type = Type.GetTypeCode (expr_type);
1989                         Expression e = null;
1990
1991                         // VB.NET Objects can be converted to anything by default
1992                         // unless, that is, an exception at runtime blows it all
1993                         if (src_type == TypeCode.Object) {
1994                                 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
1995                                 Cast ce = new Cast (cast_type, expr, loc);
1996                                 ce.IsRuntimeCast = true;
1997                                 return ce.Resolve (ec);
1998                         }
1999
2000                         switch (dest_type) {
2001                                 case TypeCode.String:
2002                                         switch (src_type) {
2003                                                 case TypeCode.SByte:                                            
2004                                                 case TypeCode.Byte:
2005                                                         e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
2006                                                         break;  
2007                                                 case TypeCode.UInt16:
2008                                                 case TypeCode.Int16:
2009                                                         e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
2010                                                         break;          
2011                                                 case TypeCode.UInt32:                                   
2012                                                 case TypeCode.Int32:
2013                                                         e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
2014                                                         break;                                                  
2015                                                 case TypeCode.UInt64:   
2016                                                 case TypeCode.Int64:
2017                                                         e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
2018                                                         break;                                                  
2019                                                 case TypeCode.Char:
2020                                                         e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
2021                                                         break;                                                          
2022                                                 case TypeCode.Single:
2023                                                         e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
2024                                                         break;          
2025                                                 case TypeCode.Double:
2026                                                         e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
2027                                                         break;                                                                                                                                                  
2028                                                 case TypeCode.Boolean:
2029                                                         e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
2030                                                         break;  
2031                                                 case TypeCode.DateTime:
2032                                                         e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
2033                                                         break;          
2034                                                 case TypeCode.Decimal:
2035                                                         e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
2036                                                         break;          
2037                                                 case TypeCode.Object:
2038                                                         e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
2039                                                         break;                                                                                                                                                                                                                          
2040                                         }
2041                                         break;
2042                                         
2043                                 case TypeCode.Int32:
2044                                 case TypeCode.UInt32:   
2045                                         switch (src_type) {                                             
2046                                                 case TypeCode.String:                           
2047                                                         e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
2048                                                         break;          
2049                                                 case TypeCode.Object:                           
2050                                                         e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
2051                                                         break;                                                                                  
2052                                         }
2053                                         break;  
2054
2055                                 case TypeCode.Int16:
2056                                 case TypeCode.UInt16:   
2057                                         switch (src_type) {                                             
2058                                                 case TypeCode.String:                           
2059                                                         e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
2060                                                         break;          
2061                                                 case TypeCode.Object:                           
2062                                                         e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
2063                                                         break;                                                                                  
2064                                         }
2065                                         break;  
2066                                 case TypeCode.Byte:
2067                                         // Ok, this *is* broken
2068                                         e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
2069                                         break;  
2070                                 case TypeCode.DateTime: 
2071                                         switch (src_type) {                                             
2072                                                 case TypeCode.String:                           
2073                                                         e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
2074                                                         break;          
2075                                                 case TypeCode.Object:                           
2076                                                         e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
2077                                                         break;                                                                                  
2078                                         }
2079                                         break;                                                                                                          
2080                         }
2081                         
2082                         // We must examine separately some types that
2083                         // don't have a TypeCode but are supported 
2084                         // in the runtime
2085                         if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
2086                                 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
2087                         }
2088                         
2089                         return e;
2090                 }
2091                                                                                                 
2092                 /// <summary>
2093                 ///   Attempts to apply the 'Standard Implicit
2094                 ///   Conversion' rules to the expression 'expr' into
2095                 ///   the 'target_type'.  It returns a new expression
2096                 ///   that can be used in a context that expects a
2097                 ///   'target_type'.
2098                 ///
2099                 ///   This is different from 'ConvertImplicit' in that the
2100                 ///   user defined implicit conversions are excluded. 
2101                 /// </summary>
2102                 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
2103                                                                   Type target_type, Location loc)
2104                 {
2105                         Type expr_type = expr.Type;
2106                         Expression e;
2107
2108                         if (expr_type == target_type)
2109                                 return expr;
2110
2111                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
2112
2113                         if (e != null)
2114                                 return e;
2115
2116                         e = ImplicitReferenceConversion (expr, target_type);
2117                         if (e != null)
2118                                 return e;
2119
2120                         if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
2121                                 expr_type = TypeManager.EnumToUnderlying (expr.Type);
2122                                 expr = new EmptyCast (expr, expr_type);
2123                                 if (expr_type == target_type)
2124                                         return expr;
2125                                 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2126                                 if (e != null)
2127                                         return e;
2128                         }
2129
2130                         if (ec.InUnsafe) {
2131                                 if (expr_type.IsPointer){
2132                                         if (target_type == TypeManager.void_ptr_type)
2133                                                 return new EmptyCast (expr, target_type);
2134
2135                                         //
2136                                         // yep, comparing pointer types cant be done with
2137                                         // t1 == t2, we have to compare their element types.
2138                                         //
2139                                         if (target_type.IsPointer){
2140                                                 if (target_type.GetElementType()==expr_type.GetElementType())
2141                                                         return expr;
2142                                         }
2143                                 }
2144                                 
2145                                 if (target_type.IsPointer){
2146                                         if (expr is NullLiteral)
2147                                                 return new EmptyCast (expr, target_type);
2148                                 }
2149                         }
2150
2151                         return null;
2152                 }
2153
2154                 /// <summary>
2155                 ///   Attemps to perform an implict constant conversion of the IntConstant
2156                 ///   into a different data type using casts (See Implicit Constant
2157                 ///   Expression Conversions)
2158                 /// </summary>
2159                 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
2160                 {
2161                         int value = ic.Value;
2162
2163                         //
2164                         // FIXME: This could return constants instead of EmptyCasts
2165                         //
2166                         if (target_type == TypeManager.sbyte_type){
2167                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2168                                         return new SByteConstant ((sbyte) value);
2169                         } else if (target_type == TypeManager.byte_type){
2170                                 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2171                                         return new ByteConstant ((byte) value);
2172                         } else if (target_type == TypeManager.short_type){
2173                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2174                                         return new ShortConstant ((short) value);
2175                         } else if (target_type == TypeManager.ushort_type){
2176                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2177                                         return new UShortConstant ((ushort) value);
2178                         } else if (target_type == TypeManager.uint32_type){
2179                                 if (value >= 0)
2180                                         return new UIntConstant ((uint) value);
2181                         } else if (target_type == TypeManager.uint64_type){
2182                                 //
2183                                 // we can optimize this case: a positive int32
2184                                 // always fits on a uint64.  But we need an opcode
2185                                 // to do it.
2186                                 //
2187                                 if (value >= 0)
2188                                         return new ULongConstant ((ulong) value);
2189                         }
2190                         
2191                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
2192                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
2193                                 Constant e = (Constant) ic;
2194                                 
2195                                 //
2196                                 // Possibly, we need to create a different 0 literal before passing
2197                                 // to EnumConstant
2198                                 //n
2199                                 if (underlying == TypeManager.int64_type)
2200                                         e = new LongLiteral (0);
2201                                 else if (underlying == TypeManager.uint64_type)
2202                                         e = new ULongLiteral (0);
2203
2204                                 return new EnumConstant (e, target_type);
2205                         }
2206                         return null;
2207                 }
2208
2209                 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
2210                 {
2211                         string msg = "Cannot convert implicitly from '"+
2212                                 TypeManager.MonoBASIC_Name (source) + "' to '" +
2213                                 TypeManager.MonoBASIC_Name (target) + "'";
2214
2215                         throw new Exception (msg);
2216
2217                         Report.Error (29, loc, msg);
2218                 }
2219
2220                 /// <summary>
2221                 ///   Attemptes to implicityly convert 'target' into 'type', using
2222                 ///   ConvertImplicit.  If there is no implicit conversion, then
2223                 ///   an error is signaled
2224                 /// </summary>
2225                 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
2226                                                                   Type target_type, Location loc)
2227                 {
2228                         Expression e;
2229                         
2230                         e = ConvertImplicit (ec, source, target_type, loc);
2231
2232                         if (e != null)
2233                                 return e;
2234
2235
2236                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
2237                                 Report.Error (664, loc,
2238                                               "Double literal cannot be implicitly converted to " +
2239                                               "float type, use F suffix to create a float literal");
2240                         }
2241
2242                         Error_CannotConvertImplicit (loc, source.Type, target_type);
2243
2244                         return null;
2245                 }
2246
2247                 /// <summary>
2248                 ///   Performs the explicit numeric conversions
2249                 /// </summary>
2250                 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
2251                 {
2252                         Type expr_type = expr.Type;
2253
2254                         //
2255                         // If we have an enumeration, extract the underlying type,
2256                         // use this during the comparison, but wrap around the original
2257                         // target_type
2258                         //
2259                         Type real_target_type = target_type;
2260
2261                         if (TypeManager.IsEnumType (real_target_type))
2262                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
2263
2264                         if (StandardConversionExists (expr, real_target_type)){
2265                                 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
2266
2267                                 if (real_target_type != target_type)
2268                                         return new EmptyCast (ce, target_type);
2269                                 return ce;
2270                         }
2271                         
2272                         if (expr_type == TypeManager.sbyte_type){
2273                                 //
2274                                 // From sbyte to byte, ushort, uint, ulong, char
2275                                 //
2276                                 if (real_target_type == TypeManager.byte_type)
2277                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
2278                                 if (real_target_type == TypeManager.ushort_type)
2279                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
2280                                 if (real_target_type == TypeManager.uint32_type)
2281                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
2282                                 if (real_target_type == TypeManager.uint64_type)
2283                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
2284                                 if (real_target_type == TypeManager.char_type)
2285                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
2286                         } else if (expr_type == TypeManager.byte_type){
2287                                 //
2288                                 // From byte to sbyte and char
2289                                 //
2290                                 if (real_target_type == TypeManager.sbyte_type)
2291                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
2292                                 if (real_target_type == TypeManager.char_type)
2293                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
2294                         } else if (expr_type == TypeManager.short_type){
2295                                 //
2296                                 // From short to sbyte, byte, ushort, uint, ulong, char
2297                                 //
2298                                 if (real_target_type == TypeManager.sbyte_type)
2299                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
2300                                 if (real_target_type == TypeManager.byte_type)
2301                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
2302                                 if (real_target_type == TypeManager.ushort_type)
2303                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
2304                                 if (real_target_type == TypeManager.uint32_type)
2305                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
2306                                 if (real_target_type == TypeManager.uint64_type)
2307                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
2308                                 if (real_target_type == TypeManager.char_type)
2309                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
2310                         } else if (expr_type == TypeManager.ushort_type){
2311                                 //
2312                                 // From ushort to sbyte, byte, short, char
2313                                 //
2314                                 if (real_target_type == TypeManager.sbyte_type)
2315                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
2316                                 if (real_target_type == TypeManager.byte_type)
2317                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
2318                                 if (real_target_type == TypeManager.short_type)
2319                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
2320                                 if (real_target_type == TypeManager.char_type)
2321                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
2322                         } else if (expr_type == TypeManager.int32_type){
2323                                 //
2324                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
2325                                 //
2326                                 if (real_target_type == TypeManager.sbyte_type)
2327                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
2328                                 if (real_target_type == TypeManager.byte_type)
2329                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
2330                                 if (real_target_type == TypeManager.short_type)
2331                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
2332                                 if (real_target_type == TypeManager.ushort_type)
2333                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
2334                                 if (real_target_type == TypeManager.uint32_type)
2335                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2336                                 if (real_target_type == TypeManager.uint64_type)
2337                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2338                                 if (real_target_type == TypeManager.char_type)
2339                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2340                         } else if (expr_type == TypeManager.uint32_type){
2341                                 //
2342                                 // From uint to sbyte, byte, short, ushort, int, char
2343                                 //
2344                                 if (real_target_type == TypeManager.sbyte_type)
2345                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2346                                 if (real_target_type == TypeManager.byte_type)
2347                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2348                                 if (real_target_type == TypeManager.short_type)
2349                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2350                                 if (real_target_type == TypeManager.ushort_type)
2351                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2352                                 if (real_target_type == TypeManager.int32_type)
2353                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2354                                 if (real_target_type == TypeManager.char_type)
2355                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2356                         } else if (expr_type == TypeManager.int64_type){
2357                                 //
2358                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2359                                 //
2360                                 if (real_target_type == TypeManager.sbyte_type)
2361                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2362                                 if (real_target_type == TypeManager.byte_type)
2363                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2364                                 if (real_target_type == TypeManager.short_type)
2365                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2366                                 if (real_target_type == TypeManager.ushort_type)
2367                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2368                                 if (real_target_type == TypeManager.int32_type)
2369                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2370                                 if (real_target_type == TypeManager.uint32_type)
2371                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2372                                 if (real_target_type == TypeManager.uint64_type)
2373                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2374                                 if (real_target_type == TypeManager.char_type)
2375                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2376                         } else if (expr_type == TypeManager.uint64_type){
2377                                 //
2378                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2379                                 //
2380                                 if (real_target_type == TypeManager.sbyte_type)
2381                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2382                                 if (real_target_type == TypeManager.byte_type)
2383                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2384                                 if (real_target_type == TypeManager.short_type)
2385                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2386                                 if (real_target_type == TypeManager.ushort_type)
2387                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2388                                 if (real_target_type == TypeManager.int32_type)
2389                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2390                                 if (real_target_type == TypeManager.uint32_type)
2391                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2392                                 if (real_target_type == TypeManager.int64_type)
2393                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2394                                 if (real_target_type == TypeManager.char_type)
2395                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2396                         } else if (expr_type == TypeManager.char_type){
2397                                 //
2398                                 // From char to sbyte, byte, short
2399                                 //
2400                                 if (real_target_type == TypeManager.sbyte_type)
2401                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2402                                 if (real_target_type == TypeManager.byte_type)
2403                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2404                                 if (real_target_type == TypeManager.short_type)
2405                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2406                         } else if (expr_type == TypeManager.float_type){
2407                                 //
2408                                 // From float to sbyte, byte, short,
2409                                 // ushort, int, uint, long, ulong, char
2410                                 // or decimal
2411                                 //
2412                                 if (real_target_type == TypeManager.sbyte_type)
2413                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2414                                 if (real_target_type == TypeManager.byte_type)
2415                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2416                                 if (real_target_type == TypeManager.short_type)
2417                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2418                                 if (real_target_type == TypeManager.ushort_type)
2419                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2420                                 if (real_target_type == TypeManager.int32_type)
2421                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2422                                 if (real_target_type == TypeManager.uint32_type)
2423                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2424                                 if (real_target_type == TypeManager.int64_type)
2425                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2426                                 if (real_target_type == TypeManager.uint64_type)
2427                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2428                                 if (real_target_type == TypeManager.char_type)
2429                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2430                         } else if (expr_type == TypeManager.double_type){
2431                                 //
2432                                 // From double to byte, byte, short,
2433                                 // ushort, int, uint, long, ulong,
2434                                 // char, float or decimal
2435                                 //
2436                                 if (real_target_type == TypeManager.sbyte_type)
2437                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2438                                 if (real_target_type == TypeManager.byte_type)
2439                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2440                                 if (real_target_type == TypeManager.short_type)
2441                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2442                                 if (real_target_type == TypeManager.ushort_type)
2443                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2444                                 if (real_target_type == TypeManager.int32_type)
2445                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2446                                 if (real_target_type == TypeManager.uint32_type)
2447                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2448                                 if (real_target_type == TypeManager.int64_type)
2449                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2450                                 if (real_target_type == TypeManager.uint64_type)
2451                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2452                                 if (real_target_type == TypeManager.char_type)
2453                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2454                                 if (real_target_type == TypeManager.float_type)
2455                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2456                         } 
2457
2458                         // decimal is taken care of by the op_Explicit methods.
2459
2460                         return null;
2461                 }
2462
2463                 /// <summary>
2464                 ///  Returns whether an explicit reference conversion can be performed
2465                 ///  from source_type to target_type
2466                 /// </summary>
2467                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2468                 {
2469                         bool target_is_value_type = target_type.IsValueType;
2470                         
2471                         if (source_type == target_type)
2472                                 return true;
2473                         
2474                         //
2475                         // From object to any reference type
2476                         //
2477                         if (source_type == TypeManager.object_type && !target_is_value_type)
2478                                 return true;
2479                                         
2480                         //
2481                         // From any class S to any class-type T, provided S is a base class of T
2482                         //
2483                         if (target_type.IsSubclassOf (source_type))
2484                                 return true;
2485
2486                         //
2487                         // From any interface type S to any interface T provided S is not derived from T
2488                         //
2489                         if (source_type.IsInterface && target_type.IsInterface){
2490                                 if (!target_type.IsSubclassOf (source_type))
2491                                         return true;
2492                         }
2493                             
2494                         //
2495                         // From any class type S to any interface T, provided S is not sealed
2496                         // and provided S does not implement T.
2497                         //
2498                         if (target_type.IsInterface && !source_type.IsSealed &&
2499                             !TypeManager.ImplementsInterface (source_type, target_type))
2500                                 return true;
2501
2502                         //
2503                         // From any interface-type S to to any class type T, provided T is not
2504                         // sealed, or provided T implements S.
2505                         //
2506                         if (source_type.IsInterface &&
2507                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2508                                 return true;
2509                         
2510                         
2511                         // From an array type S with an element type Se to an array type T with an 
2512                         // element type Te provided all the following are true:
2513                         //     * S and T differe only in element type, in other words, S and T
2514                         //       have the same number of dimensions.
2515                         //     * Both Se and Te are reference types
2516                         //     * An explicit referenc conversions exist from Se to Te
2517                         //
2518                         if (source_type.IsArray && target_type.IsArray) {
2519                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2520                                         
2521                                         Type source_element_type = source_type.GetElementType ();
2522                                         Type target_element_type = target_type.GetElementType ();
2523                                         
2524                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2525                                                 if (ExplicitReferenceConversionExists (source_element_type,
2526                                                                                        target_element_type))
2527                                                         return true;
2528                                 }
2529                         }
2530                         
2531
2532                         // From System.Array to any array-type
2533                         if (source_type == TypeManager.array_type &&
2534                             target_type.IsArray){
2535                                 return true;
2536                         }
2537
2538                         //
2539                         // From System delegate to any delegate-type
2540                         //
2541                         if (source_type == TypeManager.delegate_type &&
2542                             target_type.IsSubclassOf (TypeManager.delegate_type))
2543                                 return true;
2544
2545                         //
2546                         // From ICloneable to Array or Delegate types
2547                         //
2548                         if (source_type == TypeManager.icloneable_type &&
2549                             (target_type == TypeManager.array_type ||
2550                              target_type == TypeManager.delegate_type))
2551                                 return true;
2552                         
2553                         return false;
2554                 }
2555
2556                 /// <summary>
2557                 ///   Implements Explicit Reference conversions
2558                 /// </summary>
2559                 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2560                 {
2561                         Type source_type = source.Type;
2562                         bool target_is_value_type = target_type.IsValueType;
2563
2564                         //
2565                         // From object to any reference type
2566                         //
2567                         if (source_type == TypeManager.object_type && !target_is_value_type)
2568                                 return new ClassCast (source, target_type);
2569
2570
2571                         //
2572                         // From any class S to any class-type T, provided S is a base class of T
2573                         //
2574                         if (target_type.IsSubclassOf (source_type))
2575                                 return new ClassCast (source, target_type);
2576
2577                         //
2578                         // From any interface type S to any interface T provided S is not derived from T
2579                         //
2580                         if (source_type.IsInterface && target_type.IsInterface){
2581                                 if (TypeManager.ImplementsInterface (source_type, target_type))
2582                                         return null;
2583                                 else
2584                                         return new ClassCast (source, target_type);
2585                         }
2586                             
2587                         //
2588                         // From any class type S to any interface T, provides S is not sealed
2589                         // and provided S does not implement T.
2590                         //
2591                         if (target_type.IsInterface && !source_type.IsSealed) {
2592                                 if (TypeManager.ImplementsInterface (source_type, target_type))
2593                                         return null;
2594                                 else
2595                                         return new ClassCast (source, target_type);
2596                                 
2597                         }
2598
2599                         //
2600                         // From any interface-type S to to any class type T, provided T is not
2601                         // sealed, or provided T implements S.
2602                         //
2603                         if (source_type.IsInterface) {
2604                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2605                                         return new ClassCast (source, target_type);
2606                                 else
2607                                         return null;
2608                         }
2609                         
2610                         // From an array type S with an element type Se to an array type T with an 
2611                         // element type Te provided all the following are true:
2612                         //     * S and T differe only in element type, in other words, S and T
2613                         //       have the same number of dimensions.
2614                         //     * Both Se and Te are reference types
2615                         //     * An explicit referenc conversions exist from Se to Te
2616                         //
2617                         if (source_type.IsArray && target_type.IsArray) {
2618                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2619                                         
2620                                         Type source_element_type = source_type.GetElementType ();
2621                                         Type target_element_type = target_type.GetElementType ();
2622                                         
2623                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2624                                                 if (ExplicitReferenceConversionExists (source_element_type,
2625                                                                                        target_element_type))
2626                                                         return new ClassCast (source, target_type);
2627                                 }
2628                         }
2629                         
2630
2631                         // From System.Array to any array-type
2632                         if (source_type == TypeManager.array_type &&
2633                             target_type.IsArray) {
2634                                 return new ClassCast (source, target_type);
2635                         }
2636
2637                         //
2638                         // From System delegate to any delegate-type
2639                         //
2640                         if (source_type == TypeManager.delegate_type &&
2641                             target_type.IsSubclassOf (TypeManager.delegate_type))
2642                                 return new ClassCast (source, target_type);
2643
2644                         //
2645                         // From ICloneable to Array or Delegate types
2646                         //
2647                         if (source_type == TypeManager.icloneable_type &&
2648                             (target_type == TypeManager.array_type ||
2649                              target_type == TypeManager.delegate_type))
2650                                 return new ClassCast (source, target_type);
2651                         
2652                         return null;
2653                 }
2654                 
2655                 /// <summary>
2656                 ///   Performs an explicit conversion of the expression 'expr' whose
2657                 ///   type is expr.Type to 'target_type'.
2658                 /// </summary>
2659                 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2660                                                           Type target_type, bool runtimeconv, Location loc)
2661                 {
2662                         Type expr_type = expr.Type;
2663                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2664
2665                         if (ne != null)
2666                                 return ne;
2667
2668                         ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2669                         if (ne != null)
2670                                 return ne;
2671
2672                         //
2673                         // Unboxing conversion.
2674                         //
2675                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
2676                                 return new UnboxCast (expr, target_type);
2677
2678                         //
2679                         // Enum types
2680                         //
2681                         if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2682                                 Expression e;
2683
2684                                 //
2685                                 // FIXME: Is there any reason we should have EnumConstant
2686                                 // dealt with here instead of just using always the
2687                                 // UnderlyingSystemType to wrap the type?
2688                                 //
2689                                 if (expr is EnumConstant)
2690                                         e = ((EnumConstant) expr).Child;
2691                                 else {
2692                                         e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2693                                 }
2694                                 
2695                                 Expression t = ConvertImplicit (ec, e, target_type, loc);
2696                                 if (t != null)
2697                                         return t;
2698                                 
2699                                 t = ConvertNumericExplicit (ec, e, target_type, loc);
2700                                 if (t != null)
2701                                         return t;
2702                                 
2703                                 t = NarrowingConversion (ec, e, target_type, loc);
2704                                 if (t != null)
2705                                         return t;       
2706                                                                 
2707                                 Error_CannotConvertType (loc, expr_type, target_type);
2708                                 return null;
2709                         }
2710                         
2711                         ne = ConvertReferenceExplicit (expr, target_type);
2712                         if (ne != null)
2713                                 return ne;
2714
2715                         if (ec.InUnsafe){
2716                                 if (target_type.IsPointer){
2717                                         if (expr_type.IsPointer)
2718                                                 return new EmptyCast (expr, target_type);
2719                                         
2720                                         if (expr_type == TypeManager.sbyte_type ||
2721                                             expr_type == TypeManager.byte_type ||
2722                                             expr_type == TypeManager.short_type ||
2723                                             expr_type == TypeManager.ushort_type ||
2724                                             expr_type == TypeManager.int32_type ||
2725                                             expr_type == TypeManager.uint32_type ||
2726                                             expr_type == TypeManager.uint64_type ||
2727                                             expr_type == TypeManager.int64_type)
2728                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2729                                 }
2730                                 if (expr_type.IsPointer){
2731                                         if (target_type == TypeManager.sbyte_type ||
2732                                             target_type == TypeManager.byte_type ||
2733                                             target_type == TypeManager.short_type ||
2734                                             target_type == TypeManager.ushort_type ||
2735                                             target_type == TypeManager.int32_type ||
2736                                             target_type == TypeManager.uint32_type ||
2737                                             target_type == TypeManager.uint64_type ||
2738                                             target_type == TypeManager.int64_type){
2739                                                 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2740                                                 Expression ci, ce;
2741
2742                                                 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2743
2744                                                 if (ci != null)
2745                                                         return ci;
2746
2747                                                 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2748                                                 if (ce != null)
2749                                                         return ce;
2750                                                 //
2751                                                 // We should always be able to go from an uint32
2752                                                 // implicitly or explicitly to the other integral
2753                                                 // types
2754                                                 //
2755                                                 throw new Exception ("Internal compiler error");
2756                                         }
2757                                 }
2758                         }
2759                         
2760                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
2761                         if (ne != null)
2762                                 return ne;
2763
2764                         if (!(runtimeconv))     {
2765                                 ne = NarrowingConversion (ec, expr, target_type, loc);
2766                                 if (ne != null)
2767                                         return ne;
2768                                 
2769                                 Error_CannotConvertType (loc, expr_type, target_type);
2770                         }
2771                         return null;
2772                 }
2773
2774                 /// <summary>
2775                 ///   Same as ConvertExplicit, only it doesn't include user defined conversions
2776                 /// </summary>
2777                 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2778                                                                   Type target_type, Location l)
2779                 {
2780                         Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2781
2782                         if (ne != null)
2783                                 return ne;
2784
2785                         ne = ConvertNumericExplicit (ec, expr, target_type, l);
2786                         if (ne != null)
2787                                 return ne;
2788
2789                         ne = ConvertReferenceExplicit (expr, target_type);
2790                         if (ne != null)
2791                                 return ne;
2792
2793                         ne = NarrowingConversion (ec, expr, target_type, l);
2794                         if (ne != null)
2795                                 return ne;                              
2796
2797                         Error_CannotConvertType (l, expr.Type, target_type);
2798                         return null;
2799                 }
2800
2801                 static string ExprClassName (ExprClass c)
2802                 {
2803                         switch (c){
2804                         case ExprClass.Invalid:
2805                                 return "Invalid";
2806                         case ExprClass.Value:
2807                                 return "value";
2808                         case ExprClass.Variable:
2809                                 return "variable";
2810                         case ExprClass.Namespace:
2811                                 return "namespace";
2812                         case ExprClass.Type:
2813                                 return "type";
2814                         case ExprClass.MethodGroup:
2815                                 return "method group";
2816                         case ExprClass.PropertyAccess:
2817                                 return "property access";
2818                         case ExprClass.EventAccess:
2819                                 return "event access";
2820                         case ExprClass.IndexerAccess:
2821                                 return "indexer access";
2822                         case ExprClass.Nothing:
2823                                 return "null";
2824                         }
2825                         throw new Exception ("Should not happen");
2826                 }
2827                 
2828                 /// <summary>
2829                 ///   Reports that we were expecting 'expr' to be of class 'expected'
2830                 /// </summary>
2831                 public void Error118 (string expected)
2832                 {
2833                         string kind = "Unknown";
2834                         
2835                         kind = ExprClassName (eclass);
2836
2837                         Error (118, "Expression denotes a '" + kind +
2838                                "' where a '" + expected + "' was expected");
2839                 }
2840
2841                 public void Error118 (ResolveFlags flags)
2842                 {
2843                         ArrayList valid = new ArrayList (10);
2844
2845                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
2846                                 valid.Add ("variable");
2847                                 valid.Add ("value");
2848                         }
2849
2850                         if ((flags & ResolveFlags.Type) != 0)
2851                                 valid.Add ("type");
2852
2853                         if ((flags & ResolveFlags.MethodGroup) != 0)
2854                                 valid.Add ("method group");
2855
2856                         if ((flags & ResolveFlags.SimpleName) != 0)
2857                                 valid.Add ("simple name");
2858
2859                         if (valid.Count == 0)
2860                                 valid.Add ("unknown");
2861
2862                         StringBuilder sb = new StringBuilder ();
2863                         for (int i = 0; i < valid.Count; i++) {
2864                                 if (i > 0)
2865                                         sb.Append (", ");
2866                                 else if (i == valid.Count)
2867                                         sb.Append (" or ");
2868                                 sb.Append (valid [i]);
2869                         }
2870
2871                         string kind = ExprClassName (eclass);
2872
2873                         Error (119, "Expression denotes a '" + kind + "' where " +
2874                                "a '" + sb.ToString () + "' was expected");
2875                 }
2876                 
2877                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2878                 {
2879                         Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2880                                       TypeManager.MonoBASIC_Name (t));
2881                 }
2882
2883                 public static void UnsafeError (Location loc)
2884                 {
2885                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2886                 }
2887                 
2888                 /// <summary>
2889                 ///   Converts the IntConstant, UIntConstant, LongConstant or
2890                 ///   ULongConstant into the integral target_type.   Notice
2891                 ///   that we do not return an 'Expression' we do return
2892                 ///   a boxed integral type.
2893                 ///
2894                 ///   FIXME: Since I added the new constants, we need to
2895                 ///   also support conversions from CharConstant, ByteConstant,
2896                 ///   SByteConstant, UShortConstant, ShortConstant
2897                 ///
2898                 ///   This is used by the switch statement, so the domain
2899                 ///   of work is restricted to the literals above, and the
2900                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
2901                 ///   short, uint64 and int64
2902                 /// </summary>
2903                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2904                 {
2905                         string s = "";
2906
2907                         if (c.Type == target_type)
2908                                 return ((Constant) c).GetValue ();
2909
2910                         //
2911                         // Make into one of the literals we handle, we dont really care
2912                         // about this value as we will just return a few limited types
2913                         // 
2914                         if (c is EnumConstant)
2915                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
2916
2917                         if (c is IntConstant){
2918                                 int v = ((IntConstant) c).Value;
2919                                 
2920                                 if (target_type == TypeManager.uint32_type){
2921                                         if (v >= 0)
2922                                                 return (uint) v;
2923                                 } else if (target_type == TypeManager.char_type){
2924                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2925                                                 return (char) v;
2926                                 } else if (target_type == TypeManager.byte_type){
2927                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2928                                                 return (byte) v;
2929                                 } else if (target_type == TypeManager.sbyte_type){
2930                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2931                                                 return (sbyte) v;
2932                                 } else if (target_type == TypeManager.short_type){
2933                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2934                                                 return (short) v;
2935                                 } else if (target_type == TypeManager.ushort_type){
2936                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2937                                                 return (ushort) v;
2938                                 } else if (target_type == TypeManager.int64_type)
2939                                         return (long) v;
2940                                 else if (target_type == TypeManager.uint64_type){
2941                                         if (v > 0)
2942                                                 return (ulong) v;
2943                                 }
2944
2945                                 s = v.ToString ();
2946                         } else if (c is UIntConstant){
2947                                 uint v = ((UIntConstant) c).Value;
2948
2949                                 if (target_type == TypeManager.int32_type){
2950                                         if (v <= Int32.MaxValue)
2951                                                 return (int) v;
2952                                 } else if (target_type == TypeManager.char_type){
2953                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2954                                                 return (char) v;
2955                                 } else if (target_type == TypeManager.byte_type){
2956                                         if (v <= Byte.MaxValue)
2957                                                 return (byte) v;
2958                                 } else if (target_type == TypeManager.sbyte_type){
2959                                         if (v <= SByte.MaxValue)
2960                                                 return (sbyte) v;
2961                                 } else if (target_type == TypeManager.short_type){
2962                                         if (v <= UInt16.MaxValue)
2963                                                 return (short) v;
2964                                 } else if (target_type == TypeManager.ushort_type){
2965                                         if (v <= UInt16.MaxValue)
2966                                                 return (ushort) v;
2967                                 } else if (target_type == TypeManager.int64_type)
2968                                         return (long) v;
2969                                 else if (target_type == TypeManager.uint64_type)
2970                                         return (ulong) v;
2971                                 s = v.ToString ();
2972                         } else if (c is LongConstant){ 
2973                                 long v = ((LongConstant) c).Value;
2974
2975                                 if (target_type == TypeManager.int32_type){
2976                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2977                                                 return (int) v;
2978                                 } else if (target_type == TypeManager.uint32_type){
2979                                         if (v >= 0 && v <= UInt32.MaxValue)
2980                                                 return (uint) v;
2981                                 } else if (target_type == TypeManager.char_type){
2982                                         if (v >= Char.MinValue && v <= Char.MaxValue)
2983                                                 return (char) v;
2984                                 } else if (target_type == TypeManager.byte_type){
2985                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
2986                                                 return (byte) v;
2987                                 } else if (target_type == TypeManager.sbyte_type){
2988                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
2989                                                 return (sbyte) v;
2990                                 } else if (target_type == TypeManager.short_type){
2991                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2992                                                 return (short) v;
2993                                 } else if (target_type == TypeManager.ushort_type){
2994                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2995                                                 return (ushort) v;
2996                                 } else if (target_type == TypeManager.uint64_type){
2997                                         if (v > 0)
2998                                                 return (ulong) v;
2999                                 }
3000                                 s = v.ToString ();
3001                         } else if (c is ULongConstant){
3002                                 ulong v = ((ULongConstant) c).Value;
3003
3004                                 if (target_type == TypeManager.int32_type){
3005                                         if (v <= Int32.MaxValue)
3006                                                 return (int) v;
3007                                 } else if (target_type == TypeManager.uint32_type){
3008                                         if (v <= UInt32.MaxValue)
3009                                                 return (uint) v;
3010                                 } else if (target_type == TypeManager.char_type){
3011                                         if (v >= Char.MinValue && v <= Char.MaxValue)
3012                                                 return (char) v;
3013                                 } else if (target_type == TypeManager.byte_type){
3014                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
3015                                                 return (byte) v;
3016                                 } else if (target_type == TypeManager.sbyte_type){
3017                                         if (v <= (int) SByte.MaxValue)
3018                                                 return (sbyte) v;
3019                                 } else if (target_type == TypeManager.short_type){
3020                                         if (v <= UInt16.MaxValue)
3021                                                 return (short) v;
3022                                 } else if (target_type == TypeManager.ushort_type){
3023                                         if (v <= UInt16.MaxValue)
3024                                                 return (ushort) v;
3025                                 } else if (target_type == TypeManager.int64_type){
3026                                         if (v <= Int64.MaxValue)
3027                                                 return (long) v;
3028                                 }
3029                                 s = v.ToString ();
3030                         } else if (c is ByteConstant){
3031                                 byte v = ((ByteConstant) c).Value;
3032                                 
3033                                 if (target_type == TypeManager.int32_type)
3034                                         return (int) v;
3035                                 else if (target_type == TypeManager.uint32_type)
3036                                         return (uint) v;
3037                                 else if (target_type == TypeManager.char_type)
3038                                         return (char) v;
3039                                 else if (target_type == TypeManager.sbyte_type){
3040                                         if (v <= SByte.MaxValue)
3041                                                 return (sbyte) v;
3042                                 } else if (target_type == TypeManager.short_type)
3043                                         return (short) v;
3044                                 else if (target_type == TypeManager.ushort_type)
3045                                         return (ushort) v;
3046                                 else if (target_type == TypeManager.int64_type)
3047                                         return (long) v;
3048                                 else if (target_type == TypeManager.uint64_type)
3049                                         return (ulong) v;
3050                                 s = v.ToString ();
3051                         } else if (c is SByteConstant){
3052                                 sbyte v = ((SByteConstant) c).Value;
3053                                 
3054                                 if (target_type == TypeManager.int32_type)
3055                                         return (int) v;
3056                                 else if (target_type == TypeManager.uint32_type){
3057                                         if (v >= 0)
3058                                                 return (uint) v;
3059                                 } else if (target_type == TypeManager.char_type){
3060                                         if (v >= 0)
3061                                                 return (char) v;
3062                                 } else if (target_type == TypeManager.byte_type){
3063                                         if (v >= 0)
3064                                                 return (byte) v;
3065                                 } else if (target_type == TypeManager.short_type)
3066                                         return (short) v;
3067                                 else if (target_type == TypeManager.ushort_type){
3068                                         if (v >= 0)
3069                                                 return (ushort) v;
3070                                 } else if (target_type == TypeManager.int64_type)
3071                                         return (long) v;
3072                                 else if (target_type == TypeManager.uint64_type){
3073                                         if (v >= 0)
3074                                                 return (ulong) v;
3075                                 }
3076                                 s = v.ToString ();
3077                         } else if (c is ShortConstant){
3078                                 short v = ((ShortConstant) c).Value;
3079                                 
3080                                 if (target_type == TypeManager.int32_type){
3081                                         return (int) v;
3082                                 } else if (target_type == TypeManager.uint32_type){
3083                                         if (v >= 0)
3084                                                 return (uint) v;
3085                                 } else if (target_type == TypeManager.char_type){
3086                                         if (v >= 0)
3087                                                 return (char) v;
3088                                 } else if (target_type == TypeManager.byte_type){
3089                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
3090                                                 return (byte) v;
3091                                 } else if (target_type == TypeManager.sbyte_type){
3092                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
3093                                                 return (sbyte) v;
3094                                 } else if (target_type == TypeManager.ushort_type){
3095                                         if (v >= 0)
3096                                                 return (ushort) v;
3097                                 } else if (target_type == TypeManager.int64_type)
3098                                         return (long) v;
3099                                 else if (target_type == TypeManager.uint64_type)
3100                                         return (ulong) v;
3101
3102                                 s = v.ToString ();
3103                         } else if (c is UShortConstant){
3104                                 ushort v = ((UShortConstant) c).Value;
3105                                 
3106                                 if (target_type == TypeManager.int32_type)
3107                                         return (int) v;
3108                                 else if (target_type == TypeManager.uint32_type)
3109                                         return (uint) v;
3110                                 else if (target_type == TypeManager.char_type){
3111                                         if (v >= Char.MinValue && v <= Char.MaxValue)
3112                                                 return (char) v;
3113                                 } else if (target_type == TypeManager.byte_type){
3114                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
3115                                                 return (byte) v;
3116                                 } else if (target_type == TypeManager.sbyte_type){
3117                                         if (v <= SByte.MaxValue)
3118                                                 return (byte) v;
3119                                 } else if (target_type == TypeManager.short_type){
3120                                         if (v <= Int16.MaxValue)
3121                                                 return (short) v;
3122                                 } else if (target_type == TypeManager.int64_type)
3123                                         return (long) v;
3124                                 else if (target_type == TypeManager.uint64_type)
3125                                         return (ulong) v;
3126
3127                                 s = v.ToString ();
3128                         } else if (c is CharConstant){
3129                                 char v = ((CharConstant) c).Value;
3130                                 
3131                                 if (target_type == TypeManager.int32_type)
3132                                         return (int) v;
3133                                 else if (target_type == TypeManager.uint32_type)
3134                                         return (uint) v;
3135                                 else if (target_type == TypeManager.byte_type){
3136                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
3137                                                 return (byte) v;
3138                                 } else if (target_type == TypeManager.sbyte_type){
3139                                         if (v <= SByte.MaxValue)
3140                                                 return (sbyte) v;
3141                                 } else if (target_type == TypeManager.short_type){
3142                                         if (v <= Int16.MaxValue)
3143                                                 return (short) v;
3144                                 } else if (target_type == TypeManager.ushort_type)
3145                                         return (short) v;
3146                                 else if (target_type == TypeManager.int64_type)
3147                                         return (long) v;
3148                                 else if (target_type == TypeManager.uint64_type)
3149                                         return (ulong) v;
3150
3151                                 s = v.ToString ();
3152                         }
3153                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
3154                         return null;
3155                 }
3156
3157                 //
3158                 // Load the object from the pointer.  
3159                 //
3160                 public static void LoadFromPtr (ILGenerator ig, Type t)
3161                 {
3162                         if (t == TypeManager.int32_type)
3163                                 ig.Emit (OpCodes.Ldind_I4);
3164                         else if (t == TypeManager.uint32_type)
3165                                 ig.Emit (OpCodes.Ldind_U4);
3166                         else if (t == TypeManager.short_type)
3167                                 ig.Emit (OpCodes.Ldind_I2);
3168                         else if (t == TypeManager.ushort_type)
3169                                 ig.Emit (OpCodes.Ldind_U2);
3170                         else if (t == TypeManager.char_type)
3171                                 ig.Emit (OpCodes.Ldind_U2);
3172                         else if (t == TypeManager.byte_type)
3173                                 ig.Emit (OpCodes.Ldind_U1);
3174                         else if (t == TypeManager.sbyte_type)
3175                                 ig.Emit (OpCodes.Ldind_I1);
3176                         else if (t == TypeManager.uint64_type)
3177                                 ig.Emit (OpCodes.Ldind_I8);
3178                         else if (t == TypeManager.int64_type)
3179                                 ig.Emit (OpCodes.Ldind_I8);
3180                         else if (t == TypeManager.float_type)
3181                                 ig.Emit (OpCodes.Ldind_R4);
3182                         else if (t == TypeManager.double_type)
3183                                 ig.Emit (OpCodes.Ldind_R8);
3184                         else if (t == TypeManager.bool_type)
3185                                 ig.Emit (OpCodes.Ldind_I1);
3186                         else if (t == TypeManager.intptr_type)
3187                                 ig.Emit (OpCodes.Ldind_I);
3188                         else if (TypeManager.IsEnumType (t)) {
3189                                 if (t == TypeManager.enum_type)
3190                                         ig.Emit (OpCodes.Ldind_Ref);
3191                                 else
3192                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
3193                         } else if (t.IsValueType)
3194                                 ig.Emit (OpCodes.Ldobj, t);
3195                         else
3196                                 ig.Emit (OpCodes.Ldind_Ref);
3197                 }
3198
3199                 //
3200                 // The stack contains the pointer and the value of type 'type'
3201                 //
3202                 public static void StoreFromPtr (ILGenerator ig, Type type)
3203                 {
3204                         if (TypeManager.IsEnumType (type))
3205                                 type = TypeManager.EnumToUnderlying (type);
3206                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
3207                                 ig.Emit (OpCodes.Stind_I4);
3208                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
3209                                 ig.Emit (OpCodes.Stind_I8);
3210                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
3211                                  type == TypeManager.ushort_type)
3212                                 ig.Emit (OpCodes.Stind_I2);
3213                         else if (type == TypeManager.float_type)
3214                                 ig.Emit (OpCodes.Stind_R4);
3215                         else if (type == TypeManager.double_type)
3216                                 ig.Emit (OpCodes.Stind_R8);
3217                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
3218                                  type == TypeManager.bool_type)
3219                                 ig.Emit (OpCodes.Stind_I1);
3220                         else if (type == TypeManager.intptr_type)
3221                                 ig.Emit (OpCodes.Stind_I);
3222                         else if (type.IsValueType)
3223                                 ig.Emit (OpCodes.Stobj, type);
3224                         else
3225                                 ig.Emit (OpCodes.Stind_Ref);
3226                 }
3227                 
3228                 //
3229                 // Returns the size of type 't' if known, otherwise, 0
3230                 //
3231                 public static int GetTypeSize (Type t)
3232                 {
3233                         t = TypeManager.TypeToCoreType (t);
3234                         if (t == TypeManager.int32_type ||
3235                             t == TypeManager.uint32_type ||
3236                             t == TypeManager.float_type)
3237                                 return 4;
3238                         else if (t == TypeManager.int64_type ||
3239                                  t == TypeManager.uint64_type ||
3240                                  t == TypeManager.double_type)
3241                                 return 8;
3242                         else if (t == TypeManager.byte_type ||
3243                                  t == TypeManager.sbyte_type ||
3244                                  t == TypeManager.bool_type)    
3245                                 return 1;
3246                         else if (t == TypeManager.short_type ||
3247                                  t == TypeManager.char_type ||
3248                                  t == TypeManager.ushort_type)
3249                                 return 2;
3250                         else if (t == TypeManager.decimal_type)
3251                                 return 16;
3252                         else
3253                                 return 0;
3254                 }
3255
3256                 //
3257                 // Default implementation of IAssignMethod.CacheTemporaries
3258                 //
3259                 public void CacheTemporaries (EmitContext ec)
3260                 {
3261                 }
3262
3263                 static void Error_NegativeArrayIndex (Location loc)
3264                 {
3265                         Report.Error (284, loc, "Can not create array with a negative size");
3266                 }
3267                 
3268                 //
3269                 // Converts 'source' to an int, uint, long or ulong.
3270                 //
3271                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
3272                 {
3273                         Expression target;
3274                         
3275                         bool old_checked = ec.CheckState;
3276                         ec.CheckState = true;
3277                         
3278                         target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
3279                         if (target == null){
3280                                 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
3281                                 if (target == null){
3282                                         target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
3283                                         if (target == null){
3284                                                 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
3285                                                 if (target == null)
3286                                                         Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
3287                                         }
3288                                 }
3289                         } 
3290                         ec.CheckState = old_checked;
3291
3292                         //
3293                         // Only positive constants are allowed at compile time
3294                         //
3295                         if (target is Constant){
3296                                 if (target is IntConstant){
3297                                         if (((IntConstant) target).Value < 0){
3298                                                 Error_NegativeArrayIndex (loc);
3299                                                 return null;
3300                                         }
3301                                 }
3302
3303                                 if (target is LongConstant){
3304                                         if (((LongConstant) target).Value < 0){
3305                                                 Error_NegativeArrayIndex (loc);
3306                                                 return null;
3307                                         }
3308                                 }
3309                                 
3310                         }
3311
3312                         return target;
3313                 }
3314                 
3315         }
3316
3317         /// <summary>
3318         ///   This is just a base class for expressions that can
3319         ///   appear on statements (invocations, object creation,
3320         ///   assignments, post/pre increment and decrement).  The idea
3321         ///   being that they would support an extra Emition interface that
3322         ///   does not leave a result on the stack.
3323         /// </summary>
3324         public abstract class ExpressionStatement : Expression {
3325
3326                 /// <summary>
3327                 ///   Requests the expression to be emitted in a 'statement'
3328                 ///   context.  This means that no new value is left on the
3329                 ///   stack after invoking this method (constrasted with
3330                 ///   Emit that will always leave a value on the stack).
3331                 /// </summary>
3332                 public abstract void EmitStatement (EmitContext ec);
3333         }
3334
3335         /// <summary>
3336         ///   This kind of cast is used to encapsulate the child
3337         ///   whose type is child.Type into an expression that is
3338         ///   reported to return "return_type".  This is used to encapsulate
3339         ///   expressions which have compatible types, but need to be dealt
3340         ///   at higher levels with.
3341         ///
3342         ///   For example, a "byte" expression could be encapsulated in one
3343         ///   of these as an "unsigned int".  The type for the expression
3344         ///   would be "unsigned int".
3345         ///
3346         /// </summary>
3347         public class EmptyCast : Expression {
3348                 protected Expression child;
3349
3350                 public EmptyCast (Expression child, Type return_type)
3351                 {
3352                         eclass = child.eclass;
3353                         type = return_type;
3354                         this.child = child;
3355                 }
3356
3357                 public override Expression DoResolve (EmitContext ec)
3358                 {
3359                         // This should never be invoked, we are born in fully
3360                         // initialized state.
3361
3362                         return this;
3363                 }
3364
3365                 public override void Emit (EmitContext ec)
3366                 {
3367                         child.Emit (ec);
3368                 }
3369         }
3370
3371         /// <summary>
3372         ///  This class is used to wrap literals which belong inside Enums
3373         /// </summary>
3374         public class EnumConstant : Constant {
3375                 public Constant Child;
3376
3377                 public EnumConstant (Constant child, Type enum_type)
3378                 {
3379                         eclass = child.eclass;
3380                         this.Child = child;
3381                         type = enum_type;
3382                 }
3383                 
3384                 public override Expression DoResolve (EmitContext ec)
3385                 {
3386                         // This should never be invoked, we are born in fully
3387                         // initialized state.
3388
3389                         return this;
3390                 }
3391
3392                 public override void Emit (EmitContext ec)
3393                 {
3394                         Child.Emit (ec);
3395                 }
3396
3397                 public override object GetValue ()
3398                 {
3399                         return Child.GetValue ();
3400                 }
3401
3402                 //
3403                 // Converts from one of the valid underlying types for an enumeration
3404                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3405                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3406                 //
3407                 public Constant WidenToCompilerConstant ()
3408                 {
3409                         Type t = TypeManager.EnumToUnderlying (Child.Type);
3410                         object v = ((Constant) Child).GetValue ();;
3411                         
3412                         if (t == TypeManager.int32_type)
3413                                 return new IntConstant ((int) v);
3414                         if (t == TypeManager.uint32_type)
3415                                 return new UIntConstant ((uint) v);
3416                         if (t == TypeManager.int64_type)
3417                                 return new LongConstant ((long) v);
3418                         if (t == TypeManager.uint64_type)
3419                                 return new ULongConstant ((ulong) v);
3420                         if (t == TypeManager.short_type)
3421                                 return new ShortConstant ((short) v);
3422                         if (t == TypeManager.ushort_type)
3423                                 return new UShortConstant ((ushort) v);
3424                         if (t == TypeManager.byte_type)
3425                                 return new ByteConstant ((byte) v);
3426                         if (t == TypeManager.sbyte_type)
3427                                 return new SByteConstant ((sbyte) v);
3428
3429                         throw new Exception ("Invalid enumeration underlying type: " + t);
3430                 }
3431
3432                 //
3433                 // Extracts the value in the enumeration on its native representation
3434                 //
3435                 public object GetPlainValue ()
3436                 {
3437                         Type t = TypeManager.EnumToUnderlying (Child.Type);
3438                         object v = ((Constant) Child).GetValue ();;
3439                         
3440                         if (t == TypeManager.int32_type)
3441                                 return (int) v;
3442                         if (t == TypeManager.uint32_type)
3443                                 return (uint) v;
3444                         if (t == TypeManager.int64_type)
3445                                 return (long) v;
3446                         if (t == TypeManager.uint64_type)
3447                                 return (ulong) v;
3448                         if (t == TypeManager.short_type)
3449                                 return (short) v;
3450                         if (t == TypeManager.ushort_type)
3451                                 return (ushort) v;
3452                         if (t == TypeManager.byte_type)
3453                                 return (byte) v;
3454                         if (t == TypeManager.sbyte_type)
3455                                 return (sbyte) v;
3456
3457                         return null;
3458                 }
3459                 
3460                 public override string AsString ()
3461                 {
3462                         return Child.AsString ();
3463                 }
3464
3465                 public override DoubleConstant ConvertToDouble ()
3466                 {
3467                         return Child.ConvertToDouble ();
3468                 }
3469
3470                 public override FloatConstant ConvertToFloat ()
3471                 {
3472                         return Child.ConvertToFloat ();
3473                 }
3474
3475                 public override ULongConstant ConvertToULong ()
3476                 {
3477                         return Child.ConvertToULong ();
3478                 }
3479
3480                 public override LongConstant ConvertToLong ()
3481                 {
3482                         return Child.ConvertToLong ();
3483                 }
3484
3485                 public override UIntConstant ConvertToUInt ()
3486                 {
3487                         return Child.ConvertToUInt ();
3488                 }
3489
3490                 public override IntConstant ConvertToInt ()
3491                 {
3492                         return Child.ConvertToInt ();
3493                 }
3494         }
3495
3496         /// <summary>
3497         ///   This kind of cast is used to encapsulate Value Types in objects.
3498         ///
3499         ///   The effect of it is to box the value type emitted by the previous
3500         ///   operation.
3501         /// </summary>
3502         public class BoxedCast : EmptyCast {
3503
3504                 public BoxedCast (Expression expr)
3505                         : base (expr, TypeManager.object_type)
3506                 {
3507                 }
3508
3509                 public override Expression DoResolve (EmitContext ec)
3510                 {
3511                         // This should never be invoked, we are born in fully
3512                         // initialized state.
3513
3514                         return this;
3515                 }
3516
3517                 public override void Emit (EmitContext ec)
3518                 {
3519                         base.Emit (ec);
3520                         
3521                         ec.ig.Emit (OpCodes.Box, child.Type);
3522                 }
3523         }
3524
3525         public class UnboxCast : EmptyCast {
3526                 public UnboxCast (Expression expr, Type return_type)
3527                         : base (expr, return_type)
3528                 {
3529                 }
3530
3531                 public override Expression DoResolve (EmitContext ec)
3532                 {
3533                         // This should never be invoked, we are born in fully
3534                         // initialized state.
3535
3536                         return this;
3537                 }
3538
3539                 public override void Emit (EmitContext ec)
3540                 {
3541                         Type t = type;
3542                         ILGenerator ig = ec.ig;
3543                         
3544                         base.Emit (ec);
3545                         ig.Emit (OpCodes.Unbox, t);
3546
3547                         LoadFromPtr (ig, t);
3548                 }
3549         }
3550         
3551         /// <summary>
3552         ///   This is used to perform explicit numeric conversions.
3553         ///
3554         ///   Explicit numeric conversions might trigger exceptions in a checked
3555         ///   context, so they should generate the conv.ovf opcodes instead of
3556         ///   conv opcodes.
3557         /// </summary>
3558         public class ConvCast : EmptyCast {
3559                 public enum Mode : byte {
3560                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3561                         U1_I1, U1_CH,
3562                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3563                         U2_I1, U2_U1, U2_I2, U2_CH,
3564                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3565                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3566                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3567                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3568                         CH_I1, CH_U1, CH_I2,
3569                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3570                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3571                 }
3572
3573                 Mode mode;
3574                 bool checked_state;
3575                 
3576                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3577                         : base (child, return_type)
3578                 {
3579                         checked_state = ec.CheckState;
3580                         mode = m;
3581                 }
3582
3583                 public override Expression DoResolve (EmitContext ec)
3584                 {
3585                         // This should never be invoked, we are born in fully
3586                         // initialized state.
3587
3588                         return this;
3589                 }
3590
3591                 public override void Emit (EmitContext ec)
3592                 {
3593                         ILGenerator ig = ec.ig;
3594                         
3595                         base.Emit (ec);
3596
3597                         if (checked_state){
3598                                 switch (mode){
3599                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3600                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3601                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3602                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3603                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3604
3605                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3606                                 case Mode.U1_CH: /* nothing */ break;
3607
3608                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3609                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3610                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3611                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3612                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3613                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3614
3615                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3616                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3617                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3618                                 case Mode.U2_CH: /* nothing */ break;
3619
3620                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3621                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3622                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3623                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3624                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3625                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3626                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3627
3628                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3629                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3630                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3631                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3632                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3633                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3634
3635                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3636                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3637                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3638                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3639                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3640                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3641                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3642                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3643
3644                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3645                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3646                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3647                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3648                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3649                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3650                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3651                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3652
3653                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3654                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3655                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3656
3657                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3658                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3659                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3660                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3661                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3662                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3663                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3664                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3665                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3666
3667                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3668                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3669                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3670                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3671                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3672                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3673                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3674                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3675                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3676                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3677                                 }
3678                         } else {
3679                                 switch (mode){
3680                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3681                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3682                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3683                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3684                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3685
3686                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3687                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3688
3689                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3690                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3691                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3692                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3693                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3694                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3695
3696                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3697                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3698                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3699                                 case Mode.U2_CH: /* nothing */ break;
3700
3701                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3702                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3703                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3704                                 case Mode.I4_U4: /* nothing */ break;
3705                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3706                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3707                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3708
3709                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3710                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3711                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3712                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3713                                 case Mode.U4_I4: /* nothing */ break;
3714                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3715
3716                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3717                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3718                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3719                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3720                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3721                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3722                                 case Mode.I8_U8: /* nothing */ break;
3723                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3724
3725                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3726                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3727                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3728                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3729                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3730                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3731                                 case Mode.U8_I8: /* nothing */ break;
3732                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3733
3734                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3735                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3736                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3737
3738                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3739                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3740                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3741                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3742                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3743                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3744                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3745                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3746                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3747
3748                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3749                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3750                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3751                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3752                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3753                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3754                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3755                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3756                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3757                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3758                                 }
3759                         }
3760                 }
3761         }
3762         
3763         public class OpcodeCast : EmptyCast {
3764                 OpCode op, op2;
3765                 bool second_valid;
3766                 
3767                 public OpcodeCast (Expression child, Type return_type, OpCode op)
3768                         : base (child, return_type)
3769                         
3770                 {
3771                         
3772                         this.op = op;
3773                         second_valid = false;
3774                 }
3775
3776                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3777                         : base (child, return_type)
3778                         
3779                 {
3780                         this.op = op;
3781                         this.op2 = op2;
3782                         second_valid = true;
3783                 }
3784
3785                 public override Expression DoResolve (EmitContext ec)
3786                 {
3787                         // This should never be invoked, we are born in fully
3788                         // initialized state.
3789
3790                         return this;
3791                 }
3792
3793                 public override void Emit (EmitContext ec)
3794                 {
3795                         base.Emit (ec);
3796                         ec.ig.Emit (op);
3797
3798                         if (second_valid)
3799                                 ec.ig.Emit (op2);
3800                 }                       
3801         }
3802
3803
3804         public class NumericToBoolCast : EmptyCast 
3805         {
3806                 Type src_type;
3807                 
3808                 public NumericToBoolCast (Expression src, Type src_type)
3809                         : base (src, TypeManager.bool_type)
3810                         
3811                 {
3812                         this.src_type = src_type;
3813                 }
3814
3815                 public override Expression DoResolve (EmitContext ec)
3816                 {
3817                         return this;
3818                 }
3819
3820                 public override void Emit (EmitContext ec)
3821                 {
3822                         base.Emit (ec);
3823
3824                         if (src_type == TypeManager.byte_type ||
3825                                 src_type == TypeManager.short_type ||
3826                                 src_type == TypeManager.int32_type) {
3827                                 
3828                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
3829                                 ec.ig.Emit (OpCodes.Cgt_Un);
3830                                 return;
3831                         } 
3832
3833                         if (src_type == TypeManager.int64_type) {
3834                                 ec.ig.Emit (OpCodes.Ldc_I8, (long) 0);
3835                                 ec.ig.Emit (OpCodes.Cgt_Un);
3836                                 return;
3837                         } 
3838
3839                         if (src_type == TypeManager.float_type) {
3840                                 ec.ig.Emit (OpCodes.Ldc_R4, (float) 0);
3841                                 ec.ig.Emit (OpCodes.Ceq);
3842                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
3843                                 ec.ig.Emit (OpCodes.Ceq);
3844                                 return;
3845                         } 
3846
3847                         if (src_type == TypeManager.double_type) {
3848                                 ec.ig.Emit (OpCodes.Ldc_R8, (double) 0);
3849                                 ec.ig.Emit (OpCodes.Ceq);
3850                                 ec.ig.Emit (OpCodes.Ldc_I4_0);
3851                                 ec.ig.Emit (OpCodes.Ceq);
3852                                 return;
3853                         }
3854                 }                       
3855         }
3856
3857         /// <summary>
3858         ///   This kind of cast is used to encapsulate a child and cast it
3859         ///   to the class requested
3860         /// </summary>
3861         public class ClassCast : EmptyCast {
3862                 public ClassCast (Expression child, Type return_type)
3863                         : base (child, return_type)
3864                         
3865                 {
3866                 }
3867
3868                 public override Expression DoResolve (EmitContext ec)
3869                 {
3870                         // This should never be invoked, we are born in fully
3871                         // initialized state.
3872
3873                         return this;
3874                 }
3875
3876                 public override void Emit (EmitContext ec)
3877                 {
3878                         base.Emit (ec);
3879
3880                         ec.ig.Emit (OpCodes.Castclass, type);
3881                 }                       
3882                 
3883         }
3884         
3885         /// <summary>
3886         ///   SimpleName expressions are initially formed of a single
3887         ///   word and it only happens at the beginning of the expression.
3888         /// </summary>
3889         ///
3890         /// <remarks>
3891         ///   The expression will try to be bound to a Field, a Method
3892         ///   group or a Property.  If those fail we pass the name to our
3893         ///   caller and the SimpleName is compounded to perform a type
3894         ///   lookup.  The idea behind this process is that we want to avoid
3895         ///   creating a namespace map from the assemblies, as that requires
3896         ///   the GetExportedTypes function to be called and a hashtable to
3897         ///   be constructed which reduces startup time.  If later we find
3898         ///   that this is slower, we should create a 'NamespaceExpr' expression
3899         ///   that fully participates in the resolution process. 
3900         ///   
3901         ///   For example 'System.Console.WriteLine' is decomposed into
3902         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3903         ///   
3904         ///   The first SimpleName wont produce a match on its own, so it will
3905         ///   be turned into:
3906         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
3907         ///   
3908         ///   System.Console will produce a TypeExpr match.
3909         ///   
3910         ///   The downside of this is that we might be hitting 'LookupType' too many
3911         ///   times with this scheme.
3912         /// </remarks>
3913         public class SimpleName : Expression, ITypeExpression {
3914                 public readonly string Name;
3915                 
3916                 public SimpleName (string name, Location l)
3917                 {
3918                         Name = name;
3919                         loc = l;
3920                 }
3921
3922                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3923                 {
3924                         if (ec.IsFieldInitializer)
3925                                 Report.Error (
3926                                         236, l,
3927                                         "A field initializer cannot reference the non-static field, " +
3928                                         "method or property '"+name+"'");
3929                         else
3930                                 Report.Error (
3931                                         120, l,
3932                                         "An object reference is required " +
3933                                         "for the non-static field '"+name+"'");
3934                 }
3935                 
3936                 //
3937                 // Checks whether we are trying to access an instance
3938                 // property, method or field from a static body.
3939                 //
3940                 Expression MemberStaticCheck (EmitContext ec, Expression e)
3941                 {
3942                         if (e is IMemberExpr){
3943                                 IMemberExpr member = (IMemberExpr) e;
3944                                 
3945                                 if (!member.IsStatic){
3946                                         Error_ObjectRefRequired (ec, loc, Name);
3947                                         return null;
3948                                 }
3949                         }
3950
3951                         return e;
3952                 }
3953                 
3954                 public override Expression DoResolve (EmitContext ec)
3955                 {
3956                         return SimpleNameResolve (ec, null, false);
3957                 }
3958
3959                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3960                 {
3961                         return SimpleNameResolve (ec, right_side, false);
3962                 }
3963                 
3964
3965                 public Expression DoResolveAllowStatic (EmitContext ec)
3966                 {
3967                         return SimpleNameResolve (ec, null, true);
3968                 }
3969
3970                 public Expression DoResolveType (EmitContext ec)
3971                 {
3972                         //
3973                         // Stage 3: Lookup symbol in the various namespaces. 
3974                         //
3975                         DeclSpace ds = ec.DeclSpace;
3976                         Type t;
3977                         string alias_value;
3978
3979                         if (ec.ResolvingTypeTree){
3980                                 int errors = Report.Errors;
3981                                 Type dt = ec.DeclSpace.FindType (loc, Name);
3982                                 if (Report.Errors != errors)
3983                                         return null;
3984                                 
3985                                 if (dt != null)
3986                                         return new TypeExpr (dt, loc);
3987                         }
3988
3989                         if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3990                                 return new TypeExpr (t, loc);
3991                                 
3992
3993                         //
3994                         // Stage 2 part b: Lookup up if we are an alias to a type
3995                         // or a namespace.
3996                         //
3997                         // Since we are cheating: we only do the Alias lookup for
3998                         // namespaces if the name does not include any dots in it
3999                         //
4000                                 
4001                         alias_value = ec.DeclSpace.LookupAlias (Name);
4002                                 
4003                         if (Name.IndexOf ('.') == -1 && alias_value != null) {
4004                                 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
4005                                         return new TypeExpr (t, loc);
4006                                         
4007                                 // we have alias value, but it isn't Type, so try if it's namespace
4008                                 return new SimpleName (alias_value, loc);
4009                         }
4010                                 
4011                         // No match, maybe our parent can compose us
4012                         // into something meaningful.
4013                         return this;
4014                 }
4015
4016                 /// <remarks>
4017                 ///   7.5.2: Simple Names. 
4018                 ///
4019                 ///   Local Variables and Parameters are handled at
4020                 ///   parse time, so they never occur as SimpleNames.
4021                 ///
4022                 ///   The 'allow_static' flag is used by MemberAccess only
4023                 ///   and it is used to inform us that it is ok for us to 
4024                 ///   avoid the static check, because MemberAccess might end
4025                 ///   up resolving the Name as a Type name and the access as
4026                 ///   a static type access.
4027                 ///
4028                 ///   ie: Type Type; .... { Type.GetType (""); }
4029                 ///
4030                 ///   Type is both an instance variable and a Type;  Type.GetType
4031                 ///   is the static method not an instance method of type.
4032                 /// </remarks>
4033                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
4034                 {
4035                         Expression e = null;
4036                         
4037                         //
4038                         // Stage 1: Performed by the parser (binding to locals or parameters).
4039                         //
4040                         Block current_block = ec.CurrentBlock;
4041                         if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
4042                                 LocalVariableReference var;
4043
4044                                 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
4045
4046                                 if (right_side != null)
4047                                         return var.ResolveLValue (ec, right_side);
4048                                 else
4049                                         return var.Resolve (ec);
4050                         }
4051
4052                         if (current_block != null){
4053                                 int idx = -1;
4054                                 Parameter par = null;
4055                                 Parameters pars = current_block.Parameters;
4056                                 if (pars != null)
4057                                         par = pars.GetParameterByName (Name, out idx);
4058
4059                                 if (par != null) {
4060                                         ParameterReference param;
4061                                         
4062                                         param = new ParameterReference (pars, idx, Name, loc);
4063
4064                                         if (right_side != null)
4065                                                 return param.ResolveLValue (ec, right_side);
4066                                         else
4067                                                 return param.Resolve (ec);
4068                                 }
4069                         }
4070
4071                         //
4072                         // Stage 2: Lookup members 
4073                         //
4074
4075                         //
4076                         // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
4077                         // Hence we have two different cases
4078                         //
4079
4080                         DeclSpace lookup_ds = ec.DeclSpace;
4081                         do {
4082                                 if (lookup_ds.TypeBuilder == null)
4083                                         break;
4084
4085                                 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
4086                                 if (e != null)
4087                                         break;
4088
4089                                 //
4090                                 // Classes/structs keep looking, enums break
4091                                 //
4092                                 if (lookup_ds is TypeContainer)
4093                                         lookup_ds = ((TypeContainer) lookup_ds).Parent;
4094                                 else
4095                                         break;
4096                         } while (lookup_ds != null);
4097                                 
4098                         if (e == null && ec.ContainerType != null)
4099                                 e = MemberLookup (ec, ec.ContainerType, Name, loc);
4100
4101 // #52067 - Start - Trying to solve
4102
4103                         if (e == null) {
4104                                 
4105                                 ArrayList lookups = new ArrayList();
4106                                 ArrayList typelookups = new ArrayList();
4107                                 
4108                                 int split = Name.LastIndexOf('.');
4109                                 if (split != -1) {
4110                                         String nameSpacePart = Name.Substring(0, split);
4111                                         String memberNamePart = Name.Substring(split + 1);
4112                                         foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
4113                                                 e = MemberLookup(ec, type, memberNamePart, loc);
4114                                                 if (e != null) {
4115                                                         lookups.Add(e);
4116                                                         typelookups.Add(type);
4117                                                 }
4118                                         }
4119                                 }
4120                                 
4121                                 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
4122                                 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
4123                                         e = MemberLookup(ec, type, Name, loc);
4124                                         if (e != null) {
4125                                                 lookups.Add(e);
4126                                                 typelookups.Add(type);
4127                                         }
4128                                 }
4129                                 if (lookups.Count == 1) { 
4130                                         e = (Expression)lookups[0];
4131                                 } else {
4132                                         if (lookups.Count > 1) {
4133                                                 StringBuilder sb = new StringBuilder();
4134                                                 foreach(Type type in typelookups)
4135                                                         sb.Append("'" + type.FullName + "'");                                           
4136                                                 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
4137                                                 return null;
4138                                         }
4139                                 }
4140                         }
4141
4142 // #52067 - End
4143
4144                         if (e == null)
4145                                 return DoResolveType (ec);
4146
4147                         if (e is TypeExpr)
4148                                 return e;
4149
4150                         if (e is IMemberExpr) {
4151                                 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
4152                                 if (e == null)
4153                                         return null;
4154
4155                                 IMemberExpr me = e as IMemberExpr;
4156                                 if (me == null)
4157                                         return e;
4158
4159                                 // This fails if ResolveMemberAccess() was unable to decide whether
4160                                 // it's a field or a type of the same name.
4161                                 if (!me.IsStatic && (me.InstanceExpression == null))
4162                                         return e;
4163
4164 /* FIXME    If this is not commented out, it seems that it's not possible to reach class members in mBas.
4165             Maybe a grammar-related problem?
4166
4167                                 if (!me.IsStatic &&
4168                                     TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
4169                                         Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
4170                                                "outer type '" + me.DeclaringType + "' via nested type '" +
4171                                                me.InstanceExpression.Type + "'");
4172                                         return null;
4173                                 }
4174 */
4175                                 if (right_side != null)
4176                                         e = e.DoResolveLValue (ec, right_side);
4177                                 else
4178                                         e = e.DoResolve (ec);
4179
4180                                 return e;
4181                         }
4182
4183                         if (ec.IsStatic || ec.IsFieldInitializer){
4184                                 if (allow_static)
4185                                         return e;
4186
4187                                 return MemberStaticCheck (ec, e);
4188                         } else
4189                                 return e;
4190                 }
4191                 
4192                 public override void Emit (EmitContext ec)
4193                 {
4194                         //
4195                         // If this is ever reached, then we failed to
4196                         // find the name as a namespace
4197                         //
4198
4199                         Error (30451, "The name '" + Name +
4200                                "' does not exist in the class '" +
4201                                ec.DeclSpace.Name + "'");
4202                 }
4203
4204                 public override string ToString ()
4205                 {
4206                         return Name;
4207                 }
4208         }
4209         
4210         /// <summary>
4211         ///   Fully resolved expression that evaluates to a type
4212         /// </summary>
4213         public class TypeExpr : Expression, ITypeExpression {
4214                 public TypeExpr (Type t, Location l)
4215                 {
4216                         Type = t;
4217                         eclass = ExprClass.Type;
4218                         loc = l;
4219                 }
4220
4221                 public virtual Expression DoResolveType (EmitContext ec)
4222                 {
4223                         return this;
4224                 }
4225
4226                 override public Expression DoResolve (EmitContext ec)
4227                 {
4228                         return this;
4229                 }
4230
4231                 override public void Emit (EmitContext ec)
4232                 {
4233                         throw new Exception ("Should never be called");
4234                 }
4235
4236                 public override string ToString ()
4237                 {
4238                         return Type.ToString ();
4239                 }
4240         }
4241
4242         /// <summary>
4243         ///   Used to create types from a fully qualified name.  These are just used
4244         ///   by the parser to setup the core types.  A TypeLookupExpression is always
4245         ///   classified as a type.
4246         /// </summary>
4247         public class TypeLookupExpression : TypeExpr {
4248                 string name;
4249                 
4250                 public TypeLookupExpression (string name) : base (null, Location.Null)
4251                 {
4252                         this.name = name;
4253                 }
4254
4255                 public override Expression DoResolveType (EmitContext ec)
4256                 {
4257                         if (type == null)
4258                                 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
4259                         return this;
4260                 }
4261
4262                 public override Expression DoResolve (EmitContext ec)
4263                 {
4264                         return DoResolveType (ec);
4265                 }
4266
4267                 public override void Emit (EmitContext ec)
4268                 {
4269                         throw new Exception ("Should never be called");
4270                 }
4271
4272                 public override string ToString ()
4273                 {
4274                         return name;
4275                 }
4276         }
4277
4278         /// <summary>
4279         ///   MethodGroup Expression.
4280         ///  
4281         ///   This is a fully resolved expression that evaluates to a type
4282         /// </summary>
4283         public class MethodGroupExpr : Expression, IMemberExpr {
4284                 public MethodBase [] Methods;
4285                 Expression instance_expression = null;
4286                 bool is_explicit_impl = false;
4287                 
4288                 public MethodGroupExpr (MemberInfo [] mi, Location l)
4289                 {
4290                         Methods = new MethodBase [mi.Length];
4291                         mi.CopyTo (Methods, 0);
4292                         eclass = ExprClass.MethodGroup;
4293                         type = TypeManager.object_type;
4294                         loc = l;
4295                 }
4296
4297                 public MethodGroupExpr (ArrayList list, Location l)
4298                 {
4299                         Methods = new MethodBase [list.Count];
4300
4301                         try {
4302                                 list.CopyTo (Methods, 0);
4303                         } catch {
4304                                 foreach (MemberInfo m in list){
4305                                         if (!(m is MethodBase)){
4306                                                 Console.WriteLine ("Name " + m.Name);
4307                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
4308                                         }
4309                                 }
4310                                 throw;
4311                         }
4312                         loc = l;
4313                         eclass = ExprClass.MethodGroup;
4314                         type = TypeManager.object_type;
4315                 }
4316
4317                 public Type DeclaringType {
4318                         get {
4319                                 return Methods [0].DeclaringType;
4320                         }
4321                 }
4322                 
4323                 //
4324                 // 'A method group may have associated an instance expression' 
4325                 // 
4326                 public Expression InstanceExpression {
4327                         get {
4328                                 return instance_expression;
4329                         }
4330
4331                         set {
4332                                 instance_expression = value;
4333                         }
4334                 }
4335
4336                 public bool IsExplicitImpl {
4337                         get {
4338                                 return is_explicit_impl;
4339                         }
4340
4341                         set {
4342                                 is_explicit_impl = value;
4343                         }
4344                 }
4345
4346                 public string Name {
4347                         get {
4348                                 return Methods [0].Name;
4349                         }
4350                 }
4351
4352                 public bool IsInstance {
4353                         get {
4354                                 foreach (MethodBase mb in Methods)
4355                                         if (!mb.IsStatic)
4356                                                 return true;
4357
4358                                 return false;
4359                         }
4360                 }
4361
4362                 public bool IsStatic {
4363                         get {
4364                                 foreach (MethodBase mb in Methods)
4365                                         if (mb.IsStatic)
4366                                                 return true;
4367
4368                                 return false;
4369                         }
4370                 }
4371                 
4372                 override public Expression DoResolve (EmitContext ec)
4373                 {
4374                         if (instance_expression != null) {
4375                                 instance_expression = instance_expression.DoResolve (ec);
4376                                 if (instance_expression == null)
4377                                         return null;
4378                         }
4379
4380                         return this;
4381                 }
4382
4383                 public void ReportUsageError ()
4384                 {
4385                         Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
4386                                       Methods [0].Name + "()' is referenced without parentheses");
4387                 }
4388
4389                 override public void Emit (EmitContext ec)
4390                 {
4391                         ReportUsageError ();
4392                 }
4393
4394                 bool RemoveMethods (bool keep_static)
4395                 {
4396                         ArrayList smethods = new ArrayList ();
4397
4398                         foreach (MethodBase mb in Methods){
4399                                 if (mb.IsStatic == keep_static)
4400                                         smethods.Add (mb);
4401                         }
4402
4403                         if (smethods.Count == 0)
4404                                 return false;
4405
4406                         Methods = new MethodBase [smethods.Count];
4407                         smethods.CopyTo (Methods, 0);
4408
4409                         return true;
4410                 }
4411                 
4412                 /// <summary>
4413                 ///   Removes any instance methods from the MethodGroup, returns
4414                 ///   false if the resulting set is empty.
4415                 /// </summary>
4416                 public bool RemoveInstanceMethods ()
4417                 {
4418                         return RemoveMethods (true);
4419                 }
4420
4421                 /// <summary>
4422                 ///   Removes any static methods from the MethodGroup, returns
4423                 ///   false if the resulting set is empty.
4424                 /// </summary>
4425                 public bool RemoveStaticMethods ()
4426                 {
4427                         return RemoveMethods (false);
4428                 }
4429         }
4430
4431         /// <summary>
4432         ///   Fully resolved expression that evaluates to a Field
4433         /// </summary>
4434         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4435                 public readonly FieldInfo FieldInfo;
4436                 Expression instance_expr;
4437                 
4438                 public FieldExpr (FieldInfo fi, Location l)
4439                 {
4440                         FieldInfo = fi;
4441                         eclass = ExprClass.Variable;
4442                         type = fi.FieldType;
4443                         loc = l;
4444                 }
4445
4446                 public string Name {
4447                         get {
4448                                 return FieldInfo.Name;
4449                         }
4450                 }
4451
4452                 public bool IsInstance {
4453                         get {
4454                                 return !FieldInfo.IsStatic;
4455                         }
4456                 }
4457
4458                 public bool IsStatic {
4459                         get {
4460                                 return FieldInfo.IsStatic;
4461                         }
4462                 }
4463
4464                 public Type DeclaringType {
4465                         get {
4466                                 return FieldInfo.DeclaringType;
4467                         }
4468                 }
4469
4470                 public Expression InstanceExpression {
4471                         get {
4472                                 return instance_expr;
4473                         }
4474
4475                         set {
4476                                 instance_expr = value;
4477                         }
4478                 }
4479
4480                 override public Expression DoResolve (EmitContext ec)
4481                 {
4482                         if (!FieldInfo.IsStatic){
4483                                 if (instance_expr == null){
4484                                         throw new Exception ("non-static FieldExpr without instance var\n" +
4485                                                              "You have to assign the Instance variable\n" +
4486                                                              "Of the FieldExpr to set this\n");
4487                                 }
4488
4489                                 // Resolve the field's instance expression while flow analysis is turned
4490                                 // off: when accessing a field "a.b", we must check whether the field
4491                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4492                                 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4493                                                                        ResolveFlags.DisableFlowAnalysis);
4494                                 if (instance_expr == null)
4495                                         return null;
4496                         }
4497
4498                         // If the instance expression is a local variable or parameter.
4499                         IVariable var = instance_expr as IVariable;
4500                         if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4501                                 return null;
4502
4503                         return this;
4504                 }
4505
4506                 void Report_AssignToReadonly (bool is_instance)
4507                 {
4508                         string msg;
4509                         
4510                         if (is_instance)
4511                                 msg = "Readonly field can not be assigned outside " +
4512                                 "of constructor or variable initializer";
4513                         else
4514                                 msg = "A static readonly field can only be assigned in " +
4515                                 "a static constructor";
4516
4517                         Report.Error (is_instance ? 191 : 198, loc, msg);
4518                 }
4519                 
4520                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4521                 {
4522                         IVariable var = instance_expr as IVariable;
4523                         if (var != null)
4524                                 var.SetFieldAssigned (ec, FieldInfo.Name);
4525
4526                         Expression e = DoResolve (ec);
4527
4528                         if (e == null)
4529                                 return null;
4530                         
4531                         if (!FieldInfo.IsInitOnly)
4532                                 return this;
4533
4534                         //
4535                         // InitOnly fields can only be assigned in constructors
4536                         //
4537
4538                         if (ec.IsConstructor)
4539                                 return this;
4540
4541                         Report_AssignToReadonly (true);
4542                         
4543                         return null;
4544                 }
4545
4546                 override public void Emit (EmitContext ec)
4547                 {
4548                         ILGenerator ig = ec.ig;
4549                         bool is_volatile = false;
4550
4551                         if (FieldInfo is FieldBuilder){
4552                                 FieldBase f = TypeManager.GetField (FieldInfo);
4553
4554                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4555                                         is_volatile = true;
4556                                 
4557                                 f.status |= Field.Status.USED;
4558                         }
4559                         
4560                         if (FieldInfo.IsStatic){
4561                                 if (is_volatile)
4562                                         ig.Emit (OpCodes.Volatile);
4563                                 
4564                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4565                         } else {
4566                                 if (instance_expr.Type.IsValueType){
4567                                         IMemoryLocation ml;
4568                                         LocalTemporary tempo = null;
4569                                         
4570                                         if (!(instance_expr is IMemoryLocation)){
4571                                                 tempo = new LocalTemporary (
4572                                                         ec, instance_expr.Type);
4573
4574                                                 InstanceExpression.Emit (ec);
4575                                                 tempo.Store (ec);
4576                                                 ml = tempo;
4577                                         } else
4578                                                 ml = (IMemoryLocation) instance_expr;
4579
4580                                         ml.AddressOf (ec, AddressOp.Load);
4581                                 } else 
4582                                         instance_expr.Emit (ec);
4583
4584                                 if (is_volatile)
4585                                         ig.Emit (OpCodes.Volatile);
4586                                 
4587                                 ig.Emit (OpCodes.Ldfld, FieldInfo);
4588                         }
4589                 }
4590
4591                 public void EmitAssign (EmitContext ec, Expression source)
4592                 {
4593                         FieldAttributes fa = FieldInfo.Attributes;
4594                         bool is_static = (fa & FieldAttributes.Static) != 0;
4595                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4596                         ILGenerator ig = ec.ig;
4597
4598                         if (is_readonly && !ec.IsConstructor){
4599                                 Report_AssignToReadonly (!is_static);
4600                                 return;
4601                         }
4602                         
4603                         if (!is_static){
4604                                 Expression instance = instance_expr;
4605
4606                                 if (instance.Type.IsValueType){
4607                                         if (instance is IMemoryLocation){
4608                                                 IMemoryLocation ml = (IMemoryLocation) instance;
4609
4610                                                 ml.AddressOf (ec, AddressOp.Store);
4611                                         } else
4612                                                 throw new Exception ("The " + instance + " of type " +
4613                                                                      instance.Type +
4614                                                                      " represents a ValueType and does " +
4615                                                                      "not implement IMemoryLocation");
4616                                 } else
4617                                         instance.Emit (ec);
4618                         }
4619                         source.Emit (ec);
4620
4621                         if (FieldInfo is FieldBuilder){
4622                                 FieldBase f = TypeManager.GetField (FieldInfo);
4623                                 
4624                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4625                                         ig.Emit (OpCodes.Volatile);
4626                         }
4627                         
4628                         if (is_static)
4629                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
4630                         else 
4631                                 ig.Emit (OpCodes.Stfld, FieldInfo);
4632
4633                         if (FieldInfo is FieldBuilder){
4634                                 FieldBase f = TypeManager.GetField (FieldInfo);
4635
4636                                 f.status |= Field.Status.ASSIGNED;
4637                         }
4638                 }
4639                 
4640                 public void AddressOf (EmitContext ec, AddressOp mode)
4641                 {
4642                         ILGenerator ig = ec.ig;
4643                         
4644                         if (FieldInfo is FieldBuilder){
4645                                 FieldBase f = TypeManager.GetField (FieldInfo);
4646                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4647                                         ig.Emit (OpCodes.Volatile);
4648                         }
4649
4650                         if (FieldInfo is FieldBuilder){
4651                                 FieldBase f = TypeManager.GetField (FieldInfo);
4652
4653                                 if ((mode & AddressOp.Store) != 0)
4654                                         f.status |= Field.Status.ASSIGNED;
4655                                 if ((mode & AddressOp.Load) != 0)
4656                                         f.status |= Field.Status.USED;
4657                         }
4658
4659                         //
4660                         // Handle initonly fields specially: make a copy and then
4661                         // get the address of the copy.
4662                         //
4663                         if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4664                                 LocalBuilder local;
4665                                 
4666                                 Emit (ec);
4667                                 local = ig.DeclareLocal (type);
4668                                 ig.Emit (OpCodes.Stloc, local);
4669                                 ig.Emit (OpCodes.Ldloca, local);
4670                                 return;
4671                         }
4672
4673                         if (FieldInfo.IsStatic)
4674                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4675                         else {
4676                                 if (instance_expr is IMemoryLocation)
4677                                         ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4678                                 else
4679                                         instance_expr.Emit (ec);
4680                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
4681                         }
4682                 }
4683         }
4684         
4685         /// <summary>
4686         ///   Expression that evaluates to a Property.  The Assign class
4687         ///   might set the 'Value' expression if we are in an assignment.
4688         ///
4689         ///   This is not an LValue because we need to re-write the expression, we
4690         ///   can not take data from the stack and store it.  
4691         /// </summary>
4692         public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4693                 public readonly PropertyInfo PropertyInfo;
4694                 public bool IsBase;
4695                 MethodInfo getter, setter;
4696                 bool is_static;
4697                 public ArrayList PropertyArgs;
4698
4699                 Expression instance_expr;
4700
4701                 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4702                 {
4703                         PropertyInfo = pi;
4704                         eclass = ExprClass.PropertyAccess;
4705                         PropertyArgs = new ArrayList();
4706                         is_static = false;
4707                         loc = l;
4708
4709                         type = TypeManager.TypeToCoreType (pi.PropertyType);
4710
4711                         ResolveAccessors (ec);
4712                 }
4713
4714                 public string Name {
4715                         get {
4716                                 return PropertyInfo.Name;
4717                         }
4718                 }
4719
4720                 public bool IsInstance {
4721                         get {
4722                                 return !is_static;
4723                         }
4724                 }
4725
4726                 public bool IsStatic {
4727                         get {
4728                                 return is_static;
4729                         }
4730                 }
4731                 
4732                 public Type DeclaringType {
4733                         get {
4734                                 return PropertyInfo.DeclaringType;
4735                         }
4736                 }
4737
4738                 //
4739                 // The instance expression associated with this expression
4740                 //
4741                 public Expression InstanceExpression {
4742                         set {
4743                                 instance_expr = value;
4744                         }
4745
4746                         get {
4747                                 return instance_expr;
4748                         }
4749                 }
4750
4751                 public bool VerifyAssignable ()
4752                 {
4753                         if (!PropertyInfo.CanWrite){
4754                                 Report.Error (200, loc, 
4755                                               "The property '" + PropertyInfo.Name +
4756                                               "' can not be assigned to, as it has not set accessor");
4757                                 return false;
4758                         }
4759
4760                         return true;
4761                 }
4762
4763                 void ResolveAccessors (EmitContext ec)
4764                 {
4765                         BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4766                         MemberInfo [] group;
4767                         
4768                         group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4769                                                               MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4770
4771                         //
4772                         // The first method is the closest to us
4773                         //
4774                         if (group != null && group.Length > 0){
4775                                 getter = (MethodInfo) group [0];
4776
4777                                 if (getter.IsStatic)
4778                                         is_static = true;
4779                         }                       
4780
4781                         //
4782                         // The first method is the closest to us
4783                         //
4784                         group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4785                                                           MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4786                         if (group != null && group.Length > 0){
4787                                 setter = (MethodInfo) group [0];
4788                                 if (setter.IsStatic)
4789                                         is_static = true;
4790                         }
4791                 }
4792
4793                 override public Expression DoResolve (EmitContext ec)
4794                 {
4795                         if (getter == null){
4796                                 Report.Error (30524, loc, 
4797                                               "The property '" + PropertyInfo.Name +
4798                                               "' can not be used in " +
4799                                               "this context because it lacks a get accessor");
4800                                 return null;
4801                         }
4802
4803                         if ((instance_expr == null) && ec.IsStatic && !is_static) {
4804                                 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4805                                 return null;
4806                         }
4807
4808                         if (instance_expr != null) {
4809                                 instance_expr = instance_expr.DoResolve (ec);
4810                                 if (instance_expr == null)
4811                                         return null;
4812                         }
4813
4814                         return this;
4815                 }
4816
4817                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4818                 {
4819                         if (setter == null){
4820                                 Report.Error (30526, loc, 
4821                                               "The property '" + PropertyInfo.Name +
4822                                               "' can not be used in " +
4823                                               "this context because it lacks a set accessor");
4824                                 return null;
4825                         }
4826
4827                         if (instance_expr != null) {
4828                                 instance_expr = instance_expr.DoResolve (ec);
4829                                 if (instance_expr == null)
4830                                         return null;
4831                         }
4832
4833                         return this;
4834                 }
4835
4836                 override public void Emit (EmitContext ec)
4837                 {
4838                         //
4839                         // Special case: length of single dimension array property is turned into ldlen
4840                         //
4841                         if ((getter == TypeManager.system_int_array_get_length) ||
4842                             (getter == TypeManager.int_array_get_length)){
4843                                 Type iet = instance_expr.Type;
4844
4845                                 //
4846                                 // System.Array.Length can be called, but the Type does not
4847                                 // support invoking GetArrayRank, so test for that case first
4848                                 //
4849                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4850                                         instance_expr.Emit (ec);
4851                                         ec.ig.Emit (OpCodes.Ldlen);
4852                                         return;
4853                                 }
4854                         }
4855                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4856                 }
4857
4858                 //
4859                 // Implements the IAssignMethod interface for assignments
4860                 //
4861                 public void EmitAssign (EmitContext ec, Expression source)
4862                 {
4863                         Argument arg = new Argument (source, Argument.AType.Expression);
4864                         ArrayList args = new ArrayList ();
4865 //HERE
4866                         args.Add (arg);
4867                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4868                 }
4869
4870                 override public void EmitStatement (EmitContext ec)
4871                 {
4872                         Emit (ec);
4873                         ec.ig.Emit (OpCodes.Pop);
4874                 }
4875         }
4876
4877         /// <summary>
4878         ///   Fully resolved expression that evaluates to an Event
4879         /// </summary>
4880         public class EventExpr : Expression, IMemberExpr {
4881                 public readonly EventInfo EventInfo;
4882                 public Expression instance_expr;
4883
4884                 bool is_static;
4885                 MethodInfo add_accessor, remove_accessor;
4886                 
4887                 public EventExpr (EventInfo ei, Location loc)
4888                 {
4889                         EventInfo = ei;
4890                         this.loc = loc;
4891                         eclass = ExprClass.EventAccess;
4892
4893                         add_accessor = TypeManager.GetAddMethod (ei);
4894                         remove_accessor = TypeManager.GetRemoveMethod (ei);
4895                         
4896                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
4897                                 is_static = true;
4898
4899                         if (EventInfo is MyEventBuilder)
4900                                 type = ((MyEventBuilder) EventInfo).EventType;
4901                         else
4902                                 type = EventInfo.EventHandlerType;
4903                 }
4904
4905                 public string Name {
4906                         get {
4907                                 return EventInfo.Name;
4908                         }
4909                 }
4910
4911                 public bool IsInstance {
4912                         get {
4913                                 return !is_static;
4914                         }
4915                 }
4916
4917                 public bool IsStatic {
4918                         get {
4919                                 return is_static;
4920                         }
4921                 }
4922
4923                 public Type DeclaringType {
4924                         get {
4925                                 return EventInfo.DeclaringType;
4926                         }
4927                 }
4928
4929                 public Expression InstanceExpression {
4930                         get {
4931                                 return instance_expr;
4932                         }
4933
4934                         set {
4935                                 instance_expr = value;
4936                         }
4937                 }
4938
4939                 Expression field_expr = null;
4940
4941                 public override Expression DoResolve (EmitContext ec)
4942                 {
4943                         if (instance_expr != null) {
4944                                 instance_expr = instance_expr.DoResolve (ec);
4945                                 if (instance_expr == null)
4946                                         return null;
4947                         }
4948
4949                         if (this.DeclaringType == ec.ContainerType)     {
4950                                 MemberInfo mi = GetFieldFromEvent (this);
4951                                 if (mi == null)
4952                                         return null;
4953                                 field_expr = ExprClassFromMemberInfo (ec, mi, loc);
4954                                 ((FieldExpr) field_expr).InstanceExpression = instance_expr;
4955                                 field_expr = field_expr.DoResolve (ec);
4956                                 if (field_expr == null)
4957                                         return null;
4958                         }
4959
4960                         return this;
4961                 }
4962
4963                 public override void Emit (EmitContext ec)
4964                 {
4965                         if (field_expr != null)
4966                                 field_expr.Emit (ec);
4967                 }
4968
4969                 public void EmitAddOrRemove (EmitContext ec, Expression source)
4970                 {
4971                         Expression handler = ((Binary) source).Right;
4972                         
4973                         Argument arg = new Argument (handler, Argument.AType.Expression);
4974                         ArrayList args = new ArrayList ();
4975                                 
4976                         args.Add (arg);
4977                         
4978                         if (((Binary) source).Oper == Binary.Operator.Addition)
4979                                 Invocation.EmitCall (
4980                                         ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4981                         else
4982                                 Invocation.EmitCall (
4983                                         ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
4984                 }
4985         }
4986 }