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 {
105 /// This interface denotes an expression which evaluates to a member
106 /// of a struct or a class.
108 public interface IMemberExpr
111 /// The name of this member.
118 /// Whether this is an instance member.
125 /// Whether this is a static member.
132 /// The type which declares this member.
139 /// The instance expression associated with this member, if it's a
140 /// non-static member.
142 Expression InstanceExpression {
148 /// Base class for expressions
150 public abstract class Expression {
151 public ExprClass eclass;
153 protected Location loc;
165 public Location Location {
172 /// Utility wrapper routine for Error, just to beautify the code
174 public void Error (int error, string s)
176 if (!Location.IsNull (loc))
177 Report.Error (error, loc, s);
179 Report.Error (error, s);
183 /// Utility wrapper routine for Warning, just to beautify the code
185 public void Warning (int warning, string s)
187 if (!Location.IsNull (loc))
188 Report.Warning (warning, loc, s);
190 Report.Warning (warning, s);
194 /// Utility wrapper routine for Warning, only prints the warning if
195 /// warnings of level `level' are enabled.
197 public void Warning (int warning, int level, string s)
199 if (level <= RootContext.WarningLevel)
200 Warning (warning, s);
204 /// Performs semantic analysis on the Expression
208 /// The Resolve method is invoked to perform the semantic analysis
211 /// The return value is an expression (it can be the
212 /// same expression in some cases) or a new
213 /// expression that better represents this node.
215 /// For example, optimizations of Unary (LiteralInt)
216 /// would return a new LiteralInt with a negated
219 /// If there is an error during semantic analysis,
220 /// then an error should be reported (using Report)
221 /// and a null value should be returned.
223 /// There are two side effects expected from calling
224 /// Resolve(): the the field variable "eclass" should
225 /// be set to any value of the enumeration
226 /// `ExprClass' and the type variable should be set
227 /// to a valid type (this is the type of the
230 public abstract Expression DoResolve (EmitContext ec);
232 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
234 return DoResolve (ec);
238 // This is used if the expression should be resolved as a type.
239 // the default implementation fails. Use this method in
240 // those participants in the SimpleName chain system.
242 public virtual Expression ResolveAsTypeStep (EmitContext ec)
248 // This is used to resolve the expression as a type, a null
249 // value will be returned if the expression is not a type
252 public Expression ResolveAsTypeTerminal (EmitContext ec)
254 Expression e = ResolveAsTypeStep (ec);
264 /// Resolves an expression and performs semantic analysis on it.
268 /// Currently Resolve wraps DoResolve to perform sanity
269 /// checking and assertion checking on what we expect from Resolve.
271 public Expression Resolve (EmitContext ec, ResolveFlags flags)
273 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
274 return ResolveAsTypeStep (ec);
276 bool old_do_flow_analysis = ec.DoFlowAnalysis;
277 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
278 ec.DoFlowAnalysis = false;
281 if (this is SimpleName)
282 e = ((SimpleName) this).DoResolveAllowStatic (ec);
286 ec.DoFlowAnalysis = old_do_flow_analysis;
291 if (e is SimpleName){
292 SimpleName s = (SimpleName) e;
294 if ((flags & ResolveFlags.SimpleName) == 0) {
295 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
296 ec.DeclSpace.Name, loc);
303 if ((e is TypeExpr) || (e is ComposedCast)) {
304 if ((flags & ResolveFlags.Type) == 0) {
305 e.Error_UnexpectedKind (flags);
314 if ((flags & ResolveFlags.VariableOrValue) == 0) {
315 e.Error_UnexpectedKind (flags);
320 case ExprClass.MethodGroup:
321 if ((flags & ResolveFlags.MethodGroup) == 0) {
322 ((MethodGroupExpr) e).ReportUsageError ();
327 case ExprClass.Value:
328 case ExprClass.Variable:
329 case ExprClass.PropertyAccess:
330 case ExprClass.EventAccess:
331 case ExprClass.IndexerAccess:
332 if ((flags & ResolveFlags.VariableOrValue) == 0) {
333 Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
334 Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
335 FieldInfo fi = ((FieldExpr) e).FieldInfo;
337 Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
338 e.Error_UnexpectedKind (flags);
344 throw new Exception ("Expression " + e.GetType () +
345 " ExprClass is Invalid after resolve");
349 throw new Exception (
350 "Expression " + e.GetType () +
351 " did not set its type after Resolve\n" +
352 "called from: " + this.GetType ());
358 /// Resolves an expression and performs semantic analysis on it.
360 public Expression Resolve (EmitContext ec)
362 return Resolve (ec, ResolveFlags.VariableOrValue);
366 /// Resolves an expression for LValue assignment
370 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
371 /// checking and assertion checking on what we expect from Resolve
373 public Expression ResolveLValue (EmitContext ec, Expression right_side)
375 Expression e = DoResolveLValue (ec, right_side);
378 if (e is SimpleName){
379 SimpleName s = (SimpleName) e;
380 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
381 ec.DeclSpace.Name, loc);
385 if (e.eclass == ExprClass.Invalid)
386 throw new Exception ("Expression " + e +
387 " ExprClass is Invalid after resolve");
389 if (e.eclass == ExprClass.MethodGroup) {
390 ((MethodGroupExpr) e).ReportUsageError ();
395 throw new Exception ("Expression " + e +
396 " did not set its type after Resolve");
403 /// Emits the code for the expression
407 /// The Emit method is invoked to generate the code
408 /// for the expression.
410 public abstract void Emit (EmitContext ec);
413 /// Protected constructor. Only derivate types should
414 /// be able to be created
417 protected Expression ()
419 eclass = ExprClass.Invalid;
424 /// Returns a literalized version of a literal FieldInfo
428 /// The possible return values are:
429 /// IntConstant, UIntConstant
430 /// LongLiteral, ULongConstant
431 /// FloatConstant, DoubleConstant
434 /// The value returned is already resolved.
436 public static Constant Constantify (object v, Type t)
438 if (t == TypeManager.int32_type)
439 return new IntConstant ((int) v);
440 else if (t == TypeManager.uint32_type)
441 return new UIntConstant ((uint) v);
442 else if (t == TypeManager.int64_type)
443 return new LongConstant ((long) v);
444 else if (t == TypeManager.uint64_type)
445 return new ULongConstant ((ulong) v);
446 else if (t == TypeManager.float_type)
447 return new FloatConstant ((float) v);
448 else if (t == TypeManager.double_type)
449 return new DoubleConstant ((double) v);
450 else if (t == TypeManager.string_type)
451 return new StringConstant ((string) v);
452 else if (t == TypeManager.short_type)
453 return new ShortConstant ((short)v);
454 else if (t == TypeManager.ushort_type)
455 return new UShortConstant ((ushort)v);
456 else if (t == TypeManager.sbyte_type)
457 return new SByteConstant (((sbyte)v));
458 else if (t == TypeManager.byte_type)
459 return new ByteConstant ((byte)v);
460 else if (t == TypeManager.char_type)
461 return new CharConstant ((char)v);
462 else if (t == TypeManager.bool_type)
463 return new BoolConstant ((bool) v);
464 else if (TypeManager.IsEnumType (t)){
465 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
467 return new EnumConstant (e, t);
469 throw new Exception ("Unknown type for constant (" + t +
474 /// Returns a fully formed expression after a MemberLookup
476 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
479 return new EventExpr ((EventInfo) mi, loc);
480 else if (mi is FieldInfo)
481 return new FieldExpr ((FieldInfo) mi, loc);
482 else if (mi is PropertyInfo)
483 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
484 else if (mi is Type){
485 return new TypeExpr ((System.Type) mi, loc);
492 // FIXME: Probably implement a cache for (t,name,current_access_set)?
494 // This code could use some optimizations, but we need to do some
495 // measurements. For example, we could use a delegate to `flag' when
496 // something can not any longer be a method-group (because it is something
500 // If the return value is an Array, then it is an array of
503 // If the return value is an MemberInfo, it is anything, but a Method
507 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
508 // the arguments here and have MemberLookup return only the methods that
509 // match the argument count/type, unlike we are doing now (we delay this
512 // This is so we can catch correctly attempts to invoke instance methods
513 // from a static body (scan for error 120 in ResolveSimpleName).
516 // FIXME: Potential optimization, have a static ArrayList
519 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
520 MemberTypes mt, BindingFlags bf, Location loc)
522 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
526 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
527 // `qualifier_type' or null to lookup members in the current class.
530 public static Expression MemberLookup (EmitContext ec, Type container_type,
531 Type qualifier_type, Type queried_type,
532 string name, MemberTypes mt,
533 BindingFlags bf, Location loc)
535 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
536 queried_type, mt, bf, name);
541 int count = mi.Length;
543 if (mi [0] is MethodBase)
544 return new MethodGroupExpr (mi, loc);
549 return ExprClassFromMemberInfo (ec, mi [0], loc);
552 public const MemberTypes AllMemberTypes =
553 MemberTypes.Constructor |
557 MemberTypes.NestedType |
558 MemberTypes.Property;
560 public const BindingFlags AllBindingFlags =
561 BindingFlags.Public |
562 BindingFlags.Static |
563 BindingFlags.Instance;
565 public static Expression MemberLookup (EmitContext ec, Type queried_type,
566 string name, Location loc)
568 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
569 AllMemberTypes, AllBindingFlags, loc);
572 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
573 Type queried_type, string name, Location loc)
575 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
576 name, AllMemberTypes, AllBindingFlags, loc);
579 public static Expression MethodLookup (EmitContext ec, Type queried_type,
580 string name, Location loc)
582 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
583 MemberTypes.Method, AllBindingFlags, loc);
587 /// This is a wrapper for MemberLookup that is not used to "probe", but
588 /// to find a final definition. If the final definition is not found, we
589 /// look for private members and display a useful debugging message if we
592 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
593 Type queried_type, string name, Location loc)
595 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
596 AllMemberTypes, AllBindingFlags, loc);
599 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
600 Type queried_type, string name,
601 MemberTypes mt, BindingFlags bf,
606 int errors = Report.Errors;
608 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
614 // Error has already been reported.
615 if (errors < Report.Errors)
618 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
622 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
623 Type queried_type, string name,
624 string class_name, Location loc)
626 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
627 AllMemberTypes, AllBindingFlags |
628 BindingFlags.NonPublic, name);
630 if (lookup == null) {
631 if (class_name != null)
632 Report.Error (103, loc, "The name `" + name + "' could not be " +
633 "found in `" + class_name + "'");
636 117, loc, "`" + queried_type + "' does not contain a " +
637 "definition for `" + name + "'");
641 if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
642 ec.ContainerType.IsSubclassOf (qualifier_type)) {
643 // Although a derived class can access protected members of
644 // its base class it cannot do so through an instance of the
645 // base class (CS1540). If the qualifier_type is a parent of the
646 // ec.ContainerType and the lookup succeeds with the latter one,
647 // then we are in this situation.
649 lookup = TypeManager.MemberLookup (
650 ec.ContainerType, ec.ContainerType, ec.ContainerType,
651 AllMemberTypes, AllBindingFlags, name);
653 if (lookup != null) {
655 1540, loc, "Cannot access protected member `" +
656 TypeManager.CSharpName (qualifier_type) + "." +
657 name + "' " + "via a qualifier of type `" +
658 TypeManager.CSharpName (qualifier_type) + "'; the " +
659 "qualifier must be of type `" +
660 TypeManager.CSharpName (ec.ContainerType) + "' " +
661 "(or derived from it)");
666 if (qualifier_type != null)
668 122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
669 name + "' is inaccessible due to its protection level");
672 122, loc, "`" + name + "' is inaccessible due to its " +
676 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
678 EventInfo ei = event_expr.EventInfo;
680 return TypeManager.GetPrivateFieldOfEvent (ei);
684 /// Returns an expression that can be used to invoke operator true
685 /// on the expression if it exists.
687 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
690 Expression operator_group;
692 operator_group = MethodLookup (ec, e.Type, "op_True", loc);
693 if (operator_group == null)
696 ArrayList arguments = new ArrayList ();
697 arguments.Add (new Argument (e, Argument.AType.Expression));
698 method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc);
703 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
707 /// Resolves the expression `e' into a boolean expression: either through
708 /// an implicit conversion, or through an `operator true' invocation
710 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
716 Expression converted = e;
717 if (e.Type != TypeManager.bool_type)
718 converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
721 // If no implicit conversion to bool exists, try using `operator true'
723 if (converted == null){
724 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
725 if (operator_true == null){
727 31, loc, "Can not convert the expression to a boolean");
737 static string ExprClassName (ExprClass c)
740 case ExprClass.Invalid:
742 case ExprClass.Value:
744 case ExprClass.Variable:
746 case ExprClass.Namespace:
750 case ExprClass.MethodGroup:
751 return "method group";
752 case ExprClass.PropertyAccess:
753 return "property access";
754 case ExprClass.EventAccess:
755 return "event access";
756 case ExprClass.IndexerAccess:
757 return "indexer access";
758 case ExprClass.Nothing:
761 throw new Exception ("Should not happen");
765 /// Reports that we were expecting `expr' to be of class `expected'
767 public void Error_UnexpectedKind (string expected)
769 string kind = "Unknown";
771 kind = ExprClassName (eclass);
773 Error (118, "Expression denotes a `" + kind +
774 "' where a `" + expected + "' was expected");
777 public void Error_UnexpectedKind (ResolveFlags flags)
779 ArrayList valid = new ArrayList (10);
781 if ((flags & ResolveFlags.VariableOrValue) != 0) {
782 valid.Add ("variable");
786 if ((flags & ResolveFlags.Type) != 0)
789 if ((flags & ResolveFlags.MethodGroup) != 0)
790 valid.Add ("method group");
792 if ((flags & ResolveFlags.SimpleName) != 0)
793 valid.Add ("simple name");
795 if (valid.Count == 0)
796 valid.Add ("unknown");
798 StringBuilder sb = new StringBuilder ();
799 for (int i = 0; i < valid.Count; i++) {
802 else if (i == valid.Count)
804 sb.Append (valid [i]);
807 string kind = ExprClassName (eclass);
809 Error (119, "Expression denotes a `" + kind + "' where " +
810 "a `" + sb.ToString () + "' was expected");
813 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
815 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
816 TypeManager.CSharpName (t));
819 public static void UnsafeError (Location loc)
821 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
825 /// Converts the IntConstant, UIntConstant, LongConstant or
826 /// ULongConstant into the integral target_type. Notice
827 /// that we do not return an `Expression' we do return
828 /// a boxed integral type.
830 /// FIXME: Since I added the new constants, we need to
831 /// also support conversions from CharConstant, ByteConstant,
832 /// SByteConstant, UShortConstant, ShortConstant
834 /// This is used by the switch statement, so the domain
835 /// of work is restricted to the literals above, and the
836 /// targets are int32, uint32, char, byte, sbyte, ushort,
837 /// short, uint64 and int64
839 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
843 if (c.Type == target_type)
844 return ((Constant) c).GetValue ();
847 // Make into one of the literals we handle, we dont really care
848 // about this value as we will just return a few limited types
850 if (c is EnumConstant)
851 c = ((EnumConstant)c).WidenToCompilerConstant ();
853 if (c is IntConstant){
854 int v = ((IntConstant) c).Value;
856 if (target_type == TypeManager.uint32_type){
859 } else if (target_type == TypeManager.char_type){
860 if (v >= Char.MinValue && v <= Char.MaxValue)
862 } else if (target_type == TypeManager.byte_type){
863 if (v >= Byte.MinValue && v <= Byte.MaxValue)
865 } else if (target_type == TypeManager.sbyte_type){
866 if (v >= SByte.MinValue && v <= SByte.MaxValue)
868 } else if (target_type == TypeManager.short_type){
869 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
871 } else if (target_type == TypeManager.ushort_type){
872 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
874 } else if (target_type == TypeManager.int64_type)
876 else if (target_type == TypeManager.uint64_type){
882 } else if (c is UIntConstant){
883 uint v = ((UIntConstant) c).Value;
885 if (target_type == TypeManager.int32_type){
886 if (v <= Int32.MaxValue)
888 } else if (target_type == TypeManager.char_type){
889 if (v >= Char.MinValue && v <= Char.MaxValue)
891 } else if (target_type == TypeManager.byte_type){
892 if (v <= Byte.MaxValue)
894 } else if (target_type == TypeManager.sbyte_type){
895 if (v <= SByte.MaxValue)
897 } else if (target_type == TypeManager.short_type){
898 if (v <= UInt16.MaxValue)
900 } else if (target_type == TypeManager.ushort_type){
901 if (v <= UInt16.MaxValue)
903 } else if (target_type == TypeManager.int64_type)
905 else if (target_type == TypeManager.uint64_type)
908 } else if (c is LongConstant){
909 long v = ((LongConstant) c).Value;
911 if (target_type == TypeManager.int32_type){
912 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
914 } else if (target_type == TypeManager.uint32_type){
915 if (v >= 0 && v <= UInt32.MaxValue)
917 } else if (target_type == TypeManager.char_type){
918 if (v >= Char.MinValue && v <= Char.MaxValue)
920 } else if (target_type == TypeManager.byte_type){
921 if (v >= Byte.MinValue && v <= Byte.MaxValue)
923 } else if (target_type == TypeManager.sbyte_type){
924 if (v >= SByte.MinValue && v <= SByte.MaxValue)
926 } else if (target_type == TypeManager.short_type){
927 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
929 } else if (target_type == TypeManager.ushort_type){
930 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
932 } else if (target_type == TypeManager.uint64_type){
937 } else if (c is ULongConstant){
938 ulong v = ((ULongConstant) c).Value;
940 if (target_type == TypeManager.int32_type){
941 if (v <= Int32.MaxValue)
943 } else if (target_type == TypeManager.uint32_type){
944 if (v <= UInt32.MaxValue)
946 } else if (target_type == TypeManager.char_type){
947 if (v >= Char.MinValue && v <= Char.MaxValue)
949 } else if (target_type == TypeManager.byte_type){
950 if (v >= Byte.MinValue && v <= Byte.MaxValue)
952 } else if (target_type == TypeManager.sbyte_type){
953 if (v <= (int) SByte.MaxValue)
955 } else if (target_type == TypeManager.short_type){
956 if (v <= UInt16.MaxValue)
958 } else if (target_type == TypeManager.ushort_type){
959 if (v <= UInt16.MaxValue)
961 } else if (target_type == TypeManager.int64_type){
962 if (v <= Int64.MaxValue)
966 } else if (c is ByteConstant){
967 byte v = ((ByteConstant) c).Value;
969 if (target_type == TypeManager.int32_type)
971 else if (target_type == TypeManager.uint32_type)
973 else if (target_type == TypeManager.char_type)
975 else if (target_type == TypeManager.sbyte_type){
976 if (v <= SByte.MaxValue)
978 } else if (target_type == TypeManager.short_type)
980 else if (target_type == TypeManager.ushort_type)
982 else if (target_type == TypeManager.int64_type)
984 else if (target_type == TypeManager.uint64_type)
987 } else if (c is SByteConstant){
988 sbyte v = ((SByteConstant) c).Value;
990 if (target_type == TypeManager.int32_type)
992 else if (target_type == TypeManager.uint32_type){
995 } else if (target_type == TypeManager.char_type){
998 } else if (target_type == TypeManager.byte_type){
1001 } else if (target_type == TypeManager.short_type)
1003 else if (target_type == TypeManager.ushort_type){
1006 } else if (target_type == TypeManager.int64_type)
1008 else if (target_type == TypeManager.uint64_type){
1013 } else if (c is ShortConstant){
1014 short v = ((ShortConstant) c).Value;
1016 if (target_type == TypeManager.int32_type){
1018 } else if (target_type == TypeManager.uint32_type){
1021 } else if (target_type == TypeManager.char_type){
1024 } else if (target_type == TypeManager.byte_type){
1025 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1027 } else if (target_type == TypeManager.sbyte_type){
1028 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1030 } else if (target_type == TypeManager.ushort_type){
1033 } else if (target_type == TypeManager.int64_type)
1035 else if (target_type == TypeManager.uint64_type)
1039 } else if (c is UShortConstant){
1040 ushort v = ((UShortConstant) c).Value;
1042 if (target_type == TypeManager.int32_type)
1044 else if (target_type == TypeManager.uint32_type)
1046 else if (target_type == TypeManager.char_type){
1047 if (v >= Char.MinValue && v <= Char.MaxValue)
1049 } else if (target_type == TypeManager.byte_type){
1050 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1052 } else if (target_type == TypeManager.sbyte_type){
1053 if (v <= SByte.MaxValue)
1055 } else if (target_type == TypeManager.short_type){
1056 if (v <= Int16.MaxValue)
1058 } else if (target_type == TypeManager.int64_type)
1060 else if (target_type == TypeManager.uint64_type)
1064 } else if (c is CharConstant){
1065 char v = ((CharConstant) c).Value;
1067 if (target_type == TypeManager.int32_type)
1069 else if (target_type == TypeManager.uint32_type)
1071 else if (target_type == TypeManager.byte_type){
1072 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1074 } else if (target_type == TypeManager.sbyte_type){
1075 if (v <= SByte.MaxValue)
1077 } else if (target_type == TypeManager.short_type){
1078 if (v <= Int16.MaxValue)
1080 } else if (target_type == TypeManager.ushort_type)
1082 else if (target_type == TypeManager.int64_type)
1084 else if (target_type == TypeManager.uint64_type)
1089 Error_ConstantValueCannotBeConverted (loc, s, target_type);
1094 // Load the object from the pointer.
1096 public static void LoadFromPtr (ILGenerator ig, Type t)
1098 if (t == TypeManager.int32_type)
1099 ig.Emit (OpCodes.Ldind_I4);
1100 else if (t == TypeManager.uint32_type)
1101 ig.Emit (OpCodes.Ldind_U4);
1102 else if (t == TypeManager.short_type)
1103 ig.Emit (OpCodes.Ldind_I2);
1104 else if (t == TypeManager.ushort_type)
1105 ig.Emit (OpCodes.Ldind_U2);
1106 else if (t == TypeManager.char_type)
1107 ig.Emit (OpCodes.Ldind_U2);
1108 else if (t == TypeManager.byte_type)
1109 ig.Emit (OpCodes.Ldind_U1);
1110 else if (t == TypeManager.sbyte_type)
1111 ig.Emit (OpCodes.Ldind_I1);
1112 else if (t == TypeManager.uint64_type)
1113 ig.Emit (OpCodes.Ldind_I8);
1114 else if (t == TypeManager.int64_type)
1115 ig.Emit (OpCodes.Ldind_I8);
1116 else if (t == TypeManager.float_type)
1117 ig.Emit (OpCodes.Ldind_R4);
1118 else if (t == TypeManager.double_type)
1119 ig.Emit (OpCodes.Ldind_R8);
1120 else if (t == TypeManager.bool_type)
1121 ig.Emit (OpCodes.Ldind_I1);
1122 else if (t == TypeManager.intptr_type)
1123 ig.Emit (OpCodes.Ldind_I);
1124 else if (TypeManager.IsEnumType (t)) {
1125 if (t == TypeManager.enum_type)
1126 ig.Emit (OpCodes.Ldind_Ref);
1128 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1129 } else if (t.IsValueType)
1130 ig.Emit (OpCodes.Ldobj, t);
1131 else if (t.IsPointer)
1132 ig.Emit (OpCodes.Ldind_I);
1134 ig.Emit (OpCodes.Ldind_Ref);
1138 // The stack contains the pointer and the value of type `type'
1140 public static void StoreFromPtr (ILGenerator ig, Type type)
1142 if (TypeManager.IsEnumType (type))
1143 type = TypeManager.EnumToUnderlying (type);
1144 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1145 ig.Emit (OpCodes.Stind_I4);
1146 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1147 ig.Emit (OpCodes.Stind_I8);
1148 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1149 type == TypeManager.ushort_type)
1150 ig.Emit (OpCodes.Stind_I2);
1151 else if (type == TypeManager.float_type)
1152 ig.Emit (OpCodes.Stind_R4);
1153 else if (type == TypeManager.double_type)
1154 ig.Emit (OpCodes.Stind_R8);
1155 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1156 type == TypeManager.bool_type)
1157 ig.Emit (OpCodes.Stind_I1);
1158 else if (type == TypeManager.intptr_type)
1159 ig.Emit (OpCodes.Stind_I);
1160 else if (type.IsValueType)
1161 ig.Emit (OpCodes.Stobj, type);
1163 ig.Emit (OpCodes.Stind_Ref);
1167 // Returns the size of type `t' if known, otherwise, 0
1169 public static int GetTypeSize (Type t)
1171 t = TypeManager.TypeToCoreType (t);
1172 if (t == TypeManager.int32_type ||
1173 t == TypeManager.uint32_type ||
1174 t == TypeManager.float_type)
1176 else if (t == TypeManager.int64_type ||
1177 t == TypeManager.uint64_type ||
1178 t == TypeManager.double_type)
1180 else if (t == TypeManager.byte_type ||
1181 t == TypeManager.sbyte_type ||
1182 t == TypeManager.bool_type)
1184 else if (t == TypeManager.short_type ||
1185 t == TypeManager.char_type ||
1186 t == TypeManager.ushort_type)
1188 else if (t == TypeManager.decimal_type)
1195 // Default implementation of IAssignMethod.CacheTemporaries
1197 public void CacheTemporaries (EmitContext ec)
1201 static void Error_NegativeArrayIndex (Location loc)
1203 Report.Error (284, loc, "Can not create array with a negative size");
1207 // Converts `source' to an int, uint, long or ulong.
1209 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1213 bool old_checked = ec.CheckState;
1214 ec.CheckState = true;
1216 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1217 if (target == null){
1218 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1219 if (target == null){
1220 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1221 if (target == null){
1222 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1224 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1228 ec.CheckState = old_checked;
1231 // Only positive constants are allowed at compile time
1233 if (target is Constant){
1234 if (target is IntConstant){
1235 if (((IntConstant) target).Value < 0){
1236 Error_NegativeArrayIndex (loc);
1241 if (target is LongConstant){
1242 if (((LongConstant) target).Value < 0){
1243 Error_NegativeArrayIndex (loc);
1256 /// This is just a base class for expressions that can
1257 /// appear on statements (invocations, object creation,
1258 /// assignments, post/pre increment and decrement). The idea
1259 /// being that they would support an extra Emition interface that
1260 /// does not leave a result on the stack.
1262 public abstract class ExpressionStatement : Expression {
1265 /// Requests the expression to be emitted in a `statement'
1266 /// context. This means that no new value is left on the
1267 /// stack after invoking this method (constrasted with
1268 /// Emit that will always leave a value on the stack).
1270 public abstract void EmitStatement (EmitContext ec);
1274 /// This kind of cast is used to encapsulate the child
1275 /// whose type is child.Type into an expression that is
1276 /// reported to return "return_type". This is used to encapsulate
1277 /// expressions which have compatible types, but need to be dealt
1278 /// at higher levels with.
1280 /// For example, a "byte" expression could be encapsulated in one
1281 /// of these as an "unsigned int". The type for the expression
1282 /// would be "unsigned int".
1285 public class EmptyCast : Expression {
1286 protected Expression child;
1288 public EmptyCast (Expression child, Type return_type)
1290 eclass = child.eclass;
1295 public override Expression DoResolve (EmitContext ec)
1297 // This should never be invoked, we are born in fully
1298 // initialized state.
1303 public override void Emit (EmitContext ec)
1310 // We need to special case this since an empty cast of
1311 // a NullLiteral is still a Constant
1313 public class NullCast : Constant {
1314 protected Expression child;
1316 public NullCast (Expression child, Type return_type)
1318 eclass = child.eclass;
1323 override public string AsString ()
1328 public override object GetValue ()
1333 public override Expression DoResolve (EmitContext ec)
1335 // This should never be invoked, we are born in fully
1336 // initialized state.
1341 public override void Emit (EmitContext ec)
1349 /// This class is used to wrap literals which belong inside Enums
1351 public class EnumConstant : Constant {
1352 public Constant Child;
1354 public EnumConstant (Constant child, Type enum_type)
1356 eclass = child.eclass;
1361 public override Expression DoResolve (EmitContext ec)
1363 // This should never be invoked, we are born in fully
1364 // initialized state.
1369 public override void Emit (EmitContext ec)
1374 public override object GetValue ()
1376 return Child.GetValue ();
1380 // Converts from one of the valid underlying types for an enumeration
1381 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1382 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1384 public Constant WidenToCompilerConstant ()
1386 Type t = TypeManager.EnumToUnderlying (Child.Type);
1387 object v = ((Constant) Child).GetValue ();;
1389 if (t == TypeManager.int32_type)
1390 return new IntConstant ((int) v);
1391 if (t == TypeManager.uint32_type)
1392 return new UIntConstant ((uint) v);
1393 if (t == TypeManager.int64_type)
1394 return new LongConstant ((long) v);
1395 if (t == TypeManager.uint64_type)
1396 return new ULongConstant ((ulong) v);
1397 if (t == TypeManager.short_type)
1398 return new ShortConstant ((short) v);
1399 if (t == TypeManager.ushort_type)
1400 return new UShortConstant ((ushort) v);
1401 if (t == TypeManager.byte_type)
1402 return new ByteConstant ((byte) v);
1403 if (t == TypeManager.sbyte_type)
1404 return new SByteConstant ((sbyte) v);
1406 throw new Exception ("Invalid enumeration underlying type: " + t);
1410 // Extracts the value in the enumeration on its native representation
1412 public object GetPlainValue ()
1414 Type t = TypeManager.EnumToUnderlying (Child.Type);
1415 object v = ((Constant) Child).GetValue ();;
1417 if (t == TypeManager.int32_type)
1419 if (t == TypeManager.uint32_type)
1421 if (t == TypeManager.int64_type)
1423 if (t == TypeManager.uint64_type)
1425 if (t == TypeManager.short_type)
1427 if (t == TypeManager.ushort_type)
1429 if (t == TypeManager.byte_type)
1431 if (t == TypeManager.sbyte_type)
1437 public override string AsString ()
1439 return Child.AsString ();
1442 public override DoubleConstant ConvertToDouble ()
1444 return Child.ConvertToDouble ();
1447 public override FloatConstant ConvertToFloat ()
1449 return Child.ConvertToFloat ();
1452 public override ULongConstant ConvertToULong ()
1454 return Child.ConvertToULong ();
1457 public override LongConstant ConvertToLong ()
1459 return Child.ConvertToLong ();
1462 public override UIntConstant ConvertToUInt ()
1464 return Child.ConvertToUInt ();
1467 public override IntConstant ConvertToInt ()
1469 return Child.ConvertToInt ();
1474 /// This kind of cast is used to encapsulate Value Types in objects.
1476 /// The effect of it is to box the value type emitted by the previous
1479 public class BoxedCast : EmptyCast {
1481 public BoxedCast (Expression expr)
1482 : base (expr, TypeManager.object_type)
1486 public BoxedCast (Expression expr, Type target_type)
1487 : base (expr, target_type)
1491 public override Expression DoResolve (EmitContext ec)
1493 // This should never be invoked, we are born in fully
1494 // initialized state.
1499 public override void Emit (EmitContext ec)
1503 ec.ig.Emit (OpCodes.Box, child.Type);
1507 public class UnboxCast : EmptyCast {
1508 public UnboxCast (Expression expr, Type return_type)
1509 : base (expr, return_type)
1513 public override Expression DoResolve (EmitContext ec)
1515 // This should never be invoked, we are born in fully
1516 // initialized state.
1521 public override void Emit (EmitContext ec)
1524 ILGenerator ig = ec.ig;
1527 ig.Emit (OpCodes.Unbox, t);
1529 LoadFromPtr (ig, t);
1534 /// This is used to perform explicit numeric conversions.
1536 /// Explicit numeric conversions might trigger exceptions in a checked
1537 /// context, so they should generate the conv.ovf opcodes instead of
1540 public class ConvCast : EmptyCast {
1541 public enum Mode : byte {
1542 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1544 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1545 U2_I1, U2_U1, U2_I2, U2_CH,
1546 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1547 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1548 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1549 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1550 CH_I1, CH_U1, CH_I2,
1551 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1552 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1558 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1559 : base (child, return_type)
1561 checked_state = ec.CheckState;
1565 public override Expression DoResolve (EmitContext ec)
1567 // This should never be invoked, we are born in fully
1568 // initialized state.
1573 public override string ToString ()
1575 return String.Format ("ConvCast ({0}, {1})", mode, child);
1578 public override void Emit (EmitContext ec)
1580 ILGenerator ig = ec.ig;
1586 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1587 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1588 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1589 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1590 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1592 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1593 case Mode.U1_CH: /* nothing */ break;
1595 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1596 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1597 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1598 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1599 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1600 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1602 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1603 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1604 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1605 case Mode.U2_CH: /* nothing */ break;
1607 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1608 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1609 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1610 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1611 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1612 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1613 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1615 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1616 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1617 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1618 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1619 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1620 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1622 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1623 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1624 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1625 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1626 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1627 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1628 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1629 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1631 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1632 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1633 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1634 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1635 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1636 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1637 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1638 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1640 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1641 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1642 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1644 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1645 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1646 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1647 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1648 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1649 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1650 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1651 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1652 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1654 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1655 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1656 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1657 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1658 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1659 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1660 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1661 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1662 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1663 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1667 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1668 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1669 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1670 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1671 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1673 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1674 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1676 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1677 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1678 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1679 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1680 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1681 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1683 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1684 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1685 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1686 case Mode.U2_CH: /* nothing */ break;
1688 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1689 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1690 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1691 case Mode.I4_U4: /* nothing */ break;
1692 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1693 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1694 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1696 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1697 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1698 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1699 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1700 case Mode.U4_I4: /* nothing */ break;
1701 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1703 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1704 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1705 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1706 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1707 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1708 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1709 case Mode.I8_U8: /* nothing */ break;
1710 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1712 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1713 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1714 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1715 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1716 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1717 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1718 case Mode.U8_I8: /* nothing */ break;
1719 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1721 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1722 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1723 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1725 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1726 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1727 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1728 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1729 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1730 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1731 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1732 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1733 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1735 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1736 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1737 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1738 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1739 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1740 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1741 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1742 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1743 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1744 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1750 public class OpcodeCast : EmptyCast {
1754 public OpcodeCast (Expression child, Type return_type, OpCode op)
1755 : base (child, return_type)
1759 second_valid = false;
1762 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1763 : base (child, return_type)
1768 second_valid = true;
1771 public override Expression DoResolve (EmitContext ec)
1773 // This should never be invoked, we are born in fully
1774 // initialized state.
1779 public override void Emit (EmitContext ec)
1790 /// This kind of cast is used to encapsulate a child and cast it
1791 /// to the class requested
1793 public class ClassCast : EmptyCast {
1794 public ClassCast (Expression child, Type return_type)
1795 : base (child, return_type)
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)
1812 ec.ig.Emit (OpCodes.Castclass, type);
1818 /// SimpleName expressions are initially formed of a single
1819 /// word and it only happens at the beginning of the expression.
1823 /// The expression will try to be bound to a Field, a Method
1824 /// group or a Property. If those fail we pass the name to our
1825 /// caller and the SimpleName is compounded to perform a type
1826 /// lookup. The idea behind this process is that we want to avoid
1827 /// creating a namespace map from the assemblies, as that requires
1828 /// the GetExportedTypes function to be called and a hashtable to
1829 /// be constructed which reduces startup time. If later we find
1830 /// that this is slower, we should create a `NamespaceExpr' expression
1831 /// that fully participates in the resolution process.
1833 /// For example `System.Console.WriteLine' is decomposed into
1834 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
1836 /// The first SimpleName wont produce a match on its own, so it will
1838 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
1840 /// System.Console will produce a TypeExpr match.
1842 /// The downside of this is that we might be hitting `LookupType' too many
1843 /// times with this scheme.
1845 public class SimpleName : Expression {
1849 // If true, then we are a simple name, not composed with a ".
1853 public SimpleName (string a, string b, Location l)
1855 Name = String.Concat (a, ".", b);
1860 public SimpleName (string name, Location l)
1867 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
1869 if (ec.IsFieldInitializer)
1872 "A field initializer cannot reference the non-static field, " +
1873 "method or property `"+name+"'");
1877 "An object reference is required " +
1878 "for the non-static field `"+name+"'");
1882 // Checks whether we are trying to access an instance
1883 // property, method or field from a static body.
1885 Expression MemberStaticCheck (EmitContext ec, Expression e)
1887 if (e is IMemberExpr){
1888 IMemberExpr member = (IMemberExpr) e;
1890 if (!member.IsStatic){
1891 Error_ObjectRefRequired (ec, loc, Name);
1899 public override Expression DoResolve (EmitContext ec)
1901 return SimpleNameResolve (ec, null, false);
1904 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
1906 return SimpleNameResolve (ec, right_side, false);
1910 public Expression DoResolveAllowStatic (EmitContext ec)
1912 return SimpleNameResolve (ec, null, true);
1915 public override Expression ResolveAsTypeStep (EmitContext ec)
1917 DeclSpace ds = ec.DeclSpace;
1918 Namespace ns = ds.Namespace;
1923 // Since we are cheating: we only do the Alias lookup for
1924 // namespaces if the name does not include any dots in it
1926 if (ns != null && is_base)
1927 alias_value = ns.LookupAlias (Name);
1931 if (ec.ResolvingTypeTree){
1932 if (alias_value != null){
1933 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1934 return new TypeExpr (t, loc);
1937 int errors = Report.Errors;
1938 Type dt = ec.DeclSpace.FindType (loc, Name);
1940 if (Report.Errors != errors)
1944 return new TypeExpr (dt, loc);
1948 // First, the using aliases
1950 if (alias_value != null){
1951 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
1952 return new TypeExpr (t, loc);
1954 // we have alias value, but it isn't Type, so try if it's namespace
1955 return new SimpleName (alias_value, loc);
1959 // Stage 2: Lookup up if we are an alias to a type
1963 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
1964 return new TypeExpr (t, loc);
1966 // No match, maybe our parent can compose us
1967 // into something meaningful.
1972 /// 7.5.2: Simple Names.
1974 /// Local Variables and Parameters are handled at
1975 /// parse time, so they never occur as SimpleNames.
1977 /// The `allow_static' flag is used by MemberAccess only
1978 /// and it is used to inform us that it is ok for us to
1979 /// avoid the static check, because MemberAccess might end
1980 /// up resolving the Name as a Type name and the access as
1981 /// a static type access.
1983 /// ie: Type Type; .... { Type.GetType (""); }
1985 /// Type is both an instance variable and a Type; Type.GetType
1986 /// is the static method not an instance method of type.
1988 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
1990 Expression e = null;
1993 // Since we are cheating (is_base is our hint
1994 // that we are the beginning of the name): we
1995 // only do the Alias lookup for namespaces if
1996 // the name does not include any dots in it
1998 Namespace ns = ec.DeclSpace.Namespace;
1999 if (is_base && ns != null){
2000 string alias_value = ns.LookupAlias (Name);
2001 if (alias_value != null){
2005 if ((t = TypeManager.LookupType (Name)) != null)
2006 return new TypeExpr (t, loc);
2008 // No match, maybe our parent can compose us
2009 // into something meaningful.
2016 // Stage 1: Performed by the parser (binding to locals or parameters).
2018 Block current_block = ec.CurrentBlock;
2019 if (current_block != null){
2020 LocalInfo vi = current_block.GetLocalInfo (Name);
2024 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2026 if (right_side != null)
2027 return var.ResolveLValue (ec, right_side);
2029 return var.Resolve (ec);
2033 Parameter par = null;
2034 Parameters pars = current_block.Parameters;
2036 par = pars.GetParameterByName (Name, out idx);
2039 ParameterReference param;
2041 param = new ParameterReference (pars, current_block, idx, Name, loc);
2043 if (right_side != null)
2044 return param.ResolveLValue (ec, right_side);
2046 return param.Resolve (ec);
2051 // Stage 2: Lookup members
2054 DeclSpace lookup_ds = ec.DeclSpace;
2056 if (lookup_ds.TypeBuilder == null)
2059 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2063 lookup_ds =lookup_ds.Parent;
2064 } while (lookup_ds != null);
2066 if (e == null && ec.ContainerType != null)
2067 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2070 return ResolveAsTypeStep (ec);
2075 if (e is IMemberExpr) {
2076 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2080 IMemberExpr me = e as IMemberExpr;
2084 // This fails if ResolveMemberAccess() was unable to decide whether
2085 // it's a field or a type of the same name.
2086 if (!me.IsStatic && (me.InstanceExpression == null))
2090 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType) &&
2091 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType)) {
2092 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2093 "outer type `" + me.DeclaringType + "' via nested type `" +
2094 me.InstanceExpression.Type + "'");
2098 if (right_side != null)
2099 e = e.DoResolveLValue (ec, right_side);
2101 e = e.DoResolve (ec);
2106 if (ec.IsStatic || ec.IsFieldInitializer){
2110 return MemberStaticCheck (ec, e);
2115 public override void Emit (EmitContext ec)
2118 // If this is ever reached, then we failed to
2119 // find the name as a namespace
2122 Error (103, "The name `" + Name +
2123 "' does not exist in the class `" +
2124 ec.DeclSpace.Name + "'");
2127 public override string ToString ()
2134 /// Fully resolved expression that evaluates to a type
2136 public class TypeExpr : Expression {
2137 public TypeExpr (Type t, Location l)
2140 eclass = ExprClass.Type;
2144 public override Expression ResolveAsTypeStep (EmitContext ec)
2149 override public Expression DoResolve (EmitContext ec)
2154 override public void Emit (EmitContext ec)
2156 throw new Exception ("Should never be called");
2159 public override string ToString ()
2161 return Type.ToString ();
2166 /// Used to create types from a fully qualified name. These are just used
2167 /// by the parser to setup the core types. A TypeLookupExpression is always
2168 /// classified as a type.
2170 public class TypeLookupExpression : TypeExpr {
2173 public TypeLookupExpression (string name) : base (null, Location.Null)
2178 public override Expression ResolveAsTypeStep (EmitContext ec)
2181 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
2185 public override Expression DoResolve (EmitContext ec)
2187 return ResolveAsTypeStep (ec);
2190 public override void Emit (EmitContext ec)
2192 throw new Exception ("Should never be called");
2195 public override string ToString ()
2202 /// MethodGroup Expression.
2204 /// This is a fully resolved expression that evaluates to a type
2206 public class MethodGroupExpr : Expression, IMemberExpr {
2207 public MethodBase [] Methods;
2208 Expression instance_expression = null;
2209 bool is_explicit_impl = false;
2211 public MethodGroupExpr (MemberInfo [] mi, Location l)
2213 Methods = new MethodBase [mi.Length];
2214 mi.CopyTo (Methods, 0);
2215 eclass = ExprClass.MethodGroup;
2216 type = TypeManager.object_type;
2220 public MethodGroupExpr (ArrayList list, Location l)
2222 Methods = new MethodBase [list.Count];
2225 list.CopyTo (Methods, 0);
2227 foreach (MemberInfo m in list){
2228 if (!(m is MethodBase)){
2229 Console.WriteLine ("Name " + m.Name);
2230 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2236 eclass = ExprClass.MethodGroup;
2237 type = TypeManager.object_type;
2240 public Type DeclaringType {
2242 return Methods [0].DeclaringType;
2247 // `A method group may have associated an instance expression'
2249 public Expression InstanceExpression {
2251 return instance_expression;
2255 instance_expression = value;
2259 public bool IsExplicitImpl {
2261 return is_explicit_impl;
2265 is_explicit_impl = value;
2269 public string Name {
2271 return Methods [0].Name;
2275 public bool IsInstance {
2277 foreach (MethodBase mb in Methods)
2285 public bool IsStatic {
2287 foreach (MethodBase mb in Methods)
2295 override public Expression DoResolve (EmitContext ec)
2298 instance_expression = null;
2300 if (instance_expression != null) {
2301 instance_expression = instance_expression.DoResolve (ec);
2302 if (instance_expression == null)
2309 public void ReportUsageError ()
2311 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
2312 Methods [0].Name + "()' is referenced without parentheses");
2315 override public void Emit (EmitContext ec)
2317 ReportUsageError ();
2320 bool RemoveMethods (bool keep_static)
2322 ArrayList smethods = new ArrayList ();
2324 foreach (MethodBase mb in Methods){
2325 if (mb.IsStatic == keep_static)
2329 if (smethods.Count == 0)
2332 Methods = new MethodBase [smethods.Count];
2333 smethods.CopyTo (Methods, 0);
2339 /// Removes any instance methods from the MethodGroup, returns
2340 /// false if the resulting set is empty.
2342 public bool RemoveInstanceMethods ()
2344 return RemoveMethods (true);
2348 /// Removes any static methods from the MethodGroup, returns
2349 /// false if the resulting set is empty.
2351 public bool RemoveStaticMethods ()
2353 return RemoveMethods (false);
2358 /// Fully resolved expression that evaluates to a Field
2360 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2361 public readonly FieldInfo FieldInfo;
2362 Expression instance_expr;
2363 VariableInfo variable_info;
2365 public FieldExpr (FieldInfo fi, Location l)
2368 eclass = ExprClass.Variable;
2369 type = fi.FieldType;
2373 public string Name {
2375 return FieldInfo.Name;
2379 public bool IsInstance {
2381 return !FieldInfo.IsStatic;
2385 public bool IsStatic {
2387 return FieldInfo.IsStatic;
2391 public Type DeclaringType {
2393 return FieldInfo.DeclaringType;
2397 public Expression InstanceExpression {
2399 return instance_expr;
2403 instance_expr = value;
2407 public VariableInfo VariableInfo {
2409 return variable_info;
2413 override public Expression DoResolve (EmitContext ec)
2415 if (!FieldInfo.IsStatic){
2416 if (instance_expr == null){
2418 // This can happen when referencing an instance field using
2419 // a fully qualified type expression: TypeName.InstanceField = xxx
2421 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2425 // Resolve the field's instance expression while flow analysis is turned
2426 // off: when accessing a field "a.b", we must check whether the field
2427 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2428 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2429 ResolveFlags.DisableFlowAnalysis);
2430 if (instance_expr == null)
2434 // If the instance expression is a local variable or parameter.
2435 IVariable var = instance_expr as IVariable;
2436 if ((var == null) || (var.VariableInfo == null))
2439 VariableInfo vi = var.VariableInfo;
2440 if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
2443 variable_info = vi.GetSubStruct (FieldInfo.Name);
2447 void Report_AssignToReadonly (bool is_instance)
2452 msg = "Readonly field can not be assigned outside " +
2453 "of constructor or variable initializer";
2455 msg = "A static readonly field can only be assigned in " +
2456 "a static constructor";
2458 Report.Error (is_instance ? 191 : 198, loc, msg);
2461 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2463 IVariable var = instance_expr as IVariable;
2464 if ((var != null) && (var.VariableInfo != null))
2465 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
2467 Expression e = DoResolve (ec);
2472 if (!FieldInfo.IsInitOnly)
2476 // InitOnly fields can only be assigned in constructors
2479 if (ec.IsConstructor){
2480 if (ec.ContainerType == FieldInfo.DeclaringType)
2484 Report_AssignToReadonly (true);
2489 override public void Emit (EmitContext ec)
2491 ILGenerator ig = ec.ig;
2492 bool is_volatile = false;
2494 if (FieldInfo is FieldBuilder){
2495 FieldBase f = TypeManager.GetField (FieldInfo);
2497 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2500 f.status |= Field.Status.USED;
2503 if (FieldInfo.IsStatic){
2505 ig.Emit (OpCodes.Volatile);
2507 ig.Emit (OpCodes.Ldsfld, FieldInfo);
2511 if (instance_expr.Type.IsValueType){
2513 LocalTemporary tempo = null;
2515 if (!(instance_expr is IMemoryLocation)){
2516 tempo = new LocalTemporary (ec, instance_expr.Type);
2518 if (ec.RemapToProxy)
2521 InstanceExpression.Emit (ec);
2525 ml = (IMemoryLocation) instance_expr;
2527 ml.AddressOf (ec, AddressOp.Load);
2529 if (ec.RemapToProxy)
2532 instance_expr.Emit (ec);
2535 ig.Emit (OpCodes.Volatile);
2537 ig.Emit (OpCodes.Ldfld, FieldInfo);
2540 public void EmitAssign (EmitContext ec, Expression source)
2542 FieldAttributes fa = FieldInfo.Attributes;
2543 bool is_static = (fa & FieldAttributes.Static) != 0;
2544 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
2545 ILGenerator ig = ec.ig;
2547 if (is_readonly && !ec.IsConstructor){
2548 Report_AssignToReadonly (!is_static);
2553 Expression instance = instance_expr;
2555 if (instance.Type.IsValueType){
2556 if (instance is IMemoryLocation){
2557 IMemoryLocation ml = (IMemoryLocation) instance;
2559 ml.AddressOf (ec, AddressOp.Store);
2561 throw new Exception ("The " + instance + " of type " +
2563 " represents a ValueType and does " +
2564 "not implement IMemoryLocation");
2566 if (ec.RemapToProxy)
2575 if (FieldInfo is FieldBuilder){
2576 FieldBase f = TypeManager.GetField (FieldInfo);
2578 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
2579 ig.Emit (OpCodes.Volatile);
2581 f.status |= Field.Status.ASSIGNED;
2585 ig.Emit (OpCodes.Stsfld, FieldInfo);
2587 ig.Emit (OpCodes.Stfld, FieldInfo);
2590 public void AddressOf (EmitContext ec, AddressOp mode)
2592 ILGenerator ig = ec.ig;
2594 if (FieldInfo is FieldBuilder){
2595 FieldBase f = TypeManager.GetField (FieldInfo);
2596 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
2597 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
2601 if ((mode & AddressOp.Store) != 0)
2602 f.status |= Field.Status.ASSIGNED;
2603 if ((mode & AddressOp.Load) != 0)
2604 f.status |= Field.Status.USED;
2608 // Handle initonly fields specially: make a copy and then
2609 // get the address of the copy.
2611 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
2615 local = ig.DeclareLocal (type);
2616 ig.Emit (OpCodes.Stloc, local);
2617 ig.Emit (OpCodes.Ldloca, local);
2621 if (FieldInfo.IsStatic)
2622 ig.Emit (OpCodes.Ldsflda, FieldInfo);
2625 // In the case of `This', we call the AddressOf method, which will
2626 // only load the pointer, and not perform an Ldobj immediately after
2627 // the value has been loaded into the stack.
2629 if (instance_expr is This)
2630 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
2631 else if (instance_expr.Type.IsValueType && instance_expr is IMemoryLocation){
2632 IMemoryLocation ml = (IMemoryLocation) instance_expr;
2634 ml.AddressOf (ec, AddressOp.LoadStore);
2636 instance_expr.Emit (ec);
2637 ig.Emit (OpCodes.Ldflda, FieldInfo);
2643 /// Expression that evaluates to a Property. The Assign class
2644 /// might set the `Value' expression if we are in an assignment.
2646 /// This is not an LValue because we need to re-write the expression, we
2647 /// can not take data from the stack and store it.
2649 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
2650 public readonly PropertyInfo PropertyInfo;
2653 // This is set externally by the `BaseAccess' class
2656 MethodInfo getter, setter;
2658 bool must_do_cs1540_check;
2660 Expression instance_expr;
2662 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
2665 eclass = ExprClass.PropertyAccess;
2669 type = TypeManager.TypeToCoreType (pi.PropertyType);
2671 ResolveAccessors (ec);
2674 public string Name {
2676 return PropertyInfo.Name;
2680 public bool IsInstance {
2686 public bool IsStatic {
2692 public Type DeclaringType {
2694 return PropertyInfo.DeclaringType;
2699 // The instance expression associated with this expression
2701 public Expression InstanceExpression {
2703 instance_expr = value;
2707 return instance_expr;
2711 public bool VerifyAssignable ()
2713 if (setter == null) {
2714 Report.Error (200, loc,
2715 "The property `" + PropertyInfo.Name +
2716 "' can not be assigned to, as it has not set accessor");
2723 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
2725 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
2726 BindingFlags.Static | BindingFlags.Instance;
2729 group = TypeManager.MemberLookup (
2730 invocation_type, invocation_type, PropertyInfo.DeclaringType,
2731 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
2734 // The first method is the closest to us
2739 foreach (MethodInfo mi in group) {
2740 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
2743 // If only accessible to the current class or children
2745 if (ma == MethodAttributes.Private) {
2746 Type declaring_type = mi.DeclaringType;
2748 if (invocation_type != declaring_type){
2749 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2757 // FamAndAssem requires that we not only derivate, but we are on the
2760 if (ma == MethodAttributes.FamANDAssem){
2761 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2767 // Assembly and FamORAssem succeed if we're in the same assembly.
2768 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2769 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
2775 // We already know that we aren't in the same assembly.
2776 if (ma == MethodAttributes.Assembly)
2779 // Family and FamANDAssem require that we derive.
2780 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2781 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
2784 must_do_cs1540_check = true;
2797 // We also perform the permission checking here, as the PropertyInfo does not
2798 // hold the information for the accessibility of its setter/getter
2800 void ResolveAccessors (EmitContext ec)
2802 getter = GetAccessor (ec.ContainerType, "get");
2803 if ((getter != null) && getter.IsStatic)
2806 setter = GetAccessor (ec.ContainerType, "set");
2807 if ((setter != null) && setter.IsStatic)
2810 if (setter == null && getter == null){
2811 Error (122, "`" + PropertyInfo.Name + "' " +
2812 "is inaccessible because of its protection level");
2817 bool InstanceResolve (EmitContext ec)
2819 if ((instance_expr == null) && ec.IsStatic && !is_static) {
2820 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
2824 if (instance_expr != null) {
2825 instance_expr = instance_expr.DoResolve (ec);
2826 if (instance_expr == null)
2830 if (must_do_cs1540_check && (instance_expr != null)) {
2831 if ((instance_expr.Type != ec.ContainerType) &&
2832 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
2833 Report.Error (1540, loc, "Cannot access protected member `" +
2834 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
2835 "' via a qualifier of type `" +
2836 TypeManager.CSharpName (instance_expr.Type) +
2837 "'; the qualifier must be of type `" +
2838 TypeManager.CSharpName (ec.ContainerType) +
2839 "' (or derived from it)");
2847 override public Expression DoResolve (EmitContext ec)
2849 if (getter == null){
2851 // The following condition happens if the PropertyExpr was
2852 // created, but is invalid (ie, the property is inaccessible),
2853 // and we did not want to embed the knowledge about this in
2854 // the caller routine. This only avoids double error reporting.
2859 Report.Error (154, loc,
2860 "The property `" + PropertyInfo.Name +
2861 "' can not be used in " +
2862 "this context because it lacks a get accessor");
2866 if (!InstanceResolve (ec))
2870 // Only base will allow this invocation to happen.
2872 if (IsBase && getter.IsAbstract){
2873 Report.Error (205, loc, "Cannot call an abstract base property: " +
2874 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2881 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
2883 if (setter == null){
2885 // The following condition happens if the PropertyExpr was
2886 // created, but is invalid (ie, the property is inaccessible),
2887 // and we did not want to embed the knowledge about this in
2888 // the caller routine. This only avoids double error reporting.
2893 Report.Error (154, loc,
2894 "The property `" + PropertyInfo.Name +
2895 "' can not be used in " +
2896 "this context because it lacks a set accessor");
2900 if (!InstanceResolve (ec))
2904 // Only base will allow this invocation to happen.
2906 if (IsBase && setter.IsAbstract){
2907 Report.Error (205, loc, "Cannot call an abstract base property: " +
2908 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
2914 override public void Emit (EmitContext ec)
2917 // Special case: length of single dimension array property is turned into ldlen
2919 if ((getter == TypeManager.system_int_array_get_length) ||
2920 (getter == TypeManager.int_array_get_length)){
2921 Type iet = instance_expr.Type;
2924 // System.Array.Length can be called, but the Type does not
2925 // support invoking GetArrayRank, so test for that case first
2927 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
2928 instance_expr.Emit (ec);
2929 ec.ig.Emit (OpCodes.Ldlen);
2934 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
2939 // Implements the IAssignMethod interface for assignments
2941 public void EmitAssign (EmitContext ec, Expression source)
2943 Argument arg = new Argument (source, Argument.AType.Expression);
2944 ArrayList args = new ArrayList ();
2947 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
2950 override public void EmitStatement (EmitContext ec)
2953 ec.ig.Emit (OpCodes.Pop);
2958 /// Fully resolved expression that evaluates to an Event
2960 public class EventExpr : Expression, IMemberExpr {
2961 public readonly EventInfo EventInfo;
2962 public Expression instance_expr;
2965 MethodInfo add_accessor, remove_accessor;
2967 public EventExpr (EventInfo ei, Location loc)
2971 eclass = ExprClass.EventAccess;
2973 add_accessor = TypeManager.GetAddMethod (ei);
2974 remove_accessor = TypeManager.GetRemoveMethod (ei);
2976 if (add_accessor.IsStatic || remove_accessor.IsStatic)
2979 if (EventInfo is MyEventBuilder){
2980 MyEventBuilder eb = (MyEventBuilder) EventInfo;
2981 type = eb.EventType;
2984 type = EventInfo.EventHandlerType;
2987 public string Name {
2989 return EventInfo.Name;
2993 public bool IsInstance {
2999 public bool IsStatic {
3005 public Type DeclaringType {
3007 return EventInfo.DeclaringType;
3011 public Expression InstanceExpression {
3013 return instance_expr;
3017 instance_expr = value;
3021 public override Expression DoResolve (EmitContext ec)
3023 if (instance_expr != null) {
3024 instance_expr = instance_expr.DoResolve (ec);
3025 if (instance_expr == null)
3033 public override void Emit (EmitContext ec)
3035 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
3038 public void EmitAddOrRemove (EmitContext ec, Expression source)
3040 Expression handler = ((Binary) source).Right;
3042 Argument arg = new Argument (handler, Argument.AType.Expression);
3043 ArrayList args = new ArrayList ();
3047 if (((Binary) source).Oper == Binary.Operator.Addition)
3048 Invocation.EmitCall (
3049 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3051 Invocation.EmitCall (
3052 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);