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)
1609 foreach (MethodBase mb in union.Methods){
1610 ParameterData pd = Invocation.GetParameterData (mb);
1611 MethodInfo mi = (MethodInfo) mb;
1613 if (pd.ParameterType (0) == most_specific_source &&
1614 mi.ReturnType == most_specific_target) {
1620 if (method == null || count > 1)
1625 // This will do the conversion to the best match that we
1626 // found. Now we need to perform an implict standard conversion
1627 // if the best match was not the type that we were requested
1630 if (look_for_explicit)
1631 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1633 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1639 e = new UserCast ((MethodInfo) method, source, loc);
1640 if (e.Type != target){
1641 if (!look_for_explicit)
1642 e = ConvertImplicitStandard (ec, e, target, loc);
1644 e = ConvertExplicitStandard (ec, e, target, loc);
1651 /// Converts implicitly the resolved expression `expr' into the
1652 /// `target_type'. It returns a new expression that can be used
1653 /// in a context that expects a `target_type'.
1655 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1656 Type target_type, Location loc)
1658 Type expr_type = expr.Type;
1661 if (expr_type == target_type)
1664 if (target_type == null)
1665 throw new Exception ("Target type is null");
1667 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1671 e = ImplicitUserConversion (ec, expr, target_type, loc);
1680 /// Attempts to apply the `Standard Implicit
1681 /// Conversion' rules to the expression `expr' into
1682 /// the `target_type'. It returns a new expression
1683 /// that can be used in a context that expects a
1686 /// This is different from `ConvertImplicit' in that the
1687 /// user defined implicit conversions are excluded.
1689 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1690 Type target_type, Location loc)
1692 Type expr_type = expr.Type;
1695 if (expr_type == target_type)
1698 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1702 e = ImplicitReferenceConversion (expr, target_type);
1706 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1707 IntLiteral i = (IntLiteral) expr;
1710 return new EmptyCast (expr, target_type);
1714 if (expr_type.IsPointer){
1715 if (target_type == TypeManager.void_ptr_type)
1716 return new EmptyCast (expr, target_type);
1719 // yep, comparing pointer types cant be done with
1720 // t1 == t2, we have to compare their element types.
1722 if (target_type.IsPointer){
1723 if (target_type.GetElementType() == expr_type.GetElementType())
1728 if (target_type.IsPointer) {
1729 if (expr is NullLiteral)
1730 return new EmptyCast (expr, target_type);
1732 if (expr_type == TypeManager.void_ptr_type)
1733 return new EmptyCast (expr, target_type);
1741 /// Attemps to perform an implict constant conversion of the IntConstant
1742 /// into a different data type using casts (See Implicit Constant
1743 /// Expression Conversions)
1745 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1747 int value = ic.Value;
1750 // FIXME: This could return constants instead of EmptyCasts
1752 if (target_type == TypeManager.sbyte_type){
1753 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1754 return new SByteConstant ((sbyte) value);
1755 } else if (target_type == TypeManager.byte_type){
1756 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1757 return new ByteConstant ((byte) value);
1758 } else if (target_type == TypeManager.short_type){
1759 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1760 return new ShortConstant ((short) value);
1761 } else if (target_type == TypeManager.ushort_type){
1762 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1763 return new UShortConstant ((ushort) value);
1764 } else if (target_type == TypeManager.uint32_type){
1766 return new UIntConstant ((uint) value);
1767 } else if (target_type == TypeManager.uint64_type){
1769 // we can optimize this case: a positive int32
1770 // always fits on a uint64. But we need an opcode
1774 return new ULongConstant ((ulong) value);
1775 } else if (target_type == TypeManager.double_type)
1776 return new DoubleConstant ((double) value);
1777 else if (target_type == TypeManager.float_type)
1778 return new FloatConstant ((float) value);
1780 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1781 Type underlying = TypeManager.EnumToUnderlying (target_type);
1782 Constant e = (Constant) ic;
1785 // Possibly, we need to create a different 0 literal before passing
1788 if (underlying == TypeManager.int64_type)
1789 e = new LongLiteral (0);
1790 else if (underlying == TypeManager.uint64_type)
1791 e = new ULongLiteral (0);
1793 return new EnumConstant (e, target_type);
1798 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1800 string msg = "Cannot convert implicitly from `"+
1801 TypeManager.CSharpName (source) + "' to `" +
1802 TypeManager.CSharpName (target) + "'";
1804 Report.Error (29, loc, msg);
1808 /// Attemptes to implicityly convert `target' into `type', using
1809 /// ConvertImplicit. If there is no implicit conversion, then
1810 /// an error is signaled
1812 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1813 Type target_type, Location loc)
1817 e = ConvertImplicit (ec, source, target_type, loc);
1821 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1822 Report.Error (664, loc,
1823 "Double literal cannot be implicitly converted to " +
1824 "float type, use F suffix to create a float literal");
1827 Error_CannotConvertImplicit (loc, source.Type, target_type);
1833 /// Performs the explicit numeric conversions
1835 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1837 Type expr_type = expr.Type;
1840 // If we have an enumeration, extract the underlying type,
1841 // use this during the comparison, but wrap around the original
1844 Type real_target_type = target_type;
1846 if (TypeManager.IsEnumType (real_target_type))
1847 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1849 if (StandardConversionExists (expr, real_target_type)){
1850 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1852 if (real_target_type != target_type)
1853 return new EmptyCast (ce, target_type);
1857 if (expr_type == TypeManager.sbyte_type){
1859 // From sbyte to byte, ushort, uint, ulong, char
1861 if (real_target_type == TypeManager.byte_type)
1862 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1863 if (real_target_type == TypeManager.ushort_type)
1864 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1865 if (real_target_type == TypeManager.uint32_type)
1866 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1867 if (real_target_type == TypeManager.uint64_type)
1868 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1869 if (real_target_type == TypeManager.char_type)
1870 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1871 } else if (expr_type == TypeManager.byte_type){
1873 // From byte to sbyte and char
1875 if (real_target_type == TypeManager.sbyte_type)
1876 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1877 if (real_target_type == TypeManager.char_type)
1878 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1879 } else if (expr_type == TypeManager.short_type){
1881 // From short to sbyte, byte, ushort, uint, ulong, char
1883 if (real_target_type == TypeManager.sbyte_type)
1884 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1885 if (real_target_type == TypeManager.byte_type)
1886 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1887 if (real_target_type == TypeManager.ushort_type)
1888 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1889 if (real_target_type == TypeManager.uint32_type)
1890 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1891 if (real_target_type == TypeManager.uint64_type)
1892 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1893 if (real_target_type == TypeManager.char_type)
1894 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1895 } else if (expr_type == TypeManager.ushort_type){
1897 // From ushort to sbyte, byte, short, char
1899 if (real_target_type == TypeManager.sbyte_type)
1900 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1901 if (real_target_type == TypeManager.byte_type)
1902 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1903 if (real_target_type == TypeManager.short_type)
1904 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1905 if (real_target_type == TypeManager.char_type)
1906 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1907 } else if (expr_type == TypeManager.int32_type){
1909 // From int to sbyte, byte, short, ushort, uint, ulong, char
1911 if (real_target_type == TypeManager.sbyte_type)
1912 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1913 if (real_target_type == TypeManager.byte_type)
1914 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1915 if (real_target_type == TypeManager.short_type)
1916 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1917 if (real_target_type == TypeManager.ushort_type)
1918 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1919 if (real_target_type == TypeManager.uint32_type)
1920 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1921 if (real_target_type == TypeManager.uint64_type)
1922 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1923 if (real_target_type == TypeManager.char_type)
1924 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1925 } else if (expr_type == TypeManager.uint32_type){
1927 // From uint to sbyte, byte, short, ushort, int, char
1929 if (real_target_type == TypeManager.sbyte_type)
1930 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1931 if (real_target_type == TypeManager.byte_type)
1932 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1933 if (real_target_type == TypeManager.short_type)
1934 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1935 if (real_target_type == TypeManager.ushort_type)
1936 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1937 if (real_target_type == TypeManager.int32_type)
1938 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1939 if (real_target_type == TypeManager.char_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1941 } else if (expr_type == TypeManager.int64_type){
1943 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1945 if (real_target_type == TypeManager.sbyte_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1947 if (real_target_type == TypeManager.byte_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1949 if (real_target_type == TypeManager.short_type)
1950 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1951 if (real_target_type == TypeManager.ushort_type)
1952 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1953 if (real_target_type == TypeManager.int32_type)
1954 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1955 if (real_target_type == TypeManager.uint32_type)
1956 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1957 if (real_target_type == TypeManager.uint64_type)
1958 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1959 if (real_target_type == TypeManager.char_type)
1960 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1961 } else if (expr_type == TypeManager.uint64_type){
1963 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1965 if (real_target_type == TypeManager.sbyte_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1967 if (real_target_type == TypeManager.byte_type)
1968 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1969 if (real_target_type == TypeManager.short_type)
1970 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1971 if (real_target_type == TypeManager.ushort_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1973 if (real_target_type == TypeManager.int32_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1975 if (real_target_type == TypeManager.uint32_type)
1976 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1977 if (real_target_type == TypeManager.int64_type)
1978 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1979 if (real_target_type == TypeManager.char_type)
1980 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1981 } else if (expr_type == TypeManager.char_type){
1983 // From char to sbyte, byte, short
1985 if (real_target_type == TypeManager.sbyte_type)
1986 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1987 if (real_target_type == TypeManager.byte_type)
1988 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1989 if (real_target_type == TypeManager.short_type)
1990 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1991 } else if (expr_type == TypeManager.float_type){
1993 // From float to sbyte, byte, short,
1994 // ushort, int, uint, long, ulong, char
1997 if (real_target_type == TypeManager.sbyte_type)
1998 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1999 if (real_target_type == TypeManager.byte_type)
2000 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2001 if (real_target_type == TypeManager.short_type)
2002 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2003 if (real_target_type == TypeManager.ushort_type)
2004 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2005 if (real_target_type == TypeManager.int32_type)
2006 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2007 if (real_target_type == TypeManager.uint32_type)
2008 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2009 if (real_target_type == TypeManager.int64_type)
2010 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2011 if (real_target_type == TypeManager.uint64_type)
2012 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2013 if (real_target_type == TypeManager.char_type)
2014 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2015 } else if (expr_type == TypeManager.double_type){
2017 // From double to byte, byte, short,
2018 // ushort, int, uint, long, ulong,
2019 // char, float or decimal
2021 if (real_target_type == TypeManager.sbyte_type)
2022 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2023 if (real_target_type == TypeManager.byte_type)
2024 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2025 if (real_target_type == TypeManager.short_type)
2026 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2027 if (real_target_type == TypeManager.ushort_type)
2028 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2029 if (real_target_type == TypeManager.int32_type)
2030 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2031 if (real_target_type == TypeManager.uint32_type)
2032 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2033 if (real_target_type == TypeManager.int64_type)
2034 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2035 if (real_target_type == TypeManager.uint64_type)
2036 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2037 if (real_target_type == TypeManager.char_type)
2038 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2039 if (real_target_type == TypeManager.float_type)
2040 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2043 // decimal is taken care of by the op_Explicit methods.
2049 /// Returns whether an explicit reference conversion can be performed
2050 /// from source_type to target_type
2052 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2054 bool target_is_value_type = target_type.IsValueType;
2056 if (source_type == target_type)
2060 // From object to any reference type
2062 if (source_type == TypeManager.object_type && !target_is_value_type)
2066 // From any class S to any class-type T, provided S is a base class of T
2068 if (target_type.IsSubclassOf (source_type))
2072 // From any interface type S to any interface T provided S is not derived from T
2074 if (source_type.IsInterface && target_type.IsInterface){
2075 if (!target_type.IsSubclassOf (source_type))
2080 // From any class type S to any interface T, provided S is not sealed
2081 // and provided S does not implement T.
2083 if (target_type.IsInterface && !source_type.IsSealed &&
2084 !TypeManager.ImplementsInterface (source_type, target_type))
2088 // From any interface-type S to to any class type T, provided T is not
2089 // sealed, or provided T implements S.
2091 if (source_type.IsInterface &&
2092 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2096 // From an array type S with an element type Se to an array type T with an
2097 // element type Te provided all the following are true:
2098 // * S and T differe only in element type, in other words, S and T
2099 // have the same number of dimensions.
2100 // * Both Se and Te are reference types
2101 // * An explicit referenc conversions exist from Se to Te
2103 if (source_type.IsArray && target_type.IsArray) {
2104 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2106 Type source_element_type = source_type.GetElementType ();
2107 Type target_element_type = target_type.GetElementType ();
2109 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2110 if (ExplicitReferenceConversionExists (source_element_type,
2111 target_element_type))
2117 // From System.Array to any array-type
2118 if (source_type == TypeManager.array_type &&
2119 target_type.IsArray){
2124 // From System delegate to any delegate-type
2126 if (source_type == TypeManager.delegate_type &&
2127 target_type.IsSubclassOf (TypeManager.delegate_type))
2131 // From ICloneable to Array or Delegate types
2133 if (source_type == TypeManager.icloneable_type &&
2134 (target_type == TypeManager.array_type ||
2135 target_type == TypeManager.delegate_type))
2142 /// Implements Explicit Reference conversions
2144 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2146 Type source_type = source.Type;
2147 bool target_is_value_type = target_type.IsValueType;
2150 // From object to any reference type
2152 if (source_type == TypeManager.object_type && !target_is_value_type)
2153 return new ClassCast (source, target_type);
2157 // From any class S to any class-type T, provided S is a base class of T
2159 if (target_type.IsSubclassOf (source_type))
2160 return new ClassCast (source, target_type);
2163 // From any interface type S to any interface T provided S is not derived from T
2165 if (source_type.IsInterface && target_type.IsInterface){
2166 if (TypeManager.ImplementsInterface (source_type, target_type))
2169 return new ClassCast (source, target_type);
2173 // From any class type S to any interface T, provides S is not sealed
2174 // and provided S does not implement T.
2176 if (target_type.IsInterface && !source_type.IsSealed) {
2177 if (TypeManager.ImplementsInterface (source_type, target_type))
2180 return new ClassCast (source, target_type);
2185 // From any interface-type S to to any class type T, provided T is not
2186 // sealed, or provided T implements S.
2188 if (source_type.IsInterface) {
2189 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2190 return new ClassCast (source, target_type);
2195 // From an array type S with an element type Se to an array type T with an
2196 // element type Te provided all the following are true:
2197 // * S and T differe only in element type, in other words, S and T
2198 // have the same number of dimensions.
2199 // * Both Se and Te are reference types
2200 // * An explicit referenc conversions exist from Se to Te
2202 if (source_type.IsArray && target_type.IsArray) {
2203 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2205 Type source_element_type = source_type.GetElementType ();
2206 Type target_element_type = target_type.GetElementType ();
2208 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2209 if (ExplicitReferenceConversionExists (source_element_type,
2210 target_element_type))
2211 return new ClassCast (source, target_type);
2216 // From System.Array to any array-type
2217 if (source_type == TypeManager.array_type &&
2218 target_type.IsArray) {
2219 return new ClassCast (source, target_type);
2223 // From System delegate to any delegate-type
2225 if (source_type == TypeManager.delegate_type &&
2226 target_type.IsSubclassOf (TypeManager.delegate_type))
2227 return new ClassCast (source, target_type);
2230 // From ICloneable to Array or Delegate types
2232 if (source_type == TypeManager.icloneable_type &&
2233 (target_type == TypeManager.array_type ||
2234 target_type == TypeManager.delegate_type))
2235 return new ClassCast (source, target_type);
2241 /// Performs an explicit conversion of the expression `expr' whose
2242 /// type is expr.Type to `target_type'.
2244 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2245 Type target_type, Location loc)
2247 Type expr_type = expr.Type;
2248 Type original_expr_type = expr_type;
2250 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
2251 if (target_type == TypeManager.enum_type ||
2252 target_type == TypeManager.object_type) {
2253 if (expr is EnumConstant)
2254 expr = ((EnumConstant) expr).Child;
2255 // We really need all these casts here .... :-(
2256 expr = new BoxedCast (new EmptyCast (expr, expr_type));
2257 return new EmptyCast (expr, target_type);
2258 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
2259 target_type.IsSubclassOf (TypeManager.enum_type))
2260 return new UnboxCast (expr, target_type);
2263 // Notice that we have kept the expr_type unmodified, which is only
2265 if (expr is EnumConstant)
2266 expr = ((EnumConstant) expr).Child;
2268 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2269 expr_type = expr.Type;
2272 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2277 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2282 // Unboxing conversion.
2284 if (expr_type == TypeManager.object_type && target_type.IsValueType){
2285 if (expr is NullLiteral){
2286 Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
2289 return new UnboxCast (expr, target_type);
2293 ne = ConvertReferenceExplicit (expr, target_type);
2298 if (target_type.IsPointer){
2299 if (expr_type.IsPointer)
2300 return new EmptyCast (expr, target_type);
2302 if (expr_type == TypeManager.sbyte_type ||
2303 expr_type == TypeManager.byte_type ||
2304 expr_type == TypeManager.short_type ||
2305 expr_type == TypeManager.ushort_type ||
2306 expr_type == TypeManager.int32_type ||
2307 expr_type == TypeManager.uint32_type ||
2308 expr_type == TypeManager.uint64_type ||
2309 expr_type == TypeManager.int64_type)
2310 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2312 if (expr_type.IsPointer){
2313 if (target_type == TypeManager.sbyte_type ||
2314 target_type == TypeManager.byte_type ||
2315 target_type == TypeManager.short_type ||
2316 target_type == TypeManager.ushort_type ||
2317 target_type == TypeManager.int32_type ||
2318 target_type == TypeManager.uint32_type ||
2319 target_type == TypeManager.uint64_type ||
2320 target_type == TypeManager.int64_type){
2321 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2324 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2329 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2333 // We should always be able to go from an uint32
2334 // implicitly or explicitly to the other integral
2337 throw new Exception ("Internal compiler error");
2342 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2346 Error_CannotConvertType (loc, original_expr_type, target_type);
2351 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2353 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2354 Type target_type, Location l)
2356 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2361 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2365 ne = ConvertReferenceExplicit (expr, target_type);
2369 Error_CannotConvertType (l, expr.Type, target_type);
2373 static string ExprClassName (ExprClass c)
2376 case ExprClass.Invalid:
2378 case ExprClass.Value:
2380 case ExprClass.Variable:
2382 case ExprClass.Namespace:
2384 case ExprClass.Type:
2386 case ExprClass.MethodGroup:
2387 return "method group";
2388 case ExprClass.PropertyAccess:
2389 return "property access";
2390 case ExprClass.EventAccess:
2391 return "event access";
2392 case ExprClass.IndexerAccess:
2393 return "indexer access";
2394 case ExprClass.Nothing:
2397 throw new Exception ("Should not happen");
2401 /// Reports that we were expecting `expr' to be of class `expected'
2403 public void Error118 (string expected)
2405 string kind = "Unknown";
2407 kind = ExprClassName (eclass);
2409 Error (118, "Expression denotes a `" + kind +
2410 "' where a `" + expected + "' was expected");
2413 public void Error118 (ResolveFlags flags)
2415 ArrayList valid = new ArrayList (10);
2417 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2418 valid.Add ("variable");
2419 valid.Add ("value");
2422 if ((flags & ResolveFlags.Type) != 0)
2425 if ((flags & ResolveFlags.MethodGroup) != 0)
2426 valid.Add ("method group");
2428 if ((flags & ResolveFlags.SimpleName) != 0)
2429 valid.Add ("simple name");
2431 if (valid.Count == 0)
2432 valid.Add ("unknown");
2434 StringBuilder sb = new StringBuilder ();
2435 for (int i = 0; i < valid.Count; i++) {
2438 else if (i == valid.Count)
2440 sb.Append (valid [i]);
2443 string kind = ExprClassName (eclass);
2445 Error (119, "Expression denotes a `" + kind + "' where " +
2446 "a `" + sb.ToString () + "' was expected");
2449 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2451 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2452 TypeManager.CSharpName (t));
2455 public static void UnsafeError (Location loc)
2457 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2461 /// Converts the IntConstant, UIntConstant, LongConstant or
2462 /// ULongConstant into the integral target_type. Notice
2463 /// that we do not return an `Expression' we do return
2464 /// a boxed integral type.
2466 /// FIXME: Since I added the new constants, we need to
2467 /// also support conversions from CharConstant, ByteConstant,
2468 /// SByteConstant, UShortConstant, ShortConstant
2470 /// This is used by the switch statement, so the domain
2471 /// of work is restricted to the literals above, and the
2472 /// targets are int32, uint32, char, byte, sbyte, ushort,
2473 /// short, uint64 and int64
2475 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2479 if (c.Type == target_type)
2480 return ((Constant) c).GetValue ();
2483 // Make into one of the literals we handle, we dont really care
2484 // about this value as we will just return a few limited types
2486 if (c is EnumConstant)
2487 c = ((EnumConstant)c).WidenToCompilerConstant ();
2489 if (c is IntConstant){
2490 int v = ((IntConstant) c).Value;
2492 if (target_type == TypeManager.uint32_type){
2495 } else if (target_type == TypeManager.char_type){
2496 if (v >= Char.MinValue && v <= Char.MaxValue)
2498 } else if (target_type == TypeManager.byte_type){
2499 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2501 } else if (target_type == TypeManager.sbyte_type){
2502 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2504 } else if (target_type == TypeManager.short_type){
2505 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2507 } else if (target_type == TypeManager.ushort_type){
2508 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2510 } else if (target_type == TypeManager.int64_type)
2512 else if (target_type == TypeManager.uint64_type){
2518 } else if (c is UIntConstant){
2519 uint v = ((UIntConstant) c).Value;
2521 if (target_type == TypeManager.int32_type){
2522 if (v <= Int32.MaxValue)
2524 } else if (target_type == TypeManager.char_type){
2525 if (v >= Char.MinValue && v <= Char.MaxValue)
2527 } else if (target_type == TypeManager.byte_type){
2528 if (v <= Byte.MaxValue)
2530 } else if (target_type == TypeManager.sbyte_type){
2531 if (v <= SByte.MaxValue)
2533 } else if (target_type == TypeManager.short_type){
2534 if (v <= UInt16.MaxValue)
2536 } else if (target_type == TypeManager.ushort_type){
2537 if (v <= UInt16.MaxValue)
2539 } else if (target_type == TypeManager.int64_type)
2541 else if (target_type == TypeManager.uint64_type)
2544 } else if (c is LongConstant){
2545 long v = ((LongConstant) c).Value;
2547 if (target_type == TypeManager.int32_type){
2548 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2550 } else if (target_type == TypeManager.uint32_type){
2551 if (v >= 0 && v <= UInt32.MaxValue)
2553 } else if (target_type == TypeManager.char_type){
2554 if (v >= Char.MinValue && v <= Char.MaxValue)
2556 } else if (target_type == TypeManager.byte_type){
2557 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2559 } else if (target_type == TypeManager.sbyte_type){
2560 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2562 } else if (target_type == TypeManager.short_type){
2563 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2565 } else if (target_type == TypeManager.ushort_type){
2566 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2568 } else if (target_type == TypeManager.uint64_type){
2573 } else if (c is ULongConstant){
2574 ulong v = ((ULongConstant) c).Value;
2576 if (target_type == TypeManager.int32_type){
2577 if (v <= Int32.MaxValue)
2579 } else if (target_type == TypeManager.uint32_type){
2580 if (v <= UInt32.MaxValue)
2582 } else if (target_type == TypeManager.char_type){
2583 if (v >= Char.MinValue && v <= Char.MaxValue)
2585 } else if (target_type == TypeManager.byte_type){
2586 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2588 } else if (target_type == TypeManager.sbyte_type){
2589 if (v <= (int) SByte.MaxValue)
2591 } else if (target_type == TypeManager.short_type){
2592 if (v <= UInt16.MaxValue)
2594 } else if (target_type == TypeManager.ushort_type){
2595 if (v <= UInt16.MaxValue)
2597 } else if (target_type == TypeManager.int64_type){
2598 if (v <= Int64.MaxValue)
2602 } else if (c is ByteConstant){
2603 byte v = ((ByteConstant) c).Value;
2605 if (target_type == TypeManager.int32_type)
2607 else if (target_type == TypeManager.uint32_type)
2609 else if (target_type == TypeManager.char_type)
2611 else if (target_type == TypeManager.sbyte_type){
2612 if (v <= SByte.MaxValue)
2614 } else if (target_type == TypeManager.short_type)
2616 else if (target_type == TypeManager.ushort_type)
2618 else if (target_type == TypeManager.int64_type)
2620 else if (target_type == TypeManager.uint64_type)
2623 } else if (c is SByteConstant){
2624 sbyte v = ((SByteConstant) c).Value;
2626 if (target_type == TypeManager.int32_type)
2628 else if (target_type == TypeManager.uint32_type){
2631 } else if (target_type == TypeManager.char_type){
2634 } else if (target_type == TypeManager.byte_type){
2637 } else if (target_type == TypeManager.short_type)
2639 else if (target_type == TypeManager.ushort_type){
2642 } else if (target_type == TypeManager.int64_type)
2644 else if (target_type == TypeManager.uint64_type){
2649 } else if (c is ShortConstant){
2650 short v = ((ShortConstant) c).Value;
2652 if (target_type == TypeManager.int32_type){
2654 } else if (target_type == TypeManager.uint32_type){
2657 } else if (target_type == TypeManager.char_type){
2660 } else if (target_type == TypeManager.byte_type){
2661 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2663 } else if (target_type == TypeManager.sbyte_type){
2664 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2666 } else if (target_type == TypeManager.ushort_type){
2669 } else if (target_type == TypeManager.int64_type)
2671 else if (target_type == TypeManager.uint64_type)
2675 } else if (c is UShortConstant){
2676 ushort v = ((UShortConstant) c).Value;
2678 if (target_type == TypeManager.int32_type)
2680 else if (target_type == TypeManager.uint32_type)
2682 else if (target_type == TypeManager.char_type){
2683 if (v >= Char.MinValue && v <= Char.MaxValue)
2685 } else if (target_type == TypeManager.byte_type){
2686 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2688 } else if (target_type == TypeManager.sbyte_type){
2689 if (v <= SByte.MaxValue)
2691 } else if (target_type == TypeManager.short_type){
2692 if (v <= Int16.MaxValue)
2694 } else if (target_type == TypeManager.int64_type)
2696 else if (target_type == TypeManager.uint64_type)
2700 } else if (c is CharConstant){
2701 char v = ((CharConstant) c).Value;
2703 if (target_type == TypeManager.int32_type)
2705 else if (target_type == TypeManager.uint32_type)
2707 else if (target_type == TypeManager.byte_type){
2708 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2710 } else if (target_type == TypeManager.sbyte_type){
2711 if (v <= SByte.MaxValue)
2713 } else if (target_type == TypeManager.short_type){
2714 if (v <= Int16.MaxValue)
2716 } else if (target_type == TypeManager.ushort_type)
2718 else if (target_type == TypeManager.int64_type)
2720 else if (target_type == TypeManager.uint64_type)
2725 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2730 // Load the object from the pointer.
2732 public static void LoadFromPtr (ILGenerator ig, Type t)
2734 if (t == TypeManager.int32_type)
2735 ig.Emit (OpCodes.Ldind_I4);
2736 else if (t == TypeManager.uint32_type)
2737 ig.Emit (OpCodes.Ldind_U4);
2738 else if (t == TypeManager.short_type)
2739 ig.Emit (OpCodes.Ldind_I2);
2740 else if (t == TypeManager.ushort_type)
2741 ig.Emit (OpCodes.Ldind_U2);
2742 else if (t == TypeManager.char_type)
2743 ig.Emit (OpCodes.Ldind_U2);
2744 else if (t == TypeManager.byte_type)
2745 ig.Emit (OpCodes.Ldind_U1);
2746 else if (t == TypeManager.sbyte_type)
2747 ig.Emit (OpCodes.Ldind_I1);
2748 else if (t == TypeManager.uint64_type)
2749 ig.Emit (OpCodes.Ldind_I8);
2750 else if (t == TypeManager.int64_type)
2751 ig.Emit (OpCodes.Ldind_I8);
2752 else if (t == TypeManager.float_type)
2753 ig.Emit (OpCodes.Ldind_R4);
2754 else if (t == TypeManager.double_type)
2755 ig.Emit (OpCodes.Ldind_R8);
2756 else if (t == TypeManager.bool_type)
2757 ig.Emit (OpCodes.Ldind_I1);
2758 else if (t == TypeManager.intptr_type)
2759 ig.Emit (OpCodes.Ldind_I);
2760 else if (TypeManager.IsEnumType (t)) {
2761 if (t == TypeManager.enum_type)
2762 ig.Emit (OpCodes.Ldind_Ref);
2764 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2765 } else if (t.IsValueType)
2766 ig.Emit (OpCodes.Ldobj, t);
2768 ig.Emit (OpCodes.Ldind_Ref);
2772 // The stack contains the pointer and the value of type `type'
2774 public static void StoreFromPtr (ILGenerator ig, Type type)
2776 if (TypeManager.IsEnumType (type))
2777 type = TypeManager.EnumToUnderlying (type);
2778 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2779 ig.Emit (OpCodes.Stind_I4);
2780 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2781 ig.Emit (OpCodes.Stind_I8);
2782 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2783 type == TypeManager.ushort_type)
2784 ig.Emit (OpCodes.Stind_I2);
2785 else if (type == TypeManager.float_type)
2786 ig.Emit (OpCodes.Stind_R4);
2787 else if (type == TypeManager.double_type)
2788 ig.Emit (OpCodes.Stind_R8);
2789 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2790 type == TypeManager.bool_type)
2791 ig.Emit (OpCodes.Stind_I1);
2792 else if (type == TypeManager.intptr_type)
2793 ig.Emit (OpCodes.Stind_I);
2794 else if (type.IsValueType)
2795 ig.Emit (OpCodes.Stobj, type);
2797 ig.Emit (OpCodes.Stind_Ref);
2801 // Returns the size of type `t' if known, otherwise, 0
2803 public static int GetTypeSize (Type t)
2805 t = TypeManager.TypeToCoreType (t);
2806 if (t == TypeManager.int32_type ||
2807 t == TypeManager.uint32_type ||
2808 t == TypeManager.float_type)
2810 else if (t == TypeManager.int64_type ||
2811 t == TypeManager.uint64_type ||
2812 t == TypeManager.double_type)
2814 else if (t == TypeManager.byte_type ||
2815 t == TypeManager.sbyte_type ||
2816 t == TypeManager.bool_type)
2818 else if (t == TypeManager.short_type ||
2819 t == TypeManager.char_type ||
2820 t == TypeManager.ushort_type)
2822 else if (t == TypeManager.decimal_type)
2829 // Default implementation of IAssignMethod.CacheTemporaries
2831 public void CacheTemporaries (EmitContext ec)
2835 static void Error_NegativeArrayIndex (Location loc)
2837 Report.Error (284, loc, "Can not create array with a negative size");
2841 // Converts `source' to an int, uint, long or ulong.
2843 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2847 bool old_checked = ec.CheckState;
2848 ec.CheckState = true;
2850 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2851 if (target == null){
2852 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2853 if (target == null){
2854 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2855 if (target == null){
2856 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2858 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2862 ec.CheckState = old_checked;
2865 // Only positive constants are allowed at compile time
2867 if (target is Constant){
2868 if (target is IntConstant){
2869 if (((IntConstant) target).Value < 0){
2870 Error_NegativeArrayIndex (loc);
2875 if (target is LongConstant){
2876 if (((LongConstant) target).Value < 0){
2877 Error_NegativeArrayIndex (loc);
2890 /// This is just a base class for expressions that can
2891 /// appear on statements (invocations, object creation,
2892 /// assignments, post/pre increment and decrement). The idea
2893 /// being that they would support an extra Emition interface that
2894 /// does not leave a result on the stack.
2896 public abstract class ExpressionStatement : Expression {
2899 /// Requests the expression to be emitted in a `statement'
2900 /// context. This means that no new value is left on the
2901 /// stack after invoking this method (constrasted with
2902 /// Emit that will always leave a value on the stack).
2904 public abstract void EmitStatement (EmitContext ec);
2908 /// This kind of cast is used to encapsulate the child
2909 /// whose type is child.Type into an expression that is
2910 /// reported to return "return_type". This is used to encapsulate
2911 /// expressions which have compatible types, but need to be dealt
2912 /// at higher levels with.
2914 /// For example, a "byte" expression could be encapsulated in one
2915 /// of these as an "unsigned int". The type for the expression
2916 /// would be "unsigned int".
2919 public class EmptyCast : Expression {
2920 protected Expression child;
2922 public EmptyCast (Expression child, Type return_type)
2924 eclass = child.eclass;
2929 public Expression Peel ()
2931 if (child is EmptyCast)
2932 return ((EmptyCast) child).Peel ();
2936 public override Expression DoResolve (EmitContext ec)
2938 // This should never be invoked, we are born in fully
2939 // initialized state.
2944 public override void Emit (EmitContext ec)
2951 /// This class is used to wrap literals which belong inside Enums
2953 public class EnumConstant : Constant {
2954 public Constant Child;
2956 public EnumConstant (Constant child, Type enum_type)
2958 eclass = child.eclass;
2963 public override Expression DoResolve (EmitContext ec)
2965 // This should never be invoked, we are born in fully
2966 // initialized state.
2971 public override void Emit (EmitContext ec)
2976 public override object GetValue ()
2978 return Child.GetValue ();
2982 // Converts from one of the valid underlying types for an enumeration
2983 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2984 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2986 public Constant WidenToCompilerConstant ()
2988 Type t = TypeManager.EnumToUnderlying (Child.Type);
2989 object v = ((Constant) Child).GetValue ();;
2991 if (t == TypeManager.int32_type)
2992 return new IntConstant ((int) v);
2993 if (t == TypeManager.uint32_type)
2994 return new UIntConstant ((uint) v);
2995 if (t == TypeManager.int64_type)
2996 return new LongConstant ((long) v);
2997 if (t == TypeManager.uint64_type)
2998 return new ULongConstant ((ulong) v);
2999 if (t == TypeManager.short_type)
3000 return new ShortConstant ((short) v);
3001 if (t == TypeManager.ushort_type)
3002 return new UShortConstant ((ushort) v);
3003 if (t == TypeManager.byte_type)
3004 return new ByteConstant ((byte) v);
3005 if (t == TypeManager.sbyte_type)
3006 return new SByteConstant ((sbyte) v);
3008 throw new Exception ("Invalid enumeration underlying type: " + t);
3012 // Extracts the value in the enumeration on its native representation
3014 public object GetPlainValue ()
3016 Type t = TypeManager.EnumToUnderlying (Child.Type);
3017 object v = ((Constant) Child).GetValue ();;
3019 if (t == TypeManager.int32_type)
3021 if (t == TypeManager.uint32_type)
3023 if (t == TypeManager.int64_type)
3025 if (t == TypeManager.uint64_type)
3027 if (t == TypeManager.short_type)
3029 if (t == TypeManager.ushort_type)
3031 if (t == TypeManager.byte_type)
3033 if (t == TypeManager.sbyte_type)
3039 public override string AsString ()
3041 return Child.AsString ();
3044 public override DoubleConstant ConvertToDouble ()
3046 return Child.ConvertToDouble ();
3049 public override FloatConstant ConvertToFloat ()
3051 return Child.ConvertToFloat ();
3054 public override ULongConstant ConvertToULong ()
3056 return Child.ConvertToULong ();
3059 public override LongConstant ConvertToLong ()
3061 return Child.ConvertToLong ();
3064 public override UIntConstant ConvertToUInt ()
3066 return Child.ConvertToUInt ();
3069 public override IntConstant ConvertToInt ()
3071 return Child.ConvertToInt ();
3076 /// This kind of cast is used to encapsulate Value Types in objects.
3078 /// The effect of it is to box the value type emitted by the previous
3081 public class BoxedCast : EmptyCast {
3083 public BoxedCast (Expression expr)
3084 : base (expr, TypeManager.object_type)
3088 public override Expression DoResolve (EmitContext ec)
3090 // This should never be invoked, we are born in fully
3091 // initialized state.
3096 public override void Emit (EmitContext ec)
3100 ec.ig.Emit (OpCodes.Box, child.Type);
3104 public class UnboxCast : EmptyCast {
3105 public UnboxCast (Expression expr, Type return_type)
3106 : base (expr, return_type)
3110 public override Expression DoResolve (EmitContext ec)
3112 // This should never be invoked, we are born in fully
3113 // initialized state.
3118 public override void Emit (EmitContext ec)
3121 ILGenerator ig = ec.ig;
3124 ig.Emit (OpCodes.Unbox, t);
3126 LoadFromPtr (ig, t);
3131 /// This is used to perform explicit numeric conversions.
3133 /// Explicit numeric conversions might trigger exceptions in a checked
3134 /// context, so they should generate the conv.ovf opcodes instead of
3137 public class ConvCast : EmptyCast {
3138 public enum Mode : byte {
3139 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3141 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3142 U2_I1, U2_U1, U2_I2, U2_CH,
3143 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3144 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3145 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3146 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3147 CH_I1, CH_U1, CH_I2,
3148 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3149 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3155 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3156 : base (child, return_type)
3158 checked_state = ec.CheckState;
3162 public override Expression DoResolve (EmitContext ec)
3164 // This should never be invoked, we are born in fully
3165 // initialized state.
3170 public override void Emit (EmitContext ec)
3172 ILGenerator ig = ec.ig;
3178 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3179 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3180 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3181 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3182 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3184 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3185 case Mode.U1_CH: /* nothing */ break;
3187 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3188 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3189 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3190 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3191 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3192 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3194 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3195 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3196 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3197 case Mode.U2_CH: /* nothing */ break;
3199 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3200 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3201 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3202 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3203 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3204 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3205 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3207 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3208 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3209 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3210 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3211 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3212 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3214 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3215 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3216 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3217 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3218 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3219 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3220 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3221 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3223 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3224 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3225 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3226 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3227 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3228 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3229 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3230 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3232 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3233 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3234 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3236 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3237 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3238 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3239 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3240 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3241 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3242 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3243 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3244 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3246 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3247 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3248 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3249 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3250 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3251 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3252 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3253 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3254 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3255 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3259 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3260 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3261 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3262 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3263 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3265 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3266 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3268 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3269 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3270 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3271 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3272 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3273 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3275 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3276 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3277 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3278 case Mode.U2_CH: /* nothing */ break;
3280 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3281 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3282 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3283 case Mode.I4_U4: /* nothing */ break;
3284 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3285 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3286 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3288 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3289 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3290 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3291 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3292 case Mode.U4_I4: /* nothing */ break;
3293 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3295 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3296 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3297 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3298 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3299 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3300 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3301 case Mode.I8_U8: /* nothing */ break;
3302 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3304 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3305 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3306 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3307 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3308 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3309 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3310 case Mode.U8_I8: /* nothing */ break;
3311 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3313 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3314 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3315 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3317 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3318 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3319 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3320 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3321 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3322 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3323 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3324 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3325 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3327 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3328 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3329 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3330 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3331 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3332 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3333 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3334 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3335 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3336 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3342 public class OpcodeCast : EmptyCast {
3346 public OpcodeCast (Expression child, Type return_type, OpCode op)
3347 : base (child, return_type)
3351 second_valid = false;
3354 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3355 : base (child, return_type)
3360 second_valid = true;
3363 public override Expression DoResolve (EmitContext ec)
3365 // This should never be invoked, we are born in fully
3366 // initialized state.
3371 public override void Emit (EmitContext ec)
3382 /// This kind of cast is used to encapsulate a child and cast it
3383 /// to the class requested
3385 public class ClassCast : EmptyCast {
3386 public ClassCast (Expression child, Type return_type)
3387 : base (child, return_type)
3392 public override Expression DoResolve (EmitContext ec)
3394 // This should never be invoked, we are born in fully
3395 // initialized state.
3400 public override void Emit (EmitContext ec)
3404 ec.ig.Emit (OpCodes.Castclass, type);
3410 /// SimpleName expressions are initially formed of a single
3411 /// word and it only happens at the beginning of the expression.
3415 /// The expression will try to be bound to a Field, a Method
3416 /// group or a Property. If those fail we pass the name to our
3417 /// caller and the SimpleName is compounded to perform a type
3418 /// lookup. The idea behind this process is that we want to avoid
3419 /// creating a namespace map from the assemblies, as that requires
3420 /// the GetExportedTypes function to be called and a hashtable to
3421 /// be constructed which reduces startup time. If later we find
3422 /// that this is slower, we should create a `NamespaceExpr' expression
3423 /// that fully participates in the resolution process.
3425 /// For example `System.Console.WriteLine' is decomposed into
3426 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3428 /// The first SimpleName wont produce a match on its own, so it will
3430 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3432 /// System.Console will produce a TypeExpr match.
3434 /// The downside of this is that we might be hitting `LookupType' too many
3435 /// times with this scheme.
3437 public class SimpleName : Expression, ITypeExpression {
3438 public readonly string Name;
3441 // If true, then we are a simple name, not composed with a ".
3445 public SimpleName (string a, string b, Location l)
3447 Name = String.Concat (a, ".", b);
3452 public SimpleName (string name, Location l)
3459 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3461 if (ec.IsFieldInitializer)
3464 "A field initializer cannot reference the non-static field, " +
3465 "method or property `"+name+"'");
3469 "An object reference is required " +
3470 "for the non-static field `"+name+"'");
3474 // Checks whether we are trying to access an instance
3475 // property, method or field from a static body.
3477 Expression MemberStaticCheck (EmitContext ec, Expression e)
3479 if (e is IMemberExpr){
3480 IMemberExpr member = (IMemberExpr) e;
3482 if (!member.IsStatic){
3483 Error_ObjectRefRequired (ec, loc, Name);
3491 public override Expression DoResolve (EmitContext ec)
3493 return SimpleNameResolve (ec, null, false);
3496 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3498 return SimpleNameResolve (ec, right_side, false);
3502 public Expression DoResolveAllowStatic (EmitContext ec)
3504 return SimpleNameResolve (ec, null, true);
3507 public Expression DoResolveType (EmitContext ec)
3509 DeclSpace ds = ec.DeclSpace;
3510 Namespace ns = ds.Namespace;
3515 // Since we are cheating: we only do the Alias lookup for
3516 // namespaces if the name does not include any dots in it
3518 if (ns != null && is_base)
3519 alias_value = ns.LookupAlias (Name);
3523 if (ec.ResolvingTypeTree){
3524 if (alias_value != null){
3525 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3526 return new TypeExpr (t, loc);
3529 int errors = Report.Errors;
3530 Type dt = ec.DeclSpace.FindType (loc, Name);
3531 if (Report.Errors != errors)
3535 return new TypeExpr (dt, loc);
3539 // First, the using aliases
3541 if (alias_value != null){
3542 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3543 return new TypeExpr (t, loc);
3545 // we have alias value, but it isn't Type, so try if it's namespace
3546 return new SimpleName (alias_value, loc);
3550 // Stage 2: Lookup up if we are an alias to a type
3554 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3555 return new TypeExpr (t, loc);
3557 // No match, maybe our parent can compose us
3558 // into something meaningful.
3563 /// 7.5.2: Simple Names.
3565 /// Local Variables and Parameters are handled at
3566 /// parse time, so they never occur as SimpleNames.
3568 /// The `allow_static' flag is used by MemberAccess only
3569 /// and it is used to inform us that it is ok for us to
3570 /// avoid the static check, because MemberAccess might end
3571 /// up resolving the Name as a Type name and the access as
3572 /// a static type access.
3574 /// ie: Type Type; .... { Type.GetType (""); }
3576 /// Type is both an instance variable and a Type; Type.GetType
3577 /// is the static method not an instance method of type.
3579 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3581 Expression e = null;
3584 // Stage 1: Performed by the parser (binding to locals or parameters).
3586 Block current_block = ec.CurrentBlock;
3587 if (current_block != null && current_block.GetVariableInfo (Name) != null){
3588 LocalVariableReference var;
3590 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3592 if (right_side != null)
3593 return var.ResolveLValue (ec, right_side);
3595 return var.Resolve (ec);
3598 if (current_block != null){
3600 Parameter par = null;
3601 Parameters pars = current_block.Parameters;
3603 par = pars.GetParameterByName (Name, out idx);
3606 ParameterReference param;
3608 param = new ParameterReference (pars, idx, Name, loc);
3610 if (right_side != null)
3611 return param.ResolveLValue (ec, right_side);
3613 return param.Resolve (ec);
3618 // Stage 2: Lookup members
3621 DeclSpace lookup_ds = ec.DeclSpace;
3623 if (lookup_ds.TypeBuilder == null)
3626 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3630 lookup_ds =lookup_ds.Parent;
3631 } while (lookup_ds != null);
3633 if (e == null && ec.ContainerType != null)
3634 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3637 return DoResolveType (ec);
3642 if (e is IMemberExpr) {
3643 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3647 IMemberExpr me = e as IMemberExpr;
3651 // This fails if ResolveMemberAccess() was unable to decide whether
3652 // it's a field or a type of the same name.
3653 if (!me.IsStatic && (me.InstanceExpression == null))
3657 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3658 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3659 "outer type `" + me.DeclaringType + "' via nested type `" +
3660 me.InstanceExpression.Type + "'");
3664 if (right_side != null)
3665 e = e.DoResolveLValue (ec, right_side);
3667 e = e.DoResolve (ec);
3672 if (ec.IsStatic || ec.IsFieldInitializer){
3676 return MemberStaticCheck (ec, e);
3681 public override void Emit (EmitContext ec)
3684 // If this is ever reached, then we failed to
3685 // find the name as a namespace
3688 Error (103, "The name `" + Name +
3689 "' does not exist in the class `" +
3690 ec.DeclSpace.Name + "'");
3693 public override string ToString ()
3700 /// Fully resolved expression that evaluates to a type
3702 public class TypeExpr : Expression, ITypeExpression {
3703 public TypeExpr (Type t, Location l)
3706 eclass = ExprClass.Type;
3710 public virtual Expression DoResolveType (EmitContext ec)
3715 override public Expression DoResolve (EmitContext ec)
3720 override public void Emit (EmitContext ec)
3722 throw new Exception ("Should never be called");
3725 public override string ToString ()
3727 return Type.ToString ();
3732 /// Used to create types from a fully qualified name. These are just used
3733 /// by the parser to setup the core types. A TypeLookupExpression is always
3734 /// classified as a type.
3736 public class TypeLookupExpression : TypeExpr {
3739 public TypeLookupExpression (string name) : base (null, Location.Null)
3744 public override Expression DoResolveType (EmitContext ec)
3747 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3751 public override Expression DoResolve (EmitContext ec)
3753 return DoResolveType (ec);
3756 public override void Emit (EmitContext ec)
3758 throw new Exception ("Should never be called");
3761 public override string ToString ()
3768 /// MethodGroup Expression.
3770 /// This is a fully resolved expression that evaluates to a type
3772 public class MethodGroupExpr : Expression, IMemberExpr {
3773 public MethodBase [] Methods;
3774 Expression instance_expression = null;
3775 bool is_explicit_impl = false;
3777 public MethodGroupExpr (MemberInfo [] mi, Location l)
3779 Methods = new MethodBase [mi.Length];
3780 mi.CopyTo (Methods, 0);
3781 eclass = ExprClass.MethodGroup;
3782 type = TypeManager.object_type;
3786 public MethodGroupExpr (ArrayList list, Location l)
3788 Methods = new MethodBase [list.Count];
3791 list.CopyTo (Methods, 0);
3793 foreach (MemberInfo m in list){
3794 if (!(m is MethodBase)){
3795 Console.WriteLine ("Name " + m.Name);
3796 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3802 eclass = ExprClass.MethodGroup;
3803 type = TypeManager.object_type;
3806 public Type DeclaringType {
3808 return Methods [0].DeclaringType;
3813 // `A method group may have associated an instance expression'
3815 public Expression InstanceExpression {
3817 return instance_expression;
3821 instance_expression = value;
3825 public bool IsExplicitImpl {
3827 return is_explicit_impl;
3831 is_explicit_impl = value;
3835 public string Name {
3837 return Methods [0].Name;
3841 public bool IsInstance {
3843 foreach (MethodBase mb in Methods)
3851 public bool IsStatic {
3853 foreach (MethodBase mb in Methods)
3861 override public Expression DoResolve (EmitContext ec)
3863 if (instance_expression != null) {
3864 instance_expression = instance_expression.DoResolve (ec);
3865 if (instance_expression == null)
3872 public void ReportUsageError ()
3874 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3875 Methods [0].Name + "()' is referenced without parentheses");
3878 override public void Emit (EmitContext ec)
3880 ReportUsageError ();
3883 bool RemoveMethods (bool keep_static)
3885 ArrayList smethods = new ArrayList ();
3887 foreach (MethodBase mb in Methods){
3888 if (mb.IsStatic == keep_static)
3892 if (smethods.Count == 0)
3895 Methods = new MethodBase [smethods.Count];
3896 smethods.CopyTo (Methods, 0);
3902 /// Removes any instance methods from the MethodGroup, returns
3903 /// false if the resulting set is empty.
3905 public bool RemoveInstanceMethods ()
3907 return RemoveMethods (true);
3911 /// Removes any static methods from the MethodGroup, returns
3912 /// false if the resulting set is empty.
3914 public bool RemoveStaticMethods ()
3916 return RemoveMethods (false);
3921 /// Fully resolved expression that evaluates to a Field
3923 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3924 public readonly FieldInfo FieldInfo;
3925 Expression instance_expr;
3927 public FieldExpr (FieldInfo fi, Location l)
3930 eclass = ExprClass.Variable;
3931 type = fi.FieldType;
3935 public string Name {
3937 return FieldInfo.Name;
3941 public bool IsInstance {
3943 return !FieldInfo.IsStatic;
3947 public bool IsStatic {
3949 return FieldInfo.IsStatic;
3953 public Type DeclaringType {
3955 return FieldInfo.DeclaringType;
3959 public Expression InstanceExpression {
3961 return instance_expr;
3965 instance_expr = value;
3969 override public Expression DoResolve (EmitContext ec)
3971 if (!FieldInfo.IsStatic){
3972 if (instance_expr == null){
3974 // This can happen when referencing an instance field using
3975 // a fully qualified type expression: TypeName.InstanceField = xxx
3977 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
3981 // Resolve the field's instance expression while flow analysis is turned
3982 // off: when accessing a field "a.b", we must check whether the field
3983 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3984 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3985 ResolveFlags.DisableFlowAnalysis);
3986 if (instance_expr == null)
3990 // If the instance expression is a local variable or parameter.
3991 IVariable var = instance_expr as IVariable;
3992 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3998 void Report_AssignToReadonly (bool is_instance)
4003 msg = "Readonly field can not be assigned outside " +
4004 "of constructor or variable initializer";
4006 msg = "A static readonly field can only be assigned in " +
4007 "a static constructor";
4009 Report.Error (is_instance ? 191 : 198, loc, msg);
4012 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4014 IVariable var = instance_expr as IVariable;
4016 var.SetFieldAssigned (ec, FieldInfo.Name);
4018 Expression e = DoResolve (ec);
4023 if (!FieldInfo.IsInitOnly)
4027 // InitOnly fields can only be assigned in constructors
4030 if (ec.IsConstructor)
4033 Report_AssignToReadonly (true);
4038 override public void Emit (EmitContext ec)
4040 ILGenerator ig = ec.ig;
4041 bool is_volatile = false;
4043 if (FieldInfo is FieldBuilder){
4044 FieldBase f = TypeManager.GetField (FieldInfo);
4046 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4049 f.status |= Field.Status.USED;
4052 if (FieldInfo.IsStatic){
4054 ig.Emit (OpCodes.Volatile);
4056 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4058 if (instance_expr.Type.IsValueType){
4060 LocalTemporary tempo = null;
4062 if (!(instance_expr is IMemoryLocation)){
4063 tempo = new LocalTemporary (
4064 ec, instance_expr.Type);
4066 InstanceExpression.Emit (ec);
4070 ml = (IMemoryLocation) instance_expr;
4072 ml.AddressOf (ec, AddressOp.Load);
4074 instance_expr.Emit (ec);
4077 ig.Emit (OpCodes.Volatile);
4079 ig.Emit (OpCodes.Ldfld, FieldInfo);
4083 public void EmitAssign (EmitContext ec, Expression source)
4085 FieldAttributes fa = FieldInfo.Attributes;
4086 bool is_static = (fa & FieldAttributes.Static) != 0;
4087 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4088 ILGenerator ig = ec.ig;
4090 if (is_readonly && !ec.IsConstructor){
4091 Report_AssignToReadonly (!is_static);
4096 Expression instance = instance_expr;
4098 if (instance.Type.IsValueType){
4099 if (instance is IMemoryLocation){
4100 IMemoryLocation ml = (IMemoryLocation) instance;
4102 ml.AddressOf (ec, AddressOp.Store);
4104 throw new Exception ("The " + instance + " of type " +
4106 " represents a ValueType and does " +
4107 "not implement IMemoryLocation");
4113 if (FieldInfo is FieldBuilder){
4114 FieldBase f = TypeManager.GetField (FieldInfo);
4116 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4117 ig.Emit (OpCodes.Volatile);
4121 ig.Emit (OpCodes.Stsfld, FieldInfo);
4123 ig.Emit (OpCodes.Stfld, FieldInfo);
4125 if (FieldInfo is FieldBuilder){
4126 FieldBase f = TypeManager.GetField (FieldInfo);
4128 f.status |= Field.Status.ASSIGNED;
4132 public void AddressOf (EmitContext ec, AddressOp mode)
4134 ILGenerator ig = ec.ig;
4136 if (FieldInfo is FieldBuilder){
4137 FieldBase f = TypeManager.GetField (FieldInfo);
4138 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4139 ig.Emit (OpCodes.Volatile);
4142 if (FieldInfo is FieldBuilder){
4143 FieldBase f = TypeManager.GetField (FieldInfo);
4145 if ((mode & AddressOp.Store) != 0)
4146 f.status |= Field.Status.ASSIGNED;
4147 if ((mode & AddressOp.Load) != 0)
4148 f.status |= Field.Status.USED;
4152 // Handle initonly fields specially: make a copy and then
4153 // get the address of the copy.
4155 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4159 local = ig.DeclareLocal (type);
4160 ig.Emit (OpCodes.Stloc, local);
4161 ig.Emit (OpCodes.Ldloca, local);
4165 if (FieldInfo.IsStatic)
4166 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4169 if (instance_expr is IMemoryLocation)
4170 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4173 instance_expr.Emit (ec);
4174 ig.Emit (OpCodes.Ldflda, FieldInfo);
4180 /// Expression that evaluates to a Property. The Assign class
4181 /// might set the `Value' expression if we are in an assignment.
4183 /// This is not an LValue because we need to re-write the expression, we
4184 /// can not take data from the stack and store it.
4186 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4187 public readonly PropertyInfo PropertyInfo;
4190 // This is set externally by the `BaseAccess' class
4193 MethodInfo getter, setter;
4195 bool must_do_cs1540_check;
4197 Expression instance_expr;
4199 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4202 eclass = ExprClass.PropertyAccess;
4206 type = TypeManager.TypeToCoreType (pi.PropertyType);
4208 ResolveAccessors (ec);
4211 public string Name {
4213 return PropertyInfo.Name;
4217 public bool IsInstance {
4223 public bool IsStatic {
4229 public Type DeclaringType {
4231 return PropertyInfo.DeclaringType;
4236 // The instance expression associated with this expression
4238 public Expression InstanceExpression {
4240 instance_expr = value;
4244 return instance_expr;
4248 public bool VerifyAssignable ()
4250 if (setter == null) {
4251 Report.Error (200, loc,
4252 "The property `" + PropertyInfo.Name +
4253 "' can not be assigned to, as it has not set accessor");
4260 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4262 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4263 BindingFlags.Static | BindingFlags.Instance;
4266 group = TypeManager.MemberLookup (
4267 invocation_type, invocation_type, PropertyInfo.DeclaringType,
4268 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4271 // The first method is the closest to us
4276 foreach (MethodInfo mi in group) {
4277 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4280 // If only accessible to the current class or children
4282 if (ma == MethodAttributes.Private) {
4283 Type declaring_type = mi.DeclaringType;
4285 if (invocation_type != declaring_type){
4286 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4294 // FamAndAssem requires that we not only derivate, but we are on the
4297 if (ma == MethodAttributes.FamANDAssem){
4298 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4304 // Assembly and FamORAssem succeed if we're in the same assembly.
4305 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4306 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4312 // We already know that we aren't in the same assembly.
4313 if (ma == MethodAttributes.Assembly)
4316 // Family and FamANDAssem require that we derive.
4317 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4318 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4321 must_do_cs1540_check = true;
4334 // We also perform the permission checking here, as the PropertyInfo does not
4335 // hold the information for the accessibility of its setter/getter
4337 void ResolveAccessors (EmitContext ec)
4339 getter = GetAccessor (ec.ContainerType, "get");
4340 if ((getter != null) && getter.IsStatic)
4343 setter = GetAccessor (ec.ContainerType, "set");
4344 if ((setter != null) && setter.IsStatic)
4347 if (setter == null && getter == null){
4348 Error (122, "`" + PropertyInfo.Name + "' " +
4349 "is inaccessible because of its protection level");
4354 bool InstanceResolve (EmitContext ec)
4356 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4357 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4361 if (instance_expr != null) {
4362 instance_expr = instance_expr.DoResolve (ec);
4363 if (instance_expr == null)
4367 if (must_do_cs1540_check && (instance_expr != null)) {
4368 if ((instance_expr.Type != ec.ContainerType) &&
4369 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4370 Report.Error (1540, loc, "Cannot access protected member `" +
4371 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4372 "' via a qualifier of type `" +
4373 TypeManager.CSharpName (instance_expr.Type) +
4374 "'; the qualifier must be of type `" +
4375 TypeManager.CSharpName (ec.ContainerType) +
4376 "' (or derived from it)");
4384 override public Expression DoResolve (EmitContext ec)
4386 if (getter == null){
4388 // The following condition happens if the PropertyExpr was
4389 // created, but is invalid (ie, the property is inaccessible),
4390 // and we did not want to embed the knowledge about this in
4391 // the caller routine. This only avoids double error reporting.
4396 Report.Error (154, loc,
4397 "The property `" + PropertyInfo.Name +
4398 "' can not be used in " +
4399 "this context because it lacks a get accessor");
4403 if (!InstanceResolve (ec))
4407 // Only base will allow this invocation to happen.
4409 if (IsBase && getter.IsAbstract){
4410 Report.Error (205, loc, "Cannot call an abstract base property: " +
4411 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4418 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4420 if (setter == null){
4422 // The following condition happens if the PropertyExpr was
4423 // created, but is invalid (ie, the property is inaccessible),
4424 // and we did not want to embed the knowledge about this in
4425 // the caller routine. This only avoids double error reporting.
4430 Report.Error (154, loc,
4431 "The property `" + PropertyInfo.Name +
4432 "' can not be used in " +
4433 "this context because it lacks a set accessor");
4437 if (!InstanceResolve (ec))
4441 // Only base will allow this invocation to happen.
4443 if (IsBase && setter.IsAbstract){
4444 Report.Error (205, loc, "Cannot call an abstract base property: " +
4445 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4451 override public void Emit (EmitContext ec)
4454 // Special case: length of single dimension array property is turned into ldlen
4456 if ((getter == TypeManager.system_int_array_get_length) ||
4457 (getter == TypeManager.int_array_get_length)){
4458 Type iet = instance_expr.Type;
4461 // System.Array.Length can be called, but the Type does not
4462 // support invoking GetArrayRank, so test for that case first
4464 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4465 instance_expr.Emit (ec);
4466 ec.ig.Emit (OpCodes.Ldlen);
4471 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4476 // Implements the IAssignMethod interface for assignments
4478 public void EmitAssign (EmitContext ec, Expression source)
4480 Argument arg = new Argument (source, Argument.AType.Expression);
4481 ArrayList args = new ArrayList ();
4484 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4487 override public void EmitStatement (EmitContext ec)
4490 ec.ig.Emit (OpCodes.Pop);
4495 /// Fully resolved expression that evaluates to an Event
4497 public class EventExpr : Expression, IMemberExpr {
4498 public readonly EventInfo EventInfo;
4499 public Expression instance_expr;
4502 MethodInfo add_accessor, remove_accessor;
4504 public EventExpr (EventInfo ei, Location loc)
4508 eclass = ExprClass.EventAccess;
4510 add_accessor = TypeManager.GetAddMethod (ei);
4511 remove_accessor = TypeManager.GetRemoveMethod (ei);
4513 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4516 if (EventInfo is MyEventBuilder){
4517 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4518 type = eb.EventType;
4521 type = EventInfo.EventHandlerType;
4524 public string Name {
4526 return EventInfo.Name;
4530 public bool IsInstance {
4536 public bool IsStatic {
4542 public Type DeclaringType {
4544 return EventInfo.DeclaringType;
4548 public Expression InstanceExpression {
4550 return instance_expr;
4554 instance_expr = value;
4558 public override Expression DoResolve (EmitContext ec)
4560 if (instance_expr != null) {
4561 instance_expr = instance_expr.DoResolve (ec);
4562 if (instance_expr == null)
4570 public override void Emit (EmitContext ec)
4572 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4575 public void EmitAddOrRemove (EmitContext ec, Expression source)
4577 Expression handler = ((Binary) source).Right;
4579 Argument arg = new Argument (handler, Argument.AType.Expression);
4580 ArrayList args = new ArrayList ();
4584 if (((Binary) source).Oper == Binary.Operator.Addition)
4585 Invocation.EmitCall (
4586 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4588 Invocation.EmitCall (
4589 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);