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 ce.IsRuntimeCast = true;
1680 return ce.Resolve (ec);
1683 switch (dest_type) {
1684 case TypeCode.String:
1686 case TypeCode.SByte:
1688 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
1690 case TypeCode.UInt16:
1691 case TypeCode.Int16:
1692 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
1694 case TypeCode.UInt32:
1695 case TypeCode.Int32:
1696 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
1698 case TypeCode.UInt64:
1699 case TypeCode.Int64:
1700 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
1703 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
1705 case TypeCode.Single:
1706 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
1708 case TypeCode.Double:
1709 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
1711 case TypeCode.Boolean:
1712 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
1714 case TypeCode.DateTime:
1715 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
1717 case TypeCode.Decimal:
1718 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
1720 case TypeCode.Object:
1721 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
1726 case TypeCode.Int32:
1727 case TypeCode.UInt32:
1729 case TypeCode.String:
1730 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
1732 case TypeCode.Object:
1733 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
1738 case TypeCode.Int16:
1739 case TypeCode.UInt16:
1741 case TypeCode.String:
1742 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
1744 case TypeCode.Object:
1745 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
1750 // Ok, this *is* broken
1751 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
1755 // We must examine separately some types that
1756 // don't have a TypeCode but are supported
1758 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
1759 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
1766 /// Attempts to apply the 'Standard Implicit
1767 /// Conversion' rules to the expression 'expr' into
1768 /// the 'target_type'. It returns a new expression
1769 /// that can be used in a context that expects a
1772 /// This is different from 'ConvertImplicit' in that the
1773 /// user defined implicit conversions are excluded.
1775 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
1776 Type target_type, Location loc)
1778 Type expr_type = expr.Type;
1781 if (expr_type == target_type)
1784 e = ImplicitNumericConversion (ec, expr, target_type, loc);
1788 e = ImplicitReferenceConversion (expr, target_type);
1792 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1793 IntLiteral i = (IntLiteral) expr;
1796 return new EmptyCast (expr, target_type);
1800 if (expr_type.IsPointer){
1801 if (target_type == TypeManager.void_ptr_type)
1802 return new EmptyCast (expr, target_type);
1805 // yep, comparing pointer types cant be done with
1806 // t1 == t2, we have to compare their element types.
1808 if (target_type.IsPointer){
1809 if (target_type.GetElementType()==expr_type.GetElementType())
1814 if (target_type.IsPointer){
1815 if (expr is NullLiteral)
1816 return new EmptyCast (expr, target_type);
1824 /// Attemps to perform an implict constant conversion of the IntConstant
1825 /// into a different data type using casts (See Implicit Constant
1826 /// Expression Conversions)
1828 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1830 int value = ic.Value;
1833 // FIXME: This could return constants instead of EmptyCasts
1835 if (target_type == TypeManager.sbyte_type){
1836 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1837 return new SByteConstant ((sbyte) value);
1838 } else if (target_type == TypeManager.byte_type){
1839 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1840 return new ByteConstant ((byte) value);
1841 } else if (target_type == TypeManager.short_type){
1842 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1843 return new ShortConstant ((short) value);
1844 } else if (target_type == TypeManager.ushort_type){
1845 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1846 return new UShortConstant ((ushort) value);
1847 } else if (target_type == TypeManager.uint32_type){
1849 return new UIntConstant ((uint) value);
1850 } else if (target_type == TypeManager.uint64_type){
1852 // we can optimize this case: a positive int32
1853 // always fits on a uint64. But we need an opcode
1857 return new ULongConstant ((ulong) value);
1860 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1861 Type underlying = TypeManager.EnumToUnderlying (target_type);
1862 Constant e = (Constant) ic;
1865 // Possibly, we need to create a different 0 literal before passing
1868 if (underlying == TypeManager.int64_type)
1869 e = new LongLiteral (0);
1870 else if (underlying == TypeManager.uint64_type)
1871 e = new ULongLiteral (0);
1873 return new EnumConstant (e, target_type);
1878 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
1880 string msg = "Cannot convert implicitly from '"+
1881 TypeManager.MonoBASIC_Name (source) + "' to '" +
1882 TypeManager.MonoBASIC_Name (target) + "'";
1884 Report.Error (29, loc, msg);
1888 /// Attemptes to implicityly convert 'target' into 'type', using
1889 /// ConvertImplicit. If there is no implicit conversion, then
1890 /// an error is signaled
1892 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
1893 Type target_type, Location loc)
1897 e = ConvertImplicit (ec, source, target_type, loc);
1901 if (source is DoubleLiteral && target_type == TypeManager.float_type){
1902 Report.Error (664, loc,
1903 "Double literal cannot be implicitly converted to " +
1904 "float type, use F suffix to create a float literal");
1907 Error_CannotConvertImplicit (loc, source.Type, target_type);
1913 /// Performs the explicit numeric conversions
1915 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
1917 Type expr_type = expr.Type;
1920 // If we have an enumeration, extract the underlying type,
1921 // use this during the comparison, but wrap around the original
1924 Type real_target_type = target_type;
1926 if (TypeManager.IsEnumType (real_target_type))
1927 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1929 if (StandardConversionExists (expr, real_target_type)){
1930 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
1932 if (real_target_type != target_type)
1933 return new EmptyCast (ce, target_type);
1937 if (expr_type == TypeManager.sbyte_type){
1939 // From sbyte to byte, ushort, uint, ulong, char
1941 if (real_target_type == TypeManager.byte_type)
1942 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1943 if (real_target_type == TypeManager.ushort_type)
1944 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1945 if (real_target_type == TypeManager.uint32_type)
1946 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1947 if (real_target_type == TypeManager.uint64_type)
1948 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1949 if (real_target_type == TypeManager.char_type)
1950 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1951 } else if (expr_type == TypeManager.byte_type){
1953 // From byte to sbyte and char
1955 if (real_target_type == TypeManager.sbyte_type)
1956 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1957 if (real_target_type == TypeManager.char_type)
1958 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1959 } else if (expr_type == TypeManager.short_type){
1961 // From short to sbyte, byte, ushort, uint, ulong, char
1963 if (real_target_type == TypeManager.sbyte_type)
1964 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1965 if (real_target_type == TypeManager.byte_type)
1966 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1967 if (real_target_type == TypeManager.ushort_type)
1968 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1969 if (real_target_type == TypeManager.uint32_type)
1970 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1971 if (real_target_type == TypeManager.uint64_type)
1972 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1973 if (real_target_type == TypeManager.char_type)
1974 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1975 } else if (expr_type == TypeManager.ushort_type){
1977 // From ushort to sbyte, byte, short, char
1979 if (real_target_type == TypeManager.sbyte_type)
1980 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1981 if (real_target_type == TypeManager.byte_type)
1982 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1983 if (real_target_type == TypeManager.short_type)
1984 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1985 if (real_target_type == TypeManager.char_type)
1986 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1987 } else if (expr_type == TypeManager.int32_type){
1989 // From int to sbyte, byte, short, ushort, uint, ulong, char
1991 if (real_target_type == TypeManager.sbyte_type)
1992 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1993 if (real_target_type == TypeManager.byte_type)
1994 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1995 if (real_target_type == TypeManager.short_type)
1996 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1997 if (real_target_type == TypeManager.ushort_type)
1998 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1999 if (real_target_type == TypeManager.uint32_type)
2000 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2001 if (real_target_type == TypeManager.uint64_type)
2002 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2003 if (real_target_type == TypeManager.char_type)
2004 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2005 } else if (expr_type == TypeManager.uint32_type){
2007 // From uint to sbyte, byte, short, ushort, int, char
2009 if (real_target_type == TypeManager.sbyte_type)
2010 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2011 if (real_target_type == TypeManager.byte_type)
2012 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2013 if (real_target_type == TypeManager.short_type)
2014 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2015 if (real_target_type == TypeManager.ushort_type)
2016 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2017 if (real_target_type == TypeManager.int32_type)
2018 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2019 if (real_target_type == TypeManager.char_type)
2020 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2021 } else if (expr_type == TypeManager.int64_type){
2023 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2025 if (real_target_type == TypeManager.sbyte_type)
2026 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2027 if (real_target_type == TypeManager.byte_type)
2028 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2029 if (real_target_type == TypeManager.short_type)
2030 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2031 if (real_target_type == TypeManager.ushort_type)
2032 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2033 if (real_target_type == TypeManager.int32_type)
2034 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2035 if (real_target_type == TypeManager.uint32_type)
2036 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2037 if (real_target_type == TypeManager.uint64_type)
2038 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2039 if (real_target_type == TypeManager.char_type)
2040 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2041 } else if (expr_type == TypeManager.uint64_type){
2043 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2045 if (real_target_type == TypeManager.sbyte_type)
2046 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2047 if (real_target_type == TypeManager.byte_type)
2048 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2049 if (real_target_type == TypeManager.short_type)
2050 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2051 if (real_target_type == TypeManager.ushort_type)
2052 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2053 if (real_target_type == TypeManager.int32_type)
2054 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2055 if (real_target_type == TypeManager.uint32_type)
2056 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2057 if (real_target_type == TypeManager.int64_type)
2058 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2059 if (real_target_type == TypeManager.char_type)
2060 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2061 } else if (expr_type == TypeManager.char_type){
2063 // From char to sbyte, byte, short
2065 if (real_target_type == TypeManager.sbyte_type)
2066 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2067 if (real_target_type == TypeManager.byte_type)
2068 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2069 if (real_target_type == TypeManager.short_type)
2070 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2071 } else if (expr_type == TypeManager.float_type){
2073 // From float to sbyte, byte, short,
2074 // ushort, int, uint, long, ulong, char
2077 if (real_target_type == TypeManager.sbyte_type)
2078 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2079 if (real_target_type == TypeManager.byte_type)
2080 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2081 if (real_target_type == TypeManager.short_type)
2082 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2083 if (real_target_type == TypeManager.ushort_type)
2084 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2085 if (real_target_type == TypeManager.int32_type)
2086 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2087 if (real_target_type == TypeManager.uint32_type)
2088 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2089 if (real_target_type == TypeManager.int64_type)
2090 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2091 if (real_target_type == TypeManager.uint64_type)
2092 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2093 if (real_target_type == TypeManager.char_type)
2094 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2095 } else if (expr_type == TypeManager.double_type){
2097 // From double to byte, byte, short,
2098 // ushort, int, uint, long, ulong,
2099 // char, float or decimal
2101 if (real_target_type == TypeManager.sbyte_type)
2102 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2103 if (real_target_type == TypeManager.byte_type)
2104 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2105 if (real_target_type == TypeManager.short_type)
2106 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2107 if (real_target_type == TypeManager.ushort_type)
2108 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2109 if (real_target_type == TypeManager.int32_type)
2110 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2111 if (real_target_type == TypeManager.uint32_type)
2112 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2113 if (real_target_type == TypeManager.int64_type)
2114 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2115 if (real_target_type == TypeManager.uint64_type)
2116 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2117 if (real_target_type == TypeManager.char_type)
2118 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2119 if (real_target_type == TypeManager.float_type)
2120 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2123 // decimal is taken care of by the op_Explicit methods.
2129 /// Returns whether an explicit reference conversion can be performed
2130 /// from source_type to target_type
2132 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2134 bool target_is_value_type = target_type.IsValueType;
2136 if (source_type == target_type)
2140 // From object to any reference type
2142 if (source_type == TypeManager.object_type && !target_is_value_type)
2146 // From any class S to any class-type T, provided S is a base class of T
2148 if (target_type.IsSubclassOf (source_type))
2152 // From any interface type S to any interface T provided S is not derived from T
2154 if (source_type.IsInterface && target_type.IsInterface){
2155 if (!target_type.IsSubclassOf (source_type))
2160 // From any class type S to any interface T, provided S is not sealed
2161 // and provided S does not implement T.
2163 if (target_type.IsInterface && !source_type.IsSealed &&
2164 !TypeManager.ImplementsInterface (source_type, target_type))
2168 // From any interface-type S to to any class type T, provided T is not
2169 // sealed, or provided T implements S.
2171 if (source_type.IsInterface &&
2172 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2176 // From an array type S with an element type Se to an array type T with an
2177 // element type Te provided all the following are true:
2178 // * S and T differe only in element type, in other words, S and T
2179 // have the same number of dimensions.
2180 // * Both Se and Te are reference types
2181 // * An explicit referenc conversions exist from Se to Te
2183 if (source_type.IsArray && target_type.IsArray) {
2184 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2186 Type source_element_type = source_type.GetElementType ();
2187 Type target_element_type = target_type.GetElementType ();
2189 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2190 if (ExplicitReferenceConversionExists (source_element_type,
2191 target_element_type))
2197 // From System.Array to any array-type
2198 if (source_type == TypeManager.array_type &&
2199 target_type.IsArray){
2204 // From System delegate to any delegate-type
2206 if (source_type == TypeManager.delegate_type &&
2207 target_type.IsSubclassOf (TypeManager.delegate_type))
2211 // From ICloneable to Array or Delegate types
2213 if (source_type == TypeManager.icloneable_type &&
2214 (target_type == TypeManager.array_type ||
2215 target_type == TypeManager.delegate_type))
2222 /// Implements Explicit Reference conversions
2224 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2226 Type source_type = source.Type;
2227 bool target_is_value_type = target_type.IsValueType;
2230 // From object to any reference type
2232 if (source_type == TypeManager.object_type && !target_is_value_type)
2233 return new ClassCast (source, target_type);
2237 // From any class S to any class-type T, provided S is a base class of T
2239 if (target_type.IsSubclassOf (source_type))
2240 return new ClassCast (source, target_type);
2243 // From any interface type S to any interface T provided S is not derived from T
2245 if (source_type.IsInterface && target_type.IsInterface){
2246 if (TypeManager.ImplementsInterface (source_type, target_type))
2249 return new ClassCast (source, target_type);
2253 // From any class type S to any interface T, provides S is not sealed
2254 // and provided S does not implement T.
2256 if (target_type.IsInterface && !source_type.IsSealed) {
2257 if (TypeManager.ImplementsInterface (source_type, target_type))
2260 return new ClassCast (source, target_type);
2265 // From any interface-type S to to any class type T, provided T is not
2266 // sealed, or provided T implements S.
2268 if (source_type.IsInterface) {
2269 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2270 return new ClassCast (source, target_type);
2275 // From an array type S with an element type Se to an array type T with an
2276 // element type Te provided all the following are true:
2277 // * S and T differe only in element type, in other words, S and T
2278 // have the same number of dimensions.
2279 // * Both Se and Te are reference types
2280 // * An explicit referenc conversions exist from Se to Te
2282 if (source_type.IsArray && target_type.IsArray) {
2283 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2285 Type source_element_type = source_type.GetElementType ();
2286 Type target_element_type = target_type.GetElementType ();
2288 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2289 if (ExplicitReferenceConversionExists (source_element_type,
2290 target_element_type))
2291 return new ClassCast (source, target_type);
2296 // From System.Array to any array-type
2297 if (source_type == TypeManager.array_type &&
2298 target_type.IsArray) {
2299 return new ClassCast (source, target_type);
2303 // From System delegate to any delegate-type
2305 if (source_type == TypeManager.delegate_type &&
2306 target_type.IsSubclassOf (TypeManager.delegate_type))
2307 return new ClassCast (source, target_type);
2310 // From ICloneable to Array or Delegate types
2312 if (source_type == TypeManager.icloneable_type &&
2313 (target_type == TypeManager.array_type ||
2314 target_type == TypeManager.delegate_type))
2315 return new ClassCast (source, target_type);
2321 /// Performs an explicit conversion of the expression 'expr' whose
2322 /// type is expr.Type to 'target_type'.
2324 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2325 Type target_type, bool runtimeconv, Location loc)
2327 Type expr_type = expr.Type;
2328 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2333 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2338 // Unboxing conversion.
2340 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2341 return new UnboxCast (expr, target_type);
2346 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2350 // FIXME: Is there any reason we should have EnumConstant
2351 // dealt with here instead of just using always the
2352 // UnderlyingSystemType to wrap the type?
2354 if (expr is EnumConstant)
2355 e = ((EnumConstant) expr).Child;
2357 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2360 Expression t = ConvertImplicit (ec, e, target_type, loc);
2364 t = ConvertNumericExplicit (ec, e, target_type, loc);
2368 t = RuntimeConversion (ec, e, target_type, loc);
2372 Error_CannotConvertType (loc, expr_type, target_type);
2376 ne = ConvertReferenceExplicit (expr, target_type);
2381 if (target_type.IsPointer){
2382 if (expr_type.IsPointer)
2383 return new EmptyCast (expr, target_type);
2385 if (expr_type == TypeManager.sbyte_type ||
2386 expr_type == TypeManager.byte_type ||
2387 expr_type == TypeManager.short_type ||
2388 expr_type == TypeManager.ushort_type ||
2389 expr_type == TypeManager.int32_type ||
2390 expr_type == TypeManager.uint32_type ||
2391 expr_type == TypeManager.uint64_type ||
2392 expr_type == TypeManager.int64_type)
2393 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2395 if (expr_type.IsPointer){
2396 if (target_type == TypeManager.sbyte_type ||
2397 target_type == TypeManager.byte_type ||
2398 target_type == TypeManager.short_type ||
2399 target_type == TypeManager.ushort_type ||
2400 target_type == TypeManager.int32_type ||
2401 target_type == TypeManager.uint32_type ||
2402 target_type == TypeManager.uint64_type ||
2403 target_type == TypeManager.int64_type){
2404 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2407 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2412 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2416 // We should always be able to go from an uint32
2417 // implicitly or explicitly to the other integral
2420 throw new Exception ("Internal compiler error");
2425 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2429 if (!(runtimeconv)) {
2430 ne = RuntimeConversion (ec, expr, target_type, loc);
2434 Error_CannotConvertType (loc, expr_type, target_type);
2440 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2442 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2443 Type target_type, Location l)
2445 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2450 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2454 ne = ConvertReferenceExplicit (expr, target_type);
2458 ne = RuntimeConversion (ec, expr, target_type, l);
2462 Error_CannotConvertType (l, expr.Type, target_type);
2466 static string ExprClassName (ExprClass c)
2469 case ExprClass.Invalid:
2471 case ExprClass.Value:
2473 case ExprClass.Variable:
2475 case ExprClass.Namespace:
2477 case ExprClass.Type:
2479 case ExprClass.MethodGroup:
2480 return "method group";
2481 case ExprClass.PropertyAccess:
2482 return "property access";
2483 case ExprClass.EventAccess:
2484 return "event access";
2485 case ExprClass.IndexerAccess:
2486 return "indexer access";
2487 case ExprClass.Nothing:
2490 throw new Exception ("Should not happen");
2494 /// Reports that we were expecting 'expr' to be of class 'expected'
2496 public void Error118 (string expected)
2498 string kind = "Unknown";
2500 kind = ExprClassName (eclass);
2502 Error (118, "Expression denotes a '" + kind +
2503 "' where a '" + expected + "' was expected");
2506 public void Error118 (ResolveFlags flags)
2508 ArrayList valid = new ArrayList (10);
2510 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2511 valid.Add ("variable");
2512 valid.Add ("value");
2515 if ((flags & ResolveFlags.Type) != 0)
2518 if ((flags & ResolveFlags.MethodGroup) != 0)
2519 valid.Add ("method group");
2521 if ((flags & ResolveFlags.SimpleName) != 0)
2522 valid.Add ("simple name");
2524 if (valid.Count == 0)
2525 valid.Add ("unknown");
2527 StringBuilder sb = new StringBuilder ();
2528 for (int i = 0; i < valid.Count; i++) {
2531 else if (i == valid.Count)
2533 sb.Append (valid [i]);
2536 string kind = ExprClassName (eclass);
2538 Error (119, "Expression denotes a '" + kind + "' where " +
2539 "a '" + sb.ToString () + "' was expected");
2542 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2544 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2545 TypeManager.MonoBASIC_Name (t));
2548 public static void UnsafeError (Location loc)
2550 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2554 /// Converts the IntConstant, UIntConstant, LongConstant or
2555 /// ULongConstant into the integral target_type. Notice
2556 /// that we do not return an 'Expression' we do return
2557 /// a boxed integral type.
2559 /// FIXME: Since I added the new constants, we need to
2560 /// also support conversions from CharConstant, ByteConstant,
2561 /// SByteConstant, UShortConstant, ShortConstant
2563 /// This is used by the switch statement, so the domain
2564 /// of work is restricted to the literals above, and the
2565 /// targets are int32, uint32, char, byte, sbyte, ushort,
2566 /// short, uint64 and int64
2568 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2572 if (c.Type == target_type)
2573 return ((Constant) c).GetValue ();
2576 // Make into one of the literals we handle, we dont really care
2577 // about this value as we will just return a few limited types
2579 if (c is EnumConstant)
2580 c = ((EnumConstant)c).WidenToCompilerConstant ();
2582 if (c is IntConstant){
2583 int v = ((IntConstant) c).Value;
2585 if (target_type == TypeManager.uint32_type){
2588 } else if (target_type == TypeManager.char_type){
2589 if (v >= Char.MinValue && v <= Char.MaxValue)
2591 } else if (target_type == TypeManager.byte_type){
2592 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2594 } else if (target_type == TypeManager.sbyte_type){
2595 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2597 } else if (target_type == TypeManager.short_type){
2598 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2600 } else if (target_type == TypeManager.ushort_type){
2601 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2603 } else if (target_type == TypeManager.int64_type)
2605 else if (target_type == TypeManager.uint64_type){
2611 } else if (c is UIntConstant){
2612 uint v = ((UIntConstant) c).Value;
2614 if (target_type == TypeManager.int32_type){
2615 if (v <= Int32.MaxValue)
2617 } else if (target_type == TypeManager.char_type){
2618 if (v >= Char.MinValue && v <= Char.MaxValue)
2620 } else if (target_type == TypeManager.byte_type){
2621 if (v <= Byte.MaxValue)
2623 } else if (target_type == TypeManager.sbyte_type){
2624 if (v <= SByte.MaxValue)
2626 } else if (target_type == TypeManager.short_type){
2627 if (v <= UInt16.MaxValue)
2629 } else if (target_type == TypeManager.ushort_type){
2630 if (v <= UInt16.MaxValue)
2632 } else if (target_type == TypeManager.int64_type)
2634 else if (target_type == TypeManager.uint64_type)
2637 } else if (c is LongConstant){
2638 long v = ((LongConstant) c).Value;
2640 if (target_type == TypeManager.int32_type){
2641 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2643 } else if (target_type == TypeManager.uint32_type){
2644 if (v >= 0 && v <= UInt32.MaxValue)
2646 } else if (target_type == TypeManager.char_type){
2647 if (v >= Char.MinValue && v <= Char.MaxValue)
2649 } else if (target_type == TypeManager.byte_type){
2650 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2652 } else if (target_type == TypeManager.sbyte_type){
2653 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2655 } else if (target_type == TypeManager.short_type){
2656 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2658 } else if (target_type == TypeManager.ushort_type){
2659 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2661 } else if (target_type == TypeManager.uint64_type){
2666 } else if (c is ULongConstant){
2667 ulong v = ((ULongConstant) c).Value;
2669 if (target_type == TypeManager.int32_type){
2670 if (v <= Int32.MaxValue)
2672 } else if (target_type == TypeManager.uint32_type){
2673 if (v <= UInt32.MaxValue)
2675 } else if (target_type == TypeManager.char_type){
2676 if (v >= Char.MinValue && v <= Char.MaxValue)
2678 } else if (target_type == TypeManager.byte_type){
2679 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2681 } else if (target_type == TypeManager.sbyte_type){
2682 if (v <= (int) SByte.MaxValue)
2684 } else if (target_type == TypeManager.short_type){
2685 if (v <= UInt16.MaxValue)
2687 } else if (target_type == TypeManager.ushort_type){
2688 if (v <= UInt16.MaxValue)
2690 } else if (target_type == TypeManager.int64_type){
2691 if (v <= Int64.MaxValue)
2695 } else if (c is ByteConstant){
2696 byte v = ((ByteConstant) c).Value;
2698 if (target_type == TypeManager.int32_type)
2700 else if (target_type == TypeManager.uint32_type)
2702 else if (target_type == TypeManager.char_type)
2704 else if (target_type == TypeManager.sbyte_type){
2705 if (v <= SByte.MaxValue)
2707 } else if (target_type == TypeManager.short_type)
2709 else if (target_type == TypeManager.ushort_type)
2711 else if (target_type == TypeManager.int64_type)
2713 else if (target_type == TypeManager.uint64_type)
2716 } else if (c is SByteConstant){
2717 sbyte v = ((SByteConstant) c).Value;
2719 if (target_type == TypeManager.int32_type)
2721 else if (target_type == TypeManager.uint32_type){
2724 } else if (target_type == TypeManager.char_type){
2727 } else if (target_type == TypeManager.byte_type){
2730 } else if (target_type == TypeManager.short_type)
2732 else if (target_type == TypeManager.ushort_type){
2735 } else if (target_type == TypeManager.int64_type)
2737 else if (target_type == TypeManager.uint64_type){
2742 } else if (c is ShortConstant){
2743 short v = ((ShortConstant) c).Value;
2745 if (target_type == TypeManager.int32_type){
2747 } else if (target_type == TypeManager.uint32_type){
2750 } else if (target_type == TypeManager.char_type){
2753 } else if (target_type == TypeManager.byte_type){
2754 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2756 } else if (target_type == TypeManager.sbyte_type){
2757 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2759 } else if (target_type == TypeManager.ushort_type){
2762 } else if (target_type == TypeManager.int64_type)
2764 else if (target_type == TypeManager.uint64_type)
2768 } else if (c is UShortConstant){
2769 ushort v = ((UShortConstant) c).Value;
2771 if (target_type == TypeManager.int32_type)
2773 else if (target_type == TypeManager.uint32_type)
2775 else if (target_type == TypeManager.char_type){
2776 if (v >= Char.MinValue && v <= Char.MaxValue)
2778 } else if (target_type == TypeManager.byte_type){
2779 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2781 } else if (target_type == TypeManager.sbyte_type){
2782 if (v <= SByte.MaxValue)
2784 } else if (target_type == TypeManager.short_type){
2785 if (v <= Int16.MaxValue)
2787 } else if (target_type == TypeManager.int64_type)
2789 else if (target_type == TypeManager.uint64_type)
2793 } else if (c is CharConstant){
2794 char v = ((CharConstant) c).Value;
2796 if (target_type == TypeManager.int32_type)
2798 else if (target_type == TypeManager.uint32_type)
2800 else if (target_type == TypeManager.byte_type){
2801 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2803 } else if (target_type == TypeManager.sbyte_type){
2804 if (v <= SByte.MaxValue)
2806 } else if (target_type == TypeManager.short_type){
2807 if (v <= Int16.MaxValue)
2809 } else if (target_type == TypeManager.ushort_type)
2811 else if (target_type == TypeManager.int64_type)
2813 else if (target_type == TypeManager.uint64_type)
2818 Error_ConstantValueCannotBeConverted (loc, s, target_type);
2823 // Load the object from the pointer.
2825 public static void LoadFromPtr (ILGenerator ig, Type t)
2827 if (t == TypeManager.int32_type)
2828 ig.Emit (OpCodes.Ldind_I4);
2829 else if (t == TypeManager.uint32_type)
2830 ig.Emit (OpCodes.Ldind_U4);
2831 else if (t == TypeManager.short_type)
2832 ig.Emit (OpCodes.Ldind_I2);
2833 else if (t == TypeManager.ushort_type)
2834 ig.Emit (OpCodes.Ldind_U2);
2835 else if (t == TypeManager.char_type)
2836 ig.Emit (OpCodes.Ldind_U2);
2837 else if (t == TypeManager.byte_type)
2838 ig.Emit (OpCodes.Ldind_U1);
2839 else if (t == TypeManager.sbyte_type)
2840 ig.Emit (OpCodes.Ldind_I1);
2841 else if (t == TypeManager.uint64_type)
2842 ig.Emit (OpCodes.Ldind_I8);
2843 else if (t == TypeManager.int64_type)
2844 ig.Emit (OpCodes.Ldind_I8);
2845 else if (t == TypeManager.float_type)
2846 ig.Emit (OpCodes.Ldind_R4);
2847 else if (t == TypeManager.double_type)
2848 ig.Emit (OpCodes.Ldind_R8);
2849 else if (t == TypeManager.bool_type)
2850 ig.Emit (OpCodes.Ldind_I1);
2851 else if (t == TypeManager.intptr_type)
2852 ig.Emit (OpCodes.Ldind_I);
2853 else if (TypeManager.IsEnumType (t)) {
2854 if (t == TypeManager.enum_type)
2855 ig.Emit (OpCodes.Ldind_Ref);
2857 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
2858 } else if (t.IsValueType)
2859 ig.Emit (OpCodes.Ldobj, t);
2861 ig.Emit (OpCodes.Ldind_Ref);
2865 // The stack contains the pointer and the value of type 'type'
2867 public static void StoreFromPtr (ILGenerator ig, Type type)
2869 if (TypeManager.IsEnumType (type))
2870 type = TypeManager.EnumToUnderlying (type);
2871 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
2872 ig.Emit (OpCodes.Stind_I4);
2873 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
2874 ig.Emit (OpCodes.Stind_I8);
2875 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
2876 type == TypeManager.ushort_type)
2877 ig.Emit (OpCodes.Stind_I2);
2878 else if (type == TypeManager.float_type)
2879 ig.Emit (OpCodes.Stind_R4);
2880 else if (type == TypeManager.double_type)
2881 ig.Emit (OpCodes.Stind_R8);
2882 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
2883 type == TypeManager.bool_type)
2884 ig.Emit (OpCodes.Stind_I1);
2885 else if (type == TypeManager.intptr_type)
2886 ig.Emit (OpCodes.Stind_I);
2887 else if (type.IsValueType)
2888 ig.Emit (OpCodes.Stobj, type);
2890 ig.Emit (OpCodes.Stind_Ref);
2894 // Returns the size of type 't' if known, otherwise, 0
2896 public static int GetTypeSize (Type t)
2898 t = TypeManager.TypeToCoreType (t);
2899 if (t == TypeManager.int32_type ||
2900 t == TypeManager.uint32_type ||
2901 t == TypeManager.float_type)
2903 else if (t == TypeManager.int64_type ||
2904 t == TypeManager.uint64_type ||
2905 t == TypeManager.double_type)
2907 else if (t == TypeManager.byte_type ||
2908 t == TypeManager.sbyte_type ||
2909 t == TypeManager.bool_type)
2911 else if (t == TypeManager.short_type ||
2912 t == TypeManager.char_type ||
2913 t == TypeManager.ushort_type)
2915 else if (t == TypeManager.decimal_type)
2922 // Default implementation of IAssignMethod.CacheTemporaries
2924 public void CacheTemporaries (EmitContext ec)
2928 static void Error_NegativeArrayIndex (Location loc)
2930 Report.Error (284, loc, "Can not create array with a negative size");
2934 // Converts 'source' to an int, uint, long or ulong.
2936 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
2940 bool old_checked = ec.CheckState;
2941 ec.CheckState = true;
2943 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
2944 if (target == null){
2945 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
2946 if (target == null){
2947 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
2948 if (target == null){
2949 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
2951 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
2955 ec.CheckState = old_checked;
2958 // Only positive constants are allowed at compile time
2960 if (target is Constant){
2961 if (target is IntConstant){
2962 if (((IntConstant) target).Value < 0){
2963 Error_NegativeArrayIndex (loc);
2968 if (target is LongConstant){
2969 if (((LongConstant) target).Value < 0){
2970 Error_NegativeArrayIndex (loc);
2983 /// This is just a base class for expressions that can
2984 /// appear on statements (invocations, object creation,
2985 /// assignments, post/pre increment and decrement). The idea
2986 /// being that they would support an extra Emition interface that
2987 /// does not leave a result on the stack.
2989 public abstract class ExpressionStatement : Expression {
2992 /// Requests the expression to be emitted in a 'statement'
2993 /// context. This means that no new value is left on the
2994 /// stack after invoking this method (constrasted with
2995 /// Emit that will always leave a value on the stack).
2997 public abstract void EmitStatement (EmitContext ec);
3001 /// This kind of cast is used to encapsulate the child
3002 /// whose type is child.Type into an expression that is
3003 /// reported to return "return_type". This is used to encapsulate
3004 /// expressions which have compatible types, but need to be dealt
3005 /// at higher levels with.
3007 /// For example, a "byte" expression could be encapsulated in one
3008 /// of these as an "unsigned int". The type for the expression
3009 /// would be "unsigned int".
3012 public class EmptyCast : Expression {
3013 protected Expression child;
3015 public EmptyCast (Expression child, Type return_type)
3017 eclass = child.eclass;
3022 public override Expression DoResolve (EmitContext ec)
3024 // This should never be invoked, we are born in fully
3025 // initialized state.
3030 public override void Emit (EmitContext ec)
3037 /// This class is used to wrap literals which belong inside Enums
3039 public class EnumConstant : Constant {
3040 public Constant Child;
3042 public EnumConstant (Constant child, Type enum_type)
3044 eclass = child.eclass;
3049 public override Expression DoResolve (EmitContext ec)
3051 // This should never be invoked, we are born in fully
3052 // initialized state.
3057 public override void Emit (EmitContext ec)
3062 public override object GetValue ()
3064 return Child.GetValue ();
3068 // Converts from one of the valid underlying types for an enumeration
3069 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3070 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3072 public Constant WidenToCompilerConstant ()
3074 Type t = TypeManager.EnumToUnderlying (Child.Type);
3075 object v = ((Constant) Child).GetValue ();;
3077 if (t == TypeManager.int32_type)
3078 return new IntConstant ((int) v);
3079 if (t == TypeManager.uint32_type)
3080 return new UIntConstant ((uint) v);
3081 if (t == TypeManager.int64_type)
3082 return new LongConstant ((long) v);
3083 if (t == TypeManager.uint64_type)
3084 return new ULongConstant ((ulong) v);
3085 if (t == TypeManager.short_type)
3086 return new ShortConstant ((short) v);
3087 if (t == TypeManager.ushort_type)
3088 return new UShortConstant ((ushort) v);
3089 if (t == TypeManager.byte_type)
3090 return new ByteConstant ((byte) v);
3091 if (t == TypeManager.sbyte_type)
3092 return new SByteConstant ((sbyte) v);
3094 throw new Exception ("Invalid enumeration underlying type: " + t);
3098 // Extracts the value in the enumeration on its native representation
3100 public object GetPlainValue ()
3102 Type t = TypeManager.EnumToUnderlying (Child.Type);
3103 object v = ((Constant) Child).GetValue ();;
3105 if (t == TypeManager.int32_type)
3107 if (t == TypeManager.uint32_type)
3109 if (t == TypeManager.int64_type)
3111 if (t == TypeManager.uint64_type)
3113 if (t == TypeManager.short_type)
3115 if (t == TypeManager.ushort_type)
3117 if (t == TypeManager.byte_type)
3119 if (t == TypeManager.sbyte_type)
3125 public override string AsString ()
3127 return Child.AsString ();
3130 public override DoubleConstant ConvertToDouble ()
3132 return Child.ConvertToDouble ();
3135 public override FloatConstant ConvertToFloat ()
3137 return Child.ConvertToFloat ();
3140 public override ULongConstant ConvertToULong ()
3142 return Child.ConvertToULong ();
3145 public override LongConstant ConvertToLong ()
3147 return Child.ConvertToLong ();
3150 public override UIntConstant ConvertToUInt ()
3152 return Child.ConvertToUInt ();
3155 public override IntConstant ConvertToInt ()
3157 return Child.ConvertToInt ();
3162 /// This kind of cast is used to encapsulate Value Types in objects.
3164 /// The effect of it is to box the value type emitted by the previous
3167 public class BoxedCast : EmptyCast {
3169 public BoxedCast (Expression expr)
3170 : base (expr, TypeManager.object_type)
3174 public override Expression DoResolve (EmitContext ec)
3176 // This should never be invoked, we are born in fully
3177 // initialized state.
3182 public override void Emit (EmitContext ec)
3186 ec.ig.Emit (OpCodes.Box, child.Type);
3190 public class UnboxCast : EmptyCast {
3191 public UnboxCast (Expression expr, Type return_type)
3192 : base (expr, return_type)
3196 public override Expression DoResolve (EmitContext ec)
3198 // This should never be invoked, we are born in fully
3199 // initialized state.
3204 public override void Emit (EmitContext ec)
3207 ILGenerator ig = ec.ig;
3210 ig.Emit (OpCodes.Unbox, t);
3212 LoadFromPtr (ig, t);
3217 /// This is used to perform explicit numeric conversions.
3219 /// Explicit numeric conversions might trigger exceptions in a checked
3220 /// context, so they should generate the conv.ovf opcodes instead of
3223 public class ConvCast : EmptyCast {
3224 public enum Mode : byte {
3225 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3227 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3228 U2_I1, U2_U1, U2_I2, U2_CH,
3229 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3230 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3231 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3232 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3233 CH_I1, CH_U1, CH_I2,
3234 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3235 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3241 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3242 : base (child, return_type)
3244 checked_state = ec.CheckState;
3248 public override Expression DoResolve (EmitContext ec)
3250 // This should never be invoked, we are born in fully
3251 // initialized state.
3256 public override void Emit (EmitContext ec)
3258 ILGenerator ig = ec.ig;
3264 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3265 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3266 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3267 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3268 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3270 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3271 case Mode.U1_CH: /* nothing */ break;
3273 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3274 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3275 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3276 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3277 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3278 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3280 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3281 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3282 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3283 case Mode.U2_CH: /* nothing */ break;
3285 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3286 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3287 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3288 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3289 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3290 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3291 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3293 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3294 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3295 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3296 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3297 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3298 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3300 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3301 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3302 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3303 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3304 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3305 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3306 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3307 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3309 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3310 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3311 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3312 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3313 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3314 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3315 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3316 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3318 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3319 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3320 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3322 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3323 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3324 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3325 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3326 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3327 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3328 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3329 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3330 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3332 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3333 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3334 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3335 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3336 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3337 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3338 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3339 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3340 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3341 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3345 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3346 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3347 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3348 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3349 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3351 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3352 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3354 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3355 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3356 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3357 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3358 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3359 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3361 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3362 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3363 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3364 case Mode.U2_CH: /* nothing */ break;
3366 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3367 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3368 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3369 case Mode.I4_U4: /* nothing */ break;
3370 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3371 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3372 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3374 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3375 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3376 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3377 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3378 case Mode.U4_I4: /* nothing */ break;
3379 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3381 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3382 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3383 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3384 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3385 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3386 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3387 case Mode.I8_U8: /* nothing */ break;
3388 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3390 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3391 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3392 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3393 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3394 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3395 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3396 case Mode.U8_I8: /* nothing */ break;
3397 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3399 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3400 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3401 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3403 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3404 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3405 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3406 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3407 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3408 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3409 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3410 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3411 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3413 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3414 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3415 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3416 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3417 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3418 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3419 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3420 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3421 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3422 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3428 public class OpcodeCast : EmptyCast {
3432 public OpcodeCast (Expression child, Type return_type, OpCode op)
3433 : base (child, return_type)
3437 second_valid = false;
3440 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3441 : base (child, return_type)
3446 second_valid = true;
3449 public override Expression DoResolve (EmitContext ec)
3451 // This should never be invoked, we are born in fully
3452 // initialized state.
3457 public override void Emit (EmitContext ec)
3468 /// This kind of cast is used to encapsulate a child and cast it
3469 /// to the class requested
3471 public class ClassCast : EmptyCast {
3472 public ClassCast (Expression child, Type return_type)
3473 : base (child, return_type)
3478 public override Expression DoResolve (EmitContext ec)
3480 // This should never be invoked, we are born in fully
3481 // initialized state.
3486 public override void Emit (EmitContext ec)
3490 ec.ig.Emit (OpCodes.Castclass, type);
3496 /// SimpleName expressions are initially formed of a single
3497 /// word and it only happens at the beginning of the expression.
3501 /// The expression will try to be bound to a Field, a Method
3502 /// group or a Property. If those fail we pass the name to our
3503 /// caller and the SimpleName is compounded to perform a type
3504 /// lookup. The idea behind this process is that we want to avoid
3505 /// creating a namespace map from the assemblies, as that requires
3506 /// the GetExportedTypes function to be called and a hashtable to
3507 /// be constructed which reduces startup time. If later we find
3508 /// that this is slower, we should create a 'NamespaceExpr' expression
3509 /// that fully participates in the resolution process.
3511 /// For example 'System.Console.WriteLine' is decomposed into
3512 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3514 /// The first SimpleName wont produce a match on its own, so it will
3516 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3518 /// System.Console will produce a TypeExpr match.
3520 /// The downside of this is that we might be hitting 'LookupType' too many
3521 /// times with this scheme.
3523 public class SimpleName : Expression, ITypeExpression {
3524 public readonly string Name;
3526 public SimpleName (string name, Location l)
3532 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3534 if (ec.IsFieldInitializer)
3537 "A field initializer cannot reference the non-static field, " +
3538 "method or property '"+name+"'");
3542 "An object reference is required " +
3543 "for the non-static field '"+name+"'");
3547 // Checks whether we are trying to access an instance
3548 // property, method or field from a static body.
3550 Expression MemberStaticCheck (EmitContext ec, Expression e)
3552 if (e is IMemberExpr){
3553 IMemberExpr member = (IMemberExpr) e;
3555 if (!member.IsStatic){
3556 Error_ObjectRefRequired (ec, loc, Name);
3564 public override Expression DoResolve (EmitContext ec)
3566 return SimpleNameResolve (ec, null, false);
3569 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3571 return SimpleNameResolve (ec, right_side, false);
3575 public Expression DoResolveAllowStatic (EmitContext ec)
3577 return SimpleNameResolve (ec, null, true);
3580 public Expression DoResolveType (EmitContext ec)
3583 // Stage 3: Lookup symbol in the various namespaces.
3585 DeclSpace ds = ec.DeclSpace;
3589 if (ec.ResolvingTypeTree){
3590 int errors = Report.Errors;
3591 Type dt = ec.DeclSpace.FindType (loc, Name);
3592 if (Report.Errors != errors)
3596 return new TypeExpr (dt, loc);
3599 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
3600 return new TypeExpr (t, loc);
3604 // Stage 2 part b: Lookup up if we are an alias to a type
3607 // Since we are cheating: we only do the Alias lookup for
3608 // namespaces if the name does not include any dots in it
3611 alias_value = ec.DeclSpace.LookupAlias (Name);
3613 if (Name.IndexOf ('.') == -1 && alias_value != null) {
3614 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
3615 return new TypeExpr (t, loc);
3617 // we have alias value, but it isn't Type, so try if it's namespace
3618 return new SimpleName (alias_value, loc);
3621 // No match, maybe our parent can compose us
3622 // into something meaningful.
3627 /// 7.5.2: Simple Names.
3629 /// Local Variables and Parameters are handled at
3630 /// parse time, so they never occur as SimpleNames.
3632 /// The 'allow_static' flag is used by MemberAccess only
3633 /// and it is used to inform us that it is ok for us to
3634 /// avoid the static check, because MemberAccess might end
3635 /// up resolving the Name as a Type name and the access as
3636 /// a static type access.
3638 /// ie: Type Type; .... { Type.GetType (""); }
3640 /// Type is both an instance variable and a Type; Type.GetType
3641 /// is the static method not an instance method of type.
3643 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
3645 Expression e = null;
3648 // Stage 1: Performed by the parser (binding to locals or parameters).
3650 Block current_block = ec.CurrentBlock;
3651 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
3652 LocalVariableReference var;
3654 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
3656 if (right_side != null)
3657 return var.ResolveLValue (ec, right_side);
3659 return var.Resolve (ec);
3662 if (current_block != null){
3664 Parameter par = null;
3665 Parameters pars = current_block.Parameters;
3667 par = pars.GetParameterByName (Name, out idx);
3670 ParameterReference param;
3672 param = new ParameterReference (pars, idx, Name, loc);
3674 if (right_side != null)
3675 return param.ResolveLValue (ec, right_side);
3677 return param.Resolve (ec);
3682 // Stage 2: Lookup members
3686 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
3687 // Hence we have two different cases
3690 DeclSpace lookup_ds = ec.DeclSpace;
3692 if (lookup_ds.TypeBuilder == null)
3695 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
3700 // Classes/structs keep looking, enums break
3702 if (lookup_ds is TypeContainer)
3703 lookup_ds = ((TypeContainer) lookup_ds).Parent;
3706 } while (lookup_ds != null);
3708 if (e == null && ec.ContainerType != null)
3709 e = MemberLookup (ec, ec.ContainerType, Name, loc);
3711 // #52067 - Start - Trying to solve
3715 ArrayList lookups = new ArrayList();
3716 ArrayList typelookups = new ArrayList();
3718 int split = Name.LastIndexOf('.');
3720 String nameSpacePart = Name.Substring(0, split);
3721 String memberNamePart = Name.Substring(split + 1);
3722 foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
3723 e = MemberLookup(ec, type, memberNamePart, loc);
3726 typelookups.Add(type);
3731 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
3732 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
3733 e = MemberLookup(ec, type, Name, loc);
3736 typelookups.Add(type);
3739 if (lookups.Count == 1) {
3740 e = (Expression)lookups[0];
3742 if (lookups.Count > 1) {
3743 StringBuilder sb = new StringBuilder();
3744 foreach(Type type in typelookups)
3745 sb.Append("'" + type.FullName + "'");
3746 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
3755 return DoResolveType (ec);
3760 if (e is IMemberExpr) {
3761 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
3765 IMemberExpr me = e as IMemberExpr;
3769 // This fails if ResolveMemberAccess() was unable to decide whether
3770 // it's a field or a type of the same name.
3771 if (!me.IsStatic && (me.InstanceExpression == null))
3774 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
3775 Maybe a grammar-related problem?
3778 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
3779 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
3780 "outer type '" + me.DeclaringType + "' via nested type '" +
3781 me.InstanceExpression.Type + "'");
3785 if (right_side != null)
3786 e = e.DoResolveLValue (ec, right_side);
3788 e = e.DoResolve (ec);
3793 if (ec.IsStatic || ec.IsFieldInitializer){
3797 return MemberStaticCheck (ec, e);
3802 public override void Emit (EmitContext ec)
3805 // If this is ever reached, then we failed to
3806 // find the name as a namespace
3809 Error (30451, "The name '" + Name +
3810 "' does not exist in the class '" +
3811 ec.DeclSpace.Name + "'");
3814 public override string ToString ()
3821 /// Fully resolved expression that evaluates to a type
3823 public class TypeExpr : Expression, ITypeExpression {
3824 public TypeExpr (Type t, Location l)
3827 eclass = ExprClass.Type;
3831 public virtual Expression DoResolveType (EmitContext ec)
3836 override public Expression DoResolve (EmitContext ec)
3841 override public void Emit (EmitContext ec)
3843 throw new Exception ("Should never be called");
3846 public override string ToString ()
3848 return Type.ToString ();
3853 /// Used to create types from a fully qualified name. These are just used
3854 /// by the parser to setup the core types. A TypeLookupExpression is always
3855 /// classified as a type.
3857 public class TypeLookupExpression : TypeExpr {
3860 public TypeLookupExpression (string name) : base (null, Location.Null)
3865 public override Expression DoResolveType (EmitContext ec)
3868 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
3872 public override Expression DoResolve (EmitContext ec)
3874 return DoResolveType (ec);
3877 public override void Emit (EmitContext ec)
3879 throw new Exception ("Should never be called");
3882 public override string ToString ()
3889 /// MethodGroup Expression.
3891 /// This is a fully resolved expression that evaluates to a type
3893 public class MethodGroupExpr : Expression, IMemberExpr {
3894 public MethodBase [] Methods;
3895 Expression instance_expression = null;
3896 bool is_explicit_impl = false;
3898 public MethodGroupExpr (MemberInfo [] mi, Location l)
3900 Methods = new MethodBase [mi.Length];
3901 mi.CopyTo (Methods, 0);
3902 eclass = ExprClass.MethodGroup;
3903 type = TypeManager.object_type;
3907 public MethodGroupExpr (ArrayList list, Location l)
3909 Methods = new MethodBase [list.Count];
3912 list.CopyTo (Methods, 0);
3914 foreach (MemberInfo m in list){
3915 if (!(m is MethodBase)){
3916 Console.WriteLine ("Name " + m.Name);
3917 Console.WriteLine ("Found a: " + m.GetType ().FullName);
3923 eclass = ExprClass.MethodGroup;
3924 type = TypeManager.object_type;
3927 public Type DeclaringType {
3929 return Methods [0].DeclaringType;
3934 // 'A method group may have associated an instance expression'
3936 public Expression InstanceExpression {
3938 return instance_expression;
3942 instance_expression = value;
3946 public bool IsExplicitImpl {
3948 return is_explicit_impl;
3952 is_explicit_impl = value;
3956 public string Name {
3958 return Methods [0].Name;
3962 public bool IsInstance {
3964 foreach (MethodBase mb in Methods)
3972 public bool IsStatic {
3974 foreach (MethodBase mb in Methods)
3982 override public Expression DoResolve (EmitContext ec)
3984 if (instance_expression != null) {
3985 instance_expression = instance_expression.DoResolve (ec);
3986 if (instance_expression == null)
3993 public void ReportUsageError ()
3995 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
3996 Methods [0].Name + "()' is referenced without parentheses");
3999 override public void Emit (EmitContext ec)
4001 ReportUsageError ();
4004 bool RemoveMethods (bool keep_static)
4006 ArrayList smethods = new ArrayList ();
4008 foreach (MethodBase mb in Methods){
4009 if (mb.IsStatic == keep_static)
4013 if (smethods.Count == 0)
4016 Methods = new MethodBase [smethods.Count];
4017 smethods.CopyTo (Methods, 0);
4023 /// Removes any instance methods from the MethodGroup, returns
4024 /// false if the resulting set is empty.
4026 public bool RemoveInstanceMethods ()
4028 return RemoveMethods (true);
4032 /// Removes any static methods from the MethodGroup, returns
4033 /// false if the resulting set is empty.
4035 public bool RemoveStaticMethods ()
4037 return RemoveMethods (false);
4042 /// Fully resolved expression that evaluates to a Field
4044 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4045 public readonly FieldInfo FieldInfo;
4046 Expression instance_expr;
4048 public FieldExpr (FieldInfo fi, Location l)
4051 eclass = ExprClass.Variable;
4052 type = fi.FieldType;
4056 public string Name {
4058 return FieldInfo.Name;
4062 public bool IsInstance {
4064 return !FieldInfo.IsStatic;
4068 public bool IsStatic {
4070 return FieldInfo.IsStatic;
4074 public Type DeclaringType {
4076 return FieldInfo.DeclaringType;
4080 public Expression InstanceExpression {
4082 return instance_expr;
4086 instance_expr = value;
4090 override public Expression DoResolve (EmitContext ec)
4092 if (!FieldInfo.IsStatic){
4093 if (instance_expr == null){
4094 throw new Exception ("non-static FieldExpr without instance var\n" +
4095 "You have to assign the Instance variable\n" +
4096 "Of the FieldExpr to set this\n");
4099 // Resolve the field's instance expression while flow analysis is turned
4100 // off: when accessing a field "a.b", we must check whether the field
4101 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4102 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4103 ResolveFlags.DisableFlowAnalysis);
4104 if (instance_expr == null)
4108 // If the instance expression is a local variable or parameter.
4109 IVariable var = instance_expr as IVariable;
4110 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4116 void Report_AssignToReadonly (bool is_instance)
4121 msg = "Readonly field can not be assigned outside " +
4122 "of constructor or variable initializer";
4124 msg = "A static readonly field can only be assigned in " +
4125 "a static constructor";
4127 Report.Error (is_instance ? 191 : 198, loc, msg);
4130 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4132 IVariable var = instance_expr as IVariable;
4134 var.SetFieldAssigned (ec, FieldInfo.Name);
4136 Expression e = DoResolve (ec);
4141 if (!FieldInfo.IsInitOnly)
4145 // InitOnly fields can only be assigned in constructors
4148 if (ec.IsConstructor)
4151 Report_AssignToReadonly (true);
4156 override public void Emit (EmitContext ec)
4158 ILGenerator ig = ec.ig;
4159 bool is_volatile = false;
4161 if (FieldInfo is FieldBuilder){
4162 FieldBase f = TypeManager.GetField (FieldInfo);
4164 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4167 f.status |= Field.Status.USED;
4170 if (FieldInfo.IsStatic){
4172 ig.Emit (OpCodes.Volatile);
4174 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4176 if (instance_expr.Type.IsValueType){
4178 LocalTemporary tempo = null;
4180 if (!(instance_expr is IMemoryLocation)){
4181 tempo = new LocalTemporary (
4182 ec, instance_expr.Type);
4184 InstanceExpression.Emit (ec);
4188 ml = (IMemoryLocation) instance_expr;
4190 ml.AddressOf (ec, AddressOp.Load);
4192 instance_expr.Emit (ec);
4195 ig.Emit (OpCodes.Volatile);
4197 ig.Emit (OpCodes.Ldfld, FieldInfo);
4201 public void EmitAssign (EmitContext ec, Expression source)
4203 FieldAttributes fa = FieldInfo.Attributes;
4204 bool is_static = (fa & FieldAttributes.Static) != 0;
4205 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4206 ILGenerator ig = ec.ig;
4208 if (is_readonly && !ec.IsConstructor){
4209 Report_AssignToReadonly (!is_static);
4214 Expression instance = instance_expr;
4216 if (instance.Type.IsValueType){
4217 if (instance is IMemoryLocation){
4218 IMemoryLocation ml = (IMemoryLocation) instance;
4220 ml.AddressOf (ec, AddressOp.Store);
4222 throw new Exception ("The " + instance + " of type " +
4224 " represents a ValueType and does " +
4225 "not implement IMemoryLocation");
4231 if (FieldInfo is FieldBuilder){
4232 FieldBase f = TypeManager.GetField (FieldInfo);
4234 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4235 ig.Emit (OpCodes.Volatile);
4239 ig.Emit (OpCodes.Stsfld, FieldInfo);
4241 ig.Emit (OpCodes.Stfld, FieldInfo);
4243 if (FieldInfo is FieldBuilder){
4244 FieldBase f = TypeManager.GetField (FieldInfo);
4246 f.status |= Field.Status.ASSIGNED;
4250 public void AddressOf (EmitContext ec, AddressOp mode)
4252 ILGenerator ig = ec.ig;
4254 if (FieldInfo is FieldBuilder){
4255 FieldBase f = TypeManager.GetField (FieldInfo);
4256 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4257 ig.Emit (OpCodes.Volatile);
4260 if (FieldInfo is FieldBuilder){
4261 FieldBase f = TypeManager.GetField (FieldInfo);
4263 if ((mode & AddressOp.Store) != 0)
4264 f.status |= Field.Status.ASSIGNED;
4265 if ((mode & AddressOp.Load) != 0)
4266 f.status |= Field.Status.USED;
4270 // Handle initonly fields specially: make a copy and then
4271 // get the address of the copy.
4273 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4277 local = ig.DeclareLocal (type);
4278 ig.Emit (OpCodes.Stloc, local);
4279 ig.Emit (OpCodes.Ldloca, local);
4283 if (FieldInfo.IsStatic)
4284 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4286 if (instance_expr is IMemoryLocation)
4287 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4289 instance_expr.Emit (ec);
4290 ig.Emit (OpCodes.Ldflda, FieldInfo);
4296 /// Expression that evaluates to a Property. The Assign class
4297 /// might set the 'Value' expression if we are in an assignment.
4299 /// This is not an LValue because we need to re-write the expression, we
4300 /// can not take data from the stack and store it.
4302 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4303 public readonly PropertyInfo PropertyInfo;
4305 MethodInfo getter, setter;
4307 public ArrayList PropertyArgs;
4309 Expression instance_expr;
4311 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4314 eclass = ExprClass.PropertyAccess;
4315 PropertyArgs = new ArrayList();
4319 type = TypeManager.TypeToCoreType (pi.PropertyType);
4321 ResolveAccessors (ec);
4324 public string Name {
4326 return PropertyInfo.Name;
4330 public bool IsInstance {
4336 public bool IsStatic {
4342 public Type DeclaringType {
4344 return PropertyInfo.DeclaringType;
4349 // The instance expression associated with this expression
4351 public Expression InstanceExpression {
4353 instance_expr = value;
4357 return instance_expr;
4361 public bool VerifyAssignable ()
4363 if (!PropertyInfo.CanWrite){
4364 Report.Error (200, loc,
4365 "The property '" + PropertyInfo.Name +
4366 "' can not be assigned to, as it has not set accessor");
4373 void ResolveAccessors (EmitContext ec)
4375 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4376 MemberInfo [] group;
4378 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4379 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4382 // The first method is the closest to us
4384 if (group != null && group.Length > 0){
4385 getter = (MethodInfo) group [0];
4387 if (getter.IsStatic)
4392 // The first method is the closest to us
4394 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4395 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4396 if (group != null && group.Length > 0){
4397 setter = (MethodInfo) group [0];
4398 if (setter.IsStatic)
4403 override public Expression DoResolve (EmitContext ec)
4405 if (getter == null){
4406 Report.Error (154, loc,
4407 "The property '" + PropertyInfo.Name +
4408 "' can not be used in " +
4409 "this context because it lacks a get accessor");
4413 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4414 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4418 if (instance_expr != null) {
4419 instance_expr = instance_expr.DoResolve (ec);
4420 if (instance_expr == null)
4427 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4429 if (setter == null){
4430 Report.Error (154, loc,
4431 "The property '" + PropertyInfo.Name +
4432 "' can not be used in " +
4433 "this context because it lacks a set accessor");
4437 if (instance_expr != null) {
4438 instance_expr = instance_expr.DoResolve (ec);
4439 if (instance_expr == null)
4446 override public void Emit (EmitContext ec)
4449 // Special case: length of single dimension array property is turned into ldlen
4451 if ((getter == TypeManager.system_int_array_get_length) ||
4452 (getter == TypeManager.int_array_get_length)){
4453 Type iet = instance_expr.Type;
4456 // System.Array.Length can be called, but the Type does not
4457 // support invoking GetArrayRank, so test for that case first
4459 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4460 instance_expr.Emit (ec);
4461 ec.ig.Emit (OpCodes.Ldlen);
4465 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4469 // Implements the IAssignMethod interface for assignments
4471 public void EmitAssign (EmitContext ec, Expression source)
4473 Argument arg = new Argument (source, Argument.AType.Expression);
4474 ArrayList args = new ArrayList ();
4477 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4480 override public void EmitStatement (EmitContext ec)
4483 ec.ig.Emit (OpCodes.Pop);
4488 /// Fully resolved expression that evaluates to an Event
4490 public class EventExpr : Expression, IMemberExpr {
4491 public readonly EventInfo EventInfo;
4492 public Expression instance_expr;
4495 MethodInfo add_accessor, remove_accessor;
4497 public EventExpr (EventInfo ei, Location loc)
4501 eclass = ExprClass.EventAccess;
4503 add_accessor = TypeManager.GetAddMethod (ei);
4504 remove_accessor = TypeManager.GetRemoveMethod (ei);
4506 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4509 if (EventInfo is MyEventBuilder)
4510 type = ((MyEventBuilder) EventInfo).EventType;
4512 type = EventInfo.EventHandlerType;
4515 public string Name {
4517 return EventInfo.Name;
4521 public bool IsInstance {
4527 public bool IsStatic {
4533 public Type DeclaringType {
4535 return EventInfo.DeclaringType;
4539 public Expression InstanceExpression {
4541 return instance_expr;
4545 instance_expr = value;
4549 public override Expression DoResolve (EmitContext ec)
4551 if (instance_expr != null) {
4552 instance_expr = instance_expr.DoResolve (ec);
4553 if (instance_expr == null)
4560 public override void Emit (EmitContext ec)
4562 Report.Error (70, loc, "The event '" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
4565 public void EmitAddOrRemove (EmitContext ec, Expression source)
4567 Expression handler = ((Binary) source).Right;
4569 Argument arg = new Argument (handler, Argument.AType.Expression);
4570 ArrayList args = new ArrayList ();
4574 if (((Binary) source).Oper == Binary.Operator.Addition)
4575 Invocation.EmitCall (
4576 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
4578 Invocation.EmitCall (
4579 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);