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