2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001 Ximian, Inc.
11 namespace Mono.MonoBASIC {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This is used to tell Resolve in which types of expressions we're
44 public enum ResolveFlags {
45 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48 // Returns a type expression.
51 // Returns a method group.
54 // Allows SimpleNames to be returned.
55 // This is used by MemberAccess to construct long names that can not be
56 // partially resolved (namespace-qualified names for example).
59 // Mask of all the expression class flags.
62 // Disable control flow analysis while resolving the expression.
63 // This is used when resolving the instance expression of a field expression.
64 DisableFlowAnalysis = 16
68 // This is just as a hint to AddressOf of what will be done with the
71 public enum AddressOp {
78 /// This interface is implemented by variables
80 public interface IMemoryLocation {
82 /// The AddressOf method should generate code that loads
83 /// the address of the object and leaves it on the stack.
85 /// The 'mode' argument is used to notify the expression
86 /// of whether this will be used to read from the address or
87 /// write to the address.
89 /// This is just a hint that can be used to provide good error
90 /// reporting, and should have no other side effects.
92 void AddressOf (EmitContext ec, AddressOp mode);
96 /// This interface is implemented by variables
98 public interface IVariable {
100 /// Checks whether the variable has already been assigned at
101 /// the current position of the method's control flow and
102 /// reports an appropriate error message if not.
104 /// If the variable is a struct, then this call checks whether
105 /// all of its fields (including all private ones) have been
108 bool IsAssigned (EmitContext ec, Location loc);
111 /// Checks whether field 'name' in this struct has been assigned.
113 bool IsFieldAssigned (EmitContext ec, string name, Location loc);
116 /// Tells the flow analysis code that the variable has already
117 /// been assigned at the current code position.
119 /// If the variable is a struct, this call marks all its fields
120 /// (including private fields) as being assigned.
122 void SetAssigned (EmitContext ec);
125 /// Tells the flow analysis code that field 'name' in this struct
126 /// has already been assigned atthe current code position.
128 void SetFieldAssigned (EmitContext ec, string name);
132 /// This interface denotes an expression which evaluates to a member
133 /// of a struct or a class.
135 public interface IMemberExpr
138 /// The name of this member.
145 /// Whether this is an instance member.
152 /// Whether this is a static member.
159 /// The type which declares this member.
166 /// The instance expression associated with this member, if it's a
167 /// non-static member.
169 Expression InstanceExpression {
175 /// Expression which resolves to a type.
177 public interface ITypeExpression
180 /// Resolve the expression, but only lookup types.
182 Expression DoResolveType (EmitContext ec);
186 /// Base class for expressions
188 public abstract class Expression {
189 public ExprClass eclass;
191 protected Location loc;
203 public Location Location {
210 /// Utility wrapper routine for Error, just to beautify the code
212 public void Error (int error, string s)
214 if (!Location.IsNull (loc))
215 Report.Error (error, loc, s);
217 Report.Error (error, s);
221 /// Utility wrapper routine for Warning, just to beautify the code
223 public void Warning (int warning, string s)
225 if (!Location.IsNull (loc))
226 Report.Warning (warning, loc, s);
228 Report.Warning (warning, s);
232 /// Utility wrapper routine for Warning, only prints the warning if
233 /// warnings of level 'level' are enabled.
235 public void Warning (int warning, int level, string s)
237 if (level <= RootContext.WarningLevel)
238 Warning (warning, s);
241 static public void Error_CannotConvertType (Location loc, Type source, Type target)
243 Report.Error (30, loc, "Cannot convert type '" +
244 TypeManager.MonoBASIC_Name (source) + "' to '" +
245 TypeManager.MonoBASIC_Name (target) + "'");
249 /// Performs semantic analysis on the Expression
253 /// The Resolve method is invoked to perform the semantic analysis
256 /// The return value is an expression (it can be the
257 /// same expression in some cases) or a new
258 /// expression that better represents this node.
260 /// For example, optimizations of Unary (LiteralInt)
261 /// would return a new LiteralInt with a negated
264 /// If there is an error during semantic analysis,
265 /// then an error should be reported (using Report)
266 /// and a null value should be returned.
268 /// There are two side effects expected from calling
269 /// Resolve(): the the field variable "eclass" should
270 /// be set to any value of the enumeration
271 /// 'ExprClass' and the type variable should be set
272 /// to a valid type (this is the type of the
275 public abstract Expression DoResolve (EmitContext ec);
277 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
279 return DoResolve (ec);
283 /// Resolves an expression and performs semantic analysis on it.
287 /// Currently Resolve wraps DoResolve to perform sanity
288 /// checking and assertion checking on what we expect from Resolve.
290 public Expression Resolve (EmitContext ec, ResolveFlags flags)
292 // Are we doing a types-only search ?
293 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
294 ITypeExpression type_expr = this as ITypeExpression;
296 if (type_expr == null)
299 return type_expr.DoResolveType (ec);
302 bool old_do_flow_analysis = ec.DoFlowAnalysis;
303 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
304 ec.DoFlowAnalysis = false;
308 if (this is SimpleName)
309 e = ((SimpleName) this).DoResolveAllowStatic (ec);
313 ec.DoFlowAnalysis = old_do_flow_analysis;
319 if (e is SimpleName){
320 SimpleName s = (SimpleName) e;
322 if ((flags & ResolveFlags.SimpleName) == 0) {
324 object lookup = TypeManager.MemberLookup (
325 ec.ContainerType, ec.ContainerType, AllMemberTypes,
326 AllBindingFlags | BindingFlags.NonPublic, s.Name);
328 Error (30390, "'" + s.Name + "' " +
329 "is inaccessible because of its protection level");
331 Error (30451, "The name '" + s.Name + "' could not be " +
332 "found in '" + ec.DeclSpace.Name + "'");
339 if ((e is TypeExpr) || (e is ComposedCast)) {
340 if ((flags & ResolveFlags.Type) == 0) {
350 if ((flags & ResolveFlags.VariableOrValue) == 0) {
356 case ExprClass.MethodGroup:
357 if ((flags & ResolveFlags.MethodGroup) == 0) {
358 MethodGroupExpr mg = (MethodGroupExpr) e;
359 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
360 Expression te = i.Resolve(ec);
361 //((MethodGroupExpr) e).ReportUsageError ();
367 case ExprClass.Value:
368 case ExprClass.Variable:
369 case ExprClass.PropertyAccess:
370 case ExprClass.EventAccess:
371 case ExprClass.IndexerAccess:
372 if ((flags & ResolveFlags.VariableOrValue) == 0) {
379 throw new Exception ("Expression " + e.GetType () +
380 " ExprClass is Invalid after resolve");
384 throw new Exception (
385 "Expression " + e.GetType () +
386 " did not set its type after Resolve\n" +
387 "called from: " + this.GetType ());
393 /// Resolves an expression and performs semantic analysis on it.
395 public Expression Resolve (EmitContext ec)
397 return Resolve (ec, ResolveFlags.VariableOrValue);
401 /// Resolves an expression for LValue assignment
405 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
406 /// checking and assertion checking on what we expect from Resolve
408 public Expression ResolveLValue (EmitContext ec, Expression right_side)
410 Expression e = DoResolveLValue (ec, right_side);
413 if (e is SimpleName){
414 SimpleName s = (SimpleName) e;
418 "The name '" + s.Name + "' could not be found in '" +
419 ec.DeclSpace.Name + "'");
423 if (e.eclass == ExprClass.Invalid)
424 throw new Exception ("Expression " + e +
425 " ExprClass is Invalid after resolve");
427 if (e.eclass == ExprClass.MethodGroup) {
428 MethodGroupExpr mg = (MethodGroupExpr) e;
429 Invocation i = new Invocation (mg, new ArrayList(), Location.Null);
430 Expression te = i.Resolve(ec);
432 //((MethodGroupExpr) e).ReportUsageError ();
437 throw new Exception ("Expression " + e +
438 " did not set its type after Resolve");
445 /// Emits the code for the expression
449 /// The Emit method is invoked to generate the code
450 /// for the expression.
452 public abstract void Emit (EmitContext ec);
455 /// Protected constructor. Only derivate types should
456 /// be able to be created
459 protected Expression ()
461 eclass = ExprClass.Invalid;
466 /// Returns a literalized version of a literal FieldInfo
470 /// The possible return values are:
471 /// IntConstant, UIntConstant
472 /// LongLiteral, ULongConstant
473 /// FloatConstant, DoubleConstant
476 /// The value returned is already resolved.
478 public static Constant Constantify (object v, Type t)
480 if (t == TypeManager.int32_type)
481 return new IntConstant ((int) v);
482 else if (t == TypeManager.uint32_type)
483 return new UIntConstant ((uint) v);
484 else if (t == TypeManager.int64_type)
485 return new LongConstant ((long) v);
486 else if (t == TypeManager.uint64_type)
487 return new ULongConstant ((ulong) v);
488 else if (t == TypeManager.float_type)
489 return new FloatConstant ((float) v);
490 else if (t == TypeManager.double_type)
491 return new DoubleConstant ((double) v);
492 else if (t == TypeManager.string_type)
493 return new StringConstant ((string) v);
494 else if (t == TypeManager.short_type)
495 return new ShortConstant ((short)v);
496 else if (t == TypeManager.ushort_type)
497 return new UShortConstant ((ushort)v);
498 else if (t == TypeManager.sbyte_type)
499 return new SByteConstant (((sbyte)v));
500 else if (t == TypeManager.byte_type)
501 return new ByteConstant ((byte)v);
502 else if (t == TypeManager.char_type)
503 return new CharConstant ((char)v);
504 else if (t == TypeManager.bool_type)
505 return new BoolConstant ((bool) v);
506 else if (TypeManager.IsEnumType (t)){
507 Constant e = Constantify (v, TypeManager.TypeToCoreType (v.GetType ()));
509 return new EnumConstant (e, t);
511 throw new Exception ("Unknown type for constant (" + t +
516 /// Returns a fully formed expression after a MemberLookup
518 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
521 return new EventExpr ((EventInfo) mi, loc);
522 else if (mi is FieldInfo)
523 return new FieldExpr ((FieldInfo) mi, loc);
524 else if (mi is PropertyInfo)
525 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
526 else if (mi is Type){
527 return new TypeExpr ((System.Type) mi, loc);
534 // FIXME: Probably implement a cache for (t,name,current_access_set)?
536 // This code could use some optimizations, but we need to do some
537 // measurements. For example, we could use a delegate to 'flag' when
538 // something can not any longer be a method-group (because it is something
542 // If the return value is an Array, then it is an array of
545 // If the return value is an MemberInfo, it is anything, but a Method
549 // FIXME: When calling MemberLookup inside an 'Invocation', we should pass
550 // the arguments here and have MemberLookup return only the methods that
551 // match the argument count/type, unlike we are doing now (we delay this
554 // This is so we can catch correctly attempts to invoke instance methods
555 // from a static body (scan for error 120 in ResolveSimpleName).
558 // FIXME: Potential optimization, have a static ArrayList
561 public static Expression MemberLookup (EmitContext ec, Type t, string name,
562 MemberTypes mt, BindingFlags bf, Location loc)
564 return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
568 // Lookup type 't' for code in class 'invocation_type'. Note that it's important
569 // to set 'invocation_type' correctly since this method also checks whether the
570 // invoking class is allowed to access the member in class 't'. When you want to
571 // explicitly do a lookup in the base class, you must set both 't' and 'invocation_type'
572 // to the base class (although a derived class can access protected members of its base
573 // class it cannot do so through an instance of the base class (error CS1540)).
576 public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
577 string name, MemberTypes mt, BindingFlags bf,
580 MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
585 int count = mi.Length;
588 return new MethodGroupExpr (mi, loc);
590 if (mi [0] is MethodBase)
591 return new MethodGroupExpr (mi, loc);
593 return ExprClassFromMemberInfo (ec, mi [0], loc);
596 public const MemberTypes AllMemberTypes =
597 MemberTypes.Constructor |
601 MemberTypes.NestedType |
602 MemberTypes.Property;
604 public const BindingFlags AllBindingFlags =
605 BindingFlags.Public |
606 BindingFlags.Static |
607 BindingFlags.Instance |
608 BindingFlags.IgnoreCase;
610 public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
612 return MemberLookup (ec, ec.ContainerType, t, name, AllMemberTypes, AllBindingFlags, loc);
615 public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
617 return MemberLookup (ec, ec.ContainerType, t, name,
618 MemberTypes.Method, AllBindingFlags, loc);
622 /// This is a wrapper for MemberLookup that is not used to "probe", but
623 /// to find a final definition. If the final definition is not found, we
624 /// look for private members and display a useful debugging message if we
627 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
630 return MemberLookupFinal (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
633 public static Expression MemberLookupFinal (EmitContext ec, Type t, string name,
634 MemberTypes mt, BindingFlags bf, Location loc)
638 int errors = Report.Errors;
640 e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
645 // Error has already been reported.
646 if (errors < Report.Errors)
649 e = MemberLookup (ec, t, name, AllMemberTypes,
650 AllBindingFlags | BindingFlags.NonPublic, loc);
653 30456, loc, "'" + t + "' does not contain a definition " +
654 "for '" + name + "'");
657 30390, loc, "'" + t + "." + name +
658 "' is inaccessible due to its protection level");
664 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
666 EventInfo ei = event_expr.EventInfo;
668 return TypeManager.GetPrivateFieldOfEvent (ei);
671 static EmptyExpression MyEmptyExpr;
672 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
674 Type expr_type = expr.Type;
676 if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
677 // if we are a method group, emit a warning
683 // notice that it is possible to write "ValueType v = 1", the ValueType here
684 // is an abstract class, and not really a value type, so we apply the same rules.
686 if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
688 // A pointer type cannot be converted to object
690 if (expr_type.IsPointer)
693 if (expr_type.IsValueType)
694 return new BoxedCast (expr);
695 if (expr_type.IsClass || expr_type.IsInterface)
696 return new EmptyCast (expr, target_type);
697 } else if (expr_type.IsSubclassOf (target_type)) {
699 // Special case: enumeration to System.Enum.
700 // System.Enum is not a value type, it is a class, so we need
701 // a boxing conversion
703 if (expr_type.IsEnum)
704 return new BoxedCast (expr);
706 return new EmptyCast (expr, target_type);
709 // This code is kind of mirrored inside StandardConversionExists
710 // with the small distinction that we only probe there
712 // Always ensure that the code here and there is in sync
714 // from the null type to any reference-type.
715 if (expr is NullLiteral && !target_type.IsValueType)
716 return new EmptyCast (expr, target_type);
718 // from any class-type S to any interface-type T.
719 if (target_type.IsInterface) {
720 if (TypeManager.ImplementsInterface (expr_type, target_type)){
721 if (expr_type.IsClass)
722 return new EmptyCast (expr, target_type);
723 else if (expr_type.IsValueType)
724 return new BoxedCast (expr);
728 // from any interface type S to interface-type T.
729 if (expr_type.IsInterface && target_type.IsInterface) {
730 if (TypeManager.ImplementsInterface (expr_type, target_type))
731 return new EmptyCast (expr, target_type);
736 // from an array-type S to an array-type of type T
737 if (expr_type.IsArray && target_type.IsArray) {
738 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
740 Type expr_element_type = expr_type.GetElementType ();
742 if (MyEmptyExpr == null)
743 MyEmptyExpr = new EmptyExpression ();
745 MyEmptyExpr.SetType (expr_element_type);
746 Type target_element_type = target_type.GetElementType ();
748 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
749 if (StandardConversionExists (MyEmptyExpr,
750 target_element_type))
751 return new EmptyCast (expr, target_type);
756 // from an array-type to System.Array
757 if (expr_type.IsArray && target_type == TypeManager.array_type)
758 return new EmptyCast (expr, target_type);
760 // from any delegate type to System.Delegate
761 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
762 target_type == TypeManager.delegate_type)
763 return new EmptyCast (expr, target_type);
765 // from any array-type or delegate type into System.ICloneable.
766 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
767 if (target_type == TypeManager.icloneable_type)
768 return new EmptyCast (expr, target_type);
778 /// Implicit Numeric Conversions.
780 /// expr is the expression to convert, returns a new expression of type
781 /// target_type or null if an implicit conversion is not possible.
783 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
784 Type target_type, Location loc)
786 Type expr_type = expr.Type;
789 // Attempt to do the implicit constant expression conversions
791 if (expr is IntConstant){
794 e = TryImplicitIntConversion (target_type, (IntConstant) expr);
798 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
800 // Try the implicit constant expression conversion
801 // from long to ulong, instead of a nice routine,
804 long v = ((LongConstant) expr).Value;
806 return new ULongConstant ((ulong) v);
809 Type real_target_type = target_type;
811 if (target_type == TypeManager.bool_type) {
813 if (expr_type == TypeManager.decimal_type) {
814 return RTConversionExpression (ec, "System.Convert",".ToBoolean" , expr, loc);
817 if ((expr_type != TypeManager.char_type) &&
818 (expr_type != TypeManager.string_type))
819 return new NumericToBoolCast (expr, expr.Type);
822 if (expr_type == TypeManager.bool_type){
824 if (real_target_type == TypeManager.sbyte_type)
825 return new BoolToNumericCast (expr, target_type);
826 if (real_target_type == TypeManager.byte_type)
827 return new BoolToNumericCast (expr, target_type);
828 if (real_target_type == TypeManager.int32_type)
829 return new BoolToNumericCast (expr, target_type);
830 if (real_target_type == TypeManager.int64_type)
831 return new BoolToNumericCast (expr, target_type);
832 if (real_target_type == TypeManager.double_type)
833 return new BoolToNumericCast (expr, target_type);
834 if (real_target_type == TypeManager.float_type)
835 return new BoolToNumericCast (expr, target_type);
836 if (real_target_type == TypeManager.short_type)
837 return new BoolToNumericCast (expr, target_type);
838 if (real_target_type == TypeManager.decimal_type)
839 return RTConversionExpression(ec, "DecimalType.FromBoolean", expr, loc);
840 } else if (expr_type == TypeManager.sbyte_type){
842 // From sbyte to short, int, long, float, double.
844 if (real_target_type == TypeManager.int32_type)
845 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
846 if (real_target_type == TypeManager.int64_type)
847 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
848 if (real_target_type == TypeManager.double_type)
849 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
850 if (real_target_type == TypeManager.float_type)
851 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
852 if (real_target_type == TypeManager.short_type)
853 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
854 } else if (expr_type == TypeManager.byte_type){
856 // From byte to short, ushort, int, uint, long, ulong, float, double
858 if ((real_target_type == TypeManager.short_type) ||
859 (real_target_type == TypeManager.ushort_type) ||
860 (real_target_type == TypeManager.int32_type) ||
861 (real_target_type == TypeManager.uint32_type))
862 return new EmptyCast (expr, target_type);
864 if (real_target_type == TypeManager.uint64_type)
865 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
866 if (real_target_type == TypeManager.int64_type)
867 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
868 if (real_target_type == TypeManager.float_type)
869 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
870 if (real_target_type == TypeManager.double_type)
871 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
872 } else if (expr_type == TypeManager.short_type){
874 // From short to int, long, float, double
876 if (real_target_type == TypeManager.int32_type)
877 return new EmptyCast (expr, target_type);
878 if (real_target_type == TypeManager.int64_type)
879 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
880 if (real_target_type == TypeManager.double_type)
881 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
882 if (real_target_type == TypeManager.float_type)
883 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
884 } else if (expr_type == TypeManager.ushort_type){
886 // From ushort to int, uint, long, ulong, float, double
888 if (real_target_type == TypeManager.uint32_type)
889 return new EmptyCast (expr, target_type);
891 if (real_target_type == TypeManager.uint64_type)
892 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
893 if (real_target_type == TypeManager.int32_type)
894 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
895 if (real_target_type == TypeManager.int64_type)
896 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
897 if (real_target_type == TypeManager.double_type)
898 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
899 if (real_target_type == TypeManager.float_type)
900 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
901 } else if (expr_type == TypeManager.int32_type){
903 // From int to long, float, double
905 if (real_target_type == TypeManager.int64_type)
906 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
907 if (real_target_type == TypeManager.double_type)
908 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
909 if (real_target_type == TypeManager.float_type)
910 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
911 } else if (expr_type == TypeManager.uint32_type){
913 // From uint to long, ulong, float, double
915 if (real_target_type == TypeManager.int64_type)
916 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
917 if (real_target_type == TypeManager.uint64_type)
918 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
919 if (real_target_type == TypeManager.double_type)
920 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
922 if (real_target_type == TypeManager.float_type)
923 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
925 } else if (expr_type == TypeManager.int64_type){
927 // From long/ulong to float, double
929 if (real_target_type == TypeManager.double_type)
930 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
931 if (real_target_type == TypeManager.float_type)
932 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
933 } else if (expr_type == TypeManager.uint64_type){
935 // From ulong to float, double
937 if (real_target_type == TypeManager.double_type)
938 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
940 if (real_target_type == TypeManager.float_type)
941 return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
943 } else if (expr_type == TypeManager.char_type){
945 // From char to ushort, int, uint, long, ulong, float, double
947 if ((real_target_type == TypeManager.ushort_type) ||
948 (real_target_type == TypeManager.int32_type) ||
949 (real_target_type == TypeManager.uint32_type))
950 return new EmptyCast (expr, target_type);
951 if (real_target_type == TypeManager.uint64_type)
952 return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
953 if (real_target_type == TypeManager.int64_type)
954 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
955 if (real_target_type == TypeManager.float_type)
956 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
957 if (real_target_type == TypeManager.double_type)
958 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
959 } else if (expr_type == TypeManager.string_type){
961 if (real_target_type == TypeManager.bool_type)
962 return RTConversionExpression (ec, "BooleanType.FromString" , expr, loc);
963 if (real_target_type == TypeManager.decimal_type)
964 return RTConversionExpression (ec, "DecimalType.FromString" , expr, loc);
965 if (real_target_type == TypeManager.float_type)
966 return RTConversionExpression (ec, "SingleType.FromString" , expr, loc);
967 if (real_target_type == TypeManager.short_type)
968 return RTConversionExpression (ec, "ShortType.FromString" , expr, loc);
969 if (real_target_type == TypeManager.int64_type)
970 return RTConversionExpression (ec, "LongType.FromString" , expr, loc);
971 if (real_target_type == TypeManager.int32_type)
972 return RTConversionExpression (ec, "IntegerType.FromString" , expr, loc);
973 if (real_target_type == TypeManager.double_type)
974 return RTConversionExpression (ec, "DoubleType.FromString" , expr, loc);
975 if (real_target_type == TypeManager.byte_type)
976 return RTConversionExpression (ec, "ByteType.FromString" , expr, loc);
977 } else if (expr_type == TypeManager.float_type){
981 if (real_target_type == TypeManager.decimal_type)
982 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
983 if (real_target_type == TypeManager.double_type)
984 return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
986 } else if (expr_type == TypeManager.double_type){
988 if (real_target_type == TypeManager.decimal_type)
989 return RTConversionExpression (ec, "System.Convert", ".ToDecimal" , expr, loc);
990 } else if (expr_type == TypeManager.decimal_type){
992 if (real_target_type == TypeManager.bool_type)
993 return RTConversionExpression (ec, "BooleanType.FromDecimal" , expr, loc);
994 if (real_target_type == TypeManager.short_type)
995 return RTConversionExpression(ec, "System.Convert", ".ToInt16", expr, loc);
996 if (real_target_type == TypeManager.byte_type)
997 return RTConversionExpression(ec, "System.Convert", ".ToByte", expr, loc);
998 if (real_target_type == TypeManager.int32_type)
999 return RTConversionExpression(ec, "System.Convert", ".ToInt32", expr, loc);
1000 if (real_target_type == TypeManager.int64_type)
1001 return RTConversionExpression(ec, "System.Convert", ".ToInt64", expr, loc);
1002 if (real_target_type == TypeManager.float_type)
1003 return RTConversionExpression(ec, "System.Convert", ".ToSingle", expr, loc);
1004 if (real_target_type == TypeManager.double_type)
1005 return RTConversionExpression(ec, "System.Convert", ".ToDouble", expr, loc);
1006 if (real_target_type == TypeManager.char_type)
1007 return RTConversionExpression(ec, "System.Convert", ".ToChar", expr, loc);
1014 // Tests whether an implicit reference conversion exists between expr_type
1017 public static bool ImplicitReferenceConversionExists (Expression expr, Type expr_type, Type target_type)
1020 // This is the boxed case.
1022 if (target_type == TypeManager.object_type) {
1023 if ((expr_type.IsClass) ||
1024 (expr_type.IsValueType) ||
1025 (expr_type.IsInterface))
1028 } else if (expr_type.IsSubclassOf (target_type)) {
1031 // Please remember that all code below actually comes
1032 // from ImplicitReferenceConversion so make sure code remains in sync
1034 // from any class-type S to any interface-type T.
1035 if (target_type.IsInterface) {
1036 if (TypeManager.ImplementsInterface (expr_type, target_type))
1040 // from any interface type S to interface-type T.
1041 if (expr_type.IsInterface && target_type.IsInterface)
1042 if (TypeManager.ImplementsInterface (expr_type, target_type))
1045 // from an array-type S to an array-type of type T
1046 if (expr_type.IsArray && target_type.IsArray) {
1047 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
1049 Type expr_element_type = expr_type.GetElementType ();
1051 if (MyEmptyExpr == null)
1052 MyEmptyExpr = new EmptyExpression ();
1054 MyEmptyExpr.SetType (expr_element_type);
1055 Type target_element_type = target_type.GetElementType ();
1057 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
1058 if (StandardConversionExists (MyEmptyExpr,
1059 target_element_type))
1064 // from an array-type to System.Array
1065 if (expr_type.IsArray && (target_type == TypeManager.array_type))
1068 // from any delegate type to System.Delegate
1069 if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
1070 target_type == TypeManager.delegate_type)
1071 if (target_type.IsAssignableFrom (expr_type))
1074 // from any array-type or delegate type into System.ICloneable.
1075 if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
1076 if (target_type == TypeManager.icloneable_type)
1079 // from the null type to any reference-type.
1080 if (expr is NullLiteral && !target_type.IsValueType &&
1081 !TypeManager.IsEnumType (target_type))
1090 /// Same as StandardConversionExists except that it also looks at
1091 /// implicit user defined conversions - needed for overload resolution
1093 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
1095 if (StandardConversionExists (expr, target_type) == true)
1099 Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
1109 /// Determines if a standard implicit conversion exists from
1110 /// expr_type to target_type
1112 public static bool StandardConversionExists (Expression expr, Type target_type)
1114 return WideningConversionExists (expr, expr.type, target_type);
1117 public static bool WideningConversionExists (Type expr_type, Type target_type)
1119 return WideningConversionExists (null, expr_type, target_type);
1122 public static bool WideningConversionExists (Expression expr, Type target_type)
1124 return WideningConversionExists (expr, expr.Type, target_type);
1127 public static bool WideningConversionExists (Expression expr, Type expr_type, Type target_type)
1130 if (expr_type == null || expr_type == TypeManager.void_type)
1133 if (expr_type == target_type)
1136 // Conversions from enum to underlying type are widening.
1137 if (expr_type.IsSubclassOf (TypeManager.enum_type))
1138 expr_type = TypeManager.EnumToUnderlying (expr_type);
1140 if (expr_type == target_type)
1143 // First numeric conversions
1145 if (expr_type == TypeManager.sbyte_type){
1147 // From sbyte to short, int, long, float, double.
1149 if ((target_type == TypeManager.int32_type) ||
1150 (target_type == TypeManager.int64_type) ||
1151 (target_type == TypeManager.double_type) ||
1152 (target_type == TypeManager.float_type) ||
1153 (target_type == TypeManager.short_type) ||
1154 (target_type == TypeManager.decimal_type))
1157 } else if (expr_type == TypeManager.byte_type){
1159 // From byte to short, ushort, int, uint, long, ulong, float, double
1161 if ((target_type == TypeManager.short_type) ||
1162 (target_type == TypeManager.bool_type) ||
1163 (target_type == TypeManager.ushort_type) ||
1164 (target_type == TypeManager.int32_type) ||
1165 (target_type == TypeManager.uint32_type) ||
1166 (target_type == TypeManager.uint64_type) ||
1167 (target_type == TypeManager.int64_type) ||
1168 (target_type == TypeManager.float_type) ||
1169 (target_type == TypeManager.double_type) ||
1170 (target_type == TypeManager.decimal_type))
1173 } else if (expr_type == TypeManager.short_type){
1175 // From short to int, long, float, double
1177 if ((target_type == TypeManager.int32_type) ||
1178 (target_type == TypeManager.bool_type) ||
1179 (target_type == TypeManager.int64_type) ||
1180 (target_type == TypeManager.double_type) ||
1181 (target_type == TypeManager.float_type) ||
1182 (target_type == TypeManager.decimal_type))
1185 } else if (expr_type == TypeManager.ushort_type){
1187 // From ushort to int, uint, long, ulong, float, double
1189 if ((target_type == TypeManager.uint32_type) ||
1190 (target_type == TypeManager.uint64_type) ||
1191 (target_type == TypeManager.int32_type) ||
1192 (target_type == TypeManager.int64_type) ||
1193 (target_type == TypeManager.double_type) ||
1194 (target_type == TypeManager.float_type) ||
1195 (target_type == TypeManager.decimal_type))
1198 } else if (expr_type == TypeManager.int32_type){
1200 // From int to long, float, double
1202 if ((target_type == TypeManager.int64_type) ||
1203 (target_type == TypeManager.bool_type) ||
1204 (target_type == TypeManager.double_type) ||
1205 (target_type == TypeManager.float_type) ||
1206 (target_type == TypeManager.decimal_type))
1209 } else if (expr_type == TypeManager.uint32_type){
1211 // From uint to long, ulong, float, double
1213 if ((target_type == TypeManager.int64_type) ||
1214 (target_type == TypeManager.bool_type) ||
1215 (target_type == TypeManager.uint64_type) ||
1216 (target_type == TypeManager.double_type) ||
1217 (target_type == TypeManager.float_type) ||
1218 (target_type == TypeManager.decimal_type))
1221 } else if ((expr_type == TypeManager.uint64_type) ||
1222 (expr_type == TypeManager.int64_type)) {
1224 // From long/ulong to float, double
1226 if ((target_type == TypeManager.double_type) ||
1227 (target_type == TypeManager.bool_type) ||
1228 (target_type == TypeManager.float_type) ||
1229 (target_type == TypeManager.decimal_type))
1232 } else if (expr_type == TypeManager.char_type){
1234 // From char to ushort, int, uint, long, ulong, float, double
1236 if ((target_type == TypeManager.ushort_type) ||
1237 (target_type == TypeManager.int32_type) ||
1238 (target_type == TypeManager.uint32_type) ||
1239 (target_type == TypeManager.uint64_type) ||
1240 (target_type == TypeManager.int64_type) ||
1241 (target_type == TypeManager.float_type) ||
1242 (target_type == TypeManager.double_type) ||
1243 (target_type == TypeManager.string_type) ||
1244 (target_type == TypeManager.decimal_type))
1247 } else if (expr_type == TypeManager.float_type){
1249 // float to double, decimal
1251 if (target_type == TypeManager.double_type)
1253 } else if (expr_type == TypeManager.double_type){
1255 if ((target_type == TypeManager.bool_type))
1259 if (ImplicitReferenceConversionExists (expr, expr_type, target_type))
1262 if (expr is IntConstant){
1263 int value = ((IntConstant) expr).Value;
1265 if (target_type == TypeManager.sbyte_type){
1266 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1268 } else if (target_type == TypeManager.byte_type){
1269 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
1271 } else if (target_type == TypeManager.short_type){
1272 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1274 } else if (target_type == TypeManager.ushort_type){
1275 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1277 } else if (target_type == TypeManager.uint32_type){
1280 } else if (target_type == TypeManager.uint64_type){
1282 // we can optimize this case: a positive int32
1283 // always fits on a uint64. But we need an opcode
1290 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
1294 if (expr is LongConstant && target_type == TypeManager.uint64_type){
1296 // Try the implicit constant expression conversion
1297 // from long to ulong, instead of a nice routine,
1298 // we just inline it
1300 long v = ((LongConstant) expr).Value;
1305 if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
1306 IntLiteral i = (IntLiteral) expr;
1312 if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
1319 // Used internally by FindMostEncompassedType, this is used
1320 // to avoid creating lots of objects in the tight loop inside
1321 // FindMostEncompassedType
1323 static EmptyExpression priv_fmet_param;
1326 /// Finds "most encompassed type" according to the spec (13.4.2)
1327 /// amongst the methods in the MethodGroupExpr
1329 static Type FindMostEncompassedType (ArrayList types)
1333 if (priv_fmet_param == null)
1334 priv_fmet_param = new EmptyExpression ();
1336 foreach (Type t in types){
1337 priv_fmet_param.SetType (t);
1344 if (StandardConversionExists (priv_fmet_param, best))
1352 // Used internally by FindMostEncompassingType, this is used
1353 // to avoid creating lots of objects in the tight loop inside
1354 // FindMostEncompassingType
1356 static EmptyExpression priv_fmee_ret;
1359 /// Finds "most encompassing type" according to the spec (13.4.2)
1360 /// amongst the types in the given set
1362 static Type FindMostEncompassingType (ArrayList types)
1366 if (priv_fmee_ret == null)
1367 priv_fmee_ret = new EmptyExpression ();
1369 foreach (Type t in types){
1370 priv_fmee_ret.SetType (best);
1377 if (StandardConversionExists (priv_fmee_ret, t))
1385 // Used to avoid creating too many objects
1387 static EmptyExpression priv_fms_expr;
1390 /// Finds the most specific source Sx according to the rules of the spec (13.4.4)
1391 /// by making use of FindMostEncomp* methods. Applies the correct rules separately
1392 /// for explicit and implicit conversion operators.
1394 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
1395 bool apply_explicit_conv_rules,
1398 ArrayList src_types_set = new ArrayList ();
1400 if (priv_fms_expr == null)
1401 priv_fms_expr = new EmptyExpression ();
1404 // If any operator converts from S then Sx = S
1406 Type source_type= source.Type;
1407 foreach (MethodBase mb in me.Methods){
1408 ParameterData pd = Invocation.GetParameterData (mb);
1409 Type param_type = pd.ParameterType (0);
1411 if (param_type == source_type)
1414 if (apply_explicit_conv_rules) {
1417 // Find the set of applicable user-defined conversion operators, U. This set
1419 // user-defined implicit or explicit conversion operators declared by
1420 // the classes or structs in D that convert from a type encompassing
1421 // or encompassed by S to a type encompassing or encompassed by T
1423 priv_fms_expr.SetType (param_type);
1424 if (StandardConversionExists (priv_fms_expr, source_type))
1425 src_types_set.Add (param_type);
1427 if (StandardConversionExists (source, param_type))
1428 src_types_set.Add (param_type);
1432 // Only if S is encompassed by param_type
1434 if (StandardConversionExists (source, param_type))
1435 src_types_set.Add (param_type);
1440 // Explicit Conv rules
1442 if (apply_explicit_conv_rules) {
1443 ArrayList candidate_set = new ArrayList ();
1445 foreach (Type param_type in src_types_set){
1446 if (StandardConversionExists (source, param_type))
1447 candidate_set.Add (param_type);
1450 if (candidate_set.Count != 0)
1451 return FindMostEncompassedType (candidate_set);
1457 if (apply_explicit_conv_rules)
1458 return FindMostEncompassingType (src_types_set);
1460 return FindMostEncompassedType (src_types_set);
1464 // Useful in avoiding proliferation of objects
1466 static EmptyExpression priv_fmt_expr;
1469 /// Finds the most specific target Tx according to section 13.4.4
1471 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
1472 bool apply_explicit_conv_rules,
1475 ArrayList tgt_types_set = new ArrayList ();
1477 if (priv_fmt_expr == null)
1478 priv_fmt_expr = new EmptyExpression ();
1481 // If any operator converts to T then Tx = T
1483 foreach (MethodInfo mi in me.Methods){
1484 Type ret_type = mi.ReturnType;
1486 if (ret_type == target)
1489 if (apply_explicit_conv_rules) {
1492 // Find the set of applicable user-defined conversion operators, U.
1494 // This set consists of the
1495 // user-defined implicit or explicit conversion operators declared by
1496 // the classes or structs in D that convert from a type encompassing
1497 // or encompassed by S to a type encompassing or encompassed by T
1499 priv_fms_expr.SetType (ret_type);
1500 if (StandardConversionExists (priv_fms_expr, target))
1501 tgt_types_set.Add (ret_type);
1503 priv_fms_expr.SetType (target);
1504 if (StandardConversionExists (priv_fms_expr, ret_type))
1505 tgt_types_set.Add (ret_type);
1509 // Only if T is encompassed by param_type
1511 priv_fms_expr.SetType (ret_type);
1512 if (StandardConversionExists (priv_fms_expr, target))
1513 tgt_types_set.Add (ret_type);
1518 // Explicit conv rules
1520 if (apply_explicit_conv_rules) {
1521 ArrayList candidate_set = new ArrayList ();
1523 foreach (Type ret_type in tgt_types_set){
1524 priv_fmt_expr.SetType (ret_type);
1526 if (StandardConversionExists (priv_fmt_expr, target))
1527 candidate_set.Add (ret_type);
1530 if (candidate_set.Count != 0)
1531 return FindMostEncompassingType (candidate_set);
1535 // Okay, final case !
1537 if (apply_explicit_conv_rules)
1538 return FindMostEncompassedType (tgt_types_set);
1540 return FindMostEncompassingType (tgt_types_set);
1544 /// User-defined Implicit conversions
1546 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1547 Type target, Location loc)
1549 return UserDefinedConversion (ec, source, target, loc, false);
1553 /// User-defined Explicit conversions
1555 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1556 Type target, Location loc)
1558 return UserDefinedConversion (ec, source, target, loc, true);
1562 /// Computes the MethodGroup for the user-defined conversion
1563 /// operators from source_type to target_type. 'look_for_explicit'
1564 /// controls whether we should also include the list of explicit
1567 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1568 Type source_type, Type target_type,
1569 Location loc, bool look_for_explicit)
1571 Expression mg1 = null, mg2 = null;
1572 Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1576 // FIXME : How does the False operator come into the picture ?
1577 // This doesn't look complete and very correct !
1579 if (target_type == TypeManager.bool_type && !look_for_explicit)
1580 op_name = "op_True";
1582 op_name = "op_Implicit";
1584 MethodGroupExpr union3;
1586 mg1 = MethodLookup (ec, source_type, op_name, loc);
1587 if (source_type.BaseType != null)
1588 mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1591 union3 = (MethodGroupExpr) mg2;
1592 else if (mg2 == null)
1593 union3 = (MethodGroupExpr) mg1;
1595 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1597 mg1 = MethodLookup (ec, target_type, op_name, loc);
1600 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1602 union3 = (MethodGroupExpr) mg1;
1605 if (target_type.BaseType != null)
1606 mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1610 union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1612 union3 = (MethodGroupExpr) mg1;
1615 MethodGroupExpr union4 = null;
1617 if (look_for_explicit) {
1618 op_name = "op_Explicit";
1620 mg5 = MemberLookup (ec, source_type, op_name, loc);
1621 if (source_type.BaseType != null)
1622 mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
1624 mg7 = MemberLookup (ec, target_type, op_name, loc);
1625 if (target_type.BaseType != null)
1626 mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
1628 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1629 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1631 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1634 return Invocation.MakeUnionSet (union3, union4, loc);
1638 /// User-defined conversions
1640 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1641 Type target, Location loc,
1642 bool look_for_explicit)
1644 MethodGroupExpr union;
1645 Type source_type = source.Type;
1646 MethodBase method = null;
1648 union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1652 Type most_specific_source, most_specific_target;
1655 foreach (MethodBase m in union.Methods){
1656 Console.WriteLine ("Name: " + m.Name);
1657 Console.WriteLine (" : " + ((MethodInfo)m).ReturnType);
1661 most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
1662 if (most_specific_source == null)
1665 most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
1666 if (most_specific_target == null)
1671 foreach (MethodBase mb in union.Methods){
1672 ParameterData pd = Invocation.GetParameterData (mb);
1673 MethodInfo mi = (MethodInfo) mb;
1675 if (pd.ParameterType (0) == most_specific_source &&
1676 mi.ReturnType == most_specific_target) {
1682 if (method == null || count > 1)
1687 // This will do the conversion to the best match that we
1688 // found. Now we need to perform an implict standard conversion
1689 // if the best match was not the type that we were requested
1692 if (look_for_explicit)
1693 source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
1695 source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
1701 e = new UserCast ((MethodInfo) method, source, loc);
1702 if (e.Type != target){
1703 if (!look_for_explicit)
1704 e = ConvertImplicitStandard (ec, e, target, loc);
1706 e = ConvertExplicitStandard (ec, e, target, loc);
1712 /// Converts implicitly the resolved expression 'expr' into the
1713 /// 'target_type'. It returns a new expression that can be used
1714 /// in a context that expects a 'target_type'.
1716 static public Expression ConvertImplicit (EmitContext ec, Expression expr,
1717 Type target_type, Location loc)
1719 Type expr_type = expr.Type;
1723 if (expr_type == target_type)
1726 if (target_type == null)
1727 throw new Exception ("Target type is null");
1729 e = ConvertImplicitStandard (ec, expr, target_type, loc);
1733 e = ImplicitUserConversion (ec, expr, target_type, loc);
1738 e = NarrowingConversion (ec, expr, target_type, loc);
1746 /// Converts the resolved expression 'expr' into the
1747 /// 'target_type' using the Microsoft.VisualBasic runtime.
1748 /// It returns a new expression that can be used
1749 /// in a context that expects a 'target_type'.
1751 static private Expression RTConversionExpression (EmitContext ec, string s, Expression expr, Location loc)
1757 etmp = Mono.MonoBASIC.Parser.DecomposeQI("Microsoft.VisualBasic.CompilerServices." + s, loc);
1758 args = new ArrayList();
1759 arg = new Argument (expr, Argument.AType.Expression);
1761 e = (Expression) new Invocation (etmp, args, loc);
1766 static private Expression RTConversionExpression (EmitContext ec, string ns, string method, Expression expr, Location loc)
1772 etmp = Mono.MonoBASIC.Parser.DecomposeQI(ns+method, loc);
1773 args = new ArrayList();
1774 arg = new Argument (expr, Argument.AType.Expression);
1776 e = (Expression) new Invocation (etmp, args, loc);
1782 static public bool NarrowingConversionExists (EmitContext ec, Expression expr, Type target_type)
1784 Type expr_type = expr.Type;
1786 if (target_type == TypeManager.sbyte_type){
1788 // To sbyte from short, int, long, float, double.
1790 if ((expr_type == TypeManager.int32_type) ||
1791 (expr_type == TypeManager.int64_type) ||
1792 (expr_type == TypeManager.double_type) ||
1793 (expr_type == TypeManager.float_type) ||
1794 (expr_type == TypeManager.short_type) ||
1795 (expr_type == TypeManager.decimal_type))
1798 } else if (target_type == TypeManager.byte_type){
1800 // To byte from short, ushort, int, uint, long, ulong, float, double
1802 if ((expr_type == TypeManager.short_type) ||
1803 (expr_type == TypeManager.ushort_type) ||
1804 (expr_type == TypeManager.int32_type) ||
1805 (expr_type == TypeManager.uint32_type) ||
1806 (expr_type == TypeManager.uint64_type) ||
1807 (expr_type == TypeManager.int64_type) ||
1808 (expr_type == TypeManager.float_type) ||
1809 (expr_type == TypeManager.double_type) ||
1810 (expr_type == TypeManager.decimal_type))
1813 } else if (target_type == TypeManager.short_type){
1815 // To short from int, long, float, double
1817 if ((expr_type == TypeManager.int32_type) ||
1818 (expr_type == TypeManager.int64_type) ||
1819 (expr_type == TypeManager.double_type) ||
1820 (expr_type == TypeManager.float_type) ||
1821 (expr_type == TypeManager.decimal_type))
1824 } else if (target_type == TypeManager.ushort_type){
1826 // To ushort from int, uint, long, ulong, float, double
1828 if ((expr_type == TypeManager.uint32_type) ||
1829 (expr_type == TypeManager.uint64_type) ||
1830 (expr_type == TypeManager.int32_type) ||
1831 (expr_type == TypeManager.int64_type) ||
1832 (expr_type == TypeManager.double_type) ||
1833 (expr_type == TypeManager.float_type) ||
1834 (expr_type == TypeManager.decimal_type))
1837 } else if (target_type == TypeManager.int32_type){
1839 // To int from long, float, double
1841 if ((expr_type == TypeManager.int64_type) ||
1842 (expr_type == TypeManager.double_type) ||
1843 (expr_type == TypeManager.float_type) ||
1844 (expr_type == TypeManager.decimal_type))
1847 } else if (target_type == TypeManager.uint32_type){
1849 // To uint from long, ulong, float, double
1851 if ((expr_type == TypeManager.int64_type) ||
1852 (expr_type == TypeManager.uint64_type) ||
1853 (expr_type == TypeManager.double_type) ||
1854 (expr_type == TypeManager.float_type) ||
1855 (expr_type == TypeManager.decimal_type))
1858 } else if ((target_type == TypeManager.uint64_type) ||
1859 (target_type == TypeManager.int64_type)) {
1861 // To long/ulong from float, double
1863 if ((expr_type == TypeManager.double_type) ||
1864 (expr_type == TypeManager.float_type) ||
1865 (expr_type == TypeManager.decimal_type))
1868 } else if (target_type == TypeManager.char_type){
1870 // To char from ushort, int, uint, long, ulong, float, double
1872 if ((expr_type == TypeManager.ushort_type) ||
1873 (expr_type == TypeManager.int32_type) ||
1874 (expr_type == TypeManager.uint32_type) ||
1875 (expr_type == TypeManager.uint64_type) ||
1876 (expr_type == TypeManager.int64_type) ||
1877 (expr_type == TypeManager.float_type) ||
1878 (expr_type == TypeManager.double_type) ||
1879 (expr_type == TypeManager.decimal_type))
1882 } else if (target_type == TypeManager.float_type){
1884 // To float from double
1886 if (expr_type == TypeManager.double_type)
1890 return (NarrowingConversion (ec, expr, target_type,Location.Null)) != null;
1893 static public Expression NarrowingConversion (EmitContext ec, Expression expr,
1894 Type target_type, Location loc)
1896 Type expr_type = expr.Type;
1898 if (target_type == TypeManager.sbyte_type){
1900 // To sbyte from short, int, long, float, double.
1902 if ((expr_type == TypeManager.int32_type) ||
1903 (expr_type == TypeManager.int64_type) ||
1904 (expr_type == TypeManager.double_type) ||
1905 (expr_type == TypeManager.float_type) ||
1906 (expr_type == TypeManager.short_type) ||
1907 (expr_type == TypeManager.decimal_type))
1908 return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1910 } else if (target_type == TypeManager.byte_type){
1912 // To byte from short, ushort, int, uint, long, ulong, float, double
1914 if ((expr_type == TypeManager.short_type) ||
1915 (expr_type == TypeManager.ushort_type) ||
1916 (expr_type == TypeManager.int32_type) ||
1917 (expr_type == TypeManager.uint32_type) ||
1918 (expr_type == TypeManager.uint64_type) ||
1919 (expr_type == TypeManager.int64_type) ||
1920 (expr_type == TypeManager.float_type) ||
1921 (expr_type == TypeManager.double_type) ||
1922 (expr_type == TypeManager.decimal_type))
1923 return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1925 } else if (target_type == TypeManager.short_type){
1927 // To short from int, long, float, double
1929 if ((expr_type == TypeManager.int32_type) ||
1930 (expr_type == TypeManager.int64_type) ||
1931 (expr_type == TypeManager.double_type) ||
1932 (expr_type == TypeManager.float_type) ||
1933 (expr_type == TypeManager.decimal_type))
1935 return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1937 } else if (target_type == TypeManager.ushort_type){
1939 // To ushort from int, uint, long, ulong, float, double
1941 if ((expr_type == TypeManager.uint32_type) ||
1942 (expr_type == TypeManager.uint64_type) ||
1943 (expr_type == TypeManager.int32_type) ||
1944 (expr_type == TypeManager.int64_type) ||
1945 (expr_type == TypeManager.double_type) ||
1946 (expr_type == TypeManager.float_type) ||
1947 (expr_type == TypeManager.decimal_type))
1948 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1950 } else if (target_type == TypeManager.int32_type){
1952 // To int from long, float, double
1954 if ((expr_type == TypeManager.int64_type) ||
1955 (expr_type == TypeManager.double_type) ||
1956 (expr_type == TypeManager.float_type) ||
1957 (expr_type == TypeManager.decimal_type))
1958 return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1960 } else if (target_type == TypeManager.uint32_type){
1962 // To uint from long, ulong, float, double
1964 if ((expr_type == TypeManager.int64_type) ||
1965 (expr_type == TypeManager.uint64_type) ||
1966 (expr_type == TypeManager.double_type) ||
1967 (expr_type == TypeManager.float_type) ||
1968 (expr_type == TypeManager.decimal_type))
1969 return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1971 } else if ((target_type == TypeManager.uint64_type) ||
1972 (target_type == TypeManager.int64_type)) {
1974 // To long/ulong from float, double
1976 if ((expr_type == TypeManager.double_type) ||
1977 (expr_type == TypeManager.float_type) ||
1978 (expr_type == TypeManager.decimal_type))
1979 return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1981 } else if (target_type == TypeManager.char_type){
1983 // To char from ushort, int, uint, long, ulong, float, double
1985 if ((expr_type == TypeManager.ushort_type) ||
1986 (expr_type == TypeManager.int32_type) ||
1987 (expr_type == TypeManager.uint32_type) ||
1988 (expr_type == TypeManager.uint64_type) ||
1989 (expr_type == TypeManager.int64_type) ||
1990 (expr_type == TypeManager.float_type) ||
1991 (expr_type == TypeManager.double_type) ||
1992 (expr_type == TypeManager.decimal_type))
1993 return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1995 } else if (target_type == TypeManager.float_type){
1997 // To float from double
1999 if (expr_type == TypeManager.double_type)
2000 return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
2003 TypeCode dest_type = Type.GetTypeCode (target_type);
2004 TypeCode src_type = Type.GetTypeCode (expr_type);
2005 Expression e = null;
2007 // VB.NET Objects can be converted to anything by default
2008 // unless, that is, an exception at runtime blows it all
2009 if (src_type == TypeCode.Object) {
2010 Expression cast_type = Mono.MonoBASIC.Parser.DecomposeQI(target_type.ToString(), loc);
2011 Cast ce = new Cast (cast_type, expr, loc);
2012 ce.IsRuntimeCast = true;
2013 return ce.Resolve (ec);
2016 switch (dest_type) {
2017 case TypeCode.String:
2019 case TypeCode.SByte:
2021 e = RTConversionExpression(ec, "StringType.FromByte", expr, loc);
2023 case TypeCode.UInt16:
2024 case TypeCode.Int16:
2025 e = RTConversionExpression(ec, "StringType.FromShort", expr, loc);
2027 case TypeCode.UInt32:
2028 case TypeCode.Int32:
2029 e = RTConversionExpression(ec, "StringType.FromInteger", expr, loc);
2031 case TypeCode.UInt64:
2032 case TypeCode.Int64:
2033 e = RTConversionExpression(ec, "StringType.FromLong", expr, loc);
2036 e = RTConversionExpression(ec, "StringType.FromChar", expr, loc);
2038 case TypeCode.Single:
2039 e = RTConversionExpression(ec, "StringType.FromSingle", expr, loc);
2041 case TypeCode.Double:
2042 e = RTConversionExpression(ec, "StringType.FromDouble", expr, loc);
2044 case TypeCode.Boolean:
2045 e = RTConversionExpression(ec, "StringType.FromBoolean", expr, loc);
2047 case TypeCode.DateTime:
2048 e = RTConversionExpression(ec, "StringType.FromDate", expr, loc);
2050 case TypeCode.Decimal:
2051 e = RTConversionExpression(ec, "StringType.FromDecimal", expr, loc);
2053 case TypeCode.Object:
2054 e = RTConversionExpression(ec, "StringType.FromObject", expr, loc);
2059 case TypeCode.Int32:
2060 case TypeCode.UInt32:
2062 case TypeCode.String:
2063 e = RTConversionExpression(ec, "IntegerType.FromString", expr, loc);
2065 case TypeCode.Object:
2066 e = RTConversionExpression(ec, "IntegerType.FromObject", expr, loc);
2071 case TypeCode.Int16:
2072 case TypeCode.UInt16:
2074 case TypeCode.String:
2075 e = RTConversionExpression(ec, "ShortType.FromString", expr, loc);
2077 case TypeCode.Object:
2078 e = RTConversionExpression(ec, "ShortType.FromObject", expr, loc);
2083 // Ok, this *is* broken
2084 e = RTConversionExpression(ec, "ByteType.FromObject", expr, loc);
2086 case TypeCode.DateTime:
2088 case TypeCode.String:
2089 e = RTConversionExpression(ec, "DateType.FromString", expr, loc);
2091 case TypeCode.Object:
2092 e = RTConversionExpression(ec, "DateType.FromObject", expr, loc);
2098 // We must examine separately some types that
2099 // don't have a TypeCode but are supported
2101 if (expr_type == typeof(System.String) && target_type == typeof (System.Char[])) {
2102 e = RTConversionExpression(ec, "CharArrayType.FromString", expr, loc);
2109 /// Attempts to apply the 'Standard Implicit
2110 /// Conversion' rules to the expression 'expr' into
2111 /// the 'target_type'. It returns a new expression
2112 /// that can be used in a context that expects a
2115 /// This is different from 'ConvertImplicit' in that the
2116 /// user defined implicit conversions are excluded.
2118 static public Expression ConvertImplicitStandard (EmitContext ec, Expression expr,
2119 Type target_type, Location loc)
2121 Type expr_type = expr.Type;
2124 if (expr_type == target_type)
2127 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2132 e = ImplicitReferenceConversion (expr, target_type);
2136 if (expr.Type.IsSubclassOf (TypeManager.enum_type)) {
2137 expr_type = TypeManager.EnumToUnderlying (expr.Type);
2138 expr = new EmptyCast (expr, expr_type);
2139 if (expr_type == target_type)
2141 e = ImplicitNumericConversion (ec, expr, target_type, loc);
2147 if (expr_type.IsPointer){
2148 if (target_type == TypeManager.void_ptr_type)
2149 return new EmptyCast (expr, target_type);
2152 // yep, comparing pointer types cant be done with
2153 // t1 == t2, we have to compare their element types.
2155 if (target_type.IsPointer){
2156 if (target_type.GetElementType()==expr_type.GetElementType())
2161 if (target_type.IsPointer){
2162 if (expr is NullLiteral)
2163 return new EmptyCast (expr, target_type);
2171 /// Attemps to perform an implict constant conversion of the IntConstant
2172 /// into a different data type using casts (See Implicit Constant
2173 /// Expression Conversions)
2175 static protected Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
2177 int value = ic.Value;
2180 // FIXME: This could return constants instead of EmptyCasts
2182 if (target_type == TypeManager.sbyte_type){
2183 if (value >= SByte.MinValue && value <= SByte.MaxValue)
2184 return new SByteConstant ((sbyte) value);
2185 } else if (target_type == TypeManager.byte_type){
2186 if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
2187 return new ByteConstant ((byte) value);
2188 } else if (target_type == TypeManager.short_type){
2189 if (value >= Int16.MinValue && value <= Int16.MaxValue)
2190 return new ShortConstant ((short) value);
2191 } else if (target_type == TypeManager.ushort_type){
2192 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
2193 return new UShortConstant ((ushort) value);
2194 } else if (target_type == TypeManager.uint32_type){
2196 return new UIntConstant ((uint) value);
2197 } else if (target_type == TypeManager.uint64_type){
2199 // we can optimize this case: a positive int32
2200 // always fits on a uint64. But we need an opcode
2204 return new ULongConstant ((ulong) value);
2207 if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
2208 Type underlying = TypeManager.EnumToUnderlying (target_type);
2209 Constant e = (Constant) ic;
2212 // Possibly, we need to create a different 0 literal before passing
2215 if (underlying == TypeManager.int64_type)
2216 e = new LongLiteral (0);
2217 else if (underlying == TypeManager.uint64_type)
2218 e = new ULongLiteral (0);
2220 return new EnumConstant (e, target_type);
2225 static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
2227 string msg = "Cannot convert implicitly from '"+
2228 TypeManager.MonoBASIC_Name (source) + "' to '" +
2229 TypeManager.MonoBASIC_Name (target) + "'";
2231 throw new Exception (msg);
2233 Report.Error (30512, loc, msg);
2237 /// Attemptes to implicityly convert 'target' into 'type', using
2238 /// ConvertImplicit. If there is no implicit conversion, then
2239 /// an error is signaled
2241 static public Expression ConvertImplicitRequired (EmitContext ec, Expression source,
2242 Type target_type, Location loc)
2246 e = ConvertImplicit (ec, source, target_type, loc);
2252 if (source is DoubleLiteral && target_type == TypeManager.float_type){
2253 Report.Error (664, loc,
2254 "Double literal cannot be implicitly converted to " +
2255 "float type, use F suffix to create a float literal");
2258 Error_CannotConvertImplicit (loc, source.Type, target_type);
2264 /// Performs the explicit numeric conversions
2266 static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
2268 Type expr_type = expr.Type;
2271 // If we have an enumeration, extract the underlying type,
2272 // use this during the comparison, but wrap around the original
2275 Type real_target_type = target_type;
2277 if (TypeManager.IsEnumType (real_target_type))
2278 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
2280 if (StandardConversionExists (expr, real_target_type)){
2281 Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
2283 if (real_target_type != target_type)
2284 return new EmptyCast (ce, target_type);
2288 if (expr_type == TypeManager.sbyte_type){
2290 // From sbyte to byte, ushort, uint, ulong, char
2292 if (real_target_type == TypeManager.byte_type)
2293 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
2294 if (real_target_type == TypeManager.ushort_type)
2295 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
2296 if (real_target_type == TypeManager.uint32_type)
2297 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
2298 if (real_target_type == TypeManager.uint64_type)
2299 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
2300 if (real_target_type == TypeManager.char_type)
2301 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
2302 } else if (expr_type == TypeManager.byte_type){
2304 // From byte to sbyte and char
2306 if (real_target_type == TypeManager.sbyte_type)
2307 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
2308 if (real_target_type == TypeManager.char_type)
2309 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
2310 } else if (expr_type == TypeManager.short_type){
2312 // From short to sbyte, byte, ushort, uint, ulong, char
2314 if (real_target_type == TypeManager.sbyte_type)
2315 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
2316 if (real_target_type == TypeManager.byte_type)
2317 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
2318 if (real_target_type == TypeManager.ushort_type)
2319 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
2320 if (real_target_type == TypeManager.uint32_type)
2321 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
2322 if (real_target_type == TypeManager.uint64_type)
2323 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
2324 if (real_target_type == TypeManager.char_type)
2325 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
2326 } else if (expr_type == TypeManager.ushort_type){
2328 // From ushort to sbyte, byte, short, char
2330 if (real_target_type == TypeManager.sbyte_type)
2331 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
2332 if (real_target_type == TypeManager.byte_type)
2333 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
2334 if (real_target_type == TypeManager.short_type)
2335 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
2336 if (real_target_type == TypeManager.char_type)
2337 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
2338 } else if (expr_type == TypeManager.int32_type){
2340 // From int to sbyte, byte, short, ushort, uint, ulong, char
2342 if (real_target_type == TypeManager.sbyte_type)
2343 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
2344 if (real_target_type == TypeManager.byte_type)
2345 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
2346 if (real_target_type == TypeManager.short_type)
2347 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
2348 if (real_target_type == TypeManager.ushort_type)
2349 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
2350 if (real_target_type == TypeManager.uint32_type)
2351 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
2352 if (real_target_type == TypeManager.uint64_type)
2353 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
2354 if (real_target_type == TypeManager.char_type)
2355 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
2356 } else if (expr_type == TypeManager.uint32_type){
2358 // From uint to sbyte, byte, short, ushort, int, char
2360 if (real_target_type == TypeManager.sbyte_type)
2361 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
2362 if (real_target_type == TypeManager.byte_type)
2363 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
2364 if (real_target_type == TypeManager.short_type)
2365 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
2366 if (real_target_type == TypeManager.ushort_type)
2367 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
2368 if (real_target_type == TypeManager.int32_type)
2369 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
2370 if (real_target_type == TypeManager.char_type)
2371 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
2372 } else if (expr_type == TypeManager.int64_type){
2374 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
2376 if (real_target_type == TypeManager.sbyte_type)
2377 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
2378 if (real_target_type == TypeManager.byte_type)
2379 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
2380 if (real_target_type == TypeManager.short_type)
2381 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
2382 if (real_target_type == TypeManager.ushort_type)
2383 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
2384 if (real_target_type == TypeManager.int32_type)
2385 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
2386 if (real_target_type == TypeManager.uint32_type)
2387 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
2388 if (real_target_type == TypeManager.uint64_type)
2389 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
2390 if (real_target_type == TypeManager.char_type)
2391 return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
2392 } else if (expr_type == TypeManager.uint64_type){
2394 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
2396 if (real_target_type == TypeManager.sbyte_type)
2397 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
2398 if (real_target_type == TypeManager.byte_type)
2399 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
2400 if (real_target_type == TypeManager.short_type)
2401 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
2402 if (real_target_type == TypeManager.ushort_type)
2403 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
2404 if (real_target_type == TypeManager.int32_type)
2405 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
2406 if (real_target_type == TypeManager.uint32_type)
2407 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
2408 if (real_target_type == TypeManager.int64_type)
2409 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
2410 if (real_target_type == TypeManager.char_type)
2411 return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
2412 } else if (expr_type == TypeManager.char_type){
2414 // From char to sbyte, byte, short
2416 if (real_target_type == TypeManager.sbyte_type)
2417 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
2418 if (real_target_type == TypeManager.byte_type)
2419 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
2420 if (real_target_type == TypeManager.short_type)
2421 return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
2422 } else if (expr_type == TypeManager.float_type){
2424 // From float to sbyte, byte, short,
2425 // ushort, int, uint, long, ulong, char
2428 if (real_target_type == TypeManager.sbyte_type)
2429 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
2430 if (real_target_type == TypeManager.byte_type)
2431 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
2432 if (real_target_type == TypeManager.short_type)
2433 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
2434 if (real_target_type == TypeManager.ushort_type)
2435 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
2436 if (real_target_type == TypeManager.int32_type)
2437 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
2438 if (real_target_type == TypeManager.uint32_type)
2439 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
2440 if (real_target_type == TypeManager.int64_type)
2441 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
2442 if (real_target_type == TypeManager.uint64_type)
2443 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
2444 if (real_target_type == TypeManager.char_type)
2445 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
2446 } else if (expr_type == TypeManager.double_type){
2448 // From double to byte, byte, short,
2449 // ushort, int, uint, long, ulong,
2450 // char, float or decimal
2452 if (real_target_type == TypeManager.sbyte_type)
2453 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
2454 if (real_target_type == TypeManager.byte_type)
2455 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
2456 if (real_target_type == TypeManager.short_type)
2457 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
2458 if (real_target_type == TypeManager.ushort_type)
2459 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
2460 if (real_target_type == TypeManager.int32_type)
2461 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
2462 if (real_target_type == TypeManager.uint32_type)
2463 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
2464 if (real_target_type == TypeManager.int64_type)
2465 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
2466 if (real_target_type == TypeManager.uint64_type)
2467 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
2468 if (real_target_type == TypeManager.char_type)
2469 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
2470 if (real_target_type == TypeManager.float_type)
2471 return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
2474 // decimal is taken care of by the op_Explicit methods.
2480 /// Returns whether an explicit reference conversion can be performed
2481 /// from source_type to target_type
2483 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
2485 bool target_is_value_type = target_type.IsValueType;
2487 if (source_type == target_type)
2491 // From object to any reference type
2493 if (source_type == TypeManager.object_type && !target_is_value_type)
2497 // From any class S to any class-type T, provided S is a base class of T
2499 if (target_type.IsSubclassOf (source_type))
2503 // From any interface type S to any interface T provided S is not derived from T
2505 if (source_type.IsInterface && target_type.IsInterface){
2506 if (!target_type.IsSubclassOf (source_type))
2511 // From any class type S to any interface T, provided S is not sealed
2512 // and provided S does not implement T.
2514 if (target_type.IsInterface && !source_type.IsSealed &&
2515 !TypeManager.ImplementsInterface (source_type, target_type))
2519 // From any interface-type S to to any class type T, provided T is not
2520 // sealed, or provided T implements S.
2522 if (source_type.IsInterface &&
2523 (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
2527 // From an array type S with an element type Se to an array type T with an
2528 // element type Te provided all the following are true:
2529 // * S and T differe only in element type, in other words, S and T
2530 // have the same number of dimensions.
2531 // * Both Se and Te are reference types
2532 // * An explicit referenc conversions exist from Se to Te
2534 if (source_type.IsArray && target_type.IsArray) {
2535 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2537 Type source_element_type = source_type.GetElementType ();
2538 Type target_element_type = target_type.GetElementType ();
2540 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2541 if (ExplicitReferenceConversionExists (source_element_type,
2542 target_element_type))
2548 // From System.Array to any array-type
2549 if (source_type == TypeManager.array_type &&
2550 target_type.IsArray){
2555 // From System delegate to any delegate-type
2557 if (source_type == TypeManager.delegate_type &&
2558 target_type.IsSubclassOf (TypeManager.delegate_type))
2562 // From ICloneable to Array or Delegate types
2564 if (source_type == TypeManager.icloneable_type &&
2565 (target_type == TypeManager.array_type ||
2566 target_type == TypeManager.delegate_type))
2573 /// Implements Explicit Reference conversions
2575 static Expression ConvertReferenceExplicit (Expression source, Type target_type)
2577 Type source_type = source.Type;
2578 bool target_is_value_type = target_type.IsValueType;
2581 // From object to any reference type
2583 if (source_type == TypeManager.object_type && !target_is_value_type)
2584 return new ClassCast (source, target_type);
2588 // From any class S to any class-type T, provided S is a base class of T
2590 if (target_type.IsSubclassOf (source_type))
2591 return new ClassCast (source, target_type);
2594 // From any interface type S to any interface T provided S is not derived from T
2596 if (source_type.IsInterface && target_type.IsInterface){
2597 if (TypeManager.ImplementsInterface (source_type, target_type))
2600 return new ClassCast (source, target_type);
2604 // From any class type S to any interface T, provides S is not sealed
2605 // and provided S does not implement T.
2607 if (target_type.IsInterface && !source_type.IsSealed) {
2608 if (TypeManager.ImplementsInterface (source_type, target_type))
2611 return new ClassCast (source, target_type);
2616 // From any interface-type S to to any class type T, provided T is not
2617 // sealed, or provided T implements S.
2619 if (source_type.IsInterface) {
2620 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
2621 return new ClassCast (source, target_type);
2626 // From an array type S with an element type Se to an array type T with an
2627 // element type Te provided all the following are true:
2628 // * S and T differe only in element type, in other words, S and T
2629 // have the same number of dimensions.
2630 // * Both Se and Te are reference types
2631 // * An explicit referenc conversions exist from Se to Te
2633 if (source_type.IsArray && target_type.IsArray) {
2634 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
2636 Type source_element_type = source_type.GetElementType ();
2637 Type target_element_type = target_type.GetElementType ();
2639 if (!source_element_type.IsValueType && !target_element_type.IsValueType)
2640 if (ExplicitReferenceConversionExists (source_element_type,
2641 target_element_type))
2642 return new ClassCast (source, target_type);
2647 // From System.Array to any array-type
2648 if (source_type == TypeManager.array_type &&
2649 target_type.IsArray) {
2650 return new ClassCast (source, target_type);
2654 // From System delegate to any delegate-type
2656 if (source_type == TypeManager.delegate_type &&
2657 target_type.IsSubclassOf (TypeManager.delegate_type))
2658 return new ClassCast (source, target_type);
2661 // From ICloneable to Array or Delegate types
2663 if (source_type == TypeManager.icloneable_type &&
2664 (target_type == TypeManager.array_type ||
2665 target_type == TypeManager.delegate_type))
2666 return new ClassCast (source, target_type);
2672 /// Performs an explicit conversion of the expression 'expr' whose
2673 /// type is expr.Type to 'target_type'.
2675 static public Expression ConvertExplicit (EmitContext ec, Expression expr,
2676 Type target_type, bool runtimeconv, Location loc)
2678 Type expr_type = expr.Type;
2679 Expression ne = ConvertImplicitStandard (ec, expr, target_type, loc);
2684 ne = ConvertNumericExplicit (ec, expr, target_type, loc);
2689 // Unboxing conversion.
2691 if (expr_type == TypeManager.object_type && target_type.IsValueType)
2692 return new UnboxCast (expr, target_type);
2697 if (expr_type.IsSubclassOf (TypeManager.enum_type)) {
2701 // FIXME: Is there any reason we should have EnumConstant
2702 // dealt with here instead of just using always the
2703 // UnderlyingSystemType to wrap the type?
2705 if (expr is EnumConstant)
2706 e = ((EnumConstant) expr).Child;
2708 e = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
2711 Expression t = ConvertImplicit (ec, e, target_type, loc);
2715 t = ConvertNumericExplicit (ec, e, target_type, loc);
2719 t = NarrowingConversion (ec, e, target_type, loc);
2723 Error_CannotConvertType (loc, expr_type, target_type);
2727 ne = ConvertReferenceExplicit (expr, target_type);
2732 if (target_type.IsPointer){
2733 if (expr_type.IsPointer)
2734 return new EmptyCast (expr, target_type);
2736 if (expr_type == TypeManager.sbyte_type ||
2737 expr_type == TypeManager.byte_type ||
2738 expr_type == TypeManager.short_type ||
2739 expr_type == TypeManager.ushort_type ||
2740 expr_type == TypeManager.int32_type ||
2741 expr_type == TypeManager.uint32_type ||
2742 expr_type == TypeManager.uint64_type ||
2743 expr_type == TypeManager.int64_type)
2744 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
2746 if (expr_type.IsPointer){
2747 if (target_type == TypeManager.sbyte_type ||
2748 target_type == TypeManager.byte_type ||
2749 target_type == TypeManager.short_type ||
2750 target_type == TypeManager.ushort_type ||
2751 target_type == TypeManager.int32_type ||
2752 target_type == TypeManager.uint32_type ||
2753 target_type == TypeManager.uint64_type ||
2754 target_type == TypeManager.int64_type){
2755 Expression e = new EmptyCast (expr, TypeManager.uint32_type);
2758 ci = ConvertImplicitStandard (ec, e, target_type, loc);
2763 ce = ConvertNumericExplicit (ec, e, target_type, loc);
2767 // We should always be able to go from an uint32
2768 // implicitly or explicitly to the other integral
2771 throw new Exception ("Internal compiler error");
2776 ne = ExplicitUserConversion (ec, expr, target_type, loc);
2780 if (!(runtimeconv)) {
2781 ne = NarrowingConversion (ec, expr, target_type, loc);
2785 Error_CannotConvertType (loc, expr_type, target_type);
2791 /// Same as ConvertExplicit, only it doesn't include user defined conversions
2793 static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
2794 Type target_type, Location l)
2796 Expression ne = ConvertImplicitStandard (ec, expr, target_type, l);
2801 ne = ConvertNumericExplicit (ec, expr, target_type, l);
2805 ne = ConvertReferenceExplicit (expr, target_type);
2809 ne = NarrowingConversion (ec, expr, target_type, l);
2813 Error_CannotConvertType (l, expr.Type, target_type);
2817 static string ExprClassName (ExprClass c)
2820 case ExprClass.Invalid:
2822 case ExprClass.Value:
2824 case ExprClass.Variable:
2826 case ExprClass.Namespace:
2828 case ExprClass.Type:
2830 case ExprClass.MethodGroup:
2831 return "method group";
2832 case ExprClass.PropertyAccess:
2833 return "property access";
2834 case ExprClass.EventAccess:
2835 return "event access";
2836 case ExprClass.IndexerAccess:
2837 return "indexer access";
2838 case ExprClass.Nothing:
2841 throw new Exception ("Should not happen");
2845 /// Reports that we were expecting 'expr' to be of class 'expected'
2847 public void Error118 (string expected)
2849 string kind = "Unknown";
2851 kind = ExprClassName (eclass);
2853 Error (118, "Expression denotes a '" + kind +
2854 "' where a '" + expected + "' was expected");
2857 public void Error118 (ResolveFlags flags)
2859 ArrayList valid = new ArrayList (10);
2861 if ((flags & ResolveFlags.VariableOrValue) != 0) {
2862 valid.Add ("variable");
2863 valid.Add ("value");
2866 if ((flags & ResolveFlags.Type) != 0)
2869 if ((flags & ResolveFlags.MethodGroup) != 0)
2870 valid.Add ("method group");
2872 if ((flags & ResolveFlags.SimpleName) != 0)
2873 valid.Add ("simple name");
2875 if (valid.Count == 0)
2876 valid.Add ("unknown");
2878 StringBuilder sb = new StringBuilder ();
2879 for (int i = 0; i < valid.Count; i++) {
2882 else if (i == valid.Count)
2884 sb.Append (valid [i]);
2887 string kind = ExprClassName (eclass);
2889 Error (119, "Expression denotes a '" + kind + "' where " +
2890 "a '" + sb.ToString () + "' was expected");
2893 static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
2895 Report.Error (31, l, "Constant value '" + val + "' cannot be converted to " +
2896 TypeManager.MonoBASIC_Name (t));
2899 public static void UnsafeError (Location loc)
2901 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
2905 /// Converts the IntConstant, UIntConstant, LongConstant or
2906 /// ULongConstant into the integral target_type. Notice
2907 /// that we do not return an 'Expression' we do return
2908 /// a boxed integral type.
2910 /// FIXME: Since I added the new constants, we need to
2911 /// also support conversions from CharConstant, ByteConstant,
2912 /// SByteConstant, UShortConstant, ShortConstant
2914 /// This is used by the switch statement, so the domain
2915 /// of work is restricted to the literals above, and the
2916 /// targets are int32, uint32, char, byte, sbyte, ushort,
2917 /// short, uint64 and int64
2919 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
2923 if (c.Type == target_type)
2924 return ((Constant) c).GetValue ();
2927 // Make into one of the literals we handle, we dont really care
2928 // about this value as we will just return a few limited types
2930 if (c is EnumConstant)
2931 c = ((EnumConstant)c).WidenToCompilerConstant ();
2933 if (c is IntConstant){
2934 int v = ((IntConstant) c).Value;
2936 if (target_type == TypeManager.uint32_type){
2939 } else if (target_type == TypeManager.char_type){
2940 if (v >= Char.MinValue && v <= Char.MaxValue)
2942 } else if (target_type == TypeManager.byte_type){
2943 if (v >= Byte.MinValue && v <= Byte.MaxValue)
2945 } else if (target_type == TypeManager.sbyte_type){
2946 if (v >= SByte.MinValue && v <= SByte.MaxValue)
2948 } else if (target_type == TypeManager.short_type){
2949 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
2951 } else if (target_type == TypeManager.ushort_type){
2952 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
2954 } else if (target_type == TypeManager.int64_type)
2956 else if (target_type == TypeManager.uint64_type){
2962 } else if (c is UIntConstant){
2963 uint v = ((UIntConstant) c).Value;
2965 if (target_type == TypeManager.int32_type){
2966 if (v <= Int32.MaxValue)
2968 } else if (target_type == TypeManager.char_type){
2969 if (v >= Char.MinValue && v <= Char.MaxValue)
2971 } else if (target_type == TypeManager.byte_type){
2972 if (v <= Byte.MaxValue)
2974 } else if (target_type == TypeManager.sbyte_type){
2975 if (v <= SByte.MaxValue)
2977 } else if (target_type == TypeManager.short_type){
2978 if (v <= UInt16.MaxValue)
2980 } else if (target_type == TypeManager.ushort_type){
2981 if (v <= UInt16.MaxValue)
2983 } else if (target_type == TypeManager.int64_type)
2985 else if (target_type == TypeManager.uint64_type)
2988 } else if (c is LongConstant){
2989 long v = ((LongConstant) c).Value;
2991 if (target_type == TypeManager.int32_type){
2992 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
2994 } else if (target_type == TypeManager.uint32_type){
2995 if (v >= 0 && v <= UInt32.MaxValue)
2997 } else if (target_type == TypeManager.char_type){
2998 if (v >= Char.MinValue && v <= Char.MaxValue)
3000 } else if (target_type == TypeManager.byte_type){
3001 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3003 } else if (target_type == TypeManager.sbyte_type){
3004 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3006 } else if (target_type == TypeManager.short_type){
3007 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
3009 } else if (target_type == TypeManager.ushort_type){
3010 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
3012 } else if (target_type == TypeManager.uint64_type){
3017 } else if (c is ULongConstant){
3018 ulong v = ((ULongConstant) c).Value;
3020 if (target_type == TypeManager.int32_type){
3021 if (v <= Int32.MaxValue)
3023 } else if (target_type == TypeManager.uint32_type){
3024 if (v <= UInt32.MaxValue)
3026 } else if (target_type == TypeManager.char_type){
3027 if (v >= Char.MinValue && v <= Char.MaxValue)
3029 } else if (target_type == TypeManager.byte_type){
3030 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3032 } else if (target_type == TypeManager.sbyte_type){
3033 if (v <= (int) SByte.MaxValue)
3035 } else if (target_type == TypeManager.short_type){
3036 if (v <= UInt16.MaxValue)
3038 } else if (target_type == TypeManager.ushort_type){
3039 if (v <= UInt16.MaxValue)
3041 } else if (target_type == TypeManager.int64_type){
3042 if (v <= Int64.MaxValue)
3046 } else if (c is ByteConstant){
3047 byte v = ((ByteConstant) c).Value;
3049 if (target_type == TypeManager.int32_type)
3051 else if (target_type == TypeManager.uint32_type)
3053 else if (target_type == TypeManager.char_type)
3055 else if (target_type == TypeManager.sbyte_type){
3056 if (v <= SByte.MaxValue)
3058 } else if (target_type == TypeManager.short_type)
3060 else if (target_type == TypeManager.ushort_type)
3062 else if (target_type == TypeManager.int64_type)
3064 else if (target_type == TypeManager.uint64_type)
3067 } else if (c is SByteConstant){
3068 sbyte v = ((SByteConstant) c).Value;
3070 if (target_type == TypeManager.int32_type)
3072 else if (target_type == TypeManager.uint32_type){
3075 } else if (target_type == TypeManager.char_type){
3078 } else if (target_type == TypeManager.byte_type){
3081 } else if (target_type == TypeManager.short_type)
3083 else if (target_type == TypeManager.ushort_type){
3086 } else if (target_type == TypeManager.int64_type)
3088 else if (target_type == TypeManager.uint64_type){
3093 } else if (c is ShortConstant){
3094 short v = ((ShortConstant) c).Value;
3096 if (target_type == TypeManager.int32_type){
3098 } else if (target_type == TypeManager.uint32_type){
3101 } else if (target_type == TypeManager.char_type){
3104 } else if (target_type == TypeManager.byte_type){
3105 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3107 } else if (target_type == TypeManager.sbyte_type){
3108 if (v >= SByte.MinValue && v <= SByte.MaxValue)
3110 } else if (target_type == TypeManager.ushort_type){
3113 } else if (target_type == TypeManager.int64_type)
3115 else if (target_type == TypeManager.uint64_type)
3119 } else if (c is UShortConstant){
3120 ushort v = ((UShortConstant) c).Value;
3122 if (target_type == TypeManager.int32_type)
3124 else if (target_type == TypeManager.uint32_type)
3126 else if (target_type == TypeManager.char_type){
3127 if (v >= Char.MinValue && v <= Char.MaxValue)
3129 } else if (target_type == TypeManager.byte_type){
3130 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3132 } else if (target_type == TypeManager.sbyte_type){
3133 if (v <= SByte.MaxValue)
3135 } else if (target_type == TypeManager.short_type){
3136 if (v <= Int16.MaxValue)
3138 } else if (target_type == TypeManager.int64_type)
3140 else if (target_type == TypeManager.uint64_type)
3144 } else if (c is CharConstant){
3145 char v = ((CharConstant) c).Value;
3147 if (target_type == TypeManager.int32_type)
3149 else if (target_type == TypeManager.uint32_type)
3151 else if (target_type == TypeManager.byte_type){
3152 if (v >= Byte.MinValue && v <= Byte.MaxValue)
3154 } else if (target_type == TypeManager.sbyte_type){
3155 if (v <= SByte.MaxValue)
3157 } else if (target_type == TypeManager.short_type){
3158 if (v <= Int16.MaxValue)
3160 } else if (target_type == TypeManager.ushort_type)
3162 else if (target_type == TypeManager.int64_type)
3164 else if (target_type == TypeManager.uint64_type)
3169 Error_ConstantValueCannotBeConverted (loc, s, target_type);
3174 // Load the object from the pointer.
3176 public static void LoadFromPtr (ILGenerator ig, Type t)
3178 if (t == TypeManager.int32_type)
3179 ig.Emit (OpCodes.Ldind_I4);
3180 else if (t == TypeManager.uint32_type)
3181 ig.Emit (OpCodes.Ldind_U4);
3182 else if (t == TypeManager.short_type)
3183 ig.Emit (OpCodes.Ldind_I2);
3184 else if (t == TypeManager.ushort_type)
3185 ig.Emit (OpCodes.Ldind_U2);
3186 else if (t == TypeManager.char_type)
3187 ig.Emit (OpCodes.Ldind_U2);
3188 else if (t == TypeManager.byte_type)
3189 ig.Emit (OpCodes.Ldind_U1);
3190 else if (t == TypeManager.sbyte_type)
3191 ig.Emit (OpCodes.Ldind_I1);
3192 else if (t == TypeManager.uint64_type)
3193 ig.Emit (OpCodes.Ldind_I8);
3194 else if (t == TypeManager.int64_type)
3195 ig.Emit (OpCodes.Ldind_I8);
3196 else if (t == TypeManager.float_type)
3197 ig.Emit (OpCodes.Ldind_R4);
3198 else if (t == TypeManager.double_type)
3199 ig.Emit (OpCodes.Ldind_R8);
3200 else if (t == TypeManager.bool_type)
3201 ig.Emit (OpCodes.Ldind_I1);
3202 else if (t == TypeManager.intptr_type)
3203 ig.Emit (OpCodes.Ldind_I);
3204 else if (TypeManager.IsEnumType (t)) {
3205 if (t == TypeManager.enum_type)
3206 ig.Emit (OpCodes.Ldind_Ref);
3208 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
3209 } else if (t.IsValueType)
3210 ig.Emit (OpCodes.Ldobj, t);
3212 ig.Emit (OpCodes.Ldind_Ref);
3216 // The stack contains the pointer and the value of type 'type'
3218 public static void StoreFromPtr (ILGenerator ig, Type type)
3220 if (TypeManager.IsEnumType (type))
3221 type = TypeManager.EnumToUnderlying (type);
3222 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
3223 ig.Emit (OpCodes.Stind_I4);
3224 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
3225 ig.Emit (OpCodes.Stind_I8);
3226 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
3227 type == TypeManager.ushort_type)
3228 ig.Emit (OpCodes.Stind_I2);
3229 else if (type == TypeManager.float_type)
3230 ig.Emit (OpCodes.Stind_R4);
3231 else if (type == TypeManager.double_type)
3232 ig.Emit (OpCodes.Stind_R8);
3233 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
3234 type == TypeManager.bool_type)
3235 ig.Emit (OpCodes.Stind_I1);
3236 else if (type == TypeManager.intptr_type)
3237 ig.Emit (OpCodes.Stind_I);
3238 else if (type.IsValueType)
3239 ig.Emit (OpCodes.Stobj, type);
3241 ig.Emit (OpCodes.Stind_Ref);
3245 // Returns the size of type 't' if known, otherwise, 0
3247 public static int GetTypeSize (Type t)
3249 t = TypeManager.TypeToCoreType (t);
3250 if (t == TypeManager.int32_type ||
3251 t == TypeManager.uint32_type ||
3252 t == TypeManager.float_type)
3254 else if (t == TypeManager.int64_type ||
3255 t == TypeManager.uint64_type ||
3256 t == TypeManager.double_type)
3258 else if (t == TypeManager.byte_type ||
3259 t == TypeManager.sbyte_type ||
3260 t == TypeManager.bool_type)
3262 else if (t == TypeManager.short_type ||
3263 t == TypeManager.char_type ||
3264 t == TypeManager.ushort_type)
3266 else if (t == TypeManager.decimal_type)
3273 // Default implementation of IAssignMethod.CacheTemporaries
3275 public void CacheTemporaries (EmitContext ec)
3279 static void Error_NegativeArrayIndex (Location loc)
3281 Report.Error (284, loc, "Can not create array with a negative size");
3285 // Converts 'source' to an int, uint, long or ulong.
3287 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
3291 bool old_checked = ec.CheckState;
3292 ec.CheckState = true;
3294 target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
3295 if (target == null){
3296 target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
3297 if (target == null){
3298 target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
3299 if (target == null){
3300 target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
3302 Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
3306 ec.CheckState = old_checked;
3309 // Only positive constants are allowed at compile time
3311 if (target is Constant){
3312 if (target is IntConstant){
3313 if (((IntConstant) target).Value < 0){
3314 Error_NegativeArrayIndex (loc);
3319 if (target is LongConstant){
3320 if (((LongConstant) target).Value < 0){
3321 Error_NegativeArrayIndex (loc);
3334 /// This is just a base class for expressions that can
3335 /// appear on statements (invocations, object creation,
3336 /// assignments, post/pre increment and decrement). The idea
3337 /// being that they would support an extra Emition interface that
3338 /// does not leave a result on the stack.
3340 public abstract class ExpressionStatement : Expression {
3343 /// Requests the expression to be emitted in a 'statement'
3344 /// context. This means that no new value is left on the
3345 /// stack after invoking this method (constrasted with
3346 /// Emit that will always leave a value on the stack).
3348 public abstract void EmitStatement (EmitContext ec);
3352 /// This kind of cast is used to encapsulate the child
3353 /// whose type is child.Type into an expression that is
3354 /// reported to return "return_type". This is used to encapsulate
3355 /// expressions which have compatible types, but need to be dealt
3356 /// at higher levels with.
3358 /// For example, a "byte" expression could be encapsulated in one
3359 /// of these as an "unsigned int". The type for the expression
3360 /// would be "unsigned int".
3363 public class EmptyCast : Expression {
3364 protected Expression child;
3366 public EmptyCast (Expression child, Type return_type)
3368 eclass = child.eclass;
3373 public override Expression DoResolve (EmitContext ec)
3375 // This should never be invoked, we are born in fully
3376 // initialized state.
3381 public override void Emit (EmitContext ec)
3388 /// This class is used to wrap literals which belong inside Enums
3390 public class EnumConstant : Constant {
3391 public Constant Child;
3393 public EnumConstant (Constant child, Type enum_type)
3395 eclass = child.eclass;
3400 public override Expression DoResolve (EmitContext ec)
3402 // This should never be invoked, we are born in fully
3403 // initialized state.
3408 public override void Emit (EmitContext ec)
3413 public override object GetValue ()
3415 return Child.GetValue ();
3419 // Converts from one of the valid underlying types for an enumeration
3420 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
3421 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
3423 public Constant WidenToCompilerConstant ()
3425 Type t = TypeManager.EnumToUnderlying (Child.Type);
3426 object v = ((Constant) Child).GetValue ();;
3428 if (t == TypeManager.int32_type)
3429 return new IntConstant ((int) v);
3430 if (t == TypeManager.uint32_type)
3431 return new UIntConstant ((uint) v);
3432 if (t == TypeManager.int64_type)
3433 return new LongConstant ((long) v);
3434 if (t == TypeManager.uint64_type)
3435 return new ULongConstant ((ulong) v);
3436 if (t == TypeManager.short_type)
3437 return new ShortConstant ((short) v);
3438 if (t == TypeManager.ushort_type)
3439 return new UShortConstant ((ushort) v);
3440 if (t == TypeManager.byte_type)
3441 return new ByteConstant ((byte) v);
3442 if (t == TypeManager.sbyte_type)
3443 return new SByteConstant ((sbyte) v);
3445 throw new Exception ("Invalid enumeration underlying type: " + t);
3449 // Extracts the value in the enumeration on its native representation
3451 public object GetPlainValue ()
3453 Type t = TypeManager.EnumToUnderlying (Child.Type);
3454 object v = ((Constant) Child).GetValue ();;
3456 if (t == TypeManager.int32_type)
3458 if (t == TypeManager.uint32_type)
3460 if (t == TypeManager.int64_type)
3462 if (t == TypeManager.uint64_type)
3464 if (t == TypeManager.short_type)
3466 if (t == TypeManager.ushort_type)
3468 if (t == TypeManager.byte_type)
3470 if (t == TypeManager.sbyte_type)
3476 public override string AsString ()
3478 return Child.AsString ();
3481 public override DoubleConstant ConvertToDouble ()
3483 return Child.ConvertToDouble ();
3486 public override FloatConstant ConvertToFloat ()
3488 return Child.ConvertToFloat ();
3491 public override ULongConstant ConvertToULong ()
3493 return Child.ConvertToULong ();
3496 public override LongConstant ConvertToLong ()
3498 return Child.ConvertToLong ();
3501 public override UIntConstant ConvertToUInt ()
3503 return Child.ConvertToUInt ();
3506 public override IntConstant ConvertToInt ()
3508 return Child.ConvertToInt ();
3513 /// This kind of cast is used to encapsulate Value Types in objects.
3515 /// The effect of it is to box the value type emitted by the previous
3518 public class BoxedCast : EmptyCast {
3520 public BoxedCast (Expression expr)
3521 : base (expr, TypeManager.object_type)
3525 public override Expression DoResolve (EmitContext ec)
3527 // This should never be invoked, we are born in fully
3528 // initialized state.
3533 public override void Emit (EmitContext ec)
3537 ec.ig.Emit (OpCodes.Box, child.Type);
3541 public class UnboxCast : EmptyCast {
3542 public UnboxCast (Expression expr, Type return_type)
3543 : base (expr, return_type)
3547 public override Expression DoResolve (EmitContext ec)
3549 // This should never be invoked, we are born in fully
3550 // initialized state.
3555 public override void Emit (EmitContext ec)
3558 ILGenerator ig = ec.ig;
3561 ig.Emit (OpCodes.Unbox, t);
3563 LoadFromPtr (ig, t);
3568 /// This is used to perform explicit numeric conversions.
3570 /// Explicit numeric conversions might trigger exceptions in a checked
3571 /// context, so they should generate the conv.ovf opcodes instead of
3574 public class ConvCast : EmptyCast {
3575 public enum Mode : byte {
3576 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
3578 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
3579 U2_I1, U2_U1, U2_I2, U2_CH,
3580 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
3581 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
3582 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
3583 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
3584 CH_I1, CH_U1, CH_I2,
3585 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
3586 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
3592 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
3593 : base (child, return_type)
3595 checked_state = ec.CheckState;
3599 public override Expression DoResolve (EmitContext ec)
3601 // This should never be invoked, we are born in fully
3602 // initialized state.
3607 public override void Emit (EmitContext ec)
3609 ILGenerator ig = ec.ig;
3615 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3616 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3617 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3618 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3619 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3621 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3622 case Mode.U1_CH: /* nothing */ break;
3624 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3625 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3626 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3627 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3628 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3629 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3631 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3632 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3633 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3634 case Mode.U2_CH: /* nothing */ break;
3636 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3637 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3638 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3639 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3640 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3641 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3642 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3644 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3645 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3646 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3647 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3648 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3649 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3651 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3652 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3653 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3654 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3655 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3656 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3657 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3658 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3660 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3661 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3662 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3663 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3664 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
3665 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
3666 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
3667 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
3669 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
3670 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
3671 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
3673 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3674 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3675 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3676 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3677 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3678 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3679 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3680 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3681 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3683 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
3684 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
3685 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
3686 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3687 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
3688 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
3689 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
3690 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
3691 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
3692 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3696 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
3697 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
3698 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
3699 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
3700 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
3702 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
3703 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
3705 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
3706 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
3707 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
3708 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
3709 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
3710 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
3712 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
3713 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
3714 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
3715 case Mode.U2_CH: /* nothing */ break;
3717 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
3718 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
3719 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
3720 case Mode.I4_U4: /* nothing */ break;
3721 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
3722 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
3723 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
3725 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
3726 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
3727 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
3728 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
3729 case Mode.U4_I4: /* nothing */ break;
3730 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
3732 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
3733 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
3734 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
3735 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
3736 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
3737 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
3738 case Mode.I8_U8: /* nothing */ break;
3739 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
3741 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
3742 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
3743 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
3744 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
3745 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
3746 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
3747 case Mode.U8_I8: /* nothing */ break;
3748 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
3750 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
3751 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
3752 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
3754 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
3755 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
3756 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
3757 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
3758 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
3759 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
3760 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
3761 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
3762 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
3764 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
3765 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
3766 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
3767 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
3768 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
3769 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
3770 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
3771 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
3772 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
3773 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
3779 public class OpcodeCast : EmptyCast {
3783 public OpcodeCast (Expression child, Type return_type, OpCode op)
3784 : base (child, return_type)
3788 second_valid = false;
3791 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
3792 : base (child, return_type)
3797 second_valid = true;
3800 public override Expression DoResolve (EmitContext ec)
3802 // This should never be invoked, we are born in fully
3803 // initialized state.
3808 public override void Emit (EmitContext ec)
3819 public class NumericToBoolCast : EmptyCast
3823 public NumericToBoolCast (Expression src, Type src_type)
3824 : base (src, TypeManager.bool_type)
3827 this.src_type = src_type;
3830 public override Expression DoResolve (EmitContext ec)
3835 public override void Emit (EmitContext ec)
3839 if (src_type == TypeManager.byte_type ||
3840 src_type == TypeManager.short_type ||
3841 src_type == TypeManager.int32_type) {
3843 ec.ig.Emit (OpCodes.Ldc_I4_0);
3844 ec.ig.Emit (OpCodes.Cgt_Un);
3848 if (src_type == TypeManager.int64_type) {
3849 ec.ig.Emit (OpCodes.Ldc_I8, (long) 0);
3850 ec.ig.Emit (OpCodes.Cgt_Un);
3854 if (src_type == TypeManager.float_type) {
3855 ec.ig.Emit (OpCodes.Ldc_R4, (float) 0);
3856 ec.ig.Emit (OpCodes.Ceq);
3857 ec.ig.Emit (OpCodes.Ldc_I4_0);
3858 ec.ig.Emit (OpCodes.Ceq);
3862 if (src_type == TypeManager.double_type) {
3863 ec.ig.Emit (OpCodes.Ldc_R8, (double) 0);
3864 ec.ig.Emit (OpCodes.Ceq);
3865 ec.ig.Emit (OpCodes.Ldc_I4_0);
3866 ec.ig.Emit (OpCodes.Ceq);
3872 public class BoolToNumericCast : EmptyCast
3878 public BoolToNumericCast (Expression src, Type target_type)
3879 : base (src, target_type)
3882 this.target_type = target_type;
3885 public override Expression DoResolve (EmitContext ec)
3890 public override void Emit (EmitContext ec)
3894 if (target_type == TypeManager.byte_type) {
3895 conv = OpCodes.Conv_U1;
3896 } else if (target_type == TypeManager.short_type) {
3897 conv = OpCodes.Conv_I2;
3898 } else if (target_type == TypeManager.int32_type) {
3899 conv = OpCodes.Conv_I4;
3900 } else if (target_type == TypeManager.int64_type) {
3901 conv = OpCodes.Conv_I8;
3902 } else if (target_type == TypeManager.float_type) {
3903 conv = OpCodes.Conv_R4;
3904 } else if (target_type == TypeManager.double_type) {
3905 conv = OpCodes.Conv_R8;
3908 ec.ig.Emit (OpCodes.Ldc_I4_0);
3909 ec.ig.Emit (OpCodes.Cgt_Un);
3910 ec.ig.Emit (OpCodes.Neg);
3917 /// This kind of cast is used to encapsulate a child and cast it
3918 /// to the class requested
3920 public class ClassCast : EmptyCast {
3921 public ClassCast (Expression child, Type return_type)
3922 : base (child, return_type)
3927 public override Expression DoResolve (EmitContext ec)
3929 // This should never be invoked, we are born in fully
3930 // initialized state.
3935 public override void Emit (EmitContext ec)
3939 ec.ig.Emit (OpCodes.Castclass, type);
3945 /// SimpleName expressions are initially formed of a single
3946 /// word and it only happens at the beginning of the expression.
3950 /// The expression will try to be bound to a Field, a Method
3951 /// group or a Property. If those fail we pass the name to our
3952 /// caller and the SimpleName is compounded to perform a type
3953 /// lookup. The idea behind this process is that we want to avoid
3954 /// creating a namespace map from the assemblies, as that requires
3955 /// the GetExportedTypes function to be called and a hashtable to
3956 /// be constructed which reduces startup time. If later we find
3957 /// that this is slower, we should create a 'NamespaceExpr' expression
3958 /// that fully participates in the resolution process.
3960 /// For example 'System.Console.WriteLine' is decomposed into
3961 /// MemberAccess (MemberAccess (SimpleName ("System"), "Console"), "WriteLine")
3963 /// The first SimpleName wont produce a match on its own, so it will
3965 /// MemberAccess (SimpleName ("System.Console"), "WriteLine").
3967 /// System.Console will produce a TypeExpr match.
3969 /// The downside of this is that we might be hitting 'LookupType' too many
3970 /// times with this scheme.
3972 public class SimpleName : Expression, ITypeExpression {
3973 public readonly string Name;
3975 public SimpleName (string name, Location l)
3981 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
3983 if (ec.IsFieldInitializer)
3986 "A field initializer cannot reference the non-static field, " +
3987 "method or property '"+name+"'");
3991 "An object reference is required " +
3992 "for the non-static field '"+name+"'");
3996 // Checks whether we are trying to access an instance
3997 // property, method or field from a static body.
3999 Expression MemberStaticCheck (EmitContext ec, Expression e)
4001 if (e is IMemberExpr){
4002 IMemberExpr member = (IMemberExpr) e;
4004 if (!member.IsStatic){
4005 Error_ObjectRefRequired (ec, loc, Name);
4013 public override Expression DoResolve (EmitContext ec)
4015 return SimpleNameResolve (ec, null, false);
4018 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
4020 return SimpleNameResolve (ec, right_side, false);
4024 public Expression DoResolveAllowStatic (EmitContext ec)
4026 return SimpleNameResolve (ec, null, true);
4029 public Expression DoResolveType (EmitContext ec)
4032 // Stage 3: Lookup symbol in the various namespaces.
4034 DeclSpace ds = ec.DeclSpace;
4038 if (ec.ResolvingTypeTree){
4039 int errors = Report.Errors;
4040 Type dt = ec.DeclSpace.FindType (loc, Name);
4041 if (Report.Errors != errors)
4045 return new TypeExpr (dt, loc);
4048 if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
4049 return new TypeExpr (t, loc);
4053 // Stage 2 part b: Lookup up if we are an alias to a type
4056 // Since we are cheating: we only do the Alias lookup for
4057 // namespaces if the name does not include any dots in it
4060 alias_value = ec.DeclSpace.LookupAlias (Name);
4062 if (Name.IndexOf ('.') == -1 && alias_value != null) {
4063 if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
4064 return new TypeExpr (t, loc);
4066 // we have alias value, but it isn't Type, so try if it's namespace
4067 return new SimpleName (alias_value, loc);
4070 // No match, maybe our parent can compose us
4071 // into something meaningful.
4076 /// 7.5.2: Simple Names.
4078 /// Local Variables and Parameters are handled at
4079 /// parse time, so they never occur as SimpleNames.
4081 /// The 'allow_static' flag is used by MemberAccess only
4082 /// and it is used to inform us that it is ok for us to
4083 /// avoid the static check, because MemberAccess might end
4084 /// up resolving the Name as a Type name and the access as
4085 /// a static type access.
4087 /// ie: Type Type; .... { Type.GetType (""); }
4089 /// Type is both an instance variable and a Type; Type.GetType
4090 /// is the static method not an instance method of type.
4092 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
4094 Expression e = null;
4097 // Stage 1: Performed by the parser (binding to locals or parameters).
4099 Block current_block = ec.CurrentBlock;
4100 if (ec.InvokingOwnOverload == false && current_block != null && current_block.IsVariableDefined (Name)){
4101 LocalVariableReference var;
4103 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
4105 if (right_side != null)
4106 return var.ResolveLValue (ec, right_side);
4108 return var.Resolve (ec);
4111 if (current_block != null){
4113 Parameter par = null;
4114 Parameters pars = current_block.Parameters;
4116 par = pars.GetParameterByName (Name, out idx);
4119 ParameterReference param;
4121 param = new ParameterReference (pars, idx, Name, loc);
4123 if (right_side != null)
4124 return param.ResolveLValue (ec, right_side);
4126 return param.Resolve (ec);
4131 // Stage 2: Lookup members
4135 // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
4136 // Hence we have two different cases
4139 DeclSpace lookup_ds = ec.DeclSpace;
4141 if (lookup_ds.TypeBuilder == null)
4144 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
4149 // Classes/structs keep looking, enums break
4151 if (lookup_ds is TypeContainer)
4152 lookup_ds = ((TypeContainer) lookup_ds).Parent;
4155 } while (lookup_ds != null);
4157 if (e == null && ec.ContainerType != null)
4158 e = MemberLookup (ec, ec.ContainerType, Name, loc);
4160 // #52067 - Start - Trying to solve
4164 ArrayList lookups = new ArrayList();
4165 ArrayList typelookups = new ArrayList();
4167 int split = Name.LastIndexOf('.');
4169 String nameSpacePart = Name.Substring(0, split);
4170 String memberNamePart = Name.Substring(split + 1);
4171 foreach(Type type in TypeManager.GetPertinentStandardModules(nameSpacePart)) {
4172 e = MemberLookup(ec, type, memberNamePart, loc);
4175 typelookups.Add(type);
4180 string[] NamespacesInScope = RootContext.SourceBeingCompiled.GetNamespacesInScope(ec.DeclSpace.Namespace.Name);
4181 foreach(Type type in TypeManager.GetPertinentStandardModules(NamespacesInScope)) {
4182 e = MemberLookup(ec, type, Name, loc);
4185 typelookups.Add(type);
4188 if (lookups.Count == 1) {
4189 e = (Expression)lookups[0];
4191 if (lookups.Count > 1) {
4192 StringBuilder sb = new StringBuilder();
4193 foreach(Type type in typelookups)
4194 sb.Append("'" + type.FullName + "'");
4195 Error (-1, "The name '" + Name + "' can be resolved to a member of more than one standard module: " + sb.ToString() + ". Please fully qualify it.");
4204 return DoResolveType (ec);
4209 if (e is IMemberExpr) {
4210 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
4214 IMemberExpr me = e as IMemberExpr;
4218 // This fails if ResolveMemberAccess() was unable to decide whether
4219 // it's a field or a type of the same name.
4220 if (!me.IsStatic && (me.InstanceExpression == null))
4223 /* FIXME If this is not commented out, it seems that it's not possible to reach class members in mBas.
4224 Maybe a grammar-related problem?
4227 TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
4228 Error (38, "Cannot access nonstatic member '" + me.Name + "' of " +
4229 "outer type '" + me.DeclaringType + "' via nested type '" +
4230 me.InstanceExpression.Type + "'");
4234 if (right_side != null)
4235 e = e.DoResolveLValue (ec, right_side);
4237 e = e.DoResolve (ec);
4242 if (ec.IsStatic || ec.IsFieldInitializer){
4246 return MemberStaticCheck (ec, e);
4251 public override void Emit (EmitContext ec)
4254 // If this is ever reached, then we failed to
4255 // find the name as a namespace
4258 Error (30451, "The name '" + Name +
4259 "' does not exist in the class '" +
4260 ec.DeclSpace.Name + "'");
4263 public override string ToString ()
4270 /// Fully resolved expression that evaluates to a type
4272 public class TypeExpr : Expression, ITypeExpression {
4273 public TypeExpr (Type t, Location l)
4276 eclass = ExprClass.Type;
4280 public virtual Expression DoResolveType (EmitContext ec)
4285 override public Expression DoResolve (EmitContext ec)
4290 override public void Emit (EmitContext ec)
4292 throw new Exception ("Should never be called");
4295 public override string ToString ()
4297 return Type.ToString ();
4302 /// Used to create types from a fully qualified name. These are just used
4303 /// by the parser to setup the core types. A TypeLookupExpression is always
4304 /// classified as a type.
4306 public class TypeLookupExpression : TypeExpr {
4309 public TypeLookupExpression (string name) : base (null, Location.Null)
4314 public override Expression DoResolveType (EmitContext ec)
4317 type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
4321 public override Expression DoResolve (EmitContext ec)
4323 return DoResolveType (ec);
4326 public override void Emit (EmitContext ec)
4328 throw new Exception ("Should never be called");
4331 public override string ToString ()
4338 /// MethodGroup Expression.
4340 /// This is a fully resolved expression that evaluates to a type
4342 public class MethodGroupExpr : Expression, IMemberExpr {
4343 public MethodBase [] Methods;
4344 Expression instance_expression = null;
4345 bool is_explicit_impl = false;
4347 public MethodGroupExpr (MemberInfo [] mi, Location l)
4349 Methods = new MethodBase [mi.Length];
4350 mi.CopyTo (Methods, 0);
4351 eclass = ExprClass.MethodGroup;
4352 type = TypeManager.object_type;
4356 public MethodGroupExpr (ArrayList list, Location l)
4358 Methods = new MethodBase [list.Count];
4361 list.CopyTo (Methods, 0);
4363 foreach (MemberInfo m in list){
4364 if (!(m is MethodBase)){
4365 Console.WriteLine ("Name " + m.Name);
4366 Console.WriteLine ("Found a: " + m.GetType ().FullName);
4372 eclass = ExprClass.MethodGroup;
4373 type = TypeManager.object_type;
4376 public Type DeclaringType {
4378 return Methods [0].DeclaringType;
4383 // 'A method group may have associated an instance expression'
4385 public Expression InstanceExpression {
4387 return instance_expression;
4391 instance_expression = value;
4395 public bool IsExplicitImpl {
4397 return is_explicit_impl;
4401 is_explicit_impl = value;
4405 public string Name {
4407 return Methods [0].Name;
4411 public bool IsInstance {
4413 foreach (MethodBase mb in Methods)
4421 public bool IsStatic {
4423 foreach (MethodBase mb in Methods)
4431 override public Expression DoResolve (EmitContext ec)
4433 if (instance_expression != null) {
4434 instance_expression = instance_expression.DoResolve (ec);
4435 if (instance_expression == null)
4442 public void ReportUsageError ()
4444 Report.Error (654, loc, "Method '" + Methods [0].DeclaringType + "." +
4445 Methods [0].Name + "()' is referenced without parentheses");
4448 override public void Emit (EmitContext ec)
4450 ReportUsageError ();
4453 bool RemoveMethods (bool keep_static)
4455 ArrayList smethods = new ArrayList ();
4457 foreach (MethodBase mb in Methods){
4458 if (mb.IsStatic == keep_static)
4462 if (smethods.Count == 0)
4465 Methods = new MethodBase [smethods.Count];
4466 smethods.CopyTo (Methods, 0);
4472 /// Removes any instance methods from the MethodGroup, returns
4473 /// false if the resulting set is empty.
4475 public bool RemoveInstanceMethods ()
4477 return RemoveMethods (true);
4481 /// Removes any static methods from the MethodGroup, returns
4482 /// false if the resulting set is empty.
4484 public bool RemoveStaticMethods ()
4486 return RemoveMethods (false);
4491 /// Fully resolved expression that evaluates to a Field
4493 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
4494 public readonly FieldInfo FieldInfo;
4495 Expression instance_expr;
4497 public FieldExpr (FieldInfo fi, Location l)
4500 eclass = ExprClass.Variable;
4501 type = fi.FieldType;
4505 public string Name {
4507 return FieldInfo.Name;
4511 public bool IsInstance {
4513 return !FieldInfo.IsStatic;
4517 public bool IsStatic {
4519 return FieldInfo.IsStatic;
4523 public Type DeclaringType {
4525 return FieldInfo.DeclaringType;
4529 public Expression InstanceExpression {
4531 return instance_expr;
4535 instance_expr = value;
4539 override public Expression DoResolve (EmitContext ec)
4541 if (!FieldInfo.IsStatic){
4542 if (instance_expr == null){
4543 throw new Exception ("non-static FieldExpr without instance var\n" +
4544 "You have to assign the Instance variable\n" +
4545 "Of the FieldExpr to set this\n");
4548 // Resolve the field's instance expression while flow analysis is turned
4549 // off: when accessing a field "a.b", we must check whether the field
4550 // "a.b" is initialized, not whether the whole struct "a" is initialized.
4551 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
4552 ResolveFlags.DisableFlowAnalysis);
4553 if (instance_expr == null)
4557 // If the instance expression is a local variable or parameter.
4558 IVariable var = instance_expr as IVariable;
4559 if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
4565 void Report_AssignToReadonly (bool is_instance)
4570 msg = "Readonly field can not be assigned outside " +
4571 "of constructor or variable initializer";
4573 msg = "A static readonly field can only be assigned in " +
4574 "a static constructor";
4576 Report.Error (is_instance ? 191 : 198, loc, msg);
4579 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4581 IVariable var = instance_expr as IVariable;
4583 var.SetFieldAssigned (ec, FieldInfo.Name);
4585 Expression e = DoResolve (ec);
4590 if (!FieldInfo.IsInitOnly)
4594 // InitOnly fields can only be assigned in constructors
4597 if (ec.IsConstructor)
4600 Report_AssignToReadonly (true);
4605 override public void Emit (EmitContext ec)
4607 ILGenerator ig = ec.ig;
4608 bool is_volatile = false;
4610 if (FieldInfo is FieldBuilder){
4611 FieldBase f = TypeManager.GetField (FieldInfo);
4613 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4616 f.status |= Field.Status.USED;
4619 if (FieldInfo.IsStatic){
4621 ig.Emit (OpCodes.Volatile);
4623 ig.Emit (OpCodes.Ldsfld, FieldInfo);
4625 if (instance_expr.Type.IsValueType){
4627 LocalTemporary tempo = null;
4629 if (!(instance_expr is IMemoryLocation)){
4630 tempo = new LocalTemporary (
4631 ec, instance_expr.Type);
4633 InstanceExpression.Emit (ec);
4637 ml = (IMemoryLocation) instance_expr;
4639 ml.AddressOf (ec, AddressOp.Load);
4641 instance_expr.Emit (ec);
4644 ig.Emit (OpCodes.Volatile);
4646 ig.Emit (OpCodes.Ldfld, FieldInfo);
4650 public void EmitAssign (EmitContext ec, Expression source)
4652 FieldAttributes fa = FieldInfo.Attributes;
4653 bool is_static = (fa & FieldAttributes.Static) != 0;
4654 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
4655 ILGenerator ig = ec.ig;
4657 if (is_readonly && !ec.IsConstructor){
4658 Report_AssignToReadonly (!is_static);
4663 Expression instance = instance_expr;
4665 if (instance.Type.IsValueType){
4666 if (instance is IMemoryLocation){
4667 IMemoryLocation ml = (IMemoryLocation) instance;
4669 ml.AddressOf (ec, AddressOp.Store);
4671 throw new Exception ("The " + instance + " of type " +
4673 " represents a ValueType and does " +
4674 "not implement IMemoryLocation");
4680 if (FieldInfo is FieldBuilder){
4681 FieldBase f = TypeManager.GetField (FieldInfo);
4683 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4684 ig.Emit (OpCodes.Volatile);
4688 ig.Emit (OpCodes.Stsfld, FieldInfo);
4690 ig.Emit (OpCodes.Stfld, FieldInfo);
4692 if (FieldInfo is FieldBuilder){
4693 FieldBase f = TypeManager.GetField (FieldInfo);
4695 f.status |= Field.Status.ASSIGNED;
4699 public void AddressOf (EmitContext ec, AddressOp mode)
4701 ILGenerator ig = ec.ig;
4703 if (FieldInfo is FieldBuilder){
4704 FieldBase f = TypeManager.GetField (FieldInfo);
4705 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
4706 ig.Emit (OpCodes.Volatile);
4709 if (FieldInfo is FieldBuilder){
4710 FieldBase f = TypeManager.GetField (FieldInfo);
4712 if ((mode & AddressOp.Store) != 0)
4713 f.status |= Field.Status.ASSIGNED;
4714 if ((mode & AddressOp.Load) != 0)
4715 f.status |= Field.Status.USED;
4719 // Handle initonly fields specially: make a copy and then
4720 // get the address of the copy.
4722 if (FieldInfo.IsInitOnly && !ec.IsConstructor){
4726 local = ig.DeclareLocal (type);
4727 ig.Emit (OpCodes.Stloc, local);
4728 ig.Emit (OpCodes.Ldloca, local);
4732 if (FieldInfo.IsStatic)
4733 ig.Emit (OpCodes.Ldsflda, FieldInfo);
4735 if (instance_expr is IMemoryLocation)
4736 ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
4738 instance_expr.Emit (ec);
4739 ig.Emit (OpCodes.Ldflda, FieldInfo);
4745 /// Expression that evaluates to a Property. The Assign class
4746 /// might set the 'Value' expression if we are in an assignment.
4748 /// This is not an LValue because we need to re-write the expression, we
4749 /// can not take data from the stack and store it.
4751 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
4752 public readonly PropertyInfo PropertyInfo;
4754 MethodInfo getter, setter;
4756 public ArrayList PropertyArgs;
4758 Expression instance_expr;
4760 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
4763 eclass = ExprClass.PropertyAccess;
4764 PropertyArgs = new ArrayList();
4768 type = TypeManager.TypeToCoreType (pi.PropertyType);
4770 ResolveAccessors (ec);
4773 public string Name {
4775 return PropertyInfo.Name;
4779 public bool IsInstance {
4785 public bool IsStatic {
4791 public Type DeclaringType {
4793 return PropertyInfo.DeclaringType;
4798 // The instance expression associated with this expression
4800 public Expression InstanceExpression {
4802 instance_expr = value;
4806 return instance_expr;
4810 public bool VerifyAssignable ()
4812 if (!PropertyInfo.CanWrite){
4813 Report.Error (200, loc,
4814 "The property '" + PropertyInfo.Name +
4815 "' can not be assigned to, as it has not set accessor");
4822 void ResolveAccessors (EmitContext ec)
4824 BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
4825 MemberInfo [] group;
4827 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4828 MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
4831 // The first method is the closest to us
4833 if (group != null && group.Length > 0){
4834 getter = (MethodInfo) group [0];
4836 if (getter.IsStatic)
4841 // The first method is the closest to us
4843 group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
4844 MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
4845 if (group != null && group.Length > 0){
4846 setter = (MethodInfo) group [0];
4847 if (setter.IsStatic)
4852 override public Expression DoResolve (EmitContext ec)
4854 if (getter == null){
4855 Report.Error (30524, loc,
4856 "The property '" + PropertyInfo.Name +
4857 "' can not be used in " +
4858 "this context because it lacks a get accessor");
4862 if ((instance_expr == null) && ec.IsStatic && !is_static) {
4863 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
4867 if (instance_expr != null) {
4868 instance_expr = instance_expr.DoResolve (ec);
4869 if (instance_expr == null)
4876 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
4878 if (setter == null){
4879 Report.Error (30526, loc,
4880 "The property '" + PropertyInfo.Name +
4881 "' can not be used in " +
4882 "this context because it lacks a set accessor");
4886 if (instance_expr != null) {
4887 instance_expr = instance_expr.DoResolve (ec);
4888 if (instance_expr == null)
4895 override public void Emit (EmitContext ec)
4898 // Special case: length of single dimension array property is turned into ldlen
4900 if ((getter == TypeManager.system_int_array_get_length) ||
4901 (getter == TypeManager.int_array_get_length)){
4902 Type iet = instance_expr.Type;
4905 // System.Array.Length can be called, but the Type does not
4906 // support invoking GetArrayRank, so test for that case first
4908 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)){
4909 instance_expr.Emit (ec);
4910 ec.ig.Emit (OpCodes.Ldlen);
4914 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, PropertyArgs, loc);
4918 // Implements the IAssignMethod interface for assignments
4920 public void EmitAssign (EmitContext ec, Expression source)
4922 Argument arg = new Argument (source, Argument.AType.Expression);
4923 ArrayList args = new ArrayList ();
4926 Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, PropertyArgs,loc);
4929 override public void EmitStatement (EmitContext ec)
4932 ec.ig.Emit (OpCodes.Pop);
4937 /// Fully resolved expression that evaluates to an Event
4939 public class EventExpr : Expression, IMemberExpr {
4940 public readonly EventInfo EventInfo;
4941 public Expression instance_expr;
4944 MethodInfo add_accessor, remove_accessor;
4946 public EventExpr (EventInfo ei, Location loc)
4950 eclass = ExprClass.EventAccess;
4952 add_accessor = TypeManager.GetAddMethod (ei);
4953 remove_accessor = TypeManager.GetRemoveMethod (ei);
4955 if (add_accessor.IsStatic || remove_accessor.IsStatic)
4958 if (EventInfo is MyEventBuilder)
4959 type = ((MyEventBuilder) EventInfo).EventType;
4961 type = EventInfo.EventHandlerType;
4964 public string Name {
4966 return EventInfo.Name;
4970 public bool IsInstance {
4976 public bool IsStatic {
4982 public Type DeclaringType {
4984 return EventInfo.DeclaringType;
4988 public Expression InstanceExpression {
4990 return instance_expr;
4994 instance_expr = value;
4998 Expression field_expr = null;
5000 public override Expression DoResolve (EmitContext ec)
5002 if (instance_expr != null) {
5003 instance_expr = instance_expr.DoResolve (ec);
5004 if (instance_expr == null)
5008 if (this.DeclaringType == ec.ContainerType) {
5009 MemberInfo mi = GetFieldFromEvent (this);
5012 field_expr = ExprClassFromMemberInfo (ec, mi, loc);
5013 ((FieldExpr) field_expr).InstanceExpression = instance_expr;
5014 field_expr = field_expr.DoResolve (ec);
5015 if (field_expr == null)
5022 public override void Emit (EmitContext ec)
5024 if (field_expr != null)
5025 field_expr.Emit (ec);
5028 public void EmitAddOrRemove (EmitContext ec, Expression source)
5030 Expression handler = ((Binary) source).Right;
5032 Argument arg = new Argument (handler, Argument.AType.Expression);
5033 ArrayList args = new ArrayList ();
5037 if (((Binary) source).Oper == Binary.Operator.Addition)
5038 Invocation.EmitCall (
5039 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
5041 Invocation.EmitCall (
5042 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);