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