2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 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 {
100 /// Checks whether the variable has already been assigned at
101 /// the current position of the method's control flow and
102 /// reports an appropriate error message if not.
104 /// If the variable is a struct, then this call checks whether
105 /// all of its fields (including all private ones) have been
108 bool IsAssigned (EmitContext ec, Location loc);
111 /// Checks whether field `name' in this struct has been assigned.
113 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
116 /// Tells the flow analysis code that the variable has already
117 /// been assigned at the current code position.
119 /// If the variable is a struct, this call marks all its fields
120 /// (including private fields) as being assigned.
122 void SetAssigned (EmitContext ec);
125 /// Tells the flow analysis code that field `name' in this struct
126 /// has already been assigned atthe current code position.
128 void SetFieldAssigned (EmitContext ec, string name);
132 /// This interface denotes an expression which evaluates to a member
133 /// of a struct or a class.
135 public interface IMemberExpr
138 /// The name of this member.
145 /// Whether this is an instance member.
152 /// Whether this is a static member.
159 /// The type which declares this member.
166 /// The instance expression associated with this member, if it's a
167 /// non-static member.
169 Expression InstanceExpression {
175 /// Expression which resolves to a type.
177 public interface ITypeExpression
180 /// Resolve the expression, but only lookup types.
182 Expression DoResolveType (EmitContext ec);
186 /// Base class for expressions
188 public abstract class Expression {
189 public ExprClass eclass;
191 protected Location loc;
203 public Location Location {
210 /// Utility wrapper routine for Error, just to beautify the code
212 public void Error (int error, string s)
214 if (!Location.IsNull (loc))
215 Report.Error (error, loc, s);
217 Report.Error (error, s);
221 /// Utility wrapper routine for Warning, just to beautify the code
223 public void Warning (int warning, string s)
225 if (!Location.IsNull (loc))
226 Report.Warning (warning, loc, s);
228 Report.Warning (warning, s);
232 /// Utility wrapper routine for Warning, only prints the warning if
233 /// warnings of level `level' are enabled.
235 public void Warning (int warning, int level, string s)
237 if (level <= RootContext.WarningLevel)
238 Warning (warning, s);
241 static public void Error_CannotConvertType (Location loc, Type source, Type target)
243 Report.Error (30, loc, "Cannot convert type '" +
244 TypeManager.CSharpName (source) + "' to '" +
245 TypeManager.CSharpName (target) + "'");
249 /// Performs semantic analysis on the Expression
253 /// The Resolve method is invoked to perform the semantic analysis
256 /// The return value is an expression (it can be the
257 /// same expression in some cases) or a new
258 /// expression that better represents this node.
260 /// For example, optimizations of Unary (LiteralInt)
261 /// would return a new LiteralInt with a negated
264 /// If there is an error during semantic analysis,
265 /// then an error should be reported (using Report)
266 /// and a null value should be returned.
268 /// There are two side effects expected from calling
269 /// Resolve(): the the field variable "eclass" should
270 /// be set to any value of the enumeration
271 /// `ExprClass' and the type variable should be set
272 /// to a valid type (this is the type of the
275 public abstract Expression DoResolve (EmitContext ec);
277 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
279 return DoResolve (ec);
283 /// Resolves an expression and performs semantic analysis on it.
287 /// Currently Resolve wraps DoResolve to perform sanity
288 /// checking and assertion checking on what we expect from Resolve.
290 public Expression Resolve (EmitContext ec, ResolveFlags flags)
292 // Are we doing a types-only search ?
293 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294 ITypeExpression type_expr = this as ITypeExpression;
296 if (type_expr == null)
299 return type_expr.DoResolveType (ec);
302 bool old_do_flow_analysis = ec.DoFlowAnalysis;
303 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304 ec.DoFlowAnalysis = false;
307 if (this is SimpleName)
308 e = ((SimpleName) this).DoResolveAllowStatic (ec);
312 ec.DoFlowAnalysis = old_do_flow_analysis;
317 if (e is SimpleName){
318 SimpleName s = (SimpleName) e;
320 if ((flags & ResolveFlags.SimpleName) == 0) {
321 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
322 ec.DeclSpace.Name, loc);
329 if ((e is TypeExpr) || (e is ComposedCast)) {
330 if ((flags & ResolveFlags.Type) == 0) {
340 if ((flags & ResolveFlags.VariableOrValue) == 0) {
346 case ExprClass.MethodGroup:
347 if ((flags & ResolveFlags.MethodGroup) == 0) {
348 ((MethodGroupExpr) e).ReportUsageError ();
353 case ExprClass.Value:
354 case ExprClass.Variable:
355 case ExprClass.PropertyAccess:
356 case ExprClass.EventAccess:
357 case ExprClass.IndexerAccess:
358 if ((flags & ResolveFlags.VariableOrValue) == 0) {
365 throw new Exception ("Expression " + e.GetType () +
366 " ExprClass is Invalid after resolve");
370 throw new Exception (
371 "Expression " + e.GetType () +
372 " did not set its type after Resolve\n" +
373 "called from: " + this.GetType ());
379 /// Resolves an expression and performs semantic analysis on it.
381 public Expression Resolve (EmitContext ec)
383 return Resolve (ec, ResolveFlags.VariableOrValue);
387 /// Resolves an expression for LValue assignment
391 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
392 /// checking and assertion checking on what we expect from Resolve
394 public Expression ResolveLValue (EmitContext ec, Expression right_side)
396 Expression e = DoResolveLValue (ec, right_side);
399 if (e is SimpleName){
400 SimpleName s = (SimpleName) e;
401 MemberLookupFailed (ec, null, ec.ContainerType, s.Name,
402 ec.DeclSpace.Name, loc);
406 if (e.eclass == ExprClass.Invalid)
407 throw new Exception ("Expression " + e +
408 " ExprClass is Invalid after resolve");
410 if (e.eclass == ExprClass.MethodGroup) {
411 ((MethodGroupExpr) e).ReportUsageError ();
416 throw new Exception ("Expression " + e +
417 " did not set its type after Resolve");
424 /// Emits the code for the expression
428 /// The Emit method is invoked to generate the code
429 /// for the expression.
431 public abstract void Emit (EmitContext ec);
434 /// Protected constructor. Only derivate types should
435 /// be able to be created
438 protected Expression ()
440 eclass = ExprClass.Invalid;
445 /// Returns a literalized version of a literal FieldInfo
449 /// The possible return values are:
450 /// IntConstant, UIntConstant
451 /// LongLiteral, ULongConstant
452 /// FloatConstant, DoubleConstant
455 /// The value returned is already resolved.
457 public static Constant Constantify (object v, Type t)
459 if (t == TypeManager.int32_type)
460 return new IntConstant ((int) v);
461 else if (t == TypeManager.uint32_type)
462 return new UIntConstant ((uint) v);
463 else if (t == TypeManager.int64_type)
464 return new LongConstant ((long) v);
465 else if (t == TypeManager.uint64_type)
466 return new ULongConstant ((ulong) v);
467 else if (t == TypeManager.float_type)
468 return new FloatConstant ((float) v);
469 else if (t == TypeManager.double_type)
470 return new DoubleConstant ((double) v);
471 else if (t == TypeManager.string_type)
472 return new StringConstant ((string) v);
473 else if (t == TypeManager.short_type)
474 return new ShortConstant ((short)v);
475 else if (t == TypeManager.ushort_type)
476 return new UShortConstant ((ushort)v);
477 else if (t == TypeManager.sbyte_type)
478 return new SByteConstant (((sbyte)v));
479 else if (t == TypeManager.byte_type)
480 return new ByteConstant ((byte)v);
481 else if (t == TypeManager.char_type)
482 return new CharConstant ((char)v);
483 else if (t == TypeManager.bool_type)
484 return new BoolConstant ((bool) v);
485 else if (TypeManager.IsEnumType (t)){
486 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
488 return new EnumConstant (e, t);
490 throw new Exception ("Unknown type for constant (" + t +
495 /// Returns a fully formed expression after a MemberLookup
497 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
500 return new EventExpr ((EventInfo) mi, loc);
501 else if (mi is FieldInfo)
502 return new FieldExpr ((FieldInfo) mi, loc);
503 else if (mi is PropertyInfo)
504 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
505 else if (mi is Type){
506 return new TypeExpr ((System.Type) mi, loc);
513 // FIXME: Probably implement a cache for (t,name,current_access_set)?
515 // This code could use some optimizations, but we need to do some
516 // measurements. For example, we could use a delegate to `flag' when
517 // something can not any longer be a method-group (because it is something
521 // If the return value is an Array, then it is an array of
524 // If the return value is an MemberInfo, it is anything, but a Method
528 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
529 // the arguments here and have MemberLookup return only the methods that
530 // match the argument count/type, unlike we are doing now (we delay this
533 // This is so we can catch correctly attempts to invoke instance methods
534 // from a static body (scan for error 120 in ResolveSimpleName).
537 // FIXME: Potential optimization, have a static ArrayList
540 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
541 MemberTypes mt, BindingFlags bf, Location loc)
543 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
547 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
548 // `qualifier_type' or null to lookup members in the current class.
551 public static Expression MemberLookup (EmitContext ec, Type container_type,
552 Type qualifier_type, Type queried_type,
553 string name, MemberTypes mt,
554 BindingFlags bf, Location loc)
556 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
557 queried_type, mt, bf, name);
562 int count = mi.Length;
565 return new MethodGroupExpr (mi, loc);
567 if (mi [0] is MethodBase)
568 return new MethodGroupExpr (mi, loc);
570 return ExprClassFromMemberInfo (ec, mi [0], loc);
573 public const MemberTypes AllMemberTypes =
574 MemberTypes.Constructor |
578 MemberTypes.NestedType |
579 MemberTypes.Property;
581 public const BindingFlags AllBindingFlags =
582 BindingFlags.Public |
583 BindingFlags.Static |
584 BindingFlags.Instance;
586 public static Expression MemberLookup (EmitContext ec, Type queried_type,
587 string name, Location loc)
589 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
590 AllMemberTypes, AllBindingFlags, loc);
593 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
594 Type queried_type, string name, Location loc)
596 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
597 name, AllMemberTypes, AllBindingFlags, loc);
600 public static Expression MethodLookup (EmitContext ec, Type queried_type,
601 string name, Location loc)
603 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
604 MemberTypes.Method, AllBindingFlags, loc);
608 /// This is a wrapper for MemberLookup that is not used to "probe", but
609 /// to find a final definition. If the final definition is not found, we
610 /// look for private members and display a useful debugging message if we
613 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
614 Type queried_type, string name, Location loc)
616 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
617 AllMemberTypes, AllBindingFlags, loc);
620 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
621 Type queried_type, string name,
622 MemberTypes mt, BindingFlags bf,
627 int errors = Report.Errors;
629 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
635 // Error has already been reported.
636 if (errors < Report.Errors)
639 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, loc);
643 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
644 Type queried_type, string name,
645 string class_name, Location loc)
647 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
648 AllMemberTypes, AllBindingFlags |
649 BindingFlags.NonPublic, name);
651 if (lookup == null) {
652 if (class_name != null)
653 Report.Error (103, loc, "The name `" + name + "' could not be " +
654 "found in `" + class_name + "'");
657 117, loc, "`" + queried_type + "' does not contain a " +
658 "definition for `" + name + "'");
662 if ((qualifier_type != null) && (qualifier_type != ec.ContainerType) &&
663 ec.ContainerType.IsSubclassOf (qualifier_type)) {
664 // Although a derived class can access protected members of
665 // its base class it cannot do so through an instance of the
666 // base class (CS1540). If the qualifier_type is a parent of the
667 // ec.ContainerType and the lookup succeeds with the latter one,
668 // then we are in this situation.
670 lookup = TypeManager.MemberLookup (
671 ec.ContainerType, ec.ContainerType, ec.ContainerType,
672 AllMemberTypes, AllBindingFlags, name);
674 if (lookup != null) {
676 1540, loc, "Cannot access protected member `" +
677 TypeManager.CSharpName (qualifier_type) + "." +
678 name + "' " + "via a qualifier of type `" +
679 TypeManager.CSharpName (qualifier_type) + "'; the " +
680 "qualifier must be of type `" +
681 TypeManager.CSharpName (ec.ContainerType) + "' " +
682 "(or derived from it)");
687 if (qualifier_type != null)
689 122, loc, "`" + TypeManager.CSharpName (qualifier_type) + "." +
690 name + "' is inaccessible due to its protection level");
693 122, loc, "`" + name + "' is inaccessible due to its " +
697 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
699 EventInfo ei = event_expr.EventInfo;
701 return TypeManager.GetPrivateFieldOfEvent (ei);
704 static EmptyExpression MyEmptyExpr;
705 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
707 Type expr_type = expr.Type;
709 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
710 // if we are a method group, emit a warning
716 // notice that it is possible to write "ValueType v = 1", the ValueType here
717 // is an abstract class, and not really a value type, so we apply the same rules.
719 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
721 // A pointer type cannot be converted to object
723 if (expr_type.IsPointer)
726 if (expr_type.IsValueType)
727 return new BoxedCast (expr);
728 if (expr_type.IsClass || expr_type.IsInterface)
729 return new EmptyCast (expr, target_type);
730 } else if (expr_type.IsSubclassOf (target_type)) {
732 // Special case: enumeration to System.Enum.
733 // System.Enum is not a value type, it is a class, so we need
734 // a boxing conversion
736 if (expr_type.IsEnum)
737 return new BoxedCast (expr);
739 return new EmptyCast (expr, target_type);
742 // This code is kind of mirrored inside StandardConversionExists
743 // with the small distinction that we only probe there
745 // Always ensure that the code here and there is in sync
747 // from the null type to any reference-type.
748 if (expr is NullLiteral && !target_type.IsValueType)
749 return new EmptyCast (expr, target_type);
751 // from any class-type S to any interface-type T.
752 if (target_type.IsInterface) {
753 if (TypeManager.ImplementsInterface (expr_type, target_type)){
754 if (expr_type.IsClass)
755 return new EmptyCast (expr, target_type);
756 else if (expr_type.IsValueType)
757 return new BoxedCast (expr);
761 // from any interface type S to interface-type T.
762 if (expr_type.IsInterface && target_type.IsInterface) {
763 if (TypeManager.ImplementsInterface (expr_type, target_type))
764 return new EmptyCast (expr, target_type);
769 // from an array-type S to an array-type of type T
770 if (expr_type.IsArray && target_type.IsArray) {
771 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
773 Type expr_element_type = expr_type.GetElementType ();
775 if (MyEmptyExpr == null)
776 MyEmptyExpr = new EmptyExpression ();
778 MyEmptyExpr.SetType (expr_element_type);
779 Type target_element_type = target_type.GetElementType ();
781 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
782 if (StandardConversionExists (MyEmptyExpr,
783 target_element_type))
784 return new EmptyCast (expr, target_type);
789 // from an array-type to System.Array
790 if (expr_type.IsArray && target_type == TypeManager.array_type)
791 return new EmptyCast (expr, target_type);
793 // from any delegate type to System.Delegate
794 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
795 target_type == TypeManager.delegate_type)
796 return new EmptyCast (expr, target_type);
798 // from any array-type or delegate type into System.ICloneable.
799 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
800 if (target_type == TypeManager.icloneable_type)
801 return new EmptyCast (expr, target_type);
811 /// Implicit Numeric Conversions.
813 /// expr is the expression to convert, returns a new expression of type
814 /// target_type or null if an implicit conversion is not possible.
816 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
817 Type target_type, Location loc)
819 Type expr_type = expr.Type;
822 // Attempt to do the implicit constant expression conversions
824 if (expr is IntConstant){
827 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
831 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
833 // Try the implicit constant expression conversion
834 // from long to ulong, instead of a nice routine,
837 long v = ((LongConstant) expr).Value;
839 return new ULongConstant ((ulong) v);
842 Type real_target_type = target_type;
844 if (expr_type == TypeManager.sbyte_type){
846 // From sbyte to short, int, long, float, double.
848 if (real_target_type == TypeManager.int32_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
850 if (real_target_type == TypeManager.int64_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
852 if (real_target_type == TypeManager.double_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
854 if (real_target_type == TypeManager.float_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
856 if (real_target_type == TypeManager.short_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
858 } else if (expr_type == TypeManager.byte_type){
860 // From byte to short, ushort, int, uint, long, ulong, float, double
862 if ((real_target_type == TypeManager.short_type) ||
863 (real_target_type == TypeManager.ushort_type) ||
864 (real_target_type == TypeManager.int32_type) ||
865 (real_target_type == TypeManager.uint32_type))
866 return new EmptyCast (expr, target_type);
868 if (real_target_type == TypeManager.uint64_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
870 if (real_target_type == TypeManager.int64_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
872 if (real_target_type == TypeManager.float_type)
873 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
874 if (real_target_type == TypeManager.double_type)
875 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
876 } else if (expr_type == TypeManager.short_type){
878 // From short to int, long, float, double
880 if (real_target_type == TypeManager.int32_type)
881 return new EmptyCast (expr, target_type);
882 if (real_target_type == TypeManager.int64_type)
883 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
884 if (real_target_type == TypeManager.double_type)
885 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
886 if (real_target_type == TypeManager.float_type)
887 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
888 } else if (expr_type == TypeManager.ushort_type){
890 // From ushort to int, uint, long, ulong, float, double
892 if (real_target_type == TypeManager.uint32_type)
893 return new EmptyCast (expr, target_type);
895 if (real_target_type == TypeManager.uint64_type)
896 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
897 if (real_target_type == TypeManager.int32_type)
898 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
899 if (real_target_type == TypeManager.int64_type)
900 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
901 if (real_target_type == TypeManager.double_type)
902 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
903 if (real_target_type == TypeManager.float_type)
904 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
905 } else if (expr_type == TypeManager.int32_type){
907 // From int to long, float, double
909 if (real_target_type == TypeManager.int64_type)
910 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
911 if (real_target_type == TypeManager.double_type)
912 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
913 if (real_target_type == TypeManager.float_type)
914 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
915 } else if (expr_type == TypeManager.uint32_type){
917 // From uint to long, ulong, float, double
919 if (real_target_type == TypeManager.int64_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
921 if (real_target_type == TypeManager.uint64_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
923 if (real_target_type == TypeManager.double_type)
924 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
926 if (real_target_type == TypeManager.float_type)
927 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
929 } else if (expr_type == TypeManager.int64_type){
931 // From long/ulong to float, double
933 if (real_target_type == TypeManager.double_type)
934 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
935 if (real_target_type == TypeManager.float_type)
936 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
937 } else if (expr_type == TypeManager.uint64_type){
939 // From ulong to float, double
941 if (real_target_type == TypeManager.double_type)
942 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
944 if (real_target_type == TypeManager.float_type)
945 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
947 } else if (expr_type == TypeManager.char_type){
949 // From char to ushort, int, uint, long, ulong, float, double
951 if ((real_target_type == TypeManager.ushort_type) ||
952 (real_target_type == TypeManager.int32_type) ||
953 (real_target_type == TypeManager.uint32_type))
954 return new EmptyCast (expr, target_type);
955 if (real_target_type == TypeManager.uint64_type)
956 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
957 if (real_target_type == TypeManager.int64_type)
958 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
959 if (real_target_type == TypeManager.float_type)
960 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
961 if (real_target_type == TypeManager.double_type)
962 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
963 } else if (expr_type == TypeManager.float_type){
967 if (real_target_type == TypeManager.double_type)
968 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
975 // Tests whether an implicit reference conversion exists between expr_type
978 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
980 Type expr_type = expr.Type;
983 // This is the boxed case.
985 if (target_type == TypeManager.object_type) {
986 if ((expr_type.IsClass) ||
987 (expr_type.IsValueType) ||
988 (expr_type.IsInterface))
991 } else if (expr_type.IsSubclassOf (target_type)) {
994 // Please remember that all code below actually comes
995 // from ImplicitReferenceConversion so make sure code remains in sync
997 // from any class-type S to any interface-type T.
998 if (target_type.IsInterface) {
999 if (TypeManager.ImplementsInterface (expr_type, target_type))
1003 // from any interface type S to interface-type T.
1004 if (expr_type.IsInterface && target_type.IsInterface)
1005 if (TypeManager.ImplementsInterface (expr_type, target_type))
1008 // from an array-type S to an array-type of type T
1009 if (expr_type.IsArray && target_type.IsArray) {
1010 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1012 Type expr_element_type = expr_type.GetElementType ();
1014 if (MyEmptyExpr == null)
1015 MyEmptyExpr = new EmptyExpression ();
1017 MyEmptyExpr.SetType (expr_element_type);
1018 Type target_element_type = target_type.GetElementType ();
1020 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1021 if (StandardConversionExists (MyEmptyExpr,
1022 target_element_type))
1027 // from an array-type to System.Array
1028 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1031 // from any delegate type to System.Delegate
1032 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1033 target_type == TypeManager.delegate_type)
1034 if (target_type.IsAssignableFrom (expr_type))
1037 // from any array-type or delegate type into System.ICloneable.
1038 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1039 if (target_type == TypeManager.icloneable_type)
1042 // from the null type to any reference-type.
1043 if (expr is NullLiteral && !target_type.IsValueType &&
1044 !TypeManager.IsEnumType (target_type))
1053 /// Same as StandardConversionExists except that it also looks at
1054 /// implicit user defined conversions - needed for overload resolution
1056 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1058 if (StandardConversionExists (expr, target_type) == true)
1061 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1069 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
1071 Expression dummy = ImplicitUserConversion (
1072 ec, new EmptyExpression (source), target, Location.Null);
1073 return dummy != null;
1077 /// Determines if a standard implicit conversion exists from
1078 /// expr_type to target_type
1080 public static bool StandardConversionExists (Expression expr, Type target_type)
1082 Type expr_type = expr.Type;
1084 if (expr_type == TypeManager.void_type)
1087 if (expr_type == target_type)
1090 // First numeric conversions
1092 if (expr_type == TypeManager.sbyte_type){
1094 // From sbyte to short, int, long, float, double.
1096 if ((target_type == TypeManager.int32_type) ||
1097 (target_type == TypeManager.int64_type) ||
1098 (target_type == TypeManager.double_type) ||
1099 (target_type == TypeManager.float_type) ||
1100 (target_type == TypeManager.short_type) ||
1101 (target_type == TypeManager.decimal_type))
1104 } else if (expr_type == TypeManager.byte_type){
1106 // From byte to short, ushort, int, uint, long, ulong, float, double
1108 if ((target_type == TypeManager.short_type) ||
1109 (target_type == TypeManager.ushort_type) ||
1110 (target_type == TypeManager.int32_type) ||
1111 (target_type == TypeManager.uint32_type) ||
1112 (target_type == TypeManager.uint64_type) ||
1113 (target_type == TypeManager.int64_type) ||
1114 (target_type == TypeManager.float_type) ||
1115 (target_type == TypeManager.double_type) ||
1116 (target_type == TypeManager.decimal_type))
1119 } else if (expr_type == TypeManager.short_type){
1121 // From short to int, long, float, double
1123 if ((target_type == TypeManager.int32_type) ||
1124 (target_type == TypeManager.int64_type) ||
1125 (target_type == TypeManager.double_type) ||
1126 (target_type == TypeManager.float_type) ||
1127 (target_type == TypeManager.decimal_type))
1130 } else if (expr_type == TypeManager.ushort_type){
1132 // From ushort to int, uint, long, ulong, float, double
1134 if ((target_type == TypeManager.uint32_type) ||
1135 (target_type == TypeManager.uint64_type) ||
1136 (target_type == TypeManager.int32_type) ||
1137 (target_type == TypeManager.int64_type) ||
1138 (target_type == TypeManager.double_type) ||
1139 (target_type == TypeManager.float_type) ||
1140 (target_type == TypeManager.decimal_type))
1143 } else if (expr_type == TypeManager.int32_type){
1145 // From int to long, float, double
1147 if ((target_type == TypeManager.int64_type) ||
1148 (target_type == TypeManager.double_type) ||
1149 (target_type == TypeManager.float_type) ||
1150 (target_type == TypeManager.decimal_type))
1153 } else if (expr_type == TypeManager.uint32_type){
1155 // From uint to long, ulong, float, double
1157 if ((target_type == TypeManager.int64_type) ||
1158 (target_type == TypeManager.uint64_type) ||
1159 (target_type == TypeManager.double_type) ||
1160 (target_type == TypeManager.float_type) ||
1161 (target_type == TypeManager.decimal_type))
1164 } else if ((expr_type == TypeManager.uint64_type) ||
1165 (expr_type == TypeManager.int64_type)) {
1167 // From long/ulong to float, double
1169 if ((target_type == TypeManager.double_type) ||
1170 (target_type == TypeManager.float_type) ||
1171 (target_type == TypeManager.decimal_type))
1174 } else if (expr_type == TypeManager.char_type){
1176 // From char to ushort, int, uint, long, ulong, float, double
1178 if ((target_type == TypeManager.ushort_type) ||
1179 (target_type == TypeManager.int32_type) ||
1180 (target_type == TypeManager.uint32_type) ||
1181 (target_type == TypeManager.uint64_type) ||
1182 (target_type == TypeManager.int64_type) ||
1183 (target_type == TypeManager.float_type) ||
1184 (target_type == TypeManager.double_type) ||
1185 (target_type == TypeManager.decimal_type))
1188 } else if (expr_type == TypeManager.float_type){
1192 if (target_type == TypeManager.double_type)
1196 if (ImplicitReferenceConversionExists (expr, target_type))
1199 if (expr is IntConstant){
1200 int value = ((IntConstant) expr).Value;
1202 if (target_type == TypeManager.sbyte_type){
1203 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1205 } else if (target_type == TypeManager.byte_type){
1206 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1208 } else if (target_type == TypeManager.short_type){
1209 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1211 } else if (target_type == TypeManager.ushort_type){
1212 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1214 } else if (target_type == TypeManager.uint32_type){
1217 } else if (target_type == TypeManager.uint64_type){
1219 // we can optimize this case: a positive int32
1220 // always fits on a uint64. But we need an opcode
1227 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1231 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1233 // Try the implicit constant expression conversion
1234 // from long to ulong, instead of a nice routine,
1235 // we just inline it
1237 long v = ((LongConstant) expr).Value;
1242 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1243 IntLiteral i = (IntLiteral) expr;
1249 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1256 // Used internally by FindMostEncompassedType, this is used
1257 // to avoid creating lots of objects in the tight loop inside
1258 // FindMostEncompassedType
1260 static EmptyExpression priv_fmet_param;
1263 /// Finds "most encompassed type" according to the spec (13.4.2)
1264 /// amongst the methods in the MethodGroupExpr
1266 static Type FindMostEncompassedType (ArrayList types)
1270 if (priv_fmet_param == null)
1271 priv_fmet_param = new EmptyExpression ();
1273 foreach (Type t in types){
1274 priv_fmet_param.SetType (t);
1281 if (StandardConversionExists (priv_fmet_param, best))
1289 // Used internally by FindMostEncompassingType, this is used
1290 // to avoid creating lots of objects in the tight loop inside
1291 // FindMostEncompassingType
1293 static EmptyExpression priv_fmee_ret;
1296 /// Finds "most encompassing type" according to the spec (13.4.2)
1297 /// amongst the types in the given set
1299 static Type FindMostEncompassingType (ArrayList types)
1303 if (priv_fmee_ret == null)
1304 priv_fmee_ret = new EmptyExpression ();
1306 foreach (Type t in types){
1307 priv_fmee_ret.SetType (best);
1314 if (StandardConversionExists (priv_fmee_ret, t))
1322 // Used to avoid creating too many objects
1324 static EmptyExpression priv_fms_expr;
1327 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1328 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1329 /// for explicit and implicit conversion operators.
1331 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1332 bool apply_explicit_conv_rules,
1335 ArrayList src_types_set = new ArrayList ();
1337 if (priv_fms_expr == null)
1338 priv_fms_expr = new EmptyExpression ();
1341 // If any operator converts from S then Sx = S
1343 Type source_type = source.Type;
1344 foreach (MethodBase mb in me.Methods){
1345 ParameterData pd = Invocation.GetParameterData (mb);
1346 Type param_type = pd.ParameterType (0);
1348 if (param_type == source_type)
1351 if (apply_explicit_conv_rules) {
1354 // Find the set of applicable user-defined conversion operators, U. This set
1356 // user-defined implicit or explicit conversion operators declared by
1357 // the classes or structs in D that convert from a type encompassing
1358 // or encompassed by S to a type encompassing or encompassed by T
1360 priv_fms_expr.SetType (param_type);
1361 if (StandardConversionExists (priv_fms_expr, source_type))
1362 src_types_set.Add (param_type);
1364 if (StandardConversionExists (source, param_type))
1365 src_types_set.Add (param_type);
1369 // Only if S is encompassed by param_type
1371 if (StandardConversionExists (source, param_type))
1372 src_types_set.Add (param_type);
1377 // Explicit Conv rules
1379 if (apply_explicit_conv_rules) {
1380 ArrayList candidate_set = new ArrayList ();
1382 foreach (Type param_type in src_types_set){
1383 if (StandardConversionExists (source, param_type))
1384 candidate_set.Add (param_type);
1387 if (candidate_set.Count != 0)
1388 return FindMostEncompassedType (candidate_set);
1394 if (apply_explicit_conv_rules)
1395 return FindMostEncompassingType (src_types_set);
1397 return FindMostEncompassedType (src_types_set);
1401 // Useful in avoiding proliferation of objects
1403 static EmptyExpression priv_fmt_expr;
1406 /// Finds the most specific target Tx according to section 13.4.4
1408 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1409 bool apply_explicit_conv_rules,
1412 ArrayList tgt_types_set = new ArrayList ();
1414 if (priv_fmt_expr == null)
1415 priv_fmt_expr = new EmptyExpression ();
1418 // If any operator converts to T then Tx = T
1420 foreach (MethodInfo mi in me.Methods){
1421 Type ret_type = mi.ReturnType;
1423 if (ret_type == target)
1426 if (apply_explicit_conv_rules) {
1429 // Find the set of applicable user-defined conversion operators, U.
1431 // This set consists of the
1432 // user-defined implicit or explicit conversion operators declared by
1433 // the classes or structs in D that convert from a type encompassing
1434 // or encompassed by S to a type encompassing or encompassed by T
1436 priv_fms_expr.SetType (ret_type);
1437 if (StandardConversionExists (priv_fms_expr, target))
1438 tgt_types_set.Add (ret_type);
1440 priv_fms_expr.SetType (target);
1441 if (StandardConversionExists (priv_fms_expr, ret_type))
1442 tgt_types_set.Add (ret_type);
1446 // Only if T is encompassed by param_type
1448 priv_fms_expr.SetType (ret_type);
1449 if (StandardConversionExists (priv_fms_expr, target))
1450 tgt_types_set.Add (ret_type);
1455 // Explicit conv rules
1457 if (apply_explicit_conv_rules) {
1458 ArrayList candidate_set = new ArrayList ();
1460 foreach (Type ret_type in tgt_types_set){
1461 priv_fmt_expr.SetType (ret_type);
1463 if (StandardConversionExists (priv_fmt_expr, target))
1464 candidate_set.Add (ret_type);
1467 if (candidate_set.Count != 0)
1468 return FindMostEncompassingType (candidate_set);
1472 // Okay, final case !
1474 if (apply_explicit_conv_rules)
1475 return FindMostEncompassedType (tgt_types_set);
1477 return FindMostEncompassingType (tgt_types_set);
1481 /// User-defined Implicit conversions
1483 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1484 Type target, Location loc)
1486 return UserDefinedConversion (ec, source, target, loc, false);
1490 /// User-defined Explicit conversions
1492 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1493 Type target, Location loc)
1495 return UserDefinedConversion (ec, source, target, loc, true);
1499 /// Computes the MethodGroup for the user-defined conversion
1500 /// operators from source_type to target_type. `look_for_explicit'
1501 /// controls whether we should also include the list of explicit
1504 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1505 Type source_type, Type target_type,
1506 Location loc, bool look_for_explicit)
1508 Expression mg1 = null, mg2 = null;
1509 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1513 // FIXME : How does the False operator come into the picture ?
1514 // This doesn't look complete and very correct !
1516 if (target_type == TypeManager.bool_type && !look_for_explicit)
1517 op_name = "op_True";
1519 op_name = "op_Implicit";
1521 MethodGroupExpr union3;
1523 mg1 = MethodLookup (ec, source_type, op_name, loc);
1524 if (source_type.BaseType != null)
1525 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1528 union3 = (MethodGroupExpr) mg2;
1529 else if (mg2 == null)
1530 union3 = (MethodGroupExpr) mg1;
1532 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1534 mg1 = MethodLookup (ec, target_type, op_name, loc);
1537 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1539 union3 = (MethodGroupExpr) mg1;
1542 if (target_type.BaseType != null)
1543 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1547 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1549 union3 = (MethodGroupExpr) mg1;
1552 MethodGroupExpr union4 = null;
1554 if (look_for_explicit) {
1555 op_name = "op_Explicit";
1557 mg5 = MemberLookup (ec, source_type, op_name, loc);
1558 if (source_type.BaseType != null)
1559 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1561 mg7 = MemberLookup (ec, target_type, op_name, loc);
1562 if (target_type.BaseType != null)
1563 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1565 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1566 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1568 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1571 return Invocation.MakeUnionSet (union3, union4, loc);
1575 /// User-defined conversions
1577 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1578 Type target, Location loc,
1579 bool look_for_explicit)
1581 MethodGroupExpr union;
1582 Type source_type = source.Type;
1583 MethodBase method = null;
1585 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1589 Type most_specific_source, most_specific_target;
1592 foreach (MethodBase m in union.Methods){
1593 Console.WriteLine ("Name: " + m.Name);
1594 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1598 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1599 if (most_specific_source == null)
1602 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1603 if (most_specific_target == null)
1608 foreach (MethodBase mb in union.Methods){
1609 ParameterData pd = Invocation.GetParameterData (mb);
1610 MethodInfo mi = (MethodInfo) mb;
1612 if (pd.ParameterType (0) == most_specific_source &&
1613 mi.ReturnType == most_specific_target) {
1619 if (method == null || count > 1)
1624 // This will do the conversion to the best match that we
1625 // found. Now we need to perform an implict standard conversion
1626 // if the best match was not the type that we were requested
1629 if (look_for_explicit)
1630 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1632 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1638 e = new UserCast ((MethodInfo) method, source, loc);
1639 if (e.Type != target){
1640 if (!look_for_explicit)
1641 e = ConvertImplicitStandard (ec, e, target, loc);
1643 e = ConvertExplicitStandard (ec, e, target, loc);
1649 /// Converts implicitly the resolved expression `expr' into the
1650 /// `target_type'. It returns a new expression that can be used
1651 /// in a context that expects a `target_type'.
1653 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1654 Type target_type, Location loc)
1656 Type expr_type = expr.Type;
1659 if (expr_type == target_type)
1662 if (target_type == null)
1663 throw new Exception ("Target type is null");
1665 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1669 e = ImplicitUserConversion (ec, expr, target_type, loc);
1678 /// Attempts to apply the `Standard Implicit
1679 /// Conversion' rules to the expression `expr' into
1680 /// the `target_type'. It returns a new expression
1681 /// that can be used in a context that expects a
1684 /// This is different from `ConvertImplicit' in that the
1685 /// user defined implicit conversions are excluded.
1687 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1688 Type target_type, Location loc)
1690 Type expr_type = expr.Type;
1693 if (expr_type == target_type)
1696 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1700 e = ImplicitReferenceConversion (expr, target_type);
1704 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1705 IntLiteral i = (IntLiteral) expr;
1708 return new EmptyCast (expr, target_type);
1712 if (expr_type.IsPointer){
1713 if (target_type == TypeManager.void_ptr_type)
1714 return new EmptyCast (expr, target_type);
1717 // yep, comparing pointer types cant be done with
1718 // t1 == t2, we have to compare their element types.
1720 if (target_type.IsPointer){
1721 if (target_type.GetElementType()==expr_type.GetElementType())
1726 if (target_type.IsPointer){
1727 if (expr is NullLiteral)
1728 return new EmptyCast (expr, target_type);
1736 /// Attemps to perform an implict constant conversion of the IntConstant
1737 /// into a different data type using casts (See Implicit Constant
1738 /// Expression Conversions)
1740 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1742 int value = ic.Value;
1745 // FIXME: This could return constants instead of EmptyCasts
1747 if (target_type == TypeManager.sbyte_type){
1748 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1749 return new SByteConstant ((sbyte) value);
1750 } else if (target_type == TypeManager.byte_type){
1751 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1752 return new ByteConstant ((byte) value);
1753 } else if (target_type == TypeManager.short_type){
1754 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1755 return new ShortConstant ((short) value);
1756 } else if (target_type == TypeManager.ushort_type){
1757 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1758 return new UShortConstant ((ushort) value);
1759 } else if (target_type == TypeManager.uint32_type){
1761 return new UIntConstant ((uint) value);
1762 } else if (target_type == TypeManager.uint64_type){
1764 // we can optimize this case: a positive int32
1765 // always fits on a uint64. But we need an opcode
1769 return new ULongConstant ((ulong) value);
1770 } else if (target_type == TypeManager.double_type)
1771 return new DoubleConstant ((double) value);
1772 else if (target_type == TypeManager.float_type)
1773 return new FloatConstant ((float) value);
1775 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1776 Type underlying = TypeManager.EnumToUnderlying (target_type);
1777 Constant e = (Constant) ic;
1780 // Possibly, we need to create a different 0 literal before passing
1783 if (underlying == TypeManager.int64_type)
1784 e = new LongLiteral (0);
1785 else if (underlying == TypeManager.uint64_type)
1786 e = new ULongLiteral (0);
1788 return new EnumConstant (e, target_type);
1793 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1795 string msg = "Cannot convert implicitly from `"+
1796 TypeManager.CSharpName (source) + "' to `" +
1797 TypeManager.CSharpName (target) + "'";
1799 Report.Error (29, loc, msg);
1803 /// Attemptes to implicityly convert `target' into `type', using
1804 /// ConvertImplicit. If there is no implicit conversion, then
1805 /// an error is signaled
1807 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1808 Type target_type, Location loc)
1812 e = ConvertImplicit (ec, source, target_type, loc);
1816 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1817 Report.Error (664, loc,
1818 "Double literal cannot be implicitly converted to " +
1819 "float type, use F suffix to create a float literal");
1822 Error_CannotConvertImplicit (loc, source.Type, target_type);
1828 /// Performs the explicit numeric conversions
1830 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1832 Type expr_type = expr.Type;
1835 // If we have an enumeration, extract the underlying type,
1836 // use this during the comparison, but wrap around the original
1839 Type real_target_type = target_type;
1841 if (TypeManager.IsEnumType (real_target_type))
1842 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1844 if (StandardConversionExists (expr, real_target_type)){
1845 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1847 if (real_target_type != target_type)
1848 return new EmptyCast (ce, target_type);
1852 if (expr_type == TypeManager.sbyte_type){
1854 // From sbyte to byte, ushort, uint, ulong, char
1856 if (real_target_type == TypeManager.byte_type)
1857 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1858 if (real_target_type == TypeManager.ushort_type)
1859 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1860 if (real_target_type == TypeManager.uint32_type)
1861 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1862 if (real_target_type == TypeManager.uint64_type)
1863 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1864 if (real_target_type == TypeManager.char_type)
1865 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1866 } else if (expr_type == TypeManager.byte_type){
1868 // From byte to sbyte and char
1870 if (real_target_type == TypeManager.sbyte_type)
1871 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1872 if (real_target_type == TypeManager.char_type)
1873 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1874 } else if (expr_type == TypeManager.short_type){
1876 // From short to sbyte, byte, ushort, uint, ulong, char
1878 if (real_target_type == TypeManager.sbyte_type)
1879 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1880 if (real_target_type == TypeManager.byte_type)
1881 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1882 if (real_target_type == TypeManager.ushort_type)
1883 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1884 if (real_target_type == TypeManager.uint32_type)
1885 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1886 if (real_target_type == TypeManager.uint64_type)
1887 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1888 if (real_target_type == TypeManager.char_type)
1889 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1890 } else if (expr_type == TypeManager.ushort_type){
1892 // From ushort to sbyte, byte, short, char
1894 if (real_target_type == TypeManager.sbyte_type)
1895 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1896 if (real_target_type == TypeManager.byte_type)
1897 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1898 if (real_target_type == TypeManager.short_type)
1899 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1900 if (real_target_type == TypeManager.char_type)
1901 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1902 } else if (expr_type == TypeManager.int32_type){
1904 // From int to sbyte, byte, short, ushort, uint, ulong, char
1906 if (real_target_type == TypeManager.sbyte_type)
1907 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1908 if (real_target_type == TypeManager.byte_type)
1909 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1910 if (real_target_type == TypeManager.short_type)
1911 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1912 if (real_target_type == TypeManager.ushort_type)
1913 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1914 if (real_target_type == TypeManager.uint32_type)
1915 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1916 if (real_target_type == TypeManager.uint64_type)
1917 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1918 if (real_target_type == TypeManager.char_type)
1919 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1920 } else if (expr_type == TypeManager.uint32_type){
1922 // From uint to sbyte, byte, short, ushort, int, char
1924 if (real_target_type == TypeManager.sbyte_type)
1925 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1926 if (real_target_type == TypeManager.byte_type)
1927 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1928 if (real_target_type == TypeManager.short_type)
1929 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1930 if (real_target_type == TypeManager.ushort_type)
1931 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1932 if (real_target_type == TypeManager.int32_type)
1933 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1934 if (real_target_type == TypeManager.char_type)
1935 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1936 } else if (expr_type == TypeManager.int64_type){
1938 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1940 if (real_target_type == TypeManager.sbyte_type)
1941 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1942 if (real_target_type == TypeManager.byte_type)
1943 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1944 if (real_target_type == TypeManager.short_type)
1945 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1946 if (real_target_type == TypeManager.ushort_type)
1947 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1948 if (real_target_type == TypeManager.int32_type)
1949 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1950 if (real_target_type == TypeManager.uint32_type)
1951 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1952 if (real_target_type == TypeManager.uint64_type)
1953 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1954 if (real_target_type == TypeManager.char_type)
1955 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1956 } else if (expr_type == TypeManager.uint64_type){
1958 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1960 if (real_target_type == TypeManager.sbyte_type)
1961 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1962 if (real_target_type == TypeManager.byte_type)
1963 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1964 if (real_target_type == TypeManager.short_type)
1965 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1966 if (real_target_type == TypeManager.ushort_type)
1967 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1968 if (real_target_type == TypeManager.int32_type)
1969 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1970 if (real_target_type == TypeManager.uint32_type)
1971 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1972 if (real_target_type == TypeManager.int64_type)
1973 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1974 if (real_target_type == TypeManager.char_type)
1975 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1976 } else if (expr_type == TypeManager.char_type){
1978 // From char to sbyte, byte, short
1980 if (real_target_type == TypeManager.sbyte_type)
1981 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1982 if (real_target_type == TypeManager.byte_type)
1983 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1984 if (real_target_type == TypeManager.short_type)
1985 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1986 } else if (expr_type == TypeManager.float_type){
1988 // From float to sbyte, byte, short,
1989 // ushort, int, uint, long, ulong, char
1992 if (real_target_type == TypeManager.sbyte_type)
1993 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1994 if (real_target_type == TypeManager.byte_type)
1995 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1996 if (real_target_type == TypeManager.short_type)
1997 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1998 if (real_target_type == TypeManager.ushort_type)
1999 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2000 if (real_target_type == TypeManager.int32_type)
2001 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2002 if (real_target_type == TypeManager.uint32_type)
2003 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2004 if (real_target_type == TypeManager.int64_type)
2005 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2006 if (real_target_type == TypeManager.uint64_type)
2007 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2008 if (real_target_type == TypeManager.char_type)
2009 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2010 } else if (expr_type == TypeManager.double_type){
2012 // From double to byte, byte, short,
2013 // ushort, int, uint, long, ulong,
2014 // char, float or decimal
2016 if (real_target_type == TypeManager.sbyte_type)
2017 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2018 if (real_target_type == TypeManager.byte_type)
2019 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2020 if (real_target_type == TypeManager.short_type)
2021 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2022 if (real_target_type == TypeManager.ushort_type)
2023 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2024 if (real_target_type == TypeManager.int32_type)
2025 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2026 if (real_target_type == TypeManager.uint32_type)
2027 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2028 if (real_target_type == TypeManager.int64_type)
2029 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2030 if (real_target_type == TypeManager.uint64_type)
2031 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2032 if (real_target_type == TypeManager.char_type)
2033 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2034 if (real_target_type == TypeManager.float_type)
2035 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2038 // decimal is taken care of by the op_Explicit methods.
2044 /// Returns whether an explicit reference conversion can be performed
2045 /// from source_type to target_type
2047 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2049 bool target_is_value_type = target_type.IsValueType;
2051 if (source_type == target_type)
2055 // From object to any reference type
2057 if (source_type == TypeManager.object_type && !target_is_value_type)
2061 // From any class S to any class-type T, provided S is a base class of T
2063 if (target_type.IsSubclassOf (source_type))
2067 // From any interface type S to any interface T provided S is not derived from T
2069 if (source_type.IsInterface && target_type.IsInterface){
2070 if (!target_type.IsSubclassOf (source_type))
2075 // From any class type S to any interface T, provided S is not sealed
2076 // and provided S does not implement T.
2078 if (target_type.IsInterface && !source_type.IsSealed &&
2079 !TypeManager.ImplementsInterface (source_type, target_type))
2083 // From any interface-type S to to any class type T, provided T is not
2084 // sealed, or provided T implements S.
2086 if (source_type.IsInterface &&
2087 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2091 // From an array type S with an element type Se to an array type T with an
2092 // element type Te provided all the following are true:
2093 // * S and T differe only in element type, in other words, S and T
2094 // have the same number of dimensions.
2095 // * Both Se and Te are reference types
2096 // * An explicit referenc conversions exist from Se to Te
2098 if (source_type.IsArray && target_type.IsArray) {
2099 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2101 Type source_element_type = source_type.GetElementType ();
2102 Type target_element_type = target_type.GetElementType ();
2104 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2105 if (ExplicitReferenceConversionExists (source_element_type,
2106 target_element_type))
2112 // From System.Array to any array-type
2113 if (source_type == TypeManager.array_type &&
2114 target_type.IsArray){
2119 // From System delegate to any delegate-type
2121 if (source_type == TypeManager.delegate_type &&
2122 target_type.IsSubclassOf (TypeManager.delegate_type))
2126 // From ICloneable to Array or Delegate types
2128 if (source_type == TypeManager.icloneable_type &&
2129 (target_type == TypeManager.array_type ||
2130 target_type == TypeManager.delegate_type))
2137 /// Implements Explicit Reference conversions
2139 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2141 Type source_type = source.Type;
2142 bool target_is_value_type = target_type.IsValueType;
2145 // From object to any reference type
2147 if (source_type == TypeManager.object_type && !target_is_value_type)
2148 return new ClassCast (source, target_type);
2152 // From any class S to any class-type T, provided S is a base class of T
2154 if (target_type.IsSubclassOf (source_type))
2155 return new ClassCast (source, target_type);
2158 // From any interface type S to any interface T provided S is not derived from T
2160 if (source_type.IsInterface && target_type.IsInterface){
2161 if (TypeManager.ImplementsInterface (source_type, target_type))
2164 return new ClassCast (source, target_type);
2168 // From any class type S to any interface T, provides S is not sealed
2169 // and provided S does not implement T.
2171 if (target_type.IsInterface && !source_type.IsSealed) {
2172 if (TypeManager.ImplementsInterface (source_type, target_type))
2175 return new ClassCast (source, target_type);
2180 // From any interface-type S to to any class type T, provided T is not
2181 // sealed, or provided T implements S.
2183 if (source_type.IsInterface) {
2184 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2185 return new ClassCast (source, target_type);
2190 // From an array type S with an element type Se to an array type T with an
2191 // element type Te provided all the following are true:
2192 // * S and T differe only in element type, in other words, S and T
2193 // have the same number of dimensions.
2194 // * Both Se and Te are reference types
2195 // * An explicit referenc conversions exist from Se to Te
2197 if (source_type.IsArray && target_type.IsArray) {
2198 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2200 Type source_element_type = source_type.GetElementType ();
2201 Type target_element_type = target_type.GetElementType ();
2203 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2204 if (ExplicitReferenceConversionExists (source_element_type,
2205 target_element_type))
2206 return new ClassCast (source, target_type);
2211 // From System.Array to any array-type
2212 if (source_type == TypeManager.array_type &&
2213 target_type.IsArray) {
2214 return new ClassCast (source, target_type);
2218 // From System delegate to any delegate-type
2220 if (source_type == TypeManager.delegate_type &&
2221 target_type.IsSubclassOf (TypeManager.delegate_type))
2222 return new ClassCast (source, target_type);
2225 // From ICloneable to Array or Delegate types
2227 if (source_type == TypeManager.icloneable_type &&
2228 (target_type == TypeManager.array_type ||
2229 target_type == TypeManager.delegate_type))
2230 return new ClassCast (source, target_type);
2236 /// Performs an explicit conversion of the expression `expr' whose
2237 /// type is expr.Type to `target_type'.
2239 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2240 Type target_type, Location loc)
2242 Type expr_type = expr.Type;
2243 Type original_expr_type = expr_type;
2245 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
2246 if (target_type == TypeManager.enum_type ||
2247 target_type == TypeManager.object_type) {
2248 if (expr is EnumConstant)
2249 expr = ((EnumConstant) expr).Child;
2250 // We really need all these casts here .... :-(
2251 expr = new BoxedCast (new EmptyCast (expr, expr_type));
2252 return new EmptyCast (expr, target_type);
2253 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
2254 target_type.IsSubclassOf (TypeManager.enum_type))
2255 return new UnboxCast (expr, target_type);
2258 // Notice that we have kept the expr_type unmodified, which is only
2260 if (expr is EnumConstant)
2261 expr = ((EnumConstant) expr).Child;
2263 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2264 expr_type = expr.Type;
2267 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2272 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2277 // Unboxing conversion.
2279 if (expr_type == TypeManager.object_type && target_type.IsValueType){
2280 if (expr is NullLiteral){
2281 Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
2284 return new UnboxCast (expr, target_type);
2288 ne = ConvertReferenceExplicit (expr, target_type);
2293 if (target_type.IsPointer){
2294 if (expr_type.IsPointer)
2295 return new EmptyCast (expr, target_type);
2297 if (expr_type == TypeManager.sbyte_type ||
2298 expr_type == TypeManager.byte_type ||
2299 expr_type == TypeManager.short_type ||
2300 expr_type == TypeManager.ushort_type ||
2301 expr_type == TypeManager.int32_type ||
2302 expr_type == TypeManager.uint32_type ||
2303 expr_type == TypeManager.uint64_type ||
2304 expr_type == TypeManager.int64_type)
2305 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2307 if (expr_type.IsPointer){
2308 if (target_type == TypeManager.sbyte_type ||
2309 target_type == TypeManager.byte_type ||
2310 target_type == TypeManager.short_type ||
2311 target_type == TypeManager.ushort_type ||
2312 target_type == TypeManager.int32_type ||
2313 target_type == TypeManager.uint32_type ||
2314 target_type == TypeManager.uint64_type ||
2315 target_type == TypeManager.int64_type){
2316 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2319 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2324 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2328 // We should always be able to go from an uint32
2329 // implicitly or explicitly to the other integral
2332 throw new Exception ("Internal compiler error");
2337 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2341 Error_CannotConvertType (loc, original_expr_type, target_type);
2346 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2348 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2349 Type target_type, Location l)
2351 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2356 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2360 ne = ConvertReferenceExplicit (expr, target_type);
2364 Error_CannotConvertType (l, expr.Type, target_type);
2368 static string ExprClassName (ExprClass c)
2371 case ExprClass.Invalid:
2373 case ExprClass.Value:
2375 case ExprClass.Variable:
2377 case ExprClass.Namespace:
2379 case ExprClass.Type:
2381 case ExprClass.MethodGroup:
2382 return "method group";
2383 case ExprClass.PropertyAccess:
2384 return "property access";
2385 case ExprClass.EventAccess:
2386 return "event access";
2387 case ExprClass.IndexerAccess:
2388 return "indexer access";
2389 case ExprClass.Nothing:
2392 throw new Exception ("Should not happen");
2396 /// Reports that we were expecting `expr' to be of class `expected'
2398 public void Error118 (string expected)
2400 string kind = "Unknown";
2402 kind = ExprClassName (eclass);
2404 Error (118, "Expression denotes a `" + kind +
2405 "' where a `" + expected + "' was expected");
2408 public void Error118 (ResolveFlags flags)
2410 ArrayList valid = new ArrayList (10);
2412 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2413 valid.Add ("variable");
2414 valid.Add ("value");
2417 if ((flags & ResolveFlags.Type) != 0)
2420 if ((flags & ResolveFlags.MethodGroup) != 0)
2421 valid.Add ("method group");
2423 if ((flags & ResolveFlags.SimpleName) != 0)
2424 valid.Add ("simple name");
2426 if (valid.Count == 0)
2427 valid.Add ("unknown");
2429 StringBuilder sb = new StringBuilder ();
2430 for (int i = 0; i < valid.Count; i++) {
2433 else if (i == valid.Count)
2435 sb.Append (valid [i]);
2438 string kind = ExprClassName (eclass);
2440 Error (119, "Expression denotes a `" + kind + "' where " +
2441 "a `" + sb.ToString () + "' was expected");
2444 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2446 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2447 TypeManager.CSharpName (t));
2450 public static void UnsafeError (Location loc)
2452 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2456 /// Converts the IntConstant, UIntConstant, LongConstant or
2457 /// ULongConstant into the integral target_type. Notice
2458 /// that we do not return an `Expression' we do return
2459 /// a boxed integral type.
2461 /// FIXME: Since I added the new constants, we need to
2462 /// also support conversions from CharConstant, ByteConstant,
2463 /// SByteConstant, UShortConstant, ShortConstant
2465 /// This is used by the switch statement, so the domain
2466 /// of work is restricted to the literals above, and the
2467 /// targets are int32, uint32, char, byte, sbyte, ushort,
2468 /// short, uint64 and int64
2470 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2474 if (c.Type == target_type)
2475 return ((Constant) c).GetValue ();
2478 // Make into one of the literals we handle, we dont really care
2479 // about this value as we will just return a few limited types
2481 if (c is EnumConstant)
2482 c = ((EnumConstant)c).WidenToCompilerConstant ();
2484 if (c is IntConstant){
2485 int v = ((IntConstant) c).Value;
2487 if (target_type == TypeManager.uint32_type){
2490 } else if (target_type == TypeManager.char_type){
2491 if (v >= Char.MinValue && v <= Char.MaxValue)
2493 } else if (target_type == TypeManager.byte_type){
2494 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2496 } else if (target_type == TypeManager.sbyte_type){
2497 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2499 } else if (target_type == TypeManager.short_type){
2500 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2502 } else if (target_type == TypeManager.ushort_type){
2503 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2505 } else if (target_type == TypeManager.int64_type)
2507 else if (target_type == TypeManager.uint64_type){
2513 } else if (c is UIntConstant){
2514 uint v = ((UIntConstant) c).Value;
2516 if (target_type == TypeManager.int32_type){
2517 if (v <= Int32.MaxValue)
2519 } else if (target_type == TypeManager.char_type){
2520 if (v >= Char.MinValue && v <= Char.MaxValue)
2522 } else if (target_type == TypeManager.byte_type){
2523 if (v <= Byte.MaxValue)
2525 } else if (target_type == TypeManager.sbyte_type){
2526 if (v <= SByte.MaxValue)
2528 } else if (target_type == TypeManager.short_type){
2529 if (v <= UInt16.MaxValue)
2531 } else if (target_type == TypeManager.ushort_type){
2532 if (v <= UInt16.MaxValue)
2534 } else if (target_type == TypeManager.int64_type)
2536 else if (target_type == TypeManager.uint64_type)
2539 } else if (c is LongConstant){
2540 long v = ((LongConstant) c).Value;
2542 if (target_type == TypeManager.int32_type){
2543 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2545 } else if (target_type == TypeManager.uint32_type){
2546 if (v >= 0 && v <= UInt32.MaxValue)
2548 } else if (target_type == TypeManager.char_type){
2549 if (v >= Char.MinValue && v <= Char.MaxValue)
2551 } else if (target_type == TypeManager.byte_type){
2552 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2554 } else if (target_type == TypeManager.sbyte_type){
2555 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2557 } else if (target_type == TypeManager.short_type){
2558 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2560 } else if (target_type == TypeManager.ushort_type){
2561 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2563 } else if (target_type == TypeManager.uint64_type){
2568 } else if (c is ULongConstant){
2569 ulong v = ((ULongConstant) c).Value;
2571 if (target_type == TypeManager.int32_type){
2572 if (v <= Int32.MaxValue)
2574 } else if (target_type == TypeManager.uint32_type){
2575 if (v <= UInt32.MaxValue)
2577 } else if (target_type == TypeManager.char_type){
2578 if (v >= Char.MinValue && v <= Char.MaxValue)
2580 } else if (target_type == TypeManager.byte_type){
2581 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2583 } else if (target_type == TypeManager.sbyte_type){
2584 if (v <= (int) SByte.MaxValue)
2586 } else if (target_type == TypeManager.short_type){
2587 if (v <= UInt16.MaxValue)
2589 } else if (target_type == TypeManager.ushort_type){
2590 if (v <= UInt16.MaxValue)
2592 } else if (target_type == TypeManager.int64_type){
2593 if (v <= Int64.MaxValue)
2597 } else if (c is ByteConstant){
2598 byte v = ((ByteConstant) c).Value;
2600 if (target_type == TypeManager.int32_type)
2602 else if (target_type == TypeManager.uint32_type)
2604 else if (target_type == TypeManager.char_type)
2606 else if (target_type == TypeManager.sbyte_type){
2607 if (v <= SByte.MaxValue)
2609 } else if (target_type == TypeManager.short_type)
2611 else if (target_type == TypeManager.ushort_type)
2613 else if (target_type == TypeManager.int64_type)
2615 else if (target_type == TypeManager.uint64_type)
2618 } else if (c is SByteConstant){
2619 sbyte v = ((SByteConstant) c).Value;
2621 if (target_type == TypeManager.int32_type)
2623 else if (target_type == TypeManager.uint32_type){
2626 } else if (target_type == TypeManager.char_type){
2629 } else if (target_type == TypeManager.byte_type){
2632 } else if (target_type == TypeManager.short_type)
2634 else if (target_type == TypeManager.ushort_type){
2637 } else if (target_type == TypeManager.int64_type)
2639 else if (target_type == TypeManager.uint64_type){
2644 } else if (c is ShortConstant){
2645 short v = ((ShortConstant) c).Value;
2647 if (target_type == TypeManager.int32_type){
2649 } else if (target_type == TypeManager.uint32_type){
2652 } else if (target_type == TypeManager.char_type){
2655 } else if (target_type == TypeManager.byte_type){
2656 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2658 } else if (target_type == TypeManager.sbyte_type){
2659 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2661 } else if (target_type == TypeManager.ushort_type){
2664 } else if (target_type == TypeManager.int64_type)
2666 else if (target_type == TypeManager.uint64_type)
2670 } else if (c is UShortConstant){
2671 ushort v = ((UShortConstant) c).Value;
2673 if (target_type == TypeManager.int32_type)
2675 else if (target_type == TypeManager.uint32_type)
2677 else if (target_type == TypeManager.char_type){
2678 if (v >= Char.MinValue && v <= Char.MaxValue)
2680 } else if (target_type == TypeManager.byte_type){
2681 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2683 } else if (target_type == TypeManager.sbyte_type){
2684 if (v <= SByte.MaxValue)
2686 } else if (target_type == TypeManager.short_type){
2687 if (v <= Int16.MaxValue)
2689 } else if (target_type == TypeManager.int64_type)
2691 else if (target_type == TypeManager.uint64_type)
2695 } else if (c is CharConstant){
2696 char v = ((CharConstant) c).Value;
2698 if (target_type == TypeManager.int32_type)
2700 else if (target_type == TypeManager.uint32_type)
2702 else if (target_type == TypeManager.byte_type){
2703 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2705 } else if (target_type == TypeManager.sbyte_type){
2706 if (v <= SByte.MaxValue)
2708 } else if (target_type == TypeManager.short_type){
2709 if (v <= Int16.MaxValue)
2711 } else if (target_type == TypeManager.ushort_type)
2713 else if (target_type == TypeManager.int64_type)
2715 else if (target_type == TypeManager.uint64_type)
2720 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2725 // Load the object from the pointer.
2727 public static void LoadFromPtr (ILGenerator ig, Type t)
2729 if (t == TypeManager.int32_type)
2730 ig.Emit (OpCodes.Ldind_I4);
2731 else if (t == TypeManager.uint32_type)
2732 ig.Emit (OpCodes.Ldind_U4);
2733 else if (t == TypeManager.short_type)
2734 ig.Emit (OpCodes.Ldind_I2);
2735 else if (t == TypeManager.ushort_type)
2736 ig.Emit (OpCodes.Ldind_U2);
2737 else if (t == TypeManager.char_type)
2738 ig.Emit (OpCodes.Ldind_U2);
2739 else if (t == TypeManager.byte_type)
2740 ig.Emit (OpCodes.Ldind_U1);
2741 else if (t == TypeManager.sbyte_type)
2742 ig.Emit (OpCodes.Ldind_I1);
2743 else if (t == TypeManager.uint64_type)
2744 ig.Emit (OpCodes.Ldind_I8);
2745 else if (t == TypeManager.int64_type)
2746 ig.Emit (OpCodes.Ldind_I8);
2747 else if (t == TypeManager.float_type)
2748 ig.Emit (OpCodes.Ldind_R4);
2749 else if (t == TypeManager.double_type)
2750 ig.Emit (OpCodes.Ldind_R8);
2751 else if (t == TypeManager.bool_type)
2752 ig.Emit (OpCodes.Ldind_I1);
2753 else if (t == TypeManager.intptr_type)
2754 ig.Emit (OpCodes.Ldind_I);
2755 else if (TypeManager.IsEnumType (t)) {
2756 if (t == TypeManager.enum_type)
2757 ig.Emit (OpCodes.Ldind_Ref);
2759 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2760 } else if (t.IsValueType)
2761 ig.Emit (OpCodes.Ldobj, t);
2763 ig.Emit (OpCodes.Ldind_Ref);
2767 // The stack contains the pointer and the value of type `type'
2769 public static void StoreFromPtr (ILGenerator ig, Type type)
2771 if (TypeManager.IsEnumType (type))
2772 type = TypeManager.EnumToUnderlying (type);
2773 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2774 ig.Emit (OpCodes.Stind_I4);
2775 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2776 ig.Emit (OpCodes.Stind_I8);
2777 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2778 type == TypeManager.ushort_type)
2779 ig.Emit (OpCodes.Stind_I2);
2780 else if (type == TypeManager.float_type)
2781 ig.Emit (OpCodes.Stind_R4);
2782 else if (type == TypeManager.double_type)
2783 ig.Emit (OpCodes.Stind_R8);
2784 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2785 type == TypeManager.bool_type)
2786 ig.Emit (OpCodes.Stind_I1);
2787 else if (type == TypeManager.intptr_type)
2788 ig.Emit (OpCodes.Stind_I);
2789 else if (type.IsValueType)
2790 ig.Emit (OpCodes.Stobj, type);
2792 ig.Emit (OpCodes.Stind_Ref);
2796 // Returns the size of type `t' if known, otherwise, 0
2798 public static int GetTypeSize (Type t)
2800 t = TypeManager.TypeToCoreType (t);
2801 if (t == TypeManager.int32_type ||
2802 t == TypeManager.uint32_type ||
2803 t == TypeManager.float_type)
2805 else if (t == TypeManager.int64_type ||
2806 t == TypeManager.uint64_type ||
2807 t == TypeManager.double_type)
2809 else if (t == TypeManager.byte_type ||
2810 t == TypeManager.sbyte_type ||
2811 t == TypeManager.bool_type)
2813 else if (t == TypeManager.short_type ||
2814 t == TypeManager.char_type ||
2815 t == TypeManager.ushort_type)
2817 else if (t == TypeManager.decimal_type)
2824 // Default implementation of IAssignMethod.CacheTemporaries
2826 public void CacheTemporaries (EmitContext ec)
2830 static void Error_NegativeArrayIndex (Location loc)
2832 Report.Error (284, loc, "Can not create array with a negative size");
2836 // Converts `source' to an int, uint, long or ulong.
2838 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2842 bool old_checked = ec.CheckState;
2843 ec.CheckState = true;
2845 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2846 if (target == null){
2847 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2848 if (target == null){
2849 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2850 if (target == null){
2851 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2853 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2857 ec.CheckState = old_checked;
2860 // Only positive constants are allowed at compile time
2862 if (target is Constant){
2863 if (target is IntConstant){
2864 if (((IntConstant) target).Value < 0){
2865 Error_NegativeArrayIndex (loc);
2870 if (target is LongConstant){
2871 if (((LongConstant) target).Value < 0){
2872 Error_NegativeArrayIndex (loc);
2885 /// This is just a base class for expressions that can
2886 /// appear on statements (invocations, object creation,
2887 /// assignments, post/pre increment and decrement). The idea
2888 /// being that they would support an extra Emition interface that
2889 /// does not leave a result on the stack.
2891 public abstract class ExpressionStatement : Expression {
2894 /// Requests the expression to be emitted in a `statement'
2895 /// context. This means that no new value is left on the
2896 /// stack after invoking this method (constrasted with
2897 /// Emit that will always leave a value on the stack).
2899 public abstract void EmitStatement (EmitContext ec);
2903 /// This kind of cast is used to encapsulate the child
2904 /// whose type is child.Type into an expression that is
2905 /// reported to return "return_type". This is used to encapsulate
2906 /// expressions which have compatible types, but need to be dealt
2907 /// at higher levels with.
2909 /// For example, a "byte" expression could be encapsulated in one
2910 /// of these as an "unsigned int". The type for the expression
2911 /// would be "unsigned int".
2914 public class EmptyCast : Expression {
2915 protected Expression child;
2917 public EmptyCast (Expression child, Type return_type)
2919 eclass = child.eclass;
2924 public Expression Peel ()
2926 if (child is EmptyCast)
2927 return ((EmptyCast) child).Peel ();
2931 public override Expression DoResolve (EmitContext ec)
2933 // This should never be invoked, we are born in fully
2934 // initialized state.
2939 public override void Emit (EmitContext ec)
2946 /// This class is used to wrap literals which belong inside Enums
2948 public class EnumConstant : Constant {
2949 public Constant Child;
2951 public EnumConstant (Constant child, Type enum_type)
2953 eclass = child.eclass;
2958 public override Expression DoResolve (EmitContext ec)
2960 // This should never be invoked, we are born in fully
2961 // initialized state.
2966 public override void Emit (EmitContext ec)
2971 public override object GetValue ()
2973 return Child.GetValue ();
2977 // Converts from one of the valid underlying types for an enumeration
2978 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2979 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2981 public Constant WidenToCompilerConstant ()
2983 Type t = TypeManager.EnumToUnderlying (Child.Type);
2984 object v = ((Constant) Child).GetValue ();;
2986 if (t == TypeManager.int32_type)
2987 return new IntConstant ((int) v);
2988 if (t == TypeManager.uint32_type)
2989 return new UIntConstant ((uint) v);
2990 if (t == TypeManager.int64_type)
2991 return new LongConstant ((long) v);
2992 if (t == TypeManager.uint64_type)
2993 return new ULongConstant ((ulong) v);
2994 if (t == TypeManager.short_type)
2995 return new ShortConstant ((short) v);
2996 if (t == TypeManager.ushort_type)
2997 return new UShortConstant ((ushort) v);
2998 if (t == TypeManager.byte_type)
2999 return new ByteConstant ((byte) v);
3000 if (t == TypeManager.sbyte_type)
3001 return new SByteConstant ((sbyte) v);
3003 throw new Exception ("Invalid enumeration underlying type: " + t);
3007 // Extracts the value in the enumeration on its native representation
3009 public object GetPlainValue ()
3011 Type t = TypeManager.EnumToUnderlying (Child.Type);
3012 object v = ((Constant) Child).GetValue ();;
3014 if (t == TypeManager.int32_type)
3016 if (t == TypeManager.uint32_type)
3018 if (t == TypeManager.int64_type)
3020 if (t == TypeManager.uint64_type)
3022 if (t == TypeManager.short_type)
3024 if (t == TypeManager.ushort_type)
3026 if (t == TypeManager.byte_type)
3028 if (t == TypeManager.sbyte_type)
3034 public override string AsString ()
3036 return Child.AsString ();
3039 public override DoubleConstant ConvertToDouble ()
3041 return Child.ConvertToDouble ();
3044 public override FloatConstant ConvertToFloat ()
3046 return Child.ConvertToFloat ();
3049 public override ULongConstant ConvertToULong ()
3051 return Child.ConvertToULong ();
3054 public override LongConstant ConvertToLong ()
3056 return Child.ConvertToLong ();
3059 public override UIntConstant ConvertToUInt ()
3061 return Child.ConvertToUInt ();
3064 public override IntConstant ConvertToInt ()
3066 return Child.ConvertToInt ();
3071 /// This kind of cast is used to encapsulate Value Types in objects.
3073 /// The effect of it is to box the value type emitted by the previous
3076 public class BoxedCast : EmptyCast {
3078 public BoxedCast (Expression expr)
3079 : base (expr, TypeManager.object_type)
3083 public override Expression DoResolve (EmitContext ec)
3085 // This should never be invoked, we are born in fully
3086 // initialized state.
3091 public override void Emit (EmitContext ec)
3095 ec.ig.Emit (OpCodes.Box, child.Type);
3099 public class UnboxCast : EmptyCast {
3100 public UnboxCast (Expression expr, Type return_type)
3101 : base (expr, return_type)
3105 public override Expression DoResolve (EmitContext ec)
3107 // This should never be invoked, we are born in fully
3108 // initialized state.
3113 public override void Emit (EmitContext ec)
3116 ILGenerator ig = ec.ig;
3119 ig.Emit (OpCodes.Unbox, t);
3121 LoadFromPtr (ig, t);
3126 /// This is used to perform explicit numeric conversions.
3128 /// Explicit numeric conversions might trigger exceptions in a checked
3129 /// context, so they should generate the conv.ovf opcodes instead of
3132 public class ConvCast : EmptyCast {
3133 public enum Mode : byte {
3134 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3136 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3137 U2_I1, U2_U1, U2_I2, U2_CH,
3138 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3139 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3140 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3141 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3142 CH_I1, CH_U1, CH_I2,
3143 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3144 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3150 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3151 : base (child, return_type)
3153 checked_state = ec.CheckState;
3157 public override Expression DoResolve (EmitContext ec)
3159 // This should never be invoked, we are born in fully
3160 // initialized state.
3165 public override void Emit (EmitContext ec)
3167 ILGenerator ig = ec.ig;
3173 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3174 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3175 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3176 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3177 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3179 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3180 case Mode.U1_CH: /* nothing */ break;
3182 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3183 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3184 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3185 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3186 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3187 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3189 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3190 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3191 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3192 case Mode.U2_CH: /* nothing */ break;
3194 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3195 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3196 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3197 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3198 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3199 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3200 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3202 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3203 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3204 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3205 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3206 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3207 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3209 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3210 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3211 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3212 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3213 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3214 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3215 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3216 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3218 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3219 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3220 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3221 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3222 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3223 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3224 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3225 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3227 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3228 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3229 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3231 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3232 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3233 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3234 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3235 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3236 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3237 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3238 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3239 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3241 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3242 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3243 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3244 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3245 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3246 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3247 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3248 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3249 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3250 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3254 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3255 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3256 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3257 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3258 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3260 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3261 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3263 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3264 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3265 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3266 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3267 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3268 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3270 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3271 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3272 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3273 case Mode.U2_CH: /* nothing */ break;
3275 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3276 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3277 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3278 case Mode.I4_U4: /* nothing */ break;
3279 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3280 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3281 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3283 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3284 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3285 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3286 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3287 case Mode.U4_I4: /* nothing */ break;
3288 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3290 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3291 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3292 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3293 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3294 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3295 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3296 case Mode.I8_U8: /* nothing */ break;
3297 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3299 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3300 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3301 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3302 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3303 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3304 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3305 case Mode.U8_I8: /* nothing */ break;
3306 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3308 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3309 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3310 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3312 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3313 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3314 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3315 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3316 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3317 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3318 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3319 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3320 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3322 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3323 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3324 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3325 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3326 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3327 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3328 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3329 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3330 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3331 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3337 public class OpcodeCast : EmptyCast {
3341 public OpcodeCast (Expression child, Type return_type, OpCode op)
3342 : base (child, return_type)
3346 second_valid = false;
3349 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3350 : base (child, return_type)
3355 second_valid = true;
3358 public override Expression DoResolve (EmitContext ec)
3360 // This should never be invoked, we are born in fully
3361 // initialized state.
3366 public override void Emit (EmitContext ec)
3377 /// This kind of cast is used to encapsulate a child and cast it
3378 /// to the class requested
3380 public class ClassCast : EmptyCast {
3381 public ClassCast (Expression child, Type return_type)
3382 : base (child, return_type)
3387 public override Expression DoResolve (EmitContext ec)
3389 // This should never be invoked, we are born in fully
3390 // initialized state.
3395 public override void Emit (EmitContext ec)
3399 ec.ig.Emit (OpCodes.Castclass, type);
3405 /// SimpleName expressions are initially formed of a single
3406 /// word and it only happens at the beginning of the expression.
3410 /// The expression will try to be bound to a Field, a Method
3411 /// group or a Property. If those fail we pass the name to our
3412 /// caller and the SimpleName is compounded to perform a type
3413 /// lookup. The idea behind this process is that we want to avoid
3414 /// creating a namespace map from the assemblies, as that requires
3415 /// the GetExportedTypes function to be called and a hashtable to
3416 /// be constructed which reduces startup time. If later we find
3417 /// that this is slower, we should create a `NamespaceExpr' expression
3418 /// that fully participates in the resolution process.
3420 /// For example `System.Console.WriteLine' is decomposed into
3421 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3423 /// The first SimpleName wont produce a match on its own, so it will
3425 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3427 /// System.Console will produce a TypeExpr match.
3429 /// The downside of this is that we might be hitting `LookupType' too many
3430 /// times with this scheme.
3432 public class SimpleName : Expression, ITypeExpression {
3433 public readonly string Name;
3436 // If true, then we are a simple name, not composed with a ".
3440 public SimpleName (string a, string b, Location l)
3442 Name = String.Concat (a, ".", b);
3447 public SimpleName (string name, Location l)
3454 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3456 if (ec.IsFieldInitializer)
3459 "A field initializer cannot reference the non-static field, " +
3460 "method or property `"+name+"'");
3464 "An object reference is required " +
3465 "for the non-static field `"+name+"'");
3469 // Checks whether we are trying to access an instance
3470 // property, method or field from a static body.
3472 Expression MemberStaticCheck (EmitContext ec, Expression e)
3474 if (e is IMemberExpr){
3475 IMemberExpr member = (IMemberExpr) e;
3477 if (!member.IsStatic){
3478 Error_ObjectRefRequired (ec, loc, Name);
3486 public override Expression DoResolve (EmitContext ec)
3488 return SimpleNameResolve (ec, null, false);
3491 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3493 return SimpleNameResolve (ec, right_side, false);
3497 public Expression DoResolveAllowStatic (EmitContext ec)
3499 return SimpleNameResolve (ec, null, true);
3502 public Expression DoResolveType (EmitContext ec)
3504 DeclSpace ds = ec.DeclSpace;
3505 Namespace ns = ds.Namespace;
3510 // Since we are cheating: we only do the Alias lookup for
3511 // namespaces if the name does not include any dots in it
3513 if (ns != null && is_base)
3514 alias_value = ns.LookupAlias (Name);
3518 if (ec.ResolvingTypeTree){
3519 if (alias_value != null){
3520 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3521 return new TypeExpr (t, loc);
3524 int errors = Report.Errors;
3525 Type dt = ec.DeclSpace.FindType (loc, Name);
3526 if (Report.Errors != errors)
3530 return new TypeExpr (dt, loc);
3534 // First, the using aliases
3536 if (alias_value != null){
3537 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3538 return new TypeExpr (t, loc);
3540 // we have alias value, but it isn't Type, so try if it's namespace
3541 return new SimpleName (alias_value, loc);
3545 // Stage 2: Lookup up if we are an alias to a type
3549 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3550 return new TypeExpr (t, loc);
3552 // No match, maybe our parent can compose us
3553 // into something meaningful.
3558 /// 7.5.2: Simple Names.
3560 /// Local Variables and Parameters are handled at
3561 /// parse time, so they never occur as SimpleNames.
3563 /// The `allow_static' flag is used by MemberAccess only
3564 /// and it is used to inform us that it is ok for us to
3565 /// avoid the static check, because MemberAccess might end
3566 /// up resolving the Name as a Type name and the access as
3567 /// a static type access.
3569 /// ie: Type Type; .... { Type.GetType (""); }
3571 /// Type is both an instance variable and a Type; Type.GetType
3572 /// is the static method not an instance method of type.
3574 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3576 Expression e = null;
3579 // Stage 1: Performed by the parser (binding to locals or parameters).
3581 Block current_block = ec.CurrentBlock;
3582 if (current_block != null && current_block.GetVariableInfo (Name) != null){
3583 LocalVariableReference var;
3585 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3587 if (right_side != null)
3588 return var.ResolveLValue (ec, right_side);
3590 return var.Resolve (ec);
3593 if (current_block != null){
3595 Parameter par = null;
3596 Parameters pars = current_block.Parameters;
3598 par = pars.GetParameterByName (Name, out idx);
3601 ParameterReference param;
3603 param = new ParameterReference (pars, idx, Name, loc);
3605 if (right_side != null)
3606 return param.ResolveLValue (ec, right_side);
3608 return param.Resolve (ec);
3613 // Stage 2: Lookup members
3616 DeclSpace lookup_ds = ec.DeclSpace;
3618 if (lookup_ds.TypeBuilder == null)
3621 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3625 lookup_ds =lookup_ds.Parent;
3626 } while (lookup_ds != null);
3628 if (e == null && ec.ContainerType != null)
3629 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3632 return DoResolveType (ec);
3637 if (e is IMemberExpr) {
3638 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3642 IMemberExpr me = e as IMemberExpr;
3646 // This fails if ResolveMemberAccess() was unable to decide whether
3647 // it's a field or a type of the same name.
3648 if (!me.IsStatic && (me.InstanceExpression == null))
3652 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3653 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3654 "outer type `" + me.DeclaringType + "' via nested type `" +
3655 me.InstanceExpression.Type + "'");
3659 if (right_side != null)
3660 e = e.DoResolveLValue (ec, right_side);
3662 e = e.DoResolve (ec);
3667 if (ec.IsStatic || ec.IsFieldInitializer){
3671 return MemberStaticCheck (ec, e);
3676 public override void Emit (EmitContext ec)
3679 // If this is ever reached, then we failed to
3680 // find the name as a namespace
3683 Error (103, "The name `" + Name +
3684 "' does not exist in the class `" +
3685 ec.DeclSpace.Name + "'");
3688 public override string ToString ()
3695 /// Fully resolved expression that evaluates to a type
3697 public class TypeExpr : Expression, ITypeExpression {
3698 public TypeExpr (Type t, Location l)
3701 eclass = ExprClass.Type;
3705 public virtual Expression DoResolveType (EmitContext ec)
3710 override public Expression DoResolve (EmitContext ec)
3715 override public void Emit (EmitContext ec)
3717 throw new Exception ("Should never be called");
3720 public override string ToString ()
3722 return Type.ToString ();
3727 /// Used to create types from a fully qualified name. These are just used
3728 /// by the parser to setup the core types. A TypeLookupExpression is always
3729 /// classified as a type.
3731 public class TypeLookupExpression : TypeExpr {
3734 public TypeLookupExpression (string name) : base (null, Location.Null)
3739 public override Expression DoResolveType (EmitContext ec)
3742 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3746 public override Expression DoResolve (EmitContext ec)
3748 return DoResolveType (ec);
3751 public override void Emit (EmitContext ec)
3753 throw new Exception ("Should never be called");
3756 public override string ToString ()
3763 /// MethodGroup Expression.
3765 /// This is a fully resolved expression that evaluates to a type
3767 public class MethodGroupExpr : Expression, IMemberExpr {
3768 public MethodBase [] Methods;
3769 Expression instance_expression = null;
3770 bool is_explicit_impl = false;
3772 public MethodGroupExpr (MemberInfo [] mi, Location l)
3774 Methods = new MethodBase [mi.Length];
3775 mi.CopyTo (Methods, 0);
3776 eclass = ExprClass.MethodGroup;
3777 type = TypeManager.object_type;
3781 public MethodGroupExpr (ArrayList list, Location l)
3783 Methods = new MethodBase [list.Count];
3786 list.CopyTo (Methods, 0);
3788 foreach (MemberInfo m in list){
3789 if (!(m is MethodBase)){
3790 Console.WriteLine ("Name " + m.Name);
3791 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3797 eclass = ExprClass.MethodGroup;
3798 type = TypeManager.object_type;
3801 public Type DeclaringType {
3803 return Methods [0].DeclaringType;
3808 // `A method group may have associated an instance expression'
3810 public Expression InstanceExpression {
3812 return instance_expression;
3816 instance_expression = value;
3820 public bool IsExplicitImpl {
3822 return is_explicit_impl;
3826 is_explicit_impl = value;
3830 public string Name {
3832 return Methods [0].Name;
3836 public bool IsInstance {
3838 foreach (MethodBase mb in Methods)
3846 public bool IsStatic {
3848 foreach (MethodBase mb in Methods)
3856 override public Expression DoResolve (EmitContext ec)
3858 if (instance_expression != null) {
3859 instance_expression = instance_expression.DoResolve (ec);
3860 if (instance_expression == null)
3867 public void ReportUsageError ()
3869 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3870 Methods [0].Name + "()' is referenced without parentheses");
3873 override public void Emit (EmitContext ec)
3875 ReportUsageError ();
3878 bool RemoveMethods (bool keep_static)
3880 ArrayList smethods = new ArrayList ();
3882 foreach (MethodBase mb in Methods){
3883 if (mb.IsStatic == keep_static)
3887 if (smethods.Count == 0)
3890 Methods = new MethodBase [smethods.Count];
3891 smethods.CopyTo (Methods, 0);
3897 /// Removes any instance methods from the MethodGroup, returns
3898 /// false if the resulting set is empty.
3900 public bool RemoveInstanceMethods ()
3902 return RemoveMethods (true);
3906 /// Removes any static methods from the MethodGroup, returns
3907 /// false if the resulting set is empty.
3909 public bool RemoveStaticMethods ()
3911 return RemoveMethods (false);
3916 /// Fully resolved expression that evaluates to a Field
3918 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3919 public readonly FieldInfo FieldInfo;
3920 Expression instance_expr;
3922 public FieldExpr (FieldInfo fi, Location l)
3925 eclass = ExprClass.Variable;
3926 type = fi.FieldType;
3930 public string Name {
3932 return FieldInfo.Name;
3936 public bool IsInstance {
3938 return !FieldInfo.IsStatic;
3942 public bool IsStatic {
3944 return FieldInfo.IsStatic;
3948 public Type DeclaringType {
3950 return FieldInfo.DeclaringType;
3954 public Expression InstanceExpression {
3956 return instance_expr;
3960 instance_expr = value;
3964 override public Expression DoResolve (EmitContext ec)
3966 if (!FieldInfo.IsStatic){
3967 if (instance_expr == null){
3968 throw new Exception ("non-static FieldExpr without instance var\n" +
3969 "You have to assign the Instance variable\n" +
3970 "Of the FieldExpr to set this\n");
3973 // Resolve the field's instance expression while flow analysis is turned
3974 // off: when accessing a field "a.b", we must check whether the field
3975 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3976 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3977 ResolveFlags.DisableFlowAnalysis);
3978 if (instance_expr == null)
3982 // If the instance expression is a local variable or parameter.
3983 IVariable var = instance_expr as IVariable;
3984 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3990 void Report_AssignToReadonly (bool is_instance)
3995 msg = "Readonly field can not be assigned outside " +
3996 "of constructor or variable initializer";
3998 msg = "A static readonly field can only be assigned in " +
3999 "a static constructor";
4001 Report.Error (is_instance ? 191 : 198, loc, msg);
4004 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4006 IVariable var = instance_expr as IVariable;
4008 var.SetFieldAssigned (ec, FieldInfo.Name);
4010 Expression e = DoResolve (ec);
4015 if (!FieldInfo.IsInitOnly)
4019 // InitOnly fields can only be assigned in constructors
4022 if (ec.IsConstructor)
4025 Report_AssignToReadonly (true);
4030 override public void Emit (EmitContext ec)
4032 ILGenerator ig = ec.ig;
4033 bool is_volatile = false;
4035 if (FieldInfo is FieldBuilder){
4036 FieldBase f = TypeManager.GetField (FieldInfo);
4038 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4041 f.status |= Field.Status.USED;
4044 if (FieldInfo.IsStatic){
4046 ig.Emit (OpCodes.Volatile);
4048 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4050 if (instance_expr.Type.IsValueType){
4052 LocalTemporary tempo = null;
4054 if (!(instance_expr is IMemoryLocation)){
4055 tempo = new LocalTemporary (
4056 ec, instance_expr.Type);
4058 InstanceExpression.Emit (ec);
4062 ml = (IMemoryLocation) instance_expr;
4064 ml.AddressOf (ec, AddressOp.Load);
4066 instance_expr.Emit (ec);
4069 ig.Emit (OpCodes.Volatile);
4071 ig.Emit (OpCodes.Ldfld, FieldInfo);
4075 public void EmitAssign (EmitContext ec, Expression source)
4077 FieldAttributes fa = FieldInfo.Attributes;
4078 bool is_static = (fa & FieldAttributes.Static) != 0;
4079 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4080 ILGenerator ig = ec.ig;
4082 if (is_readonly && !ec.IsConstructor){
4083 Report_AssignToReadonly (!is_static);
4088 Expression instance = instance_expr;
4090 if (instance.Type.IsValueType){
4091 if (instance is IMemoryLocation){
4092 IMemoryLocation ml = (IMemoryLocation) instance;
4094 ml.AddressOf (ec, AddressOp.Store);
4096 throw new Exception ("The " + instance + " of type " +
4098 " represents a ValueType and does " +
4099 "not implement IMemoryLocation");
4105 if (FieldInfo is FieldBuilder){
4106 FieldBase f = TypeManager.GetField (FieldInfo);
4108 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4109 ig.Emit (OpCodes.Volatile);
4113 ig.Emit (OpCodes.Stsfld, FieldInfo);
4115 ig.Emit (OpCodes.Stfld, FieldInfo);
4117 if (FieldInfo is FieldBuilder){
4118 FieldBase f = TypeManager.GetField (FieldInfo);
4120 f.status |= Field.Status.ASSIGNED;
4124 public void AddressOf (EmitContext ec, AddressOp mode)
4126 ILGenerator ig = ec.ig;
4128 if (FieldInfo is FieldBuilder){
4129 FieldBase f = TypeManager.GetField (FieldInfo);
4130 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4131 ig.Emit (OpCodes.Volatile);
4134 if (FieldInfo is FieldBuilder){
4135 FieldBase f = TypeManager.GetField (FieldInfo);
4137 if ((mode & AddressOp.Store) != 0)
4138 f.status |= Field.Status.ASSIGNED;
4139 if ((mode & AddressOp.Load) != 0)
4140 f.status |= Field.Status.USED;
4144 // Handle initonly fields specially: make a copy and then
4145 // get the address of the copy.
4147 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4151 local = ig.DeclareLocal (type);
4152 ig.Emit (OpCodes.Stloc, local);
4153 ig.Emit (OpCodes.Ldloca, local);
4157 if (FieldInfo.IsStatic)
4158 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4161 if (instance_expr is IMemoryLocation)
4162 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4165 instance_expr.Emit (ec);
4166 ig.Emit (OpCodes.Ldflda, FieldInfo);
4172 /// Expression that evaluates to a Property. The Assign class
4173 /// might set the `Value' expression if we are in an assignment.
4175 /// This is not an LValue because we need to re-write the expression, we
4176 /// can not take data from the stack and store it.
4178 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4179 public readonly PropertyInfo PropertyInfo;
4182 // This is set externally by the `BaseAccess' class
4185 MethodInfo getter, setter;
4187 bool must_do_cs1540_check;
4189 Expression instance_expr;
4191 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4194 eclass = ExprClass.PropertyAccess;
4198 type = TypeManager.TypeToCoreType (pi.PropertyType);
4200 ResolveAccessors (ec);
4203 public string Name {
4205 return PropertyInfo.Name;
4209 public bool IsInstance {
4215 public bool IsStatic {
4221 public Type DeclaringType {
4223 return PropertyInfo.DeclaringType;
4228 // The instance expression associated with this expression
4230 public Expression InstanceExpression {
4232 instance_expr = value;
4236 return instance_expr;
4240 public bool VerifyAssignable ()
4242 if (setter == null) {
4243 Report.Error (200, loc,
4244 "The property `" + PropertyInfo.Name +
4245 "' can not be assigned to, as it has not set accessor");
4252 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4254 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4255 BindingFlags.Static | BindingFlags.Instance;
4258 group = TypeManager.MemberLookup (
4259 invocation_type, invocation_type, PropertyInfo.DeclaringType,
4260 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4263 // The first method is the closest to us
4268 foreach (MethodInfo mi in group) {
4269 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4272 // If only accessible to the current class or children
4274 if (ma == MethodAttributes.Private) {
4275 Type declaring_type = mi.DeclaringType;
4277 if (invocation_type != declaring_type){
4278 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4286 // FamAndAssem requires that we not only derivate, but we are on the
4289 if (ma == MethodAttributes.FamANDAssem){
4290 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4296 // Assembly and FamORAssem succeed if we're in the same assembly.
4297 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4298 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4304 // We already know that we aren't in the same assembly.
4305 if (ma == MethodAttributes.Assembly)
4308 // Family and FamANDAssem require that we derive.
4309 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4310 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4313 must_do_cs1540_check = true;
4326 // We also perform the permission checking here, as the PropertyInfo does not
4327 // hold the information for the accessibility of its setter/getter
4329 void ResolveAccessors (EmitContext ec)
4331 getter = GetAccessor (ec.ContainerType, "get");
4332 if ((getter != null) && getter.IsStatic)
4335 setter = GetAccessor (ec.ContainerType, "set");
4336 if ((setter != null) && setter.IsStatic)
4339 if (setter == null && getter == null){
4340 Error (122, "`" + PropertyInfo.Name + "' " +
4341 "is inaccessible because of its protection level");
4346 bool InstanceResolve (EmitContext ec)
4348 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4349 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4353 if (instance_expr != null) {
4354 instance_expr = instance_expr.DoResolve (ec);
4355 if (instance_expr == null)
4359 if (must_do_cs1540_check && (instance_expr != null)) {
4360 if ((instance_expr.Type != ec.ContainerType) &&
4361 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4362 Report.Error (1540, loc, "Cannot access protected member `" +
4363 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4364 "' via a qualifier of type `" +
4365 TypeManager.CSharpName (instance_expr.Type) +
4366 "'; the qualifier must be of type `" +
4367 TypeManager.CSharpName (ec.ContainerType) +
4368 "' (or derived from it)");
4376 override public Expression DoResolve (EmitContext ec)
4378 if (getter == null){
4380 // The following condition happens if the PropertyExpr was
4381 // created, but is invalid (ie, the property is inaccessible),
4382 // and we did not want to embed the knowledge about this in
4383 // the caller routine. This only avoids double error reporting.
4388 Report.Error (154, loc,
4389 "The property `" + PropertyInfo.Name +
4390 "' can not be used in " +
4391 "this context because it lacks a get accessor");
4395 if (!InstanceResolve (ec))
4399 // Only base will allow this invocation to happen.
4401 if (IsBase && getter.IsAbstract){
4402 Report.Error (205, loc, "Cannot call an abstract base property: " +
4403 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4410 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4412 if (setter == null){
4414 // The following condition happens if the PropertyExpr was
4415 // created, but is invalid (ie, the property is inaccessible),
4416 // and we did not want to embed the knowledge about this in
4417 // the caller routine. This only avoids double error reporting.
4422 Report.Error (154, loc,
4423 "The property `" + PropertyInfo.Name +
4424 "' can not be used in " +
4425 "this context because it lacks a set accessor");
4429 if (!InstanceResolve (ec))
4433 // Only base will allow this invocation to happen.
4435 if (IsBase && setter.IsAbstract){
4436 Report.Error (205, loc, "Cannot call an abstract base property: " +
4437 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4443 override public void Emit (EmitContext ec)
4446 // Special case: length of single dimension array property is turned into ldlen
4448 if ((getter == TypeManager.system_int_array_get_length) ||
4449 (getter == TypeManager.int_array_get_length)){
4450 Type iet = instance_expr.Type;
4453 // System.Array.Length can be called, but the Type does not
4454 // support invoking GetArrayRank, so test for that case first
4456 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4457 instance_expr.Emit (ec);
4458 ec.ig.Emit (OpCodes.Ldlen);
4463 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4468 // Implements the IAssignMethod interface for assignments
4470 public void EmitAssign (EmitContext ec, Expression source)
4472 Argument arg = new Argument (source, Argument.AType.Expression);
4473 ArrayList args = new ArrayList ();
4476 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4479 override public void EmitStatement (EmitContext ec)
4482 ec.ig.Emit (OpCodes.Pop);
4487 /// Fully resolved expression that evaluates to an Event
4489 public class EventExpr : Expression, IMemberExpr {
4490 public readonly EventInfo EventInfo;
4491 public Expression instance_expr;
4494 MethodInfo add_accessor, remove_accessor;
4496 public EventExpr (EventInfo ei, Location loc)
4500 eclass = ExprClass.EventAccess;
4502 add_accessor = TypeManager.GetAddMethod (ei);
4503 remove_accessor = TypeManager.GetRemoveMethod (ei);
4505 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4508 if (EventInfo is MyEventBuilder){
4509 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4510 type = eb.EventType;
4513 type = EventInfo.EventHandlerType;
4516 public string Name {
4518 return EventInfo.Name;
4522 public bool IsInstance {
4528 public bool IsStatic {
4534 public Type DeclaringType {
4536 return EventInfo.DeclaringType;
4540 public Expression InstanceExpression {
4542 return instance_expr;
4546 instance_expr = value;
4550 public override Expression DoResolve (EmitContext ec)
4552 if (instance_expr != null) {
4553 instance_expr = instance_expr.DoResolve (ec);
4554 if (instance_expr == null)
4562 public override void Emit (EmitContext ec)
4564 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4567 public void EmitAddOrRemove (EmitContext ec, Expression source)
4569 Expression handler = ((Binary) source).Right;
4571 Argument arg = new Argument (handler, Argument.AType.Expression);
4572 ArrayList args = new ArrayList ();
4576 if (((Binary) source).Oper == Binary.Operator.Addition)
4577 Invocation.EmitCall (
4578 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4580 Invocation.EmitCall (
4581 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);