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