2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.MonoBASIC {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This is used to tell Resolve in which types of expressions we're
44 public enum ResolveFlags {
45 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48 // Returns a type expression.
51 // Returns a method group.
54 // Allows SimpleNames to be returned.
55 // This is used by MemberAccess to construct long names that can not be
56 // partially resolved (namespace-qualified names for example).
59 // Mask of all the expression class flags.
62 // Disable control flow analysis while resolving the expression.
63 // This is used when resolving the instance expression of a field expression.
64 DisableFlowAnalysis = 16
68 // This is just as a hint to AddressOf of what will be done with the
71 public enum AddressOp {
78 /// This interface is implemented by variables
80 public interface IMemoryLocation {
82 /// The AddressOf method should generate code that loads
83 /// the address of the object and leaves it on the stack.
85 /// The 'mode' argument is used to notify the expression
86 /// of whether this will be used to read from the address or
87 /// write to the address.
89 /// This is just a hint that can be used to provide good error
90 /// reporting, and should have no other side effects.
92 void AddressOf (EmitContext ec, AddressOp mode);
96 /// This interface is implemented by variables
98 public interface IVariable {
100 /// Checks whether the variable has already been assigned at
101 /// the current position of the method's control flow and
102 /// reports an appropriate error message if not.
104 /// If the variable is a struct, then this call checks whether
105 /// all of its fields (including all private ones) have been
108 bool IsAssigned (EmitContext ec, Location loc);
111 /// Checks whether field 'name' in this struct has been assigned.
113 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
116 /// Tells the flow analysis code that the variable has already
117 /// been assigned at the current code position.
119 /// If the variable is a struct, this call marks all its fields
120 /// (including private fields) as being assigned.
122 void SetAssigned (EmitContext ec);
125 /// Tells the flow analysis code that field 'name' in this struct
126 /// has already been assigned atthe current code position.
128 void SetFieldAssigned (EmitContext ec, string name);
132 /// This interface denotes an expression which evaluates to a member
133 /// of a struct or a class.
135 public interface IMemberExpr
138 /// The name of this member.
145 /// Whether this is an instance member.
152 /// Whether this is a static member.
159 /// The type which declares this member.
166 /// The instance expression associated with this member, if it's a
167 /// non-static member.
169 Expression InstanceExpression {
175 /// Expression which resolves to a type.
177 public interface ITypeExpression
180 /// Resolve the expression, but only lookup types.
182 Expression DoResolveType (EmitContext ec);
186 /// Base class for expressions
188 public abstract class Expression {
189 public ExprClass eclass;
191 protected Location loc;
203 public Location Location {
210 /// Utility wrapper routine for Error, just to beautify the code
212 public void Error (int error, string s)
214 if (!Location.IsNull (loc))
215 Report.Error (error, loc, s);
217 Report.Error (error, s);
221 /// Utility wrapper routine for Warning, just to beautify the code
223 public void Warning (int warning, string s)
225 if (!Location.IsNull (loc))
226 Report.Warning (warning, loc, s);
228 Report.Warning (warning, s);
232 /// Utility wrapper routine for Warning, only prints the warning if
233 /// warnings of level 'level' are enabled.
235 public void Warning (int warning, int level, string s)
237 if (level <= RootContext.WarningLevel)
238 Warning (warning, s);
241 static public void Error_CannotConvertType (Location loc, Type source, Type target)
243 Report.Error (30, loc, "Cannot convert type '" +
244 TypeManager.MonoBASIC_Name (source) + "' to '" +
245 TypeManager.MonoBASIC_Name (target) + "'");
249 /// Performs semantic analysis on the Expression
253 /// The Resolve method is invoked to perform the semantic analysis
256 /// The return value is an expression (it can be the
257 /// same expression in some cases) or a new
258 /// expression that better represents this node.
260 /// For example, optimizations of Unary (LiteralInt)
261 /// would return a new LiteralInt with a negated
264 /// If there is an error during semantic analysis,
265 /// then an error should be reported (using Report)
266 /// and a null value should be returned.
268 /// There are two side effects expected from calling
269 /// Resolve(): the the field variable "eclass" should
270 /// be set to any value of the enumeration
271 /// 'ExprClass' and the type variable should be set
272 /// to a valid type (this is the type of the
275 public abstract Expression DoResolve (EmitContext ec);
277 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
279 return DoResolve (ec);
283 /// Resolves an expression and performs semantic analysis on it.
287 /// Currently Resolve wraps DoResolve to perform sanity
288 /// checking and assertion checking on what we expect from Resolve.
290 public Expression Resolve (EmitContext ec, ResolveFlags flags)
292 // Are we doing a types-only search ?
293 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294 ITypeExpression type_expr = this as ITypeExpression;
296 if (type_expr == null)
299 return type_expr.DoResolveType (ec);
302 bool old_do_flow_analysis = ec.DoFlowAnalysis;
303 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304 ec.DoFlowAnalysis = false;
308 if (this is SimpleName)
309 e = ((SimpleName) this).DoResolveAllowStatic (ec);
313 ec.DoFlowAnalysis = old_do_flow_analysis;
319 if (e is SimpleName){
320 SimpleName s = (SimpleName) e;
322 if ((flags & ResolveFlags.SimpleName) == 0) {
324 object lookup = TypeManager.MemberLookup (
325 ec.ContainerType, ec.ContainerType, AllMemberTypes,
326 AllBindingFlags | BindingFlags.NonPublic, s.Name);
328 Error (30390, "'" + s.Name + "' " +
329 "is inaccessible because of its protection level");
331 Error (30451, "The name '" + s.Name + "' could not be " +
332 "found in '" + ec.DeclSpace.Name + "'");
339 if ((e is TypeExpr) || (e is ComposedCast)) {
340 if ((flags & ResolveFlags.Type) == 0) {
350 if ((flags & ResolveFlags.VariableOrValue) == 0) {
356 case ExprClass.MethodGroup:
357 if ((flags & ResolveFlags.MethodGroup) == 0) {
358 MethodGroupExpr mg = (MethodGroupExpr) e;
359 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
360 Expression te = i.Resolve(ec);
361 //((MethodGroupExpr) e).ReportUsageError ();
367 case ExprClass.Value:
368 case ExprClass.Variable:
369 case ExprClass.PropertyAccess:
370 case ExprClass.EventAccess:
371 case ExprClass.IndexerAccess:
372 if ((flags & ResolveFlags.VariableOrValue) == 0) {
379 throw new Exception ("Expression " + e.GetType () +
380 " ExprClass is Invalid after resolve");
384 throw new Exception (
385 "Expression " + e.GetType () +
386 " did not set its type after Resolve\n" +
387 "called from: " + this.GetType ());
393 /// Resolves an expression and performs semantic analysis on it.
395 public Expression Resolve (EmitContext ec)
397 return Resolve (ec, ResolveFlags.VariableOrValue);
401 /// Resolves an expression for LValue assignment
405 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
406 /// checking and assertion checking on what we expect from Resolve
408 public Expression ResolveLValue (EmitContext ec, Expression right_side)
410 Expression e = DoResolveLValue (ec, right_side);
413 if (e is SimpleName){
414 SimpleName s = (SimpleName) e;
418 "The name '" + s.Name + "' could not be found in '" +
419 ec.DeclSpace.Name + "'");
423 if (e.eclass == ExprClass.Invalid)
424 throw new Exception ("Expression " + e +
425 " ExprClass is Invalid after resolve");
427 if (e.eclass == ExprClass.MethodGroup) {
428 MethodGroupExpr mg = (MethodGroupExpr) e;
429 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
430 Expression te = i.Resolve(ec);
432 //((MethodGroupExpr) e).ReportUsageError ();
437 throw new Exception ("Expression " + e +
438 " did not set its type after Resolve");
445 /// Emits the code for the expression
449 /// The Emit method is invoked to generate the code
450 /// for the expression.
452 public abstract void Emit (EmitContext ec);
455 /// Protected constructor. Only derivate types should
456 /// be able to be created
459 protected Expression ()
461 eclass = ExprClass.Invalid;
466 /// Returns a literalized version of a literal FieldInfo
470 /// The possible return values are:
471 /// IntConstant, UIntConstant
472 /// LongLiteral, ULongConstant
473 /// FloatConstant, DoubleConstant
476 /// The value returned is already resolved.
478 public static Constant Constantify (object v, Type t)
480 if (t == TypeManager.int32_type)
481 return new IntConstant ((int) v);
482 else if (t == TypeManager.uint32_type)
483 return new UIntConstant ((uint) v);
484 else if (t == TypeManager.int64_type)
485 return new LongConstant ((long) v);
486 else if (t == TypeManager.uint64_type)
487 return new ULongConstant ((ulong) v);
488 else if (t == TypeManager.float_type)
489 return new FloatConstant ((float) v);
490 else if (t == TypeManager.double_type)
491 return new DoubleConstant ((double) v);
492 else if (t == TypeManager.string_type)
493 return new StringConstant ((string) v);
494 else if (t == TypeManager.short_type)
495 return new ShortConstant ((short)v);
496 else if (t == TypeManager.ushort_type)
497 return new UShortConstant ((ushort)v);
498 else if (t == TypeManager.sbyte_type)
499 return new SByteConstant (((sbyte)v));
500 else if (t == TypeManager.byte_type)
501 return new ByteConstant ((byte)v);
502 else if (t == TypeManager.char_type)
503 return new CharConstant ((char)v);
504 else if (t == TypeManager.bool_type)
505 return new BoolConstant ((bool) v);
506 else if (TypeManager.IsEnumType (t)){
507 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
509 return new EnumConstant (e, t);
511 throw new Exception ("Unknown type for constant (" + t +
516 /// Returns a fully formed expression after a MemberLookup
518 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
521 return new EventExpr ((EventInfo) mi, loc);
522 else if (mi is FieldInfo)
523 return new FieldExpr ((FieldInfo) mi, loc);
524 else if (mi is PropertyInfo)
525 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
526 else if (mi is Type){
527 return new TypeExpr ((System.Type) mi, loc);
534 // FIXME: Probably implement a cache for (t,name,current_access_set)?
536 // This code could use some optimizations, but we need to do some
537 // measurements. For example, we could use a delegate to 'flag' when
538 // something can not any longer be a method-group (because it is something
542 // If the return value is an Array, then it is an array of
545 // If the return value is an MemberInfo, it is anything, but a Method
549 // FIXME: When calling MemberLookup inside an 'Invocation', we should pass
550 // the arguments here and have MemberLookup return only the methods that
551 // match the argument count/type, unlike we are doing now (we delay this
554 // This is so we can catch correctly attempts to invoke instance methods
555 // from a static body (scan for error 120 in ResolveSimpleName).
558 // FIXME: Potential optimization, have a static ArrayList
561 public static Expression MemberLookup (EmitContext ec, Type t, string name,
562 MemberTypes mt, BindingFlags bf, Location loc)
564 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
568 // Lookup type 't' for code in class 'invocation_type'. Note that it's important
569 // to set 'invocation_type' correctly since this method also checks whether the
570 // invoking class is allowed to access the member in class 't'. When you want to
571 // explicitly do a lookup in the base class, you must set both 't' and 'invocation_type'
572 // to the base class (although a derived class can access protected members of its base
573 // class it cannot do so through an instance of the base class (error CS1540)).
576 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
577 string name, MemberTypes mt, BindingFlags bf,
580 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
585 int count = mi.Length;
588 return new MethodGroupExpr (mi, loc);
590 if (mi [0] is MethodBase)
591 return new MethodGroupExpr (mi, loc);
593 return ExprClassFromMemberInfo (ec, mi [0], loc);
596 public const MemberTypes AllMemberTypes =
597 MemberTypes.Constructor |
601 MemberTypes.NestedType |
602 MemberTypes.Property;
604 public const BindingFlags AllBindingFlags =
605 BindingFlags.Public |
606 BindingFlags.Static |
607 BindingFlags.Instance |
608 BindingFlags.IgnoreCase;
610 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
612 return MemberLookup (ec, ec.ContainerType, t, name, AllMemberTypes, AllBindingFlags, loc);
615 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
617 return MemberLookup (ec, ec.ContainerType, t, name,
618 MemberTypes.Method, AllBindingFlags, loc);
622 /// This is a wrapper for MemberLookup that is not used to "probe", but
623 /// to find a final definition. If the final definition is not found, we
624 /// look for private members and display a useful debugging message if we
627 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
630 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
633 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
634 MemberTypes mt, BindingFlags bf, Location loc)
638 int errors = Report.Errors;
640 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
645 // Error has already been reported.
646 if (errors < Report.Errors)
649 e = MemberLookup (ec, t, name, AllMemberTypes,
650 AllBindingFlags | BindingFlags.NonPublic, loc);
653 30456, loc, "'" + t + "' does not contain a definition " +
654 "for '" + name + "'");
657 30390, loc, "'" + t + "." + name +
658 "' is inaccessible due to its protection level");
664 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
666 EventInfo ei = event_expr.EventInfo;
668 return TypeManager.GetPrivateFieldOfEvent (ei);
671 static EmptyExpression MyEmptyExpr;
672 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
674 Type expr_type = expr.Type;
676 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
677 // if we are a method group, emit a warning
683 // notice that it is possible to write "ValueType v = 1", the ValueType here
684 // is an abstract class, and not really a value type, so we apply the same rules.
686 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
688 // A pointer type cannot be converted to object
690 if (expr_type.IsPointer)
693 if (expr_type.IsValueType)
694 return new BoxedCast (expr);
695 if (expr_type.IsClass || expr_type.IsInterface)
696 return new EmptyCast (expr, target_type);
697 } else if (expr_type.IsSubclassOf (target_type)) {
699 // Special case: enumeration to System.Enum.
700 // System.Enum is not a value type, it is a class, so we need
701 // a boxing conversion
703 if (expr_type.IsEnum)
704 return new BoxedCast (expr);
706 return new EmptyCast (expr, target_type);
709 // This code is kind of mirrored inside StandardConversionExists
710 // with the small distinction that we only probe there
712 // Always ensure that the code here and there is in sync
714 // from the null type to any reference-type.
715 if (expr is NullLiteral && !target_type.IsValueType)
716 return new EmptyCast (expr, target_type);
718 // from any class-type S to any interface-type T.
719 if (target_type.IsInterface) {
720 if (TypeManager.ImplementsInterface (expr_type, target_type)){
721 if (expr_type.IsClass)
722 return new EmptyCast (expr, target_type);
723 else if (expr_type.IsValueType)
724 return new BoxedCast (expr);
728 // from any interface type S to interface-type T.
729 if (expr_type.IsInterface && target_type.IsInterface) {
730 if (TypeManager.ImplementsInterface (expr_type, target_type))
731 return new EmptyCast (expr, target_type);
736 // from an array-type S to an array-type of type T
737 if (expr_type.IsArray && target_type.IsArray) {
738 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
740 Type expr_element_type = expr_type.GetElementType ();
742 if (MyEmptyExpr == null)
743 MyEmptyExpr = new EmptyExpression ();
745 MyEmptyExpr.SetType (expr_element_type);
746 Type target_element_type = target_type.GetElementType ();
748 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
749 if (StandardConversionExists (MyEmptyExpr,
750 target_element_type))
751 return new EmptyCast (expr, target_type);
756 // from an array-type to System.Array
757 if (expr_type.IsArray && target_type == TypeManager.array_type)
758 return new EmptyCast (expr, target_type);
760 // from any delegate type to System.Delegate
761 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
762 target_type == TypeManager.delegate_type)
763 return new EmptyCast (expr, target_type);
765 // from any array-type or delegate type into System.ICloneable.
766 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
767 if (target_type == TypeManager.icloneable_type)
768 return new EmptyCast (expr, target_type);
778 /// Implicit Numeric Conversions.
780 /// expr is the expression to convert, returns a new expression of type
781 /// target_type or null if an implicit conversion is not possible.
783 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
784 Type target_type, Location loc)
786 Type expr_type = expr.Type;
789 // Attempt to do the implicit constant expression conversions
791 if (expr is IntConstant){
794 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
798 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
800 // Try the implicit constant expression conversion
801 // from long to ulong, instead of a nice routine,
804 long v = ((LongConstant) expr).Value;
806 return new ULongConstant ((ulong) v);
809 Type real_target_type = target_type;
811 if (target_type == TypeManager.bool_type) {
813 if (expr_type == TypeManager.decimal_type) {
814 return RTConversionExpression (ec, "System.Convert", ".ToBoolean" , expr, loc);
816 return new NumericToBoolCast (expr, expr.Type);
819 if (expr_type == TypeManager.bool_type){
821 if (real_target_type == TypeManager.sbyte_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
823 if (real_target_type == TypeManager.byte_type)
824 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
825 if (real_target_type == TypeManager.int32_type)
826 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
827 if (real_target_type == TypeManager.int64_type)
828 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
829 if (real_target_type == TypeManager.double_type)
830 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
831 if (real_target_type == TypeManager.float_type)
832 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
833 if (real_target_type == TypeManager.short_type)
834 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
835 if (real_target_type == TypeManager.decimal_type)
836 return RTConversionExpression(ec, "System.Convert", ".ToDecimal", expr, loc);
837 } else if (expr_type == TypeManager.sbyte_type){
839 // From sbyte to short, int, long, float, double.
841 if (real_target_type == TypeManager.int32_type)
842 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
843 if (real_target_type == TypeManager.int64_type)
844 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
845 if (real_target_type == TypeManager.double_type)
846 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
847 if (real_target_type == TypeManager.float_type)
848 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
849 if (real_target_type == TypeManager.short_type)
850 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
851 } else if (expr_type == TypeManager.byte_type){
853 // From byte to short, ushort, int, uint, long, ulong, float, double
855 if ((real_target_type == TypeManager.short_type) ||
856 (real_target_type == TypeManager.ushort_type) ||
857 (real_target_type == TypeManager.int32_type) ||
858 (real_target_type == TypeManager.uint32_type))
859 return new EmptyCast (expr, target_type);
861 if (real_target_type == TypeManager.uint64_type)
862 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
863 if (real_target_type == TypeManager.int64_type)
864 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
865 if (real_target_type == TypeManager.float_type)
866 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
867 if (real_target_type == TypeManager.double_type)
868 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
869 } else if (expr_type == TypeManager.short_type){
871 // From short to int, long, float, double
873 if (real_target_type == TypeManager.int32_type)
874 return new EmptyCast (expr, target_type);
875 if (real_target_type == TypeManager.int64_type)
876 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
877 if (real_target_type == TypeManager.double_type)
878 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
879 if (real_target_type == TypeManager.float_type)
880 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
881 } else if (expr_type == TypeManager.ushort_type){
883 // From ushort to int, uint, long, ulong, float, double
885 if (real_target_type == TypeManager.uint32_type)
886 return new EmptyCast (expr, target_type);
888 if (real_target_type == TypeManager.uint64_type)
889 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
890 if (real_target_type == TypeManager.int32_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
892 if (real_target_type == TypeManager.int64_type)
893 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
894 if (real_target_type == TypeManager.double_type)
895 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
896 if (real_target_type == TypeManager.float_type)
897 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
898 } else if (expr_type == TypeManager.int32_type){
900 // From int to long, float, double
902 if (real_target_type == TypeManager.int64_type)
903 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
904 if (real_target_type == TypeManager.double_type)
905 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
906 if (real_target_type == TypeManager.float_type)
907 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
908 } else if (expr_type == TypeManager.uint32_type){
910 // From uint to long, ulong, float, double
912 if (real_target_type == TypeManager.int64_type)
913 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
914 if (real_target_type == TypeManager.uint64_type)
915 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
916 if (real_target_type == TypeManager.double_type)
917 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
919 if (real_target_type == TypeManager.float_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
922 } else if (expr_type == TypeManager.int64_type){
924 // From long/ulong to float, double
926 if (real_target_type == TypeManager.double_type)
927 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
928 if (real_target_type == TypeManager.float_type)
929 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
930 } else if (expr_type == TypeManager.uint64_type){
932 // From ulong to float, double
934 if (real_target_type == TypeManager.double_type)
935 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
937 if (real_target_type == TypeManager.float_type)
938 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
940 } else if (expr_type == TypeManager.char_type){
942 // From char to ushort, int, uint, long, ulong, float, double
944 if ((real_target_type == TypeManager.ushort_type) ||
945 (real_target_type == TypeManager.int32_type) ||
946 (real_target_type == TypeManager.uint32_type))
947 return new EmptyCast (expr, target_type);
948 if (real_target_type == TypeManager.uint64_type)
949 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
950 if (real_target_type == TypeManager.int64_type)
951 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
952 if (real_target_type == TypeManager.float_type)
953 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
954 if (real_target_type == TypeManager.double_type)
955 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
956 } else if (expr_type == TypeManager.string_type){
958 if (real_target_type == TypeManager.bool_type)
959 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
960 if (real_target_type == TypeManager.decimal_type)
961 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
962 } else if (expr_type == TypeManager.float_type){
966 if (real_target_type == TypeManager.decimal_type)
967 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
968 if (real_target_type == TypeManager.double_type)
969 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
971 } else if (expr_type == TypeManager.double_type){
973 if (real_target_type == TypeManager.decimal_type)
974 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
975 } else if (expr_type == TypeManager.decimal_type){
977 if (real_target_type == TypeManager.bool_type)
978 return RTConversionExpression (ec, "System.Convert", ".ToBoolean" , expr, loc);
979 if (real_target_type == TypeManager.short_type)
980 return RTConversionExpression (ec, "System.Convert", ".ToInt16" , expr, loc);
981 if (real_target_type == TypeManager.byte_type)
982 return RTConversionExpression (ec, "System.Convert", ".ToByte" , expr, loc);
983 if (real_target_type == TypeManager.int32_type)
984 return RTConversionExpression (ec, "System.Convert", ".ToInt32" , expr, loc);
985 if (real_target_type == TypeManager.int64_type)
986 return RTConversionExpression (ec, "System.Convert", ".ToInt64" , expr, loc);
987 if (real_target_type == TypeManager.float_type)
988 return RTConversionExpression (ec, "System.Convert", ".ToSingle" , expr, loc);
989 if (real_target_type == TypeManager.double_type)
990 return RTConversionExpression (ec, "System.Convert", ".ToDouble" , expr, loc);
991 if (real_target_type == TypeManager.char_type)
992 return RTConversionExpression (ec, "System.Convert", ".ToString" , expr, loc);
999 // Tests whether an implicit reference conversion exists between expr_type
1002 public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
1005 // This is the boxed case.
1007 if (target_type == TypeManager.object_type) {
1008 if ((expr_type.IsClass) ||
1009 (expr_type.IsValueType) ||
1010 (expr_type.IsInterface))
1013 } else if (expr_type.IsSubclassOf (target_type)) {
1016 // Please remember that all code below actually comes
1017 // from ImplicitReferenceConversion so make sure code remains in sync
1019 // from any class-type S to any interface-type T.
1020 if (target_type.IsInterface) {
1021 if (TypeManager.ImplementsInterface (expr_type, target_type))
1025 // from any interface type S to interface-type T.
1026 if (expr_type.IsInterface && target_type.IsInterface)
1027 if (TypeManager.ImplementsInterface (expr_type, target_type))
1030 // from an array-type S to an array-type of type T
1031 if (expr_type.IsArray && target_type.IsArray) {
1032 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1034 Type expr_element_type = expr_type.GetElementType ();
1036 if (MyEmptyExpr == null)
1037 MyEmptyExpr = new EmptyExpression ();
1039 MyEmptyExpr.SetType (expr_element_type);
1040 Type target_element_type = target_type.GetElementType ();
1042 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1043 if (StandardConversionExists (MyEmptyExpr,
1044 target_element_type))
1049 // from an array-type to System.Array
1050 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1053 // from any delegate type to System.Delegate
1054 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1055 target_type == TypeManager.delegate_type)
1056 if (target_type.IsAssignableFrom (expr_type))
1059 // from any array-type or delegate type into System.ICloneable.
1060 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1061 if (target_type == TypeManager.icloneable_type)
1064 // from the null type to any reference-type.
1065 if (expr is NullLiteral && !target_type.IsValueType &&
1066 !TypeManager.IsEnumType (target_type))
1075 /// Same as StandardConversionExists except that it also looks at
1076 /// implicit user defined conversions - needed for overload resolution
1078 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1080 if (StandardConversionExists (expr, target_type) == true)
1084 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1094 /// Determines if a standard implicit conversion exists from
1095 /// expr_type to target_type
1097 public static bool StandardConversionExists (Expression expr, Type target_type)
1099 return WideningConversionExists (expr, expr.type, target_type);
1102 public static bool WideningConversionExists (Type expr_type, Type target_type)
1104 return WideningConversionExists (null, expr_type, target_type);
1107 public static bool WideningConversionExists (Expression expr, Type target_type)
1109 return WideningConversionExists (expr, expr.Type, target_type);
1112 public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
1115 if (expr_type == null || expr_type == TypeManager.void_type)
1118 if (expr_type == target_type)
1121 // Conversions from enum to underlying type are widening.
1122 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1123 expr_type = TypeManager.EnumToUnderlying (expr_type);
1125 if (expr_type == target_type)
1128 // First numeric conversions
1130 if (expr_type == TypeManager.sbyte_type){
1132 // From sbyte to short, int, long, float, double.
1134 if ((target_type == TypeManager.int32_type) ||
1135 (target_type == TypeManager.int64_type) ||
1136 (target_type == TypeManager.double_type) ||
1137 (target_type == TypeManager.float_type) ||
1138 (target_type == TypeManager.short_type) ||
1139 (target_type == TypeManager.decimal_type))
1142 } else if (expr_type == TypeManager.byte_type){
1144 // From byte to short, ushort, int, uint, long, ulong, float, double
1146 if ((target_type == TypeManager.short_type) ||
1147 (target_type == TypeManager.bool_type) ||
1148 (target_type == TypeManager.ushort_type) ||
1149 (target_type == TypeManager.int32_type) ||
1150 (target_type == TypeManager.uint32_type) ||
1151 (target_type == TypeManager.uint64_type) ||
1152 (target_type == TypeManager.int64_type) ||
1153 (target_type == TypeManager.float_type) ||
1154 (target_type == TypeManager.double_type) ||
1155 (target_type == TypeManager.decimal_type))
1158 } else if (expr_type == TypeManager.short_type){
1160 // From short to int, long, float, double
1162 if ((target_type == TypeManager.int32_type) ||
1163 (target_type == TypeManager.bool_type) ||
1164 (target_type == TypeManager.int64_type) ||
1165 (target_type == TypeManager.double_type) ||
1166 (target_type == TypeManager.float_type) ||
1167 (target_type == TypeManager.decimal_type))
1170 } else if (expr_type == TypeManager.ushort_type){
1172 // From ushort to int, uint, long, ulong, float, double
1174 if ((target_type == TypeManager.uint32_type) ||
1175 (target_type == TypeManager.uint64_type) ||
1176 (target_type == TypeManager.int32_type) ||
1177 (target_type == TypeManager.int64_type) ||
1178 (target_type == TypeManager.double_type) ||
1179 (target_type == TypeManager.float_type) ||
1180 (target_type == TypeManager.decimal_type))
1183 } else if (expr_type == TypeManager.int32_type){
1185 // From int to long, float, double
1187 if ((target_type == TypeManager.int64_type) ||
1188 (target_type == TypeManager.bool_type) ||
1189 (target_type == TypeManager.double_type) ||
1190 (target_type == TypeManager.float_type) ||
1191 (target_type == TypeManager.decimal_type))
1194 } else if (expr_type == TypeManager.uint32_type){
1196 // From uint to long, ulong, float, double
1198 if ((target_type == TypeManager.int64_type) ||
1199 (target_type == TypeManager.bool_type) ||
1200 (target_type == TypeManager.uint64_type) ||
1201 (target_type == TypeManager.double_type) ||
1202 (target_type == TypeManager.float_type) ||
1203 (target_type == TypeManager.decimal_type))
1206 } else if ((expr_type == TypeManager.uint64_type) ||
1207 (expr_type == TypeManager.int64_type)) {
1209 // From long/ulong to float, double
1211 if ((target_type == TypeManager.double_type) ||
1212 (target_type == TypeManager.bool_type) ||
1213 (target_type == TypeManager.float_type) ||
1214 (target_type == TypeManager.decimal_type))
1217 } else if (expr_type == TypeManager.char_type){
1219 // From char to ushort, int, uint, long, ulong, float, double
1221 if ((target_type == TypeManager.ushort_type) ||
1222 (target_type == TypeManager.int32_type) ||
1223 (target_type == TypeManager.uint32_type) ||
1224 (target_type == TypeManager.uint64_type) ||
1225 (target_type == TypeManager.int64_type) ||
1226 (target_type == TypeManager.float_type) ||
1227 (target_type == TypeManager.double_type) ||
1228 (target_type == TypeManager.decimal_type))
1231 } else if (expr_type == TypeManager.float_type){
1233 // float to double, decimal
1235 if (target_type == TypeManager.double_type)
1237 } else if (expr_type == TypeManager.double_type){
1239 if ((target_type == TypeManager.bool_type))
1243 if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
1246 if (expr is IntConstant){
1247 int value = ((IntConstant) expr).Value;
1249 if (target_type == TypeManager.sbyte_type){
1250 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1252 } else if (target_type == TypeManager.byte_type){
1253 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1255 } else if (target_type == TypeManager.short_type){
1256 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1258 } else if (target_type == TypeManager.ushort_type){
1259 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1261 } else if (target_type == TypeManager.uint32_type){
1264 } else if (target_type == TypeManager.uint64_type){
1266 // we can optimize this case: a positive int32
1267 // always fits on a uint64. But we need an opcode
1274 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1278 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1280 // Try the implicit constant expression conversion
1281 // from long to ulong, instead of a nice routine,
1282 // we just inline it
1284 long v = ((LongConstant) expr).Value;
1289 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1290 IntLiteral i = (IntLiteral) expr;
1296 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1303 // Used internally by FindMostEncompassedType, this is used
1304 // to avoid creating lots of objects in the tight loop inside
1305 // FindMostEncompassedType
1307 static EmptyExpression priv_fmet_param;
1310 /// Finds "most encompassed type" according to the spec (13.4.2)
1311 /// amongst the methods in the MethodGroupExpr
1313 static Type FindMostEncompassedType (ArrayList types)
1317 if (priv_fmet_param == null)
1318 priv_fmet_param = new EmptyExpression ();
1320 foreach (Type t in types){
1321 priv_fmet_param.SetType (t);
1328 if (StandardConversionExists (priv_fmet_param, best))
1336 // Used internally by FindMostEncompassingType, this is used
1337 // to avoid creating lots of objects in the tight loop inside
1338 // FindMostEncompassingType
1340 static EmptyExpression priv_fmee_ret;
1343 /// Finds "most encompassing type" according to the spec (13.4.2)
1344 /// amongst the types in the given set
1346 static Type FindMostEncompassingType (ArrayList types)
1350 if (priv_fmee_ret == null)
1351 priv_fmee_ret = new EmptyExpression ();
1353 foreach (Type t in types){
1354 priv_fmee_ret.SetType (best);
1361 if (StandardConversionExists (priv_fmee_ret, t))
1369 // Used to avoid creating too many objects
1371 static EmptyExpression priv_fms_expr;
1374 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1375 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1376 /// for explicit and implicit conversion operators.
1378 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1379 bool apply_explicit_conv_rules,
1382 ArrayList src_types_set = new ArrayList ();
1384 if (priv_fms_expr == null)
1385 priv_fms_expr = new EmptyExpression ();
1388 // If any operator converts from S then Sx = S
1390 Type source_type = source.Type;
1391 foreach (MethodBase mb in me.Methods){
1392 ParameterData pd = Invocation.GetParameterData (mb);
1393 Type param_type = pd.ParameterType (0);
1395 if (param_type == source_type)
1398 if (apply_explicit_conv_rules) {
1401 // Find the set of applicable user-defined conversion operators, U. This set
1403 // user-defined implicit or explicit conversion operators declared by
1404 // the classes or structs in D that convert from a type encompassing
1405 // or encompassed by S to a type encompassing or encompassed by T
1407 priv_fms_expr.SetType (param_type);
1408 if (StandardConversionExists (priv_fms_expr, source_type))
1409 src_types_set.Add (param_type);
1411 if (StandardConversionExists (source, param_type))
1412 src_types_set.Add (param_type);
1416 // Only if S is encompassed by param_type
1418 if (StandardConversionExists (source, param_type))
1419 src_types_set.Add (param_type);
1424 // Explicit Conv rules
1426 if (apply_explicit_conv_rules) {
1427 ArrayList candidate_set = new ArrayList ();
1429 foreach (Type param_type in src_types_set){
1430 if (StandardConversionExists (source, param_type))
1431 candidate_set.Add (param_type);
1434 if (candidate_set.Count != 0)
1435 return FindMostEncompassedType (candidate_set);
1441 if (apply_explicit_conv_rules)
1442 return FindMostEncompassingType (src_types_set);
1444 return FindMostEncompassedType (src_types_set);
1448 // Useful in avoiding proliferation of objects
1450 static EmptyExpression priv_fmt_expr;
1453 /// Finds the most specific target Tx according to section 13.4.4
1455 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1456 bool apply_explicit_conv_rules,
1459 ArrayList tgt_types_set = new ArrayList ();
1461 if (priv_fmt_expr == null)
1462 priv_fmt_expr = new EmptyExpression ();
1465 // If any operator converts to T then Tx = T
1467 foreach (MethodInfo mi in me.Methods){
1468 Type ret_type = mi.ReturnType;
1470 if (ret_type == target)
1473 if (apply_explicit_conv_rules) {
1476 // Find the set of applicable user-defined conversion operators, U.
1478 // This set consists of the
1479 // user-defined implicit or explicit conversion operators declared by
1480 // the classes or structs in D that convert from a type encompassing
1481 // or encompassed by S to a type encompassing or encompassed by T
1483 priv_fms_expr.SetType (ret_type);
1484 if (StandardConversionExists (priv_fms_expr, target))
1485 tgt_types_set.Add (ret_type);
1487 priv_fms_expr.SetType (target);
1488 if (StandardConversionExists (priv_fms_expr, ret_type))
1489 tgt_types_set.Add (ret_type);
1493 // Only if T is encompassed by param_type
1495 priv_fms_expr.SetType (ret_type);
1496 if (StandardConversionExists (priv_fms_expr, target))
1497 tgt_types_set.Add (ret_type);
1502 // Explicit conv rules
1504 if (apply_explicit_conv_rules) {
1505 ArrayList candidate_set = new ArrayList ();
1507 foreach (Type ret_type in tgt_types_set){
1508 priv_fmt_expr.SetType (ret_type);
1510 if (StandardConversionExists (priv_fmt_expr, target))
1511 candidate_set.Add (ret_type);
1514 if (candidate_set.Count != 0)
1515 return FindMostEncompassingType (candidate_set);
1519 // Okay, final case !
1521 if (apply_explicit_conv_rules)
1522 return FindMostEncompassedType (tgt_types_set);
1524 return FindMostEncompassingType (tgt_types_set);
1528 /// User-defined Implicit conversions
1530 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1531 Type target, Location loc)
1533 return UserDefinedConversion (ec, source, target, loc, false);
1537 /// User-defined Explicit conversions
1539 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1540 Type target, Location loc)
1542 return UserDefinedConversion (ec, source, target, loc, true);
1546 /// Computes the MethodGroup for the user-defined conversion
1547 /// operators from source_type to target_type. 'look_for_explicit'
1548 /// controls whether we should also include the list of explicit
1551 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1552 Type source_type, Type target_type,
1553 Location loc, bool look_for_explicit)
1555 Expression mg1 = null, mg2 = null;
1556 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1560 // FIXME : How does the False operator come into the picture ?
1561 // This doesn't look complete and very correct !
1563 if (target_type == TypeManager.bool_type && !look_for_explicit)
1564 op_name = "op_True";
1566 op_name = "op_Implicit";
1568 MethodGroupExpr union3;
1570 mg1 = MethodLookup (ec, source_type, op_name, loc);
1571 if (source_type.BaseType != null)
1572 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1575 union3 = (MethodGroupExpr) mg2;
1576 else if (mg2 == null)
1577 union3 = (MethodGroupExpr) mg1;
1579 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1581 mg1 = MethodLookup (ec, target_type, op_name, loc);
1584 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1586 union3 = (MethodGroupExpr) mg1;
1589 if (target_type.BaseType != null)
1590 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1594 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1596 union3 = (MethodGroupExpr) mg1;
1599 MethodGroupExpr union4 = null;
1601 if (look_for_explicit) {
1602 op_name = "op_Explicit";
1604 mg5 = MemberLookup (ec, source_type, op_name, loc);
1605 if (source_type.BaseType != null)
1606 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1608 mg7 = MemberLookup (ec, target_type, op_name, loc);
1609 if (target_type.BaseType != null)
1610 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1612 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1613 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1615 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1618 return Invocation.MakeUnionSet (union3, union4, loc);
1622 /// User-defined conversions
1624 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1625 Type target, Location loc,
1626 bool look_for_explicit)
1628 MethodGroupExpr union;
1629 Type source_type = source.Type;
1630 MethodBase method = null;
1632 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1636 Type most_specific_source, most_specific_target;
1639 foreach (MethodBase m in union.Methods){
1640 Console.WriteLine ("Name: " + m.Name);
1641 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1645 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1646 if (most_specific_source == null)
1649 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1650 if (most_specific_target == null)
1655 foreach (MethodBase mb in union.Methods){
1656 ParameterData pd = Invocation.GetParameterData (mb);
1657 MethodInfo mi = (MethodInfo) mb;
1659 if (pd.ParameterType (0) == most_specific_source &&
1660 mi.ReturnType == most_specific_target) {
1666 if (method == null || count > 1)
1671 // This will do the conversion to the best match that we
1672 // found. Now we need to perform an implict standard conversion
1673 // if the best match was not the type that we were requested
1676 if (look_for_explicit)
1677 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1679 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1685 e = new UserCast ((MethodInfo) method, source, loc);
1686 if (e.Type != target){
1687 if (!look_for_explicit)
1688 e = ConvertImplicitStandard (ec, e, target, loc);
1690 e = ConvertExplicitStandard (ec, e, target, loc);
1696 /// Converts implicitly the resolved expression 'expr' into the
1697 /// 'target_type'. It returns a new expression that can be used
1698 /// in a context that expects a 'target_type'.
1700 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1701 Type target_type, Location loc)
1703 Type expr_type = expr.Type;
1707 if (expr_type == target_type)
1710 if (target_type == null)
1711 throw new Exception ("Target type is null");
1713 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1717 e = ImplicitUserConversion (ec, expr, target_type, loc);
1722 e = NarrowingConversion (ec, expr, target_type, loc);
1730 /// Converts the resolved expression 'expr' into the
1731 /// 'target_type' using the Microsoft.VisualBasic runtime.
1732 /// It returns a new expression that can be used
1733 /// in a context that expects a 'target_type'.
1735 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1741 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1742 args = new ArrayList();
1743 arg = new Argument (expr, Argument.AType.Expression);
1745 e = (Expression) new Invocation (etmp, args, loc);
1750 static private Expression RTConversionExpression (EmitContext ec, string ns, string method, Expression expr, Location loc)
1756 etmp = Mono.MonoBASIC.Parser.DecomposeQI(ns+method, loc);
1757 args = new ArrayList();
1758 arg = new Argument (expr, Argument.AType.Expression);
1760 e = (Expression) new Invocation (etmp, args, loc);
1766 static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
1768 Type expr_type = expr.Type;
1770 if (target_type == TypeManager.sbyte_type){
1772 // To sbyte from short, int, long, float, double.
1774 if ((expr_type == TypeManager.int32_type) ||
1775 (expr_type == TypeManager.int64_type) ||
1776 (expr_type == TypeManager.double_type) ||
1777 (expr_type == TypeManager.float_type) ||
1778 (expr_type == TypeManager.short_type) ||
1779 (expr_type == TypeManager.decimal_type))
1782 } else if (target_type == TypeManager.byte_type){
1784 // To byte from short, ushort, int, uint, long, ulong, float, double
1786 if ((expr_type == TypeManager.short_type) ||
1787 (expr_type == TypeManager.ushort_type) ||
1788 (expr_type == TypeManager.int32_type) ||
1789 (expr_type == TypeManager.uint32_type) ||
1790 (expr_type == TypeManager.uint64_type) ||
1791 (expr_type == TypeManager.int64_type) ||
1792 (expr_type == TypeManager.float_type) ||
1793 (expr_type == TypeManager.double_type) ||
1794 (expr_type == TypeManager.decimal_type))
1797 } else if (target_type == TypeManager.short_type){
1799 // To short from int, long, float, double
1801 if ((expr_type == TypeManager.int32_type) ||
1802 (expr_type == TypeManager.int64_type) ||
1803 (expr_type == TypeManager.double_type) ||
1804 (expr_type == TypeManager.float_type) ||
1805 (expr_type == TypeManager.decimal_type))
1808 } else if (target_type == TypeManager.ushort_type){
1810 // To ushort from int, uint, long, ulong, float, double
1812 if ((expr_type == TypeManager.uint32_type) ||
1813 (expr_type == TypeManager.uint64_type) ||
1814 (expr_type == TypeManager.int32_type) ||
1815 (expr_type == TypeManager.int64_type) ||
1816 (expr_type == TypeManager.double_type) ||
1817 (expr_type == TypeManager.float_type) ||
1818 (expr_type == TypeManager.decimal_type))
1821 } else if (target_type == TypeManager.int32_type){
1823 // To int from long, float, double
1825 if ((expr_type == TypeManager.int64_type) ||
1826 (expr_type == TypeManager.double_type) ||
1827 (expr_type == TypeManager.float_type) ||
1828 (expr_type == TypeManager.decimal_type))
1831 } else if (target_type == TypeManager.uint32_type){
1833 // To uint from long, ulong, float, double
1835 if ((expr_type == TypeManager.int64_type) ||
1836 (expr_type == TypeManager.uint64_type) ||
1837 (expr_type == TypeManager.double_type) ||
1838 (expr_type == TypeManager.float_type) ||
1839 (expr_type == TypeManager.decimal_type))
1842 } else if ((target_type == TypeManager.uint64_type) ||
1843 (target_type == TypeManager.int64_type)) {
1845 // To long/ulong from float, double
1847 if ((expr_type == TypeManager.double_type) ||
1848 (expr_type == TypeManager.float_type) ||
1849 (expr_type == TypeManager.decimal_type))
1852 } else if (target_type == TypeManager.char_type){
1854 // To char from ushort, int, uint, long, ulong, float, double
1856 if ((expr_type == TypeManager.ushort_type) ||
1857 (expr_type == TypeManager.int32_type) ||
1858 (expr_type == TypeManager.uint32_type) ||
1859 (expr_type == TypeManager.uint64_type) ||
1860 (expr_type == TypeManager.int64_type) ||
1861 (expr_type == TypeManager.float_type) ||
1862 (expr_type == TypeManager.double_type) ||
1863 (expr_type == TypeManager.decimal_type))
1866 } else if (target_type == TypeManager.float_type){
1868 // To float from double
1870 if (expr_type == TypeManager.double_type)
1874 return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;
1877 static public Expression NarrowingConversion (EmitContext ec, Expression expr,
1878 Type target_type, Location loc)
1880 Type expr_type = expr.Type;
1882 if (target_type == TypeManager.sbyte_type){
1884 // To sbyte from short, int, long, float, double.
1886 if ((expr_type == TypeManager.int32_type) ||
1887 (expr_type == TypeManager.int64_type) ||
1888 (expr_type == TypeManager.double_type) ||
1889 (expr_type == TypeManager.float_type) ||
1890 (expr_type == TypeManager.short_type) ||
1891 (expr_type == TypeManager.decimal_type))
1892 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1894 } else if (target_type == TypeManager.byte_type){
1896 // To byte from short, ushort, int, uint, long, ulong, float, double
1898 if ((expr_type == TypeManager.short_type) ||
1899 (expr_type == TypeManager.ushort_type) ||
1900 (expr_type == TypeManager.int32_type) ||
1901 (expr_type == TypeManager.uint32_type) ||
1902 (expr_type == TypeManager.uint64_type) ||
1903 (expr_type == TypeManager.int64_type) ||
1904 (expr_type == TypeManager.float_type) ||
1905 (expr_type == TypeManager.double_type) ||
1906 (expr_type == TypeManager.decimal_type))
1907 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1909 } else if (target_type == TypeManager.short_type){
1911 // To short from int, long, float, double
1913 if ((expr_type == TypeManager.int32_type) ||
1914 (expr_type == TypeManager.int64_type) ||
1915 (expr_type == TypeManager.double_type) ||
1916 (expr_type == TypeManager.float_type) ||
1917 (expr_type == TypeManager.decimal_type))
1919 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1921 } else if (target_type == TypeManager.ushort_type){
1923 // To ushort from int, uint, long, ulong, float, double
1925 if ((expr_type == TypeManager.uint32_type) ||
1926 (expr_type == TypeManager.uint64_type) ||
1927 (expr_type == TypeManager.int32_type) ||
1928 (expr_type == TypeManager.int64_type) ||
1929 (expr_type == TypeManager.double_type) ||
1930 (expr_type == TypeManager.float_type) ||
1931 (expr_type == TypeManager.decimal_type))
1932 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1934 } else if (target_type == TypeManager.int32_type){
1936 // To int from long, float, double
1938 if ((expr_type == TypeManager.int64_type) ||
1939 (expr_type == TypeManager.double_type) ||
1940 (expr_type == TypeManager.float_type) ||
1941 (expr_type == TypeManager.decimal_type))
1942 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1944 } else if (target_type == TypeManager.uint32_type){
1946 // To uint from long, ulong, float, double
1948 if ((expr_type == TypeManager.int64_type) ||
1949 (expr_type == TypeManager.uint64_type) ||
1950 (expr_type == TypeManager.double_type) ||
1951 (expr_type == TypeManager.float_type) ||
1952 (expr_type == TypeManager.decimal_type))
1953 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1955 } else if ((target_type == TypeManager.uint64_type) ||
1956 (target_type == TypeManager.int64_type)) {
1958 // To long/ulong from float, double
1960 if ((expr_type == TypeManager.double_type) ||
1961 (expr_type == TypeManager.float_type) ||
1962 (expr_type == TypeManager.decimal_type))
1963 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1965 } else if (target_type == TypeManager.char_type){
1967 // To char from ushort, int, uint, long, ulong, float, double
1969 if ((expr_type == TypeManager.ushort_type) ||
1970 (expr_type == TypeManager.int32_type) ||
1971 (expr_type == TypeManager.uint32_type) ||
1972 (expr_type == TypeManager.uint64_type) ||
1973 (expr_type == TypeManager.int64_type) ||
1974 (expr_type == TypeManager.float_type) ||
1975 (expr_type == TypeManager.double_type) ||
1976 (expr_type == TypeManager.decimal_type))
1977 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1979 } else if (target_type == TypeManager.float_type){
1981 // To float from double
1983 if (expr_type == TypeManager.double_type)
1984 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1987 TypeCode dest_type = Type.GetTypeCode (target_type);
1988 TypeCode src_type = Type.GetTypeCode (expr_type);
1989 Expression e = null;
1991 // VB.NET Objects can be converted to anything by default
1992 // unless, that is, an exception at runtime blows it all
1993 if (src_type == TypeCode.Object) {
1994 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
1995 Cast ce = new Cast (cast_type, expr, loc);
1996 ce.IsRuntimeCast = true;
1997 return ce.Resolve (ec);
2000 switch (dest_type) {
2001 case TypeCode.String:
2003 case TypeCode.SByte:
2005 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
2007 case TypeCode.UInt16:
2008 case TypeCode.Int16:
2009 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
2011 case TypeCode.UInt32:
2012 case TypeCode.Int32:
2013 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
2015 case TypeCode.UInt64:
2016 case TypeCode.Int64:
2017 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
2020 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
2022 case TypeCode.Single:
2023 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
2025 case TypeCode.Double:
2026 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
2028 case TypeCode.Boolean:
2029 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
2031 case TypeCode.DateTime:
2032 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
2034 case TypeCode.Decimal:
2035 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
2037 case TypeCode.Object:
2038 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
2043 case TypeCode.Int32:
2044 case TypeCode.UInt32:
2046 case TypeCode.String:
2047 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
2049 case TypeCode.Object:
2050 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
2055 case TypeCode.Int16:
2056 case TypeCode.UInt16:
2058 case TypeCode.String:
2059 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
2061 case TypeCode.Object:
2062 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
2067 // Ok, this *is* broken
2068 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
2070 case TypeCode.DateTime:
2072 case TypeCode.String:
2073 e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
2075 case TypeCode.Object:
2076 e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
2082 // We must examine separately some types that
2083 // don't have a TypeCode but are supported
2085 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
2086 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
2093 /// Attempts to apply the 'Standard Implicit
2094 /// Conversion' rules to the expression 'expr' into
2095 /// the 'target_type'. It returns a new expression
2096 /// that can be used in a context that expects a
2099 /// This is different from 'ConvertImplicit' in that the
2100 /// user defined implicit conversions are excluded.
2102 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
2103 Type target_type, Location loc)
2105 Type expr_type = expr.Type;
2108 if (expr_type == target_type)
2111 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2116 e = ImplicitReferenceConversion (expr, target_type);
2120 if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
2121 expr_type = TypeManager.EnumToUnderlying (expr.Type);
2122 expr = new EmptyCast (expr, expr_type);
2123 if (expr_type == target_type)
2125 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2131 if (expr_type.IsPointer){
2132 if (target_type == TypeManager.void_ptr_type)
2133 return new EmptyCast (expr, target_type);
2136 // yep, comparing pointer types cant be done with
2137 // t1 == t2, we have to compare their element types.
2139 if (target_type.IsPointer){
2140 if (target_type.GetElementType()==expr_type.GetElementType())
2145 if (target_type.IsPointer){
2146 if (expr is NullLiteral)
2147 return new EmptyCast (expr, target_type);
2155 /// Attemps to perform an implict constant conversion of the IntConstant
2156 /// into a different data type using casts (See Implicit Constant
2157 /// Expression Conversions)
2159 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
2161 int value = ic.Value;
2164 // FIXME: This could return constants instead of EmptyCasts
2166 if (target_type == TypeManager.sbyte_type){
2167 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2168 return new SByteConstant ((sbyte) value);
2169 } else if (target_type == TypeManager.byte_type){
2170 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2171 return new ByteConstant ((byte) value);
2172 } else if (target_type == TypeManager.short_type){
2173 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2174 return new ShortConstant ((short) value);
2175 } else if (target_type == TypeManager.ushort_type){
2176 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2177 return new UShortConstant ((ushort) value);
2178 } else if (target_type == TypeManager.uint32_type){
2180 return new UIntConstant ((uint) value);
2181 } else if (target_type == TypeManager.uint64_type){
2183 // we can optimize this case: a positive int32
2184 // always fits on a uint64. But we need an opcode
2188 return new ULongConstant ((ulong) value);
2191 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
2192 Type underlying = TypeManager.EnumToUnderlying (target_type);
2193 Constant e = (Constant) ic;
2196 // Possibly, we need to create a different 0 literal before passing
2199 if (underlying == TypeManager.int64_type)
2200 e = new LongLiteral (0);
2201 else if (underlying == TypeManager.uint64_type)
2202 e = new ULongLiteral (0);
2204 return new EnumConstant (e, target_type);
2209 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
2211 string msg = "Cannot convert implicitly from '"+
2212 TypeManager.MonoBASIC_Name (source) + "' to '" +
2213 TypeManager.MonoBASIC_Name (target) + "'";
2215 throw new Exception (msg);
2217 Report.Error (29, loc, msg);
2221 /// Attemptes to implicityly convert 'target' into 'type', using
2222 /// ConvertImplicit. If there is no implicit conversion, then
2223 /// an error is signaled
2225 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
2226 Type target_type, Location loc)
2230 e = ConvertImplicit (ec, source, target_type, loc);
2236 if (source is DoubleLiteral && target_type == TypeManager.float_type){
2237 Report.Error (664, loc,
2238 "Double literal cannot be implicitly converted to " +
2239 "float type, use F suffix to create a float literal");
2242 Error_CannotConvertImplicit (loc, source.Type, target_type);
2248 /// Performs the explicit numeric conversions
2250 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
2252 Type expr_type = expr.Type;
2255 // If we have an enumeration, extract the underlying type,
2256 // use this during the comparison, but wrap around the original
2259 Type real_target_type = target_type;
2261 if (TypeManager.IsEnumType (real_target_type))
2262 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
2264 if (StandardConversionExists (expr, real_target_type)){
2265 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
2267 if (real_target_type != target_type)
2268 return new EmptyCast (ce, target_type);
2272 if (expr_type == TypeManager.sbyte_type){
2274 // From sbyte to byte, ushort, uint, ulong, char
2276 if (real_target_type == TypeManager.byte_type)
2277 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
2278 if (real_target_type == TypeManager.ushort_type)
2279 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
2280 if (real_target_type == TypeManager.uint32_type)
2281 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
2282 if (real_target_type == TypeManager.uint64_type)
2283 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
2284 if (real_target_type == TypeManager.char_type)
2285 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
2286 } else if (expr_type == TypeManager.byte_type){
2288 // From byte to sbyte and char
2290 if (real_target_type == TypeManager.sbyte_type)
2291 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
2292 if (real_target_type == TypeManager.char_type)
2293 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
2294 } else if (expr_type == TypeManager.short_type){
2296 // From short to sbyte, byte, ushort, uint, ulong, char
2298 if (real_target_type == TypeManager.sbyte_type)
2299 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
2300 if (real_target_type == TypeManager.byte_type)
2301 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
2302 if (real_target_type == TypeManager.ushort_type)
2303 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
2304 if (real_target_type == TypeManager.uint32_type)
2305 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
2306 if (real_target_type == TypeManager.uint64_type)
2307 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
2308 if (real_target_type == TypeManager.char_type)
2309 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
2310 } else if (expr_type == TypeManager.ushort_type){
2312 // From ushort to sbyte, byte, short, char
2314 if (real_target_type == TypeManager.sbyte_type)
2315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
2316 if (real_target_type == TypeManager.byte_type)
2317 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
2318 if (real_target_type == TypeManager.short_type)
2319 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
2320 if (real_target_type == TypeManager.char_type)
2321 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
2322 } else if (expr_type == TypeManager.int32_type){
2324 // From int to sbyte, byte, short, ushort, uint, ulong, char
2326 if (real_target_type == TypeManager.sbyte_type)
2327 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
2328 if (real_target_type == TypeManager.byte_type)
2329 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
2330 if (real_target_type == TypeManager.short_type)
2331 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
2332 if (real_target_type == TypeManager.ushort_type)
2333 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
2334 if (real_target_type == TypeManager.uint32_type)
2335 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2336 if (real_target_type == TypeManager.uint64_type)
2337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2338 if (real_target_type == TypeManager.char_type)
2339 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2340 } else if (expr_type == TypeManager.uint32_type){
2342 // From uint to sbyte, byte, short, ushort, int, char
2344 if (real_target_type == TypeManager.sbyte_type)
2345 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2346 if (real_target_type == TypeManager.byte_type)
2347 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2348 if (real_target_type == TypeManager.short_type)
2349 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2350 if (real_target_type == TypeManager.ushort_type)
2351 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2352 if (real_target_type == TypeManager.int32_type)
2353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2354 if (real_target_type == TypeManager.char_type)
2355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2356 } else if (expr_type == TypeManager.int64_type){
2358 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2360 if (real_target_type == TypeManager.sbyte_type)
2361 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2362 if (real_target_type == TypeManager.byte_type)
2363 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2364 if (real_target_type == TypeManager.short_type)
2365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2366 if (real_target_type == TypeManager.ushort_type)
2367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2368 if (real_target_type == TypeManager.int32_type)
2369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2370 if (real_target_type == TypeManager.uint32_type)
2371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2372 if (real_target_type == TypeManager.uint64_type)
2373 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2374 if (real_target_type == TypeManager.char_type)
2375 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2376 } else if (expr_type == TypeManager.uint64_type){
2378 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2380 if (real_target_type == TypeManager.sbyte_type)
2381 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2382 if (real_target_type == TypeManager.byte_type)
2383 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2384 if (real_target_type == TypeManager.short_type)
2385 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2386 if (real_target_type == TypeManager.ushort_type)
2387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2388 if (real_target_type == TypeManager.int32_type)
2389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2390 if (real_target_type == TypeManager.uint32_type)
2391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2392 if (real_target_type == TypeManager.int64_type)
2393 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2394 if (real_target_type == TypeManager.char_type)
2395 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2396 } else if (expr_type == TypeManager.char_type){
2398 // From char to sbyte, byte, short
2400 if (real_target_type == TypeManager.sbyte_type)
2401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2402 if (real_target_type == TypeManager.byte_type)
2403 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2404 if (real_target_type == TypeManager.short_type)
2405 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2406 } else if (expr_type == TypeManager.float_type){
2408 // From float to sbyte, byte, short,
2409 // ushort, int, uint, long, ulong, char
2412 if (real_target_type == TypeManager.sbyte_type)
2413 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2414 if (real_target_type == TypeManager.byte_type)
2415 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2416 if (real_target_type == TypeManager.short_type)
2417 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2418 if (real_target_type == TypeManager.ushort_type)
2419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2420 if (real_target_type == TypeManager.int32_type)
2421 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2422 if (real_target_type == TypeManager.uint32_type)
2423 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2424 if (real_target_type == TypeManager.int64_type)
2425 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2426 if (real_target_type == TypeManager.uint64_type)
2427 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2428 if (real_target_type == TypeManager.char_type)
2429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2430 } else if (expr_type == TypeManager.double_type){
2432 // From double to byte, byte, short,
2433 // ushort, int, uint, long, ulong,
2434 // char, float or decimal
2436 if (real_target_type == TypeManager.sbyte_type)
2437 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2438 if (real_target_type == TypeManager.byte_type)
2439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2440 if (real_target_type == TypeManager.short_type)
2441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2442 if (real_target_type == TypeManager.ushort_type)
2443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2444 if (real_target_type == TypeManager.int32_type)
2445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2446 if (real_target_type == TypeManager.uint32_type)
2447 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2448 if (real_target_type == TypeManager.int64_type)
2449 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2450 if (real_target_type == TypeManager.uint64_type)
2451 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2452 if (real_target_type == TypeManager.char_type)
2453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2454 if (real_target_type == TypeManager.float_type)
2455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2458 // decimal is taken care of by the op_Explicit methods.
2464 /// Returns whether an explicit reference conversion can be performed
2465 /// from source_type to target_type
2467 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2469 bool target_is_value_type = target_type.IsValueType;
2471 if (source_type == target_type)
2475 // From object to any reference type
2477 if (source_type == TypeManager.object_type && !target_is_value_type)
2481 // From any class S to any class-type T, provided S is a base class of T
2483 if (target_type.IsSubclassOf (source_type))
2487 // From any interface type S to any interface T provided S is not derived from T
2489 if (source_type.IsInterface && target_type.IsInterface){
2490 if (!target_type.IsSubclassOf (source_type))
2495 // From any class type S to any interface T, provided S is not sealed
2496 // and provided S does not implement T.
2498 if (target_type.IsInterface && !source_type.IsSealed &&
2499 !TypeManager.ImplementsInterface (source_type, target_type))
2503 // From any interface-type S to to any class type T, provided T is not
2504 // sealed, or provided T implements S.
2506 if (source_type.IsInterface &&
2507 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2511 // From an array type S with an element type Se to an array type T with an
2512 // element type Te provided all the following are true:
2513 // * S and T differe only in element type, in other words, S and T
2514 // have the same number of dimensions.
2515 // * Both Se and Te are reference types
2516 // * An explicit referenc conversions exist from Se to Te
2518 if (source_type.IsArray && target_type.IsArray) {
2519 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2521 Type source_element_type = source_type.GetElementType ();
2522 Type target_element_type = target_type.GetElementType ();
2524 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2525 if (ExplicitReferenceConversionExists (source_element_type,
2526 target_element_type))
2532 // From System.Array to any array-type
2533 if (source_type == TypeManager.array_type &&
2534 target_type.IsArray){
2539 // From System delegate to any delegate-type
2541 if (source_type == TypeManager.delegate_type &&
2542 target_type.IsSubclassOf (TypeManager.delegate_type))
2546 // From ICloneable to Array or Delegate types
2548 if (source_type == TypeManager.icloneable_type &&
2549 (target_type == TypeManager.array_type ||
2550 target_type == TypeManager.delegate_type))
2557 /// Implements Explicit Reference conversions
2559 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2561 Type source_type = source.Type;
2562 bool target_is_value_type = target_type.IsValueType;
2565 // From object to any reference type
2567 if (source_type == TypeManager.object_type && !target_is_value_type)
2568 return new ClassCast (source, target_type);
2572 // From any class S to any class-type T, provided S is a base class of T
2574 if (target_type.IsSubclassOf (source_type))
2575 return new ClassCast (source, target_type);
2578 // From any interface type S to any interface T provided S is not derived from T
2580 if (source_type.IsInterface && target_type.IsInterface){
2581 if (TypeManager.ImplementsInterface (source_type, target_type))
2584 return new ClassCast (source, target_type);
2588 // From any class type S to any interface T, provides S is not sealed
2589 // and provided S does not implement T.
2591 if (target_type.IsInterface && !source_type.IsSealed) {
2592 if (TypeManager.ImplementsInterface (source_type, target_type))
2595 return new ClassCast (source, target_type);
2600 // From any interface-type S to to any class type T, provided T is not
2601 // sealed, or provided T implements S.
2603 if (source_type.IsInterface) {
2604 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2605 return new ClassCast (source, target_type);
2610 // From an array type S with an element type Se to an array type T with an
2611 // element type Te provided all the following are true:
2612 // * S and T differe only in element type, in other words, S and T
2613 // have the same number of dimensions.
2614 // * Both Se and Te are reference types
2615 // * An explicit referenc conversions exist from Se to Te
2617 if (source_type.IsArray && target_type.IsArray) {
2618 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2620 Type source_element_type = source_type.GetElementType ();
2621 Type target_element_type = target_type.GetElementType ();
2623 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2624 if (ExplicitReferenceConversionExists (source_element_type,
2625 target_element_type))
2626 return new ClassCast (source, target_type);
2631 // From System.Array to any array-type
2632 if (source_type == TypeManager.array_type &&
2633 target_type.IsArray) {
2634 return new ClassCast (source, target_type);
2638 // From System delegate to any delegate-type
2640 if (source_type == TypeManager.delegate_type &&
2641 target_type.IsSubclassOf (TypeManager.delegate_type))
2642 return new ClassCast (source, target_type);
2645 // From ICloneable to Array or Delegate types
2647 if (source_type == TypeManager.icloneable_type &&
2648 (target_type == TypeManager.array_type ||
2649 target_type == TypeManager.delegate_type))
2650 return new ClassCast (source, target_type);
2656 /// Performs an explicit conversion of the expression 'expr' whose
2657 /// type is expr.Type to 'target_type'.
2659 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2660 Type target_type, bool runtimeconv, Location loc)
2662 Type expr_type = expr.Type;
2663 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2668 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2673 // Unboxing conversion.
2675 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2676 return new UnboxCast (expr, target_type);
2681 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2685 // FIXME: Is there any reason we should have EnumConstant
2686 // dealt with here instead of just using always the
2687 // UnderlyingSystemType to wrap the type?
2689 if (expr is EnumConstant)
2690 e = ((EnumConstant) expr).Child;
2692 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2695 Expression t = ConvertImplicit (ec, e, target_type, loc);
2699 t = ConvertNumericExplicit (ec, e, target_type, loc);
2703 t = NarrowingConversion (ec, e, target_type, loc);
2707 Error_CannotConvertType (loc, expr_type, target_type);
2711 ne = ConvertReferenceExplicit (expr, target_type);
2716 if (target_type.IsPointer){
2717 if (expr_type.IsPointer)
2718 return new EmptyCast (expr, target_type);
2720 if (expr_type == TypeManager.sbyte_type ||
2721 expr_type == TypeManager.byte_type ||
2722 expr_type == TypeManager.short_type ||
2723 expr_type == TypeManager.ushort_type ||
2724 expr_type == TypeManager.int32_type ||
2725 expr_type == TypeManager.uint32_type ||
2726 expr_type == TypeManager.uint64_type ||
2727 expr_type == TypeManager.int64_type)
2728 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2730 if (expr_type.IsPointer){
2731 if (target_type == TypeManager.sbyte_type ||
2732 target_type == TypeManager.byte_type ||
2733 target_type == TypeManager.short_type ||
2734 target_type == TypeManager.ushort_type ||
2735 target_type == TypeManager.int32_type ||
2736 target_type == TypeManager.uint32_type ||
2737 target_type == TypeManager.uint64_type ||
2738 target_type == TypeManager.int64_type){
2739 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2742 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2747 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2751 // We should always be able to go from an uint32
2752 // implicitly or explicitly to the other integral
2755 throw new Exception ("Internal compiler error");
2760 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2764 if (!(runtimeconv)) {
2765 ne = NarrowingConversion (ec, expr, target_type, loc);
2769 Error_CannotConvertType (loc, expr_type, target_type);
2775 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2777 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2778 Type target_type, Location l)
2780 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2785 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2789 ne = ConvertReferenceExplicit (expr, target_type);
2793 ne = NarrowingConversion (ec, expr, target_type, l);
2797 Error_CannotConvertType (l, expr.Type, target_type);
2801 static string ExprClassName (ExprClass c)
2804 case ExprClass.Invalid:
2806 case ExprClass.Value:
2808 case ExprClass.Variable:
2810 case ExprClass.Namespace:
2812 case ExprClass.Type:
2814 case ExprClass.MethodGroup:
2815 return "method group";
2816 case ExprClass.PropertyAccess:
2817 return "property access";
2818 case ExprClass.EventAccess:
2819 return "event access";
2820 case ExprClass.IndexerAccess:
2821 return "indexer access";
2822 case ExprClass.Nothing:
2825 throw new Exception ("Should not happen");
2829 /// Reports that we were expecting 'expr' to be of class 'expected'
2831 public void Error118 (string expected)
2833 string kind = "Unknown";
2835 kind = ExprClassName (eclass);
2837 Error (118, "Expression denotes a '" + kind +
2838 "' where a '" + expected + "' was expected");
2841 public void Error118 (ResolveFlags flags)
2843 ArrayList valid = new ArrayList (10);
2845 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2846 valid.Add ("variable");
2847 valid.Add ("value");
2850 if ((flags & ResolveFlags.Type) != 0)
2853 if ((flags & ResolveFlags.MethodGroup) != 0)
2854 valid.Add ("method group");
2856 if ((flags & ResolveFlags.SimpleName) != 0)
2857 valid.Add ("simple name");
2859 if (valid.Count == 0)
2860 valid.Add ("unknown");
2862 StringBuilder sb = new StringBuilder ();
2863 for (int i = 0; i < valid.Count; i++) {
2866 else if (i == valid.Count)
2868 sb.Append (valid [i]);
2871 string kind = ExprClassName (eclass);
2873 Error (119, "Expression denotes a '" + kind + "' where " +
2874 "a '" + sb.ToString () + "' was expected");
2877 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2879 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2880 TypeManager.MonoBASIC_Name (t));
2883 public static void UnsafeError (Location loc)
2885 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2889 /// Converts the IntConstant, UIntConstant, LongConstant or
2890 /// ULongConstant into the integral target_type. Notice
2891 /// that we do not return an 'Expression' we do return
2892 /// a boxed integral type.
2894 /// FIXME: Since I added the new constants, we need to
2895 /// also support conversions from CharConstant, ByteConstant,
2896 /// SByteConstant, UShortConstant, ShortConstant
2898 /// This is used by the switch statement, so the domain
2899 /// of work is restricted to the literals above, and the
2900 /// targets are int32, uint32, char, byte, sbyte, ushort,
2901 /// short, uint64 and int64
2903 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2907 if (c.Type == target_type)
2908 return ((Constant) c).GetValue ();
2911 // Make into one of the literals we handle, we dont really care
2912 // about this value as we will just return a few limited types
2914 if (c is EnumConstant)
2915 c = ((EnumConstant)c).WidenToCompilerConstant ();
2917 if (c is IntConstant){
2918 int v = ((IntConstant) c).Value;
2920 if (target_type == TypeManager.uint32_type){
2923 } else if (target_type == TypeManager.char_type){
2924 if (v >= Char.MinValue && v <= Char.MaxValue)
2926 } else if (target_type == TypeManager.byte_type){
2927 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2929 } else if (target_type == TypeManager.sbyte_type){
2930 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2932 } else if (target_type == TypeManager.short_type){
2933 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2935 } else if (target_type == TypeManager.ushort_type){
2936 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2938 } else if (target_type == TypeManager.int64_type)
2940 else if (target_type == TypeManager.uint64_type){
2946 } else if (c is UIntConstant){
2947 uint v = ((UIntConstant) c).Value;
2949 if (target_type == TypeManager.int32_type){
2950 if (v <= Int32.MaxValue)
2952 } else if (target_type == TypeManager.char_type){
2953 if (v >= Char.MinValue && v <= Char.MaxValue)
2955 } else if (target_type == TypeManager.byte_type){
2956 if (v <= Byte.MaxValue)
2958 } else if (target_type == TypeManager.sbyte_type){
2959 if (v <= SByte.MaxValue)
2961 } else if (target_type == TypeManager.short_type){
2962 if (v <= UInt16.MaxValue)
2964 } else if (target_type == TypeManager.ushort_type){
2965 if (v <= UInt16.MaxValue)
2967 } else if (target_type == TypeManager.int64_type)
2969 else if (target_type == TypeManager.uint64_type)
2972 } else if (c is LongConstant){
2973 long v = ((LongConstant) c).Value;
2975 if (target_type == TypeManager.int32_type){
2976 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2978 } else if (target_type == TypeManager.uint32_type){
2979 if (v >= 0 && v <= UInt32.MaxValue)
2981 } else if (target_type == TypeManager.char_type){
2982 if (v >= Char.MinValue && v <= Char.MaxValue)
2984 } else if (target_type == TypeManager.byte_type){
2985 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2987 } else if (target_type == TypeManager.sbyte_type){
2988 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2990 } else if (target_type == TypeManager.short_type){
2991 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2993 } else if (target_type == TypeManager.ushort_type){
2994 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2996 } else if (target_type == TypeManager.uint64_type){
3001 } else if (c is ULongConstant){
3002 ulong v = ((ULongConstant) c).Value;
3004 if (target_type == TypeManager.int32_type){
3005 if (v <= Int32.MaxValue)
3007 } else if (target_type == TypeManager.uint32_type){
3008 if (v <= UInt32.MaxValue)
3010 } else if (target_type == TypeManager.char_type){
3011 if (v >= Char.MinValue && v <= Char.MaxValue)
3013 } else if (target_type == TypeManager.byte_type){
3014 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3016 } else if (target_type == TypeManager.sbyte_type){
3017 if (v <= (int) SByte.MaxValue)
3019 } else if (target_type == TypeManager.short_type){
3020 if (v <= UInt16.MaxValue)
3022 } else if (target_type == TypeManager.ushort_type){
3023 if (v <= UInt16.MaxValue)
3025 } else if (target_type == TypeManager.int64_type){
3026 if (v <= Int64.MaxValue)
3030 } else if (c is ByteConstant){
3031 byte v = ((ByteConstant) c).Value;
3033 if (target_type == TypeManager.int32_type)
3035 else if (target_type == TypeManager.uint32_type)
3037 else if (target_type == TypeManager.char_type)
3039 else if (target_type == TypeManager.sbyte_type){
3040 if (v <= SByte.MaxValue)
3042 } else if (target_type == TypeManager.short_type)
3044 else if (target_type == TypeManager.ushort_type)
3046 else if (target_type == TypeManager.int64_type)
3048 else if (target_type == TypeManager.uint64_type)
3051 } else if (c is SByteConstant){
3052 sbyte v = ((SByteConstant) c).Value;
3054 if (target_type == TypeManager.int32_type)
3056 else if (target_type == TypeManager.uint32_type){
3059 } else if (target_type == TypeManager.char_type){
3062 } else if (target_type == TypeManager.byte_type){
3065 } else if (target_type == TypeManager.short_type)
3067 else if (target_type == TypeManager.ushort_type){
3070 } else if (target_type == TypeManager.int64_type)
3072 else if (target_type == TypeManager.uint64_type){
3077 } else if (c is ShortConstant){
3078 short v = ((ShortConstant) c).Value;
3080 if (target_type == TypeManager.int32_type){
3082 } else if (target_type == TypeManager.uint32_type){
3085 } else if (target_type == TypeManager.char_type){
3088 } else if (target_type == TypeManager.byte_type){
3089 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3091 } else if (target_type == TypeManager.sbyte_type){
3092 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3094 } else if (target_type == TypeManager.ushort_type){
3097 } else if (target_type == TypeManager.int64_type)
3099 else if (target_type == TypeManager.uint64_type)
3103 } else if (c is UShortConstant){
3104 ushort v = ((UShortConstant) c).Value;
3106 if (target_type == TypeManager.int32_type)
3108 else if (target_type == TypeManager.uint32_type)
3110 else if (target_type == TypeManager.char_type){
3111 if (v >= Char.MinValue && v <= Char.MaxValue)
3113 } else if (target_type == TypeManager.byte_type){
3114 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3116 } else if (target_type == TypeManager.sbyte_type){
3117 if (v <= SByte.MaxValue)
3119 } else if (target_type == TypeManager.short_type){
3120 if (v <= Int16.MaxValue)
3122 } else if (target_type == TypeManager.int64_type)
3124 else if (target_type == TypeManager.uint64_type)
3128 } else if (c is CharConstant){
3129 char v = ((CharConstant) c).Value;
3131 if (target_type == TypeManager.int32_type)
3133 else if (target_type == TypeManager.uint32_type)
3135 else if (target_type == TypeManager.byte_type){
3136 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3138 } else if (target_type == TypeManager.sbyte_type){
3139 if (v <= SByte.MaxValue)
3141 } else if (target_type == TypeManager.short_type){
3142 if (v <= Int16.MaxValue)
3144 } else if (target_type == TypeManager.ushort_type)
3146 else if (target_type == TypeManager.int64_type)
3148 else if (target_type == TypeManager.uint64_type)
3153 Error_ConstantValueCannotBeConverted (loc, s, target_type);
3158 // Load the object from the pointer.
3160 public static void LoadFromPtr (ILGenerator ig, Type t)
3162 if (t == TypeManager.int32_type)
3163 ig.Emit (OpCodes.Ldind_I4);
3164 else if (t == TypeManager.uint32_type)
3165 ig.Emit (OpCodes.Ldind_U4);
3166 else if (t == TypeManager.short_type)
3167 ig.Emit (OpCodes.Ldind_I2);
3168 else if (t == TypeManager.ushort_type)
3169 ig.Emit (OpCodes.Ldind_U2);
3170 else if (t == TypeManager.char_type)
3171 ig.Emit (OpCodes.Ldind_U2);
3172 else if (t == TypeManager.byte_type)
3173 ig.Emit (OpCodes.Ldind_U1);
3174 else if (t == TypeManager.sbyte_type)
3175 ig.Emit (OpCodes.Ldind_I1);
3176 else if (t == TypeManager.uint64_type)
3177 ig.Emit (OpCodes.Ldind_I8);
3178 else if (t == TypeManager.int64_type)
3179 ig.Emit (OpCodes.Ldind_I8);
3180 else if (t == TypeManager.float_type)
3181 ig.Emit (OpCodes.Ldind_R4);
3182 else if (t == TypeManager.double_type)
3183 ig.Emit (OpCodes.Ldind_R8);
3184 else if (t == TypeManager.bool_type)
3185 ig.Emit (OpCodes.Ldind_I1);
3186 else if (t == TypeManager.intptr_type)
3187 ig.Emit (OpCodes.Ldind_I);
3188 else if (TypeManager.IsEnumType (t)) {
3189 if (t == TypeManager.enum_type)
3190 ig.Emit (OpCodes.Ldind_Ref);
3192 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
3193 } else if (t.IsValueType)
3194 ig.Emit (OpCodes.Ldobj, t);
3196 ig.Emit (OpCodes.Ldind_Ref);
3200 // The stack contains the pointer and the value of type 'type'
3202 public static void StoreFromPtr (ILGenerator ig, Type type)
3204 if (TypeManager.IsEnumType (type))
3205 type = TypeManager.EnumToUnderlying (type);
3206 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
3207 ig.Emit (OpCodes.Stind_I4);
3208 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
3209 ig.Emit (OpCodes.Stind_I8);
3210 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
3211 type == TypeManager.ushort_type)
3212 ig.Emit (OpCodes.Stind_I2);
3213 else if (type == TypeManager.float_type)
3214 ig.Emit (OpCodes.Stind_R4);
3215 else if (type == TypeManager.double_type)
3216 ig.Emit (OpCodes.Stind_R8);
3217 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
3218 type == TypeManager.bool_type)
3219 ig.Emit (OpCodes.Stind_I1);
3220 else if (type == TypeManager.intptr_type)
3221 ig.Emit (OpCodes.Stind_I);
3222 else if (type.IsValueType)
3223 ig.Emit (OpCodes.Stobj, type);
3225 ig.Emit (OpCodes.Stind_Ref);
3229 // Returns the size of type 't' if known, otherwise, 0
3231 public static int GetTypeSize (Type t)
3233 t = TypeManager.TypeToCoreType (t);
3234 if (t == TypeManager.int32_type ||
3235 t == TypeManager.uint32_type ||
3236 t == TypeManager.float_type)
3238 else if (t == TypeManager.int64_type ||
3239 t == TypeManager.uint64_type ||
3240 t == TypeManager.double_type)
3242 else if (t == TypeManager.byte_type ||
3243 t == TypeManager.sbyte_type ||
3244 t == TypeManager.bool_type)
3246 else if (t == TypeManager.short_type ||
3247 t == TypeManager.char_type ||
3248 t == TypeManager.ushort_type)
3250 else if (t == TypeManager.decimal_type)
3257 // Default implementation of IAssignMethod.CacheTemporaries
3259 public void CacheTemporaries (EmitContext ec)
3263 static void Error_NegativeArrayIndex (Location loc)
3265 Report.Error (284, loc, "Can not create array with a negative size");
3269 // Converts 'source' to an int, uint, long or ulong.
3271 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
3275 bool old_checked = ec.CheckState;
3276 ec.CheckState = true;
3278 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
3279 if (target == null){
3280 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
3281 if (target == null){
3282 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
3283 if (target == null){
3284 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
3286 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
3290 ec.CheckState = old_checked;
3293 // Only positive constants are allowed at compile time
3295 if (target is Constant){
3296 if (target is IntConstant){
3297 if (((IntConstant) target).Value < 0){
3298 Error_NegativeArrayIndex (loc);
3303 if (target is LongConstant){
3304 if (((LongConstant) target).Value < 0){
3305 Error_NegativeArrayIndex (loc);
3318 /// This is just a base class for expressions that can
3319 /// appear on statements (invocations, object creation,
3320 /// assignments, post/pre increment and decrement). The idea
3321 /// being that they would support an extra Emition interface that
3322 /// does not leave a result on the stack.
3324 public abstract class ExpressionStatement : Expression {
3327 /// Requests the expression to be emitted in a 'statement'
3328 /// context. This means that no new value is left on the
3329 /// stack after invoking this method (constrasted with
3330 /// Emit that will always leave a value on the stack).
3332 public abstract void EmitStatement (EmitContext ec);
3336 /// This kind of cast is used to encapsulate the child
3337 /// whose type is child.Type into an expression that is
3338 /// reported to return "return_type". This is used to encapsulate
3339 /// expressions which have compatible types, but need to be dealt
3340 /// at higher levels with.
3342 /// For example, a "byte" expression could be encapsulated in one
3343 /// of these as an "unsigned int". The type for the expression
3344 /// would be "unsigned int".
3347 public class EmptyCast : Expression {
3348 protected Expression child;
3350 public EmptyCast (Expression child, Type return_type)
3352 eclass = child.eclass;
3357 public override Expression DoResolve (EmitContext ec)
3359 // This should never be invoked, we are born in fully
3360 // initialized state.
3365 public override void Emit (EmitContext ec)
3372 /// This class is used to wrap literals which belong inside Enums
3374 public class EnumConstant : Constant {
3375 public Constant Child;
3377 public EnumConstant (Constant child, Type enum_type)
3379 eclass = child.eclass;
3384 public override Expression DoResolve (EmitContext ec)
3386 // This should never be invoked, we are born in fully
3387 // initialized state.
3392 public override void Emit (EmitContext ec)
3397 public override object GetValue ()
3399 return Child.GetValue ();
3403 // Converts from one of the valid underlying types for an enumeration
3404 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3405 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3407 public Constant WidenToCompilerConstant ()
3409 Type t = TypeManager.EnumToUnderlying (Child.Type);
3410 object v = ((Constant) Child).GetValue ();;
3412 if (t == TypeManager.int32_type)
3413 return new IntConstant ((int) v);
3414 if (t == TypeManager.uint32_type)
3415 return new UIntConstant ((uint) v);
3416 if (t == TypeManager.int64_type)
3417 return new LongConstant ((long) v);
3418 if (t == TypeManager.uint64_type)
3419 return new ULongConstant ((ulong) v);
3420 if (t == TypeManager.short_type)
3421 return new ShortConstant ((short) v);
3422 if (t == TypeManager.ushort_type)
3423 return new UShortConstant ((ushort) v);
3424 if (t == TypeManager.byte_type)
3425 return new ByteConstant ((byte) v);
3426 if (t == TypeManager.sbyte_type)
3427 return new SByteConstant ((sbyte) v);
3429 throw new Exception ("Invalid enumeration underlying type: " + t);
3433 // Extracts the value in the enumeration on its native representation
3435 public object GetPlainValue ()
3437 Type t = TypeManager.EnumToUnderlying (Child.Type);
3438 object v = ((Constant) Child).GetValue ();;
3440 if (t == TypeManager.int32_type)
3442 if (t == TypeManager.uint32_type)
3444 if (t == TypeManager.int64_type)
3446 if (t == TypeManager.uint64_type)
3448 if (t == TypeManager.short_type)
3450 if (t == TypeManager.ushort_type)
3452 if (t == TypeManager.byte_type)
3454 if (t == TypeManager.sbyte_type)
3460 public override string AsString ()
3462 return Child.AsString ();
3465 public override DoubleConstant ConvertToDouble ()
3467 return Child.ConvertToDouble ();
3470 public override FloatConstant ConvertToFloat ()
3472 return Child.ConvertToFloat ();
3475 public override ULongConstant ConvertToULong ()
3477 return Child.ConvertToULong ();
3480 public override LongConstant ConvertToLong ()
3482 return Child.ConvertToLong ();
3485 public override UIntConstant ConvertToUInt ()
3487 return Child.ConvertToUInt ();
3490 public override IntConstant ConvertToInt ()
3492 return Child.ConvertToInt ();
3497 /// This kind of cast is used to encapsulate Value Types in objects.
3499 /// The effect of it is to box the value type emitted by the previous
3502 public class BoxedCast : EmptyCast {
3504 public BoxedCast (Expression expr)
3505 : base (expr, TypeManager.object_type)
3509 public override Expression DoResolve (EmitContext ec)
3511 // This should never be invoked, we are born in fully
3512 // initialized state.
3517 public override void Emit (EmitContext ec)
3521 ec.ig.Emit (OpCodes.Box, child.Type);
3525 public class UnboxCast : EmptyCast {
3526 public UnboxCast (Expression expr, Type return_type)
3527 : base (expr, return_type)
3531 public override Expression DoResolve (EmitContext ec)
3533 // This should never be invoked, we are born in fully
3534 // initialized state.
3539 public override void Emit (EmitContext ec)
3542 ILGenerator ig = ec.ig;
3545 ig.Emit (OpCodes.Unbox, t);
3547 LoadFromPtr (ig, t);
3552 /// This is used to perform explicit numeric conversions.
3554 /// Explicit numeric conversions might trigger exceptions in a checked
3555 /// context, so they should generate the conv.ovf opcodes instead of
3558 public class ConvCast : EmptyCast {
3559 public enum Mode : byte {
3560 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3562 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3563 U2_I1, U2_U1, U2_I2, U2_CH,
3564 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3565 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3566 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3567 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3568 CH_I1, CH_U1, CH_I2,
3569 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3570 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3576 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3577 : base (child, return_type)
3579 checked_state = ec.CheckState;
3583 public override Expression DoResolve (EmitContext ec)
3585 // This should never be invoked, we are born in fully
3586 // initialized state.
3591 public override void Emit (EmitContext ec)
3593 ILGenerator ig = ec.ig;
3599 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3600 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3601 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3602 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3603 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3605 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3606 case Mode.U1_CH: /* nothing */ break;
3608 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3609 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3610 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3611 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3612 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3613 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3615 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3616 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3617 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3618 case Mode.U2_CH: /* nothing */ break;
3620 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3621 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3622 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3623 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3624 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3625 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3626 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3628 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3629 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3630 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3631 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3632 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3633 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3635 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3636 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3637 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3638 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3639 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3640 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3641 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3642 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3644 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3645 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3646 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3647 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3648 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3649 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3650 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3651 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3653 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3654 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3655 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3657 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3658 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3659 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3660 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3661 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3662 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3663 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3664 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3665 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3667 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3668 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3669 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3670 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3671 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3672 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3673 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3674 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3675 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3676 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3680 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3681 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3682 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3683 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3684 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3686 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3687 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3689 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3690 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3691 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3692 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3693 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3694 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3696 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3697 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3698 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3699 case Mode.U2_CH: /* nothing */ break;
3701 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3702 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3703 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3704 case Mode.I4_U4: /* nothing */ break;
3705 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3706 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3707 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3709 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3710 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3711 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3712 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3713 case Mode.U4_I4: /* nothing */ break;
3714 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3716 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3717 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3718 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3719 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3720 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3721 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3722 case Mode.I8_U8: /* nothing */ break;
3723 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3725 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3726 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3727 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3728 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3729 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3730 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3731 case Mode.U8_I8: /* nothing */ break;
3732 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3734 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3735 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3736 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3738 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3739 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3740 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3741 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3742 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3743 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3744 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3745 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3746 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3748 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3749 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3750 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3751 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3752 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3753 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3754 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3755 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3756 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3757 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3763 public class OpcodeCast : EmptyCast {
3767 public OpcodeCast (Expression child, Type return_type, OpCode op)
3768 : base (child, return_type)
3773 second_valid = false;
3776 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3777 : base (child, return_type)
3782 second_valid = true;
3785 public override Expression DoResolve (EmitContext ec)
3787 // This should never be invoked, we are born in fully
3788 // initialized state.
3793 public override void Emit (EmitContext ec)
3804 public class NumericToBoolCast : EmptyCast
3808 public NumericToBoolCast (Expression src, Type src_type)
3809 : base (src, TypeManager.bool_type)
3812 this.src_type = src_type;
3815 public override Expression DoResolve (EmitContext ec)
3820 public override void Emit (EmitContext ec)
3824 if (src_type == TypeManager.byte_type ||
3825 src_type == TypeManager.short_type ||
3826 src_type == TypeManager.int32_type) {
3828 ec.ig.Emit (OpCodes.Ldc_I4_0);
3829 ec.ig.Emit (OpCodes.Cgt_Un);
3833 if (src_type == TypeManager.int64_type) {
3834 ec.ig.Emit (OpCodes.Ldc_I8, (long) 0);
3835 ec.ig.Emit (OpCodes.Cgt_Un);
3839 if (src_type == TypeManager.float_type) {
3840 ec.ig.Emit (OpCodes.Ldc_R4, (float) 0);
3841 ec.ig.Emit (OpCodes.Ceq);
3842 ec.ig.Emit (OpCodes.Ldc_I4_0);
3843 ec.ig.Emit (OpCodes.Ceq);
3847 if (src_type == TypeManager.double_type) {
3848 ec.ig.Emit (OpCodes.Ldc_R8, (double) 0);
3849 ec.ig.Emit (OpCodes.Ceq);
3850 ec.ig.Emit (OpCodes.Ldc_I4_0);
3851 ec.ig.Emit (OpCodes.Ceq);
3858 /// This kind of cast is used to encapsulate a child and cast it
3859 /// to the class requested
3861 public class ClassCast : EmptyCast {
3862 public ClassCast (Expression child, Type return_type)
3863 : base (child, return_type)
3868 public override Expression DoResolve (EmitContext ec)
3870 // This should never be invoked, we are born in fully
3871 // initialized state.
3876 public override void Emit (EmitContext ec)
3880 ec.ig.Emit (OpCodes.Castclass, type);
3886 /// SimpleName expressions are initially formed of a single
3887 /// word and it only happens at the beginning of the expression.
3891 /// The expression will try to be bound to a Field, a Method
3892 /// group or a Property. If those fail we pass the name to our
3893 /// caller and the SimpleName is compounded to perform a type
3894 /// lookup. The idea behind this process is that we want to avoid
3895 /// creating a namespace map from the assemblies, as that requires
3896 /// the GetExportedTypes function to be called and a hashtable to
3897 /// be constructed which reduces startup time. If later we find
3898 /// that this is slower, we should create a 'NamespaceExpr' expression
3899 /// that fully participates in the resolution process.
3901 /// For example 'System.Console.WriteLine' is decomposed into
3902 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3904 /// The first SimpleName wont produce a match on its own, so it will
3906 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3908 /// System.Console will produce a TypeExpr match.
3910 /// The downside of this is that we might be hitting 'LookupType' too many
3911 /// times with this scheme.
3913 public class SimpleName : Expression, ITypeExpression {
3914 public readonly string Name;
3916 public SimpleName (string name, Location l)
3922 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3924 if (ec.IsFieldInitializer)
3927 "A field initializer cannot reference the non-static field, " +
3928 "method or property '"+name+"'");
3932 "An object reference is required " +
3933 "for the non-static field '"+name+"'");
3937 // Checks whether we are trying to access an instance
3938 // property, method or field from a static body.
3940 Expression MemberStaticCheck (EmitContext ec, Expression e)
3942 if (e is IMemberExpr){
3943 IMemberExpr member = (IMemberExpr) e;
3945 if (!member.IsStatic){
3946 Error_ObjectRefRequired (ec, loc, Name);
3954 public override Expression DoResolve (EmitContext ec)
3956 return SimpleNameResolve (ec, null, false);
3959 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3961 return SimpleNameResolve (ec, right_side, false);
3965 public Expression DoResolveAllowStatic (EmitContext ec)
3967 return SimpleNameResolve (ec, null, true);
3970 public Expression DoResolveType (EmitContext ec)
3973 // Stage 3: Lookup symbol in the various namespaces.
3975 DeclSpace ds = ec.DeclSpace;
3979 if (ec.ResolvingTypeTree){
3980 int errors = Report.Errors;
3981 Type dt = ec.DeclSpace.FindType (loc, Name);
3982 if (Report.Errors != errors)
3986 return new TypeExpr (dt, loc);
3989 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3990 return new TypeExpr (t, loc);
3994 // Stage 2 part b: Lookup up if we are an alias to a type
3997 // Since we are cheating: we only do the Alias lookup for
3998 // namespaces if the name does not include any dots in it
4001 alias_value = ec.DeclSpace.LookupAlias (Name);
4003 if (Name.IndexOf ('.') == -1 && alias_value != null) {
4004 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
4005 return new TypeExpr (t, loc);
4007 // we have alias value, but it isn't Type, so try if it's namespace
4008 return new SimpleName (alias_value, loc);
4011 // No match, maybe our parent can compose us
4012 // into something meaningful.
4017 /// 7.5.2: Simple Names.
4019 /// Local Variables and Parameters are handled at
4020 /// parse time, so they never occur as SimpleNames.
4022 /// The 'allow_static' flag is used by MemberAccess only
4023 /// and it is used to inform us that it is ok for us to
4024 /// avoid the static check, because MemberAccess might end
4025 /// up resolving the Name as a Type name and the access as
4026 /// a static type access.
4028 /// ie: Type Type; .... { Type.GetType (""); }
4030 /// Type is both an instance variable and a Type; Type.GetType
4031 /// is the static method not an instance method of type.
4033 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
4035 Expression e = null;
4038 // Stage 1: Performed by the parser (binding to locals or parameters).
4040 Block current_block = ec.CurrentBlock;
4041 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
4042 LocalVariableReference var;
4044 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
4046 if (right_side != null)
4047 return var.ResolveLValue (ec, right_side);
4049 return var.Resolve (ec);
4052 if (current_block != null){
4054 Parameter par = null;
4055 Parameters pars = current_block.Parameters;
4057 par = pars.GetParameterByName (Name, out idx);
4060 ParameterReference param;
4062 param = new ParameterReference (pars, idx, Name, loc);
4064 if (right_side != null)
4065 return param.ResolveLValue (ec, right_side);
4067 return param.Resolve (ec);
4072 // Stage 2: Lookup members
4076 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
4077 // Hence we have two different cases
4080 DeclSpace lookup_ds = ec.DeclSpace;
4082 if (lookup_ds.TypeBuilder == null)
4085 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
4090 // Classes/structs keep looking, enums break
4092 if (lookup_ds is TypeContainer)
4093 lookup_ds = ((TypeContainer) lookup_ds).Parent;
4096 } while (lookup_ds != null);
4098 if (e == null && ec.ContainerType != null)
4099 e = MemberLookup (ec, ec.ContainerType, Name, loc);
4101 // #52067 - Start - Trying to solve
4105 ArrayList lookups = new ArrayList();
4106 ArrayList typelookups = new ArrayList();
4108 int split = Name.LastIndexOf('.');
4110 String nameSpacePart = Name.Substring(0, split);
4111 String memberNamePart = Name.Substring(split + 1);
4112 foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
4113 e = MemberLookup(ec, type, memberNamePart, loc);
4116 typelookups.Add(type);
4121 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
4122 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
4123 e = MemberLookup(ec, type, Name, loc);
4126 typelookups.Add(type);
4129 if (lookups.Count == 1) {
4130 e = (Expression)lookups[0];
4132 if (lookups.Count > 1) {
4133 StringBuilder sb = new StringBuilder();
4134 foreach(Type type in typelookups)
4135 sb.Append("'" + type.FullName + "'");
4136 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
4145 return DoResolveType (ec);
4150 if (e is IMemberExpr) {
4151 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
4155 IMemberExpr me = e as IMemberExpr;
4159 // This fails if ResolveMemberAccess() was unable to decide whether
4160 // it's a field or a type of the same name.
4161 if (!me.IsStatic && (me.InstanceExpression == null))
4164 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
4165 Maybe a grammar-related problem?
4168 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
4169 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
4170 "outer type '" + me.DeclaringType + "' via nested type '" +
4171 me.InstanceExpression.Type + "'");
4175 if (right_side != null)
4176 e = e.DoResolveLValue (ec, right_side);
4178 e = e.DoResolve (ec);
4183 if (ec.IsStatic || ec.IsFieldInitializer){
4187 return MemberStaticCheck (ec, e);
4192 public override void Emit (EmitContext ec)
4195 // If this is ever reached, then we failed to
4196 // find the name as a namespace
4199 Error (30451, "The name '" + Name +
4200 "' does not exist in the class '" +
4201 ec.DeclSpace.Name + "'");
4204 public override string ToString ()
4211 /// Fully resolved expression that evaluates to a type
4213 public class TypeExpr : Expression, ITypeExpression {
4214 public TypeExpr (Type t, Location l)
4217 eclass = ExprClass.Type;
4221 public virtual Expression DoResolveType (EmitContext ec)
4226 override public Expression DoResolve (EmitContext ec)
4231 override public void Emit (EmitContext ec)
4233 throw new Exception ("Should never be called");
4236 public override string ToString ()
4238 return Type.ToString ();
4243 /// Used to create types from a fully qualified name. These are just used
4244 /// by the parser to setup the core types. A TypeLookupExpression is always
4245 /// classified as a type.
4247 public class TypeLookupExpression : TypeExpr {
4250 public TypeLookupExpression (string name) : base (null, Location.Null)
4255 public override Expression DoResolveType (EmitContext ec)
4258 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
4262 public override Expression DoResolve (EmitContext ec)
4264 return DoResolveType (ec);
4267 public override void Emit (EmitContext ec)
4269 throw new Exception ("Should never be called");
4272 public override string ToString ()
4279 /// MethodGroup Expression.
4281 /// This is a fully resolved expression that evaluates to a type
4283 public class MethodGroupExpr : Expression, IMemberExpr {
4284 public MethodBase [] Methods;
4285 Expression instance_expression = null;
4286 bool is_explicit_impl = false;
4288 public MethodGroupExpr (MemberInfo [] mi, Location l)
4290 Methods = new MethodBase [mi.Length];
4291 mi.CopyTo (Methods, 0);
4292 eclass = ExprClass.MethodGroup;
4293 type = TypeManager.object_type;
4297 public MethodGroupExpr (ArrayList list, Location l)
4299 Methods = new MethodBase [list.Count];
4302 list.CopyTo (Methods, 0);
4304 foreach (MemberInfo m in list){
4305 if (!(m is MethodBase)){
4306 Console.WriteLine ("Name " + m.Name);
4307 Console.WriteLine ("Found a: " + m.GetType ().FullName);
4313 eclass = ExprClass.MethodGroup;
4314 type = TypeManager.object_type;
4317 public Type DeclaringType {
4319 return Methods [0].DeclaringType;
4324 // 'A method group may have associated an instance expression'
4326 public Expression InstanceExpression {
4328 return instance_expression;
4332 instance_expression = value;
4336 public bool IsExplicitImpl {
4338 return is_explicit_impl;
4342 is_explicit_impl = value;
4346 public string Name {
4348 return Methods [0].Name;
4352 public bool IsInstance {
4354 foreach (MethodBase mb in Methods)
4362 public bool IsStatic {
4364 foreach (MethodBase mb in Methods)
4372 override public Expression DoResolve (EmitContext ec)
4374 if (instance_expression != null) {
4375 instance_expression = instance_expression.DoResolve (ec);
4376 if (instance_expression == null)
4383 public void ReportUsageError ()
4385 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
4386 Methods [0].Name + "()' is referenced without parentheses");
4389 override public void Emit (EmitContext ec)
4391 ReportUsageError ();
4394 bool RemoveMethods (bool keep_static)
4396 ArrayList smethods = new ArrayList ();
4398 foreach (MethodBase mb in Methods){
4399 if (mb.IsStatic == keep_static)
4403 if (smethods.Count == 0)
4406 Methods = new MethodBase [smethods.Count];
4407 smethods.CopyTo (Methods, 0);
4413 /// Removes any instance methods from the MethodGroup, returns
4414 /// false if the resulting set is empty.
4416 public bool RemoveInstanceMethods ()
4418 return RemoveMethods (true);
4422 /// Removes any static methods from the MethodGroup, returns
4423 /// false if the resulting set is empty.
4425 public bool RemoveStaticMethods ()
4427 return RemoveMethods (false);
4432 /// Fully resolved expression that evaluates to a Field
4434 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4435 public readonly FieldInfo FieldInfo;
4436 Expression instance_expr;
4438 public FieldExpr (FieldInfo fi, Location l)
4441 eclass = ExprClass.Variable;
4442 type = fi.FieldType;
4446 public string Name {
4448 return FieldInfo.Name;
4452 public bool IsInstance {
4454 return !FieldInfo.IsStatic;
4458 public bool IsStatic {
4460 return FieldInfo.IsStatic;
4464 public Type DeclaringType {
4466 return FieldInfo.DeclaringType;
4470 public Expression InstanceExpression {
4472 return instance_expr;
4476 instance_expr = value;
4480 override public Expression DoResolve (EmitContext ec)
4482 if (!FieldInfo.IsStatic){
4483 if (instance_expr == null){
4484 throw new Exception ("non-static FieldExpr without instance var\n" +
4485 "You have to assign the Instance variable\n" +
4486 "Of the FieldExpr to set this\n");
4489 // Resolve the field's instance expression while flow analysis is turned
4490 // off: when accessing a field "a.b", we must check whether the field
4491 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4492 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4493 ResolveFlags.DisableFlowAnalysis);
4494 if (instance_expr == null)
4498 // If the instance expression is a local variable or parameter.
4499 IVariable var = instance_expr as IVariable;
4500 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4506 void Report_AssignToReadonly (bool is_instance)
4511 msg = "Readonly field can not be assigned outside " +
4512 "of constructor or variable initializer";
4514 msg = "A static readonly field can only be assigned in " +
4515 "a static constructor";
4517 Report.Error (is_instance ? 191 : 198, loc, msg);
4520 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4522 IVariable var = instance_expr as IVariable;
4524 var.SetFieldAssigned (ec, FieldInfo.Name);
4526 Expression e = DoResolve (ec);
4531 if (!FieldInfo.IsInitOnly)
4535 // InitOnly fields can only be assigned in constructors
4538 if (ec.IsConstructor)
4541 Report_AssignToReadonly (true);
4546 override public void Emit (EmitContext ec)
4548 ILGenerator ig = ec.ig;
4549 bool is_volatile = false;
4551 if (FieldInfo is FieldBuilder){
4552 FieldBase f = TypeManager.GetField (FieldInfo);
4554 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4557 f.status |= Field.Status.USED;
4560 if (FieldInfo.IsStatic){
4562 ig.Emit (OpCodes.Volatile);
4564 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4566 if (instance_expr.Type.IsValueType){
4568 LocalTemporary tempo = null;
4570 if (!(instance_expr is IMemoryLocation)){
4571 tempo = new LocalTemporary (
4572 ec, instance_expr.Type);
4574 InstanceExpression.Emit (ec);
4578 ml = (IMemoryLocation) instance_expr;
4580 ml.AddressOf (ec, AddressOp.Load);
4582 instance_expr.Emit (ec);
4585 ig.Emit (OpCodes.Volatile);
4587 ig.Emit (OpCodes.Ldfld, FieldInfo);
4591 public void EmitAssign (EmitContext ec, Expression source)
4593 FieldAttributes fa = FieldInfo.Attributes;
4594 bool is_static = (fa & FieldAttributes.Static) != 0;
4595 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4596 ILGenerator ig = ec.ig;
4598 if (is_readonly && !ec.IsConstructor){
4599 Report_AssignToReadonly (!is_static);
4604 Expression instance = instance_expr;
4606 if (instance.Type.IsValueType){
4607 if (instance is IMemoryLocation){
4608 IMemoryLocation ml = (IMemoryLocation) instance;
4610 ml.AddressOf (ec, AddressOp.Store);
4612 throw new Exception ("The " + instance + " of type " +
4614 " represents a ValueType and does " +
4615 "not implement IMemoryLocation");
4621 if (FieldInfo is FieldBuilder){
4622 FieldBase f = TypeManager.GetField (FieldInfo);
4624 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4625 ig.Emit (OpCodes.Volatile);
4629 ig.Emit (OpCodes.Stsfld, FieldInfo);
4631 ig.Emit (OpCodes.Stfld, FieldInfo);
4633 if (FieldInfo is FieldBuilder){
4634 FieldBase f = TypeManager.GetField (FieldInfo);
4636 f.status |= Field.Status.ASSIGNED;
4640 public void AddressOf (EmitContext ec, AddressOp mode)
4642 ILGenerator ig = ec.ig;
4644 if (FieldInfo is FieldBuilder){
4645 FieldBase f = TypeManager.GetField (FieldInfo);
4646 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4647 ig.Emit (OpCodes.Volatile);
4650 if (FieldInfo is FieldBuilder){
4651 FieldBase f = TypeManager.GetField (FieldInfo);
4653 if ((mode & AddressOp.Store) != 0)
4654 f.status |= Field.Status.ASSIGNED;
4655 if ((mode & AddressOp.Load) != 0)
4656 f.status |= Field.Status.USED;
4660 // Handle initonly fields specially: make a copy and then
4661 // get the address of the copy.
4663 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4667 local = ig.DeclareLocal (type);
4668 ig.Emit (OpCodes.Stloc, local);
4669 ig.Emit (OpCodes.Ldloca, local);
4673 if (FieldInfo.IsStatic)
4674 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4676 if (instance_expr is IMemoryLocation)
4677 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4679 instance_expr.Emit (ec);
4680 ig.Emit (OpCodes.Ldflda, FieldInfo);
4686 /// Expression that evaluates to a Property. The Assign class
4687 /// might set the 'Value' expression if we are in an assignment.
4689 /// This is not an LValue because we need to re-write the expression, we
4690 /// can not take data from the stack and store it.
4692 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4693 public readonly PropertyInfo PropertyInfo;
4695 MethodInfo getter, setter;
4697 public ArrayList PropertyArgs;
4699 Expression instance_expr;
4701 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4704 eclass = ExprClass.PropertyAccess;
4705 PropertyArgs = new ArrayList();
4709 type = TypeManager.TypeToCoreType (pi.PropertyType);
4711 ResolveAccessors (ec);
4714 public string Name {
4716 return PropertyInfo.Name;
4720 public bool IsInstance {
4726 public bool IsStatic {
4732 public Type DeclaringType {
4734 return PropertyInfo.DeclaringType;
4739 // The instance expression associated with this expression
4741 public Expression InstanceExpression {
4743 instance_expr = value;
4747 return instance_expr;
4751 public bool VerifyAssignable ()
4753 if (!PropertyInfo.CanWrite){
4754 Report.Error (200, loc,
4755 "The property '" + PropertyInfo.Name +
4756 "' can not be assigned to, as it has not set accessor");
4763 void ResolveAccessors (EmitContext ec)
4765 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4766 MemberInfo [] group;
4768 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4769 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4772 // The first method is the closest to us
4774 if (group != null && group.Length > 0){
4775 getter = (MethodInfo) group [0];
4777 if (getter.IsStatic)
4782 // The first method is the closest to us
4784 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4785 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4786 if (group != null && group.Length > 0){
4787 setter = (MethodInfo) group [0];
4788 if (setter.IsStatic)
4793 override public Expression DoResolve (EmitContext ec)
4795 if (getter == null){
4796 Report.Error (30524, loc,
4797 "The property '" + PropertyInfo.Name +
4798 "' can not be used in " +
4799 "this context because it lacks a get accessor");
4803 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4804 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4808 if (instance_expr != null) {
4809 instance_expr = instance_expr.DoResolve (ec);
4810 if (instance_expr == null)
4817 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4819 if (setter == null){
4820 Report.Error (30526, loc,
4821 "The property '" + PropertyInfo.Name +
4822 "' can not be used in " +
4823 "this context because it lacks a set accessor");
4827 if (instance_expr != null) {
4828 instance_expr = instance_expr.DoResolve (ec);
4829 if (instance_expr == null)
4836 override public void Emit (EmitContext ec)
4839 // Special case: length of single dimension array property is turned into ldlen
4841 if ((getter == TypeManager.system_int_array_get_length) ||
4842 (getter == TypeManager.int_array_get_length)){
4843 Type iet = instance_expr.Type;
4846 // System.Array.Length can be called, but the Type does not
4847 // support invoking GetArrayRank, so test for that case first
4849 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4850 instance_expr.Emit (ec);
4851 ec.ig.Emit (OpCodes.Ldlen);
4855 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4859 // Implements the IAssignMethod interface for assignments
4861 public void EmitAssign (EmitContext ec, Expression source)
4863 Argument arg = new Argument (source, Argument.AType.Expression);
4864 ArrayList args = new ArrayList ();
4867 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4870 override public void EmitStatement (EmitContext ec)
4873 ec.ig.Emit (OpCodes.Pop);
4878 /// Fully resolved expression that evaluates to an Event
4880 public class EventExpr : Expression, IMemberExpr {
4881 public readonly EventInfo EventInfo;
4882 public Expression instance_expr;
4885 MethodInfo add_accessor, remove_accessor;
4887 public EventExpr (EventInfo ei, Location loc)
4891 eclass = ExprClass.EventAccess;
4893 add_accessor = TypeManager.GetAddMethod (ei);
4894 remove_accessor = TypeManager.GetRemoveMethod (ei);
4896 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4899 if (EventInfo is MyEventBuilder)
4900 type = ((MyEventBuilder) EventInfo).EventType;
4902 type = EventInfo.EventHandlerType;
4905 public string Name {
4907 return EventInfo.Name;
4911 public bool IsInstance {
4917 public bool IsStatic {
4923 public Type DeclaringType {
4925 return EventInfo.DeclaringType;
4929 public Expression InstanceExpression {
4931 return instance_expr;
4935 instance_expr = value;
4939 Expression field_expr = null;
4941 public override Expression DoResolve (EmitContext ec)
4943 if (instance_expr != null) {
4944 instance_expr = instance_expr.DoResolve (ec);
4945 if (instance_expr == null)
4949 if (this.DeclaringType == ec.ContainerType) {
4950 MemberInfo mi = GetFieldFromEvent (this);
4953 field_expr = ExprClassFromMemberInfo (ec, mi, loc);
4954 ((FieldExpr) field_expr).InstanceExpression = instance_expr;
4955 field_expr = field_expr.DoResolve (ec);
4956 if (field_expr == null)
4963 public override void Emit (EmitContext ec)
4965 if (field_expr != null)
4966 field_expr.Emit (ec);
4969 public void EmitAddOrRemove (EmitContext ec, Expression source)
4971 Expression handler = ((Binary) source).Right;
4973 Argument arg = new Argument (handler, Argument.AType.Expression);
4974 ArrayList args = new ArrayList ();
4978 if (((Binary) source).Oper == Binary.Operator.Addition)
4979 Invocation.EmitCall (
4980 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4982 Invocation.EmitCall (
4983 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);