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 TypeExpr ResolveAsTypeTerminal (EmitContext ec)
256 return ResolveAsTypeStep (ec) as TypeExpr;
260 /// Resolves an expression and performs semantic analysis on it.
264 /// Currently Resolve wraps DoResolve to perform sanity
265 /// checking and assertion checking on what we expect from Resolve.
267 public Expression Resolve (EmitContext ec, ResolveFlags flags)
269 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
270 return ResolveAsTypeStep (ec);
272 bool old_do_flow_analysis = ec.DoFlowAnalysis;
273 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
274 ec.DoFlowAnalysis = false;
277 if (this is SimpleName)
278 e = ((SimpleName) this).DoResolveAllowStatic (ec);
282 ec.DoFlowAnalysis = old_do_flow_analysis;
287 if (e is SimpleName){
288 SimpleName s = (SimpleName) e;
290 if ((flags & ResolveFlags.SimpleName) == 0) {
291 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
292 ec.DeclSpace.Name, loc);
299 if ((e is TypeExpr) || (e is ComposedCast)) {
300 if ((flags & ResolveFlags.Type) == 0) {
301 e.Error_UnexpectedKind (flags);
310 if ((flags & ResolveFlags.VariableOrValue) == 0) {
311 e.Error_UnexpectedKind (flags);
316 case ExprClass.MethodGroup:
317 if ((flags & ResolveFlags.MethodGroup) == 0) {
318 ((MethodGroupExpr) e).ReportUsageError ();
323 case ExprClass.Value:
324 case ExprClass.Variable:
325 case ExprClass.PropertyAccess:
326 case ExprClass.EventAccess:
327 case ExprClass.IndexerAccess:
328 if ((flags & ResolveFlags.VariableOrValue) == 0) {
329 Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
330 Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
331 FieldInfo fi = ((FieldExpr) e).FieldInfo;
333 Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
334 e.Error_UnexpectedKind (flags);
340 throw new Exception ("Expression " + e.GetType () +
341 " ExprClass is Invalid after resolve");
345 throw new Exception (
346 "Expression " + e.GetType () +
347 " did not set its type after Resolve\n" +
348 "called from: " + this.GetType ());
354 /// Resolves an expression and performs semantic analysis on it.
356 public Expression Resolve (EmitContext ec)
358 return Resolve (ec, ResolveFlags.VariableOrValue);
362 /// Resolves an expression for LValue assignment
366 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
367 /// checking and assertion checking on what we expect from Resolve
369 public Expression ResolveLValue (EmitContext ec, Expression right_side)
371 Expression e = DoResolveLValue (ec, right_side);
374 if (e is SimpleName){
375 SimpleName s = (SimpleName) e;
376 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
377 ec.DeclSpace.Name, loc);
381 if (e.eclass == ExprClass.Invalid)
382 throw new Exception ("Expression " + e +
383 " ExprClass is Invalid after resolve");
385 if (e.eclass == ExprClass.MethodGroup) {
386 ((MethodGroupExpr) e).ReportUsageError ();
391 throw new Exception ("Expression " + e +
392 " did not set its type after Resolve");
399 /// Emits the code for the expression
403 /// The Emit method is invoked to generate the code
404 /// for the expression.
406 public abstract void Emit (EmitContext ec);
409 /// Protected constructor. Only derivate types should
410 /// be able to be created
413 protected Expression ()
415 eclass = ExprClass.Invalid;
420 /// Returns a literalized version of a literal FieldInfo
424 /// The possible return values are:
425 /// IntConstant, UIntConstant
426 /// LongLiteral, ULongConstant
427 /// FloatConstant, DoubleConstant
430 /// The value returned is already resolved.
432 public static Constant Constantify (object v, Type t)
434 if (t == TypeManager.int32_type)
435 return new IntConstant ((int) v);
436 else if (t == TypeManager.uint32_type)
437 return new UIntConstant ((uint) v);
438 else if (t == TypeManager.int64_type)
439 return new LongConstant ((long) v);
440 else if (t == TypeManager.uint64_type)
441 return new ULongConstant ((ulong) v);
442 else if (t == TypeManager.float_type)
443 return new FloatConstant ((float) v);
444 else if (t == TypeManager.double_type)
445 return new DoubleConstant ((double) v);
446 else if (t == TypeManager.string_type)
447 return new StringConstant ((string) v);
448 else if (t == TypeManager.short_type)
449 return new ShortConstant ((short)v);
450 else if (t == TypeManager.ushort_type)
451 return new UShortConstant ((ushort)v);
452 else if (t == TypeManager.sbyte_type)
453 return new SByteConstant (((sbyte)v));
454 else if (t == TypeManager.byte_type)
455 return new ByteConstant ((byte)v);
456 else if (t == TypeManager.char_type)
457 return new CharConstant ((char)v);
458 else if (t == TypeManager.bool_type)
459 return new BoolConstant ((bool) v);
460 else if (TypeManager.IsEnumType (t)){
461 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
463 return new EnumConstant (e, t);
465 throw new Exception ("Unknown type for constant (" + t +
470 /// Returns a fully formed expression after a MemberLookup
472 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
475 return new EventExpr ((EventInfo) mi, loc);
476 else if (mi is FieldInfo)
477 return new FieldExpr ((FieldInfo) mi, loc);
478 else if (mi is PropertyInfo)
479 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
480 else if (mi is Type){
481 return new TypeExpression ((System.Type) mi, loc);
488 // FIXME: Probably implement a cache for (t,name,current_access_set)?
490 // This code could use some optimizations, but we need to do some
491 // measurements. For example, we could use a delegate to `flag' when
492 // something can not any longer be a method-group (because it is something
496 // If the return value is an Array, then it is an array of
499 // If the return value is an MemberInfo, it is anything, but a Method
503 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
504 // the arguments here and have MemberLookup return only the methods that
505 // match the argument count/type, unlike we are doing now (we delay this
508 // This is so we can catch correctly attempts to invoke instance methods
509 // from a static body (scan for error 120 in ResolveSimpleName).
512 // FIXME: Potential optimization, have a static ArrayList
515 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
516 MemberTypes mt, BindingFlags bf, Location loc)
518 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
522 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
523 // `qualifier_type' or null to lookup members in the current class.
526 public static Expression MemberLookup (EmitContext ec, Type container_type,
527 Type qualifier_type, Type queried_type,
528 string name, MemberTypes mt,
529 BindingFlags bf, Location loc)
531 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
532 queried_type, mt, bf, name);
537 int count = mi.Length;
539 if (mi [0] is MethodBase)
540 return new MethodGroupExpr (mi, loc);
545 return ExprClassFromMemberInfo (ec, mi [0], loc);
548 public const MemberTypes AllMemberTypes =
549 MemberTypes.Constructor |
553 MemberTypes.NestedType |
554 MemberTypes.Property;
556 public const BindingFlags AllBindingFlags =
557 BindingFlags.Public |
558 BindingFlags.Static |
559 BindingFlags.Instance;
561 public static Expression MemberLookup (EmitContext ec, Type queried_type,
562 string name, Location loc)
564 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
565 AllMemberTypes, AllBindingFlags, loc);
568 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
569 Type queried_type, string name, Location loc)
571 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
572 name, AllMemberTypes, AllBindingFlags, loc);
575 public static Expression MethodLookup (EmitContext ec, Type queried_type,
576 string name, Location loc)
578 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
579 MemberTypes.Method, AllBindingFlags, loc);
583 /// This is a wrapper for MemberLookup that is not used to "probe", but
584 /// to find a final definition. If the final definition is not found, we
585 /// look for private members and display a useful debugging message if we
588 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
589 Type queried_type, string name, Location loc)
591 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
592 AllMemberTypes, AllBindingFlags, loc);
595 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
596 Type queried_type, string name,
597 MemberTypes mt, BindingFlags bf,
602 int errors = Report.Errors;
604 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
610 // Error has already been reported.
611 if (errors < Report.Errors)
614 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
618 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
619 Type queried_type, string name,
620 string class_name, Location loc)
622 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
623 AllMemberTypes, AllBindingFlags |
624 BindingFlags.NonPublic, name);
626 if (lookup == null) {
627 if (class_name != null)
628 Report.Error (103, loc, "The name `" + name + "' could not be " +
629 "found in `" + class_name + "'");
632 117, loc, "`" + queried_type + "' does not contain a " +
633 "definition for `" + name + "'");
637 if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
638 ec.ContainerType.IsSubclassOf (qualifier_type)) {
639 // Although a derived class can access protected members of
640 // its base class it cannot do so through an instance of the
641 // base class (CS1540). If the qualifier_type is a parent of the
642 // ec.ContainerType and the lookup succeeds with the latter one,
643 // then we are in this situation.
645 lookup = TypeManager.MemberLookup (
646 ec.ContainerType, ec.ContainerType, ec.ContainerType,
647 AllMemberTypes, AllBindingFlags, name);
649 if (lookup != null) {
651 1540, loc, "Cannot access protected member `" +
652 TypeManager.CSharpName (qualifier_type) + "." +
653 name + "' " + "via a qualifier of type `" +
654 TypeManager.CSharpName (qualifier_type) + "'; the " +
655 "qualifier must be of type `" +
656 TypeManager.CSharpName (ec.ContainerType) + "' " +
657 "(or derived from it)");
662 if (qualifier_type != null)
664 122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
665 name + "' is inaccessible due to its protection level");
668 122, loc, "`" + name + "' is inaccessible due to its " +
672 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
674 EventInfo ei = event_expr.EventInfo;
676 return TypeManager.GetPrivateFieldOfEvent (ei);
680 /// Returns an expression that can be used to invoke operator true
681 /// on the expression if it exists.
683 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
685 return GetOperatorTrueOrFalse (ec, e, true, loc);
689 /// Returns an expression that can be used to invoke operator false
690 /// on the expression if it exists.
692 static public StaticCallExpr GetOperatorFalse (EmitContext ec, Expression e, Location loc)
694 return GetOperatorTrueOrFalse (ec, e, false, loc);
697 static StaticCallExpr GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
700 Expression operator_group;
702 operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
703 if (operator_group == null)
706 ArrayList arguments = new ArrayList ();
707 arguments.Add (new Argument (e, Argument.AType.Expression));
708 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc);
713 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
717 /// Resolves the expression `e' into a boolean expression: either through
718 /// an implicit conversion, or through an `operator true' invocation
720 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
726 Expression converted = e;
727 if (e.Type != TypeManager.bool_type)
728 converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
731 // If no implicit conversion to bool exists, try using `operator true'
733 if (converted == null){
734 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
735 if (operator_true == null){
737 31, loc, "Can not convert the expression to a boolean");
747 static string ExprClassName (ExprClass c)
750 case ExprClass.Invalid:
752 case ExprClass.Value:
754 case ExprClass.Variable:
756 case ExprClass.Namespace:
760 case ExprClass.MethodGroup:
761 return "method group";
762 case ExprClass.PropertyAccess:
763 return "property access";
764 case ExprClass.EventAccess:
765 return "event access";
766 case ExprClass.IndexerAccess:
767 return "indexer access";
768 case ExprClass.Nothing:
771 throw new Exception ("Should not happen");
775 /// Reports that we were expecting `expr' to be of class `expected'
777 public void Error_UnexpectedKind (string expected)
779 string kind = "Unknown";
781 kind = ExprClassName (eclass);
783 Error (118, "Expression denotes a `" + kind +
784 "' where a `" + expected + "' was expected");
787 public void Error_UnexpectedKind (ResolveFlags flags)
789 ArrayList valid = new ArrayList (10);
791 if ((flags & ResolveFlags.VariableOrValue) != 0) {
792 valid.Add ("variable");
796 if ((flags & ResolveFlags.Type) != 0)
799 if ((flags & ResolveFlags.MethodGroup) != 0)
800 valid.Add ("method group");
802 if ((flags & ResolveFlags.SimpleName) != 0)
803 valid.Add ("simple name");
805 if (valid.Count == 0)
806 valid.Add ("unknown");
808 StringBuilder sb = new StringBuilder ();
809 for (int i = 0; i < valid.Count; i++) {
812 else if (i == valid.Count)
814 sb.Append (valid [i]);
817 string kind = ExprClassName (eclass);
819 Error (119, "Expression denotes a `" + kind + "' where " +
820 "a `" + sb.ToString () + "' was expected");
823 static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
825 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
826 TypeManager.CSharpName (t));
829 public static void UnsafeError (Location loc)
831 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
835 /// Converts the IntConstant, UIntConstant, LongConstant or
836 /// ULongConstant into the integral target_type. Notice
837 /// that we do not return an `Expression' we do return
838 /// a boxed integral type.
840 /// FIXME: Since I added the new constants, we need to
841 /// also support conversions from CharConstant, ByteConstant,
842 /// SByteConstant, UShortConstant, ShortConstant
844 /// This is used by the switch statement, so the domain
845 /// of work is restricted to the literals above, and the
846 /// targets are int32, uint32, char, byte, sbyte, ushort,
847 /// short, uint64 and int64
849 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
851 if (!Convert.ImplicitStandardConversionExists (c, target_type)){
852 Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
858 if (c.Type == target_type)
859 return ((Constant) c).GetValue ();
862 // Make into one of the literals we handle, we dont really care
863 // about this value as we will just return a few limited types
865 if (c is EnumConstant)
866 c = ((EnumConstant)c).WidenToCompilerConstant ();
868 if (c is IntConstant){
869 int v = ((IntConstant) c).Value;
871 if (target_type == TypeManager.uint32_type){
874 } else if (target_type == TypeManager.char_type){
875 if (v >= Char.MinValue && v <= Char.MaxValue)
877 } else if (target_type == TypeManager.byte_type){
878 if (v >= Byte.MinValue && v <= Byte.MaxValue)
880 } else if (target_type == TypeManager.sbyte_type){
881 if (v >= SByte.MinValue && v <= SByte.MaxValue)
883 } else if (target_type == TypeManager.short_type){
884 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
886 } else if (target_type == TypeManager.ushort_type){
887 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
889 } else if (target_type == TypeManager.int64_type)
891 else if (target_type == TypeManager.uint64_type){
897 } else if (c is UIntConstant){
898 uint v = ((UIntConstant) c).Value;
900 if (target_type == TypeManager.int32_type){
901 if (v <= Int32.MaxValue)
903 } else if (target_type == TypeManager.char_type){
904 if (v >= Char.MinValue && v <= Char.MaxValue)
906 } else if (target_type == TypeManager.byte_type){
907 if (v <= Byte.MaxValue)
909 } else if (target_type == TypeManager.sbyte_type){
910 if (v <= SByte.MaxValue)
912 } else if (target_type == TypeManager.short_type){
913 if (v <= UInt16.MaxValue)
915 } else if (target_type == TypeManager.ushort_type){
916 if (v <= UInt16.MaxValue)
918 } else if (target_type == TypeManager.int64_type)
920 else if (target_type == TypeManager.uint64_type)
923 } else if (c is LongConstant){
924 long v = ((LongConstant) c).Value;
926 if (target_type == TypeManager.int32_type){
927 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
929 } else if (target_type == TypeManager.uint32_type){
930 if (v >= 0 && v <= UInt32.MaxValue)
932 } else if (target_type == TypeManager.char_type){
933 if (v >= Char.MinValue && v <= Char.MaxValue)
935 } else if (target_type == TypeManager.byte_type){
936 if (v >= Byte.MinValue && v <= Byte.MaxValue)
938 } else if (target_type == TypeManager.sbyte_type){
939 if (v >= SByte.MinValue && v <= SByte.MaxValue)
941 } else if (target_type == TypeManager.short_type){
942 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
944 } else if (target_type == TypeManager.ushort_type){
945 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
947 } else if (target_type == TypeManager.uint64_type){
952 } else if (c is ULongConstant){
953 ulong v = ((ULongConstant) c).Value;
955 if (target_type == TypeManager.int32_type){
956 if (v <= Int32.MaxValue)
958 } else if (target_type == TypeManager.uint32_type){
959 if (v <= UInt32.MaxValue)
961 } else if (target_type == TypeManager.char_type){
962 if (v >= Char.MinValue && v <= Char.MaxValue)
964 } else if (target_type == TypeManager.byte_type){
965 if (v >= Byte.MinValue && v <= Byte.MaxValue)
967 } else if (target_type == TypeManager.sbyte_type){
968 if (v <= (int) SByte.MaxValue)
970 } else if (target_type == TypeManager.short_type){
971 if (v <= UInt16.MaxValue)
973 } else if (target_type == TypeManager.ushort_type){
974 if (v <= UInt16.MaxValue)
976 } else if (target_type == TypeManager.int64_type){
977 if (v <= Int64.MaxValue)
981 } else if (c is ByteConstant){
982 byte v = ((ByteConstant) c).Value;
984 if (target_type == TypeManager.int32_type)
986 else if (target_type == TypeManager.uint32_type)
988 else if (target_type == TypeManager.char_type)
990 else if (target_type == TypeManager.sbyte_type){
991 if (v <= SByte.MaxValue)
993 } else if (target_type == TypeManager.short_type)
995 else if (target_type == TypeManager.ushort_type)
997 else if (target_type == TypeManager.int64_type)
999 else if (target_type == TypeManager.uint64_type)
1002 } else if (c is SByteConstant){
1003 sbyte v = ((SByteConstant) c).Value;
1005 if (target_type == TypeManager.int32_type)
1007 else if (target_type == TypeManager.uint32_type){
1010 } else if (target_type == TypeManager.char_type){
1013 } else if (target_type == TypeManager.byte_type){
1016 } else if (target_type == TypeManager.short_type)
1018 else if (target_type == TypeManager.ushort_type){
1021 } else if (target_type == TypeManager.int64_type)
1023 else if (target_type == TypeManager.uint64_type){
1028 } else if (c is ShortConstant){
1029 short v = ((ShortConstant) c).Value;
1031 if (target_type == TypeManager.int32_type){
1033 } else if (target_type == TypeManager.uint32_type){
1036 } else if (target_type == TypeManager.char_type){
1039 } else if (target_type == TypeManager.byte_type){
1040 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1042 } else if (target_type == TypeManager.sbyte_type){
1043 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1045 } else if (target_type == TypeManager.ushort_type){
1048 } else if (target_type == TypeManager.int64_type)
1050 else if (target_type == TypeManager.uint64_type)
1054 } else if (c is UShortConstant){
1055 ushort v = ((UShortConstant) c).Value;
1057 if (target_type == TypeManager.int32_type)
1059 else if (target_type == TypeManager.uint32_type)
1061 else if (target_type == TypeManager.char_type){
1062 if (v >= Char.MinValue && v <= Char.MaxValue)
1064 } else if (target_type == TypeManager.byte_type){
1065 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1067 } else if (target_type == TypeManager.sbyte_type){
1068 if (v <= SByte.MaxValue)
1070 } else if (target_type == TypeManager.short_type){
1071 if (v <= Int16.MaxValue)
1073 } else if (target_type == TypeManager.int64_type)
1075 else if (target_type == TypeManager.uint64_type)
1079 } else if (c is CharConstant){
1080 char v = ((CharConstant) c).Value;
1082 if (target_type == TypeManager.int32_type)
1084 else if (target_type == TypeManager.uint32_type)
1086 else if (target_type == TypeManager.byte_type){
1087 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1089 } else if (target_type == TypeManager.sbyte_type){
1090 if (v <= SByte.MaxValue)
1092 } else if (target_type == TypeManager.short_type){
1093 if (v <= Int16.MaxValue)
1095 } else if (target_type == TypeManager.ushort_type)
1097 else if (target_type == TypeManager.int64_type)
1099 else if (target_type == TypeManager.uint64_type)
1104 Error_ConstantValueCannotBeConverted (loc, s, target_type);
1109 // Load the object from the pointer.
1111 public static void LoadFromPtr (ILGenerator ig, Type t)
1113 if (t == TypeManager.int32_type)
1114 ig.Emit (OpCodes.Ldind_I4);
1115 else if (t == TypeManager.uint32_type)
1116 ig.Emit (OpCodes.Ldind_U4);
1117 else if (t == TypeManager.short_type)
1118 ig.Emit (OpCodes.Ldind_I2);
1119 else if (t == TypeManager.ushort_type)
1120 ig.Emit (OpCodes.Ldind_U2);
1121 else if (t == TypeManager.char_type)
1122 ig.Emit (OpCodes.Ldind_U2);
1123 else if (t == TypeManager.byte_type)
1124 ig.Emit (OpCodes.Ldind_U1);
1125 else if (t == TypeManager.sbyte_type)
1126 ig.Emit (OpCodes.Ldind_I1);
1127 else if (t == TypeManager.uint64_type)
1128 ig.Emit (OpCodes.Ldind_I8);
1129 else if (t == TypeManager.int64_type)
1130 ig.Emit (OpCodes.Ldind_I8);
1131 else if (t == TypeManager.float_type)
1132 ig.Emit (OpCodes.Ldind_R4);
1133 else if (t == TypeManager.double_type)
1134 ig.Emit (OpCodes.Ldind_R8);
1135 else if (t == TypeManager.bool_type)
1136 ig.Emit (OpCodes.Ldind_I1);
1137 else if (t == TypeManager.intptr_type)
1138 ig.Emit (OpCodes.Ldind_I);
1139 else if (TypeManager.IsEnumType (t)) {
1140 if (t == TypeManager.enum_type)
1141 ig.Emit (OpCodes.Ldind_Ref);
1143 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1144 } else if (t.IsValueType)
1145 ig.Emit (OpCodes.Ldobj, t);
1146 else if (t.IsPointer)
1147 ig.Emit (OpCodes.Ldind_I);
1149 ig.Emit (OpCodes.Ldind_Ref);
1153 // The stack contains the pointer and the value of type `type'
1155 public static void StoreFromPtr (ILGenerator ig, Type type)
1157 if (TypeManager.IsEnumType (type))
1158 type = TypeManager.EnumToUnderlying (type);
1159 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1160 ig.Emit (OpCodes.Stind_I4);
1161 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1162 ig.Emit (OpCodes.Stind_I8);
1163 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1164 type == TypeManager.ushort_type)
1165 ig.Emit (OpCodes.Stind_I2);
1166 else if (type == TypeManager.float_type)
1167 ig.Emit (OpCodes.Stind_R4);
1168 else if (type == TypeManager.double_type)
1169 ig.Emit (OpCodes.Stind_R8);
1170 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1171 type == TypeManager.bool_type)
1172 ig.Emit (OpCodes.Stind_I1);
1173 else if (type == TypeManager.intptr_type)
1174 ig.Emit (OpCodes.Stind_I);
1175 else if (type.IsValueType)
1176 ig.Emit (OpCodes.Stobj, type);
1178 ig.Emit (OpCodes.Stind_Ref);
1182 // Returns the size of type `t' if known, otherwise, 0
1184 public static int GetTypeSize (Type t)
1186 t = TypeManager.TypeToCoreType (t);
1187 if (t == TypeManager.int32_type ||
1188 t == TypeManager.uint32_type ||
1189 t == TypeManager.float_type)
1191 else if (t == TypeManager.int64_type ||
1192 t == TypeManager.uint64_type ||
1193 t == TypeManager.double_type)
1195 else if (t == TypeManager.byte_type ||
1196 t == TypeManager.sbyte_type ||
1197 t == TypeManager.bool_type)
1199 else if (t == TypeManager.short_type ||
1200 t == TypeManager.char_type ||
1201 t == TypeManager.ushort_type)
1203 else if (t == TypeManager.decimal_type)
1210 // Default implementation of IAssignMethod.CacheTemporaries
1212 public void CacheTemporaries (EmitContext ec)
1216 static void Error_NegativeArrayIndex (Location loc)
1218 Report.Error (284, loc, "Can not create array with a negative size");
1222 // Converts `source' to an int, uint, long or ulong.
1224 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1228 bool old_checked = ec.CheckState;
1229 ec.CheckState = true;
1231 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1232 if (target == null){
1233 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1234 if (target == null){
1235 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1236 if (target == null){
1237 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1239 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1243 ec.CheckState = old_checked;
1246 // Only positive constants are allowed at compile time
1248 if (target is Constant){
1249 if (target is IntConstant){
1250 if (((IntConstant) target).Value < 0){
1251 Error_NegativeArrayIndex (loc);
1256 if (target is LongConstant){
1257 if (((LongConstant) target).Value < 0){
1258 Error_NegativeArrayIndex (loc);
1271 /// This is just a base class for expressions that can
1272 /// appear on statements (invocations, object creation,
1273 /// assignments, post/pre increment and decrement). The idea
1274 /// being that they would support an extra Emition interface that
1275 /// does not leave a result on the stack.
1277 public abstract class ExpressionStatement : Expression {
1279 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1281 Expression e = Resolve (ec);
1285 ExpressionStatement es = e as ExpressionStatement;
1287 Error (201, "Only assignment, call, increment, decrement and new object " +
1288 "expressions can be used as a statement");
1294 /// Requests the expression to be emitted in a `statement'
1295 /// context. This means that no new value is left on the
1296 /// stack after invoking this method (constrasted with
1297 /// Emit that will always leave a value on the stack).
1299 public abstract void EmitStatement (EmitContext ec);
1303 /// This kind of cast is used to encapsulate the child
1304 /// whose type is child.Type into an expression that is
1305 /// reported to return "return_type". This is used to encapsulate
1306 /// expressions which have compatible types, but need to be dealt
1307 /// at higher levels with.
1309 /// For example, a "byte" expression could be encapsulated in one
1310 /// of these as an "unsigned int". The type for the expression
1311 /// would be "unsigned int".
1314 public class EmptyCast : Expression {
1315 protected Expression child;
1317 public EmptyCast (Expression child, Type return_type)
1319 eclass = child.eclass;
1324 public override Expression DoResolve (EmitContext ec)
1326 // This should never be invoked, we are born in fully
1327 // initialized state.
1332 public override void Emit (EmitContext ec)
1339 // We need to special case this since an empty cast of
1340 // a NullLiteral is still a Constant
1342 public class NullCast : Constant {
1343 protected Expression child;
1345 public NullCast (Expression child, Type return_type)
1347 eclass = child.eclass;
1352 override public string AsString ()
1357 public override object GetValue ()
1362 public override Expression DoResolve (EmitContext ec)
1364 // This should never be invoked, we are born in fully
1365 // initialized state.
1370 public override void Emit (EmitContext ec)
1378 /// This class is used to wrap literals which belong inside Enums
1380 public class EnumConstant : Constant {
1381 public Constant Child;
1383 public EnumConstant (Constant child, Type enum_type)
1385 eclass = child.eclass;
1390 public override Expression DoResolve (EmitContext ec)
1392 // This should never be invoked, we are born in fully
1393 // initialized state.
1398 public override void Emit (EmitContext ec)
1403 public override object GetValue ()
1405 return Child.GetValue ();
1409 // Converts from one of the valid underlying types for an enumeration
1410 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1411 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1413 public Constant WidenToCompilerConstant ()
1415 Type t = TypeManager.EnumToUnderlying (Child.Type);
1416 object v = ((Constant) Child).GetValue ();;
1418 if (t == TypeManager.int32_type)
1419 return new IntConstant ((int) v);
1420 if (t == TypeManager.uint32_type)
1421 return new UIntConstant ((uint) v);
1422 if (t == TypeManager.int64_type)
1423 return new LongConstant ((long) v);
1424 if (t == TypeManager.uint64_type)
1425 return new ULongConstant ((ulong) v);
1426 if (t == TypeManager.short_type)
1427 return new ShortConstant ((short) v);
1428 if (t == TypeManager.ushort_type)
1429 return new UShortConstant ((ushort) v);
1430 if (t == TypeManager.byte_type)
1431 return new ByteConstant ((byte) v);
1432 if (t == TypeManager.sbyte_type)
1433 return new SByteConstant ((sbyte) v);
1435 throw new Exception ("Invalid enumeration underlying type: " + t);
1439 // Extracts the value in the enumeration on its native representation
1441 public object GetPlainValue ()
1443 Type t = TypeManager.EnumToUnderlying (Child.Type);
1444 object v = ((Constant) Child).GetValue ();;
1446 if (t == TypeManager.int32_type)
1448 if (t == TypeManager.uint32_type)
1450 if (t == TypeManager.int64_type)
1452 if (t == TypeManager.uint64_type)
1454 if (t == TypeManager.short_type)
1456 if (t == TypeManager.ushort_type)
1458 if (t == TypeManager.byte_type)
1460 if (t == TypeManager.sbyte_type)
1466 public override string AsString ()
1468 return Child.AsString ();
1471 public override DoubleConstant ConvertToDouble ()
1473 return Child.ConvertToDouble ();
1476 public override FloatConstant ConvertToFloat ()
1478 return Child.ConvertToFloat ();
1481 public override ULongConstant ConvertToULong ()
1483 return Child.ConvertToULong ();
1486 public override LongConstant ConvertToLong ()
1488 return Child.ConvertToLong ();
1491 public override UIntConstant ConvertToUInt ()
1493 return Child.ConvertToUInt ();
1496 public override IntConstant ConvertToInt ()
1498 return Child.ConvertToInt ();
1503 /// This kind of cast is used to encapsulate Value Types in objects.
1505 /// The effect of it is to box the value type emitted by the previous
1508 public class BoxedCast : EmptyCast {
1510 public BoxedCast (Expression expr)
1511 : base (expr, TypeManager.object_type)
1515 public BoxedCast (Expression expr, Type target_type)
1516 : base (expr, target_type)
1520 public override Expression DoResolve (EmitContext ec)
1522 // This should never be invoked, we are born in fully
1523 // initialized state.
1528 public override void Emit (EmitContext ec)
1532 ec.ig.Emit (OpCodes.Box, child.Type);
1536 public class UnboxCast : EmptyCast {
1537 public UnboxCast (Expression expr, Type return_type)
1538 : base (expr, return_type)
1542 public override Expression DoResolve (EmitContext ec)
1544 // This should never be invoked, we are born in fully
1545 // initialized state.
1550 public override void Emit (EmitContext ec)
1553 ILGenerator ig = ec.ig;
1556 ig.Emit (OpCodes.Unbox, t);
1558 LoadFromPtr (ig, t);
1563 /// This is used to perform explicit numeric conversions.
1565 /// Explicit numeric conversions might trigger exceptions in a checked
1566 /// context, so they should generate the conv.ovf opcodes instead of
1569 public class ConvCast : EmptyCast {
1570 public enum Mode : byte {
1571 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1573 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1574 U2_I1, U2_U1, U2_I2, U2_CH,
1575 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1576 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1577 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1578 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1579 CH_I1, CH_U1, CH_I2,
1580 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1581 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1587 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1588 : base (child, return_type)
1590 checked_state = ec.CheckState;
1594 public override Expression DoResolve (EmitContext ec)
1596 // This should never be invoked, we are born in fully
1597 // initialized state.
1602 public override string ToString ()
1604 return String.Format ("ConvCast ({0}, {1})", mode, child);
1607 public override void Emit (EmitContext ec)
1609 ILGenerator ig = ec.ig;
1615 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1616 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1617 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1618 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1619 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1621 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1622 case Mode.U1_CH: /* nothing */ break;
1624 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1625 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1626 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1627 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1628 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1629 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1631 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1632 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1633 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1634 case Mode.U2_CH: /* nothing */ break;
1636 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1637 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1638 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1639 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1640 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1641 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1642 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1644 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1645 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1646 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1647 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1648 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1649 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1651 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1652 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1653 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1654 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1655 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1656 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1657 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1658 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1660 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1661 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1662 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1663 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1664 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1665 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1666 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1667 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1669 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1670 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1671 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1673 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1674 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1675 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1676 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1677 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1678 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1679 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1680 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1681 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1683 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1684 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1685 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1686 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1687 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1688 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1689 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1690 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1691 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1692 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1696 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1697 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1698 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1699 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1700 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1702 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1703 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1705 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1706 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1707 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1708 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1709 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1710 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1712 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1713 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1714 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1715 case Mode.U2_CH: /* nothing */ break;
1717 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1718 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1719 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1720 case Mode.I4_U4: /* nothing */ break;
1721 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1722 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1723 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1725 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1726 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1727 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1728 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1729 case Mode.U4_I4: /* nothing */ break;
1730 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1732 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1733 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1734 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1735 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1736 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1737 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1738 case Mode.I8_U8: /* nothing */ break;
1739 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1741 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1742 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1743 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1744 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1745 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1746 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1747 case Mode.U8_I8: /* nothing */ break;
1748 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1750 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1751 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1752 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1754 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1755 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1756 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1757 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1758 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1759 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1760 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1761 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1762 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1764 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1765 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1766 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1767 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1768 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1769 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1770 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1771 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1772 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1773 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1779 public class OpcodeCast : EmptyCast {
1783 public OpcodeCast (Expression child, Type return_type, OpCode op)
1784 : base (child, return_type)
1788 second_valid = false;
1791 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1792 : base (child, return_type)
1797 second_valid = true;
1800 public override Expression DoResolve (EmitContext ec)
1802 // This should never be invoked, we are born in fully
1803 // initialized state.
1808 public override void Emit (EmitContext ec)
1819 /// This kind of cast is used to encapsulate a child and cast it
1820 /// to the class requested
1822 public class ClassCast : EmptyCast {
1823 public ClassCast (Expression child, Type return_type)
1824 : base (child, return_type)
1829 public override Expression DoResolve (EmitContext ec)
1831 // This should never be invoked, we are born in fully
1832 // initialized state.
1837 public override void Emit (EmitContext ec)
1841 ec.ig.Emit (OpCodes.Castclass, type);
1847 /// SimpleName expressions are initially formed of a single
1848 /// word and it only happens at the beginning of the expression.
1852 /// The expression will try to be bound to a Field, a Method
1853 /// group or a Property. If those fail we pass the name to our
1854 /// caller and the SimpleName is compounded to perform a type
1855 /// lookup. The idea behind this process is that we want to avoid
1856 /// creating a namespace map from the assemblies, as that requires
1857 /// the GetExportedTypes function to be called and a hashtable to
1858 /// be constructed which reduces startup time. If later we find
1859 /// that this is slower, we should create a `NamespaceExpr' expression
1860 /// that fully participates in the resolution process.
1862 /// For example `System.Console.WriteLine' is decomposed into
1863 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1865 /// The first SimpleName wont produce a match on its own, so it will
1867 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
1869 /// System.Console will produce a TypeExpr match.
1871 /// The downside of this is that we might be hitting `LookupType' too many
1872 /// times with this scheme.
1874 public class SimpleName : Expression {
1878 // If true, then we are a simple name, not composed with a ".
1882 public SimpleName (string a, string b, Location l)
1884 Name = String.Concat (a, ".", b);
1889 public SimpleName (string name, Location l)
1896 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1898 if (ec.IsFieldInitializer)
1901 "A field initializer cannot reference the non-static field, " +
1902 "method or property `"+name+"'");
1906 "An object reference is required " +
1907 "for the non-static field `"+name+"'");
1911 // Checks whether we are trying to access an instance
1912 // property, method or field from a static body.
1914 Expression MemberStaticCheck (EmitContext ec, Expression e)
1916 if (e is IMemberExpr){
1917 IMemberExpr member = (IMemberExpr) e;
1919 if (!member.IsStatic){
1920 Error_ObjectRefRequired (ec, loc, Name);
1928 public override Expression DoResolve (EmitContext ec)
1930 return SimpleNameResolve (ec, null, false);
1933 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1935 return SimpleNameResolve (ec, right_side, false);
1939 public Expression DoResolveAllowStatic (EmitContext ec)
1941 return SimpleNameResolve (ec, null, true);
1944 public override Expression ResolveAsTypeStep (EmitContext ec)
1946 DeclSpace ds = ec.DeclSpace;
1947 NamespaceEntry ns = ds.NamespaceEntry;
1952 // Since we are cheating: we only do the Alias lookup for
1953 // namespaces if the name does not include any dots in it
1955 if (ns != null && is_base)
1956 alias_value = ns.LookupAlias (Name);
1960 if (ec.ResolvingTypeTree){
1961 if (alias_value != null){
1962 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1963 return new TypeExpression (t, loc);
1966 int errors = Report.Errors;
1967 Type dt = ec.DeclSpace.FindType (loc, Name);
1969 if (Report.Errors != errors)
1973 return new TypeExpression (dt, loc);
1977 // First, the using aliases
1979 if (alias_value != null){
1980 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1981 return new TypeExpression (t, loc);
1983 // we have alias value, but it isn't Type, so try if it's namespace
1984 return new SimpleName (alias_value, loc);
1988 // Stage 2: Lookup up if we are an alias to a type
1992 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
1993 return new TypeExpression (t, loc);
1995 // No match, maybe our parent can compose us
1996 // into something meaningful.
2001 /// 7.5.2: Simple Names.
2003 /// Local Variables and Parameters are handled at
2004 /// parse time, so they never occur as SimpleNames.
2006 /// The `allow_static' flag is used by MemberAccess only
2007 /// and it is used to inform us that it is ok for us to
2008 /// avoid the static check, because MemberAccess might end
2009 /// up resolving the Name as a Type name and the access as
2010 /// a static type access.
2012 /// ie: Type Type; .... { Type.GetType (""); }
2014 /// Type is both an instance variable and a Type; Type.GetType
2015 /// is the static method not an instance method of type.
2017 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
2019 Expression e = null;
2022 // Stage 1: Performed by the parser (binding to locals or parameters).
2024 Block current_block = ec.CurrentBlock;
2025 if (current_block != null){
2026 LocalInfo vi = current_block.GetLocalInfo (Name);
2030 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2032 if (right_side != null)
2033 return var.ResolveLValue (ec, right_side);
2035 return var.Resolve (ec);
2039 Parameter par = null;
2040 Parameters pars = current_block.Parameters;
2042 par = pars.GetParameterByName (Name, out idx);
2045 ParameterReference param;
2047 param = new ParameterReference (pars, current_block, idx, Name, loc);
2049 if (right_side != null)
2050 return param.ResolveLValue (ec, right_side);
2052 return param.Resolve (ec);
2057 // Stage 2: Lookup members
2060 DeclSpace lookup_ds = ec.DeclSpace;
2062 if (lookup_ds.TypeBuilder == null)
2065 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2069 lookup_ds =lookup_ds.Parent;
2070 } while (lookup_ds != null);
2072 if (e == null && ec.ContainerType != null)
2073 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2077 // Since we are cheating (is_base is our hint
2078 // that we are the beginning of the name): we
2079 // only do the Alias lookup for namespaces if
2080 // the name does not include any dots in it
2082 NamespaceEntry ns = ec.DeclSpace.NamespaceEntry;
2083 if (is_base && ns != null){
2084 string alias_value = ns.LookupAlias (Name);
2085 if (alias_value != null){
2089 if ((t = TypeManager.LookupType (Name)) != null)
2090 return new TypeExpression (t, loc);
2092 // No match, maybe our parent can compose us
2093 // into something meaningful.
2098 return ResolveAsTypeStep (ec);
2104 if (e is IMemberExpr) {
2105 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2109 IMemberExpr me = e as IMemberExpr;
2113 // This fails if ResolveMemberAccess() was unable to decide whether
2114 // it's a field or a type of the same name.
2115 if (!me.IsStatic && (me.InstanceExpression == null))
2119 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
2120 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
2121 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2122 "outer type `" + me.DeclaringType + "' via nested type `" +
2123 me.InstanceExpression.Type + "'");
2127 if (right_side != null)
2128 e = e.DoResolveLValue (ec, right_side);
2130 e = e.DoResolve (ec);
2135 if (ec.IsStatic || ec.IsFieldInitializer){
2139 return MemberStaticCheck (ec, e);
2144 public override void Emit (EmitContext ec)
2147 // If this is ever reached, then we failed to
2148 // find the name as a namespace
2151 Error (103, "The name `" + Name +
2152 "' does not exist in the class `" +
2153 ec.DeclSpace.Name + "'");
2156 public override string ToString ()
2163 /// Fully resolved expression that evaluates to a type
2165 public abstract class TypeExpr : Expression {
2166 override public Expression ResolveAsTypeStep (EmitContext ec)
2168 TypeExpr t = DoResolveAsTypeStep (ec);
2172 eclass = ExprClass.Type;
2176 override public Expression DoResolve (EmitContext ec)
2178 return ResolveAsTypeTerminal (ec);
2181 override public void Emit (EmitContext ec)
2183 throw new Exception ("Should never be called");
2186 public virtual bool CheckAccessLevel (DeclSpace ds)
2188 return ds.CheckAccessLevel (Type);
2191 public virtual bool AsAccessible (DeclSpace ds, int flags)
2193 return ds.AsAccessible (Type, flags);
2196 public virtual bool IsClass {
2197 get { return Type.IsClass; }
2200 public virtual bool IsValueType {
2201 get { return Type.IsValueType; }
2204 public virtual bool IsInterface {
2205 get { return Type.IsInterface; }
2208 public virtual bool IsSealed {
2209 get { return Type.IsSealed; }
2212 public virtual bool CanInheritFrom ()
2214 if (Type == TypeManager.enum_type ||
2215 (Type == TypeManager.value_type && RootContext.StdLib) ||
2216 Type == TypeManager.delegate_type ||
2217 Type == TypeManager.array_type)
2223 public virtual bool IsAttribute {
2225 return Type == TypeManager.attribute_type ||
2226 Type.IsSubclassOf (TypeManager.attribute_type);
2230 public virtual TypeExpr[] GetInterfaces ()
2232 return TypeManager.GetInterfaces (Type);
2235 public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
2237 public virtual Type ResolveType (EmitContext ec)
2239 TypeExpr t = ResolveAsTypeTerminal (ec);
2246 public abstract string Name {
2250 public override bool Equals (object obj)
2252 TypeExpr tobj = obj as TypeExpr;
2256 return Type == tobj.Type;
2259 public override string ToString ()
2265 public class TypeExpression : TypeExpr {
2266 public TypeExpression (Type t, Location l)
2269 eclass = ExprClass.Type;
2273 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2278 public override string Name {
2280 return Type.ToString ();
2286 /// Used to create types from a fully qualified name. These are just used
2287 /// by the parser to setup the core types. A TypeLookupExpression is always
2288 /// classified as a type.
2290 public class TypeLookupExpression : TypeExpr {
2293 public TypeLookupExpression (string name)
2298 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2301 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
2305 public override string Name {
2313 /// MethodGroup Expression.
2315 /// This is a fully resolved expression that evaluates to a type
2317 public class MethodGroupExpr : Expression, IMemberExpr {
2318 public MethodBase [] Methods;
2319 Expression instance_expression = null;
2320 bool is_explicit_impl = false;
2322 public MethodGroupExpr (MemberInfo [] mi, Location l)
2324 Methods = new MethodBase [mi.Length];
2325 mi.CopyTo (Methods, 0);
2326 eclass = ExprClass.MethodGroup;
2327 type = TypeManager.object_type;
2331 public MethodGroupExpr (ArrayList list, Location l)
2333 Methods = new MethodBase [list.Count];
2336 list.CopyTo (Methods, 0);
2338 foreach (MemberInfo m in list){
2339 if (!(m is MethodBase)){
2340 Console.WriteLine ("Name " + m.Name);
2341 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2348 eclass = ExprClass.MethodGroup;
2349 type = TypeManager.object_type;
2352 public Type DeclaringType {
2355 // The methods are arranged in this order:
2356 // derived type -> base type
2358 return Methods [0].DeclaringType;
2363 // `A method group may have associated an instance expression'
2365 public Expression InstanceExpression {
2367 return instance_expression;
2371 instance_expression = value;
2375 public bool IsExplicitImpl {
2377 return is_explicit_impl;
2381 is_explicit_impl = value;
2385 public string Name {
2387 return Methods [0].Name;
2391 public bool IsInstance {
2393 foreach (MethodBase mb in Methods)
2401 public bool IsStatic {
2403 foreach (MethodBase mb in Methods)
2411 override public Expression DoResolve (EmitContext ec)
2414 instance_expression = null;
2416 if (instance_expression != null) {
2417 instance_expression = instance_expression.DoResolve (ec);
2418 if (instance_expression == null)
2425 public void ReportUsageError ()
2427 Report.Error (654, loc, "Method `" + DeclaringType + "." +
2428 Name + "()' is referenced without parentheses");
2431 override public void Emit (EmitContext ec)
2433 ReportUsageError ();
2436 bool RemoveMethods (bool keep_static)
2438 ArrayList smethods = new ArrayList ();
2440 foreach (MethodBase mb in Methods){
2441 if (mb.IsStatic == keep_static)
2445 if (smethods.Count == 0)
2448 Methods = new MethodBase [smethods.Count];
2449 smethods.CopyTo (Methods, 0);
2455 /// Removes any instance methods from the MethodGroup, returns
2456 /// false if the resulting set is empty.
2458 public bool RemoveInstanceMethods ()
2460 return RemoveMethods (true);
2464 /// Removes any static methods from the MethodGroup, returns
2465 /// false if the resulting set is empty.
2467 public bool RemoveStaticMethods ()
2469 return RemoveMethods (false);
2474 /// Fully resolved expression that evaluates to a Field
2476 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2477 public readonly FieldInfo FieldInfo;
2478 Expression instance_expr;
2479 VariableInfo variable_info;
2481 public FieldExpr (FieldInfo fi, Location l)
2484 eclass = ExprClass.Variable;
2485 type = fi.FieldType;
2489 public string Name {
2491 return FieldInfo.Name;
2495 public bool IsInstance {
2497 return !FieldInfo.IsStatic;
2501 public bool IsStatic {
2503 return FieldInfo.IsStatic;
2507 public Type DeclaringType {
2509 return FieldInfo.DeclaringType;
2513 public Expression InstanceExpression {
2515 return instance_expr;
2519 instance_expr = value;
2523 public VariableInfo VariableInfo {
2525 return variable_info;
2529 override public Expression DoResolve (EmitContext ec)
2531 if (!FieldInfo.IsStatic){
2532 if (instance_expr == null){
2534 // This can happen when referencing an instance field using
2535 // a fully qualified type expression: TypeName.InstanceField = xxx
2537 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2541 // Resolve the field's instance expression while flow analysis is turned
2542 // off: when accessing a field "a.b", we must check whether the field
2543 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2544 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2545 ResolveFlags.DisableFlowAnalysis);
2546 if (instance_expr == null)
2550 // If the instance expression is a local variable or parameter.
2551 IVariable var = instance_expr as IVariable;
2552 if ((var == null) || (var.VariableInfo == null))
2555 VariableInfo vi = var.VariableInfo;
2556 if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2559 variable_info = vi.GetSubStruct (FieldInfo.Name);
2563 void Report_AssignToReadonly (bool is_instance)
2568 msg = "Readonly field can not be assigned outside " +
2569 "of constructor or variable initializer";
2571 msg = "A static readonly field can only be assigned in " +
2572 "a static constructor";
2574 Report.Error (is_instance ? 191 : 198, loc, msg);
2577 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2579 IVariable var = instance_expr as IVariable;
2580 if ((var != null) && (var.VariableInfo != null))
2581 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2583 Expression e = DoResolve (ec);
2588 if (!FieldInfo.IsStatic && (instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation))) {
2589 // FIXME: Provide better error reporting.
2590 Error (1612, "Cannot modify expression because it is not a variable.");
2594 if (!FieldInfo.IsInitOnly)
2597 FieldBase fb = TypeManager.GetField (FieldInfo);
2599 fb.IsAssigned = true;
2602 // InitOnly fields can only be assigned in constructors
2605 if (ec.IsConstructor){
2606 if (IsStatic && !ec.IsStatic)
2607 Report_AssignToReadonly (false);
2609 if (ec.ContainerType == FieldInfo.DeclaringType)
2613 Report_AssignToReadonly (true);
2618 public bool VerifyFixed (bool is_expression)
2620 IVariable variable = instance_expr as IVariable;
2621 if ((variable == null) || !variable.VerifyFixed (true))
2627 override public void Emit (EmitContext ec)
2629 ILGenerator ig = ec.ig;
2630 bool is_volatile = false;
2632 if (FieldInfo is FieldBuilder){
2633 FieldBase f = TypeManager.GetField (FieldInfo);
2635 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2638 f.status |= Field.Status.USED;
2642 if (FieldInfo.IsStatic){
2644 ig.Emit (OpCodes.Volatile);
2646 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2650 if (instance_expr.Type.IsValueType){
2652 LocalTemporary tempo = null;
2654 if (!(instance_expr is IMemoryLocation)){
2655 tempo = new LocalTemporary (ec, instance_expr.Type);
2657 if (ec.RemapToProxy)
2660 InstanceExpression.Emit (ec);
2664 ml = (IMemoryLocation) instance_expr;
2666 ml.AddressOf (ec, AddressOp.Load);
2668 if (ec.RemapToProxy)
2671 instance_expr.Emit (ec);
2674 ig.Emit (OpCodes.Volatile);
2676 ig.Emit (OpCodes.Ldfld, FieldInfo);
2679 public void EmitAssign (EmitContext ec, Expression source)
2681 FieldAttributes fa = FieldInfo.Attributes;
2682 bool is_static = (fa & FieldAttributes.Static) != 0;
2683 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
2684 ILGenerator ig = ec.ig;
2686 if (is_readonly && !ec.IsConstructor){
2687 Report_AssignToReadonly (!is_static);
2692 Expression instance = instance_expr;
2694 if (instance.Type.IsValueType){
2695 IMemoryLocation ml = (IMemoryLocation) instance;
2697 ml.AddressOf (ec, AddressOp.Store);
2699 if (ec.RemapToProxy)
2708 if (FieldInfo is FieldBuilder){
2709 FieldBase f = TypeManager.GetField (FieldInfo);
2711 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2712 ig.Emit (OpCodes.Volatile);
2714 f.status |= Field.Status.ASSIGNED;
2719 ig.Emit (OpCodes.Stsfld, FieldInfo);
2721 ig.Emit (OpCodes.Stfld, FieldInfo);
2724 public void AddressOf (EmitContext ec, AddressOp mode)
2726 ILGenerator ig = ec.ig;
2728 if (FieldInfo is FieldBuilder){
2729 FieldBase f = TypeManager.GetField (FieldInfo);
2731 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
2732 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
2736 if ((mode & AddressOp.Store) != 0)
2737 f.status |= Field.Status.ASSIGNED;
2738 if ((mode & AddressOp.Load) != 0)
2739 f.status |= Field.Status.USED;
2744 // Handle initonly fields specially: make a copy and then
2745 // get the address of the copy.
2747 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
2751 local = ig.DeclareLocal (type);
2752 ig.Emit (OpCodes.Stloc, local);
2753 ig.Emit (OpCodes.Ldloca, local);
2757 if (FieldInfo.IsStatic)
2758 ig.Emit (OpCodes.Ldsflda, FieldInfo);
2761 // In the case of `This', we call the AddressOf method, which will
2762 // only load the pointer, and not perform an Ldobj immediately after
2763 // the value has been loaded into the stack.
2765 if (instance_expr is This)
2766 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
2767 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
2768 IMemoryLocation ml = (IMemoryLocation) instance_expr;
2770 ml.AddressOf (ec, AddressOp.LoadStore);
2772 instance_expr.Emit (ec);
2773 ig.Emit (OpCodes.Ldflda, FieldInfo);
2779 // A FieldExpr whose address can not be taken
2781 public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
2782 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
2786 public new void AddressOf (EmitContext ec, AddressOp mode)
2788 Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
2793 /// Expression that evaluates to a Property. The Assign class
2794 /// might set the `Value' expression if we are in an assignment.
2796 /// This is not an LValue because we need to re-write the expression, we
2797 /// can not take data from the stack and store it.
2799 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
2800 public readonly PropertyInfo PropertyInfo;
2803 // This is set externally by the `BaseAccess' class
2806 MethodInfo getter, setter;
2808 bool must_do_cs1540_check;
2810 Expression instance_expr;
2812 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
2815 eclass = ExprClass.PropertyAccess;
2819 type = TypeManager.TypeToCoreType (pi.PropertyType);
2821 ResolveAccessors (ec);
2824 public string Name {
2826 return PropertyInfo.Name;
2830 public bool IsInstance {
2836 public bool IsStatic {
2842 public Type DeclaringType {
2844 return PropertyInfo.DeclaringType;
2849 // The instance expression associated with this expression
2851 public Expression InstanceExpression {
2853 instance_expr = value;
2857 return instance_expr;
2861 public bool VerifyAssignable ()
2863 if (setter == null) {
2864 Report.Error (200, loc,
2865 "The property `" + PropertyInfo.Name +
2866 "' can not be assigned to, as it has not set accessor");
2873 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
2875 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
2876 BindingFlags.Static | BindingFlags.Instance;
2879 group = TypeManager.MemberLookup (
2880 invocation_type, invocation_type, PropertyInfo.DeclaringType,
2881 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
2884 // The first method is the closest to us
2889 foreach (MethodInfo mi in group) {
2890 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
2893 // If only accessible to the current class or children
2895 if (ma == MethodAttributes.Private) {
2896 Type declaring_type = mi.DeclaringType;
2898 if (invocation_type != declaring_type){
2899 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2907 // FamAndAssem requires that we not only derivate, but we are on the
2910 if (ma == MethodAttributes.FamANDAssem){
2911 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2917 // Assembly and FamORAssem succeed if we're in the same assembly.
2918 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2919 if (mi.DeclaringType.Assembly == invocation_type.Assembly)
2923 // We already know that we aren't in the same assembly.
2924 if (ma == MethodAttributes.Assembly)
2927 // Family and FamANDAssem require that we derive.
2928 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem) || (ma == MethodAttributes.FamORAssem)){
2929 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2932 must_do_cs1540_check = true;
2945 // We also perform the permission checking here, as the PropertyInfo does not
2946 // hold the information for the accessibility of its setter/getter
2948 void ResolveAccessors (EmitContext ec)
2950 getter = GetAccessor (ec.ContainerType, "get");
2951 if ((getter != null) && getter.IsStatic)
2954 setter = GetAccessor (ec.ContainerType, "set");
2955 if ((setter != null) && setter.IsStatic)
2958 if (setter == null && getter == null){
2959 Error (122, "`" + PropertyInfo.Name + "' " +
2960 "is inaccessible because of its protection level");
2965 bool InstanceResolve (EmitContext ec)
2967 if ((instance_expr == null) && ec.IsStatic && !is_static) {
2968 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
2972 if (instance_expr != null) {
2973 instance_expr = instance_expr.DoResolve (ec);
2974 if (instance_expr == null)
2978 if (must_do_cs1540_check && (instance_expr != null)) {
2979 if ((instance_expr.Type != ec.ContainerType) &&
2980 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
2981 Report.Error (1540, loc, "Cannot access protected member `" +
2982 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
2983 "' via a qualifier of type `" +
2984 TypeManager.CSharpName (instance_expr.Type) +
2985 "'; the qualifier must be of type `" +
2986 TypeManager.CSharpName (ec.ContainerType) +
2987 "' (or derived from it)");
2995 override public Expression DoResolve (EmitContext ec)
2997 if (getter == null){
2999 // The following condition happens if the PropertyExpr was
3000 // created, but is invalid (ie, the property is inaccessible),
3001 // and we did not want to embed the knowledge about this in
3002 // the caller routine. This only avoids double error reporting.
3007 Report.Error (154, loc,
3008 "The property `" + PropertyInfo.Name +
3009 "' can not be used in " +
3010 "this context because it lacks a get accessor");
3014 if (!InstanceResolve (ec))
3018 // Only base will allow this invocation to happen.
3020 if (IsBase && getter.IsAbstract){
3021 Report.Error (205, loc, "Cannot call an abstract base property: " +
3022 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3029 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3031 if (setter == null){
3033 // The following condition happens if the PropertyExpr was
3034 // created, but is invalid (ie, the property is inaccessible),
3035 // and we did not want to embed the knowledge about this in
3036 // the caller routine. This only avoids double error reporting.
3041 Report.Error (154, loc,
3042 "The property `" + PropertyInfo.Name +
3043 "' can not be used in " +
3044 "this context because it lacks a set accessor");
3048 if (!InstanceResolve (ec))
3052 // Only base will allow this invocation to happen.
3054 if (IsBase && setter.IsAbstract){
3055 Report.Error (205, loc, "Cannot call an abstract base property: " +
3056 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3062 override public void Emit (EmitContext ec)
3065 // Special case: length of single dimension array property is turned into ldlen
3067 if ((getter == TypeManager.system_int_array_get_length) ||
3068 (getter == TypeManager.int_array_get_length)){
3069 Type iet = instance_expr.Type;
3072 // System.Array.Length can be called, but the Type does not
3073 // support invoking GetArrayRank, so test for that case first
3075 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
3076 instance_expr.Emit (ec);
3077 ec.ig.Emit (OpCodes.Ldlen);
3078 ec.ig.Emit (OpCodes.Conv_I4);
3083 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
3088 // Implements the IAssignMethod interface for assignments
3090 public void EmitAssign (EmitContext ec, Expression source)
3092 Argument arg = new Argument (source, Argument.AType.Expression);
3093 ArrayList args = new ArrayList ();
3096 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
3099 override public void EmitStatement (EmitContext ec)
3102 ec.ig.Emit (OpCodes.Pop);
3107 /// Fully resolved expression that evaluates to an Event
3109 public class EventExpr : Expression, IMemberExpr {
3110 public readonly EventInfo EventInfo;
3111 public Expression instance_expr;
3114 MethodInfo add_accessor, remove_accessor;
3116 public EventExpr (EventInfo ei, Location loc)
3120 eclass = ExprClass.EventAccess;
3122 add_accessor = TypeManager.GetAddMethod (ei);
3123 remove_accessor = TypeManager.GetRemoveMethod (ei);
3125 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3128 if (EventInfo is MyEventBuilder){
3129 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3130 type = eb.EventType;
3133 type = EventInfo.EventHandlerType;
3136 public string Name {
3138 return EventInfo.Name;
3142 public bool IsInstance {
3148 public bool IsStatic {
3154 public Type DeclaringType {
3156 return EventInfo.DeclaringType;
3160 public Expression InstanceExpression {
3162 return instance_expr;
3166 instance_expr = value;
3170 public override Expression DoResolve (EmitContext ec)
3172 if (instance_expr != null) {
3173 instance_expr = instance_expr.DoResolve (ec);
3174 if (instance_expr == null)
3182 public override void Emit (EmitContext ec)
3184 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3187 public void EmitAddOrRemove (EmitContext ec, Expression source)
3189 BinaryDelegate source_del = (BinaryDelegate) source;
3190 Expression handler = source_del.Right;
3192 Argument arg = new Argument (handler, Argument.AType.Expression);
3193 ArrayList args = new ArrayList ();
3197 if (source_del.IsAddition)
3198 Invocation.EmitCall (
3199 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3201 Invocation.EmitCall (
3202 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);