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