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