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