2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
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,
25 public enum ExprClass : byte {
40 /// This is used to tell Resolve in which types of expressions we're
44 public enum ResolveFlags {
45 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48 // Returns a type expression.
51 // Returns a method group.
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).
59 // Mask of all the expression class flags.
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
68 // This is just as a hint to AddressOf of what will be done with the
71 public enum AddressOp {
78 /// This interface is implemented by variables
80 public interface IMemoryLocation {
82 /// The AddressOf method should generate code that loads
83 /// the address of the object and leaves it on the stack.
85 /// The `mode' argument is used to notify the expression
86 /// of whether this will be used to read from the address or
87 /// write to the address.
89 /// This is just a hint that can be used to provide good error
90 /// reporting, and should have no other side effects.
92 void AddressOf (EmitContext ec, AddressOp mode);
96 /// This interface is implemented by variables
98 public interface IVariable {
99 VariableInfo VariableInfo {
103 bool VerifyFixed (bool is_expression);
107 /// This interface denotes an expression which evaluates to a member
108 /// of a struct or a class.
110 public interface IMemberExpr
113 /// The name of this member.
120 /// Whether this is an instance member.
127 /// Whether this is a static member.
134 /// The type which declares this member.
141 /// The instance expression associated with this member, if it's a
142 /// non-static member.
144 Expression InstanceExpression {
150 /// Base class for expressions
152 public abstract class Expression {
153 public ExprClass eclass;
155 protected Location loc;
167 public Location Location {
174 /// Utility wrapper routine for Error, just to beautify the code
176 public void Error (int error, string s)
178 if (!Location.IsNull (loc))
179 Report.Error (error, loc, s);
181 Report.Error (error, s);
185 /// Utility wrapper routine for Warning, just to beautify the code
187 public void Warning (int warning, string s)
189 if (!Location.IsNull (loc))
190 Report.Warning (warning, loc, s);
192 Report.Warning (warning, s);
196 /// Utility wrapper routine for Warning, only prints the warning if
197 /// warnings of level `level' are enabled.
199 public void Warning (int warning, int level, string s)
201 if (level <= RootContext.WarningLevel)
202 Warning (warning, s);
206 /// Performs semantic analysis on the Expression
210 /// The Resolve method is invoked to perform the semantic analysis
213 /// The return value is an expression (it can be the
214 /// same expression in some cases) or a new
215 /// expression that better represents this node.
217 /// For example, optimizations of Unary (LiteralInt)
218 /// would return a new LiteralInt with a negated
221 /// If there is an error during semantic analysis,
222 /// then an error should be reported (using Report)
223 /// and a null value should be returned.
225 /// There are two side effects expected from calling
226 /// Resolve(): the the field variable "eclass" should
227 /// be set to any value of the enumeration
228 /// `ExprClass' and the type variable should be set
229 /// to a valid type (this is the type of the
232 public abstract Expression DoResolve (EmitContext ec);
234 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
236 return DoResolve (ec);
240 // This is used if the expression should be resolved as a type.
241 // the default implementation fails. Use this method in
242 // those participants in the SimpleName chain system.
244 public virtual Expression ResolveAsTypeStep (EmitContext ec)
250 // This is used to resolve the expression as a type, a null
251 // value will be returned if the expression is not a type
254 public Expression ResolveAsTypeTerminal (EmitContext ec)
256 Expression e = ResolveAsTypeStep (ec);
266 /// Resolves an expression and performs semantic analysis on it.
270 /// Currently Resolve wraps DoResolve to perform sanity
271 /// checking and assertion checking on what we expect from Resolve.
273 public Expression Resolve (EmitContext ec, ResolveFlags flags)
275 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
276 return ResolveAsTypeStep (ec);
278 bool old_do_flow_analysis = ec.DoFlowAnalysis;
279 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
280 ec.DoFlowAnalysis = false;
283 if (this is SimpleName)
284 e = ((SimpleName) this).DoResolveAllowStatic (ec);
288 ec.DoFlowAnalysis = old_do_flow_analysis;
293 if (e is SimpleName){
294 SimpleName s = (SimpleName) e;
296 if ((flags & ResolveFlags.SimpleName) == 0) {
297 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
298 ec.DeclSpace.Name, loc);
305 if ((e is TypeExpr) || (e is ComposedCast)) {
306 if ((flags & ResolveFlags.Type) == 0) {
307 e.Error_UnexpectedKind (flags);
316 if ((flags & ResolveFlags.VariableOrValue) == 0) {
317 e.Error_UnexpectedKind (flags);
322 case ExprClass.MethodGroup:
323 if ((flags & ResolveFlags.MethodGroup) == 0) {
324 ((MethodGroupExpr) e).ReportUsageError ();
329 case ExprClass.Value:
330 case ExprClass.Variable:
331 case ExprClass.PropertyAccess:
332 case ExprClass.EventAccess:
333 case ExprClass.IndexerAccess:
334 if ((flags & ResolveFlags.VariableOrValue) == 0) {
335 Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
336 Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
337 FieldInfo fi = ((FieldExpr) e).FieldInfo;
339 Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
340 e.Error_UnexpectedKind (flags);
346 throw new Exception ("Expression " + e.GetType () +
347 " ExprClass is Invalid after resolve");
351 throw new Exception (
352 "Expression " + e.GetType () +
353 " did not set its type after Resolve\n" +
354 "called from: " + this.GetType ());
360 /// Resolves an expression and performs semantic analysis on it.
362 public Expression Resolve (EmitContext ec)
364 return Resolve (ec, ResolveFlags.VariableOrValue);
368 /// Resolves an expression for LValue assignment
372 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
373 /// checking and assertion checking on what we expect from Resolve
375 public Expression ResolveLValue (EmitContext ec, Expression right_side)
377 Expression e = DoResolveLValue (ec, right_side);
380 if (e is SimpleName){
381 SimpleName s = (SimpleName) e;
382 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
383 ec.DeclSpace.Name, loc);
387 if (e.eclass == ExprClass.Invalid)
388 throw new Exception ("Expression " + e +
389 " ExprClass is Invalid after resolve");
391 if (e.eclass == ExprClass.MethodGroup) {
392 ((MethodGroupExpr) e).ReportUsageError ();
397 throw new Exception ("Expression " + e +
398 " did not set its type after Resolve");
405 /// Emits the code for the expression
409 /// The Emit method is invoked to generate the code
410 /// for the expression.
412 public abstract void Emit (EmitContext ec);
415 /// Protected constructor. Only derivate types should
416 /// be able to be created
419 protected Expression ()
421 eclass = ExprClass.Invalid;
426 /// Returns a literalized version of a literal FieldInfo
430 /// The possible return values are:
431 /// IntConstant, UIntConstant
432 /// LongLiteral, ULongConstant
433 /// FloatConstant, DoubleConstant
436 /// The value returned is already resolved.
438 public static Constant Constantify (object v, Type t)
440 if (t == TypeManager.int32_type)
441 return new IntConstant ((int) v);
442 else if (t == TypeManager.uint32_type)
443 return new UIntConstant ((uint) v);
444 else if (t == TypeManager.int64_type)
445 return new LongConstant ((long) v);
446 else if (t == TypeManager.uint64_type)
447 return new ULongConstant ((ulong) v);
448 else if (t == TypeManager.float_type)
449 return new FloatConstant ((float) v);
450 else if (t == TypeManager.double_type)
451 return new DoubleConstant ((double) v);
452 else if (t == TypeManager.string_type)
453 return new StringConstant ((string) v);
454 else if (t == TypeManager.short_type)
455 return new ShortConstant ((short)v);
456 else if (t == TypeManager.ushort_type)
457 return new UShortConstant ((ushort)v);
458 else if (t == TypeManager.sbyte_type)
459 return new SByteConstant (((sbyte)v));
460 else if (t == TypeManager.byte_type)
461 return new ByteConstant ((byte)v);
462 else if (t == TypeManager.char_type)
463 return new CharConstant ((char)v);
464 else if (t == TypeManager.bool_type)
465 return new BoolConstant ((bool) v);
466 else if (TypeManager.IsEnumType (t)){
467 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
469 return new EnumConstant (e, t);
471 throw new Exception ("Unknown type for constant (" + t +
476 /// Returns a fully formed expression after a MemberLookup
478 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
481 return new EventExpr ((EventInfo) mi, loc);
482 else if (mi is FieldInfo)
483 return new FieldExpr ((FieldInfo) mi, loc);
484 else if (mi is PropertyInfo)
485 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
486 else if (mi is Type){
487 return new TypeExpr ((System.Type) mi, loc);
494 // FIXME: Probably implement a cache for (t,name,current_access_set)?
496 // This code could use some optimizations, but we need to do some
497 // measurements. For example, we could use a delegate to `flag' when
498 // something can not any longer be a method-group (because it is something
502 // If the return value is an Array, then it is an array of
505 // If the return value is an MemberInfo, it is anything, but a Method
509 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
510 // the arguments here and have MemberLookup return only the methods that
511 // match the argument count/type, unlike we are doing now (we delay this
514 // This is so we can catch correctly attempts to invoke instance methods
515 // from a static body (scan for error 120 in ResolveSimpleName).
518 // FIXME: Potential optimization, have a static ArrayList
521 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
522 MemberTypes mt, BindingFlags bf, Location loc)
524 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
528 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
529 // `qualifier_type' or null to lookup members in the current class.
532 public static Expression MemberLookup (EmitContext ec, Type container_type,
533 Type qualifier_type, Type queried_type,
534 string name, MemberTypes mt,
535 BindingFlags bf, Location loc)
537 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
538 queried_type, mt, bf, name);
543 int count = mi.Length;
545 if (mi [0] is MethodBase)
546 return new MethodGroupExpr (mi, loc);
551 return ExprClassFromMemberInfo (ec, mi [0], loc);
554 public const MemberTypes AllMemberTypes =
555 MemberTypes.Constructor |
559 MemberTypes.NestedType |
560 MemberTypes.Property;
562 public const BindingFlags AllBindingFlags =
563 BindingFlags.Public |
564 BindingFlags.Static |
565 BindingFlags.Instance;
567 public static Expression MemberLookup (EmitContext ec, Type queried_type,
568 string name, Location loc)
570 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
571 AllMemberTypes, AllBindingFlags, loc);
574 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
575 Type queried_type, string name, Location loc)
577 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
578 name, AllMemberTypes, AllBindingFlags, loc);
581 public static Expression MethodLookup (EmitContext ec, Type queried_type,
582 string name, Location loc)
584 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
585 MemberTypes.Method, AllBindingFlags, loc);
589 /// This is a wrapper for MemberLookup that is not used to "probe", but
590 /// to find a final definition. If the final definition is not found, we
591 /// look for private members and display a useful debugging message if we
594 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
595 Type queried_type, string name, Location loc)
597 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
598 AllMemberTypes, AllBindingFlags, loc);
601 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
602 Type queried_type, string name,
603 MemberTypes mt, BindingFlags bf,
608 int errors = Report.Errors;
610 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
616 // Error has already been reported.
617 if (errors < Report.Errors)
620 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
624 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
625 Type queried_type, string name,
626 string class_name, Location loc)
628 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
629 AllMemberTypes, AllBindingFlags |
630 BindingFlags.NonPublic, name);
632 if (lookup == null) {
633 if (class_name != null)
634 Report.Error (103, loc, "The name `" + name + "' could not be " +
635 "found in `" + class_name + "'");
638 117, loc, "`" + queried_type + "' does not contain a " +
639 "definition for `" + name + "'");
643 if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
644 ec.ContainerType.IsSubclassOf (qualifier_type)) {
645 // Although a derived class can access protected members of
646 // its base class it cannot do so through an instance of the
647 // base class (CS1540). If the qualifier_type is a parent of the
648 // ec.ContainerType and the lookup succeeds with the latter one,
649 // then we are in this situation.
651 lookup = TypeManager.MemberLookup (
652 ec.ContainerType, ec.ContainerType, ec.ContainerType,
653 AllMemberTypes, AllBindingFlags, name);
655 if (lookup != null) {
657 1540, loc, "Cannot access protected member `" +
658 TypeManager.CSharpName (qualifier_type) + "." +
659 name + "' " + "via a qualifier of type `" +
660 TypeManager.CSharpName (qualifier_type) + "'; the " +
661 "qualifier must be of type `" +
662 TypeManager.CSharpName (ec.ContainerType) + "' " +
663 "(or derived from it)");
668 if (qualifier_type != null)
670 122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
671 name + "' is inaccessible due to its protection level");
674 122, loc, "`" + name + "' is inaccessible due to its " +
678 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
680 EventInfo ei = event_expr.EventInfo;
682 return TypeManager.GetPrivateFieldOfEvent (ei);
686 /// Returns an expression that can be used to invoke operator true
687 /// on the expression if it exists.
689 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
691 return GetOperatorTrueOrFalse (ec, e, true, loc);
695 /// Returns an expression that can be used to invoke operator false
696 /// on the expression if it exists.
698 static public StaticCallExpr GetOperatorFalse (EmitContext ec, Expression e, Location loc)
700 return GetOperatorTrueOrFalse (ec, e, false, loc);
703 static StaticCallExpr GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
706 Expression operator_group;
708 operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
709 if (operator_group == null)
712 ArrayList arguments = new ArrayList ();
713 arguments.Add (new Argument (e, Argument.AType.Expression));
714 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc);
719 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
723 /// Resolves the expression `e' into a boolean expression: either through
724 /// an implicit conversion, or through an `operator true' invocation
726 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
732 Expression converted = e;
733 if (e.Type != TypeManager.bool_type)
734 converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
737 // If no implicit conversion to bool exists, try using `operator true'
739 if (converted == null){
740 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
741 if (operator_true == null){
743 31, loc, "Can not convert the expression to a boolean");
753 static string ExprClassName (ExprClass c)
756 case ExprClass.Invalid:
758 case ExprClass.Value:
760 case ExprClass.Variable:
762 case ExprClass.Namespace:
766 case ExprClass.MethodGroup:
767 return "method group";
768 case ExprClass.PropertyAccess:
769 return "property access";
770 case ExprClass.EventAccess:
771 return "event access";
772 case ExprClass.IndexerAccess:
773 return "indexer access";
774 case ExprClass.Nothing:
777 throw new Exception ("Should not happen");
781 /// Reports that we were expecting `expr' to be of class `expected'
783 public void Error_UnexpectedKind (string expected)
785 string kind = "Unknown";
787 kind = ExprClassName (eclass);
789 Error (118, "Expression denotes a `" + kind +
790 "' where a `" + expected + "' was expected");
793 public void Error_UnexpectedKind (ResolveFlags flags)
795 ArrayList valid = new ArrayList (10);
797 if ((flags & ResolveFlags.VariableOrValue) != 0) {
798 valid.Add ("variable");
802 if ((flags & ResolveFlags.Type) != 0)
805 if ((flags & ResolveFlags.MethodGroup) != 0)
806 valid.Add ("method group");
808 if ((flags & ResolveFlags.SimpleName) != 0)
809 valid.Add ("simple name");
811 if (valid.Count == 0)
812 valid.Add ("unknown");
814 StringBuilder sb = new StringBuilder ();
815 for (int i = 0; i < valid.Count; i++) {
818 else if (i == valid.Count)
820 sb.Append (valid [i]);
823 string kind = ExprClassName (eclass);
825 Error (119, "Expression denotes a `" + kind + "' where " +
826 "a `" + sb.ToString () + "' was expected");
829 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
831 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
832 TypeManager.CSharpName (t));
835 public static void UnsafeError (Location loc)
837 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
841 /// Converts the IntConstant, UIntConstant, LongConstant or
842 /// ULongConstant into the integral target_type. Notice
843 /// that we do not return an `Expression' we do return
844 /// a boxed integral type.
846 /// FIXME: Since I added the new constants, we need to
847 /// also support conversions from CharConstant, ByteConstant,
848 /// SByteConstant, UShortConstant, ShortConstant
850 /// This is used by the switch statement, so the domain
851 /// of work is restricted to the literals above, and the
852 /// targets are int32, uint32, char, byte, sbyte, ushort,
853 /// short, uint64 and int64
855 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
857 if (!Convert.ImplicitStandardConversionExists (c, target_type)){
858 Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
864 if (c.Type == target_type)
865 return ((Constant) c).GetValue ();
868 // Make into one of the literals we handle, we dont really care
869 // about this value as we will just return a few limited types
871 if (c is EnumConstant)
872 c = ((EnumConstant)c).WidenToCompilerConstant ();
874 if (c is IntConstant){
875 int v = ((IntConstant) c).Value;
877 if (target_type == TypeManager.uint32_type){
880 } else if (target_type == TypeManager.char_type){
881 if (v >= Char.MinValue && v <= Char.MaxValue)
883 } else if (target_type == TypeManager.byte_type){
884 if (v >= Byte.MinValue && v <= Byte.MaxValue)
886 } else if (target_type == TypeManager.sbyte_type){
887 if (v >= SByte.MinValue && v <= SByte.MaxValue)
889 } else if (target_type == TypeManager.short_type){
890 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
892 } else if (target_type == TypeManager.ushort_type){
893 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
895 } else if (target_type == TypeManager.int64_type)
897 else if (target_type == TypeManager.uint64_type){
903 } else if (c is UIntConstant){
904 uint v = ((UIntConstant) c).Value;
906 if (target_type == TypeManager.int32_type){
907 if (v <= Int32.MaxValue)
909 } else if (target_type == TypeManager.char_type){
910 if (v >= Char.MinValue && v <= Char.MaxValue)
912 } else if (target_type == TypeManager.byte_type){
913 if (v <= Byte.MaxValue)
915 } else if (target_type == TypeManager.sbyte_type){
916 if (v <= SByte.MaxValue)
918 } else if (target_type == TypeManager.short_type){
919 if (v <= UInt16.MaxValue)
921 } else if (target_type == TypeManager.ushort_type){
922 if (v <= UInt16.MaxValue)
924 } else if (target_type == TypeManager.int64_type)
926 else if (target_type == TypeManager.uint64_type)
929 } else if (c is LongConstant){
930 long v = ((LongConstant) c).Value;
932 if (target_type == TypeManager.int32_type){
933 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
935 } else if (target_type == TypeManager.uint32_type){
936 if (v >= 0 && v <= UInt32.MaxValue)
938 } else if (target_type == TypeManager.char_type){
939 if (v >= Char.MinValue && v <= Char.MaxValue)
941 } else if (target_type == TypeManager.byte_type){
942 if (v >= Byte.MinValue && v <= Byte.MaxValue)
944 } else if (target_type == TypeManager.sbyte_type){
945 if (v >= SByte.MinValue && v <= SByte.MaxValue)
947 } else if (target_type == TypeManager.short_type){
948 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
950 } else if (target_type == TypeManager.ushort_type){
951 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
953 } else if (target_type == TypeManager.uint64_type){
958 } else if (c is ULongConstant){
959 ulong v = ((ULongConstant) c).Value;
961 if (target_type == TypeManager.int32_type){
962 if (v <= Int32.MaxValue)
964 } else if (target_type == TypeManager.uint32_type){
965 if (v <= UInt32.MaxValue)
967 } else if (target_type == TypeManager.char_type){
968 if (v >= Char.MinValue && v <= Char.MaxValue)
970 } else if (target_type == TypeManager.byte_type){
971 if (v >= Byte.MinValue && v <= Byte.MaxValue)
973 } else if (target_type == TypeManager.sbyte_type){
974 if (v <= (int) SByte.MaxValue)
976 } else if (target_type == TypeManager.short_type){
977 if (v <= UInt16.MaxValue)
979 } else if (target_type == TypeManager.ushort_type){
980 if (v <= UInt16.MaxValue)
982 } else if (target_type == TypeManager.int64_type){
983 if (v <= Int64.MaxValue)
987 } else if (c is ByteConstant){
988 byte v = ((ByteConstant) c).Value;
990 if (target_type == TypeManager.int32_type)
992 else if (target_type == TypeManager.uint32_type)
994 else if (target_type == TypeManager.char_type)
996 else if (target_type == TypeManager.sbyte_type){
997 if (v <= SByte.MaxValue)
999 } else if (target_type == TypeManager.short_type)
1001 else if (target_type == TypeManager.ushort_type)
1003 else if (target_type == TypeManager.int64_type)
1005 else if (target_type == TypeManager.uint64_type)
1008 } else if (c is SByteConstant){
1009 sbyte v = ((SByteConstant) c).Value;
1011 if (target_type == TypeManager.int32_type)
1013 else if (target_type == TypeManager.uint32_type){
1016 } else if (target_type == TypeManager.char_type){
1019 } else if (target_type == TypeManager.byte_type){
1022 } else if (target_type == TypeManager.short_type)
1024 else if (target_type == TypeManager.ushort_type){
1027 } else if (target_type == TypeManager.int64_type)
1029 else if (target_type == TypeManager.uint64_type){
1034 } else if (c is ShortConstant){
1035 short v = ((ShortConstant) c).Value;
1037 if (target_type == TypeManager.int32_type){
1039 } else if (target_type == TypeManager.uint32_type){
1042 } else if (target_type == TypeManager.char_type){
1045 } else if (target_type == TypeManager.byte_type){
1046 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1048 } else if (target_type == TypeManager.sbyte_type){
1049 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1051 } else if (target_type == TypeManager.ushort_type){
1054 } else if (target_type == TypeManager.int64_type)
1056 else if (target_type == TypeManager.uint64_type)
1060 } else if (c is UShortConstant){
1061 ushort v = ((UShortConstant) c).Value;
1063 if (target_type == TypeManager.int32_type)
1065 else if (target_type == TypeManager.uint32_type)
1067 else if (target_type == TypeManager.char_type){
1068 if (v >= Char.MinValue && v <= Char.MaxValue)
1070 } else if (target_type == TypeManager.byte_type){
1071 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1073 } else if (target_type == TypeManager.sbyte_type){
1074 if (v <= SByte.MaxValue)
1076 } else if (target_type == TypeManager.short_type){
1077 if (v <= Int16.MaxValue)
1079 } else if (target_type == TypeManager.int64_type)
1081 else if (target_type == TypeManager.uint64_type)
1085 } else if (c is CharConstant){
1086 char v = ((CharConstant) c).Value;
1088 if (target_type == TypeManager.int32_type)
1090 else if (target_type == TypeManager.uint32_type)
1092 else if (target_type == TypeManager.byte_type){
1093 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1095 } else if (target_type == TypeManager.sbyte_type){
1096 if (v <= SByte.MaxValue)
1098 } else if (target_type == TypeManager.short_type){
1099 if (v <= Int16.MaxValue)
1101 } else if (target_type == TypeManager.ushort_type)
1103 else if (target_type == TypeManager.int64_type)
1105 else if (target_type == TypeManager.uint64_type)
1110 Error_ConstantValueCannotBeConverted (loc, s, target_type);
1115 // Load the object from the pointer.
1117 public static void LoadFromPtr (ILGenerator ig, Type t)
1119 if (t == TypeManager.int32_type)
1120 ig.Emit (OpCodes.Ldind_I4);
1121 else if (t == TypeManager.uint32_type)
1122 ig.Emit (OpCodes.Ldind_U4);
1123 else if (t == TypeManager.short_type)
1124 ig.Emit (OpCodes.Ldind_I2);
1125 else if (t == TypeManager.ushort_type)
1126 ig.Emit (OpCodes.Ldind_U2);
1127 else if (t == TypeManager.char_type)
1128 ig.Emit (OpCodes.Ldind_U2);
1129 else if (t == TypeManager.byte_type)
1130 ig.Emit (OpCodes.Ldind_U1);
1131 else if (t == TypeManager.sbyte_type)
1132 ig.Emit (OpCodes.Ldind_I1);
1133 else if (t == TypeManager.uint64_type)
1134 ig.Emit (OpCodes.Ldind_I8);
1135 else if (t == TypeManager.int64_type)
1136 ig.Emit (OpCodes.Ldind_I8);
1137 else if (t == TypeManager.float_type)
1138 ig.Emit (OpCodes.Ldind_R4);
1139 else if (t == TypeManager.double_type)
1140 ig.Emit (OpCodes.Ldind_R8);
1141 else if (t == TypeManager.bool_type)
1142 ig.Emit (OpCodes.Ldind_I1);
1143 else if (t == TypeManager.intptr_type)
1144 ig.Emit (OpCodes.Ldind_I);
1145 else if (TypeManager.IsEnumType (t)) {
1146 if (t == TypeManager.enum_type)
1147 ig.Emit (OpCodes.Ldind_Ref);
1149 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1150 } else if (t.IsValueType)
1151 ig.Emit (OpCodes.Ldobj, t);
1152 else if (t.IsPointer)
1153 ig.Emit (OpCodes.Ldind_I);
1155 ig.Emit (OpCodes.Ldind_Ref);
1159 // The stack contains the pointer and the value of type `type'
1161 public static void StoreFromPtr (ILGenerator ig, Type type)
1163 if (TypeManager.IsEnumType (type))
1164 type = TypeManager.EnumToUnderlying (type);
1165 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1166 ig.Emit (OpCodes.Stind_I4);
1167 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1168 ig.Emit (OpCodes.Stind_I8);
1169 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1170 type == TypeManager.ushort_type)
1171 ig.Emit (OpCodes.Stind_I2);
1172 else if (type == TypeManager.float_type)
1173 ig.Emit (OpCodes.Stind_R4);
1174 else if (type == TypeManager.double_type)
1175 ig.Emit (OpCodes.Stind_R8);
1176 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1177 type == TypeManager.bool_type)
1178 ig.Emit (OpCodes.Stind_I1);
1179 else if (type == TypeManager.intptr_type)
1180 ig.Emit (OpCodes.Stind_I);
1181 else if (type.IsValueType)
1182 ig.Emit (OpCodes.Stobj, type);
1184 ig.Emit (OpCodes.Stind_Ref);
1188 // Returns the size of type `t' if known, otherwise, 0
1190 public static int GetTypeSize (Type t)
1192 t = TypeManager.TypeToCoreType (t);
1193 if (t == TypeManager.int32_type ||
1194 t == TypeManager.uint32_type ||
1195 t == TypeManager.float_type)
1197 else if (t == TypeManager.int64_type ||
1198 t == TypeManager.uint64_type ||
1199 t == TypeManager.double_type)
1201 else if (t == TypeManager.byte_type ||
1202 t == TypeManager.sbyte_type ||
1203 t == TypeManager.bool_type)
1205 else if (t == TypeManager.short_type ||
1206 t == TypeManager.char_type ||
1207 t == TypeManager.ushort_type)
1209 else if (t == TypeManager.decimal_type)
1216 // Default implementation of IAssignMethod.CacheTemporaries
1218 public void CacheTemporaries (EmitContext ec)
1222 static void Error_NegativeArrayIndex (Location loc)
1224 Report.Error (284, loc, "Can not create array with a negative size");
1228 // Converts `source' to an int, uint, long or ulong.
1230 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1234 bool old_checked = ec.CheckState;
1235 ec.CheckState = true;
1237 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1238 if (target == null){
1239 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1240 if (target == null){
1241 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1242 if (target == null){
1243 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1245 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1249 ec.CheckState = old_checked;
1252 // Only positive constants are allowed at compile time
1254 if (target is Constant){
1255 if (target is IntConstant){
1256 if (((IntConstant) target).Value < 0){
1257 Error_NegativeArrayIndex (loc);
1262 if (target is LongConstant){
1263 if (((LongConstant) target).Value < 0){
1264 Error_NegativeArrayIndex (loc);
1277 /// This is just a base class for expressions that can
1278 /// appear on statements (invocations, object creation,
1279 /// assignments, post/pre increment and decrement). The idea
1280 /// being that they would support an extra Emition interface that
1281 /// does not leave a result on the stack.
1283 public abstract class ExpressionStatement : Expression {
1285 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1287 Expression e = Resolve (ec);
1291 ExpressionStatement es = e as ExpressionStatement;
1293 Error (201, "Only assignment, call, increment, decrement and new object " +
1294 "expressions can be used as a statement");
1300 /// Requests the expression to be emitted in a `statement'
1301 /// context. This means that no new value is left on the
1302 /// stack after invoking this method (constrasted with
1303 /// Emit that will always leave a value on the stack).
1305 public abstract void EmitStatement (EmitContext ec);
1309 /// This kind of cast is used to encapsulate the child
1310 /// whose type is child.Type into an expression that is
1311 /// reported to return "return_type". This is used to encapsulate
1312 /// expressions which have compatible types, but need to be dealt
1313 /// at higher levels with.
1315 /// For example, a "byte" expression could be encapsulated in one
1316 /// of these as an "unsigned int". The type for the expression
1317 /// would be "unsigned int".
1320 public class EmptyCast : Expression {
1321 protected Expression child;
1323 public EmptyCast (Expression child, Type return_type)
1325 eclass = child.eclass;
1330 public override Expression DoResolve (EmitContext ec)
1332 // This should never be invoked, we are born in fully
1333 // initialized state.
1338 public override void Emit (EmitContext ec)
1345 // We need to special case this since an empty cast of
1346 // a NullLiteral is still a Constant
1348 public class NullCast : Constant {
1349 protected Expression child;
1351 public NullCast (Expression child, Type return_type)
1353 eclass = child.eclass;
1358 override public string AsString ()
1363 public override object GetValue ()
1368 public override Expression DoResolve (EmitContext ec)
1370 // This should never be invoked, we are born in fully
1371 // initialized state.
1376 public override void Emit (EmitContext ec)
1384 /// This class is used to wrap literals which belong inside Enums
1386 public class EnumConstant : Constant {
1387 public Constant Child;
1389 public EnumConstant (Constant child, Type enum_type)
1391 eclass = child.eclass;
1396 public override Expression DoResolve (EmitContext ec)
1398 // This should never be invoked, we are born in fully
1399 // initialized state.
1404 public override void Emit (EmitContext ec)
1409 public override object GetValue ()
1411 return Child.GetValue ();
1415 // Converts from one of the valid underlying types for an enumeration
1416 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1417 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1419 public Constant WidenToCompilerConstant ()
1421 Type t = TypeManager.EnumToUnderlying (Child.Type);
1422 object v = ((Constant) Child).GetValue ();;
1424 if (t == TypeManager.int32_type)
1425 return new IntConstant ((int) v);
1426 if (t == TypeManager.uint32_type)
1427 return new UIntConstant ((uint) v);
1428 if (t == TypeManager.int64_type)
1429 return new LongConstant ((long) v);
1430 if (t == TypeManager.uint64_type)
1431 return new ULongConstant ((ulong) v);
1432 if (t == TypeManager.short_type)
1433 return new ShortConstant ((short) v);
1434 if (t == TypeManager.ushort_type)
1435 return new UShortConstant ((ushort) v);
1436 if (t == TypeManager.byte_type)
1437 return new ByteConstant ((byte) v);
1438 if (t == TypeManager.sbyte_type)
1439 return new SByteConstant ((sbyte) v);
1441 throw new Exception ("Invalid enumeration underlying type: " + t);
1445 // Extracts the value in the enumeration on its native representation
1447 public object GetPlainValue ()
1449 Type t = TypeManager.EnumToUnderlying (Child.Type);
1450 object v = ((Constant) Child).GetValue ();;
1452 if (t == TypeManager.int32_type)
1454 if (t == TypeManager.uint32_type)
1456 if (t == TypeManager.int64_type)
1458 if (t == TypeManager.uint64_type)
1460 if (t == TypeManager.short_type)
1462 if (t == TypeManager.ushort_type)
1464 if (t == TypeManager.byte_type)
1466 if (t == TypeManager.sbyte_type)
1472 public override string AsString ()
1474 return Child.AsString ();
1477 public override DoubleConstant ConvertToDouble ()
1479 return Child.ConvertToDouble ();
1482 public override FloatConstant ConvertToFloat ()
1484 return Child.ConvertToFloat ();
1487 public override ULongConstant ConvertToULong ()
1489 return Child.ConvertToULong ();
1492 public override LongConstant ConvertToLong ()
1494 return Child.ConvertToLong ();
1497 public override UIntConstant ConvertToUInt ()
1499 return Child.ConvertToUInt ();
1502 public override IntConstant ConvertToInt ()
1504 return Child.ConvertToInt ();
1509 /// This kind of cast is used to encapsulate Value Types in objects.
1511 /// The effect of it is to box the value type emitted by the previous
1514 public class BoxedCast : EmptyCast {
1516 public BoxedCast (Expression expr)
1517 : base (expr, TypeManager.object_type)
1521 public BoxedCast (Expression expr, Type target_type)
1522 : base (expr, target_type)
1526 public override Expression DoResolve (EmitContext ec)
1528 // This should never be invoked, we are born in fully
1529 // initialized state.
1534 public override void Emit (EmitContext ec)
1538 ec.ig.Emit (OpCodes.Box, child.Type);
1542 public class UnboxCast : EmptyCast {
1543 public UnboxCast (Expression expr, Type return_type)
1544 : base (expr, return_type)
1548 public override Expression DoResolve (EmitContext ec)
1550 // This should never be invoked, we are born in fully
1551 // initialized state.
1556 public override void Emit (EmitContext ec)
1559 ILGenerator ig = ec.ig;
1562 ig.Emit (OpCodes.Unbox, t);
1564 LoadFromPtr (ig, t);
1569 /// This is used to perform explicit numeric conversions.
1571 /// Explicit numeric conversions might trigger exceptions in a checked
1572 /// context, so they should generate the conv.ovf opcodes instead of
1575 public class ConvCast : EmptyCast {
1576 public enum Mode : byte {
1577 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1579 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1580 U2_I1, U2_U1, U2_I2, U2_CH,
1581 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1582 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1583 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1584 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1585 CH_I1, CH_U1, CH_I2,
1586 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1587 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1593 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1594 : base (child, return_type)
1596 checked_state = ec.CheckState;
1600 public override Expression DoResolve (EmitContext ec)
1602 // This should never be invoked, we are born in fully
1603 // initialized state.
1608 public override string ToString ()
1610 return String.Format ("ConvCast ({0}, {1})", mode, child);
1613 public override void Emit (EmitContext ec)
1615 ILGenerator ig = ec.ig;
1621 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1622 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1623 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1624 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1625 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1627 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1628 case Mode.U1_CH: /* nothing */ break;
1630 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1631 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1632 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1633 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1634 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1635 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1637 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1638 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1639 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1640 case Mode.U2_CH: /* nothing */ break;
1642 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1643 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1644 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1645 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1646 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1647 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1648 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1650 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1651 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1652 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1653 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1654 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1655 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1657 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1658 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1659 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1660 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1661 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1662 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1663 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1664 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1666 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1667 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1668 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1669 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1670 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1671 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1672 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1673 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1675 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1676 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1677 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1679 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1680 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1681 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1682 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1683 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1684 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1685 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1686 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1687 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1689 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1690 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1691 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1692 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1693 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1694 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1695 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1696 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1697 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1698 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1702 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1703 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1704 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1705 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1706 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1708 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1709 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1711 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1712 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1713 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1714 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1715 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1716 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1718 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1719 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1720 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1721 case Mode.U2_CH: /* nothing */ break;
1723 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1724 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1725 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1726 case Mode.I4_U4: /* nothing */ break;
1727 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1728 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1729 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1731 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1732 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1733 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1734 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1735 case Mode.U4_I4: /* nothing */ break;
1736 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1738 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1739 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1740 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1741 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1742 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1743 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1744 case Mode.I8_U8: /* nothing */ break;
1745 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1747 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1748 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1749 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1750 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1751 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1752 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1753 case Mode.U8_I8: /* nothing */ break;
1754 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1756 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1757 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1758 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1760 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1761 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1762 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1763 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1764 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1765 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1766 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1767 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1768 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1770 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1771 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1772 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1773 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1774 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1775 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1776 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1777 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1778 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1779 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1785 public class OpcodeCast : EmptyCast {
1789 public OpcodeCast (Expression child, Type return_type, OpCode op)
1790 : base (child, return_type)
1794 second_valid = false;
1797 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1798 : base (child, return_type)
1803 second_valid = true;
1806 public override Expression DoResolve (EmitContext ec)
1808 // This should never be invoked, we are born in fully
1809 // initialized state.
1814 public override void Emit (EmitContext ec)
1825 /// This kind of cast is used to encapsulate a child and cast it
1826 /// to the class requested
1828 public class ClassCast : EmptyCast {
1829 public ClassCast (Expression child, Type return_type)
1830 : base (child, return_type)
1835 public override Expression DoResolve (EmitContext ec)
1837 // This should never be invoked, we are born in fully
1838 // initialized state.
1843 public override void Emit (EmitContext ec)
1847 ec.ig.Emit (OpCodes.Castclass, type);
1853 /// SimpleName expressions are initially formed of a single
1854 /// word and it only happens at the beginning of the expression.
1858 /// The expression will try to be bound to a Field, a Method
1859 /// group or a Property. If those fail we pass the name to our
1860 /// caller and the SimpleName is compounded to perform a type
1861 /// lookup. The idea behind this process is that we want to avoid
1862 /// creating a namespace map from the assemblies, as that requires
1863 /// the GetExportedTypes function to be called and a hashtable to
1864 /// be constructed which reduces startup time. If later we find
1865 /// that this is slower, we should create a `NamespaceExpr' expression
1866 /// that fully participates in the resolution process.
1868 /// For example `System.Console.WriteLine' is decomposed into
1869 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1871 /// The first SimpleName wont produce a match on its own, so it will
1873 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
1875 /// System.Console will produce a TypeExpr match.
1877 /// The downside of this is that we might be hitting `LookupType' too many
1878 /// times with this scheme.
1880 public class SimpleName : Expression {
1884 // If true, then we are a simple name, not composed with a ".
1888 public SimpleName (string a, string b, Location l)
1890 Name = String.Concat (a, ".", b);
1895 public SimpleName (string name, Location l)
1902 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1904 if (ec.IsFieldInitializer)
1907 "A field initializer cannot reference the non-static field, " +
1908 "method or property `"+name+"'");
1912 "An object reference is required " +
1913 "for the non-static field `"+name+"'");
1917 // Checks whether we are trying to access an instance
1918 // property, method or field from a static body.
1920 Expression MemberStaticCheck (EmitContext ec, Expression e)
1922 if (e is IMemberExpr){
1923 IMemberExpr member = (IMemberExpr) e;
1925 if (!member.IsStatic){
1926 Error_ObjectRefRequired (ec, loc, Name);
1934 public override Expression DoResolve (EmitContext ec)
1936 return SimpleNameResolve (ec, null, false);
1939 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1941 return SimpleNameResolve (ec, right_side, false);
1945 public Expression DoResolveAllowStatic (EmitContext ec)
1947 return SimpleNameResolve (ec, null, true);
1950 public override Expression ResolveAsTypeStep (EmitContext ec)
1952 DeclSpace ds = ec.DeclSpace;
1953 NamespaceEntry ns = ds.NamespaceEntry;
1958 // Since we are cheating: we only do the Alias lookup for
1959 // namespaces if the name does not include any dots in it
1961 if (ns != null && is_base)
1962 alias_value = ns.LookupAlias (Name);
1966 if (ec.ResolvingTypeTree){
1967 if (alias_value != null){
1968 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1969 return new TypeExpr (t, loc);
1972 int errors = Report.Errors;
1973 Type dt = ec.DeclSpace.FindType (loc, Name);
1975 if (Report.Errors != errors)
1979 return new TypeExpr (dt, loc);
1983 // First, the using aliases
1985 if (alias_value != null){
1986 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1987 return new TypeExpr (t, loc);
1989 // we have alias value, but it isn't Type, so try if it's namespace
1990 return new SimpleName (alias_value, loc);
1994 // Stage 2: Lookup up if we are an alias to a type
1998 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
1999 return new TypeExpr (t, loc);
2001 // No match, maybe our parent can compose us
2002 // into something meaningful.
2007 /// 7.5.2: Simple Names.
2009 /// Local Variables and Parameters are handled at
2010 /// parse time, so they never occur as SimpleNames.
2012 /// The `allow_static' flag is used by MemberAccess only
2013 /// and it is used to inform us that it is ok for us to
2014 /// avoid the static check, because MemberAccess might end
2015 /// up resolving the Name as a Type name and the access as
2016 /// a static type access.
2018 /// ie: Type Type; .... { Type.GetType (""); }
2020 /// Type is both an instance variable and a Type; Type.GetType
2021 /// is the static method not an instance method of type.
2023 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
2025 Expression e = null;
2028 // Stage 1: Performed by the parser (binding to locals or parameters).
2030 Block current_block = ec.CurrentBlock;
2031 if (current_block != null){
2032 LocalInfo vi = current_block.GetLocalInfo (Name);
2036 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2038 if (right_side != null)
2039 return var.ResolveLValue (ec, right_side);
2041 return var.Resolve (ec);
2045 Parameter par = null;
2046 Parameters pars = current_block.Parameters;
2048 par = pars.GetParameterByName (Name, out idx);
2051 ParameterReference param;
2053 param = new ParameterReference (pars, current_block, idx, Name, loc);
2055 if (right_side != null)
2056 return param.ResolveLValue (ec, right_side);
2058 return param.Resolve (ec);
2063 // Stage 2: Lookup members
2066 DeclSpace lookup_ds = ec.DeclSpace;
2068 if (lookup_ds.TypeBuilder == null)
2071 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2075 lookup_ds =lookup_ds.Parent;
2076 } while (lookup_ds != null);
2078 if (e == null && ec.ContainerType != null)
2079 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2083 // Since we are cheating (is_base is our hint
2084 // that we are the beginning of the name): we
2085 // only do the Alias lookup for namespaces if
2086 // the name does not include any dots in it
2088 NamespaceEntry ns = ec.DeclSpace.NamespaceEntry;
2089 if (is_base && ns != null){
2090 string alias_value = ns.LookupAlias (Name);
2091 if (alias_value != null){
2095 if ((t = TypeManager.LookupType (Name)) != null)
2096 return new TypeExpr (t, loc);
2098 // No match, maybe our parent can compose us
2099 // into something meaningful.
2104 return ResolveAsTypeStep (ec);
2110 if (e is IMemberExpr) {
2111 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2115 IMemberExpr me = e as IMemberExpr;
2119 // This fails if ResolveMemberAccess() was unable to decide whether
2120 // it's a field or a type of the same name.
2121 if (!me.IsStatic && (me.InstanceExpression == null))
2125 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
2126 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
2127 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2128 "outer type `" + me.DeclaringType + "' via nested type `" +
2129 me.InstanceExpression.Type + "'");
2133 if (right_side != null)
2134 e = e.DoResolveLValue (ec, right_side);
2136 e = e.DoResolve (ec);
2141 if (ec.IsStatic || ec.IsFieldInitializer){
2145 return MemberStaticCheck (ec, e);
2150 public override void Emit (EmitContext ec)
2153 // If this is ever reached, then we failed to
2154 // find the name as a namespace
2157 Error (103, "The name `" + Name +
2158 "' does not exist in the class `" +
2159 ec.DeclSpace.Name + "'");
2162 public override string ToString ()
2169 /// Fully resolved expression that evaluates to a type
2171 public class TypeExpr : Expression {
2172 public TypeExpr (Type t, Location l)
2175 eclass = ExprClass.Type;
2179 public override Expression ResolveAsTypeStep (EmitContext ec)
2184 override public Expression DoResolve (EmitContext ec)
2189 override public void Emit (EmitContext ec)
2191 throw new Exception ("Should never be called");
2194 public override string ToString ()
2196 return Type.ToString ();
2201 /// Used to create types from a fully qualified name. These are just used
2202 /// by the parser to setup the core types. A TypeLookupExpression is always
2203 /// classified as a type.
2205 public class TypeLookupExpression : TypeExpr {
2208 public TypeLookupExpression (string name) : base (null, Location.Null)
2213 public override Expression ResolveAsTypeStep (EmitContext ec)
2216 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
2220 public override Expression DoResolve (EmitContext ec)
2222 return ResolveAsTypeStep (ec);
2225 public override void Emit (EmitContext ec)
2227 throw new Exception ("Should never be called");
2230 public override string ToString ()
2237 /// MethodGroup Expression.
2239 /// This is a fully resolved expression that evaluates to a type
2241 public class MethodGroupExpr : Expression, IMemberExpr {
2242 public MethodBase [] Methods;
2243 Expression instance_expression = null;
2244 bool is_explicit_impl = false;
2246 public MethodGroupExpr (MemberInfo [] mi, Location l)
2248 Methods = new MethodBase [mi.Length];
2249 mi.CopyTo (Methods, 0);
2250 eclass = ExprClass.MethodGroup;
2251 type = TypeManager.object_type;
2255 public MethodGroupExpr (ArrayList list, Location l)
2257 Methods = new MethodBase [list.Count];
2260 list.CopyTo (Methods, 0);
2262 foreach (MemberInfo m in list){
2263 if (!(m is MethodBase)){
2264 Console.WriteLine ("Name " + m.Name);
2265 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2272 eclass = ExprClass.MethodGroup;
2273 type = TypeManager.object_type;
2276 public Type DeclaringType {
2279 // The methods are arranged in this order:
2280 // derived type -> base type
2282 return Methods [0].DeclaringType;
2287 // `A method group may have associated an instance expression'
2289 public Expression InstanceExpression {
2291 return instance_expression;
2295 instance_expression = value;
2299 public bool IsExplicitImpl {
2301 return is_explicit_impl;
2305 is_explicit_impl = value;
2309 public string Name {
2311 return Methods [0].Name;
2315 public bool IsInstance {
2317 foreach (MethodBase mb in Methods)
2325 public bool IsStatic {
2327 foreach (MethodBase mb in Methods)
2335 override public Expression DoResolve (EmitContext ec)
2338 instance_expression = null;
2340 if (instance_expression != null) {
2341 instance_expression = instance_expression.DoResolve (ec);
2342 if (instance_expression == null)
2349 public void ReportUsageError ()
2351 Report.Error (654, loc, "Method `" + DeclaringType + "." +
2352 Name + "()' is referenced without parentheses");
2355 override public void Emit (EmitContext ec)
2357 ReportUsageError ();
2360 bool RemoveMethods (bool keep_static)
2362 ArrayList smethods = new ArrayList ();
2364 foreach (MethodBase mb in Methods){
2365 if (mb.IsStatic == keep_static)
2369 if (smethods.Count == 0)
2372 Methods = new MethodBase [smethods.Count];
2373 smethods.CopyTo (Methods, 0);
2379 /// Removes any instance methods from the MethodGroup, returns
2380 /// false if the resulting set is empty.
2382 public bool RemoveInstanceMethods ()
2384 return RemoveMethods (true);
2388 /// Removes any static methods from the MethodGroup, returns
2389 /// false if the resulting set is empty.
2391 public bool RemoveStaticMethods ()
2393 return RemoveMethods (false);
2398 /// Fully resolved expression that evaluates to a Field
2400 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2401 public readonly FieldInfo FieldInfo;
2402 Expression instance_expr;
2403 VariableInfo variable_info;
2405 public FieldExpr (FieldInfo fi, Location l)
2408 eclass = ExprClass.Variable;
2409 type = fi.FieldType;
2413 public string Name {
2415 return FieldInfo.Name;
2419 public bool IsInstance {
2421 return !FieldInfo.IsStatic;
2425 public bool IsStatic {
2427 return FieldInfo.IsStatic;
2431 public Type DeclaringType {
2433 return FieldInfo.DeclaringType;
2437 public Expression InstanceExpression {
2439 return instance_expr;
2443 instance_expr = value;
2447 public VariableInfo VariableInfo {
2449 return variable_info;
2453 override public Expression DoResolve (EmitContext ec)
2455 if (!FieldInfo.IsStatic){
2456 if (instance_expr == null){
2458 // This can happen when referencing an instance field using
2459 // a fully qualified type expression: TypeName.InstanceField = xxx
2461 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2465 // Resolve the field's instance expression while flow analysis is turned
2466 // off: when accessing a field "a.b", we must check whether the field
2467 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2468 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2469 ResolveFlags.DisableFlowAnalysis);
2470 if (instance_expr == null)
2474 // If the instance expression is a local variable or parameter.
2475 IVariable var = instance_expr as IVariable;
2476 if ((var == null) || (var.VariableInfo == null))
2479 VariableInfo vi = var.VariableInfo;
2480 if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2483 variable_info = vi.GetSubStruct (FieldInfo.Name);
2487 void Report_AssignToReadonly (bool is_instance)
2492 msg = "Readonly field can not be assigned outside " +
2493 "of constructor or variable initializer";
2495 msg = "A static readonly field can only be assigned in " +
2496 "a static constructor";
2498 Report.Error (is_instance ? 191 : 198, loc, msg);
2501 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2503 IVariable var = instance_expr as IVariable;
2504 if ((var != null) && (var.VariableInfo != null))
2505 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2507 Expression e = DoResolve (ec);
2512 if (!FieldInfo.IsStatic && (instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation))) {
2513 // FIXME: Provide better error reporting.
2514 Error (1612, "Cannot modify expression because it is not a variable.");
2518 if (!FieldInfo.IsInitOnly)
2521 FieldBase fb = TypeManager.GetField (FieldInfo);
2523 fb.IsAssigned = true;
2526 // InitOnly fields can only be assigned in constructors
2529 if (ec.IsConstructor){
2530 if (IsStatic && !ec.IsStatic)
2531 Report_AssignToReadonly (false);
2533 if (ec.ContainerType == FieldInfo.DeclaringType)
2537 Report_AssignToReadonly (true);
2542 public bool VerifyFixed (bool is_expression)
2544 IVariable variable = instance_expr as IVariable;
2545 if ((variable == null) || !variable.VerifyFixed (true))
2551 override public void Emit (EmitContext ec)
2553 ILGenerator ig = ec.ig;
2554 bool is_volatile = false;
2556 if (FieldInfo is FieldBuilder){
2557 FieldBase f = TypeManager.GetField (FieldInfo);
2559 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2562 f.status |= Field.Status.USED;
2566 if (FieldInfo.IsStatic){
2568 ig.Emit (OpCodes.Volatile);
2570 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2574 if (instance_expr.Type.IsValueType){
2576 LocalTemporary tempo = null;
2578 if (!(instance_expr is IMemoryLocation)){
2579 tempo = new LocalTemporary (ec, instance_expr.Type);
2581 if (ec.RemapToProxy)
2584 InstanceExpression.Emit (ec);
2588 ml = (IMemoryLocation) instance_expr;
2590 ml.AddressOf (ec, AddressOp.Load);
2592 if (ec.RemapToProxy)
2595 instance_expr.Emit (ec);
2598 ig.Emit (OpCodes.Volatile);
2600 ig.Emit (OpCodes.Ldfld, FieldInfo);
2603 public void EmitAssign (EmitContext ec, Expression source)
2605 FieldAttributes fa = FieldInfo.Attributes;
2606 bool is_static = (fa & FieldAttributes.Static) != 0;
2607 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
2608 ILGenerator ig = ec.ig;
2610 if (is_readonly && !ec.IsConstructor){
2611 Report_AssignToReadonly (!is_static);
2616 Expression instance = instance_expr;
2618 if (instance.Type.IsValueType){
2619 IMemoryLocation ml = (IMemoryLocation) instance;
2621 ml.AddressOf (ec, AddressOp.Store);
2623 if (ec.RemapToProxy)
2632 if (FieldInfo is FieldBuilder){
2633 FieldBase f = TypeManager.GetField (FieldInfo);
2635 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2636 ig.Emit (OpCodes.Volatile);
2638 f.status |= Field.Status.ASSIGNED;
2643 ig.Emit (OpCodes.Stsfld, FieldInfo);
2645 ig.Emit (OpCodes.Stfld, FieldInfo);
2648 public void AddressOf (EmitContext ec, AddressOp mode)
2650 ILGenerator ig = ec.ig;
2652 if (FieldInfo is FieldBuilder){
2653 FieldBase f = TypeManager.GetField (FieldInfo);
2655 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
2656 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
2660 if ((mode & AddressOp.Store) != 0)
2661 f.status |= Field.Status.ASSIGNED;
2662 if ((mode & AddressOp.Load) != 0)
2663 f.status |= Field.Status.USED;
2668 // Handle initonly fields specially: make a copy and then
2669 // get the address of the copy.
2671 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
2675 local = ig.DeclareLocal (type);
2676 ig.Emit (OpCodes.Stloc, local);
2677 ig.Emit (OpCodes.Ldloca, local);
2681 if (FieldInfo.IsStatic)
2682 ig.Emit (OpCodes.Ldsflda, FieldInfo);
2685 // In the case of `This', we call the AddressOf method, which will
2686 // only load the pointer, and not perform an Ldobj immediately after
2687 // the value has been loaded into the stack.
2689 if (instance_expr is This)
2690 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
2691 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
2692 IMemoryLocation ml = (IMemoryLocation) instance_expr;
2694 ml.AddressOf (ec, AddressOp.LoadStore);
2696 instance_expr.Emit (ec);
2697 ig.Emit (OpCodes.Ldflda, FieldInfo);
2703 // A FieldExpr whose address can not be taken
2705 public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
2706 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
2710 public new void AddressOf (EmitContext ec, AddressOp mode)
2712 Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
2717 /// Expression that evaluates to a Property. The Assign class
2718 /// might set the `Value' expression if we are in an assignment.
2720 /// This is not an LValue because we need to re-write the expression, we
2721 /// can not take data from the stack and store it.
2723 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
2724 public readonly PropertyInfo PropertyInfo;
2727 // This is set externally by the `BaseAccess' class
2730 MethodInfo getter, setter;
2732 bool must_do_cs1540_check;
2734 Expression instance_expr;
2736 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
2739 eclass = ExprClass.PropertyAccess;
2743 type = TypeManager.TypeToCoreType (pi.PropertyType);
2745 ResolveAccessors (ec);
2748 public string Name {
2750 return PropertyInfo.Name;
2754 public bool IsInstance {
2760 public bool IsStatic {
2766 public Type DeclaringType {
2768 return PropertyInfo.DeclaringType;
2773 // The instance expression associated with this expression
2775 public Expression InstanceExpression {
2777 instance_expr = value;
2781 return instance_expr;
2785 public bool VerifyAssignable ()
2787 if (setter == null) {
2788 Report.Error (200, loc,
2789 "The property `" + PropertyInfo.Name +
2790 "' can not be assigned to, as it has not set accessor");
2797 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
2799 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
2800 BindingFlags.Static | BindingFlags.Instance;
2803 group = TypeManager.MemberLookup (
2804 invocation_type, invocation_type, PropertyInfo.DeclaringType,
2805 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
2808 // The first method is the closest to us
2813 foreach (MethodInfo mi in group) {
2814 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
2817 // If only accessible to the current class or children
2819 if (ma == MethodAttributes.Private) {
2820 Type declaring_type = mi.DeclaringType;
2822 if (invocation_type != declaring_type){
2823 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2831 // FamAndAssem requires that we not only derivate, but we are on the
2834 if (ma == MethodAttributes.FamANDAssem){
2835 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2841 // Assembly and FamORAssem succeed if we're in the same assembly.
2842 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2843 if (mi.DeclaringType.Assembly == invocation_type.Assembly)
2847 // We already know that we aren't in the same assembly.
2848 if (ma == MethodAttributes.Assembly)
2851 // Family and FamANDAssem require that we derive.
2852 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem) || (ma == MethodAttributes.FamORAssem)){
2853 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2856 must_do_cs1540_check = true;
2869 // We also perform the permission checking here, as the PropertyInfo does not
2870 // hold the information for the accessibility of its setter/getter
2872 void ResolveAccessors (EmitContext ec)
2874 getter = GetAccessor (ec.ContainerType, "get");
2875 if ((getter != null) && getter.IsStatic)
2878 setter = GetAccessor (ec.ContainerType, "set");
2879 if ((setter != null) && setter.IsStatic)
2882 if (setter == null && getter == null){
2883 Error (122, "`" + PropertyInfo.Name + "' " +
2884 "is inaccessible because of its protection level");
2889 bool InstanceResolve (EmitContext ec)
2891 if ((instance_expr == null) && ec.IsStatic && !is_static) {
2892 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
2896 if (instance_expr != null) {
2897 instance_expr = instance_expr.DoResolve (ec);
2898 if (instance_expr == null)
2902 if (must_do_cs1540_check && (instance_expr != null)) {
2903 if ((instance_expr.Type != ec.ContainerType) &&
2904 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
2905 Report.Error (1540, loc, "Cannot access protected member `" +
2906 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
2907 "' via a qualifier of type `" +
2908 TypeManager.CSharpName (instance_expr.Type) +
2909 "'; the qualifier must be of type `" +
2910 TypeManager.CSharpName (ec.ContainerType) +
2911 "' (or derived from it)");
2919 override public Expression DoResolve (EmitContext ec)
2921 if (getter == null){
2923 // The following condition happens if the PropertyExpr was
2924 // created, but is invalid (ie, the property is inaccessible),
2925 // and we did not want to embed the knowledge about this in
2926 // the caller routine. This only avoids double error reporting.
2931 Report.Error (154, loc,
2932 "The property `" + PropertyInfo.Name +
2933 "' can not be used in " +
2934 "this context because it lacks a get accessor");
2938 if (!InstanceResolve (ec))
2942 // Only base will allow this invocation to happen.
2944 if (IsBase && getter.IsAbstract){
2945 Report.Error (205, loc, "Cannot call an abstract base property: " +
2946 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2953 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2955 if (setter == null){
2957 // The following condition happens if the PropertyExpr was
2958 // created, but is invalid (ie, the property is inaccessible),
2959 // and we did not want to embed the knowledge about this in
2960 // the caller routine. This only avoids double error reporting.
2965 Report.Error (154, loc,
2966 "The property `" + PropertyInfo.Name +
2967 "' can not be used in " +
2968 "this context because it lacks a set accessor");
2972 if (!InstanceResolve (ec))
2976 // Only base will allow this invocation to happen.
2978 if (IsBase && setter.IsAbstract){
2979 Report.Error (205, loc, "Cannot call an abstract base property: " +
2980 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2986 override public void Emit (EmitContext ec)
2989 // Special case: length of single dimension array property is turned into ldlen
2991 if ((getter == TypeManager.system_int_array_get_length) ||
2992 (getter == TypeManager.int_array_get_length)){
2993 Type iet = instance_expr.Type;
2996 // System.Array.Length can be called, but the Type does not
2997 // support invoking GetArrayRank, so test for that case first
2999 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3000 instance_expr.Emit (ec);
3001 ec.ig.Emit (OpCodes.Ldlen);
3006 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
3011 // Implements the IAssignMethod interface for assignments
3013 public void EmitAssign (EmitContext ec, Expression source)
3015 Argument arg = new Argument (source, Argument.AType.Expression);
3016 ArrayList args = new ArrayList ();
3019 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
3022 override public void EmitStatement (EmitContext ec)
3025 ec.ig.Emit (OpCodes.Pop);
3030 /// Fully resolved expression that evaluates to an Event
3032 public class EventExpr : Expression, IMemberExpr {
3033 public readonly EventInfo EventInfo;
3034 public Expression instance_expr;
3037 MethodInfo add_accessor, remove_accessor;
3039 public EventExpr (EventInfo ei, Location loc)
3043 eclass = ExprClass.EventAccess;
3045 add_accessor = TypeManager.GetAddMethod (ei);
3046 remove_accessor = TypeManager.GetRemoveMethod (ei);
3048 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3051 if (EventInfo is MyEventBuilder){
3052 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3053 type = eb.EventType;
3056 type = EventInfo.EventHandlerType;
3059 public string Name {
3061 return EventInfo.Name;
3065 public bool IsInstance {
3071 public bool IsStatic {
3077 public Type DeclaringType {
3079 return EventInfo.DeclaringType;
3083 public Expression InstanceExpression {
3085 return instance_expr;
3089 instance_expr = value;
3093 public override Expression DoResolve (EmitContext ec)
3095 if (instance_expr != null) {
3096 instance_expr = instance_expr.DoResolve (ec);
3097 if (instance_expr == null)
3105 public override void Emit (EmitContext ec)
3107 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3110 public void EmitAddOrRemove (EmitContext ec, Expression source)
3112 BinaryDelegate source_del = (BinaryDelegate) source;
3113 Expression handler = source_del.Right;
3115 Argument arg = new Argument (handler, Argument.AType.Expression);
3116 ArrayList args = new ArrayList ();
3120 if (source_del.IsAddition)
3121 Invocation.EmitCall (
3122 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3124 Invocation.EmitCall (
3125 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);