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