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 target_type)
947 Type expr_type = expr.Type;
950 // This is the boxed case.
952 if (target_type == TypeManager.object_type) {
953 if ((expr_type.IsClass) ||
954 (expr_type.IsValueType) ||
955 (expr_type.IsInterface))
958 } else if (expr_type.IsSubclassOf (target_type)) {
961 // Please remember that all code below actually comes
962 // from ImplicitReferenceConversion so make sure code remains in sync
964 // from any class-type S to any interface-type T.
965 if (target_type.IsInterface) {
966 if (TypeManager.ImplementsInterface (expr_type, target_type))
970 // from any interface type S to interface-type T.
971 if (expr_type.IsInterface && target_type.IsInterface)
972 if (TypeManager.ImplementsInterface (expr_type, target_type))
975 // from an array-type S to an array-type of type T
976 if (expr_type.IsArray && target_type.IsArray) {
977 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
979 Type expr_element_type = expr_type.GetElementType ();
981 if (MyEmptyExpr == null)
982 MyEmptyExpr = new EmptyExpression ();
984 MyEmptyExpr.SetType (expr_element_type);
985 Type target_element_type = target_type.GetElementType ();
987 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
988 if (StandardConversionExists (MyEmptyExpr,
989 target_element_type))
994 // from an array-type to System.Array
995 if (expr_type.IsArray && (target_type == TypeManager.array_type))
998 // from any delegate type to System.Delegate
999 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1000 target_type == TypeManager.delegate_type)
1001 if (target_type.IsAssignableFrom (expr_type))
1004 // from any array-type or delegate type into System.ICloneable.
1005 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1006 if (target_type == TypeManager.icloneable_type)
1009 // from the null type to any reference-type.
1010 if (expr is NullLiteral && !target_type.IsValueType &&
1011 !TypeManager.IsEnumType (target_type))
1020 /// Same as StandardConversionExists except that it also looks at
1021 /// implicit user defined conversions - needed for overload resolution
1023 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1025 if (StandardConversionExists (expr, target_type) == true)
1028 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 Type expr_type = expr.Type;
1044 if (expr_type == null || expr_type == TypeManager.void_type)
1047 if (expr_type == target_type)
1050 // First numeric conversions
1052 if (expr_type == TypeManager.sbyte_type){
1054 // From sbyte to short, int, long, float, double.
1056 if ((target_type == TypeManager.int32_type) ||
1057 (target_type == TypeManager.int64_type) ||
1058 (target_type == TypeManager.double_type) ||
1059 (target_type == TypeManager.float_type) ||
1060 (target_type == TypeManager.short_type) ||
1061 (target_type == TypeManager.decimal_type))
1064 } else if (expr_type == TypeManager.byte_type){
1066 // From byte to short, ushort, int, uint, long, ulong, float, double
1068 if ((target_type == TypeManager.short_type) ||
1069 (target_type == TypeManager.ushort_type) ||
1070 (target_type == TypeManager.int32_type) ||
1071 (target_type == TypeManager.uint32_type) ||
1072 (target_type == TypeManager.uint64_type) ||
1073 (target_type == TypeManager.int64_type) ||
1074 (target_type == TypeManager.float_type) ||
1075 (target_type == TypeManager.double_type) ||
1076 (target_type == TypeManager.decimal_type))
1079 } else if (expr_type == TypeManager.short_type){
1081 // From short to int, long, float, double
1083 if ((target_type == TypeManager.int32_type) ||
1084 (target_type == TypeManager.int64_type) ||
1085 (target_type == TypeManager.double_type) ||
1086 (target_type == TypeManager.float_type) ||
1087 (target_type == TypeManager.decimal_type))
1090 } else if (expr_type == TypeManager.ushort_type){
1092 // From ushort to int, uint, long, ulong, float, double
1094 if ((target_type == TypeManager.uint32_type) ||
1095 (target_type == TypeManager.uint64_type) ||
1096 (target_type == TypeManager.int32_type) ||
1097 (target_type == TypeManager.int64_type) ||
1098 (target_type == TypeManager.double_type) ||
1099 (target_type == TypeManager.float_type) ||
1100 (target_type == TypeManager.decimal_type))
1103 } else if (expr_type == TypeManager.int32_type){
1105 // From int to long, float, double
1107 if ((target_type == TypeManager.int64_type) ||
1108 (target_type == TypeManager.double_type) ||
1109 (target_type == TypeManager.float_type) ||
1110 (target_type == TypeManager.decimal_type))
1113 } else if (expr_type == TypeManager.uint32_type){
1115 // From uint to long, ulong, float, double
1117 if ((target_type == TypeManager.int64_type) ||
1118 (target_type == TypeManager.uint64_type) ||
1119 (target_type == TypeManager.double_type) ||
1120 (target_type == TypeManager.float_type) ||
1121 (target_type == TypeManager.decimal_type))
1124 } else if ((expr_type == TypeManager.uint64_type) ||
1125 (expr_type == TypeManager.int64_type)) {
1127 // From long/ulong to float, double
1129 if ((target_type == TypeManager.double_type) ||
1130 (target_type == TypeManager.float_type) ||
1131 (target_type == TypeManager.decimal_type))
1134 } else if (expr_type == TypeManager.char_type){
1136 // From char to ushort, int, uint, long, ulong, float, double
1138 if ((target_type == TypeManager.ushort_type) ||
1139 (target_type == TypeManager.int32_type) ||
1140 (target_type == TypeManager.uint32_type) ||
1141 (target_type == TypeManager.uint64_type) ||
1142 (target_type == TypeManager.int64_type) ||
1143 (target_type == TypeManager.float_type) ||
1144 (target_type == TypeManager.double_type) ||
1145 (target_type == TypeManager.decimal_type))
1148 } else if (expr_type == TypeManager.float_type){
1152 if (target_type == TypeManager.double_type)
1156 if (ImplicitReferenceConversionExists (expr, target_type))
1159 if (expr is IntConstant){
1160 int value = ((IntConstant) expr).Value;
1162 if (target_type == TypeManager.sbyte_type){
1163 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1165 } else if (target_type == TypeManager.byte_type){
1166 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1168 } else if (target_type == TypeManager.short_type){
1169 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1171 } else if (target_type == TypeManager.ushort_type){
1172 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1174 } else if (target_type == TypeManager.uint32_type){
1177 } else if (target_type == TypeManager.uint64_type){
1179 // we can optimize this case: a positive int32
1180 // always fits on a uint64. But we need an opcode
1187 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1191 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1193 // Try the implicit constant expression conversion
1194 // from long to ulong, instead of a nice routine,
1195 // we just inline it
1197 long v = ((LongConstant) expr).Value;
1202 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1203 IntLiteral i = (IntLiteral) expr;
1209 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1216 // Used internally by FindMostEncompassedType, this is used
1217 // to avoid creating lots of objects in the tight loop inside
1218 // FindMostEncompassedType
1220 static EmptyExpression priv_fmet_param;
1223 /// Finds "most encompassed type" according to the spec (13.4.2)
1224 /// amongst the methods in the MethodGroupExpr
1226 static Type FindMostEncompassedType (ArrayList types)
1230 if (priv_fmet_param == null)
1231 priv_fmet_param = new EmptyExpression ();
1233 foreach (Type t in types){
1234 priv_fmet_param.SetType (t);
1241 if (StandardConversionExists (priv_fmet_param, best))
1249 // Used internally by FindMostEncompassingType, this is used
1250 // to avoid creating lots of objects in the tight loop inside
1251 // FindMostEncompassingType
1253 static EmptyExpression priv_fmee_ret;
1256 /// Finds "most encompassing type" according to the spec (13.4.2)
1257 /// amongst the types in the given set
1259 static Type FindMostEncompassingType (ArrayList types)
1263 if (priv_fmee_ret == null)
1264 priv_fmee_ret = new EmptyExpression ();
1266 foreach (Type t in types){
1267 priv_fmee_ret.SetType (best);
1274 if (StandardConversionExists (priv_fmee_ret, t))
1282 // Used to avoid creating too many objects
1284 static EmptyExpression priv_fms_expr;
1287 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1288 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1289 /// for explicit and implicit conversion operators.
1291 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1292 bool apply_explicit_conv_rules,
1295 ArrayList src_types_set = new ArrayList ();
1297 if (priv_fms_expr == null)
1298 priv_fms_expr = new EmptyExpression ();
1301 // If any operator converts from S then Sx = S
1303 Type source_type = source.Type;
1304 foreach (MethodBase mb in me.Methods){
1305 ParameterData pd = Invocation.GetParameterData (mb);
1306 Type param_type = pd.ParameterType (0);
1308 if (param_type == source_type)
1311 if (apply_explicit_conv_rules) {
1314 // Find the set of applicable user-defined conversion operators, U. This set
1316 // user-defined implicit or explicit conversion operators declared by
1317 // the classes or structs in D that convert from a type encompassing
1318 // or encompassed by S to a type encompassing or encompassed by T
1320 priv_fms_expr.SetType (param_type);
1321 if (StandardConversionExists (priv_fms_expr, source_type))
1322 src_types_set.Add (param_type);
1324 if (StandardConversionExists (source, param_type))
1325 src_types_set.Add (param_type);
1329 // Only if S is encompassed by param_type
1331 if (StandardConversionExists (source, param_type))
1332 src_types_set.Add (param_type);
1337 // Explicit Conv rules
1339 if (apply_explicit_conv_rules) {
1340 ArrayList candidate_set = new ArrayList ();
1342 foreach (Type param_type in src_types_set){
1343 if (StandardConversionExists (source, param_type))
1344 candidate_set.Add (param_type);
1347 if (candidate_set.Count != 0)
1348 return FindMostEncompassedType (candidate_set);
1354 if (apply_explicit_conv_rules)
1355 return FindMostEncompassingType (src_types_set);
1357 return FindMostEncompassedType (src_types_set);
1361 // Useful in avoiding proliferation of objects
1363 static EmptyExpression priv_fmt_expr;
1366 /// Finds the most specific target Tx according to section 13.4.4
1368 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1369 bool apply_explicit_conv_rules,
1372 ArrayList tgt_types_set = new ArrayList ();
1374 if (priv_fmt_expr == null)
1375 priv_fmt_expr = new EmptyExpression ();
1378 // If any operator converts to T then Tx = T
1380 foreach (MethodInfo mi in me.Methods){
1381 Type ret_type = mi.ReturnType;
1383 if (ret_type == target)
1386 if (apply_explicit_conv_rules) {
1389 // Find the set of applicable user-defined conversion operators, U.
1391 // This set consists of the
1392 // user-defined implicit or explicit conversion operators declared by
1393 // the classes or structs in D that convert from a type encompassing
1394 // or encompassed by S to a type encompassing or encompassed by T
1396 priv_fms_expr.SetType (ret_type);
1397 if (StandardConversionExists (priv_fms_expr, target))
1398 tgt_types_set.Add (ret_type);
1400 priv_fms_expr.SetType (target);
1401 if (StandardConversionExists (priv_fms_expr, ret_type))
1402 tgt_types_set.Add (ret_type);
1406 // Only if T is encompassed by param_type
1408 priv_fms_expr.SetType (ret_type);
1409 if (StandardConversionExists (priv_fms_expr, target))
1410 tgt_types_set.Add (ret_type);
1415 // Explicit conv rules
1417 if (apply_explicit_conv_rules) {
1418 ArrayList candidate_set = new ArrayList ();
1420 foreach (Type ret_type in tgt_types_set){
1421 priv_fmt_expr.SetType (ret_type);
1423 if (StandardConversionExists (priv_fmt_expr, target))
1424 candidate_set.Add (ret_type);
1427 if (candidate_set.Count != 0)
1428 return FindMostEncompassingType (candidate_set);
1432 // Okay, final case !
1434 if (apply_explicit_conv_rules)
1435 return FindMostEncompassedType (tgt_types_set);
1437 return FindMostEncompassingType (tgt_types_set);
1441 /// User-defined Implicit conversions
1443 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1444 Type target, Location loc)
1446 return UserDefinedConversion (ec, source, target, loc, false);
1450 /// User-defined Explicit conversions
1452 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1453 Type target, Location loc)
1455 return UserDefinedConversion (ec, source, target, loc, true);
1459 /// Computes the MethodGroup for the user-defined conversion
1460 /// operators from source_type to target_type. 'look_for_explicit'
1461 /// controls whether we should also include the list of explicit
1464 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1465 Type source_type, Type target_type,
1466 Location loc, bool look_for_explicit)
1468 Expression mg1 = null, mg2 = null;
1469 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1473 // FIXME : How does the False operator come into the picture ?
1474 // This doesn't look complete and very correct !
1476 if (target_type == TypeManager.bool_type && !look_for_explicit)
1477 op_name = "op_True";
1479 op_name = "op_Implicit";
1481 MethodGroupExpr union3;
1483 mg1 = MethodLookup (ec, source_type, op_name, loc);
1484 if (source_type.BaseType != null)
1485 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1488 union3 = (MethodGroupExpr) mg2;
1489 else if (mg2 == null)
1490 union3 = (MethodGroupExpr) mg1;
1492 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1494 mg1 = MethodLookup (ec, target_type, op_name, loc);
1497 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1499 union3 = (MethodGroupExpr) mg1;
1502 if (target_type.BaseType != null)
1503 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1507 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1509 union3 = (MethodGroupExpr) mg1;
1512 MethodGroupExpr union4 = null;
1514 if (look_for_explicit) {
1515 op_name = "op_Explicit";
1517 mg5 = MemberLookup (ec, source_type, op_name, loc);
1518 if (source_type.BaseType != null)
1519 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1521 mg7 = MemberLookup (ec, target_type, op_name, loc);
1522 if (target_type.BaseType != null)
1523 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1525 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1526 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1528 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1531 return Invocation.MakeUnionSet (union3, union4, loc);
1535 /// User-defined conversions
1537 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1538 Type target, Location loc,
1539 bool look_for_explicit)
1541 MethodGroupExpr union;
1542 Type source_type = source.Type;
1543 MethodBase method = null;
1545 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1549 Type most_specific_source, most_specific_target;
1552 foreach (MethodBase m in union.Methods){
1553 Console.WriteLine ("Name: " + m.Name);
1554 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1558 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1559 if (most_specific_source == null)
1562 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1563 if (most_specific_target == null)
1568 foreach (MethodBase mb in union.Methods){
1569 ParameterData pd = Invocation.GetParameterData (mb);
1570 MethodInfo mi = (MethodInfo) mb;
1572 if (pd.ParameterType (0) == most_specific_source &&
1573 mi.ReturnType == most_specific_target) {
1579 if (method == null || count > 1)
1584 // This will do the conversion to the best match that we
1585 // found. Now we need to perform an implict standard conversion
1586 // if the best match was not the type that we were requested
1589 if (look_for_explicit)
1590 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1592 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1598 e = new UserCast ((MethodInfo) method, source, loc);
1599 if (e.Type != target){
1600 if (!look_for_explicit)
1601 e = ConvertImplicitStandard (ec, e, target, loc);
1603 e = ConvertExplicitStandard (ec, e, target, loc);
1609 /// Converts implicitly the resolved expression 'expr' into the
1610 /// 'target_type'. It returns a new expression that can be used
1611 /// in a context that expects a 'target_type'.
1613 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1614 Type target_type, Location loc)
1616 Type expr_type = expr.Type;
1619 if (expr_type == target_type)
1622 if (target_type == null)
1623 throw new Exception ("Target type is null");
1625 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1629 e = ImplicitUserConversion (ec, expr, target_type, loc);
1633 e = RuntimeConversion (ec, expr, target_type, loc);
1641 /// Converts the resolved expression 'expr' into the
1642 /// 'target_type' using the Microsoft.VisualBasic runtime.
1643 /// It returns a new expression that can be used
1644 /// in a context that expects a 'target_type'.
1646 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1652 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1653 args = new ArrayList();
1654 arg = new Argument (expr, Argument.AType.Expression);
1656 e = (Expression) new Invocation (etmp, args, loc);
1661 static public bool RuntimeConversionExists (EmitContext ec, Expression expr, Type target_type)
1663 return (RuntimeConversion (ec, expr, target_type,Location.Null)) != null;
1666 static public Expression RuntimeConversion (EmitContext ec, Expression expr,
1667 Type target_type, Location loc)
1669 Type expr_type = expr.Type;
1670 TypeCode dest_type = Type.GetTypeCode (target_type);
1671 TypeCode src_type = Type.GetTypeCode (expr_type);
1672 Expression e = null;
1674 // VB.NET Objects can be converted to anything by default
1675 // unless, that is, an exception at runtime blows it all
1676 if (src_type == TypeCode.Object) {
1677 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
1678 Cast ce = new Cast (cast_type, expr, loc);
1679 return ce.Resolve (ec);
1682 switch (dest_type) {
1683 case TypeCode.String:
1685 case TypeCode.SByte:
1687 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
1689 case TypeCode.UInt16:
1690 case TypeCode.Int16:
1691 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
1693 case TypeCode.UInt32:
1694 case TypeCode.Int32:
1695 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
1697 case TypeCode.UInt64:
1698 case TypeCode.Int64:
1699 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
1702 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
1704 case TypeCode.Single:
1705 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
1707 case TypeCode.Double:
1708 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
1710 case TypeCode.Boolean:
1711 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
1713 case TypeCode.DateTime:
1714 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
1716 case TypeCode.Decimal:
1717 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
1719 case TypeCode.Object:
1720 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
1725 case TypeCode.Int32:
1726 case TypeCode.UInt32:
1728 case TypeCode.String:
1729 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
1731 case TypeCode.Object:
1732 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
1737 case TypeCode.Int16:
1738 case TypeCode.UInt16:
1740 case TypeCode.String:
1741 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
1743 case TypeCode.Object:
1744 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
1749 // Ok, this *is* broken
1750 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
1754 // We must examine separately some types that
1755 // don't have a TypeCode but are supported
1757 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
1758 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
1765 /// Attempts to apply the 'Standard Implicit
1766 /// Conversion' rules to the expression 'expr' into
1767 /// the 'target_type'. It returns a new expression
1768 /// that can be used in a context that expects a
1771 /// This is different from 'ConvertImplicit' in that the
1772 /// user defined implicit conversions are excluded.
1774 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1775 Type target_type, Location loc)
1777 Type expr_type = expr.Type;
1780 if (expr_type == target_type)
1783 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1787 e = ImplicitReferenceConversion (expr, target_type);
1791 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1792 IntLiteral i = (IntLiteral) expr;
1795 return new EmptyCast (expr, target_type);
1799 if (expr_type.IsPointer){
1800 if (target_type == TypeManager.void_ptr_type)
1801 return new EmptyCast (expr, target_type);
1804 // yep, comparing pointer types cant be done with
1805 // t1 == t2, we have to compare their element types.
1807 if (target_type.IsPointer){
1808 if (target_type.GetElementType()==expr_type.GetElementType())
1813 if (target_type.IsPointer){
1814 if (expr is NullLiteral)
1815 return new EmptyCast (expr, target_type);
1823 /// Attemps to perform an implict constant conversion of the IntConstant
1824 /// into a different data type using casts (See Implicit Constant
1825 /// Expression Conversions)
1827 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1829 int value = ic.Value;
1832 // FIXME: This could return constants instead of EmptyCasts
1834 if (target_type == TypeManager.sbyte_type){
1835 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1836 return new SByteConstant ((sbyte) value);
1837 } else if (target_type == TypeManager.byte_type){
1838 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1839 return new ByteConstant ((byte) value);
1840 } else if (target_type == TypeManager.short_type){
1841 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1842 return new ShortConstant ((short) value);
1843 } else if (target_type == TypeManager.ushort_type){
1844 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1845 return new UShortConstant ((ushort) value);
1846 } else if (target_type == TypeManager.uint32_type){
1848 return new UIntConstant ((uint) value);
1849 } else if (target_type == TypeManager.uint64_type){
1851 // we can optimize this case: a positive int32
1852 // always fits on a uint64. But we need an opcode
1856 return new ULongConstant ((ulong) value);
1859 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1860 Type underlying = TypeManager.EnumToUnderlying (target_type);
1861 Constant e = (Constant) ic;
1864 // Possibly, we need to create a different 0 literal before passing
1867 if (underlying == TypeManager.int64_type)
1868 e = new LongLiteral (0);
1869 else if (underlying == TypeManager.uint64_type)
1870 e = new ULongLiteral (0);
1872 return new EnumConstant (e, target_type);
1877 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1879 string msg = "Cannot convert implicitly from '"+
1880 TypeManager.MonoBASIC_Name (source) + "' to '" +
1881 TypeManager.MonoBASIC_Name (target) + "'";
1883 Report.Error (29, loc, msg);
1887 /// Attemptes to implicityly convert 'target' into 'type', using
1888 /// ConvertImplicit. If there is no implicit conversion, then
1889 /// an error is signaled
1891 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1892 Type target_type, Location loc)
1896 e = ConvertImplicit (ec, source, target_type, loc);
1900 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1901 Report.Error (664, loc,
1902 "Double literal cannot be implicitly converted to " +
1903 "float type, use F suffix to create a float literal");
1906 Error_CannotConvertImplicit (loc, source.Type, target_type);
1912 /// Performs the explicit numeric conversions
1914 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1916 Type expr_type = expr.Type;
1919 // If we have an enumeration, extract the underlying type,
1920 // use this during the comparison, but wrap around the original
1923 Type real_target_type = target_type;
1925 if (TypeManager.IsEnumType (real_target_type))
1926 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1928 if (StandardConversionExists (expr, real_target_type)){
1929 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1931 if (real_target_type != target_type)
1932 return new EmptyCast (ce, target_type);
1936 if (expr_type == TypeManager.sbyte_type){
1938 // From sbyte to byte, ushort, uint, ulong, char
1940 if (real_target_type == TypeManager.byte_type)
1941 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1942 if (real_target_type == TypeManager.ushort_type)
1943 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1944 if (real_target_type == TypeManager.uint32_type)
1945 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1946 if (real_target_type == TypeManager.uint64_type)
1947 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1948 if (real_target_type == TypeManager.char_type)
1949 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1950 } else if (expr_type == TypeManager.byte_type){
1952 // From byte to sbyte and char
1954 if (real_target_type == TypeManager.sbyte_type)
1955 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1956 if (real_target_type == TypeManager.char_type)
1957 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1958 } else if (expr_type == TypeManager.short_type){
1960 // From short to sbyte, byte, ushort, uint, ulong, char
1962 if (real_target_type == TypeManager.sbyte_type)
1963 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1964 if (real_target_type == TypeManager.byte_type)
1965 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1966 if (real_target_type == TypeManager.ushort_type)
1967 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1968 if (real_target_type == TypeManager.uint32_type)
1969 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1970 if (real_target_type == TypeManager.uint64_type)
1971 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1972 if (real_target_type == TypeManager.char_type)
1973 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1974 } else if (expr_type == TypeManager.ushort_type){
1976 // From ushort to sbyte, byte, short, char
1978 if (real_target_type == TypeManager.sbyte_type)
1979 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1980 if (real_target_type == TypeManager.byte_type)
1981 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1982 if (real_target_type == TypeManager.short_type)
1983 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1984 if (real_target_type == TypeManager.char_type)
1985 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1986 } else if (expr_type == TypeManager.int32_type){
1988 // From int to sbyte, byte, short, ushort, uint, ulong, char
1990 if (real_target_type == TypeManager.sbyte_type)
1991 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1992 if (real_target_type == TypeManager.byte_type)
1993 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1994 if (real_target_type == TypeManager.short_type)
1995 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1996 if (real_target_type == TypeManager.ushort_type)
1997 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1998 if (real_target_type == TypeManager.uint32_type)
1999 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2000 if (real_target_type == TypeManager.uint64_type)
2001 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2002 if (real_target_type == TypeManager.char_type)
2003 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2004 } else if (expr_type == TypeManager.uint32_type){
2006 // From uint to sbyte, byte, short, ushort, int, char
2008 if (real_target_type == TypeManager.sbyte_type)
2009 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2010 if (real_target_type == TypeManager.byte_type)
2011 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2012 if (real_target_type == TypeManager.short_type)
2013 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2014 if (real_target_type == TypeManager.ushort_type)
2015 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2016 if (real_target_type == TypeManager.int32_type)
2017 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2018 if (real_target_type == TypeManager.char_type)
2019 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2020 } else if (expr_type == TypeManager.int64_type){
2022 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2024 if (real_target_type == TypeManager.sbyte_type)
2025 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2026 if (real_target_type == TypeManager.byte_type)
2027 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2028 if (real_target_type == TypeManager.short_type)
2029 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2030 if (real_target_type == TypeManager.ushort_type)
2031 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2032 if (real_target_type == TypeManager.int32_type)
2033 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2034 if (real_target_type == TypeManager.uint32_type)
2035 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2036 if (real_target_type == TypeManager.uint64_type)
2037 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2038 if (real_target_type == TypeManager.char_type)
2039 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2040 } else if (expr_type == TypeManager.uint64_type){
2042 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2044 if (real_target_type == TypeManager.sbyte_type)
2045 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2046 if (real_target_type == TypeManager.byte_type)
2047 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2048 if (real_target_type == TypeManager.short_type)
2049 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2050 if (real_target_type == TypeManager.ushort_type)
2051 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2052 if (real_target_type == TypeManager.int32_type)
2053 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2054 if (real_target_type == TypeManager.uint32_type)
2055 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2056 if (real_target_type == TypeManager.int64_type)
2057 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2058 if (real_target_type == TypeManager.char_type)
2059 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2060 } else if (expr_type == TypeManager.char_type){
2062 // From char to sbyte, byte, short
2064 if (real_target_type == TypeManager.sbyte_type)
2065 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2066 if (real_target_type == TypeManager.byte_type)
2067 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2068 if (real_target_type == TypeManager.short_type)
2069 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2070 } else if (expr_type == TypeManager.float_type){
2072 // From float to sbyte, byte, short,
2073 // ushort, int, uint, long, ulong, char
2076 if (real_target_type == TypeManager.sbyte_type)
2077 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2078 if (real_target_type == TypeManager.byte_type)
2079 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2080 if (real_target_type == TypeManager.short_type)
2081 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2082 if (real_target_type == TypeManager.ushort_type)
2083 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2084 if (real_target_type == TypeManager.int32_type)
2085 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2086 if (real_target_type == TypeManager.uint32_type)
2087 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2088 if (real_target_type == TypeManager.int64_type)
2089 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2090 if (real_target_type == TypeManager.uint64_type)
2091 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2092 if (real_target_type == TypeManager.char_type)
2093 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2094 } else if (expr_type == TypeManager.double_type){
2096 // From double to byte, byte, short,
2097 // ushort, int, uint, long, ulong,
2098 // char, float or decimal
2100 if (real_target_type == TypeManager.sbyte_type)
2101 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2102 if (real_target_type == TypeManager.byte_type)
2103 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2104 if (real_target_type == TypeManager.short_type)
2105 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2106 if (real_target_type == TypeManager.ushort_type)
2107 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2108 if (real_target_type == TypeManager.int32_type)
2109 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2110 if (real_target_type == TypeManager.uint32_type)
2111 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2112 if (real_target_type == TypeManager.int64_type)
2113 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2114 if (real_target_type == TypeManager.uint64_type)
2115 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2116 if (real_target_type == TypeManager.char_type)
2117 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2118 if (real_target_type == TypeManager.float_type)
2119 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2122 // decimal is taken care of by the op_Explicit methods.
2128 /// Returns whether an explicit reference conversion can be performed
2129 /// from source_type to target_type
2131 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2133 bool target_is_value_type = target_type.IsValueType;
2135 if (source_type == target_type)
2139 // From object to any reference type
2141 if (source_type == TypeManager.object_type && !target_is_value_type)
2145 // From any class S to any class-type T, provided S is a base class of T
2147 if (target_type.IsSubclassOf (source_type))
2151 // From any interface type S to any interface T provided S is not derived from T
2153 if (source_type.IsInterface && target_type.IsInterface){
2154 if (!target_type.IsSubclassOf (source_type))
2159 // From any class type S to any interface T, provided S is not sealed
2160 // and provided S does not implement T.
2162 if (target_type.IsInterface && !source_type.IsSealed &&
2163 !TypeManager.ImplementsInterface (source_type, target_type))
2167 // From any interface-type S to to any class type T, provided T is not
2168 // sealed, or provided T implements S.
2170 if (source_type.IsInterface &&
2171 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2175 // From an array type S with an element type Se to an array type T with an
2176 // element type Te provided all the following are true:
2177 // * S and T differe only in element type, in other words, S and T
2178 // have the same number of dimensions.
2179 // * Both Se and Te are reference types
2180 // * An explicit referenc conversions exist from Se to Te
2182 if (source_type.IsArray && target_type.IsArray) {
2183 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2185 Type source_element_type = source_type.GetElementType ();
2186 Type target_element_type = target_type.GetElementType ();
2188 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2189 if (ExplicitReferenceConversionExists (source_element_type,
2190 target_element_type))
2196 // From System.Array to any array-type
2197 if (source_type == TypeManager.array_type &&
2198 target_type.IsArray){
2203 // From System delegate to any delegate-type
2205 if (source_type == TypeManager.delegate_type &&
2206 target_type.IsSubclassOf (TypeManager.delegate_type))
2210 // From ICloneable to Array or Delegate types
2212 if (source_type == TypeManager.icloneable_type &&
2213 (target_type == TypeManager.array_type ||
2214 target_type == TypeManager.delegate_type))
2221 /// Implements Explicit Reference conversions
2223 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2225 Type source_type = source.Type;
2226 bool target_is_value_type = target_type.IsValueType;
2229 // From object to any reference type
2231 if (source_type == TypeManager.object_type && !target_is_value_type)
2232 return new ClassCast (source, target_type);
2236 // From any class S to any class-type T, provided S is a base class of T
2238 if (target_type.IsSubclassOf (source_type))
2239 return new ClassCast (source, target_type);
2242 // From any interface type S to any interface T provided S is not derived from T
2244 if (source_type.IsInterface && target_type.IsInterface){
2245 if (TypeManager.ImplementsInterface (source_type, target_type))
2248 return new ClassCast (source, target_type);
2252 // From any class type S to any interface T, provides S is not sealed
2253 // and provided S does not implement T.
2255 if (target_type.IsInterface && !source_type.IsSealed) {
2256 if (TypeManager.ImplementsInterface (source_type, target_type))
2259 return new ClassCast (source, target_type);
2264 // From any interface-type S to to any class type T, provided T is not
2265 // sealed, or provided T implements S.
2267 if (source_type.IsInterface) {
2268 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2269 return new ClassCast (source, target_type);
2274 // From an array type S with an element type Se to an array type T with an
2275 // element type Te provided all the following are true:
2276 // * S and T differe only in element type, in other words, S and T
2277 // have the same number of dimensions.
2278 // * Both Se and Te are reference types
2279 // * An explicit referenc conversions exist from Se to Te
2281 if (source_type.IsArray && target_type.IsArray) {
2282 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2284 Type source_element_type = source_type.GetElementType ();
2285 Type target_element_type = target_type.GetElementType ();
2287 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2288 if (ExplicitReferenceConversionExists (source_element_type,
2289 target_element_type))
2290 return new ClassCast (source, target_type);
2295 // From System.Array to any array-type
2296 if (source_type == TypeManager.array_type &&
2297 target_type.IsArray) {
2298 return new ClassCast (source, target_type);
2302 // From System delegate to any delegate-type
2304 if (source_type == TypeManager.delegate_type &&
2305 target_type.IsSubclassOf (TypeManager.delegate_type))
2306 return new ClassCast (source, target_type);
2309 // From ICloneable to Array or Delegate types
2311 if (source_type == TypeManager.icloneable_type &&
2312 (target_type == TypeManager.array_type ||
2313 target_type == TypeManager.delegate_type))
2314 return new ClassCast (source, target_type);
2320 /// Performs an explicit conversion of the expression 'expr' whose
2321 /// type is expr.Type to 'target_type'.
2323 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2324 Type target_type, Location loc)
2326 Type expr_type = expr.Type;
2327 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2332 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2337 // Unboxing conversion.
2339 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2340 return new UnboxCast (expr, target_type);
2345 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2349 // FIXME: Is there any reason we should have EnumConstant
2350 // dealt with here instead of just using always the
2351 // UnderlyingSystemType to wrap the type?
2353 if (expr is EnumConstant)
2354 e = ((EnumConstant) expr).Child;
2356 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2359 Expression t = ConvertImplicit (ec, e, target_type, loc);
2363 t = ConvertNumericExplicit (ec, e, target_type, loc);
2367 t = RuntimeConversion (ec, e, target_type, loc);
2371 Error_CannotConvertType (loc, expr_type, target_type);
2375 ne = ConvertReferenceExplicit (expr, target_type);
2380 if (target_type.IsPointer){
2381 if (expr_type.IsPointer)
2382 return new EmptyCast (expr, target_type);
2384 if (expr_type == TypeManager.sbyte_type ||
2385 expr_type == TypeManager.byte_type ||
2386 expr_type == TypeManager.short_type ||
2387 expr_type == TypeManager.ushort_type ||
2388 expr_type == TypeManager.int32_type ||
2389 expr_type == TypeManager.uint32_type ||
2390 expr_type == TypeManager.uint64_type ||
2391 expr_type == TypeManager.int64_type)
2392 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2394 if (expr_type.IsPointer){
2395 if (target_type == TypeManager.sbyte_type ||
2396 target_type == TypeManager.byte_type ||
2397 target_type == TypeManager.short_type ||
2398 target_type == TypeManager.ushort_type ||
2399 target_type == TypeManager.int32_type ||
2400 target_type == TypeManager.uint32_type ||
2401 target_type == TypeManager.uint64_type ||
2402 target_type == TypeManager.int64_type){
2403 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2406 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2411 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2415 // We should always be able to go from an uint32
2416 // implicitly or explicitly to the other integral
2419 throw new Exception ("Internal compiler error");
2424 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2428 ne = RuntimeConversion (ec, expr, target_type, loc);
2432 Error_CannotConvertType (loc, expr_type, target_type);
2437 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2439 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2440 Type target_type, Location l)
2442 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2447 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2451 ne = ConvertReferenceExplicit (expr, target_type);
2455 ne = RuntimeConversion (ec, expr, target_type, l);
2459 Error_CannotConvertType (l, expr.Type, target_type);
2463 static string ExprClassName (ExprClass c)
2466 case ExprClass.Invalid:
2468 case ExprClass.Value:
2470 case ExprClass.Variable:
2472 case ExprClass.Namespace:
2474 case ExprClass.Type:
2476 case ExprClass.MethodGroup:
2477 return "method group";
2478 case ExprClass.PropertyAccess:
2479 return "property access";
2480 case ExprClass.EventAccess:
2481 return "event access";
2482 case ExprClass.IndexerAccess:
2483 return "indexer access";
2484 case ExprClass.Nothing:
2487 throw new Exception ("Should not happen");
2491 /// Reports that we were expecting 'expr' to be of class 'expected'
2493 public void Error118 (string expected)
2495 string kind = "Unknown";
2497 kind = ExprClassName (eclass);
2499 Error (118, "Expression denotes a '" + kind +
2500 "' where a '" + expected + "' was expected");
2503 public void Error118 (ResolveFlags flags)
2505 ArrayList valid = new ArrayList (10);
2507 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2508 valid.Add ("variable");
2509 valid.Add ("value");
2512 if ((flags & ResolveFlags.Type) != 0)
2515 if ((flags & ResolveFlags.MethodGroup) != 0)
2516 valid.Add ("method group");
2518 if ((flags & ResolveFlags.SimpleName) != 0)
2519 valid.Add ("simple name");
2521 if (valid.Count == 0)
2522 valid.Add ("unknown");
2524 StringBuilder sb = new StringBuilder ();
2525 for (int i = 0; i < valid.Count; i++) {
2528 else if (i == valid.Count)
2530 sb.Append (valid [i]);
2533 string kind = ExprClassName (eclass);
2535 Error (119, "Expression denotes a '" + kind + "' where " +
2536 "a '" + sb.ToString () + "' was expected");
2539 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2541 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2542 TypeManager.MonoBASIC_Name (t));
2545 public static void UnsafeError (Location loc)
2547 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2551 /// Converts the IntConstant, UIntConstant, LongConstant or
2552 /// ULongConstant into the integral target_type. Notice
2553 /// that we do not return an 'Expression' we do return
2554 /// a boxed integral type.
2556 /// FIXME: Since I added the new constants, we need to
2557 /// also support conversions from CharConstant, ByteConstant,
2558 /// SByteConstant, UShortConstant, ShortConstant
2560 /// This is used by the switch statement, so the domain
2561 /// of work is restricted to the literals above, and the
2562 /// targets are int32, uint32, char, byte, sbyte, ushort,
2563 /// short, uint64 and int64
2565 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2569 if (c.Type == target_type)
2570 return ((Constant) c).GetValue ();
2573 // Make into one of the literals we handle, we dont really care
2574 // about this value as we will just return a few limited types
2576 if (c is EnumConstant)
2577 c = ((EnumConstant)c).WidenToCompilerConstant ();
2579 if (c is IntConstant){
2580 int v = ((IntConstant) c).Value;
2582 if (target_type == TypeManager.uint32_type){
2585 } else if (target_type == TypeManager.char_type){
2586 if (v >= Char.MinValue && v <= Char.MaxValue)
2588 } else if (target_type == TypeManager.byte_type){
2589 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2591 } else if (target_type == TypeManager.sbyte_type){
2592 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2594 } else if (target_type == TypeManager.short_type){
2595 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2597 } else if (target_type == TypeManager.ushort_type){
2598 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2600 } else if (target_type == TypeManager.int64_type)
2602 else if (target_type == TypeManager.uint64_type){
2608 } else if (c is UIntConstant){
2609 uint v = ((UIntConstant) c).Value;
2611 if (target_type == TypeManager.int32_type){
2612 if (v <= Int32.MaxValue)
2614 } else if (target_type == TypeManager.char_type){
2615 if (v >= Char.MinValue && v <= Char.MaxValue)
2617 } else if (target_type == TypeManager.byte_type){
2618 if (v <= Byte.MaxValue)
2620 } else if (target_type == TypeManager.sbyte_type){
2621 if (v <= SByte.MaxValue)
2623 } else if (target_type == TypeManager.short_type){
2624 if (v <= UInt16.MaxValue)
2626 } else if (target_type == TypeManager.ushort_type){
2627 if (v <= UInt16.MaxValue)
2629 } else if (target_type == TypeManager.int64_type)
2631 else if (target_type == TypeManager.uint64_type)
2634 } else if (c is LongConstant){
2635 long v = ((LongConstant) c).Value;
2637 if (target_type == TypeManager.int32_type){
2638 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2640 } else if (target_type == TypeManager.uint32_type){
2641 if (v >= 0 && v <= UInt32.MaxValue)
2643 } else if (target_type == TypeManager.char_type){
2644 if (v >= Char.MinValue && v <= Char.MaxValue)
2646 } else if (target_type == TypeManager.byte_type){
2647 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2649 } else if (target_type == TypeManager.sbyte_type){
2650 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2652 } else if (target_type == TypeManager.short_type){
2653 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2655 } else if (target_type == TypeManager.ushort_type){
2656 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2658 } else if (target_type == TypeManager.uint64_type){
2663 } else if (c is ULongConstant){
2664 ulong v = ((ULongConstant) c).Value;
2666 if (target_type == TypeManager.int32_type){
2667 if (v <= Int32.MaxValue)
2669 } else if (target_type == TypeManager.uint32_type){
2670 if (v <= UInt32.MaxValue)
2672 } else if (target_type == TypeManager.char_type){
2673 if (v >= Char.MinValue && v <= Char.MaxValue)
2675 } else if (target_type == TypeManager.byte_type){
2676 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2678 } else if (target_type == TypeManager.sbyte_type){
2679 if (v <= (int) SByte.MaxValue)
2681 } else if (target_type == TypeManager.short_type){
2682 if (v <= UInt16.MaxValue)
2684 } else if (target_type == TypeManager.ushort_type){
2685 if (v <= UInt16.MaxValue)
2687 } else if (target_type == TypeManager.int64_type){
2688 if (v <= Int64.MaxValue)
2692 } else if (c is ByteConstant){
2693 byte v = ((ByteConstant) c).Value;
2695 if (target_type == TypeManager.int32_type)
2697 else if (target_type == TypeManager.uint32_type)
2699 else if (target_type == TypeManager.char_type)
2701 else if (target_type == TypeManager.sbyte_type){
2702 if (v <= SByte.MaxValue)
2704 } else if (target_type == TypeManager.short_type)
2706 else if (target_type == TypeManager.ushort_type)
2708 else if (target_type == TypeManager.int64_type)
2710 else if (target_type == TypeManager.uint64_type)
2713 } else if (c is SByteConstant){
2714 sbyte v = ((SByteConstant) c).Value;
2716 if (target_type == TypeManager.int32_type)
2718 else if (target_type == TypeManager.uint32_type){
2721 } else if (target_type == TypeManager.char_type){
2724 } else if (target_type == TypeManager.byte_type){
2727 } else if (target_type == TypeManager.short_type)
2729 else if (target_type == TypeManager.ushort_type){
2732 } else if (target_type == TypeManager.int64_type)
2734 else if (target_type == TypeManager.uint64_type){
2739 } else if (c is ShortConstant){
2740 short v = ((ShortConstant) c).Value;
2742 if (target_type == TypeManager.int32_type){
2744 } else if (target_type == TypeManager.uint32_type){
2747 } else if (target_type == TypeManager.char_type){
2750 } else if (target_type == TypeManager.byte_type){
2751 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2753 } else if (target_type == TypeManager.sbyte_type){
2754 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2756 } else if (target_type == TypeManager.ushort_type){
2759 } else if (target_type == TypeManager.int64_type)
2761 else if (target_type == TypeManager.uint64_type)
2765 } else if (c is UShortConstant){
2766 ushort v = ((UShortConstant) c).Value;
2768 if (target_type == TypeManager.int32_type)
2770 else if (target_type == TypeManager.uint32_type)
2772 else if (target_type == TypeManager.char_type){
2773 if (v >= Char.MinValue && v <= Char.MaxValue)
2775 } else if (target_type == TypeManager.byte_type){
2776 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2778 } else if (target_type == TypeManager.sbyte_type){
2779 if (v <= SByte.MaxValue)
2781 } else if (target_type == TypeManager.short_type){
2782 if (v <= Int16.MaxValue)
2784 } else if (target_type == TypeManager.int64_type)
2786 else if (target_type == TypeManager.uint64_type)
2790 } else if (c is CharConstant){
2791 char v = ((CharConstant) c).Value;
2793 if (target_type == TypeManager.int32_type)
2795 else if (target_type == TypeManager.uint32_type)
2797 else if (target_type == TypeManager.byte_type){
2798 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2800 } else if (target_type == TypeManager.sbyte_type){
2801 if (v <= SByte.MaxValue)
2803 } else if (target_type == TypeManager.short_type){
2804 if (v <= Int16.MaxValue)
2806 } else if (target_type == TypeManager.ushort_type)
2808 else if (target_type == TypeManager.int64_type)
2810 else if (target_type == TypeManager.uint64_type)
2815 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2820 // Load the object from the pointer.
2822 public static void LoadFromPtr (ILGenerator ig, Type t)
2824 if (t == TypeManager.int32_type)
2825 ig.Emit (OpCodes.Ldind_I4);
2826 else if (t == TypeManager.uint32_type)
2827 ig.Emit (OpCodes.Ldind_U4);
2828 else if (t == TypeManager.short_type)
2829 ig.Emit (OpCodes.Ldind_I2);
2830 else if (t == TypeManager.ushort_type)
2831 ig.Emit (OpCodes.Ldind_U2);
2832 else if (t == TypeManager.char_type)
2833 ig.Emit (OpCodes.Ldind_U2);
2834 else if (t == TypeManager.byte_type)
2835 ig.Emit (OpCodes.Ldind_U1);
2836 else if (t == TypeManager.sbyte_type)
2837 ig.Emit (OpCodes.Ldind_I1);
2838 else if (t == TypeManager.uint64_type)
2839 ig.Emit (OpCodes.Ldind_I8);
2840 else if (t == TypeManager.int64_type)
2841 ig.Emit (OpCodes.Ldind_I8);
2842 else if (t == TypeManager.float_type)
2843 ig.Emit (OpCodes.Ldind_R4);
2844 else if (t == TypeManager.double_type)
2845 ig.Emit (OpCodes.Ldind_R8);
2846 else if (t == TypeManager.bool_type)
2847 ig.Emit (OpCodes.Ldind_I1);
2848 else if (t == TypeManager.intptr_type)
2849 ig.Emit (OpCodes.Ldind_I);
2850 else if (TypeManager.IsEnumType (t)) {
2851 if (t == TypeManager.enum_type)
2852 ig.Emit (OpCodes.Ldind_Ref);
2854 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2855 } else if (t.IsValueType)
2856 ig.Emit (OpCodes.Ldobj, t);
2858 ig.Emit (OpCodes.Ldind_Ref);
2862 // The stack contains the pointer and the value of type 'type'
2864 public static void StoreFromPtr (ILGenerator ig, Type type)
2866 if (TypeManager.IsEnumType (type))
2867 type = TypeManager.EnumToUnderlying (type);
2868 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2869 ig.Emit (OpCodes.Stind_I4);
2870 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2871 ig.Emit (OpCodes.Stind_I8);
2872 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2873 type == TypeManager.ushort_type)
2874 ig.Emit (OpCodes.Stind_I2);
2875 else if (type == TypeManager.float_type)
2876 ig.Emit (OpCodes.Stind_R4);
2877 else if (type == TypeManager.double_type)
2878 ig.Emit (OpCodes.Stind_R8);
2879 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2880 type == TypeManager.bool_type)
2881 ig.Emit (OpCodes.Stind_I1);
2882 else if (type == TypeManager.intptr_type)
2883 ig.Emit (OpCodes.Stind_I);
2884 else if (type.IsValueType)
2885 ig.Emit (OpCodes.Stobj, type);
2887 ig.Emit (OpCodes.Stind_Ref);
2891 // Returns the size of type 't' if known, otherwise, 0
2893 public static int GetTypeSize (Type t)
2895 t = TypeManager.TypeToCoreType (t);
2896 if (t == TypeManager.int32_type ||
2897 t == TypeManager.uint32_type ||
2898 t == TypeManager.float_type)
2900 else if (t == TypeManager.int64_type ||
2901 t == TypeManager.uint64_type ||
2902 t == TypeManager.double_type)
2904 else if (t == TypeManager.byte_type ||
2905 t == TypeManager.sbyte_type ||
2906 t == TypeManager.bool_type)
2908 else if (t == TypeManager.short_type ||
2909 t == TypeManager.char_type ||
2910 t == TypeManager.ushort_type)
2912 else if (t == TypeManager.decimal_type)
2919 // Default implementation of IAssignMethod.CacheTemporaries
2921 public void CacheTemporaries (EmitContext ec)
2925 static void Error_NegativeArrayIndex (Location loc)
2927 Report.Error (284, loc, "Can not create array with a negative size");
2931 // Converts 'source' to an int, uint, long or ulong.
2933 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2937 bool old_checked = ec.CheckState;
2938 ec.CheckState = true;
2940 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2941 if (target == null){
2942 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2943 if (target == null){
2944 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2945 if (target == null){
2946 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2948 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2952 ec.CheckState = old_checked;
2955 // Only positive constants are allowed at compile time
2957 if (target is Constant){
2958 if (target is IntConstant){
2959 if (((IntConstant) target).Value < 0){
2960 Error_NegativeArrayIndex (loc);
2965 if (target is LongConstant){
2966 if (((LongConstant) target).Value < 0){
2967 Error_NegativeArrayIndex (loc);
2980 /// This is just a base class for expressions that can
2981 /// appear on statements (invocations, object creation,
2982 /// assignments, post/pre increment and decrement). The idea
2983 /// being that they would support an extra Emition interface that
2984 /// does not leave a result on the stack.
2986 public abstract class ExpressionStatement : Expression {
2989 /// Requests the expression to be emitted in a 'statement'
2990 /// context. This means that no new value is left on the
2991 /// stack after invoking this method (constrasted with
2992 /// Emit that will always leave a value on the stack).
2994 public abstract void EmitStatement (EmitContext ec);
2998 /// This kind of cast is used to encapsulate the child
2999 /// whose type is child.Type into an expression that is
3000 /// reported to return "return_type". This is used to encapsulate
3001 /// expressions which have compatible types, but need to be dealt
3002 /// at higher levels with.
3004 /// For example, a "byte" expression could be encapsulated in one
3005 /// of these as an "unsigned int". The type for the expression
3006 /// would be "unsigned int".
3009 public class EmptyCast : Expression {
3010 protected Expression child;
3012 public EmptyCast (Expression child, Type return_type)
3014 eclass = child.eclass;
3019 public override Expression DoResolve (EmitContext ec)
3021 // This should never be invoked, we are born in fully
3022 // initialized state.
3027 public override void Emit (EmitContext ec)
3034 /// This class is used to wrap literals which belong inside Enums
3036 public class EnumConstant : Constant {
3037 public Constant Child;
3039 public EnumConstant (Constant child, Type enum_type)
3041 eclass = child.eclass;
3046 public override Expression DoResolve (EmitContext ec)
3048 // This should never be invoked, we are born in fully
3049 // initialized state.
3054 public override void Emit (EmitContext ec)
3059 public override object GetValue ()
3061 return Child.GetValue ();
3065 // Converts from one of the valid underlying types for an enumeration
3066 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3067 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3069 public Constant WidenToCompilerConstant ()
3071 Type t = TypeManager.EnumToUnderlying (Child.Type);
3072 object v = ((Constant) Child).GetValue ();;
3074 if (t == TypeManager.int32_type)
3075 return new IntConstant ((int) v);
3076 if (t == TypeManager.uint32_type)
3077 return new UIntConstant ((uint) v);
3078 if (t == TypeManager.int64_type)
3079 return new LongConstant ((long) v);
3080 if (t == TypeManager.uint64_type)
3081 return new ULongConstant ((ulong) v);
3082 if (t == TypeManager.short_type)
3083 return new ShortConstant ((short) v);
3084 if (t == TypeManager.ushort_type)
3085 return new UShortConstant ((ushort) v);
3086 if (t == TypeManager.byte_type)
3087 return new ByteConstant ((byte) v);
3088 if (t == TypeManager.sbyte_type)
3089 return new SByteConstant ((sbyte) v);
3091 throw new Exception ("Invalid enumeration underlying type: " + t);
3095 // Extracts the value in the enumeration on its native representation
3097 public object GetPlainValue ()
3099 Type t = TypeManager.EnumToUnderlying (Child.Type);
3100 object v = ((Constant) Child).GetValue ();;
3102 if (t == TypeManager.int32_type)
3104 if (t == TypeManager.uint32_type)
3106 if (t == TypeManager.int64_type)
3108 if (t == TypeManager.uint64_type)
3110 if (t == TypeManager.short_type)
3112 if (t == TypeManager.ushort_type)
3114 if (t == TypeManager.byte_type)
3116 if (t == TypeManager.sbyte_type)
3122 public override string AsString ()
3124 return Child.AsString ();
3127 public override DoubleConstant ConvertToDouble ()
3129 return Child.ConvertToDouble ();
3132 public override FloatConstant ConvertToFloat ()
3134 return Child.ConvertToFloat ();
3137 public override ULongConstant ConvertToULong ()
3139 return Child.ConvertToULong ();
3142 public override LongConstant ConvertToLong ()
3144 return Child.ConvertToLong ();
3147 public override UIntConstant ConvertToUInt ()
3149 return Child.ConvertToUInt ();
3152 public override IntConstant ConvertToInt ()
3154 return Child.ConvertToInt ();
3159 /// This kind of cast is used to encapsulate Value Types in objects.
3161 /// The effect of it is to box the value type emitted by the previous
3164 public class BoxedCast : EmptyCast {
3166 public BoxedCast (Expression expr)
3167 : base (expr, TypeManager.object_type)
3171 public override Expression DoResolve (EmitContext ec)
3173 // This should never be invoked, we are born in fully
3174 // initialized state.
3179 public override void Emit (EmitContext ec)
3183 ec.ig.Emit (OpCodes.Box, child.Type);
3187 public class UnboxCast : EmptyCast {
3188 public UnboxCast (Expression expr, Type return_type)
3189 : base (expr, return_type)
3193 public override Expression DoResolve (EmitContext ec)
3195 // This should never be invoked, we are born in fully
3196 // initialized state.
3201 public override void Emit (EmitContext ec)
3204 ILGenerator ig = ec.ig;
3207 ig.Emit (OpCodes.Unbox, t);
3209 LoadFromPtr (ig, t);
3214 /// This is used to perform explicit numeric conversions.
3216 /// Explicit numeric conversions might trigger exceptions in a checked
3217 /// context, so they should generate the conv.ovf opcodes instead of
3220 public class ConvCast : EmptyCast {
3221 public enum Mode : byte {
3222 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3224 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3225 U2_I1, U2_U1, U2_I2, U2_CH,
3226 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3227 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3228 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3229 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3230 CH_I1, CH_U1, CH_I2,
3231 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3232 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3238 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3239 : base (child, return_type)
3241 checked_state = ec.CheckState;
3245 public override Expression DoResolve (EmitContext ec)
3247 // This should never be invoked, we are born in fully
3248 // initialized state.
3253 public override void Emit (EmitContext ec)
3255 ILGenerator ig = ec.ig;
3261 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3262 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3263 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3264 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3265 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3267 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3268 case Mode.U1_CH: /* nothing */ break;
3270 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3271 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3272 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3273 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3274 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3275 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3277 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3278 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3279 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3280 case Mode.U2_CH: /* nothing */ break;
3282 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3283 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3284 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3285 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3286 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3287 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3288 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3290 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3291 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3292 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3293 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3294 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3295 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3297 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3298 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3299 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3300 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3301 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3302 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3303 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3304 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3306 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3307 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3308 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3309 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3310 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3311 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3312 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3313 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3315 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3316 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3317 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3319 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3320 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3321 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3322 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3323 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3324 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3325 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3326 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3327 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3329 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3330 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3331 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3332 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3333 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3334 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3335 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3336 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3337 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3338 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3342 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3343 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3344 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3345 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3346 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3348 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3349 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3351 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3352 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3353 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3354 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3355 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3356 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3358 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3359 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3360 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3361 case Mode.U2_CH: /* nothing */ break;
3363 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3364 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3365 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3366 case Mode.I4_U4: /* nothing */ break;
3367 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3368 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3369 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3371 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3372 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3373 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3374 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3375 case Mode.U4_I4: /* nothing */ break;
3376 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3378 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3379 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3380 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3381 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3382 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3383 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3384 case Mode.I8_U8: /* nothing */ break;
3385 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3387 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3388 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3389 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3390 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3391 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3392 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3393 case Mode.U8_I8: /* nothing */ break;
3394 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3396 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3397 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3398 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3400 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3401 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3402 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3403 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3404 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3405 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3406 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3407 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3408 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3410 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3411 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3412 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3413 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3414 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3415 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3416 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3417 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3418 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3419 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3425 public class OpcodeCast : EmptyCast {
3429 public OpcodeCast (Expression child, Type return_type, OpCode op)
3430 : base (child, return_type)
3434 second_valid = false;
3437 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3438 : base (child, return_type)
3443 second_valid = true;
3446 public override Expression DoResolve (EmitContext ec)
3448 // This should never be invoked, we are born in fully
3449 // initialized state.
3454 public override void Emit (EmitContext ec)
3465 /// This kind of cast is used to encapsulate a child and cast it
3466 /// to the class requested
3468 public class ClassCast : EmptyCast {
3469 public ClassCast (Expression child, Type return_type)
3470 : base (child, return_type)
3475 public override Expression DoResolve (EmitContext ec)
3477 // This should never be invoked, we are born in fully
3478 // initialized state.
3483 public override void Emit (EmitContext ec)
3487 ec.ig.Emit (OpCodes.Castclass, type);
3493 /// SimpleName expressions are initially formed of a single
3494 /// word and it only happens at the beginning of the expression.
3498 /// The expression will try to be bound to a Field, a Method
3499 /// group or a Property. If those fail we pass the name to our
3500 /// caller and the SimpleName is compounded to perform a type
3501 /// lookup. The idea behind this process is that we want to avoid
3502 /// creating a namespace map from the assemblies, as that requires
3503 /// the GetExportedTypes function to be called and a hashtable to
3504 /// be constructed which reduces startup time. If later we find
3505 /// that this is slower, we should create a 'NamespaceExpr' expression
3506 /// that fully participates in the resolution process.
3508 /// For example 'System.Console.WriteLine' is decomposed into
3509 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3511 /// The first SimpleName wont produce a match on its own, so it will
3513 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3515 /// System.Console will produce a TypeExpr match.
3517 /// The downside of this is that we might be hitting 'LookupType' too many
3518 /// times with this scheme.
3520 public class SimpleName : Expression, ITypeExpression {
3521 public readonly string Name;
3523 public SimpleName (string name, Location l)
3529 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3531 if (ec.IsFieldInitializer)
3534 "A field initializer cannot reference the non-static field, " +
3535 "method or property '"+name+"'");
3539 "An object reference is required " +
3540 "for the non-static field '"+name+"'");
3544 // Checks whether we are trying to access an instance
3545 // property, method or field from a static body.
3547 Expression MemberStaticCheck (EmitContext ec, Expression e)
3549 if (e is IMemberExpr){
3550 IMemberExpr member = (IMemberExpr) e;
3552 if (!member.IsStatic){
3553 Error_ObjectRefRequired (ec, loc, Name);
3561 public override Expression DoResolve (EmitContext ec)
3563 return SimpleNameResolve (ec, null, false);
3566 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3568 return SimpleNameResolve (ec, right_side, false);
3572 public Expression DoResolveAllowStatic (EmitContext ec)
3574 return SimpleNameResolve (ec, null, true);
3577 public Expression DoResolveType (EmitContext ec)
3580 // Stage 3: Lookup symbol in the various namespaces.
3582 DeclSpace ds = ec.DeclSpace;
3586 if (ec.ResolvingTypeTree){
3587 int errors = Report.Errors;
3588 Type dt = ec.DeclSpace.FindType (loc, Name);
3589 if (Report.Errors != errors)
3593 return new TypeExpr (dt, loc);
3596 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3597 return new TypeExpr (t, loc);
3601 // Stage 2 part b: Lookup up if we are an alias to a type
3604 // Since we are cheating: we only do the Alias lookup for
3605 // namespaces if the name does not include any dots in it
3608 alias_value = ec.DeclSpace.LookupAlias (Name);
3610 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3611 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3612 return new TypeExpr (t, loc);
3614 // we have alias value, but it isn't Type, so try if it's namespace
3615 return new SimpleName (alias_value, loc);
3618 // No match, maybe our parent can compose us
3619 // into something meaningful.
3624 /// 7.5.2: Simple Names.
3626 /// Local Variables and Parameters are handled at
3627 /// parse time, so they never occur as SimpleNames.
3629 /// The 'allow_static' flag is used by MemberAccess only
3630 /// and it is used to inform us that it is ok for us to
3631 /// avoid the static check, because MemberAccess might end
3632 /// up resolving the Name as a Type name and the access as
3633 /// a static type access.
3635 /// ie: Type Type; .... { Type.GetType (""); }
3637 /// Type is both an instance variable and a Type; Type.GetType
3638 /// is the static method not an instance method of type.
3640 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3642 Expression e = null;
3645 // Stage 1: Performed by the parser (binding to locals or parameters).
3647 Block current_block = ec.CurrentBlock;
3648 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
3649 LocalVariableReference var;
3651 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3653 if (right_side != null)
3654 return var.ResolveLValue (ec, right_side);
3656 return var.Resolve (ec);
3659 if (current_block != null){
3661 Parameter par = null;
3662 Parameters pars = current_block.Parameters;
3664 par = pars.GetParameterByName (Name, out idx);
3667 ParameterReference param;
3669 param = new ParameterReference (pars, idx, Name, loc);
3671 if (right_side != null)
3672 return param.ResolveLValue (ec, right_side);
3674 return param.Resolve (ec);
3679 // Stage 2: Lookup members
3683 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3684 // Hence we have two different cases
3687 DeclSpace lookup_ds = ec.DeclSpace;
3689 if (lookup_ds.TypeBuilder == null)
3692 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3697 // Classes/structs keep looking, enums break
3699 if (lookup_ds is TypeContainer)
3700 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3703 } while (lookup_ds != null);
3705 if (e == null && ec.ContainerType != null)
3706 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3708 // #52067 - Start - Trying to solve
3711 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
3712 ArrayList lookups = new ArrayList();
3713 ArrayList typelookups = new ArrayList();
3715 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
3716 e = MemberLookup(ec, type, Name, loc);
3719 typelookups.Add(type);
3721 if (lookups.Count == 1) {
3722 e = (Expression)lookups[0];
3724 if (lookups.Count > 1) {
3725 StringBuilder sb = new StringBuilder();
3726 foreach(Type type in typelookups)
3727 sb.Append("'" + type.FullName + "'");
3728 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
3737 return DoResolveType (ec);
3742 if (e is IMemberExpr) {
3743 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3747 IMemberExpr me = e as IMemberExpr;
3751 // This fails if ResolveMemberAccess() was unable to decide whether
3752 // it's a field or a type of the same name.
3753 if (!me.IsStatic && (me.InstanceExpression == null))
3756 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
3757 Maybe a grammar-related problem?
3760 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3761 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
3762 "outer type '" + me.DeclaringType + "' via nested type '" +
3763 me.InstanceExpression.Type + "'");
3767 if (right_side != null)
3768 e = e.DoResolveLValue (ec, right_side);
3770 e = e.DoResolve (ec);
3775 if (ec.IsStatic || ec.IsFieldInitializer){
3779 return MemberStaticCheck (ec, e);
3784 public override void Emit (EmitContext ec)
3787 // If this is ever reached, then we failed to
3788 // find the name as a namespace
3791 Error (30451, "The name '" + Name +
3792 "' does not exist in the class '" +
3793 ec.DeclSpace.Name + "'");
3796 public override string ToString ()
3803 /// Fully resolved expression that evaluates to a type
3805 public class TypeExpr : Expression, ITypeExpression {
3806 public TypeExpr (Type t, Location l)
3809 eclass = ExprClass.Type;
3813 public virtual Expression DoResolveType (EmitContext ec)
3818 override public Expression DoResolve (EmitContext ec)
3823 override public void Emit (EmitContext ec)
3825 throw new Exception ("Should never be called");
3828 public override string ToString ()
3830 return Type.ToString ();
3835 /// Used to create types from a fully qualified name. These are just used
3836 /// by the parser to setup the core types. A TypeLookupExpression is always
3837 /// classified as a type.
3839 public class TypeLookupExpression : TypeExpr {
3842 public TypeLookupExpression (string name) : base (null, Location.Null)
3847 public override Expression DoResolveType (EmitContext ec)
3850 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3854 public override Expression DoResolve (EmitContext ec)
3856 return DoResolveType (ec);
3859 public override void Emit (EmitContext ec)
3861 throw new Exception ("Should never be called");
3864 public override string ToString ()
3871 /// MethodGroup Expression.
3873 /// This is a fully resolved expression that evaluates to a type
3875 public class MethodGroupExpr : Expression, IMemberExpr {
3876 public MethodBase [] Methods;
3877 Expression instance_expression = null;
3878 bool is_explicit_impl = false;
3880 public MethodGroupExpr (MemberInfo [] mi, Location l)
3882 Methods = new MethodBase [mi.Length];
3883 mi.CopyTo (Methods, 0);
3884 eclass = ExprClass.MethodGroup;
3885 type = TypeManager.object_type;
3889 public MethodGroupExpr (ArrayList list, Location l)
3891 Methods = new MethodBase [list.Count];
3894 list.CopyTo (Methods, 0);
3896 foreach (MemberInfo m in list){
3897 if (!(m is MethodBase)){
3898 Console.WriteLine ("Name " + m.Name);
3899 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3905 eclass = ExprClass.MethodGroup;
3906 type = TypeManager.object_type;
3909 public Type DeclaringType {
3911 return Methods [0].DeclaringType;
3916 // 'A method group may have associated an instance expression'
3918 public Expression InstanceExpression {
3920 return instance_expression;
3924 instance_expression = value;
3928 public bool IsExplicitImpl {
3930 return is_explicit_impl;
3934 is_explicit_impl = value;
3938 public string Name {
3940 return Methods [0].Name;
3944 public bool IsInstance {
3946 foreach (MethodBase mb in Methods)
3954 public bool IsStatic {
3956 foreach (MethodBase mb in Methods)
3964 override public Expression DoResolve (EmitContext ec)
3966 if (instance_expression != null) {
3967 instance_expression = instance_expression.DoResolve (ec);
3968 if (instance_expression == null)
3975 public void ReportUsageError ()
3977 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
3978 Methods [0].Name + "()' is referenced without parentheses");
3981 override public void Emit (EmitContext ec)
3983 ReportUsageError ();
3986 bool RemoveMethods (bool keep_static)
3988 ArrayList smethods = new ArrayList ();
3990 foreach (MethodBase mb in Methods){
3991 if (mb.IsStatic == keep_static)
3995 if (smethods.Count == 0)
3998 Methods = new MethodBase [smethods.Count];
3999 smethods.CopyTo (Methods, 0);
4005 /// Removes any instance methods from the MethodGroup, returns
4006 /// false if the resulting set is empty.
4008 public bool RemoveInstanceMethods ()
4010 return RemoveMethods (true);
4014 /// Removes any static methods from the MethodGroup, returns
4015 /// false if the resulting set is empty.
4017 public bool RemoveStaticMethods ()
4019 return RemoveMethods (false);
4024 /// Fully resolved expression that evaluates to a Field
4026 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4027 public readonly FieldInfo FieldInfo;
4028 Expression instance_expr;
4030 public FieldExpr (FieldInfo fi, Location l)
4033 eclass = ExprClass.Variable;
4034 type = fi.FieldType;
4038 public string Name {
4040 return FieldInfo.Name;
4044 public bool IsInstance {
4046 return !FieldInfo.IsStatic;
4050 public bool IsStatic {
4052 return FieldInfo.IsStatic;
4056 public Type DeclaringType {
4058 return FieldInfo.DeclaringType;
4062 public Expression InstanceExpression {
4064 return instance_expr;
4068 instance_expr = value;
4072 override public Expression DoResolve (EmitContext ec)
4074 if (!FieldInfo.IsStatic){
4075 if (instance_expr == null){
4076 throw new Exception ("non-static FieldExpr without instance var\n" +
4077 "You have to assign the Instance variable\n" +
4078 "Of the FieldExpr to set this\n");
4081 // Resolve the field's instance expression while flow analysis is turned
4082 // off: when accessing a field "a.b", we must check whether the field
4083 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4084 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4085 ResolveFlags.DisableFlowAnalysis);
4086 if (instance_expr == null)
4090 // If the instance expression is a local variable or parameter.
4091 IVariable var = instance_expr as IVariable;
4092 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4098 void Report_AssignToReadonly (bool is_instance)
4103 msg = "Readonly field can not be assigned outside " +
4104 "of constructor or variable initializer";
4106 msg = "A static readonly field can only be assigned in " +
4107 "a static constructor";
4109 Report.Error (is_instance ? 191 : 198, loc, msg);
4112 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4114 IVariable var = instance_expr as IVariable;
4116 var.SetFieldAssigned (ec, FieldInfo.Name);
4118 Expression e = DoResolve (ec);
4123 if (!FieldInfo.IsInitOnly)
4127 // InitOnly fields can only be assigned in constructors
4130 if (ec.IsConstructor)
4133 Report_AssignToReadonly (true);
4138 override public void Emit (EmitContext ec)
4140 ILGenerator ig = ec.ig;
4141 bool is_volatile = false;
4143 if (FieldInfo is FieldBuilder){
4144 FieldBase f = TypeManager.GetField (FieldInfo);
4146 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4149 f.status |= Field.Status.USED;
4152 if (FieldInfo.IsStatic){
4154 ig.Emit (OpCodes.Volatile);
4156 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4158 if (instance_expr.Type.IsValueType){
4160 LocalTemporary tempo = null;
4162 if (!(instance_expr is IMemoryLocation)){
4163 tempo = new LocalTemporary (
4164 ec, instance_expr.Type);
4166 InstanceExpression.Emit (ec);
4170 ml = (IMemoryLocation) instance_expr;
4172 ml.AddressOf (ec, AddressOp.Load);
4174 instance_expr.Emit (ec);
4177 ig.Emit (OpCodes.Volatile);
4179 ig.Emit (OpCodes.Ldfld, FieldInfo);
4183 public void EmitAssign (EmitContext ec, Expression source)
4185 FieldAttributes fa = FieldInfo.Attributes;
4186 bool is_static = (fa & FieldAttributes.Static) != 0;
4187 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4188 ILGenerator ig = ec.ig;
4190 if (is_readonly && !ec.IsConstructor){
4191 Report_AssignToReadonly (!is_static);
4196 Expression instance = instance_expr;
4198 if (instance.Type.IsValueType){
4199 if (instance is IMemoryLocation){
4200 IMemoryLocation ml = (IMemoryLocation) instance;
4202 ml.AddressOf (ec, AddressOp.Store);
4204 throw new Exception ("The " + instance + " of type " +
4206 " represents a ValueType and does " +
4207 "not implement IMemoryLocation");
4213 if (FieldInfo is FieldBuilder){
4214 FieldBase f = TypeManager.GetField (FieldInfo);
4216 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4217 ig.Emit (OpCodes.Volatile);
4221 ig.Emit (OpCodes.Stsfld, FieldInfo);
4223 ig.Emit (OpCodes.Stfld, FieldInfo);
4225 if (FieldInfo is FieldBuilder){
4226 FieldBase f = TypeManager.GetField (FieldInfo);
4228 f.status |= Field.Status.ASSIGNED;
4232 public void AddressOf (EmitContext ec, AddressOp mode)
4234 ILGenerator ig = ec.ig;
4236 if (FieldInfo is FieldBuilder){
4237 FieldBase f = TypeManager.GetField (FieldInfo);
4238 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4239 ig.Emit (OpCodes.Volatile);
4242 if (FieldInfo is FieldBuilder){
4243 FieldBase f = TypeManager.GetField (FieldInfo);
4245 if ((mode & AddressOp.Store) != 0)
4246 f.status |= Field.Status.ASSIGNED;
4247 if ((mode & AddressOp.Load) != 0)
4248 f.status |= Field.Status.USED;
4252 // Handle initonly fields specially: make a copy and then
4253 // get the address of the copy.
4255 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4259 local = ig.DeclareLocal (type);
4260 ig.Emit (OpCodes.Stloc, local);
4261 ig.Emit (OpCodes.Ldloca, local);
4265 if (FieldInfo.IsStatic)
4266 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4268 if (instance_expr is IMemoryLocation)
4269 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4271 instance_expr.Emit (ec);
4272 ig.Emit (OpCodes.Ldflda, FieldInfo);
4278 /// Expression that evaluates to a Property. The Assign class
4279 /// might set the 'Value' expression if we are in an assignment.
4281 /// This is not an LValue because we need to re-write the expression, we
4282 /// can not take data from the stack and store it.
4284 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4285 public readonly PropertyInfo PropertyInfo;
4287 MethodInfo getter, setter;
4289 public ArrayList PropertyArgs;
4291 Expression instance_expr;
4293 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4296 eclass = ExprClass.PropertyAccess;
4297 PropertyArgs = new ArrayList();
4301 type = TypeManager.TypeToCoreType (pi.PropertyType);
4303 ResolveAccessors (ec);
4306 public string Name {
4308 return PropertyInfo.Name;
4312 public bool IsInstance {
4318 public bool IsStatic {
4324 public Type DeclaringType {
4326 return PropertyInfo.DeclaringType;
4331 // The instance expression associated with this expression
4333 public Expression InstanceExpression {
4335 instance_expr = value;
4339 return instance_expr;
4343 public bool VerifyAssignable ()
4345 if (!PropertyInfo.CanWrite){
4346 Report.Error (200, loc,
4347 "The property '" + PropertyInfo.Name +
4348 "' can not be assigned to, as it has not set accessor");
4355 void ResolveAccessors (EmitContext ec)
4357 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4358 MemberInfo [] group;
4360 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4361 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4364 // The first method is the closest to us
4366 if (group != null && group.Length > 0){
4367 getter = (MethodInfo) group [0];
4369 if (getter.IsStatic)
4374 // The first method is the closest to us
4376 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4377 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4378 if (group != null && group.Length > 0){
4379 setter = (MethodInfo) group [0];
4380 if (setter.IsStatic)
4385 override public Expression DoResolve (EmitContext ec)
4387 if (getter == null){
4388 Report.Error (154, loc,
4389 "The property '" + PropertyInfo.Name +
4390 "' can not be used in " +
4391 "this context because it lacks a get accessor");
4395 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4396 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4400 if (instance_expr != null) {
4401 instance_expr = instance_expr.DoResolve (ec);
4402 if (instance_expr == null)
4409 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4411 if (setter == null){
4412 Report.Error (154, loc,
4413 "The property '" + PropertyInfo.Name +
4414 "' can not be used in " +
4415 "this context because it lacks a set accessor");
4419 if (instance_expr != null) {
4420 instance_expr = instance_expr.DoResolve (ec);
4421 if (instance_expr == null)
4428 override public void Emit (EmitContext ec)
4431 // Special case: length of single dimension array property is turned into ldlen
4433 if ((getter == TypeManager.system_int_array_get_length) ||
4434 (getter == TypeManager.int_array_get_length)){
4435 Type iet = instance_expr.Type;
4438 // System.Array.Length can be called, but the Type does not
4439 // support invoking GetArrayRank, so test for that case first
4441 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4442 instance_expr.Emit (ec);
4443 ec.ig.Emit (OpCodes.Ldlen);
4447 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4451 // Implements the IAssignMethod interface for assignments
4453 public void EmitAssign (EmitContext ec, Expression source)
4455 Argument arg = new Argument (source, Argument.AType.Expression);
4456 ArrayList args = new ArrayList ();
4459 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4462 override public void EmitStatement (EmitContext ec)
4465 ec.ig.Emit (OpCodes.Pop);
4470 /// Fully resolved expression that evaluates to an Event
4472 public class EventExpr : Expression, IMemberExpr {
4473 public readonly EventInfo EventInfo;
4474 public Expression instance_expr;
4477 MethodInfo add_accessor, remove_accessor;
4479 public EventExpr (EventInfo ei, Location loc)
4483 eclass = ExprClass.EventAccess;
4485 add_accessor = TypeManager.GetAddMethod (ei);
4486 remove_accessor = TypeManager.GetRemoveMethod (ei);
4488 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4491 if (EventInfo is MyEventBuilder)
4492 type = ((MyEventBuilder) EventInfo).EventType;
4494 type = EventInfo.EventHandlerType;
4497 public string Name {
4499 return EventInfo.Name;
4503 public bool IsInstance {
4509 public bool IsStatic {
4515 public Type DeclaringType {
4517 return EventInfo.DeclaringType;
4521 public Expression InstanceExpression {
4523 return instance_expr;
4527 instance_expr = value;
4531 public override Expression DoResolve (EmitContext ec)
4533 if (instance_expr != null) {
4534 instance_expr = instance_expr.DoResolve (ec);
4535 if (instance_expr == null)
4542 public override void Emit (EmitContext ec)
4544 Report.Error (70, loc, "The event '" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4547 public void EmitAddOrRemove (EmitContext ec, Expression source)
4549 Expression handler = ((Binary) source).Right;
4551 Argument arg = new Argument (handler, Argument.AType.Expression);
4552 ArrayList args = new ArrayList ();
4556 if (((Binary) source).Oper == Binary.Operator.Addition)
4557 Invocation.EmitCall (
4558 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4560 Invocation.EmitCall (
4561 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);