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;
564 if (mi [0] is MethodBase)
565 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 || expr_type == TypeManager.enum_type)
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 NullLiteralTyped (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 == TypeManager.delegate_type ||
795 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
796 target_type == TypeManager.delegate_type)
797 return new EmptyCast (expr, target_type);
799 // from any array-type or delegate type into System.ICloneable.
800 if (expr_type.IsArray ||
801 expr_type == TypeManager.delegate_type ||
802 expr_type.IsSubclassOf (TypeManager.delegate_type))
803 if (target_type == TypeManager.icloneable_type)
804 return new EmptyCast (expr, target_type);
814 /// Implicit Numeric Conversions.
816 /// expr is the expression to convert, returns a new expression of type
817 /// target_type or null if an implicit conversion is not possible.
819 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
820 Type target_type, Location loc)
822 Type expr_type = expr.Type;
825 // Attempt to do the implicit constant expression conversions
827 if (expr is Constant){
829 if (expr is IntConstant){
832 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
836 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
838 // Try the implicit constant expression conversion
839 // from long to ulong, instead of a nice routine,
842 long v = ((LongConstant) expr).Value;
844 return new ULongConstant ((ulong) v);
848 Type real_target_type = target_type;
850 if (expr_type == TypeManager.sbyte_type){
852 // From sbyte to short, int, long, float, double.
854 if (real_target_type == TypeManager.int32_type)
855 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
856 if (real_target_type == TypeManager.int64_type)
857 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
858 if (real_target_type == TypeManager.double_type)
859 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
860 if (real_target_type == TypeManager.float_type)
861 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
862 if (real_target_type == TypeManager.short_type)
863 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
864 } else if (expr_type == TypeManager.byte_type){
866 // From byte to short, ushort, int, uint, long, ulong, float, double
868 if ((real_target_type == TypeManager.short_type) ||
869 (real_target_type == TypeManager.ushort_type) ||
870 (real_target_type == TypeManager.int32_type) ||
871 (real_target_type == TypeManager.uint32_type))
872 return new EmptyCast (expr, target_type);
874 if (real_target_type == TypeManager.uint64_type)
875 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
876 if (real_target_type == TypeManager.int64_type)
877 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
878 if (real_target_type == TypeManager.float_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
880 if (real_target_type == TypeManager.double_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
882 } else if (expr_type == TypeManager.short_type){
884 // From short to int, long, float, double
886 if (real_target_type == TypeManager.int32_type)
887 return new EmptyCast (expr, target_type);
888 if (real_target_type == TypeManager.int64_type)
889 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
890 if (real_target_type == TypeManager.double_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
892 if (real_target_type == TypeManager.float_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
894 } else if (expr_type == TypeManager.ushort_type){
896 // From ushort to int, uint, long, ulong, float, double
898 if (real_target_type == TypeManager.uint32_type)
899 return new EmptyCast (expr, target_type);
901 if (real_target_type == TypeManager.uint64_type)
902 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
903 if (real_target_type == TypeManager.int32_type)
904 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
905 if (real_target_type == TypeManager.int64_type)
906 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
907 if (real_target_type == TypeManager.double_type)
908 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
909 if (real_target_type == TypeManager.float_type)
910 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
911 } else if (expr_type == TypeManager.int32_type){
913 // From int to long, float, double
915 if (real_target_type == TypeManager.int64_type)
916 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
917 if (real_target_type == TypeManager.double_type)
918 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
919 if (real_target_type == TypeManager.float_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
921 } else if (expr_type == TypeManager.uint32_type){
923 // From uint to long, ulong, float, double
925 if (real_target_type == TypeManager.int64_type)
926 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
927 if (real_target_type == TypeManager.uint64_type)
928 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
929 if (real_target_type == TypeManager.double_type)
930 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
932 if (real_target_type == TypeManager.float_type)
933 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
935 } else if (expr_type == TypeManager.int64_type){
937 // From long/ulong to float, double
939 if (real_target_type == TypeManager.double_type)
940 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
941 if (real_target_type == TypeManager.float_type)
942 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
943 } else if (expr_type == TypeManager.uint64_type){
945 // From ulong to float, double
947 if (real_target_type == TypeManager.double_type)
948 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
950 if (real_target_type == TypeManager.float_type)
951 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
953 } else if (expr_type == TypeManager.char_type){
955 // From char to ushort, int, uint, long, ulong, float, double
957 if ((real_target_type == TypeManager.ushort_type) ||
958 (real_target_type == TypeManager.int32_type) ||
959 (real_target_type == TypeManager.uint32_type))
960 return new EmptyCast (expr, target_type);
961 if (real_target_type == TypeManager.uint64_type)
962 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
963 if (real_target_type == TypeManager.int64_type)
964 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
965 if (real_target_type == TypeManager.float_type)
966 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
967 if (real_target_type == TypeManager.double_type)
968 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
969 } else if (expr_type == TypeManager.float_type){
973 if (real_target_type == TypeManager.double_type)
974 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
981 // Tests whether an implicit reference conversion exists between expr_type
984 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
986 Type expr_type = expr.Type;
989 // This is the boxed case.
991 if (target_type == TypeManager.object_type) {
992 if (expr_type.IsClass || expr_type.IsValueType ||
993 expr_type.IsInterface || expr_type == TypeManager.enum_type)
995 } else if (expr_type.IsSubclassOf (target_type)) {
998 // Please remember that all code below actually comes
999 // from ImplicitReferenceConversion so make sure code remains in sync
1001 // from any class-type S to any interface-type T.
1002 if (target_type.IsInterface) {
1003 if (TypeManager.ImplementsInterface (expr_type, target_type))
1007 // from any interface type S to interface-type T.
1008 if (expr_type.IsInterface && target_type.IsInterface)
1009 if (TypeManager.ImplementsInterface (expr_type, target_type))
1012 // from an array-type S to an array-type of type T
1013 if (expr_type.IsArray && target_type.IsArray) {
1014 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1016 Type expr_element_type = expr_type.GetElementType ();
1018 if (MyEmptyExpr == null)
1019 MyEmptyExpr = new EmptyExpression ();
1021 MyEmptyExpr.SetType (expr_element_type);
1022 Type target_element_type = target_type.GetElementType ();
1024 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1025 if (StandardConversionExists (MyEmptyExpr,
1026 target_element_type))
1031 // from an array-type to System.Array
1032 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1035 // from any delegate type to System.Delegate
1036 if ((expr_type == TypeManager.delegate_type ||
1037 expr_type.IsSubclassOf (TypeManager.delegate_type)) &&
1038 target_type == TypeManager.delegate_type)
1039 if (target_type.IsAssignableFrom (expr_type))
1042 // from any array-type or delegate type into System.ICloneable.
1043 if (expr_type.IsArray ||
1044 expr_type == TypeManager.delegate_type ||
1045 expr_type.IsSubclassOf (TypeManager.delegate_type))
1046 if (target_type == TypeManager.icloneable_type)
1049 // from the null type to any reference-type.
1050 if (expr is NullLiteral && !target_type.IsValueType &&
1051 !TypeManager.IsEnumType (target_type))
1060 /// Same as StandardConversionExists except that it also looks at
1061 /// implicit user defined conversions - needed for overload resolution
1063 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1065 if (StandardConversionExists (expr, target_type) == true)
1068 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1076 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
1078 Expression dummy = ImplicitUserConversion (
1079 ec, new EmptyExpression (source), target, Location.Null);
1080 return dummy != null;
1084 /// Determines if a standard implicit conversion exists from
1085 /// expr_type to target_type
1087 public static bool StandardConversionExists (Expression expr, Type target_type)
1089 Type expr_type = expr.Type;
1091 if (expr_type == TypeManager.void_type)
1094 if (expr_type == target_type)
1097 // First numeric conversions
1099 if (expr_type == TypeManager.sbyte_type){
1101 // From sbyte to short, int, long, float, double.
1103 if ((target_type == TypeManager.int32_type) ||
1104 (target_type == TypeManager.int64_type) ||
1105 (target_type == TypeManager.double_type) ||
1106 (target_type == TypeManager.float_type) ||
1107 (target_type == TypeManager.short_type) ||
1108 (target_type == TypeManager.decimal_type))
1111 } else if (expr_type == TypeManager.byte_type){
1113 // From byte to short, ushort, int, uint, long, ulong, float, double
1115 if ((target_type == TypeManager.short_type) ||
1116 (target_type == TypeManager.ushort_type) ||
1117 (target_type == TypeManager.int32_type) ||
1118 (target_type == TypeManager.uint32_type) ||
1119 (target_type == TypeManager.uint64_type) ||
1120 (target_type == TypeManager.int64_type) ||
1121 (target_type == TypeManager.float_type) ||
1122 (target_type == TypeManager.double_type) ||
1123 (target_type == TypeManager.decimal_type))
1126 } else if (expr_type == TypeManager.short_type){
1128 // From short to int, long, float, double
1130 if ((target_type == TypeManager.int32_type) ||
1131 (target_type == TypeManager.int64_type) ||
1132 (target_type == TypeManager.double_type) ||
1133 (target_type == TypeManager.float_type) ||
1134 (target_type == TypeManager.decimal_type))
1137 } else if (expr_type == TypeManager.ushort_type){
1139 // From ushort to int, uint, long, ulong, float, double
1141 if ((target_type == TypeManager.uint32_type) ||
1142 (target_type == TypeManager.uint64_type) ||
1143 (target_type == TypeManager.int32_type) ||
1144 (target_type == TypeManager.int64_type) ||
1145 (target_type == TypeManager.double_type) ||
1146 (target_type == TypeManager.float_type) ||
1147 (target_type == TypeManager.decimal_type))
1150 } else if (expr_type == TypeManager.int32_type){
1152 // From int to long, float, double
1154 if ((target_type == TypeManager.int64_type) ||
1155 (target_type == TypeManager.double_type) ||
1156 (target_type == TypeManager.float_type) ||
1157 (target_type == TypeManager.decimal_type))
1160 } else if (expr_type == TypeManager.uint32_type){
1162 // From uint to long, ulong, float, double
1164 if ((target_type == TypeManager.int64_type) ||
1165 (target_type == TypeManager.uint64_type) ||
1166 (target_type == TypeManager.double_type) ||
1167 (target_type == TypeManager.float_type) ||
1168 (target_type == TypeManager.decimal_type))
1171 } else if ((expr_type == TypeManager.uint64_type) ||
1172 (expr_type == TypeManager.int64_type)) {
1174 // From long/ulong to float, double
1176 if ((target_type == TypeManager.double_type) ||
1177 (target_type == TypeManager.float_type) ||
1178 (target_type == TypeManager.decimal_type))
1181 } else if (expr_type == TypeManager.char_type){
1183 // From char to ushort, int, uint, long, ulong, float, double
1185 if ((target_type == TypeManager.ushort_type) ||
1186 (target_type == TypeManager.int32_type) ||
1187 (target_type == TypeManager.uint32_type) ||
1188 (target_type == TypeManager.uint64_type) ||
1189 (target_type == TypeManager.int64_type) ||
1190 (target_type == TypeManager.float_type) ||
1191 (target_type == TypeManager.double_type) ||
1192 (target_type == TypeManager.decimal_type))
1195 } else if (expr_type == TypeManager.float_type){
1199 if (target_type == TypeManager.double_type)
1203 if (ImplicitReferenceConversionExists (expr, target_type))
1206 if (expr is IntConstant){
1207 int value = ((IntConstant) expr).Value;
1209 if (target_type == TypeManager.sbyte_type){
1210 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1212 } else if (target_type == TypeManager.byte_type){
1213 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1215 } else if (target_type == TypeManager.short_type){
1216 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1218 } else if (target_type == TypeManager.ushort_type){
1219 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1221 } else if (target_type == TypeManager.uint32_type){
1224 } else if (target_type == TypeManager.uint64_type){
1226 // we can optimize this case: a positive int32
1227 // always fits on a uint64. But we need an opcode
1234 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1238 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1240 // Try the implicit constant expression conversion
1241 // from long to ulong, instead of a nice routine,
1242 // we just inline it
1244 long v = ((LongConstant) expr).Value;
1249 if ((target_type == TypeManager.enum_type ||
1250 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1251 expr is IntLiteral){
1252 IntLiteral i = (IntLiteral) expr;
1258 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1265 // Used internally by FindMostEncompassedType, this is used
1266 // to avoid creating lots of objects in the tight loop inside
1267 // FindMostEncompassedType
1269 static EmptyExpression priv_fmet_param;
1272 /// Finds "most encompassed type" according to the spec (13.4.2)
1273 /// amongst the methods in the MethodGroupExpr
1275 static Type FindMostEncompassedType (ArrayList types)
1279 if (priv_fmet_param == null)
1280 priv_fmet_param = new EmptyExpression ();
1282 foreach (Type t in types){
1283 priv_fmet_param.SetType (t);
1290 if (StandardConversionExists (priv_fmet_param, best))
1298 // Used internally by FindMostEncompassingType, this is used
1299 // to avoid creating lots of objects in the tight loop inside
1300 // FindMostEncompassingType
1302 static EmptyExpression priv_fmee_ret;
1305 /// Finds "most encompassing type" according to the spec (13.4.2)
1306 /// amongst the types in the given set
1308 static Type FindMostEncompassingType (ArrayList types)
1312 if (priv_fmee_ret == null)
1313 priv_fmee_ret = new EmptyExpression ();
1315 foreach (Type t in types){
1316 priv_fmee_ret.SetType (best);
1323 if (StandardConversionExists (priv_fmee_ret, t))
1331 // Used to avoid creating too many objects
1333 static EmptyExpression priv_fms_expr;
1336 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1337 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1338 /// for explicit and implicit conversion operators.
1340 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1341 bool apply_explicit_conv_rules,
1344 ArrayList src_types_set = new ArrayList ();
1346 if (priv_fms_expr == null)
1347 priv_fms_expr = new EmptyExpression ();
1350 // If any operator converts from S then Sx = S
1352 Type source_type = source.Type;
1353 foreach (MethodBase mb in me.Methods){
1354 ParameterData pd = Invocation.GetParameterData (mb);
1355 Type param_type = pd.ParameterType (0);
1357 if (param_type == source_type)
1360 if (apply_explicit_conv_rules) {
1363 // Find the set of applicable user-defined conversion operators, U. This set
1365 // user-defined implicit or explicit conversion operators declared by
1366 // the classes or structs in D that convert from a type encompassing
1367 // or encompassed by S to a type encompassing or encompassed by T
1369 priv_fms_expr.SetType (param_type);
1370 if (StandardConversionExists (priv_fms_expr, source_type))
1371 src_types_set.Add (param_type);
1373 if (StandardConversionExists (source, param_type))
1374 src_types_set.Add (param_type);
1378 // Only if S is encompassed by param_type
1380 if (StandardConversionExists (source, param_type))
1381 src_types_set.Add (param_type);
1386 // Explicit Conv rules
1388 if (apply_explicit_conv_rules) {
1389 ArrayList candidate_set = new ArrayList ();
1391 foreach (Type param_type in src_types_set){
1392 if (StandardConversionExists (source, param_type))
1393 candidate_set.Add (param_type);
1396 if (candidate_set.Count != 0)
1397 return FindMostEncompassedType (candidate_set);
1403 if (apply_explicit_conv_rules)
1404 return FindMostEncompassingType (src_types_set);
1406 return FindMostEncompassedType (src_types_set);
1410 // Useful in avoiding proliferation of objects
1412 static EmptyExpression priv_fmt_expr;
1415 /// Finds the most specific target Tx according to section 13.4.4
1417 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1418 bool apply_explicit_conv_rules,
1421 ArrayList tgt_types_set = new ArrayList ();
1423 if (priv_fmt_expr == null)
1424 priv_fmt_expr = new EmptyExpression ();
1427 // If any operator converts to T then Tx = T
1429 foreach (MethodInfo mi in me.Methods){
1430 Type ret_type = mi.ReturnType;
1432 if (ret_type == target)
1435 if (apply_explicit_conv_rules) {
1438 // Find the set of applicable user-defined conversion operators, U.
1440 // This set consists of the
1441 // user-defined implicit or explicit conversion operators declared by
1442 // the classes or structs in D that convert from a type encompassing
1443 // or encompassed by S to a type encompassing or encompassed by T
1445 priv_fms_expr.SetType (ret_type);
1446 if (StandardConversionExists (priv_fms_expr, target))
1447 tgt_types_set.Add (ret_type);
1449 priv_fms_expr.SetType (target);
1450 if (StandardConversionExists (priv_fms_expr, ret_type))
1451 tgt_types_set.Add (ret_type);
1455 // Only if T is encompassed by param_type
1457 priv_fms_expr.SetType (ret_type);
1458 if (StandardConversionExists (priv_fms_expr, target))
1459 tgt_types_set.Add (ret_type);
1464 // Explicit conv rules
1466 if (apply_explicit_conv_rules) {
1467 ArrayList candidate_set = new ArrayList ();
1469 foreach (Type ret_type in tgt_types_set){
1470 priv_fmt_expr.SetType (ret_type);
1472 if (StandardConversionExists (priv_fmt_expr, target))
1473 candidate_set.Add (ret_type);
1476 if (candidate_set.Count != 0)
1477 return FindMostEncompassingType (candidate_set);
1481 // Okay, final case !
1483 if (apply_explicit_conv_rules)
1484 return FindMostEncompassedType (tgt_types_set);
1486 return FindMostEncompassingType (tgt_types_set);
1490 /// User-defined Implicit conversions
1492 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1493 Type target, Location loc)
1495 return UserDefinedConversion (ec, source, target, loc, false);
1499 /// User-defined Explicit conversions
1501 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1502 Type target, Location loc)
1504 return UserDefinedConversion (ec, source, target, loc, true);
1508 /// Computes the MethodGroup for the user-defined conversion
1509 /// operators from source_type to target_type. `look_for_explicit'
1510 /// controls whether we should also include the list of explicit
1513 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1514 Type source_type, Type target_type,
1515 Location loc, bool look_for_explicit)
1517 Expression mg1 = null, mg2 = null;
1518 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1522 // FIXME : How does the False operator come into the picture ?
1523 // This doesn't look complete and very correct !
1525 if (target_type == TypeManager.bool_type && !look_for_explicit)
1526 op_name = "op_True";
1528 op_name = "op_Implicit";
1530 MethodGroupExpr union3;
1532 mg1 = MethodLookup (ec, source_type, op_name, loc);
1533 if (source_type.BaseType != null)
1534 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1537 union3 = (MethodGroupExpr) mg2;
1538 else if (mg2 == null)
1539 union3 = (MethodGroupExpr) mg1;
1541 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1543 mg1 = MethodLookup (ec, target_type, op_name, loc);
1546 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1548 union3 = (MethodGroupExpr) mg1;
1551 if (target_type.BaseType != null)
1552 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1556 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1558 union3 = (MethodGroupExpr) mg1;
1561 MethodGroupExpr union4 = null;
1563 if (look_for_explicit) {
1564 op_name = "op_Explicit";
1566 mg5 = MemberLookup (ec, source_type, op_name, loc);
1567 if (source_type.BaseType != null)
1568 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1570 mg7 = MemberLookup (ec, target_type, op_name, loc);
1571 if (target_type.BaseType != null)
1572 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1574 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1575 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1577 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1580 return Invocation.MakeUnionSet (union3, union4, loc);
1584 /// User-defined conversions
1586 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1587 Type target, Location loc,
1588 bool look_for_explicit)
1590 MethodGroupExpr union;
1591 Type source_type = source.Type;
1592 MethodBase method = null;
1594 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1598 Type most_specific_source, most_specific_target;
1601 foreach (MethodBase m in union.Methods){
1602 Console.WriteLine ("Name: " + m.Name);
1603 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1607 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1608 if (most_specific_source == null)
1611 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1612 if (most_specific_target == null)
1618 foreach (MethodBase mb in union.Methods){
1619 ParameterData pd = Invocation.GetParameterData (mb);
1620 MethodInfo mi = (MethodInfo) mb;
1622 if (pd.ParameterType (0) == most_specific_source &&
1623 mi.ReturnType == most_specific_target) {
1629 if (method == null || count > 1)
1634 // This will do the conversion to the best match that we
1635 // found. Now we need to perform an implict standard conversion
1636 // if the best match was not the type that we were requested
1639 if (look_for_explicit)
1640 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1642 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1648 e = new UserCast ((MethodInfo) method, source, loc);
1649 if (e.Type != target){
1650 if (!look_for_explicit)
1651 e = ConvertImplicitStandard (ec, e, target, loc);
1653 e = ConvertExplicitStandard (ec, e, target, loc);
1660 /// Converts implicitly the resolved expression `expr' into the
1661 /// `target_type'. It returns a new expression that can be used
1662 /// in a context that expects a `target_type'.
1664 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1665 Type target_type, Location loc)
1667 Type expr_type = expr.Type;
1670 if (expr_type == target_type)
1673 if (target_type == null)
1674 throw new Exception ("Target type is null");
1676 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1680 e = ImplicitUserConversion (ec, expr, target_type, loc);
1689 /// Attempts to apply the `Standard Implicit
1690 /// Conversion' rules to the expression `expr' into
1691 /// the `target_type'. It returns a new expression
1692 /// that can be used in a context that expects a
1695 /// This is different from `ConvertImplicit' in that the
1696 /// user defined implicit conversions are excluded.
1698 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1699 Type target_type, Location loc)
1701 Type expr_type = expr.Type;
1704 if (expr_type == target_type)
1707 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1711 e = ImplicitReferenceConversion (expr, target_type);
1715 if ((target_type == TypeManager.enum_type ||
1716 target_type.IsSubclassOf (TypeManager.enum_type)) &&
1717 expr is IntLiteral){
1718 IntLiteral i = (IntLiteral) expr;
1721 return new EnumConstant ((Constant) expr, target_type);
1725 if (expr_type.IsPointer){
1726 if (target_type == TypeManager.void_ptr_type)
1727 return new EmptyCast (expr, target_type);
1730 // yep, comparing pointer types cant be done with
1731 // t1 == t2, we have to compare their element types.
1733 if (target_type.IsPointer){
1734 if (target_type.GetElementType() == expr_type.GetElementType())
1739 if (target_type.IsPointer) {
1740 if (expr is NullLiteral)
1741 return new EmptyCast (expr, target_type);
1743 if (expr_type == TypeManager.void_ptr_type)
1744 return new EmptyCast (expr, target_type);
1752 /// Attemps to perform an implict constant conversion of the IntConstant
1753 /// into a different data type using casts (See Implicit Constant
1754 /// Expression Conversions)
1756 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1758 int value = ic.Value;
1760 if (target_type == TypeManager.sbyte_type){
1761 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1762 return new SByteConstant ((sbyte) value);
1763 } else if (target_type == TypeManager.byte_type){
1764 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1765 return new ByteConstant ((byte) value);
1766 } else if (target_type == TypeManager.short_type){
1767 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1768 return new ShortConstant ((short) value);
1769 } else if (target_type == TypeManager.ushort_type){
1770 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1771 return new UShortConstant ((ushort) value);
1772 } else if (target_type == TypeManager.uint32_type){
1774 return new UIntConstant ((uint) value);
1775 } else if (target_type == TypeManager.uint64_type){
1777 // we can optimize this case: a positive int32
1778 // always fits on a uint64. But we need an opcode
1782 return new ULongConstant ((ulong) value);
1783 } else if (target_type == TypeManager.double_type)
1784 return new DoubleConstant ((double) value);
1785 else if (target_type == TypeManager.float_type)
1786 return new FloatConstant ((float) value);
1788 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1789 Type underlying = TypeManager.EnumToUnderlying (target_type);
1790 Constant e = (Constant) ic;
1793 // Possibly, we need to create a different 0 literal before passing
1796 if (underlying == TypeManager.int64_type)
1797 e = new LongLiteral (0);
1798 else if (underlying == TypeManager.uint64_type)
1799 e = new ULongLiteral (0);
1801 return new EnumConstant (e, target_type);
1806 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1808 string msg = "Cannot convert implicitly from `"+
1809 TypeManager.CSharpName (source) + "' to `" +
1810 TypeManager.CSharpName (target) + "'";
1812 Report.Error (29, loc, msg);
1816 /// Attemptes to implicityly convert `target' into `type', using
1817 /// ConvertImplicit. If there is no implicit conversion, then
1818 /// an error is signaled
1820 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1821 Type target_type, Location loc)
1825 e = ConvertImplicit (ec, source, target_type, loc);
1829 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1830 Report.Error (664, loc,
1831 "Double literal cannot be implicitly converted to " +
1832 "float type, use F suffix to create a float literal");
1835 Error_CannotConvertImplicit (loc, source.Type, target_type);
1841 /// Performs the explicit numeric conversions
1843 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1845 Type expr_type = expr.Type;
1848 // If we have an enumeration, extract the underlying type,
1849 // use this during the comparison, but wrap around the original
1852 Type real_target_type = target_type;
1854 if (TypeManager.IsEnumType (real_target_type))
1855 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1857 if (StandardConversionExists (expr, real_target_type)){
1858 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1860 if (real_target_type != target_type)
1861 return new EmptyCast (ce, target_type);
1865 if (expr_type == TypeManager.sbyte_type){
1867 // From sbyte to byte, ushort, uint, ulong, char
1869 if (real_target_type == TypeManager.byte_type)
1870 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1871 if (real_target_type == TypeManager.ushort_type)
1872 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1873 if (real_target_type == TypeManager.uint32_type)
1874 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1875 if (real_target_type == TypeManager.uint64_type)
1876 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1877 if (real_target_type == TypeManager.char_type)
1878 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1879 } else if (expr_type == TypeManager.byte_type){
1881 // From byte to sbyte and char
1883 if (real_target_type == TypeManager.sbyte_type)
1884 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1885 if (real_target_type == TypeManager.char_type)
1886 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1887 } else if (expr_type == TypeManager.short_type){
1889 // From short to sbyte, byte, ushort, uint, ulong, char
1891 if (real_target_type == TypeManager.sbyte_type)
1892 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1893 if (real_target_type == TypeManager.byte_type)
1894 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1895 if (real_target_type == TypeManager.ushort_type)
1896 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1897 if (real_target_type == TypeManager.uint32_type)
1898 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1899 if (real_target_type == TypeManager.uint64_type)
1900 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1901 if (real_target_type == TypeManager.char_type)
1902 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1903 } else if (expr_type == TypeManager.ushort_type){
1905 // From ushort to sbyte, byte, short, char
1907 if (real_target_type == TypeManager.sbyte_type)
1908 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1909 if (real_target_type == TypeManager.byte_type)
1910 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1911 if (real_target_type == TypeManager.short_type)
1912 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1913 if (real_target_type == TypeManager.char_type)
1914 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1915 } else if (expr_type == TypeManager.int32_type){
1917 // From int to sbyte, byte, short, ushort, uint, ulong, char
1919 if (real_target_type == TypeManager.sbyte_type)
1920 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1921 if (real_target_type == TypeManager.byte_type)
1922 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1923 if (real_target_type == TypeManager.short_type)
1924 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1925 if (real_target_type == TypeManager.ushort_type)
1926 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1927 if (real_target_type == TypeManager.uint32_type)
1928 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1929 if (real_target_type == TypeManager.uint64_type)
1930 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1931 if (real_target_type == TypeManager.char_type)
1932 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1933 } else if (expr_type == TypeManager.uint32_type){
1935 // From uint to sbyte, byte, short, ushort, int, char
1937 if (real_target_type == TypeManager.sbyte_type)
1938 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1939 if (real_target_type == TypeManager.byte_type)
1940 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1941 if (real_target_type == TypeManager.short_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1943 if (real_target_type == TypeManager.ushort_type)
1944 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1945 if (real_target_type == TypeManager.int32_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1947 if (real_target_type == TypeManager.char_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1949 } else if (expr_type == TypeManager.int64_type){
1951 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1953 if (real_target_type == TypeManager.sbyte_type)
1954 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1955 if (real_target_type == TypeManager.byte_type)
1956 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1957 if (real_target_type == TypeManager.short_type)
1958 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1959 if (real_target_type == TypeManager.ushort_type)
1960 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1961 if (real_target_type == TypeManager.int32_type)
1962 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1963 if (real_target_type == TypeManager.uint32_type)
1964 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1965 if (real_target_type == TypeManager.uint64_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1967 if (real_target_type == TypeManager.char_type)
1968 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1969 } else if (expr_type == TypeManager.uint64_type){
1971 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1973 if (real_target_type == TypeManager.sbyte_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1975 if (real_target_type == TypeManager.byte_type)
1976 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1977 if (real_target_type == TypeManager.short_type)
1978 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1979 if (real_target_type == TypeManager.ushort_type)
1980 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1981 if (real_target_type == TypeManager.int32_type)
1982 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1983 if (real_target_type == TypeManager.uint32_type)
1984 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1985 if (real_target_type == TypeManager.int64_type)
1986 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1987 if (real_target_type == TypeManager.char_type)
1988 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1989 } else if (expr_type == TypeManager.char_type){
1991 // From char to sbyte, byte, short
1993 if (real_target_type == TypeManager.sbyte_type)
1994 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1995 if (real_target_type == TypeManager.byte_type)
1996 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1997 if (real_target_type == TypeManager.short_type)
1998 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1999 } else if (expr_type == TypeManager.float_type){
2001 // From float to sbyte, byte, short,
2002 // ushort, int, uint, long, ulong, char
2005 if (real_target_type == TypeManager.sbyte_type)
2006 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2007 if (real_target_type == TypeManager.byte_type)
2008 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2009 if (real_target_type == TypeManager.short_type)
2010 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2011 if (real_target_type == TypeManager.ushort_type)
2012 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2013 if (real_target_type == TypeManager.int32_type)
2014 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2015 if (real_target_type == TypeManager.uint32_type)
2016 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2017 if (real_target_type == TypeManager.int64_type)
2018 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2019 if (real_target_type == TypeManager.uint64_type)
2020 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2021 if (real_target_type == TypeManager.char_type)
2022 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2023 } else if (expr_type == TypeManager.double_type){
2025 // From double to byte, byte, short,
2026 // ushort, int, uint, long, ulong,
2027 // char, float or decimal
2029 if (real_target_type == TypeManager.sbyte_type)
2030 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2031 if (real_target_type == TypeManager.byte_type)
2032 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2033 if (real_target_type == TypeManager.short_type)
2034 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2035 if (real_target_type == TypeManager.ushort_type)
2036 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2037 if (real_target_type == TypeManager.int32_type)
2038 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2039 if (real_target_type == TypeManager.uint32_type)
2040 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2041 if (real_target_type == TypeManager.int64_type)
2042 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2043 if (real_target_type == TypeManager.uint64_type)
2044 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2045 if (real_target_type == TypeManager.char_type)
2046 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2047 if (real_target_type == TypeManager.float_type)
2048 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2051 // decimal is taken care of by the op_Explicit methods.
2057 /// Returns whether an explicit reference conversion can be performed
2058 /// from source_type to target_type
2060 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2062 bool target_is_value_type = target_type.IsValueType;
2064 if (source_type == target_type)
2068 // From object to any reference type
2070 if (source_type == TypeManager.object_type && !target_is_value_type)
2074 // From any class S to any class-type T, provided S is a base class of T
2076 if (target_type.IsSubclassOf (source_type))
2080 // From any interface type S to any interface T provided S is not derived from T
2082 if (source_type.IsInterface && target_type.IsInterface){
2083 if (!target_type.IsSubclassOf (source_type))
2088 // From any class type S to any interface T, provided S is not sealed
2089 // and provided S does not implement T.
2091 if (target_type.IsInterface && !source_type.IsSealed &&
2092 !TypeManager.ImplementsInterface (source_type, target_type))
2096 // From any interface-type S to to any class type T, provided T is not
2097 // sealed, or provided T implements S.
2099 if (source_type.IsInterface &&
2100 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2104 // From an array type S with an element type Se to an array type T with an
2105 // element type Te provided all the following are true:
2106 // * S and T differe only in element type, in other words, S and T
2107 // have the same number of dimensions.
2108 // * Both Se and Te are reference types
2109 // * An explicit referenc conversions exist from Se to Te
2111 if (source_type.IsArray && target_type.IsArray) {
2112 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2114 Type source_element_type = source_type.GetElementType ();
2115 Type target_element_type = target_type.GetElementType ();
2117 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2118 if (ExplicitReferenceConversionExists (source_element_type,
2119 target_element_type))
2125 // From System.Array to any array-type
2126 if (source_type == TypeManager.array_type &&
2127 target_type.IsArray){
2132 // From System delegate to any delegate-type
2134 if (source_type == TypeManager.delegate_type &&
2135 target_type.IsSubclassOf (TypeManager.delegate_type))
2139 // From ICloneable to Array or Delegate types
2141 if (source_type == TypeManager.icloneable_type &&
2142 (target_type == TypeManager.array_type ||
2143 target_type == TypeManager.delegate_type))
2150 /// Implements Explicit Reference conversions
2152 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2154 Type source_type = source.Type;
2155 bool target_is_value_type = target_type.IsValueType;
2158 // From object to any reference type
2160 if (source_type == TypeManager.object_type && !target_is_value_type)
2161 return new ClassCast (source, target_type);
2165 // From any class S to any class-type T, provided S is a base class of T
2167 if (target_type.IsSubclassOf (source_type))
2168 return new ClassCast (source, target_type);
2171 // From any interface type S to any interface T provided S is not derived from T
2173 if (source_type.IsInterface && target_type.IsInterface){
2174 if (TypeManager.ImplementsInterface (source_type, target_type))
2177 return new ClassCast (source, target_type);
2181 // From any class type S to any interface T, provides S is not sealed
2182 // and provided S does not implement T.
2184 if (target_type.IsInterface && !source_type.IsSealed) {
2185 if (TypeManager.ImplementsInterface (source_type, target_type))
2188 return new ClassCast (source, target_type);
2193 // From any interface-type S to to any class type T, provided T is not
2194 // sealed, or provided T implements S.
2196 if (source_type.IsInterface) {
2197 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2198 return new ClassCast (source, target_type);
2203 // From an array type S with an element type Se to an array type T with an
2204 // element type Te provided all the following are true:
2205 // * S and T differe only in element type, in other words, S and T
2206 // have the same number of dimensions.
2207 // * Both Se and Te are reference types
2208 // * An explicit referenc conversions exist from Se to Te
2210 if (source_type.IsArray && target_type.IsArray) {
2211 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2213 Type source_element_type = source_type.GetElementType ();
2214 Type target_element_type = target_type.GetElementType ();
2216 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2217 if (ExplicitReferenceConversionExists (source_element_type,
2218 target_element_type))
2219 return new ClassCast (source, target_type);
2224 // From System.Array to any array-type
2225 if (source_type == TypeManager.array_type &&
2226 target_type.IsArray) {
2227 return new ClassCast (source, target_type);
2231 // From System delegate to any delegate-type
2233 if (source_type == TypeManager.delegate_type &&
2234 target_type.IsSubclassOf (TypeManager.delegate_type))
2235 return new ClassCast (source, target_type);
2238 // From ICloneable to Array or Delegate types
2240 if (source_type == TypeManager.icloneable_type &&
2241 (target_type == TypeManager.array_type ||
2242 target_type == TypeManager.delegate_type))
2243 return new ClassCast (source, target_type);
2249 /// Performs an explicit conversion of the expression `expr' whose
2250 /// type is expr.Type to `target_type'.
2252 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2253 Type target_type, Location loc)
2255 Type expr_type = expr.Type;
2256 Type original_expr_type = expr_type;
2258 if (expr_type.IsSubclassOf (TypeManager.enum_type)){
2259 if (target_type == TypeManager.enum_type ||
2260 target_type == TypeManager.object_type) {
2261 if (expr is EnumConstant)
2262 expr = ((EnumConstant) expr).Child;
2263 // We really need all these casts here .... :-(
2264 expr = new BoxedCast (new EmptyCast (expr, expr_type));
2265 return new EmptyCast (expr, target_type);
2266 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
2267 target_type.IsSubclassOf (TypeManager.enum_type))
2268 return new UnboxCast (expr, target_type);
2271 // Notice that we have kept the expr_type unmodified, which is only
2273 if (expr is EnumConstant)
2274 expr = ((EnumConstant) expr).Child;
2276 expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2277 expr_type = expr.Type;
2280 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2285 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2290 // Unboxing conversion.
2292 if (expr_type == TypeManager.object_type && target_type.IsValueType){
2293 if (expr is NullLiteral){
2294 Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
2297 return new UnboxCast (expr, target_type);
2301 ne = ConvertReferenceExplicit (expr, target_type);
2306 if (target_type.IsPointer){
2307 if (expr_type.IsPointer)
2308 return new EmptyCast (expr, target_type);
2310 if (expr_type == TypeManager.sbyte_type ||
2311 expr_type == TypeManager.byte_type ||
2312 expr_type == TypeManager.short_type ||
2313 expr_type == TypeManager.ushort_type ||
2314 expr_type == TypeManager.int32_type ||
2315 expr_type == TypeManager.uint32_type ||
2316 expr_type == TypeManager.uint64_type ||
2317 expr_type == TypeManager.int64_type)
2318 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2320 if (expr_type.IsPointer){
2321 if (target_type == TypeManager.sbyte_type ||
2322 target_type == TypeManager.byte_type ||
2323 target_type == TypeManager.short_type ||
2324 target_type == TypeManager.ushort_type ||
2325 target_type == TypeManager.int32_type ||
2326 target_type == TypeManager.uint32_type ||
2327 target_type == TypeManager.uint64_type ||
2328 target_type == TypeManager.int64_type){
2329 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2332 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2337 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2341 // We should always be able to go from an uint32
2342 // implicitly or explicitly to the other integral
2345 throw new Exception ("Internal compiler error");
2350 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2354 Error_CannotConvertType (loc, original_expr_type, target_type);
2359 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2361 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2362 Type target_type, Location l)
2364 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2369 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2373 ne = ConvertReferenceExplicit (expr, target_type);
2377 Error_CannotConvertType (l, expr.Type, target_type);
2381 static string ExprClassName (ExprClass c)
2384 case ExprClass.Invalid:
2386 case ExprClass.Value:
2388 case ExprClass.Variable:
2390 case ExprClass.Namespace:
2392 case ExprClass.Type:
2394 case ExprClass.MethodGroup:
2395 return "method group";
2396 case ExprClass.PropertyAccess:
2397 return "property access";
2398 case ExprClass.EventAccess:
2399 return "event access";
2400 case ExprClass.IndexerAccess:
2401 return "indexer access";
2402 case ExprClass.Nothing:
2405 throw new Exception ("Should not happen");
2409 /// Reports that we were expecting `expr' to be of class `expected'
2411 public void Error118 (string expected)
2413 string kind = "Unknown";
2415 kind = ExprClassName (eclass);
2417 Error (118, "Expression denotes a `" + kind +
2418 "' where a `" + expected + "' was expected");
2421 public void Error118 (ResolveFlags flags)
2423 ArrayList valid = new ArrayList (10);
2425 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2426 valid.Add ("variable");
2427 valid.Add ("value");
2430 if ((flags & ResolveFlags.Type) != 0)
2433 if ((flags & ResolveFlags.MethodGroup) != 0)
2434 valid.Add ("method group");
2436 if ((flags & ResolveFlags.SimpleName) != 0)
2437 valid.Add ("simple name");
2439 if (valid.Count == 0)
2440 valid.Add ("unknown");
2442 StringBuilder sb = new StringBuilder ();
2443 for (int i = 0; i < valid.Count; i++) {
2446 else if (i == valid.Count)
2448 sb.Append (valid [i]);
2451 string kind = ExprClassName (eclass);
2453 Error (119, "Expression denotes a `" + kind + "' where " +
2454 "a `" + sb.ToString () + "' was expected");
2457 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2459 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
2460 TypeManager.CSharpName (t));
2463 public static void UnsafeError (Location loc)
2465 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2469 /// Converts the IntConstant, UIntConstant, LongConstant or
2470 /// ULongConstant into the integral target_type. Notice
2471 /// that we do not return an `Expression' we do return
2472 /// a boxed integral type.
2474 /// FIXME: Since I added the new constants, we need to
2475 /// also support conversions from CharConstant, ByteConstant,
2476 /// SByteConstant, UShortConstant, ShortConstant
2478 /// This is used by the switch statement, so the domain
2479 /// of work is restricted to the literals above, and the
2480 /// targets are int32, uint32, char, byte, sbyte, ushort,
2481 /// short, uint64 and int64
2483 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2487 if (c.Type == target_type)
2488 return ((Constant) c).GetValue ();
2491 // Make into one of the literals we handle, we dont really care
2492 // about this value as we will just return a few limited types
2494 if (c is EnumConstant)
2495 c = ((EnumConstant)c).WidenToCompilerConstant ();
2497 if (c is IntConstant){
2498 int v = ((IntConstant) c).Value;
2500 if (target_type == TypeManager.uint32_type){
2503 } else if (target_type == TypeManager.char_type){
2504 if (v >= Char.MinValue && v <= Char.MaxValue)
2506 } else if (target_type == TypeManager.byte_type){
2507 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2509 } else if (target_type == TypeManager.sbyte_type){
2510 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2512 } else if (target_type == TypeManager.short_type){
2513 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2515 } else if (target_type == TypeManager.ushort_type){
2516 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2518 } else if (target_type == TypeManager.int64_type)
2520 else if (target_type == TypeManager.uint64_type){
2526 } else if (c is UIntConstant){
2527 uint v = ((UIntConstant) c).Value;
2529 if (target_type == TypeManager.int32_type){
2530 if (v <= Int32.MaxValue)
2532 } else if (target_type == TypeManager.char_type){
2533 if (v >= Char.MinValue && v <= Char.MaxValue)
2535 } else if (target_type == TypeManager.byte_type){
2536 if (v <= Byte.MaxValue)
2538 } else if (target_type == TypeManager.sbyte_type){
2539 if (v <= SByte.MaxValue)
2541 } else if (target_type == TypeManager.short_type){
2542 if (v <= UInt16.MaxValue)
2544 } else if (target_type == TypeManager.ushort_type){
2545 if (v <= UInt16.MaxValue)
2547 } else if (target_type == TypeManager.int64_type)
2549 else if (target_type == TypeManager.uint64_type)
2552 } else if (c is LongConstant){
2553 long v = ((LongConstant) c).Value;
2555 if (target_type == TypeManager.int32_type){
2556 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2558 } else if (target_type == TypeManager.uint32_type){
2559 if (v >= 0 && v <= UInt32.MaxValue)
2561 } else if (target_type == TypeManager.char_type){
2562 if (v >= Char.MinValue && v <= Char.MaxValue)
2564 } else if (target_type == TypeManager.byte_type){
2565 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2567 } else if (target_type == TypeManager.sbyte_type){
2568 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2570 } else if (target_type == TypeManager.short_type){
2571 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2573 } else if (target_type == TypeManager.ushort_type){
2574 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2576 } else if (target_type == TypeManager.uint64_type){
2581 } else if (c is ULongConstant){
2582 ulong v = ((ULongConstant) c).Value;
2584 if (target_type == TypeManager.int32_type){
2585 if (v <= Int32.MaxValue)
2587 } else if (target_type == TypeManager.uint32_type){
2588 if (v <= UInt32.MaxValue)
2590 } else if (target_type == TypeManager.char_type){
2591 if (v >= Char.MinValue && v <= Char.MaxValue)
2593 } else if (target_type == TypeManager.byte_type){
2594 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2596 } else if (target_type == TypeManager.sbyte_type){
2597 if (v <= (int) SByte.MaxValue)
2599 } else if (target_type == TypeManager.short_type){
2600 if (v <= UInt16.MaxValue)
2602 } else if (target_type == TypeManager.ushort_type){
2603 if (v <= UInt16.MaxValue)
2605 } else if (target_type == TypeManager.int64_type){
2606 if (v <= Int64.MaxValue)
2610 } else if (c is ByteConstant){
2611 byte v = ((ByteConstant) c).Value;
2613 if (target_type == TypeManager.int32_type)
2615 else if (target_type == TypeManager.uint32_type)
2617 else if (target_type == TypeManager.char_type)
2619 else if (target_type == TypeManager.sbyte_type){
2620 if (v <= SByte.MaxValue)
2622 } else if (target_type == TypeManager.short_type)
2624 else if (target_type == TypeManager.ushort_type)
2626 else if (target_type == TypeManager.int64_type)
2628 else if (target_type == TypeManager.uint64_type)
2631 } else if (c is SByteConstant){
2632 sbyte v = ((SByteConstant) c).Value;
2634 if (target_type == TypeManager.int32_type)
2636 else if (target_type == TypeManager.uint32_type){
2639 } else if (target_type == TypeManager.char_type){
2642 } else if (target_type == TypeManager.byte_type){
2645 } else if (target_type == TypeManager.short_type)
2647 else if (target_type == TypeManager.ushort_type){
2650 } else if (target_type == TypeManager.int64_type)
2652 else if (target_type == TypeManager.uint64_type){
2657 } else if (c is ShortConstant){
2658 short v = ((ShortConstant) c).Value;
2660 if (target_type == TypeManager.int32_type){
2662 } else if (target_type == TypeManager.uint32_type){
2665 } else if (target_type == TypeManager.char_type){
2668 } else if (target_type == TypeManager.byte_type){
2669 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2671 } else if (target_type == TypeManager.sbyte_type){
2672 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2674 } else if (target_type == TypeManager.ushort_type){
2677 } else if (target_type == TypeManager.int64_type)
2679 else if (target_type == TypeManager.uint64_type)
2683 } else if (c is UShortConstant){
2684 ushort v = ((UShortConstant) c).Value;
2686 if (target_type == TypeManager.int32_type)
2688 else if (target_type == TypeManager.uint32_type)
2690 else if (target_type == TypeManager.char_type){
2691 if (v >= Char.MinValue && v <= Char.MaxValue)
2693 } else if (target_type == TypeManager.byte_type){
2694 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2696 } else if (target_type == TypeManager.sbyte_type){
2697 if (v <= SByte.MaxValue)
2699 } else if (target_type == TypeManager.short_type){
2700 if (v <= Int16.MaxValue)
2702 } else if (target_type == TypeManager.int64_type)
2704 else if (target_type == TypeManager.uint64_type)
2708 } else if (c is CharConstant){
2709 char v = ((CharConstant) c).Value;
2711 if (target_type == TypeManager.int32_type)
2713 else if (target_type == TypeManager.uint32_type)
2715 else if (target_type == TypeManager.byte_type){
2716 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2718 } else if (target_type == TypeManager.sbyte_type){
2719 if (v <= SByte.MaxValue)
2721 } else if (target_type == TypeManager.short_type){
2722 if (v <= Int16.MaxValue)
2724 } else if (target_type == TypeManager.ushort_type)
2726 else if (target_type == TypeManager.int64_type)
2728 else if (target_type == TypeManager.uint64_type)
2733 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2738 // Load the object from the pointer.
2740 public static void LoadFromPtr (ILGenerator ig, Type t)
2742 if (t == TypeManager.int32_type)
2743 ig.Emit (OpCodes.Ldind_I4);
2744 else if (t == TypeManager.uint32_type)
2745 ig.Emit (OpCodes.Ldind_U4);
2746 else if (t == TypeManager.short_type)
2747 ig.Emit (OpCodes.Ldind_I2);
2748 else if (t == TypeManager.ushort_type)
2749 ig.Emit (OpCodes.Ldind_U2);
2750 else if (t == TypeManager.char_type)
2751 ig.Emit (OpCodes.Ldind_U2);
2752 else if (t == TypeManager.byte_type)
2753 ig.Emit (OpCodes.Ldind_U1);
2754 else if (t == TypeManager.sbyte_type)
2755 ig.Emit (OpCodes.Ldind_I1);
2756 else if (t == TypeManager.uint64_type)
2757 ig.Emit (OpCodes.Ldind_I8);
2758 else if (t == TypeManager.int64_type)
2759 ig.Emit (OpCodes.Ldind_I8);
2760 else if (t == TypeManager.float_type)
2761 ig.Emit (OpCodes.Ldind_R4);
2762 else if (t == TypeManager.double_type)
2763 ig.Emit (OpCodes.Ldind_R8);
2764 else if (t == TypeManager.bool_type)
2765 ig.Emit (OpCodes.Ldind_I1);
2766 else if (t == TypeManager.intptr_type)
2767 ig.Emit (OpCodes.Ldind_I);
2768 else if (TypeManager.IsEnumType (t)) {
2769 if (t == TypeManager.enum_type)
2770 ig.Emit (OpCodes.Ldind_Ref);
2772 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2773 } else if (t.IsValueType)
2774 ig.Emit (OpCodes.Ldobj, t);
2776 ig.Emit (OpCodes.Ldind_Ref);
2780 // The stack contains the pointer and the value of type `type'
2782 public static void StoreFromPtr (ILGenerator ig, Type type)
2784 if (TypeManager.IsEnumType (type))
2785 type = TypeManager.EnumToUnderlying (type);
2786 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2787 ig.Emit (OpCodes.Stind_I4);
2788 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2789 ig.Emit (OpCodes.Stind_I8);
2790 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2791 type == TypeManager.ushort_type)
2792 ig.Emit (OpCodes.Stind_I2);
2793 else if (type == TypeManager.float_type)
2794 ig.Emit (OpCodes.Stind_R4);
2795 else if (type == TypeManager.double_type)
2796 ig.Emit (OpCodes.Stind_R8);
2797 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2798 type == TypeManager.bool_type)
2799 ig.Emit (OpCodes.Stind_I1);
2800 else if (type == TypeManager.intptr_type)
2801 ig.Emit (OpCodes.Stind_I);
2802 else if (type.IsValueType)
2803 ig.Emit (OpCodes.Stobj, type);
2805 ig.Emit (OpCodes.Stind_Ref);
2809 // Returns the size of type `t' if known, otherwise, 0
2811 public static int GetTypeSize (Type t)
2813 t = TypeManager.TypeToCoreType (t);
2814 if (t == TypeManager.int32_type ||
2815 t == TypeManager.uint32_type ||
2816 t == TypeManager.float_type)
2818 else if (t == TypeManager.int64_type ||
2819 t == TypeManager.uint64_type ||
2820 t == TypeManager.double_type)
2822 else if (t == TypeManager.byte_type ||
2823 t == TypeManager.sbyte_type ||
2824 t == TypeManager.bool_type)
2826 else if (t == TypeManager.short_type ||
2827 t == TypeManager.char_type ||
2828 t == TypeManager.ushort_type)
2830 else if (t == TypeManager.decimal_type)
2837 // Default implementation of IAssignMethod.CacheTemporaries
2839 public void CacheTemporaries (EmitContext ec)
2843 static void Error_NegativeArrayIndex (Location loc)
2845 Report.Error (284, loc, "Can not create array with a negative size");
2849 // Converts `source' to an int, uint, long or ulong.
2851 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2855 bool old_checked = ec.CheckState;
2856 ec.CheckState = true;
2858 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2859 if (target == null){
2860 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2861 if (target == null){
2862 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2863 if (target == null){
2864 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2866 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2870 ec.CheckState = old_checked;
2873 // Only positive constants are allowed at compile time
2875 if (target is Constant){
2876 if (target is IntConstant){
2877 if (((IntConstant) target).Value < 0){
2878 Error_NegativeArrayIndex (loc);
2883 if (target is LongConstant){
2884 if (((LongConstant) target).Value < 0){
2885 Error_NegativeArrayIndex (loc);
2898 /// This is just a base class for expressions that can
2899 /// appear on statements (invocations, object creation,
2900 /// assignments, post/pre increment and decrement). The idea
2901 /// being that they would support an extra Emition interface that
2902 /// does not leave a result on the stack.
2904 public abstract class ExpressionStatement : Expression {
2907 /// Requests the expression to be emitted in a `statement'
2908 /// context. This means that no new value is left on the
2909 /// stack after invoking this method (constrasted with
2910 /// Emit that will always leave a value on the stack).
2912 public abstract void EmitStatement (EmitContext ec);
2916 /// This kind of cast is used to encapsulate the child
2917 /// whose type is child.Type into an expression that is
2918 /// reported to return "return_type". This is used to encapsulate
2919 /// expressions which have compatible types, but need to be dealt
2920 /// at higher levels with.
2922 /// For example, a "byte" expression could be encapsulated in one
2923 /// of these as an "unsigned int". The type for the expression
2924 /// would be "unsigned int".
2927 public class EmptyCast : Expression {
2928 protected Expression child;
2930 public EmptyCast (Expression child, Type return_type)
2932 eclass = child.eclass;
2937 public override Expression DoResolve (EmitContext ec)
2939 // This should never be invoked, we are born in fully
2940 // initialized state.
2945 public override void Emit (EmitContext ec)
2952 /// This class is used to wrap literals which belong inside Enums
2954 public class EnumConstant : Constant {
2955 public Constant Child;
2957 public EnumConstant (Constant child, Type enum_type)
2959 eclass = child.eclass;
2964 public override Expression DoResolve (EmitContext ec)
2966 // This should never be invoked, we are born in fully
2967 // initialized state.
2972 public override void Emit (EmitContext ec)
2977 public override object GetValue ()
2979 return Child.GetValue ();
2983 // Converts from one of the valid underlying types for an enumeration
2984 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
2985 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
2987 public Constant WidenToCompilerConstant ()
2989 Type t = TypeManager.EnumToUnderlying (Child.Type);
2990 object v = ((Constant) Child).GetValue ();;
2992 if (t == TypeManager.int32_type)
2993 return new IntConstant ((int) v);
2994 if (t == TypeManager.uint32_type)
2995 return new UIntConstant ((uint) v);
2996 if (t == TypeManager.int64_type)
2997 return new LongConstant ((long) v);
2998 if (t == TypeManager.uint64_type)
2999 return new ULongConstant ((ulong) v);
3000 if (t == TypeManager.short_type)
3001 return new ShortConstant ((short) v);
3002 if (t == TypeManager.ushort_type)
3003 return new UShortConstant ((ushort) v);
3004 if (t == TypeManager.byte_type)
3005 return new ByteConstant ((byte) v);
3006 if (t == TypeManager.sbyte_type)
3007 return new SByteConstant ((sbyte) v);
3009 throw new Exception ("Invalid enumeration underlying type: " + t);
3013 // Extracts the value in the enumeration on its native representation
3015 public object GetPlainValue ()
3017 Type t = TypeManager.EnumToUnderlying (Child.Type);
3018 object v = ((Constant) Child).GetValue ();;
3020 if (t == TypeManager.int32_type)
3022 if (t == TypeManager.uint32_type)
3024 if (t == TypeManager.int64_type)
3026 if (t == TypeManager.uint64_type)
3028 if (t == TypeManager.short_type)
3030 if (t == TypeManager.ushort_type)
3032 if (t == TypeManager.byte_type)
3034 if (t == TypeManager.sbyte_type)
3040 public override string AsString ()
3042 return Child.AsString ();
3045 public override DoubleConstant ConvertToDouble ()
3047 return Child.ConvertToDouble ();
3050 public override FloatConstant ConvertToFloat ()
3052 return Child.ConvertToFloat ();
3055 public override ULongConstant ConvertToULong ()
3057 return Child.ConvertToULong ();
3060 public override LongConstant ConvertToLong ()
3062 return Child.ConvertToLong ();
3065 public override UIntConstant ConvertToUInt ()
3067 return Child.ConvertToUInt ();
3070 public override IntConstant ConvertToInt ()
3072 return Child.ConvertToInt ();
3077 /// This kind of cast is used to encapsulate Value Types in objects.
3079 /// The effect of it is to box the value type emitted by the previous
3082 public class BoxedCast : EmptyCast {
3084 public BoxedCast (Expression expr)
3085 : base (expr, TypeManager.object_type)
3089 public override Expression DoResolve (EmitContext ec)
3091 // This should never be invoked, we are born in fully
3092 // initialized state.
3097 public override void Emit (EmitContext ec)
3101 ec.ig.Emit (OpCodes.Box, child.Type);
3105 public class UnboxCast : EmptyCast {
3106 public UnboxCast (Expression expr, Type return_type)
3107 : base (expr, return_type)
3111 public override Expression DoResolve (EmitContext ec)
3113 // This should never be invoked, we are born in fully
3114 // initialized state.
3119 public override void Emit (EmitContext ec)
3122 ILGenerator ig = ec.ig;
3125 ig.Emit (OpCodes.Unbox, t);
3127 LoadFromPtr (ig, t);
3132 /// This is used to perform explicit numeric conversions.
3134 /// Explicit numeric conversions might trigger exceptions in a checked
3135 /// context, so they should generate the conv.ovf opcodes instead of
3138 public class ConvCast : EmptyCast {
3139 public enum Mode : byte {
3140 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3142 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3143 U2_I1, U2_U1, U2_I2, U2_CH,
3144 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3145 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3146 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3147 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3148 CH_I1, CH_U1, CH_I2,
3149 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3150 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3156 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3157 : base (child, return_type)
3159 checked_state = ec.CheckState;
3163 public override Expression DoResolve (EmitContext ec)
3165 // This should never be invoked, we are born in fully
3166 // initialized state.
3171 public override void Emit (EmitContext ec)
3173 ILGenerator ig = ec.ig;
3179 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3180 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3181 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3182 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3183 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3185 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3186 case Mode.U1_CH: /* nothing */ break;
3188 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3189 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3190 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3191 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3192 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3193 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3195 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3196 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3197 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3198 case Mode.U2_CH: /* nothing */ break;
3200 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3201 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3202 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3203 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3204 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3205 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3206 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3208 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3209 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3210 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3211 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3212 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3213 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3215 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3216 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3217 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3218 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3219 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3220 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3221 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3222 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3224 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3225 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3226 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3227 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3228 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3229 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3230 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3231 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3233 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3234 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3235 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3237 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3238 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3239 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3240 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3241 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3242 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3243 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3244 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3245 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3247 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3248 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3249 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3250 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3251 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3252 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3253 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3254 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3255 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3256 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3260 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3261 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3262 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3263 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3264 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3266 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3267 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3269 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3270 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3271 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3272 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3273 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3274 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3276 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3277 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3278 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3279 case Mode.U2_CH: /* nothing */ break;
3281 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3282 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3283 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3284 case Mode.I4_U4: /* nothing */ break;
3285 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3286 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3287 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3289 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3290 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3291 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3292 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3293 case Mode.U4_I4: /* nothing */ break;
3294 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3296 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3297 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3298 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3299 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3300 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3301 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3302 case Mode.I8_U8: /* nothing */ break;
3303 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3305 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3306 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3307 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3308 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3309 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3310 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3311 case Mode.U8_I8: /* nothing */ break;
3312 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3314 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3315 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3316 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3318 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3319 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3320 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3321 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3322 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3323 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3324 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3325 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3326 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3328 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3329 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3330 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3331 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3332 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3333 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3334 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3335 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3336 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3337 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3343 public class OpcodeCast : EmptyCast {
3347 public OpcodeCast (Expression child, Type return_type, OpCode op)
3348 : base (child, return_type)
3352 second_valid = false;
3355 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3356 : base (child, return_type)
3361 second_valid = true;
3364 public override Expression DoResolve (EmitContext ec)
3366 // This should never be invoked, we are born in fully
3367 // initialized state.
3372 public override void Emit (EmitContext ec)
3383 /// This kind of cast is used to encapsulate a child and cast it
3384 /// to the class requested
3386 public class ClassCast : EmptyCast {
3387 public ClassCast (Expression child, Type return_type)
3388 : base (child, return_type)
3393 public override Expression DoResolve (EmitContext ec)
3395 // This should never be invoked, we are born in fully
3396 // initialized state.
3401 public override void Emit (EmitContext ec)
3405 ec.ig.Emit (OpCodes.Castclass, type);
3411 /// SimpleName expressions are initially formed of a single
3412 /// word and it only happens at the beginning of the expression.
3416 /// The expression will try to be bound to a Field, a Method
3417 /// group or a Property. If those fail we pass the name to our
3418 /// caller and the SimpleName is compounded to perform a type
3419 /// lookup. The idea behind this process is that we want to avoid
3420 /// creating a namespace map from the assemblies, as that requires
3421 /// the GetExportedTypes function to be called and a hashtable to
3422 /// be constructed which reduces startup time. If later we find
3423 /// that this is slower, we should create a `NamespaceExpr' expression
3424 /// that fully participates in the resolution process.
3426 /// For example `System.Console.WriteLine' is decomposed into
3427 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3429 /// The first SimpleName wont produce a match on its own, so it will
3431 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3433 /// System.Console will produce a TypeExpr match.
3435 /// The downside of this is that we might be hitting `LookupType' too many
3436 /// times with this scheme.
3438 public class SimpleName : Expression, ITypeExpression {
3439 public readonly string Name;
3442 // If true, then we are a simple name, not composed with a ".
3446 public SimpleName (string a, string b, Location l)
3448 Name = String.Concat (a, ".", b);
3453 public SimpleName (string name, Location l)
3460 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3462 if (ec.IsFieldInitializer)
3465 "A field initializer cannot reference the non-static field, " +
3466 "method or property `"+name+"'");
3470 "An object reference is required " +
3471 "for the non-static field `"+name+"'");
3475 // Checks whether we are trying to access an instance
3476 // property, method or field from a static body.
3478 Expression MemberStaticCheck (EmitContext ec, Expression e)
3480 if (e is IMemberExpr){
3481 IMemberExpr member = (IMemberExpr) e;
3483 if (!member.IsStatic){
3484 Error_ObjectRefRequired (ec, loc, Name);
3492 public override Expression DoResolve (EmitContext ec)
3494 return SimpleNameResolve (ec, null, false);
3497 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3499 return SimpleNameResolve (ec, right_side, false);
3503 public Expression DoResolveAllowStatic (EmitContext ec)
3505 return SimpleNameResolve (ec, null, true);
3508 public Expression DoResolveType (EmitContext ec)
3510 DeclSpace ds = ec.DeclSpace;
3511 Namespace ns = ds.Namespace;
3516 // Since we are cheating: we only do the Alias lookup for
3517 // namespaces if the name does not include any dots in it
3519 if (ns != null && is_base)
3520 alias_value = ns.LookupAlias (Name);
3524 if (ec.ResolvingTypeTree){
3525 if (alias_value != null){
3526 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3527 return new TypeExpr (t, loc);
3530 int errors = Report.Errors;
3531 Type dt = ec.DeclSpace.FindType (loc, Name);
3532 if (Report.Errors != errors)
3536 return new TypeExpr (dt, loc);
3540 // First, the using aliases
3542 if (alias_value != null){
3543 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3544 return new TypeExpr (t, loc);
3546 // we have alias value, but it isn't Type, so try if it's namespace
3547 return new SimpleName (alias_value, loc);
3551 // Stage 2: Lookup up if we are an alias to a type
3555 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3556 return new TypeExpr (t, loc);
3558 // No match, maybe our parent can compose us
3559 // into something meaningful.
3564 /// 7.5.2: Simple Names.
3566 /// Local Variables and Parameters are handled at
3567 /// parse time, so they never occur as SimpleNames.
3569 /// The `allow_static' flag is used by MemberAccess only
3570 /// and it is used to inform us that it is ok for us to
3571 /// avoid the static check, because MemberAccess might end
3572 /// up resolving the Name as a Type name and the access as
3573 /// a static type access.
3575 /// ie: Type Type; .... { Type.GetType (""); }
3577 /// Type is both an instance variable and a Type; Type.GetType
3578 /// is the static method not an instance method of type.
3580 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3582 Expression e = null;
3585 // Stage 1: Performed by the parser (binding to locals or parameters).
3587 Block current_block = ec.CurrentBlock;
3588 if (current_block != null && current_block.GetVariableInfo (Name) != null){
3589 LocalVariableReference var;
3591 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3593 if (right_side != null)
3594 return var.ResolveLValue (ec, right_side);
3596 return var.Resolve (ec);
3599 if (current_block != null){
3601 Parameter par = null;
3602 Parameters pars = current_block.Parameters;
3604 par = pars.GetParameterByName (Name, out idx);
3607 ParameterReference param;
3609 param = new ParameterReference (pars, idx, Name, loc);
3611 if (right_side != null)
3612 return param.ResolveLValue (ec, right_side);
3614 return param.Resolve (ec);
3619 // Stage 2: Lookup members
3622 DeclSpace lookup_ds = ec.DeclSpace;
3624 if (lookup_ds.TypeBuilder == null)
3627 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3631 lookup_ds =lookup_ds.Parent;
3632 } while (lookup_ds != null);
3634 if (e == null && ec.ContainerType != null)
3635 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3638 return DoResolveType (ec);
3643 if (e is IMemberExpr) {
3644 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3648 IMemberExpr me = e as IMemberExpr;
3652 // This fails if ResolveMemberAccess() was unable to decide whether
3653 // it's a field or a type of the same name.
3654 if (!me.IsStatic && (me.InstanceExpression == null))
3658 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3659 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
3660 "outer type `" + me.DeclaringType + "' via nested type `" +
3661 me.InstanceExpression.Type + "'");
3665 if (right_side != null)
3666 e = e.DoResolveLValue (ec, right_side);
3668 e = e.DoResolve (ec);
3673 if (ec.IsStatic || ec.IsFieldInitializer){
3677 return MemberStaticCheck (ec, e);
3682 public override void Emit (EmitContext ec)
3685 // If this is ever reached, then we failed to
3686 // find the name as a namespace
3689 Error (103, "The name `" + Name +
3690 "' does not exist in the class `" +
3691 ec.DeclSpace.Name + "'");
3694 public override string ToString ()
3701 /// Fully resolved expression that evaluates to a type
3703 public class TypeExpr : Expression, ITypeExpression {
3704 public TypeExpr (Type t, Location l)
3707 eclass = ExprClass.Type;
3711 public virtual Expression DoResolveType (EmitContext ec)
3716 override public Expression DoResolve (EmitContext ec)
3721 override public void Emit (EmitContext ec)
3723 throw new Exception ("Should never be called");
3726 public override string ToString ()
3728 return Type.ToString ();
3733 /// Used to create types from a fully qualified name. These are just used
3734 /// by the parser to setup the core types. A TypeLookupExpression is always
3735 /// classified as a type.
3737 public class TypeLookupExpression : TypeExpr {
3740 public TypeLookupExpression (string name) : base (null, Location.Null)
3745 public override Expression DoResolveType (EmitContext ec)
3748 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3752 public override Expression DoResolve (EmitContext ec)
3754 return DoResolveType (ec);
3757 public override void Emit (EmitContext ec)
3759 throw new Exception ("Should never be called");
3762 public override string ToString ()
3769 /// MethodGroup Expression.
3771 /// This is a fully resolved expression that evaluates to a type
3773 public class MethodGroupExpr : Expression, IMemberExpr {
3774 public MethodBase [] Methods;
3775 Expression instance_expression = null;
3776 bool is_explicit_impl = false;
3778 public MethodGroupExpr (MemberInfo [] mi, Location l)
3780 Methods = new MethodBase [mi.Length];
3781 mi.CopyTo (Methods, 0);
3782 eclass = ExprClass.MethodGroup;
3783 type = TypeManager.object_type;
3787 public MethodGroupExpr (ArrayList list, Location l)
3789 Methods = new MethodBase [list.Count];
3792 list.CopyTo (Methods, 0);
3794 foreach (MemberInfo m in list){
3795 if (!(m is MethodBase)){
3796 Console.WriteLine ("Name " + m.Name);
3797 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3803 eclass = ExprClass.MethodGroup;
3804 type = TypeManager.object_type;
3807 public Type DeclaringType {
3809 return Methods [0].DeclaringType;
3814 // `A method group may have associated an instance expression'
3816 public Expression InstanceExpression {
3818 return instance_expression;
3822 instance_expression = value;
3826 public bool IsExplicitImpl {
3828 return is_explicit_impl;
3832 is_explicit_impl = value;
3836 public string Name {
3838 return Methods [0].Name;
3842 public bool IsInstance {
3844 foreach (MethodBase mb in Methods)
3852 public bool IsStatic {
3854 foreach (MethodBase mb in Methods)
3862 override public Expression DoResolve (EmitContext ec)
3864 if (instance_expression != null) {
3865 instance_expression = instance_expression.DoResolve (ec);
3866 if (instance_expression == null)
3873 public void ReportUsageError ()
3875 Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
3876 Methods [0].Name + "()' is referenced without parentheses");
3879 override public void Emit (EmitContext ec)
3881 ReportUsageError ();
3884 bool RemoveMethods (bool keep_static)
3886 ArrayList smethods = new ArrayList ();
3888 foreach (MethodBase mb in Methods){
3889 if (mb.IsStatic == keep_static)
3893 if (smethods.Count == 0)
3896 Methods = new MethodBase [smethods.Count];
3897 smethods.CopyTo (Methods, 0);
3903 /// Removes any instance methods from the MethodGroup, returns
3904 /// false if the resulting set is empty.
3906 public bool RemoveInstanceMethods ()
3908 return RemoveMethods (true);
3912 /// Removes any static methods from the MethodGroup, returns
3913 /// false if the resulting set is empty.
3915 public bool RemoveStaticMethods ()
3917 return RemoveMethods (false);
3922 /// Fully resolved expression that evaluates to a Field
3924 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
3925 public readonly FieldInfo FieldInfo;
3926 Expression instance_expr;
3928 public FieldExpr (FieldInfo fi, Location l)
3931 eclass = ExprClass.Variable;
3932 type = fi.FieldType;
3936 public string Name {
3938 return FieldInfo.Name;
3942 public bool IsInstance {
3944 return !FieldInfo.IsStatic;
3948 public bool IsStatic {
3950 return FieldInfo.IsStatic;
3954 public Type DeclaringType {
3956 return FieldInfo.DeclaringType;
3960 public Expression InstanceExpression {
3962 return instance_expr;
3966 instance_expr = value;
3970 override public Expression DoResolve (EmitContext ec)
3972 if (!FieldInfo.IsStatic){
3973 if (instance_expr == null){
3975 // This can happen when referencing an instance field using
3976 // a fully qualified type expression: TypeName.InstanceField = xxx
3978 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
3982 // Resolve the field's instance expression while flow analysis is turned
3983 // off: when accessing a field "a.b", we must check whether the field
3984 // "a.b" is initialized, not whether the whole struct "a" is initialized.
3985 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
3986 ResolveFlags.DisableFlowAnalysis);
3987 if (instance_expr == null)
3991 // If the instance expression is a local variable or parameter.
3992 IVariable var = instance_expr as IVariable;
3993 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
3999 void Report_AssignToReadonly (bool is_instance)
4004 msg = "Readonly field can not be assigned outside " +
4005 "of constructor or variable initializer";
4007 msg = "A static readonly field can only be assigned in " +
4008 "a static constructor";
4010 Report.Error (is_instance ? 191 : 198, loc, msg);
4013 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4015 IVariable var = instance_expr as IVariable;
4017 var.SetFieldAssigned (ec, FieldInfo.Name);
4019 Expression e = DoResolve (ec);
4024 if (!FieldInfo.IsInitOnly)
4028 // InitOnly fields can only be assigned in constructors
4031 if (ec.IsConstructor){
4032 if (ec.ContainerType == FieldInfo.DeclaringType)
4036 Report_AssignToReadonly (true);
4041 override public void Emit (EmitContext ec)
4043 ILGenerator ig = ec.ig;
4044 bool is_volatile = false;
4046 if (FieldInfo is FieldBuilder){
4047 FieldBase f = TypeManager.GetField (FieldInfo);
4049 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4052 f.status |= Field.Status.USED;
4055 if (FieldInfo.IsStatic){
4057 ig.Emit (OpCodes.Volatile);
4059 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4061 if (instance_expr.Type.IsValueType){
4063 LocalTemporary tempo = null;
4065 if (!(instance_expr is IMemoryLocation)){
4066 tempo = new LocalTemporary (
4067 ec, instance_expr.Type);
4069 InstanceExpression.Emit (ec);
4073 ml = (IMemoryLocation) instance_expr;
4075 ml.AddressOf (ec, AddressOp.Load);
4077 instance_expr.Emit (ec);
4080 ig.Emit (OpCodes.Volatile);
4082 ig.Emit (OpCodes.Ldfld, FieldInfo);
4086 public void EmitAssign (EmitContext ec, Expression source)
4088 FieldAttributes fa = FieldInfo.Attributes;
4089 bool is_static = (fa & FieldAttributes.Static) != 0;
4090 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4091 ILGenerator ig = ec.ig;
4093 if (is_readonly && !ec.IsConstructor){
4094 Report_AssignToReadonly (!is_static);
4099 Expression instance = instance_expr;
4101 if (instance.Type.IsValueType){
4102 if (instance is IMemoryLocation){
4103 IMemoryLocation ml = (IMemoryLocation) instance;
4105 ml.AddressOf (ec, AddressOp.Store);
4107 throw new Exception ("The " + instance + " of type " +
4109 " represents a ValueType and does " +
4110 "not implement IMemoryLocation");
4116 if (FieldInfo is FieldBuilder){
4117 FieldBase f = TypeManager.GetField (FieldInfo);
4119 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4120 ig.Emit (OpCodes.Volatile);
4124 ig.Emit (OpCodes.Stsfld, FieldInfo);
4126 ig.Emit (OpCodes.Stfld, FieldInfo);
4128 if (FieldInfo is FieldBuilder){
4129 FieldBase f = TypeManager.GetField (FieldInfo);
4131 f.status |= Field.Status.ASSIGNED;
4135 public void AddressOf (EmitContext ec, AddressOp mode)
4137 ILGenerator ig = ec.ig;
4139 if (FieldInfo is FieldBuilder){
4140 FieldBase f = TypeManager.GetField (FieldInfo);
4141 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4142 ig.Emit (OpCodes.Volatile);
4145 if (FieldInfo is FieldBuilder){
4146 FieldBase f = TypeManager.GetField (FieldInfo);
4148 if ((mode & AddressOp.Store) != 0)
4149 f.status |= Field.Status.ASSIGNED;
4150 if ((mode & AddressOp.Load) != 0)
4151 f.status |= Field.Status.USED;
4155 // Handle initonly fields specially: make a copy and then
4156 // get the address of the copy.
4158 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4162 local = ig.DeclareLocal (type);
4163 ig.Emit (OpCodes.Stloc, local);
4164 ig.Emit (OpCodes.Ldloca, local);
4168 if (FieldInfo.IsStatic)
4169 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4172 // In the case of `This', we call the AddressOf method, which will
4173 // only load the pointer, and not perform an Ldobj immediately after
4174 // the value has been loaded into the stack.
4176 if (instance_expr is This)
4177 ((This)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4179 instance_expr.Emit (ec);
4180 ig.Emit (OpCodes.Ldflda, FieldInfo);
4186 /// Expression that evaluates to a Property. The Assign class
4187 /// might set the `Value' expression if we are in an assignment.
4189 /// This is not an LValue because we need to re-write the expression, we
4190 /// can not take data from the stack and store it.
4192 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4193 public readonly PropertyInfo PropertyInfo;
4196 // This is set externally by the `BaseAccess' class
4199 MethodInfo getter, setter;
4201 bool must_do_cs1540_check;
4203 Expression instance_expr;
4205 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4208 eclass = ExprClass.PropertyAccess;
4212 type = TypeManager.TypeToCoreType (pi.PropertyType);
4214 ResolveAccessors (ec);
4217 public string Name {
4219 return PropertyInfo.Name;
4223 public bool IsInstance {
4229 public bool IsStatic {
4235 public Type DeclaringType {
4237 return PropertyInfo.DeclaringType;
4242 // The instance expression associated with this expression
4244 public Expression InstanceExpression {
4246 instance_expr = value;
4250 return instance_expr;
4254 public bool VerifyAssignable ()
4256 if (setter == null) {
4257 Report.Error (200, loc,
4258 "The property `" + PropertyInfo.Name +
4259 "' can not be assigned to, as it has not set accessor");
4266 MethodInfo GetAccessor (Type invocation_type, string accessor_name)
4268 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
4269 BindingFlags.Static | BindingFlags.Instance;
4272 group = TypeManager.MemberLookup (
4273 invocation_type, invocation_type, PropertyInfo.DeclaringType,
4274 MemberTypes.Method, flags, accessor_name + "_" + PropertyInfo.Name);
4277 // The first method is the closest to us
4282 foreach (MethodInfo mi in group) {
4283 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
4286 // If only accessible to the current class or children
4288 if (ma == MethodAttributes.Private) {
4289 Type declaring_type = mi.DeclaringType;
4291 if (invocation_type != declaring_type){
4292 if (TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4300 // FamAndAssem requires that we not only derivate, but we are on the
4303 if (ma == MethodAttributes.FamANDAssem){
4304 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4310 // Assembly and FamORAssem succeed if we're in the same assembly.
4311 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
4312 if (mi.DeclaringType.Assembly != invocation_type.Assembly)
4318 // We already know that we aren't in the same assembly.
4319 if (ma == MethodAttributes.Assembly)
4322 // Family and FamANDAssem require that we derive.
4323 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
4324 if (!TypeManager.IsSubclassOrNestedChildOf (invocation_type, mi.DeclaringType))
4327 must_do_cs1540_check = true;
4340 // We also perform the permission checking here, as the PropertyInfo does not
4341 // hold the information for the accessibility of its setter/getter
4343 void ResolveAccessors (EmitContext ec)
4345 getter = GetAccessor (ec.ContainerType, "get");
4346 if ((getter != null) && getter.IsStatic)
4349 setter = GetAccessor (ec.ContainerType, "set");
4350 if ((setter != null) && setter.IsStatic)
4353 if (setter == null && getter == null){
4354 Error (122, "`" + PropertyInfo.Name + "' " +
4355 "is inaccessible because of its protection level");
4360 bool InstanceResolve (EmitContext ec)
4362 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4363 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4367 if (instance_expr != null) {
4368 instance_expr = instance_expr.DoResolve (ec);
4369 if (instance_expr == null)
4373 if (must_do_cs1540_check && (instance_expr != null)) {
4374 if ((instance_expr.Type != ec.ContainerType) &&
4375 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
4376 Report.Error (1540, loc, "Cannot access protected member `" +
4377 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
4378 "' via a qualifier of type `" +
4379 TypeManager.CSharpName (instance_expr.Type) +
4380 "'; the qualifier must be of type `" +
4381 TypeManager.CSharpName (ec.ContainerType) +
4382 "' (or derived from it)");
4390 override public Expression DoResolve (EmitContext ec)
4392 if (getter == null){
4394 // The following condition happens if the PropertyExpr was
4395 // created, but is invalid (ie, the property is inaccessible),
4396 // and we did not want to embed the knowledge about this in
4397 // the caller routine. This only avoids double error reporting.
4402 Report.Error (154, loc,
4403 "The property `" + PropertyInfo.Name +
4404 "' can not be used in " +
4405 "this context because it lacks a get accessor");
4409 if (!InstanceResolve (ec))
4413 // Only base will allow this invocation to happen.
4415 if (IsBase && getter.IsAbstract){
4416 Report.Error (205, loc, "Cannot call an abstract base property: " +
4417 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4424 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4426 if (setter == null){
4428 // The following condition happens if the PropertyExpr was
4429 // created, but is invalid (ie, the property is inaccessible),
4430 // and we did not want to embed the knowledge about this in
4431 // the caller routine. This only avoids double error reporting.
4436 Report.Error (154, loc,
4437 "The property `" + PropertyInfo.Name +
4438 "' can not be used in " +
4439 "this context because it lacks a set accessor");
4443 if (!InstanceResolve (ec))
4447 // Only base will allow this invocation to happen.
4449 if (IsBase && setter.IsAbstract){
4450 Report.Error (205, loc, "Cannot call an abstract base property: " +
4451 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
4457 override public void Emit (EmitContext ec)
4460 // Special case: length of single dimension array property is turned into ldlen
4462 if ((getter == TypeManager.system_int_array_get_length) ||
4463 (getter == TypeManager.int_array_get_length)){
4464 Type iet = instance_expr.Type;
4467 // System.Array.Length can be called, but the Type does not
4468 // support invoking GetArrayRank, so test for that case first
4470 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4471 instance_expr.Emit (ec);
4472 ec.ig.Emit (OpCodes.Ldlen);
4477 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
4482 // Implements the IAssignMethod interface for assignments
4484 public void EmitAssign (EmitContext ec, Expression source)
4486 Argument arg = new Argument (source, Argument.AType.Expression);
4487 ArrayList args = new ArrayList ();
4490 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
4493 override public void EmitStatement (EmitContext ec)
4496 ec.ig.Emit (OpCodes.Pop);
4501 /// Fully resolved expression that evaluates to an Event
4503 public class EventExpr : Expression, IMemberExpr {
4504 public readonly EventInfo EventInfo;
4505 public Expression instance_expr;
4508 MethodInfo add_accessor, remove_accessor;
4510 public EventExpr (EventInfo ei, Location loc)
4514 eclass = ExprClass.EventAccess;
4516 add_accessor = TypeManager.GetAddMethod (ei);
4517 remove_accessor = TypeManager.GetRemoveMethod (ei);
4519 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4522 if (EventInfo is MyEventBuilder){
4523 MyEventBuilder eb = (MyEventBuilder) EventInfo;
4524 type = eb.EventType;
4527 type = EventInfo.EventHandlerType;
4530 public string Name {
4532 return EventInfo.Name;
4536 public bool IsInstance {
4542 public bool IsStatic {
4548 public Type DeclaringType {
4550 return EventInfo.DeclaringType;
4554 public Expression InstanceExpression {
4556 return instance_expr;
4560 instance_expr = value;
4564 public override Expression DoResolve (EmitContext ec)
4566 if (instance_expr != null) {
4567 instance_expr = instance_expr.DoResolve (ec);
4568 if (instance_expr == null)
4576 public override void Emit (EmitContext ec)
4578 Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4581 public void EmitAddOrRemove (EmitContext ec, Expression source)
4583 Expression handler = ((Binary) source).Right;
4585 Argument arg = new Argument (handler, Argument.AType.Expression);
4586 ArrayList args = new ArrayList ();
4590 if (((Binary) source).Oper == Binary.Operator.Addition)
4591 Invocation.EmitCall (
4592 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4594 Invocation.EmitCall (
4595 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);