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 (expr_type == TypeManager.sbyte_type){
813 // From sbyte to short, int, long, float, double.
815 if (real_target_type == TypeManager.int32_type)
816 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
817 if (real_target_type == TypeManager.int64_type)
818 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
819 if (real_target_type == TypeManager.double_type)
820 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
821 if (real_target_type == TypeManager.float_type)
822 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
823 if (real_target_type == TypeManager.short_type)
824 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
825 } else if (expr_type == TypeManager.byte_type){
827 // From byte to short, ushort, int, uint, long, ulong, float, double
829 if ((real_target_type == TypeManager.short_type) ||
830 (real_target_type == TypeManager.ushort_type) ||
831 (real_target_type == TypeManager.int32_type) ||
832 (real_target_type == TypeManager.uint32_type))
833 return new EmptyCast (expr, target_type);
835 if (real_target_type == TypeManager.uint64_type)
836 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
837 if (real_target_type == TypeManager.int64_type)
838 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
839 if (real_target_type == TypeManager.float_type)
840 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
841 if (real_target_type == TypeManager.double_type)
842 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
843 } else if (expr_type == TypeManager.short_type){
845 // From short to int, long, float, double
847 if (real_target_type == TypeManager.int32_type)
848 return new EmptyCast (expr, target_type);
849 if (real_target_type == TypeManager.int64_type)
850 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
851 if (real_target_type == TypeManager.double_type)
852 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
853 if (real_target_type == TypeManager.float_type)
854 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
855 } else if (expr_type == TypeManager.ushort_type){
857 // From ushort to int, uint, long, ulong, float, double
859 if (real_target_type == TypeManager.uint32_type)
860 return new EmptyCast (expr, target_type);
862 if (real_target_type == TypeManager.uint64_type)
863 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
864 if (real_target_type == TypeManager.int32_type)
865 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
866 if (real_target_type == TypeManager.int64_type)
867 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
868 if (real_target_type == TypeManager.double_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
870 if (real_target_type == TypeManager.float_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
872 } else if (expr_type == TypeManager.int32_type){
874 // From int to long, float, double
876 if (real_target_type == TypeManager.int64_type)
877 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
878 if (real_target_type == TypeManager.double_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
880 if (real_target_type == TypeManager.float_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
882 } else if (expr_type == TypeManager.uint32_type){
884 // From uint to long, ulong, float, double
886 if (real_target_type == TypeManager.int64_type)
887 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
888 if (real_target_type == TypeManager.uint64_type)
889 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
890 if (real_target_type == TypeManager.double_type)
891 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
893 if (real_target_type == TypeManager.float_type)
894 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
896 } else if (expr_type == TypeManager.int64_type){
898 // From long/ulong to float, double
900 if (real_target_type == TypeManager.double_type)
901 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
902 if (real_target_type == TypeManager.float_type)
903 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
904 } else if (expr_type == TypeManager.uint64_type){
906 // From ulong to float, double
908 if (real_target_type == TypeManager.double_type)
909 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
911 if (real_target_type == TypeManager.float_type)
912 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
914 } else if (expr_type == TypeManager.char_type){
916 // From char to ushort, int, uint, long, ulong, float, double
918 if ((real_target_type == TypeManager.ushort_type) ||
919 (real_target_type == TypeManager.int32_type) ||
920 (real_target_type == TypeManager.uint32_type))
921 return new EmptyCast (expr, target_type);
922 if (real_target_type == TypeManager.uint64_type)
923 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
924 if (real_target_type == TypeManager.int64_type)
925 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
926 if (real_target_type == TypeManager.float_type)
927 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
928 if (real_target_type == TypeManager.double_type)
929 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
930 } else if (expr_type == TypeManager.float_type){
934 if (real_target_type == TypeManager.double_type)
935 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
942 // Tests whether an implicit reference conversion exists between expr_type
945 public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
948 // This is the boxed case.
950 if (target_type == TypeManager.object_type) {
951 if ((expr_type.IsClass) ||
952 (expr_type.IsValueType) ||
953 (expr_type.IsInterface))
956 } else if (expr_type.IsSubclassOf (target_type)) {
959 // Please remember that all code below actually comes
960 // from ImplicitReferenceConversion so make sure code remains in sync
962 // from any class-type S to any interface-type T.
963 if (target_type.IsInterface) {
964 if (TypeManager.ImplementsInterface (expr_type, target_type))
968 // from any interface type S to interface-type T.
969 if (expr_type.IsInterface && target_type.IsInterface)
970 if (TypeManager.ImplementsInterface (expr_type, target_type))
973 // from an array-type S to an array-type of type T
974 if (expr_type.IsArray && target_type.IsArray) {
975 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
977 Type expr_element_type = expr_type.GetElementType ();
979 if (MyEmptyExpr == null)
980 MyEmptyExpr = new EmptyExpression ();
982 MyEmptyExpr.SetType (expr_element_type);
983 Type target_element_type = target_type.GetElementType ();
985 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
986 if (StandardConversionExists (MyEmptyExpr,
987 target_element_type))
992 // from an array-type to System.Array
993 if (expr_type.IsArray && (target_type == TypeManager.array_type))
996 // from any delegate type to System.Delegate
997 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
998 target_type == TypeManager.delegate_type)
999 if (target_type.IsAssignableFrom (expr_type))
1002 // from any array-type or delegate type into System.ICloneable.
1003 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1004 if (target_type == TypeManager.icloneable_type)
1007 // from the null type to any reference-type.
1008 if (expr is NullLiteral && !target_type.IsValueType &&
1009 !TypeManager.IsEnumType (target_type))
1018 /// Same as StandardConversionExists except that it also looks at
1019 /// implicit user defined conversions - needed for overload resolution
1021 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1023 if (StandardConversionExists (expr, target_type) == true)
1027 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1037 /// Determines if a standard implicit conversion exists from
1038 /// expr_type to target_type
1040 public static bool StandardConversionExists (Expression expr, Type target_type)
1042 return WideningConversionExists (expr, expr.type, target_type);
1045 public static bool WideningConversionExists (Type expr_type, Type target_type)
1047 return WideningConversionExists (null, expr_type, target_type);
1050 public static bool WideningConversionExists (Expression expr, Type target_type)
1052 return WideningConversionExists (expr, expr.Type, target_type);
1055 public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
1057 if (expr_type == null || expr_type == TypeManager.void_type)
1060 if (expr_type == target_type)
1063 // Conversions from enum to underlying type are widening.
1064 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1065 expr_type = TypeManager.EnumToUnderlying (expr_type);
1067 if (expr_type == target_type)
1070 // First numeric conversions
1072 if (expr_type == TypeManager.sbyte_type){
1074 // From sbyte to short, int, long, float, double.
1076 if ((target_type == TypeManager.int32_type) ||
1077 (target_type == TypeManager.int64_type) ||
1078 (target_type == TypeManager.double_type) ||
1079 (target_type == TypeManager.float_type) ||
1080 (target_type == TypeManager.short_type) ||
1081 (target_type == TypeManager.decimal_type))
1084 } else if (expr_type == TypeManager.byte_type){
1086 // From byte to short, ushort, int, uint, long, ulong, float, double
1088 if ((target_type == TypeManager.short_type) ||
1089 (target_type == TypeManager.ushort_type) ||
1090 (target_type == TypeManager.int32_type) ||
1091 (target_type == TypeManager.uint32_type) ||
1092 (target_type == TypeManager.uint64_type) ||
1093 (target_type == TypeManager.int64_type) ||
1094 (target_type == TypeManager.float_type) ||
1095 (target_type == TypeManager.double_type) ||
1096 (target_type == TypeManager.decimal_type))
1099 } else if (expr_type == TypeManager.short_type){
1101 // From short to int, long, float, double
1103 if ((target_type == TypeManager.int32_type) ||
1104 (target_type == TypeManager.int64_type) ||
1105 (target_type == TypeManager.double_type) ||
1106 (target_type == TypeManager.float_type) ||
1107 (target_type == TypeManager.decimal_type))
1110 } else if (expr_type == TypeManager.ushort_type){
1112 // From ushort to int, uint, long, ulong, float, double
1114 if ((target_type == TypeManager.uint32_type) ||
1115 (target_type == TypeManager.uint64_type) ||
1116 (target_type == TypeManager.int32_type) ||
1117 (target_type == TypeManager.int64_type) ||
1118 (target_type == TypeManager.double_type) ||
1119 (target_type == TypeManager.float_type) ||
1120 (target_type == TypeManager.decimal_type))
1123 } else if (expr_type == TypeManager.int32_type){
1125 // From int to long, float, double
1127 if ((target_type == TypeManager.int64_type) ||
1128 (target_type == TypeManager.double_type) ||
1129 (target_type == TypeManager.float_type) ||
1130 (target_type == TypeManager.decimal_type))
1133 } else if (expr_type == TypeManager.uint32_type){
1135 // From uint to long, ulong, float, double
1137 if ((target_type == TypeManager.int64_type) ||
1138 (target_type == TypeManager.uint64_type) ||
1139 (target_type == TypeManager.double_type) ||
1140 (target_type == TypeManager.float_type) ||
1141 (target_type == TypeManager.decimal_type))
1144 } else if ((expr_type == TypeManager.uint64_type) ||
1145 (expr_type == TypeManager.int64_type)) {
1147 // From long/ulong to float, double
1149 if ((target_type == TypeManager.double_type) ||
1150 (target_type == TypeManager.float_type) ||
1151 (target_type == TypeManager.decimal_type))
1154 } else if (expr_type == TypeManager.char_type){
1156 // From char to ushort, int, uint, long, ulong, float, double
1158 if ((target_type == TypeManager.ushort_type) ||
1159 (target_type == TypeManager.int32_type) ||
1160 (target_type == TypeManager.uint32_type) ||
1161 (target_type == TypeManager.uint64_type) ||
1162 (target_type == TypeManager.int64_type) ||
1163 (target_type == TypeManager.float_type) ||
1164 (target_type == TypeManager.double_type) ||
1165 (target_type == TypeManager.decimal_type))
1168 } else if (expr_type == TypeManager.float_type){
1172 if (target_type == TypeManager.double_type)
1176 if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
1179 if (expr is IntConstant){
1180 int value = ((IntConstant) expr).Value;
1182 if (target_type == TypeManager.sbyte_type){
1183 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1185 } else if (target_type == TypeManager.byte_type){
1186 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1188 } else if (target_type == TypeManager.short_type){
1189 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1191 } else if (target_type == TypeManager.ushort_type){
1192 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1194 } else if (target_type == TypeManager.uint32_type){
1197 } else if (target_type == TypeManager.uint64_type){
1199 // we can optimize this case: a positive int32
1200 // always fits on a uint64. But we need an opcode
1207 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1211 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1213 // Try the implicit constant expression conversion
1214 // from long to ulong, instead of a nice routine,
1215 // we just inline it
1217 long v = ((LongConstant) expr).Value;
1222 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1223 IntLiteral i = (IntLiteral) expr;
1229 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1236 // Used internally by FindMostEncompassedType, this is used
1237 // to avoid creating lots of objects in the tight loop inside
1238 // FindMostEncompassedType
1240 static EmptyExpression priv_fmet_param;
1243 /// Finds "most encompassed type" according to the spec (13.4.2)
1244 /// amongst the methods in the MethodGroupExpr
1246 static Type FindMostEncompassedType (ArrayList types)
1250 if (priv_fmet_param == null)
1251 priv_fmet_param = new EmptyExpression ();
1253 foreach (Type t in types){
1254 priv_fmet_param.SetType (t);
1261 if (StandardConversionExists (priv_fmet_param, best))
1269 // Used internally by FindMostEncompassingType, this is used
1270 // to avoid creating lots of objects in the tight loop inside
1271 // FindMostEncompassingType
1273 static EmptyExpression priv_fmee_ret;
1276 /// Finds "most encompassing type" according to the spec (13.4.2)
1277 /// amongst the types in the given set
1279 static Type FindMostEncompassingType (ArrayList types)
1283 if (priv_fmee_ret == null)
1284 priv_fmee_ret = new EmptyExpression ();
1286 foreach (Type t in types){
1287 priv_fmee_ret.SetType (best);
1294 if (StandardConversionExists (priv_fmee_ret, t))
1302 // Used to avoid creating too many objects
1304 static EmptyExpression priv_fms_expr;
1307 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1308 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1309 /// for explicit and implicit conversion operators.
1311 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1312 bool apply_explicit_conv_rules,
1315 ArrayList src_types_set = new ArrayList ();
1317 if (priv_fms_expr == null)
1318 priv_fms_expr = new EmptyExpression ();
1321 // If any operator converts from S then Sx = S
1323 Type source_type = source.Type;
1324 foreach (MethodBase mb in me.Methods){
1325 ParameterData pd = Invocation.GetParameterData (mb);
1326 Type param_type = pd.ParameterType (0);
1328 if (param_type == source_type)
1331 if (apply_explicit_conv_rules) {
1334 // Find the set of applicable user-defined conversion operators, U. This set
1336 // user-defined implicit or explicit conversion operators declared by
1337 // the classes or structs in D that convert from a type encompassing
1338 // or encompassed by S to a type encompassing or encompassed by T
1340 priv_fms_expr.SetType (param_type);
1341 if (StandardConversionExists (priv_fms_expr, source_type))
1342 src_types_set.Add (param_type);
1344 if (StandardConversionExists (source, param_type))
1345 src_types_set.Add (param_type);
1349 // Only if S is encompassed by param_type
1351 if (StandardConversionExists (source, param_type))
1352 src_types_set.Add (param_type);
1357 // Explicit Conv rules
1359 if (apply_explicit_conv_rules) {
1360 ArrayList candidate_set = new ArrayList ();
1362 foreach (Type param_type in src_types_set){
1363 if (StandardConversionExists (source, param_type))
1364 candidate_set.Add (param_type);
1367 if (candidate_set.Count != 0)
1368 return FindMostEncompassedType (candidate_set);
1374 if (apply_explicit_conv_rules)
1375 return FindMostEncompassingType (src_types_set);
1377 return FindMostEncompassedType (src_types_set);
1381 // Useful in avoiding proliferation of objects
1383 static EmptyExpression priv_fmt_expr;
1386 /// Finds the most specific target Tx according to section 13.4.4
1388 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1389 bool apply_explicit_conv_rules,
1392 ArrayList tgt_types_set = new ArrayList ();
1394 if (priv_fmt_expr == null)
1395 priv_fmt_expr = new EmptyExpression ();
1398 // If any operator converts to T then Tx = T
1400 foreach (MethodInfo mi in me.Methods){
1401 Type ret_type = mi.ReturnType;
1403 if (ret_type == target)
1406 if (apply_explicit_conv_rules) {
1409 // Find the set of applicable user-defined conversion operators, U.
1411 // This set consists of the
1412 // user-defined implicit or explicit conversion operators declared by
1413 // the classes or structs in D that convert from a type encompassing
1414 // or encompassed by S to a type encompassing or encompassed by T
1416 priv_fms_expr.SetType (ret_type);
1417 if (StandardConversionExists (priv_fms_expr, target))
1418 tgt_types_set.Add (ret_type);
1420 priv_fms_expr.SetType (target);
1421 if (StandardConversionExists (priv_fms_expr, ret_type))
1422 tgt_types_set.Add (ret_type);
1426 // Only if T is encompassed by param_type
1428 priv_fms_expr.SetType (ret_type);
1429 if (StandardConversionExists (priv_fms_expr, target))
1430 tgt_types_set.Add (ret_type);
1435 // Explicit conv rules
1437 if (apply_explicit_conv_rules) {
1438 ArrayList candidate_set = new ArrayList ();
1440 foreach (Type ret_type in tgt_types_set){
1441 priv_fmt_expr.SetType (ret_type);
1443 if (StandardConversionExists (priv_fmt_expr, target))
1444 candidate_set.Add (ret_type);
1447 if (candidate_set.Count != 0)
1448 return FindMostEncompassingType (candidate_set);
1452 // Okay, final case !
1454 if (apply_explicit_conv_rules)
1455 return FindMostEncompassedType (tgt_types_set);
1457 return FindMostEncompassingType (tgt_types_set);
1461 /// User-defined Implicit conversions
1463 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1464 Type target, Location loc)
1466 return UserDefinedConversion (ec, source, target, loc, false);
1470 /// User-defined Explicit conversions
1472 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1473 Type target, Location loc)
1475 return UserDefinedConversion (ec, source, target, loc, true);
1479 /// Computes the MethodGroup for the user-defined conversion
1480 /// operators from source_type to target_type. 'look_for_explicit'
1481 /// controls whether we should also include the list of explicit
1484 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1485 Type source_type, Type target_type,
1486 Location loc, bool look_for_explicit)
1488 Expression mg1 = null, mg2 = null;
1489 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1493 // FIXME : How does the False operator come into the picture ?
1494 // This doesn't look complete and very correct !
1496 if (target_type == TypeManager.bool_type && !look_for_explicit)
1497 op_name = "op_True";
1499 op_name = "op_Implicit";
1501 MethodGroupExpr union3;
1503 mg1 = MethodLookup (ec, source_type, op_name, loc);
1504 if (source_type.BaseType != null)
1505 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1508 union3 = (MethodGroupExpr) mg2;
1509 else if (mg2 == null)
1510 union3 = (MethodGroupExpr) mg1;
1512 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1514 mg1 = MethodLookup (ec, target_type, op_name, loc);
1517 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1519 union3 = (MethodGroupExpr) mg1;
1522 if (target_type.BaseType != null)
1523 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1527 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1529 union3 = (MethodGroupExpr) mg1;
1532 MethodGroupExpr union4 = null;
1534 if (look_for_explicit) {
1535 op_name = "op_Explicit";
1537 mg5 = MemberLookup (ec, source_type, op_name, loc);
1538 if (source_type.BaseType != null)
1539 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1541 mg7 = MemberLookup (ec, target_type, op_name, loc);
1542 if (target_type.BaseType != null)
1543 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1545 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1546 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1548 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1551 return Invocation.MakeUnionSet (union3, union4, loc);
1555 /// User-defined conversions
1557 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1558 Type target, Location loc,
1559 bool look_for_explicit)
1561 MethodGroupExpr union;
1562 Type source_type = source.Type;
1563 MethodBase method = null;
1565 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1569 Type most_specific_source, most_specific_target;
1572 foreach (MethodBase m in union.Methods){
1573 Console.WriteLine ("Name: " + m.Name);
1574 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1578 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1579 if (most_specific_source == null)
1582 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1583 if (most_specific_target == null)
1588 foreach (MethodBase mb in union.Methods){
1589 ParameterData pd = Invocation.GetParameterData (mb);
1590 MethodInfo mi = (MethodInfo) mb;
1592 if (pd.ParameterType (0) == most_specific_source &&
1593 mi.ReturnType == most_specific_target) {
1599 if (method == null || count > 1)
1604 // This will do the conversion to the best match that we
1605 // found. Now we need to perform an implict standard conversion
1606 // if the best match was not the type that we were requested
1609 if (look_for_explicit)
1610 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1612 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1618 e = new UserCast ((MethodInfo) method, source, loc);
1619 if (e.Type != target){
1620 if (!look_for_explicit)
1621 e = ConvertImplicitStandard (ec, e, target, loc);
1623 e = ConvertExplicitStandard (ec, e, target, loc);
1629 /// Converts implicitly the resolved expression 'expr' into the
1630 /// 'target_type'. It returns a new expression that can be used
1631 /// in a context that expects a 'target_type'.
1633 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1634 Type target_type, Location loc)
1636 Type expr_type = expr.Type;
1639 if (expr_type == target_type)
1642 if (target_type == null)
1643 throw new Exception ("Target type is null");
1645 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1649 e = ImplicitUserConversion (ec, expr, target_type, loc);
1653 e = NarrowingConversion (ec, expr, target_type, loc);
1661 /// Converts the resolved expression 'expr' into the
1662 /// 'target_type' using the Microsoft.VisualBasic runtime.
1663 /// It returns a new expression that can be used
1664 /// in a context that expects a 'target_type'.
1666 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1672 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1673 args = new ArrayList();
1674 arg = new Argument (expr, Argument.AType.Expression);
1676 e = (Expression) new Invocation (etmp, args, loc);
1681 static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
1683 Type expr_type = expr.Type;
1685 if (target_type == TypeManager.sbyte_type){
1687 // To sbyte from short, int, long, float, double.
1689 if ((expr_type == TypeManager.int32_type) ||
1690 (expr_type == TypeManager.int64_type) ||
1691 (expr_type == TypeManager.double_type) ||
1692 (expr_type == TypeManager.float_type) ||
1693 (expr_type == TypeManager.short_type) ||
1694 (expr_type == TypeManager.decimal_type))
1697 } else if (target_type == TypeManager.byte_type){
1699 // To byte from short, ushort, int, uint, long, ulong, float, double
1701 if ((expr_type == TypeManager.short_type) ||
1702 (expr_type == TypeManager.ushort_type) ||
1703 (expr_type == TypeManager.int32_type) ||
1704 (expr_type == TypeManager.uint32_type) ||
1705 (expr_type == TypeManager.uint64_type) ||
1706 (expr_type == TypeManager.int64_type) ||
1707 (expr_type == TypeManager.float_type) ||
1708 (expr_type == TypeManager.double_type) ||
1709 (expr_type == TypeManager.decimal_type))
1712 } else if (target_type == TypeManager.short_type){
1714 // To short from int, long, float, double
1716 if ((expr_type == TypeManager.int32_type) ||
1717 (expr_type == TypeManager.int64_type) ||
1718 (expr_type == TypeManager.double_type) ||
1719 (expr_type == TypeManager.float_type) ||
1720 (expr_type == TypeManager.decimal_type))
1723 } else if (target_type == TypeManager.ushort_type){
1725 // To ushort from int, uint, long, ulong, float, double
1727 if ((expr_type == TypeManager.uint32_type) ||
1728 (expr_type == TypeManager.uint64_type) ||
1729 (expr_type == TypeManager.int32_type) ||
1730 (expr_type == TypeManager.int64_type) ||
1731 (expr_type == TypeManager.double_type) ||
1732 (expr_type == TypeManager.float_type) ||
1733 (expr_type == TypeManager.decimal_type))
1736 } else if (target_type == TypeManager.int32_type){
1738 // To int from long, float, double
1740 if ((expr_type == TypeManager.int64_type) ||
1741 (expr_type == TypeManager.double_type) ||
1742 (expr_type == TypeManager.float_type) ||
1743 (expr_type == TypeManager.decimal_type))
1746 } else if (target_type == TypeManager.uint32_type){
1748 // To uint from long, ulong, float, double
1750 if ((expr_type == TypeManager.int64_type) ||
1751 (expr_type == TypeManager.uint64_type) ||
1752 (expr_type == TypeManager.double_type) ||
1753 (expr_type == TypeManager.float_type) ||
1754 (expr_type == TypeManager.decimal_type))
1757 } else if ((target_type == TypeManager.uint64_type) ||
1758 (target_type == TypeManager.int64_type)) {
1760 // To long/ulong from float, double
1762 if ((expr_type == TypeManager.double_type) ||
1763 (expr_type == TypeManager.float_type) ||
1764 (expr_type == TypeManager.decimal_type))
1767 } else if (target_type == TypeManager.char_type){
1769 // To char from ushort, int, uint, long, ulong, float, double
1771 if ((expr_type == TypeManager.ushort_type) ||
1772 (expr_type == TypeManager.int32_type) ||
1773 (expr_type == TypeManager.uint32_type) ||
1774 (expr_type == TypeManager.uint64_type) ||
1775 (expr_type == TypeManager.int64_type) ||
1776 (expr_type == TypeManager.float_type) ||
1777 (expr_type == TypeManager.double_type) ||
1778 (expr_type == TypeManager.decimal_type))
1781 } else if (target_type == TypeManager.float_type){
1783 // To float from double
1785 if (expr_type == TypeManager.double_type)
1789 return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;
1792 static public Expression NarrowingConversion (EmitContext ec, Expression expr,
1793 Type target_type, Location loc)
1795 Type expr_type = expr.Type;
1797 if (target_type == TypeManager.sbyte_type){
1799 // To sbyte from short, 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.short_type) ||
1806 (expr_type == TypeManager.decimal_type))
1807 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1809 } else if (target_type == TypeManager.byte_type){
1811 // To byte from short, ushort, int, uint, long, ulong, float, double
1813 if ((expr_type == TypeManager.short_type) ||
1814 (expr_type == TypeManager.ushort_type) ||
1815 (expr_type == TypeManager.int32_type) ||
1816 (expr_type == TypeManager.uint32_type) ||
1817 (expr_type == TypeManager.uint64_type) ||
1818 (expr_type == TypeManager.int64_type) ||
1819 (expr_type == TypeManager.float_type) ||
1820 (expr_type == TypeManager.double_type) ||
1821 (expr_type == TypeManager.decimal_type))
1822 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1824 } else if (target_type == TypeManager.short_type){
1826 // To short from int, long, float, double
1828 if ((expr_type == TypeManager.int32_type) ||
1829 (expr_type == TypeManager.int64_type) ||
1830 (expr_type == TypeManager.double_type) ||
1831 (expr_type == TypeManager.float_type) ||
1832 (expr_type == TypeManager.decimal_type))
1833 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1835 } else if (target_type == TypeManager.ushort_type){
1837 // To ushort from int, uint, long, ulong, float, double
1839 if ((expr_type == TypeManager.uint32_type) ||
1840 (expr_type == TypeManager.uint64_type) ||
1841 (expr_type == TypeManager.int32_type) ||
1842 (expr_type == TypeManager.int64_type) ||
1843 (expr_type == TypeManager.double_type) ||
1844 (expr_type == TypeManager.float_type) ||
1845 (expr_type == TypeManager.decimal_type))
1846 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1848 } else if (target_type == TypeManager.int32_type){
1850 // To int from long, float, double
1852 if ((expr_type == TypeManager.int64_type) ||
1853 (expr_type == TypeManager.double_type) ||
1854 (expr_type == TypeManager.float_type) ||
1855 (expr_type == TypeManager.decimal_type))
1856 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1858 } else if (target_type == TypeManager.uint32_type){
1860 // To uint from long, ulong, float, double
1862 if ((expr_type == TypeManager.int64_type) ||
1863 (expr_type == TypeManager.uint64_type) ||
1864 (expr_type == TypeManager.double_type) ||
1865 (expr_type == TypeManager.float_type) ||
1866 (expr_type == TypeManager.decimal_type))
1867 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1869 } else if ((target_type == TypeManager.uint64_type) ||
1870 (target_type == TypeManager.int64_type)) {
1872 // To long/ulong from float, double
1874 if ((expr_type == TypeManager.double_type) ||
1875 (expr_type == TypeManager.float_type) ||
1876 (expr_type == TypeManager.decimal_type))
1877 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1879 } else if (target_type == TypeManager.char_type){
1881 // To char from ushort, int, uint, long, ulong, float, double
1883 if ((expr_type == TypeManager.ushort_type) ||
1884 (expr_type == TypeManager.int32_type) ||
1885 (expr_type == TypeManager.uint32_type) ||
1886 (expr_type == TypeManager.uint64_type) ||
1887 (expr_type == TypeManager.int64_type) ||
1888 (expr_type == TypeManager.float_type) ||
1889 (expr_type == TypeManager.double_type) ||
1890 (expr_type == TypeManager.decimal_type))
1891 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1893 } else if (target_type == TypeManager.float_type){
1895 // To float from double
1897 if (expr_type == TypeManager.double_type)
1898 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
1901 TypeCode dest_type = Type.GetTypeCode (target_type);
1902 TypeCode src_type = Type.GetTypeCode (expr_type);
1903 Expression e = null;
1905 // VB.NET Objects can be converted to anything by default
1906 // unless, that is, an exception at runtime blows it all
1907 if (src_type == TypeCode.Object) {
1908 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
1909 Cast ce = new Cast (cast_type, expr, loc);
1910 ce.IsRuntimeCast = true;
1911 return ce.Resolve (ec);
1914 switch (dest_type) {
1915 case TypeCode.String:
1917 case TypeCode.SByte:
1919 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
1921 case TypeCode.UInt16:
1922 case TypeCode.Int16:
1923 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
1925 case TypeCode.UInt32:
1926 case TypeCode.Int32:
1927 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
1929 case TypeCode.UInt64:
1930 case TypeCode.Int64:
1931 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
1934 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
1936 case TypeCode.Single:
1937 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
1939 case TypeCode.Double:
1940 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
1942 case TypeCode.Boolean:
1943 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
1945 case TypeCode.DateTime:
1946 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
1948 case TypeCode.Decimal:
1949 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
1951 case TypeCode.Object:
1952 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
1957 case TypeCode.Int32:
1958 case TypeCode.UInt32:
1960 case TypeCode.String:
1961 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
1963 case TypeCode.Object:
1964 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
1969 case TypeCode.Int16:
1970 case TypeCode.UInt16:
1972 case TypeCode.String:
1973 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
1975 case TypeCode.Object:
1976 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
1981 // Ok, this *is* broken
1982 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
1984 case TypeCode.DateTime:
1986 case TypeCode.String:
1987 e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
1989 case TypeCode.Object:
1990 e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
1996 // We must examine separately some types that
1997 // don't have a TypeCode but are supported
1999 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
2000 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
2007 /// Attempts to apply the 'Standard Implicit
2008 /// Conversion' rules to the expression 'expr' into
2009 /// the 'target_type'. It returns a new expression
2010 /// that can be used in a context that expects a
2013 /// This is different from 'ConvertImplicit' in that the
2014 /// user defined implicit conversions are excluded.
2016 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
2017 Type target_type, Location loc)
2019 Type expr_type = expr.Type;
2022 if (expr_type == target_type)
2025 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2029 e = ImplicitReferenceConversion (expr, target_type);
2033 if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
2034 expr_type = TypeManager.EnumToUnderlying (expr.Type);
2035 expr = new EmptyCast (expr, expr_type);
2036 if (expr_type == target_type)
2038 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2044 if (expr_type.IsPointer){
2045 if (target_type == TypeManager.void_ptr_type)
2046 return new EmptyCast (expr, target_type);
2049 // yep, comparing pointer types cant be done with
2050 // t1 == t2, we have to compare their element types.
2052 if (target_type.IsPointer){
2053 if (target_type.GetElementType()==expr_type.GetElementType())
2058 if (target_type.IsPointer){
2059 if (expr is NullLiteral)
2060 return new EmptyCast (expr, target_type);
2068 /// Attemps to perform an implict constant conversion of the IntConstant
2069 /// into a different data type using casts (See Implicit Constant
2070 /// Expression Conversions)
2072 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
2074 int value = ic.Value;
2077 // FIXME: This could return constants instead of EmptyCasts
2079 if (target_type == TypeManager.sbyte_type){
2080 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2081 return new SByteConstant ((sbyte) value);
2082 } else if (target_type == TypeManager.byte_type){
2083 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2084 return new ByteConstant ((byte) value);
2085 } else if (target_type == TypeManager.short_type){
2086 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2087 return new ShortConstant ((short) value);
2088 } else if (target_type == TypeManager.ushort_type){
2089 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2090 return new UShortConstant ((ushort) value);
2091 } else if (target_type == TypeManager.uint32_type){
2093 return new UIntConstant ((uint) value);
2094 } else if (target_type == TypeManager.uint64_type){
2096 // we can optimize this case: a positive int32
2097 // always fits on a uint64. But we need an opcode
2101 return new ULongConstant ((ulong) value);
2104 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
2105 Type underlying = TypeManager.EnumToUnderlying (target_type);
2106 Constant e = (Constant) ic;
2109 // Possibly, we need to create a different 0 literal before passing
2112 if (underlying == TypeManager.int64_type)
2113 e = new LongLiteral (0);
2114 else if (underlying == TypeManager.uint64_type)
2115 e = new ULongLiteral (0);
2117 return new EnumConstant (e, target_type);
2122 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
2124 string msg = "Cannot convert implicitly from '"+
2125 TypeManager.MonoBASIC_Name (source) + "' to '" +
2126 TypeManager.MonoBASIC_Name (target) + "'";
2128 Report.Error (29, loc, msg);
2132 /// Attemptes to implicityly convert 'target' into 'type', using
2133 /// ConvertImplicit. If there is no implicit conversion, then
2134 /// an error is signaled
2136 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
2137 Type target_type, Location loc)
2141 e = ConvertImplicit (ec, source, target_type, loc);
2145 if (source is DoubleLiteral && target_type == TypeManager.float_type){
2146 Report.Error (664, loc,
2147 "Double literal cannot be implicitly converted to " +
2148 "float type, use F suffix to create a float literal");
2151 Error_CannotConvertImplicit (loc, source.Type, target_type);
2157 /// Performs the explicit numeric conversions
2159 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
2161 Type expr_type = expr.Type;
2164 // If we have an enumeration, extract the underlying type,
2165 // use this during the comparison, but wrap around the original
2168 Type real_target_type = target_type;
2170 if (TypeManager.IsEnumType (real_target_type))
2171 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
2173 if (StandardConversionExists (expr, real_target_type)){
2174 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
2176 if (real_target_type != target_type)
2177 return new EmptyCast (ce, target_type);
2181 if (expr_type == TypeManager.sbyte_type){
2183 // From sbyte to byte, ushort, uint, ulong, char
2185 if (real_target_type == TypeManager.byte_type)
2186 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
2187 if (real_target_type == TypeManager.ushort_type)
2188 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
2189 if (real_target_type == TypeManager.uint32_type)
2190 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
2191 if (real_target_type == TypeManager.uint64_type)
2192 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
2193 if (real_target_type == TypeManager.char_type)
2194 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
2195 } else if (expr_type == TypeManager.byte_type){
2197 // From byte to sbyte and char
2199 if (real_target_type == TypeManager.sbyte_type)
2200 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
2201 if (real_target_type == TypeManager.char_type)
2202 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
2203 } else if (expr_type == TypeManager.short_type){
2205 // From short to sbyte, byte, ushort, uint, ulong, char
2207 if (real_target_type == TypeManager.sbyte_type)
2208 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
2209 if (real_target_type == TypeManager.byte_type)
2210 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
2211 if (real_target_type == TypeManager.ushort_type)
2212 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
2213 if (real_target_type == TypeManager.uint32_type)
2214 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
2215 if (real_target_type == TypeManager.uint64_type)
2216 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
2217 if (real_target_type == TypeManager.char_type)
2218 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
2219 } else if (expr_type == TypeManager.ushort_type){
2221 // From ushort to sbyte, byte, short, char
2223 if (real_target_type == TypeManager.sbyte_type)
2224 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
2225 if (real_target_type == TypeManager.byte_type)
2226 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
2227 if (real_target_type == TypeManager.short_type)
2228 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
2229 if (real_target_type == TypeManager.char_type)
2230 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
2231 } else if (expr_type == TypeManager.int32_type){
2233 // From int to sbyte, byte, short, ushort, uint, ulong, char
2235 if (real_target_type == TypeManager.sbyte_type)
2236 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
2237 if (real_target_type == TypeManager.byte_type)
2238 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
2239 if (real_target_type == TypeManager.short_type)
2240 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
2241 if (real_target_type == TypeManager.ushort_type)
2242 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
2243 if (real_target_type == TypeManager.uint32_type)
2244 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2245 if (real_target_type == TypeManager.uint64_type)
2246 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2247 if (real_target_type == TypeManager.char_type)
2248 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2249 } else if (expr_type == TypeManager.uint32_type){
2251 // From uint to sbyte, byte, short, ushort, int, char
2253 if (real_target_type == TypeManager.sbyte_type)
2254 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2255 if (real_target_type == TypeManager.byte_type)
2256 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2257 if (real_target_type == TypeManager.short_type)
2258 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2259 if (real_target_type == TypeManager.ushort_type)
2260 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2261 if (real_target_type == TypeManager.int32_type)
2262 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2263 if (real_target_type == TypeManager.char_type)
2264 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2265 } else if (expr_type == TypeManager.int64_type){
2267 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2269 if (real_target_type == TypeManager.sbyte_type)
2270 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2271 if (real_target_type == TypeManager.byte_type)
2272 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2273 if (real_target_type == TypeManager.short_type)
2274 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2275 if (real_target_type == TypeManager.ushort_type)
2276 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2277 if (real_target_type == TypeManager.int32_type)
2278 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2279 if (real_target_type == TypeManager.uint32_type)
2280 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2281 if (real_target_type == TypeManager.uint64_type)
2282 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2283 if (real_target_type == TypeManager.char_type)
2284 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2285 } else if (expr_type == TypeManager.uint64_type){
2287 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2289 if (real_target_type == TypeManager.sbyte_type)
2290 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2291 if (real_target_type == TypeManager.byte_type)
2292 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2293 if (real_target_type == TypeManager.short_type)
2294 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2295 if (real_target_type == TypeManager.ushort_type)
2296 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2297 if (real_target_type == TypeManager.int32_type)
2298 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2299 if (real_target_type == TypeManager.uint32_type)
2300 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2301 if (real_target_type == TypeManager.int64_type)
2302 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2303 if (real_target_type == TypeManager.char_type)
2304 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2305 } else if (expr_type == TypeManager.char_type){
2307 // From char to sbyte, byte, short
2309 if (real_target_type == TypeManager.sbyte_type)
2310 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2311 if (real_target_type == TypeManager.byte_type)
2312 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2313 if (real_target_type == TypeManager.short_type)
2314 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2315 } else if (expr_type == TypeManager.float_type){
2317 // From float to sbyte, byte, short,
2318 // ushort, int, uint, long, ulong, char
2321 if (real_target_type == TypeManager.sbyte_type)
2322 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2323 if (real_target_type == TypeManager.byte_type)
2324 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2325 if (real_target_type == TypeManager.short_type)
2326 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2327 if (real_target_type == TypeManager.ushort_type)
2328 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2329 if (real_target_type == TypeManager.int32_type)
2330 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2331 if (real_target_type == TypeManager.uint32_type)
2332 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2333 if (real_target_type == TypeManager.int64_type)
2334 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2335 if (real_target_type == TypeManager.uint64_type)
2336 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2337 if (real_target_type == TypeManager.char_type)
2338 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2339 } else if (expr_type == TypeManager.double_type){
2341 // From double to byte, byte, short,
2342 // ushort, int, uint, long, ulong,
2343 // char, float or decimal
2345 if (real_target_type == TypeManager.sbyte_type)
2346 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2347 if (real_target_type == TypeManager.byte_type)
2348 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2349 if (real_target_type == TypeManager.short_type)
2350 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2351 if (real_target_type == TypeManager.ushort_type)
2352 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2353 if (real_target_type == TypeManager.int32_type)
2354 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2355 if (real_target_type == TypeManager.uint32_type)
2356 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2357 if (real_target_type == TypeManager.int64_type)
2358 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2359 if (real_target_type == TypeManager.uint64_type)
2360 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2361 if (real_target_type == TypeManager.char_type)
2362 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2363 if (real_target_type == TypeManager.float_type)
2364 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2367 // decimal is taken care of by the op_Explicit methods.
2373 /// Returns whether an explicit reference conversion can be performed
2374 /// from source_type to target_type
2376 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2378 bool target_is_value_type = target_type.IsValueType;
2380 if (source_type == target_type)
2384 // From object to any reference type
2386 if (source_type == TypeManager.object_type && !target_is_value_type)
2390 // From any class S to any class-type T, provided S is a base class of T
2392 if (target_type.IsSubclassOf (source_type))
2396 // From any interface type S to any interface T provided S is not derived from T
2398 if (source_type.IsInterface && target_type.IsInterface){
2399 if (!target_type.IsSubclassOf (source_type))
2404 // From any class type S to any interface T, provided S is not sealed
2405 // and provided S does not implement T.
2407 if (target_type.IsInterface && !source_type.IsSealed &&
2408 !TypeManager.ImplementsInterface (source_type, target_type))
2412 // From any interface-type S to to any class type T, provided T is not
2413 // sealed, or provided T implements S.
2415 if (source_type.IsInterface &&
2416 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2420 // From an array type S with an element type Se to an array type T with an
2421 // element type Te provided all the following are true:
2422 // * S and T differe only in element type, in other words, S and T
2423 // have the same number of dimensions.
2424 // * Both Se and Te are reference types
2425 // * An explicit referenc conversions exist from Se to Te
2427 if (source_type.IsArray && target_type.IsArray) {
2428 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2430 Type source_element_type = source_type.GetElementType ();
2431 Type target_element_type = target_type.GetElementType ();
2433 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2434 if (ExplicitReferenceConversionExists (source_element_type,
2435 target_element_type))
2441 // From System.Array to any array-type
2442 if (source_type == TypeManager.array_type &&
2443 target_type.IsArray){
2448 // From System delegate to any delegate-type
2450 if (source_type == TypeManager.delegate_type &&
2451 target_type.IsSubclassOf (TypeManager.delegate_type))
2455 // From ICloneable to Array or Delegate types
2457 if (source_type == TypeManager.icloneable_type &&
2458 (target_type == TypeManager.array_type ||
2459 target_type == TypeManager.delegate_type))
2466 /// Implements Explicit Reference conversions
2468 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2470 Type source_type = source.Type;
2471 bool target_is_value_type = target_type.IsValueType;
2474 // From object to any reference type
2476 if (source_type == TypeManager.object_type && !target_is_value_type)
2477 return new ClassCast (source, target_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))
2484 return new ClassCast (source, target_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 (TypeManager.ImplementsInterface (source_type, target_type))
2493 return new ClassCast (source, target_type);
2497 // From any class type S to any interface T, provides S is not sealed
2498 // and provided S does not implement T.
2500 if (target_type.IsInterface && !source_type.IsSealed) {
2501 if (TypeManager.ImplementsInterface (source_type, target_type))
2504 return new ClassCast (source, target_type);
2509 // From any interface-type S to to any class type T, provided T is not
2510 // sealed, or provided T implements S.
2512 if (source_type.IsInterface) {
2513 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2514 return new ClassCast (source, target_type);
2519 // From an array type S with an element type Se to an array type T with an
2520 // element type Te provided all the following are true:
2521 // * S and T differe only in element type, in other words, S and T
2522 // have the same number of dimensions.
2523 // * Both Se and Te are reference types
2524 // * An explicit referenc conversions exist from Se to Te
2526 if (source_type.IsArray && target_type.IsArray) {
2527 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2529 Type source_element_type = source_type.GetElementType ();
2530 Type target_element_type = target_type.GetElementType ();
2532 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2533 if (ExplicitReferenceConversionExists (source_element_type,
2534 target_element_type))
2535 return new ClassCast (source, target_type);
2540 // From System.Array to any array-type
2541 if (source_type == TypeManager.array_type &&
2542 target_type.IsArray) {
2543 return new ClassCast (source, target_type);
2547 // From System delegate to any delegate-type
2549 if (source_type == TypeManager.delegate_type &&
2550 target_type.IsSubclassOf (TypeManager.delegate_type))
2551 return new ClassCast (source, target_type);
2554 // From ICloneable to Array or Delegate types
2556 if (source_type == TypeManager.icloneable_type &&
2557 (target_type == TypeManager.array_type ||
2558 target_type == TypeManager.delegate_type))
2559 return new ClassCast (source, target_type);
2565 /// Performs an explicit conversion of the expression 'expr' whose
2566 /// type is expr.Type to 'target_type'.
2568 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2569 Type target_type, bool runtimeconv, Location loc)
2571 Type expr_type = expr.Type;
2572 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2577 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2582 // Unboxing conversion.
2584 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2585 return new UnboxCast (expr, target_type);
2590 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2594 // FIXME: Is there any reason we should have EnumConstant
2595 // dealt with here instead of just using always the
2596 // UnderlyingSystemType to wrap the type?
2598 if (expr is EnumConstant)
2599 e = ((EnumConstant) expr).Child;
2601 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2604 Expression t = ConvertImplicit (ec, e, target_type, loc);
2608 t = ConvertNumericExplicit (ec, e, target_type, loc);
2612 t = NarrowingConversion (ec, e, target_type, loc);
2616 Error_CannotConvertType (loc, expr_type, target_type);
2620 ne = ConvertReferenceExplicit (expr, target_type);
2625 if (target_type.IsPointer){
2626 if (expr_type.IsPointer)
2627 return new EmptyCast (expr, target_type);
2629 if (expr_type == TypeManager.sbyte_type ||
2630 expr_type == TypeManager.byte_type ||
2631 expr_type == TypeManager.short_type ||
2632 expr_type == TypeManager.ushort_type ||
2633 expr_type == TypeManager.int32_type ||
2634 expr_type == TypeManager.uint32_type ||
2635 expr_type == TypeManager.uint64_type ||
2636 expr_type == TypeManager.int64_type)
2637 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2639 if (expr_type.IsPointer){
2640 if (target_type == TypeManager.sbyte_type ||
2641 target_type == TypeManager.byte_type ||
2642 target_type == TypeManager.short_type ||
2643 target_type == TypeManager.ushort_type ||
2644 target_type == TypeManager.int32_type ||
2645 target_type == TypeManager.uint32_type ||
2646 target_type == TypeManager.uint64_type ||
2647 target_type == TypeManager.int64_type){
2648 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2651 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2656 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2660 // We should always be able to go from an uint32
2661 // implicitly or explicitly to the other integral
2664 throw new Exception ("Internal compiler error");
2669 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2673 if (!(runtimeconv)) {
2674 ne = NarrowingConversion (ec, expr, target_type, loc);
2678 Error_CannotConvertType (loc, expr_type, target_type);
2684 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2686 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2687 Type target_type, Location l)
2689 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2694 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2698 ne = ConvertReferenceExplicit (expr, target_type);
2702 ne = NarrowingConversion (ec, expr, target_type, l);
2706 Error_CannotConvertType (l, expr.Type, target_type);
2710 static string ExprClassName (ExprClass c)
2713 case ExprClass.Invalid:
2715 case ExprClass.Value:
2717 case ExprClass.Variable:
2719 case ExprClass.Namespace:
2721 case ExprClass.Type:
2723 case ExprClass.MethodGroup:
2724 return "method group";
2725 case ExprClass.PropertyAccess:
2726 return "property access";
2727 case ExprClass.EventAccess:
2728 return "event access";
2729 case ExprClass.IndexerAccess:
2730 return "indexer access";
2731 case ExprClass.Nothing:
2734 throw new Exception ("Should not happen");
2738 /// Reports that we were expecting 'expr' to be of class 'expected'
2740 public void Error118 (string expected)
2742 string kind = "Unknown";
2744 kind = ExprClassName (eclass);
2746 Error (118, "Expression denotes a '" + kind +
2747 "' where a '" + expected + "' was expected");
2750 public void Error118 (ResolveFlags flags)
2752 ArrayList valid = new ArrayList (10);
2754 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2755 valid.Add ("variable");
2756 valid.Add ("value");
2759 if ((flags & ResolveFlags.Type) != 0)
2762 if ((flags & ResolveFlags.MethodGroup) != 0)
2763 valid.Add ("method group");
2765 if ((flags & ResolveFlags.SimpleName) != 0)
2766 valid.Add ("simple name");
2768 if (valid.Count == 0)
2769 valid.Add ("unknown");
2771 StringBuilder sb = new StringBuilder ();
2772 for (int i = 0; i < valid.Count; i++) {
2775 else if (i == valid.Count)
2777 sb.Append (valid [i]);
2780 string kind = ExprClassName (eclass);
2782 Error (119, "Expression denotes a '" + kind + "' where " +
2783 "a '" + sb.ToString () + "' was expected");
2786 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2788 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2789 TypeManager.MonoBASIC_Name (t));
2792 public static void UnsafeError (Location loc)
2794 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2798 /// Converts the IntConstant, UIntConstant, LongConstant or
2799 /// ULongConstant into the integral target_type. Notice
2800 /// that we do not return an 'Expression' we do return
2801 /// a boxed integral type.
2803 /// FIXME: Since I added the new constants, we need to
2804 /// also support conversions from CharConstant, ByteConstant,
2805 /// SByteConstant, UShortConstant, ShortConstant
2807 /// This is used by the switch statement, so the domain
2808 /// of work is restricted to the literals above, and the
2809 /// targets are int32, uint32, char, byte, sbyte, ushort,
2810 /// short, uint64 and int64
2812 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2816 if (c.Type == target_type)
2817 return ((Constant) c).GetValue ();
2820 // Make into one of the literals we handle, we dont really care
2821 // about this value as we will just return a few limited types
2823 if (c is EnumConstant)
2824 c = ((EnumConstant)c).WidenToCompilerConstant ();
2826 if (c is IntConstant){
2827 int v = ((IntConstant) c).Value;
2829 if (target_type == TypeManager.uint32_type){
2832 } else if (target_type == TypeManager.char_type){
2833 if (v >= Char.MinValue && v <= Char.MaxValue)
2835 } else if (target_type == TypeManager.byte_type){
2836 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2838 } else if (target_type == TypeManager.sbyte_type){
2839 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2841 } else if (target_type == TypeManager.short_type){
2842 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2844 } else if (target_type == TypeManager.ushort_type){
2845 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2847 } else if (target_type == TypeManager.int64_type)
2849 else if (target_type == TypeManager.uint64_type){
2855 } else if (c is UIntConstant){
2856 uint v = ((UIntConstant) c).Value;
2858 if (target_type == TypeManager.int32_type){
2859 if (v <= Int32.MaxValue)
2861 } else if (target_type == TypeManager.char_type){
2862 if (v >= Char.MinValue && v <= Char.MaxValue)
2864 } else if (target_type == TypeManager.byte_type){
2865 if (v <= Byte.MaxValue)
2867 } else if (target_type == TypeManager.sbyte_type){
2868 if (v <= SByte.MaxValue)
2870 } else if (target_type == TypeManager.short_type){
2871 if (v <= UInt16.MaxValue)
2873 } else if (target_type == TypeManager.ushort_type){
2874 if (v <= UInt16.MaxValue)
2876 } else if (target_type == TypeManager.int64_type)
2878 else if (target_type == TypeManager.uint64_type)
2881 } else if (c is LongConstant){
2882 long v = ((LongConstant) c).Value;
2884 if (target_type == TypeManager.int32_type){
2885 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2887 } else if (target_type == TypeManager.uint32_type){
2888 if (v >= 0 && v <= UInt32.MaxValue)
2890 } else if (target_type == TypeManager.char_type){
2891 if (v >= Char.MinValue && v <= Char.MaxValue)
2893 } else if (target_type == TypeManager.byte_type){
2894 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2896 } else if (target_type == TypeManager.sbyte_type){
2897 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2899 } else if (target_type == TypeManager.short_type){
2900 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2902 } else if (target_type == TypeManager.ushort_type){
2903 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2905 } else if (target_type == TypeManager.uint64_type){
2910 } else if (c is ULongConstant){
2911 ulong v = ((ULongConstant) c).Value;
2913 if (target_type == TypeManager.int32_type){
2914 if (v <= Int32.MaxValue)
2916 } else if (target_type == TypeManager.uint32_type){
2917 if (v <= UInt32.MaxValue)
2919 } else if (target_type == TypeManager.char_type){
2920 if (v >= Char.MinValue && v <= Char.MaxValue)
2922 } else if (target_type == TypeManager.byte_type){
2923 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2925 } else if (target_type == TypeManager.sbyte_type){
2926 if (v <= (int) SByte.MaxValue)
2928 } else if (target_type == TypeManager.short_type){
2929 if (v <= UInt16.MaxValue)
2931 } else if (target_type == TypeManager.ushort_type){
2932 if (v <= UInt16.MaxValue)
2934 } else if (target_type == TypeManager.int64_type){
2935 if (v <= Int64.MaxValue)
2939 } else if (c is ByteConstant){
2940 byte v = ((ByteConstant) c).Value;
2942 if (target_type == TypeManager.int32_type)
2944 else if (target_type == TypeManager.uint32_type)
2946 else if (target_type == TypeManager.char_type)
2948 else if (target_type == TypeManager.sbyte_type){
2949 if (v <= SByte.MaxValue)
2951 } else if (target_type == TypeManager.short_type)
2953 else if (target_type == TypeManager.ushort_type)
2955 else if (target_type == TypeManager.int64_type)
2957 else if (target_type == TypeManager.uint64_type)
2960 } else if (c is SByteConstant){
2961 sbyte v = ((SByteConstant) c).Value;
2963 if (target_type == TypeManager.int32_type)
2965 else if (target_type == TypeManager.uint32_type){
2968 } else if (target_type == TypeManager.char_type){
2971 } else if (target_type == TypeManager.byte_type){
2974 } else if (target_type == TypeManager.short_type)
2976 else if (target_type == TypeManager.ushort_type){
2979 } else if (target_type == TypeManager.int64_type)
2981 else if (target_type == TypeManager.uint64_type){
2986 } else if (c is ShortConstant){
2987 short v = ((ShortConstant) c).Value;
2989 if (target_type == TypeManager.int32_type){
2991 } else if (target_type == TypeManager.uint32_type){
2994 } else if (target_type == TypeManager.char_type){
2997 } else if (target_type == TypeManager.byte_type){
2998 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3000 } else if (target_type == TypeManager.sbyte_type){
3001 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3003 } else if (target_type == TypeManager.ushort_type){
3006 } else if (target_type == TypeManager.int64_type)
3008 else if (target_type == TypeManager.uint64_type)
3012 } else if (c is UShortConstant){
3013 ushort v = ((UShortConstant) c).Value;
3015 if (target_type == TypeManager.int32_type)
3017 else if (target_type == TypeManager.uint32_type)
3019 else if (target_type == TypeManager.char_type){
3020 if (v >= Char.MinValue && v <= Char.MaxValue)
3022 } else if (target_type == TypeManager.byte_type){
3023 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3025 } else if (target_type == TypeManager.sbyte_type){
3026 if (v <= SByte.MaxValue)
3028 } else if (target_type == TypeManager.short_type){
3029 if (v <= Int16.MaxValue)
3031 } else if (target_type == TypeManager.int64_type)
3033 else if (target_type == TypeManager.uint64_type)
3037 } else if (c is CharConstant){
3038 char v = ((CharConstant) c).Value;
3040 if (target_type == TypeManager.int32_type)
3042 else if (target_type == TypeManager.uint32_type)
3044 else if (target_type == TypeManager.byte_type){
3045 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3047 } else if (target_type == TypeManager.sbyte_type){
3048 if (v <= SByte.MaxValue)
3050 } else if (target_type == TypeManager.short_type){
3051 if (v <= Int16.MaxValue)
3053 } else if (target_type == TypeManager.ushort_type)
3055 else if (target_type == TypeManager.int64_type)
3057 else if (target_type == TypeManager.uint64_type)
3062 Error_ConstantValueCannotBeConverted (loc, s, target_type);
3067 // Load the object from the pointer.
3069 public static void LoadFromPtr (ILGenerator ig, Type t)
3071 if (t == TypeManager.int32_type)
3072 ig.Emit (OpCodes.Ldind_I4);
3073 else if (t == TypeManager.uint32_type)
3074 ig.Emit (OpCodes.Ldind_U4);
3075 else if (t == TypeManager.short_type)
3076 ig.Emit (OpCodes.Ldind_I2);
3077 else if (t == TypeManager.ushort_type)
3078 ig.Emit (OpCodes.Ldind_U2);
3079 else if (t == TypeManager.char_type)
3080 ig.Emit (OpCodes.Ldind_U2);
3081 else if (t == TypeManager.byte_type)
3082 ig.Emit (OpCodes.Ldind_U1);
3083 else if (t == TypeManager.sbyte_type)
3084 ig.Emit (OpCodes.Ldind_I1);
3085 else if (t == TypeManager.uint64_type)
3086 ig.Emit (OpCodes.Ldind_I8);
3087 else if (t == TypeManager.int64_type)
3088 ig.Emit (OpCodes.Ldind_I8);
3089 else if (t == TypeManager.float_type)
3090 ig.Emit (OpCodes.Ldind_R4);
3091 else if (t == TypeManager.double_type)
3092 ig.Emit (OpCodes.Ldind_R8);
3093 else if (t == TypeManager.bool_type)
3094 ig.Emit (OpCodes.Ldind_I1);
3095 else if (t == TypeManager.intptr_type)
3096 ig.Emit (OpCodes.Ldind_I);
3097 else if (TypeManager.IsEnumType (t)) {
3098 if (t == TypeManager.enum_type)
3099 ig.Emit (OpCodes.Ldind_Ref);
3101 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
3102 } else if (t.IsValueType)
3103 ig.Emit (OpCodes.Ldobj, t);
3105 ig.Emit (OpCodes.Ldind_Ref);
3109 // The stack contains the pointer and the value of type 'type'
3111 public static void StoreFromPtr (ILGenerator ig, Type type)
3113 if (TypeManager.IsEnumType (type))
3114 type = TypeManager.EnumToUnderlying (type);
3115 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
3116 ig.Emit (OpCodes.Stind_I4);
3117 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
3118 ig.Emit (OpCodes.Stind_I8);
3119 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
3120 type == TypeManager.ushort_type)
3121 ig.Emit (OpCodes.Stind_I2);
3122 else if (type == TypeManager.float_type)
3123 ig.Emit (OpCodes.Stind_R4);
3124 else if (type == TypeManager.double_type)
3125 ig.Emit (OpCodes.Stind_R8);
3126 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
3127 type == TypeManager.bool_type)
3128 ig.Emit (OpCodes.Stind_I1);
3129 else if (type == TypeManager.intptr_type)
3130 ig.Emit (OpCodes.Stind_I);
3131 else if (type.IsValueType)
3132 ig.Emit (OpCodes.Stobj, type);
3134 ig.Emit (OpCodes.Stind_Ref);
3138 // Returns the size of type 't' if known, otherwise, 0
3140 public static int GetTypeSize (Type t)
3142 t = TypeManager.TypeToCoreType (t);
3143 if (t == TypeManager.int32_type ||
3144 t == TypeManager.uint32_type ||
3145 t == TypeManager.float_type)
3147 else if (t == TypeManager.int64_type ||
3148 t == TypeManager.uint64_type ||
3149 t == TypeManager.double_type)
3151 else if (t == TypeManager.byte_type ||
3152 t == TypeManager.sbyte_type ||
3153 t == TypeManager.bool_type)
3155 else if (t == TypeManager.short_type ||
3156 t == TypeManager.char_type ||
3157 t == TypeManager.ushort_type)
3159 else if (t == TypeManager.decimal_type)
3166 // Default implementation of IAssignMethod.CacheTemporaries
3168 public void CacheTemporaries (EmitContext ec)
3172 static void Error_NegativeArrayIndex (Location loc)
3174 Report.Error (284, loc, "Can not create array with a negative size");
3178 // Converts 'source' to an int, uint, long or ulong.
3180 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
3184 bool old_checked = ec.CheckState;
3185 ec.CheckState = true;
3187 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
3188 if (target == null){
3189 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
3190 if (target == null){
3191 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
3192 if (target == null){
3193 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
3195 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
3199 ec.CheckState = old_checked;
3202 // Only positive constants are allowed at compile time
3204 if (target is Constant){
3205 if (target is IntConstant){
3206 if (((IntConstant) target).Value < 0){
3207 Error_NegativeArrayIndex (loc);
3212 if (target is LongConstant){
3213 if (((LongConstant) target).Value < 0){
3214 Error_NegativeArrayIndex (loc);
3227 /// This is just a base class for expressions that can
3228 /// appear on statements (invocations, object creation,
3229 /// assignments, post/pre increment and decrement). The idea
3230 /// being that they would support an extra Emition interface that
3231 /// does not leave a result on the stack.
3233 public abstract class ExpressionStatement : Expression {
3236 /// Requests the expression to be emitted in a 'statement'
3237 /// context. This means that no new value is left on the
3238 /// stack after invoking this method (constrasted with
3239 /// Emit that will always leave a value on the stack).
3241 public abstract void EmitStatement (EmitContext ec);
3245 /// This kind of cast is used to encapsulate the child
3246 /// whose type is child.Type into an expression that is
3247 /// reported to return "return_type". This is used to encapsulate
3248 /// expressions which have compatible types, but need to be dealt
3249 /// at higher levels with.
3251 /// For example, a "byte" expression could be encapsulated in one
3252 /// of these as an "unsigned int". The type for the expression
3253 /// would be "unsigned int".
3256 public class EmptyCast : Expression {
3257 protected Expression child;
3259 public EmptyCast (Expression child, Type return_type)
3261 eclass = child.eclass;
3266 public override Expression DoResolve (EmitContext ec)
3268 // This should never be invoked, we are born in fully
3269 // initialized state.
3274 public override void Emit (EmitContext ec)
3281 /// This class is used to wrap literals which belong inside Enums
3283 public class EnumConstant : Constant {
3284 public Constant Child;
3286 public EnumConstant (Constant child, Type enum_type)
3288 eclass = child.eclass;
3293 public override Expression DoResolve (EmitContext ec)
3295 // This should never be invoked, we are born in fully
3296 // initialized state.
3301 public override void Emit (EmitContext ec)
3306 public override object GetValue ()
3308 return Child.GetValue ();
3312 // Converts from one of the valid underlying types for an enumeration
3313 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3314 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3316 public Constant WidenToCompilerConstant ()
3318 Type t = TypeManager.EnumToUnderlying (Child.Type);
3319 object v = ((Constant) Child).GetValue ();;
3321 if (t == TypeManager.int32_type)
3322 return new IntConstant ((int) v);
3323 if (t == TypeManager.uint32_type)
3324 return new UIntConstant ((uint) v);
3325 if (t == TypeManager.int64_type)
3326 return new LongConstant ((long) v);
3327 if (t == TypeManager.uint64_type)
3328 return new ULongConstant ((ulong) v);
3329 if (t == TypeManager.short_type)
3330 return new ShortConstant ((short) v);
3331 if (t == TypeManager.ushort_type)
3332 return new UShortConstant ((ushort) v);
3333 if (t == TypeManager.byte_type)
3334 return new ByteConstant ((byte) v);
3335 if (t == TypeManager.sbyte_type)
3336 return new SByteConstant ((sbyte) v);
3338 throw new Exception ("Invalid enumeration underlying type: " + t);
3342 // Extracts the value in the enumeration on its native representation
3344 public object GetPlainValue ()
3346 Type t = TypeManager.EnumToUnderlying (Child.Type);
3347 object v = ((Constant) Child).GetValue ();;
3349 if (t == TypeManager.int32_type)
3351 if (t == TypeManager.uint32_type)
3353 if (t == TypeManager.int64_type)
3355 if (t == TypeManager.uint64_type)
3357 if (t == TypeManager.short_type)
3359 if (t == TypeManager.ushort_type)
3361 if (t == TypeManager.byte_type)
3363 if (t == TypeManager.sbyte_type)
3369 public override string AsString ()
3371 return Child.AsString ();
3374 public override DoubleConstant ConvertToDouble ()
3376 return Child.ConvertToDouble ();
3379 public override FloatConstant ConvertToFloat ()
3381 return Child.ConvertToFloat ();
3384 public override ULongConstant ConvertToULong ()
3386 return Child.ConvertToULong ();
3389 public override LongConstant ConvertToLong ()
3391 return Child.ConvertToLong ();
3394 public override UIntConstant ConvertToUInt ()
3396 return Child.ConvertToUInt ();
3399 public override IntConstant ConvertToInt ()
3401 return Child.ConvertToInt ();
3406 /// This kind of cast is used to encapsulate Value Types in objects.
3408 /// The effect of it is to box the value type emitted by the previous
3411 public class BoxedCast : EmptyCast {
3413 public BoxedCast (Expression expr)
3414 : base (expr, TypeManager.object_type)
3418 public override Expression DoResolve (EmitContext ec)
3420 // This should never be invoked, we are born in fully
3421 // initialized state.
3426 public override void Emit (EmitContext ec)
3430 ec.ig.Emit (OpCodes.Box, child.Type);
3434 public class UnboxCast : EmptyCast {
3435 public UnboxCast (Expression expr, Type return_type)
3436 : base (expr, return_type)
3440 public override Expression DoResolve (EmitContext ec)
3442 // This should never be invoked, we are born in fully
3443 // initialized state.
3448 public override void Emit (EmitContext ec)
3451 ILGenerator ig = ec.ig;
3454 ig.Emit (OpCodes.Unbox, t);
3456 LoadFromPtr (ig, t);
3461 /// This is used to perform explicit numeric conversions.
3463 /// Explicit numeric conversions might trigger exceptions in a checked
3464 /// context, so they should generate the conv.ovf opcodes instead of
3467 public class ConvCast : EmptyCast {
3468 public enum Mode : byte {
3469 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3471 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3472 U2_I1, U2_U1, U2_I2, U2_CH,
3473 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3474 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3475 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3476 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3477 CH_I1, CH_U1, CH_I2,
3478 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3479 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3485 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3486 : base (child, return_type)
3488 checked_state = ec.CheckState;
3492 public override Expression DoResolve (EmitContext ec)
3494 // This should never be invoked, we are born in fully
3495 // initialized state.
3500 public override void Emit (EmitContext ec)
3502 ILGenerator ig = ec.ig;
3508 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3509 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3510 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3511 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3512 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3514 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3515 case Mode.U1_CH: /* nothing */ break;
3517 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3518 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3519 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3520 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3521 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3522 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3524 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3525 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3526 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3527 case Mode.U2_CH: /* nothing */ break;
3529 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3530 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3531 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3532 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3533 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3534 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3535 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3537 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3538 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3539 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3540 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3541 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3542 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3544 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3545 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3546 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3547 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3548 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3549 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3550 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3551 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3553 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3554 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3555 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3556 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3557 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3558 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3559 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3560 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3562 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3563 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3564 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3566 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3567 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3568 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3569 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3570 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3571 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3572 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3573 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3574 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3576 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3577 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3578 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3579 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3580 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3581 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3582 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3583 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3584 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3585 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3589 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3590 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3591 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3592 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3593 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3595 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3596 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3598 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3599 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3600 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3601 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3602 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3603 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3605 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3606 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3607 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3608 case Mode.U2_CH: /* nothing */ break;
3610 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3611 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3612 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3613 case Mode.I4_U4: /* nothing */ break;
3614 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3615 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3616 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3618 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3619 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3620 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3621 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3622 case Mode.U4_I4: /* nothing */ break;
3623 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3625 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3626 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3627 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3628 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3629 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3630 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3631 case Mode.I8_U8: /* nothing */ break;
3632 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3634 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3635 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3636 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3637 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3638 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3639 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3640 case Mode.U8_I8: /* nothing */ break;
3641 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3643 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3644 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3645 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3647 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3648 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3649 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3650 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3651 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3652 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3653 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3654 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3655 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3657 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3658 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3659 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3660 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3661 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3662 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3663 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3664 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3665 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3666 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3672 public class OpcodeCast : EmptyCast {
3676 public OpcodeCast (Expression child, Type return_type, OpCode op)
3677 : base (child, return_type)
3681 second_valid = false;
3684 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3685 : base (child, return_type)
3690 second_valid = true;
3693 public override Expression DoResolve (EmitContext ec)
3695 // This should never be invoked, we are born in fully
3696 // initialized state.
3701 public override void Emit (EmitContext ec)
3712 /// This kind of cast is used to encapsulate a child and cast it
3713 /// to the class requested
3715 public class ClassCast : EmptyCast {
3716 public ClassCast (Expression child, Type return_type)
3717 : base (child, return_type)
3722 public override Expression DoResolve (EmitContext ec)
3724 // This should never be invoked, we are born in fully
3725 // initialized state.
3730 public override void Emit (EmitContext ec)
3734 ec.ig.Emit (OpCodes.Castclass, type);
3740 /// SimpleName expressions are initially formed of a single
3741 /// word and it only happens at the beginning of the expression.
3745 /// The expression will try to be bound to a Field, a Method
3746 /// group or a Property. If those fail we pass the name to our
3747 /// caller and the SimpleName is compounded to perform a type
3748 /// lookup. The idea behind this process is that we want to avoid
3749 /// creating a namespace map from the assemblies, as that requires
3750 /// the GetExportedTypes function to be called and a hashtable to
3751 /// be constructed which reduces startup time. If later we find
3752 /// that this is slower, we should create a 'NamespaceExpr' expression
3753 /// that fully participates in the resolution process.
3755 /// For example 'System.Console.WriteLine' is decomposed into
3756 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3758 /// The first SimpleName wont produce a match on its own, so it will
3760 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3762 /// System.Console will produce a TypeExpr match.
3764 /// The downside of this is that we might be hitting 'LookupType' too many
3765 /// times with this scheme.
3767 public class SimpleName : Expression, ITypeExpression {
3768 public readonly string Name;
3770 public SimpleName (string name, Location l)
3776 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3778 if (ec.IsFieldInitializer)
3781 "A field initializer cannot reference the non-static field, " +
3782 "method or property '"+name+"'");
3786 "An object reference is required " +
3787 "for the non-static field '"+name+"'");
3791 // Checks whether we are trying to access an instance
3792 // property, method or field from a static body.
3794 Expression MemberStaticCheck (EmitContext ec, Expression e)
3796 if (e is IMemberExpr){
3797 IMemberExpr member = (IMemberExpr) e;
3799 if (!member.IsStatic){
3800 Error_ObjectRefRequired (ec, loc, Name);
3808 public override Expression DoResolve (EmitContext ec)
3810 return SimpleNameResolve (ec, null, false);
3813 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3815 return SimpleNameResolve (ec, right_side, false);
3819 public Expression DoResolveAllowStatic (EmitContext ec)
3821 return SimpleNameResolve (ec, null, true);
3824 public Expression DoResolveType (EmitContext ec)
3827 // Stage 3: Lookup symbol in the various namespaces.
3829 DeclSpace ds = ec.DeclSpace;
3833 if (ec.ResolvingTypeTree){
3834 int errors = Report.Errors;
3835 Type dt = ec.DeclSpace.FindType (loc, Name);
3836 if (Report.Errors != errors)
3840 return new TypeExpr (dt, loc);
3843 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3844 return new TypeExpr (t, loc);
3848 // Stage 2 part b: Lookup up if we are an alias to a type
3851 // Since we are cheating: we only do the Alias lookup for
3852 // namespaces if the name does not include any dots in it
3855 alias_value = ec.DeclSpace.LookupAlias (Name);
3857 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3858 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3859 return new TypeExpr (t, loc);
3861 // we have alias value, but it isn't Type, so try if it's namespace
3862 return new SimpleName (alias_value, loc);
3865 // No match, maybe our parent can compose us
3866 // into something meaningful.
3871 /// 7.5.2: Simple Names.
3873 /// Local Variables and Parameters are handled at
3874 /// parse time, so they never occur as SimpleNames.
3876 /// The 'allow_static' flag is used by MemberAccess only
3877 /// and it is used to inform us that it is ok for us to
3878 /// avoid the static check, because MemberAccess might end
3879 /// up resolving the Name as a Type name and the access as
3880 /// a static type access.
3882 /// ie: Type Type; .... { Type.GetType (""); }
3884 /// Type is both an instance variable and a Type; Type.GetType
3885 /// is the static method not an instance method of type.
3887 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3889 Expression e = null;
3892 // Stage 1: Performed by the parser (binding to locals or parameters).
3894 Block current_block = ec.CurrentBlock;
3895 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
3896 LocalVariableReference var;
3898 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3900 if (right_side != null)
3901 return var.ResolveLValue (ec, right_side);
3903 return var.Resolve (ec);
3906 if (current_block != null){
3908 Parameter par = null;
3909 Parameters pars = current_block.Parameters;
3911 par = pars.GetParameterByName (Name, out idx);
3914 ParameterReference param;
3916 param = new ParameterReference (pars, idx, Name, loc);
3918 if (right_side != null)
3919 return param.ResolveLValue (ec, right_side);
3921 return param.Resolve (ec);
3926 // Stage 2: Lookup members
3930 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3931 // Hence we have two different cases
3934 DeclSpace lookup_ds = ec.DeclSpace;
3936 if (lookup_ds.TypeBuilder == null)
3939 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3944 // Classes/structs keep looking, enums break
3946 if (lookup_ds is TypeContainer)
3947 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3950 } while (lookup_ds != null);
3952 if (e == null && ec.ContainerType != null)
3953 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3955 // #52067 - Start - Trying to solve
3959 ArrayList lookups = new ArrayList();
3960 ArrayList typelookups = new ArrayList();
3962 int split = Name.LastIndexOf('.');
3964 String nameSpacePart = Name.Substring(0, split);
3965 String memberNamePart = Name.Substring(split + 1);
3966 foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
3967 e = MemberLookup(ec, type, memberNamePart, loc);
3970 typelookups.Add(type);
3975 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
3976 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
3977 e = MemberLookup(ec, type, Name, loc);
3980 typelookups.Add(type);
3983 if (lookups.Count == 1) {
3984 e = (Expression)lookups[0];
3986 if (lookups.Count > 1) {
3987 StringBuilder sb = new StringBuilder();
3988 foreach(Type type in typelookups)
3989 sb.Append("'" + type.FullName + "'");
3990 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
3999 return DoResolveType (ec);
4004 if (e is IMemberExpr) {
4005 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
4009 IMemberExpr me = e as IMemberExpr;
4013 // This fails if ResolveMemberAccess() was unable to decide whether
4014 // it's a field or a type of the same name.
4015 if (!me.IsStatic && (me.InstanceExpression == null))
4018 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
4019 Maybe a grammar-related problem?
4022 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
4023 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
4024 "outer type '" + me.DeclaringType + "' via nested type '" +
4025 me.InstanceExpression.Type + "'");
4029 if (right_side != null)
4030 e = e.DoResolveLValue (ec, right_side);
4032 e = e.DoResolve (ec);
4037 if (ec.IsStatic || ec.IsFieldInitializer){
4041 return MemberStaticCheck (ec, e);
4046 public override void Emit (EmitContext ec)
4049 // If this is ever reached, then we failed to
4050 // find the name as a namespace
4053 Error (30451, "The name '" + Name +
4054 "' does not exist in the class '" +
4055 ec.DeclSpace.Name + "'");
4058 public override string ToString ()
4065 /// Fully resolved expression that evaluates to a type
4067 public class TypeExpr : Expression, ITypeExpression {
4068 public TypeExpr (Type t, Location l)
4071 eclass = ExprClass.Type;
4075 public virtual Expression DoResolveType (EmitContext ec)
4080 override public Expression DoResolve (EmitContext ec)
4085 override public void Emit (EmitContext ec)
4087 throw new Exception ("Should never be called");
4090 public override string ToString ()
4092 return Type.ToString ();
4097 /// Used to create types from a fully qualified name. These are just used
4098 /// by the parser to setup the core types. A TypeLookupExpression is always
4099 /// classified as a type.
4101 public class TypeLookupExpression : TypeExpr {
4104 public TypeLookupExpression (string name) : base (null, Location.Null)
4109 public override Expression DoResolveType (EmitContext ec)
4112 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
4116 public override Expression DoResolve (EmitContext ec)
4118 return DoResolveType (ec);
4121 public override void Emit (EmitContext ec)
4123 throw new Exception ("Should never be called");
4126 public override string ToString ()
4133 /// MethodGroup Expression.
4135 /// This is a fully resolved expression that evaluates to a type
4137 public class MethodGroupExpr : Expression, IMemberExpr {
4138 public MethodBase [] Methods;
4139 Expression instance_expression = null;
4140 bool is_explicit_impl = false;
4142 public MethodGroupExpr (MemberInfo [] mi, Location l)
4144 Methods = new MethodBase [mi.Length];
4145 mi.CopyTo (Methods, 0);
4146 eclass = ExprClass.MethodGroup;
4147 type = TypeManager.object_type;
4151 public MethodGroupExpr (ArrayList list, Location l)
4153 Methods = new MethodBase [list.Count];
4156 list.CopyTo (Methods, 0);
4158 foreach (MemberInfo m in list){
4159 if (!(m is MethodBase)){
4160 Console.WriteLine ("Name " + m.Name);
4161 Console.WriteLine ("Found a: " + m.GetType ().FullName);
4167 eclass = ExprClass.MethodGroup;
4168 type = TypeManager.object_type;
4171 public Type DeclaringType {
4173 return Methods [0].DeclaringType;
4178 // 'A method group may have associated an instance expression'
4180 public Expression InstanceExpression {
4182 return instance_expression;
4186 instance_expression = value;
4190 public bool IsExplicitImpl {
4192 return is_explicit_impl;
4196 is_explicit_impl = value;
4200 public string Name {
4202 return Methods [0].Name;
4206 public bool IsInstance {
4208 foreach (MethodBase mb in Methods)
4216 public bool IsStatic {
4218 foreach (MethodBase mb in Methods)
4226 override public Expression DoResolve (EmitContext ec)
4228 if (instance_expression != null) {
4229 instance_expression = instance_expression.DoResolve (ec);
4230 if (instance_expression == null)
4237 public void ReportUsageError ()
4239 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
4240 Methods [0].Name + "()' is referenced without parentheses");
4243 override public void Emit (EmitContext ec)
4245 ReportUsageError ();
4248 bool RemoveMethods (bool keep_static)
4250 ArrayList smethods = new ArrayList ();
4252 foreach (MethodBase mb in Methods){
4253 if (mb.IsStatic == keep_static)
4257 if (smethods.Count == 0)
4260 Methods = new MethodBase [smethods.Count];
4261 smethods.CopyTo (Methods, 0);
4267 /// Removes any instance methods from the MethodGroup, returns
4268 /// false if the resulting set is empty.
4270 public bool RemoveInstanceMethods ()
4272 return RemoveMethods (true);
4276 /// Removes any static methods from the MethodGroup, returns
4277 /// false if the resulting set is empty.
4279 public bool RemoveStaticMethods ()
4281 return RemoveMethods (false);
4286 /// Fully resolved expression that evaluates to a Field
4288 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4289 public readonly FieldInfo FieldInfo;
4290 Expression instance_expr;
4292 public FieldExpr (FieldInfo fi, Location l)
4295 eclass = ExprClass.Variable;
4296 type = fi.FieldType;
4300 public string Name {
4302 return FieldInfo.Name;
4306 public bool IsInstance {
4308 return !FieldInfo.IsStatic;
4312 public bool IsStatic {
4314 return FieldInfo.IsStatic;
4318 public Type DeclaringType {
4320 return FieldInfo.DeclaringType;
4324 public Expression InstanceExpression {
4326 return instance_expr;
4330 instance_expr = value;
4334 override public Expression DoResolve (EmitContext ec)
4336 if (!FieldInfo.IsStatic){
4337 if (instance_expr == null){
4338 throw new Exception ("non-static FieldExpr without instance var\n" +
4339 "You have to assign the Instance variable\n" +
4340 "Of the FieldExpr to set this\n");
4343 // Resolve the field's instance expression while flow analysis is turned
4344 // off: when accessing a field "a.b", we must check whether the field
4345 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4346 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4347 ResolveFlags.DisableFlowAnalysis);
4348 if (instance_expr == null)
4352 // If the instance expression is a local variable or parameter.
4353 IVariable var = instance_expr as IVariable;
4354 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4360 void Report_AssignToReadonly (bool is_instance)
4365 msg = "Readonly field can not be assigned outside " +
4366 "of constructor or variable initializer";
4368 msg = "A static readonly field can only be assigned in " +
4369 "a static constructor";
4371 Report.Error (is_instance ? 191 : 198, loc, msg);
4374 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4376 IVariable var = instance_expr as IVariable;
4378 var.SetFieldAssigned (ec, FieldInfo.Name);
4380 Expression e = DoResolve (ec);
4385 if (!FieldInfo.IsInitOnly)
4389 // InitOnly fields can only be assigned in constructors
4392 if (ec.IsConstructor)
4395 Report_AssignToReadonly (true);
4400 override public void Emit (EmitContext ec)
4402 ILGenerator ig = ec.ig;
4403 bool is_volatile = false;
4405 if (FieldInfo is FieldBuilder){
4406 FieldBase f = TypeManager.GetField (FieldInfo);
4408 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4411 f.status |= Field.Status.USED;
4414 if (FieldInfo.IsStatic){
4416 ig.Emit (OpCodes.Volatile);
4418 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4420 if (instance_expr.Type.IsValueType){
4422 LocalTemporary tempo = null;
4424 if (!(instance_expr is IMemoryLocation)){
4425 tempo = new LocalTemporary (
4426 ec, instance_expr.Type);
4428 InstanceExpression.Emit (ec);
4432 ml = (IMemoryLocation) instance_expr;
4434 ml.AddressOf (ec, AddressOp.Load);
4436 instance_expr.Emit (ec);
4439 ig.Emit (OpCodes.Volatile);
4441 ig.Emit (OpCodes.Ldfld, FieldInfo);
4445 public void EmitAssign (EmitContext ec, Expression source)
4447 FieldAttributes fa = FieldInfo.Attributes;
4448 bool is_static = (fa & FieldAttributes.Static) != 0;
4449 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4450 ILGenerator ig = ec.ig;
4452 if (is_readonly && !ec.IsConstructor){
4453 Report_AssignToReadonly (!is_static);
4458 Expression instance = instance_expr;
4460 if (instance.Type.IsValueType){
4461 if (instance is IMemoryLocation){
4462 IMemoryLocation ml = (IMemoryLocation) instance;
4464 ml.AddressOf (ec, AddressOp.Store);
4466 throw new Exception ("The " + instance + " of type " +
4468 " represents a ValueType and does " +
4469 "not implement IMemoryLocation");
4475 if (FieldInfo is FieldBuilder){
4476 FieldBase f = TypeManager.GetField (FieldInfo);
4478 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4479 ig.Emit (OpCodes.Volatile);
4483 ig.Emit (OpCodes.Stsfld, FieldInfo);
4485 ig.Emit (OpCodes.Stfld, FieldInfo);
4487 if (FieldInfo is FieldBuilder){
4488 FieldBase f = TypeManager.GetField (FieldInfo);
4490 f.status |= Field.Status.ASSIGNED;
4494 public void AddressOf (EmitContext ec, AddressOp mode)
4496 ILGenerator ig = ec.ig;
4498 if (FieldInfo is FieldBuilder){
4499 FieldBase f = TypeManager.GetField (FieldInfo);
4500 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4501 ig.Emit (OpCodes.Volatile);
4504 if (FieldInfo is FieldBuilder){
4505 FieldBase f = TypeManager.GetField (FieldInfo);
4507 if ((mode & AddressOp.Store) != 0)
4508 f.status |= Field.Status.ASSIGNED;
4509 if ((mode & AddressOp.Load) != 0)
4510 f.status |= Field.Status.USED;
4514 // Handle initonly fields specially: make a copy and then
4515 // get the address of the copy.
4517 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4521 local = ig.DeclareLocal (type);
4522 ig.Emit (OpCodes.Stloc, local);
4523 ig.Emit (OpCodes.Ldloca, local);
4527 if (FieldInfo.IsStatic)
4528 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4530 if (instance_expr is IMemoryLocation)
4531 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4533 instance_expr.Emit (ec);
4534 ig.Emit (OpCodes.Ldflda, FieldInfo);
4540 /// Expression that evaluates to a Property. The Assign class
4541 /// might set the 'Value' expression if we are in an assignment.
4543 /// This is not an LValue because we need to re-write the expression, we
4544 /// can not take data from the stack and store it.
4546 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4547 public readonly PropertyInfo PropertyInfo;
4549 MethodInfo getter, setter;
4551 public ArrayList PropertyArgs;
4553 Expression instance_expr;
4555 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4558 eclass = ExprClass.PropertyAccess;
4559 PropertyArgs = new ArrayList();
4563 type = TypeManager.TypeToCoreType (pi.PropertyType);
4565 ResolveAccessors (ec);
4568 public string Name {
4570 return PropertyInfo.Name;
4574 public bool IsInstance {
4580 public bool IsStatic {
4586 public Type DeclaringType {
4588 return PropertyInfo.DeclaringType;
4593 // The instance expression associated with this expression
4595 public Expression InstanceExpression {
4597 instance_expr = value;
4601 return instance_expr;
4605 public bool VerifyAssignable ()
4607 if (!PropertyInfo.CanWrite){
4608 Report.Error (200, loc,
4609 "The property '" + PropertyInfo.Name +
4610 "' can not be assigned to, as it has not set accessor");
4617 void ResolveAccessors (EmitContext ec)
4619 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4620 MemberInfo [] group;
4622 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4623 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4626 // The first method is the closest to us
4628 if (group != null && group.Length > 0){
4629 getter = (MethodInfo) group [0];
4631 if (getter.IsStatic)
4636 // The first method is the closest to us
4638 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4639 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4640 if (group != null && group.Length > 0){
4641 setter = (MethodInfo) group [0];
4642 if (setter.IsStatic)
4647 override public Expression DoResolve (EmitContext ec)
4649 if (getter == null){
4650 Report.Error (30524, loc,
4651 "The property '" + PropertyInfo.Name +
4652 "' can not be used in " +
4653 "this context because it lacks a get accessor");
4657 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4658 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4662 if (instance_expr != null) {
4663 instance_expr = instance_expr.DoResolve (ec);
4664 if (instance_expr == null)
4671 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4673 if (setter == null){
4674 Report.Error (30526, loc,
4675 "The property '" + PropertyInfo.Name +
4676 "' can not be used in " +
4677 "this context because it lacks a set accessor");
4681 if (instance_expr != null) {
4682 instance_expr = instance_expr.DoResolve (ec);
4683 if (instance_expr == null)
4690 override public void Emit (EmitContext ec)
4693 // Special case: length of single dimension array property is turned into ldlen
4695 if ((getter == TypeManager.system_int_array_get_length) ||
4696 (getter == TypeManager.int_array_get_length)){
4697 Type iet = instance_expr.Type;
4700 // System.Array.Length can be called, but the Type does not
4701 // support invoking GetArrayRank, so test for that case first
4703 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4704 instance_expr.Emit (ec);
4705 ec.ig.Emit (OpCodes.Ldlen);
4709 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4713 // Implements the IAssignMethod interface for assignments
4715 public void EmitAssign (EmitContext ec, Expression source)
4717 Argument arg = new Argument (source, Argument.AType.Expression);
4718 ArrayList args = new ArrayList ();
4721 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4724 override public void EmitStatement (EmitContext ec)
4727 ec.ig.Emit (OpCodes.Pop);
4732 /// Fully resolved expression that evaluates to an Event
4734 public class EventExpr : Expression, IMemberExpr {
4735 public readonly EventInfo EventInfo;
4736 public Expression instance_expr;
4739 MethodInfo add_accessor, remove_accessor;
4741 public EventExpr (EventInfo ei, Location loc)
4745 eclass = ExprClass.EventAccess;
4747 add_accessor = TypeManager.GetAddMethod (ei);
4748 remove_accessor = TypeManager.GetRemoveMethod (ei);
4750 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4753 if (EventInfo is MyEventBuilder)
4754 type = ((MyEventBuilder) EventInfo).EventType;
4756 type = EventInfo.EventHandlerType;
4759 public string Name {
4761 return EventInfo.Name;
4765 public bool IsInstance {
4771 public bool IsStatic {
4777 public Type DeclaringType {
4779 return EventInfo.DeclaringType;
4783 public Expression InstanceExpression {
4785 return instance_expr;
4789 instance_expr = value;
4793 Expression field_expr = null;
4795 public override Expression DoResolve (EmitContext ec)
4797 if (instance_expr != null) {
4798 instance_expr = instance_expr.DoResolve (ec);
4799 if (instance_expr == null)
4803 if (this.DeclaringType == ec.ContainerType) {
4804 MemberInfo mi = GetFieldFromEvent (this);
4807 field_expr = ExprClassFromMemberInfo (ec, mi, loc);
4808 ((FieldExpr) field_expr).InstanceExpression = instance_expr;
4809 field_expr = field_expr.DoResolve (ec);
4810 if (field_expr == null)
4817 public override void Emit (EmitContext ec)
4819 if (field_expr != null)
4820 field_expr.Emit (ec);
4823 public void EmitAddOrRemove (EmitContext ec, Expression source)
4825 Expression handler = ((Binary) source).Right;
4827 Argument arg = new Argument (handler, Argument.AType.Expression);
4828 ArrayList args = new ArrayList ();
4832 if (((Binary) source).Oper == Binary.Operator.Addition)
4833 Invocation.EmitCall (
4834 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4836 Invocation.EmitCall (
4837 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);