2003-05-27 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
1 //
2 // ecore.cs: Core of the Expression representation for the intermediate tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 //
9 //
10
11 namespace Mono.CSharp {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
15         using System.Reflection;
16         using System.Reflection.Emit;
17         using System.Text;
18
19         /// <remarks>
20         ///   The ExprClass class contains the is used to pass the 
21         ///   classification of an expression (value, variable, namespace,
22         ///   type, method group, property access, event access, indexer access,
23         ///   nothing).
24         /// </remarks>
25         public enum ExprClass : byte {
26                 Invalid,
27                 
28                 Value,
29                 Variable,
30                 Namespace,
31                 Type,
32                 MethodGroup,
33                 PropertyAccess,
34                 EventAccess,
35                 IndexerAccess,
36                 Nothing, 
37         }
38
39         /// <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                 VariableInfo VariableInfo {
100                         get;
101                 }
102         }
103
104         /// <summary>
105         ///   This interface denotes an expression which evaluates to a member
106         ///   of a struct or a class.
107         /// </summary>
108         public interface IMemberExpr
109         {
110                 /// <summary>
111                 ///   The name of this member.
112                 /// </summary>
113                 string Name {
114                         get;
115                 }
116
117                 /// <summary>
118                 ///   Whether this is an instance member.
119                 /// </summary>
120                 bool IsInstance {
121                         get;
122                 }
123
124                 /// <summary>
125                 ///   Whether this is a static member.
126                 /// </summary>
127                 bool IsStatic {
128                         get;
129                 }
130
131                 /// <summary>
132                 ///   The type which declares this member.
133                 /// </summary>
134                 Type DeclaringType {
135                         get;
136                 }
137
138                 /// <summary>
139                 ///   The instance expression associated with this member, if it's a
140                 ///   non-static member.
141                 /// </summary>
142                 Expression InstanceExpression {
143                         get; set;
144                 }
145         }
146
147         /// <remarks>
148         ///   Base class for expressions
149         /// </remarks>
150         public abstract class Expression {
151                 public ExprClass eclass;
152                 protected Type type;
153                 protected Location loc;
154                 
155                 public Type Type {
156                         get {
157                                 return type;
158                         }
159
160                         set {
161                                 type = value;
162                         }
163                 }
164
165                 public Location Location {
166                         get {
167                                 return loc;
168                         }
169                 }
170
171                 /// <summary>
172                 ///   Utility wrapper routine for Error, just to beautify the code
173                 /// </summary>
174                 public void Error (int error, string s)
175                 {
176                         if (!Location.IsNull (loc))
177                                 Report.Error (error, loc, s);
178                         else
179                                 Report.Error (error, s);
180                 }
181
182                 /// <summary>
183                 ///   Utility wrapper routine for Warning, just to beautify the code
184                 /// </summary>
185                 public void Warning (int warning, string s)
186                 {
187                         if (!Location.IsNull (loc))
188                                 Report.Warning (warning, loc, s);
189                         else
190                                 Report.Warning (warning, s);
191                 }
192
193                 /// <summary>
194                 ///   Utility wrapper routine for Warning, only prints the warning if
195                 ///   warnings of level `level' are enabled.
196                 /// </summary>
197                 public void Warning (int warning, int level, string s)
198                 {
199                         if (level <= RootContext.WarningLevel)
200                                 Warning (warning, s);
201                 }
202
203                 /// <summary>
204                 ///   Performs semantic analysis on the Expression
205                 /// </summary>
206                 ///
207                 /// <remarks>
208                 ///   The Resolve method is invoked to perform the semantic analysis
209                 ///   on the node.
210                 ///
211                 ///   The return value is an expression (it can be the
212                 ///   same expression in some cases) or a new
213                 ///   expression that better represents this node.
214                 ///   
215                 ///   For example, optimizations of Unary (LiteralInt)
216                 ///   would return a new LiteralInt with a negated
217                 ///   value.
218                 ///   
219                 ///   If there is an error during semantic analysis,
220                 ///   then an error should be reported (using Report)
221                 ///   and a null value should be returned.
222                 ///   
223                 ///   There are two side effects expected from calling
224                 ///   Resolve(): the the field variable "eclass" should
225                 ///   be set to any value of the enumeration
226                 ///   `ExprClass' and the type variable should be set
227                 ///   to a valid type (this is the type of the
228                 ///   expression).
229                 /// </remarks>
230                 public abstract Expression DoResolve (EmitContext ec);
231
232                 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
233                 {
234                         return DoResolve (ec);
235                 }
236
237                 //
238                 // This is used if the expression should be resolved as a type.
239                 // the default implementation fails.   Use this method in
240                 // those participants in the SimpleName chain system.
241                 //
242                 public virtual Expression ResolveAsTypeStep (EmitContext ec)
243                 {
244                         return null;
245                 }
246
247                 //
248                 // This is used to resolve the expression as a type, a null
249                 // value will be returned if the expression is not a type
250                 // reference
251                 //
252                 public Expression ResolveAsTypeTerminal (EmitContext ec)
253                 {
254                         Expression e = ResolveAsTypeStep (ec);
255
256                         if (e == null)
257                                 return null;
258                         if (e is SimpleName)
259                                 return null;
260                         return e;
261                 }
262                
263                 /// <summary>
264                 ///   Resolves an expression and performs semantic analysis on it.
265                 /// </summary>
266                 ///
267                 /// <remarks>
268                 ///   Currently Resolve wraps DoResolve to perform sanity
269                 ///   checking and assertion checking on what we expect from Resolve.
270                 /// </remarks>
271                 public Expression Resolve (EmitContext ec, ResolveFlags flags)
272                 {
273                         if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) 
274                                 return ResolveAsTypeStep (ec);
275
276                         bool old_do_flow_analysis = ec.DoFlowAnalysis;
277                         if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
278                                 ec.DoFlowAnalysis = false;
279
280                         Expression e;
281                         if (this is SimpleName)
282                                 e = ((SimpleName) this).DoResolveAllowStatic (ec);
283                         else 
284                                 e = DoResolve (ec);
285
286                         ec.DoFlowAnalysis = old_do_flow_analysis;
287
288                         if (e == null)
289                                 return null;
290
291                         if (e is SimpleName){
292                                 SimpleName s = (SimpleName) e;
293
294                                 if ((flags & ResolveFlags.SimpleName) == 0) {
295                                         MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
296                                                             ec.DeclSpace.Name, loc);
297                                         return null;
298                                 }
299
300                                 return s;
301                         }
302
303                         if ((e is TypeExpr) || (e is ComposedCast)) {
304                                 if ((flags & ResolveFlags.Type) == 0) {
305                                         e.Error_UnexpectedKind (flags);
306                                         return null;
307                                 }
308
309                                 return e;
310                         }
311
312                         switch (e.eclass) {
313                         case ExprClass.Type:
314                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
315                                         e.Error_UnexpectedKind (flags);
316                                         return null;
317                                 }
318                                 break;
319
320                         case ExprClass.MethodGroup:
321                                 if ((flags & ResolveFlags.MethodGroup) == 0) {
322                                         ((MethodGroupExpr) e).ReportUsageError ();
323                                         return null;
324                                 }
325                                 break;
326
327                         case ExprClass.Value:
328                         case ExprClass.Variable:
329                         case ExprClass.PropertyAccess:
330                         case ExprClass.EventAccess:
331                         case ExprClass.IndexerAccess:
332                                 if ((flags & ResolveFlags.VariableOrValue) == 0) {
333                                         Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
334                                         Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
335                                         FieldInfo fi = ((FieldExpr) e).FieldInfo;
336                                         
337                                         Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
338                                         e.Error_UnexpectedKind (flags);
339                                         return null;
340                                 }
341                                 break;
342
343                         default:
344                                 throw new Exception ("Expression " + e.GetType () +
345                                                      " ExprClass is Invalid after resolve");
346                         }
347
348                         if (e.type == null)
349                                 throw new Exception (
350                                         "Expression " + e.GetType () +
351                                         " did not set its type after Resolve\n" +
352                                         "called from: " + this.GetType ());
353
354                         return e;
355                 }
356
357                 /// <summary>
358                 ///   Resolves an expression and performs semantic analysis on it.
359                 /// </summary>
360                 public Expression Resolve (EmitContext ec)
361                 {
362                         return Resolve (ec, ResolveFlags.VariableOrValue);
363                 }
364
365                 /// <summary>
366                 ///   Resolves an expression for LValue assignment
367                 /// </summary>
368                 ///
369                 /// <remarks>
370                 ///   Currently ResolveLValue wraps DoResolveLValue to perform sanity
371                 ///   checking and assertion checking on what we expect from Resolve
372                 /// </remarks>
373                 public Expression ResolveLValue (EmitContext ec, Expression right_side)
374                 {
375                         Expression e = DoResolveLValue (ec, right_side);
376
377                         if (e != null){
378                                 if (e is SimpleName){
379                                         SimpleName s = (SimpleName) e;
380                                         MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
381                                                             ec.DeclSpace.Name, loc);
382                                         return null;
383                                 }
384
385                                 if (e.eclass == ExprClass.Invalid)
386                                         throw new Exception ("Expression " + e +
387                                                              " ExprClass is Invalid after resolve");
388
389                                 if (e.eclass == ExprClass.MethodGroup) {
390                                         ((MethodGroupExpr) e).ReportUsageError ();
391                                         return null;
392                                 }
393
394                                 if (e.type == null)
395                                         throw new Exception ("Expression " + e +
396                                                              " did not set its type after Resolve");
397                         }
398
399                         return e;
400                 }
401                 
402                 /// <summary>
403                 ///   Emits the code for the expression
404                 /// </summary>
405                 ///
406                 /// <remarks>
407                 ///   The Emit method is invoked to generate the code
408                 ///   for the expression.  
409                 /// </remarks>
410                 public abstract void Emit (EmitContext ec);
411
412                 /// <summary>
413                 ///   Protected constructor.  Only derivate types should
414                 ///   be able to be created
415                 /// </summary>
416
417                 protected Expression ()
418                 {
419                         eclass = ExprClass.Invalid;
420                         type = null;
421                 }
422
423                 /// <summary>
424                 ///   Returns a literalized version of a literal FieldInfo
425                 /// </summary>
426                 ///
427                 /// <remarks>
428                 ///   The possible return values are:
429                 ///      IntConstant, UIntConstant
430                 ///      LongLiteral, ULongConstant
431                 ///      FloatConstant, DoubleConstant
432                 ///      StringConstant
433                 ///
434                 ///   The value returned is already resolved.
435                 /// </remarks>
436                 public static Constant Constantify (object v, Type t)
437                 {
438                         if (t == TypeManager.int32_type)
439                                 return new IntConstant ((int) v);
440                         else if (t == TypeManager.uint32_type)
441                                 return new UIntConstant ((uint) v);
442                         else if (t == TypeManager.int64_type)
443                                 return new LongConstant ((long) v);
444                         else if (t == TypeManager.uint64_type)
445                                 return new ULongConstant ((ulong) v);
446                         else if (t == TypeManager.float_type)
447                                 return new FloatConstant ((float) v);
448                         else if (t == TypeManager.double_type)
449                                 return new DoubleConstant ((double) v);
450                         else if (t == TypeManager.string_type)
451                                 return new StringConstant ((string) v);
452                         else if (t == TypeManager.short_type)
453                                 return new ShortConstant ((short)v);
454                         else if (t == TypeManager.ushort_type)
455                                 return new UShortConstant ((ushort)v);
456                         else if (t == TypeManager.sbyte_type)
457                                 return new SByteConstant (((sbyte)v));
458                         else if (t == TypeManager.byte_type)
459                                 return new ByteConstant ((byte)v);
460                         else if (t == TypeManager.char_type)
461                                 return new CharConstant ((char)v);
462                         else if (t == TypeManager.bool_type)
463                                 return new BoolConstant ((bool) v);
464                         else if (TypeManager.IsEnumType (t)){
465                                 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
466
467                                 return new EnumConstant (e, t);
468                         } else
469                                 throw new Exception ("Unknown type for constant (" + t +
470                                                      "), details: " + v);
471                 }
472
473                 /// <summary>
474                 ///   Returns a fully formed expression after a MemberLookup
475                 /// </summary>
476                 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
477                 {
478                         if (mi is EventInfo)
479                                 return new EventExpr ((EventInfo) mi, loc);
480                         else if (mi is FieldInfo)
481                                 return new FieldExpr ((FieldInfo) mi, loc);
482                         else if (mi is PropertyInfo)
483                                 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
484                         else if (mi is Type){
485                                 return new TypeExpr ((System.Type) mi, loc);
486                         }
487
488                         return null;
489                 }
490
491                 //
492                 // FIXME: Probably implement a cache for (t,name,current_access_set)?
493                 //
494                 // This code could use some optimizations, but we need to do some
495                 // measurements.  For example, we could use a delegate to `flag' when
496                 // something can not any longer be a method-group (because it is something
497                 // else).
498                 //
499                 // Return values:
500                 //     If the return value is an Array, then it is an array of
501                 //     MethodBases
502                 //   
503                 //     If the return value is an MemberInfo, it is anything, but a Method
504                 //
505                 //     null on error.
506                 //
507                 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
508                 // the arguments here and have MemberLookup return only the methods that
509                 // match the argument count/type, unlike we are doing now (we delay this
510                 // decision).
511                 //
512                 // This is so we can catch correctly attempts to invoke instance methods
513                 // from a static body (scan for error 120 in ResolveSimpleName).
514                 //
515                 //
516                 // FIXME: Potential optimization, have a static ArrayList
517                 //
518
519                 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
520                                                        MemberTypes mt, BindingFlags bf, Location loc)
521                 {
522                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
523                 }
524
525                 //
526                 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
527                 // `qualifier_type' or null to lookup members in the current class.
528                 //
529
530                 public static Expression MemberLookup (EmitContext ec, Type container_type,
531                                                        Type qualifier_type, Type queried_type,
532                                                        string name, MemberTypes mt,
533                                                        BindingFlags bf, Location loc)
534                 {
535                         MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
536                                                                      queried_type, mt, bf, name);
537
538                         if (mi == null)
539                                 return null;
540
541                         int count = mi.Length;
542
543                         if (mi [0] is MethodBase)
544                                 return new MethodGroupExpr (mi, loc);
545
546                         if (count > 1)
547                                 return null;
548
549                         return ExprClassFromMemberInfo (ec, mi [0], loc);
550                 }
551
552                 public const MemberTypes AllMemberTypes =
553                         MemberTypes.Constructor |
554                         MemberTypes.Event       |
555                         MemberTypes.Field       |
556                         MemberTypes.Method      |
557                         MemberTypes.NestedType  |
558                         MemberTypes.Property;
559                 
560                 public const BindingFlags AllBindingFlags =
561                         BindingFlags.Public |
562                         BindingFlags.Static |
563                         BindingFlags.Instance;
564
565                 public static Expression MemberLookup (EmitContext ec, Type queried_type,
566                                                        string name, Location loc)
567                 {
568                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
569                                              AllMemberTypes, AllBindingFlags, loc);
570                 }
571
572                 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
573                                                        Type queried_type, string name, Location loc)
574                 {
575                         return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
576                                              name, AllMemberTypes, AllBindingFlags, loc);
577                 }
578
579                 public static Expression MethodLookup (EmitContext ec, Type queried_type,
580                                                        string name, Location loc)
581                 {
582                         return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
583                                              MemberTypes.Method, AllBindingFlags, loc);
584                 }
585
586                 /// <summary>
587                 ///   This is a wrapper for MemberLookup that is not used to "probe", but
588                 ///   to find a final definition.  If the final definition is not found, we
589                 ///   look for private members and display a useful debugging message if we
590                 ///   find it.
591                 /// </summary>
592                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
593                                                             Type queried_type, string name, Location loc)
594                 {
595                         return MemberLookupFinal (ec, qualifier_type, queried_type, name,
596                                                   AllMemberTypes, AllBindingFlags, loc);
597                 }
598
599                 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
600                                                             Type queried_type, string name,
601                                                             MemberTypes mt, BindingFlags bf,
602                                                             Location loc)
603                 {
604                         Expression e;
605
606                         int errors = Report.Errors;
607
608                         e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
609                                           name, mt, bf, loc);
610
611                         if (e != null)
612                                 return e;
613
614                         // Error has already been reported.
615                         if (errors < Report.Errors)
616                                 return null;
617
618                         MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
619                         return null;
620                 }
621
622                 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
623                                                        Type queried_type, string name,
624                                                        string class_name, Location loc)
625                 {
626                         object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
627                                                                   AllMemberTypes, AllBindingFlags |
628                                                                   BindingFlags.NonPublic, name);
629
630                         if (lookup == null) {
631                                 if (class_name != null)
632                                         Report.Error (103, loc, "The name `" + name + "' could not be " +
633                                                       "found in `" + class_name + "'");
634                                 else
635                                         Report.Error (
636                                                 117, loc, "`" + queried_type + "' does not contain a " +
637                                                 "definition for `" + name + "'");
638                                 return;
639                         }
640
641                         if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
642                             ec.ContainerType.IsSubclassOf (qualifier_type)) {
643                                 // Although a derived class can access protected members of
644                                 // its base class it cannot do so through an instance of the
645                                 // base class (CS1540).  If the qualifier_type is a parent of the
646                                 // ec.ContainerType and the lookup succeeds with the latter one,
647                                 // then we are in this situation.
648
649                                 lookup = TypeManager.MemberLookup (
650                                         ec.ContainerType, ec.ContainerType, ec.ContainerType,
651                                         AllMemberTypes, AllBindingFlags, name);
652
653                                 if (lookup != null) {
654                                         Report.Error (
655                                                 1540, loc, "Cannot access protected member `" +
656                                                 TypeManager.CSharpName (qualifier_type) + "." +
657                                                 name + "' " + "via a qualifier of type `" +
658                                                 TypeManager.CSharpName (qualifier_type) + "'; the " +
659                                                 "qualifier must be of type `" +
660                                                 TypeManager.CSharpName (ec.ContainerType) + "' " +
661                                                 "(or derived from it)");
662                                         return;
663                                 }
664                         }
665
666                         if (qualifier_type != null)
667                                 Report.Error (
668                                         122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
669                                         name + "' is inaccessible due to its protection level");
670                         else
671                                 Report.Error (
672                                         122, loc, "`" + name + "' is inaccessible due to its " +
673                                         "protection level");
674                 }
675
676                 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
677                 {
678                         EventInfo ei = event_expr.EventInfo;
679
680                         return TypeManager.GetPrivateFieldOfEvent (ei);
681                 }
682                 
683                 /// <summary>
684                 ///   Returns an expression that can be used to invoke operator true
685                 ///   on the expression if it exists.
686                 /// </summary>
687                 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
688                 {
689                         MethodBase method;
690                         Expression operator_group;
691
692                         operator_group = MethodLookup (ec, e.Type, "op_True", loc);
693                         if (operator_group == null)
694                                 return null;
695
696                         ArrayList arguments = new ArrayList ();
697                         arguments.Add (new Argument (e, Argument.AType.Expression));
698                         method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc);
699
700                         if (method == null)
701                                 return null;
702
703                         return new StaticCallExpr ((MethodInfo) method, arguments, loc);
704                 }
705
706                 /// <summary>
707                 ///   Resolves the expression `e' into a boolean expression: either through
708                 ///   an implicit conversion, or through an `operator true' invocation
709                 /// </summary>
710                 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
711                 {
712                         e = e.Resolve (ec);
713                         if (e == null)
714                                 return null;
715
716                         Expression converted = e;
717                         if (e.Type != TypeManager.bool_type)
718                                 converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
719
720                         //
721                         // If no implicit conversion to bool exists, try using `operator true'
722                         //
723                         if (converted == null){
724                                 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
725                                 if (operator_true == null){
726                                         Report.Error (
727                                                 31, loc, "Can not convert the expression to a boolean");
728                                         return null;
729                                 }
730                                 e = operator_true;
731                         } else
732                                 e = converted;
733
734                         return e;
735                 }
736                 
737                 static string ExprClassName (ExprClass c)
738                 {
739                         switch (c){
740                         case ExprClass.Invalid:
741                                 return "Invalid";
742                         case ExprClass.Value:
743                                 return "value";
744                         case ExprClass.Variable:
745                                 return "variable";
746                         case ExprClass.Namespace:
747                                 return "namespace";
748                         case ExprClass.Type:
749                                 return "type";
750                         case ExprClass.MethodGroup:
751                                 return "method group";
752                         case ExprClass.PropertyAccess:
753                                 return "property access";
754                         case ExprClass.EventAccess:
755                                 return "event access";
756                         case ExprClass.IndexerAccess:
757                                 return "indexer access";
758                         case ExprClass.Nothing:
759                                 return "null";
760                         }
761                         throw new Exception ("Should not happen");
762                 }
763                 
764                 /// <summary>
765                 ///   Reports that we were expecting `expr' to be of class `expected'
766                 /// </summary>
767                 public void Error_UnexpectedKind (string expected)
768                 {
769                         string kind = "Unknown";
770                         
771                         kind = ExprClassName (eclass);
772
773                         Error (118, "Expression denotes a `" + kind +
774                                "' where a `" + expected + "' was expected");
775                 }
776
777                 public void Error_UnexpectedKind (ResolveFlags flags)
778                 {
779                         ArrayList valid = new ArrayList (10);
780
781                         if ((flags & ResolveFlags.VariableOrValue) != 0) {
782                                 valid.Add ("variable");
783                                 valid.Add ("value");
784                         }
785
786                         if ((flags & ResolveFlags.Type) != 0)
787                                 valid.Add ("type");
788
789                         if ((flags & ResolveFlags.MethodGroup) != 0)
790                                 valid.Add ("method group");
791
792                         if ((flags & ResolveFlags.SimpleName) != 0)
793                                 valid.Add ("simple name");
794
795                         if (valid.Count == 0)
796                                 valid.Add ("unknown");
797
798                         StringBuilder sb = new StringBuilder ();
799                         for (int i = 0; i < valid.Count; i++) {
800                                 if (i > 0)
801                                         sb.Append (", ");
802                                 else if (i == valid.Count)
803                                         sb.Append (" or ");
804                                 sb.Append (valid [i]);
805                         }
806
807                         string kind = ExprClassName (eclass);
808
809                         Error (119, "Expression denotes a `" + kind + "' where " +
810                                "a `" + sb.ToString () + "' was expected");
811                 }
812                 
813                 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
814                 {
815                         Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
816                                       TypeManager.CSharpName (t));
817                 }
818
819                 public static void UnsafeError (Location loc)
820                 {
821                         Report.Error (214, loc, "Pointers may only be used in an unsafe context");
822                 }
823                 
824                 /// <summary>
825                 ///   Converts the IntConstant, UIntConstant, LongConstant or
826                 ///   ULongConstant into the integral target_type.   Notice
827                 ///   that we do not return an `Expression' we do return
828                 ///   a boxed integral type.
829                 ///
830                 ///   FIXME: Since I added the new constants, we need to
831                 ///   also support conversions from CharConstant, ByteConstant,
832                 ///   SByteConstant, UShortConstant, ShortConstant
833                 ///
834                 ///   This is used by the switch statement, so the domain
835                 ///   of work is restricted to the literals above, and the
836                 ///   targets are int32, uint32, char, byte, sbyte, ushort,
837                 ///   short, uint64 and int64
838                 /// </summary>
839                 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
840                 {
841                         string s = "";
842
843                         if (c.Type == target_type)
844                                 return ((Constant) c).GetValue ();
845
846                         //
847                         // Make into one of the literals we handle, we dont really care
848                         // about this value as we will just return a few limited types
849                         // 
850                         if (c is EnumConstant)
851                                 c = ((EnumConstant)c).WidenToCompilerConstant ();
852
853                         if (c is IntConstant){
854                                 int v = ((IntConstant) c).Value;
855                                 
856                                 if (target_type == TypeManager.uint32_type){
857                                         if (v >= 0)
858                                                 return (uint) v;
859                                 } else if (target_type == TypeManager.char_type){
860                                         if (v >= Char.MinValue && v <= Char.MaxValue)
861                                                 return (char) v;
862                                 } else if (target_type == TypeManager.byte_type){
863                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
864                                                 return (byte) v;
865                                 } else if (target_type == TypeManager.sbyte_type){
866                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
867                                                 return (sbyte) v;
868                                 } else if (target_type == TypeManager.short_type){
869                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
870                                                 return (short) v;
871                                 } else if (target_type == TypeManager.ushort_type){
872                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
873                                                 return (ushort) v;
874                                 } else if (target_type == TypeManager.int64_type)
875                                         return (long) v;
876                                 else if (target_type == TypeManager.uint64_type){
877                                         if (v > 0)
878                                                 return (ulong) v;
879                                 }
880
881                                 s = v.ToString ();
882                         } else if (c is UIntConstant){
883                                 uint v = ((UIntConstant) c).Value;
884
885                                 if (target_type == TypeManager.int32_type){
886                                         if (v <= Int32.MaxValue)
887                                                 return (int) v;
888                                 } else if (target_type == TypeManager.char_type){
889                                         if (v >= Char.MinValue && v <= Char.MaxValue)
890                                                 return (char) v;
891                                 } else if (target_type == TypeManager.byte_type){
892                                         if (v <= Byte.MaxValue)
893                                                 return (byte) v;
894                                 } else if (target_type == TypeManager.sbyte_type){
895                                         if (v <= SByte.MaxValue)
896                                                 return (sbyte) v;
897                                 } else if (target_type == TypeManager.short_type){
898                                         if (v <= UInt16.MaxValue)
899                                                 return (short) v;
900                                 } else if (target_type == TypeManager.ushort_type){
901                                         if (v <= UInt16.MaxValue)
902                                                 return (ushort) v;
903                                 } else if (target_type == TypeManager.int64_type)
904                                         return (long) v;
905                                 else if (target_type == TypeManager.uint64_type)
906                                         return (ulong) v;
907                                 s = v.ToString ();
908                         } else if (c is LongConstant){ 
909                                 long v = ((LongConstant) c).Value;
910
911                                 if (target_type == TypeManager.int32_type){
912                                         if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
913                                                 return (int) v;
914                                 } else if (target_type == TypeManager.uint32_type){
915                                         if (v >= 0 && v <= UInt32.MaxValue)
916                                                 return (uint) v;
917                                 } else if (target_type == TypeManager.char_type){
918                                         if (v >= Char.MinValue && v <= Char.MaxValue)
919                                                 return (char) v;
920                                 } else if (target_type == TypeManager.byte_type){
921                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
922                                                 return (byte) v;
923                                 } else if (target_type == TypeManager.sbyte_type){
924                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
925                                                 return (sbyte) v;
926                                 } else if (target_type == TypeManager.short_type){
927                                         if (v >= Int16.MinValue && v <= UInt16.MaxValue)
928                                                 return (short) v;
929                                 } else if (target_type == TypeManager.ushort_type){
930                                         if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
931                                                 return (ushort) v;
932                                 } else if (target_type == TypeManager.uint64_type){
933                                         if (v > 0)
934                                                 return (ulong) v;
935                                 }
936                                 s = v.ToString ();
937                         } else if (c is ULongConstant){
938                                 ulong v = ((ULongConstant) c).Value;
939
940                                 if (target_type == TypeManager.int32_type){
941                                         if (v <= Int32.MaxValue)
942                                                 return (int) v;
943                                 } else if (target_type == TypeManager.uint32_type){
944                                         if (v <= UInt32.MaxValue)
945                                                 return (uint) v;
946                                 } else if (target_type == TypeManager.char_type){
947                                         if (v >= Char.MinValue && v <= Char.MaxValue)
948                                                 return (char) v;
949                                 } else if (target_type == TypeManager.byte_type){
950                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
951                                                 return (byte) v;
952                                 } else if (target_type == TypeManager.sbyte_type){
953                                         if (v <= (int) SByte.MaxValue)
954                                                 return (sbyte) v;
955                                 } else if (target_type == TypeManager.short_type){
956                                         if (v <= UInt16.MaxValue)
957                                                 return (short) v;
958                                 } else if (target_type == TypeManager.ushort_type){
959                                         if (v <= UInt16.MaxValue)
960                                                 return (ushort) v;
961                                 } else if (target_type == TypeManager.int64_type){
962                                         if (v <= Int64.MaxValue)
963                                                 return (long) v;
964                                 }
965                                 s = v.ToString ();
966                         } else if (c is ByteConstant){
967                                 byte v = ((ByteConstant) c).Value;
968                                 
969                                 if (target_type == TypeManager.int32_type)
970                                         return (int) v;
971                                 else if (target_type == TypeManager.uint32_type)
972                                         return (uint) v;
973                                 else if (target_type == TypeManager.char_type)
974                                         return (char) v;
975                                 else if (target_type == TypeManager.sbyte_type){
976                                         if (v <= SByte.MaxValue)
977                                                 return (sbyte) v;
978                                 } else if (target_type == TypeManager.short_type)
979                                         return (short) v;
980                                 else if (target_type == TypeManager.ushort_type)
981                                         return (ushort) v;
982                                 else if (target_type == TypeManager.int64_type)
983                                         return (long) v;
984                                 else if (target_type == TypeManager.uint64_type)
985                                         return (ulong) v;
986                                 s = v.ToString ();
987                         } else if (c is SByteConstant){
988                                 sbyte v = ((SByteConstant) c).Value;
989                                 
990                                 if (target_type == TypeManager.int32_type)
991                                         return (int) v;
992                                 else if (target_type == TypeManager.uint32_type){
993                                         if (v >= 0)
994                                                 return (uint) v;
995                                 } else if (target_type == TypeManager.char_type){
996                                         if (v >= 0)
997                                                 return (char) v;
998                                 } else if (target_type == TypeManager.byte_type){
999                                         if (v >= 0)
1000                                                 return (byte) v;
1001                                 } else if (target_type == TypeManager.short_type)
1002                                         return (short) v;
1003                                 else if (target_type == TypeManager.ushort_type){
1004                                         if (v >= 0)
1005                                                 return (ushort) v;
1006                                 } else if (target_type == TypeManager.int64_type)
1007                                         return (long) v;
1008                                 else if (target_type == TypeManager.uint64_type){
1009                                         if (v >= 0)
1010                                                 return (ulong) v;
1011                                 }
1012                                 s = v.ToString ();
1013                         } else if (c is ShortConstant){
1014                                 short v = ((ShortConstant) c).Value;
1015                                 
1016                                 if (target_type == TypeManager.int32_type){
1017                                         return (int) v;
1018                                 } else if (target_type == TypeManager.uint32_type){
1019                                         if (v >= 0)
1020                                                 return (uint) v;
1021                                 } else if (target_type == TypeManager.char_type){
1022                                         if (v >= 0)
1023                                                 return (char) v;
1024                                 } else if (target_type == TypeManager.byte_type){
1025                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1026                                                 return (byte) v;
1027                                 } else if (target_type == TypeManager.sbyte_type){
1028                                         if (v >= SByte.MinValue && v <= SByte.MaxValue)
1029                                                 return (sbyte) v;
1030                                 } else if (target_type == TypeManager.ushort_type){
1031                                         if (v >= 0)
1032                                                 return (ushort) v;
1033                                 } else if (target_type == TypeManager.int64_type)
1034                                         return (long) v;
1035                                 else if (target_type == TypeManager.uint64_type)
1036                                         return (ulong) v;
1037
1038                                 s = v.ToString ();
1039                         } else if (c is UShortConstant){
1040                                 ushort v = ((UShortConstant) c).Value;
1041                                 
1042                                 if (target_type == TypeManager.int32_type)
1043                                         return (int) v;
1044                                 else if (target_type == TypeManager.uint32_type)
1045                                         return (uint) v;
1046                                 else if (target_type == TypeManager.char_type){
1047                                         if (v >= Char.MinValue && v <= Char.MaxValue)
1048                                                 return (char) v;
1049                                 } else if (target_type == TypeManager.byte_type){
1050                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1051                                                 return (byte) v;
1052                                 } else if (target_type == TypeManager.sbyte_type){
1053                                         if (v <= SByte.MaxValue)
1054                                                 return (byte) v;
1055                                 } else if (target_type == TypeManager.short_type){
1056                                         if (v <= Int16.MaxValue)
1057                                                 return (short) v;
1058                                 } else if (target_type == TypeManager.int64_type)
1059                                         return (long) v;
1060                                 else if (target_type == TypeManager.uint64_type)
1061                                         return (ulong) v;
1062
1063                                 s = v.ToString ();
1064                         } else if (c is CharConstant){
1065                                 char v = ((CharConstant) c).Value;
1066                                 
1067                                 if (target_type == TypeManager.int32_type)
1068                                         return (int) v;
1069                                 else if (target_type == TypeManager.uint32_type)
1070                                         return (uint) v;
1071                                 else if (target_type == TypeManager.byte_type){
1072                                         if (v >= Byte.MinValue && v <= Byte.MaxValue)
1073                                                 return (byte) v;
1074                                 } else if (target_type == TypeManager.sbyte_type){
1075                                         if (v <= SByte.MaxValue)
1076                                                 return (sbyte) v;
1077                                 } else if (target_type == TypeManager.short_type){
1078                                         if (v <= Int16.MaxValue)
1079                                                 return (short) v;
1080                                 } else if (target_type == TypeManager.ushort_type)
1081                                         return (short) v;
1082                                 else if (target_type == TypeManager.int64_type)
1083                                         return (long) v;
1084                                 else if (target_type == TypeManager.uint64_type)
1085                                         return (ulong) v;
1086
1087                                 s = v.ToString ();
1088                         }
1089                         Error_ConstantValueCannotBeConverted (loc, s, target_type);
1090                         return null;
1091                 }
1092
1093                 //
1094                 // Load the object from the pointer.  
1095                 //
1096                 public static void LoadFromPtr (ILGenerator ig, Type t)
1097                 {
1098                         if (t == TypeManager.int32_type)
1099                                 ig.Emit (OpCodes.Ldind_I4);
1100                         else if (t == TypeManager.uint32_type)
1101                                 ig.Emit (OpCodes.Ldind_U4);
1102                         else if (t == TypeManager.short_type)
1103                                 ig.Emit (OpCodes.Ldind_I2);
1104                         else if (t == TypeManager.ushort_type)
1105                                 ig.Emit (OpCodes.Ldind_U2);
1106                         else if (t == TypeManager.char_type)
1107                                 ig.Emit (OpCodes.Ldind_U2);
1108                         else if (t == TypeManager.byte_type)
1109                                 ig.Emit (OpCodes.Ldind_U1);
1110                         else if (t == TypeManager.sbyte_type)
1111                                 ig.Emit (OpCodes.Ldind_I1);
1112                         else if (t == TypeManager.uint64_type)
1113                                 ig.Emit (OpCodes.Ldind_I8);
1114                         else if (t == TypeManager.int64_type)
1115                                 ig.Emit (OpCodes.Ldind_I8);
1116                         else if (t == TypeManager.float_type)
1117                                 ig.Emit (OpCodes.Ldind_R4);
1118                         else if (t == TypeManager.double_type)
1119                                 ig.Emit (OpCodes.Ldind_R8);
1120                         else if (t == TypeManager.bool_type)
1121                                 ig.Emit (OpCodes.Ldind_I1);
1122                         else if (t == TypeManager.intptr_type)
1123                                 ig.Emit (OpCodes.Ldind_I);
1124                         else if (TypeManager.IsEnumType (t)) {
1125                                 if (t == TypeManager.enum_type)
1126                                         ig.Emit (OpCodes.Ldind_Ref);
1127                                 else
1128                                         LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1129                         } else if (t.IsValueType)
1130                                 ig.Emit (OpCodes.Ldobj, t);
1131                         else if (t.IsPointer)
1132                                 ig.Emit (OpCodes.Ldind_I);
1133                         else 
1134                                 ig.Emit (OpCodes.Ldind_Ref);
1135                 }
1136
1137                 //
1138                 // The stack contains the pointer and the value of type `type'
1139                 //
1140                 public static void StoreFromPtr (ILGenerator ig, Type type)
1141                 {
1142                         if (TypeManager.IsEnumType (type))
1143                                 type = TypeManager.EnumToUnderlying (type);
1144                         if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1145                                 ig.Emit (OpCodes.Stind_I4);
1146                         else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1147                                 ig.Emit (OpCodes.Stind_I8);
1148                         else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1149                                  type == TypeManager.ushort_type)
1150                                 ig.Emit (OpCodes.Stind_I2);
1151                         else if (type == TypeManager.float_type)
1152                                 ig.Emit (OpCodes.Stind_R4);
1153                         else if (type == TypeManager.double_type)
1154                                 ig.Emit (OpCodes.Stind_R8);
1155                         else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1156                                  type == TypeManager.bool_type)
1157                                 ig.Emit (OpCodes.Stind_I1);
1158                         else if (type == TypeManager.intptr_type)
1159                                 ig.Emit (OpCodes.Stind_I);
1160                         else if (type.IsValueType)
1161                                 ig.Emit (OpCodes.Stobj, type);
1162                         else
1163                                 ig.Emit (OpCodes.Stind_Ref);
1164                 }
1165                 
1166                 //
1167                 // Returns the size of type `t' if known, otherwise, 0
1168                 //
1169                 public static int GetTypeSize (Type t)
1170                 {
1171                         t = TypeManager.TypeToCoreType (t);
1172                         if (t == TypeManager.int32_type ||
1173                             t == TypeManager.uint32_type ||
1174                             t == TypeManager.float_type)
1175                                 return 4;
1176                         else if (t == TypeManager.int64_type ||
1177                                  t == TypeManager.uint64_type ||
1178                                  t == TypeManager.double_type)
1179                                 return 8;
1180                         else if (t == TypeManager.byte_type ||
1181                                  t == TypeManager.sbyte_type ||
1182                                  t == TypeManager.bool_type)    
1183                                 return 1;
1184                         else if (t == TypeManager.short_type ||
1185                                  t == TypeManager.char_type ||
1186                                  t == TypeManager.ushort_type)
1187                                 return 2;
1188                         else if (t == TypeManager.decimal_type)
1189                                 return 16;
1190                         else
1191                                 return 0;
1192                 }
1193
1194                 //
1195                 // Default implementation of IAssignMethod.CacheTemporaries
1196                 //
1197                 public void CacheTemporaries (EmitContext ec)
1198                 {
1199                 }
1200
1201                 static void Error_NegativeArrayIndex (Location loc)
1202                 {
1203                         Report.Error (284, loc, "Can not create array with a negative size");
1204                 }
1205                 
1206                 //
1207                 // Converts `source' to an int, uint, long or ulong.
1208                 //
1209                 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1210                 {
1211                         Expression target;
1212                         
1213                         bool old_checked = ec.CheckState;
1214                         ec.CheckState = true;
1215                         
1216                         target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1217                         if (target == null){
1218                                 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1219                                 if (target == null){
1220                                         target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1221                                         if (target == null){
1222                                                 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1223                                                 if (target == null)
1224                                                         Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1225                                         }
1226                                 }
1227                         } 
1228                         ec.CheckState = old_checked;
1229
1230                         //
1231                         // Only positive constants are allowed at compile time
1232                         //
1233                         if (target is Constant){
1234                                 if (target is IntConstant){
1235                                         if (((IntConstant) target).Value < 0){
1236                                                 Error_NegativeArrayIndex (loc);
1237                                                 return null;
1238                                         }
1239                                 }
1240
1241                                 if (target is LongConstant){
1242                                         if (((LongConstant) target).Value < 0){
1243                                                 Error_NegativeArrayIndex (loc);
1244                                                 return null;
1245                                         }
1246                                 }
1247                                 
1248                         }
1249
1250                         return target;
1251                 }
1252                 
1253         }
1254
1255         /// <summary>
1256         ///   This is just a base class for expressions that can
1257         ///   appear on statements (invocations, object creation,
1258         ///   assignments, post/pre increment and decrement).  The idea
1259         ///   being that they would support an extra Emition interface that
1260         ///   does not leave a result on the stack.
1261         /// </summary>
1262         public abstract class ExpressionStatement : Expression {
1263
1264                 /// <summary>
1265                 ///   Requests the expression to be emitted in a `statement'
1266                 ///   context.  This means that no new value is left on the
1267                 ///   stack after invoking this method (constrasted with
1268                 ///   Emit that will always leave a value on the stack).
1269                 /// </summary>
1270                 public abstract void EmitStatement (EmitContext ec);
1271         }
1272
1273         /// <summary>
1274         ///   This kind of cast is used to encapsulate the child
1275         ///   whose type is child.Type into an expression that is
1276         ///   reported to return "return_type".  This is used to encapsulate
1277         ///   expressions which have compatible types, but need to be dealt
1278         ///   at higher levels with.
1279         ///
1280         ///   For example, a "byte" expression could be encapsulated in one
1281         ///   of these as an "unsigned int".  The type for the expression
1282         ///   would be "unsigned int".
1283         ///
1284         /// </summary>
1285         public class EmptyCast : Expression {
1286                 protected Expression child;
1287                 
1288                 public EmptyCast (Expression child, Type return_type)
1289                 {
1290                         eclass = child.eclass;
1291                         type = return_type;
1292                         this.child = child;
1293                 }
1294
1295                 public override Expression DoResolve (EmitContext ec)
1296                 {
1297                         // This should never be invoked, we are born in fully
1298                         // initialized state.
1299
1300                         return this;
1301                 }
1302
1303                 public override void Emit (EmitContext ec)
1304                 {
1305                         child.Emit (ec);
1306                 }
1307         }
1308
1309         //
1310         // We need to special case this since an empty cast of
1311         // a NullLiteral is still a Constant
1312         //
1313         public class NullCast : Constant {
1314                 protected Expression child;
1315                                 
1316                 public NullCast (Expression child, Type return_type)
1317                 {
1318                         eclass = child.eclass;
1319                         type = return_type;
1320                         this.child = child;
1321                 }
1322
1323                 override public string AsString ()
1324                 {
1325                         return "null";
1326                 }
1327
1328                 public override object GetValue ()
1329                 {
1330                         return null;
1331                 }
1332
1333                 public override Expression DoResolve (EmitContext ec)
1334                 {
1335                         // This should never be invoked, we are born in fully
1336                         // initialized state.
1337
1338                         return this;
1339                 }
1340
1341                 public override void Emit (EmitContext ec)
1342                 {
1343                         child.Emit (ec);
1344                 }
1345         }
1346
1347
1348         /// <summary>
1349         ///  This class is used to wrap literals which belong inside Enums
1350         /// </summary>
1351         public class EnumConstant : Constant {
1352                 public Constant Child;
1353
1354                 public EnumConstant (Constant child, Type enum_type)
1355                 {
1356                         eclass = child.eclass;
1357                         this.Child = child;
1358                         type = enum_type;
1359                 }
1360                 
1361                 public override Expression DoResolve (EmitContext ec)
1362                 {
1363                         // This should never be invoked, we are born in fully
1364                         // initialized state.
1365
1366                         return this;
1367                 }
1368
1369                 public override void Emit (EmitContext ec)
1370                 {
1371                         Child.Emit (ec);
1372                 }
1373
1374                 public override object GetValue ()
1375                 {
1376                         return Child.GetValue ();
1377                 }
1378
1379                 //
1380                 // Converts from one of the valid underlying types for an enumeration
1381                 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1382                 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1383                 //
1384                 public Constant WidenToCompilerConstant ()
1385                 {
1386                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1387                         object v = ((Constant) Child).GetValue ();;
1388                         
1389                         if (t == TypeManager.int32_type)
1390                                 return new IntConstant ((int) v);
1391                         if (t == TypeManager.uint32_type)
1392                                 return new UIntConstant ((uint) v);
1393                         if (t == TypeManager.int64_type)
1394                                 return new LongConstant ((long) v);
1395                         if (t == TypeManager.uint64_type)
1396                                 return new ULongConstant ((ulong) v);
1397                         if (t == TypeManager.short_type)
1398                                 return new ShortConstant ((short) v);
1399                         if (t == TypeManager.ushort_type)
1400                                 return new UShortConstant ((ushort) v);
1401                         if (t == TypeManager.byte_type)
1402                                 return new ByteConstant ((byte) v);
1403                         if (t == TypeManager.sbyte_type)
1404                                 return new SByteConstant ((sbyte) v);
1405
1406                         throw new Exception ("Invalid enumeration underlying type: " + t);
1407                 }
1408
1409                 //
1410                 // Extracts the value in the enumeration on its native representation
1411                 //
1412                 public object GetPlainValue ()
1413                 {
1414                         Type t = TypeManager.EnumToUnderlying (Child.Type);
1415                         object v = ((Constant) Child).GetValue ();;
1416                         
1417                         if (t == TypeManager.int32_type)
1418                                 return (int) v;
1419                         if (t == TypeManager.uint32_type)
1420                                 return (uint) v;
1421                         if (t == TypeManager.int64_type)
1422                                 return (long) v;
1423                         if (t == TypeManager.uint64_type)
1424                                 return (ulong) v;
1425                         if (t == TypeManager.short_type)
1426                                 return (short) v;
1427                         if (t == TypeManager.ushort_type)
1428                                 return (ushort) v;
1429                         if (t == TypeManager.byte_type)
1430                                 return (byte) v;
1431                         if (t == TypeManager.sbyte_type)
1432                                 return (sbyte) v;
1433
1434                         return null;
1435                 }
1436                 
1437                 public override string AsString ()
1438                 {
1439                         return Child.AsString ();
1440                 }
1441
1442                 public override DoubleConstant ConvertToDouble ()
1443                 {
1444                         return Child.ConvertToDouble ();
1445                 }
1446
1447                 public override FloatConstant ConvertToFloat ()
1448                 {
1449                         return Child.ConvertToFloat ();
1450                 }
1451
1452                 public override ULongConstant ConvertToULong ()
1453                 {
1454                         return Child.ConvertToULong ();
1455                 }
1456
1457                 public override LongConstant ConvertToLong ()
1458                 {
1459                         return Child.ConvertToLong ();
1460                 }
1461
1462                 public override UIntConstant ConvertToUInt ()
1463                 {
1464                         return Child.ConvertToUInt ();
1465                 }
1466
1467                 public override IntConstant ConvertToInt ()
1468                 {
1469                         return Child.ConvertToInt ();
1470                 }
1471         }
1472
1473         /// <summary>
1474         ///   This kind of cast is used to encapsulate Value Types in objects.
1475         ///
1476         ///   The effect of it is to box the value type emitted by the previous
1477         ///   operation.
1478         /// </summary>
1479         public class BoxedCast : EmptyCast {
1480
1481                 public BoxedCast (Expression expr)
1482                         : base (expr, TypeManager.object_type) 
1483                 {
1484                 }
1485
1486                 public BoxedCast (Expression expr, Type target_type)
1487                         : base (expr, target_type)
1488                 {
1489                 }
1490                 
1491                 public override Expression DoResolve (EmitContext ec)
1492                 {
1493                         // This should never be invoked, we are born in fully
1494                         // initialized state.
1495
1496                         return this;
1497                 }
1498
1499                 public override void Emit (EmitContext ec)
1500                 {
1501                         base.Emit (ec);
1502                         
1503                         ec.ig.Emit (OpCodes.Box, child.Type);
1504                 }
1505         }
1506
1507         public class UnboxCast : EmptyCast {
1508                 public UnboxCast (Expression expr, Type return_type)
1509                         : base (expr, return_type)
1510                 {
1511                 }
1512
1513                 public override Expression DoResolve (EmitContext ec)
1514                 {
1515                         // This should never be invoked, we are born in fully
1516                         // initialized state.
1517
1518                         return this;
1519                 }
1520
1521                 public override void Emit (EmitContext ec)
1522                 {
1523                         Type t = type;
1524                         ILGenerator ig = ec.ig;
1525                         
1526                         base.Emit (ec);
1527                         ig.Emit (OpCodes.Unbox, t);
1528
1529                         LoadFromPtr (ig, t);
1530                 }
1531         }
1532         
1533         /// <summary>
1534         ///   This is used to perform explicit numeric conversions.
1535         ///
1536         ///   Explicit numeric conversions might trigger exceptions in a checked
1537         ///   context, so they should generate the conv.ovf opcodes instead of
1538         ///   conv opcodes.
1539         /// </summary>
1540         public class ConvCast : EmptyCast {
1541                 public enum Mode : byte {
1542                         I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1543                         U1_I1, U1_CH,
1544                         I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1545                         U2_I1, U2_U1, U2_I2, U2_CH,
1546                         I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1547                         U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1548                         I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1549                         U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1550                         CH_I1, CH_U1, CH_I2,
1551                         R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1552                         R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1553                 }
1554
1555                 Mode mode;
1556                 bool checked_state;
1557                 
1558                 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1559                         : base (child, return_type)
1560                 {
1561                         checked_state = ec.CheckState;
1562                         mode = m;
1563                 }
1564
1565                 public override Expression DoResolve (EmitContext ec)
1566                 {
1567                         // This should never be invoked, we are born in fully
1568                         // initialized state.
1569
1570                         return this;
1571                 }
1572
1573                 public override string ToString ()
1574                 {
1575                         return String.Format ("ConvCast ({0}, {1})", mode, child);
1576                 }
1577                 
1578                 public override void Emit (EmitContext ec)
1579                 {
1580                         ILGenerator ig = ec.ig;
1581                         
1582                         base.Emit (ec);
1583
1584                         if (checked_state){
1585                                 switch (mode){
1586                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1587                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1588                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1589                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1590                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1591
1592                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1593                                 case Mode.U1_CH: /* nothing */ break;
1594
1595                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1596                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1597                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1598                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1599                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1600                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1601
1602                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1603                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1604                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1605                                 case Mode.U2_CH: /* nothing */ break;
1606
1607                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1608                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1609                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1610                                 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1611                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1612                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1613                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1614
1615                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1616                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1617                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1618                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1619                                 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1620                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1621
1622                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1623                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1624                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1625                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1626                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1627                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1628                                 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1629                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1630
1631                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1632                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1633                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1634                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1635                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1636                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1637                                 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1638                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1639
1640                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1641                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1642                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1643
1644                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1645                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1646                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1647                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1648                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1649                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1650                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1651                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1652                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1653
1654                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1655                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1656                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1657                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1658                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1659                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1660                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1661                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1662                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1663                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1664                                 }
1665                         } else {
1666                                 switch (mode){
1667                                 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1668                                 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1669                                 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1670                                 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1671                                 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1672
1673                                 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1674                                 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1675
1676                                 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1677                                 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1678                                 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1679                                 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1680                                 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1681                                 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1682
1683                                 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1684                                 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1685                                 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1686                                 case Mode.U2_CH: /* nothing */ break;
1687
1688                                 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1689                                 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1690                                 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1691                                 case Mode.I4_U4: /* nothing */ break;
1692                                 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1693                                 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1694                                 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1695
1696                                 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1697                                 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1698                                 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1699                                 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1700                                 case Mode.U4_I4: /* nothing */ break;
1701                                 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1702
1703                                 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1704                                 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1705                                 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1706                                 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1707                                 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1708                                 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1709                                 case Mode.I8_U8: /* nothing */ break;
1710                                 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1711
1712                                 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1713                                 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1714                                 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1715                                 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1716                                 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1717                                 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1718                                 case Mode.U8_I8: /* nothing */ break;
1719                                 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1720
1721                                 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1722                                 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1723                                 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1724
1725                                 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1726                                 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1727                                 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1728                                 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1729                                 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1730                                 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1731                                 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1732                                 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1733                                 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1734
1735                                 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1736                                 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1737                                 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1738                                 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1739                                 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1740                                 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1741                                 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1742                                 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1743                                 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1744                                 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1745                                 }
1746                         }
1747                 }
1748         }
1749         
1750         public class OpcodeCast : EmptyCast {
1751                 OpCode op, op2;
1752                 bool second_valid;
1753                 
1754                 public OpcodeCast (Expression child, Type return_type, OpCode op)
1755                         : base (child, return_type)
1756                         
1757                 {
1758                         this.op = op;
1759                         second_valid = false;
1760                 }
1761
1762                 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1763                         : base (child, return_type)
1764                         
1765                 {
1766                         this.op = op;
1767                         this.op2 = op2;
1768                         second_valid = true;
1769                 }
1770
1771                 public override Expression DoResolve (EmitContext ec)
1772                 {
1773                         // This should never be invoked, we are born in fully
1774                         // initialized state.
1775
1776                         return this;
1777                 }
1778
1779                 public override void Emit (EmitContext ec)
1780                 {
1781                         base.Emit (ec);
1782                         ec.ig.Emit (op);
1783
1784                         if (second_valid)
1785                                 ec.ig.Emit (op2);
1786                 }                       
1787         }
1788
1789         /// <summary>
1790         ///   This kind of cast is used to encapsulate a child and cast it
1791         ///   to the class requested
1792         /// </summary>
1793         public class ClassCast : EmptyCast {
1794                 public ClassCast (Expression child, Type return_type)
1795                         : base (child, return_type)
1796                         
1797                 {
1798                 }
1799
1800                 public override Expression DoResolve (EmitContext ec)
1801                 {
1802                         // This should never be invoked, we are born in fully
1803                         // initialized state.
1804
1805                         return this;
1806                 }
1807
1808                 public override void Emit (EmitContext ec)
1809                 {
1810                         base.Emit (ec);
1811
1812                         ec.ig.Emit (OpCodes.Castclass, type);
1813                 }                       
1814                 
1815         }
1816         
1817         /// <summary>
1818         ///   SimpleName expressions are initially formed of a single
1819         ///   word and it only happens at the beginning of the expression.
1820         /// </summary>
1821         ///
1822         /// <remarks>
1823         ///   The expression will try to be bound to a Field, a Method
1824         ///   group or a Property.  If those fail we pass the name to our
1825         ///   caller and the SimpleName is compounded to perform a type
1826         ///   lookup.  The idea behind this process is that we want to avoid
1827         ///   creating a namespace map from the assemblies, as that requires
1828         ///   the GetExportedTypes function to be called and a hashtable to
1829         ///   be constructed which reduces startup time.  If later we find
1830         ///   that this is slower, we should create a `NamespaceExpr' expression
1831         ///   that fully participates in the resolution process. 
1832         ///   
1833         ///   For example `System.Console.WriteLine' is decomposed into
1834         ///   MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1835         ///   
1836         ///   The first SimpleName wont produce a match on its own, so it will
1837         ///   be turned into:
1838         ///   MemberAccess (SimpleName ("System.Console"), "WriteLine").
1839         ///   
1840         ///   System.Console will produce a TypeExpr match.
1841         ///   
1842         ///   The downside of this is that we might be hitting `LookupType' too many
1843         ///   times with this scheme.
1844         /// </remarks>
1845         public class SimpleName : Expression {
1846                 public string Name;
1847
1848                 //
1849                 // If true, then we are a simple name, not composed with a ".
1850                 //
1851                 bool is_base;
1852
1853                 public SimpleName (string a, string b, Location l)
1854                 {
1855                         Name = String.Concat (a, ".", b);
1856                         loc = l;
1857                         is_base = false;
1858                 }
1859                 
1860                 public SimpleName (string name, Location l)
1861                 {
1862                         Name = name;
1863                         loc = l;
1864                         is_base = true;
1865                 }
1866
1867                 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1868                 {
1869                         if (ec.IsFieldInitializer)
1870                                 Report.Error (
1871                                         236, l,
1872                                         "A field initializer cannot reference the non-static field, " +
1873                                         "method or property `"+name+"'");
1874                         else
1875                                 Report.Error (
1876                                         120, l,
1877                                         "An object reference is required " +
1878                                         "for the non-static field `"+name+"'");
1879                 }
1880                 
1881                 //
1882                 // Checks whether we are trying to access an instance
1883                 // property, method or field from a static body.
1884                 //
1885                 Expression MemberStaticCheck (EmitContext ec, Expression e)
1886                 {
1887                         if (e is IMemberExpr){
1888                                 IMemberExpr member = (IMemberExpr) e;
1889                                 
1890                                 if (!member.IsStatic){
1891                                         Error_ObjectRefRequired (ec, loc, Name);
1892                                         return null;
1893                                 }
1894                         }
1895
1896                         return e;
1897                 }
1898                 
1899                 public override Expression DoResolve (EmitContext ec)
1900                 {
1901                         return SimpleNameResolve (ec, null, false);
1902                 }
1903
1904                 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1905                 {
1906                         return SimpleNameResolve (ec, right_side, false);
1907                 }
1908                 
1909
1910                 public Expression DoResolveAllowStatic (EmitContext ec)
1911                 {
1912                         return SimpleNameResolve (ec, null, true);
1913                 }
1914
1915                 public override Expression ResolveAsTypeStep (EmitContext ec)
1916                 {
1917                         DeclSpace ds = ec.DeclSpace;
1918                         Namespace ns = ds.Namespace;
1919                         Type t;
1920                         string alias_value;
1921
1922                         //
1923                         // Since we are cheating: we only do the Alias lookup for
1924                         // namespaces if the name does not include any dots in it
1925                         //
1926                         if (ns != null && is_base)
1927                                 alias_value = ns.LookupAlias (Name);
1928                         else
1929                                 alias_value = null;
1930
1931                         if (ec.ResolvingTypeTree){
1932                                 if (alias_value != null){
1933                                         if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1934                                                 return new TypeExpr (t, loc);
1935                                 }
1936
1937                                 int errors = Report.Errors;
1938                                 Type dt = ec.DeclSpace.FindType (loc, Name);
1939                                 
1940                                 if (Report.Errors != errors)
1941                                         return null;
1942                                 
1943                                 if (dt != null)
1944                                         return new TypeExpr (dt, loc);
1945                         }
1946
1947                         //
1948                         // First, the using aliases
1949                         //
1950                         if (alias_value != null){
1951                                 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1952                                         return new TypeExpr (t, loc);
1953                                 
1954                                 // we have alias value, but it isn't Type, so try if it's namespace
1955                                 return new SimpleName (alias_value, loc);
1956                         }
1957                         
1958                         //
1959                         // Stage 2: Lookup up if we are an alias to a type
1960                         // or a namespace.
1961                         //
1962
1963                         if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
1964                                 return new TypeExpr (t, loc);
1965                                 
1966                         // No match, maybe our parent can compose us
1967                         // into something meaningful.
1968                         return this;
1969                 }
1970
1971                 /// <remarks>
1972                 ///   7.5.2: Simple Names. 
1973                 ///
1974                 ///   Local Variables and Parameters are handled at
1975                 ///   parse time, so they never occur as SimpleNames.
1976                 ///
1977                 ///   The `allow_static' flag is used by MemberAccess only
1978                 ///   and it is used to inform us that it is ok for us to 
1979                 ///   avoid the static check, because MemberAccess might end
1980                 ///   up resolving the Name as a Type name and the access as
1981                 ///   a static type access.
1982                 ///
1983                 ///   ie: Type Type; .... { Type.GetType (""); }
1984                 ///
1985                 ///   Type is both an instance variable and a Type;  Type.GetType
1986                 ///   is the static method not an instance method of type.
1987                 /// </remarks>
1988                 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
1989                 {
1990                         Expression e = null;
1991
1992                         //
1993                         // Since we are cheating (is_base is our hint
1994                         // that we are the beginning of the name): we
1995                         // only do the Alias lookup for namespaces if
1996                         // the name does not include any dots in it
1997                         //
1998                         Namespace ns = ec.DeclSpace.Namespace;
1999                         if (is_base && ns != null){
2000                                 string alias_value = ns.LookupAlias (Name);
2001                                 if (alias_value != null){
2002                                         Name = alias_value;
2003                                         Type t;
2004
2005                                         if ((t = TypeManager.LookupType (Name)) != null)
2006                                                 return new TypeExpr (t, loc);
2007                                         
2008                                         // No match, maybe our parent can compose us
2009                                         // into something meaningful.
2010                                         return this;
2011                                 }
2012                         }
2013                         
2014                         
2015                         //
2016                         // Stage 1: Performed by the parser (binding to locals or parameters).
2017                         //
2018                         Block current_block = ec.CurrentBlock;
2019                         if (current_block != null){
2020                                 LocalInfo vi = current_block.GetLocalInfo (Name);
2021                                 if (vi != null){
2022                                         Expression var;
2023                                         
2024                                         var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2025                                         
2026                                         if (right_side != null)
2027                                                 return var.ResolveLValue (ec, right_side);
2028                                         else
2029                                                 return var.Resolve (ec);
2030                                 }
2031
2032                                 int idx = -1;
2033                                 Parameter par = null;
2034                                 Parameters pars = current_block.Parameters;
2035                                 if (pars != null)
2036                                         par = pars.GetParameterByName (Name, out idx);
2037
2038                                 if (par != null) {
2039                                         ParameterReference param;
2040                                         
2041                                         param = new ParameterReference (pars, current_block, idx, Name, loc);
2042
2043                                         if (right_side != null)
2044                                                 return param.ResolveLValue (ec, right_side);
2045                                         else
2046                                                 return param.Resolve (ec);
2047                                 }
2048                         }
2049                         
2050                         //
2051                         // Stage 2: Lookup members 
2052                         //
2053
2054                         DeclSpace lookup_ds = ec.DeclSpace;
2055                         do {
2056                                 if (lookup_ds.TypeBuilder == null)
2057                                         break;
2058
2059                                 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2060                                 if (e != null)
2061                                         break;
2062
2063                                 lookup_ds =lookup_ds.Parent;
2064                         } while (lookup_ds != null);
2065                                 
2066                         if (e == null && ec.ContainerType != null)
2067                                 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2068
2069                         if (e == null)
2070                                 return ResolveAsTypeStep (ec);
2071
2072                         if (e is TypeExpr)
2073                                 return e;
2074
2075                         if (e is IMemberExpr) {
2076                                 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2077                                 if (e == null)
2078                                         return null;
2079
2080                                 IMemberExpr me = e as IMemberExpr;
2081                                 if (me == null)
2082                                         return e;
2083
2084                                 // This fails if ResolveMemberAccess() was unable to decide whether
2085                                 // it's a field or a type of the same name.
2086                                 if (!me.IsStatic && (me.InstanceExpression == null))
2087                                         return e;
2088
2089                                 if (!me.IsStatic &&
2090                                     TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
2091                                     !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
2092                                         Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2093                                                "outer type `" + me.DeclaringType + "' via nested type `" +
2094                                                me.InstanceExpression.Type + "'");
2095                                         return null;
2096                                 }
2097
2098                                 if (right_side != null)
2099                                         e = e.DoResolveLValue (ec, right_side);
2100                                 else
2101                                         e = e.DoResolve (ec);
2102
2103                                 return e;                               
2104                         }
2105
2106                         if (ec.IsStatic || ec.IsFieldInitializer){
2107                                 if (allow_static)
2108                                         return e;
2109
2110                                 return MemberStaticCheck (ec, e);
2111                         } else
2112                                 return e;
2113                 }
2114                 
2115                 public override void Emit (EmitContext ec)
2116                 {
2117                         //
2118                         // If this is ever reached, then we failed to
2119                         // find the name as a namespace
2120                         //
2121
2122                         Error (103, "The name `" + Name +
2123                                "' does not exist in the class `" +
2124                                ec.DeclSpace.Name + "'");
2125                 }
2126
2127                 public override string ToString ()
2128                 {
2129                         return Name;
2130                 }
2131         }
2132         
2133         /// <summary>
2134         ///   Fully resolved expression that evaluates to a type
2135         /// </summary>
2136         public class TypeExpr : Expression {
2137                 public TypeExpr (Type t, Location l)
2138                 {
2139                         Type = t;
2140                         eclass = ExprClass.Type;
2141                         loc = l;
2142                 }
2143
2144                 public override Expression ResolveAsTypeStep (EmitContext ec)
2145                 {
2146                         return this;
2147                 }
2148
2149                 override public Expression DoResolve (EmitContext ec)
2150                 {
2151                         return this;
2152                 }
2153
2154                 override public void Emit (EmitContext ec)
2155                 {
2156                         throw new Exception ("Should never be called");
2157                 }
2158
2159                 public override string ToString ()
2160                 {
2161                         return Type.ToString ();
2162                 }
2163         }
2164
2165         /// <summary>
2166         ///   Used to create types from a fully qualified name.  These are just used
2167         ///   by the parser to setup the core types.  A TypeLookupExpression is always
2168         ///   classified as a type.
2169         /// </summary>
2170         public class TypeLookupExpression : TypeExpr {
2171                 string name;
2172                 
2173                 public TypeLookupExpression (string name) : base (null, Location.Null)
2174                 {
2175                         this.name = name;
2176                 }
2177
2178                 public override Expression ResolveAsTypeStep (EmitContext ec)
2179                 {
2180                         if (type == null)
2181                                 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
2182                         return this;
2183                 }
2184
2185                 public override Expression DoResolve (EmitContext ec)
2186                 {
2187                         return ResolveAsTypeStep (ec);
2188                 }
2189
2190                 public override void Emit (EmitContext ec)
2191                 {
2192                         throw new Exception ("Should never be called");
2193                 }
2194
2195                 public override string ToString ()
2196                 {
2197                         return name;
2198                 }
2199         }
2200
2201         /// <summary>
2202         ///   MethodGroup Expression.
2203         ///  
2204         ///   This is a fully resolved expression that evaluates to a type
2205         /// </summary>
2206         public class MethodGroupExpr : Expression, IMemberExpr {
2207                 public MethodBase [] Methods;
2208                 Expression instance_expression = null;
2209                 bool is_explicit_impl = false;
2210                 
2211                 public MethodGroupExpr (MemberInfo [] mi, Location l)
2212                 {
2213                         Methods = new MethodBase [mi.Length];
2214                         mi.CopyTo (Methods, 0);
2215                         eclass = ExprClass.MethodGroup;
2216                         type = TypeManager.object_type;
2217                         loc = l;
2218                 }
2219
2220                 public MethodGroupExpr (ArrayList list, Location l)
2221                 {
2222                         Methods = new MethodBase [list.Count];
2223
2224                         try {
2225                                 list.CopyTo (Methods, 0);
2226                         } catch {
2227                                 foreach (MemberInfo m in list){
2228                                         if (!(m is MethodBase)){
2229                                                 Console.WriteLine ("Name " + m.Name);
2230                                                 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2231                                         }
2232                                 }
2233                                 throw;
2234                         }
2235                         loc = l;
2236                         eclass = ExprClass.MethodGroup;
2237                         type = TypeManager.object_type;
2238                 }
2239
2240                 public Type DeclaringType {
2241                         get {
2242                                 return Methods [0].DeclaringType;
2243                         }
2244                 }
2245                 
2246                 //
2247                 // `A method group may have associated an instance expression' 
2248                 // 
2249                 public Expression InstanceExpression {
2250                         get {
2251                                 return instance_expression;
2252                         }
2253
2254                         set {
2255                                 instance_expression = value;
2256                         }
2257                 }
2258
2259                 public bool IsExplicitImpl {
2260                         get {
2261                                 return is_explicit_impl;
2262                         }
2263
2264                         set {
2265                                 is_explicit_impl = value;
2266                         }
2267                 }
2268
2269                 public string Name {
2270                         get {
2271                                 return Methods [0].Name;
2272                         }
2273                 }
2274
2275                 public bool IsInstance {
2276                         get {
2277                                 foreach (MethodBase mb in Methods)
2278                                         if (!mb.IsStatic)
2279                                                 return true;
2280
2281                                 return false;
2282                         }
2283                 }
2284
2285                 public bool IsStatic {
2286                         get {
2287                                 foreach (MethodBase mb in Methods)
2288                                         if (mb.IsStatic)
2289                                                 return true;
2290
2291                                 return false;
2292                         }
2293                 }
2294                 
2295                 override public Expression DoResolve (EmitContext ec)
2296                 {
2297                         if (!IsInstance)
2298                                 instance_expression = null;
2299
2300                         if (instance_expression != null) {
2301                                 instance_expression = instance_expression.DoResolve (ec);
2302                                 if (instance_expression == null)
2303                                         return null;
2304                         }
2305
2306                         return this;
2307                 }
2308
2309                 public void ReportUsageError ()
2310                 {
2311                         Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
2312                                       Methods [0].Name + "()' is referenced without parentheses");
2313                 }
2314
2315                 override public void Emit (EmitContext ec)
2316                 {
2317                         ReportUsageError ();
2318                 }
2319
2320                 bool RemoveMethods (bool keep_static)
2321                 {
2322                         ArrayList smethods = new ArrayList ();
2323
2324                         foreach (MethodBase mb in Methods){
2325                                 if (mb.IsStatic == keep_static)
2326                                         smethods.Add (mb);
2327                         }
2328
2329                         if (smethods.Count == 0)
2330                                 return false;
2331
2332                         Methods = new MethodBase [smethods.Count];
2333                         smethods.CopyTo (Methods, 0);
2334
2335                         return true;
2336                 }
2337                 
2338                 /// <summary>
2339                 ///   Removes any instance methods from the MethodGroup, returns
2340                 ///   false if the resulting set is empty.
2341                 /// </summary>
2342                 public bool RemoveInstanceMethods ()
2343                 {
2344                         return RemoveMethods (true);
2345                 }
2346
2347                 /// <summary>
2348                 ///   Removes any static methods from the MethodGroup, returns
2349                 ///   false if the resulting set is empty.
2350                 /// </summary>
2351                 public bool RemoveStaticMethods ()
2352                 {
2353                         return RemoveMethods (false);
2354                 }
2355         }
2356
2357         /// <summary>
2358         ///   Fully resolved expression that evaluates to a Field
2359         /// </summary>
2360         public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2361                 public readonly FieldInfo FieldInfo;
2362                 Expression instance_expr;
2363                 VariableInfo variable_info;
2364                 
2365                 public FieldExpr (FieldInfo fi, Location l)
2366                 {
2367                         FieldInfo = fi;
2368                         eclass = ExprClass.Variable;
2369                         type = fi.FieldType;
2370                         loc = l;
2371                 }
2372
2373                 public string Name {
2374                         get {
2375                                 return FieldInfo.Name;
2376                         }
2377                 }
2378
2379                 public bool IsInstance {
2380                         get {
2381                                 return !FieldInfo.IsStatic;
2382                         }
2383                 }
2384
2385                 public bool IsStatic {
2386                         get {
2387                                 return FieldInfo.IsStatic;
2388                         }
2389                 }
2390
2391                 public Type DeclaringType {
2392                         get {
2393                                 return FieldInfo.DeclaringType;
2394                         }
2395                 }
2396
2397                 public Expression InstanceExpression {
2398                         get {
2399                                 return instance_expr;
2400                         }
2401
2402                         set {
2403                                 instance_expr = value;
2404                         }
2405                 }
2406
2407                 public VariableInfo VariableInfo {
2408                         get {
2409                                 return variable_info;
2410                         }
2411                 }
2412
2413                 override public Expression DoResolve (EmitContext ec)
2414                 {
2415                         if (!FieldInfo.IsStatic){
2416                                 if (instance_expr == null){
2417                                         //
2418                                         // This can happen when referencing an instance field using
2419                                         // a fully qualified type expression: TypeName.InstanceField = xxx
2420                                         // 
2421                                         SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2422                                         return null;
2423                                 }
2424
2425                                 // Resolve the field's instance expression while flow analysis is turned
2426                                 // off: when accessing a field "a.b", we must check whether the field
2427                                 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2428                                 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2429                                                                        ResolveFlags.DisableFlowAnalysis);
2430                                 if (instance_expr == null)
2431                                         return null;
2432                         }
2433
2434                         // If the instance expression is a local variable or parameter.
2435                         IVariable var = instance_expr as IVariable;
2436                         if ((var == null) || (var.VariableInfo == null))
2437                                 return this;
2438
2439                         VariableInfo vi = var.VariableInfo;
2440                         if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2441                                 return null;
2442
2443                         variable_info = vi.GetSubStruct (FieldInfo.Name);
2444                         return this;
2445                 }
2446
2447                 void Report_AssignToReadonly (bool is_instance)
2448                 {
2449                         string msg;
2450                         
2451                         if (is_instance)
2452                                 msg = "Readonly field can not be assigned outside " +
2453                                 "of constructor or variable initializer";
2454                         else
2455                                 msg = "A static readonly field can only be assigned in " +
2456                                 "a static constructor";
2457
2458                         Report.Error (is_instance ? 191 : 198, loc, msg);
2459                 }
2460                 
2461                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2462                 {
2463                         IVariable var = instance_expr as IVariable;
2464                         if ((var != null) && (var.VariableInfo != null))
2465                                 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2466
2467                         Expression e = DoResolve (ec);
2468
2469                         if (e == null)
2470                                 return null;
2471                         
2472                         if (!FieldInfo.IsInitOnly)
2473                                 return this;
2474
2475                         //
2476                         // InitOnly fields can only be assigned in constructors
2477                         //
2478
2479                         if (ec.IsConstructor){
2480                                 if (ec.ContainerType == FieldInfo.DeclaringType)
2481                                         return this;
2482                         }
2483
2484                         Report_AssignToReadonly (true);
2485                         
2486                         return null;
2487                 }
2488
2489                 override public void Emit (EmitContext ec)
2490                 {
2491                         ILGenerator ig = ec.ig;
2492                         bool is_volatile = false;
2493
2494                         if (FieldInfo is FieldBuilder){
2495                                 FieldBase f = TypeManager.GetField (FieldInfo);
2496
2497                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2498                                         is_volatile = true;
2499                                 
2500                                 f.status |= Field.Status.USED;
2501                         } 
2502                         
2503                         if (FieldInfo.IsStatic){
2504                                 if (is_volatile)
2505                                         ig.Emit (OpCodes.Volatile);
2506                                 
2507                                 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2508                                 return;
2509                         }
2510                         
2511                         if (instance_expr.Type.IsValueType){
2512                                 IMemoryLocation ml;
2513                                 LocalTemporary tempo = null;
2514                                 
2515                                 if (!(instance_expr is IMemoryLocation)){
2516                                         tempo = new LocalTemporary (ec, instance_expr.Type);
2517                                         
2518                                         if (ec.RemapToProxy)
2519                                                 ec.EmitThis ();
2520                         
2521                                         InstanceExpression.Emit (ec);
2522                                         tempo.Store (ec);
2523                                         ml = tempo;
2524                                 } else
2525                                         ml = (IMemoryLocation) instance_expr;
2526                                 
2527                                 ml.AddressOf (ec, AddressOp.Load);
2528                         } else {
2529                                 if (ec.RemapToProxy)
2530                                         ec.EmitThis ();
2531                                 else
2532                                         instance_expr.Emit (ec);
2533                         }
2534                         if (is_volatile)
2535                                 ig.Emit (OpCodes.Volatile);
2536                         
2537                         ig.Emit (OpCodes.Ldfld, FieldInfo);
2538                 }
2539
2540                 public void EmitAssign (EmitContext ec, Expression source)
2541                 {
2542                         FieldAttributes fa = FieldInfo.Attributes;
2543                         bool is_static = (fa & FieldAttributes.Static) != 0;
2544                         bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
2545                         ILGenerator ig = ec.ig;
2546
2547                         if (is_readonly && !ec.IsConstructor){
2548                                 Report_AssignToReadonly (!is_static);
2549                                 return;
2550                         }
2551                         
2552                         if (!is_static){
2553                                 Expression instance = instance_expr;
2554
2555                                 if (instance.Type.IsValueType){
2556                                         if (instance is IMemoryLocation){
2557                                                 IMemoryLocation ml = (IMemoryLocation) instance;
2558
2559                                                 ml.AddressOf (ec, AddressOp.Store);
2560                                         } else
2561                                                 throw new Exception ("The " + instance + " of type " +
2562                                                                      instance.Type +
2563                                                                      " represents a ValueType and does " +
2564                                                                      "not implement IMemoryLocation");
2565                                 } else {
2566                                         if (ec.RemapToProxy)
2567                                                 ec.EmitThis ();
2568                                         else
2569                                                 instance.Emit (ec);
2570                                 }
2571                         }
2572
2573                         source.Emit (ec);
2574
2575                         if (FieldInfo is FieldBuilder){
2576                                 FieldBase f = TypeManager.GetField (FieldInfo);
2577                                 
2578                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2579                                         ig.Emit (OpCodes.Volatile);
2580                                 
2581                                 f.status |= Field.Status.ASSIGNED;
2582                         } 
2583
2584                         if (is_static)
2585                                 ig.Emit (OpCodes.Stsfld, FieldInfo);
2586                         else 
2587                                 ig.Emit (OpCodes.Stfld, FieldInfo);
2588                 }
2589                 
2590                 public void AddressOf (EmitContext ec, AddressOp mode)
2591                 {
2592                         ILGenerator ig = ec.ig;
2593                         
2594                         if (FieldInfo is FieldBuilder){
2595                                 FieldBase f = TypeManager.GetField (FieldInfo);
2596                                 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
2597                                         Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
2598                                         return;
2599                                 }
2600                                 
2601                                 if ((mode & AddressOp.Store) != 0)
2602                                         f.status |= Field.Status.ASSIGNED;
2603                                 if ((mode & AddressOp.Load) != 0)
2604                                         f.status |= Field.Status.USED;
2605                         } 
2606
2607                         //
2608                         // Handle initonly fields specially: make a copy and then
2609                         // get the address of the copy.
2610                         //
2611                         if (FieldInfo.IsInitOnly && !ec.IsConstructor){
2612                                 LocalBuilder local;
2613                                 
2614                                 Emit (ec);
2615                                 local = ig.DeclareLocal (type);
2616                                 ig.Emit (OpCodes.Stloc, local);
2617                                 ig.Emit (OpCodes.Ldloca, local);
2618                                 return;
2619                         }
2620
2621                         if (FieldInfo.IsStatic)
2622                                 ig.Emit (OpCodes.Ldsflda, FieldInfo);
2623                         else {
2624                                 //
2625                                 // In the case of `This', we call the AddressOf method, which will
2626                                 // only load the pointer, and not perform an Ldobj immediately after
2627                                 // the value has been loaded into the stack.
2628                                 //
2629                                 if (instance_expr is This)
2630                                         ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
2631                                 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
2632                                         IMemoryLocation ml = (IMemoryLocation) instance_expr;
2633
2634                                         ml.AddressOf (ec, AddressOp.LoadStore);
2635                                 } else
2636                                         instance_expr.Emit (ec);
2637                                 ig.Emit (OpCodes.Ldflda, FieldInfo);
2638                         }
2639                 }
2640         }
2641
2642         /// <summary>
2643         ///   Expression that evaluates to a Property.  The Assign class
2644         ///   might set the `Value' expression if we are in an assignment.
2645         ///
2646         ///   This is not an LValue because we need to re-write the expression, we
2647         ///   can not take data from the stack and store it.  
2648         /// </summary>
2649         public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
2650                 public readonly PropertyInfo PropertyInfo;
2651
2652                 //
2653                 // This is set externally by the  `BaseAccess' class
2654                 //
2655                 public bool IsBase;
2656                 MethodInfo getter, setter;
2657                 bool is_static;
2658                 bool must_do_cs1540_check;
2659                 
2660                 Expression instance_expr;
2661
2662                 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
2663                 {
2664                         PropertyInfo = pi;
2665                         eclass = ExprClass.PropertyAccess;
2666                         is_static = false;
2667                         loc = l;
2668
2669                         type = TypeManager.TypeToCoreType (pi.PropertyType);
2670
2671                         ResolveAccessors (ec);
2672                 }
2673
2674                 public string Name {
2675                         get {
2676                                 return PropertyInfo.Name;
2677                         }
2678                 }
2679
2680                 public bool IsInstance {
2681                         get {
2682                                 return !is_static;
2683                         }
2684                 }
2685
2686                 public bool IsStatic {
2687                         get {
2688                                 return is_static;
2689                         }
2690                 }
2691                 
2692                 public Type DeclaringType {
2693                         get {
2694                                 return PropertyInfo.DeclaringType;
2695                         }
2696                 }
2697
2698                 //
2699                 // The instance expression associated with this expression
2700                 //
2701                 public Expression InstanceExpression {
2702                         set {
2703                                 instance_expr = value;
2704                         }
2705
2706                         get {
2707                                 return instance_expr;
2708                         }
2709                 }
2710
2711                 public bool VerifyAssignable ()
2712                 {
2713                         if (setter == null) {
2714                                 Report.Error (200, loc, 
2715                                               "The property `" + PropertyInfo.Name +
2716                                               "' can not be assigned to, as it has not set accessor");
2717                                 return false;
2718                         }
2719
2720                         return true;
2721                 }
2722
2723                 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
2724                 {
2725                         BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
2726                                 BindingFlags.Static | BindingFlags.Instance;
2727                         MemberInfo[] group;
2728
2729                         group = TypeManager.MemberLookup (
2730                                 invocation_type, invocation_type, PropertyInfo.DeclaringType,
2731                                 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
2732
2733                         //
2734                         // The first method is the closest to us
2735                         //
2736                         if (group == null)
2737                                 return null;
2738
2739                         foreach (MethodInfo mi in group) {
2740                                 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
2741
2742                                 //
2743                                 // If only accessible to the current class or children
2744                                 //
2745                                 if (ma == MethodAttributes.Private) {
2746                                         Type declaring_type = mi.DeclaringType;
2747                                         
2748                                         if (invocation_type != declaring_type){
2749                                                 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2750                                                         return mi;
2751                                                 else
2752                                                         continue;
2753                                         } else
2754                                                 return mi;
2755                                 }
2756                                 //
2757                                 // FamAndAssem requires that we not only derivate, but we are on the
2758                                 // same assembly.  
2759                                 //
2760                                 if (ma == MethodAttributes.FamANDAssem){
2761                                         if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2762                                                 continue;
2763                                         else
2764                                                 return mi;
2765                                 }
2766
2767                                 // Assembly and FamORAssem succeed if we're in the same assembly.
2768                                 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2769                                         if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2770                                                 continue;
2771                                         else
2772                                                 return mi;
2773                                 }
2774
2775                                 // We already know that we aren't in the same assembly.
2776                                 if (ma == MethodAttributes.Assembly)
2777                                         continue;
2778
2779                                 // Family and FamANDAssem require that we derive.
2780                                 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2781                                         if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2782                                                 continue;
2783                                         else {
2784                                                 must_do_cs1540_check = true;
2785
2786                                                 return mi;
2787                                         }
2788                                 }
2789
2790                                 return mi;
2791                         }
2792
2793                         return null;
2794                 }
2795
2796                 //
2797                 // We also perform the permission checking here, as the PropertyInfo does not
2798                 // hold the information for the accessibility of its setter/getter
2799                 //
2800                 void ResolveAccessors (EmitContext ec)
2801                 {
2802                         getter = GetAccessor (ec.ContainerType, "get");
2803                         if ((getter != null) && getter.IsStatic)
2804                                 is_static = true;
2805
2806                         setter = GetAccessor (ec.ContainerType, "set");
2807                         if ((setter != null) && setter.IsStatic)
2808                                 is_static = true;
2809
2810                         if (setter == null && getter == null){
2811                                 Error (122, "`" + PropertyInfo.Name + "' " +
2812                                        "is inaccessible because of its protection level");
2813                                 
2814                         }
2815                 }
2816
2817                 bool InstanceResolve (EmitContext ec)
2818                 {
2819                         if ((instance_expr == null) && ec.IsStatic && !is_static) {
2820                                 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
2821                                 return false;
2822                         }
2823
2824                         if (instance_expr != null) {
2825                                 instance_expr = instance_expr.DoResolve (ec);
2826                                 if (instance_expr == null)
2827                                         return false;
2828                         }
2829
2830                         if (must_do_cs1540_check && (instance_expr != null)) {
2831                                 if ((instance_expr.Type != ec.ContainerType) &&
2832                                     ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
2833                                         Report.Error (1540, loc, "Cannot access protected member `" +
2834                                                       PropertyInfo.DeclaringType + "." + PropertyInfo.Name + 
2835                                                       "' via a qualifier of type `" +
2836                                                       TypeManager.CSharpName (instance_expr.Type) +
2837                                                       "'; the qualifier must be of type `" +
2838                                                       TypeManager.CSharpName (ec.ContainerType) +
2839                                                       "' (or derived from it)");
2840                                         return false;
2841                                 }
2842                         }
2843
2844                         return true;
2845                 }
2846                 
2847                 override public Expression DoResolve (EmitContext ec)
2848                 {
2849                         if (getter == null){
2850                                 //
2851                                 // The following condition happens if the PropertyExpr was
2852                                 // created, but is invalid (ie, the property is inaccessible),
2853                                 // and we did not want to embed the knowledge about this in
2854                                 // the caller routine.  This only avoids double error reporting.
2855                                 //
2856                                 if (setter == null)
2857                                         return null;
2858                                 
2859                                 Report.Error (154, loc, 
2860                                               "The property `" + PropertyInfo.Name +
2861                                               "' can not be used in " +
2862                                               "this context because it lacks a get accessor");
2863                                 return null;
2864                         } 
2865
2866                         if (!InstanceResolve (ec))
2867                                 return null;
2868
2869                         //
2870                         // Only base will allow this invocation to happen.
2871                         //
2872                         if (IsBase && getter.IsAbstract){
2873                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
2874                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2875                                 return null;
2876                         }
2877
2878                         return this;
2879                 }
2880
2881                 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2882                 {
2883                         if (setter == null){
2884                                 //
2885                                 // The following condition happens if the PropertyExpr was
2886                                 // created, but is invalid (ie, the property is inaccessible),
2887                                 // and we did not want to embed the knowledge about this in
2888                                 // the caller routine.  This only avoids double error reporting.
2889                                 //
2890                                 if (getter == null)
2891                                         return null;
2892                                 
2893                                 Report.Error (154, loc, 
2894                                               "The property `" + PropertyInfo.Name +
2895                                               "' can not be used in " +
2896                                               "this context because it lacks a set accessor");
2897                                 return null;
2898                         }
2899
2900                         if (!InstanceResolve (ec))
2901                                 return null;
2902                         
2903                         //
2904                         // Only base will allow this invocation to happen.
2905                         //
2906                         if (IsBase && setter.IsAbstract){
2907                                 Report.Error (205, loc, "Cannot call an abstract base property: " +
2908                                               PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2909                                 return null;
2910                         }
2911                         return this;
2912                 }
2913
2914                 override public void Emit (EmitContext ec)
2915                 {
2916                         //
2917                         // Special case: length of single dimension array property is turned into ldlen
2918                         //
2919                         if ((getter == TypeManager.system_int_array_get_length) ||
2920                             (getter == TypeManager.int_array_get_length)){
2921                                 Type iet = instance_expr.Type;
2922
2923                                 //
2924                                 // System.Array.Length can be called, but the Type does not
2925                                 // support invoking GetArrayRank, so test for that case first
2926                                 //
2927                                 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
2928                                         instance_expr.Emit (ec);
2929                                         ec.ig.Emit (OpCodes.Ldlen);
2930                                         return;
2931                                 }
2932                         }
2933
2934                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
2935                         
2936                 }
2937
2938                 //
2939                 // Implements the IAssignMethod interface for assignments
2940                 //
2941                 public void EmitAssign (EmitContext ec, Expression source)
2942                 {
2943                         Argument arg = new Argument (source, Argument.AType.Expression);
2944                         ArrayList args = new ArrayList ();
2945
2946                         args.Add (arg);
2947                         Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
2948                 }
2949
2950                 override public void EmitStatement (EmitContext ec)
2951                 {
2952                         Emit (ec);
2953                         ec.ig.Emit (OpCodes.Pop);
2954                 }
2955         }
2956
2957         /// <summary>
2958         ///   Fully resolved expression that evaluates to an Event
2959         /// </summary>
2960         public class EventExpr : Expression, IMemberExpr {
2961                 public readonly EventInfo EventInfo;
2962                 public Expression instance_expr;
2963
2964                 bool is_static;
2965                 MethodInfo add_accessor, remove_accessor;
2966                 
2967                 public EventExpr (EventInfo ei, Location loc)
2968                 {
2969                         EventInfo = ei;
2970                         this.loc = loc;
2971                         eclass = ExprClass.EventAccess;
2972
2973                         add_accessor = TypeManager.GetAddMethod (ei);
2974                         remove_accessor = TypeManager.GetRemoveMethod (ei);
2975                         
2976                         if (add_accessor.IsStatic || remove_accessor.IsStatic)
2977                                 is_static = true;
2978
2979                         if (EventInfo is MyEventBuilder){
2980                                 MyEventBuilder eb = (MyEventBuilder) EventInfo;
2981                                 type = eb.EventType;
2982                                 eb.SetUsed ();
2983                         } else
2984                                 type = EventInfo.EventHandlerType;
2985                 }
2986
2987                 public string Name {
2988                         get {
2989                                 return EventInfo.Name;
2990                         }
2991                 }
2992
2993                 public bool IsInstance {
2994                         get {
2995                                 return !is_static;
2996                         }
2997                 }
2998
2999                 public bool IsStatic {
3000                         get {
3001                                 return is_static;
3002                         }
3003                 }
3004
3005                 public Type DeclaringType {
3006                         get {
3007                                 return EventInfo.DeclaringType;
3008                         }
3009                 }
3010
3011                 public Expression InstanceExpression {
3012                         get {
3013                                 return instance_expr;
3014                         }
3015
3016                         set {
3017                                 instance_expr = value;
3018                         }
3019                 }
3020
3021                 public override Expression DoResolve (EmitContext ec)
3022                 {
3023                         if (instance_expr != null) {
3024                                 instance_expr = instance_expr.DoResolve (ec);
3025                                 if (instance_expr == null)
3026                                         return null;
3027                         }
3028
3029                         
3030                         return this;
3031                 }
3032
3033                 public override void Emit (EmitContext ec)
3034                 {
3035                         Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3036                 }
3037
3038                 public void EmitAddOrRemove (EmitContext ec, Expression source)
3039                 {
3040                         Expression handler = ((Binary) source).Right;
3041                         
3042                         Argument arg = new Argument (handler, Argument.AType.Expression);
3043                         ArrayList args = new ArrayList ();
3044                                 
3045                         args.Add (arg);
3046                         
3047                         if (((Binary) source).Oper == Binary.Operator.Addition)
3048                                 Invocation.EmitCall (
3049                                         ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3050                         else
3051                                 Invocation.EmitCall (
3052                                         ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
3053                 }
3054         }
3055 }