2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Manjula GHM (mmanjula@novell.com)
8 // (C) 2001 Ximian, Inc.
12 namespace Mono.MonoBASIC {
14 using System.Collections;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
21 /// The ExprClass class contains the is used to pass the
22 /// classification of an expression (value, variable, namespace,
23 /// type, method group, property access, event access, indexer access,
26 public enum ExprClass : byte {
41 /// This is used to tell Resolve in which types of expressions we're
45 public enum ResolveFlags {
46 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
49 // Returns a type expression.
52 // Returns a method group.
55 // Allows SimpleNames to be returned.
56 // This is used by MemberAccess to construct long names that can not be
57 // partially resolved (namespace-qualified names for example).
60 // Mask of all the expression class flags.
63 // Disable control flow analysis while resolving the expression.
64 // This is used when resolving the instance expression of a field expression.
65 DisableFlowAnalysis = 16
69 // This is just as a hint to AddressOf of what will be done with the
72 public enum AddressOp {
79 /// This interface is implemented by variables
81 public interface IMemoryLocation {
83 /// The AddressOf method should generate code that loads
84 /// the address of the object and leaves it on the stack.
86 /// The 'mode' argument is used to notify the expression
87 /// of whether this will be used to read from the address or
88 /// write to the address.
90 /// This is just a hint that can be used to provide good error
91 /// reporting, and should have no other side effects.
93 void AddressOf (EmitContext ec, AddressOp mode);
97 /// This interface is implemented by variables
99 public interface IVariable {
101 /// Checks whether the variable has already been assigned at
102 /// the current position of the method's control flow and
103 /// reports an appropriate error message if not.
105 /// If the variable is a struct, then this call checks whether
106 /// all of its fields (including all private ones) have been
109 bool IsAssigned (EmitContext ec, Location loc);
112 /// Checks whether field 'name' in this struct has been assigned.
114 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
117 /// Tells the flow analysis code that the variable has already
118 /// been assigned at the current code position.
120 /// If the variable is a struct, this call marks all its fields
121 /// (including private fields) as being assigned.
123 void SetAssigned (EmitContext ec);
126 /// Tells the flow analysis code that field 'name' in this struct
127 /// has already been assigned atthe current code position.
129 void SetFieldAssigned (EmitContext ec, string name);
133 /// This interface denotes an expression which evaluates to a member
134 /// of a struct or a class.
136 public interface IMemberExpr
139 /// The name of this member.
146 /// Whether this is an instance member.
153 /// Whether this is a static member.
160 /// The type which declares this member.
167 /// The instance expression associated with this member, if it's a
168 /// non-static member.
170 Expression InstanceExpression {
176 /// Expression which resolves to a type.
178 public interface ITypeExpression
181 /// Resolve the expression, but only lookup types.
183 Expression DoResolveType (EmitContext ec);
187 /// Base class for expressions
189 public abstract class Expression {
190 public ExprClass eclass;
192 protected Location loc;
204 public Location Location {
211 /// Utility wrapper routine for Error, just to beautify the code
213 public void Error (int error, string s)
215 if (!Location.IsNull (loc))
216 Report.Error (error, loc, s);
218 Report.Error (error, s);
222 /// Utility wrapper routine for Warning, just to beautify the code
224 public void Warning (int warning, string s)
226 if (!Location.IsNull (loc))
227 Report.Warning (warning, loc, s);
229 Report.Warning (warning, s);
233 /// Utility wrapper routine for Warning, only prints the warning if
234 /// warnings of level 'level' are enabled.
236 public void Warning (int warning, int level, string s)
238 if (level <= RootContext.WarningLevel)
239 Warning (warning, s);
242 static public void Error_CannotConvertType (Location loc, Type source, Type target)
244 Report.Error (30, loc, "Cannot convert type '" +
245 TypeManager.MonoBASIC_Name (source) + "' to '" +
246 TypeManager.MonoBASIC_Name (target) + "'");
250 /// Performs semantic analysis on the Expression
254 /// The Resolve method is invoked to perform the semantic analysis
257 /// The return value is an expression (it can be the
258 /// same expression in some cases) or a new
259 /// expression that better represents this node.
261 /// For example, optimizations of Unary (LiteralInt)
262 /// would return a new LiteralInt with a negated
265 /// If there is an error during semantic analysis,
266 /// then an error should be reported (using Report)
267 /// and a null value should be returned.
269 /// There are two side effects expected from calling
270 /// Resolve(): the the field variable "eclass" should
271 /// be set to any value of the enumeration
272 /// 'ExprClass' and the type variable should be set
273 /// to a valid type (this is the type of the
276 public abstract Expression DoResolve (EmitContext ec);
278 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
280 return DoResolve (ec);
284 /// Resolves an expression and performs semantic analysis on it.
288 /// Currently Resolve wraps DoResolve to perform sanity
289 /// checking and assertion checking on what we expect from Resolve.
291 public Expression Resolve (EmitContext ec, ResolveFlags flags)
293 // Are we doing a types-only search ?
294 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
295 ITypeExpression type_expr = this as ITypeExpression;
297 if (type_expr == null)
300 return type_expr.DoResolveType (ec);
303 bool old_do_flow_analysis = ec.DoFlowAnalysis;
304 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
305 ec.DoFlowAnalysis = false;
309 if (this is SimpleName)
310 e = ((SimpleName) this).DoResolveAllowStatic (ec);
314 ec.DoFlowAnalysis = old_do_flow_analysis;
320 if (e is SimpleName){
321 SimpleName s = (SimpleName) e;
323 if ((flags & ResolveFlags.SimpleName) == 0) {
325 object lookup = TypeManager.MemberLookup (
326 ec.ContainerType, ec.ContainerType, AllMemberTypes,
327 AllBindingFlags | BindingFlags.NonPublic, s.Name);
329 Error (30390, "'" + s.Name + "' " +
330 "is inaccessible because of its protection level");
332 Error (30451, "The name '" + s.Name + "' could not be " +
333 "found in '" + ec.DeclSpace.Name + "'");
340 if ((e is TypeExpr) || (e is ComposedCast)) {
341 if ((flags & ResolveFlags.Type) == 0) {
351 if ((flags & ResolveFlags.VariableOrValue) == 0) {
357 case ExprClass.MethodGroup:
358 if ((flags & ResolveFlags.MethodGroup) == 0) {
359 MethodGroupExpr mg = (MethodGroupExpr) e;
360 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
361 Expression te = i.Resolve(ec);
362 //((MethodGroupExpr) e).ReportUsageError ();
368 case ExprClass.Value:
369 case ExprClass.Variable:
370 case ExprClass.PropertyAccess:
371 case ExprClass.EventAccess:
372 case ExprClass.IndexerAccess:
373 if ((flags & ResolveFlags.VariableOrValue) == 0) {
380 throw new Exception ("Expression " + e.GetType () +
381 " ExprClass is Invalid after resolve");
385 throw new Exception (
386 "Expression " + e.GetType () +
387 " did not set its type after Resolve\n" +
388 "called from: " + this.GetType ());
394 /// Resolves an expression and performs semantic analysis on it.
396 public Expression Resolve (EmitContext ec)
398 return Resolve (ec, ResolveFlags.VariableOrValue);
402 /// Resolves an expression for LValue assignment
406 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
407 /// checking and assertion checking on what we expect from Resolve
409 public Expression ResolveLValue (EmitContext ec, Expression right_side)
411 Expression e = DoResolveLValue (ec, right_side);
414 if (e is SimpleName){
415 SimpleName s = (SimpleName) e;
419 "The name '" + s.Name + "' could not be found in '" +
420 ec.DeclSpace.Name + "'");
424 if (e.eclass == ExprClass.Invalid)
425 throw new Exception ("Expression " + e +
426 " ExprClass is Invalid after resolve");
428 if (e.eclass == ExprClass.MethodGroup) {
429 MethodGroupExpr mg = (MethodGroupExpr) e;
430 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
431 Expression te = i.Resolve(ec);
433 //((MethodGroupExpr) e).ReportUsageError ();
438 throw new Exception ("Expression " + e +
439 " did not set its type after Resolve");
446 /// Emits the code for the expression
450 /// The Emit method is invoked to generate the code
451 /// for the expression.
453 public abstract void Emit (EmitContext ec);
456 /// Protected constructor. Only derivate types should
457 /// be able to be created
460 protected Expression ()
462 eclass = ExprClass.Invalid;
467 /// Returns a literalized version of a literal FieldInfo
471 /// The possible return values are:
472 /// IntConstant, UIntConstant
473 /// LongLiteral, ULongConstant
474 /// FloatConstant, DoubleConstant
477 /// The value returned is already resolved.
479 public static Constant Constantify (object v, Type t)
481 if (t == TypeManager.int32_type)
482 return new IntConstant ((int) v);
483 else if (t == TypeManager.uint32_type)
484 return new UIntConstant ((uint) v);
485 else if (t == TypeManager.int64_type)
486 return new LongConstant ((long) v);
487 else if (t == TypeManager.uint64_type)
488 return new ULongConstant ((ulong) v);
489 else if (t == TypeManager.float_type)
490 return new FloatConstant ((float) v);
491 else if (t == TypeManager.double_type)
492 return new DoubleConstant ((double) v);
493 else if (t == TypeManager.string_type)
494 return new StringConstant ((string) v);
495 else if (t == TypeManager.short_type)
496 return new ShortConstant ((short)v);
497 else if (t == TypeManager.ushort_type)
498 return new UShortConstant ((ushort)v);
499 else if (t == TypeManager.sbyte_type)
500 return new SByteConstant (((sbyte)v));
501 else if (t == TypeManager.byte_type)
502 return new ByteConstant ((byte)v);
503 else if (t == TypeManager.char_type)
504 return new CharConstant ((char)v);
505 else if (t == TypeManager.bool_type)
506 return new BoolConstant ((bool) v);
507 else if (t == TypeManager.decimal_type)
508 return new DecimalConstant ((decimal)v);
509 else if (TypeManager.IsEnumType (t)){
510 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
512 return new EnumConstant (e, t);
514 throw new Exception ("Unknown type for constant (" + t +
519 /// Returns a fully formed expression after a MemberLookup
521 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
524 return new EventExpr ((EventInfo) mi, loc);
525 else if (mi is FieldInfo)
526 return new FieldExpr ((FieldInfo) mi, loc);
527 else if (mi is PropertyInfo)
528 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
529 else if (mi is Type){
530 return new TypeExpr ((System.Type) mi, loc);
537 // FIXME: Probably implement a cache for (t,name,current_access_set)?
539 // This code could use some optimizations, but we need to do some
540 // measurements. For example, we could use a delegate to 'flag' when
541 // something can not any longer be a method-group (because it is something
545 // If the return value is an Array, then it is an array of
548 // If the return value is an MemberInfo, it is anything, but a Method
552 // FIXME: When calling MemberLookup inside an 'Invocation', we should pass
553 // the arguments here and have MemberLookup return only the methods that
554 // match the argument count/type, unlike we are doing now (we delay this
557 // This is so we can catch correctly attempts to invoke instance methods
558 // from a static body (scan for error 120 in ResolveSimpleName).
561 // FIXME: Potential optimization, have a static ArrayList
564 public static Expression MemberLookup (EmitContext ec, Type t, string name,
565 MemberTypes mt, BindingFlags bf, Location loc)
567 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
571 // Lookup type 't' for code in class 'invocation_type'. Note that it's important
572 // to set 'invocation_type' correctly since this method also checks whether the
573 // invoking class is allowed to access the member in class 't'. When you want to
574 // explicitly do a lookup in the base class, you must set both 't' and 'invocation_type'
575 // to the base class (although a derived class can access protected members of its base
576 // class it cannot do so through an instance of the base class (error CS1540)).
579 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
580 string name, MemberTypes mt, BindingFlags bf,
583 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
588 int count = mi.Length;
590 if (mi [0] is MethodBase)
591 return new MethodGroupExpr (mi, loc);
596 return ExprClassFromMemberInfo (ec, mi [0], loc);
599 public const MemberTypes AllMemberTypes =
600 MemberTypes.Constructor |
604 MemberTypes.NestedType |
605 MemberTypes.Property;
607 public const BindingFlags AllBindingFlags =
608 BindingFlags.Public |
609 BindingFlags.Static |
610 BindingFlags.Instance |
611 BindingFlags.IgnoreCase;
613 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
615 return MemberLookup (ec, ec.ContainerType, t, name, AllMemberTypes, AllBindingFlags, loc);
618 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
620 return MemberLookup (ec, ec.ContainerType, t, name,
621 MemberTypes.Method, AllBindingFlags, loc);
625 /// This is a wrapper for MemberLookup that is not used to "probe", but
626 /// to find a final definition. If the final definition is not found, we
627 /// look for private members and display a useful debugging message if we
630 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
633 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
636 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
637 MemberTypes mt, BindingFlags bf, Location loc)
641 int errors = Report.Errors;
643 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
648 // Error has already been reported.
649 if (errors < Report.Errors)
652 e = MemberLookup (ec, t, name, AllMemberTypes,
653 AllBindingFlags | BindingFlags.NonPublic, loc);
656 30456, loc, "'" + t + "' does not contain a definition " +
657 "for '" + name + "'");
660 30390, loc, "'" + t + "." + name +
661 "' is inaccessible due to its protection level");
667 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
669 EventInfo ei = event_expr.EventInfo;
671 return TypeManager.GetPrivateFieldOfEvent (ei);
674 static EmptyExpression MyEmptyExpr;
675 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
677 Type expr_type = expr.Type;
679 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
680 // if we are a method group, emit a warning
686 // notice that it is possible to write "ValueType v = 1", the ValueType here
687 // is an abstract class, and not really a value type, so we apply the same rules.
689 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
691 // A pointer type cannot be converted to object
693 if (expr_type.IsPointer)
696 if (expr_type.IsValueType)
697 return new BoxedCast (expr);
698 if (expr_type.IsClass || expr_type.IsInterface)
699 return new EmptyCast (expr, target_type);
700 } else if (expr_type.IsSubclassOf (target_type)) {
702 // Special case: enumeration to System.Enum.
703 // System.Enum is not a value type, it is a class, so we need
704 // a boxing conversion
706 if (expr_type.IsEnum)
707 return new BoxedCast (expr);
709 return new EmptyCast (expr, target_type);
712 // This code is kind of mirrored inside StandardConversionExists
713 // with the small distinction that we only probe there
715 // Always ensure that the code here and there is in sync
717 // from the null type to any reference-type.
718 if (expr is NullLiteral && !target_type.IsValueType)
719 return new EmptyCast (expr, target_type);
721 // from any class-type S to any interface-type T.
722 if (target_type.IsInterface) {
723 if (TypeManager.ImplementsInterface (expr_type, target_type)){
724 if (expr_type.IsClass)
725 return new EmptyCast (expr, target_type);
726 else if (expr_type.IsValueType)
727 return new BoxedCast (expr);
731 // from any interface type S to interface-type T.
732 if (expr_type.IsInterface && target_type.IsInterface) {
733 if (TypeManager.ImplementsInterface (expr_type, target_type))
734 return new EmptyCast (expr, target_type);
739 // from an array-type S to an array-type of type T
740 if (expr_type.IsArray && target_type.IsArray) {
741 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
743 Type expr_element_type = expr_type.GetElementType ();
745 if (MyEmptyExpr == null)
746 MyEmptyExpr = new EmptyExpression ();
748 MyEmptyExpr.SetType (expr_element_type);
749 Type target_element_type = target_type.GetElementType ();
751 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
752 if (StandardConversionExists (MyEmptyExpr,
753 target_element_type))
754 return new EmptyCast (expr, target_type);
759 // from an array-type to System.Array
760 if (expr_type.IsArray && target_type == TypeManager.array_type)
761 return new EmptyCast (expr, target_type);
763 // from any delegate type to System.Delegate
764 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
765 target_type == TypeManager.delegate_type)
766 return new EmptyCast (expr, target_type);
768 // from any array-type or delegate type into System.ICloneable.
769 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
770 if (target_type == TypeManager.icloneable_type)
771 return new EmptyCast (expr, target_type);
781 /// Implicit Numeric Conversions.
783 /// expr is the expression to convert, returns a new expression of type
784 /// target_type or null if an implicit conversion is not possible.
786 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
787 Type target_type, Location loc)
789 Type expr_type = expr.Type;
792 // Attempt to do the implicit constant expression conversions
794 if (expr is BoolConstant || expr is IntConstant || expr is LongConstant || expr is DoubleConstant || expr is FloatConstant){
797 e = TryImplicitNumericConversion (target_type, (Constant) expr);
801 if (target_type == TypeManager.byte_type ||
802 target_type == TypeManager.short_type ||
803 target_type == TypeManager.int32_type ||
804 target_type == TypeManager.int64_type ||
805 target_type == TypeManager.float_type) {
808 if (expr is IntConstant)
809 val = ((IntConstant) expr).Value.ToString();
810 if (expr is LongConstant)
811 val = ((LongConstant) expr).Value.ToString();
812 if (expr is FloatConstant)
813 val = ((FloatConstant) expr).Value.ToString();
814 if (expr is DoubleConstant)
815 val = ((DoubleConstant) expr).Value.ToString();
816 Error_ConstantValueCannotBeConverted(loc, val, target_type);
819 } else if (expr is LongConstant && target_type == TypeManager.uint64_type) {
821 // Try the implicit constant expression conversion
822 // from long to ulong, instead of a nice routine,
825 long v = ((LongConstant) expr).Value;
827 return new ULongConstant ((ulong) v);
830 Type real_target_type = target_type;
832 if (target_type == TypeManager.bool_type) {
834 if (expr_type == TypeManager.decimal_type) {
835 return RTConversionExpression (ec, "System.Convert",".ToBoolean" , expr, loc);
838 if ((expr_type != TypeManager.char_type) &&
839 (expr_type != TypeManager.string_type) &&
840 (expr_type != TypeManager.object_type))
841 return new NumericToBoolCast (expr, expr.Type);
844 if (expr_type == TypeManager.bool_type){
846 if (real_target_type == TypeManager.sbyte_type)
847 return new BoolToNumericCast (expr, target_type);
848 if (real_target_type == TypeManager.byte_type)
849 return new BoolToNumericCast (expr, target_type);
850 if (real_target_type == TypeManager.int32_type)
851 return new BoolToNumericCast (expr, target_type);
852 if (real_target_type == TypeManager.int64_type)
853 return new BoolToNumericCast (expr, target_type);
854 if (real_target_type == TypeManager.double_type)
855 return new BoolToNumericCast (expr, target_type);
856 if (real_target_type == TypeManager.float_type)
857 return new BoolToNumericCast (expr, target_type);
858 if (real_target_type == TypeManager.short_type)
859 return new BoolToNumericCast (expr, target_type);
860 if (real_target_type == TypeManager.decimal_type)
861 return RTConversionExpression(ec, "DecimalType.FromBoolean", expr, loc);
862 } else if (expr_type == TypeManager.sbyte_type){
864 // From sbyte to short, int, long, float, double.
866 if (real_target_type == TypeManager.int32_type)
867 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
868 if (real_target_type == TypeManager.int64_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
870 if (real_target_type == TypeManager.double_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
872 if (real_target_type == TypeManager.float_type)
873 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
874 if (real_target_type == TypeManager.short_type)
875 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
876 } else if (expr_type == TypeManager.byte_type){
878 // From byte to short, ushort, int, uint, long, ulong, float, double
880 if ((real_target_type == TypeManager.short_type) ||
881 (real_target_type == TypeManager.ushort_type) ||
882 (real_target_type == TypeManager.int32_type) ||
883 (real_target_type == TypeManager.uint32_type))
884 return new EmptyCast (expr, target_type);
886 if (real_target_type == TypeManager.uint64_type)
887 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
888 if (real_target_type == TypeManager.int64_type)
889 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
890 if (real_target_type == TypeManager.float_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
892 if (real_target_type == TypeManager.double_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
894 } else if (expr_type == TypeManager.short_type){
896 // From short to int, long, float, double
898 if (real_target_type == TypeManager.int32_type)
899 return new EmptyCast (expr, target_type);
900 if (real_target_type == TypeManager.int64_type)
901 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
902 if (real_target_type == TypeManager.double_type)
903 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
904 if (real_target_type == TypeManager.float_type)
905 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
906 } else if (expr_type == TypeManager.ushort_type){
908 // From ushort to int, uint, long, ulong, float, double
910 if (real_target_type == TypeManager.uint32_type)
911 return new EmptyCast (expr, target_type);
913 if (real_target_type == TypeManager.uint64_type)
914 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
915 if (real_target_type == TypeManager.int32_type)
916 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
917 if (real_target_type == TypeManager.int64_type)
918 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
919 if (real_target_type == TypeManager.double_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
921 if (real_target_type == TypeManager.float_type)
922 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
923 } else if (expr_type == TypeManager.int32_type){
925 // From int to long, float, double
927 if (real_target_type == TypeManager.int64_type)
928 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
929 if (real_target_type == TypeManager.double_type)
930 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
931 if (real_target_type == TypeManager.float_type)
932 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
933 } else if (expr_type == TypeManager.uint32_type){
935 // From uint to long, ulong, float, double
937 if (real_target_type == TypeManager.int64_type)
938 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
939 if (real_target_type == TypeManager.uint64_type)
940 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
941 if (real_target_type == TypeManager.double_type)
942 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
944 if (real_target_type == TypeManager.float_type)
945 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
947 } else if (expr_type == TypeManager.int64_type){
949 // From long/ulong to float, double
951 if (real_target_type == TypeManager.double_type)
952 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
953 if (real_target_type == TypeManager.float_type)
954 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
955 } else if (expr_type == TypeManager.uint64_type){
957 // From ulong to float, double
959 if (real_target_type == TypeManager.double_type)
960 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
962 if (real_target_type == TypeManager.float_type)
963 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
965 } else if (expr_type == TypeManager.string_type){
967 if (real_target_type == TypeManager.bool_type)
968 return RTConversionExpression (ec, "BooleanType.FromString" , expr, loc);
969 if (real_target_type == TypeManager.decimal_type)
970 return RTConversionExpression (ec, "DecimalType.FromString" , expr, loc);
971 if (real_target_type == TypeManager.float_type)
972 return RTConversionExpression (ec, "SingleType.FromString" , expr, loc);
973 if (real_target_type == TypeManager.short_type)
974 return RTConversionExpression (ec, "ShortType.FromString" , expr, loc);
975 if (real_target_type == TypeManager.int64_type)
976 return RTConversionExpression (ec, "LongType.FromString" , expr, loc);
977 if (real_target_type == TypeManager.int32_type)
978 return RTConversionExpression (ec, "IntegerType.FromString" , expr, loc);
979 if (real_target_type == TypeManager.double_type)
980 return RTConversionExpression (ec, "DoubleType.FromString" , expr, loc);
981 if (real_target_type == TypeManager.byte_type)
982 return RTConversionExpression (ec, "ByteType.FromString" , expr, loc);
983 } else if (expr_type == TypeManager.float_type){
987 if (real_target_type == TypeManager.decimal_type)
988 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
989 if (real_target_type == TypeManager.double_type)
990 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
992 } else if (expr_type == TypeManager.double_type){
994 if (real_target_type == TypeManager.decimal_type)
995 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
996 } else if (expr_type == TypeManager.decimal_type){
998 if (real_target_type == TypeManager.bool_type)
999 return RTConversionExpression (ec, "BooleanType.FromDecimal" , expr, loc);
1000 if (real_target_type == TypeManager.short_type)
1001 return RTConversionExpression(ec, "System.Convert", ".ToInt16", expr, loc);
1002 if (real_target_type == TypeManager.byte_type)
1003 return RTConversionExpression(ec, "System.Convert", ".ToByte", expr, loc);
1004 if (real_target_type == TypeManager.int32_type)
1005 return RTConversionExpression(ec, "System.Convert", ".ToInt32", expr, loc);
1006 if (real_target_type == TypeManager.int64_type)
1007 return RTConversionExpression(ec, "System.Convert", ".ToInt64", expr, loc);
1008 if (real_target_type == TypeManager.float_type)
1009 return RTConversionExpression(ec, "System.Convert", ".ToSingle", expr, loc);
1010 if (real_target_type == TypeManager.double_type)
1011 return RTConversionExpression(ec, "System.Convert", ".ToDouble", expr, loc);
1018 // Tests whether an implicit reference conversion exists between expr_type
1021 public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
1024 // This is the boxed case.
1026 if (target_type == TypeManager.object_type) {
1027 if ((expr_type.IsClass) ||
1028 (expr_type.IsValueType) ||
1029 (expr_type.IsInterface))
1032 } else if (expr_type.IsSubclassOf (target_type)) {
1035 // Please remember that all code below actually comes
1036 // from ImplicitReferenceConversion so make sure code remains in sync
1038 // from any class-type S to any interface-type T.
1039 if (target_type.IsInterface) {
1040 if (TypeManager.ImplementsInterface (expr_type, target_type))
1044 // from any interface type S to interface-type T.
1045 if (expr_type.IsInterface && target_type.IsInterface)
1046 if (TypeManager.ImplementsInterface (expr_type, target_type))
1049 // from an array-type S to an array-type of type T
1050 if (expr_type.IsArray && target_type.IsArray) {
1051 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1053 Type expr_element_type = expr_type.GetElementType ();
1055 if (MyEmptyExpr == null)
1056 MyEmptyExpr = new EmptyExpression ();
1058 MyEmptyExpr.SetType (expr_element_type);
1059 Type target_element_type = target_type.GetElementType ();
1061 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1062 if (StandardConversionExists (MyEmptyExpr,
1063 target_element_type))
1068 // from an array-type to System.Array
1069 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1072 // from any delegate type to System.Delegate
1073 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1074 target_type == TypeManager.delegate_type)
1075 if (target_type.IsAssignableFrom (expr_type))
1078 // from any array-type or delegate type into System.ICloneable.
1079 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1080 if (target_type == TypeManager.icloneable_type)
1083 // from the null type to any reference-type.
1084 if (expr is NullLiteral && !target_type.IsValueType &&
1085 !TypeManager.IsEnumType (target_type))
1094 /// Same as StandardConversionExists except that it also looks at
1095 /// implicit user defined conversions - needed for overload resolution
1097 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1099 if (StandardConversionExists (expr, target_type) == true)
1103 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1113 /// Determines if a standard implicit conversion exists from
1114 /// expr_type to target_type
1116 public static bool StandardConversionExists (Expression expr, Type target_type)
1118 return WideningConversionExists (expr, expr.type, target_type);
1121 public static bool WideningConversionExists (Type expr_type, Type target_type)
1123 return WideningConversionExists (null, expr_type, target_type);
1126 public static bool WideningConversionExists (Expression expr, Type target_type)
1128 return WideningConversionExists (expr, expr.Type, target_type);
1131 public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
1134 if (expr_type == null || expr_type == TypeManager.void_type)
1137 if (expr_type == target_type)
1140 // Conversions from enum to underlying type are widening.
1141 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1142 expr_type = TypeManager.EnumToUnderlying (expr_type);
1144 if (expr_type == target_type)
1147 // First numeric conversions
1149 if (expr_type == TypeManager.sbyte_type){
1151 // From sbyte to short, int, long, float, double.
1153 if ((target_type == TypeManager.int32_type) ||
1154 (target_type == TypeManager.int64_type) ||
1155 (target_type == TypeManager.double_type) ||
1156 (target_type == TypeManager.float_type) ||
1157 (target_type == TypeManager.short_type) ||
1158 (target_type == TypeManager.decimal_type))
1161 } else if (expr_type == TypeManager.byte_type){
1163 // From byte to short, ushort, int, uint, long, ulong, float, double
1165 if ((target_type == TypeManager.short_type) ||
1166 (target_type == TypeManager.bool_type) ||
1167 (target_type == TypeManager.ushort_type) ||
1168 (target_type == TypeManager.int32_type) ||
1169 (target_type == TypeManager.uint32_type) ||
1170 (target_type == TypeManager.uint64_type) ||
1171 (target_type == TypeManager.int64_type) ||
1172 (target_type == TypeManager.float_type) ||
1173 (target_type == TypeManager.double_type) ||
1174 (target_type == TypeManager.decimal_type))
1177 } else if (expr_type == TypeManager.short_type){
1179 // From short to int, long, float, double
1181 if ((target_type == TypeManager.int32_type) ||
1182 (target_type == TypeManager.bool_type) ||
1183 (target_type == TypeManager.int64_type) ||
1184 (target_type == TypeManager.double_type) ||
1185 (target_type == TypeManager.float_type) ||
1186 (target_type == TypeManager.decimal_type))
1189 } else if (expr_type == TypeManager.ushort_type){
1191 // From ushort to int, uint, long, ulong, float, double
1193 if ((target_type == TypeManager.uint32_type) ||
1194 (target_type == TypeManager.uint64_type) ||
1195 (target_type == TypeManager.int32_type) ||
1196 (target_type == TypeManager.int64_type) ||
1197 (target_type == TypeManager.double_type) ||
1198 (target_type == TypeManager.float_type) ||
1199 (target_type == TypeManager.decimal_type))
1202 } else if (expr_type == TypeManager.int32_type){
1204 // From int to long, float, double
1206 if ((target_type == TypeManager.int64_type) ||
1207 (target_type == TypeManager.bool_type) ||
1208 (target_type == TypeManager.double_type) ||
1209 (target_type == TypeManager.float_type) ||
1210 (target_type == TypeManager.decimal_type))
1213 } else if (expr_type == TypeManager.uint32_type){
1215 // From uint to long, ulong, float, double
1217 if ((target_type == TypeManager.int64_type) ||
1218 (target_type == TypeManager.bool_type) ||
1219 (target_type == TypeManager.uint64_type) ||
1220 (target_type == TypeManager.double_type) ||
1221 (target_type == TypeManager.float_type) ||
1222 (target_type == TypeManager.decimal_type))
1225 } else if ((expr_type == TypeManager.uint64_type) ||
1226 (expr_type == TypeManager.int64_type)) {
1228 // From long/ulong to float, double
1230 if ((target_type == TypeManager.double_type) ||
1231 (target_type == TypeManager.bool_type) ||
1232 (target_type == TypeManager.float_type) ||
1233 (target_type == TypeManager.decimal_type))
1236 } else if (expr_type == TypeManager.decimal_type) {
1237 if (target_type == TypeManager.float_type ||
1238 target_type == TypeManager.double_type)
1240 } else if (expr_type == TypeManager.float_type){
1242 // float to double, decimal
1244 if (target_type == TypeManager.double_type)
1246 } else if (expr_type == TypeManager.double_type){
1248 if ((target_type == TypeManager.bool_type))
1252 if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
1256 if (expr is IntConstant){
1257 int value = ((IntConstant) expr).Value;
1259 if (target_type == TypeManager.sbyte_type){
1260 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1262 } else if (target_type == TypeManager.byte_type){
1263 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1265 } else if (target_type == TypeManager.short_type){
1266 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1268 } else if (target_type == TypeManager.ushort_type){
1269 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1271 } else if (target_type == TypeManager.uint32_type){
1274 } else if (target_type == TypeManager.uint64_type){
1276 // we can optimize this case: a positive int32
1277 // always fits on a uint64. But we need an opcode
1284 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1288 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1290 // Try the implicit constant expression conversion
1291 // from long to ulong, instead of a nice routine,
1292 // we just inline it
1294 long v = ((LongConstant) expr).Value;
1300 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1301 IntLiteral i = (IntLiteral) expr;
1307 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1314 // Used internally by FindMostEncompassedType, this is used
1315 // to avoid creating lots of objects in the tight loop inside
1316 // FindMostEncompassedType
1318 static EmptyExpression priv_fmet_param;
1321 /// Finds "most encompassed type" according to the spec (13.4.2)
1322 /// amongst the methods in the MethodGroupExpr
1324 static Type FindMostEncompassedType (ArrayList types)
1328 if (priv_fmet_param == null)
1329 priv_fmet_param = new EmptyExpression ();
1331 foreach (Type t in types){
1332 priv_fmet_param.SetType (t);
1339 if (StandardConversionExists (priv_fmet_param, best))
1347 // Used internally by FindMostEncompassingType, this is used
1348 // to avoid creating lots of objects in the tight loop inside
1349 // FindMostEncompassingType
1351 static EmptyExpression priv_fmee_ret;
1354 /// Finds "most encompassing type" according to the spec (13.4.2)
1355 /// amongst the types in the given set
1357 static Type FindMostEncompassingType (ArrayList types)
1361 if (priv_fmee_ret == null)
1362 priv_fmee_ret = new EmptyExpression ();
1364 foreach (Type t in types){
1365 priv_fmee_ret.SetType (best);
1372 if (StandardConversionExists (priv_fmee_ret, t))
1380 // Used to avoid creating too many objects
1382 static EmptyExpression priv_fms_expr;
1385 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1386 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1387 /// for explicit and implicit conversion operators.
1389 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1390 bool apply_explicit_conv_rules,
1393 ArrayList src_types_set = new ArrayList ();
1395 if (priv_fms_expr == null)
1396 priv_fms_expr = new EmptyExpression ();
1399 // If any operator converts from S then Sx = S
1401 Type source_type= source.Type;
1402 foreach (MethodBase mb in me.Methods){
1403 ParameterData pd = Invocation.GetParameterData (mb);
1404 Type param_type = pd.ParameterType (0);
1406 if (param_type == source_type)
1409 if (apply_explicit_conv_rules) {
1412 // Find the set of applicable user-defined conversion operators, U. This set
1414 // user-defined implicit or explicit conversion operators declared by
1415 // the classes or structs in D that convert from a type encompassing
1416 // or encompassed by S to a type encompassing or encompassed by T
1418 priv_fms_expr.SetType (param_type);
1419 if (StandardConversionExists (priv_fms_expr, source_type))
1420 src_types_set.Add (param_type);
1422 if (StandardConversionExists (source, param_type))
1423 src_types_set.Add (param_type);
1427 // Only if S is encompassed by param_type
1429 if (StandardConversionExists (source, param_type))
1430 src_types_set.Add (param_type);
1435 // Explicit Conv rules
1437 if (apply_explicit_conv_rules) {
1438 ArrayList candidate_set = new ArrayList ();
1440 foreach (Type param_type in src_types_set){
1441 if (StandardConversionExists (source, param_type))
1442 candidate_set.Add (param_type);
1445 if (candidate_set.Count != 0)
1446 return FindMostEncompassedType (candidate_set);
1452 if (apply_explicit_conv_rules)
1453 return FindMostEncompassingType (src_types_set);
1455 return FindMostEncompassedType (src_types_set);
1459 // Useful in avoiding proliferation of objects
1461 static EmptyExpression priv_fmt_expr;
1464 /// Finds the most specific target Tx according to section 13.4.4
1466 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1467 bool apply_explicit_conv_rules,
1470 ArrayList tgt_types_set = new ArrayList ();
1472 if (priv_fmt_expr == null)
1473 priv_fmt_expr = new EmptyExpression ();
1476 // If any operator converts to T then Tx = T
1478 foreach (MethodInfo mi in me.Methods){
1479 Type ret_type = mi.ReturnType;
1481 if (ret_type == target)
1484 if (apply_explicit_conv_rules) {
1487 // Find the set of applicable user-defined conversion operators, U.
1489 // This set consists of the
1490 // user-defined implicit or explicit conversion operators declared by
1491 // the classes or structs in D that convert from a type encompassing
1492 // or encompassed by S to a type encompassing or encompassed by T
1494 priv_fms_expr.SetType (ret_type);
1495 if (StandardConversionExists (priv_fms_expr, target))
1496 tgt_types_set.Add (ret_type);
1498 priv_fms_expr.SetType (target);
1499 if (StandardConversionExists (priv_fms_expr, ret_type))
1500 tgt_types_set.Add (ret_type);
1504 // Only if T is encompassed by param_type
1506 priv_fms_expr.SetType (ret_type);
1507 if (StandardConversionExists (priv_fms_expr, target))
1508 tgt_types_set.Add (ret_type);
1513 // Explicit conv rules
1515 if (apply_explicit_conv_rules) {
1516 ArrayList candidate_set = new ArrayList ();
1518 foreach (Type ret_type in tgt_types_set){
1519 priv_fmt_expr.SetType (ret_type);
1521 if (StandardConversionExists (priv_fmt_expr, target))
1522 candidate_set.Add (ret_type);
1525 if (candidate_set.Count != 0)
1526 return FindMostEncompassingType (candidate_set);
1530 // Okay, final case !
1532 if (apply_explicit_conv_rules)
1533 return FindMostEncompassedType (tgt_types_set);
1535 return FindMostEncompassingType (tgt_types_set);
1539 /// User-defined Implicit conversions
1541 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1542 Type target, Location loc)
1544 return UserDefinedConversion (ec, source, target, loc, false);
1548 /// User-defined Explicit conversions
1550 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1551 Type target, Location loc)
1553 return UserDefinedConversion (ec, source, target, loc, true);
1557 /// Computes the MethodGroup for the user-defined conversion
1558 /// operators from source_type to target_type. 'look_for_explicit'
1559 /// controls whether we should also include the list of explicit
1562 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1563 Type source_type, Type target_type,
1564 Location loc, bool look_for_explicit)
1566 Expression mg1 = null, mg2 = null;
1567 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1571 // FIXME : How does the False operator come into the picture ?
1572 // This doesn't look complete and very correct !
1574 if (target_type == TypeManager.bool_type && !look_for_explicit)
1575 op_name = "op_True";
1577 op_name = "op_Implicit";
1579 MethodGroupExpr union3;
1581 mg1 = MethodLookup (ec, source_type, op_name, loc);
1582 if (source_type.BaseType != null)
1583 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1586 union3 = (MethodGroupExpr) mg2;
1587 else if (mg2 == null)
1588 union3 = (MethodGroupExpr) mg1;
1590 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1592 mg1 = MethodLookup (ec, target_type, op_name, loc);
1595 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1597 union3 = (MethodGroupExpr) mg1;
1600 if (target_type.BaseType != null)
1601 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1605 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1607 union3 = (MethodGroupExpr) mg1;
1610 MethodGroupExpr union4 = null;
1612 if (look_for_explicit) {
1613 op_name = "op_Explicit";
1615 mg5 = MemberLookup (ec, source_type, op_name, loc);
1616 if (source_type.BaseType != null)
1617 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1619 mg7 = MemberLookup (ec, target_type, op_name, loc);
1620 if (target_type.BaseType != null)
1621 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1623 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1624 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1626 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1629 return Invocation.MakeUnionSet (union3, union4, loc);
1633 /// User-defined conversions
1635 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1636 Type target, Location loc,
1637 bool look_for_explicit)
1639 MethodGroupExpr union;
1640 Type source_type = source.Type;
1641 MethodBase method = null;
1643 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1647 Type most_specific_source, most_specific_target;
1650 foreach (MethodBase m in union.Methods){
1651 Console.WriteLine ("Name: " + m.Name);
1652 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1656 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1657 if (most_specific_source == null)
1660 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1661 if (most_specific_target == null)
1666 foreach (MethodBase mb in union.Methods){
1667 ParameterData pd = Invocation.GetParameterData (mb);
1668 MethodInfo mi = (MethodInfo) mb;
1670 if (pd.ParameterType (0) == most_specific_source &&
1671 mi.ReturnType == most_specific_target) {
1677 if (method == null || count > 1)
1682 // This will do the conversion to the best match that we
1683 // found. Now we need to perform an implict standard conversion
1684 // if the best match was not the type that we were requested
1687 if (look_for_explicit)
1688 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1690 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1696 e = new UserCast ((MethodInfo) method, source, loc);
1697 if (e.Type != target){
1698 if (!look_for_explicit)
1699 e = ConvertImplicitStandard (ec, e, target, loc);
1701 e = ConvertExplicitStandard (ec, e, target, loc);
1707 /// Converts implicitly the resolved expression 'expr' into the
1708 /// 'target_type'. It returns a new expression that can be used
1709 /// in a context that expects a 'target_type'.
1711 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1712 Type target_type, Location loc)
1714 Type expr_type = expr.Type;
1718 if (expr_type == target_type)
1721 if (target_type == null)
1722 throw new Exception ("Target type is null");
1724 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1728 e = ImplicitUserConversion (ec, expr, target_type, loc);
1733 e = NarrowingConversion (ec, expr, target_type, loc);
1741 /// Converts the resolved expression 'expr' into the
1742 /// 'target_type' using the Microsoft.VisualBasic runtime.
1743 /// It returns a new expression that can be used
1744 /// in a context that expects a 'target_type'.
1746 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1752 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1753 args = new ArrayList();
1754 arg = new Argument (expr, Argument.AType.Expression);
1756 e = (Expression) new Invocation (etmp, args, loc);
1761 static private Expression RTConversionExpression (EmitContext ec, string ns, string method, Expression expr, Location loc)
1767 etmp = Mono.MonoBASIC.Parser.DecomposeQI(ns+method, loc);
1768 args = new ArrayList();
1769 arg = new Argument (expr, Argument.AType.Expression);
1771 e = (Expression) new Invocation (etmp, args, loc);
1777 static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
1779 Type expr_type = expr.Type;
1780 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1781 expr_type = TypeManager.EnumToUnderlying (expr_type);
1783 if (target_type.IsSubclassOf (TypeManager.enum_type))
1784 target_type = TypeManager.EnumToUnderlying (target_type);
1787 if (expr_type == target_type)
1790 if (target_type == TypeManager.sbyte_type){
1792 // To sbyte from short, int, long, float, double.
1794 if ((expr_type == TypeManager.int32_type) ||
1795 (expr_type == TypeManager.int64_type) ||
1796 (expr_type == TypeManager.double_type) ||
1797 (expr_type == TypeManager.float_type) ||
1798 (expr_type == TypeManager.short_type) ||
1799 (expr_type == TypeManager.decimal_type))
1802 } else if (target_type == TypeManager.byte_type){
1804 // To byte from short, ushort, int, uint, long, ulong, float, double
1806 if ((expr_type == TypeManager.short_type) ||
1807 (expr_type == TypeManager.ushort_type) ||
1808 (expr_type == TypeManager.int32_type) ||
1809 (expr_type == TypeManager.uint32_type) ||
1810 (expr_type == TypeManager.uint64_type) ||
1811 (expr_type == TypeManager.int64_type) ||
1812 (expr_type == TypeManager.float_type) ||
1813 (expr_type == TypeManager.double_type) ||
1814 (expr_type == TypeManager.decimal_type))
1817 } else if (target_type == TypeManager.short_type){
1819 // To short from int, long, float, double
1821 if ((expr_type == TypeManager.int32_type) ||
1822 (expr_type == TypeManager.int64_type) ||
1823 (expr_type == TypeManager.double_type) ||
1824 (expr_type == TypeManager.float_type) ||
1825 (expr_type == TypeManager.decimal_type))
1828 } else if (target_type == TypeManager.ushort_type){
1830 // To ushort from int, uint, long, ulong, float, double
1832 if ((expr_type == TypeManager.uint32_type) ||
1833 (expr_type == TypeManager.uint64_type) ||
1834 (expr_type == TypeManager.int32_type) ||
1835 (expr_type == TypeManager.int64_type) ||
1836 (expr_type == TypeManager.double_type) ||
1837 (expr_type == TypeManager.float_type) ||
1838 (expr_type == TypeManager.decimal_type))
1841 } else if (target_type == TypeManager.int32_type){
1843 // To int from long, float, double
1845 if ((expr_type == TypeManager.int64_type) ||
1846 (expr_type == TypeManager.double_type) ||
1847 (expr_type == TypeManager.float_type) ||
1848 (expr_type == TypeManager.decimal_type))
1851 } else if (target_type == TypeManager.uint32_type){
1853 // To uint from long, ulong, float, double
1855 if ((expr_type == TypeManager.int64_type) ||
1856 (expr_type == TypeManager.uint64_type) ||
1857 (expr_type == TypeManager.double_type) ||
1858 (expr_type == TypeManager.float_type) ||
1859 (expr_type == TypeManager.decimal_type))
1862 } else if ((target_type == TypeManager.uint64_type) ||
1863 (target_type == TypeManager.int64_type)) {
1865 // To long/ulong from float, double
1867 if ((expr_type == TypeManager.double_type) ||
1868 (expr_type == TypeManager.float_type) ||
1869 (expr_type == TypeManager.decimal_type))
1872 } else if (target_type == TypeManager.decimal_type){
1873 if (expr_type == TypeManager.float_type ||
1874 expr_type == TypeManager.double_type)
1876 } else if (target_type == TypeManager.float_type){
1878 // To float from double
1880 if (expr_type == TypeManager.double_type)
1884 return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;
1887 static public Expression NarrowingConversion (EmitContext ec, Expression expr,
1888 Type target_type, Location loc)
1890 Type expr_type = expr.Type;
1892 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1893 expr_type = TypeManager.EnumToUnderlying (expr_type);
1895 if (target_type.IsSubclassOf (TypeManager.enum_type))
1896 target_type = TypeManager.EnumToUnderlying (target_type);
1898 if (expr_type == target_type)
1901 if (target_type == TypeManager.sbyte_type){
1903 // To sbyte from short, int, long, float, double.
1905 if (expr_type == TypeManager.int32_type)
1906 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1907 if (expr_type == TypeManager.int64_type)
1908 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1909 if (expr_type == TypeManager.short_type)
1910 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1912 if (expr_type == TypeManager.float_type) {
1913 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1914 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I1);
1916 if (expr_type == TypeManager.double_type) {
1917 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1918 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I1);
1921 } else if (target_type == TypeManager.byte_type){
1923 // To byte from short, ushort, int, uint, long, ulong, float, double
1925 if (expr_type == TypeManager.short_type)
1926 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1927 if (expr_type == TypeManager.ushort_type)
1928 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1929 if (expr_type == TypeManager.int32_type)
1930 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1931 if (expr_type == TypeManager.uint32_type)
1932 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1933 if (expr_type == TypeManager.uint64_type)
1934 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1935 if (expr_type == TypeManager.int64_type)
1936 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1938 if (expr_type == TypeManager.float_type) {
1939 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1940 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U1);
1942 if (expr_type == TypeManager.double_type) {
1943 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1944 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U1);
1947 } else if (target_type == TypeManager.short_type) {
1949 // To short from int, long, float, double
1951 if (expr_type == TypeManager.int32_type)
1952 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1953 if (expr_type == TypeManager.int64_type)
1954 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1956 if (expr_type == TypeManager.float_type) {
1957 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1958 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I2);
1960 if (expr_type == TypeManager.double_type) {
1961 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1962 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I2);
1965 } else if (target_type == TypeManager.ushort_type) {
1967 // To ushort from int, uint, long, ulong, float, double
1969 if (expr_type == TypeManager.uint32_type)
1970 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1971 if (expr_type == TypeManager.uint64_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1973 if (expr_type == TypeManager.int32_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1975 if (expr_type == TypeManager.int64_type)
1976 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1978 if (expr_type == TypeManager.float_type) {
1979 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1980 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U2);
1983 if (expr_type == TypeManager.double_type) {
1984 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1985 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U2);
1988 } else if (target_type == TypeManager.int32_type){
1990 // To int from long, float, double
1992 if (expr_type == TypeManager.int64_type)
1993 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1995 if (expr_type == TypeManager.float_type) {
1996 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
1997 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I4);
1999 if (expr_type == TypeManager.double_type) {
2000 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2001 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I4);
2004 } else if (target_type == TypeManager.uint32_type){
2006 // To uint from long, ulong, float, double
2008 if (expr_type == TypeManager.int64_type)
2009 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2010 if (expr_type == TypeManager.uint64_type)
2011 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2012 if (expr_type == TypeManager.float_type) {
2013 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2014 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U4);
2016 if (expr_type == TypeManager.double_type) {
2017 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2018 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U4);
2021 } else if (target_type == TypeManager.uint64_type) {
2023 // To long/ulong from float, double
2025 if (expr_type == TypeManager.float_type) {
2026 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2027 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U8);
2029 if (expr_type == TypeManager.double_type) {
2030 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2031 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U8);
2034 } else if (target_type == TypeManager.int64_type) {
2036 // To long/ulong from float, double
2038 if (expr_type == TypeManager.float_type) {
2039 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2040 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I8);
2042 if (expr_type == TypeManager.double_type) {
2043 Expression rounded_expr = RTConversionExpression(ec, "System.Math", ".Round", expr, loc);
2044 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I8);
2047 } else if (target_type == TypeManager.float_type){
2049 // To float from double
2051 if (expr_type == TypeManager.double_type)
2052 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2055 TypeCode dest_type = Type.GetTypeCode (target_type);
2056 TypeCode src_type = Type.GetTypeCode (expr_type);
2057 Expression e = null;
2059 switch (dest_type) {
2060 case TypeCode.String:
2062 case TypeCode.SByte:
2064 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
2066 case TypeCode.UInt16:
2067 case TypeCode.Int16:
2068 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
2070 case TypeCode.UInt32:
2071 case TypeCode.Int32:
2072 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
2074 case TypeCode.UInt64:
2075 case TypeCode.Int64:
2076 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
2079 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
2081 case TypeCode.Single:
2082 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
2084 case TypeCode.Double:
2085 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
2087 case TypeCode.Boolean:
2088 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
2090 case TypeCode.DateTime:
2091 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
2093 case TypeCode.Decimal:
2094 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
2096 case TypeCode.Object:
2097 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
2102 case TypeCode.Double:
2104 case TypeCode.String:
2105 e = RTConversionExpression(ec, "DoubleType.FromString", expr, loc);
2107 case TypeCode.Object:
2108 e = RTConversionExpression(ec, "DoubleType.FromObject", expr, loc);
2113 case TypeCode.Single:
2115 case TypeCode.String:
2116 e = RTConversionExpression(ec, "SingleType.FromString", expr, loc);
2118 case TypeCode.Object:
2119 e = RTConversionExpression(ec, "SingleType.FromObject", expr, loc);
2124 case TypeCode.Decimal:
2126 case TypeCode.String:
2127 e = RTConversionExpression(ec, "DecimalType.FromString", expr, loc);
2129 case TypeCode.Object:
2130 e = RTConversionExpression(ec, "DecimalType.FromObject", expr, loc);
2135 case TypeCode.Int64:
2136 case TypeCode.UInt64:
2138 case TypeCode.String:
2139 e = RTConversionExpression(ec, "LongType.FromString", expr, loc);
2141 case TypeCode.Object:
2142 e = RTConversionExpression(ec, "LongType.FromObject", expr, loc);
2146 case TypeCode.Int32:
2147 case TypeCode.UInt32:
2149 case TypeCode.String:
2150 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
2152 case TypeCode.Object:
2153 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
2158 case TypeCode.Int16:
2159 case TypeCode.UInt16:
2161 case TypeCode.String:
2162 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
2164 case TypeCode.Object:
2165 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
2172 case TypeCode.String:
2173 e = RTConversionExpression(ec, "BooleanType.FromString", expr, loc);
2175 case TypeCode.Object:
2176 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
2180 case TypeCode.Boolean:
2182 case TypeCode.String:
2183 e = RTConversionExpression(ec, "BooleanType.FromString", expr, loc);
2185 case TypeCode.Object:
2186 e = RTConversionExpression(ec, "BooleanType.FromObject", expr, loc);
2190 case TypeCode.DateTime:
2192 case TypeCode.String:
2193 e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
2195 case TypeCode.Object:
2196 e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
2203 case TypeCode.String:
2204 e = RTConversionExpression(ec, "CharType.FromString", expr, loc);
2211 // We must examine separately some types that
2212 // don't have a TypeCode but are supported
2214 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
2215 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
2219 // VB.NET Objects can be converted to anything by default
2220 // unless, that is, an exception at runtime blows it all
2221 if (src_type == TypeCode.Object) {
2222 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
2223 Cast ce = new Cast (cast_type, expr, loc);
2224 ce.IsRuntimeCast = true;
2225 return ce.Resolve (ec);
2230 static public Expression ConvertNothingToDefaultValues (EmitContext ec, Expression expr,
2231 Type target_type, Location loc)
2233 switch (Type.GetTypeCode (target_type)) {
2234 case TypeCode.Boolean :
2235 return new BoolConstant (false);
2236 case TypeCode.Byte :
2237 return new ByteConstant (0);
2238 case TypeCode.Char :
2239 return new CharConstant ((char)0);
2240 case TypeCode.SByte :
2241 return new SByteConstant (0);
2242 case TypeCode.Int16 :
2243 return new ShortConstant (0);
2244 case TypeCode.Int32 :
2245 return new IntConstant (0);
2246 case TypeCode.Int64 :
2247 return new LongConstant (0);
2248 case TypeCode.Decimal :
2249 return new DecimalConstant (System.Decimal.Zero);
2250 case TypeCode.Single :
2251 return new FloatConstant (0.0F);
2252 case TypeCode.Double :
2253 return new DoubleConstant (0.0);
2260 /// Attempts to apply the 'Standard Implicit
2261 /// Conversion' rules to the expression 'expr' into
2262 /// the 'target_type'. It returns a new expression
2263 /// that can be used in a context that expects a
2266 /// This is different from 'ConvertImplicit' in that the
2267 /// user defined implicit conversions are excluded.
2269 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
2270 Type target_type, Location loc)
2272 Type expr_type = expr.Type;
2274 if (expr_type.IsSubclassOf (TypeManager.enum_type))
2275 expr_type = TypeManager.EnumToUnderlying (expr_type);
2279 if (expr is NullLiteral) {
2280 if (target_type == TypeManager.string_type)
2282 e = ConvertNothingToDefaultValues (ec, expr, target_type, loc);
2287 if (expr_type == target_type)
2290 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2295 if (expr is StringConstant && target_type == TypeManager.char_type)
2296 return new CharConstant (((StringConstant) expr).Value [0]);
2298 if (expr is CharConstant && target_type == TypeManager.string_type)
2299 return new StringConstant (((CharConstant) expr).Value.ToString ());
2301 e = ImplicitReferenceConversion (expr, target_type);
2305 if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
2306 expr_type = TypeManager.EnumToUnderlying (expr.Type);
2307 expr = new EmptyCast (expr, expr_type);
2308 if (expr_type == target_type)
2310 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2316 if (expr_type.IsPointer){
2317 if (target_type == TypeManager.void_ptr_type)
2318 return new EmptyCast (expr, target_type);
2321 // yep, comparing pointer types cant be done with
2322 // t1 == t2, we have to compare their element types.
2324 if (target_type.IsPointer){
2325 if (target_type.GetElementType()==expr_type.GetElementType())
2330 if (target_type.IsPointer){
2331 if (expr is NullLiteral)
2332 return new EmptyCast (expr, target_type);
2340 /// Attemps to perform an implict constant conversion of the any Numeric Constant
2341 /// into a different data type using casts (See Implicit Constant
2342 /// Expression Conversions)
2344 static protected Expression TryImplicitNumericConversion (Type target_type, Constant ic)
2347 if (ic is BoolConstant) {
2348 bool val = (bool) ((BoolConstant)ic).Value;
2350 if (target_type == TypeManager.byte_type)
2351 value = Byte.MaxValue;
2356 if (ic is IntConstant)
2357 value = (double)((IntConstant)ic).Value;
2359 if (ic is LongConstant)
2360 value = (double) ((LongConstant)ic).Value;
2362 if (ic is FloatConstant) {
2363 value = (double) ((FloatConstant)ic).Value;
2366 if (ic is DoubleConstant) {
2367 value = ((DoubleConstant)ic).Value;
2371 // FIXME: This could return constants instead of EmptyCasts
2373 if (target_type == TypeManager.bool_type){
2375 return new BoolConstant (true);
2376 return new BoolConstant (false);
2377 } else if (target_type == TypeManager.sbyte_type){
2378 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2379 return new SByteConstant ((sbyte) System.Math.Round (value));
2380 } else if (target_type == TypeManager.byte_type){
2381 if (value >= Byte.MinValue && value <= Byte.MaxValue)
2382 return new ByteConstant ((byte) System.Math.Round (value));
2383 } else if (target_type == TypeManager.short_type){
2384 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2385 return new ShortConstant ((short) System.Math.Round (value));
2386 } else if (target_type == TypeManager.ushort_type){
2387 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2388 return new UShortConstant ((ushort) System.Math.Round (value));
2389 } else if (target_type == TypeManager.int32_type){
2390 if (value >= Int32.MinValue && value <= Int32.MaxValue)
2391 return new IntConstant ((int) System.Math.Round (value));
2392 } else if (target_type == TypeManager.uint32_type){
2394 return new UIntConstant ((uint) System.Math.Round (value));
2395 } else if (target_type == TypeManager.int64_type){
2396 return new LongConstant ((long) System.Math.Round (value));
2397 } else if (target_type == TypeManager.uint64_type){
2399 // we can optimize this case: a positive int32
2400 // always fits on a uint64. But we need an opcode
2404 return new ULongConstant ((ulong)System.Math.Round ( value));
2405 } else if (target_type == TypeManager.float_type){
2406 return new FloatConstant ((float) value);
2407 } else if (target_type == TypeManager.double_type){
2408 return new DoubleConstant ((double) value);
2411 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
2412 Type underlying = TypeManager.EnumToUnderlying (target_type);
2413 Constant e = (Constant) ic;
2416 // Possibly, we need to create a different 0 literal before passing
2419 if (underlying == TypeManager.int64_type)
2420 e = new LongLiteral (0);
2421 else if (underlying == TypeManager.uint64_type)
2422 e = new ULongLiteral (0);
2424 return new EnumConstant (e, target_type);
2429 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
2431 string msg = "Cannot convert implicitly from '"+
2432 TypeManager.MonoBASIC_Name (source) + "' to '" +
2433 TypeManager.MonoBASIC_Name (target) + "'";
2435 throw new Exception (msg);
2437 // Report.Error (30512, loc, msg);
2441 /// Attemptes to implicityly convert 'target' into 'type', using
2442 /// ConvertImplicit. If there is no implicit conversion, then
2443 /// an error is signaled
2445 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
2446 Type target_type, Location loc)
2450 e = ConvertImplicit (ec, source, target_type, loc);
2456 if (source is DoubleLiteral && target_type == TypeManager.float_type){
2457 Report.Error (664, loc,
2458 "Double literal cannot be implicitly converted to " +
2459 "float type, use F suffix to create a float literal");
2462 Error_CannotConvertImplicit (loc, source.Type, target_type);
2468 /// Performs the explicit numeric conversions
2470 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
2472 Type expr_type = expr.Type;
2475 // If we have an enumeration, extract the underlying type,
2476 // use this during the comparison, but wrap around the original
2479 Type real_target_type = target_type;
2481 if (TypeManager.IsEnumType (real_target_type))
2482 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
2484 if (StandardConversionExists (expr, real_target_type)){
2485 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
2487 if (real_target_type != target_type)
2488 return new EmptyCast (ce, target_type);
2492 if (expr_type == TypeManager.sbyte_type){
2494 // From sbyte to byte, ushort, uint, ulong, char
2496 if (real_target_type == TypeManager.byte_type)
2497 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
2498 if (real_target_type == TypeManager.ushort_type)
2499 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
2500 if (real_target_type == TypeManager.uint32_type)
2501 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
2502 if (real_target_type == TypeManager.uint64_type)
2503 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
2504 } else if (expr_type == TypeManager.byte_type){
2506 // From byte to sbyte and char
2508 if (real_target_type == TypeManager.sbyte_type)
2509 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
2510 } else if (expr_type == TypeManager.short_type){
2512 // From short to sbyte, byte, ushort, uint, ulong, char
2514 if (real_target_type == TypeManager.sbyte_type)
2515 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
2516 if (real_target_type == TypeManager.byte_type)
2517 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
2518 if (real_target_type == TypeManager.ushort_type)
2519 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
2520 if (real_target_type == TypeManager.uint32_type)
2521 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
2522 if (real_target_type == TypeManager.uint64_type)
2523 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
2524 } else if (expr_type == TypeManager.ushort_type){
2526 // From ushort to sbyte, byte, short, char
2528 if (real_target_type == TypeManager.sbyte_type)
2529 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
2530 if (real_target_type == TypeManager.byte_type)
2531 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
2532 if (real_target_type == TypeManager.short_type)
2533 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
2534 } else if (expr_type == TypeManager.int32_type){
2536 // From int to sbyte, byte, short, ushort, uint, ulong, char
2538 if (real_target_type == TypeManager.sbyte_type)
2539 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
2540 if (real_target_type == TypeManager.byte_type)
2541 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
2542 if (real_target_type == TypeManager.short_type)
2543 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
2544 if (real_target_type == TypeManager.ushort_type)
2545 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
2546 if (real_target_type == TypeManager.uint32_type)
2547 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2548 if (real_target_type == TypeManager.uint64_type)
2549 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2550 } else if (expr_type == TypeManager.uint32_type){
2552 // From uint to sbyte, byte, short, ushort, int, char
2554 if (real_target_type == TypeManager.sbyte_type)
2555 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2556 if (real_target_type == TypeManager.byte_type)
2557 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2558 if (real_target_type == TypeManager.short_type)
2559 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2560 if (real_target_type == TypeManager.ushort_type)
2561 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2562 if (real_target_type == TypeManager.int32_type)
2563 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2564 } else if (expr_type == TypeManager.int64_type){
2566 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2568 if (real_target_type == TypeManager.sbyte_type)
2569 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2570 if (real_target_type == TypeManager.byte_type)
2571 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2572 if (real_target_type == TypeManager.short_type)
2573 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2574 if (real_target_type == TypeManager.ushort_type)
2575 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2576 if (real_target_type == TypeManager.int32_type)
2577 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2578 if (real_target_type == TypeManager.uint32_type)
2579 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2580 if (real_target_type == TypeManager.uint64_type)
2581 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2582 } else if (expr_type == TypeManager.uint64_type){
2584 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2586 if (real_target_type == TypeManager.sbyte_type)
2587 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2588 if (real_target_type == TypeManager.byte_type)
2589 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2590 if (real_target_type == TypeManager.short_type)
2591 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2592 if (real_target_type == TypeManager.ushort_type)
2593 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2594 if (real_target_type == TypeManager.int32_type)
2595 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2596 if (real_target_type == TypeManager.uint32_type)
2597 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2598 if (real_target_type == TypeManager.int64_type)
2599 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2600 } else if (expr_type == TypeManager.float_type){
2602 // From float to sbyte, byte, short,
2603 // ushort, int, uint, long, ulong, char
2606 Expression rounded_expr = RTConversionExpression(ec, "System.Math",".Round" , expr, loc);
2607 if (real_target_type == TypeManager.sbyte_type)
2608 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I1);
2609 if (real_target_type == TypeManager.byte_type)
2610 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U1);
2611 if (real_target_type == TypeManager.short_type)
2612 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I2);
2613 if (real_target_type == TypeManager.ushort_type)
2614 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U2);
2615 if (real_target_type == TypeManager.int32_type)
2616 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I4);
2617 if (real_target_type == TypeManager.uint32_type)
2618 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U4);
2619 if (real_target_type == TypeManager.int64_type)
2620 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_I8);
2621 if (real_target_type == TypeManager.uint64_type)
2622 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R4_U8);
2623 } else if (expr_type == TypeManager.double_type){
2625 // From double to byte, byte, short,
2626 // ushort, int, uint, long, ulong,
2627 // char, float or decimal
2629 Expression rounded_expr = RTConversionExpression(ec, "System.Math",".Round" , expr, loc);
2630 if (real_target_type == TypeManager.sbyte_type)
2631 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I1);
2632 if (real_target_type == TypeManager.byte_type)
2633 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U1);
2634 if (real_target_type == TypeManager.short_type)
2635 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I2);
2636 if (real_target_type == TypeManager.ushort_type)
2637 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U2);
2638 if (real_target_type == TypeManager.int32_type)
2639 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I4);
2640 if (real_target_type == TypeManager.uint32_type)
2641 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U4);
2642 if (real_target_type == TypeManager.int64_type)
2643 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_I8);
2644 if (real_target_type == TypeManager.uint64_type)
2645 return new ConvCast (ec, rounded_expr, target_type, ConvCast.Mode.R8_U8);
2646 if (real_target_type == TypeManager.float_type)
2647 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2650 // decimal is taken care of by the op_Explicit methods.
2656 /// Returns whether an explicit reference conversion can be performed
2657 /// from source_type to target_type
2659 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2661 bool target_is_value_type = target_type.IsValueType;
2663 if (source_type == target_type)
2667 // From object to any reference type
2669 if (source_type == TypeManager.object_type && !target_is_value_type)
2673 // From any class S to any class-type T, provided S is a base class of T
2675 if (target_type.IsSubclassOf (source_type))
2679 // From any interface type S to any interface T provided S is not derived from T
2681 if (source_type.IsInterface && target_type.IsInterface){
2682 if (!target_type.IsSubclassOf (source_type))
2687 // From any class type S to any interface T, provided S is not sealed
2688 // and provided S does not implement T.
2690 if (target_type.IsInterface && !source_type.IsSealed &&
2691 !TypeManager.ImplementsInterface (source_type, target_type))
2695 // From any interface-type S to to any class type T, provided T is not
2696 // sealed, or provided T implements S.
2698 if (source_type.IsInterface &&
2699 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2703 // From an array type S with an element type Se to an array type T with an
2704 // element type Te provided all the following are true:
2705 // * S and T differe only in element type, in other words, S and T
2706 // have the same number of dimensions.
2707 // * Both Se and Te are reference types
2708 // * An explicit referenc conversions exist from Se to Te
2710 if (source_type.IsArray && target_type.IsArray) {
2711 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2713 Type source_element_type = source_type.GetElementType ();
2714 Type target_element_type = target_type.GetElementType ();
2716 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2717 if (ExplicitReferenceConversionExists (source_element_type,
2718 target_element_type))
2724 // From System.Array to any array-type
2725 if (source_type == TypeManager.array_type &&
2726 target_type.IsArray){
2731 // From System delegate to any delegate-type
2733 if (source_type == TypeManager.delegate_type &&
2734 target_type.IsSubclassOf (TypeManager.delegate_type))
2738 // From ICloneable to Array or Delegate types
2740 if (source_type == TypeManager.icloneable_type &&
2741 (target_type == TypeManager.array_type ||
2742 target_type == TypeManager.delegate_type))
2749 /// Implements Explicit Reference conversions
2751 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2753 Type source_type = source.Type;
2754 bool target_is_value_type = target_type.IsValueType;
2757 // From object to any reference type
2759 if (source_type == TypeManager.object_type && !target_is_value_type)
2760 return new ClassCast (source, target_type);
2764 // From any class S to any class-type T, provided S is a base class of T
2766 if (target_type.IsSubclassOf (source_type))
2767 return new ClassCast (source, target_type);
2770 // From any interface type S to any interface T provided S is not derived from T
2772 if (source_type.IsInterface && target_type.IsInterface){
2773 if (TypeManager.ImplementsInterface (source_type, target_type))
2776 return new ClassCast (source, target_type);
2780 // From any class type S to any interface T, provides S is not sealed
2781 // and provided S does not implement T.
2783 if (target_type.IsInterface && !source_type.IsSealed) {
2784 if (TypeManager.ImplementsInterface (source_type, target_type))
2787 return new ClassCast (source, target_type);
2792 // From any interface-type S to to any class type T, provided T is not
2793 // sealed, or provided T implements S.
2795 if (source_type.IsInterface) {
2796 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2797 return new ClassCast (source, target_type);
2802 // From an array type S with an element type Se to an array type T with an
2803 // element type Te provided all the following are true:
2804 // * S and T differe only in element type, in other words, S and T
2805 // have the same number of dimensions.
2806 // * Both Se and Te are reference types
2807 // * An explicit referenc conversions exist from Se to Te
2809 if (source_type.IsArray && target_type.IsArray) {
2810 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2812 Type source_element_type = source_type.GetElementType ();
2813 Type target_element_type = target_type.GetElementType ();
2815 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2816 if (ExplicitReferenceConversionExists (source_element_type,
2817 target_element_type))
2818 return new ClassCast (source, target_type);
2823 // From System.Array to any array-type
2824 if (source_type == TypeManager.array_type &&
2825 target_type.IsArray) {
2826 return new ClassCast (source, target_type);
2830 // From System delegate to any delegate-type
2832 if (source_type == TypeManager.delegate_type &&
2833 target_type.IsSubclassOf (TypeManager.delegate_type))
2834 return new ClassCast (source, target_type);
2837 // From ICloneable to Array or Delegate types
2839 if (source_type == TypeManager.icloneable_type &&
2840 (target_type == TypeManager.array_type ||
2841 target_type == TypeManager.delegate_type))
2842 return new ClassCast (source, target_type);
2848 /// Performs an explicit conversion of the expression 'expr' whose
2849 /// type is expr.Type to 'target_type'.
2851 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2852 Type target_type, bool runtimeconv, Location loc)
2854 Type expr_type = expr.Type;
2855 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2860 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2865 // Unboxing conversion.
2867 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2868 return new UnboxCast (expr, target_type);
2873 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2877 // FIXME: Is there any reason we should have EnumConstant
2878 // dealt with here instead of just using always the
2879 // UnderlyingSystemType to wrap the type?
2881 if (expr is EnumConstant)
2882 e = ((EnumConstant) expr).Child;
2884 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2887 Expression t = ConvertImplicit (ec, e, target_type, loc);
2891 t = ConvertNumericExplicit (ec, e, target_type, loc);
2895 t = NarrowingConversion (ec, e, target_type, loc);
2899 Error_CannotConvertType (loc, expr_type, target_type);
2903 ne = ConvertReferenceExplicit (expr, target_type);
2908 if (target_type.IsPointer){
2909 if (expr_type.IsPointer)
2910 return new EmptyCast (expr, target_type);
2912 if (expr_type == TypeManager.sbyte_type ||
2913 expr_type == TypeManager.byte_type ||
2914 expr_type == TypeManager.short_type ||
2915 expr_type == TypeManager.ushort_type ||
2916 expr_type == TypeManager.int32_type ||
2917 expr_type == TypeManager.uint32_type ||
2918 expr_type == TypeManager.uint64_type ||
2919 expr_type == TypeManager.int64_type)
2920 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2922 if (expr_type.IsPointer){
2923 if (target_type == TypeManager.sbyte_type ||
2924 target_type == TypeManager.byte_type ||
2925 target_type == TypeManager.short_type ||
2926 target_type == TypeManager.ushort_type ||
2927 target_type == TypeManager.int32_type ||
2928 target_type == TypeManager.uint32_type ||
2929 target_type == TypeManager.uint64_type ||
2930 target_type == TypeManager.int64_type){
2931 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2934 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2939 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2943 // We should always be able to go from an uint32
2944 // implicitly or explicitly to the other integral
2947 throw new Exception ("Internal compiler error");
2952 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2956 if (!(runtimeconv)) {
2957 ne = NarrowingConversion (ec, expr, target_type, loc);
2961 Error_CannotConvertType (loc, expr_type, target_type);
2967 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2969 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2970 Type target_type, Location l)
2972 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2977 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2981 ne = ConvertReferenceExplicit (expr, target_type);
2985 ne = NarrowingConversion (ec, expr, target_type, l);
2989 Error_CannotConvertType (l, expr.Type, target_type);
2993 static string ExprClassName (ExprClass c)
2996 case ExprClass.Invalid:
2998 case ExprClass.Value:
3000 case ExprClass.Variable:
3002 case ExprClass.Namespace:
3004 case ExprClass.Type:
3006 case ExprClass.MethodGroup:
3007 return "method group";
3008 case ExprClass.PropertyAccess:
3009 return "property access";
3010 case ExprClass.EventAccess:
3011 return "event access";
3012 case ExprClass.IndexerAccess:
3013 return "indexer access";
3014 case ExprClass.Nothing:
3017 throw new Exception ("Should not happen");
3021 /// Reports that we were expecting 'expr' to be of class 'expected'
3023 public void Error118 (string expected)
3025 string kind = "Unknown";
3027 kind = ExprClassName (eclass);
3029 Error (118, "Expression denotes a '" + kind +
3030 "' where a '" + expected + "' was expected");
3033 public void Error118 (ResolveFlags flags)
3035 ArrayList valid = new ArrayList (10);
3037 if ((flags & ResolveFlags.VariableOrValue) != 0) {
3038 valid.Add ("variable");
3039 valid.Add ("value");
3042 if ((flags & ResolveFlags.Type) != 0)
3045 if ((flags & ResolveFlags.MethodGroup) != 0)
3046 valid.Add ("method group");
3048 if ((flags & ResolveFlags.SimpleName) != 0)
3049 valid.Add ("simple name");
3051 if (valid.Count == 0)
3052 valid.Add ("unknown");
3054 StringBuilder sb = new StringBuilder ();
3055 for (int i = 0; i < valid.Count; i++) {
3058 else if (i == valid.Count)
3060 sb.Append (valid [i]);
3063 string kind = ExprClassName (eclass);
3065 Error (119, "Expression denotes a '" + kind + "' where " +
3066 "a '" + sb.ToString () + "' was expected");
3069 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
3071 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
3072 TypeManager.MonoBASIC_Name (t));
3075 public static void UnsafeError (Location loc)
3077 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
3081 /// Converts the IntConstant, UIntConstant, LongConstant or
3082 /// ULongConstant,Double into the integral target_type. Notice
3083 /// that we do not return an 'Expression' we do return
3084 /// a boxed integral type.
3086 /// FIXME: Since I added the new constants, we need to
3087 /// also support conversions from CharConstant, ByteConstant,
3088 /// SByteConstant, UShortConstant, ShortConstant
3090 /// This is used by the switch statement, so the domain
3091 /// of work is restricted to the literals above, and the
3092 /// targets are int32, uint32, char, byte, sbyte, ushort,
3093 /// short, uint64 and int64
3095 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
3099 if (c.Type == target_type)
3100 return ((Constant) c).GetValue ();
3103 // Make into one of the literals we handle, we dont really care
3104 // about this value as we will just return a few limited types
3106 if (c is EnumConstant)
3107 c = ((EnumConstant)c).WidenToCompilerConstant ();
3109 if (c is IntConstant){
3110 int v = ((IntConstant) c).Value;
3112 if (target_type == TypeManager.uint32_type){
3115 } else if (target_type == TypeManager.byte_type){
3116 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3118 } else if (target_type == TypeManager.sbyte_type){
3119 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3121 } else if (target_type == TypeManager.short_type){
3122 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
3124 } else if (target_type == TypeManager.ushort_type){
3125 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
3127 } else if (target_type == TypeManager.int64_type)
3129 else if (target_type == TypeManager.uint64_type){
3135 } else if (c is UIntConstant){
3136 uint v = ((UIntConstant) c).Value;
3138 if (target_type == TypeManager.int32_type){
3139 if (v <= Int32.MaxValue)
3141 } else if (target_type == TypeManager.byte_type){
3142 if (v <= Byte.MaxValue)
3144 } else if (target_type == TypeManager.sbyte_type){
3145 if (v <= SByte.MaxValue)
3147 } else if (target_type == TypeManager.short_type){
3148 if (v <= UInt16.MaxValue)
3150 } else if (target_type == TypeManager.ushort_type){
3151 if (v <= UInt16.MaxValue)
3153 } else if (target_type == TypeManager.int64_type)
3155 else if (target_type == TypeManager.uint64_type)
3158 } else if (c is LongConstant){
3159 long v = ((LongConstant) c).Value;
3161 if (target_type == TypeManager.int32_type){
3162 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
3164 } else if (target_type == TypeManager.uint32_type){
3165 if (v >= 0 && v <= UInt32.MaxValue)
3167 } else if (target_type == TypeManager.byte_type){
3168 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3170 } else if (target_type == TypeManager.sbyte_type){
3171 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3173 } else if (target_type == TypeManager.short_type){
3174 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
3176 } else if (target_type == TypeManager.ushort_type){
3177 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
3179 } else if (target_type == TypeManager.uint64_type){
3184 } else if (c is ULongConstant){
3185 ulong v = ((ULongConstant) c).Value;
3187 if (target_type == TypeManager.int32_type){
3188 if (v <= Int32.MaxValue)
3190 } else if (target_type == TypeManager.uint32_type){
3191 if (v <= UInt32.MaxValue)
3193 } else if (target_type == TypeManager.byte_type){
3194 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3196 } else if (target_type == TypeManager.sbyte_type){
3197 if (v <= (int) SByte.MaxValue)
3199 } else if (target_type == TypeManager.short_type){
3200 if (v <= UInt16.MaxValue)
3202 } else if (target_type == TypeManager.ushort_type){
3203 if (v <= UInt16.MaxValue)
3205 } else if (target_type == TypeManager.int64_type){
3206 if (v <= Int64.MaxValue)
3210 } else if (c is ByteConstant){
3211 byte v = ((ByteConstant) c).Value;
3213 if (target_type == TypeManager.int32_type)
3215 else if (target_type == TypeManager.uint32_type)
3217 else if (target_type == TypeManager.sbyte_type){
3218 if (v <= SByte.MaxValue)
3220 } else if (target_type == TypeManager.short_type)
3222 else if (target_type == TypeManager.ushort_type)
3224 else if (target_type == TypeManager.int64_type)
3226 else if (target_type == TypeManager.uint64_type)
3229 } else if (c is SByteConstant){
3230 sbyte v = ((SByteConstant) c).Value;
3232 if (target_type == TypeManager.int32_type)
3234 else if (target_type == TypeManager.uint32_type){
3237 } else if (target_type == TypeManager.byte_type){
3240 } else if (target_type == TypeManager.short_type)
3242 else if (target_type == TypeManager.ushort_type){
3245 } else if (target_type == TypeManager.int64_type)
3247 else if (target_type == TypeManager.uint64_type){
3252 } else if (c is ShortConstant){
3253 short v = ((ShortConstant) c).Value;
3255 if (target_type == TypeManager.int32_type){
3257 } else if (target_type == TypeManager.uint32_type){
3260 } else if (target_type == TypeManager.byte_type){
3261 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3263 } else if (target_type == TypeManager.sbyte_type){
3264 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3266 } else if (target_type == TypeManager.ushort_type){
3269 } else if (target_type == TypeManager.int64_type)
3271 else if (target_type == TypeManager.uint64_type)
3275 } else if (c is UShortConstant){
3276 ushort v = ((UShortConstant) c).Value;
3278 if (target_type == TypeManager.int32_type)
3280 else if (target_type == TypeManager.uint32_type)
3282 else if (target_type == TypeManager.byte_type){
3283 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3285 } else if (target_type == TypeManager.sbyte_type){
3286 if (v <= SByte.MaxValue)
3288 } else if (target_type == TypeManager.short_type){
3289 if (v <= Int16.MaxValue)
3291 } else if (target_type == TypeManager.int64_type)
3293 else if (target_type == TypeManager.uint64_type)
3297 } else if (c is CharConstant){
3298 char v = ((CharConstant) c).Value;
3300 if (target_type == TypeManager.int32_type)
3302 else if (target_type == TypeManager.uint32_type)
3304 else if (target_type == TypeManager.byte_type){
3305 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3307 } else if (target_type == TypeManager.sbyte_type){
3308 if (v <= SByte.MaxValue)
3310 } else if (target_type == TypeManager.short_type){
3311 if (v <= Int16.MaxValue)
3313 } else if (target_type == TypeManager.ushort_type)
3315 else if (target_type == TypeManager.int64_type)
3317 else if (target_type == TypeManager.uint64_type)
3322 } else if (c is DoubleConstant){
3323 double v = ((DoubleConstant) c).Value;
3325 if (target_type == TypeManager.sbyte_type){
3326 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3327 return new SByteConstant ((sbyte) System.Math.Round (v));
3328 } else if (target_type == TypeManager.byte_type){
3329 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3330 return new ByteConstant ((byte) System.Math.Round (v));
3331 } else if (target_type == TypeManager.short_type){
3332 if (v >= Int16.MinValue && v <= Int16.MaxValue)
3333 return new ShortConstant ((short) System.Math.Round (v));
3334 } else if (target_type == TypeManager.ushort_type){
3335 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
3336 return new UShortConstant ((ushort) System.Math.Round (v));
3337 } else if (target_type == TypeManager.int32_type){
3338 if (v >= Int32.MinValue && v <= Int32.MaxValue)
3339 return new IntConstant ((int) System.Math.Round (v));
3340 } else if (target_type == TypeManager.uint32_type){
3341 if (v >= 0 && v <= UInt32.MaxValue)
3342 return new UIntConstant ((uint) System.Math.Round (v));
3343 } else if (target_type == TypeManager.uint64_type){
3345 return new ULongConstant ((ulong) System.Math.Round (v));
3350 Error_ConstantValueCannotBeConverted (loc, s, target_type);
3355 // Load the object from the pointer.
3357 public static void LoadFromPtr (ILGenerator ig, Type t)
3359 if (t == TypeManager.int32_type)
3360 ig.Emit (OpCodes.Ldind_I4);
3361 else if (t == TypeManager.uint32_type)
3362 ig.Emit (OpCodes.Ldind_U4);
3363 else if (t == TypeManager.short_type)
3364 ig.Emit (OpCodes.Ldind_I2);
3365 else if (t == TypeManager.ushort_type)
3366 ig.Emit (OpCodes.Ldind_U2);
3367 else if (t == TypeManager.char_type)
3368 ig.Emit (OpCodes.Ldind_U2);
3369 else if (t == TypeManager.byte_type)
3370 ig.Emit (OpCodes.Ldind_U1);
3371 else if (t == TypeManager.sbyte_type)
3372 ig.Emit (OpCodes.Ldind_I1);
3373 else if (t == TypeManager.uint64_type)
3374 ig.Emit (OpCodes.Ldind_I8);
3375 else if (t == TypeManager.int64_type)
3376 ig.Emit (OpCodes.Ldind_I8);
3377 else if (t == TypeManager.float_type)
3378 ig.Emit (OpCodes.Ldind_R4);
3379 else if (t == TypeManager.double_type)
3380 ig.Emit (OpCodes.Ldind_R8);
3381 else if (t == TypeManager.bool_type)
3382 ig.Emit (OpCodes.Ldind_I1);
3383 else if (t == TypeManager.intptr_type)
3384 ig.Emit (OpCodes.Ldind_I);
3385 else if (TypeManager.IsEnumType (t)) {
3386 if (t == TypeManager.enum_type)
3387 ig.Emit (OpCodes.Ldind_Ref);
3389 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
3390 } else if (t.IsValueType)
3391 ig.Emit (OpCodes.Ldobj, t);
3393 ig.Emit (OpCodes.Ldind_Ref);
3397 // The stack contains the pointer and the value of type 'type'
3399 public static void StoreFromPtr (ILGenerator ig, Type type)
3401 if (TypeManager.IsEnumType (type))
3402 type = TypeManager.EnumToUnderlying (type);
3403 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
3404 ig.Emit (OpCodes.Stind_I4);
3405 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
3406 ig.Emit (OpCodes.Stind_I8);
3407 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
3408 type == TypeManager.ushort_type)
3409 ig.Emit (OpCodes.Stind_I2);
3410 else if (type == TypeManager.float_type)
3411 ig.Emit (OpCodes.Stind_R4);
3412 else if (type == TypeManager.double_type)
3413 ig.Emit (OpCodes.Stind_R8);
3414 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
3415 type == TypeManager.bool_type)
3416 ig.Emit (OpCodes.Stind_I1);
3417 else if (type == TypeManager.intptr_type)
3418 ig.Emit (OpCodes.Stind_I);
3419 else if (type.IsValueType)
3420 ig.Emit (OpCodes.Stobj, type);
3422 ig.Emit (OpCodes.Stind_Ref);
3426 // Returns the size of type 't' if known, otherwise, 0
3428 public static int GetTypeSize (Type t)
3430 t = TypeManager.TypeToCoreType (t);
3431 if (t == TypeManager.int32_type ||
3432 t == TypeManager.uint32_type ||
3433 t == TypeManager.float_type)
3435 else if (t == TypeManager.int64_type ||
3436 t == TypeManager.uint64_type ||
3437 t == TypeManager.double_type)
3439 else if (t == TypeManager.byte_type ||
3440 t == TypeManager.sbyte_type ||
3441 t == TypeManager.bool_type)
3443 else if (t == TypeManager.short_type ||
3444 t == TypeManager.char_type ||
3445 t == TypeManager.ushort_type)
3447 else if (t == TypeManager.decimal_type)
3454 // Default implementation of IAssignMethod.CacheTemporaries
3456 public void CacheTemporaries (EmitContext ec)
3460 static void Error_NegativeArrayIndex (Location loc)
3462 Report.Error (284, loc, "Can not create array with a negative size");
3466 // Converts 'source' to an int, uint, long or ulong.
3468 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
3472 bool old_checked = ec.CheckState;
3473 ec.CheckState = true;
3475 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
3476 if (target == null){
3477 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
3478 if (target == null){
3479 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
3480 if (target == null){
3481 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
3483 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
3487 ec.CheckState = old_checked;
3490 // Only positive constants are allowed at compile time
3492 if (target is Constant){
3493 if (target is IntConstant){
3494 if (((IntConstant) target).Value < 0){
3495 Error_NegativeArrayIndex (loc);
3500 if (target is LongConstant){
3501 if (((LongConstant) target).Value < 0){
3502 Error_NegativeArrayIndex (loc);
3515 /// This is just a base class for expressions that can
3516 /// appear on statements (invocations, object creation,
3517 /// assignments, post/pre increment and decrement). The idea
3518 /// being that they would support an extra Emition interface that
3519 /// does not leave a result on the stack.
3521 public abstract class ExpressionStatement : Expression {
3524 /// Requests the expression to be emitted in a 'statement'
3525 /// context. This means that no new value is left on the
3526 /// stack after invoking this method (constrasted with
3527 /// Emit that will always leave a value on the stack).
3529 public abstract void EmitStatement (EmitContext ec);
3533 /// This kind of cast is used to encapsulate the child
3534 /// whose type is child.Type into an expression that is
3535 /// reported to return "return_type". This is used to encapsulate
3536 /// expressions which have compatible types, but need to be dealt
3537 /// at higher levels with.
3539 /// For example, a "byte" expression could be encapsulated in one
3540 /// of these as an "unsigned int". The type for the expression
3541 /// would be "unsigned int".
3544 public class EmptyCast : Expression {
3545 protected Expression child;
3547 public EmptyCast (Expression child, Type return_type)
3549 eclass = child.eclass;
3554 public override Expression DoResolve (EmitContext ec)
3556 // This should never be invoked, we are born in fully
3557 // initialized state.
3562 public override void Emit (EmitContext ec)
3569 /// This class is used to wrap literals which belong inside Enums
3571 public class EnumConstant : Constant {
3572 public Constant Child;
3574 public EnumConstant (Constant child, Type enum_type)
3576 eclass = child.eclass;
3581 public override Expression DoResolve (EmitContext ec)
3583 // This should never be invoked, we are born in fully
3584 // initialized state.
3589 public override void Emit (EmitContext ec)
3594 public override object GetValue ()
3596 return Child.GetValue ();
3600 // Converts from one of the valid underlying types for an enumeration
3601 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3602 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3604 public Constant WidenToCompilerConstant ()
3606 Type t = TypeManager.EnumToUnderlying (Child.Type);
3607 object v = ((Constant) Child).GetValue ();;
3609 if (t == TypeManager.int32_type)
3610 return new IntConstant ((int) v);
3611 if (t == TypeManager.uint32_type)
3612 return new UIntConstant ((uint) v);
3613 if (t == TypeManager.int64_type)
3614 return new LongConstant ((long) v);
3615 if (t == TypeManager.uint64_type)
3616 return new ULongConstant ((ulong) v);
3617 if (t == TypeManager.short_type)
3618 return new ShortConstant ((short) v);
3619 if (t == TypeManager.ushort_type)
3620 return new UShortConstant ((ushort) v);
3621 if (t == TypeManager.byte_type)
3622 return new ByteConstant ((byte) v);
3623 if (t == TypeManager.sbyte_type)
3624 return new SByteConstant ((sbyte) v);
3626 throw new Exception ("Invalid enumeration underlying type: " + t);
3630 // Extracts the value in the enumeration on its native representation
3632 public object GetPlainValue ()
3634 Type t = TypeManager.EnumToUnderlying (Child.Type);
3635 object v = ((Constant) Child).GetValue ();;
3637 if (t == TypeManager.int32_type)
3639 if (t == TypeManager.uint32_type)
3641 if (t == TypeManager.int64_type)
3643 if (t == TypeManager.uint64_type)
3645 if (t == TypeManager.short_type)
3647 if (t == TypeManager.ushort_type)
3649 if (t == TypeManager.byte_type)
3651 if (t == TypeManager.sbyte_type)
3657 public override string AsString ()
3659 return Child.AsString ();
3662 public override DoubleConstant ConvertToDouble ()
3664 return Child.ConvertToDouble ();
3667 public override FloatConstant ConvertToFloat ()
3669 return Child.ConvertToFloat ();
3672 public override ULongConstant ConvertToULong ()
3674 return Child.ConvertToULong ();
3677 public override LongConstant ConvertToLong ()
3679 return Child.ConvertToLong ();
3682 public override UIntConstant ConvertToUInt ()
3684 return Child.ConvertToUInt ();
3687 public override IntConstant ConvertToInt ()
3689 return Child.ConvertToInt ();
3694 /// This kind of cast is used to encapsulate Value Types in objects.
3696 /// The effect of it is to box the value type emitted by the previous
3699 public class BoxedCast : EmptyCast {
3701 public BoxedCast (Expression expr)
3702 : base (expr, TypeManager.object_type)
3706 public override Expression DoResolve (EmitContext ec)
3708 // This should never be invoked, we are born in fully
3709 // initialized state.
3714 public override void Emit (EmitContext ec)
3718 ec.ig.Emit (OpCodes.Box, child.Type);
3722 public class UnboxCast : EmptyCast {
3723 public UnboxCast (Expression expr, Type return_type)
3724 : base (expr, return_type)
3728 public override Expression DoResolve (EmitContext ec)
3730 // This should never be invoked, we are born in fully
3731 // initialized state.
3736 public override void Emit (EmitContext ec)
3739 ILGenerator ig = ec.ig;
3742 ig.Emit (OpCodes.Unbox, t);
3744 LoadFromPtr (ig, t);
3749 /// This is used to perform explicit numeric conversions.
3751 /// Explicit numeric conversions might trigger exceptions in a checked
3752 /// context, so they should generate the conv.ovf opcodes instead of
3755 public class ConvCast : EmptyCast {
3756 public enum Mode : byte {
3757 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3759 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3760 U2_I1, U2_U1, U2_I2, U2_CH,
3761 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3762 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3763 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3764 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3765 CH_I1, CH_U1, CH_I2,
3766 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3767 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3773 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3774 : base (child, return_type)
3776 checked_state = ec.CheckState;
3780 public override Expression DoResolve (EmitContext ec)
3782 // This should never be invoked, we are born in fully
3783 // initialized state.
3788 public override void Emit (EmitContext ec)
3790 ILGenerator ig = ec.ig;
3796 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3797 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3798 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3799 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3800 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3802 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3803 case Mode.U1_CH: /* nothing */ break;
3805 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3806 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3807 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3808 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3809 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3810 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3812 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3813 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3814 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3815 case Mode.U2_CH: /* nothing */ break;
3817 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3818 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3819 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3820 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3821 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3822 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3823 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3825 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3826 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3827 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3828 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3829 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3830 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3832 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3833 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3834 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3835 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3836 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3837 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3838 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3839 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3841 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3842 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3843 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3844 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3845 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3846 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3847 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3848 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3850 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3851 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3852 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3854 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3855 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3856 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3857 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3858 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3859 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3860 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3861 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3862 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3864 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3865 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3866 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3867 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3868 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3869 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3870 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3871 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3872 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3873 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3877 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3878 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3879 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3880 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3881 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3883 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3884 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3886 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3887 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3888 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3889 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3890 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3891 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3893 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3894 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3895 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3896 case Mode.U2_CH: /* nothing */ break;
3898 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3899 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3900 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3901 case Mode.I4_U4: /* nothing */ break;
3902 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3903 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3904 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3906 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3907 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3908 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3909 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3910 case Mode.U4_I4: /* nothing */ break;
3911 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3913 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3914 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3915 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3916 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3917 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3918 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3919 case Mode.I8_U8: /* nothing */ break;
3920 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3922 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3923 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3924 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3925 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3926 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3927 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3928 case Mode.U8_I8: /* nothing */ break;
3929 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3931 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3932 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3933 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3935 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3936 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3937 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3938 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3939 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3940 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3941 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3942 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3943 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3945 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3946 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3947 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3948 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3949 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3950 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3951 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3952 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3953 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3954 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3960 public class OpcodeCast : EmptyCast {
3964 public OpcodeCast (Expression child, Type return_type, OpCode op)
3965 : base (child, return_type)
3969 second_valid = false;
3972 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3973 : base (child, return_type)
3978 second_valid = true;
3981 public override Expression DoResolve (EmitContext ec)
3983 // This should never be invoked, we are born in fully
3984 // initialized state.
3989 public override void Emit (EmitContext ec)
4000 public class NumericToBoolCast : EmptyCast
4004 public NumericToBoolCast (Expression src, Type src_type)
4005 : base (src, TypeManager.bool_type)
4008 this.src_type = src_type;
4011 public override Expression DoResolve (EmitContext ec)
4016 public override void Emit (EmitContext ec)
4020 if (src_type == TypeManager.byte_type ||
4021 src_type == TypeManager.short_type ||
4022 src_type == TypeManager.int32_type) {
4024 ec.ig.Emit (OpCodes.Ldc_I4_0);
4025 ec.ig.Emit (OpCodes.Cgt_Un);
4029 if (src_type == TypeManager.int64_type) {
4030 ec.ig.Emit (OpCodes.Ldc_I8, (long) 0);
4031 ec.ig.Emit (OpCodes.Cgt_Un);
4035 if (src_type == TypeManager.float_type) {
4036 ec.ig.Emit (OpCodes.Ldc_R4, (float) 0);
4037 ec.ig.Emit (OpCodes.Ceq);
4038 ec.ig.Emit (OpCodes.Ldc_I4_0);
4039 ec.ig.Emit (OpCodes.Ceq);
4043 if (src_type == TypeManager.double_type) {
4044 ec.ig.Emit (OpCodes.Ldc_R8, (double) 0);
4045 ec.ig.Emit (OpCodes.Ceq);
4046 ec.ig.Emit (OpCodes.Ldc_I4_0);
4047 ec.ig.Emit (OpCodes.Ceq);
4053 public class BoolToNumericCast : EmptyCast
4058 public BoolToNumericCast (Expression src, Type target_type)
4059 : base (src, target_type)
4062 this.target_type = target_type;
4065 public override Expression DoResolve (EmitContext ec)
4070 public override void Emit (EmitContext ec)
4074 if (target_type == TypeManager.byte_type) {
4075 conv = OpCodes.Conv_U1;
4076 } else if (target_type == TypeManager.short_type) {
4077 conv = OpCodes.Conv_I2;
4078 } else if (target_type == TypeManager.int32_type) {
4079 conv = OpCodes.Conv_I4;
4080 } else if (target_type == TypeManager.int64_type) {
4081 conv = OpCodes.Conv_I8;
4082 } else if (target_type == TypeManager.float_type) {
4083 conv = OpCodes.Conv_R4;
4084 } else if (target_type == TypeManager.double_type) {
4085 conv = OpCodes.Conv_R8;
4088 ec.ig.Emit (OpCodes.Ldc_I4_0);
4089 ec.ig.Emit (OpCodes.Cgt_Un);
4090 ec.ig.Emit (OpCodes.Neg);
4097 /// This kind of cast is used to encapsulate a child and cast it
4098 /// to the class requested
4100 public class ClassCast : EmptyCast {
4101 public ClassCast (Expression child, Type return_type)
4102 : base (child, return_type)
4107 public override Expression DoResolve (EmitContext ec)
4109 // This should never be invoked, we are born in fully
4110 // initialized state.
4115 public override void Emit (EmitContext ec)
4119 ec.ig.Emit (OpCodes.Castclass, type);
4125 /// SimpleName expressions are initially formed of a single
4126 /// word and it only happens at the beginning of the expression.
4130 /// The expression will try to be bound to a Field, a Method
4131 /// group or a Property. If those fail we pass the name to our
4132 /// caller and the SimpleName is compounded to perform a type
4133 /// lookup. The idea behind this process is that we want to avoid
4134 /// creating a namespace map from the assemblies, as that requires
4135 /// the GetExportedTypes function to be called and a hashtable to
4136 /// be constructed which reduces startup time. If later we find
4137 /// that this is slower, we should create a 'NamespaceExpr' expression
4138 /// that fully participates in the resolution process.
4140 /// For example 'System.Console.WriteLine' is decomposed into
4141 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
4143 /// The first SimpleName wont produce a match on its own, so it will
4145 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
4147 /// System.Console will produce a TypeExpr match.
4149 /// The downside of this is that we might be hitting 'LookupType' too many
4150 /// times with this scheme.
4152 public class SimpleName : Expression, ITypeExpression {
4153 public readonly string Name;
4155 public SimpleName (string name, Location l)
4161 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
4163 if (ec.IsFieldInitializer)
4166 "A field initializer cannot reference the non-static field, " +
4167 "method or property '"+name+"'");
4171 "An object reference is required " +
4172 "for the non-static field '"+name+"'");
4176 // Checks whether we are trying to access an instance
4177 // property, method or field from a static body.
4179 Expression MemberStaticCheck (EmitContext ec, Expression e)
4181 if (e is IMemberExpr){
4182 IMemberExpr member = (IMemberExpr) e;
4184 if (!member.IsStatic){
4185 Error_ObjectRefRequired (ec, loc, Name);
4193 public override Expression DoResolve (EmitContext ec)
4195 return SimpleNameResolve (ec, null, false);
4198 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
4200 return SimpleNameResolve (ec, right_side, false);
4204 public Expression DoResolveAllowStatic (EmitContext ec)
4206 return SimpleNameResolve (ec, null, true);
4209 public Expression DoResolveType (EmitContext ec)
4212 // Stage 3: Lookup symbol in the various namespaces.
4214 DeclSpace ds = ec.DeclSpace;
4218 if (ec.ResolvingTypeTree){
4219 int errors = Report.Errors;
4220 Type dt = ec.DeclSpace.FindType (loc, Name);
4221 if (Report.Errors != errors)
4225 return new TypeExpr (dt, loc);
4228 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
4229 return new TypeExpr (t, loc);
4233 // Stage 2 part b: Lookup up if we are an alias to a type
4236 // Since we are cheating: we only do the Alias lookup for
4237 // namespaces if the name does not include any dots in it
4240 alias_value = ec.DeclSpace.LookupAlias (Name);
4242 if (Name.IndexOf ('.') == -1 && alias_value != null) {
4243 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
4244 return new TypeExpr (t, loc);
4246 // we have alias value, but it isn't Type, so try if it's namespace
4247 return new SimpleName (alias_value, loc);
4250 // No match, maybe our parent can compose us
4251 // into something meaningful.
4256 /// 7.5.2: Simple Names.
4258 /// Local Variables and Parameters are handled at
4259 /// parse time, so they never occur as SimpleNames.
4261 /// The 'allow_static' flag is used by MemberAccess only
4262 /// and it is used to inform us that it is ok for us to
4263 /// avoid the static check, because MemberAccess might end
4264 /// up resolving the Name as a Type name and the access as
4265 /// a static type access.
4267 /// ie: Type Type; .... { Type.GetType (""); }
4269 /// Type is both an instance variable and a Type; Type.GetType
4270 /// is the static method not an instance method of type.
4272 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
4274 Expression e = null;
4277 // Stage 1: Performed by the parser (binding to locals or parameters).
4279 Block current_block = ec.CurrentBlock;
4280 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
4281 LocalVariableReference var;
4283 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
4285 if (right_side != null)
4286 return var.ResolveLValue (ec, right_side);
4288 return var.Resolve (ec);
4291 if (current_block != null){
4293 Parameter par = null;
4294 Parameters pars = current_block.Parameters;
4296 par = pars.GetParameterByName (Name, out idx);
4299 ParameterReference param;
4301 param = new ParameterReference (pars, idx, Name, loc);
4303 if (right_side != null)
4304 return param.ResolveLValue (ec, right_side);
4306 return param.Resolve (ec);
4311 // Stage 2: Lookup members
4315 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
4316 // Hence we have two different cases
4319 DeclSpace lookup_ds = ec.DeclSpace;
4321 if (lookup_ds.TypeBuilder == null)
4324 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
4329 // Classes/structs keep looking, enums break
4331 if (lookup_ds is TypeContainer)
4332 lookup_ds = ((TypeContainer) lookup_ds).Parent;
4335 } while (lookup_ds != null);
4337 if (e == null && ec.ContainerType != null)
4338 e = MemberLookup (ec, ec.ContainerType, Name, loc);
4340 // #52067 - Start - Trying to solve
4344 ArrayList lookups = new ArrayList();
4345 ArrayList typelookups = new ArrayList();
4347 int split = Name.LastIndexOf('.');
4349 String nameSpacePart = Name.Substring(0, split);
4350 String memberNamePart = Name.Substring(split + 1);
4351 foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
4352 e = MemberLookup(ec, type, memberNamePart, loc);
4355 typelookups.Add(type);
4360 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
4361 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
4362 e = MemberLookup(ec, type, Name, loc);
4365 typelookups.Add(type);
4368 if (lookups.Count == 1) {
4369 e = (Expression)lookups[0];
4371 if (lookups.Count > 1) {
4372 StringBuilder sb = new StringBuilder();
4373 foreach(Type type in typelookups)
4374 sb.Append("'" + type.FullName + "'");
4375 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
4384 return DoResolveType (ec);
4389 if (e is IMemberExpr) {
4390 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
4394 IMemberExpr me = e as IMemberExpr;
4398 // This fails if ResolveMemberAccess() was unable to decide whether
4399 // it's a field or a type of the same name.
4400 if (!me.IsStatic && (me.InstanceExpression == null))
4403 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
4404 Maybe a grammar-related problem?
4407 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
4408 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
4409 "outer type '" + me.DeclaringType + "' via nested type '" +
4410 me.InstanceExpression.Type + "'");
4414 if (right_side != null)
4415 e = e.DoResolveLValue (ec, right_side);
4417 e = e.DoResolve (ec);
4422 if (ec.IsStatic || ec.IsFieldInitializer){
4426 return MemberStaticCheck (ec, e);
4431 public override void Emit (EmitContext ec)
4434 // If this is ever reached, then we failed to
4435 // find the name as a namespace
4438 Error (30451, "The name '" + Name +
4439 "' does not exist in the class '" +
4440 ec.DeclSpace.Name + "'");
4443 public override string ToString ()
4450 /// Fully resolved expression that evaluates to a type
4452 public class TypeExpr : Expression, ITypeExpression {
4453 public TypeExpr (Type t, Location l)
4456 eclass = ExprClass.Type;
4460 public virtual Expression DoResolveType (EmitContext ec)
4465 override public Expression DoResolve (EmitContext ec)
4470 override public void Emit (EmitContext ec)
4472 throw new Exception ("Should never be called");
4475 public override string ToString ()
4477 return Type.ToString ();
4482 /// Used to create types from a fully qualified name. These are just used
4483 /// by the parser to setup the core types. A TypeLookupExpression is always
4484 /// classified as a type.
4486 public class TypeLookupExpression : TypeExpr {
4489 public TypeLookupExpression (string name) : base (null, Location.Null)
4494 public override Expression DoResolveType (EmitContext ec)
4497 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
4501 public override Expression DoResolve (EmitContext ec)
4503 return DoResolveType (ec);
4506 public override void Emit (EmitContext ec)
4508 throw new Exception ("Should never be called");
4511 public override string ToString ()
4518 /// MethodGroup Expression.
4520 /// This is a fully resolved expression that evaluates to a type
4522 public class MethodGroupExpr : Expression, IMemberExpr {
4523 public MethodBase [] Methods;
4524 Expression instance_expression = null;
4525 bool is_explicit_impl = false;
4527 public MethodGroupExpr (MemberInfo [] mi, Location l)
4529 Methods = new MethodBase [mi.Length];
4530 mi.CopyTo (Methods, 0);
4531 eclass = ExprClass.MethodGroup;
4532 type = TypeManager.object_type;
4536 public MethodGroupExpr (ArrayList list, Location l)
4538 Methods = new MethodBase [list.Count];
4541 list.CopyTo (Methods, 0);
4543 foreach (MemberInfo m in list){
4544 if (!(m is MethodBase)){
4545 Console.WriteLine ("Name " + m.Name);
4546 Console.WriteLine ("Found a: " + m.GetType ().FullName);
4552 eclass = ExprClass.MethodGroup;
4553 type = TypeManager.object_type;
4556 public Type DeclaringType {
4558 return Methods [0].DeclaringType;
4563 // 'A method group may have associated an instance expression'
4565 public Expression InstanceExpression {
4567 return instance_expression;
4571 instance_expression = value;
4575 public bool IsExplicitImpl {
4577 return is_explicit_impl;
4581 is_explicit_impl = value;
4585 public string Name {
4587 return Methods [0].Name;
4591 public bool IsInstance {
4593 foreach (MethodBase mb in Methods)
4601 public bool IsStatic {
4603 foreach (MethodBase mb in Methods)
4611 override public Expression DoResolve (EmitContext ec)
4613 if (instance_expression != null) {
4614 instance_expression = instance_expression.DoResolve (ec);
4615 if (instance_expression == null)
4622 public void ReportUsageError ()
4624 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
4625 Methods [0].Name + "()' is referenced without parentheses");
4628 override public void Emit (EmitContext ec)
4630 ReportUsageError ();
4633 bool RemoveMethods (bool keep_static)
4635 ArrayList smethods = new ArrayList ();
4637 foreach (MethodBase mb in Methods){
4638 if (mb.IsStatic == keep_static)
4642 if (smethods.Count == 0)
4645 Methods = new MethodBase [smethods.Count];
4646 smethods.CopyTo (Methods, 0);
4652 /// Removes any instance methods from the MethodGroup, returns
4653 /// false if the resulting set is empty.
4655 public bool RemoveInstanceMethods ()
4657 return RemoveMethods (true);
4661 /// Removes any static methods from the MethodGroup, returns
4662 /// false if the resulting set is empty.
4664 public bool RemoveStaticMethods ()
4666 return RemoveMethods (false);
4671 /// Fully resolved expression that evaluates to a Field
4673 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4674 public readonly FieldInfo FieldInfo;
4675 Expression instance_expr;
4677 public FieldExpr (FieldInfo fi, Location l)
4680 eclass = ExprClass.Variable;
4681 type = fi.FieldType;
4685 public string Name {
4687 return FieldInfo.Name;
4691 public bool IsInstance {
4693 return !FieldInfo.IsStatic;
4697 public bool IsStatic {
4699 return FieldInfo.IsStatic;
4703 public Type DeclaringType {
4705 return FieldInfo.DeclaringType;
4709 public Expression InstanceExpression {
4711 return instance_expr;
4715 instance_expr = value;
4719 override public Expression DoResolve (EmitContext ec)
4721 if (!FieldInfo.IsStatic){
4722 if (instance_expr == null){
4723 throw new Exception ("non-static FieldExpr without instance var\n" +
4724 "You have to assign the Instance variable\n" +
4725 "Of the FieldExpr to set this\n");
4728 // Resolve the field's instance expression while flow analysis is turned
4729 // off: when accessing a field "a.b", we must check whether the field
4730 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4731 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4732 ResolveFlags.DisableFlowAnalysis);
4733 if (instance_expr == null)
4737 // If the instance expression is a local variable or parameter.
4738 IVariable var = instance_expr as IVariable;
4739 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4745 void Report_AssignToReadonly (bool is_instance)
4750 msg = "Readonly field can not be assigned outside " +
4751 "of constructor or variable initializer";
4753 msg = "A static readonly field can only be assigned in " +
4754 "a static constructor";
4756 Report.Error (is_instance ? 191 : 198, loc, msg);
4759 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4761 IVariable var = instance_expr as IVariable;
4763 var.SetFieldAssigned (ec, FieldInfo.Name);
4765 Expression e = DoResolve (ec);
4770 if (!FieldInfo.IsInitOnly)
4774 // InitOnly fields can only be assigned in constructors
4777 if (ec.IsConstructor)
4780 Report_AssignToReadonly (true);
4785 override public void Emit (EmitContext ec)
4787 ILGenerator ig = ec.ig;
4788 bool is_volatile = false;
4790 if (FieldInfo is FieldBuilder){
4791 FieldBase f = TypeManager.GetField (FieldInfo);
4793 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4796 f.status |= Field.Status.USED;
4799 if (FieldInfo.IsStatic){
4801 ig.Emit (OpCodes.Volatile);
4803 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4805 if (instance_expr.Type.IsValueType){
4807 LocalTemporary tempo = null;
4809 if (!(instance_expr is IMemoryLocation)){
4810 tempo = new LocalTemporary (
4811 ec, instance_expr.Type);
4813 InstanceExpression.Emit (ec);
4817 ml = (IMemoryLocation) instance_expr;
4819 ml.AddressOf (ec, AddressOp.Load);
4821 instance_expr.Emit (ec);
4824 ig.Emit (OpCodes.Volatile);
4826 ig.Emit (OpCodes.Ldfld, FieldInfo);
4830 public void EmitAssign (EmitContext ec, Expression source)
4832 FieldAttributes fa = FieldInfo.Attributes;
4833 bool is_static = (fa & FieldAttributes.Static) != 0;
4834 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4835 ILGenerator ig = ec.ig;
4837 if (is_readonly && !ec.IsConstructor){
4838 Report_AssignToReadonly (!is_static);
4843 Expression instance = instance_expr;
4845 if (instance.Type.IsValueType){
4846 if (instance is IMemoryLocation){
4847 IMemoryLocation ml = (IMemoryLocation) instance;
4849 ml.AddressOf (ec, AddressOp.Store);
4851 throw new Exception ("The " + instance + " of type " +
4853 " represents a ValueType and does " +
4854 "not implement IMemoryLocation");
4860 if (FieldInfo is FieldBuilder){
4861 FieldBase f = TypeManager.GetField (FieldInfo);
4863 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4864 ig.Emit (OpCodes.Volatile);
4868 ig.Emit (OpCodes.Stsfld, FieldInfo);
4870 ig.Emit (OpCodes.Stfld, FieldInfo);
4872 if (FieldInfo is FieldBuilder){
4873 FieldBase f = TypeManager.GetField (FieldInfo);
4875 f.status |= Field.Status.ASSIGNED;
4879 public void AddressOf (EmitContext ec, AddressOp mode)
4881 ILGenerator ig = ec.ig;
4883 if (FieldInfo is FieldBuilder){
4884 FieldBase f = TypeManager.GetField (FieldInfo);
4885 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4886 ig.Emit (OpCodes.Volatile);
4889 if (FieldInfo is FieldBuilder){
4890 FieldBase f = TypeManager.GetField (FieldInfo);
4892 if ((mode & AddressOp.Store) != 0)
4893 f.status |= Field.Status.ASSIGNED;
4894 if ((mode & AddressOp.Load) != 0)
4895 f.status |= Field.Status.USED;
4899 // Handle initonly fields specially: make a copy and then
4900 // get the address of the copy.
4902 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4906 local = ig.DeclareLocal (type);
4907 ig.Emit (OpCodes.Stloc, local);
4908 ig.Emit (OpCodes.Ldloca, local);
4912 if (FieldInfo.IsStatic)
4913 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4915 if (instance_expr is IMemoryLocation)
4916 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4918 instance_expr.Emit (ec);
4919 ig.Emit (OpCodes.Ldflda, FieldInfo);
4925 /// Expression that evaluates to a Property. The Assign class
4926 /// might set the 'Value' expression if we are in an assignment.
4928 /// This is not an LValue because we need to re-write the expression, we
4929 /// can not take data from the stack and store it.
4931 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4932 public readonly PropertyInfo PropertyInfo;
4934 MethodInfo getter, setter;
4936 public ArrayList PropertyArgs;
4938 Expression instance_expr;
4940 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4943 eclass = ExprClass.PropertyAccess;
4944 PropertyArgs = null;
4948 type = TypeManager.TypeToCoreType (pi.PropertyType);
4950 ResolveAccessors (ec);
4953 public string Name {
4955 return PropertyInfo.Name;
4959 public bool IsInstance {
4965 public bool IsStatic {
4971 public Type DeclaringType {
4973 return PropertyInfo.DeclaringType;
4978 // The instance expression associated with this expression
4980 public Expression InstanceExpression {
4982 instance_expr = value;
4986 return instance_expr;
4990 public bool VerifyAssignable ()
4992 if (!PropertyInfo.CanWrite){
4993 Report.Error (200, loc,
4994 "The property '" + PropertyInfo.Name +
4995 "' can not be assigned to, as it has not set accessor");
5002 void ResolveAccessors (EmitContext ec)
5004 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
5005 MemberInfo [] group;
5007 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
5008 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
5011 // The first method is the closest to us
5013 if (group != null && group.Length > 0){
5014 getter = (MethodInfo) group [0];
5016 if (getter.IsStatic)
5021 // The first method is the closest to us
5023 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
5024 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
5025 if (group != null && group.Length > 0){
5026 setter = (MethodInfo) group [0];
5027 if (setter.IsStatic)
5032 override public Expression DoResolve (EmitContext ec)
5034 if (getter == null){
5035 Report.Error (30524, loc,
5036 "The property '" + PropertyInfo.Name +
5037 "' can not be used in " +
5038 "this context because it lacks a get accessor");
5042 if ((instance_expr == null) && ec.IsStatic && !is_static) {
5043 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
5047 if (instance_expr != null) {
5048 instance_expr = instance_expr.DoResolve (ec);
5049 if (instance_expr == null)
5056 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
5058 if (setter == null){
5059 Report.Error (30526, loc,
5060 "The property '" + PropertyInfo.Name +
5061 "' can not be used in " +
5062 "this context because it lacks a set accessor");
5066 if (instance_expr != null) {
5067 instance_expr = instance_expr.DoResolve (ec);
5068 if (instance_expr == null)
5075 override public void Emit (EmitContext ec)
5078 // Special case: length of single dimension array property is turned into ldlen
5080 if ((getter == TypeManager.system_int_array_get_length) ||
5081 (getter == TypeManager.int_array_get_length)){
5082 Type iet = instance_expr.Type;
5085 // System.Array.Length can be called, but the Type does not
5086 // support invoking GetArrayRank, so test for that case first
5088 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
5089 instance_expr.Emit (ec);
5090 ec.ig.Emit (OpCodes.Ldlen);
5094 if (PropertyArgs == null)
5095 PropertyArgs = new ArrayList ();
5096 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
5100 // Implements the IAssignMethod interface for assignments
5102 public void EmitAssign (EmitContext ec, Expression source)
5104 Argument arg = new Argument (source, Argument.AType.Expression);
5105 ArrayList args = new ArrayList ();
5108 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
5111 override public void EmitStatement (EmitContext ec)
5114 ec.ig.Emit (OpCodes.Pop);
5119 /// Fully resolved expression that evaluates to an Event
5121 public class EventExpr : Expression, IMemberExpr {
5122 public readonly EventInfo EventInfo;
5123 public Expression instance_expr;
5126 MethodInfo add_accessor, remove_accessor;
5128 public EventExpr (EventInfo ei, Location loc)
5132 eclass = ExprClass.EventAccess;
5134 add_accessor = TypeManager.GetAddMethod (ei);
5135 remove_accessor = TypeManager.GetRemoveMethod (ei);
5137 if (add_accessor.IsStatic || remove_accessor.IsStatic)
5140 if (EventInfo is MyEventBuilder)
5141 type = ((MyEventBuilder) EventInfo).EventType;
5143 type = EventInfo.EventHandlerType;
5146 public string Name {
5148 return EventInfo.Name;
5152 public bool IsInstance {
5158 public bool IsStatic {
5164 public Type DeclaringType {
5166 return EventInfo.DeclaringType;
5170 public Expression InstanceExpression {
5172 return instance_expr;
5176 instance_expr = value;
5180 Expression field_expr = null;
5182 public override Expression DoResolve (EmitContext ec)
5184 if (instance_expr != null) {
5185 instance_expr = instance_expr.DoResolve (ec);
5186 if (instance_expr == null)
5190 if (this.DeclaringType == ec.ContainerType) {
5191 MemberInfo mi = GetFieldFromEvent (this);
5194 field_expr = ExprClassFromMemberInfo (ec, mi, loc);
5195 ((FieldExpr) field_expr).InstanceExpression = instance_expr;
5196 field_expr = field_expr.DoResolve (ec);
5197 if (field_expr == null)
5204 public override void Emit (EmitContext ec)
5206 if (field_expr != null)
5207 field_expr.Emit (ec);
5210 public void EmitAddOrRemove (EmitContext ec, Expression source)
5212 Expression handler = ((Binary) source).Right;
5214 Argument arg = new Argument (handler, Argument.AType.Expression);
5215 ArrayList args = new ArrayList ();
5219 if (((Binary) source).Oper == Binary.Operator.Addition)
5220 Invocation.EmitCall (
5221 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
5223 Invocation.EmitCall (
5224 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);